Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / scsi / megaraid / megaraid_sas_base.c
blobf4b0690450d2402dd8978aea51bed78d1aa48d96
1 /*
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
21 * Sreenivas Bagalkote
22 * Sumant Patro
23 * Bo Yang
24 * Adam Radford
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>
46 #include <linux/fs.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
61 * Number of sectors per IO command
62 * Will be set in megasas_init_mfi if user does not provide
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 "Maximum number of sectors per IO command");
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
86 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 "before resetting adapter. Default: 180");
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
95 int rdpq_enable = 1;
96 module_param(rdpq_enable, int, S_IRUGO);
97 MODULE_PARM_DESC(rdpq_enable, " Allocate reply queue in chunks for large queue depth enable/disable Default: disable(0)");
99 unsigned int dual_qdepth_disable;
100 module_param(dual_qdepth_disable, int, S_IRUGO);
101 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
104 module_param(scmd_timeout, int, S_IRUGO);
105 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107 MODULE_LICENSE("GPL");
108 MODULE_VERSION(MEGASAS_VERSION);
109 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
110 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
112 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
113 static int megasas_get_pd_list(struct megasas_instance *instance);
114 static int megasas_ld_list_query(struct megasas_instance *instance,
115 u8 query_type);
116 static int megasas_issue_init_mfi(struct megasas_instance *instance);
117 static int megasas_register_aen(struct megasas_instance *instance,
118 u32 seq_num, u32 class_locale_word);
119 static int
120 megasas_get_pd_info(struct megasas_instance *instance, u16 device_id);
122 * PCI ID table for all supported controllers
124 static struct pci_device_id megasas_pci_table[] = {
126 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
127 /* xscale IOP */
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
129 /* ppc IOP */
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
131 /* ppc IOP */
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
133 /* gen2*/
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
135 /* gen2*/
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
137 /* skinny*/
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
139 /* skinny*/
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
141 /* xscale IOP, vega */
142 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
143 /* xscale IOP */
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
145 /* Fusion */
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
147 /* Plasma */
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
149 /* Invader */
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
151 /* Fury */
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
153 /* Intruder */
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
155 /* Intruder 24 port*/
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
157 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
161 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
163 static int megasas_mgmt_majorno;
164 struct megasas_mgmt_info megasas_mgmt_info;
165 static struct fasync_struct *megasas_async_queue;
166 static DEFINE_MUTEX(megasas_async_queue_mutex);
168 static int megasas_poll_wait_aen;
169 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
170 static u32 support_poll_for_event;
171 u32 megasas_dbg_lvl;
172 static u32 support_device_change;
174 /* define lock for aen poll */
175 spinlock_t poll_aen_lock;
177 void
178 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
179 u8 alt_status);
180 static u32
181 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
182 static int
183 megasas_adp_reset_gen2(struct megasas_instance *instance,
184 struct megasas_register_set __iomem *reg_set);
185 static irqreturn_t megasas_isr(int irq, void *devp);
186 static u32
187 megasas_init_adapter_mfi(struct megasas_instance *instance);
189 megasas_build_and_issue_cmd(struct megasas_instance *instance,
190 struct scsi_cmnd *scmd);
191 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
192 void
193 megasas_release_fusion(struct megasas_instance *instance);
195 megasas_ioc_init_fusion(struct megasas_instance *instance);
196 void
197 megasas_free_cmds_fusion(struct megasas_instance *instance);
199 megasas_get_map_info(struct megasas_instance *instance);
201 megasas_sync_map_info(struct megasas_instance *instance);
203 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
204 int seconds);
205 void megasas_reset_reply_desc(struct megasas_instance *instance);
206 void megasas_fusion_ocr_wq(struct work_struct *work);
207 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
208 int initial);
209 int megasas_check_mpio_paths(struct megasas_instance *instance,
210 struct scsi_cmnd *scmd);
213 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
215 instance->instancet->fire_cmd(instance,
216 cmd->frame_phys_addr, 0, instance->reg_set);
217 return 0;
221 * megasas_get_cmd - Get a command from the free pool
222 * @instance: Adapter soft state
224 * Returns a free command from the pool
226 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
227 *instance)
229 unsigned long flags;
230 struct megasas_cmd *cmd = NULL;
232 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
234 if (!list_empty(&instance->cmd_pool)) {
235 cmd = list_entry((&instance->cmd_pool)->next,
236 struct megasas_cmd, list);
237 list_del_init(&cmd->list);
238 } else {
239 dev_err(&instance->pdev->dev, "Command pool empty!\n");
242 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
243 return cmd;
247 * megasas_return_cmd - Return a cmd to free command pool
248 * @instance: Adapter soft state
249 * @cmd: Command packet to be returned to free command pool
251 inline void
252 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
254 unsigned long flags;
255 u32 blk_tags;
256 struct megasas_cmd_fusion *cmd_fusion;
257 struct fusion_context *fusion = instance->ctrl_context;
259 /* This flag is used only for fusion adapter.
260 * Wait for Interrupt for Polled mode DCMD
262 if (cmd->flags & DRV_DCMD_POLLED_MODE)
263 return;
265 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
267 if (fusion) {
268 blk_tags = instance->max_scsi_cmds + cmd->index;
269 cmd_fusion = fusion->cmd_list[blk_tags];
270 megasas_return_cmd_fusion(instance, cmd_fusion);
272 cmd->scmd = NULL;
273 cmd->frame_count = 0;
274 cmd->flags = 0;
275 if (!fusion && reset_devices)
276 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
277 list_add(&cmd->list, (&instance->cmd_pool)->next);
279 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
283 static const char *
284 format_timestamp(uint32_t timestamp)
286 static char buffer[32];
288 if ((timestamp & 0xff000000) == 0xff000000)
289 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
290 0x00ffffff);
291 else
292 snprintf(buffer, sizeof(buffer), "%us", timestamp);
293 return buffer;
296 static const char *
297 format_class(int8_t class)
299 static char buffer[6];
301 switch (class) {
302 case MFI_EVT_CLASS_DEBUG:
303 return "debug";
304 case MFI_EVT_CLASS_PROGRESS:
305 return "progress";
306 case MFI_EVT_CLASS_INFO:
307 return "info";
308 case MFI_EVT_CLASS_WARNING:
309 return "WARN";
310 case MFI_EVT_CLASS_CRITICAL:
311 return "CRIT";
312 case MFI_EVT_CLASS_FATAL:
313 return "FATAL";
314 case MFI_EVT_CLASS_DEAD:
315 return "DEAD";
316 default:
317 snprintf(buffer, sizeof(buffer), "%d", class);
318 return buffer;
323 * megasas_decode_evt: Decode FW AEN event and print critical event
324 * for information.
325 * @instance: Adapter soft state
327 static void
328 megasas_decode_evt(struct megasas_instance *instance)
330 struct megasas_evt_detail *evt_detail = instance->evt_detail;
331 union megasas_evt_class_locale class_locale;
332 class_locale.word = le32_to_cpu(evt_detail->cl.word);
334 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
335 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
336 le32_to_cpu(evt_detail->seq_num),
337 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
338 (class_locale.members.locale),
339 format_class(class_locale.members.class),
340 evt_detail->description);
344 * The following functions are defined for xscale
345 * (deviceid : 1064R, PERC5) controllers
349 * megasas_enable_intr_xscale - Enables interrupts
350 * @regs: MFI register set
352 static inline void
353 megasas_enable_intr_xscale(struct megasas_instance *instance)
355 struct megasas_register_set __iomem *regs;
357 regs = instance->reg_set;
358 writel(0, &(regs)->outbound_intr_mask);
360 /* Dummy readl to force pci flush */
361 readl(&regs->outbound_intr_mask);
365 * megasas_disable_intr_xscale -Disables interrupt
366 * @regs: MFI register set
368 static inline void
369 megasas_disable_intr_xscale(struct megasas_instance *instance)
371 struct megasas_register_set __iomem *regs;
372 u32 mask = 0x1f;
374 regs = instance->reg_set;
375 writel(mask, &regs->outbound_intr_mask);
376 /* Dummy readl to force pci flush */
377 readl(&regs->outbound_intr_mask);
381 * megasas_read_fw_status_reg_xscale - returns the current FW status value
382 * @regs: MFI register set
384 static u32
385 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
387 return readl(&(regs)->outbound_msg_0);
390 * megasas_clear_interrupt_xscale - Check & clear interrupt
391 * @regs: MFI register set
393 static int
394 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
396 u32 status;
397 u32 mfiStatus = 0;
400 * Check if it is our interrupt
402 status = readl(&regs->outbound_intr_status);
404 if (status & MFI_OB_INTR_STATUS_MASK)
405 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
406 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
407 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
410 * Clear the interrupt by writing back the same value
412 if (mfiStatus)
413 writel(status, &regs->outbound_intr_status);
415 /* Dummy readl to force pci flush */
416 readl(&regs->outbound_intr_status);
418 return mfiStatus;
422 * megasas_fire_cmd_xscale - Sends command to the FW
423 * @frame_phys_addr : Physical address of cmd
424 * @frame_count : Number of frames for the command
425 * @regs : MFI register set
427 static inline void
428 megasas_fire_cmd_xscale(struct megasas_instance *instance,
429 dma_addr_t frame_phys_addr,
430 u32 frame_count,
431 struct megasas_register_set __iomem *regs)
433 unsigned long flags;
435 spin_lock_irqsave(&instance->hba_lock, flags);
436 writel((frame_phys_addr >> 3)|(frame_count),
437 &(regs)->inbound_queue_port);
438 spin_unlock_irqrestore(&instance->hba_lock, flags);
442 * megasas_adp_reset_xscale - For controller reset
443 * @regs: MFI register set
445 static int
446 megasas_adp_reset_xscale(struct megasas_instance *instance,
447 struct megasas_register_set __iomem *regs)
449 u32 i;
450 u32 pcidata;
452 writel(MFI_ADP_RESET, &regs->inbound_doorbell);
454 for (i = 0; i < 3; i++)
455 msleep(1000); /* sleep for 3 secs */
456 pcidata = 0;
457 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
458 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
459 if (pcidata & 0x2) {
460 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
461 pcidata &= ~0x2;
462 pci_write_config_dword(instance->pdev,
463 MFI_1068_PCSR_OFFSET, pcidata);
465 for (i = 0; i < 2; i++)
466 msleep(1000); /* need to wait 2 secs again */
468 pcidata = 0;
469 pci_read_config_dword(instance->pdev,
470 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
471 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
472 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
473 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
474 pcidata = 0;
475 pci_write_config_dword(instance->pdev,
476 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
479 return 0;
483 * megasas_check_reset_xscale - For controller reset check
484 * @regs: MFI register set
486 static int
487 megasas_check_reset_xscale(struct megasas_instance *instance,
488 struct megasas_register_set __iomem *regs)
490 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
491 (le32_to_cpu(*instance->consumer) ==
492 MEGASAS_ADPRESET_INPROG_SIGN))
493 return 1;
494 return 0;
497 static struct megasas_instance_template megasas_instance_template_xscale = {
499 .fire_cmd = megasas_fire_cmd_xscale,
500 .enable_intr = megasas_enable_intr_xscale,
501 .disable_intr = megasas_disable_intr_xscale,
502 .clear_intr = megasas_clear_intr_xscale,
503 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
504 .adp_reset = megasas_adp_reset_xscale,
505 .check_reset = megasas_check_reset_xscale,
506 .service_isr = megasas_isr,
507 .tasklet = megasas_complete_cmd_dpc,
508 .init_adapter = megasas_init_adapter_mfi,
509 .build_and_issue_cmd = megasas_build_and_issue_cmd,
510 .issue_dcmd = megasas_issue_dcmd,
514 * This is the end of set of functions & definitions specific
515 * to xscale (deviceid : 1064R, PERC5) controllers
519 * The following functions are defined for ppc (deviceid : 0x60)
520 * controllers
524 * megasas_enable_intr_ppc - Enables interrupts
525 * @regs: MFI register set
527 static inline void
528 megasas_enable_intr_ppc(struct megasas_instance *instance)
530 struct megasas_register_set __iomem *regs;
532 regs = instance->reg_set;
533 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
535 writel(~0x80000000, &(regs)->outbound_intr_mask);
537 /* Dummy readl to force pci flush */
538 readl(&regs->outbound_intr_mask);
542 * megasas_disable_intr_ppc - Disable interrupt
543 * @regs: MFI register set
545 static inline void
546 megasas_disable_intr_ppc(struct megasas_instance *instance)
548 struct megasas_register_set __iomem *regs;
549 u32 mask = 0xFFFFFFFF;
551 regs = instance->reg_set;
552 writel(mask, &regs->outbound_intr_mask);
553 /* Dummy readl to force pci flush */
554 readl(&regs->outbound_intr_mask);
558 * megasas_read_fw_status_reg_ppc - returns the current FW status value
559 * @regs: MFI register set
561 static u32
562 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
564 return readl(&(regs)->outbound_scratch_pad);
568 * megasas_clear_interrupt_ppc - Check & clear interrupt
569 * @regs: MFI register set
571 static int
572 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
574 u32 status, mfiStatus = 0;
577 * Check if it is our interrupt
579 status = readl(&regs->outbound_intr_status);
581 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
582 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
584 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
585 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
588 * Clear the interrupt by writing back the same value
590 writel(status, &regs->outbound_doorbell_clear);
592 /* Dummy readl to force pci flush */
593 readl(&regs->outbound_doorbell_clear);
595 return mfiStatus;
599 * megasas_fire_cmd_ppc - Sends command to the FW
600 * @frame_phys_addr : Physical address of cmd
601 * @frame_count : Number of frames for the command
602 * @regs : MFI register set
604 static inline void
605 megasas_fire_cmd_ppc(struct megasas_instance *instance,
606 dma_addr_t frame_phys_addr,
607 u32 frame_count,
608 struct megasas_register_set __iomem *regs)
610 unsigned long flags;
612 spin_lock_irqsave(&instance->hba_lock, flags);
613 writel((frame_phys_addr | (frame_count<<1))|1,
614 &(regs)->inbound_queue_port);
615 spin_unlock_irqrestore(&instance->hba_lock, flags);
619 * megasas_check_reset_ppc - For controller reset check
620 * @regs: MFI register set
622 static int
623 megasas_check_reset_ppc(struct megasas_instance *instance,
624 struct megasas_register_set __iomem *regs)
626 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
627 return 1;
629 return 0;
632 static struct megasas_instance_template megasas_instance_template_ppc = {
634 .fire_cmd = megasas_fire_cmd_ppc,
635 .enable_intr = megasas_enable_intr_ppc,
636 .disable_intr = megasas_disable_intr_ppc,
637 .clear_intr = megasas_clear_intr_ppc,
638 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
639 .adp_reset = megasas_adp_reset_xscale,
640 .check_reset = megasas_check_reset_ppc,
641 .service_isr = megasas_isr,
642 .tasklet = megasas_complete_cmd_dpc,
643 .init_adapter = megasas_init_adapter_mfi,
644 .build_and_issue_cmd = megasas_build_and_issue_cmd,
645 .issue_dcmd = megasas_issue_dcmd,
649 * megasas_enable_intr_skinny - Enables interrupts
650 * @regs: MFI register set
652 static inline void
653 megasas_enable_intr_skinny(struct megasas_instance *instance)
655 struct megasas_register_set __iomem *regs;
657 regs = instance->reg_set;
658 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
660 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
662 /* Dummy readl to force pci flush */
663 readl(&regs->outbound_intr_mask);
667 * megasas_disable_intr_skinny - Disables interrupt
668 * @regs: MFI register set
670 static inline void
671 megasas_disable_intr_skinny(struct megasas_instance *instance)
673 struct megasas_register_set __iomem *regs;
674 u32 mask = 0xFFFFFFFF;
676 regs = instance->reg_set;
677 writel(mask, &regs->outbound_intr_mask);
678 /* Dummy readl to force pci flush */
679 readl(&regs->outbound_intr_mask);
683 * megasas_read_fw_status_reg_skinny - returns the current FW status value
684 * @regs: MFI register set
686 static u32
687 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
689 return readl(&(regs)->outbound_scratch_pad);
693 * megasas_clear_interrupt_skinny - Check & clear interrupt
694 * @regs: MFI register set
696 static int
697 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
699 u32 status;
700 u32 mfiStatus = 0;
703 * Check if it is our interrupt
705 status = readl(&regs->outbound_intr_status);
707 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
708 return 0;
712 * Check if it is our interrupt
714 if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
715 MFI_STATE_FAULT) {
716 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
717 } else
718 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
721 * Clear the interrupt by writing back the same value
723 writel(status, &regs->outbound_intr_status);
726 * dummy read to flush PCI
728 readl(&regs->outbound_intr_status);
730 return mfiStatus;
734 * megasas_fire_cmd_skinny - Sends command to the FW
735 * @frame_phys_addr : Physical address of cmd
736 * @frame_count : Number of frames for the command
737 * @regs : MFI register set
739 static inline void
740 megasas_fire_cmd_skinny(struct megasas_instance *instance,
741 dma_addr_t frame_phys_addr,
742 u32 frame_count,
743 struct megasas_register_set __iomem *regs)
745 unsigned long flags;
747 spin_lock_irqsave(&instance->hba_lock, flags);
748 writel(upper_32_bits(frame_phys_addr),
749 &(regs)->inbound_high_queue_port);
750 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
751 &(regs)->inbound_low_queue_port);
752 mmiowb();
753 spin_unlock_irqrestore(&instance->hba_lock, flags);
757 * megasas_check_reset_skinny - For controller reset check
758 * @regs: MFI register set
760 static int
761 megasas_check_reset_skinny(struct megasas_instance *instance,
762 struct megasas_register_set __iomem *regs)
764 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
765 return 1;
767 return 0;
770 static struct megasas_instance_template megasas_instance_template_skinny = {
772 .fire_cmd = megasas_fire_cmd_skinny,
773 .enable_intr = megasas_enable_intr_skinny,
774 .disable_intr = megasas_disable_intr_skinny,
775 .clear_intr = megasas_clear_intr_skinny,
776 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
777 .adp_reset = megasas_adp_reset_gen2,
778 .check_reset = megasas_check_reset_skinny,
779 .service_isr = megasas_isr,
780 .tasklet = megasas_complete_cmd_dpc,
781 .init_adapter = megasas_init_adapter_mfi,
782 .build_and_issue_cmd = megasas_build_and_issue_cmd,
783 .issue_dcmd = megasas_issue_dcmd,
788 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
789 * controllers
793 * megasas_enable_intr_gen2 - Enables interrupts
794 * @regs: MFI register set
796 static inline void
797 megasas_enable_intr_gen2(struct megasas_instance *instance)
799 struct megasas_register_set __iomem *regs;
801 regs = instance->reg_set;
802 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
804 /* write ~0x00000005 (4 & 1) to the intr mask*/
805 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
807 /* Dummy readl to force pci flush */
808 readl(&regs->outbound_intr_mask);
812 * megasas_disable_intr_gen2 - Disables interrupt
813 * @regs: MFI register set
815 static inline void
816 megasas_disable_intr_gen2(struct megasas_instance *instance)
818 struct megasas_register_set __iomem *regs;
819 u32 mask = 0xFFFFFFFF;
821 regs = instance->reg_set;
822 writel(mask, &regs->outbound_intr_mask);
823 /* Dummy readl to force pci flush */
824 readl(&regs->outbound_intr_mask);
828 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
829 * @regs: MFI register set
831 static u32
832 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
834 return readl(&(regs)->outbound_scratch_pad);
838 * megasas_clear_interrupt_gen2 - Check & clear interrupt
839 * @regs: MFI register set
841 static int
842 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
844 u32 status;
845 u32 mfiStatus = 0;
848 * Check if it is our interrupt
850 status = readl(&regs->outbound_intr_status);
852 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
853 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
855 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
856 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
860 * Clear the interrupt by writing back the same value
862 if (mfiStatus)
863 writel(status, &regs->outbound_doorbell_clear);
865 /* Dummy readl to force pci flush */
866 readl(&regs->outbound_intr_status);
868 return mfiStatus;
871 * megasas_fire_cmd_gen2 - Sends command to the FW
872 * @frame_phys_addr : Physical address of cmd
873 * @frame_count : Number of frames for the command
874 * @regs : MFI register set
876 static inline void
877 megasas_fire_cmd_gen2(struct megasas_instance *instance,
878 dma_addr_t frame_phys_addr,
879 u32 frame_count,
880 struct megasas_register_set __iomem *regs)
882 unsigned long flags;
884 spin_lock_irqsave(&instance->hba_lock, flags);
885 writel((frame_phys_addr | (frame_count<<1))|1,
886 &(regs)->inbound_queue_port);
887 spin_unlock_irqrestore(&instance->hba_lock, flags);
891 * megasas_adp_reset_gen2 - For controller reset
892 * @regs: MFI register set
894 static int
895 megasas_adp_reset_gen2(struct megasas_instance *instance,
896 struct megasas_register_set __iomem *reg_set)
898 u32 retry = 0 ;
899 u32 HostDiag;
900 u32 __iomem *seq_offset = &reg_set->seq_offset;
901 u32 __iomem *hostdiag_offset = &reg_set->host_diag;
903 if (instance->instancet == &megasas_instance_template_skinny) {
904 seq_offset = &reg_set->fusion_seq_offset;
905 hostdiag_offset = &reg_set->fusion_host_diag;
908 writel(0, seq_offset);
909 writel(4, seq_offset);
910 writel(0xb, seq_offset);
911 writel(2, seq_offset);
912 writel(7, seq_offset);
913 writel(0xd, seq_offset);
915 msleep(1000);
917 HostDiag = (u32)readl(hostdiag_offset);
919 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
920 msleep(100);
921 HostDiag = (u32)readl(hostdiag_offset);
922 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
923 retry, HostDiag);
925 if (retry++ >= 100)
926 return 1;
930 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
932 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
934 ssleep(10);
936 HostDiag = (u32)readl(hostdiag_offset);
937 while (HostDiag & DIAG_RESET_ADAPTER) {
938 msleep(100);
939 HostDiag = (u32)readl(hostdiag_offset);
940 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
941 retry, HostDiag);
943 if (retry++ >= 1000)
944 return 1;
947 return 0;
951 * megasas_check_reset_gen2 - For controller reset check
952 * @regs: MFI register set
954 static int
955 megasas_check_reset_gen2(struct megasas_instance *instance,
956 struct megasas_register_set __iomem *regs)
958 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
959 return 1;
961 return 0;
964 static struct megasas_instance_template megasas_instance_template_gen2 = {
966 .fire_cmd = megasas_fire_cmd_gen2,
967 .enable_intr = megasas_enable_intr_gen2,
968 .disable_intr = megasas_disable_intr_gen2,
969 .clear_intr = megasas_clear_intr_gen2,
970 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
971 .adp_reset = megasas_adp_reset_gen2,
972 .check_reset = megasas_check_reset_gen2,
973 .service_isr = megasas_isr,
974 .tasklet = megasas_complete_cmd_dpc,
975 .init_adapter = megasas_init_adapter_mfi,
976 .build_and_issue_cmd = megasas_build_and_issue_cmd,
977 .issue_dcmd = megasas_issue_dcmd,
981 * This is the end of set of functions & definitions
982 * specific to gen2 (deviceid : 0x78, 0x79) controllers
986 * Template added for TB (Fusion)
988 extern struct megasas_instance_template megasas_instance_template_fusion;
991 * megasas_issue_polled - Issues a polling command
992 * @instance: Adapter soft state
993 * @cmd: Command packet to be issued
995 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
998 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1000 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1002 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1003 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1005 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
1006 (instance->instancet->issue_dcmd(instance, cmd))) {
1007 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1008 __func__, __LINE__);
1009 return DCMD_NOT_FIRED;
1012 return wait_and_poll(instance, cmd, instance->requestorId ?
1013 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1017 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1018 * @instance: Adapter soft state
1019 * @cmd: Command to be issued
1020 * @timeout: Timeout in seconds
1022 * This function waits on an event for the command to be returned from ISR.
1023 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1024 * Used to issue ioctl commands.
1027 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1028 struct megasas_cmd *cmd, int timeout)
1030 int ret = 0;
1031 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1033 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
1034 (instance->instancet->issue_dcmd(instance, cmd))) {
1035 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1036 __func__, __LINE__);
1037 return DCMD_NOT_FIRED;
1040 if (timeout) {
1041 ret = wait_event_timeout(instance->int_cmd_wait_q,
1042 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1043 if (!ret) {
1044 dev_err(&instance->pdev->dev, "Failed from %s %d DCMD Timed out\n",
1045 __func__, __LINE__);
1046 return DCMD_TIMEOUT;
1048 } else
1049 wait_event(instance->int_cmd_wait_q,
1050 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1052 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1053 DCMD_SUCCESS : DCMD_FAILED;
1057 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1058 * @instance: Adapter soft state
1059 * @cmd_to_abort: Previously issued cmd to be aborted
1060 * @timeout: Timeout in seconds
1062 * MFI firmware can abort previously issued AEN comamnd (automatic event
1063 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1064 * cmd and waits for return status.
1065 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1067 static int
1068 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1069 struct megasas_cmd *cmd_to_abort, int timeout)
1071 struct megasas_cmd *cmd;
1072 struct megasas_abort_frame *abort_fr;
1073 int ret = 0;
1075 cmd = megasas_get_cmd(instance);
1077 if (!cmd)
1078 return -1;
1080 abort_fr = &cmd->frame->abort;
1083 * Prepare and issue the abort frame
1085 abort_fr->cmd = MFI_CMD_ABORT;
1086 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1087 abort_fr->flags = cpu_to_le16(0);
1088 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1089 abort_fr->abort_mfi_phys_addr_lo =
1090 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1091 abort_fr->abort_mfi_phys_addr_hi =
1092 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1094 cmd->sync_cmd = 1;
1095 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1097 if ((atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) ||
1098 (instance->instancet->issue_dcmd(instance, cmd))) {
1099 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1100 __func__, __LINE__);
1101 return DCMD_NOT_FIRED;
1104 if (timeout) {
1105 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1106 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1107 if (!ret) {
1108 dev_err(&instance->pdev->dev, "Failed from %s %d Abort Timed out\n",
1109 __func__, __LINE__);
1110 return DCMD_TIMEOUT;
1112 } else
1113 wait_event(instance->abort_cmd_wait_q,
1114 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1116 cmd->sync_cmd = 0;
1118 megasas_return_cmd(instance, cmd);
1119 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1120 DCMD_SUCCESS : DCMD_FAILED;
1124 * megasas_make_sgl32 - Prepares 32-bit SGL
1125 * @instance: Adapter soft state
1126 * @scp: SCSI command from the mid-layer
1127 * @mfi_sgl: SGL to be filled in
1129 * If successful, this function returns the number of SG elements. Otherwise,
1130 * it returnes -1.
1132 static int
1133 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1134 union megasas_sgl *mfi_sgl)
1136 int i;
1137 int sge_count;
1138 struct scatterlist *os_sgl;
1140 sge_count = scsi_dma_map(scp);
1141 BUG_ON(sge_count < 0);
1143 if (sge_count) {
1144 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1145 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1146 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1149 return sge_count;
1153 * megasas_make_sgl64 - Prepares 64-bit SGL
1154 * @instance: Adapter soft state
1155 * @scp: SCSI command from the mid-layer
1156 * @mfi_sgl: SGL to be filled in
1158 * If successful, this function returns the number of SG elements. Otherwise,
1159 * it returnes -1.
1161 static int
1162 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1163 union megasas_sgl *mfi_sgl)
1165 int i;
1166 int sge_count;
1167 struct scatterlist *os_sgl;
1169 sge_count = scsi_dma_map(scp);
1170 BUG_ON(sge_count < 0);
1172 if (sge_count) {
1173 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1174 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1175 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1178 return sge_count;
1182 * megasas_make_sgl_skinny - Prepares IEEE SGL
1183 * @instance: Adapter soft state
1184 * @scp: SCSI command from the mid-layer
1185 * @mfi_sgl: SGL to be filled in
1187 * If successful, this function returns the number of SG elements. Otherwise,
1188 * it returnes -1.
1190 static int
1191 megasas_make_sgl_skinny(struct megasas_instance *instance,
1192 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1194 int i;
1195 int sge_count;
1196 struct scatterlist *os_sgl;
1198 sge_count = scsi_dma_map(scp);
1200 if (sge_count) {
1201 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1202 mfi_sgl->sge_skinny[i].length =
1203 cpu_to_le32(sg_dma_len(os_sgl));
1204 mfi_sgl->sge_skinny[i].phys_addr =
1205 cpu_to_le64(sg_dma_address(os_sgl));
1206 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1209 return sge_count;
1213 * megasas_get_frame_count - Computes the number of frames
1214 * @frame_type : type of frame- io or pthru frame
1215 * @sge_count : number of sg elements
1217 * Returns the number of frames required for numnber of sge's (sge_count)
1220 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1221 u8 sge_count, u8 frame_type)
1223 int num_cnt;
1224 int sge_bytes;
1225 u32 sge_sz;
1226 u32 frame_count = 0;
1228 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1229 sizeof(struct megasas_sge32);
1231 if (instance->flag_ieee) {
1232 sge_sz = sizeof(struct megasas_sge_skinny);
1236 * Main frame can contain 2 SGEs for 64-bit SGLs and
1237 * 3 SGEs for 32-bit SGLs for ldio &
1238 * 1 SGEs for 64-bit SGLs and
1239 * 2 SGEs for 32-bit SGLs for pthru frame
1241 if (unlikely(frame_type == PTHRU_FRAME)) {
1242 if (instance->flag_ieee == 1) {
1243 num_cnt = sge_count - 1;
1244 } else if (IS_DMA64)
1245 num_cnt = sge_count - 1;
1246 else
1247 num_cnt = sge_count - 2;
1248 } else {
1249 if (instance->flag_ieee == 1) {
1250 num_cnt = sge_count - 1;
1251 } else if (IS_DMA64)
1252 num_cnt = sge_count - 2;
1253 else
1254 num_cnt = sge_count - 3;
1257 if (num_cnt > 0) {
1258 sge_bytes = sge_sz * num_cnt;
1260 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1261 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1263 /* Main frame */
1264 frame_count += 1;
1266 if (frame_count > 7)
1267 frame_count = 8;
1268 return frame_count;
1272 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1273 * @instance: Adapter soft state
1274 * @scp: SCSI command
1275 * @cmd: Command to be prepared in
1277 * This function prepares CDB commands. These are typcially pass-through
1278 * commands to the devices.
1280 static int
1281 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1282 struct megasas_cmd *cmd)
1284 u32 is_logical;
1285 u32 device_id;
1286 u16 flags = 0;
1287 struct megasas_pthru_frame *pthru;
1289 is_logical = MEGASAS_IS_LOGICAL(scp);
1290 device_id = MEGASAS_DEV_INDEX(scp);
1291 pthru = (struct megasas_pthru_frame *)cmd->frame;
1293 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1294 flags = MFI_FRAME_DIR_WRITE;
1295 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1296 flags = MFI_FRAME_DIR_READ;
1297 else if (scp->sc_data_direction == PCI_DMA_NONE)
1298 flags = MFI_FRAME_DIR_NONE;
1300 if (instance->flag_ieee == 1) {
1301 flags |= MFI_FRAME_IEEE;
1305 * Prepare the DCDB frame
1307 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1308 pthru->cmd_status = 0x0;
1309 pthru->scsi_status = 0x0;
1310 pthru->target_id = device_id;
1311 pthru->lun = scp->device->lun;
1312 pthru->cdb_len = scp->cmd_len;
1313 pthru->timeout = 0;
1314 pthru->pad_0 = 0;
1315 pthru->flags = cpu_to_le16(flags);
1316 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1318 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1321 * If the command is for the tape device, set the
1322 * pthru timeout to the os layer timeout value.
1324 if (scp->device->type == TYPE_TAPE) {
1325 if ((scp->request->timeout / HZ) > 0xFFFF)
1326 pthru->timeout = cpu_to_le16(0xFFFF);
1327 else
1328 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1332 * Construct SGL
1334 if (instance->flag_ieee == 1) {
1335 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1336 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1337 &pthru->sgl);
1338 } else if (IS_DMA64) {
1339 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1340 pthru->sge_count = megasas_make_sgl64(instance, scp,
1341 &pthru->sgl);
1342 } else
1343 pthru->sge_count = megasas_make_sgl32(instance, scp,
1344 &pthru->sgl);
1346 if (pthru->sge_count > instance->max_num_sge) {
1347 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1348 pthru->sge_count);
1349 return 0;
1353 * Sense info specific
1355 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1356 pthru->sense_buf_phys_addr_hi =
1357 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1358 pthru->sense_buf_phys_addr_lo =
1359 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1362 * Compute the total number of frames this command consumes. FW uses
1363 * this number to pull sufficient number of frames from host memory.
1365 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1366 PTHRU_FRAME);
1368 return cmd->frame_count;
1372 * megasas_build_ldio - Prepares IOs to logical devices
1373 * @instance: Adapter soft state
1374 * @scp: SCSI command
1375 * @cmd: Command to be prepared
1377 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1379 static int
1380 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1381 struct megasas_cmd *cmd)
1383 u32 device_id;
1384 u8 sc = scp->cmnd[0];
1385 u16 flags = 0;
1386 struct megasas_io_frame *ldio;
1388 device_id = MEGASAS_DEV_INDEX(scp);
1389 ldio = (struct megasas_io_frame *)cmd->frame;
1391 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1392 flags = MFI_FRAME_DIR_WRITE;
1393 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1394 flags = MFI_FRAME_DIR_READ;
1396 if (instance->flag_ieee == 1) {
1397 flags |= MFI_FRAME_IEEE;
1401 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1403 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1404 ldio->cmd_status = 0x0;
1405 ldio->scsi_status = 0x0;
1406 ldio->target_id = device_id;
1407 ldio->timeout = 0;
1408 ldio->reserved_0 = 0;
1409 ldio->pad_0 = 0;
1410 ldio->flags = cpu_to_le16(flags);
1411 ldio->start_lba_hi = 0;
1412 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1415 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1417 if (scp->cmd_len == 6) {
1418 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1419 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1420 ((u32) scp->cmnd[2] << 8) |
1421 (u32) scp->cmnd[3]);
1423 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1427 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1429 else if (scp->cmd_len == 10) {
1430 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1431 ((u32) scp->cmnd[7] << 8));
1432 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1433 ((u32) scp->cmnd[3] << 16) |
1434 ((u32) scp->cmnd[4] << 8) |
1435 (u32) scp->cmnd[5]);
1439 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1441 else if (scp->cmd_len == 12) {
1442 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1443 ((u32) scp->cmnd[7] << 16) |
1444 ((u32) scp->cmnd[8] << 8) |
1445 (u32) scp->cmnd[9]);
1447 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1448 ((u32) scp->cmnd[3] << 16) |
1449 ((u32) scp->cmnd[4] << 8) |
1450 (u32) scp->cmnd[5]);
1454 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1456 else if (scp->cmd_len == 16) {
1457 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1458 ((u32) scp->cmnd[11] << 16) |
1459 ((u32) scp->cmnd[12] << 8) |
1460 (u32) scp->cmnd[13]);
1462 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1463 ((u32) scp->cmnd[7] << 16) |
1464 ((u32) scp->cmnd[8] << 8) |
1465 (u32) scp->cmnd[9]);
1467 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1468 ((u32) scp->cmnd[3] << 16) |
1469 ((u32) scp->cmnd[4] << 8) |
1470 (u32) scp->cmnd[5]);
1475 * Construct SGL
1477 if (instance->flag_ieee) {
1478 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1479 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1480 &ldio->sgl);
1481 } else if (IS_DMA64) {
1482 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1483 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1484 } else
1485 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1487 if (ldio->sge_count > instance->max_num_sge) {
1488 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1489 ldio->sge_count);
1490 return 0;
1494 * Sense info specific
1496 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1497 ldio->sense_buf_phys_addr_hi = 0;
1498 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1501 * Compute the total number of frames this command consumes. FW uses
1502 * this number to pull sufficient number of frames from host memory.
1504 cmd->frame_count = megasas_get_frame_count(instance,
1505 ldio->sge_count, IO_FRAME);
1507 return cmd->frame_count;
1511 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1512 * and whether it's RW or non RW
1513 * @scmd: SCSI command
1516 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1518 int ret;
1520 switch (cmd->cmnd[0]) {
1521 case READ_10:
1522 case WRITE_10:
1523 case READ_12:
1524 case WRITE_12:
1525 case READ_6:
1526 case WRITE_6:
1527 case READ_16:
1528 case WRITE_16:
1529 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1530 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1531 break;
1532 default:
1533 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1534 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1536 return ret;
1540 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1541 * in FW
1542 * @instance: Adapter soft state
1544 static inline void
1545 megasas_dump_pending_frames(struct megasas_instance *instance)
1547 struct megasas_cmd *cmd;
1548 int i,n;
1549 union megasas_sgl *mfi_sgl;
1550 struct megasas_io_frame *ldio;
1551 struct megasas_pthru_frame *pthru;
1552 u32 sgcount;
1553 u32 max_cmd = instance->max_fw_cmds;
1555 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1556 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1557 if (IS_DMA64)
1558 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1559 else
1560 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1562 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1563 for (i = 0; i < max_cmd; i++) {
1564 cmd = instance->cmd_list[i];
1565 if (!cmd->scmd)
1566 continue;
1567 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1568 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1569 ldio = (struct megasas_io_frame *)cmd->frame;
1570 mfi_sgl = &ldio->sgl;
1571 sgcount = ldio->sge_count;
1572 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1573 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1574 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1575 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1576 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1577 } else {
1578 pthru = (struct megasas_pthru_frame *) cmd->frame;
1579 mfi_sgl = &pthru->sgl;
1580 sgcount = pthru->sge_count;
1581 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1582 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1583 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1584 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1585 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1587 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1588 for (n = 0; n < sgcount; n++) {
1589 if (IS_DMA64)
1590 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1591 le32_to_cpu(mfi_sgl->sge64[n].length),
1592 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1593 else
1594 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1595 le32_to_cpu(mfi_sgl->sge32[n].length),
1596 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1599 } /*for max_cmd*/
1600 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1601 for (i = 0; i < max_cmd; i++) {
1603 cmd = instance->cmd_list[i];
1605 if (cmd->sync_cmd == 1)
1606 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1608 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1612 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1613 struct scsi_cmnd *scmd)
1615 struct megasas_cmd *cmd;
1616 u32 frame_count;
1618 cmd = megasas_get_cmd(instance);
1619 if (!cmd)
1620 return SCSI_MLQUEUE_HOST_BUSY;
1623 * Logical drive command
1625 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1626 frame_count = megasas_build_ldio(instance, scmd, cmd);
1627 else
1628 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1630 if (!frame_count)
1631 goto out_return_cmd;
1633 cmd->scmd = scmd;
1634 scmd->SCp.ptr = (char *)cmd;
1637 * Issue the command to the FW
1639 atomic_inc(&instance->fw_outstanding);
1641 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1642 cmd->frame_count-1, instance->reg_set);
1644 return 0;
1645 out_return_cmd:
1646 megasas_return_cmd(instance, cmd);
1647 return SCSI_MLQUEUE_HOST_BUSY;
1652 * megasas_queue_command - Queue entry point
1653 * @scmd: SCSI command to be queued
1654 * @done: Callback entry point
1656 static int
1657 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1659 struct megasas_instance *instance;
1660 struct MR_PRIV_DEVICE *mr_device_priv_data;
1662 instance = (struct megasas_instance *)
1663 scmd->device->host->hostdata;
1665 if (instance->unload == 1) {
1666 scmd->result = DID_NO_CONNECT << 16;
1667 scmd->scsi_done(scmd);
1668 return 0;
1671 if (instance->issuepend_done == 0)
1672 return SCSI_MLQUEUE_HOST_BUSY;
1675 /* Check for an mpio path and adjust behavior */
1676 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1677 if (megasas_check_mpio_paths(instance, scmd) ==
1678 (DID_RESET << 16)) {
1679 return SCSI_MLQUEUE_HOST_BUSY;
1680 } else {
1681 scmd->result = DID_NO_CONNECT << 16;
1682 scmd->scsi_done(scmd);
1683 return 0;
1687 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1688 scmd->result = DID_NO_CONNECT << 16;
1689 scmd->scsi_done(scmd);
1690 return 0;
1693 mr_device_priv_data = scmd->device->hostdata;
1694 if (!mr_device_priv_data) {
1695 scmd->result = DID_NO_CONNECT << 16;
1696 scmd->scsi_done(scmd);
1697 return 0;
1700 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1701 return SCSI_MLQUEUE_HOST_BUSY;
1703 if (mr_device_priv_data->tm_busy)
1704 return SCSI_MLQUEUE_DEVICE_BUSY;
1707 scmd->result = 0;
1709 if (MEGASAS_IS_LOGICAL(scmd) &&
1710 (scmd->device->id >= instance->fw_supported_vd_count ||
1711 scmd->device->lun)) {
1712 scmd->result = DID_BAD_TARGET << 16;
1713 goto out_done;
1716 switch (scmd->cmnd[0]) {
1717 case SYNCHRONIZE_CACHE:
1719 * FW takes care of flush cache on its own
1720 * No need to send it down
1722 scmd->result = DID_OK << 16;
1723 goto out_done;
1724 default:
1725 break;
1728 return instance->instancet->build_and_issue_cmd(instance, scmd);
1730 out_done:
1731 scmd->scsi_done(scmd);
1732 return 0;
1735 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1737 int i;
1739 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1741 if ((megasas_mgmt_info.instance[i]) &&
1742 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1743 return megasas_mgmt_info.instance[i];
1746 return NULL;
1750 * megasas_update_sdev_properties - Update sdev structure based on controller's FW capabilities
1752 * @sdev: OS provided scsi device
1754 * Returns void
1756 void megasas_update_sdev_properties(struct scsi_device *sdev)
1758 u16 pd_index = 0;
1759 u32 device_id, ld;
1760 struct megasas_instance *instance;
1761 struct fusion_context *fusion;
1762 struct MR_PRIV_DEVICE *mr_device_priv_data;
1763 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1764 struct MR_LD_RAID *raid;
1765 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1767 instance = megasas_lookup_instance(sdev->host->host_no);
1768 fusion = instance->ctrl_context;
1769 mr_device_priv_data = sdev->hostdata;
1771 if (!fusion)
1772 return;
1774 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1775 instance->use_seqnum_jbod_fp) {
1776 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1777 sdev->id;
1778 pd_sync = (void *)fusion->pd_seq_sync
1779 [(instance->pd_seq_map_id - 1) & 1];
1780 mr_device_priv_data->is_tm_capable =
1781 pd_sync->seq[pd_index].capability.tmCapable;
1782 } else {
1783 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1784 + sdev->id;
1785 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1786 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1787 raid = MR_LdRaidGet(ld, local_map_ptr);
1789 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1790 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1791 mr_device_priv_data->is_tm_capable =
1792 raid->capability.tmCapable;
1796 static void megasas_set_device_queue_depth(struct scsi_device *sdev)
1798 u16 pd_index = 0;
1799 int ret = DCMD_FAILED;
1800 struct megasas_instance *instance;
1802 instance = megasas_lookup_instance(sdev->host->host_no);
1804 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1805 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
1807 if (instance->pd_info) {
1808 mutex_lock(&instance->hba_mutex);
1809 ret = megasas_get_pd_info(instance, pd_index);
1810 mutex_unlock(&instance->hba_mutex);
1813 if (ret != DCMD_SUCCESS)
1814 return;
1816 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1818 switch (instance->pd_list[pd_index].interface) {
1819 case SAS_PD:
1820 scsi_change_queue_depth(sdev, MEGASAS_SAS_QD);
1821 break;
1823 case SATA_PD:
1824 scsi_change_queue_depth(sdev, MEGASAS_SATA_QD);
1825 break;
1827 default:
1828 scsi_change_queue_depth(sdev, MEGASAS_DEFAULT_PD_QD);
1835 static int megasas_slave_configure(struct scsi_device *sdev)
1837 u16 pd_index = 0;
1838 struct megasas_instance *instance;
1840 instance = megasas_lookup_instance(sdev->host->host_no);
1841 if (instance->pd_list_not_supported) {
1842 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1843 sdev->type == TYPE_DISK) {
1844 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1845 sdev->id;
1846 if (instance->pd_list[pd_index].driveState !=
1847 MR_PD_STATE_SYSTEM)
1848 return -ENXIO;
1851 megasas_set_device_queue_depth(sdev);
1852 megasas_update_sdev_properties(sdev);
1855 * The RAID firmware may require extended timeouts.
1857 blk_queue_rq_timeout(sdev->request_queue,
1858 scmd_timeout * HZ);
1860 return 0;
1863 static int megasas_slave_alloc(struct scsi_device *sdev)
1865 u16 pd_index = 0;
1866 struct megasas_instance *instance ;
1867 struct MR_PRIV_DEVICE *mr_device_priv_data;
1869 instance = megasas_lookup_instance(sdev->host->host_no);
1870 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1872 * Open the OS scan to the SYSTEM PD
1874 pd_index =
1875 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1876 sdev->id;
1877 if ((instance->pd_list_not_supported ||
1878 instance->pd_list[pd_index].driveState ==
1879 MR_PD_STATE_SYSTEM)) {
1880 goto scan_target;
1882 return -ENXIO;
1885 scan_target:
1886 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
1887 GFP_KERNEL);
1888 if (!mr_device_priv_data)
1889 return -ENOMEM;
1890 sdev->hostdata = mr_device_priv_data;
1891 return 0;
1894 static void megasas_slave_destroy(struct scsi_device *sdev)
1896 kfree(sdev->hostdata);
1897 sdev->hostdata = NULL;
1901 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1902 * kill adapter
1903 * @instance: Adapter soft state
1906 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1908 int i;
1909 struct megasas_cmd *cmd_mfi;
1910 struct megasas_cmd_fusion *cmd_fusion;
1911 struct fusion_context *fusion = instance->ctrl_context;
1913 /* Find all outstanding ioctls */
1914 if (fusion) {
1915 for (i = 0; i < instance->max_fw_cmds; i++) {
1916 cmd_fusion = fusion->cmd_list[i];
1917 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1918 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1919 if (cmd_mfi->sync_cmd &&
1920 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1921 megasas_complete_cmd(instance,
1922 cmd_mfi, DID_OK);
1925 } else {
1926 for (i = 0; i < instance->max_fw_cmds; i++) {
1927 cmd_mfi = instance->cmd_list[i];
1928 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1929 MFI_CMD_ABORT)
1930 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1936 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1938 /* Set critical error to block I/O & ioctls in case caller didn't */
1939 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
1940 /* Wait 1 second to ensure IO or ioctls in build have posted */
1941 msleep(1000);
1942 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1943 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1944 (instance->ctrl_context)) {
1945 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1946 /* Flush */
1947 readl(&instance->reg_set->doorbell);
1948 if (instance->requestorId && instance->peerIsPresent)
1949 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1950 } else {
1951 writel(MFI_STOP_ADP,
1952 &instance->reg_set->inbound_doorbell);
1954 /* Complete outstanding ioctls when adapter is killed */
1955 megasas_complete_outstanding_ioctls(instance);
1959 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1960 * restored to max value
1961 * @instance: Adapter soft state
1964 void
1965 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1967 unsigned long flags;
1969 if (instance->flag & MEGASAS_FW_BUSY
1970 && time_after(jiffies, instance->last_time + 5 * HZ)
1971 && atomic_read(&instance->fw_outstanding) <
1972 instance->throttlequeuedepth + 1) {
1974 spin_lock_irqsave(instance->host->host_lock, flags);
1975 instance->flag &= ~MEGASAS_FW_BUSY;
1977 instance->host->can_queue = instance->cur_can_queue;
1978 spin_unlock_irqrestore(instance->host->host_lock, flags);
1983 * megasas_complete_cmd_dpc - Returns FW's controller structure
1984 * @instance_addr: Address of adapter soft state
1986 * Tasklet to complete cmds
1988 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1990 u32 producer;
1991 u32 consumer;
1992 u32 context;
1993 struct megasas_cmd *cmd;
1994 struct megasas_instance *instance =
1995 (struct megasas_instance *)instance_addr;
1996 unsigned long flags;
1998 /* If we have already declared adapter dead, donot complete cmds */
1999 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2000 return;
2002 spin_lock_irqsave(&instance->completion_lock, flags);
2004 producer = le32_to_cpu(*instance->producer);
2005 consumer = le32_to_cpu(*instance->consumer);
2007 while (consumer != producer) {
2008 context = le32_to_cpu(instance->reply_queue[consumer]);
2009 if (context >= instance->max_fw_cmds) {
2010 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2011 context);
2012 BUG();
2015 cmd = instance->cmd_list[context];
2017 megasas_complete_cmd(instance, cmd, DID_OK);
2019 consumer++;
2020 if (consumer == (instance->max_fw_cmds + 1)) {
2021 consumer = 0;
2025 *instance->consumer = cpu_to_le32(producer);
2027 spin_unlock_irqrestore(&instance->completion_lock, flags);
2030 * Check if we can restore can_queue
2032 megasas_check_and_restore_queue_depth(instance);
2036 * megasas_start_timer - Initializes a timer object
2037 * @instance: Adapter soft state
2038 * @timer: timer object to be initialized
2039 * @fn: timer function
2040 * @interval: time interval between timer function call
2043 void megasas_start_timer(struct megasas_instance *instance,
2044 struct timer_list *timer,
2045 void *fn, unsigned long interval)
2047 init_timer(timer);
2048 timer->expires = jiffies + interval;
2049 timer->data = (unsigned long)instance;
2050 timer->function = fn;
2051 add_timer(timer);
2054 static void
2055 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2057 static void
2058 process_fw_state_change_wq(struct work_struct *work);
2060 void megasas_do_ocr(struct megasas_instance *instance)
2062 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2063 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2064 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2065 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2067 instance->instancet->disable_intr(instance);
2068 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2069 instance->issuepend_done = 0;
2071 atomic_set(&instance->fw_outstanding, 0);
2072 megasas_internal_reset_defer_cmds(instance);
2073 process_fw_state_change_wq(&instance->work_init);
2076 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2077 int initial)
2079 struct megasas_cmd *cmd;
2080 struct megasas_dcmd_frame *dcmd;
2081 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2082 dma_addr_t new_affiliation_111_h;
2083 int ld, retval = 0;
2084 u8 thisVf;
2086 cmd = megasas_get_cmd(instance);
2088 if (!cmd) {
2089 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2090 "Failed to get cmd for scsi%d\n",
2091 instance->host->host_no);
2092 return -ENOMEM;
2095 dcmd = &cmd->frame->dcmd;
2097 if (!instance->vf_affiliation_111) {
2098 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2099 "affiliation for scsi%d\n", instance->host->host_no);
2100 megasas_return_cmd(instance, cmd);
2101 return -ENOMEM;
2104 if (initial)
2105 memset(instance->vf_affiliation_111, 0,
2106 sizeof(struct MR_LD_VF_AFFILIATION_111));
2107 else {
2108 new_affiliation_111 =
2109 pci_alloc_consistent(instance->pdev,
2110 sizeof(struct MR_LD_VF_AFFILIATION_111),
2111 &new_affiliation_111_h);
2112 if (!new_affiliation_111) {
2113 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2114 "memory for new affiliation for scsi%d\n",
2115 instance->host->host_no);
2116 megasas_return_cmd(instance, cmd);
2117 return -ENOMEM;
2119 memset(new_affiliation_111, 0,
2120 sizeof(struct MR_LD_VF_AFFILIATION_111));
2123 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2125 dcmd->cmd = MFI_CMD_DCMD;
2126 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2127 dcmd->sge_count = 1;
2128 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2129 dcmd->timeout = 0;
2130 dcmd->pad_0 = 0;
2131 dcmd->data_xfer_len =
2132 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2133 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2135 if (initial)
2136 dcmd->sgl.sge32[0].phys_addr =
2137 cpu_to_le32(instance->vf_affiliation_111_h);
2138 else
2139 dcmd->sgl.sge32[0].phys_addr =
2140 cpu_to_le32(new_affiliation_111_h);
2142 dcmd->sgl.sge32[0].length = cpu_to_le32(
2143 sizeof(struct MR_LD_VF_AFFILIATION_111));
2145 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2146 "scsi%d\n", instance->host->host_no);
2148 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2149 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2150 " failed with status 0x%x for scsi%d\n",
2151 dcmd->cmd_status, instance->host->host_no);
2152 retval = 1; /* Do a scan if we couldn't get affiliation */
2153 goto out;
2156 if (!initial) {
2157 thisVf = new_affiliation_111->thisVf;
2158 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2159 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2160 new_affiliation_111->map[ld].policy[thisVf]) {
2161 dev_warn(&instance->pdev->dev, "SR-IOV: "
2162 "Got new LD/VF affiliation for scsi%d\n",
2163 instance->host->host_no);
2164 memcpy(instance->vf_affiliation_111,
2165 new_affiliation_111,
2166 sizeof(struct MR_LD_VF_AFFILIATION_111));
2167 retval = 1;
2168 goto out;
2171 out:
2172 if (new_affiliation_111) {
2173 pci_free_consistent(instance->pdev,
2174 sizeof(struct MR_LD_VF_AFFILIATION_111),
2175 new_affiliation_111,
2176 new_affiliation_111_h);
2179 megasas_return_cmd(instance, cmd);
2181 return retval;
2184 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2185 int initial)
2187 struct megasas_cmd *cmd;
2188 struct megasas_dcmd_frame *dcmd;
2189 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2190 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2191 dma_addr_t new_affiliation_h;
2192 int i, j, retval = 0, found = 0, doscan = 0;
2193 u8 thisVf;
2195 cmd = megasas_get_cmd(instance);
2197 if (!cmd) {
2198 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2199 "Failed to get cmd for scsi%d\n",
2200 instance->host->host_no);
2201 return -ENOMEM;
2204 dcmd = &cmd->frame->dcmd;
2206 if (!instance->vf_affiliation) {
2207 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2208 "affiliation for scsi%d\n", instance->host->host_no);
2209 megasas_return_cmd(instance, cmd);
2210 return -ENOMEM;
2213 if (initial)
2214 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2215 sizeof(struct MR_LD_VF_AFFILIATION));
2216 else {
2217 new_affiliation =
2218 pci_alloc_consistent(instance->pdev,
2219 (MAX_LOGICAL_DRIVES + 1) *
2220 sizeof(struct MR_LD_VF_AFFILIATION),
2221 &new_affiliation_h);
2222 if (!new_affiliation) {
2223 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2224 "memory for new affiliation for scsi%d\n",
2225 instance->host->host_no);
2226 megasas_return_cmd(instance, cmd);
2227 return -ENOMEM;
2229 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2230 sizeof(struct MR_LD_VF_AFFILIATION));
2233 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2235 dcmd->cmd = MFI_CMD_DCMD;
2236 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2237 dcmd->sge_count = 1;
2238 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2239 dcmd->timeout = 0;
2240 dcmd->pad_0 = 0;
2241 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2242 sizeof(struct MR_LD_VF_AFFILIATION));
2243 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2245 if (initial)
2246 dcmd->sgl.sge32[0].phys_addr =
2247 cpu_to_le32(instance->vf_affiliation_h);
2248 else
2249 dcmd->sgl.sge32[0].phys_addr =
2250 cpu_to_le32(new_affiliation_h);
2252 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2253 sizeof(struct MR_LD_VF_AFFILIATION));
2255 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2256 "scsi%d\n", instance->host->host_no);
2259 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2260 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2261 " failed with status 0x%x for scsi%d\n",
2262 dcmd->cmd_status, instance->host->host_no);
2263 retval = 1; /* Do a scan if we couldn't get affiliation */
2264 goto out;
2267 if (!initial) {
2268 if (!new_affiliation->ldCount) {
2269 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2270 "affiliation for passive path for scsi%d\n",
2271 instance->host->host_no);
2272 retval = 1;
2273 goto out;
2275 newmap = new_affiliation->map;
2276 savedmap = instance->vf_affiliation->map;
2277 thisVf = new_affiliation->thisVf;
2278 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2279 found = 0;
2280 for (j = 0; j < instance->vf_affiliation->ldCount;
2281 j++) {
2282 if (newmap->ref.targetId ==
2283 savedmap->ref.targetId) {
2284 found = 1;
2285 if (newmap->policy[thisVf] !=
2286 savedmap->policy[thisVf]) {
2287 doscan = 1;
2288 goto out;
2291 savedmap = (struct MR_LD_VF_MAP *)
2292 ((unsigned char *)savedmap +
2293 savedmap->size);
2295 if (!found && newmap->policy[thisVf] !=
2296 MR_LD_ACCESS_HIDDEN) {
2297 doscan = 1;
2298 goto out;
2300 newmap = (struct MR_LD_VF_MAP *)
2301 ((unsigned char *)newmap + newmap->size);
2304 newmap = new_affiliation->map;
2305 savedmap = instance->vf_affiliation->map;
2307 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2308 found = 0;
2309 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2310 if (savedmap->ref.targetId ==
2311 newmap->ref.targetId) {
2312 found = 1;
2313 if (savedmap->policy[thisVf] !=
2314 newmap->policy[thisVf]) {
2315 doscan = 1;
2316 goto out;
2319 newmap = (struct MR_LD_VF_MAP *)
2320 ((unsigned char *)newmap +
2321 newmap->size);
2323 if (!found && savedmap->policy[thisVf] !=
2324 MR_LD_ACCESS_HIDDEN) {
2325 doscan = 1;
2326 goto out;
2328 savedmap = (struct MR_LD_VF_MAP *)
2329 ((unsigned char *)savedmap +
2330 savedmap->size);
2333 out:
2334 if (doscan) {
2335 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2336 "affiliation for scsi%d\n", instance->host->host_no);
2337 memcpy(instance->vf_affiliation, new_affiliation,
2338 new_affiliation->size);
2339 retval = 1;
2342 if (new_affiliation)
2343 pci_free_consistent(instance->pdev,
2344 (MAX_LOGICAL_DRIVES + 1) *
2345 sizeof(struct MR_LD_VF_AFFILIATION),
2346 new_affiliation, new_affiliation_h);
2347 megasas_return_cmd(instance, cmd);
2349 return retval;
2352 /* This function will get the current SR-IOV LD/VF affiliation */
2353 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2354 int initial)
2356 int retval;
2358 if (instance->PlasmaFW111)
2359 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2360 else
2361 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2362 return retval;
2365 /* This function will tell FW to start the SR-IOV heartbeat */
2366 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2367 int initial)
2369 struct megasas_cmd *cmd;
2370 struct megasas_dcmd_frame *dcmd;
2371 int retval = 0;
2373 cmd = megasas_get_cmd(instance);
2375 if (!cmd) {
2376 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2377 "Failed to get cmd for scsi%d\n",
2378 instance->host->host_no);
2379 return -ENOMEM;
2382 dcmd = &cmd->frame->dcmd;
2384 if (initial) {
2385 instance->hb_host_mem =
2386 pci_zalloc_consistent(instance->pdev,
2387 sizeof(struct MR_CTRL_HB_HOST_MEM),
2388 &instance->hb_host_mem_h);
2389 if (!instance->hb_host_mem) {
2390 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2391 " memory for heartbeat host memory for scsi%d\n",
2392 instance->host->host_no);
2393 retval = -ENOMEM;
2394 goto out;
2398 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2400 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2401 dcmd->cmd = MFI_CMD_DCMD;
2402 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2403 dcmd->sge_count = 1;
2404 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2405 dcmd->timeout = 0;
2406 dcmd->pad_0 = 0;
2407 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2408 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2409 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2410 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2412 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2413 instance->host->host_no);
2415 if (instance->ctrl_context && !instance->mask_interrupts)
2416 retval = megasas_issue_blocked_cmd(instance, cmd,
2417 MEGASAS_ROUTINE_WAIT_TIME_VF);
2418 else
2419 retval = megasas_issue_polled(instance, cmd);
2421 if (retval) {
2422 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2423 "_MEM_ALLOC DCMD %s for scsi%d\n",
2424 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2425 "timed out" : "failed", instance->host->host_no);
2426 retval = 1;
2429 out:
2430 megasas_return_cmd(instance, cmd);
2432 return retval;
2435 /* Handler for SR-IOV heartbeat */
2436 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2438 struct megasas_instance *instance =
2439 (struct megasas_instance *)instance_addr;
2441 if (instance->hb_host_mem->HB.fwCounter !=
2442 instance->hb_host_mem->HB.driverCounter) {
2443 instance->hb_host_mem->HB.driverCounter =
2444 instance->hb_host_mem->HB.fwCounter;
2445 mod_timer(&instance->sriov_heartbeat_timer,
2446 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2447 } else {
2448 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2449 "completed for scsi%d\n", instance->host->host_no);
2450 schedule_work(&instance->work_init);
2455 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2456 * @instance: Adapter soft state
2458 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2459 * complete all its outstanding commands. Returns error if one or more IOs
2460 * are pending after this time period. It also marks the controller dead.
2462 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2464 int i, sl, outstanding;
2465 u32 reset_index;
2466 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2467 unsigned long flags;
2468 struct list_head clist_local;
2469 struct megasas_cmd *reset_cmd;
2470 u32 fw_state;
2472 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2473 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2474 __func__, __LINE__);
2475 return FAILED;
2478 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2480 INIT_LIST_HEAD(&clist_local);
2481 spin_lock_irqsave(&instance->hba_lock, flags);
2482 list_splice_init(&instance->internal_reset_pending_q,
2483 &clist_local);
2484 spin_unlock_irqrestore(&instance->hba_lock, flags);
2486 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2487 for (i = 0; i < wait_time; i++) {
2488 msleep(1000);
2489 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2490 break;
2493 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2494 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2495 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2496 return FAILED;
2499 reset_index = 0;
2500 while (!list_empty(&clist_local)) {
2501 reset_cmd = list_entry((&clist_local)->next,
2502 struct megasas_cmd, list);
2503 list_del_init(&reset_cmd->list);
2504 if (reset_cmd->scmd) {
2505 reset_cmd->scmd->result = DID_RESET << 16;
2506 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2507 reset_index, reset_cmd,
2508 reset_cmd->scmd->cmnd[0]);
2510 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2511 megasas_return_cmd(instance, reset_cmd);
2512 } else if (reset_cmd->sync_cmd) {
2513 dev_notice(&instance->pdev->dev, "%p synch cmds"
2514 "reset queue\n",
2515 reset_cmd);
2517 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2518 instance->instancet->fire_cmd(instance,
2519 reset_cmd->frame_phys_addr,
2520 0, instance->reg_set);
2521 } else {
2522 dev_notice(&instance->pdev->dev, "%p unexpected"
2523 "cmds lst\n",
2524 reset_cmd);
2526 reset_index++;
2529 return SUCCESS;
2532 for (i = 0; i < resetwaittime; i++) {
2533 outstanding = atomic_read(&instance->fw_outstanding);
2535 if (!outstanding)
2536 break;
2538 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2539 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2540 "commands to complete\n",i,outstanding);
2542 * Call cmd completion routine. Cmd to be
2543 * be completed directly without depending on isr.
2545 megasas_complete_cmd_dpc((unsigned long)instance);
2548 msleep(1000);
2551 i = 0;
2552 outstanding = atomic_read(&instance->fw_outstanding);
2553 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2555 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2556 goto no_outstanding;
2558 if (instance->disableOnlineCtrlReset)
2559 goto kill_hba_and_failed;
2560 do {
2561 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2562 dev_info(&instance->pdev->dev,
2563 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, oustanding 0x%x\n",
2564 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2565 if (i == 3)
2566 goto kill_hba_and_failed;
2567 megasas_do_ocr(instance);
2569 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2570 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2571 __func__, __LINE__);
2572 return FAILED;
2574 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2575 __func__, __LINE__);
2577 for (sl = 0; sl < 10; sl++)
2578 msleep(500);
2580 outstanding = atomic_read(&instance->fw_outstanding);
2582 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2583 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2584 goto no_outstanding;
2586 i++;
2587 } while (i <= 3);
2589 no_outstanding:
2591 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2592 __func__, __LINE__);
2593 return SUCCESS;
2595 kill_hba_and_failed:
2597 /* Reset not supported, kill adapter */
2598 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2599 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2600 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2601 atomic_read(&instance->fw_outstanding));
2602 megasas_dump_pending_frames(instance);
2603 megaraid_sas_kill_hba(instance);
2605 return FAILED;
2609 * megasas_generic_reset - Generic reset routine
2610 * @scmd: Mid-layer SCSI command
2612 * This routine implements a generic reset handler for device, bus and host
2613 * reset requests. Device, bus and host specific reset handlers can use this
2614 * function after they do their specific tasks.
2616 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2618 int ret_val;
2619 struct megasas_instance *instance;
2621 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2623 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2624 scmd->cmnd[0], scmd->retries);
2626 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2627 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2628 return FAILED;
2631 ret_val = megasas_wait_for_outstanding(instance);
2632 if (ret_val == SUCCESS)
2633 dev_notice(&instance->pdev->dev, "reset successful\n");
2634 else
2635 dev_err(&instance->pdev->dev, "failed to do reset\n");
2637 return ret_val;
2641 * megasas_reset_timer - quiesce the adapter if required
2642 * @scmd: scsi cmnd
2644 * Sets the FW busy flag and reduces the host->can_queue if the
2645 * cmd has not been completed within the timeout period.
2647 static enum
2648 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2650 struct megasas_instance *instance;
2651 unsigned long flags;
2653 if (time_after(jiffies, scmd->jiffies_at_alloc +
2654 (scmd_timeout * 2) * HZ)) {
2655 return BLK_EH_NOT_HANDLED;
2658 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2659 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2660 /* FW is busy, throttle IO */
2661 spin_lock_irqsave(instance->host->host_lock, flags);
2663 instance->host->can_queue = instance->throttlequeuedepth;
2664 instance->last_time = jiffies;
2665 instance->flag |= MEGASAS_FW_BUSY;
2667 spin_unlock_irqrestore(instance->host->host_lock, flags);
2669 return BLK_EH_RESET_TIMER;
2673 * megasas_reset_bus_host - Bus & host reset handler entry point
2675 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2677 int ret;
2678 struct megasas_instance *instance;
2680 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2683 * First wait for all commands to complete
2685 if (instance->ctrl_context)
2686 ret = megasas_reset_fusion(scmd->device->host, 1);
2687 else
2688 ret = megasas_generic_reset(scmd);
2690 return ret;
2694 * megasas_task_abort - Issues task abort request to firmware
2695 * (supported only for fusion adapters)
2696 * @scmd: SCSI command pointer
2698 static int megasas_task_abort(struct scsi_cmnd *scmd)
2700 int ret;
2701 struct megasas_instance *instance;
2703 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2705 if (instance->ctrl_context)
2706 ret = megasas_task_abort_fusion(scmd);
2707 else {
2708 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2709 ret = FAILED;
2712 return ret;
2716 * megasas_reset_target: Issues target reset request to firmware
2717 * (supported only for fusion adapters)
2718 * @scmd: SCSI command pointer
2720 static int megasas_reset_target(struct scsi_cmnd *scmd)
2722 int ret;
2723 struct megasas_instance *instance;
2725 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2727 if (instance->ctrl_context)
2728 ret = megasas_reset_target_fusion(scmd);
2729 else {
2730 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2731 ret = FAILED;
2734 return ret;
2738 * megasas_bios_param - Returns disk geometry for a disk
2739 * @sdev: device handle
2740 * @bdev: block device
2741 * @capacity: drive capacity
2742 * @geom: geometry parameters
2744 static int
2745 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2746 sector_t capacity, int geom[])
2748 int heads;
2749 int sectors;
2750 sector_t cylinders;
2751 unsigned long tmp;
2753 /* Default heads (64) & sectors (32) */
2754 heads = 64;
2755 sectors = 32;
2757 tmp = heads * sectors;
2758 cylinders = capacity;
2760 sector_div(cylinders, tmp);
2763 * Handle extended translation size for logical drives > 1Gb
2766 if (capacity >= 0x200000) {
2767 heads = 255;
2768 sectors = 63;
2769 tmp = heads*sectors;
2770 cylinders = capacity;
2771 sector_div(cylinders, tmp);
2774 geom[0] = heads;
2775 geom[1] = sectors;
2776 geom[2] = cylinders;
2778 return 0;
2781 static void megasas_aen_polling(struct work_struct *work);
2784 * megasas_service_aen - Processes an event notification
2785 * @instance: Adapter soft state
2786 * @cmd: AEN command completed by the ISR
2788 * For AEN, driver sends a command down to FW that is held by the FW till an
2789 * event occurs. When an event of interest occurs, FW completes the command
2790 * that it was previously holding.
2792 * This routines sends SIGIO signal to processes that have registered with the
2793 * driver for AEN.
2795 static void
2796 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2798 unsigned long flags;
2801 * Don't signal app if it is just an aborted previously registered aen
2803 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2804 spin_lock_irqsave(&poll_aen_lock, flags);
2805 megasas_poll_wait_aen = 1;
2806 spin_unlock_irqrestore(&poll_aen_lock, flags);
2807 wake_up(&megasas_poll_wait);
2808 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2810 else
2811 cmd->abort_aen = 0;
2813 instance->aen_cmd = NULL;
2815 megasas_return_cmd(instance, cmd);
2817 if ((instance->unload == 0) &&
2818 ((instance->issuepend_done == 1))) {
2819 struct megasas_aen_event *ev;
2821 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2822 if (!ev) {
2823 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2824 } else {
2825 ev->instance = instance;
2826 instance->ev = ev;
2827 INIT_DELAYED_WORK(&ev->hotplug_work,
2828 megasas_aen_polling);
2829 schedule_delayed_work(&ev->hotplug_work, 0);
2834 static ssize_t
2835 megasas_fw_crash_buffer_store(struct device *cdev,
2836 struct device_attribute *attr, const char *buf, size_t count)
2838 struct Scsi_Host *shost = class_to_shost(cdev);
2839 struct megasas_instance *instance =
2840 (struct megasas_instance *) shost->hostdata;
2841 int val = 0;
2842 unsigned long flags;
2844 if (kstrtoint(buf, 0, &val) != 0)
2845 return -EINVAL;
2847 spin_lock_irqsave(&instance->crashdump_lock, flags);
2848 instance->fw_crash_buffer_offset = val;
2849 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2850 return strlen(buf);
2853 static ssize_t
2854 megasas_fw_crash_buffer_show(struct device *cdev,
2855 struct device_attribute *attr, char *buf)
2857 struct Scsi_Host *shost = class_to_shost(cdev);
2858 struct megasas_instance *instance =
2859 (struct megasas_instance *) shost->hostdata;
2860 u32 size;
2861 unsigned long buff_addr;
2862 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2863 unsigned long src_addr;
2864 unsigned long flags;
2865 u32 buff_offset;
2867 spin_lock_irqsave(&instance->crashdump_lock, flags);
2868 buff_offset = instance->fw_crash_buffer_offset;
2869 if (!instance->crash_dump_buf &&
2870 !((instance->fw_crash_state == AVAILABLE) ||
2871 (instance->fw_crash_state == COPYING))) {
2872 dev_err(&instance->pdev->dev,
2873 "Firmware crash dump is not available\n");
2874 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2875 return -EINVAL;
2878 buff_addr = (unsigned long) buf;
2880 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2881 dev_err(&instance->pdev->dev,
2882 "Firmware crash dump offset is out of range\n");
2883 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2884 return 0;
2887 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2888 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2890 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2891 (buff_offset % dmachunk);
2892 memcpy(buf, (void *)src_addr, size);
2893 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2895 return size;
2898 static ssize_t
2899 megasas_fw_crash_buffer_size_show(struct device *cdev,
2900 struct device_attribute *attr, char *buf)
2902 struct Scsi_Host *shost = class_to_shost(cdev);
2903 struct megasas_instance *instance =
2904 (struct megasas_instance *) shost->hostdata;
2906 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2907 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2910 static ssize_t
2911 megasas_fw_crash_state_store(struct device *cdev,
2912 struct device_attribute *attr, const char *buf, size_t count)
2914 struct Scsi_Host *shost = class_to_shost(cdev);
2915 struct megasas_instance *instance =
2916 (struct megasas_instance *) shost->hostdata;
2917 int val = 0;
2918 unsigned long flags;
2920 if (kstrtoint(buf, 0, &val) != 0)
2921 return -EINVAL;
2923 if ((val <= AVAILABLE || val > COPY_ERROR)) {
2924 dev_err(&instance->pdev->dev, "application updates invalid "
2925 "firmware crash state\n");
2926 return -EINVAL;
2929 instance->fw_crash_state = val;
2931 if ((val == COPIED) || (val == COPY_ERROR)) {
2932 spin_lock_irqsave(&instance->crashdump_lock, flags);
2933 megasas_free_host_crash_buffer(instance);
2934 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2935 if (val == COPY_ERROR)
2936 dev_info(&instance->pdev->dev, "application failed to "
2937 "copy Firmware crash dump\n");
2938 else
2939 dev_info(&instance->pdev->dev, "Firmware crash dump "
2940 "copied successfully\n");
2942 return strlen(buf);
2945 static ssize_t
2946 megasas_fw_crash_state_show(struct device *cdev,
2947 struct device_attribute *attr, char *buf)
2949 struct Scsi_Host *shost = class_to_shost(cdev);
2950 struct megasas_instance *instance =
2951 (struct megasas_instance *) shost->hostdata;
2953 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2956 static ssize_t
2957 megasas_page_size_show(struct device *cdev,
2958 struct device_attribute *attr, char *buf)
2960 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2963 static ssize_t
2964 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
2965 char *buf)
2967 struct Scsi_Host *shost = class_to_shost(cdev);
2968 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
2970 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
2973 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2974 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2975 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2976 megasas_fw_crash_buffer_size_show, NULL);
2977 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2978 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2979 static DEVICE_ATTR(page_size, S_IRUGO,
2980 megasas_page_size_show, NULL);
2981 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
2982 megasas_ldio_outstanding_show, NULL);
2984 struct device_attribute *megaraid_host_attrs[] = {
2985 &dev_attr_fw_crash_buffer_size,
2986 &dev_attr_fw_crash_buffer,
2987 &dev_attr_fw_crash_state,
2988 &dev_attr_page_size,
2989 &dev_attr_ldio_outstanding,
2990 NULL,
2994 * Scsi host template for megaraid_sas driver
2996 static struct scsi_host_template megasas_template = {
2998 .module = THIS_MODULE,
2999 .name = "Avago SAS based MegaRAID driver",
3000 .proc_name = "megaraid_sas",
3001 .slave_configure = megasas_slave_configure,
3002 .slave_alloc = megasas_slave_alloc,
3003 .slave_destroy = megasas_slave_destroy,
3004 .queuecommand = megasas_queue_command,
3005 .eh_target_reset_handler = megasas_reset_target,
3006 .eh_abort_handler = megasas_task_abort,
3007 .eh_host_reset_handler = megasas_reset_bus_host,
3008 .eh_timed_out = megasas_reset_timer,
3009 .shost_attrs = megaraid_host_attrs,
3010 .bios_param = megasas_bios_param,
3011 .use_clustering = ENABLE_CLUSTERING,
3012 .change_queue_depth = scsi_change_queue_depth,
3013 .no_write_same = 1,
3017 * megasas_complete_int_cmd - Completes an internal command
3018 * @instance: Adapter soft state
3019 * @cmd: Command to be completed
3021 * The megasas_issue_blocked_cmd() function waits for a command to complete
3022 * after it issues a command. This function wakes up that waiting routine by
3023 * calling wake_up() on the wait queue.
3025 static void
3026 megasas_complete_int_cmd(struct megasas_instance *instance,
3027 struct megasas_cmd *cmd)
3029 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3030 wake_up(&instance->int_cmd_wait_q);
3034 * megasas_complete_abort - Completes aborting a command
3035 * @instance: Adapter soft state
3036 * @cmd: Cmd that was issued to abort another cmd
3038 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3039 * after it issues an abort on a previously issued command. This function
3040 * wakes up all functions waiting on the same wait queue.
3042 static void
3043 megasas_complete_abort(struct megasas_instance *instance,
3044 struct megasas_cmd *cmd)
3046 if (cmd->sync_cmd) {
3047 cmd->sync_cmd = 0;
3048 cmd->cmd_status_drv = 0;
3049 wake_up(&instance->abort_cmd_wait_q);
3054 * megasas_complete_cmd - Completes a command
3055 * @instance: Adapter soft state
3056 * @cmd: Command to be completed
3057 * @alt_status: If non-zero, use this value as status to
3058 * SCSI mid-layer instead of the value returned
3059 * by the FW. This should be used if caller wants
3060 * an alternate status (as in the case of aborted
3061 * commands)
3063 void
3064 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3065 u8 alt_status)
3067 int exception = 0;
3068 struct megasas_header *hdr = &cmd->frame->hdr;
3069 unsigned long flags;
3070 struct fusion_context *fusion = instance->ctrl_context;
3071 u32 opcode, status;
3073 /* flag for the retry reset */
3074 cmd->retry_for_fw_reset = 0;
3076 if (cmd->scmd)
3077 cmd->scmd->SCp.ptr = NULL;
3079 switch (hdr->cmd) {
3080 case MFI_CMD_INVALID:
3081 /* Some older 1068 controller FW may keep a pended
3082 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3083 when booting the kdump kernel. Ignore this command to
3084 prevent a kernel panic on shutdown of the kdump kernel. */
3085 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3086 "completed\n");
3087 dev_warn(&instance->pdev->dev, "If you have a controller "
3088 "other than PERC5, please upgrade your firmware\n");
3089 break;
3090 case MFI_CMD_PD_SCSI_IO:
3091 case MFI_CMD_LD_SCSI_IO:
3094 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3095 * issued either through an IO path or an IOCTL path. If it
3096 * was via IOCTL, we will send it to internal completion.
3098 if (cmd->sync_cmd) {
3099 cmd->sync_cmd = 0;
3100 megasas_complete_int_cmd(instance, cmd);
3101 break;
3104 case MFI_CMD_LD_READ:
3105 case MFI_CMD_LD_WRITE:
3107 if (alt_status) {
3108 cmd->scmd->result = alt_status << 16;
3109 exception = 1;
3112 if (exception) {
3114 atomic_dec(&instance->fw_outstanding);
3116 scsi_dma_unmap(cmd->scmd);
3117 cmd->scmd->scsi_done(cmd->scmd);
3118 megasas_return_cmd(instance, cmd);
3120 break;
3123 switch (hdr->cmd_status) {
3125 case MFI_STAT_OK:
3126 cmd->scmd->result = DID_OK << 16;
3127 break;
3129 case MFI_STAT_SCSI_IO_FAILED:
3130 case MFI_STAT_LD_INIT_IN_PROGRESS:
3131 cmd->scmd->result =
3132 (DID_ERROR << 16) | hdr->scsi_status;
3133 break;
3135 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3137 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3139 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3140 memset(cmd->scmd->sense_buffer, 0,
3141 SCSI_SENSE_BUFFERSIZE);
3142 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3143 hdr->sense_len);
3145 cmd->scmd->result |= DRIVER_SENSE << 24;
3148 break;
3150 case MFI_STAT_LD_OFFLINE:
3151 case MFI_STAT_DEVICE_NOT_FOUND:
3152 cmd->scmd->result = DID_BAD_TARGET << 16;
3153 break;
3155 default:
3156 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3157 hdr->cmd_status);
3158 cmd->scmd->result = DID_ERROR << 16;
3159 break;
3162 atomic_dec(&instance->fw_outstanding);
3164 scsi_dma_unmap(cmd->scmd);
3165 cmd->scmd->scsi_done(cmd->scmd);
3166 megasas_return_cmd(instance, cmd);
3168 break;
3170 case MFI_CMD_SMP:
3171 case MFI_CMD_STP:
3172 case MFI_CMD_DCMD:
3173 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3174 /* Check for LD map update */
3175 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3176 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3177 fusion->fast_path_io = 0;
3178 spin_lock_irqsave(instance->host->host_lock, flags);
3179 instance->map_update_cmd = NULL;
3180 if (cmd->frame->hdr.cmd_status != 0) {
3181 if (cmd->frame->hdr.cmd_status !=
3182 MFI_STAT_NOT_FOUND)
3183 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3184 cmd->frame->hdr.cmd_status);
3185 else {
3186 megasas_return_cmd(instance, cmd);
3187 spin_unlock_irqrestore(
3188 instance->host->host_lock,
3189 flags);
3190 break;
3192 } else
3193 instance->map_id++;
3194 megasas_return_cmd(instance, cmd);
3197 * Set fast path IO to ZERO.
3198 * Validate Map will set proper value.
3199 * Meanwhile all IOs will go as LD IO.
3201 if (MR_ValidateMapInfo(instance))
3202 fusion->fast_path_io = 1;
3203 else
3204 fusion->fast_path_io = 0;
3205 megasas_sync_map_info(instance);
3206 spin_unlock_irqrestore(instance->host->host_lock,
3207 flags);
3208 break;
3210 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3211 opcode == MR_DCMD_CTRL_EVENT_GET) {
3212 spin_lock_irqsave(&poll_aen_lock, flags);
3213 megasas_poll_wait_aen = 0;
3214 spin_unlock_irqrestore(&poll_aen_lock, flags);
3217 /* FW has an updated PD sequence */
3218 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3219 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3221 spin_lock_irqsave(instance->host->host_lock, flags);
3222 status = cmd->frame->hdr.cmd_status;
3223 instance->jbod_seq_cmd = NULL;
3224 megasas_return_cmd(instance, cmd);
3226 if (status == MFI_STAT_OK) {
3227 instance->pd_seq_map_id++;
3228 /* Re-register a pd sync seq num cmd */
3229 if (megasas_sync_pd_seq_num(instance, true))
3230 instance->use_seqnum_jbod_fp = false;
3231 } else
3232 instance->use_seqnum_jbod_fp = false;
3234 spin_unlock_irqrestore(instance->host->host_lock, flags);
3235 break;
3239 * See if got an event notification
3241 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3242 megasas_service_aen(instance, cmd);
3243 else
3244 megasas_complete_int_cmd(instance, cmd);
3246 break;
3248 case MFI_CMD_ABORT:
3250 * Cmd issued to abort another cmd returned
3252 megasas_complete_abort(instance, cmd);
3253 break;
3255 default:
3256 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3257 hdr->cmd);
3258 break;
3263 * megasas_issue_pending_cmds_again - issue all pending cmds
3264 * in FW again because of the fw reset
3265 * @instance: Adapter soft state
3267 static inline void
3268 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3270 struct megasas_cmd *cmd;
3271 struct list_head clist_local;
3272 union megasas_evt_class_locale class_locale;
3273 unsigned long flags;
3274 u32 seq_num;
3276 INIT_LIST_HEAD(&clist_local);
3277 spin_lock_irqsave(&instance->hba_lock, flags);
3278 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3279 spin_unlock_irqrestore(&instance->hba_lock, flags);
3281 while (!list_empty(&clist_local)) {
3282 cmd = list_entry((&clist_local)->next,
3283 struct megasas_cmd, list);
3284 list_del_init(&cmd->list);
3286 if (cmd->sync_cmd || cmd->scmd) {
3287 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3288 "detected to be pending while HBA reset\n",
3289 cmd, cmd->scmd, cmd->sync_cmd);
3291 cmd->retry_for_fw_reset++;
3293 if (cmd->retry_for_fw_reset == 3) {
3294 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3295 "was tried multiple times during reset."
3296 "Shutting down the HBA\n",
3297 cmd, cmd->scmd, cmd->sync_cmd);
3298 instance->instancet->disable_intr(instance);
3299 atomic_set(&instance->fw_reset_no_pci_access, 1);
3300 megaraid_sas_kill_hba(instance);
3301 return;
3305 if (cmd->sync_cmd == 1) {
3306 if (cmd->scmd) {
3307 dev_notice(&instance->pdev->dev, "unexpected"
3308 "cmd attached to internal command!\n");
3310 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3311 "on the internal reset queue,"
3312 "issue it again.\n", cmd);
3313 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3314 instance->instancet->fire_cmd(instance,
3315 cmd->frame_phys_addr,
3316 0, instance->reg_set);
3317 } else if (cmd->scmd) {
3318 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3319 "detected on the internal queue, issue again.\n",
3320 cmd, cmd->scmd->cmnd[0]);
3322 atomic_inc(&instance->fw_outstanding);
3323 instance->instancet->fire_cmd(instance,
3324 cmd->frame_phys_addr,
3325 cmd->frame_count-1, instance->reg_set);
3326 } else {
3327 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3328 "internal reset defer list while re-issue!!\n",
3329 cmd);
3333 if (instance->aen_cmd) {
3334 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3335 megasas_return_cmd(instance, instance->aen_cmd);
3337 instance->aen_cmd = NULL;
3341 * Initiate AEN (Asynchronous Event Notification)
3343 seq_num = instance->last_seq_num;
3344 class_locale.members.reserved = 0;
3345 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3346 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3348 megasas_register_aen(instance, seq_num, class_locale.word);
3352 * Move the internal reset pending commands to a deferred queue.
3354 * We move the commands pending at internal reset time to a
3355 * pending queue. This queue would be flushed after successful
3356 * completion of the internal reset sequence. if the internal reset
3357 * did not complete in time, the kernel reset handler would flush
3358 * these commands.
3360 static void
3361 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3363 struct megasas_cmd *cmd;
3364 int i;
3365 u32 max_cmd = instance->max_fw_cmds;
3366 u32 defer_index;
3367 unsigned long flags;
3369 defer_index = 0;
3370 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3371 for (i = 0; i < max_cmd; i++) {
3372 cmd = instance->cmd_list[i];
3373 if (cmd->sync_cmd == 1 || cmd->scmd) {
3374 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3375 "on the defer queue as internal\n",
3376 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3378 if (!list_empty(&cmd->list)) {
3379 dev_notice(&instance->pdev->dev, "ERROR while"
3380 " moving this cmd:%p, %d %p, it was"
3381 "discovered on some list?\n",
3382 cmd, cmd->sync_cmd, cmd->scmd);
3384 list_del_init(&cmd->list);
3386 defer_index++;
3387 list_add_tail(&cmd->list,
3388 &instance->internal_reset_pending_q);
3391 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3395 static void
3396 process_fw_state_change_wq(struct work_struct *work)
3398 struct megasas_instance *instance =
3399 container_of(work, struct megasas_instance, work_init);
3400 u32 wait;
3401 unsigned long flags;
3403 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3404 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3405 atomic_read(&instance->adprecovery));
3406 return ;
3409 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3410 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3411 "state, restarting it...\n");
3413 instance->instancet->disable_intr(instance);
3414 atomic_set(&instance->fw_outstanding, 0);
3416 atomic_set(&instance->fw_reset_no_pci_access, 1);
3417 instance->instancet->adp_reset(instance, instance->reg_set);
3418 atomic_set(&instance->fw_reset_no_pci_access, 0);
3420 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3421 "initiating next stage...\n");
3423 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3424 "state 2 starting...\n");
3426 /* waiting for about 20 second before start the second init */
3427 for (wait = 0; wait < 30; wait++) {
3428 msleep(1000);
3431 if (megasas_transition_to_ready(instance, 1)) {
3432 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3434 atomic_set(&instance->fw_reset_no_pci_access, 1);
3435 megaraid_sas_kill_hba(instance);
3436 return ;
3439 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3440 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3441 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3443 *instance->consumer = *instance->producer;
3444 } else {
3445 *instance->consumer = 0;
3446 *instance->producer = 0;
3449 megasas_issue_init_mfi(instance);
3451 spin_lock_irqsave(&instance->hba_lock, flags);
3452 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3453 spin_unlock_irqrestore(&instance->hba_lock, flags);
3454 instance->instancet->enable_intr(instance);
3456 megasas_issue_pending_cmds_again(instance);
3457 instance->issuepend_done = 1;
3462 * megasas_deplete_reply_queue - Processes all completed commands
3463 * @instance: Adapter soft state
3464 * @alt_status: Alternate status to be returned to
3465 * SCSI mid-layer instead of the status
3466 * returned by the FW
3467 * Note: this must be called with hba lock held
3469 static int
3470 megasas_deplete_reply_queue(struct megasas_instance *instance,
3471 u8 alt_status)
3473 u32 mfiStatus;
3474 u32 fw_state;
3476 if ((mfiStatus = instance->instancet->check_reset(instance,
3477 instance->reg_set)) == 1) {
3478 return IRQ_HANDLED;
3481 if ((mfiStatus = instance->instancet->clear_intr(
3482 instance->reg_set)
3483 ) == 0) {
3484 /* Hardware may not set outbound_intr_status in MSI-X mode */
3485 if (!instance->msix_vectors)
3486 return IRQ_NONE;
3489 instance->mfiStatus = mfiStatus;
3491 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3492 fw_state = instance->instancet->read_fw_status_reg(
3493 instance->reg_set) & MFI_STATE_MASK;
3495 if (fw_state != MFI_STATE_FAULT) {
3496 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3497 fw_state);
3500 if ((fw_state == MFI_STATE_FAULT) &&
3501 (instance->disableOnlineCtrlReset == 0)) {
3502 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3504 if ((instance->pdev->device ==
3505 PCI_DEVICE_ID_LSI_SAS1064R) ||
3506 (instance->pdev->device ==
3507 PCI_DEVICE_ID_DELL_PERC5) ||
3508 (instance->pdev->device ==
3509 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3511 *instance->consumer =
3512 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3516 instance->instancet->disable_intr(instance);
3517 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3518 instance->issuepend_done = 0;
3520 atomic_set(&instance->fw_outstanding, 0);
3521 megasas_internal_reset_defer_cmds(instance);
3523 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3524 fw_state, atomic_read(&instance->adprecovery));
3526 schedule_work(&instance->work_init);
3527 return IRQ_HANDLED;
3529 } else {
3530 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3531 fw_state, instance->disableOnlineCtrlReset);
3535 tasklet_schedule(&instance->isr_tasklet);
3536 return IRQ_HANDLED;
3539 * megasas_isr - isr entry point
3541 static irqreturn_t megasas_isr(int irq, void *devp)
3543 struct megasas_irq_context *irq_context = devp;
3544 struct megasas_instance *instance = irq_context->instance;
3545 unsigned long flags;
3546 irqreturn_t rc;
3548 if (atomic_read(&instance->fw_reset_no_pci_access))
3549 return IRQ_HANDLED;
3551 spin_lock_irqsave(&instance->hba_lock, flags);
3552 rc = megasas_deplete_reply_queue(instance, DID_OK);
3553 spin_unlock_irqrestore(&instance->hba_lock, flags);
3555 return rc;
3559 * megasas_transition_to_ready - Move the FW to READY state
3560 * @instance: Adapter soft state
3562 * During the initialization, FW passes can potentially be in any one of
3563 * several possible states. If the FW in operational, waiting-for-handshake
3564 * states, driver must take steps to bring it to ready state. Otherwise, it
3565 * has to wait for the ready state.
3568 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3570 int i;
3571 u8 max_wait;
3572 u32 fw_state;
3573 u32 cur_state;
3574 u32 abs_state, curr_abs_state;
3576 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3577 fw_state = abs_state & MFI_STATE_MASK;
3579 if (fw_state != MFI_STATE_READY)
3580 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3581 " state\n");
3583 while (fw_state != MFI_STATE_READY) {
3585 switch (fw_state) {
3587 case MFI_STATE_FAULT:
3588 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3589 if (ocr) {
3590 max_wait = MEGASAS_RESET_WAIT_TIME;
3591 cur_state = MFI_STATE_FAULT;
3592 break;
3593 } else
3594 return -ENODEV;
3596 case MFI_STATE_WAIT_HANDSHAKE:
3598 * Set the CLR bit in inbound doorbell
3600 if ((instance->pdev->device ==
3601 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3602 (instance->pdev->device ==
3603 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3604 (instance->ctrl_context))
3605 writel(
3606 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3607 &instance->reg_set->doorbell);
3608 else
3609 writel(
3610 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3611 &instance->reg_set->inbound_doorbell);
3613 max_wait = MEGASAS_RESET_WAIT_TIME;
3614 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3615 break;
3617 case MFI_STATE_BOOT_MESSAGE_PENDING:
3618 if ((instance->pdev->device ==
3619 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3620 (instance->pdev->device ==
3621 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3622 (instance->ctrl_context))
3623 writel(MFI_INIT_HOTPLUG,
3624 &instance->reg_set->doorbell);
3625 else
3626 writel(MFI_INIT_HOTPLUG,
3627 &instance->reg_set->inbound_doorbell);
3629 max_wait = MEGASAS_RESET_WAIT_TIME;
3630 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3631 break;
3633 case MFI_STATE_OPERATIONAL:
3635 * Bring it to READY state; assuming max wait 10 secs
3637 instance->instancet->disable_intr(instance);
3638 if ((instance->pdev->device ==
3639 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3640 (instance->pdev->device ==
3641 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3642 (instance->ctrl_context)) {
3643 writel(MFI_RESET_FLAGS,
3644 &instance->reg_set->doorbell);
3646 if (instance->ctrl_context) {
3647 for (i = 0; i < (10 * 1000); i += 20) {
3648 if (readl(
3649 &instance->
3650 reg_set->
3651 doorbell) & 1)
3652 msleep(20);
3653 else
3654 break;
3657 } else
3658 writel(MFI_RESET_FLAGS,
3659 &instance->reg_set->inbound_doorbell);
3661 max_wait = MEGASAS_RESET_WAIT_TIME;
3662 cur_state = MFI_STATE_OPERATIONAL;
3663 break;
3665 case MFI_STATE_UNDEFINED:
3667 * This state should not last for more than 2 seconds
3669 max_wait = MEGASAS_RESET_WAIT_TIME;
3670 cur_state = MFI_STATE_UNDEFINED;
3671 break;
3673 case MFI_STATE_BB_INIT:
3674 max_wait = MEGASAS_RESET_WAIT_TIME;
3675 cur_state = MFI_STATE_BB_INIT;
3676 break;
3678 case MFI_STATE_FW_INIT:
3679 max_wait = MEGASAS_RESET_WAIT_TIME;
3680 cur_state = MFI_STATE_FW_INIT;
3681 break;
3683 case MFI_STATE_FW_INIT_2:
3684 max_wait = MEGASAS_RESET_WAIT_TIME;
3685 cur_state = MFI_STATE_FW_INIT_2;
3686 break;
3688 case MFI_STATE_DEVICE_SCAN:
3689 max_wait = MEGASAS_RESET_WAIT_TIME;
3690 cur_state = MFI_STATE_DEVICE_SCAN;
3691 break;
3693 case MFI_STATE_FLUSH_CACHE:
3694 max_wait = MEGASAS_RESET_WAIT_TIME;
3695 cur_state = MFI_STATE_FLUSH_CACHE;
3696 break;
3698 default:
3699 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3700 fw_state);
3701 return -ENODEV;
3705 * The cur_state should not last for more than max_wait secs
3707 for (i = 0; i < (max_wait * 1000); i++) {
3708 curr_abs_state = instance->instancet->
3709 read_fw_status_reg(instance->reg_set);
3711 if (abs_state == curr_abs_state) {
3712 msleep(1);
3713 } else
3714 break;
3718 * Return error if fw_state hasn't changed after max_wait
3720 if (curr_abs_state == abs_state) {
3721 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3722 "in %d secs\n", fw_state, max_wait);
3723 return -ENODEV;
3726 abs_state = curr_abs_state;
3727 fw_state = curr_abs_state & MFI_STATE_MASK;
3729 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3731 return 0;
3735 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3736 * @instance: Adapter soft state
3738 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3740 int i;
3741 u32 max_cmd = instance->max_mfi_cmds;
3742 struct megasas_cmd *cmd;
3744 if (!instance->frame_dma_pool)
3745 return;
3748 * Return all frames to pool
3750 for (i = 0; i < max_cmd; i++) {
3752 cmd = instance->cmd_list[i];
3754 if (cmd->frame)
3755 pci_pool_free(instance->frame_dma_pool, cmd->frame,
3756 cmd->frame_phys_addr);
3758 if (cmd->sense)
3759 pci_pool_free(instance->sense_dma_pool, cmd->sense,
3760 cmd->sense_phys_addr);
3764 * Now destroy the pool itself
3766 pci_pool_destroy(instance->frame_dma_pool);
3767 pci_pool_destroy(instance->sense_dma_pool);
3769 instance->frame_dma_pool = NULL;
3770 instance->sense_dma_pool = NULL;
3774 * megasas_create_frame_pool - Creates DMA pool for cmd frames
3775 * @instance: Adapter soft state
3777 * Each command packet has an embedded DMA memory buffer that is used for
3778 * filling MFI frame and the SG list that immediately follows the frame. This
3779 * function creates those DMA memory buffers for each command packet by using
3780 * PCI pool facility.
3782 static int megasas_create_frame_pool(struct megasas_instance *instance)
3784 int i;
3785 u32 max_cmd;
3786 u32 sge_sz;
3787 u32 total_sz;
3788 u32 frame_count;
3789 struct megasas_cmd *cmd;
3791 max_cmd = instance->max_mfi_cmds;
3794 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3795 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3797 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3798 sizeof(struct megasas_sge32);
3800 if (instance->flag_ieee)
3801 sge_sz = sizeof(struct megasas_sge_skinny);
3804 * For MFI controllers.
3805 * max_num_sge = 60
3806 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
3807 * Total 960 byte (15 MFI frame of 64 byte)
3809 * Fusion adapter require only 3 extra frame.
3810 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3811 * max_sge_sz = 12 byte (sizeof megasas_sge64)
3812 * Total 192 byte (3 MFI frame of 64 byte)
3814 frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3815 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3817 * Use DMA pool facility provided by PCI layer
3819 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3820 instance->pdev, total_sz, 256, 0);
3822 if (!instance->frame_dma_pool) {
3823 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3824 return -ENOMEM;
3827 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3828 instance->pdev, 128, 4, 0);
3830 if (!instance->sense_dma_pool) {
3831 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3833 pci_pool_destroy(instance->frame_dma_pool);
3834 instance->frame_dma_pool = NULL;
3836 return -ENOMEM;
3840 * Allocate and attach a frame to each of the commands in cmd_list.
3841 * By making cmd->index as the context instead of the &cmd, we can
3842 * always use 32bit context regardless of the architecture
3844 for (i = 0; i < max_cmd; i++) {
3846 cmd = instance->cmd_list[i];
3848 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3849 GFP_KERNEL, &cmd->frame_phys_addr);
3851 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3852 GFP_KERNEL, &cmd->sense_phys_addr);
3855 * megasas_teardown_frame_pool() takes care of freeing
3856 * whatever has been allocated
3858 if (!cmd->frame || !cmd->sense) {
3859 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3860 megasas_teardown_frame_pool(instance);
3861 return -ENOMEM;
3864 memset(cmd->frame, 0, total_sz);
3865 cmd->frame->io.context = cpu_to_le32(cmd->index);
3866 cmd->frame->io.pad_0 = 0;
3867 if (!instance->ctrl_context && reset_devices)
3868 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3871 return 0;
3875 * megasas_free_cmds - Free all the cmds in the free cmd pool
3876 * @instance: Adapter soft state
3878 void megasas_free_cmds(struct megasas_instance *instance)
3880 int i;
3882 /* First free the MFI frame pool */
3883 megasas_teardown_frame_pool(instance);
3885 /* Free all the commands in the cmd_list */
3886 for (i = 0; i < instance->max_mfi_cmds; i++)
3888 kfree(instance->cmd_list[i]);
3890 /* Free the cmd_list buffer itself */
3891 kfree(instance->cmd_list);
3892 instance->cmd_list = NULL;
3894 INIT_LIST_HEAD(&instance->cmd_pool);
3898 * megasas_alloc_cmds - Allocates the command packets
3899 * @instance: Adapter soft state
3901 * Each command that is issued to the FW, whether IO commands from the OS or
3902 * internal commands like IOCTLs, are wrapped in local data structure called
3903 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3904 * the FW.
3906 * Each frame has a 32-bit field called context (tag). This context is used
3907 * to get back the megasas_cmd from the frame when a frame gets completed in
3908 * the ISR. Typically the address of the megasas_cmd itself would be used as
3909 * the context. But we wanted to keep the differences between 32 and 64 bit
3910 * systems to the mininum. We always use 32 bit integers for the context. In
3911 * this driver, the 32 bit values are the indices into an array cmd_list.
3912 * This array is used only to look up the megasas_cmd given the context. The
3913 * free commands themselves are maintained in a linked list called cmd_pool.
3915 int megasas_alloc_cmds(struct megasas_instance *instance)
3917 int i;
3918 int j;
3919 u32 max_cmd;
3920 struct megasas_cmd *cmd;
3921 struct fusion_context *fusion;
3923 fusion = instance->ctrl_context;
3924 max_cmd = instance->max_mfi_cmds;
3927 * instance->cmd_list is an array of struct megasas_cmd pointers.
3928 * Allocate the dynamic array first and then allocate individual
3929 * commands.
3931 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3933 if (!instance->cmd_list) {
3934 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3935 return -ENOMEM;
3938 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3940 for (i = 0; i < max_cmd; i++) {
3941 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3942 GFP_KERNEL);
3944 if (!instance->cmd_list[i]) {
3946 for (j = 0; j < i; j++)
3947 kfree(instance->cmd_list[j]);
3949 kfree(instance->cmd_list);
3950 instance->cmd_list = NULL;
3952 return -ENOMEM;
3956 for (i = 0; i < max_cmd; i++) {
3957 cmd = instance->cmd_list[i];
3958 memset(cmd, 0, sizeof(struct megasas_cmd));
3959 cmd->index = i;
3960 cmd->scmd = NULL;
3961 cmd->instance = instance;
3963 list_add_tail(&cmd->list, &instance->cmd_pool);
3967 * Create a frame pool and assign one frame to each cmd
3969 if (megasas_create_frame_pool(instance)) {
3970 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3971 megasas_free_cmds(instance);
3974 return 0;
3978 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
3979 * @instance: Adapter soft state
3981 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
3982 * or FW is not under OCR.
3984 inline int
3985 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
3987 if (!instance->ctrl_context)
3988 return KILL_ADAPTER;
3989 else if (instance->unload ||
3990 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
3991 return IGNORE_TIMEOUT;
3992 else
3993 return INITIATE_OCR;
3996 static int
3997 megasas_get_pd_info(struct megasas_instance *instance, u16 device_id)
3999 int ret;
4000 struct megasas_cmd *cmd;
4001 struct megasas_dcmd_frame *dcmd;
4003 cmd = megasas_get_cmd(instance);
4005 if (!cmd) {
4006 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4007 return -ENOMEM;
4010 dcmd = &cmd->frame->dcmd;
4012 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4013 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4015 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4016 dcmd->cmd = MFI_CMD_DCMD;
4017 dcmd->cmd_status = 0xFF;
4018 dcmd->sge_count = 1;
4019 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4020 dcmd->timeout = 0;
4021 dcmd->pad_0 = 0;
4022 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4023 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4024 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->pd_info_h);
4025 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_PD_INFO));
4027 if (instance->ctrl_context && !instance->mask_interrupts)
4028 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4029 else
4030 ret = megasas_issue_polled(instance, cmd);
4032 switch (ret) {
4033 case DCMD_SUCCESS:
4034 instance->pd_list[device_id].interface =
4035 instance->pd_info->state.ddf.pdType.intf;
4036 break;
4038 case DCMD_TIMEOUT:
4040 switch (dcmd_timeout_ocr_possible(instance)) {
4041 case INITIATE_OCR:
4042 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4043 megasas_reset_fusion(instance->host,
4044 MFI_IO_TIMEOUT_OCR);
4045 break;
4046 case KILL_ADAPTER:
4047 megaraid_sas_kill_hba(instance);
4048 break;
4049 case IGNORE_TIMEOUT:
4050 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4051 __func__, __LINE__);
4052 break;
4055 break;
4058 if (ret != DCMD_TIMEOUT)
4059 megasas_return_cmd(instance, cmd);
4061 return ret;
4064 * megasas_get_pd_list_info - Returns FW's pd_list structure
4065 * @instance: Adapter soft state
4066 * @pd_list: pd_list structure
4068 * Issues an internal command (DCMD) to get the FW's controller PD
4069 * list structure. This information is mainly used to find out SYSTEM
4070 * supported by the FW.
4072 static int
4073 megasas_get_pd_list(struct megasas_instance *instance)
4075 int ret = 0, pd_index = 0;
4076 struct megasas_cmd *cmd;
4077 struct megasas_dcmd_frame *dcmd;
4078 struct MR_PD_LIST *ci;
4079 struct MR_PD_ADDRESS *pd_addr;
4080 dma_addr_t ci_h = 0;
4082 cmd = megasas_get_cmd(instance);
4084 if (!cmd) {
4085 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4086 return -ENOMEM;
4089 dcmd = &cmd->frame->dcmd;
4091 ci = pci_alloc_consistent(instance->pdev,
4092 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
4094 if (!ci) {
4095 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
4096 megasas_return_cmd(instance, cmd);
4097 return -ENOMEM;
4100 memset(ci, 0, sizeof(*ci));
4101 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4103 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4104 dcmd->mbox.b[1] = 0;
4105 dcmd->cmd = MFI_CMD_DCMD;
4106 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4107 dcmd->sge_count = 1;
4108 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4109 dcmd->timeout = 0;
4110 dcmd->pad_0 = 0;
4111 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4112 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4113 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4114 dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4116 if (instance->ctrl_context && !instance->mask_interrupts)
4117 ret = megasas_issue_blocked_cmd(instance, cmd,
4118 MFI_IO_TIMEOUT_SECS);
4119 else
4120 ret = megasas_issue_polled(instance, cmd);
4122 switch (ret) {
4123 case DCMD_FAILED:
4124 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4125 "failed/not supported by firmware\n");
4127 if (instance->ctrl_context)
4128 megaraid_sas_kill_hba(instance);
4129 else
4130 instance->pd_list_not_supported = 1;
4131 break;
4132 case DCMD_TIMEOUT:
4134 switch (dcmd_timeout_ocr_possible(instance)) {
4135 case INITIATE_OCR:
4136 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4138 * DCMD failed from AEN path.
4139 * AEN path already hold reset_mutex to avoid PCI access
4140 * while OCR is in progress.
4142 mutex_unlock(&instance->reset_mutex);
4143 megasas_reset_fusion(instance->host,
4144 MFI_IO_TIMEOUT_OCR);
4145 mutex_lock(&instance->reset_mutex);
4146 break;
4147 case KILL_ADAPTER:
4148 megaraid_sas_kill_hba(instance);
4149 break;
4150 case IGNORE_TIMEOUT:
4151 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4152 __func__, __LINE__);
4153 break;
4156 break;
4158 case DCMD_SUCCESS:
4159 pd_addr = ci->addr;
4161 if ((le32_to_cpu(ci->count) >
4162 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4163 break;
4165 memset(instance->local_pd_list, 0,
4166 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4168 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4169 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4170 le16_to_cpu(pd_addr->deviceId);
4171 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4172 pd_addr->scsiDevType;
4173 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4174 MR_PD_STATE_SYSTEM;
4175 pd_addr++;
4178 memcpy(instance->pd_list, instance->local_pd_list,
4179 sizeof(instance->pd_list));
4180 break;
4184 pci_free_consistent(instance->pdev,
4185 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
4186 ci, ci_h);
4188 if (ret != DCMD_TIMEOUT)
4189 megasas_return_cmd(instance, cmd);
4191 return ret;
4195 * megasas_get_ld_list_info - Returns FW's ld_list structure
4196 * @instance: Adapter soft state
4197 * @ld_list: ld_list structure
4199 * Issues an internal command (DCMD) to get the FW's controller PD
4200 * list structure. This information is mainly used to find out SYSTEM
4201 * supported by the FW.
4203 static int
4204 megasas_get_ld_list(struct megasas_instance *instance)
4206 int ret = 0, ld_index = 0, ids = 0;
4207 struct megasas_cmd *cmd;
4208 struct megasas_dcmd_frame *dcmd;
4209 struct MR_LD_LIST *ci;
4210 dma_addr_t ci_h = 0;
4211 u32 ld_count;
4213 cmd = megasas_get_cmd(instance);
4215 if (!cmd) {
4216 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4217 return -ENOMEM;
4220 dcmd = &cmd->frame->dcmd;
4222 ci = pci_alloc_consistent(instance->pdev,
4223 sizeof(struct MR_LD_LIST),
4224 &ci_h);
4226 if (!ci) {
4227 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
4228 megasas_return_cmd(instance, cmd);
4229 return -ENOMEM;
4232 memset(ci, 0, sizeof(*ci));
4233 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4235 if (instance->supportmax256vd)
4236 dcmd->mbox.b[0] = 1;
4237 dcmd->cmd = MFI_CMD_DCMD;
4238 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4239 dcmd->sge_count = 1;
4240 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4241 dcmd->timeout = 0;
4242 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4243 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4244 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4245 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4246 dcmd->pad_0 = 0;
4248 if (instance->ctrl_context && !instance->mask_interrupts)
4249 ret = megasas_issue_blocked_cmd(instance, cmd,
4250 MFI_IO_TIMEOUT_SECS);
4251 else
4252 ret = megasas_issue_polled(instance, cmd);
4254 ld_count = le32_to_cpu(ci->ldCount);
4256 switch (ret) {
4257 case DCMD_FAILED:
4258 megaraid_sas_kill_hba(instance);
4259 break;
4260 case DCMD_TIMEOUT:
4262 switch (dcmd_timeout_ocr_possible(instance)) {
4263 case INITIATE_OCR:
4264 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4266 * DCMD failed from AEN path.
4267 * AEN path already hold reset_mutex to avoid PCI access
4268 * while OCR is in progress.
4270 mutex_unlock(&instance->reset_mutex);
4271 megasas_reset_fusion(instance->host,
4272 MFI_IO_TIMEOUT_OCR);
4273 mutex_lock(&instance->reset_mutex);
4274 break;
4275 case KILL_ADAPTER:
4276 megaraid_sas_kill_hba(instance);
4277 break;
4278 case IGNORE_TIMEOUT:
4279 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4280 __func__, __LINE__);
4281 break;
4284 break;
4286 case DCMD_SUCCESS:
4287 if (ld_count > instance->fw_supported_vd_count)
4288 break;
4290 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4292 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4293 if (ci->ldList[ld_index].state != 0) {
4294 ids = ci->ldList[ld_index].ref.targetId;
4295 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4299 break;
4302 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_LIST), ci, ci_h);
4304 if (ret != DCMD_TIMEOUT)
4305 megasas_return_cmd(instance, cmd);
4307 return ret;
4311 * megasas_ld_list_query - Returns FW's ld_list structure
4312 * @instance: Adapter soft state
4313 * @ld_list: ld_list structure
4315 * Issues an internal command (DCMD) to get the FW's controller PD
4316 * list structure. This information is mainly used to find out SYSTEM
4317 * supported by the FW.
4319 static int
4320 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4322 int ret = 0, ld_index = 0, ids = 0;
4323 struct megasas_cmd *cmd;
4324 struct megasas_dcmd_frame *dcmd;
4325 struct MR_LD_TARGETID_LIST *ci;
4326 dma_addr_t ci_h = 0;
4327 u32 tgtid_count;
4329 cmd = megasas_get_cmd(instance);
4331 if (!cmd) {
4332 dev_warn(&instance->pdev->dev,
4333 "megasas_ld_list_query: Failed to get cmd\n");
4334 return -ENOMEM;
4337 dcmd = &cmd->frame->dcmd;
4339 ci = pci_alloc_consistent(instance->pdev,
4340 sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4342 if (!ci) {
4343 dev_warn(&instance->pdev->dev,
4344 "Failed to alloc mem for ld_list_query\n");
4345 megasas_return_cmd(instance, cmd);
4346 return -ENOMEM;
4349 memset(ci, 0, sizeof(*ci));
4350 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4352 dcmd->mbox.b[0] = query_type;
4353 if (instance->supportmax256vd)
4354 dcmd->mbox.b[2] = 1;
4356 dcmd->cmd = MFI_CMD_DCMD;
4357 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4358 dcmd->sge_count = 1;
4359 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4360 dcmd->timeout = 0;
4361 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4362 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4363 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4364 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4365 dcmd->pad_0 = 0;
4367 if (instance->ctrl_context && !instance->mask_interrupts)
4368 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4369 else
4370 ret = megasas_issue_polled(instance, cmd);
4372 switch (ret) {
4373 case DCMD_FAILED:
4374 dev_info(&instance->pdev->dev,
4375 "DCMD not supported by firmware - %s %d\n",
4376 __func__, __LINE__);
4377 ret = megasas_get_ld_list(instance);
4378 break;
4379 case DCMD_TIMEOUT:
4380 switch (dcmd_timeout_ocr_possible(instance)) {
4381 case INITIATE_OCR:
4382 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4384 * DCMD failed from AEN path.
4385 * AEN path already hold reset_mutex to avoid PCI access
4386 * while OCR is in progress.
4388 mutex_unlock(&instance->reset_mutex);
4389 megasas_reset_fusion(instance->host,
4390 MFI_IO_TIMEOUT_OCR);
4391 mutex_lock(&instance->reset_mutex);
4392 break;
4393 case KILL_ADAPTER:
4394 megaraid_sas_kill_hba(instance);
4395 break;
4396 case IGNORE_TIMEOUT:
4397 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4398 __func__, __LINE__);
4399 break;
4402 break;
4403 case DCMD_SUCCESS:
4404 tgtid_count = le32_to_cpu(ci->count);
4406 if ((tgtid_count > (instance->fw_supported_vd_count)))
4407 break;
4409 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4410 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4411 ids = ci->targetId[ld_index];
4412 instance->ld_ids[ids] = ci->targetId[ld_index];
4415 break;
4418 pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4419 ci, ci_h);
4421 if (ret != DCMD_TIMEOUT)
4422 megasas_return_cmd(instance, cmd);
4424 return ret;
4428 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4429 * instance : Controller's instance
4431 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4433 struct fusion_context *fusion;
4434 u32 old_map_sz;
4435 u32 new_map_sz;
4437 fusion = instance->ctrl_context;
4438 /* For MFI based controllers return dummy success */
4439 if (!fusion)
4440 return;
4442 instance->supportmax256vd =
4443 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4444 /* Below is additional check to address future FW enhancement */
4445 if (instance->ctrl_info->max_lds > 64)
4446 instance->supportmax256vd = 1;
4448 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4449 * MEGASAS_MAX_DEV_PER_CHANNEL;
4450 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4451 * MEGASAS_MAX_DEV_PER_CHANNEL;
4452 if (instance->supportmax256vd) {
4453 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4454 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4455 } else {
4456 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4457 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4460 dev_info(&instance->pdev->dev,
4461 "firmware type\t: %s\n",
4462 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4463 "Legacy(64 VD) firmware");
4465 old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4466 (sizeof(struct MR_LD_SPAN_MAP) *
4467 (instance->fw_supported_vd_count - 1));
4468 new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4469 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4470 (sizeof(struct MR_LD_SPAN_MAP) *
4471 (instance->drv_supported_vd_count - 1));
4473 fusion->max_map_sz = max(old_map_sz, new_map_sz);
4476 if (instance->supportmax256vd)
4477 fusion->current_map_sz = new_map_sz;
4478 else
4479 fusion->current_map_sz = old_map_sz;
4483 * megasas_get_controller_info - Returns FW's controller structure
4484 * @instance: Adapter soft state
4486 * Issues an internal command (DCMD) to get the FW's controller structure.
4487 * This information is mainly used to find out the maximum IO transfer per
4488 * command supported by the FW.
4491 megasas_get_ctrl_info(struct megasas_instance *instance)
4493 int ret = 0;
4494 struct megasas_cmd *cmd;
4495 struct megasas_dcmd_frame *dcmd;
4496 struct megasas_ctrl_info *ci;
4497 struct megasas_ctrl_info *ctrl_info;
4498 dma_addr_t ci_h = 0;
4500 ctrl_info = instance->ctrl_info;
4502 cmd = megasas_get_cmd(instance);
4504 if (!cmd) {
4505 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4506 return -ENOMEM;
4509 dcmd = &cmd->frame->dcmd;
4511 ci = pci_alloc_consistent(instance->pdev,
4512 sizeof(struct megasas_ctrl_info), &ci_h);
4514 if (!ci) {
4515 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4516 megasas_return_cmd(instance, cmd);
4517 return -ENOMEM;
4520 memset(ci, 0, sizeof(*ci));
4521 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4523 dcmd->cmd = MFI_CMD_DCMD;
4524 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4525 dcmd->sge_count = 1;
4526 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4527 dcmd->timeout = 0;
4528 dcmd->pad_0 = 0;
4529 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4530 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4531 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4532 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4533 dcmd->mbox.b[0] = 1;
4535 if (instance->ctrl_context && !instance->mask_interrupts)
4536 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4537 else
4538 ret = megasas_issue_polled(instance, cmd);
4540 switch (ret) {
4541 case DCMD_SUCCESS:
4542 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4543 /* Save required controller information in
4544 * CPU endianness format.
4546 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4547 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4548 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4550 /* Update the latest Ext VD info.
4551 * From Init path, store current firmware details.
4552 * From OCR path, detect any firmware properties changes.
4553 * in case of Firmware upgrade without system reboot.
4555 megasas_update_ext_vd_details(instance);
4556 instance->use_seqnum_jbod_fp =
4557 ctrl_info->adapterOperations3.useSeqNumJbodFP;
4559 /*Check whether controller is iMR or MR */
4560 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4561 dev_info(&instance->pdev->dev,
4562 "controller type\t: %s(%dMB)\n",
4563 instance->is_imr ? "iMR" : "MR",
4564 le16_to_cpu(ctrl_info->memory_size));
4566 instance->disableOnlineCtrlReset =
4567 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4568 instance->secure_jbod_support =
4569 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4570 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4571 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4572 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4573 instance->secure_jbod_support ? "Yes" : "No");
4574 break;
4576 case DCMD_TIMEOUT:
4577 switch (dcmd_timeout_ocr_possible(instance)) {
4578 case INITIATE_OCR:
4579 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4580 megasas_reset_fusion(instance->host,
4581 MFI_IO_TIMEOUT_OCR);
4582 break;
4583 case KILL_ADAPTER:
4584 megaraid_sas_kill_hba(instance);
4585 break;
4586 case IGNORE_TIMEOUT:
4587 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4588 __func__, __LINE__);
4589 break;
4591 case DCMD_FAILED:
4592 megaraid_sas_kill_hba(instance);
4593 break;
4597 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4598 ci, ci_h);
4600 megasas_return_cmd(instance, cmd);
4603 return ret;
4607 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
4608 * to firmware
4610 * @instance: Adapter soft state
4611 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
4612 MR_CRASH_BUF_TURN_OFF = 0
4613 MR_CRASH_BUF_TURN_ON = 1
4614 * @return 0 on success non-zero on failure.
4615 * Issues an internal command (DCMD) to set parameters for crash dump feature.
4616 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4617 * that driver supports crash dump feature. This DCMD will be sent only if
4618 * crash dump feature is supported by the FW.
4621 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4622 u8 crash_buf_state)
4624 int ret = 0;
4625 struct megasas_cmd *cmd;
4626 struct megasas_dcmd_frame *dcmd;
4628 cmd = megasas_get_cmd(instance);
4630 if (!cmd) {
4631 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4632 return -ENOMEM;
4636 dcmd = &cmd->frame->dcmd;
4638 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4639 dcmd->mbox.b[0] = crash_buf_state;
4640 dcmd->cmd = MFI_CMD_DCMD;
4641 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4642 dcmd->sge_count = 1;
4643 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4644 dcmd->timeout = 0;
4645 dcmd->pad_0 = 0;
4646 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4647 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4648 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4649 dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4651 if (instance->ctrl_context && !instance->mask_interrupts)
4652 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4653 else
4654 ret = megasas_issue_polled(instance, cmd);
4656 if (ret == DCMD_TIMEOUT) {
4657 switch (dcmd_timeout_ocr_possible(instance)) {
4658 case INITIATE_OCR:
4659 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4660 megasas_reset_fusion(instance->host,
4661 MFI_IO_TIMEOUT_OCR);
4662 break;
4663 case KILL_ADAPTER:
4664 megaraid_sas_kill_hba(instance);
4665 break;
4666 case IGNORE_TIMEOUT:
4667 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4668 __func__, __LINE__);
4669 break;
4671 } else
4672 megasas_return_cmd(instance, cmd);
4674 return ret;
4678 * megasas_issue_init_mfi - Initializes the FW
4679 * @instance: Adapter soft state
4681 * Issues the INIT MFI cmd
4683 static int
4684 megasas_issue_init_mfi(struct megasas_instance *instance)
4686 __le32 context;
4687 struct megasas_cmd *cmd;
4688 struct megasas_init_frame *init_frame;
4689 struct megasas_init_queue_info *initq_info;
4690 dma_addr_t init_frame_h;
4691 dma_addr_t initq_info_h;
4694 * Prepare a init frame. Note the init frame points to queue info
4695 * structure. Each frame has SGL allocated after first 64 bytes. For
4696 * this frame - since we don't need any SGL - we use SGL's space as
4697 * queue info structure
4699 * We will not get a NULL command below. We just created the pool.
4701 cmd = megasas_get_cmd(instance);
4703 init_frame = (struct megasas_init_frame *)cmd->frame;
4704 initq_info = (struct megasas_init_queue_info *)
4705 ((unsigned long)init_frame + 64);
4707 init_frame_h = cmd->frame_phys_addr;
4708 initq_info_h = init_frame_h + 64;
4710 context = init_frame->context;
4711 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4712 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4713 init_frame->context = context;
4715 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4716 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4718 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4719 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4721 init_frame->cmd = MFI_CMD_INIT;
4722 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4723 init_frame->queue_info_new_phys_addr_lo =
4724 cpu_to_le32(lower_32_bits(initq_info_h));
4725 init_frame->queue_info_new_phys_addr_hi =
4726 cpu_to_le32(upper_32_bits(initq_info_h));
4728 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4731 * disable the intr before firing the init frame to FW
4733 instance->instancet->disable_intr(instance);
4736 * Issue the init frame in polled mode
4739 if (megasas_issue_polled(instance, cmd)) {
4740 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4741 megasas_return_cmd(instance, cmd);
4742 goto fail_fw_init;
4745 megasas_return_cmd(instance, cmd);
4747 return 0;
4749 fail_fw_init:
4750 return -EINVAL;
4753 static u32
4754 megasas_init_adapter_mfi(struct megasas_instance *instance)
4756 struct megasas_register_set __iomem *reg_set;
4757 u32 context_sz;
4758 u32 reply_q_sz;
4760 reg_set = instance->reg_set;
4763 * Get various operational parameters from status register
4765 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4767 * Reduce the max supported cmds by 1. This is to ensure that the
4768 * reply_q_sz (1 more than the max cmd that driver may send)
4769 * does not exceed max cmds that the FW can support
4771 instance->max_fw_cmds = instance->max_fw_cmds-1;
4772 instance->max_mfi_cmds = instance->max_fw_cmds;
4773 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4774 0x10;
4776 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4777 * are reserved for IOCTL + driver's internal DCMDs.
4779 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4780 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4781 instance->max_scsi_cmds = (instance->max_fw_cmds -
4782 MEGASAS_SKINNY_INT_CMDS);
4783 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4784 } else {
4785 instance->max_scsi_cmds = (instance->max_fw_cmds -
4786 MEGASAS_INT_CMDS);
4787 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4790 instance->cur_can_queue = instance->max_scsi_cmds;
4792 * Create a pool of commands
4794 if (megasas_alloc_cmds(instance))
4795 goto fail_alloc_cmds;
4798 * Allocate memory for reply queue. Length of reply queue should
4799 * be _one_ more than the maximum commands handled by the firmware.
4801 * Note: When FW completes commands, it places corresponding contex
4802 * values in this circular reply queue. This circular queue is a fairly
4803 * typical producer-consumer queue. FW is the producer (of completed
4804 * commands) and the driver is the consumer.
4806 context_sz = sizeof(u32);
4807 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4809 instance->reply_queue = pci_alloc_consistent(instance->pdev,
4810 reply_q_sz,
4811 &instance->reply_queue_h);
4813 if (!instance->reply_queue) {
4814 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4815 goto fail_reply_queue;
4818 if (megasas_issue_init_mfi(instance))
4819 goto fail_fw_init;
4821 if (megasas_get_ctrl_info(instance)) {
4822 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4823 "Fail from %s %d\n", instance->unique_id,
4824 __func__, __LINE__);
4825 goto fail_fw_init;
4828 instance->fw_support_ieee = 0;
4829 instance->fw_support_ieee =
4830 (instance->instancet->read_fw_status_reg(reg_set) &
4831 0x04000000);
4833 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4834 instance->fw_support_ieee);
4836 if (instance->fw_support_ieee)
4837 instance->flag_ieee = 1;
4839 return 0;
4841 fail_fw_init:
4843 pci_free_consistent(instance->pdev, reply_q_sz,
4844 instance->reply_queue, instance->reply_queue_h);
4845 fail_reply_queue:
4846 megasas_free_cmds(instance);
4848 fail_alloc_cmds:
4849 return 1;
4853 * megasas_setup_irqs_msix - register legacy interrupts.
4854 * @instance: Adapter soft state
4856 * Do not enable interrupt, only setup ISRs.
4858 * Return 0 on success.
4860 static int
4861 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4863 struct pci_dev *pdev;
4865 pdev = instance->pdev;
4866 instance->irq_context[0].instance = instance;
4867 instance->irq_context[0].MSIxIndex = 0;
4868 if (request_irq(pdev->irq, instance->instancet->service_isr,
4869 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4870 dev_err(&instance->pdev->dev,
4871 "Failed to register IRQ from %s %d\n",
4872 __func__, __LINE__);
4873 return -1;
4875 return 0;
4879 * megasas_setup_irqs_msix - register MSI-x interrupts.
4880 * @instance: Adapter soft state
4881 * @is_probe: Driver probe check
4883 * Do not enable interrupt, only setup ISRs.
4885 * Return 0 on success.
4887 static int
4888 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4890 int i, j, cpu;
4891 struct pci_dev *pdev;
4893 pdev = instance->pdev;
4895 /* Try MSI-x */
4896 cpu = cpumask_first(cpu_online_mask);
4897 for (i = 0; i < instance->msix_vectors; i++) {
4898 instance->irq_context[i].instance = instance;
4899 instance->irq_context[i].MSIxIndex = i;
4900 if (request_irq(instance->msixentry[i].vector,
4901 instance->instancet->service_isr, 0, "megasas",
4902 &instance->irq_context[i])) {
4903 dev_err(&instance->pdev->dev,
4904 "Failed to register IRQ for vector %d.\n", i);
4905 for (j = 0; j < i; j++) {
4906 if (smp_affinity_enable)
4907 irq_set_affinity_hint(
4908 instance->msixentry[j].vector, NULL);
4909 free_irq(instance->msixentry[j].vector,
4910 &instance->irq_context[j]);
4912 /* Retry irq register for IO_APIC*/
4913 instance->msix_vectors = 0;
4914 if (is_probe)
4915 return megasas_setup_irqs_ioapic(instance);
4916 else
4917 return -1;
4919 if (smp_affinity_enable) {
4920 if (irq_set_affinity_hint(instance->msixentry[i].vector,
4921 get_cpu_mask(cpu)))
4922 dev_err(&instance->pdev->dev,
4923 "Failed to set affinity hint"
4924 " for cpu %d\n", cpu);
4925 cpu = cpumask_next(cpu, cpu_online_mask);
4928 return 0;
4932 * megasas_destroy_irqs- unregister interrupts.
4933 * @instance: Adapter soft state
4934 * return: void
4936 static void
4937 megasas_destroy_irqs(struct megasas_instance *instance) {
4939 int i;
4941 if (instance->msix_vectors)
4942 for (i = 0; i < instance->msix_vectors; i++) {
4943 if (smp_affinity_enable)
4944 irq_set_affinity_hint(
4945 instance->msixentry[i].vector, NULL);
4946 free_irq(instance->msixentry[i].vector,
4947 &instance->irq_context[i]);
4949 else
4950 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4954 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
4955 * @instance: Adapter soft state
4956 * @is_probe: Driver probe check
4958 * Return 0 on success.
4960 void
4961 megasas_setup_jbod_map(struct megasas_instance *instance)
4963 int i;
4964 struct fusion_context *fusion = instance->ctrl_context;
4965 u32 pd_seq_map_sz;
4967 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4968 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4970 if (reset_devices || !fusion ||
4971 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4972 dev_info(&instance->pdev->dev,
4973 "Jbod map is not supported %s %d\n",
4974 __func__, __LINE__);
4975 instance->use_seqnum_jbod_fp = false;
4976 return;
4979 if (fusion->pd_seq_sync[0])
4980 goto skip_alloc;
4982 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4983 fusion->pd_seq_sync[i] = dma_alloc_coherent
4984 (&instance->pdev->dev, pd_seq_map_sz,
4985 &fusion->pd_seq_phys[i], GFP_KERNEL);
4986 if (!fusion->pd_seq_sync[i]) {
4987 dev_err(&instance->pdev->dev,
4988 "Failed to allocate memory from %s %d\n",
4989 __func__, __LINE__);
4990 if (i == 1) {
4991 dma_free_coherent(&instance->pdev->dev,
4992 pd_seq_map_sz, fusion->pd_seq_sync[0],
4993 fusion->pd_seq_phys[0]);
4994 fusion->pd_seq_sync[0] = NULL;
4996 instance->use_seqnum_jbod_fp = false;
4997 return;
5001 skip_alloc:
5002 if (!megasas_sync_pd_seq_num(instance, false) &&
5003 !megasas_sync_pd_seq_num(instance, true))
5004 instance->use_seqnum_jbod_fp = true;
5005 else
5006 instance->use_seqnum_jbod_fp = false;
5010 * megasas_init_fw - Initializes the FW
5011 * @instance: Adapter soft state
5013 * This is the main function for initializing firmware
5016 static int megasas_init_fw(struct megasas_instance *instance)
5018 u32 max_sectors_1;
5019 u32 max_sectors_2;
5020 u32 tmp_sectors, msix_enable, scratch_pad_2;
5021 resource_size_t base_addr;
5022 struct megasas_register_set __iomem *reg_set;
5023 struct megasas_ctrl_info *ctrl_info = NULL;
5024 unsigned long bar_list;
5025 int i, loop, fw_msix_count = 0;
5026 struct IOV_111 *iovPtr;
5027 struct fusion_context *fusion;
5029 fusion = instance->ctrl_context;
5031 /* Find first memory bar */
5032 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5033 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
5034 if (pci_request_selected_regions(instance->pdev, instance->bar,
5035 "megasas: LSI")) {
5036 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5037 return -EBUSY;
5040 base_addr = pci_resource_start(instance->pdev, instance->bar);
5041 instance->reg_set = ioremap_nocache(base_addr, 8192);
5043 if (!instance->reg_set) {
5044 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5045 goto fail_ioremap;
5048 reg_set = instance->reg_set;
5050 switch (instance->pdev->device) {
5051 case PCI_DEVICE_ID_LSI_FUSION:
5052 case PCI_DEVICE_ID_LSI_PLASMA:
5053 case PCI_DEVICE_ID_LSI_INVADER:
5054 case PCI_DEVICE_ID_LSI_FURY:
5055 case PCI_DEVICE_ID_LSI_INTRUDER:
5056 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5057 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5058 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5059 instance->instancet = &megasas_instance_template_fusion;
5060 break;
5061 case PCI_DEVICE_ID_LSI_SAS1078R:
5062 case PCI_DEVICE_ID_LSI_SAS1078DE:
5063 instance->instancet = &megasas_instance_template_ppc;
5064 break;
5065 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5066 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5067 instance->instancet = &megasas_instance_template_gen2;
5068 break;
5069 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5070 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5071 instance->instancet = &megasas_instance_template_skinny;
5072 break;
5073 case PCI_DEVICE_ID_LSI_SAS1064R:
5074 case PCI_DEVICE_ID_DELL_PERC5:
5075 default:
5076 instance->instancet = &megasas_instance_template_xscale;
5077 break;
5080 if (megasas_transition_to_ready(instance, 0)) {
5081 atomic_set(&instance->fw_reset_no_pci_access, 1);
5082 instance->instancet->adp_reset
5083 (instance, instance->reg_set);
5084 atomic_set(&instance->fw_reset_no_pci_access, 0);
5085 dev_info(&instance->pdev->dev,
5086 "FW restarted successfully from %s!\n",
5087 __func__);
5089 /*waitting for about 30 second before retry*/
5090 ssleep(30);
5092 if (megasas_transition_to_ready(instance, 0))
5093 goto fail_ready_state;
5097 * MSI-X host index 0 is common for all adapter.
5098 * It is used for all MPT based Adapters.
5100 instance->reply_post_host_index_addr[0] =
5101 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
5102 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5104 /* Check if MSI-X is supported while in ready state */
5105 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
5106 0x4000000) >> 0x1a;
5107 if (msix_enable && !msix_disable) {
5108 scratch_pad_2 = readl
5109 (&instance->reg_set->outbound_scratch_pad_2);
5110 /* Check max MSI-X vectors */
5111 if (fusion) {
5112 if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
5113 instance->msix_vectors = (scratch_pad_2
5114 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5115 fw_msix_count = instance->msix_vectors;
5116 } else { /* Invader series supports more than 8 MSI-x vectors*/
5117 instance->msix_vectors = ((scratch_pad_2
5118 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5119 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5120 if (rdpq_enable)
5121 instance->is_rdpq = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ?
5122 1 : 0;
5123 fw_msix_count = instance->msix_vectors;
5124 /* Save 1-15 reply post index address to local memory
5125 * Index 0 is already saved from reg offset
5126 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5128 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5129 instance->reply_post_host_index_addr[loop] =
5130 (u32 __iomem *)
5131 ((u8 __iomem *)instance->reg_set +
5132 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5133 + (loop * 0x10));
5136 if (msix_vectors)
5137 instance->msix_vectors = min(msix_vectors,
5138 instance->msix_vectors);
5139 } else /* MFI adapters */
5140 instance->msix_vectors = 1;
5141 /* Don't bother allocating more MSI-X vectors than cpus */
5142 instance->msix_vectors = min(instance->msix_vectors,
5143 (unsigned int)num_online_cpus());
5144 for (i = 0; i < instance->msix_vectors; i++)
5145 instance->msixentry[i].entry = i;
5146 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
5147 1, instance->msix_vectors);
5148 if (i > 0)
5149 instance->msix_vectors = i;
5150 else
5151 instance->msix_vectors = 0;
5154 dev_info(&instance->pdev->dev,
5155 "firmware supports msix\t: (%d)", fw_msix_count);
5156 dev_info(&instance->pdev->dev,
5157 "current msix/online cpus\t: (%d/%d)\n",
5158 instance->msix_vectors, (unsigned int)num_online_cpus());
5159 dev_info(&instance->pdev->dev,
5160 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5162 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5163 (unsigned long)instance);
5165 if (instance->msix_vectors ?
5166 megasas_setup_irqs_msix(instance, 1) :
5167 megasas_setup_irqs_ioapic(instance))
5168 goto fail_setup_irqs;
5170 instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
5171 GFP_KERNEL);
5172 if (instance->ctrl_info == NULL)
5173 goto fail_init_adapter;
5176 * Below are default value for legacy Firmware.
5177 * non-fusion based controllers
5179 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5180 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5181 /* Get operational params, sge flags, send init cmd to controller */
5182 if (instance->instancet->init_adapter(instance))
5183 goto fail_init_adapter;
5186 instance->instancet->enable_intr(instance);
5188 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5190 megasas_setup_jbod_map(instance);
5192 /** for passthrough
5193 * the following function will get the PD LIST.
5195 memset(instance->pd_list, 0,
5196 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5197 if (megasas_get_pd_list(instance) < 0) {
5198 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5199 goto fail_get_pd_list;
5202 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5203 if (megasas_ld_list_query(instance,
5204 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
5205 megasas_get_ld_list(instance);
5208 * Compute the max allowed sectors per IO: The controller info has two
5209 * limits on max sectors. Driver should use the minimum of these two.
5211 * 1 << stripe_sz_ops.min = max sectors per strip
5213 * Note that older firmwares ( < FW ver 30) didn't report information
5214 * to calculate max_sectors_1. So the number ended up as zero always.
5216 tmp_sectors = 0;
5217 ctrl_info = instance->ctrl_info;
5219 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5220 le16_to_cpu(ctrl_info->max_strips_per_io);
5221 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5223 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5225 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5226 instance->passive = ctrl_info->cluster.passive;
5227 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5228 instance->UnevenSpanSupport =
5229 ctrl_info->adapterOperations2.supportUnevenSpans;
5230 if (instance->UnevenSpanSupport) {
5231 struct fusion_context *fusion = instance->ctrl_context;
5232 if (MR_ValidateMapInfo(instance))
5233 fusion->fast_path_io = 1;
5234 else
5235 fusion->fast_path_io = 0;
5238 if (ctrl_info->host_interface.SRIOV) {
5239 instance->requestorId = ctrl_info->iov.requestorId;
5240 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5241 if (!ctrl_info->adapterOperations2.activePassive)
5242 instance->PlasmaFW111 = 1;
5244 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5245 instance->PlasmaFW111 ? "1.11" : "new");
5247 if (instance->PlasmaFW111) {
5248 iovPtr = (struct IOV_111 *)
5249 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5250 instance->requestorId = iovPtr->requestorId;
5253 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5254 instance->requestorId);
5257 instance->crash_dump_fw_support =
5258 ctrl_info->adapterOperations3.supportCrashDump;
5259 instance->crash_dump_drv_support =
5260 (instance->crash_dump_fw_support &&
5261 instance->crash_dump_buf);
5262 if (instance->crash_dump_drv_support)
5263 megasas_set_crash_dump_params(instance,
5264 MR_CRASH_BUF_TURN_OFF);
5266 else {
5267 if (instance->crash_dump_buf)
5268 pci_free_consistent(instance->pdev,
5269 CRASH_DMA_BUF_SIZE,
5270 instance->crash_dump_buf,
5271 instance->crash_dump_h);
5272 instance->crash_dump_buf = NULL;
5276 dev_info(&instance->pdev->dev,
5277 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5278 le16_to_cpu(ctrl_info->pci.vendor_id),
5279 le16_to_cpu(ctrl_info->pci.device_id),
5280 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5281 le16_to_cpu(ctrl_info->pci.sub_device_id));
5282 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5283 instance->UnevenSpanSupport ? "yes" : "no");
5284 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5285 instance->crash_dump_drv_support ? "yes" : "no");
5286 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5287 instance->use_seqnum_jbod_fp ? "yes" : "no");
5290 instance->max_sectors_per_req = instance->max_num_sge *
5291 SGE_BUFFER_SIZE / 512;
5292 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5293 instance->max_sectors_per_req = tmp_sectors;
5295 /* Check for valid throttlequeuedepth module parameter */
5296 if (throttlequeuedepth &&
5297 throttlequeuedepth <= instance->max_scsi_cmds)
5298 instance->throttlequeuedepth = throttlequeuedepth;
5299 else
5300 instance->throttlequeuedepth =
5301 MEGASAS_THROTTLE_QUEUE_DEPTH;
5303 if (resetwaittime > MEGASAS_RESET_WAIT_TIME)
5304 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5306 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5307 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5309 /* Launch SR-IOV heartbeat timer */
5310 if (instance->requestorId) {
5311 if (!megasas_sriov_start_heartbeat(instance, 1))
5312 megasas_start_timer(instance,
5313 &instance->sriov_heartbeat_timer,
5314 megasas_sriov_heartbeat_handler,
5315 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5316 else
5317 instance->skip_heartbeat_timer_del = 1;
5320 return 0;
5322 fail_get_pd_list:
5323 instance->instancet->disable_intr(instance);
5324 fail_init_adapter:
5325 megasas_destroy_irqs(instance);
5326 fail_setup_irqs:
5327 if (instance->msix_vectors)
5328 pci_disable_msix(instance->pdev);
5329 instance->msix_vectors = 0;
5330 fail_ready_state:
5331 kfree(instance->ctrl_info);
5332 instance->ctrl_info = NULL;
5333 iounmap(instance->reg_set);
5335 fail_ioremap:
5336 pci_release_selected_regions(instance->pdev, instance->bar);
5338 return -EINVAL;
5342 * megasas_release_mfi - Reverses the FW initialization
5343 * @instance: Adapter soft state
5345 static void megasas_release_mfi(struct megasas_instance *instance)
5347 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
5349 if (instance->reply_queue)
5350 pci_free_consistent(instance->pdev, reply_q_sz,
5351 instance->reply_queue, instance->reply_queue_h);
5353 megasas_free_cmds(instance);
5355 iounmap(instance->reg_set);
5357 pci_release_selected_regions(instance->pdev, instance->bar);
5361 * megasas_get_seq_num - Gets latest event sequence numbers
5362 * @instance: Adapter soft state
5363 * @eli: FW event log sequence numbers information
5365 * FW maintains a log of all events in a non-volatile area. Upper layers would
5366 * usually find out the latest sequence number of the events, the seq number at
5367 * the boot etc. They would "read" all the events below the latest seq number
5368 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
5369 * number), they would subsribe to AEN (asynchronous event notification) and
5370 * wait for the events to happen.
5372 static int
5373 megasas_get_seq_num(struct megasas_instance *instance,
5374 struct megasas_evt_log_info *eli)
5376 struct megasas_cmd *cmd;
5377 struct megasas_dcmd_frame *dcmd;
5378 struct megasas_evt_log_info *el_info;
5379 dma_addr_t el_info_h = 0;
5381 cmd = megasas_get_cmd(instance);
5383 if (!cmd) {
5384 return -ENOMEM;
5387 dcmd = &cmd->frame->dcmd;
5388 el_info = pci_alloc_consistent(instance->pdev,
5389 sizeof(struct megasas_evt_log_info),
5390 &el_info_h);
5392 if (!el_info) {
5393 megasas_return_cmd(instance, cmd);
5394 return -ENOMEM;
5397 memset(el_info, 0, sizeof(*el_info));
5398 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5400 dcmd->cmd = MFI_CMD_DCMD;
5401 dcmd->cmd_status = 0x0;
5402 dcmd->sge_count = 1;
5403 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5404 dcmd->timeout = 0;
5405 dcmd->pad_0 = 0;
5406 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5407 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5408 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5409 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5411 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS) ==
5412 DCMD_SUCCESS) {
5414 * Copy the data back into callers buffer
5416 eli->newest_seq_num = el_info->newest_seq_num;
5417 eli->oldest_seq_num = el_info->oldest_seq_num;
5418 eli->clear_seq_num = el_info->clear_seq_num;
5419 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5420 eli->boot_seq_num = el_info->boot_seq_num;
5421 } else
5422 dev_err(&instance->pdev->dev, "DCMD failed "
5423 "from %s\n", __func__);
5425 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5426 el_info, el_info_h);
5428 megasas_return_cmd(instance, cmd);
5430 return 0;
5434 * megasas_register_aen - Registers for asynchronous event notification
5435 * @instance: Adapter soft state
5436 * @seq_num: The starting sequence number
5437 * @class_locale: Class of the event
5439 * This function subscribes for AEN for events beyond the @seq_num. It requests
5440 * to be notified if and only if the event is of type @class_locale
5442 static int
5443 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5444 u32 class_locale_word)
5446 int ret_val;
5447 struct megasas_cmd *cmd;
5448 struct megasas_dcmd_frame *dcmd;
5449 union megasas_evt_class_locale curr_aen;
5450 union megasas_evt_class_locale prev_aen;
5453 * If there an AEN pending already (aen_cmd), check if the
5454 * class_locale of that pending AEN is inclusive of the new
5455 * AEN request we currently have. If it is, then we don't have
5456 * to do anything. In other words, whichever events the current
5457 * AEN request is subscribing to, have already been subscribed
5458 * to.
5460 * If the old_cmd is _not_ inclusive, then we have to abort
5461 * that command, form a class_locale that is superset of both
5462 * old and current and re-issue to the FW
5465 curr_aen.word = class_locale_word;
5467 if (instance->aen_cmd) {
5469 prev_aen.word =
5470 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5473 * A class whose enum value is smaller is inclusive of all
5474 * higher values. If a PROGRESS (= -1) was previously
5475 * registered, then a new registration requests for higher
5476 * classes need not be sent to FW. They are automatically
5477 * included.
5479 * Locale numbers don't have such hierarchy. They are bitmap
5480 * values
5482 if ((prev_aen.members.class <= curr_aen.members.class) &&
5483 !((prev_aen.members.locale & curr_aen.members.locale) ^
5484 curr_aen.members.locale)) {
5486 * Previously issued event registration includes
5487 * current request. Nothing to do.
5489 return 0;
5490 } else {
5491 curr_aen.members.locale |= prev_aen.members.locale;
5493 if (prev_aen.members.class < curr_aen.members.class)
5494 curr_aen.members.class = prev_aen.members.class;
5496 instance->aen_cmd->abort_aen = 1;
5497 ret_val = megasas_issue_blocked_abort_cmd(instance,
5498 instance->
5499 aen_cmd, 30);
5501 if (ret_val) {
5502 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5503 "previous AEN command\n");
5504 return ret_val;
5509 cmd = megasas_get_cmd(instance);
5511 if (!cmd)
5512 return -ENOMEM;
5514 dcmd = &cmd->frame->dcmd;
5516 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5519 * Prepare DCMD for aen registration
5521 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5523 dcmd->cmd = MFI_CMD_DCMD;
5524 dcmd->cmd_status = 0x0;
5525 dcmd->sge_count = 1;
5526 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5527 dcmd->timeout = 0;
5528 dcmd->pad_0 = 0;
5529 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5530 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5531 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5532 instance->last_seq_num = seq_num;
5533 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5534 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5535 dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5537 if (instance->aen_cmd != NULL) {
5538 megasas_return_cmd(instance, cmd);
5539 return 0;
5543 * Store reference to the cmd used to register for AEN. When an
5544 * application wants us to register for AEN, we have to abort this
5545 * cmd and re-register with a new EVENT LOCALE supplied by that app
5547 instance->aen_cmd = cmd;
5550 * Issue the aen registration frame
5552 instance->instancet->issue_dcmd(instance, cmd);
5554 return 0;
5558 * megasas_start_aen - Subscribes to AEN during driver load time
5559 * @instance: Adapter soft state
5561 static int megasas_start_aen(struct megasas_instance *instance)
5563 struct megasas_evt_log_info eli;
5564 union megasas_evt_class_locale class_locale;
5567 * Get the latest sequence number from FW
5569 memset(&eli, 0, sizeof(eli));
5571 if (megasas_get_seq_num(instance, &eli))
5572 return -1;
5575 * Register AEN with FW for latest sequence number plus 1
5577 class_locale.members.reserved = 0;
5578 class_locale.members.locale = MR_EVT_LOCALE_ALL;
5579 class_locale.members.class = MR_EVT_CLASS_DEBUG;
5581 return megasas_register_aen(instance,
5582 le32_to_cpu(eli.newest_seq_num) + 1,
5583 class_locale.word);
5587 * megasas_io_attach - Attaches this driver to SCSI mid-layer
5588 * @instance: Adapter soft state
5590 static int megasas_io_attach(struct megasas_instance *instance)
5592 struct Scsi_Host *host = instance->host;
5595 * Export parameters required by SCSI mid-layer
5597 host->irq = instance->pdev->irq;
5598 host->unique_id = instance->unique_id;
5599 host->can_queue = instance->max_scsi_cmds;
5600 host->this_id = instance->init_id;
5601 host->sg_tablesize = instance->max_num_sge;
5603 if (instance->fw_support_ieee)
5604 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5607 * Check if the module parameter value for max_sectors can be used
5609 if (max_sectors && max_sectors < instance->max_sectors_per_req)
5610 instance->max_sectors_per_req = max_sectors;
5611 else {
5612 if (max_sectors) {
5613 if (((instance->pdev->device ==
5614 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5615 (instance->pdev->device ==
5616 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5617 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5618 instance->max_sectors_per_req = max_sectors;
5619 } else {
5620 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5621 "and <= %d (or < 1MB for GEN2 controller)\n",
5622 instance->max_sectors_per_req);
5627 host->max_sectors = instance->max_sectors_per_req;
5628 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5629 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5630 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5631 host->max_lun = MEGASAS_MAX_LUN;
5632 host->max_cmd_len = 16;
5635 * Notify the mid-layer about the new controller
5637 if (scsi_add_host(host, &instance->pdev->dev)) {
5638 dev_err(&instance->pdev->dev,
5639 "Failed to add host from %s %d\n",
5640 __func__, __LINE__);
5641 return -ENODEV;
5644 return 0;
5647 static int
5648 megasas_set_dma_mask(struct pci_dev *pdev)
5651 * All our controllers are capable of performing 64-bit DMA
5653 if (IS_DMA64) {
5654 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5656 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5657 goto fail_set_dma_mask;
5659 } else {
5660 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5661 goto fail_set_dma_mask;
5664 * Ensure that all data structures are allocated in 32-bit
5665 * memory.
5667 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5668 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5669 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5670 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5671 dev_info(&pdev->dev, "set 32bit DMA mask"
5672 "and 32 bit consistent mask\n");
5673 else
5674 goto fail_set_dma_mask;
5677 return 0;
5679 fail_set_dma_mask:
5680 return 1;
5684 * megasas_probe_one - PCI hotplug entry point
5685 * @pdev: PCI device structure
5686 * @id: PCI ids of supported hotplugged adapter
5688 static int megasas_probe_one(struct pci_dev *pdev,
5689 const struct pci_device_id *id)
5691 int rval, pos;
5692 struct Scsi_Host *host;
5693 struct megasas_instance *instance;
5694 u16 control = 0;
5695 struct fusion_context *fusion = NULL;
5697 /* Reset MSI-X in the kdump kernel */
5698 if (reset_devices) {
5699 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5700 if (pos) {
5701 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5702 &control);
5703 if (control & PCI_MSIX_FLAGS_ENABLE) {
5704 dev_info(&pdev->dev, "resetting MSI-X\n");
5705 pci_write_config_word(pdev,
5706 pos + PCI_MSIX_FLAGS,
5707 control &
5708 ~PCI_MSIX_FLAGS_ENABLE);
5714 * PCI prepping: enable device set bus mastering and dma mask
5716 rval = pci_enable_device_mem(pdev);
5718 if (rval) {
5719 return rval;
5722 pci_set_master(pdev);
5724 if (megasas_set_dma_mask(pdev))
5725 goto fail_set_dma_mask;
5727 host = scsi_host_alloc(&megasas_template,
5728 sizeof(struct megasas_instance));
5730 if (!host) {
5731 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5732 goto fail_alloc_instance;
5735 instance = (struct megasas_instance *)host->hostdata;
5736 memset(instance, 0, sizeof(*instance));
5737 atomic_set(&instance->fw_reset_no_pci_access, 0);
5738 instance->pdev = pdev;
5740 switch (instance->pdev->device) {
5741 case PCI_DEVICE_ID_LSI_FUSION:
5742 case PCI_DEVICE_ID_LSI_PLASMA:
5743 case PCI_DEVICE_ID_LSI_INVADER:
5744 case PCI_DEVICE_ID_LSI_FURY:
5745 case PCI_DEVICE_ID_LSI_INTRUDER:
5746 case PCI_DEVICE_ID_LSI_INTRUDER_24:
5747 case PCI_DEVICE_ID_LSI_CUTLASS_52:
5748 case PCI_DEVICE_ID_LSI_CUTLASS_53:
5750 instance->ctrl_context_pages =
5751 get_order(sizeof(struct fusion_context));
5752 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5753 instance->ctrl_context_pages);
5754 if (!instance->ctrl_context) {
5755 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5756 "memory for Fusion context info\n");
5757 goto fail_alloc_dma_buf;
5759 fusion = instance->ctrl_context;
5760 memset(fusion, 0,
5761 ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5762 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5763 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5764 fusion->adapter_type = THUNDERBOLT_SERIES;
5765 else
5766 fusion->adapter_type = INVADER_SERIES;
5768 break;
5769 default: /* For all other supported controllers */
5771 instance->producer =
5772 pci_alloc_consistent(pdev, sizeof(u32),
5773 &instance->producer_h);
5774 instance->consumer =
5775 pci_alloc_consistent(pdev, sizeof(u32),
5776 &instance->consumer_h);
5778 if (!instance->producer || !instance->consumer) {
5779 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5780 "memory for producer, consumer\n");
5781 goto fail_alloc_dma_buf;
5784 *instance->producer = 0;
5785 *instance->consumer = 0;
5786 break;
5789 /* Crash dump feature related initialisation*/
5790 instance->drv_buf_index = 0;
5791 instance->drv_buf_alloc = 0;
5792 instance->crash_dump_fw_support = 0;
5793 instance->crash_dump_app_support = 0;
5794 instance->fw_crash_state = UNAVAILABLE;
5795 spin_lock_init(&instance->crashdump_lock);
5796 instance->crash_dump_buf = NULL;
5798 megasas_poll_wait_aen = 0;
5799 instance->flag_ieee = 0;
5800 instance->ev = NULL;
5801 instance->issuepend_done = 1;
5802 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5803 instance->is_imr = 0;
5805 instance->evt_detail = pci_alloc_consistent(pdev,
5806 sizeof(struct
5807 megasas_evt_detail),
5808 &instance->evt_detail_h);
5810 if (!instance->evt_detail) {
5811 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5812 "event detail structure\n");
5813 goto fail_alloc_dma_buf;
5816 if (!reset_devices) {
5817 instance->system_info_buf = pci_zalloc_consistent(pdev,
5818 sizeof(struct MR_DRV_SYSTEM_INFO),
5819 &instance->system_info_h);
5820 if (!instance->system_info_buf)
5821 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5823 instance->pd_info = pci_alloc_consistent(pdev,
5824 sizeof(struct MR_PD_INFO), &instance->pd_info_h);
5826 if (!instance->pd_info)
5827 dev_err(&instance->pdev->dev, "Failed to alloc mem for pd_info\n");
5829 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5830 CRASH_DMA_BUF_SIZE,
5831 &instance->crash_dump_h);
5832 if (!instance->crash_dump_buf)
5833 dev_err(&pdev->dev, "Can't allocate Firmware "
5834 "crash dump DMA buffer\n");
5838 * Initialize locks and queues
5840 INIT_LIST_HEAD(&instance->cmd_pool);
5841 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5843 atomic_set(&instance->fw_outstanding,0);
5845 init_waitqueue_head(&instance->int_cmd_wait_q);
5846 init_waitqueue_head(&instance->abort_cmd_wait_q);
5848 spin_lock_init(&instance->mfi_pool_lock);
5849 spin_lock_init(&instance->hba_lock);
5850 spin_lock_init(&instance->completion_lock);
5852 mutex_init(&instance->reset_mutex);
5853 mutex_init(&instance->hba_mutex);
5856 * Initialize PCI related and misc parameters
5858 instance->host = host;
5859 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5860 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5861 instance->ctrl_info = NULL;
5864 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5865 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5866 instance->flag_ieee = 1;
5868 megasas_dbg_lvl = 0;
5869 instance->flag = 0;
5870 instance->unload = 1;
5871 instance->last_time = 0;
5872 instance->disableOnlineCtrlReset = 1;
5873 instance->UnevenSpanSupport = 0;
5875 if (instance->ctrl_context) {
5876 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5877 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5878 } else
5879 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5882 * Initialize MFI Firmware
5884 if (megasas_init_fw(instance))
5885 goto fail_init_mfi;
5887 if (instance->requestorId) {
5888 if (instance->PlasmaFW111) {
5889 instance->vf_affiliation_111 =
5890 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5891 &instance->vf_affiliation_111_h);
5892 if (!instance->vf_affiliation_111)
5893 dev_warn(&pdev->dev, "Can't allocate "
5894 "memory for VF affiliation buffer\n");
5895 } else {
5896 instance->vf_affiliation =
5897 pci_alloc_consistent(pdev,
5898 (MAX_LOGICAL_DRIVES + 1) *
5899 sizeof(struct MR_LD_VF_AFFILIATION),
5900 &instance->vf_affiliation_h);
5901 if (!instance->vf_affiliation)
5902 dev_warn(&pdev->dev, "Can't allocate "
5903 "memory for VF affiliation buffer\n");
5908 * Store instance in PCI softstate
5910 pci_set_drvdata(pdev, instance);
5913 * Add this controller to megasas_mgmt_info structure so that it
5914 * can be exported to management applications
5916 megasas_mgmt_info.count++;
5917 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5918 megasas_mgmt_info.max_index++;
5921 * Register with SCSI mid-layer
5923 if (megasas_io_attach(instance))
5924 goto fail_io_attach;
5926 instance->unload = 0;
5928 * Trigger SCSI to scan our drives
5930 scsi_scan_host(host);
5933 * Initiate AEN (Asynchronous Event Notification)
5935 if (megasas_start_aen(instance)) {
5936 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5937 goto fail_start_aen;
5940 /* Get current SR-IOV LD/VF affiliation */
5941 if (instance->requestorId)
5942 megasas_get_ld_vf_affiliation(instance, 1);
5944 return 0;
5946 fail_start_aen:
5947 fail_io_attach:
5948 megasas_mgmt_info.count--;
5949 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5950 megasas_mgmt_info.max_index--;
5952 instance->instancet->disable_intr(instance);
5953 megasas_destroy_irqs(instance);
5955 if (instance->ctrl_context)
5956 megasas_release_fusion(instance);
5957 else
5958 megasas_release_mfi(instance);
5959 if (instance->msix_vectors)
5960 pci_disable_msix(instance->pdev);
5961 fail_init_mfi:
5962 fail_alloc_dma_buf:
5963 if (instance->evt_detail)
5964 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5965 instance->evt_detail,
5966 instance->evt_detail_h);
5968 if (instance->pd_info)
5969 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
5970 instance->pd_info,
5971 instance->pd_info_h);
5972 if (instance->producer)
5973 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5974 instance->producer_h);
5975 if (instance->consumer)
5976 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5977 instance->consumer_h);
5978 scsi_host_put(host);
5980 fail_alloc_instance:
5981 fail_set_dma_mask:
5982 pci_disable_device(pdev);
5984 return -ENODEV;
5988 * megasas_flush_cache - Requests FW to flush all its caches
5989 * @instance: Adapter soft state
5991 static void megasas_flush_cache(struct megasas_instance *instance)
5993 struct megasas_cmd *cmd;
5994 struct megasas_dcmd_frame *dcmd;
5996 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
5997 return;
5999 cmd = megasas_get_cmd(instance);
6001 if (!cmd)
6002 return;
6004 dcmd = &cmd->frame->dcmd;
6006 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6008 dcmd->cmd = MFI_CMD_DCMD;
6009 dcmd->cmd_status = 0x0;
6010 dcmd->sge_count = 0;
6011 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6012 dcmd->timeout = 0;
6013 dcmd->pad_0 = 0;
6014 dcmd->data_xfer_len = 0;
6015 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
6016 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
6018 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6019 != DCMD_SUCCESS) {
6020 dev_err(&instance->pdev->dev,
6021 "return from %s %d\n", __func__, __LINE__);
6022 return;
6025 megasas_return_cmd(instance, cmd);
6029 * megasas_shutdown_controller - Instructs FW to shutdown the controller
6030 * @instance: Adapter soft state
6031 * @opcode: Shutdown/Hibernate
6033 static void megasas_shutdown_controller(struct megasas_instance *instance,
6034 u32 opcode)
6036 struct megasas_cmd *cmd;
6037 struct megasas_dcmd_frame *dcmd;
6039 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
6040 return;
6042 cmd = megasas_get_cmd(instance);
6044 if (!cmd)
6045 return;
6047 if (instance->aen_cmd)
6048 megasas_issue_blocked_abort_cmd(instance,
6049 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
6050 if (instance->map_update_cmd)
6051 megasas_issue_blocked_abort_cmd(instance,
6052 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
6053 if (instance->jbod_seq_cmd)
6054 megasas_issue_blocked_abort_cmd(instance,
6055 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
6057 dcmd = &cmd->frame->dcmd;
6059 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6061 dcmd->cmd = MFI_CMD_DCMD;
6062 dcmd->cmd_status = 0x0;
6063 dcmd->sge_count = 0;
6064 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
6065 dcmd->timeout = 0;
6066 dcmd->pad_0 = 0;
6067 dcmd->data_xfer_len = 0;
6068 dcmd->opcode = cpu_to_le32(opcode);
6070 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
6071 != DCMD_SUCCESS) {
6072 dev_err(&instance->pdev->dev,
6073 "return from %s %d\n", __func__, __LINE__);
6074 return;
6077 megasas_return_cmd(instance, cmd);
6080 #ifdef CONFIG_PM
6082 * megasas_suspend - driver suspend entry point
6083 * @pdev: PCI device structure
6084 * @state: PCI power state to suspend routine
6086 static int
6087 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
6089 struct Scsi_Host *host;
6090 struct megasas_instance *instance;
6092 instance = pci_get_drvdata(pdev);
6093 host = instance->host;
6094 instance->unload = 1;
6096 /* Shutdown SR-IOV heartbeat timer */
6097 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6098 del_timer_sync(&instance->sriov_heartbeat_timer);
6100 megasas_flush_cache(instance);
6101 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
6103 /* cancel the delayed work if this work still in queue */
6104 if (instance->ev != NULL) {
6105 struct megasas_aen_event *ev = instance->ev;
6106 cancel_delayed_work_sync(&ev->hotplug_work);
6107 instance->ev = NULL;
6110 tasklet_kill(&instance->isr_tasklet);
6112 pci_set_drvdata(instance->pdev, instance);
6113 instance->instancet->disable_intr(instance);
6115 megasas_destroy_irqs(instance);
6117 if (instance->msix_vectors)
6118 pci_disable_msix(instance->pdev);
6120 pci_save_state(pdev);
6121 pci_disable_device(pdev);
6123 pci_set_power_state(pdev, pci_choose_state(pdev, state));
6125 return 0;
6129 * megasas_resume- driver resume entry point
6130 * @pdev: PCI device structure
6132 static int
6133 megasas_resume(struct pci_dev *pdev)
6135 int rval;
6136 struct Scsi_Host *host;
6137 struct megasas_instance *instance;
6139 instance = pci_get_drvdata(pdev);
6140 host = instance->host;
6141 pci_set_power_state(pdev, PCI_D0);
6142 pci_enable_wake(pdev, PCI_D0, 0);
6143 pci_restore_state(pdev);
6146 * PCI prepping: enable device set bus mastering and dma mask
6148 rval = pci_enable_device_mem(pdev);
6150 if (rval) {
6151 dev_err(&pdev->dev, "Enable device failed\n");
6152 return rval;
6155 pci_set_master(pdev);
6157 if (megasas_set_dma_mask(pdev))
6158 goto fail_set_dma_mask;
6161 * Initialize MFI Firmware
6164 atomic_set(&instance->fw_outstanding, 0);
6167 * We expect the FW state to be READY
6169 if (megasas_transition_to_ready(instance, 0))
6170 goto fail_ready_state;
6172 /* Now re-enable MSI-X */
6173 if (instance->msix_vectors &&
6174 pci_enable_msix_exact(instance->pdev, instance->msixentry,
6175 instance->msix_vectors))
6176 goto fail_reenable_msix;
6178 if (instance->ctrl_context) {
6179 megasas_reset_reply_desc(instance);
6180 if (megasas_ioc_init_fusion(instance)) {
6181 megasas_free_cmds(instance);
6182 megasas_free_cmds_fusion(instance);
6183 goto fail_init_mfi;
6185 if (!megasas_get_map_info(instance))
6186 megasas_sync_map_info(instance);
6187 } else {
6188 *instance->producer = 0;
6189 *instance->consumer = 0;
6190 if (megasas_issue_init_mfi(instance))
6191 goto fail_init_mfi;
6194 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6195 (unsigned long)instance);
6197 if (instance->msix_vectors ?
6198 megasas_setup_irqs_msix(instance, 0) :
6199 megasas_setup_irqs_ioapic(instance))
6200 goto fail_init_mfi;
6202 /* Re-launch SR-IOV heartbeat timer */
6203 if (instance->requestorId) {
6204 if (!megasas_sriov_start_heartbeat(instance, 0))
6205 megasas_start_timer(instance,
6206 &instance->sriov_heartbeat_timer,
6207 megasas_sriov_heartbeat_handler,
6208 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
6209 else {
6210 instance->skip_heartbeat_timer_del = 1;
6211 goto fail_init_mfi;
6215 instance->instancet->enable_intr(instance);
6216 megasas_setup_jbod_map(instance);
6217 instance->unload = 0;
6220 * Initiate AEN (Asynchronous Event Notification)
6222 if (megasas_start_aen(instance))
6223 dev_err(&instance->pdev->dev, "Start AEN failed\n");
6225 return 0;
6227 fail_init_mfi:
6228 if (instance->evt_detail)
6229 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6230 instance->evt_detail,
6231 instance->evt_detail_h);
6233 if (instance->pd_info)
6234 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6235 instance->pd_info,
6236 instance->pd_info_h);
6237 if (instance->producer)
6238 pci_free_consistent(pdev, sizeof(u32), instance->producer,
6239 instance->producer_h);
6240 if (instance->consumer)
6241 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
6242 instance->consumer_h);
6243 scsi_host_put(host);
6245 fail_set_dma_mask:
6246 fail_ready_state:
6247 fail_reenable_msix:
6249 pci_disable_device(pdev);
6251 return -ENODEV;
6253 #else
6254 #define megasas_suspend NULL
6255 #define megasas_resume NULL
6256 #endif
6259 * megasas_detach_one - PCI hot"un"plug entry point
6260 * @pdev: PCI device structure
6262 static void megasas_detach_one(struct pci_dev *pdev)
6264 int i;
6265 struct Scsi_Host *host;
6266 struct megasas_instance *instance;
6267 struct fusion_context *fusion;
6268 u32 pd_seq_map_sz;
6270 instance = pci_get_drvdata(pdev);
6271 instance->unload = 1;
6272 host = instance->host;
6273 fusion = instance->ctrl_context;
6275 /* Shutdown SR-IOV heartbeat timer */
6276 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6277 del_timer_sync(&instance->sriov_heartbeat_timer);
6279 if (instance->fw_crash_state != UNAVAILABLE)
6280 megasas_free_host_crash_buffer(instance);
6281 scsi_remove_host(instance->host);
6282 megasas_flush_cache(instance);
6283 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6285 /* cancel the delayed work if this work still in queue*/
6286 if (instance->ev != NULL) {
6287 struct megasas_aen_event *ev = instance->ev;
6288 cancel_delayed_work_sync(&ev->hotplug_work);
6289 instance->ev = NULL;
6292 /* cancel all wait events */
6293 wake_up_all(&instance->int_cmd_wait_q);
6295 tasklet_kill(&instance->isr_tasklet);
6298 * Take the instance off the instance array. Note that we will not
6299 * decrement the max_index. We let this array be sparse array
6301 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6302 if (megasas_mgmt_info.instance[i] == instance) {
6303 megasas_mgmt_info.count--;
6304 megasas_mgmt_info.instance[i] = NULL;
6306 break;
6310 instance->instancet->disable_intr(instance);
6312 megasas_destroy_irqs(instance);
6314 if (instance->msix_vectors)
6315 pci_disable_msix(instance->pdev);
6317 if (instance->ctrl_context) {
6318 megasas_release_fusion(instance);
6319 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
6320 (sizeof(struct MR_PD_CFG_SEQ) *
6321 (MAX_PHYSICAL_DEVICES - 1));
6322 for (i = 0; i < 2 ; i++) {
6323 if (fusion->ld_map[i])
6324 dma_free_coherent(&instance->pdev->dev,
6325 fusion->max_map_sz,
6326 fusion->ld_map[i],
6327 fusion->ld_map_phys[i]);
6328 if (fusion->ld_drv_map[i])
6329 free_pages((ulong)fusion->ld_drv_map[i],
6330 fusion->drv_map_pages);
6331 if (fusion->pd_seq_sync[i])
6332 dma_free_coherent(&instance->pdev->dev,
6333 pd_seq_map_sz,
6334 fusion->pd_seq_sync[i],
6335 fusion->pd_seq_phys[i]);
6337 free_pages((ulong)instance->ctrl_context,
6338 instance->ctrl_context_pages);
6339 } else {
6340 megasas_release_mfi(instance);
6341 pci_free_consistent(pdev, sizeof(u32),
6342 instance->producer,
6343 instance->producer_h);
6344 pci_free_consistent(pdev, sizeof(u32),
6345 instance->consumer,
6346 instance->consumer_h);
6349 kfree(instance->ctrl_info);
6351 if (instance->evt_detail)
6352 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
6353 instance->evt_detail, instance->evt_detail_h);
6355 if (instance->pd_info)
6356 pci_free_consistent(pdev, sizeof(struct MR_PD_INFO),
6357 instance->pd_info,
6358 instance->pd_info_h);
6359 if (instance->vf_affiliation)
6360 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
6361 sizeof(struct MR_LD_VF_AFFILIATION),
6362 instance->vf_affiliation,
6363 instance->vf_affiliation_h);
6365 if (instance->vf_affiliation_111)
6366 pci_free_consistent(pdev,
6367 sizeof(struct MR_LD_VF_AFFILIATION_111),
6368 instance->vf_affiliation_111,
6369 instance->vf_affiliation_111_h);
6371 if (instance->hb_host_mem)
6372 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
6373 instance->hb_host_mem,
6374 instance->hb_host_mem_h);
6376 if (instance->crash_dump_buf)
6377 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
6378 instance->crash_dump_buf, instance->crash_dump_h);
6380 if (instance->system_info_buf)
6381 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
6382 instance->system_info_buf, instance->system_info_h);
6384 scsi_host_put(host);
6386 pci_disable_device(pdev);
6390 * megasas_shutdown - Shutdown entry point
6391 * @device: Generic device structure
6393 static void megasas_shutdown(struct pci_dev *pdev)
6395 struct megasas_instance *instance = pci_get_drvdata(pdev);
6397 instance->unload = 1;
6398 megasas_flush_cache(instance);
6399 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6400 instance->instancet->disable_intr(instance);
6401 megasas_destroy_irqs(instance);
6403 if (instance->msix_vectors)
6404 pci_disable_msix(instance->pdev);
6408 * megasas_mgmt_open - char node "open" entry point
6410 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6413 * Allow only those users with admin rights
6415 if (!capable(CAP_SYS_ADMIN))
6416 return -EACCES;
6418 return 0;
6422 * megasas_mgmt_fasync - Async notifier registration from applications
6424 * This function adds the calling process to a driver global queue. When an
6425 * event occurs, SIGIO will be sent to all processes in this queue.
6427 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6429 int rc;
6431 mutex_lock(&megasas_async_queue_mutex);
6433 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6435 mutex_unlock(&megasas_async_queue_mutex);
6437 if (rc >= 0) {
6438 /* For sanity check when we get ioctl */
6439 filep->private_data = filep;
6440 return 0;
6443 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6445 return rc;
6449 * megasas_mgmt_poll - char node "poll" entry point
6450 * */
6451 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6453 unsigned int mask;
6454 unsigned long flags;
6456 poll_wait(file, &megasas_poll_wait, wait);
6457 spin_lock_irqsave(&poll_aen_lock, flags);
6458 if (megasas_poll_wait_aen)
6459 mask = (POLLIN | POLLRDNORM);
6460 else
6461 mask = 0;
6462 megasas_poll_wait_aen = 0;
6463 spin_unlock_irqrestore(&poll_aen_lock, flags);
6464 return mask;
6468 * megasas_set_crash_dump_params_ioctl:
6469 * Send CRASH_DUMP_MODE DCMD to all controllers
6470 * @cmd: MFI command frame
6473 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6475 struct megasas_instance *local_instance;
6476 int i, error = 0;
6477 int crash_support;
6479 crash_support = cmd->frame->dcmd.mbox.w[0];
6481 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6482 local_instance = megasas_mgmt_info.instance[i];
6483 if (local_instance && local_instance->crash_dump_drv_support) {
6484 if ((atomic_read(&local_instance->adprecovery) ==
6485 MEGASAS_HBA_OPERATIONAL) &&
6486 !megasas_set_crash_dump_params(local_instance,
6487 crash_support)) {
6488 local_instance->crash_dump_app_support =
6489 crash_support;
6490 dev_info(&local_instance->pdev->dev,
6491 "Application firmware crash "
6492 "dump mode set success\n");
6493 error = 0;
6494 } else {
6495 dev_info(&local_instance->pdev->dev,
6496 "Application firmware crash "
6497 "dump mode set failed\n");
6498 error = -1;
6502 return error;
6506 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
6507 * @instance: Adapter soft state
6508 * @argp: User's ioctl packet
6510 static int
6511 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6512 struct megasas_iocpacket __user * user_ioc,
6513 struct megasas_iocpacket *ioc)
6515 struct megasas_sge32 *kern_sge32;
6516 struct megasas_cmd *cmd;
6517 void *kbuff_arr[MAX_IOCTL_SGE];
6518 dma_addr_t buf_handle = 0;
6519 int error = 0, i;
6520 void *sense = NULL;
6521 dma_addr_t sense_handle;
6522 unsigned long *sense_ptr;
6524 memset(kbuff_arr, 0, sizeof(kbuff_arr));
6526 if (ioc->sge_count > MAX_IOCTL_SGE) {
6527 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
6528 ioc->sge_count, MAX_IOCTL_SGE);
6529 return -EINVAL;
6532 cmd = megasas_get_cmd(instance);
6533 if (!cmd) {
6534 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6535 return -ENOMEM;
6539 * User's IOCTL packet has 2 frames (maximum). Copy those two
6540 * frames into our cmd's frames. cmd->frame's context will get
6541 * overwritten when we copy from user's frames. So set that value
6542 * alone separately
6544 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6545 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6546 cmd->frame->hdr.pad_0 = 0;
6547 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6548 MFI_FRAME_SGL64 |
6549 MFI_FRAME_SENSE64));
6551 if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6552 error = megasas_set_crash_dump_params_ioctl(cmd);
6553 megasas_return_cmd(instance, cmd);
6554 return error;
6558 * The management interface between applications and the fw uses
6559 * MFI frames. E.g, RAID configuration changes, LD property changes
6560 * etc are accomplishes through different kinds of MFI frames. The
6561 * driver needs to care only about substituting user buffers with
6562 * kernel buffers in SGLs. The location of SGL is embedded in the
6563 * struct iocpacket itself.
6565 kern_sge32 = (struct megasas_sge32 *)
6566 ((unsigned long)cmd->frame + ioc->sgl_off);
6569 * For each user buffer, create a mirror buffer and copy in
6571 for (i = 0; i < ioc->sge_count; i++) {
6572 if (!ioc->sgl[i].iov_len)
6573 continue;
6575 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6576 ioc->sgl[i].iov_len,
6577 &buf_handle, GFP_KERNEL);
6578 if (!kbuff_arr[i]) {
6579 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6580 "kernel SGL buffer for IOCTL\n");
6581 error = -ENOMEM;
6582 goto out;
6586 * We don't change the dma_coherent_mask, so
6587 * pci_alloc_consistent only returns 32bit addresses
6589 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6590 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6593 * We created a kernel buffer corresponding to the
6594 * user buffer. Now copy in from the user buffer
6596 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6597 (u32) (ioc->sgl[i].iov_len))) {
6598 error = -EFAULT;
6599 goto out;
6603 if (ioc->sense_len) {
6604 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6605 &sense_handle, GFP_KERNEL);
6606 if (!sense) {
6607 error = -ENOMEM;
6608 goto out;
6611 sense_ptr =
6612 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6613 *sense_ptr = cpu_to_le32(sense_handle);
6617 * Set the sync_cmd flag so that the ISR knows not to complete this
6618 * cmd to the SCSI mid-layer
6620 cmd->sync_cmd = 1;
6621 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
6622 cmd->sync_cmd = 0;
6623 dev_err(&instance->pdev->dev,
6624 "return -EBUSY from %s %d opcode 0x%x cmd->cmd_status_drv 0x%x\n",
6625 __func__, __LINE__, cmd->frame->dcmd.opcode,
6626 cmd->cmd_status_drv);
6627 return -EBUSY;
6630 cmd->sync_cmd = 0;
6632 if (instance->unload == 1) {
6633 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6634 "don't submit data to application\n");
6635 goto out;
6638 * copy out the kernel buffers to user buffers
6640 for (i = 0; i < ioc->sge_count; i++) {
6641 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6642 ioc->sgl[i].iov_len)) {
6643 error = -EFAULT;
6644 goto out;
6649 * copy out the sense
6651 if (ioc->sense_len) {
6653 * sense_ptr points to the location that has the user
6654 * sense buffer address
6656 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6657 ioc->sense_off);
6659 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6660 sense, ioc->sense_len)) {
6661 dev_err(&instance->pdev->dev, "Failed to copy out to user "
6662 "sense data\n");
6663 error = -EFAULT;
6664 goto out;
6669 * copy the status codes returned by the fw
6671 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6672 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6673 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6674 error = -EFAULT;
6677 out:
6678 if (sense) {
6679 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6680 sense, sense_handle);
6683 for (i = 0; i < ioc->sge_count; i++) {
6684 if (kbuff_arr[i]) {
6685 dma_free_coherent(&instance->pdev->dev,
6686 le32_to_cpu(kern_sge32[i].length),
6687 kbuff_arr[i],
6688 le32_to_cpu(kern_sge32[i].phys_addr));
6689 kbuff_arr[i] = NULL;
6693 megasas_return_cmd(instance, cmd);
6694 return error;
6697 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6699 struct megasas_iocpacket __user *user_ioc =
6700 (struct megasas_iocpacket __user *)arg;
6701 struct megasas_iocpacket *ioc;
6702 struct megasas_instance *instance;
6703 int error;
6704 int i;
6705 unsigned long flags;
6706 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6708 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6709 if (!ioc)
6710 return -ENOMEM;
6712 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6713 error = -EFAULT;
6714 goto out_kfree_ioc;
6717 instance = megasas_lookup_instance(ioc->host_no);
6718 if (!instance) {
6719 error = -ENODEV;
6720 goto out_kfree_ioc;
6723 /* Adjust ioctl wait time for VF mode */
6724 if (instance->requestorId)
6725 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6727 /* Block ioctls in VF mode */
6728 if (instance->requestorId && !allow_vf_ioctls) {
6729 error = -ENODEV;
6730 goto out_kfree_ioc;
6733 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
6734 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6735 error = -ENODEV;
6736 goto out_kfree_ioc;
6739 if (instance->unload == 1) {
6740 error = -ENODEV;
6741 goto out_kfree_ioc;
6744 if (down_interruptible(&instance->ioctl_sem)) {
6745 error = -ERESTARTSYS;
6746 goto out_kfree_ioc;
6749 for (i = 0; i < wait_time; i++) {
6751 spin_lock_irqsave(&instance->hba_lock, flags);
6752 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
6753 spin_unlock_irqrestore(&instance->hba_lock, flags);
6754 break;
6756 spin_unlock_irqrestore(&instance->hba_lock, flags);
6758 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6759 dev_notice(&instance->pdev->dev, "waiting"
6760 "for controller reset to finish\n");
6763 msleep(1000);
6766 spin_lock_irqsave(&instance->hba_lock, flags);
6767 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6768 spin_unlock_irqrestore(&instance->hba_lock, flags);
6770 dev_err(&instance->pdev->dev, "timed out while"
6771 "waiting for HBA to recover\n");
6772 error = -ENODEV;
6773 goto out_up;
6775 spin_unlock_irqrestore(&instance->hba_lock, flags);
6777 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6778 out_up:
6779 up(&instance->ioctl_sem);
6781 out_kfree_ioc:
6782 kfree(ioc);
6783 return error;
6786 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6788 struct megasas_instance *instance;
6789 struct megasas_aen aen;
6790 int error;
6791 int i;
6792 unsigned long flags;
6793 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6795 if (file->private_data != file) {
6796 printk(KERN_DEBUG "megasas: fasync_helper was not "
6797 "called first\n");
6798 return -EINVAL;
6801 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6802 return -EFAULT;
6804 instance = megasas_lookup_instance(aen.host_no);
6806 if (!instance)
6807 return -ENODEV;
6809 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
6810 return -ENODEV;
6813 if (instance->unload == 1) {
6814 return -ENODEV;
6817 for (i = 0; i < wait_time; i++) {
6819 spin_lock_irqsave(&instance->hba_lock, flags);
6820 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
6821 spin_unlock_irqrestore(&instance->hba_lock,
6822 flags);
6823 break;
6826 spin_unlock_irqrestore(&instance->hba_lock, flags);
6828 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6829 dev_notice(&instance->pdev->dev, "waiting for"
6830 "controller reset to finish\n");
6833 msleep(1000);
6836 spin_lock_irqsave(&instance->hba_lock, flags);
6837 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
6838 spin_unlock_irqrestore(&instance->hba_lock, flags);
6839 dev_err(&instance->pdev->dev, "timed out while waiting"
6840 "for HBA to recover\n");
6841 return -ENODEV;
6843 spin_unlock_irqrestore(&instance->hba_lock, flags);
6845 mutex_lock(&instance->reset_mutex);
6846 error = megasas_register_aen(instance, aen.seq_num,
6847 aen.class_locale_word);
6848 mutex_unlock(&instance->reset_mutex);
6849 return error;
6853 * megasas_mgmt_ioctl - char node ioctl entry point
6855 static long
6856 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6858 switch (cmd) {
6859 case MEGASAS_IOC_FIRMWARE:
6860 return megasas_mgmt_ioctl_fw(file, arg);
6862 case MEGASAS_IOC_GET_AEN:
6863 return megasas_mgmt_ioctl_aen(file, arg);
6866 return -ENOTTY;
6869 #ifdef CONFIG_COMPAT
6870 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6872 struct compat_megasas_iocpacket __user *cioc =
6873 (struct compat_megasas_iocpacket __user *)arg;
6874 struct megasas_iocpacket __user *ioc =
6875 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6876 int i;
6877 int error = 0;
6878 compat_uptr_t ptr;
6879 u32 local_sense_off;
6880 u32 local_sense_len;
6881 u32 user_sense_off;
6883 if (clear_user(ioc, sizeof(*ioc)))
6884 return -EFAULT;
6886 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6887 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6888 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6889 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6890 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6891 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6892 return -EFAULT;
6895 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6896 * sense_len is not null, so prepare the 64bit value under
6897 * the same condition.
6899 if (get_user(local_sense_off, &ioc->sense_off) ||
6900 get_user(local_sense_len, &ioc->sense_len) ||
6901 get_user(user_sense_off, &cioc->sense_off))
6902 return -EFAULT;
6904 if (local_sense_len) {
6905 void __user **sense_ioc_ptr =
6906 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
6907 compat_uptr_t *sense_cioc_ptr =
6908 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
6909 if (get_user(ptr, sense_cioc_ptr) ||
6910 put_user(compat_ptr(ptr), sense_ioc_ptr))
6911 return -EFAULT;
6914 for (i = 0; i < MAX_IOCTL_SGE; i++) {
6915 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6916 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6917 copy_in_user(&ioc->sgl[i].iov_len,
6918 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6919 return -EFAULT;
6922 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6924 if (copy_in_user(&cioc->frame.hdr.cmd_status,
6925 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6926 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6927 return -EFAULT;
6929 return error;
6932 static long
6933 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6934 unsigned long arg)
6936 switch (cmd) {
6937 case MEGASAS_IOC_FIRMWARE32:
6938 return megasas_mgmt_compat_ioctl_fw(file, arg);
6939 case MEGASAS_IOC_GET_AEN:
6940 return megasas_mgmt_ioctl_aen(file, arg);
6943 return -ENOTTY;
6945 #endif
6948 * File operations structure for management interface
6950 static const struct file_operations megasas_mgmt_fops = {
6951 .owner = THIS_MODULE,
6952 .open = megasas_mgmt_open,
6953 .fasync = megasas_mgmt_fasync,
6954 .unlocked_ioctl = megasas_mgmt_ioctl,
6955 .poll = megasas_mgmt_poll,
6956 #ifdef CONFIG_COMPAT
6957 .compat_ioctl = megasas_mgmt_compat_ioctl,
6958 #endif
6959 .llseek = noop_llseek,
6963 * PCI hotplug support registration structure
6965 static struct pci_driver megasas_pci_driver = {
6967 .name = "megaraid_sas",
6968 .id_table = megasas_pci_table,
6969 .probe = megasas_probe_one,
6970 .remove = megasas_detach_one,
6971 .suspend = megasas_suspend,
6972 .resume = megasas_resume,
6973 .shutdown = megasas_shutdown,
6977 * Sysfs driver attributes
6979 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6981 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6982 MEGASAS_VERSION);
6985 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6987 static ssize_t
6988 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6990 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6991 MEGASAS_RELDATE);
6994 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6996 static ssize_t
6997 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6999 return sprintf(buf, "%u\n", support_poll_for_event);
7002 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
7003 megasas_sysfs_show_support_poll_for_event, NULL);
7005 static ssize_t
7006 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
7008 return sprintf(buf, "%u\n", support_device_change);
7011 static DRIVER_ATTR(support_device_change, S_IRUGO,
7012 megasas_sysfs_show_support_device_change, NULL);
7014 static ssize_t
7015 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
7017 return sprintf(buf, "%u\n", megasas_dbg_lvl);
7020 static ssize_t
7021 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
7023 int retval = count;
7025 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
7026 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
7027 retval = -EINVAL;
7029 return retval;
7032 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
7033 megasas_sysfs_set_dbg_lvl);
7035 static void
7036 megasas_aen_polling(struct work_struct *work)
7038 struct megasas_aen_event *ev =
7039 container_of(work, struct megasas_aen_event, hotplug_work.work);
7040 struct megasas_instance *instance = ev->instance;
7041 union megasas_evt_class_locale class_locale;
7042 struct Scsi_Host *host;
7043 struct scsi_device *sdev1;
7044 u16 pd_index = 0;
7045 u16 ld_index = 0;
7046 int i, j, doscan = 0;
7047 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
7048 int error;
7049 u8 dcmd_ret = DCMD_SUCCESS;
7051 if (!instance) {
7052 printk(KERN_ERR "invalid instance!\n");
7053 kfree(ev);
7054 return;
7057 /* Adjust event workqueue thread wait time for VF mode */
7058 if (instance->requestorId)
7059 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
7061 /* Don't run the event workqueue thread if OCR is running */
7062 mutex_lock(&instance->reset_mutex);
7064 instance->ev = NULL;
7065 host = instance->host;
7066 if (instance->evt_detail) {
7067 megasas_decode_evt(instance);
7069 switch (le32_to_cpu(instance->evt_detail->code)) {
7071 case MR_EVT_PD_INSERTED:
7072 case MR_EVT_PD_REMOVED:
7073 dcmd_ret = megasas_get_pd_list(instance);
7074 if (dcmd_ret == DCMD_SUCCESS)
7075 doscan = SCAN_PD_CHANNEL;
7076 break;
7078 case MR_EVT_LD_OFFLINE:
7079 case MR_EVT_CFG_CLEARED:
7080 case MR_EVT_LD_DELETED:
7081 case MR_EVT_LD_CREATED:
7082 if (!instance->requestorId ||
7083 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7084 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7086 if (dcmd_ret == DCMD_SUCCESS)
7087 doscan = SCAN_VD_CHANNEL;
7089 break;
7091 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
7092 case MR_EVT_FOREIGN_CFG_IMPORTED:
7093 case MR_EVT_LD_STATE_CHANGE:
7094 dcmd_ret = megasas_get_pd_list(instance);
7096 if (dcmd_ret != DCMD_SUCCESS)
7097 break;
7099 if (!instance->requestorId ||
7100 (instance->requestorId && megasas_get_ld_vf_affiliation(instance, 0)))
7101 dcmd_ret = megasas_ld_list_query(instance, MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
7103 if (dcmd_ret != DCMD_SUCCESS)
7104 break;
7106 doscan = SCAN_VD_CHANNEL | SCAN_PD_CHANNEL;
7107 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
7108 instance->host->host_no);
7109 break;
7111 case MR_EVT_CTRL_PROP_CHANGED:
7112 dcmd_ret = megasas_get_ctrl_info(instance);
7113 break;
7114 default:
7115 doscan = 0;
7116 break;
7118 } else {
7119 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
7120 mutex_unlock(&instance->reset_mutex);
7121 kfree(ev);
7122 return;
7125 mutex_unlock(&instance->reset_mutex);
7127 if (doscan & SCAN_PD_CHANNEL) {
7128 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
7129 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7130 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
7131 sdev1 = scsi_device_lookup(host, i, j, 0);
7132 if (instance->pd_list[pd_index].driveState ==
7133 MR_PD_STATE_SYSTEM) {
7134 if (!sdev1)
7135 scsi_add_device(host, i, j, 0);
7136 else
7137 scsi_device_put(sdev1);
7138 } else {
7139 if (sdev1) {
7140 scsi_remove_device(sdev1);
7141 scsi_device_put(sdev1);
7148 if (doscan & SCAN_VD_CHANNEL) {
7149 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
7150 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
7151 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
7152 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7153 if (instance->ld_ids[ld_index] != 0xff) {
7154 if (!sdev1)
7155 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
7156 else
7157 scsi_device_put(sdev1);
7158 } else {
7159 if (sdev1) {
7160 scsi_remove_device(sdev1);
7161 scsi_device_put(sdev1);
7168 if (dcmd_ret == DCMD_SUCCESS)
7169 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
7170 else
7171 seq_num = instance->last_seq_num;
7173 /* Register AEN with FW for latest sequence number plus 1 */
7174 class_locale.members.reserved = 0;
7175 class_locale.members.locale = MR_EVT_LOCALE_ALL;
7176 class_locale.members.class = MR_EVT_CLASS_DEBUG;
7178 if (instance->aen_cmd != NULL) {
7179 kfree(ev);
7180 return;
7183 mutex_lock(&instance->reset_mutex);
7184 error = megasas_register_aen(instance, seq_num,
7185 class_locale.word);
7186 if (error)
7187 dev_err(&instance->pdev->dev,
7188 "register aen failed error %x\n", error);
7190 mutex_unlock(&instance->reset_mutex);
7191 kfree(ev);
7195 * megasas_init - Driver load entry point
7197 static int __init megasas_init(void)
7199 int rval;
7202 * Booted in kdump kernel, minimize memory footprints by
7203 * disabling few features
7205 if (reset_devices) {
7206 msix_vectors = 1;
7207 rdpq_enable = 0;
7208 dual_qdepth_disable = 1;
7212 * Announce driver version and other information
7214 pr_info("megasas: %s\n", MEGASAS_VERSION);
7216 spin_lock_init(&poll_aen_lock);
7218 support_poll_for_event = 2;
7219 support_device_change = 1;
7221 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
7224 * Register character device node
7226 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
7228 if (rval < 0) {
7229 printk(KERN_DEBUG "megasas: failed to open device node\n");
7230 return rval;
7233 megasas_mgmt_majorno = rval;
7236 * Register ourselves as PCI hotplug module
7238 rval = pci_register_driver(&megasas_pci_driver);
7240 if (rval) {
7241 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
7242 goto err_pcidrv;
7245 rval = driver_create_file(&megasas_pci_driver.driver,
7246 &driver_attr_version);
7247 if (rval)
7248 goto err_dcf_attr_ver;
7250 rval = driver_create_file(&megasas_pci_driver.driver,
7251 &driver_attr_release_date);
7252 if (rval)
7253 goto err_dcf_rel_date;
7255 rval = driver_create_file(&megasas_pci_driver.driver,
7256 &driver_attr_support_poll_for_event);
7257 if (rval)
7258 goto err_dcf_support_poll_for_event;
7260 rval = driver_create_file(&megasas_pci_driver.driver,
7261 &driver_attr_dbg_lvl);
7262 if (rval)
7263 goto err_dcf_dbg_lvl;
7264 rval = driver_create_file(&megasas_pci_driver.driver,
7265 &driver_attr_support_device_change);
7266 if (rval)
7267 goto err_dcf_support_device_change;
7269 return rval;
7271 err_dcf_support_device_change:
7272 driver_remove_file(&megasas_pci_driver.driver,
7273 &driver_attr_dbg_lvl);
7274 err_dcf_dbg_lvl:
7275 driver_remove_file(&megasas_pci_driver.driver,
7276 &driver_attr_support_poll_for_event);
7277 err_dcf_support_poll_for_event:
7278 driver_remove_file(&megasas_pci_driver.driver,
7279 &driver_attr_release_date);
7280 err_dcf_rel_date:
7281 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7282 err_dcf_attr_ver:
7283 pci_unregister_driver(&megasas_pci_driver);
7284 err_pcidrv:
7285 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7286 return rval;
7290 * megasas_exit - Driver unload entry point
7292 static void __exit megasas_exit(void)
7294 driver_remove_file(&megasas_pci_driver.driver,
7295 &driver_attr_dbg_lvl);
7296 driver_remove_file(&megasas_pci_driver.driver,
7297 &driver_attr_support_poll_for_event);
7298 driver_remove_file(&megasas_pci_driver.driver,
7299 &driver_attr_support_device_change);
7300 driver_remove_file(&megasas_pci_driver.driver,
7301 &driver_attr_release_date);
7302 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
7304 pci_unregister_driver(&megasas_pci_driver);
7305 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
7308 module_init(megasas_init);
7309 module_exit(megasas_exit);