d3f4b48be1cd00812b4e1961af2fa6faf5051bbb
[linux-2.6-microblaze.git] / drivers / scsi / megaraid / megaraid_sas_base.c
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2003-2013  LSI Corporation
5  *  Copyright (c) 2013-2016  Avago Technologies
6  *  Copyright (c) 2016-2018  Broadcom Inc.
7  *
8  *  This program is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU General Public License
10  *  as published by the Free Software Foundation; either version 2
11  *  of the License, or (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  *  Authors: Broadcom Inc.
22  *           Sreenivas Bagalkote
23  *           Sumant Patro
24  *           Bo Yang
25  *           Adam Radford
26  *           Kashyap Desai <kashyap.desai@broadcom.com>
27  *           Sumit Saxena <sumit.saxena@broadcom.com>
28  *
29  *  Send feedback to: megaraidlinux.pdl@broadcom.com
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <asm/unaligned.h>
45 #include <linux/fs.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
50 #include <linux/vmalloc.h>
51 #include <linux/irq_poll.h>
52
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include <scsi/scsi_dbg.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
61
62 /*
63  * Number of sectors per IO command
64  * Will be set in megasas_init_mfi if user does not provide
65  */
66 static unsigned int max_sectors;
67 module_param_named(max_sectors, max_sectors, int, 0);
68 MODULE_PARM_DESC(max_sectors,
69         "Maximum number of sectors per IO command");
70
71 static int msix_disable;
72 module_param(msix_disable, int, S_IRUGO);
73 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
74
75 static unsigned int msix_vectors;
76 module_param(msix_vectors, int, S_IRUGO);
77 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
78
79 static int allow_vf_ioctls;
80 module_param(allow_vf_ioctls, int, S_IRUGO);
81 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
82
83 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
84 module_param(throttlequeuedepth, int, S_IRUGO);
85 MODULE_PARM_DESC(throttlequeuedepth,
86         "Adapter queue depth when throttled due to I/O timeout. Default: 16");
87
88 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
89 module_param(resetwaittime, int, S_IRUGO);
90 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
91
92 int smp_affinity_enable = 1;
93 module_param(smp_affinity_enable, int, S_IRUGO);
94 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
95
96 int rdpq_enable = 1;
97 module_param(rdpq_enable, int, S_IRUGO);
98 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
99
100 unsigned int dual_qdepth_disable;
101 module_param(dual_qdepth_disable, int, S_IRUGO);
102 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
103
104 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
105 module_param(scmd_timeout, int, S_IRUGO);
106 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
107
108 MODULE_LICENSE("GPL");
109 MODULE_VERSION(MEGASAS_VERSION);
110 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
111 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
112
113 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
114 static int megasas_get_pd_list(struct megasas_instance *instance);
115 static int megasas_ld_list_query(struct megasas_instance *instance,
116                                  u8 query_type);
117 static int megasas_issue_init_mfi(struct megasas_instance *instance);
118 static int megasas_register_aen(struct megasas_instance *instance,
119                                 u32 seq_num, u32 class_locale_word);
120 static void megasas_get_pd_info(struct megasas_instance *instance,
121                                 struct scsi_device *sdev);
122
123 /*
124  * PCI ID table for all supported controllers
125  */
126 static struct pci_device_id megasas_pci_table[] = {
127
128         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
129         /* xscale IOP */
130         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
131         /* ppc IOP */
132         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
133         /* ppc IOP */
134         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
135         /* gen2*/
136         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
137         /* gen2*/
138         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
139         /* skinny*/
140         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
141         /* skinny*/
142         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
143         /* xscale IOP, vega */
144         {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
145         /* xscale IOP */
146         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
147         /* Fusion */
148         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
149         /* Plasma */
150         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
151         /* Invader */
152         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
153         /* Fury */
154         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
155         /* Intruder */
156         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
157         /* Intruder 24 port*/
158         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
159         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
160         /* VENTURA */
161         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
162         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
163         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
164         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
165         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
166         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
167         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
168         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
169         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
170         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
171         {}
172 };
173
174 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
175
176 static int megasas_mgmt_majorno;
177 struct megasas_mgmt_info megasas_mgmt_info;
178 static struct fasync_struct *megasas_async_queue;
179 static DEFINE_MUTEX(megasas_async_queue_mutex);
180
181 static int megasas_poll_wait_aen;
182 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
183 static u32 support_poll_for_event;
184 u32 megasas_dbg_lvl;
185 static u32 support_device_change;
186 static bool support_nvme_encapsulation;
187
188 /* define lock for aen poll */
189 spinlock_t poll_aen_lock;
190
191 void
192 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
193                      u8 alt_status);
194 static u32
195 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
196 static int
197 megasas_adp_reset_gen2(struct megasas_instance *instance,
198                        struct megasas_register_set __iomem *reg_set);
199 static irqreturn_t megasas_isr(int irq, void *devp);
200 static u32
201 megasas_init_adapter_mfi(struct megasas_instance *instance);
202 u32
203 megasas_build_and_issue_cmd(struct megasas_instance *instance,
204                             struct scsi_cmnd *scmd);
205 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
206 int
207 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
208         int seconds);
209 void megasas_fusion_ocr_wq(struct work_struct *work);
210 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
211                                          int initial);
212 static int
213 megasas_set_dma_mask(struct megasas_instance *instance);
214 static int
215 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
216 static inline void
217 megasas_free_ctrl_mem(struct megasas_instance *instance);
218 static inline int
219 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
220 static inline void
221 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
222 static inline void
223 megasas_init_ctrl_params(struct megasas_instance *instance);
224
225 u32 megasas_readl(struct megasas_instance *instance,
226                   const volatile void __iomem *addr)
227 {
228         u32 i = 0, ret_val;
229         /*
230          * Due to a HW errata in Aero controllers, reads to certain
231          * Fusion registers could intermittently return all zeroes.
232          * This behavior is transient in nature and subsequent reads will
233          * return valid value. As a workaround in driver, retry readl for
234          * upto three times until a non-zero value is read.
235          */
236         if (instance->adapter_type == AERO_SERIES) {
237                 do {
238                         ret_val = readl(addr);
239                         i++;
240                 } while (ret_val == 0 && i < 3);
241                 return ret_val;
242         } else {
243                 return readl(addr);
244         }
245 }
246
247 /**
248  * megasas_set_dma_settings -   Populate DMA address, length and flags for DCMDs
249  * @instance:                   Adapter soft state
250  * @dcmd:                       DCMD frame inside MFI command
251  * @dma_addr:                   DMA address of buffer to be passed to FW
252  * @dma_len:                    Length of DMA buffer to be passed to FW
253  * @return:                     void
254  */
255 void megasas_set_dma_settings(struct megasas_instance *instance,
256                               struct megasas_dcmd_frame *dcmd,
257                               dma_addr_t dma_addr, u32 dma_len)
258 {
259         if (instance->consistent_mask_64bit) {
260                 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
261                 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
262                 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
263
264         } else {
265                 dcmd->sgl.sge32[0].phys_addr =
266                                 cpu_to_le32(lower_32_bits(dma_addr));
267                 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
268                 dcmd->flags = cpu_to_le16(dcmd->flags);
269         }
270 }
271
272 void
273 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
274 {
275         instance->instancet->fire_cmd(instance,
276                 cmd->frame_phys_addr, 0, instance->reg_set);
277         return;
278 }
279
280 /**
281  * megasas_get_cmd -    Get a command from the free pool
282  * @instance:           Adapter soft state
283  *
284  * Returns a free command from the pool
285  */
286 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
287                                                   *instance)
288 {
289         unsigned long flags;
290         struct megasas_cmd *cmd = NULL;
291
292         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
293
294         if (!list_empty(&instance->cmd_pool)) {
295                 cmd = list_entry((&instance->cmd_pool)->next,
296                                  struct megasas_cmd, list);
297                 list_del_init(&cmd->list);
298         } else {
299                 dev_err(&instance->pdev->dev, "Command pool empty!\n");
300         }
301
302         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
303         return cmd;
304 }
305
306 /**
307  * megasas_return_cmd - Return a cmd to free command pool
308  * @instance:           Adapter soft state
309  * @cmd:                Command packet to be returned to free command pool
310  */
311 void
312 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
313 {
314         unsigned long flags;
315         u32 blk_tags;
316         struct megasas_cmd_fusion *cmd_fusion;
317         struct fusion_context *fusion = instance->ctrl_context;
318
319         /* This flag is used only for fusion adapter.
320          * Wait for Interrupt for Polled mode DCMD
321          */
322         if (cmd->flags & DRV_DCMD_POLLED_MODE)
323                 return;
324
325         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
326
327         if (fusion) {
328                 blk_tags = instance->max_scsi_cmds + cmd->index;
329                 cmd_fusion = fusion->cmd_list[blk_tags];
330                 megasas_return_cmd_fusion(instance, cmd_fusion);
331         }
332         cmd->scmd = NULL;
333         cmd->frame_count = 0;
334         cmd->flags = 0;
335         memset(cmd->frame, 0, instance->mfi_frame_size);
336         cmd->frame->io.context = cpu_to_le32(cmd->index);
337         if (!fusion && reset_devices)
338                 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
339         list_add(&cmd->list, (&instance->cmd_pool)->next);
340
341         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
342
343 }
344
345 static const char *
346 format_timestamp(uint32_t timestamp)
347 {
348         static char buffer[32];
349
350         if ((timestamp & 0xff000000) == 0xff000000)
351                 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
352                 0x00ffffff);
353         else
354                 snprintf(buffer, sizeof(buffer), "%us", timestamp);
355         return buffer;
356 }
357
358 static const char *
359 format_class(int8_t class)
360 {
361         static char buffer[6];
362
363         switch (class) {
364         case MFI_EVT_CLASS_DEBUG:
365                 return "debug";
366         case MFI_EVT_CLASS_PROGRESS:
367                 return "progress";
368         case MFI_EVT_CLASS_INFO:
369                 return "info";
370         case MFI_EVT_CLASS_WARNING:
371                 return "WARN";
372         case MFI_EVT_CLASS_CRITICAL:
373                 return "CRIT";
374         case MFI_EVT_CLASS_FATAL:
375                 return "FATAL";
376         case MFI_EVT_CLASS_DEAD:
377                 return "DEAD";
378         default:
379                 snprintf(buffer, sizeof(buffer), "%d", class);
380                 return buffer;
381         }
382 }
383
384 /**
385   * megasas_decode_evt: Decode FW AEN event and print critical event
386   * for information.
387   * @instance:                  Adapter soft state
388   */
389 static void
390 megasas_decode_evt(struct megasas_instance *instance)
391 {
392         struct megasas_evt_detail *evt_detail = instance->evt_detail;
393         union megasas_evt_class_locale class_locale;
394         class_locale.word = le32_to_cpu(evt_detail->cl.word);
395
396         if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
397                 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
398                         le32_to_cpu(evt_detail->seq_num),
399                         format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
400                         (class_locale.members.locale),
401                         format_class(class_locale.members.class),
402                         evt_detail->description);
403 }
404
405 /**
406 *       The following functions are defined for xscale
407 *       (deviceid : 1064R, PERC5) controllers
408 */
409
410 /**
411  * megasas_enable_intr_xscale - Enables interrupts
412  * @regs:                       MFI register set
413  */
414 static inline void
415 megasas_enable_intr_xscale(struct megasas_instance *instance)
416 {
417         struct megasas_register_set __iomem *regs;
418
419         regs = instance->reg_set;
420         writel(0, &(regs)->outbound_intr_mask);
421
422         /* Dummy readl to force pci flush */
423         readl(&regs->outbound_intr_mask);
424 }
425
426 /**
427  * megasas_disable_intr_xscale -Disables interrupt
428  * @regs:                       MFI register set
429  */
430 static inline void
431 megasas_disable_intr_xscale(struct megasas_instance *instance)
432 {
433         struct megasas_register_set __iomem *regs;
434         u32 mask = 0x1f;
435
436         regs = instance->reg_set;
437         writel(mask, &regs->outbound_intr_mask);
438         /* Dummy readl to force pci flush */
439         readl(&regs->outbound_intr_mask);
440 }
441
442 /**
443  * megasas_read_fw_status_reg_xscale - returns the current FW status value
444  * @regs:                       MFI register set
445  */
446 static u32
447 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
448 {
449         return readl(&instance->reg_set->outbound_msg_0);
450 }
451 /**
452  * megasas_clear_interrupt_xscale -     Check & clear interrupt
453  * @regs:                               MFI register set
454  */
455 static int
456 megasas_clear_intr_xscale(struct megasas_instance *instance)
457 {
458         u32 status;
459         u32 mfiStatus = 0;
460         struct megasas_register_set __iomem *regs;
461         regs = instance->reg_set;
462
463         /*
464          * Check if it is our interrupt
465          */
466         status = readl(&regs->outbound_intr_status);
467
468         if (status & MFI_OB_INTR_STATUS_MASK)
469                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
470         if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
471                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
472
473         /*
474          * Clear the interrupt by writing back the same value
475          */
476         if (mfiStatus)
477                 writel(status, &regs->outbound_intr_status);
478
479         /* Dummy readl to force pci flush */
480         readl(&regs->outbound_intr_status);
481
482         return mfiStatus;
483 }
484
485 /**
486  * megasas_fire_cmd_xscale -    Sends command to the FW
487  * @frame_phys_addr :           Physical address of cmd
488  * @frame_count :               Number of frames for the command
489  * @regs :                      MFI register set
490  */
491 static inline void
492 megasas_fire_cmd_xscale(struct megasas_instance *instance,
493                 dma_addr_t frame_phys_addr,
494                 u32 frame_count,
495                 struct megasas_register_set __iomem *regs)
496 {
497         unsigned long flags;
498
499         spin_lock_irqsave(&instance->hba_lock, flags);
500         writel((frame_phys_addr >> 3)|(frame_count),
501                &(regs)->inbound_queue_port);
502         spin_unlock_irqrestore(&instance->hba_lock, flags);
503 }
504
505 /**
506  * megasas_adp_reset_xscale -  For controller reset
507  * @regs:                              MFI register set
508  */
509 static int
510 megasas_adp_reset_xscale(struct megasas_instance *instance,
511         struct megasas_register_set __iomem *regs)
512 {
513         u32 i;
514         u32 pcidata;
515
516         writel(MFI_ADP_RESET, &regs->inbound_doorbell);
517
518         for (i = 0; i < 3; i++)
519                 msleep(1000); /* sleep for 3 secs */
520         pcidata  = 0;
521         pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
522         dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
523         if (pcidata & 0x2) {
524                 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
525                 pcidata &= ~0x2;
526                 pci_write_config_dword(instance->pdev,
527                                 MFI_1068_PCSR_OFFSET, pcidata);
528
529                 for (i = 0; i < 2; i++)
530                         msleep(1000); /* need to wait 2 secs again */
531
532                 pcidata  = 0;
533                 pci_read_config_dword(instance->pdev,
534                                 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
535                 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
536                 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
537                         dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
538                         pcidata = 0;
539                         pci_write_config_dword(instance->pdev,
540                                 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
541                 }
542         }
543         return 0;
544 }
545
546 /**
547  * megasas_check_reset_xscale - For controller reset check
548  * @regs:                               MFI register set
549  */
550 static int
551 megasas_check_reset_xscale(struct megasas_instance *instance,
552                 struct megasas_register_set __iomem *regs)
553 {
554         if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
555             (le32_to_cpu(*instance->consumer) ==
556                 MEGASAS_ADPRESET_INPROG_SIGN))
557                 return 1;
558         return 0;
559 }
560
561 static struct megasas_instance_template megasas_instance_template_xscale = {
562
563         .fire_cmd = megasas_fire_cmd_xscale,
564         .enable_intr = megasas_enable_intr_xscale,
565         .disable_intr = megasas_disable_intr_xscale,
566         .clear_intr = megasas_clear_intr_xscale,
567         .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
568         .adp_reset = megasas_adp_reset_xscale,
569         .check_reset = megasas_check_reset_xscale,
570         .service_isr = megasas_isr,
571         .tasklet = megasas_complete_cmd_dpc,
572         .init_adapter = megasas_init_adapter_mfi,
573         .build_and_issue_cmd = megasas_build_and_issue_cmd,
574         .issue_dcmd = megasas_issue_dcmd,
575 };
576
577 /**
578 *       This is the end of set of functions & definitions specific
579 *       to xscale (deviceid : 1064R, PERC5) controllers
580 */
581
582 /**
583 *       The following functions are defined for ppc (deviceid : 0x60)
584 *       controllers
585 */
586
587 /**
588  * megasas_enable_intr_ppc -    Enables interrupts
589  * @regs:                       MFI register set
590  */
591 static inline void
592 megasas_enable_intr_ppc(struct megasas_instance *instance)
593 {
594         struct megasas_register_set __iomem *regs;
595
596         regs = instance->reg_set;
597         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
598
599         writel(~0x80000000, &(regs)->outbound_intr_mask);
600
601         /* Dummy readl to force pci flush */
602         readl(&regs->outbound_intr_mask);
603 }
604
605 /**
606  * megasas_disable_intr_ppc -   Disable interrupt
607  * @regs:                       MFI register set
608  */
609 static inline void
610 megasas_disable_intr_ppc(struct megasas_instance *instance)
611 {
612         struct megasas_register_set __iomem *regs;
613         u32 mask = 0xFFFFFFFF;
614
615         regs = instance->reg_set;
616         writel(mask, &regs->outbound_intr_mask);
617         /* Dummy readl to force pci flush */
618         readl(&regs->outbound_intr_mask);
619 }
620
621 /**
622  * megasas_read_fw_status_reg_ppc - returns the current FW status value
623  * @regs:                       MFI register set
624  */
625 static u32
626 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
627 {
628         return readl(&instance->reg_set->outbound_scratch_pad_0);
629 }
630
631 /**
632  * megasas_clear_interrupt_ppc -        Check & clear interrupt
633  * @regs:                               MFI register set
634  */
635 static int
636 megasas_clear_intr_ppc(struct megasas_instance *instance)
637 {
638         u32 status, mfiStatus = 0;
639         struct megasas_register_set __iomem *regs;
640         regs = instance->reg_set;
641
642         /*
643          * Check if it is our interrupt
644          */
645         status = readl(&regs->outbound_intr_status);
646
647         if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
648                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
649
650         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
651                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
652
653         /*
654          * Clear the interrupt by writing back the same value
655          */
656         writel(status, &regs->outbound_doorbell_clear);
657
658         /* Dummy readl to force pci flush */
659         readl(&regs->outbound_doorbell_clear);
660
661         return mfiStatus;
662 }
663
664 /**
665  * megasas_fire_cmd_ppc -       Sends command to the FW
666  * @frame_phys_addr :           Physical address of cmd
667  * @frame_count :               Number of frames for the command
668  * @regs :                      MFI register set
669  */
670 static inline void
671 megasas_fire_cmd_ppc(struct megasas_instance *instance,
672                 dma_addr_t frame_phys_addr,
673                 u32 frame_count,
674                 struct megasas_register_set __iomem *regs)
675 {
676         unsigned long flags;
677
678         spin_lock_irqsave(&instance->hba_lock, flags);
679         writel((frame_phys_addr | (frame_count<<1))|1,
680                         &(regs)->inbound_queue_port);
681         spin_unlock_irqrestore(&instance->hba_lock, flags);
682 }
683
684 /**
685  * megasas_check_reset_ppc -    For controller reset check
686  * @regs:                               MFI register set
687  */
688 static int
689 megasas_check_reset_ppc(struct megasas_instance *instance,
690                         struct megasas_register_set __iomem *regs)
691 {
692         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
693                 return 1;
694
695         return 0;
696 }
697
698 static struct megasas_instance_template megasas_instance_template_ppc = {
699
700         .fire_cmd = megasas_fire_cmd_ppc,
701         .enable_intr = megasas_enable_intr_ppc,
702         .disable_intr = megasas_disable_intr_ppc,
703         .clear_intr = megasas_clear_intr_ppc,
704         .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
705         .adp_reset = megasas_adp_reset_xscale,
706         .check_reset = megasas_check_reset_ppc,
707         .service_isr = megasas_isr,
708         .tasklet = megasas_complete_cmd_dpc,
709         .init_adapter = megasas_init_adapter_mfi,
710         .build_and_issue_cmd = megasas_build_and_issue_cmd,
711         .issue_dcmd = megasas_issue_dcmd,
712 };
713
714 /**
715  * megasas_enable_intr_skinny - Enables interrupts
716  * @regs:                       MFI register set
717  */
718 static inline void
719 megasas_enable_intr_skinny(struct megasas_instance *instance)
720 {
721         struct megasas_register_set __iomem *regs;
722
723         regs = instance->reg_set;
724         writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
725
726         writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
727
728         /* Dummy readl to force pci flush */
729         readl(&regs->outbound_intr_mask);
730 }
731
732 /**
733  * megasas_disable_intr_skinny -        Disables interrupt
734  * @regs:                       MFI register set
735  */
736 static inline void
737 megasas_disable_intr_skinny(struct megasas_instance *instance)
738 {
739         struct megasas_register_set __iomem *regs;
740         u32 mask = 0xFFFFFFFF;
741
742         regs = instance->reg_set;
743         writel(mask, &regs->outbound_intr_mask);
744         /* Dummy readl to force pci flush */
745         readl(&regs->outbound_intr_mask);
746 }
747
748 /**
749  * megasas_read_fw_status_reg_skinny - returns the current FW status value
750  * @regs:                       MFI register set
751  */
752 static u32
753 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
754 {
755         return readl(&instance->reg_set->outbound_scratch_pad_0);
756 }
757
758 /**
759  * megasas_clear_interrupt_skinny -     Check & clear interrupt
760  * @regs:                               MFI register set
761  */
762 static int
763 megasas_clear_intr_skinny(struct megasas_instance *instance)
764 {
765         u32 status;
766         u32 mfiStatus = 0;
767         struct megasas_register_set __iomem *regs;
768         regs = instance->reg_set;
769
770         /*
771          * Check if it is our interrupt
772          */
773         status = readl(&regs->outbound_intr_status);
774
775         if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
776                 return 0;
777         }
778
779         /*
780          * Check if it is our interrupt
781          */
782         if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
783             MFI_STATE_FAULT) {
784                 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
785         } else
786                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
787
788         /*
789          * Clear the interrupt by writing back the same value
790          */
791         writel(status, &regs->outbound_intr_status);
792
793         /*
794          * dummy read to flush PCI
795          */
796         readl(&regs->outbound_intr_status);
797
798         return mfiStatus;
799 }
800
801 /**
802  * megasas_fire_cmd_skinny -    Sends command to the FW
803  * @frame_phys_addr :           Physical address of cmd
804  * @frame_count :               Number of frames for the command
805  * @regs :                      MFI register set
806  */
807 static inline void
808 megasas_fire_cmd_skinny(struct megasas_instance *instance,
809                         dma_addr_t frame_phys_addr,
810                         u32 frame_count,
811                         struct megasas_register_set __iomem *regs)
812 {
813         unsigned long flags;
814
815         spin_lock_irqsave(&instance->hba_lock, flags);
816         writel(upper_32_bits(frame_phys_addr),
817                &(regs)->inbound_high_queue_port);
818         writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
819                &(regs)->inbound_low_queue_port);
820         spin_unlock_irqrestore(&instance->hba_lock, flags);
821 }
822
823 /**
824  * megasas_check_reset_skinny - For controller reset check
825  * @regs:                               MFI register set
826  */
827 static int
828 megasas_check_reset_skinny(struct megasas_instance *instance,
829                                 struct megasas_register_set __iomem *regs)
830 {
831         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
832                 return 1;
833
834         return 0;
835 }
836
837 static struct megasas_instance_template megasas_instance_template_skinny = {
838
839         .fire_cmd = megasas_fire_cmd_skinny,
840         .enable_intr = megasas_enable_intr_skinny,
841         .disable_intr = megasas_disable_intr_skinny,
842         .clear_intr = megasas_clear_intr_skinny,
843         .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
844         .adp_reset = megasas_adp_reset_gen2,
845         .check_reset = megasas_check_reset_skinny,
846         .service_isr = megasas_isr,
847         .tasklet = megasas_complete_cmd_dpc,
848         .init_adapter = megasas_init_adapter_mfi,
849         .build_and_issue_cmd = megasas_build_and_issue_cmd,
850         .issue_dcmd = megasas_issue_dcmd,
851 };
852
853
854 /**
855 *       The following functions are defined for gen2 (deviceid : 0x78 0x79)
856 *       controllers
857 */
858
859 /**
860  * megasas_enable_intr_gen2 -  Enables interrupts
861  * @regs:                      MFI register set
862  */
863 static inline void
864 megasas_enable_intr_gen2(struct megasas_instance *instance)
865 {
866         struct megasas_register_set __iomem *regs;
867
868         regs = instance->reg_set;
869         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
870
871         /* write ~0x00000005 (4 & 1) to the intr mask*/
872         writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
873
874         /* Dummy readl to force pci flush */
875         readl(&regs->outbound_intr_mask);
876 }
877
878 /**
879  * megasas_disable_intr_gen2 - Disables interrupt
880  * @regs:                      MFI register set
881  */
882 static inline void
883 megasas_disable_intr_gen2(struct megasas_instance *instance)
884 {
885         struct megasas_register_set __iomem *regs;
886         u32 mask = 0xFFFFFFFF;
887
888         regs = instance->reg_set;
889         writel(mask, &regs->outbound_intr_mask);
890         /* Dummy readl to force pci flush */
891         readl(&regs->outbound_intr_mask);
892 }
893
894 /**
895  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
896  * @regs:                      MFI register set
897  */
898 static u32
899 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
900 {
901         return readl(&instance->reg_set->outbound_scratch_pad_0);
902 }
903
904 /**
905  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
906  * @regs:                              MFI register set
907  */
908 static int
909 megasas_clear_intr_gen2(struct megasas_instance *instance)
910 {
911         u32 status;
912         u32 mfiStatus = 0;
913         struct megasas_register_set __iomem *regs;
914         regs = instance->reg_set;
915
916         /*
917          * Check if it is our interrupt
918          */
919         status = readl(&regs->outbound_intr_status);
920
921         if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
922                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
923         }
924         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
925                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
926         }
927
928         /*
929          * Clear the interrupt by writing back the same value
930          */
931         if (mfiStatus)
932                 writel(status, &regs->outbound_doorbell_clear);
933
934         /* Dummy readl to force pci flush */
935         readl(&regs->outbound_intr_status);
936
937         return mfiStatus;
938 }
939 /**
940  * megasas_fire_cmd_gen2 -     Sends command to the FW
941  * @frame_phys_addr :          Physical address of cmd
942  * @frame_count :              Number of frames for the command
943  * @regs :                     MFI register set
944  */
945 static inline void
946 megasas_fire_cmd_gen2(struct megasas_instance *instance,
947                         dma_addr_t frame_phys_addr,
948                         u32 frame_count,
949                         struct megasas_register_set __iomem *regs)
950 {
951         unsigned long flags;
952
953         spin_lock_irqsave(&instance->hba_lock, flags);
954         writel((frame_phys_addr | (frame_count<<1))|1,
955                         &(regs)->inbound_queue_port);
956         spin_unlock_irqrestore(&instance->hba_lock, flags);
957 }
958
959 /**
960  * megasas_adp_reset_gen2 -     For controller reset
961  * @regs:                               MFI register set
962  */
963 static int
964 megasas_adp_reset_gen2(struct megasas_instance *instance,
965                         struct megasas_register_set __iomem *reg_set)
966 {
967         u32 retry = 0 ;
968         u32 HostDiag;
969         u32 __iomem *seq_offset = &reg_set->seq_offset;
970         u32 __iomem *hostdiag_offset = &reg_set->host_diag;
971
972         if (instance->instancet == &megasas_instance_template_skinny) {
973                 seq_offset = &reg_set->fusion_seq_offset;
974                 hostdiag_offset = &reg_set->fusion_host_diag;
975         }
976
977         writel(0, seq_offset);
978         writel(4, seq_offset);
979         writel(0xb, seq_offset);
980         writel(2, seq_offset);
981         writel(7, seq_offset);
982         writel(0xd, seq_offset);
983
984         msleep(1000);
985
986         HostDiag = (u32)readl(hostdiag_offset);
987
988         while (!(HostDiag & DIAG_WRITE_ENABLE)) {
989                 msleep(100);
990                 HostDiag = (u32)readl(hostdiag_offset);
991                 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
992                                         retry, HostDiag);
993
994                 if (retry++ >= 100)
995                         return 1;
996
997         }
998
999         dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1000
1001         writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1002
1003         ssleep(10);
1004
1005         HostDiag = (u32)readl(hostdiag_offset);
1006         while (HostDiag & DIAG_RESET_ADAPTER) {
1007                 msleep(100);
1008                 HostDiag = (u32)readl(hostdiag_offset);
1009                 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1010                                 retry, HostDiag);
1011
1012                 if (retry++ >= 1000)
1013                         return 1;
1014
1015         }
1016         return 0;
1017 }
1018
1019 /**
1020  * megasas_check_reset_gen2 -   For controller reset check
1021  * @regs:                               MFI register set
1022  */
1023 static int
1024 megasas_check_reset_gen2(struct megasas_instance *instance,
1025                 struct megasas_register_set __iomem *regs)
1026 {
1027         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1028                 return 1;
1029
1030         return 0;
1031 }
1032
1033 static struct megasas_instance_template megasas_instance_template_gen2 = {
1034
1035         .fire_cmd = megasas_fire_cmd_gen2,
1036         .enable_intr = megasas_enable_intr_gen2,
1037         .disable_intr = megasas_disable_intr_gen2,
1038         .clear_intr = megasas_clear_intr_gen2,
1039         .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1040         .adp_reset = megasas_adp_reset_gen2,
1041         .check_reset = megasas_check_reset_gen2,
1042         .service_isr = megasas_isr,
1043         .tasklet = megasas_complete_cmd_dpc,
1044         .init_adapter = megasas_init_adapter_mfi,
1045         .build_and_issue_cmd = megasas_build_and_issue_cmd,
1046         .issue_dcmd = megasas_issue_dcmd,
1047 };
1048
1049 /**
1050 *       This is the end of set of functions & definitions
1051 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
1052 */
1053
1054 /*
1055  * Template added for TB (Fusion)
1056  */
1057 extern struct megasas_instance_template megasas_instance_template_fusion;
1058
1059 /**
1060  * megasas_issue_polled -       Issues a polling command
1061  * @instance:                   Adapter soft state
1062  * @cmd:                        Command packet to be issued
1063  *
1064  * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1065  */
1066 int
1067 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1068 {
1069         struct megasas_header *frame_hdr = &cmd->frame->hdr;
1070
1071         frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1072         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1073
1074         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1075                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1076                         __func__, __LINE__);
1077                 return DCMD_NOT_FIRED;
1078         }
1079
1080         instance->instancet->issue_dcmd(instance, cmd);
1081
1082         return wait_and_poll(instance, cmd, instance->requestorId ?
1083                         MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1084 }
1085
1086 /**
1087  * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
1088  * @instance:                   Adapter soft state
1089  * @cmd:                        Command to be issued
1090  * @timeout:                    Timeout in seconds
1091  *
1092  * This function waits on an event for the command to be returned from ISR.
1093  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1094  * Used to issue ioctl commands.
1095  */
1096 int
1097 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1098                           struct megasas_cmd *cmd, int timeout)
1099 {
1100         int ret = 0;
1101         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1102
1103         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1104                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1105                         __func__, __LINE__);
1106                 return DCMD_NOT_FIRED;
1107         }
1108
1109         instance->instancet->issue_dcmd(instance, cmd);
1110
1111         if (timeout) {
1112                 ret = wait_event_timeout(instance->int_cmd_wait_q,
1113                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1114                 if (!ret) {
1115                         dev_err(&instance->pdev->dev,
1116                                 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1117                                 cmd->frame->dcmd.opcode, __func__);
1118                         return DCMD_TIMEOUT;
1119                 }
1120         } else
1121                 wait_event(instance->int_cmd_wait_q,
1122                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1123
1124         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1125                 DCMD_SUCCESS : DCMD_FAILED;
1126 }
1127
1128 /**
1129  * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
1130  * @instance:                           Adapter soft state
1131  * @cmd_to_abort:                       Previously issued cmd to be aborted
1132  * @timeout:                            Timeout in seconds
1133  *
1134  * MFI firmware can abort previously issued AEN comamnd (automatic event
1135  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1136  * cmd and waits for return status.
1137  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1138  */
1139 static int
1140 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1141                                 struct megasas_cmd *cmd_to_abort, int timeout)
1142 {
1143         struct megasas_cmd *cmd;
1144         struct megasas_abort_frame *abort_fr;
1145         int ret = 0;
1146         u32 opcode;
1147
1148         cmd = megasas_get_cmd(instance);
1149
1150         if (!cmd)
1151                 return -1;
1152
1153         abort_fr = &cmd->frame->abort;
1154
1155         /*
1156          * Prepare and issue the abort frame
1157          */
1158         abort_fr->cmd = MFI_CMD_ABORT;
1159         abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1160         abort_fr->flags = cpu_to_le16(0);
1161         abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1162         abort_fr->abort_mfi_phys_addr_lo =
1163                 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1164         abort_fr->abort_mfi_phys_addr_hi =
1165                 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1166
1167         cmd->sync_cmd = 1;
1168         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1169
1170         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1171                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1172                         __func__, __LINE__);
1173                 return DCMD_NOT_FIRED;
1174         }
1175
1176         instance->instancet->issue_dcmd(instance, cmd);
1177
1178         if (timeout) {
1179                 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1180                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1181                 if (!ret) {
1182                         opcode = cmd_to_abort->frame->dcmd.opcode;
1183                         dev_err(&instance->pdev->dev,
1184                                 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1185                                 opcode,  __func__);
1186                         return DCMD_TIMEOUT;
1187                 }
1188         } else
1189                 wait_event(instance->abort_cmd_wait_q,
1190                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1191
1192         cmd->sync_cmd = 0;
1193
1194         megasas_return_cmd(instance, cmd);
1195         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1196                 DCMD_SUCCESS : DCMD_FAILED;
1197 }
1198
1199 /**
1200  * megasas_make_sgl32 - Prepares 32-bit SGL
1201  * @instance:           Adapter soft state
1202  * @scp:                SCSI command from the mid-layer
1203  * @mfi_sgl:            SGL to be filled in
1204  *
1205  * If successful, this function returns the number of SG elements. Otherwise,
1206  * it returnes -1.
1207  */
1208 static int
1209 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1210                    union megasas_sgl *mfi_sgl)
1211 {
1212         int i;
1213         int sge_count;
1214         struct scatterlist *os_sgl;
1215
1216         sge_count = scsi_dma_map(scp);
1217         BUG_ON(sge_count < 0);
1218
1219         if (sge_count) {
1220                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1221                         mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1222                         mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1223                 }
1224         }
1225         return sge_count;
1226 }
1227
1228 /**
1229  * megasas_make_sgl64 - Prepares 64-bit SGL
1230  * @instance:           Adapter soft state
1231  * @scp:                SCSI command from the mid-layer
1232  * @mfi_sgl:            SGL to be filled in
1233  *
1234  * If successful, this function returns the number of SG elements. Otherwise,
1235  * it returnes -1.
1236  */
1237 static int
1238 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1239                    union megasas_sgl *mfi_sgl)
1240 {
1241         int i;
1242         int sge_count;
1243         struct scatterlist *os_sgl;
1244
1245         sge_count = scsi_dma_map(scp);
1246         BUG_ON(sge_count < 0);
1247
1248         if (sge_count) {
1249                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1250                         mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1251                         mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1252                 }
1253         }
1254         return sge_count;
1255 }
1256
1257 /**
1258  * megasas_make_sgl_skinny - Prepares IEEE SGL
1259  * @instance:           Adapter soft state
1260  * @scp:                SCSI command from the mid-layer
1261  * @mfi_sgl:            SGL to be filled in
1262  *
1263  * If successful, this function returns the number of SG elements. Otherwise,
1264  * it returnes -1.
1265  */
1266 static int
1267 megasas_make_sgl_skinny(struct megasas_instance *instance,
1268                 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1269 {
1270         int i;
1271         int sge_count;
1272         struct scatterlist *os_sgl;
1273
1274         sge_count = scsi_dma_map(scp);
1275
1276         if (sge_count) {
1277                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1278                         mfi_sgl->sge_skinny[i].length =
1279                                 cpu_to_le32(sg_dma_len(os_sgl));
1280                         mfi_sgl->sge_skinny[i].phys_addr =
1281                                 cpu_to_le64(sg_dma_address(os_sgl));
1282                         mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1283                 }
1284         }
1285         return sge_count;
1286 }
1287
1288  /**
1289  * megasas_get_frame_count - Computes the number of frames
1290  * @frame_type          : type of frame- io or pthru frame
1291  * @sge_count           : number of sg elements
1292  *
1293  * Returns the number of frames required for numnber of sge's (sge_count)
1294  */
1295
1296 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1297                         u8 sge_count, u8 frame_type)
1298 {
1299         int num_cnt;
1300         int sge_bytes;
1301         u32 sge_sz;
1302         u32 frame_count = 0;
1303
1304         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1305             sizeof(struct megasas_sge32);
1306
1307         if (instance->flag_ieee) {
1308                 sge_sz = sizeof(struct megasas_sge_skinny);
1309         }
1310
1311         /*
1312          * Main frame can contain 2 SGEs for 64-bit SGLs and
1313          * 3 SGEs for 32-bit SGLs for ldio &
1314          * 1 SGEs for 64-bit SGLs and
1315          * 2 SGEs for 32-bit SGLs for pthru frame
1316          */
1317         if (unlikely(frame_type == PTHRU_FRAME)) {
1318                 if (instance->flag_ieee == 1) {
1319                         num_cnt = sge_count - 1;
1320                 } else if (IS_DMA64)
1321                         num_cnt = sge_count - 1;
1322                 else
1323                         num_cnt = sge_count - 2;
1324         } else {
1325                 if (instance->flag_ieee == 1) {
1326                         num_cnt = sge_count - 1;
1327                 } else if (IS_DMA64)
1328                         num_cnt = sge_count - 2;
1329                 else
1330                         num_cnt = sge_count - 3;
1331         }
1332
1333         if (num_cnt > 0) {
1334                 sge_bytes = sge_sz * num_cnt;
1335
1336                 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1337                     ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1338         }
1339         /* Main frame */
1340         frame_count += 1;
1341
1342         if (frame_count > 7)
1343                 frame_count = 8;
1344         return frame_count;
1345 }
1346
1347 /**
1348  * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1349  * @instance:           Adapter soft state
1350  * @scp:                SCSI command
1351  * @cmd:                Command to be prepared in
1352  *
1353  * This function prepares CDB commands. These are typcially pass-through
1354  * commands to the devices.
1355  */
1356 static int
1357 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358                    struct megasas_cmd *cmd)
1359 {
1360         u32 is_logical;
1361         u32 device_id;
1362         u16 flags = 0;
1363         struct megasas_pthru_frame *pthru;
1364
1365         is_logical = MEGASAS_IS_LOGICAL(scp->device);
1366         device_id = MEGASAS_DEV_INDEX(scp);
1367         pthru = (struct megasas_pthru_frame *)cmd->frame;
1368
1369         if (scp->sc_data_direction == DMA_TO_DEVICE)
1370                 flags = MFI_FRAME_DIR_WRITE;
1371         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1372                 flags = MFI_FRAME_DIR_READ;
1373         else if (scp->sc_data_direction == DMA_NONE)
1374                 flags = MFI_FRAME_DIR_NONE;
1375
1376         if (instance->flag_ieee == 1) {
1377                 flags |= MFI_FRAME_IEEE;
1378         }
1379
1380         /*
1381          * Prepare the DCDB frame
1382          */
1383         pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1384         pthru->cmd_status = 0x0;
1385         pthru->scsi_status = 0x0;
1386         pthru->target_id = device_id;
1387         pthru->lun = scp->device->lun;
1388         pthru->cdb_len = scp->cmd_len;
1389         pthru->timeout = 0;
1390         pthru->pad_0 = 0;
1391         pthru->flags = cpu_to_le16(flags);
1392         pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1393
1394         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1395
1396         /*
1397          * If the command is for the tape device, set the
1398          * pthru timeout to the os layer timeout value.
1399          */
1400         if (scp->device->type == TYPE_TAPE) {
1401                 if ((scp->request->timeout / HZ) > 0xFFFF)
1402                         pthru->timeout = cpu_to_le16(0xFFFF);
1403                 else
1404                         pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1405         }
1406
1407         /*
1408          * Construct SGL
1409          */
1410         if (instance->flag_ieee == 1) {
1411                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1412                 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1413                                                       &pthru->sgl);
1414         } else if (IS_DMA64) {
1415                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1416                 pthru->sge_count = megasas_make_sgl64(instance, scp,
1417                                                       &pthru->sgl);
1418         } else
1419                 pthru->sge_count = megasas_make_sgl32(instance, scp,
1420                                                       &pthru->sgl);
1421
1422         if (pthru->sge_count > instance->max_num_sge) {
1423                 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1424                         pthru->sge_count);
1425                 return 0;
1426         }
1427
1428         /*
1429          * Sense info specific
1430          */
1431         pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1432         pthru->sense_buf_phys_addr_hi =
1433                 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1434         pthru->sense_buf_phys_addr_lo =
1435                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1436
1437         /*
1438          * Compute the total number of frames this command consumes. FW uses
1439          * this number to pull sufficient number of frames from host memory.
1440          */
1441         cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1442                                                         PTHRU_FRAME);
1443
1444         return cmd->frame_count;
1445 }
1446
1447 /**
1448  * megasas_build_ldio - Prepares IOs to logical devices
1449  * @instance:           Adapter soft state
1450  * @scp:                SCSI command
1451  * @cmd:                Command to be prepared
1452  *
1453  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1454  */
1455 static int
1456 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1457                    struct megasas_cmd *cmd)
1458 {
1459         u32 device_id;
1460         u8 sc = scp->cmnd[0];
1461         u16 flags = 0;
1462         struct megasas_io_frame *ldio;
1463
1464         device_id = MEGASAS_DEV_INDEX(scp);
1465         ldio = (struct megasas_io_frame *)cmd->frame;
1466
1467         if (scp->sc_data_direction == DMA_TO_DEVICE)
1468                 flags = MFI_FRAME_DIR_WRITE;
1469         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1470                 flags = MFI_FRAME_DIR_READ;
1471
1472         if (instance->flag_ieee == 1) {
1473                 flags |= MFI_FRAME_IEEE;
1474         }
1475
1476         /*
1477          * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1478          */
1479         ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1480         ldio->cmd_status = 0x0;
1481         ldio->scsi_status = 0x0;
1482         ldio->target_id = device_id;
1483         ldio->timeout = 0;
1484         ldio->reserved_0 = 0;
1485         ldio->pad_0 = 0;
1486         ldio->flags = cpu_to_le16(flags);
1487         ldio->start_lba_hi = 0;
1488         ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1489
1490         /*
1491          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1492          */
1493         if (scp->cmd_len == 6) {
1494                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1495                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1496                                                  ((u32) scp->cmnd[2] << 8) |
1497                                                  (u32) scp->cmnd[3]);
1498
1499                 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1500         }
1501
1502         /*
1503          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1504          */
1505         else if (scp->cmd_len == 10) {
1506                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1507                                               ((u32) scp->cmnd[7] << 8));
1508                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1509                                                  ((u32) scp->cmnd[3] << 16) |
1510                                                  ((u32) scp->cmnd[4] << 8) |
1511                                                  (u32) scp->cmnd[5]);
1512         }
1513
1514         /*
1515          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1516          */
1517         else if (scp->cmd_len == 12) {
1518                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1519                                               ((u32) scp->cmnd[7] << 16) |
1520                                               ((u32) scp->cmnd[8] << 8) |
1521                                               (u32) scp->cmnd[9]);
1522
1523                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1524                                                  ((u32) scp->cmnd[3] << 16) |
1525                                                  ((u32) scp->cmnd[4] << 8) |
1526                                                  (u32) scp->cmnd[5]);
1527         }
1528
1529         /*
1530          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1531          */
1532         else if (scp->cmd_len == 16) {
1533                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1534                                               ((u32) scp->cmnd[11] << 16) |
1535                                               ((u32) scp->cmnd[12] << 8) |
1536                                               (u32) scp->cmnd[13]);
1537
1538                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1539                                                  ((u32) scp->cmnd[7] << 16) |
1540                                                  ((u32) scp->cmnd[8] << 8) |
1541                                                  (u32) scp->cmnd[9]);
1542
1543                 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1544                                                  ((u32) scp->cmnd[3] << 16) |
1545                                                  ((u32) scp->cmnd[4] << 8) |
1546                                                  (u32) scp->cmnd[5]);
1547
1548         }
1549
1550         /*
1551          * Construct SGL
1552          */
1553         if (instance->flag_ieee) {
1554                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1555                 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1556                                               &ldio->sgl);
1557         } else if (IS_DMA64) {
1558                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1559                 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1560         } else
1561                 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1562
1563         if (ldio->sge_count > instance->max_num_sge) {
1564                 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1565                         ldio->sge_count);
1566                 return 0;
1567         }
1568
1569         /*
1570          * Sense info specific
1571          */
1572         ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1573         ldio->sense_buf_phys_addr_hi = 0;
1574         ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1575
1576         /*
1577          * Compute the total number of frames this command consumes. FW uses
1578          * this number to pull sufficient number of frames from host memory.
1579          */
1580         cmd->frame_count = megasas_get_frame_count(instance,
1581                         ldio->sge_count, IO_FRAME);
1582
1583         return cmd->frame_count;
1584 }
1585
1586 /**
1587  * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1588  *                              and whether it's RW or non RW
1589  * @scmd:                       SCSI command
1590  *
1591  */
1592 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1593 {
1594         int ret;
1595
1596         switch (cmd->cmnd[0]) {
1597         case READ_10:
1598         case WRITE_10:
1599         case READ_12:
1600         case WRITE_12:
1601         case READ_6:
1602         case WRITE_6:
1603         case READ_16:
1604         case WRITE_16:
1605                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1606                         READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1607                 break;
1608         default:
1609                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1610                         NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1611         }
1612         return ret;
1613 }
1614
1615  /**
1616  * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1617  *                                      in FW
1618  * @instance:                           Adapter soft state
1619  */
1620 static inline void
1621 megasas_dump_pending_frames(struct megasas_instance *instance)
1622 {
1623         struct megasas_cmd *cmd;
1624         int i,n;
1625         union megasas_sgl *mfi_sgl;
1626         struct megasas_io_frame *ldio;
1627         struct megasas_pthru_frame *pthru;
1628         u32 sgcount;
1629         u16 max_cmd = instance->max_fw_cmds;
1630
1631         dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1632         dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1633         if (IS_DMA64)
1634                 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1635         else
1636                 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1637
1638         dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1639         for (i = 0; i < max_cmd; i++) {
1640                 cmd = instance->cmd_list[i];
1641                 if (!cmd->scmd)
1642                         continue;
1643                 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1644                 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1645                         ldio = (struct megasas_io_frame *)cmd->frame;
1646                         mfi_sgl = &ldio->sgl;
1647                         sgcount = ldio->sge_count;
1648                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1649                         " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1650                         instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1651                         le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1652                         le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1653                 } else {
1654                         pthru = (struct megasas_pthru_frame *) cmd->frame;
1655                         mfi_sgl = &pthru->sgl;
1656                         sgcount = pthru->sge_count;
1657                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1658                         "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1659                         instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1660                         pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1661                         le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1662                 }
1663                 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1664                         for (n = 0; n < sgcount; n++) {
1665                                 if (IS_DMA64)
1666                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1667                                                 le32_to_cpu(mfi_sgl->sge64[n].length),
1668                                                 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1669                                 else
1670                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1671                                                 le32_to_cpu(mfi_sgl->sge32[n].length),
1672                                                 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1673                         }
1674                 }
1675         } /*for max_cmd*/
1676         dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1677         for (i = 0; i < max_cmd; i++) {
1678
1679                 cmd = instance->cmd_list[i];
1680
1681                 if (cmd->sync_cmd == 1)
1682                         dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1683         }
1684         dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1685 }
1686
1687 u32
1688 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1689                             struct scsi_cmnd *scmd)
1690 {
1691         struct megasas_cmd *cmd;
1692         u32 frame_count;
1693
1694         cmd = megasas_get_cmd(instance);
1695         if (!cmd)
1696                 return SCSI_MLQUEUE_HOST_BUSY;
1697
1698         /*
1699          * Logical drive command
1700          */
1701         if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1702                 frame_count = megasas_build_ldio(instance, scmd, cmd);
1703         else
1704                 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1705
1706         if (!frame_count)
1707                 goto out_return_cmd;
1708
1709         cmd->scmd = scmd;
1710         scmd->SCp.ptr = (char *)cmd;
1711
1712         /*
1713          * Issue the command to the FW
1714          */
1715         atomic_inc(&instance->fw_outstanding);
1716
1717         instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1718                                 cmd->frame_count-1, instance->reg_set);
1719
1720         return 0;
1721 out_return_cmd:
1722         megasas_return_cmd(instance, cmd);
1723         return SCSI_MLQUEUE_HOST_BUSY;
1724 }
1725
1726
1727 /**
1728  * megasas_queue_command -      Queue entry point
1729  * @scmd:                       SCSI command to be queued
1730  * @done:                       Callback entry point
1731  */
1732 static int
1733 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1734 {
1735         struct megasas_instance *instance;
1736         struct MR_PRIV_DEVICE *mr_device_priv_data;
1737
1738         instance = (struct megasas_instance *)
1739             scmd->device->host->hostdata;
1740
1741         if (instance->unload == 1) {
1742                 scmd->result = DID_NO_CONNECT << 16;
1743                 scmd->scsi_done(scmd);
1744                 return 0;
1745         }
1746
1747         if (instance->issuepend_done == 0)
1748                 return SCSI_MLQUEUE_HOST_BUSY;
1749
1750
1751         /* Check for an mpio path and adjust behavior */
1752         if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1753                 if (megasas_check_mpio_paths(instance, scmd) ==
1754                     (DID_REQUEUE << 16)) {
1755                         return SCSI_MLQUEUE_HOST_BUSY;
1756                 } else {
1757                         scmd->result = DID_NO_CONNECT << 16;
1758                         scmd->scsi_done(scmd);
1759                         return 0;
1760                 }
1761         }
1762
1763         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1764                 scmd->result = DID_NO_CONNECT << 16;
1765                 scmd->scsi_done(scmd);
1766                 return 0;
1767         }
1768
1769         mr_device_priv_data = scmd->device->hostdata;
1770         if (!mr_device_priv_data) {
1771                 scmd->result = DID_NO_CONNECT << 16;
1772                 scmd->scsi_done(scmd);
1773                 return 0;
1774         }
1775
1776         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1777                 return SCSI_MLQUEUE_HOST_BUSY;
1778
1779         if (mr_device_priv_data->tm_busy)
1780                 return SCSI_MLQUEUE_DEVICE_BUSY;
1781
1782
1783         scmd->result = 0;
1784
1785         if (MEGASAS_IS_LOGICAL(scmd->device) &&
1786             (scmd->device->id >= instance->fw_supported_vd_count ||
1787                 scmd->device->lun)) {
1788                 scmd->result = DID_BAD_TARGET << 16;
1789                 goto out_done;
1790         }
1791
1792         if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1793             MEGASAS_IS_LOGICAL(scmd->device) &&
1794             (!instance->fw_sync_cache_support)) {
1795                 scmd->result = DID_OK << 16;
1796                 goto out_done;
1797         }
1798
1799         return instance->instancet->build_and_issue_cmd(instance, scmd);
1800
1801  out_done:
1802         scmd->scsi_done(scmd);
1803         return 0;
1804 }
1805
1806 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1807 {
1808         int i;
1809
1810         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1811
1812                 if ((megasas_mgmt_info.instance[i]) &&
1813                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1814                         return megasas_mgmt_info.instance[i];
1815         }
1816
1817         return NULL;
1818 }
1819
1820 /*
1821 * megasas_set_dynamic_target_properties -
1822 * Device property set by driver may not be static and it is required to be
1823 * updated after OCR
1824 *
1825 * set tm_capable.
1826 * set dma alignment (only for eedp protection enable vd).
1827 *
1828 * @sdev: OS provided scsi device
1829 *
1830 * Returns void
1831 */
1832 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1833                                            bool is_target_prop)
1834 {
1835         u16 pd_index = 0, ld;
1836         u32 device_id;
1837         struct megasas_instance *instance;
1838         struct fusion_context *fusion;
1839         struct MR_PRIV_DEVICE *mr_device_priv_data;
1840         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1841         struct MR_LD_RAID *raid;
1842         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1843
1844         instance = megasas_lookup_instance(sdev->host->host_no);
1845         fusion = instance->ctrl_context;
1846         mr_device_priv_data = sdev->hostdata;
1847
1848         if (!fusion || !mr_device_priv_data)
1849                 return;
1850
1851         if (MEGASAS_IS_LOGICAL(sdev)) {
1852                 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1853                                         + sdev->id;
1854                 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1855                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1856                 if (ld >= instance->fw_supported_vd_count)
1857                         return;
1858                 raid = MR_LdRaidGet(ld, local_map_ptr);
1859
1860                 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1861                 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1862
1863                 mr_device_priv_data->is_tm_capable =
1864                         raid->capability.tmCapable;
1865         } else if (instance->use_seqnum_jbod_fp) {
1866                 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1867                         sdev->id;
1868                 pd_sync = (void *)fusion->pd_seq_sync
1869                                 [(instance->pd_seq_map_id - 1) & 1];
1870                 mr_device_priv_data->is_tm_capable =
1871                         pd_sync->seq[pd_index].capability.tmCapable;
1872         }
1873
1874         if (is_target_prop && instance->tgt_prop->reset_tmo) {
1875                 /*
1876                  * If FW provides a target reset timeout value, driver will use
1877                  * it. If not set, fallback to default values.
1878                  */
1879                 mr_device_priv_data->target_reset_tmo =
1880                         min_t(u8, instance->max_reset_tmo,
1881                               instance->tgt_prop->reset_tmo);
1882                 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1883         } else {
1884                 mr_device_priv_data->target_reset_tmo =
1885                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1886                 mr_device_priv_data->task_abort_tmo =
1887                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1888         }
1889 }
1890
1891 /*
1892  * megasas_set_nvme_device_properties -
1893  * set nomerges=2
1894  * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1895  * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1896  *
1897  * MR firmware provides value in KB. Caller of this function converts
1898  * kb into bytes.
1899  *
1900  * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1901  * MR firmware provides value 128 as (32 * 4K) = 128K.
1902  *
1903  * @sdev:                               scsi device
1904  * @max_io_size:                                maximum io transfer size
1905  *
1906  */
1907 static inline void
1908 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1909 {
1910         struct megasas_instance *instance;
1911         u32 mr_nvme_pg_size;
1912
1913         instance = (struct megasas_instance *)sdev->host->hostdata;
1914         mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1915                                 MR_DEFAULT_NVME_PAGE_SIZE);
1916
1917         blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1918
1919         blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1920         blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1921 }
1922
1923
1924 /*
1925  * megasas_set_static_target_properties -
1926  * Device property set by driver are static and it is not required to be
1927  * updated after OCR.
1928  *
1929  * set io timeout
1930  * set device queue depth
1931  * set nvme device properties. see - megasas_set_nvme_device_properties
1932  *
1933  * @sdev:                               scsi device
1934  * @is_target_prop                      true, if fw provided target properties.
1935  */
1936 static void megasas_set_static_target_properties(struct scsi_device *sdev,
1937                                                  bool is_target_prop)
1938 {
1939         u8 interface_type;
1940         u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1941         u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
1942         u32 tgt_device_qd;
1943         struct megasas_instance *instance;
1944         struct MR_PRIV_DEVICE *mr_device_priv_data;
1945
1946         instance = megasas_lookup_instance(sdev->host->host_no);
1947         mr_device_priv_data = sdev->hostdata;
1948         interface_type  = mr_device_priv_data->interface_type;
1949
1950         /*
1951          * The RAID firmware may require extended timeouts.
1952          */
1953         blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
1954
1955         switch (interface_type) {
1956         case SAS_PD:
1957                 device_qd = MEGASAS_SAS_QD;
1958                 break;
1959         case SATA_PD:
1960                 device_qd = MEGASAS_SATA_QD;
1961                 break;
1962         case NVME_PD:
1963                 device_qd = MEGASAS_NVME_QD;
1964                 break;
1965         }
1966
1967         if (is_target_prop) {
1968                 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1969                 if (tgt_device_qd &&
1970                     (tgt_device_qd <= instance->host->can_queue))
1971                         device_qd = tgt_device_qd;
1972
1973                 /* max_io_size_kb will be set to non zero for
1974                  * nvme based vd and syspd.
1975                  */
1976                 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1977         }
1978
1979         if (instance->nvme_page_size && max_io_size_kb)
1980                 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1981
1982         scsi_change_queue_depth(sdev, device_qd);
1983
1984 }
1985
1986
1987 static int megasas_slave_configure(struct scsi_device *sdev)
1988 {
1989         u16 pd_index = 0;
1990         struct megasas_instance *instance;
1991         int ret_target_prop = DCMD_FAILED;
1992         bool is_target_prop = false;
1993
1994         instance = megasas_lookup_instance(sdev->host->host_no);
1995         if (instance->pd_list_not_supported) {
1996                 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
1997                         pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1998                                 sdev->id;
1999                         if (instance->pd_list[pd_index].driveState !=
2000                                 MR_PD_STATE_SYSTEM)
2001                                 return -ENXIO;
2002                 }
2003         }
2004
2005         mutex_lock(&instance->reset_mutex);
2006         /* Send DCMD to Firmware and cache the information */
2007         if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2008                 megasas_get_pd_info(instance, sdev);
2009
2010         /* Some ventura firmware may not have instance->nvme_page_size set.
2011          * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2012          */
2013         if ((instance->tgt_prop) && (instance->nvme_page_size))
2014                 ret_target_prop = megasas_get_target_prop(instance, sdev);
2015
2016         is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2017         megasas_set_static_target_properties(sdev, is_target_prop);
2018
2019         /* This sdev property may change post OCR */
2020         megasas_set_dynamic_target_properties(sdev, is_target_prop);
2021
2022         mutex_unlock(&instance->reset_mutex);
2023
2024         return 0;
2025 }
2026
2027 static int megasas_slave_alloc(struct scsi_device *sdev)
2028 {
2029         u16 pd_index = 0;
2030         struct megasas_instance *instance ;
2031         struct MR_PRIV_DEVICE *mr_device_priv_data;
2032
2033         instance = megasas_lookup_instance(sdev->host->host_no);
2034         if (!MEGASAS_IS_LOGICAL(sdev)) {
2035                 /*
2036                  * Open the OS scan to the SYSTEM PD
2037                  */
2038                 pd_index =
2039                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2040                         sdev->id;
2041                 if ((instance->pd_list_not_supported ||
2042                         instance->pd_list[pd_index].driveState ==
2043                         MR_PD_STATE_SYSTEM)) {
2044                         goto scan_target;
2045                 }
2046                 return -ENXIO;
2047         }
2048
2049 scan_target:
2050         mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2051                                         GFP_KERNEL);
2052         if (!mr_device_priv_data)
2053                 return -ENOMEM;
2054         sdev->hostdata = mr_device_priv_data;
2055
2056         atomic_set(&mr_device_priv_data->r1_ldio_hint,
2057                    instance->r1_ldio_hint_default);
2058         return 0;
2059 }
2060
2061 static void megasas_slave_destroy(struct scsi_device *sdev)
2062 {
2063         kfree(sdev->hostdata);
2064         sdev->hostdata = NULL;
2065 }
2066
2067 /*
2068 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2069 *                                       kill adapter
2070 * @instance:                            Adapter soft state
2071 *
2072 */
2073 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2074 {
2075         int i;
2076         struct megasas_cmd *cmd_mfi;
2077         struct megasas_cmd_fusion *cmd_fusion;
2078         struct fusion_context *fusion = instance->ctrl_context;
2079
2080         /* Find all outstanding ioctls */
2081         if (fusion) {
2082                 for (i = 0; i < instance->max_fw_cmds; i++) {
2083                         cmd_fusion = fusion->cmd_list[i];
2084                         if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2085                                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2086                                 if (cmd_mfi->sync_cmd &&
2087                                     (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2088                                         cmd_mfi->frame->hdr.cmd_status =
2089                                                         MFI_STAT_WRONG_STATE;
2090                                         megasas_complete_cmd(instance,
2091                                                              cmd_mfi, DID_OK);
2092                                 }
2093                         }
2094                 }
2095         } else {
2096                 for (i = 0; i < instance->max_fw_cmds; i++) {
2097                         cmd_mfi = instance->cmd_list[i];
2098                         if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2099                                 MFI_CMD_ABORT)
2100                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2101                 }
2102         }
2103 }
2104
2105
2106 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2107 {
2108         /* Set critical error to block I/O & ioctls in case caller didn't */
2109         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2110         /* Wait 1 second to ensure IO or ioctls in build have posted */
2111         msleep(1000);
2112         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2113                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2114                 (instance->adapter_type != MFI_SERIES)) {
2115                 if (!instance->requestorId) {
2116                         writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2117                         /* Flush */
2118                         readl(&instance->reg_set->doorbell);
2119                 }
2120                 if (instance->requestorId && instance->peerIsPresent)
2121                         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2122         } else {
2123                 writel(MFI_STOP_ADP,
2124                         &instance->reg_set->inbound_doorbell);
2125         }
2126         /* Complete outstanding ioctls when adapter is killed */
2127         megasas_complete_outstanding_ioctls(instance);
2128 }
2129
2130  /**
2131   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2132   *                                     restored to max value
2133   * @instance:                  Adapter soft state
2134   *
2135   */
2136 void
2137 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2138 {
2139         unsigned long flags;
2140
2141         if (instance->flag & MEGASAS_FW_BUSY
2142             && time_after(jiffies, instance->last_time + 5 * HZ)
2143             && atomic_read(&instance->fw_outstanding) <
2144             instance->throttlequeuedepth + 1) {
2145
2146                 spin_lock_irqsave(instance->host->host_lock, flags);
2147                 instance->flag &= ~MEGASAS_FW_BUSY;
2148
2149                 instance->host->can_queue = instance->cur_can_queue;
2150                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2151         }
2152 }
2153
2154 /**
2155  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
2156  * @instance_addr:                      Address of adapter soft state
2157  *
2158  * Tasklet to complete cmds
2159  */
2160 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2161 {
2162         u32 producer;
2163         u32 consumer;
2164         u32 context;
2165         struct megasas_cmd *cmd;
2166         struct megasas_instance *instance =
2167                                 (struct megasas_instance *)instance_addr;
2168         unsigned long flags;
2169
2170         /* If we have already declared adapter dead, donot complete cmds */
2171         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2172                 return;
2173
2174         spin_lock_irqsave(&instance->completion_lock, flags);
2175
2176         producer = le32_to_cpu(*instance->producer);
2177         consumer = le32_to_cpu(*instance->consumer);
2178
2179         while (consumer != producer) {
2180                 context = le32_to_cpu(instance->reply_queue[consumer]);
2181                 if (context >= instance->max_fw_cmds) {
2182                         dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2183                                 context);
2184                         BUG();
2185                 }
2186
2187                 cmd = instance->cmd_list[context];
2188
2189                 megasas_complete_cmd(instance, cmd, DID_OK);
2190
2191                 consumer++;
2192                 if (consumer == (instance->max_fw_cmds + 1)) {
2193                         consumer = 0;
2194                 }
2195         }
2196
2197         *instance->consumer = cpu_to_le32(producer);
2198
2199         spin_unlock_irqrestore(&instance->completion_lock, flags);
2200
2201         /*
2202          * Check if we can restore can_queue
2203          */
2204         megasas_check_and_restore_queue_depth(instance);
2205 }
2206
2207 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2208
2209 /**
2210  * megasas_start_timer - Initializes sriov heartbeat timer object
2211  * @instance:           Adapter soft state
2212  *
2213  */
2214 void megasas_start_timer(struct megasas_instance *instance)
2215 {
2216         struct timer_list *timer = &instance->sriov_heartbeat_timer;
2217
2218         timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2219         timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2220         add_timer(timer);
2221 }
2222
2223 static void
2224 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2225
2226 static void
2227 process_fw_state_change_wq(struct work_struct *work);
2228
2229 void megasas_do_ocr(struct megasas_instance *instance)
2230 {
2231         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2232         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2233         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2234                 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2235         }
2236         instance->instancet->disable_intr(instance);
2237         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2238         instance->issuepend_done = 0;
2239
2240         atomic_set(&instance->fw_outstanding, 0);
2241         megasas_internal_reset_defer_cmds(instance);
2242         process_fw_state_change_wq(&instance->work_init);
2243 }
2244
2245 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2246                                             int initial)
2247 {
2248         struct megasas_cmd *cmd;
2249         struct megasas_dcmd_frame *dcmd;
2250         struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2251         dma_addr_t new_affiliation_111_h;
2252         int ld, retval = 0;
2253         u8 thisVf;
2254
2255         cmd = megasas_get_cmd(instance);
2256
2257         if (!cmd) {
2258                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2259                        "Failed to get cmd for scsi%d\n",
2260                         instance->host->host_no);
2261                 return -ENOMEM;
2262         }
2263
2264         dcmd = &cmd->frame->dcmd;
2265
2266         if (!instance->vf_affiliation_111) {
2267                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2268                        "affiliation for scsi%d\n", instance->host->host_no);
2269                 megasas_return_cmd(instance, cmd);
2270                 return -ENOMEM;
2271         }
2272
2273         if (initial)
2274                         memset(instance->vf_affiliation_111, 0,
2275                                sizeof(struct MR_LD_VF_AFFILIATION_111));
2276         else {
2277                 new_affiliation_111 =
2278                         dma_alloc_coherent(&instance->pdev->dev,
2279                                            sizeof(struct MR_LD_VF_AFFILIATION_111),
2280                                            &new_affiliation_111_h, GFP_KERNEL);
2281                 if (!new_affiliation_111) {
2282                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2283                                "memory for new affiliation for scsi%d\n",
2284                                instance->host->host_no);
2285                         megasas_return_cmd(instance, cmd);
2286                         return -ENOMEM;
2287                 }
2288         }
2289
2290         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2291
2292         dcmd->cmd = MFI_CMD_DCMD;
2293         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2294         dcmd->sge_count = 1;
2295         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2296         dcmd->timeout = 0;
2297         dcmd->pad_0 = 0;
2298         dcmd->data_xfer_len =
2299                 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2300         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2301
2302         if (initial)
2303                 dcmd->sgl.sge32[0].phys_addr =
2304                         cpu_to_le32(instance->vf_affiliation_111_h);
2305         else
2306                 dcmd->sgl.sge32[0].phys_addr =
2307                         cpu_to_le32(new_affiliation_111_h);
2308
2309         dcmd->sgl.sge32[0].length = cpu_to_le32(
2310                 sizeof(struct MR_LD_VF_AFFILIATION_111));
2311
2312         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2313                "scsi%d\n", instance->host->host_no);
2314
2315         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2316                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2317                        " failed with status 0x%x for scsi%d\n",
2318                        dcmd->cmd_status, instance->host->host_no);
2319                 retval = 1; /* Do a scan if we couldn't get affiliation */
2320                 goto out;
2321         }
2322
2323         if (!initial) {
2324                 thisVf = new_affiliation_111->thisVf;
2325                 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2326                         if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2327                             new_affiliation_111->map[ld].policy[thisVf]) {
2328                                 dev_warn(&instance->pdev->dev, "SR-IOV: "
2329                                        "Got new LD/VF affiliation for scsi%d\n",
2330                                        instance->host->host_no);
2331                                 memcpy(instance->vf_affiliation_111,
2332                                        new_affiliation_111,
2333                                        sizeof(struct MR_LD_VF_AFFILIATION_111));
2334                                 retval = 1;
2335                                 goto out;
2336                         }
2337         }
2338 out:
2339         if (new_affiliation_111) {
2340                 dma_free_coherent(&instance->pdev->dev,
2341                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
2342                                     new_affiliation_111,
2343                                     new_affiliation_111_h);
2344         }
2345
2346         megasas_return_cmd(instance, cmd);
2347
2348         return retval;
2349 }
2350
2351 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2352                                             int initial)
2353 {
2354         struct megasas_cmd *cmd;
2355         struct megasas_dcmd_frame *dcmd;
2356         struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2357         struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2358         dma_addr_t new_affiliation_h;
2359         int i, j, retval = 0, found = 0, doscan = 0;
2360         u8 thisVf;
2361
2362         cmd = megasas_get_cmd(instance);
2363
2364         if (!cmd) {
2365                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2366                        "Failed to get cmd for scsi%d\n",
2367                        instance->host->host_no);
2368                 return -ENOMEM;
2369         }
2370
2371         dcmd = &cmd->frame->dcmd;
2372
2373         if (!instance->vf_affiliation) {
2374                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2375                        "affiliation for scsi%d\n", instance->host->host_no);
2376                 megasas_return_cmd(instance, cmd);
2377                 return -ENOMEM;
2378         }
2379
2380         if (initial)
2381                 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2382                        sizeof(struct MR_LD_VF_AFFILIATION));
2383         else {
2384                 new_affiliation =
2385                         dma_alloc_coherent(&instance->pdev->dev,
2386                                            (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2387                                            &new_affiliation_h, GFP_KERNEL);
2388                 if (!new_affiliation) {
2389                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2390                                "memory for new affiliation for scsi%d\n",
2391                                instance->host->host_no);
2392                         megasas_return_cmd(instance, cmd);
2393                         return -ENOMEM;
2394                 }
2395         }
2396
2397         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2398
2399         dcmd->cmd = MFI_CMD_DCMD;
2400         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2401         dcmd->sge_count = 1;
2402         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2403         dcmd->timeout = 0;
2404         dcmd->pad_0 = 0;
2405         dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2406                 sizeof(struct MR_LD_VF_AFFILIATION));
2407         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2408
2409         if (initial)
2410                 dcmd->sgl.sge32[0].phys_addr =
2411                         cpu_to_le32(instance->vf_affiliation_h);
2412         else
2413                 dcmd->sgl.sge32[0].phys_addr =
2414                         cpu_to_le32(new_affiliation_h);
2415
2416         dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2417                 sizeof(struct MR_LD_VF_AFFILIATION));
2418
2419         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2420                "scsi%d\n", instance->host->host_no);
2421
2422
2423         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2424                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2425                        " failed with status 0x%x for scsi%d\n",
2426                        dcmd->cmd_status, instance->host->host_no);
2427                 retval = 1; /* Do a scan if we couldn't get affiliation */
2428                 goto out;
2429         }
2430
2431         if (!initial) {
2432                 if (!new_affiliation->ldCount) {
2433                         dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2434                                "affiliation for passive path for scsi%d\n",
2435                                instance->host->host_no);
2436                         retval = 1;
2437                         goto out;
2438                 }
2439                 newmap = new_affiliation->map;
2440                 savedmap = instance->vf_affiliation->map;
2441                 thisVf = new_affiliation->thisVf;
2442                 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2443                         found = 0;
2444                         for (j = 0; j < instance->vf_affiliation->ldCount;
2445                              j++) {
2446                                 if (newmap->ref.targetId ==
2447                                     savedmap->ref.targetId) {
2448                                         found = 1;
2449                                         if (newmap->policy[thisVf] !=
2450                                             savedmap->policy[thisVf]) {
2451                                                 doscan = 1;
2452                                                 goto out;
2453                                         }
2454                                 }
2455                                 savedmap = (struct MR_LD_VF_MAP *)
2456                                         ((unsigned char *)savedmap +
2457                                          savedmap->size);
2458                         }
2459                         if (!found && newmap->policy[thisVf] !=
2460                             MR_LD_ACCESS_HIDDEN) {
2461                                 doscan = 1;
2462                                 goto out;
2463                         }
2464                         newmap = (struct MR_LD_VF_MAP *)
2465                                 ((unsigned char *)newmap + newmap->size);
2466                 }
2467
2468                 newmap = new_affiliation->map;
2469                 savedmap = instance->vf_affiliation->map;
2470
2471                 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2472                         found = 0;
2473                         for (j = 0 ; j < new_affiliation->ldCount; j++) {
2474                                 if (savedmap->ref.targetId ==
2475                                     newmap->ref.targetId) {
2476                                         found = 1;
2477                                         if (savedmap->policy[thisVf] !=
2478                                             newmap->policy[thisVf]) {
2479                                                 doscan = 1;
2480                                                 goto out;
2481                                         }
2482                                 }
2483                                 newmap = (struct MR_LD_VF_MAP *)
2484                                         ((unsigned char *)newmap +
2485                                          newmap->size);
2486                         }
2487                         if (!found && savedmap->policy[thisVf] !=
2488                             MR_LD_ACCESS_HIDDEN) {
2489                                 doscan = 1;
2490                                 goto out;
2491                         }
2492                         savedmap = (struct MR_LD_VF_MAP *)
2493                                 ((unsigned char *)savedmap +
2494                                  savedmap->size);
2495                 }
2496         }
2497 out:
2498         if (doscan) {
2499                 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2500                        "affiliation for scsi%d\n", instance->host->host_no);
2501                 memcpy(instance->vf_affiliation, new_affiliation,
2502                        new_affiliation->size);
2503                 retval = 1;
2504         }
2505
2506         if (new_affiliation)
2507                 dma_free_coherent(&instance->pdev->dev,
2508                                     (MAX_LOGICAL_DRIVES + 1) *
2509                                     sizeof(struct MR_LD_VF_AFFILIATION),
2510                                     new_affiliation, new_affiliation_h);
2511         megasas_return_cmd(instance, cmd);
2512
2513         return retval;
2514 }
2515
2516 /* This function will get the current SR-IOV LD/VF affiliation */
2517 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2518         int initial)
2519 {
2520         int retval;
2521
2522         if (instance->PlasmaFW111)
2523                 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2524         else
2525                 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2526         return retval;
2527 }
2528
2529 /* This function will tell FW to start the SR-IOV heartbeat */
2530 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2531                                          int initial)
2532 {
2533         struct megasas_cmd *cmd;
2534         struct megasas_dcmd_frame *dcmd;
2535         int retval = 0;
2536
2537         cmd = megasas_get_cmd(instance);
2538
2539         if (!cmd) {
2540                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2541                        "Failed to get cmd for scsi%d\n",
2542                        instance->host->host_no);
2543                 return -ENOMEM;
2544         }
2545
2546         dcmd = &cmd->frame->dcmd;
2547
2548         if (initial) {
2549                 instance->hb_host_mem =
2550                         dma_alloc_coherent(&instance->pdev->dev,
2551                                            sizeof(struct MR_CTRL_HB_HOST_MEM),
2552                                            &instance->hb_host_mem_h,
2553                                            GFP_KERNEL);
2554                 if (!instance->hb_host_mem) {
2555                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2556                                " memory for heartbeat host memory for scsi%d\n",
2557                                instance->host->host_no);
2558                         retval = -ENOMEM;
2559                         goto out;
2560                 }
2561         }
2562
2563         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2564
2565         dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2566         dcmd->cmd = MFI_CMD_DCMD;
2567         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2568         dcmd->sge_count = 1;
2569         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2570         dcmd->timeout = 0;
2571         dcmd->pad_0 = 0;
2572         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2573         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2574
2575         megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2576                                  sizeof(struct MR_CTRL_HB_HOST_MEM));
2577
2578         dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2579                instance->host->host_no);
2580
2581         if ((instance->adapter_type != MFI_SERIES) &&
2582             !instance->mask_interrupts)
2583                 retval = megasas_issue_blocked_cmd(instance, cmd,
2584                         MEGASAS_ROUTINE_WAIT_TIME_VF);
2585         else
2586                 retval = megasas_issue_polled(instance, cmd);
2587
2588         if (retval) {
2589                 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2590                         "_MEM_ALLOC DCMD %s for scsi%d\n",
2591                         (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2592                         "timed out" : "failed", instance->host->host_no);
2593                 retval = 1;
2594         }
2595
2596 out:
2597         megasas_return_cmd(instance, cmd);
2598
2599         return retval;
2600 }
2601
2602 /* Handler for SR-IOV heartbeat */
2603 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2604 {
2605         struct megasas_instance *instance =
2606                 from_timer(instance, t, sriov_heartbeat_timer);
2607
2608         if (instance->hb_host_mem->HB.fwCounter !=
2609             instance->hb_host_mem->HB.driverCounter) {
2610                 instance->hb_host_mem->HB.driverCounter =
2611                         instance->hb_host_mem->HB.fwCounter;
2612                 mod_timer(&instance->sriov_heartbeat_timer,
2613                           jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2614         } else {
2615                 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2616                        "completed for scsi%d\n", instance->host->host_no);
2617                 schedule_work(&instance->work_init);
2618         }
2619 }
2620
2621 /**
2622  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2623  * @instance:                           Adapter soft state
2624  *
2625  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2626  * complete all its outstanding commands. Returns error if one or more IOs
2627  * are pending after this time period. It also marks the controller dead.
2628  */
2629 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2630 {
2631         int i, sl, outstanding;
2632         u32 reset_index;
2633         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2634         unsigned long flags;
2635         struct list_head clist_local;
2636         struct megasas_cmd *reset_cmd;
2637         u32 fw_state;
2638
2639         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2640                 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2641                 __func__, __LINE__);
2642                 return FAILED;
2643         }
2644
2645         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2646
2647                 INIT_LIST_HEAD(&clist_local);
2648                 spin_lock_irqsave(&instance->hba_lock, flags);
2649                 list_splice_init(&instance->internal_reset_pending_q,
2650                                 &clist_local);
2651                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2652
2653                 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2654                 for (i = 0; i < wait_time; i++) {
2655                         msleep(1000);
2656                         if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2657                                 break;
2658                 }
2659
2660                 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2661                         dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2662                         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2663                         return FAILED;
2664                 }
2665
2666                 reset_index = 0;
2667                 while (!list_empty(&clist_local)) {
2668                         reset_cmd = list_entry((&clist_local)->next,
2669                                                 struct megasas_cmd, list);
2670                         list_del_init(&reset_cmd->list);
2671                         if (reset_cmd->scmd) {
2672                                 reset_cmd->scmd->result = DID_REQUEUE << 16;
2673                                 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2674                                         reset_index, reset_cmd,
2675                                         reset_cmd->scmd->cmnd[0]);
2676
2677                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2678                                 megasas_return_cmd(instance, reset_cmd);
2679                         } else if (reset_cmd->sync_cmd) {
2680                                 dev_notice(&instance->pdev->dev, "%p synch cmds"
2681                                                 "reset queue\n",
2682                                                 reset_cmd);
2683
2684                                 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2685                                 instance->instancet->fire_cmd(instance,
2686                                                 reset_cmd->frame_phys_addr,
2687                                                 0, instance->reg_set);
2688                         } else {
2689                                 dev_notice(&instance->pdev->dev, "%p unexpected"
2690                                         "cmds lst\n",
2691                                         reset_cmd);
2692                         }
2693                         reset_index++;
2694                 }
2695
2696                 return SUCCESS;
2697         }
2698
2699         for (i = 0; i < resetwaittime; i++) {
2700                 outstanding = atomic_read(&instance->fw_outstanding);
2701
2702                 if (!outstanding)
2703                         break;
2704
2705                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2706                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2707                                "commands to complete\n",i,outstanding);
2708                         /*
2709                          * Call cmd completion routine. Cmd to be
2710                          * be completed directly without depending on isr.
2711                          */
2712                         megasas_complete_cmd_dpc((unsigned long)instance);
2713                 }
2714
2715                 msleep(1000);
2716         }
2717
2718         i = 0;
2719         outstanding = atomic_read(&instance->fw_outstanding);
2720         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2721
2722         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2723                 goto no_outstanding;
2724
2725         if (instance->disableOnlineCtrlReset)
2726                 goto kill_hba_and_failed;
2727         do {
2728                 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2729                         dev_info(&instance->pdev->dev,
2730                                 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2731                                 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2732                         if (i == 3)
2733                                 goto kill_hba_and_failed;
2734                         megasas_do_ocr(instance);
2735
2736                         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2737                                 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2738                                 __func__, __LINE__);
2739                                 return FAILED;
2740                         }
2741                         dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2742                                 __func__, __LINE__);
2743
2744                         for (sl = 0; sl < 10; sl++)
2745                                 msleep(500);
2746
2747                         outstanding = atomic_read(&instance->fw_outstanding);
2748
2749                         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2750                         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2751                                 goto no_outstanding;
2752                 }
2753                 i++;
2754         } while (i <= 3);
2755
2756 no_outstanding:
2757
2758         dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2759                 __func__, __LINE__);
2760         return SUCCESS;
2761
2762 kill_hba_and_failed:
2763
2764         /* Reset not supported, kill adapter */
2765         dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2766                 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2767                 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2768                 atomic_read(&instance->fw_outstanding));
2769         megasas_dump_pending_frames(instance);
2770         megaraid_sas_kill_hba(instance);
2771
2772         return FAILED;
2773 }
2774
2775 /**
2776  * megasas_generic_reset -      Generic reset routine
2777  * @scmd:                       Mid-layer SCSI command
2778  *
2779  * This routine implements a generic reset handler for device, bus and host
2780  * reset requests. Device, bus and host specific reset handlers can use this
2781  * function after they do their specific tasks.
2782  */
2783 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2784 {
2785         int ret_val;
2786         struct megasas_instance *instance;
2787
2788         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2789
2790         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2791                  scmd->cmnd[0], scmd->retries);
2792
2793         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2794                 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2795                 return FAILED;
2796         }
2797
2798         ret_val = megasas_wait_for_outstanding(instance);
2799         if (ret_val == SUCCESS)
2800                 dev_notice(&instance->pdev->dev, "reset successful\n");
2801         else
2802                 dev_err(&instance->pdev->dev, "failed to do reset\n");
2803
2804         return ret_val;
2805 }
2806
2807 /**
2808  * megasas_reset_timer - quiesce the adapter if required
2809  * @scmd:               scsi cmnd
2810  *
2811  * Sets the FW busy flag and reduces the host->can_queue if the
2812  * cmd has not been completed within the timeout period.
2813  */
2814 static enum
2815 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2816 {
2817         struct megasas_instance *instance;
2818         unsigned long flags;
2819
2820         if (time_after(jiffies, scmd->jiffies_at_alloc +
2821                                 (scmd_timeout * 2) * HZ)) {
2822                 return BLK_EH_DONE;
2823         }
2824
2825         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2826         if (!(instance->flag & MEGASAS_FW_BUSY)) {
2827                 /* FW is busy, throttle IO */
2828                 spin_lock_irqsave(instance->host->host_lock, flags);
2829
2830                 instance->host->can_queue = instance->throttlequeuedepth;
2831                 instance->last_time = jiffies;
2832                 instance->flag |= MEGASAS_FW_BUSY;
2833
2834                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2835         }
2836         return BLK_EH_RESET_TIMER;
2837 }
2838
2839 /**
2840  * megasas_dump -       This function will provide hexdump
2841  * @ptr:                Pointer starting which memory should be dumped
2842  * @size:               Size of memory to be dumped
2843  */
2844 inline void
2845 megasas_dump(void *ptr, int sz)
2846 {
2847         int i;
2848         __le32 *loc = (__le32 *)ptr;
2849
2850         for (i = 0; i < sz / sizeof(__le32); i++) {
2851                 if (i && ((i % 8) == 0))
2852                         printk("\n\t");
2853                 printk("%08x ", le32_to_cpu(loc[i]));
2854         }
2855         printk("\n");
2856 }
2857
2858 /**
2859  * megasas_dump_fusion_io -     This function will print key details
2860  *                              of SCSI IO
2861  * @scmd:                       SCSI command pointer of SCSI IO
2862  */
2863 void
2864 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2865 {
2866         struct megasas_cmd_fusion *cmd;
2867         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2868         struct megasas_instance *instance;
2869
2870         cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2871         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2872
2873         scmd_printk(KERN_INFO, scmd,
2874                     "scmd: (0x%p)  retries: 0x%x  allowed: 0x%x\n",
2875                     scmd, scmd->retries, scmd->allowed);
2876         scsi_print_command(scmd);
2877
2878         if (cmd) {
2879                 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2880                 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2881                 scmd_printk(KERN_INFO, scmd,
2882                             "RequestFlags:0x%x  MSIxIndex:0x%x  SMID:0x%x  LMID:0x%x  DevHandle:0x%x\n",
2883                             req_desc->SCSIIO.RequestFlags,
2884                             req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2885                             req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2886
2887                 printk(KERN_INFO "IO request frame:\n");
2888                 megasas_dump(cmd->io_request,
2889                              MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
2890                 printk(KERN_INFO "Chain frame:\n");
2891                 megasas_dump(cmd->sg_frame,
2892                              instance->max_chain_frame_sz);
2893         }
2894
2895 }
2896
2897 /**
2898  * megasas_reset_bus_host -     Bus & host reset handler entry point
2899  */
2900 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2901 {
2902         int ret;
2903         struct megasas_instance *instance;
2904
2905         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2906
2907         scmd_printk(KERN_INFO, scmd,
2908                 "OCR is requested due to IO timeout!!\n");
2909
2910         scmd_printk(KERN_INFO, scmd,
2911                 "SCSI host state: %d  SCSI host busy: %d  FW outstanding: %d\n",
2912                 scmd->device->host->shost_state,
2913                 scsi_host_busy(scmd->device->host),
2914                 atomic_read(&instance->fw_outstanding));
2915         /*
2916          * First wait for all commands to complete
2917          */
2918         if (instance->adapter_type == MFI_SERIES) {
2919                 ret = megasas_generic_reset(scmd);
2920         } else {
2921                 megasas_dump_fusion_io(scmd);
2922                 ret = megasas_reset_fusion(scmd->device->host,
2923                                 SCSIIO_TIMEOUT_OCR);
2924         }
2925
2926         return ret;
2927 }
2928
2929 /**
2930  * megasas_task_abort - Issues task abort request to firmware
2931  *                      (supported only for fusion adapters)
2932  * @scmd:               SCSI command pointer
2933  */
2934 static int megasas_task_abort(struct scsi_cmnd *scmd)
2935 {
2936         int ret;
2937         struct megasas_instance *instance;
2938
2939         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2940
2941         if (instance->adapter_type != MFI_SERIES)
2942                 ret = megasas_task_abort_fusion(scmd);
2943         else {
2944                 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2945                 ret = FAILED;
2946         }
2947
2948         return ret;
2949 }
2950
2951 /**
2952  * megasas_reset_target:  Issues target reset request to firmware
2953  *                        (supported only for fusion adapters)
2954  * @scmd:                 SCSI command pointer
2955  */
2956 static int megasas_reset_target(struct scsi_cmnd *scmd)
2957 {
2958         int ret;
2959         struct megasas_instance *instance;
2960
2961         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2962
2963         if (instance->adapter_type != MFI_SERIES)
2964                 ret = megasas_reset_target_fusion(scmd);
2965         else {
2966                 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2967                 ret = FAILED;
2968         }
2969
2970         return ret;
2971 }
2972
2973 /**
2974  * megasas_bios_param - Returns disk geometry for a disk
2975  * @sdev:               device handle
2976  * @bdev:               block device
2977  * @capacity:           drive capacity
2978  * @geom:               geometry parameters
2979  */
2980 static int
2981 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2982                  sector_t capacity, int geom[])
2983 {
2984         int heads;
2985         int sectors;
2986         sector_t cylinders;
2987         unsigned long tmp;
2988
2989         /* Default heads (64) & sectors (32) */
2990         heads = 64;
2991         sectors = 32;
2992
2993         tmp = heads * sectors;
2994         cylinders = capacity;
2995
2996         sector_div(cylinders, tmp);
2997
2998         /*
2999          * Handle extended translation size for logical drives > 1Gb
3000          */
3001
3002         if (capacity >= 0x200000) {
3003                 heads = 255;
3004                 sectors = 63;
3005                 tmp = heads*sectors;
3006                 cylinders = capacity;
3007                 sector_div(cylinders, tmp);
3008         }
3009
3010         geom[0] = heads;
3011         geom[1] = sectors;
3012         geom[2] = cylinders;
3013
3014         return 0;
3015 }
3016
3017 static void megasas_aen_polling(struct work_struct *work);
3018
3019 /**
3020  * megasas_service_aen -        Processes an event notification
3021  * @instance:                   Adapter soft state
3022  * @cmd:                        AEN command completed by the ISR
3023  *
3024  * For AEN, driver sends a command down to FW that is held by the FW till an
3025  * event occurs. When an event of interest occurs, FW completes the command
3026  * that it was previously holding.
3027  *
3028  * This routines sends SIGIO signal to processes that have registered with the
3029  * driver for AEN.
3030  */
3031 static void
3032 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3033 {
3034         unsigned long flags;
3035
3036         /*
3037          * Don't signal app if it is just an aborted previously registered aen
3038          */
3039         if ((!cmd->abort_aen) && (instance->unload == 0)) {
3040                 spin_lock_irqsave(&poll_aen_lock, flags);
3041                 megasas_poll_wait_aen = 1;
3042                 spin_unlock_irqrestore(&poll_aen_lock, flags);
3043                 wake_up(&megasas_poll_wait);
3044                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3045         }
3046         else
3047                 cmd->abort_aen = 0;
3048
3049         instance->aen_cmd = NULL;
3050
3051         megasas_return_cmd(instance, cmd);
3052
3053         if ((instance->unload == 0) &&
3054                 ((instance->issuepend_done == 1))) {
3055                 struct megasas_aen_event *ev;
3056
3057                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3058                 if (!ev) {
3059                         dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3060                 } else {
3061                         ev->instance = instance;
3062                         instance->ev = ev;
3063                         INIT_DELAYED_WORK(&ev->hotplug_work,
3064                                           megasas_aen_polling);
3065                         schedule_delayed_work(&ev->hotplug_work, 0);
3066                 }
3067         }
3068 }
3069
3070 static ssize_t
3071 megasas_fw_crash_buffer_store(struct device *cdev,
3072         struct device_attribute *attr, const char *buf, size_t count)
3073 {
3074         struct Scsi_Host *shost = class_to_shost(cdev);
3075         struct megasas_instance *instance =
3076                 (struct megasas_instance *) shost->hostdata;
3077         int val = 0;
3078         unsigned long flags;
3079
3080         if (kstrtoint(buf, 0, &val) != 0)
3081                 return -EINVAL;
3082
3083         spin_lock_irqsave(&instance->crashdump_lock, flags);
3084         instance->fw_crash_buffer_offset = val;
3085         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3086         return strlen(buf);
3087 }
3088
3089 static ssize_t
3090 megasas_fw_crash_buffer_show(struct device *cdev,
3091         struct device_attribute *attr, char *buf)
3092 {
3093         struct Scsi_Host *shost = class_to_shost(cdev);
3094         struct megasas_instance *instance =
3095                 (struct megasas_instance *) shost->hostdata;
3096         u32 size;
3097         unsigned long buff_addr;
3098         unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3099         unsigned long src_addr;
3100         unsigned long flags;
3101         u32 buff_offset;
3102
3103         spin_lock_irqsave(&instance->crashdump_lock, flags);
3104         buff_offset = instance->fw_crash_buffer_offset;
3105         if (!instance->crash_dump_buf &&
3106                 !((instance->fw_crash_state == AVAILABLE) ||
3107                 (instance->fw_crash_state == COPYING))) {
3108                 dev_err(&instance->pdev->dev,
3109                         "Firmware crash dump is not available\n");
3110                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3111                 return -EINVAL;
3112         }
3113
3114         buff_addr = (unsigned long) buf;
3115
3116         if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3117                 dev_err(&instance->pdev->dev,
3118                         "Firmware crash dump offset is out of range\n");
3119                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3120                 return 0;
3121         }
3122
3123         size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3124         size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3125
3126         src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3127                 (buff_offset % dmachunk);
3128         memcpy(buf, (void *)src_addr, size);
3129         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3130
3131         return size;
3132 }
3133
3134 static ssize_t
3135 megasas_fw_crash_buffer_size_show(struct device *cdev,
3136         struct device_attribute *attr, char *buf)
3137 {
3138         struct Scsi_Host *shost = class_to_shost(cdev);
3139         struct megasas_instance *instance =
3140                 (struct megasas_instance *) shost->hostdata;
3141
3142         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3143                 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3144 }
3145
3146 static ssize_t
3147 megasas_fw_crash_state_store(struct device *cdev,
3148         struct device_attribute *attr, const char *buf, size_t count)
3149 {
3150         struct Scsi_Host *shost = class_to_shost(cdev);
3151         struct megasas_instance *instance =
3152                 (struct megasas_instance *) shost->hostdata;
3153         int val = 0;
3154         unsigned long flags;
3155
3156         if (kstrtoint(buf, 0, &val) != 0)
3157                 return -EINVAL;
3158
3159         if ((val <= AVAILABLE || val > COPY_ERROR)) {
3160                 dev_err(&instance->pdev->dev, "application updates invalid "
3161                         "firmware crash state\n");
3162                 return -EINVAL;
3163         }
3164
3165         instance->fw_crash_state = val;
3166
3167         if ((val == COPIED) || (val == COPY_ERROR)) {
3168                 spin_lock_irqsave(&instance->crashdump_lock, flags);
3169                 megasas_free_host_crash_buffer(instance);
3170                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3171                 if (val == COPY_ERROR)
3172                         dev_info(&instance->pdev->dev, "application failed to "
3173                                 "copy Firmware crash dump\n");
3174                 else
3175                         dev_info(&instance->pdev->dev, "Firmware crash dump "
3176                                 "copied successfully\n");
3177         }
3178         return strlen(buf);
3179 }
3180
3181 static ssize_t
3182 megasas_fw_crash_state_show(struct device *cdev,
3183         struct device_attribute *attr, char *buf)
3184 {
3185         struct Scsi_Host *shost = class_to_shost(cdev);
3186         struct megasas_instance *instance =
3187                 (struct megasas_instance *) shost->hostdata;
3188
3189         return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3190 }
3191
3192 static ssize_t
3193 megasas_page_size_show(struct device *cdev,
3194         struct device_attribute *attr, char *buf)
3195 {
3196         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3197 }
3198
3199 static ssize_t
3200 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3201         char *buf)
3202 {
3203         struct Scsi_Host *shost = class_to_shost(cdev);
3204         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3205
3206         return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3207 }
3208
3209 static ssize_t
3210 megasas_fw_cmds_outstanding_show(struct device *cdev,
3211                                  struct device_attribute *attr, char *buf)
3212 {
3213         struct Scsi_Host *shost = class_to_shost(cdev);
3214         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3215
3216         return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3217 }
3218
3219 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3220         megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3221 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3222         megasas_fw_crash_buffer_size_show, NULL);
3223 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3224         megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3225 static DEVICE_ATTR(page_size, S_IRUGO,
3226         megasas_page_size_show, NULL);
3227 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3228         megasas_ldio_outstanding_show, NULL);
3229 static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3230         megasas_fw_cmds_outstanding_show, NULL);
3231
3232 struct device_attribute *megaraid_host_attrs[] = {
3233         &dev_attr_fw_crash_buffer_size,
3234         &dev_attr_fw_crash_buffer,
3235         &dev_attr_fw_crash_state,
3236         &dev_attr_page_size,
3237         &dev_attr_ldio_outstanding,
3238         &dev_attr_fw_cmds_outstanding,
3239         NULL,
3240 };
3241
3242 /*
3243  * Scsi host template for megaraid_sas driver
3244  */
3245 static struct scsi_host_template megasas_template = {
3246
3247         .module = THIS_MODULE,
3248         .name = "Avago SAS based MegaRAID driver",
3249         .proc_name = "megaraid_sas",
3250         .slave_configure = megasas_slave_configure,
3251         .slave_alloc = megasas_slave_alloc,
3252         .slave_destroy = megasas_slave_destroy,
3253         .queuecommand = megasas_queue_command,
3254         .eh_target_reset_handler = megasas_reset_target,
3255         .eh_abort_handler = megasas_task_abort,
3256         .eh_host_reset_handler = megasas_reset_bus_host,
3257         .eh_timed_out = megasas_reset_timer,
3258         .shost_attrs = megaraid_host_attrs,
3259         .bios_param = megasas_bios_param,
3260         .change_queue_depth = scsi_change_queue_depth,
3261         .no_write_same = 1,
3262 };
3263
3264 /**
3265  * megasas_complete_int_cmd -   Completes an internal command
3266  * @instance:                   Adapter soft state
3267  * @cmd:                        Command to be completed
3268  *
3269  * The megasas_issue_blocked_cmd() function waits for a command to complete
3270  * after it issues a command. This function wakes up that waiting routine by
3271  * calling wake_up() on the wait queue.
3272  */
3273 static void
3274 megasas_complete_int_cmd(struct megasas_instance *instance,
3275                          struct megasas_cmd *cmd)
3276 {
3277         cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3278         wake_up(&instance->int_cmd_wait_q);
3279 }
3280
3281 /**
3282  * megasas_complete_abort -     Completes aborting a command
3283  * @instance:                   Adapter soft state
3284  * @cmd:                        Cmd that was issued to abort another cmd
3285  *
3286  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3287  * after it issues an abort on a previously issued command. This function
3288  * wakes up all functions waiting on the same wait queue.
3289  */
3290 static void
3291 megasas_complete_abort(struct megasas_instance *instance,
3292                        struct megasas_cmd *cmd)
3293 {
3294         if (cmd->sync_cmd) {
3295                 cmd->sync_cmd = 0;
3296                 cmd->cmd_status_drv = 0;
3297                 wake_up(&instance->abort_cmd_wait_q);
3298         }
3299 }
3300
3301 /**
3302  * megasas_complete_cmd -       Completes a command
3303  * @instance:                   Adapter soft state
3304  * @cmd:                        Command to be completed
3305  * @alt_status:                 If non-zero, use this value as status to
3306  *                              SCSI mid-layer instead of the value returned
3307  *                              by the FW. This should be used if caller wants
3308  *                              an alternate status (as in the case of aborted
3309  *                              commands)
3310  */
3311 void
3312 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3313                      u8 alt_status)
3314 {
3315         int exception = 0;
3316         struct megasas_header *hdr = &cmd->frame->hdr;
3317         unsigned long flags;
3318         struct fusion_context *fusion = instance->ctrl_context;
3319         u32 opcode, status;
3320
3321         /* flag for the retry reset */
3322         cmd->retry_for_fw_reset = 0;
3323
3324         if (cmd->scmd)
3325                 cmd->scmd->SCp.ptr = NULL;
3326
3327         switch (hdr->cmd) {
3328         case MFI_CMD_INVALID:
3329                 /* Some older 1068 controller FW may keep a pended
3330                    MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3331                    when booting the kdump kernel.  Ignore this command to
3332                    prevent a kernel panic on shutdown of the kdump kernel. */
3333                 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3334                        "completed\n");
3335                 dev_warn(&instance->pdev->dev, "If you have a controller "
3336                        "other than PERC5, please upgrade your firmware\n");
3337                 break;
3338         case MFI_CMD_PD_SCSI_IO:
3339         case MFI_CMD_LD_SCSI_IO:
3340
3341                 /*
3342                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3343                  * issued either through an IO path or an IOCTL path. If it
3344                  * was via IOCTL, we will send it to internal completion.
3345                  */
3346                 if (cmd->sync_cmd) {
3347                         cmd->sync_cmd = 0;
3348                         megasas_complete_int_cmd(instance, cmd);
3349                         break;
3350                 }
3351                 /* fall through */
3352
3353         case MFI_CMD_LD_READ:
3354         case MFI_CMD_LD_WRITE:
3355
3356                 if (alt_status) {
3357                         cmd->scmd->result = alt_status << 16;
3358                         exception = 1;
3359                 }
3360
3361                 if (exception) {
3362
3363                         atomic_dec(&instance->fw_outstanding);
3364
3365                         scsi_dma_unmap(cmd->scmd);
3366                         cmd->scmd->scsi_done(cmd->scmd);
3367                         megasas_return_cmd(instance, cmd);
3368
3369                         break;
3370                 }
3371
3372                 switch (hdr->cmd_status) {
3373
3374                 case MFI_STAT_OK:
3375                         cmd->scmd->result = DID_OK << 16;
3376                         break;
3377
3378                 case MFI_STAT_SCSI_IO_FAILED:
3379                 case MFI_STAT_LD_INIT_IN_PROGRESS:
3380                         cmd->scmd->result =
3381                             (DID_ERROR << 16) | hdr->scsi_status;
3382                         break;
3383
3384                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3385
3386                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3387
3388                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3389                                 memset(cmd->scmd->sense_buffer, 0,
3390                                        SCSI_SENSE_BUFFERSIZE);
3391                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3392                                        hdr->sense_len);
3393
3394                                 cmd->scmd->result |= DRIVER_SENSE << 24;
3395                         }
3396
3397                         break;
3398
3399                 case MFI_STAT_LD_OFFLINE:
3400                 case MFI_STAT_DEVICE_NOT_FOUND:
3401                         cmd->scmd->result = DID_BAD_TARGET << 16;
3402                         break;
3403
3404                 default:
3405                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3406                                hdr->cmd_status);
3407                         cmd->scmd->result = DID_ERROR << 16;
3408                         break;
3409                 }
3410
3411                 atomic_dec(&instance->fw_outstanding);
3412
3413                 scsi_dma_unmap(cmd->scmd);
3414                 cmd->scmd->scsi_done(cmd->scmd);
3415                 megasas_return_cmd(instance, cmd);
3416
3417                 break;
3418
3419         case MFI_CMD_SMP:
3420         case MFI_CMD_STP:
3421         case MFI_CMD_NVME:
3422                 megasas_complete_int_cmd(instance, cmd);
3423                 break;
3424
3425         case MFI_CMD_DCMD:
3426                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3427                 /* Check for LD map update */
3428                 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3429                         && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3430                         fusion->fast_path_io = 0;
3431                         spin_lock_irqsave(instance->host->host_lock, flags);
3432                         status = cmd->frame->hdr.cmd_status;
3433                         instance->map_update_cmd = NULL;
3434                         if (status != MFI_STAT_OK) {
3435                                 if (status != MFI_STAT_NOT_FOUND)
3436                                         dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3437                                                cmd->frame->hdr.cmd_status);
3438                                 else {
3439                                         megasas_return_cmd(instance, cmd);
3440                                         spin_unlock_irqrestore(
3441                                                 instance->host->host_lock,
3442                                                 flags);
3443                                         break;
3444                                 }
3445                         }
3446
3447                         megasas_return_cmd(instance, cmd);
3448
3449                         /*
3450                          * Set fast path IO to ZERO.
3451                          * Validate Map will set proper value.
3452                          * Meanwhile all IOs will go as LD IO.
3453                          */
3454                         if (status == MFI_STAT_OK &&
3455                             (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3456                                 instance->map_id++;
3457                                 fusion->fast_path_io = 1;
3458                         } else {
3459                                 fusion->fast_path_io = 0;
3460                         }
3461
3462                         megasas_sync_map_info(instance);
3463                         spin_unlock_irqrestore(instance->host->host_lock,
3464                                                flags);
3465                         break;
3466                 }
3467                 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3468                     opcode == MR_DCMD_CTRL_EVENT_GET) {
3469                         spin_lock_irqsave(&poll_aen_lock, flags);
3470                         megasas_poll_wait_aen = 0;
3471                         spin_unlock_irqrestore(&poll_aen_lock, flags);
3472                 }
3473
3474                 /* FW has an updated PD sequence */
3475                 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3476                         (cmd->frame->dcmd.mbox.b[0] == 1)) {
3477
3478                         spin_lock_irqsave(instance->host->host_lock, flags);
3479                         status = cmd->frame->hdr.cmd_status;
3480                         instance->jbod_seq_cmd = NULL;
3481                         megasas_return_cmd(instance, cmd);
3482
3483                         if (status == MFI_STAT_OK) {
3484                                 instance->pd_seq_map_id++;
3485                                 /* Re-register a pd sync seq num cmd */
3486                                 if (megasas_sync_pd_seq_num(instance, true))
3487                                         instance->use_seqnum_jbod_fp = false;
3488                         } else
3489                                 instance->use_seqnum_jbod_fp = false;
3490
3491                         spin_unlock_irqrestore(instance->host->host_lock, flags);
3492                         break;
3493                 }
3494
3495                 /*
3496                  * See if got an event notification
3497                  */
3498                 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3499                         megasas_service_aen(instance, cmd);
3500                 else
3501                         megasas_complete_int_cmd(instance, cmd);
3502
3503                 break;
3504
3505         case MFI_CMD_ABORT:
3506                 /*
3507                  * Cmd issued to abort another cmd returned
3508                  */
3509                 megasas_complete_abort(instance, cmd);
3510                 break;
3511
3512         default:
3513                 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3514                        hdr->cmd);
3515                 megasas_complete_int_cmd(instance, cmd);
3516                 break;
3517         }
3518 }
3519
3520 /**
3521  * megasas_issue_pending_cmds_again -   issue all pending cmds
3522  *                                      in FW again because of the fw reset
3523  * @instance:                           Adapter soft state
3524  */
3525 static inline void
3526 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3527 {
3528         struct megasas_cmd *cmd;
3529         struct list_head clist_local;
3530         union megasas_evt_class_locale class_locale;
3531         unsigned long flags;
3532         u32 seq_num;
3533
3534         INIT_LIST_HEAD(&clist_local);
3535         spin_lock_irqsave(&instance->hba_lock, flags);
3536         list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3537         spin_unlock_irqrestore(&instance->hba_lock, flags);
3538
3539         while (!list_empty(&clist_local)) {
3540                 cmd = list_entry((&clist_local)->next,
3541                                         struct megasas_cmd, list);
3542                 list_del_init(&cmd->list);
3543
3544                 if (cmd->sync_cmd || cmd->scmd) {
3545                         dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3546                                 "detected to be pending while HBA reset\n",
3547                                         cmd, cmd->scmd, cmd->sync_cmd);
3548
3549                         cmd->retry_for_fw_reset++;
3550
3551                         if (cmd->retry_for_fw_reset == 3) {
3552                                 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3553                                         "was tried multiple times during reset."
3554                                         "Shutting down the HBA\n",
3555                                         cmd, cmd->scmd, cmd->sync_cmd);
3556                                 instance->instancet->disable_intr(instance);
3557                                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3558                                 megaraid_sas_kill_hba(instance);
3559                                 return;
3560                         }
3561                 }
3562
3563                 if (cmd->sync_cmd == 1) {
3564                         if (cmd->scmd) {
3565                                 dev_notice(&instance->pdev->dev, "unexpected"
3566                                         "cmd attached to internal command!\n");
3567                         }
3568                         dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3569                                                 "on the internal reset queue,"
3570                                                 "issue it again.\n", cmd);
3571                         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3572                         instance->instancet->fire_cmd(instance,
3573                                                         cmd->frame_phys_addr,
3574                                                         0, instance->reg_set);
3575                 } else if (cmd->scmd) {
3576                         dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3577                         "detected on the internal queue, issue again.\n",
3578                         cmd, cmd->scmd->cmnd[0]);
3579
3580                         atomic_inc(&instance->fw_outstanding);
3581                         instance->instancet->fire_cmd(instance,
3582                                         cmd->frame_phys_addr,
3583                                         cmd->frame_count-1, instance->reg_set);
3584                 } else {
3585                         dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3586                                 "internal reset defer list while re-issue!!\n",
3587                                 cmd);
3588                 }
3589         }
3590
3591         if (instance->aen_cmd) {
3592                 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3593                 megasas_return_cmd(instance, instance->aen_cmd);
3594
3595                 instance->aen_cmd = NULL;
3596         }
3597
3598         /*
3599          * Initiate AEN (Asynchronous Event Notification)
3600          */
3601         seq_num = instance->last_seq_num;
3602         class_locale.members.reserved = 0;
3603         class_locale.members.locale = MR_EVT_LOCALE_ALL;
3604         class_locale.members.class = MR_EVT_CLASS_DEBUG;
3605
3606         megasas_register_aen(instance, seq_num, class_locale.word);
3607 }
3608
3609 /**
3610  * Move the internal reset pending commands to a deferred queue.
3611  *
3612  * We move the commands pending at internal reset time to a
3613  * pending queue. This queue would be flushed after successful
3614  * completion of the internal reset sequence. if the internal reset
3615  * did not complete in time, the kernel reset handler would flush
3616  * these commands.
3617  **/
3618 static void
3619 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3620 {
3621         struct megasas_cmd *cmd;
3622         int i;
3623         u16 max_cmd = instance->max_fw_cmds;
3624         u32 defer_index;
3625         unsigned long flags;
3626
3627         defer_index = 0;
3628         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3629         for (i = 0; i < max_cmd; i++) {
3630                 cmd = instance->cmd_list[i];
3631                 if (cmd->sync_cmd == 1 || cmd->scmd) {
3632                         dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3633                                         "on the defer queue as internal\n",
3634                                 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3635
3636                         if (!list_empty(&cmd->list)) {
3637                                 dev_notice(&instance->pdev->dev, "ERROR while"
3638                                         " moving this cmd:%p, %d %p, it was"
3639                                         "discovered on some list?\n",
3640                                         cmd, cmd->sync_cmd, cmd->scmd);
3641
3642                                 list_del_init(&cmd->list);
3643                         }
3644                         defer_index++;
3645                         list_add_tail(&cmd->list,
3646                                 &instance->internal_reset_pending_q);
3647                 }
3648         }
3649         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3650 }
3651
3652
3653 static void
3654 process_fw_state_change_wq(struct work_struct *work)
3655 {
3656         struct megasas_instance *instance =
3657                 container_of(work, struct megasas_instance, work_init);
3658         u32 wait;
3659         unsigned long flags;
3660
3661     if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3662                 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3663                                 atomic_read(&instance->adprecovery));
3664                 return ;
3665         }
3666
3667         if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3668                 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3669                                         "state, restarting it...\n");
3670
3671                 instance->instancet->disable_intr(instance);
3672                 atomic_set(&instance->fw_outstanding, 0);
3673
3674                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3675                 instance->instancet->adp_reset(instance, instance->reg_set);
3676                 atomic_set(&instance->fw_reset_no_pci_access, 0);
3677
3678                 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3679                                         "initiating next stage...\n");
3680
3681                 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3682                                         "state 2 starting...\n");
3683
3684                 /* waiting for about 20 second before start the second init */
3685                 for (wait = 0; wait < 30; wait++) {
3686                         msleep(1000);
3687                 }
3688
3689                 if (megasas_transition_to_ready(instance, 1)) {
3690                         dev_notice(&instance->pdev->dev, "adapter not ready\n");
3691
3692                         atomic_set(&instance->fw_reset_no_pci_access, 1);
3693                         megaraid_sas_kill_hba(instance);
3694                         return ;
3695                 }
3696
3697                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3698                         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3699                         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3700                         ) {
3701                         *instance->consumer = *instance->producer;
3702                 } else {
3703                         *instance->consumer = 0;
3704                         *instance->producer = 0;
3705                 }
3706
3707                 megasas_issue_init_mfi(instance);
3708
3709                 spin_lock_irqsave(&instance->hba_lock, flags);
3710                 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3711                 spin_unlock_irqrestore(&instance->hba_lock, flags);
3712                 instance->instancet->enable_intr(instance);
3713
3714                 megasas_issue_pending_cmds_again(instance);
3715                 instance->issuepend_done = 1;
3716         }
3717 }
3718
3719 /**
3720  * megasas_deplete_reply_queue -        Processes all completed commands
3721  * @instance:                           Adapter soft state
3722  * @alt_status:                         Alternate status to be returned to
3723  *                                      SCSI mid-layer instead of the status
3724  *                                      returned by the FW
3725  * Note: this must be called with hba lock held
3726  */
3727 static int
3728 megasas_deplete_reply_queue(struct megasas_instance *instance,
3729                                         u8 alt_status)
3730 {
3731         u32 mfiStatus;
3732         u32 fw_state;
3733
3734         if ((mfiStatus = instance->instancet->check_reset(instance,
3735                                         instance->reg_set)) == 1) {
3736                 return IRQ_HANDLED;
3737         }
3738
3739         mfiStatus = instance->instancet->clear_intr(instance);
3740         if (mfiStatus == 0) {
3741                 /* Hardware may not set outbound_intr_status in MSI-X mode */
3742                 if (!instance->msix_vectors)
3743                         return IRQ_NONE;
3744         }
3745
3746         instance->mfiStatus = mfiStatus;
3747
3748         if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3749                 fw_state = instance->instancet->read_fw_status_reg(
3750                                 instance) & MFI_STATE_MASK;
3751
3752                 if (fw_state != MFI_STATE_FAULT) {
3753                         dev_notice(&instance->pdev->dev, "fw state:%x\n",
3754                                                 fw_state);
3755                 }
3756
3757                 if ((fw_state == MFI_STATE_FAULT) &&
3758                                 (instance->disableOnlineCtrlReset == 0)) {
3759                         dev_notice(&instance->pdev->dev, "wait adp restart\n");
3760
3761                         if ((instance->pdev->device ==
3762                                         PCI_DEVICE_ID_LSI_SAS1064R) ||
3763                                 (instance->pdev->device ==
3764                                         PCI_DEVICE_ID_DELL_PERC5) ||
3765                                 (instance->pdev->device ==
3766                                         PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3767
3768                                 *instance->consumer =
3769                                         cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3770                         }
3771
3772
3773                         instance->instancet->disable_intr(instance);
3774                         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3775                         instance->issuepend_done = 0;
3776
3777                         atomic_set(&instance->fw_outstanding, 0);
3778                         megasas_internal_reset_defer_cmds(instance);
3779
3780                         dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3781                                         fw_state, atomic_read(&instance->adprecovery));
3782
3783                         schedule_work(&instance->work_init);
3784                         return IRQ_HANDLED;
3785
3786                 } else {
3787                         dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3788                                 fw_state, instance->disableOnlineCtrlReset);
3789                 }
3790         }
3791
3792         tasklet_schedule(&instance->isr_tasklet);
3793         return IRQ_HANDLED;
3794 }
3795 /**
3796  * megasas_isr - isr entry point
3797  */
3798 static irqreturn_t megasas_isr(int irq, void *devp)
3799 {
3800         struct megasas_irq_context *irq_context = devp;
3801         struct megasas_instance *instance = irq_context->instance;
3802         unsigned long flags;
3803         irqreturn_t rc;
3804
3805         if (atomic_read(&instance->fw_reset_no_pci_access))
3806                 return IRQ_HANDLED;
3807
3808         spin_lock_irqsave(&instance->hba_lock, flags);
3809         rc = megasas_deplete_reply_queue(instance, DID_OK);
3810         spin_unlock_irqrestore(&instance->hba_lock, flags);
3811
3812         return rc;
3813 }
3814
3815 /**
3816  * megasas_transition_to_ready -        Move the FW to READY state
3817  * @instance:                           Adapter soft state
3818  *
3819  * During the initialization, FW passes can potentially be in any one of
3820  * several possible states. If the FW in operational, waiting-for-handshake
3821  * states, driver must take steps to bring it to ready state. Otherwise, it
3822  * has to wait for the ready state.
3823  */
3824 int
3825 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3826 {
3827         int i;
3828         u8 max_wait;
3829         u32 fw_state;
3830         u32 cur_state;
3831         u32 abs_state, curr_abs_state;
3832
3833         abs_state = instance->instancet->read_fw_status_reg(instance);
3834         fw_state = abs_state & MFI_STATE_MASK;
3835
3836         if (fw_state != MFI_STATE_READY)
3837                 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3838                        " state\n");
3839
3840         while (fw_state != MFI_STATE_READY) {
3841
3842                 switch (fw_state) {
3843
3844                 case MFI_STATE_FAULT:
3845                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3846                         if (ocr) {
3847                                 max_wait = MEGASAS_RESET_WAIT_TIME;
3848                                 cur_state = MFI_STATE_FAULT;
3849                                 break;
3850                         } else
3851                                 return -ENODEV;
3852
3853                 case MFI_STATE_WAIT_HANDSHAKE:
3854                         /*
3855                          * Set the CLR bit in inbound doorbell
3856                          */
3857                         if ((instance->pdev->device ==
3858                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3859                                 (instance->pdev->device ==
3860                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3861                                 (instance->adapter_type != MFI_SERIES))
3862                                 writel(
3863                                   MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3864                                   &instance->reg_set->doorbell);
3865                         else
3866                                 writel(
3867                                     MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3868                                         &instance->reg_set->inbound_doorbell);
3869
3870                         max_wait = MEGASAS_RESET_WAIT_TIME;
3871                         cur_state = MFI_STATE_WAIT_HANDSHAKE;
3872                         break;
3873
3874                 case MFI_STATE_BOOT_MESSAGE_PENDING:
3875                         if ((instance->pdev->device ==
3876                              PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3877                                 (instance->pdev->device ==
3878                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3879                                 (instance->adapter_type != MFI_SERIES))
3880                                 writel(MFI_INIT_HOTPLUG,
3881                                        &instance->reg_set->doorbell);
3882                         else
3883                                 writel(MFI_INIT_HOTPLUG,
3884                                         &instance->reg_set->inbound_doorbell);
3885
3886                         max_wait = MEGASAS_RESET_WAIT_TIME;
3887                         cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3888                         break;
3889
3890                 case MFI_STATE_OPERATIONAL:
3891                         /*
3892                          * Bring it to READY state; assuming max wait 10 secs
3893                          */
3894                         instance->instancet->disable_intr(instance);
3895                         if ((instance->pdev->device ==
3896                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3897                                 (instance->pdev->device ==
3898                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
3899                                 (instance->adapter_type != MFI_SERIES)) {
3900                                 writel(MFI_RESET_FLAGS,
3901                                         &instance->reg_set->doorbell);
3902
3903                                 if (instance->adapter_type != MFI_SERIES) {
3904                                         for (i = 0; i < (10 * 1000); i += 20) {
3905                                                 if (megasas_readl(
3906                                                             instance,
3907                                                             &instance->
3908                                                             reg_set->
3909                                                             doorbell) & 1)
3910                                                         msleep(20);
3911                                                 else
3912                                                         break;
3913                                         }
3914                                 }
3915                         } else
3916                                 writel(MFI_RESET_FLAGS,
3917                                         &instance->reg_set->inbound_doorbell);
3918
3919                         max_wait = MEGASAS_RESET_WAIT_TIME;
3920                         cur_state = MFI_STATE_OPERATIONAL;
3921                         break;
3922
3923                 case MFI_STATE_UNDEFINED:
3924                         /*
3925                          * This state should not last for more than 2 seconds
3926                          */
3927                         max_wait = MEGASAS_RESET_WAIT_TIME;
3928                         cur_state = MFI_STATE_UNDEFINED;
3929                         break;
3930
3931                 case MFI_STATE_BB_INIT:
3932                         max_wait = MEGASAS_RESET_WAIT_TIME;
3933                         cur_state = MFI_STATE_BB_INIT;
3934                         break;
3935
3936                 case MFI_STATE_FW_INIT:
3937                         max_wait = MEGASAS_RESET_WAIT_TIME;
3938                         cur_state = MFI_STATE_FW_INIT;
3939                         break;
3940
3941                 case MFI_STATE_FW_INIT_2:
3942                         max_wait = MEGASAS_RESET_WAIT_TIME;
3943                         cur_state = MFI_STATE_FW_INIT_2;
3944                         break;
3945
3946                 case MFI_STATE_DEVICE_SCAN:
3947                         max_wait = MEGASAS_RESET_WAIT_TIME;
3948                         cur_state = MFI_STATE_DEVICE_SCAN;
3949                         break;
3950
3951                 case MFI_STATE_FLUSH_CACHE:
3952                         max_wait = MEGASAS_RESET_WAIT_TIME;
3953                         cur_state = MFI_STATE_FLUSH_CACHE;
3954                         break;
3955
3956                 default:
3957                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3958                                fw_state);
3959                         return -ENODEV;
3960                 }
3961
3962                 /*
3963                  * The cur_state should not last for more than max_wait secs
3964                  */
3965                 for (i = 0; i < max_wait * 50; i++) {
3966                         curr_abs_state = instance->instancet->
3967                                 read_fw_status_reg(instance);
3968
3969                         if (abs_state == curr_abs_state) {
3970                                 msleep(20);
3971                         } else
3972                                 break;
3973                 }
3974
3975                 /*
3976                  * Return error if fw_state hasn't changed after max_wait
3977                  */
3978                 if (curr_abs_state == abs_state) {
3979                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3980                                "in %d secs\n", fw_state, max_wait);
3981                         return -ENODEV;
3982                 }
3983
3984                 abs_state = curr_abs_state;
3985                 fw_state = curr_abs_state & MFI_STATE_MASK;
3986         }
3987         dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3988
3989         return 0;
3990 }
3991
3992 /**
3993  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
3994  * @instance:                           Adapter soft state
3995  */
3996 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3997 {
3998         int i;
3999         u16 max_cmd = instance->max_mfi_cmds;
4000         struct megasas_cmd *cmd;
4001
4002         if (!instance->frame_dma_pool)
4003                 return;
4004
4005         /*
4006          * Return all frames to pool
4007          */
4008         for (i = 0; i < max_cmd; i++) {
4009
4010                 cmd = instance->cmd_list[i];
4011
4012                 if (cmd->frame)
4013                         dma_pool_free(instance->frame_dma_pool, cmd->frame,
4014                                       cmd->frame_phys_addr);
4015
4016                 if (cmd->sense)
4017                         dma_pool_free(instance->sense_dma_pool, cmd->sense,
4018                                       cmd->sense_phys_addr);
4019         }
4020
4021         /*
4022          * Now destroy the pool itself
4023          */
4024         dma_pool_destroy(instance->frame_dma_pool);
4025         dma_pool_destroy(instance->sense_dma_pool);
4026
4027         instance->frame_dma_pool = NULL;
4028         instance->sense_dma_pool = NULL;
4029 }
4030
4031 /**
4032  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
4033  * @instance:                   Adapter soft state
4034  *
4035  * Each command packet has an embedded DMA memory buffer that is used for
4036  * filling MFI frame and the SG list that immediately follows the frame. This
4037  * function creates those DMA memory buffers for each command packet by using
4038  * PCI pool facility.
4039  */
4040 static int megasas_create_frame_pool(struct megasas_instance *instance)
4041 {
4042         int i;
4043         u16 max_cmd;
4044         u32 sge_sz;
4045         u32 frame_count;
4046         struct megasas_cmd *cmd;
4047
4048         max_cmd = instance->max_mfi_cmds;
4049
4050         /*
4051          * Size of our frame is 64 bytes for MFI frame, followed by max SG
4052          * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4053          */
4054         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4055             sizeof(struct megasas_sge32);
4056
4057         if (instance->flag_ieee)
4058                 sge_sz = sizeof(struct megasas_sge_skinny);
4059
4060         /*
4061          * For MFI controllers.
4062          * max_num_sge = 60
4063          * max_sge_sz  = 16 byte (sizeof megasas_sge_skinny)
4064          * Total 960 byte (15 MFI frame of 64 byte)
4065          *
4066          * Fusion adapter require only 3 extra frame.
4067          * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4068          * max_sge_sz  = 12 byte (sizeof  megasas_sge64)
4069          * Total 192 byte (3 MFI frame of 64 byte)
4070          */
4071         frame_count = (instance->adapter_type == MFI_SERIES) ?
4072                         (15 + 1) : (3 + 1);
4073         instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4074         /*
4075          * Use DMA pool facility provided by PCI layer
4076          */
4077         instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4078                                         &instance->pdev->dev,
4079                                         instance->mfi_frame_size, 256, 0);
4080
4081         if (!instance->frame_dma_pool) {
4082                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4083                 return -ENOMEM;
4084         }
4085
4086         instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4087                                                    &instance->pdev->dev, 128,
4088                                                    4, 0);
4089
4090         if (!instance->sense_dma_pool) {
4091                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4092
4093                 dma_pool_destroy(instance->frame_dma_pool);
4094                 instance->frame_dma_pool = NULL;
4095
4096                 return -ENOMEM;
4097         }
4098
4099         /*
4100          * Allocate and attach a frame to each of the commands in cmd_list.
4101          * By making cmd->index as the context instead of the &cmd, we can
4102          * always use 32bit context regardless of the architecture
4103          */
4104         for (i = 0; i < max_cmd; i++) {
4105
4106                 cmd = instance->cmd_list[i];
4107
4108                 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4109                                             GFP_KERNEL, &cmd->frame_phys_addr);
4110
4111                 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4112                                             GFP_KERNEL, &cmd->sense_phys_addr);
4113
4114                 /*
4115                  * megasas_teardown_frame_pool() takes care of freeing
4116                  * whatever has been allocated
4117                  */
4118                 if (!cmd->frame || !cmd->sense) {
4119                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4120                         megasas_teardown_frame_pool(instance);
4121                         return -ENOMEM;
4122                 }
4123
4124                 cmd->frame->io.context = cpu_to_le32(cmd->index);
4125                 cmd->frame->io.pad_0 = 0;
4126                 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4127                         cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4128         }
4129
4130         return 0;
4131 }
4132
4133 /**
4134  * megasas_free_cmds -  Free all the cmds in the free cmd pool
4135  * @instance:           Adapter soft state
4136  */
4137 void megasas_free_cmds(struct megasas_instance *instance)
4138 {
4139         int i;
4140
4141         /* First free the MFI frame pool */
4142         megasas_teardown_frame_pool(instance);
4143
4144         /* Free all the commands in the cmd_list */
4145         for (i = 0; i < instance->max_mfi_cmds; i++)
4146
4147                 kfree(instance->cmd_list[i]);
4148
4149         /* Free the cmd_list buffer itself */
4150         kfree(instance->cmd_list);
4151         instance->cmd_list = NULL;
4152
4153         INIT_LIST_HEAD(&instance->cmd_pool);
4154 }
4155
4156 /**
4157  * megasas_alloc_cmds - Allocates the command packets
4158  * @instance:           Adapter soft state
4159  *
4160  * Each command that is issued to the FW, whether IO commands from the OS or
4161  * internal commands like IOCTLs, are wrapped in local data structure called
4162  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4163  * the FW.
4164  *
4165  * Each frame has a 32-bit field called context (tag). This context is used
4166  * to get back the megasas_cmd from the frame when a frame gets completed in
4167  * the ISR. Typically the address of the megasas_cmd itself would be used as
4168  * the context. But we wanted to keep the differences between 32 and 64 bit
4169  * systems to the mininum. We always use 32 bit integers for the context. In
4170  * this driver, the 32 bit values are the indices into an array cmd_list.
4171  * This array is used only to look up the megasas_cmd given the context. The
4172  * free commands themselves are maintained in a linked list called cmd_pool.
4173  */
4174 int megasas_alloc_cmds(struct megasas_instance *instance)
4175 {
4176         int i;
4177         int j;
4178         u16 max_cmd;
4179         struct megasas_cmd *cmd;
4180
4181         max_cmd = instance->max_mfi_cmds;
4182
4183         /*
4184          * instance->cmd_list is an array of struct megasas_cmd pointers.
4185          * Allocate the dynamic array first and then allocate individual
4186          * commands.
4187          */
4188         instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4189
4190         if (!instance->cmd_list) {
4191                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4192                 return -ENOMEM;
4193         }
4194
4195         memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4196
4197         for (i = 0; i < max_cmd; i++) {
4198                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4199                                                 GFP_KERNEL);
4200
4201                 if (!instance->cmd_list[i]) {
4202
4203                         for (j = 0; j < i; j++)
4204                                 kfree(instance->cmd_list[j]);
4205
4206                         kfree(instance->cmd_list);
4207                         instance->cmd_list = NULL;
4208
4209                         return -ENOMEM;
4210                 }
4211         }
4212
4213         for (i = 0; i < max_cmd; i++) {
4214                 cmd = instance->cmd_list[i];
4215                 memset(cmd, 0, sizeof(struct megasas_cmd));
4216                 cmd->index = i;
4217                 cmd->scmd = NULL;
4218                 cmd->instance = instance;
4219
4220                 list_add_tail(&cmd->list, &instance->cmd_pool);
4221         }
4222
4223         /*
4224          * Create a frame pool and assign one frame to each cmd
4225          */
4226         if (megasas_create_frame_pool(instance)) {
4227                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4228                 megasas_free_cmds(instance);
4229                 return -ENOMEM;
4230         }
4231
4232         return 0;
4233 }
4234
4235 /*
4236  * dcmd_timeout_ocr_possible -  Check if OCR is possible based on Driver/FW state.
4237  * @instance:                           Adapter soft state
4238  *
4239  * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4240  * or FW is not under OCR.
4241  */
4242 inline int
4243 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4244
4245         if (instance->adapter_type == MFI_SERIES)
4246                 return KILL_ADAPTER;
4247         else if (instance->unload ||
4248                         test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4249                 return IGNORE_TIMEOUT;
4250         else
4251                 return INITIATE_OCR;
4252 }
4253
4254 static void
4255 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4256 {
4257         int ret;
4258         struct megasas_cmd *cmd;
4259         struct megasas_dcmd_frame *dcmd;
4260
4261         struct MR_PRIV_DEVICE *mr_device_priv_data;
4262         u16 device_id = 0;
4263
4264         device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4265         cmd = megasas_get_cmd(instance);
4266
4267         if (!cmd) {
4268                 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4269                 return;
4270         }
4271
4272         dcmd = &cmd->frame->dcmd;
4273
4274         memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4275         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4276
4277         dcmd->mbox.s[0] = cpu_to_le16(device_id);
4278         dcmd->cmd = MFI_CMD_DCMD;
4279         dcmd->cmd_status = 0xFF;
4280         dcmd->sge_count = 1;
4281         dcmd->flags = MFI_FRAME_DIR_READ;
4282         dcmd->timeout = 0;
4283         dcmd->pad_0 = 0;
4284         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4285         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4286
4287         megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4288                                  sizeof(struct MR_PD_INFO));
4289
4290         if ((instance->adapter_type != MFI_SERIES) &&
4291             !instance->mask_interrupts)
4292                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4293         else
4294                 ret = megasas_issue_polled(instance, cmd);
4295
4296         switch (ret) {
4297         case DCMD_SUCCESS:
4298                 mr_device_priv_data = sdev->hostdata;
4299                 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4300                 mr_device_priv_data->interface_type =
4301                                 instance->pd_info->state.ddf.pdType.intf;
4302                 break;
4303
4304         case DCMD_TIMEOUT:
4305
4306                 switch (dcmd_timeout_ocr_possible(instance)) {
4307                 case INITIATE_OCR:
4308                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4309                         megasas_reset_fusion(instance->host,
4310                                 MFI_IO_TIMEOUT_OCR);
4311                         break;
4312                 case KILL_ADAPTER:
4313                         megaraid_sas_kill_hba(instance);
4314                         break;
4315                 case IGNORE_TIMEOUT:
4316                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4317                                 __func__, __LINE__);
4318                         break;
4319                 }
4320
4321                 break;
4322         }
4323
4324         if (ret != DCMD_TIMEOUT)
4325                 megasas_return_cmd(instance, cmd);
4326
4327         return;
4328 }
4329 /*
4330  * megasas_get_pd_list_info -   Returns FW's pd_list structure
4331  * @instance:                           Adapter soft state
4332  * @pd_list:                            pd_list structure
4333  *
4334  * Issues an internal command (DCMD) to get the FW's controller PD
4335  * list structure.  This information is mainly used to find out SYSTEM
4336  * supported by the FW.
4337  */
4338 static int
4339 megasas_get_pd_list(struct megasas_instance *instance)
4340 {
4341         int ret = 0, pd_index = 0;
4342         struct megasas_cmd *cmd;
4343         struct megasas_dcmd_frame *dcmd;
4344         struct MR_PD_LIST *ci;
4345         struct MR_PD_ADDRESS *pd_addr;
4346         dma_addr_t ci_h = 0;
4347
4348         if (instance->pd_list_not_supported) {
4349                 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4350                 "not supported by firmware\n");
4351                 return ret;
4352         }
4353
4354         ci = instance->pd_list_buf;
4355         ci_h = instance->pd_list_buf_h;
4356
4357         cmd = megasas_get_cmd(instance);
4358
4359         if (!cmd) {
4360                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4361                 return -ENOMEM;
4362         }
4363
4364         dcmd = &cmd->frame->dcmd;
4365
4366         memset(ci, 0, sizeof(*ci));
4367         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4368
4369         dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4370         dcmd->mbox.b[1] = 0;
4371         dcmd->cmd = MFI_CMD_DCMD;
4372         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4373         dcmd->sge_count = 1;
4374         dcmd->flags = MFI_FRAME_DIR_READ;
4375         dcmd->timeout = 0;
4376         dcmd->pad_0 = 0;
4377         dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4378         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4379
4380         megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4381                                  (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4382
4383         if ((instance->adapter_type != MFI_SERIES) &&
4384             !instance->mask_interrupts)
4385                 ret = megasas_issue_blocked_cmd(instance, cmd,
4386                         MFI_IO_TIMEOUT_SECS);
4387         else
4388                 ret = megasas_issue_polled(instance, cmd);
4389
4390         switch (ret) {
4391         case DCMD_FAILED:
4392                 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4393                         "failed/not supported by firmware\n");
4394
4395                 if (instance->adapter_type != MFI_SERIES)
4396                         megaraid_sas_kill_hba(instance);
4397                 else
4398                         instance->pd_list_not_supported = 1;
4399                 break;
4400         case DCMD_TIMEOUT:
4401
4402                 switch (dcmd_timeout_ocr_possible(instance)) {
4403                 case INITIATE_OCR:
4404                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4405                         /*
4406                          * DCMD failed from AEN path.
4407                          * AEN path already hold reset_mutex to avoid PCI access
4408                          * while OCR is in progress.
4409                          */
4410                         mutex_unlock(&instance->reset_mutex);
4411                         megasas_reset_fusion(instance->host,
4412                                                 MFI_IO_TIMEOUT_OCR);
4413                         mutex_lock(&instance->reset_mutex);
4414                         break;
4415                 case KILL_ADAPTER:
4416                         megaraid_sas_kill_hba(instance);
4417                         break;
4418                 case IGNORE_TIMEOUT:
4419                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4420                                 __func__, __LINE__);
4421                         break;
4422                 }
4423
4424                 break;
4425
4426         case DCMD_SUCCESS:
4427                 pd_addr = ci->addr;
4428
4429                 if ((le32_to_cpu(ci->count) >
4430                         (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4431                         break;
4432
4433                 memset(instance->local_pd_list, 0,
4434                                 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4435
4436                 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4437                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid     =
4438                                         le16_to_cpu(pd_addr->deviceId);
4439                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType       =
4440                                         pd_addr->scsiDevType;
4441                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState      =
4442                                         MR_PD_STATE_SYSTEM;
4443                         pd_addr++;
4444                 }
4445
4446                 memcpy(instance->pd_list, instance->local_pd_list,
4447                         sizeof(instance->pd_list));
4448                 break;
4449
4450         }
4451
4452         if (ret != DCMD_TIMEOUT)
4453                 megasas_return_cmd(instance, cmd);
4454
4455         return ret;
4456 }
4457
4458 /*
4459  * megasas_get_ld_list_info -   Returns FW's ld_list structure
4460  * @instance:                           Adapter soft state
4461  * @ld_list:                            ld_list structure
4462  *
4463  * Issues an internal command (DCMD) to get the FW's controller PD
4464  * list structure.  This information is mainly used to find out SYSTEM
4465  * supported by the FW.
4466  */
4467 static int
4468 megasas_get_ld_list(struct megasas_instance *instance)
4469 {
4470         int ret = 0, ld_index = 0, ids = 0;
4471         struct megasas_cmd *cmd;
4472         struct megasas_dcmd_frame *dcmd;
4473         struct MR_LD_LIST *ci;
4474         dma_addr_t ci_h = 0;
4475         u32 ld_count;
4476
4477         ci = instance->ld_list_buf;
4478         ci_h = instance->ld_list_buf_h;
4479
4480         cmd = megasas_get_cmd(instance);
4481
4482         if (!cmd) {
4483                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4484                 return -ENOMEM;
4485         }
4486
4487         dcmd = &cmd->frame->dcmd;
4488
4489         memset(ci, 0, sizeof(*ci));
4490         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4491
4492         if (instance->supportmax256vd)
4493                 dcmd->mbox.b[0] = 1;
4494         dcmd->cmd = MFI_CMD_DCMD;
4495         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4496         dcmd->sge_count = 1;
4497         dcmd->flags = MFI_FRAME_DIR_READ;
4498         dcmd->timeout = 0;
4499         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4500         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4501         dcmd->pad_0  = 0;
4502
4503         megasas_set_dma_settings(instance, dcmd, ci_h,
4504                                  sizeof(struct MR_LD_LIST));
4505
4506         if ((instance->adapter_type != MFI_SERIES) &&
4507             !instance->mask_interrupts)
4508                 ret = megasas_issue_blocked_cmd(instance, cmd,
4509                         MFI_IO_TIMEOUT_SECS);
4510         else
4511                 ret = megasas_issue_polled(instance, cmd);
4512
4513         ld_count = le32_to_cpu(ci->ldCount);
4514
4515         switch (ret) {
4516         case DCMD_FAILED:
4517                 megaraid_sas_kill_hba(instance);
4518                 break;
4519         case DCMD_TIMEOUT:
4520
4521                 switch (dcmd_timeout_ocr_possible(instance)) {
4522                 case INITIATE_OCR:
4523                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4524                         /*
4525                          * DCMD failed from AEN path.
4526                          * AEN path already hold reset_mutex to avoid PCI access
4527                          * while OCR is in progress.
4528                          */
4529                         mutex_unlock(&instance->reset_mutex);
4530                         megasas_reset_fusion(instance->host,
4531                                                 MFI_IO_TIMEOUT_OCR);
4532                         mutex_lock(&instance->reset_mutex);
4533                         break;
4534                 case KILL_ADAPTER:
4535                         megaraid_sas_kill_hba(instance);
4536                         break;
4537                 case IGNORE_TIMEOUT:
4538                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4539                                 __func__, __LINE__);
4540                         break;
4541                 }
4542
4543                 break;
4544
4545         case DCMD_SUCCESS:
4546                 if (ld_count > instance->fw_supported_vd_count)
4547                         break;
4548
4549                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4550
4551                 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4552                         if (ci->ldList[ld_index].state != 0) {
4553                                 ids = ci->ldList[ld_index].ref.targetId;
4554                                 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4555                         }
4556                 }
4557
4558                 break;
4559         }
4560
4561         if (ret != DCMD_TIMEOUT)
4562                 megasas_return_cmd(instance, cmd);
4563
4564         return ret;
4565 }
4566
4567 /**
4568  * megasas_ld_list_query -      Returns FW's ld_list structure
4569  * @instance:                           Adapter soft state
4570  * @ld_list:                            ld_list structure
4571  *
4572  * Issues an internal command (DCMD) to get the FW's controller PD
4573  * list structure.  This information is mainly used to find out SYSTEM
4574  * supported by the FW.
4575  */
4576 static int
4577 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4578 {
4579         int ret = 0, ld_index = 0, ids = 0;
4580         struct megasas_cmd *cmd;
4581         struct megasas_dcmd_frame *dcmd;
4582         struct MR_LD_TARGETID_LIST *ci;
4583         dma_addr_t ci_h = 0;
4584         u32 tgtid_count;
4585
4586         ci = instance->ld_targetid_list_buf;
4587         ci_h = instance->ld_targetid_list_buf_h;
4588
4589         cmd = megasas_get_cmd(instance);
4590
4591         if (!cmd) {
4592                 dev_warn(&instance->pdev->dev,
4593                          "megasas_ld_list_query: Failed to get cmd\n");
4594                 return -ENOMEM;
4595         }
4596
4597         dcmd = &cmd->frame->dcmd;
4598
4599         memset(ci, 0, sizeof(*ci));
4600         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4601
4602         dcmd->mbox.b[0] = query_type;
4603         if (instance->supportmax256vd)
4604                 dcmd->mbox.b[2] = 1;
4605
4606         dcmd->cmd = MFI_CMD_DCMD;
4607         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4608         dcmd->sge_count = 1;
4609         dcmd->flags = MFI_FRAME_DIR_READ;
4610         dcmd->timeout = 0;
4611         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4612         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4613         dcmd->pad_0  = 0;
4614
4615         megasas_set_dma_settings(instance, dcmd, ci_h,
4616                                  sizeof(struct MR_LD_TARGETID_LIST));
4617
4618         if ((instance->adapter_type != MFI_SERIES) &&
4619             !instance->mask_interrupts)
4620                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4621         else
4622                 ret = megasas_issue_polled(instance, cmd);
4623
4624         switch (ret) {
4625         case DCMD_FAILED:
4626                 dev_info(&instance->pdev->dev,
4627                         "DCMD not supported by firmware - %s %d\n",
4628                                 __func__, __LINE__);
4629                 ret = megasas_get_ld_list(instance);
4630                 break;
4631         case DCMD_TIMEOUT:
4632                 switch (dcmd_timeout_ocr_possible(instance)) {
4633                 case INITIATE_OCR:
4634                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4635                         /*
4636                          * DCMD failed from AEN path.
4637                          * AEN path already hold reset_mutex to avoid PCI access
4638                          * while OCR is in progress.
4639                          */
4640                         mutex_unlock(&instance->reset_mutex);
4641                         megasas_reset_fusion(instance->host,
4642                                                 MFI_IO_TIMEOUT_OCR);
4643                         mutex_lock(&instance->reset_mutex);
4644                         break;
4645                 case KILL_ADAPTER:
4646                         megaraid_sas_kill_hba(instance);
4647                         break;
4648                 case IGNORE_TIMEOUT:
4649                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4650                                 __func__, __LINE__);
4651                         break;
4652                 }
4653
4654                 break;
4655         case DCMD_SUCCESS:
4656                 tgtid_count = le32_to_cpu(ci->count);
4657
4658                 if ((tgtid_count > (instance->fw_supported_vd_count)))
4659                         break;
4660
4661                 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4662                 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4663                         ids = ci->targetId[ld_index];
4664                         instance->ld_ids[ids] = ci->targetId[ld_index];
4665                 }
4666
4667                 break;
4668         }
4669
4670         if (ret != DCMD_TIMEOUT)
4671                 megasas_return_cmd(instance, cmd);
4672
4673         return ret;
4674 }
4675
4676 /**
4677  * dcmd.opcode            - MR_DCMD_CTRL_DEVICE_LIST_GET
4678  * dcmd.mbox              - reserved
4679  * dcmd.sge IN            - ptr to return MR_HOST_DEVICE_LIST structure
4680  * Desc:    This DCMD will return the combined device list
4681  * Status:  MFI_STAT_OK - List returned successfully
4682  *          MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4683  *                                 disabled
4684  * @instance:                   Adapter soft state
4685  * @is_probe:                   Driver probe check
4686  * Return:                      0 if DCMD succeeded
4687  *                               non-zero if failed
4688  */
4689 static int
4690 megasas_host_device_list_query(struct megasas_instance *instance,
4691                                bool is_probe)
4692 {
4693         int ret, i, target_id;
4694         struct megasas_cmd *cmd;
4695         struct megasas_dcmd_frame *dcmd;
4696         struct MR_HOST_DEVICE_LIST *ci;
4697         u32 count;
4698         dma_addr_t ci_h;
4699
4700         ci = instance->host_device_list_buf;
4701         ci_h = instance->host_device_list_buf_h;
4702
4703         cmd = megasas_get_cmd(instance);
4704
4705         if (!cmd) {
4706                 dev_warn(&instance->pdev->dev,
4707                          "%s: failed to get cmd\n",
4708                          __func__);
4709                 return -ENOMEM;
4710         }
4711
4712         dcmd = &cmd->frame->dcmd;
4713
4714         memset(ci, 0, sizeof(*ci));
4715         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4716
4717         dcmd->mbox.b[0] = is_probe ? 0 : 1;
4718         dcmd->cmd = MFI_CMD_DCMD;
4719         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4720         dcmd->sge_count = 1;
4721         dcmd->flags = MFI_FRAME_DIR_READ;
4722         dcmd->timeout = 0;
4723         dcmd->pad_0 = 0;
4724         dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4725         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4726
4727         megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4728
4729         if (!instance->mask_interrupts) {
4730                 ret = megasas_issue_blocked_cmd(instance, cmd,
4731                                                 MFI_IO_TIMEOUT_SECS);
4732         } else {
4733                 ret = megasas_issue_polled(instance, cmd);
4734                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4735         }
4736
4737         switch (ret) {
4738         case DCMD_SUCCESS:
4739                 /* Fill the internal pd_list and ld_ids array based on
4740                  * targetIds returned by FW
4741                  */
4742                 count = le32_to_cpu(ci->count);
4743
4744                 memset(instance->local_pd_list, 0,
4745                        MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4746                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4747                 for (i = 0; i < count; i++) {
4748                         target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4749                         if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4750                                 instance->local_pd_list[target_id].tid = target_id;
4751                                 instance->local_pd_list[target_id].driveType =
4752                                                 ci->host_device_list[i].scsi_type;
4753                                 instance->local_pd_list[target_id].driveState =
4754                                                 MR_PD_STATE_SYSTEM;
4755                         } else {
4756                                 instance->ld_ids[target_id] = target_id;
4757                         }
4758                 }
4759
4760                 memcpy(instance->pd_list, instance->local_pd_list,
4761                        sizeof(instance->pd_list));
4762                 break;
4763
4764         case DCMD_TIMEOUT:
4765                 switch (dcmd_timeout_ocr_possible(instance)) {
4766                 case INITIATE_OCR:
4767                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4768                         megasas_reset_fusion(instance->host,
4769                                 MFI_IO_TIMEOUT_OCR);
4770                         break;
4771                 case KILL_ADAPTER:
4772                         megaraid_sas_kill_hba(instance);
4773                         break;
4774                 case IGNORE_TIMEOUT:
4775                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4776                                  __func__, __LINE__);
4777                         break;
4778                 }
4779                 break;
4780         case DCMD_FAILED:
4781                 dev_err(&instance->pdev->dev,
4782                         "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4783                         __func__);
4784                 break;
4785         }
4786
4787         if (ret != DCMD_TIMEOUT)
4788                 megasas_return_cmd(instance, cmd);
4789
4790         return ret;
4791 }
4792
4793 /*
4794  * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4795  * instance                      : Controller's instance
4796 */
4797 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4798 {
4799         struct fusion_context *fusion;
4800         u32 ventura_map_sz = 0;
4801
4802         fusion = instance->ctrl_context;
4803         /* For MFI based controllers return dummy success */
4804         if (!fusion)
4805                 return;
4806
4807         instance->supportmax256vd =
4808                 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
4809         /* Below is additional check to address future FW enhancement */
4810         if (instance->ctrl_info_buf->max_lds > 64)
4811                 instance->supportmax256vd = 1;
4812
4813         instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4814                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4815         instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4816                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4817         if (instance->supportmax256vd) {
4818                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4819                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4820         } else {
4821                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4822                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4823         }
4824
4825         dev_info(&instance->pdev->dev,
4826                 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4827                 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4828                 instance->ctrl_info_buf->max_lds);
4829
4830         if (instance->max_raid_mapsize) {
4831                 ventura_map_sz = instance->max_raid_mapsize *
4832                                                 MR_MIN_MAP_SIZE; /* 64k */
4833                 fusion->current_map_sz = ventura_map_sz;
4834                 fusion->max_map_sz = ventura_map_sz;
4835         } else {
4836                 fusion->old_map_sz =  sizeof(struct MR_FW_RAID_MAP) +
4837                                         (sizeof(struct MR_LD_SPAN_MAP) *
4838                                         (instance->fw_supported_vd_count - 1));
4839                 fusion->new_map_sz =  sizeof(struct MR_FW_RAID_MAP_EXT);
4840
4841                 fusion->max_map_sz =
4842                         max(fusion->old_map_sz, fusion->new_map_sz);
4843
4844                 if (instance->supportmax256vd)
4845                         fusion->current_map_sz = fusion->new_map_sz;
4846                 else
4847                         fusion->current_map_sz = fusion->old_map_sz;
4848         }
4849         /* irrespective of FW raid maps, driver raid map is constant */
4850         fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
4851 }
4852
4853 /*
4854  * dcmd.opcode                - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4855  * dcmd.hdr.length            - number of bytes to read
4856  * dcmd.sge                   - Ptr to MR_SNAPDUMP_PROPERTIES
4857  * Desc:                         Fill in snapdump properties
4858  * Status:                       MFI_STAT_OK- Command successful
4859  */
4860 void megasas_get_snapdump_properties(struct megasas_instance *instance)
4861 {
4862         int ret = 0;
4863         struct megasas_cmd *cmd;
4864         struct megasas_dcmd_frame *dcmd;
4865         struct MR_SNAPDUMP_PROPERTIES *ci;
4866         dma_addr_t ci_h = 0;
4867
4868         ci = instance->snapdump_prop;
4869         ci_h = instance->snapdump_prop_h;
4870
4871         if (!ci)
4872                 return;
4873
4874         cmd = megasas_get_cmd(instance);
4875
4876         if (!cmd) {
4877                 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4878                 return;
4879         }
4880
4881         dcmd = &cmd->frame->dcmd;
4882
4883         memset(ci, 0, sizeof(*ci));
4884         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4885
4886         dcmd->cmd = MFI_CMD_DCMD;
4887         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4888         dcmd->sge_count = 1;
4889         dcmd->flags = MFI_FRAME_DIR_READ;
4890         dcmd->timeout = 0;
4891         dcmd->pad_0 = 0;
4892         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
4893         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
4894
4895         megasas_set_dma_settings(instance, dcmd, ci_h,
4896                                  sizeof(struct MR_SNAPDUMP_PROPERTIES));
4897
4898         if (!instance->mask_interrupts) {
4899                 ret = megasas_issue_blocked_cmd(instance, cmd,
4900                                                 MFI_IO_TIMEOUT_SECS);
4901         } else {
4902                 ret = megasas_issue_polled(instance, cmd);
4903                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4904         }
4905
4906         switch (ret) {
4907         case DCMD_SUCCESS:
4908                 instance->snapdump_wait_time =
4909                         min_t(u8, ci->trigger_min_num_sec_before_ocr,
4910                                 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
4911                 break;
4912
4913         case DCMD_TIMEOUT:
4914                 switch (dcmd_timeout_ocr_possible(instance)) {
4915                 case INITIATE_OCR:
4916                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4917                         megasas_reset_fusion(instance->host,
4918                                 MFI_IO_TIMEOUT_OCR);
4919                         break;
4920                 case KILL_ADAPTER:
4921                         megaraid_sas_kill_hba(instance);
4922                         break;
4923                 case IGNORE_TIMEOUT:
4924                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4925                                 __func__, __LINE__);
4926                         break;
4927                 }
4928         }
4929
4930         if (ret != DCMD_TIMEOUT)
4931                 megasas_return_cmd(instance, cmd);
4932 }
4933
4934 /**
4935  * megasas_get_controller_info -        Returns FW's controller structure
4936  * @instance:                           Adapter soft state
4937  *
4938  * Issues an internal command (DCMD) to get the FW's controller structure.
4939  * This information is mainly used to find out the maximum IO transfer per
4940  * command supported by the FW.
4941  */
4942 int
4943 megasas_get_ctrl_info(struct megasas_instance *instance)
4944 {
4945         int ret = 0;
4946         struct megasas_cmd *cmd;
4947         struct megasas_dcmd_frame *dcmd;
4948         struct megasas_ctrl_info *ci;
4949         dma_addr_t ci_h = 0;
4950
4951         ci = instance->ctrl_info_buf;
4952         ci_h = instance->ctrl_info_buf_h;
4953
4954         cmd = megasas_get_cmd(instance);
4955
4956         if (!cmd) {
4957                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4958                 return -ENOMEM;
4959         }
4960
4961         dcmd = &cmd->frame->dcmd;
4962
4963         memset(ci, 0, sizeof(*ci));
4964         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4965
4966         dcmd->cmd = MFI_CMD_DCMD;
4967         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4968         dcmd->sge_count = 1;
4969         dcmd->flags = MFI_FRAME_DIR_READ;
4970         dcmd->timeout = 0;
4971         dcmd->pad_0 = 0;
4972         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4973         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4974         dcmd->mbox.b[0] = 1;
4975
4976         megasas_set_dma_settings(instance, dcmd, ci_h,
4977                                  sizeof(struct megasas_ctrl_info));
4978
4979         if ((instance->adapter_type != MFI_SERIES) &&
4980             !instance->mask_interrupts) {
4981                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4982         } else {
4983                 ret = megasas_issue_polled(instance, cmd);
4984                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4985         }
4986
4987         switch (ret) {
4988         case DCMD_SUCCESS:
4989                 /* Save required controller information in
4990                  * CPU endianness format.
4991                  */
4992                 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
4993                 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
4994                 le32_to_cpus((u32 *)&ci->adapterOperations2);
4995                 le32_to_cpus((u32 *)&ci->adapterOperations3);
4996                 le16_to_cpus((u16 *)&ci->adapter_operations4);
4997
4998                 /* Update the latest Ext VD info.
4999                  * From Init path, store current firmware details.
5000                  * From OCR path, detect any firmware properties changes.
5001                  * in case of Firmware upgrade without system reboot.
5002                  */
5003                 megasas_update_ext_vd_details(instance);
5004                 instance->use_seqnum_jbod_fp =
5005                         ci->adapterOperations3.useSeqNumJbodFP;
5006                 instance->support_morethan256jbod =
5007                         ci->adapter_operations4.support_pd_map_target_id;
5008                 instance->support_nvme_passthru =
5009                         ci->adapter_operations4.support_nvme_passthru;
5010                 instance->task_abort_tmo = ci->TaskAbortTO;
5011                 instance->max_reset_tmo = ci->MaxResetTO;
5012
5013                 /*Check whether controller is iMR or MR */
5014                 instance->is_imr = (ci->memory_size ? 0 : 1);
5015
5016                 instance->snapdump_wait_time =
5017                         (ci->properties.on_off_properties2.enable_snap_dump ?
5018                          MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5019
5020                 instance->enable_fw_dev_list =
5021                         ci->properties.on_off_properties2.enable_fw_dev_list;
5022
5023                 dev_info(&instance->pdev->dev,
5024                         "controller type\t: %s(%dMB)\n",
5025                         instance->is_imr ? "iMR" : "MR",
5026                         le16_to_cpu(ci->memory_size));
5027
5028                 instance->disableOnlineCtrlReset =
5029                         ci->properties.OnOffProperties.disableOnlineCtrlReset;
5030                 instance->secure_jbod_support =
5031                         ci->adapterOperations3.supportSecurityonJBOD;
5032                 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5033                         instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5034                 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5035                         instance->secure_jbod_support ? "Yes" : "No");
5036                 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5037                          instance->support_nvme_passthru ? "Yes" : "No");
5038                 dev_info(&instance->pdev->dev,
5039                          "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5040                          instance->task_abort_tmo, instance->max_reset_tmo);
5041
5042                 break;
5043
5044         case DCMD_TIMEOUT:
5045                 switch (dcmd_timeout_ocr_possible(instance)) {
5046                 case INITIATE_OCR:
5047                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5048                         megasas_reset_fusion(instance->host,
5049                                 MFI_IO_TIMEOUT_OCR);
5050                         break;
5051                 case KILL_ADAPTER:
5052                         megaraid_sas_kill_hba(instance);
5053                         break;
5054                 case IGNORE_TIMEOUT:
5055                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5056                                 __func__, __LINE__);
5057                         break;
5058                 }
5059                 break;
5060         case DCMD_FAILED:
5061                 megaraid_sas_kill_hba(instance);
5062                 break;
5063
5064         }
5065
5066         if (ret != DCMD_TIMEOUT)
5067                 megasas_return_cmd(instance, cmd);
5068
5069         return ret;
5070 }
5071
5072 /*
5073  * megasas_set_crash_dump_params -      Sends address of crash dump DMA buffer
5074  *                                      to firmware
5075  *
5076  * @instance:                           Adapter soft state
5077  * @crash_buf_state             -       tell FW to turn ON/OFF crash dump feature
5078                                         MR_CRASH_BUF_TURN_OFF = 0
5079                                         MR_CRASH_BUF_TURN_ON = 1
5080  * @return 0 on success non-zero on failure.
5081  * Issues an internal command (DCMD) to set parameters for crash dump feature.
5082  * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5083  * that driver supports crash dump feature. This DCMD will be sent only if
5084  * crash dump feature is supported by the FW.
5085  *
5086  */
5087 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5088         u8 crash_buf_state)
5089 {
5090         int ret = 0;
5091         struct megasas_cmd *cmd;
5092         struct megasas_dcmd_frame *dcmd;
5093
5094         cmd = megasas_get_cmd(instance);
5095
5096         if (!cmd) {
5097                 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5098                 return -ENOMEM;
5099         }
5100
5101
5102         dcmd = &cmd->frame->dcmd;
5103
5104         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5105         dcmd->mbox.b[0] = crash_buf_state;
5106         dcmd->cmd = MFI_CMD_DCMD;
5107         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5108         dcmd->sge_count = 1;
5109         dcmd->flags = MFI_FRAME_DIR_NONE;
5110         dcmd->timeout = 0;
5111         dcmd->pad_0 = 0;
5112         dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5113         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5114
5115         megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5116                                  CRASH_DMA_BUF_SIZE);
5117
5118         if ((instance->adapter_type != MFI_SERIES) &&
5119             !instance->mask_interrupts)
5120                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5121         else
5122                 ret = megasas_issue_polled(instance, cmd);
5123
5124         if (ret == DCMD_TIMEOUT) {
5125                 switch (dcmd_timeout_ocr_possible(instance)) {
5126                 case INITIATE_OCR:
5127                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5128                         megasas_reset_fusion(instance->host,
5129                                         MFI_IO_TIMEOUT_OCR);
5130                         break;
5131                 case KILL_ADAPTER:
5132                         megaraid_sas_kill_hba(instance);
5133                         break;
5134                 case IGNORE_TIMEOUT:
5135                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5136                                 __func__, __LINE__);
5137                         break;
5138                 }
5139         } else
5140                 megasas_return_cmd(instance, cmd);
5141
5142         return ret;
5143 }
5144
5145 /**
5146  * megasas_issue_init_mfi -     Initializes the FW
5147  * @instance:           Adapter soft state
5148  *
5149  * Issues the INIT MFI cmd
5150  */
5151 static int
5152 megasas_issue_init_mfi(struct megasas_instance *instance)
5153 {
5154         __le32 context;
5155         struct megasas_cmd *cmd;
5156         struct megasas_init_frame *init_frame;
5157         struct megasas_init_queue_info *initq_info;
5158         dma_addr_t init_frame_h;
5159         dma_addr_t initq_info_h;
5160
5161         /*
5162          * Prepare a init frame. Note the init frame points to queue info
5163          * structure. Each frame has SGL allocated after first 64 bytes. For
5164          * this frame - since we don't need any SGL - we use SGL's space as
5165          * queue info structure
5166          *
5167          * We will not get a NULL command below. We just created the pool.
5168          */
5169         cmd = megasas_get_cmd(instance);
5170
5171         init_frame = (struct megasas_init_frame *)cmd->frame;
5172         initq_info = (struct megasas_init_queue_info *)
5173                 ((unsigned long)init_frame + 64);
5174
5175         init_frame_h = cmd->frame_phys_addr;
5176         initq_info_h = init_frame_h + 64;
5177
5178         context = init_frame->context;
5179         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5180         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5181         init_frame->context = context;
5182
5183         initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5184         initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5185
5186         initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5187         initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5188
5189         init_frame->cmd = MFI_CMD_INIT;
5190         init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5191         init_frame->queue_info_new_phys_addr_lo =
5192                 cpu_to_le32(lower_32_bits(initq_info_h));
5193         init_frame->queue_info_new_phys_addr_hi =
5194                 cpu_to_le32(upper_32_bits(initq_info_h));
5195
5196         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5197
5198         /*
5199          * disable the intr before firing the init frame to FW
5200          */
5201         instance->instancet->disable_intr(instance);
5202
5203         /*
5204          * Issue the init frame in polled mode
5205          */
5206
5207         if (megasas_issue_polled(instance, cmd)) {
5208                 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5209                 megasas_return_cmd(instance, cmd);
5210                 goto fail_fw_init;
5211         }
5212
5213         megasas_return_cmd(instance, cmd);
5214
5215         return 0;
5216
5217 fail_fw_init:
5218         return -EINVAL;
5219 }
5220
5221 static u32
5222 megasas_init_adapter_mfi(struct megasas_instance *instance)
5223 {
5224         u32 context_sz;
5225         u32 reply_q_sz;
5226
5227         /*
5228          * Get various operational parameters from status register
5229          */
5230         instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5231         /*
5232          * Reduce the max supported cmds by 1. This is to ensure that the
5233          * reply_q_sz (1 more than the max cmd that driver may send)
5234          * does not exceed max cmds that the FW can support
5235          */
5236         instance->max_fw_cmds = instance->max_fw_cmds-1;
5237         instance->max_mfi_cmds = instance->max_fw_cmds;
5238         instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5239                                         0x10;
5240         /*
5241          * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5242          * are reserved for IOCTL + driver's internal DCMDs.
5243          */
5244         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5245                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5246                 instance->max_scsi_cmds = (instance->max_fw_cmds -
5247                         MEGASAS_SKINNY_INT_CMDS);
5248                 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5249         } else {
5250                 instance->max_scsi_cmds = (instance->max_fw_cmds -
5251                         MEGASAS_INT_CMDS);
5252                 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5253         }
5254
5255         instance->cur_can_queue = instance->max_scsi_cmds;
5256         /*
5257          * Create a pool of commands
5258          */
5259         if (megasas_alloc_cmds(instance))
5260                 goto fail_alloc_cmds;
5261
5262         /*
5263          * Allocate memory for reply queue. Length of reply queue should
5264          * be _one_ more than the maximum commands handled by the firmware.
5265          *
5266          * Note: When FW completes commands, it places corresponding contex
5267          * values in this circular reply queue. This circular queue is a fairly
5268          * typical producer-consumer queue. FW is the producer (of completed
5269          * commands) and the driver is the consumer.
5270          */
5271         context_sz = sizeof(u32);
5272         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5273
5274         instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5275                         reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5276
5277         if (!instance->reply_queue) {
5278                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5279                 goto fail_reply_queue;
5280         }
5281
5282         if (megasas_issue_init_mfi(instance))
5283                 goto fail_fw_init;
5284
5285         if (megasas_get_ctrl_info(instance)) {
5286                 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5287                         "Fail from %s %d\n", instance->unique_id,
5288                         __func__, __LINE__);
5289                 goto fail_fw_init;
5290         }
5291
5292         instance->fw_support_ieee = 0;
5293         instance->fw_support_ieee =
5294                 (instance->instancet->read_fw_status_reg(instance) &
5295                 0x04000000);
5296
5297         dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5298                         instance->fw_support_ieee);
5299
5300         if (instance->fw_support_ieee)
5301                 instance->flag_ieee = 1;
5302
5303         return 0;
5304
5305 fail_fw_init:
5306
5307         dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5308                             instance->reply_queue, instance->reply_queue_h);
5309 fail_reply_queue:
5310         megasas_free_cmds(instance);
5311
5312 fail_alloc_cmds:
5313         return 1;
5314 }
5315
5316 static
5317 void megasas_setup_irq_poll(struct megasas_instance *instance)
5318 {
5319         struct megasas_irq_context *irq_ctx;
5320         u32 count, i;
5321
5322         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5323
5324         /* Initialize IRQ poll */
5325         for (i = 0; i < count; i++) {
5326                 irq_ctx = &instance->irq_context[i];
5327                 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5328                 irq_ctx->irq_poll_scheduled = false;
5329                 irq_poll_init(&irq_ctx->irqpoll,
5330                               instance->threshold_reply_count,
5331                               megasas_irqpoll);
5332         }
5333 }
5334
5335 /*
5336  * megasas_setup_irqs_ioapic -          register legacy interrupts.
5337  * @instance:                           Adapter soft state
5338  *
5339  * Do not enable interrupt, only setup ISRs.
5340  *
5341  * Return 0 on success.
5342  */
5343 static int
5344 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5345 {
5346         struct pci_dev *pdev;
5347
5348         pdev = instance->pdev;
5349         instance->irq_context[0].instance = instance;
5350         instance->irq_context[0].MSIxIndex = 0;
5351         if (request_irq(pci_irq_vector(pdev, 0),
5352                         instance->instancet->service_isr, IRQF_SHARED,
5353                         "megasas", &instance->irq_context[0])) {
5354                 dev_err(&instance->pdev->dev,
5355                                 "Failed to register IRQ from %s %d\n",
5356                                 __func__, __LINE__);
5357                 return -1;
5358         }
5359         return 0;
5360 }
5361
5362 /**
5363  * megasas_setup_irqs_msix -            register MSI-x interrupts.
5364  * @instance:                           Adapter soft state
5365  * @is_probe:                           Driver probe check
5366  *
5367  * Do not enable interrupt, only setup ISRs.
5368  *
5369  * Return 0 on success.
5370  */
5371 static int
5372 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5373 {
5374         int i, j;
5375         struct pci_dev *pdev;
5376
5377         pdev = instance->pdev;
5378
5379         /* Try MSI-x */
5380         for (i = 0; i < instance->msix_vectors; i++) {
5381                 instance->irq_context[i].instance = instance;
5382                 instance->irq_context[i].MSIxIndex = i;
5383                 if (request_irq(pci_irq_vector(pdev, i),
5384                         instance->instancet->service_isr, 0, "megasas",
5385                         &instance->irq_context[i])) {
5386                         dev_err(&instance->pdev->dev,
5387                                 "Failed to register IRQ for vector %d.\n", i);
5388                         for (j = 0; j < i; j++)
5389                                 free_irq(pci_irq_vector(pdev, j),
5390                                          &instance->irq_context[j]);
5391                         /* Retry irq register for IO_APIC*/
5392                         instance->msix_vectors = 0;
5393                         instance->msix_load_balance = false;
5394                         if (is_probe) {
5395                                 pci_free_irq_vectors(instance->pdev);
5396                                 return megasas_setup_irqs_ioapic(instance);
5397                         } else {
5398                                 return -1;
5399                         }
5400                 }
5401         }
5402
5403         return 0;
5404 }
5405
5406 /*
5407  * megasas_destroy_irqs-                unregister interrupts.
5408  * @instance:                           Adapter soft state
5409  * return:                              void
5410  */
5411 static void
5412 megasas_destroy_irqs(struct megasas_instance *instance) {
5413
5414         int i;
5415         int count;
5416         struct megasas_irq_context *irq_ctx;
5417
5418         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5419         if (instance->adapter_type != MFI_SERIES) {
5420                 for (i = 0; i < count; i++) {
5421                         irq_ctx = &instance->irq_context[i];
5422                         irq_poll_disable(&irq_ctx->irqpoll);
5423                 }
5424         }
5425
5426         if (instance->msix_vectors)
5427                 for (i = 0; i < instance->msix_vectors; i++) {
5428                         free_irq(pci_irq_vector(instance->pdev, i),
5429                                  &instance->irq_context[i]);
5430                 }
5431         else
5432                 free_irq(pci_irq_vector(instance->pdev, 0),
5433                          &instance->irq_context[0]);
5434 }
5435
5436 /**
5437  * megasas_setup_jbod_map -     setup jbod map for FP seq_number.
5438  * @instance:                           Adapter soft state
5439  * @is_probe:                           Driver probe check
5440  *
5441  * Return 0 on success.
5442  */
5443 void
5444 megasas_setup_jbod_map(struct megasas_instance *instance)
5445 {
5446         int i;
5447         struct fusion_context *fusion = instance->ctrl_context;
5448         u32 pd_seq_map_sz;
5449
5450         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5451                 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5452
5453         if (reset_devices || !fusion ||
5454                 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
5455                 dev_info(&instance->pdev->dev,
5456                         "Jbod map is not supported %s %d\n",
5457                         __func__, __LINE__);
5458                 instance->use_seqnum_jbod_fp = false;
5459                 return;
5460         }
5461
5462         if (fusion->pd_seq_sync[0])
5463                 goto skip_alloc;
5464
5465         for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5466                 fusion->pd_seq_sync[i] = dma_alloc_coherent
5467                         (&instance->pdev->dev, pd_seq_map_sz,
5468                         &fusion->pd_seq_phys[i], GFP_KERNEL);
5469                 if (!fusion->pd_seq_sync[i]) {
5470                         dev_err(&instance->pdev->dev,
5471                                 "Failed to allocate memory from %s %d\n",
5472                                 __func__, __LINE__);
5473                         if (i == 1) {
5474                                 dma_free_coherent(&instance->pdev->dev,
5475                                         pd_seq_map_sz, fusion->pd_seq_sync[0],
5476                                         fusion->pd_seq_phys[0]);
5477                                 fusion->pd_seq_sync[0] = NULL;
5478                         }
5479                         instance->use_seqnum_jbod_fp = false;
5480                         return;
5481                 }
5482         }
5483
5484 skip_alloc:
5485         if (!megasas_sync_pd_seq_num(instance, false) &&
5486                 !megasas_sync_pd_seq_num(instance, true))
5487                 instance->use_seqnum_jbod_fp = true;
5488         else
5489                 instance->use_seqnum_jbod_fp = false;
5490 }
5491
5492 static void megasas_setup_reply_map(struct megasas_instance *instance)
5493 {
5494         const struct cpumask *mask;
5495         unsigned int queue, cpu;
5496
5497         for (queue = 0; queue < instance->msix_vectors; queue++) {
5498                 mask = pci_irq_get_affinity(instance->pdev, queue);
5499                 if (!mask)
5500                         goto fallback;
5501
5502                 for_each_cpu(cpu, mask)
5503                         instance->reply_map[cpu] = queue;
5504         }
5505         return;
5506
5507 fallback:
5508         for_each_possible_cpu(cpu)
5509                 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5510 }
5511
5512 /**
5513  * megasas_get_device_list -    Get the PD and LD device list from FW.
5514  * @instance:                   Adapter soft state
5515  * @return:                     Success or failure
5516  *
5517  * Issue DCMDs to Firmware to get the PD and LD list.
5518  * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5519  * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5520  */
5521 static
5522 int megasas_get_device_list(struct megasas_instance *instance)
5523 {
5524         memset(instance->pd_list, 0,
5525                (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5526         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5527
5528         if (instance->enable_fw_dev_list) {
5529                 if (megasas_host_device_list_query(instance, true))
5530                         return FAILED;
5531         } else {
5532                 if (megasas_get_pd_list(instance) < 0) {
5533                         dev_err(&instance->pdev->dev, "failed to get PD list\n");
5534                         return FAILED;
5535                 }
5536
5537                 if (megasas_ld_list_query(instance,
5538                                           MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5539                         dev_err(&instance->pdev->dev, "failed to get LD list\n");
5540                         return FAILED;
5541                 }
5542         }
5543
5544         return SUCCESS;
5545 }
5546 /**
5547  * megasas_init_fw -    Initializes the FW
5548  * @instance:           Adapter soft state
5549  *
5550  * This is the main function for initializing firmware
5551  */
5552
5553 static int megasas_init_fw(struct megasas_instance *instance)
5554 {
5555         u32 max_sectors_1;
5556         u32 max_sectors_2, tmp_sectors, msix_enable;
5557         u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5558         resource_size_t base_addr;
5559         struct megasas_ctrl_info *ctrl_info = NULL;
5560         unsigned long bar_list;
5561         int i, j, loop, fw_msix_count = 0;
5562         struct IOV_111 *iovPtr;
5563         struct fusion_context *fusion;
5564
5565         fusion = instance->ctrl_context;
5566
5567         /* Find first memory bar */
5568         bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5569         instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5570         if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5571                                          "megasas: LSI")) {
5572                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5573                 return -EBUSY;
5574         }
5575
5576         base_addr = pci_resource_start(instance->pdev, instance->bar);
5577         instance->reg_set = ioremap_nocache(base_addr, 8192);
5578
5579         if (!instance->reg_set) {
5580                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5581                 goto fail_ioremap;
5582         }
5583
5584         if (instance->adapter_type != MFI_SERIES)
5585                 instance->instancet = &megasas_instance_template_fusion;
5586         else {
5587                 switch (instance->pdev->device) {
5588                 case PCI_DEVICE_ID_LSI_SAS1078R:
5589                 case PCI_DEVICE_ID_LSI_SAS1078DE:
5590                         instance->instancet = &megasas_instance_template_ppc;
5591                         break;
5592                 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5593                 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5594                         instance->instancet = &megasas_instance_template_gen2;
5595                         break;
5596                 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5597                 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5598                         instance->instancet = &megasas_instance_template_skinny;
5599                         break;
5600                 case PCI_DEVICE_ID_LSI_SAS1064R:
5601                 case PCI_DEVICE_ID_DELL_PERC5:
5602                 default:
5603                         instance->instancet = &megasas_instance_template_xscale;
5604                         instance->pd_list_not_supported = 1;
5605                         break;
5606                 }
5607         }
5608
5609         if (megasas_transition_to_ready(instance, 0)) {
5610                 dev_info(&instance->pdev->dev,
5611                          "Failed to transition controller to ready from %s!\n",
5612                          __func__);
5613                 if (instance->adapter_type != MFI_SERIES) {
5614                         status_reg = instance->instancet->read_fw_status_reg(
5615                                         instance);
5616                         if (status_reg & MFI_RESET_ADAPTER) {
5617                                 if (megasas_adp_reset_wait_for_ready
5618                                         (instance, true, 0) == FAILED)
5619                                         goto fail_ready_state;
5620                         } else {
5621                                 goto fail_ready_state;
5622                         }
5623                 } else {
5624                         atomic_set(&instance->fw_reset_no_pci_access, 1);
5625                         instance->instancet->adp_reset
5626                                 (instance, instance->reg_set);
5627                         atomic_set(&instance->fw_reset_no_pci_access, 0);
5628
5629                         /*waiting for about 30 second before retry*/
5630                         ssleep(30);
5631
5632                         if (megasas_transition_to_ready(instance, 0))
5633                                 goto fail_ready_state;
5634                 }
5635
5636                 dev_info(&instance->pdev->dev,
5637                          "FW restarted successfully from %s!\n",
5638                          __func__);
5639         }
5640
5641         megasas_init_ctrl_params(instance);
5642
5643         if (megasas_set_dma_mask(instance))
5644                 goto fail_ready_state;
5645
5646         if (megasas_alloc_ctrl_mem(instance))
5647                 goto fail_alloc_dma_buf;
5648
5649         if (megasas_alloc_ctrl_dma_buffers(instance))
5650                 goto fail_alloc_dma_buf;
5651
5652         fusion = instance->ctrl_context;
5653
5654         if (instance->adapter_type >= VENTURA_SERIES) {
5655                 scratch_pad_2 =
5656                         megasas_readl(instance,
5657                                       &instance->reg_set->outbound_scratch_pad_2);
5658                 instance->max_raid_mapsize = ((scratch_pad_2 >>
5659                         MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5660                         MR_MAX_RAID_MAP_SIZE_MASK);
5661         }
5662
5663         /* Check if MSI-X is supported while in ready state */
5664         msix_enable = (instance->instancet->read_fw_status_reg(instance) &
5665                        0x4000000) >> 0x1a;
5666         if (msix_enable && !msix_disable) {
5667                 int irq_flags = PCI_IRQ_MSIX;
5668
5669                 scratch_pad_1 = megasas_readl
5670                         (instance, &instance->reg_set->outbound_scratch_pad_1);
5671                 /* Check max MSI-X vectors */
5672                 if (fusion) {
5673                         if (instance->adapter_type == THUNDERBOLT_SERIES) {
5674                                 /* Thunderbolt Series*/
5675                                 instance->msix_vectors = (scratch_pad_1
5676                                         & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5677                                 fw_msix_count = instance->msix_vectors;
5678                         } else {
5679                                 instance->msix_vectors = ((scratch_pad_1
5680                                         & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5681                                         >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5682
5683                                 /*
5684                                  * For Invader series, > 8 MSI-x vectors
5685                                  * supported by FW/HW implies combined
5686                                  * reply queue mode is enabled.
5687                                  * For Ventura series, > 16 MSI-x vectors
5688                                  * supported by FW/HW implies combined
5689                                  * reply queue mode is enabled.
5690                                  */
5691                                 switch (instance->adapter_type) {
5692                                 case INVADER_SERIES:
5693                                         if (instance->msix_vectors > 8)
5694                                                 instance->msix_combined = true;
5695                                         break;
5696                                 case AERO_SERIES:
5697                                 case VENTURA_SERIES:
5698                                         if (instance->msix_vectors > 16)
5699                                                 instance->msix_combined = true;
5700                                         break;
5701                                 }
5702
5703                                 if (rdpq_enable)
5704                                         instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
5705                                                                 1 : 0;
5706
5707                                 if (!instance->msix_combined) {
5708                                         instance->msix_load_balance = true;
5709                                         instance->smp_affinity_enable = false;
5710                                 }
5711
5712                                 fw_msix_count = instance->msix_vectors;
5713                                 /* Save 1-15 reply post index address to local memory
5714                                  * Index 0 is already saved from reg offset
5715                                  * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5716                                  */
5717                                 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5718                                         instance->reply_post_host_index_addr[loop] =
5719                                                 (u32 __iomem *)
5720                                                 ((u8 __iomem *)instance->reg_set +
5721                                                 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5722                                                 + (loop * 0x10));
5723                                 }
5724                         }
5725                         if (msix_vectors)
5726                                 instance->msix_vectors = min(msix_vectors,
5727                                         instance->msix_vectors);
5728                 } else /* MFI adapters */
5729                         instance->msix_vectors = 1;
5730
5731                 /* Don't bother allocating more MSI-X vectors than cpus */
5732                 instance->msix_vectors = min(instance->msix_vectors,
5733                                              (unsigned int)num_online_cpus());
5734                 if (instance->smp_affinity_enable)
5735                         irq_flags |= PCI_IRQ_AFFINITY;
5736                 i = pci_alloc_irq_vectors(instance->pdev, 1,
5737                                           instance->msix_vectors, irq_flags);
5738                 if (i > 0) {
5739                         instance->msix_vectors = i;
5740                 } else {
5741                         instance->msix_vectors = 0;
5742                         instance->msix_load_balance = false;
5743                 }
5744         }
5745         /*
5746          * MSI-X host index 0 is common for all adapter.
5747          * It is used for all MPT based Adapters.
5748          */
5749         if (instance->msix_combined) {
5750                 instance->reply_post_host_index_addr[0] =
5751                                 (u32 *)((u8 *)instance->reg_set +
5752                                 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5753         } else {
5754                 instance->reply_post_host_index_addr[0] =
5755                         (u32 *)((u8 *)instance->reg_set +
5756                         MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5757         }
5758
5759         if (!instance->msix_vectors) {
5760                 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5761                 if (i < 0)
5762                         goto fail_init_adapter;
5763         }
5764
5765         megasas_setup_reply_map(instance);
5766
5767         dev_info(&instance->pdev->dev,
5768                 "firmware supports msix\t: (%d)", fw_msix_count);
5769         dev_info(&instance->pdev->dev,
5770                 "current msix/online cpus\t: (%d/%d)\n",
5771                 instance->msix_vectors, (unsigned int)num_online_cpus());
5772         dev_info(&instance->pdev->dev,
5773                 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5774
5775         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5776                 (unsigned long)instance);
5777
5778         /*
5779          * Below are default value for legacy Firmware.
5780          * non-fusion based controllers
5781          */
5782         instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5783         instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5784         /* Get operational params, sge flags, send init cmd to controller */
5785         if (instance->instancet->init_adapter(instance))
5786                 goto fail_init_adapter;
5787
5788         if (instance->adapter_type >= VENTURA_SERIES) {
5789                 scratch_pad_3 =
5790                         megasas_readl(instance,
5791                                       &instance->reg_set->outbound_scratch_pad_3);
5792                 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
5793                         MR_DEFAULT_NVME_PAGE_SHIFT)
5794                         instance->nvme_page_size =
5795                                 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
5796
5797                 dev_info(&instance->pdev->dev,
5798                          "NVME page size\t: (%d)\n", instance->nvme_page_size);
5799         }
5800
5801         if (instance->msix_vectors ?
5802                 megasas_setup_irqs_msix(instance, 1) :
5803                 megasas_setup_irqs_ioapic(instance))
5804                 goto fail_init_adapter;
5805
5806         if (instance->adapter_type != MFI_SERIES)
5807                 megasas_setup_irq_poll(instance);
5808
5809         instance->instancet->enable_intr(instance);
5810
5811         dev_info(&instance->pdev->dev, "INIT adapter done\n");
5812
5813         megasas_setup_jbod_map(instance);
5814
5815         if (megasas_get_device_list(instance) != SUCCESS) {
5816                 dev_err(&instance->pdev->dev,
5817                         "%s: megasas_get_device_list failed\n",
5818                         __func__);
5819                 goto fail_get_ld_pd_list;
5820         }
5821
5822         /* stream detection initialization */
5823         if (instance->adapter_type >= VENTURA_SERIES) {
5824                 fusion->stream_detect_by_ld =
5825                         kcalloc(MAX_LOGICAL_DRIVES_EXT,
5826                                 sizeof(struct LD_STREAM_DETECT *),
5827                                 GFP_KERNEL);
5828                 if (!fusion->stream_detect_by_ld) {
5829                         dev_err(&instance->pdev->dev,
5830                                 "unable to allocate stream detection for pool of LDs\n");
5831                         goto fail_get_ld_pd_list;
5832                 }
5833                 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5834                         fusion->stream_detect_by_ld[i] =
5835                                 kzalloc(sizeof(struct LD_STREAM_DETECT),
5836                                 GFP_KERNEL);
5837                         if (!fusion->stream_detect_by_ld[i]) {
5838                                 dev_err(&instance->pdev->dev,
5839                                         "unable to allocate stream detect by LD\n ");
5840                                 for (j = 0; j < i; ++j)
5841                                         kfree(fusion->stream_detect_by_ld[j]);
5842                                 kfree(fusion->stream_detect_by_ld);
5843                                 fusion->stream_detect_by_ld = NULL;
5844                                 goto fail_get_ld_pd_list;
5845                         }
5846                         fusion->stream_detect_by_ld[i]->mru_bit_map
5847                                 = MR_STREAM_BITMAP;
5848                 }
5849         }
5850
5851         /*
5852          * Compute the max allowed sectors per IO: The controller info has two
5853          * limits on max sectors. Driver should use the minimum of these two.
5854          *
5855          * 1 << stripe_sz_ops.min = max sectors per strip
5856          *
5857          * Note that older firmwares ( < FW ver 30) didn't report information
5858          * to calculate max_sectors_1. So the number ended up as zero always.
5859          */
5860         tmp_sectors = 0;
5861         ctrl_info = instance->ctrl_info_buf;
5862
5863         max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5864                 le16_to_cpu(ctrl_info->max_strips_per_io);
5865         max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5866
5867         tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5868
5869         instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5870         instance->passive = ctrl_info->cluster.passive;
5871         memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5872         instance->UnevenSpanSupport =
5873                 ctrl_info->adapterOperations2.supportUnevenSpans;
5874         if (instance->UnevenSpanSupport) {
5875                 struct fusion_context *fusion = instance->ctrl_context;
5876                 if (MR_ValidateMapInfo(instance, instance->map_id))
5877                         fusion->fast_path_io = 1;
5878                 else
5879                         fusion->fast_path_io = 0;
5880
5881         }
5882         if (ctrl_info->host_interface.SRIOV) {
5883                 instance->requestorId = ctrl_info->iov.requestorId;
5884                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5885                         if (!ctrl_info->adapterOperations2.activePassive)
5886                             instance->PlasmaFW111 = 1;
5887
5888                         dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5889                             instance->PlasmaFW111 ? "1.11" : "new");
5890
5891                         if (instance->PlasmaFW111) {
5892                             iovPtr = (struct IOV_111 *)
5893                                 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5894                             instance->requestorId = iovPtr->requestorId;
5895                         }
5896                 }
5897                 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5898                         instance->requestorId);
5899         }
5900
5901         instance->crash_dump_fw_support =
5902                 ctrl_info->adapterOperations3.supportCrashDump;
5903         instance->crash_dump_drv_support =
5904                 (instance->crash_dump_fw_support &&
5905                 instance->crash_dump_buf);
5906         if (instance->crash_dump_drv_support)
5907                 megasas_set_crash_dump_params(instance,
5908                         MR_CRASH_BUF_TURN_OFF);
5909
5910         else {
5911                 if (instance->crash_dump_buf)
5912                         dma_free_coherent(&instance->pdev->dev,
5913                                 CRASH_DMA_BUF_SIZE,
5914                                 instance->crash_dump_buf,
5915                                 instance->crash_dump_h);
5916                 instance->crash_dump_buf = NULL;
5917         }
5918
5919         if (instance->snapdump_wait_time) {
5920                 megasas_get_snapdump_properties(instance);
5921                 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
5922                          instance->snapdump_wait_time);
5923         }
5924
5925         dev_info(&instance->pdev->dev,
5926                 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5927                 le16_to_cpu(ctrl_info->pci.vendor_id),
5928                 le16_to_cpu(ctrl_info->pci.device_id),
5929                 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5930                 le16_to_cpu(ctrl_info->pci.sub_device_id));
5931         dev_info(&instance->pdev->dev, "unevenspan support      : %s\n",
5932                 instance->UnevenSpanSupport ? "yes" : "no");
5933         dev_info(&instance->pdev->dev, "firmware crash dump     : %s\n",
5934                 instance->crash_dump_drv_support ? "yes" : "no");
5935         dev_info(&instance->pdev->dev, "jbod sync map           : %s\n",
5936                 instance->use_seqnum_jbod_fp ? "yes" : "no");
5937
5938         instance->max_sectors_per_req = instance->max_num_sge *
5939                                                 SGE_BUFFER_SIZE / 512;
5940         if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5941                 instance->max_sectors_per_req = tmp_sectors;
5942
5943         /* Check for valid throttlequeuedepth module parameter */
5944         if (throttlequeuedepth &&
5945                         throttlequeuedepth <= instance->max_scsi_cmds)
5946                 instance->throttlequeuedepth = throttlequeuedepth;
5947         else
5948                 instance->throttlequeuedepth =
5949                                 MEGASAS_THROTTLE_QUEUE_DEPTH;
5950
5951         if ((resetwaittime < 1) ||
5952             (resetwaittime > MEGASAS_RESET_WAIT_TIME))
5953                 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5954
5955         if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5956                 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5957
5958         /* Launch SR-IOV heartbeat timer */
5959         if (instance->requestorId) {
5960                 if (!megasas_sriov_start_heartbeat(instance, 1)) {
5961                         megasas_start_timer(instance);
5962                 } else {
5963                         instance->skip_heartbeat_timer_del = 1;
5964                         goto fail_get_ld_pd_list;
5965                 }
5966         }
5967
5968         /*
5969          * Create and start watchdog thread which will monitor
5970          * controller state every 1 sec and trigger OCR when
5971          * it enters fault state
5972          */
5973         if (instance->adapter_type != MFI_SERIES)
5974                 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
5975                         goto fail_start_watchdog;
5976
5977         return 0;
5978
5979 fail_start_watchdog:
5980         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5981                 del_timer_sync(&instance->sriov_heartbeat_timer);
5982 fail_get_ld_pd_list:
5983         instance->instancet->disable_intr(instance);
5984         megasas_destroy_irqs(instance);
5985 fail_init_adapter:
5986         if (instance->msix_vectors)
5987                 pci_free_irq_vectors(instance->pdev);
5988         instance->msix_vectors = 0;
5989 fail_alloc_dma_buf:
5990         megasas_free_ctrl_dma_buffers(instance);
5991         megasas_free_ctrl_mem(instance);
5992 fail_ready_state:
5993         iounmap(instance->reg_set);
5994
5995 fail_ioremap:
5996         pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5997
5998         dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5999                 __func__, __LINE__);
6000         return -EINVAL;
6001 }
6002
6003 /**
6004  * megasas_release_mfi -        Reverses the FW initialization
6005  * @instance:                   Adapter soft state
6006  */
6007 static void megasas_release_mfi(struct megasas_instance *instance)
6008 {
6009         u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6010
6011         if (instance->reply_queue)
6012                 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6013                             instance->reply_queue, instance->reply_queue_h);
6014
6015         megasas_free_cmds(instance);
6016
6017         iounmap(instance->reg_set);
6018
6019         pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6020 }
6021
6022 /**
6023  * megasas_get_seq_num -        Gets latest event sequence numbers
6024  * @instance:                   Adapter soft state
6025  * @eli:                        FW event log sequence numbers information
6026  *
6027  * FW maintains a log of all events in a non-volatile area. Upper layers would
6028  * usually find out the latest sequence number of the events, the seq number at
6029  * the boot etc. They would "read" all the events below the latest seq number
6030  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6031  * number), they would subsribe to AEN (asynchronous event notification) and
6032  * wait for the events to happen.
6033  */
6034 static int
6035 megasas_get_seq_num(struct megasas_instance *instance,
6036                     struct megasas_evt_log_info *eli)
6037 {
6038         struct megasas_cmd *cmd;
6039         struct megasas_dcmd_frame *dcmd;
6040         struct megasas_evt_log_info *el_info;
6041         dma_addr_t el_info_h = 0;
6042         int ret;
6043
6044         cmd = megasas_get_cmd(instance);
6045
6046         if (!cmd) {
6047                 return -ENOMEM;
6048         }
6049
6050         dcmd = &cmd->frame->dcmd;
6051         el_info = dma_alloc_coherent(&instance->pdev->dev,
6052                                      sizeof(struct megasas_evt_log_info),
6053                                      &el_info_h, GFP_KERNEL);
6054         if (!el_info) {
6055                 megasas_return_cmd(instance, cmd);
6056                 return -ENOMEM;
6057         }
6058
6059         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6060
6061         dcmd->cmd = MFI_CMD_DCMD;
6062         dcmd->cmd_status = 0x0;
6063         dcmd->sge_count = 1;
6064         dcmd->flags = MFI_FRAME_DIR_READ;
6065         dcmd->timeout = 0;
6066         dcmd->pad_0 = 0;
6067         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6068         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6069
6070         megasas_set_dma_settings(instance, dcmd, el_info_h,
6071                                  sizeof(struct megasas_evt_log_info));
6072
6073         ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6074         if (ret != DCMD_SUCCESS) {
6075                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6076                         __func__, __LINE__);
6077                 goto dcmd_failed;
6078         }
6079
6080         /*
6081          * Copy the data back into callers buffer
6082          */
6083         eli->newest_seq_num = el_info->newest_seq_num;
6084         eli->oldest_seq_num = el_info->oldest_seq_num;
6085         eli->clear_seq_num = el_info->clear_seq_num;
6086         eli->shutdown_seq_num = el_info->shutdown_seq_num;
6087         eli->boot_seq_num = el_info->boot_seq_num;
6088
6089 dcmd_failed:
6090         dma_free_coherent(&instance->pdev->dev,
6091                         sizeof(struct megasas_evt_log_info),
6092                         el_info, el_info_h);
6093
6094         megasas_return_cmd(instance, cmd);
6095
6096         return ret;
6097 }
6098
6099 /**
6100  * megasas_register_aen -       Registers for asynchronous event notification
6101  * @instance:                   Adapter soft state
6102  * @seq_num:                    The starting sequence number
6103  * @class_locale:               Class of the event
6104  *
6105  * This function subscribes for AEN for events beyond the @seq_num. It requests
6106  * to be notified if and only if the event is of type @class_locale
6107  */
6108 static int
6109 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6110                      u32 class_locale_word)
6111 {
6112         int ret_val;
6113         struct megasas_cmd *cmd;
6114         struct megasas_dcmd_frame *dcmd;
6115         union megasas_evt_class_locale curr_aen;
6116         union megasas_evt_class_locale prev_aen;
6117
6118         /*
6119          * If there an AEN pending already (aen_cmd), check if the
6120          * class_locale of that pending AEN is inclusive of the new
6121          * AEN request we currently have. If it is, then we don't have
6122          * to do anything. In other words, whichever events the current
6123          * AEN request is subscribing to, have already been subscribed
6124          * to.
6125          *
6126          * If the old_cmd is _not_ inclusive, then we have to abort
6127          * that command, form a class_locale that is superset of both
6128          * old and current and re-issue to the FW
6129          */
6130
6131         curr_aen.word = class_locale_word;
6132
6133         if (instance->aen_cmd) {
6134
6135                 prev_aen.word =
6136                         le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6137
6138                 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6139                     (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6140                         dev_info(&instance->pdev->dev,
6141                                  "%s %d out of range class %d send by application\n",
6142                                  __func__, __LINE__, curr_aen.members.class);
6143                         return 0;
6144                 }
6145
6146                 /*
6147                  * A class whose enum value is smaller is inclusive of all
6148                  * higher values. If a PROGRESS (= -1) was previously
6149                  * registered, then a new registration requests for higher
6150                  * classes need not be sent to FW. They are automatically
6151                  * included.
6152                  *
6153                  * Locale numbers don't have such hierarchy. They are bitmap
6154                  * values
6155                  */
6156                 if ((prev_aen.members.class <= curr_aen.members.class) &&
6157                     !((prev_aen.members.locale & curr_aen.members.locale) ^
6158                       curr_aen.members.locale)) {
6159                         /*
6160                          * Previously issued event registration includes
6161                          * current request. Nothing to do.
6162                          */
6163                         return 0;
6164                 } else {
6165                         curr_aen.members.locale |= prev_aen.members.locale;
6166
6167                         if (prev_aen.members.class < curr_aen.members.class)
6168                                 curr_aen.members.class = prev_aen.members.class;
6169
6170                         instance->aen_cmd->abort_aen = 1;
6171                         ret_val = megasas_issue_blocked_abort_cmd(instance,
6172                                                                   instance->
6173                                                                   aen_cmd, 30);
6174
6175                         if (ret_val) {
6176                                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6177                                        "previous AEN command\n");
6178                                 return ret_val;
6179                         }
6180                 }
6181         }
6182
6183         cmd = megasas_get_cmd(instance);
6184
6185         if (!cmd)
6186                 return -ENOMEM;
6187
6188         dcmd = &cmd->frame->dcmd;
6189
6190         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6191
6192         /*
6193          * Prepare DCMD for aen registration
6194          */
6195         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6196
6197         dcmd->cmd = MFI_CMD_DCMD;
6198         dcmd->cmd_status = 0x0;
6199         dcmd->sge_count = 1;
6200         dcmd->flags = MFI_FRAME_DIR_READ;
6201         dcmd->timeout = 0;
6202         dcmd->pad_0 = 0;
6203         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6204         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6205         dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6206         instance->last_seq_num = seq_num;
6207         dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6208
6209         megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6210                                  sizeof(struct megasas_evt_detail));
6211
6212         if (instance->aen_cmd != NULL) {
6213                 megasas_return_cmd(instance, cmd);
6214                 return 0;
6215         }
6216
6217         /*
6218          * Store reference to the cmd used to register for AEN. When an
6219          * application wants us to register for AEN, we have to abort this
6220          * cmd and re-register with a new EVENT LOCALE supplied by that app
6221          */
6222         instance->aen_cmd = cmd;
6223
6224         /*
6225          * Issue the aen registration frame
6226          */
6227         instance->instancet->issue_dcmd(instance, cmd);
6228
6229         return 0;
6230 }
6231
6232 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6233  *
6234  * This DCMD will fetch few properties of LD/system PD defined
6235  * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6236  *
6237  * DCMD send by drivers whenever new target is added to the OS.
6238  *
6239  * dcmd.opcode         - MR_DCMD_DEV_GET_TARGET_PROP
6240  * dcmd.mbox.b[0]      - DCMD is to be fired for LD or system PD.
6241  *                       0 = system PD, 1 = LD.
6242  * dcmd.mbox.s[1]      - TargetID for LD/system PD.
6243  * dcmd.sge IN         - Pointer to return MR_TARGET_DEV_PROPERTIES.
6244  *
6245  * @instance:           Adapter soft state
6246  * @sdev:               OS provided scsi device
6247  *
6248  * Returns 0 on success non-zero on failure.
6249  */
6250 int
6251 megasas_get_target_prop(struct megasas_instance *instance,
6252                         struct scsi_device *sdev)
6253 {
6254         int ret;
6255         struct megasas_cmd *cmd;
6256         struct megasas_dcmd_frame *dcmd;
6257         u16 targetId = (sdev->channel % 2) + sdev->id;
6258
6259         cmd = megasas_get_cmd(instance);
6260
6261         if (!cmd) {
6262                 dev_err(&instance->pdev->dev,
6263                         "Failed to get cmd %s\n", __func__);
6264                 return -ENOMEM;
6265         }
6266
6267         dcmd = &cmd->frame->dcmd;
6268
6269         memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6270         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6271         dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6272
6273         dcmd->mbox.s[1] = cpu_to_le16(targetId);
6274         dcmd->cmd = MFI_CMD_DCMD;
6275         dcmd->cmd_status = 0xFF;
6276         dcmd->sge_count = 1;
6277         dcmd->flags = MFI_FRAME_DIR_READ;
6278         dcmd->timeout = 0;
6279         dcmd->pad_0 = 0;
6280         dcmd->data_xfer_len =
6281                 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6282         dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6283
6284         megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6285                                  sizeof(struct MR_TARGET_PROPERTIES));
6286
6287         if ((instance->adapter_type != MFI_SERIES) &&
6288             !instance->mask_interrupts)
6289                 ret = megasas_issue_blocked_cmd(instance,
6290                                                 cmd, MFI_IO_TIMEOUT_SECS);
6291         else
6292                 ret = megasas_issue_polled(instance, cmd);
6293
6294         switch (ret) {
6295         case DCMD_TIMEOUT:
6296                 switch (dcmd_timeout_ocr_possible(instance)) {
6297                 case INITIATE_OCR:
6298                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6299                         megasas_reset_fusion(instance->host,
6300                                              MFI_IO_TIMEOUT_OCR);
6301                         break;
6302                 case KILL_ADAPTER:
6303                         megaraid_sas_kill_hba(instance);
6304                         break;
6305                 case IGNORE_TIMEOUT:
6306                         dev_info(&instance->pdev->dev,
6307                                  "Ignore DCMD timeout: %s %d\n",
6308                                  __func__, __LINE__);
6309                         break;
6310                 }
6311                 break;
6312
6313         default:
6314                 megasas_return_cmd(instance, cmd);
6315         }
6316         if (ret != DCMD_SUCCESS)
6317                 dev_err(&instance->pdev->dev,
6318                         "return from %s %d return value %d\n",
6319                         __func__, __LINE__, ret);
6320
6321         return ret;
6322 }
6323
6324 /**
6325  * megasas_start_aen -  Subscribes to AEN during driver load time
6326  * @instance:           Adapter soft state
6327  */
6328 static int megasas_start_aen(struct megasas_instance *instance)
6329 {
6330         struct megasas_evt_log_info eli;
6331         union megasas_evt_class_locale class_locale;
6332
6333         /*
6334          * Get the latest sequence number from FW
6335          */
6336         memset(&eli, 0, sizeof(eli));
6337
6338         if (megasas_get_seq_num(instance, &eli))
6339                 return -1;
6340
6341         /*
6342          * Register AEN with FW for latest sequence number plus 1
6343          */
6344         class_locale.members.reserved = 0;
6345         class_locale.members.locale = MR_EVT_LOCALE_ALL;
6346         class_locale.members.class = MR_EVT_CLASS_DEBUG;
6347
6348         return megasas_register_aen(instance,
6349                         le32_to_cpu(eli.newest_seq_num) + 1,
6350                         class_locale.word);
6351 }
6352
6353 /**
6354  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
6355  * @instance:           Adapter soft state
6356  */
6357 static int megasas_io_attach(struct megasas_instance *instance)
6358 {
6359         struct Scsi_Host *host = instance->host;
6360
6361         /*
6362          * Export parameters required by SCSI mid-layer
6363          */
6364         host->unique_id = instance->unique_id;
6365         host->can_queue = instance->max_scsi_cmds;
6366         host->this_id = instance->init_id;
6367         host->sg_tablesize = instance->max_num_sge;
6368
6369         if (instance->fw_support_ieee)
6370                 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6371
6372         /*
6373          * Check if the module parameter value for max_sectors can be used
6374          */
6375         if (max_sectors && max_sectors < instance->max_sectors_per_req)
6376                 instance->max_sectors_per_req = max_sectors;
6377         else {
6378                 if (max_sectors) {
6379                         if (((instance->pdev->device ==
6380                                 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6381                                 (instance->pdev->device ==
6382                                 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6383                                 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6384                                 instance->max_sectors_per_req = max_sectors;
6385                         } else {
6386                         dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6387                                 "and <= %d (or < 1MB for GEN2 controller)\n",
6388                                 instance->max_sectors_per_req);
6389                         }
6390                 }
6391         }
6392
6393         host->max_sectors = instance->max_sectors_per_req;
6394         host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6395         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6396         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6397         host->max_lun = MEGASAS_MAX_LUN;
6398         host->max_cmd_len = 16;
6399
6400         /*
6401          * Notify the mid-layer about the new controller
6402          */
6403         if (scsi_add_host(host, &instance->pdev->dev)) {
6404                 dev_err(&instance->pdev->dev,
6405                         "Failed to add host from %s %d\n",
6406                         __func__, __LINE__);
6407                 return -ENODEV;
6408         }
6409
6410         return 0;
6411 }
6412
6413 /**
6414  * megasas_set_dma_mask -       Set DMA mask for supported controllers
6415  *
6416  * @instance:           Adapter soft state
6417  * Description:
6418  *
6419  * For Ventura, driver/FW will operate in 63bit DMA addresses.
6420  *
6421  * For invader-
6422  *      By default, driver/FW will operate in 32bit DMA addresses
6423  *      for consistent DMA mapping but if 32 bit consistent
6424  *      DMA mask fails, driver will try with 63 bit consistent
6425  *      mask provided FW is true 63bit DMA capable
6426  *
6427  * For older controllers(Thunderbolt and MFI based adapters)-
6428  *      driver/FW will operate in 32 bit consistent DMA addresses.
6429  */
6430 static int
6431 megasas_set_dma_mask(struct megasas_instance *instance)
6432 {
6433         u64 consistent_mask;
6434         struct pci_dev *pdev;
6435         u32 scratch_pad_1;
6436
6437         pdev = instance->pdev;
6438         consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6439                                 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6440
6441         if (IS_DMA64) {
6442                 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6443                     dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6444                         goto fail_set_dma_mask;
6445
6446                 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6447                     (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6448                      dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6449                         /*
6450                          * If 32 bit DMA mask fails, then try for 64 bit mask
6451                          * for FW capable of handling 64 bit DMA.
6452                          */
6453                         scratch_pad_1 = megasas_readl
6454                                 (instance, &instance->reg_set->outbound_scratch_pad_1);
6455
6456                         if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6457                                 goto fail_set_dma_mask;
6458                         else if (dma_set_mask_and_coherent(&pdev->dev,
6459                                                            DMA_BIT_MASK(63)))
6460                                 goto fail_set_dma_mask;
6461                 }
6462         } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6463                 goto fail_set_dma_mask;
6464
6465         if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6466                 instance->consistent_mask_64bit = false;
6467         else
6468                 instance->consistent_mask_64bit = true;
6469
6470         dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6471                  ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6472                  (instance->consistent_mask_64bit ? "63" : "32"));
6473
6474         return 0;
6475
6476 fail_set_dma_mask:
6477         dev_err(&pdev->dev, "Failed to set DMA mask\n");
6478         return -1;
6479
6480 }
6481
6482 /*
6483  * megasas_set_adapter_type -   Set adapter type.
6484  *                              Supported controllers can be divided in
6485  *                              different categories-
6486  *                                      enum MR_ADAPTER_TYPE {
6487  *                                              MFI_SERIES = 1,
6488  *                                              THUNDERBOLT_SERIES = 2,
6489  *                                              INVADER_SERIES = 3,
6490  *                                              VENTURA_SERIES = 4,
6491  *                                              AERO_SERIES = 5,
6492  *                                      };
6493  * @instance:                   Adapter soft state
6494  * return:                      void
6495  */
6496 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6497 {
6498         if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6499             (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6500                 instance->adapter_type = MFI_SERIES;
6501         } else {
6502                 switch (instance->pdev->device) {
6503                 case PCI_DEVICE_ID_LSI_AERO_10E1:
6504                 case PCI_DEVICE_ID_LSI_AERO_10E2:
6505                 case PCI_DEVICE_ID_LSI_AERO_10E5:
6506                 case PCI_DEVICE_ID_LSI_AERO_10E6:
6507                         instance->adapter_type = AERO_SERIES;
6508                         break;
6509                 case PCI_DEVICE_ID_LSI_VENTURA:
6510                 case PCI_DEVICE_ID_LSI_CRUSADER:
6511                 case PCI_DEVICE_ID_LSI_HARPOON:
6512                 case PCI_DEVICE_ID_LSI_TOMCAT:
6513                 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6514                 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6515                         instance->adapter_type = VENTURA_SERIES;
6516                         break;
6517                 case PCI_DEVICE_ID_LSI_FUSION:
6518                 case PCI_DEVICE_ID_LSI_PLASMA:
6519                         instance->adapter_type = THUNDERBOLT_SERIES;
6520                         break;
6521                 case PCI_DEVICE_ID_LSI_INVADER:
6522                 case PCI_DEVICE_ID_LSI_INTRUDER:
6523                 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6524                 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6525                 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6526                 case PCI_DEVICE_ID_LSI_FURY:
6527                         instance->adapter_type = INVADER_SERIES;
6528                         break;
6529                 default: /* For all other supported controllers */
6530                         instance->adapter_type = MFI_SERIES;
6531                         break;
6532                 }
6533         }
6534 }
6535
6536 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6537 {
6538         instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6539                         sizeof(u32), &instance->producer_h, GFP_KERNEL);
6540         instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6541                         sizeof(u32), &instance->consumer_h, GFP_KERNEL);
6542
6543         if (!instance->producer || !instance->consumer) {
6544                 dev_err(&instance->pdev->dev,
6545                         "Failed to allocate memory for producer, consumer\n");
6546                 return -1;
6547         }
6548
6549         *instance->producer = 0;
6550         *instance->consumer = 0;
6551         return 0;
6552 }
6553
6554 /**
6555  * megasas_alloc_ctrl_mem -     Allocate per controller memory for core data
6556  *                              structures which are not common across MFI
6557  *                              adapters and fusion adapters.
6558  *                              For MFI based adapters, allocate producer and
6559  *                              consumer buffers. For fusion adapters, allocate
6560  *                              memory for fusion context.
6561  * @instance:                   Adapter soft state
6562  * return:                      0 for SUCCESS
6563  */
6564 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6565 {
6566         instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
6567                                       GFP_KERNEL);
6568         if (!instance->reply_map)
6569                 return -ENOMEM;
6570
6571         switch (instance->adapter_type) {
6572         case MFI_SERIES:
6573                 if (megasas_alloc_mfi_ctrl_mem(instance))
6574                         goto fail;
6575                 break;
6576         case AERO_SERIES:
6577         case VENTURA_SERIES:
6578         case THUNDERBOLT_SERIES:
6579         case INVADER_SERIES:
6580                 if (megasas_alloc_fusion_context(instance))
6581                         goto fail;
6582                 break;
6583         }
6584
6585         return 0;
6586  fail:
6587         kfree(instance->reply_map);
6588         instance->reply_map = NULL;
6589         return -ENOMEM;
6590 }
6591
6592 /*
6593  * megasas_free_ctrl_mem -      Free fusion context for fusion adapters and
6594  *                              producer, consumer buffers for MFI adapters
6595  *
6596  * @instance -                  Adapter soft instance
6597  *
6598  */
6599 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6600 {
6601         kfree(instance->reply_map);
6602         if (instance->adapter_type == MFI_SERIES) {
6603                 if (instance->producer)
6604                         dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6605                                             instance->producer,
6606                                             instance->producer_h);
6607                 if (instance->consumer)
6608                         dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6609                                             instance->consumer,
6610                                             instance->consumer_h);
6611         } else {
6612                 megasas_free_fusion_context(instance);
6613         }
6614 }
6615
6616 /**
6617  * megasas_alloc_ctrl_dma_buffers -     Allocate consistent DMA buffers during
6618  *                                      driver load time
6619  *
6620  * @instance-                           Adapter soft instance
6621  * @return-                             O for SUCCESS
6622  */
6623 static inline
6624 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6625 {
6626         struct pci_dev *pdev = instance->pdev;
6627         struct fusion_context *fusion = instance->ctrl_context;
6628
6629         instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6630                         sizeof(struct megasas_evt_detail),
6631                         &instance->evt_detail_h, GFP_KERNEL);
6632
6633         if (!instance->evt_detail) {
6634                 dev_err(&instance->pdev->dev,
6635                         "Failed to allocate event detail buffer\n");
6636                 return -ENOMEM;
6637         }
6638
6639         if (fusion) {
6640                 fusion->ioc_init_request =
6641                         dma_alloc_coherent(&pdev->dev,
6642                                            sizeof(struct MPI2_IOC_INIT_REQUEST),
6643                                            &fusion->ioc_init_request_phys,
6644                                            GFP_KERNEL);
6645
6646                 if (!fusion->ioc_init_request) {
6647                         dev_err(&pdev->dev,
6648                                 "Failed to allocate PD list buffer\n");
6649                         return -ENOMEM;
6650                 }
6651
6652                 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6653                                 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6654                                 &instance->snapdump_prop_h, GFP_KERNEL);
6655
6656                 if (!instance->snapdump_prop)
6657                         dev_err(&pdev->dev,
6658                                 "Failed to allocate snapdump properties buffer\n");
6659
6660                 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6661                                                         HOST_DEVICE_LIST_SZ,
6662                                                         &instance->host_device_list_buf_h,
6663                                                         GFP_KERNEL);
6664
6665                 if (!instance->host_device_list_buf) {
6666                         dev_err(&pdev->dev,
6667                                 "Failed to allocate targetid list buffer\n");
6668                         return -ENOMEM;
6669                 }
6670
6671         }
6672
6673         instance->pd_list_buf =
6674                 dma_alloc_coherent(&pdev->dev,
6675                                      MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6676                                      &instance->pd_list_buf_h, GFP_KERNEL);
6677
6678         if (!instance->pd_list_buf) {
6679                 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6680                 return -ENOMEM;
6681         }
6682
6683         instance->ctrl_info_buf =
6684                 dma_alloc_coherent(&pdev->dev,
6685                                      sizeof(struct megasas_ctrl_info),
6686                                      &instance->ctrl_info_buf_h, GFP_KERNEL);
6687
6688         if (!instance->ctrl_info_buf) {
6689                 dev_err(&pdev->dev,
6690                         "Failed to allocate controller info buffer\n");
6691                 return -ENOMEM;
6692         }
6693
6694         instance->ld_list_buf =
6695                 dma_alloc_coherent(&pdev->dev,
6696                                      sizeof(struct MR_LD_LIST),
6697                                      &instance->ld_list_buf_h, GFP_KERNEL);
6698
6699         if (!instance->ld_list_buf) {
6700                 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6701                 return -ENOMEM;
6702         }
6703
6704         instance->ld_targetid_list_buf =
6705                 dma_alloc_coherent(&pdev->dev,
6706                                 sizeof(struct MR_LD_TARGETID_LIST),
6707                                 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
6708
6709         if (!instance->ld_targetid_list_buf) {
6710                 dev_err(&pdev->dev,
6711                         "Failed to allocate LD targetid list buffer\n");
6712                 return -ENOMEM;
6713         }
6714
6715         if (!reset_devices) {
6716                 instance->system_info_buf =
6717                         dma_alloc_coherent(&pdev->dev,
6718                                         sizeof(struct MR_DRV_SYSTEM_INFO),
6719                                         &instance->system_info_h, GFP_KERNEL);
6720                 instance->pd_info =
6721                         dma_alloc_coherent(&pdev->dev,
6722                                         sizeof(struct MR_PD_INFO),
6723                                         &instance->pd_info_h, GFP_KERNEL);
6724                 instance->tgt_prop =
6725                         dma_alloc_coherent(&pdev->dev,
6726                                         sizeof(struct MR_TARGET_PROPERTIES),
6727                                         &instance->tgt_prop_h, GFP_KERNEL);
6728                 instance->crash_dump_buf =
6729                         dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6730                                         &instance->crash_dump_h, GFP_KERNEL);
6731
6732                 if (!instance->system_info_buf)
6733                         dev_err(&instance->pdev->dev,
6734                                 "Failed to allocate system info buffer\n");
6735
6736                 if (!instance->pd_info)
6737                         dev_err(&instance->pdev->dev,
6738                                 "Failed to allocate pd_info buffer\n");
6739
6740                 if (!instance->tgt_prop)
6741                         dev_err(&instance->pdev->dev,
6742                                 "Failed to allocate tgt_prop buffer\n");
6743
6744                 if (!instance->crash_dump_buf)
6745                         dev_err(&instance->pdev->dev,
6746                                 "Failed to allocate crash dump buffer\n");
6747         }
6748
6749         return 0;
6750 }
6751
6752 /*
6753  * megasas_free_ctrl_dma_buffers -      Free consistent DMA buffers allocated
6754  *                                      during driver load time
6755  *
6756  * @instance-                           Adapter soft instance
6757  *
6758  */
6759 static inline
6760 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6761 {
6762         struct pci_dev *pdev = instance->pdev;
6763         struct fusion_context *fusion = instance->ctrl_context;
6764
6765         if (instance->evt_detail)
6766                 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
6767                                     instance->evt_detail,
6768                                     instance->evt_detail_h);
6769
6770         if (fusion && fusion->ioc_init_request)
6771                 dma_free_coherent(&pdev->dev,
6772                                   sizeof(struct MPI2_IOC_INIT_REQUEST),
6773                                   fusion->ioc_init_request,
6774                                   fusion->ioc_init_request_phys);
6775
6776         if (instance->pd_list_buf)
6777                 dma_free_coherent(&pdev->dev,
6778                                     MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6779                                     instance->pd_list_buf,
6780                                     instance->pd_list_buf_h);
6781
6782         if (instance->ld_list_buf)
6783                 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
6784                                     instance->ld_list_buf,
6785                                     instance->ld_list_buf_h);
6786
6787         if (instance->ld_targetid_list_buf)
6788                 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
6789                                     instance->ld_targetid_list_buf,
6790                                     instance->ld_targetid_list_buf_h);
6791
6792         if (instance->ctrl_info_buf)
6793                 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
6794                                     instance->ctrl_info_buf,
6795                                     instance->ctrl_info_buf_h);
6796
6797         if (instance->system_info_buf)
6798                 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
6799                                     instance->system_info_buf,
6800                                     instance->system_info_h);
6801
6802         if (instance->pd_info)
6803                 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
6804                                     instance->pd_info, instance->pd_info_h);
6805
6806         if (instance->tgt_prop)
6807                 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
6808                                     instance->tgt_prop, instance->tgt_prop_h);
6809
6810         if (instance->crash_dump_buf)
6811                 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6812                                     instance->crash_dump_buf,
6813                                     instance->crash_dump_h);
6814
6815         if (instance->snapdump_prop)
6816                 dma_free_coherent(&pdev->dev,
6817                                   sizeof(struct MR_SNAPDUMP_PROPERTIES),
6818                                   instance->snapdump_prop,
6819                                   instance->snapdump_prop_h);
6820
6821         if (instance->host_device_list_buf)
6822                 dma_free_coherent(&pdev->dev,
6823                                   HOST_DEVICE_LIST_SZ,
6824                                   instance->host_device_list_buf,
6825                                   instance->host_device_list_buf_h);
6826
6827 }
6828
6829 /*
6830  * megasas_init_ctrl_params -           Initialize controller's instance
6831  *                                      parameters before FW init
6832  * @instance -                          Adapter soft instance
6833  * @return -                            void
6834  */
6835 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6836 {
6837         instance->fw_crash_state = UNAVAILABLE;
6838
6839         megasas_poll_wait_aen = 0;
6840         instance->issuepend_done = 1;
6841         atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6842
6843         /*
6844          * Initialize locks and queues
6845          */
6846         INIT_LIST_HEAD(&instance->cmd_pool);
6847         INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6848
6849         atomic_set(&instance->fw_outstanding, 0);
6850         atomic64_set(&instance->total_io_count, 0);
6851
6852         init_waitqueue_head(&instance->int_cmd_wait_q);
6853         init_waitqueue_head(&instance->abort_cmd_wait_q);
6854
6855         spin_lock_init(&instance->crashdump_lock);
6856         spin_lock_init(&instance->mfi_pool_lock);
6857         spin_lock_init(&instance->hba_lock);
6858         spin_lock_init(&instance->stream_lock);
6859         spin_lock_init(&instance->completion_lock);
6860
6861         mutex_init(&instance->reset_mutex);
6862
6863         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6864             (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6865                 instance->flag_ieee = 1;
6866
6867         megasas_dbg_lvl = 0;
6868         instance->flag = 0;
6869         instance->unload = 1;
6870         instance->last_time = 0;
6871         instance->disableOnlineCtrlReset = 1;
6872         instance->UnevenSpanSupport = 0;
6873         instance->smp_affinity_enable = smp_affinity_enable ? true : false;
6874         instance->msix_load_balance = false;
6875
6876         if (instance->adapter_type != MFI_SERIES)
6877                 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
6878         else
6879                 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
6880 }
6881
6882 /**
6883  * megasas_probe_one -  PCI hotplug entry point
6884  * @pdev:               PCI device structure
6885  * @id:                 PCI ids of supported hotplugged adapter
6886  */
6887 static int megasas_probe_one(struct pci_dev *pdev,
6888                              const struct pci_device_id *id)
6889 {
6890         int rval, pos;
6891         struct Scsi_Host *host;
6892         struct megasas_instance *instance;
6893         u16 control = 0;
6894
6895         switch (pdev->device) {
6896         case PCI_DEVICE_ID_LSI_AERO_10E1:
6897         case PCI_DEVICE_ID_LSI_AERO_10E5:
6898                 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
6899                 break;
6900         }
6901
6902         /* Reset MSI-X in the kdump kernel */
6903         if (reset_devices) {
6904                 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6905                 if (pos) {
6906                         pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
6907                                              &control);
6908                         if (control & PCI_MSIX_FLAGS_ENABLE) {
6909                                 dev_info(&pdev->dev, "resetting MSI-X\n");
6910                                 pci_write_config_word(pdev,
6911                                                       pos + PCI_MSIX_FLAGS,
6912                                                       control &
6913                                                       ~PCI_MSIX_FLAGS_ENABLE);
6914                         }
6915                 }
6916         }
6917
6918         /*
6919          * PCI prepping: enable device set bus mastering and dma mask
6920          */
6921         rval = pci_enable_device_mem(pdev);
6922
6923         if (rval) {
6924                 return rval;
6925         }
6926
6927         pci_set_master(pdev);
6928
6929         host = scsi_host_alloc(&megasas_template,
6930                                sizeof(struct megasas_instance));
6931
6932         if (!host) {
6933                 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
6934                 goto fail_alloc_instance;
6935         }
6936
6937         instance = (struct megasas_instance *)host->hostdata;
6938         memset(instance, 0, sizeof(*instance));
6939         atomic_set(&instance->fw_reset_no_pci_access, 0);
6940
6941         /*
6942          * Initialize PCI related and misc parameters
6943          */
6944         instance->pdev = pdev;
6945         instance->host = host;
6946         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6947         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6948
6949         megasas_set_adapter_type(instance);
6950
6951         /*
6952          * Initialize MFI Firmware
6953          */
6954         if (megasas_init_fw(instance))
6955                 goto fail_init_mfi;
6956
6957         if (instance->requestorId) {
6958                 if (instance->PlasmaFW111) {
6959                         instance->vf_affiliation_111 =
6960                                 dma_alloc_coherent(&pdev->dev,
6961                                         sizeof(struct MR_LD_VF_AFFILIATION_111),
6962                                         &instance->vf_affiliation_111_h,
6963                                         GFP_KERNEL);
6964                         if (!instance->vf_affiliation_111)
6965                                 dev_warn(&pdev->dev, "Can't allocate "
6966                                        "memory for VF affiliation buffer\n");
6967                 } else {
6968                         instance->vf_affiliation =
6969                                 dma_alloc_coherent(&pdev->dev,
6970                                         (MAX_LOGICAL_DRIVES + 1) *
6971                                         sizeof(struct MR_LD_VF_AFFILIATION),
6972                                         &instance->vf_affiliation_h,
6973                                         GFP_KERNEL);
6974                         if (!instance->vf_affiliation)
6975                                 dev_warn(&pdev->dev, "Can't allocate "
6976                                        "memory for VF affiliation buffer\n");
6977                 }
6978         }
6979
6980         /*
6981          * Store instance in PCI softstate
6982          */
6983         pci_set_drvdata(pdev, instance);
6984
6985         /*
6986          * Add this controller to megasas_mgmt_info structure so that it
6987          * can be exported to management applications
6988          */
6989         megasas_mgmt_info.count++;
6990         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6991         megasas_mgmt_info.max_index++;
6992
6993         /*
6994          * Register with SCSI mid-layer
6995          */
6996         if (megasas_io_attach(instance))
6997                 goto fail_io_attach;
6998
6999         instance->unload = 0;
7000         /*
7001          * Trigger SCSI to scan our drives
7002          */
7003         if (!instance->enable_fw_dev_list ||
7004             (instance->host_device_list_buf->count > 0))
7005                 scsi_scan_host(host);
7006
7007         /*
7008          * Initiate AEN (Asynchronous Event Notification)
7009          */
7010         if (megasas_start_aen(instance)) {
7011                 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7012                 goto fail_start_aen;
7013         }
7014
7015         /* Get current SR-IOV LD/VF affiliation */
7016         if (instance->requestorId)
7017                 megasas_get_ld_vf_affiliation(instance, 1);
7018
7019         return 0;
7020
7021 fail_start_aen:
7022 fail_io_attach:
7023         megasas_mgmt_info.count--;
7024         megasas_mgmt_info.max_index--;
7025         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7026
7027         instance->instancet->disable_intr(instance);
7028         megasas_destroy_irqs(instance);
7029
7030         if (instance->adapter_type != MFI_SERIES)
7031                 megasas_release_fusion(instance);
7032         else
7033                 megasas_release_mfi(instance);
7034         if (instance->msix_vectors)
7035                 pci_free_irq_vectors(instance->pdev);
7036 fail_init_mfi:
7037         scsi_host_put(host);
7038 fail_alloc_instance:
7039         pci_disable_device(pdev);
7040
7041         return -ENODEV;
7042 }
7043
7044 /**
7045  * megasas_flush_cache -        Requests FW to flush all its caches
7046  * @instance:                   Adapter soft state
7047  */
7048 static void megasas_flush_cache(struct megasas_instance *instance)
7049 {
7050         struct megasas_cmd *cmd;
7051         struct megasas_dcmd_frame *dcmd;
7052
7053         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7054                 return;
7055
7056         cmd = megasas_get_cmd(instance);
7057
7058         if (!cmd)
7059                 return;
7060
7061         dcmd = &cmd->frame->dcmd;
7062
7063         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7064
7065         dcmd->cmd = MFI_CMD_DCMD;
7066         dcmd->cmd_status = 0x0;
7067         dcmd->sge_count = 0;
7068         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7069         dcmd->timeout = 0;
7070         dcmd->pad_0 = 0;
7071         dcmd->data_xfer_len = 0;
7072         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7073         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7074
7075         if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7076                         != DCMD_SUCCESS) {
7077                 dev_err(&instance->pdev->dev,
7078                         "return from %s %d\n", __func__, __LINE__);
7079                 return;
7080         }
7081
7082         megasas_return_cmd(instance, cmd);
7083 }
7084
7085 /**
7086  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
7087  * @instance:                           Adapter soft state
7088  * @opcode:                             Shutdown/Hibernate
7089  */
7090 static void megasas_shutdown_controller(struct megasas_instance *instance,
7091                                         u32 opcode)
7092 {
7093         struct megasas_cmd *cmd;
7094         struct megasas_dcmd_frame *dcmd;
7095
7096         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7097                 return;
7098
7099         cmd = megasas_get_cmd(instance);
7100
7101         if (!cmd)
7102                 return;
7103
7104         if (instance->aen_cmd)
7105                 megasas_issue_blocked_abort_cmd(instance,
7106                         instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7107         if (instance->map_update_cmd)
7108                 megasas_issue_blocked_abort_cmd(instance,
7109                         instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7110         if (instance->jbod_seq_cmd)
7111                 megasas_issue_blocked_abort_cmd(instance,
7112                         instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7113
7114         dcmd = &cmd->frame->dcmd;
7115
7116         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7117
7118         dcmd->cmd = MFI_CMD_DCMD;
7119         dcmd->cmd_status = 0x0;
7120         dcmd->sge_count = 0;
7121         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7122         dcmd->timeout = 0;
7123         dcmd->pad_0 = 0;
7124         dcmd->data_xfer_len = 0;
7125         dcmd->opcode = cpu_to_le32(opcode);
7126
7127         if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7128                         != DCMD_SUCCESS) {
7129                 dev_err(&instance->pdev->dev,
7130                         "return from %s %d\n", __func__, __LINE__);
7131                 return;
7132         }
7133
7134         megasas_return_cmd(instance, cmd);
7135 }
7136
7137 #ifdef CONFIG_PM
7138 /**
7139  * megasas_suspend -    driver suspend entry point
7140  * @pdev:               PCI device structure
7141  * @state:              PCI power state to suspend routine
7142  */
7143 static int
7144 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7145 {
7146         struct Scsi_Host *host;
7147         struct megasas_instance *instance;
7148
7149         instance = pci_get_drvdata(pdev);
7150         host = instance->host;
7151         instance->unload = 1;
7152
7153         /* Shutdown SR-IOV heartbeat timer */
7154         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7155                 del_timer_sync(&instance->sriov_heartbeat_timer);
7156
7157         /* Stop the FW fault detection watchdog */
7158         if (instance->adapter_type != MFI_SERIES)
7159                 megasas_fusion_stop_watchdog(instance);
7160
7161         megasas_flush_cache(instance);
7162         megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7163
7164         /* cancel the delayed work if this work still in queue */
7165         if (instance->ev != NULL) {
7166                 struct megasas_aen_event *ev = instance->ev;
7167                 cancel_delayed_work_sync(&ev->hotplug_work);
7168                 instance->ev = NULL;
7169         }
7170
7171         tasklet_kill(&instance->isr_tasklet);
7172
7173         pci_set_drvdata(instance->pdev, instance);
7174         instance->instancet->disable_intr(instance);
7175
7176         megasas_destroy_irqs(instance);
7177
7178         if (instance->msix_vectors)
7179                 pci_free_irq_vectors(instance->pdev);
7180
7181         pci_save_state(pdev);
7182         pci_disable_device(pdev);
7183
7184         pci_set_power_state(pdev, pci_choose_state(pdev, state));
7185
7186         return 0;
7187 }
7188
7189 /**
7190  * megasas_resume-      driver resume entry point
7191  * @pdev:               PCI device structure
7192  */
7193 static int
7194 megasas_resume(struct pci_dev *pdev)
7195 {
7196         int rval;
7197         struct Scsi_Host *host;
7198         struct megasas_instance *instance;
7199         int irq_flags = PCI_IRQ_LEGACY;
7200
7201         instance = pci_get_drvdata(pdev);
7202         host = instance->host;
7203         pci_set_power_state(pdev, PCI_D0);
7204         pci_enable_wake(pdev, PCI_D0, 0);
7205         pci_restore_state(pdev);
7206
7207         /*
7208          * PCI prepping: enable device set bus mastering and dma mask
7209          */
7210         rval = pci_enable_device_mem(pdev);
7211
7212         if (rval) {
7213                 dev_err(&pdev->dev, "Enable device failed\n");
7214                 return rval;
7215         }
7216
7217         pci_set_master(pdev);
7218
7219         /*
7220          * We expect the FW state to be READY
7221          */
7222         if (megasas_transition_to_ready(instance, 0))
7223                 goto fail_ready_state;
7224
7225         if (megasas_set_dma_mask(instance))
7226                 goto fail_set_dma_mask;
7227
7228         /*
7229          * Initialize MFI Firmware
7230          */
7231
7232         atomic_set(&instance->fw_outstanding, 0);
7233         atomic_set(&instance->ldio_outstanding, 0);
7234
7235         /* Now re-enable MSI-X */
7236         if (instance->msix_vectors) {
7237                 irq_flags = PCI_IRQ_MSIX;
7238                 if (instance->smp_affinity_enable)
7239                         irq_flags |= PCI_IRQ_AFFINITY;
7240         }
7241         rval = pci_alloc_irq_vectors(instance->pdev, 1,
7242                                      instance->msix_vectors ?
7243                                      instance->msix_vectors : 1, irq_flags);
7244         if (rval < 0)
7245                 goto fail_reenable_msix;
7246
7247         megasas_setup_reply_map(instance);
7248
7249         if (instance->adapter_type != MFI_SERIES) {
7250                 megasas_reset_reply_desc(instance);
7251                 if (megasas_ioc_init_fusion(instance)) {
7252                         megasas_free_cmds(instance);
7253                         megasas_free_cmds_fusion(instance);
7254                         goto fail_init_mfi;
7255                 }
7256                 if (!megasas_get_map_info(instance))
7257                         megasas_sync_map_info(instance);
7258         } else {
7259                 *instance->producer = 0;
7260                 *instance->consumer = 0;
7261                 if (megasas_issue_init_mfi(instance))
7262                         goto fail_init_mfi;
7263         }
7264
7265         if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7266                 goto fail_init_mfi;
7267
7268         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7269                      (unsigned long)instance);
7270
7271         if (instance->msix_vectors ?
7272                         megasas_setup_irqs_msix(instance, 0) :
7273                         megasas_setup_irqs_ioapic(instance))
7274                 goto fail_init_mfi;
7275
7276         if (instance->adapter_type != MFI_SERIES)
7277                 megasas_setup_irq_poll(instance);
7278
7279         /* Re-launch SR-IOV heartbeat timer */
7280         if (instance->requestorId) {
7281                 if (!megasas_sriov_start_heartbeat(instance, 0))
7282                         megasas_start_timer(instance);
7283                 else {
7284                         instance->skip_heartbeat_timer_del = 1;
7285                         goto fail_init_mfi;
7286                 }
7287         }
7288
7289         instance->instancet->enable_intr(instance);
7290         megasas_setup_jbod_map(instance);
7291         instance->unload = 0;
7292
7293         /*
7294          * Initiate AEN (Asynchronous Event Notification)
7295          */
7296         if (megasas_start_aen(instance))
7297                 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7298
7299         /* Re-launch FW fault watchdog */
7300         if (instance->adapter_type != MFI_SERIES)
7301                 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7302                         goto fail_start_watchdog;
7303
7304         return 0;
7305
7306 fail_start_watchdog:
7307         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7308                 del_timer_sync(&instance->sriov_heartbeat_timer);
7309 fail_init_mfi:
7310         megasas_free_ctrl_dma_buffers(instance);
7311         megasas_free_ctrl_mem(instance);
7312         scsi_host_put(host);
7313
7314 fail_reenable_msix:
7315 fail_set_dma_mask:
7316 fail_ready_state:
7317
7318         pci_disable_device(pdev);
7319
7320         return -ENODEV;
7321 }
7322 #else
7323 #define megasas_suspend NULL
7324 #define megasas_resume  NULL
7325 #endif
7326
7327 static inline int
7328 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7329 {
7330         int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7331         int i;
7332         u8 adp_state;
7333
7334         for (i = 0; i < wait_time; i++) {
7335                 adp_state = atomic_read(&instance->adprecovery);
7336                 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7337                     (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7338                         break;
7339
7340                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7341                         dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7342
7343                 msleep(1000);
7344         }
7345
7346         if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7347                 dev_info(&instance->pdev->dev,
7348                          "%s HBA failed to become operational, adp_state %d\n",
7349                          __func__, adp_state);
7350                 return 1;
7351         }
7352
7353         return 0;
7354 }
7355
7356 /**
7357  * megasas_detach_one - PCI hot"un"plug entry point
7358  * @pdev:               PCI device structure
7359  */
7360 static void megasas_detach_one(struct pci_dev *pdev)
7361 {
7362         int i;
7363         struct Scsi_Host *host;
7364         struct megasas_instance *instance;
7365         struct fusion_context *fusion;
7366         u32 pd_seq_map_sz;
7367
7368         instance = pci_get_drvdata(pdev);
7369         host = instance->host;
7370         fusion = instance->ctrl_context;
7371
7372         /* Shutdown SR-IOV heartbeat timer */
7373         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7374                 del_timer_sync(&instance->sriov_heartbeat_timer);
7375
7376         /* Stop the FW fault detection watchdog */
7377         if (instance->adapter_type != MFI_SERIES)
7378                 megasas_fusion_stop_watchdog(instance);
7379
7380         if (instance->fw_crash_state != UNAVAILABLE)
7381                 megasas_free_host_crash_buffer(instance);
7382         scsi_remove_host(instance->host);
7383         instance->unload = 1;
7384
7385         if (megasas_wait_for_adapter_operational(instance))
7386                 goto skip_firing_dcmds;
7387
7388         megasas_flush_cache(instance);
7389         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7390
7391 skip_firing_dcmds:
7392         /* cancel the delayed work if this work still in queue*/
7393         if (instance->ev != NULL) {
7394                 struct megasas_aen_event *ev = instance->ev;
7395                 cancel_delayed_work_sync(&ev->hotplug_work);
7396                 instance->ev = NULL;
7397         }
7398
7399         /* cancel all wait events */
7400         wake_up_all(&instance->int_cmd_wait_q);
7401
7402         tasklet_kill(&instance->isr_tasklet);
7403
7404         /*
7405          * Take the instance off the instance array. Note that we will not
7406          * decrement the max_index. We let this array be sparse array
7407          */
7408         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7409                 if (megasas_mgmt_info.instance[i] == instance) {
7410                         megasas_mgmt_info.count--;
7411                         megasas_mgmt_info.instance[i] = NULL;
7412
7413                         break;
7414                 }
7415         }
7416
7417         instance->instancet->disable_intr(instance);
7418
7419         megasas_destroy_irqs(instance);
7420
7421         if (instance->msix_vectors)
7422                 pci_free_irq_vectors(instance->pdev);
7423
7424         if (instance->adapter_type >= VENTURA_SERIES) {
7425                 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7426                         kfree(fusion->stream_detect_by_ld[i]);
7427                 kfree(fusion->stream_detect_by_ld);
7428                 fusion->stream_detect_by_ld = NULL;
7429         }
7430
7431
7432         if (instance->adapter_type != MFI_SERIES) {
7433                 megasas_release_fusion(instance);
7434                         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7435                                 (sizeof(struct MR_PD_CFG_SEQ) *
7436                                         (MAX_PHYSICAL_DEVICES - 1));
7437                 for (i = 0; i < 2 ; i++) {
7438                         if (fusion->ld_map[i])
7439                                 dma_free_coherent(&instance->pdev->dev,
7440                                                   fusion->max_map_sz,
7441                                                   fusion->ld_map[i],
7442                                                   fusion->ld_map_phys[i]);
7443                         if (fusion->ld_drv_map[i]) {
7444                                 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7445                                         vfree(fusion->ld_drv_map[i]);
7446                                 else
7447                                         free_pages((ulong)fusion->ld_drv_map[i],
7448                                                    fusion->drv_map_pages);
7449                         }
7450
7451                         if (fusion->pd_seq_sync[i])
7452                                 dma_free_coherent(&instance->pdev->dev,
7453                                         pd_seq_map_sz,
7454                                         fusion->pd_seq_sync[i],
7455                                         fusion->pd_seq_phys[i]);
7456                 }
7457         } else {
7458                 megasas_release_mfi(instance);
7459         }
7460
7461         if (instance->vf_affiliation)
7462                 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7463                                     sizeof(struct MR_LD_VF_AFFILIATION),
7464                                     instance->vf_affiliation,
7465                                     instance->vf_affiliation_h);
7466
7467         if (instance->vf_affiliation_111)
7468                 dma_free_coherent(&pdev->dev,
7469                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
7470                                     instance->vf_affiliation_111,
7471                                     instance->vf_affiliation_111_h);
7472
7473         if (instance->hb_host_mem)
7474                 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
7475                                     instance->hb_host_mem,
7476                                     instance->hb_host_mem_h);
7477
7478         megasas_free_ctrl_dma_buffers(instance);
7479
7480         megasas_free_ctrl_mem(instance);
7481
7482         scsi_host_put(host);
7483
7484         pci_disable_device(pdev);
7485 }
7486
7487 /**
7488  * megasas_shutdown -   Shutdown entry point
7489  * @device:             Generic device structure
7490  */
7491 static void megasas_shutdown(struct pci_dev *pdev)
7492 {
7493         struct megasas_instance *instance = pci_get_drvdata(pdev);
7494
7495         instance->unload = 1;
7496
7497         if (megasas_wait_for_adapter_operational(instance))
7498                 goto skip_firing_dcmds;
7499
7500         megasas_flush_cache(instance);
7501         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7502
7503 skip_firing_dcmds:
7504         instance->instancet->disable_intr(instance);
7505         megasas_destroy_irqs(instance);
7506
7507         if (instance->msix_vectors)
7508                 pci_free_irq_vectors(instance->pdev);
7509 }
7510
7511 /**
7512  * megasas_mgmt_open -  char node "open" entry point
7513  */
7514 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7515 {
7516         /*
7517          * Allow only those users with admin rights
7518          */
7519         if (!capable(CAP_SYS_ADMIN))
7520                 return -EACCES;
7521
7522         return 0;
7523 }
7524
7525 /**
7526  * megasas_mgmt_fasync -        Async notifier registration from applications
7527  *
7528  * This function adds the calling process to a driver global queue. When an
7529  * event occurs, SIGIO will be sent to all processes in this queue.
7530  */
7531 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7532 {
7533         int rc;
7534
7535         mutex_lock(&megasas_async_queue_mutex);
7536
7537         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7538
7539         mutex_unlock(&megasas_async_queue_mutex);
7540
7541         if (rc >= 0) {
7542                 /* For sanity check when we get ioctl */
7543                 filep->private_data = filep;
7544                 return 0;
7545         }
7546
7547         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7548
7549         return rc;
7550 }
7551
7552 /**
7553  * megasas_mgmt_poll -  char node "poll" entry point
7554  * */
7555 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
7556 {
7557         __poll_t mask;
7558         unsigned long flags;
7559
7560         poll_wait(file, &megasas_poll_wait, wait);
7561         spin_lock_irqsave(&poll_aen_lock, flags);
7562         if (megasas_poll_wait_aen)
7563                 mask = (EPOLLIN | EPOLLRDNORM);
7564         else
7565                 mask = 0;
7566         megasas_poll_wait_aen = 0;
7567         spin_unlock_irqrestore(&poll_aen_lock, flags);
7568         return mask;
7569 }
7570
7571 /*
7572  * megasas_set_crash_dump_params_ioctl:
7573  *              Send CRASH_DUMP_MODE DCMD to all controllers
7574  * @cmd:        MFI command frame
7575  */
7576
7577 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
7578 {
7579         struct megasas_instance *local_instance;
7580         int i, error = 0;
7581         int crash_support;
7582
7583         crash_support = cmd->frame->dcmd.mbox.w[0];
7584
7585         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7586                 local_instance = megasas_mgmt_info.instance[i];
7587                 if (local_instance && local_instance->crash_dump_drv_support) {
7588                         if ((atomic_read(&local_instance->adprecovery) ==
7589                                 MEGASAS_HBA_OPERATIONAL) &&
7590                                 !megasas_set_crash_dump_params(local_instance,
7591                                         crash_support)) {
7592                                 local_instance->crash_dump_app_support =
7593                                         crash_support;
7594                                 dev_info(&local_instance->pdev->dev,
7595                                         "Application firmware crash "
7596                                         "dump mode set success\n");
7597                                 error = 0;
7598                         } else {
7599                                 dev_info(&local_instance->pdev->dev,
7600                                         "Application firmware crash "
7601                                         "dump mode set failed\n");
7602                                 error = -1;
7603                         }
7604                 }
7605         }
7606         return error;
7607 }
7608
7609 /**
7610  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
7611  * @instance:                   Adapter soft state
7612  * @argp:                       User's ioctl packet
7613  */
7614 static int
7615 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7616                       struct megasas_iocpacket __user * user_ioc,
7617                       struct megasas_iocpacket *ioc)
7618 {
7619         struct megasas_sge64 *kern_sge64 = NULL;
7620         struct megasas_sge32 *kern_sge32 = NULL;
7621         struct megasas_cmd *cmd;
7622         void *kbuff_arr[MAX_IOCTL_SGE];
7623         dma_addr_t buf_handle = 0;
7624         int error = 0, i;
7625         void *sense = NULL;
7626         dma_addr_t sense_handle;
7627         unsigned long *sense_ptr;
7628         u32 opcode = 0;
7629
7630         memset(kbuff_arr, 0, sizeof(kbuff_arr));
7631
7632         if (ioc->sge_count > MAX_IOCTL_SGE) {
7633                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] >  max limit [%d]\n",
7634                        ioc->sge_count, MAX_IOCTL_SGE);
7635                 return -EINVAL;
7636         }
7637
7638         if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7639             ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7640             !instance->support_nvme_passthru)) {
7641                 dev_err(&instance->pdev->dev,
7642                         "Received invalid ioctl command 0x%x\n",
7643                         ioc->frame.hdr.cmd);
7644                 return -ENOTSUPP;
7645         }
7646
7647         cmd = megasas_get_cmd(instance);
7648         if (!cmd) {
7649                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
7650                 return -ENOMEM;
7651         }
7652
7653         /*
7654          * User's IOCTL packet has 2 frames (maximum). Copy those two
7655          * frames into our cmd's frames. cmd->frame's context will get
7656          * overwritten when we copy from user's frames. So set that value
7657          * alone separately
7658          */
7659         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
7660         cmd->frame->hdr.context = cpu_to_le32(cmd->index);
7661         cmd->frame->hdr.pad_0 = 0;
7662
7663         cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7664
7665         if (instance->consistent_mask_64bit)
7666                 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7667                                        MFI_FRAME_SENSE64));
7668         else
7669                 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
7670                                                MFI_FRAME_SENSE64));
7671
7672         if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7673                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
7674
7675         if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
7676                 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7677                         megasas_return_cmd(instance, cmd);
7678                         return -1;
7679                 }
7680         }
7681
7682         if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
7683                 error = megasas_set_crash_dump_params_ioctl(cmd);
7684                 megasas_return_cmd(instance, cmd);
7685                 return error;
7686         }
7687
7688         /*
7689          * The management interface between applications and the fw uses
7690          * MFI frames. E.g, RAID configuration changes, LD property changes
7691          * etc are accomplishes through different kinds of MFI frames. The
7692          * driver needs to care only about substituting user buffers with
7693          * kernel buffers in SGLs. The location of SGL is embedded in the
7694          * struct iocpacket itself.
7695          */
7696         if (instance->consistent_mask_64bit)
7697                 kern_sge64 = (struct megasas_sge64 *)
7698                         ((unsigned long)cmd->frame + ioc->sgl_off);
7699         else
7700                 kern_sge32 = (struct megasas_sge32 *)
7701                         ((unsigned long)cmd->frame + ioc->sgl_off);
7702
7703         /*
7704          * For each user buffer, create a mirror buffer and copy in
7705          */
7706         for (i = 0; i < ioc->sge_count; i++) {
7707                 if (!ioc->sgl[i].iov_len)
7708                         continue;
7709
7710                 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
7711                                                     ioc->sgl[i].iov_len,
7712                                                     &buf_handle, GFP_KERNEL);
7713                 if (!kbuff_arr[i]) {
7714                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7715                                "kernel SGL buffer for IOCTL\n");
7716                         error = -ENOMEM;
7717                         goto out;
7718                 }
7719
7720                 /*
7721                  * We don't change the dma_coherent_mask, so
7722                  * dma_alloc_coherent only returns 32bit addresses
7723                  */
7724                 if (instance->consistent_mask_64bit) {
7725                         kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7726                         kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7727                 } else {
7728                         kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7729                         kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7730                 }
7731
7732                 /*
7733                  * We created a kernel buffer corresponding to the
7734                  * user buffer. Now copy in from the user buffer
7735                  */
7736                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7737                                    (u32) (ioc->sgl[i].iov_len))) {
7738                         error = -EFAULT;
7739                         goto out;
7740                 }
7741         }
7742
7743         if (ioc->sense_len) {
7744                 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7745                                              &sense_handle, GFP_KERNEL);
7746                 if (!sense) {
7747                         error = -ENOMEM;
7748                         goto out;
7749                 }
7750
7751                 sense_ptr =
7752                 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
7753                 if (instance->consistent_mask_64bit)
7754                         *sense_ptr = cpu_to_le64(sense_handle);
7755                 else
7756                         *sense_ptr = cpu_to_le32(sense_handle);
7757         }
7758
7759         /*
7760          * Set the sync_cmd flag so that the ISR knows not to complete this
7761          * cmd to the SCSI mid-layer
7762          */
7763         cmd->sync_cmd = 1;
7764         if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7765                 cmd->sync_cmd = 0;
7766                 dev_err(&instance->pdev->dev,
7767                         "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7768                         __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7769                         cmd->cmd_status_drv);
7770                 return -EBUSY;
7771         }
7772
7773         cmd->sync_cmd = 0;
7774
7775         if (instance->unload == 1) {
7776                 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7777                         "don't submit data to application\n");
7778                 goto out;
7779         }
7780         /*
7781          * copy out the kernel buffers to user buffers
7782          */
7783         for (i = 0; i < ioc->sge_count; i++) {
7784                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7785                                  ioc->sgl[i].iov_len)) {
7786                         error = -EFAULT;
7787                         goto out;
7788                 }
7789         }
7790
7791         /*
7792          * copy out the sense
7793          */
7794         if (ioc->sense_len) {
7795                 /*
7796                  * sense_ptr points to the location that has the user
7797                  * sense buffer address
7798                  */
7799                 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7800                                 ioc->sense_off);
7801
7802                 if (copy_to_user((void __user *)((unsigned long)
7803                                  get_unaligned((unsigned long *)sense_ptr)),
7804                                  sense, ioc->sense_len)) {
7805                         dev_err(&instance->pdev->dev, "Failed to copy out to user "
7806                                         "sense data\n");
7807                         error = -EFAULT;
7808                         goto out;
7809                 }
7810         }
7811
7812         /*
7813          * copy the status codes returned by the fw
7814          */
7815         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7816                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
7817                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
7818                 error = -EFAULT;
7819         }
7820
7821 out:
7822         if (sense) {
7823                 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
7824                                     sense, sense_handle);
7825         }
7826
7827         for (i = 0; i < ioc->sge_count; i++) {
7828                 if (kbuff_arr[i]) {
7829                         if (instance->consistent_mask_64bit)
7830                                 dma_free_coherent(&instance->pdev->dev,
7831                                         le32_to_cpu(kern_sge64[i].length),
7832                                         kbuff_arr[i],
7833                                         le64_to_cpu(kern_sge64[i].phys_addr));
7834                         else
7835                                 dma_free_coherent(&instance->pdev->dev,
7836                                         le32_to_cpu(kern_sge32[i].length),
7837                                         kbuff_arr[i],
7838                                         le32_to_cpu(kern_sge32[i].phys_addr));
7839                         kbuff_arr[i] = NULL;
7840                 }
7841         }
7842
7843         megasas_return_cmd(instance, cmd);
7844         return error;
7845 }
7846
7847 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7848 {
7849         struct megasas_iocpacket __user *user_ioc =
7850             (struct megasas_iocpacket __user *)arg;
7851         struct megasas_iocpacket *ioc;
7852         struct megasas_instance *instance;
7853         int error;
7854
7855         ioc = memdup_user(user_ioc, sizeof(*ioc));
7856         if (IS_ERR(ioc))
7857                 return PTR_ERR(ioc);
7858
7859         instance = megasas_lookup_instance(ioc->host_no);
7860         if (!instance) {
7861                 error = -ENODEV;
7862                 goto out_kfree_ioc;
7863         }
7864
7865         /* Block ioctls in VF mode */
7866         if (instance->requestorId && !allow_vf_ioctls) {
7867                 error = -ENODEV;
7868                 goto out_kfree_ioc;
7869         }
7870
7871         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7872                 dev_err(&instance->pdev->dev, "Controller in crit error\n");
7873                 error = -ENODEV;
7874                 goto out_kfree_ioc;
7875         }
7876
7877         if (instance->unload == 1) {
7878                 error = -ENODEV;
7879                 goto out_kfree_ioc;
7880         }
7881
7882         if (down_interruptible(&instance->ioctl_sem)) {
7883                 error = -ERESTARTSYS;
7884                 goto out_kfree_ioc;
7885         }
7886
7887         if  (megasas_wait_for_adapter_operational(instance)) {
7888                 error = -ENODEV;
7889                 goto out_up;
7890         }
7891
7892         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
7893 out_up:
7894         up(&instance->ioctl_sem);
7895
7896 out_kfree_ioc:
7897         kfree(ioc);
7898         return error;
7899 }
7900
7901 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7902 {
7903         struct megasas_instance *instance;
7904         struct megasas_aen aen;
7905         int error;
7906
7907         if (file->private_data != file) {
7908                 printk(KERN_DEBUG "megasas: fasync_helper was not "
7909                        "called first\n");
7910                 return -EINVAL;
7911         }
7912
7913         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7914                 return -EFAULT;
7915
7916         instance = megasas_lookup_instance(aen.host_no);
7917
7918         if (!instance)
7919                 return -ENODEV;
7920
7921         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7922                 return -ENODEV;
7923         }
7924
7925         if (instance->unload == 1) {
7926                 return -ENODEV;
7927         }
7928
7929         if  (megasas_wait_for_adapter_operational(instance))
7930                 return -ENODEV;
7931
7932         mutex_lock(&instance->reset_mutex);
7933         error = megasas_register_aen(instance, aen.seq_num,
7934                                      aen.class_locale_word);
7935         mutex_unlock(&instance->reset_mutex);
7936         return error;
7937 }
7938
7939 /**
7940  * megasas_mgmt_ioctl - char node ioctl entry point
7941  */
7942 static long
7943 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7944 {
7945         switch (cmd) {
7946         case MEGASAS_IOC_FIRMWARE:
7947                 return megasas_mgmt_ioctl_fw(file, arg);
7948
7949         case MEGASAS_IOC_GET_AEN:
7950                 return megasas_mgmt_ioctl_aen(file, arg);
7951         }
7952
7953         return -ENOTTY;
7954 }
7955
7956 #ifdef CONFIG_COMPAT
7957 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7958 {
7959         struct compat_megasas_iocpacket __user *cioc =
7960             (struct compat_megasas_iocpacket __user *)arg;
7961         struct megasas_iocpacket __user *ioc =
7962             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7963         int i;
7964         int error = 0;
7965         compat_uptr_t ptr;
7966         u32 local_sense_off;
7967         u32 local_sense_len;
7968         u32 user_sense_off;
7969
7970         if (clear_user(ioc, sizeof(*ioc)))
7971                 return -EFAULT;
7972
7973         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7974             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7975             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7976             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7977             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7978             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7979                 return -EFAULT;
7980
7981         /*
7982          * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7983          * sense_len is not null, so prepare the 64bit value under
7984          * the same condition.
7985          */
7986         if (get_user(local_sense_off, &ioc->sense_off) ||
7987                 get_user(local_sense_len, &ioc->sense_len) ||
7988                 get_user(user_sense_off, &cioc->sense_off))
7989                 return -EFAULT;
7990
7991         if (local_sense_off != user_sense_off)
7992                 return -EINVAL;
7993
7994         if (local_sense_len) {
7995                 void __user **sense_ioc_ptr =
7996                         (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
7997                 compat_uptr_t *sense_cioc_ptr =
7998                         (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
7999                 if (get_user(ptr, sense_cioc_ptr) ||
8000                     put_user(compat_ptr(ptr), sense_ioc_ptr))
8001                         return -EFAULT;
8002         }
8003
8004         for (i = 0; i < MAX_IOCTL_SGE; i++) {
8005                 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8006                     put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8007                     copy_in_user(&ioc->sgl[i].iov_len,
8008                                  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8009                         return -EFAULT;
8010         }
8011
8012         error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8013
8014         if (copy_in_user(&cioc->frame.hdr.cmd_status,
8015                          &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8016                 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8017                 return -EFAULT;
8018         }
8019         return error;
8020 }
8021
8022 static long
8023 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8024                           unsigned long arg)
8025 {
8026         switch (cmd) {
8027         case MEGASAS_IOC_FIRMWARE32:
8028                 return megasas_mgmt_compat_ioctl_fw(file, arg);
8029         case MEGASAS_IOC_GET_AEN:
8030                 return megasas_mgmt_ioctl_aen(file, arg);
8031         }
8032
8033         return -ENOTTY;
8034 }
8035 #endif
8036
8037 /*
8038  * File operations structure for management interface
8039  */
8040 static const struct file_operations megasas_mgmt_fops = {
8041         .owner = THIS_MODULE,
8042         .open = megasas_mgmt_open,
8043         .fasync = megasas_mgmt_fasync,
8044         .unlocked_ioctl = megasas_mgmt_ioctl,
8045         .poll = megasas_mgmt_poll,
8046 #ifdef CONFIG_COMPAT
8047         .compat_ioctl = megasas_mgmt_compat_ioctl,
8048 #endif
8049         .llseek = noop_llseek,
8050 };
8051
8052 /*
8053  * PCI hotplug support registration structure
8054  */
8055 static struct pci_driver megasas_pci_driver = {
8056
8057         .name = "megaraid_sas",
8058         .id_table = megasas_pci_table,
8059         .probe = megasas_probe_one,
8060         .remove = megasas_detach_one,
8061         .suspend = megasas_suspend,
8062         .resume = megasas_resume,
8063         .shutdown = megasas_shutdown,
8064 };
8065
8066 /*
8067  * Sysfs driver attributes
8068  */
8069 static ssize_t version_show(struct device_driver *dd, char *buf)
8070 {
8071         return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8072                         MEGASAS_VERSION);
8073 }
8074 static DRIVER_ATTR_RO(version);
8075
8076 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8077 {
8078         return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8079                 MEGASAS_RELDATE);
8080 }
8081 static DRIVER_ATTR_RO(release_date);
8082
8083 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8084 {
8085         return sprintf(buf, "%u\n", support_poll_for_event);
8086 }
8087 static DRIVER_ATTR_RO(support_poll_for_event);
8088
8089 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8090 {
8091         return sprintf(buf, "%u\n", support_device_change);
8092 }
8093 static DRIVER_ATTR_RO(support_device_change);
8094
8095 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8096 {
8097         return sprintf(buf, "%u\n", megasas_dbg_lvl);
8098 }
8099
8100 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8101                              size_t count)
8102 {
8103         int retval = count;
8104
8105         if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8106                 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8107                 retval = -EINVAL;
8108         }
8109         return retval;
8110 }
8111 static DRIVER_ATTR_RW(dbg_lvl);
8112
8113 static ssize_t
8114 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8115 {
8116         return sprintf(buf, "%u\n", support_nvme_encapsulation);
8117 }
8118
8119 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8120
8121 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8122 {
8123         sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8124         scsi_remove_device(sdev);
8125         scsi_device_put(sdev);
8126 }
8127
8128 /**
8129  * megasas_update_device_list - Update the PD and LD device list from FW
8130  *                              after an AEN event notification
8131  * @instance:                   Adapter soft state
8132  * @event_type:                 Indicates type of event (PD or LD event)
8133  *
8134  * @return:                     Success or failure
8135  *
8136  * Issue DCMDs to Firmware to update the internal device list in driver.
8137  * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8138  * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8139  */
8140 static
8141 int megasas_update_device_list(struct megasas_instance *instance,
8142                                int event_type)
8143 {
8144         int dcmd_ret = DCMD_SUCCESS;
8145
8146         if (instance->enable_fw_dev_list) {
8147                 dcmd_ret = megasas_host_device_list_query(instance, false);
8148                 if (dcmd_ret != DCMD_SUCCESS)
8149                         goto out;
8150         } else {
8151                 if (event_type & SCAN_PD_CHANNEL) {
8152                         dcmd_ret = megasas_get_pd_list(instance);
8153
8154                         if (dcmd_ret != DCMD_SUCCESS)
8155                                 goto out;
8156                 }
8157
8158                 if (event_type & SCAN_VD_CHANNEL) {
8159                         if (!instance->requestorId ||
8160                             (instance->requestorId &&
8161                              megasas_get_ld_vf_affiliation(instance, 0))) {
8162                                 dcmd_ret = megasas_ld_list_query(instance,
8163                                                 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8164                                 if (dcmd_ret != DCMD_SUCCESS)
8165                                         goto out;
8166                         }
8167                 }
8168         }
8169
8170 out:
8171         return dcmd_ret;
8172 }
8173
8174 /**
8175  * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8176  *                              after an AEN event notification
8177  * @instance:                   Adapter soft state
8178  * @scan_type:                  Indicates type of devices (PD/LD) to add
8179  * @return                      void
8180  */
8181 static
8182 void megasas_add_remove_devices(struct megasas_instance *instance,
8183                                 int scan_type)
8184 {
8185         int i, j;
8186         u16 pd_index = 0;
8187         u16 ld_index = 0;
8188         u16 channel = 0, id = 0;
8189         struct Scsi_Host *host;
8190         struct scsi_device *sdev1;
8191         struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8192         struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8193
8194         host = instance->host;
8195
8196         if (instance->enable_fw_dev_list) {
8197                 targetid_list = instance->host_device_list_buf;
8198                 for (i = 0; i < targetid_list->count; i++) {
8199                         targetid_entry = &targetid_list->host_device_list[i];
8200                         if (targetid_entry->flags.u.bits.is_sys_pd) {
8201                                 channel = le16_to_cpu(targetid_entry->target_id) /
8202                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8203                                 id = le16_to_cpu(targetid_entry->target_id) %
8204                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8205                         } else {
8206                                 channel = MEGASAS_MAX_PD_CHANNELS +
8207                                           (le16_to_cpu(targetid_entry->target_id) /
8208                                            MEGASAS_MAX_DEV_PER_CHANNEL);
8209                                 id = le16_to_cpu(targetid_entry->target_id) %
8210                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8211                         }
8212                         sdev1 = scsi_device_lookup(host, channel, id, 0);
8213                         if (!sdev1) {
8214                                 scsi_add_device(host, channel, id, 0);
8215                         } else {
8216                                 scsi_device_put(sdev1);
8217                         }
8218                 }
8219         }
8220
8221         if (scan_type & SCAN_PD_CHANNEL) {
8222                 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8223                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8224                                 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8225                                 sdev1 = scsi_device_lookup(host, i, j, 0);
8226                                 if (instance->pd_list[pd_index].driveState ==
8227                                                         MR_PD_STATE_SYSTEM) {
8228                                         if (!sdev1)
8229                                                 scsi_add_device(host, i, j, 0);
8230                                         else
8231                                                 scsi_device_put(sdev1);
8232                                 } else {
8233                                         if (sdev1)
8234                                                 megasas_remove_scsi_device(sdev1);
8235                                 }
8236                         }
8237                 }
8238         }
8239
8240         if (scan_type & SCAN_VD_CHANNEL) {
8241                 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8242                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8243                                 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8244                                 sdev1 = scsi_device_lookup(host,
8245                                                 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8246                                 if (instance->ld_ids[ld_index] != 0xff) {
8247                                         if (!sdev1)
8248                                                 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8249                                         else
8250                                                 scsi_device_put(sdev1);
8251                                 } else {
8252                                         if (sdev1)
8253                                                 megasas_remove_scsi_device(sdev1);
8254                                 }
8255                         }
8256                 }
8257         }
8258
8259 }
8260
8261 static void
8262 megasas_aen_polling(struct work_struct *work)
8263 {
8264         struct megasas_aen_event *ev =
8265                 container_of(work, struct megasas_aen_event, hotplug_work.work);
8266         struct megasas_instance *instance = ev->instance;
8267         union megasas_evt_class_locale class_locale;
8268         int event_type = 0;
8269         u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
8270         int error;
8271         u8  dcmd_ret = DCMD_SUCCESS;
8272
8273         if (!instance) {
8274                 printk(KERN_ERR "invalid instance!\n");
8275                 kfree(ev);
8276                 return;
8277         }
8278
8279         /* Adjust event workqueue thread wait time for VF mode */
8280         if (instance->requestorId)
8281                 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8282
8283         /* Don't run the event workqueue thread if OCR is running */
8284         mutex_lock(&instance->reset_mutex);
8285
8286         instance->ev = NULL;
8287         if (instance->evt_detail) {
8288                 megasas_decode_evt(instance);
8289
8290                 switch (le32_to_cpu(instance->evt_detail->code)) {
8291
8292                 case MR_EVT_PD_INSERTED:
8293                 case MR_EVT_PD_REMOVED:
8294                         event_type = SCAN_PD_CHANNEL;
8295                         break;
8296
8297                 case MR_EVT_LD_OFFLINE:
8298                 case MR_EVT_CFG_CLEARED:
8299                 case MR_EVT_LD_DELETED:
8300                 case MR_EVT_LD_CREATED:
8301                         event_type = SCAN_VD_CHANNEL;
8302                         break;
8303
8304                 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8305                 case MR_EVT_FOREIGN_CFG_IMPORTED:
8306                 case MR_EVT_LD_STATE_CHANGE:
8307                         event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8308                         dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8309                                 instance->host->host_no);
8310                         break;
8311
8312                 case MR_EVT_CTRL_PROP_CHANGED:
8313                         dcmd_ret = megasas_get_ctrl_info(instance);
8314                         if (dcmd_ret == DCMD_SUCCESS &&
8315                             instance->snapdump_wait_time) {
8316                                 megasas_get_snapdump_properties(instance);
8317                                 dev_info(&instance->pdev->dev,
8318                                          "Snap dump wait time\t: %d\n",
8319                                          instance->snapdump_wait_time);
8320                         }
8321                         break;
8322                 default:
8323                         event_type = 0;
8324                         break;
8325                 }
8326         } else {
8327                 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8328                 mutex_unlock(&instance->reset_mutex);
8329                 kfree(ev);
8330                 return;
8331         }
8332
8333         if (event_type)
8334                 dcmd_ret = megasas_update_device_list(instance, event_type);
8335
8336         mutex_unlock(&instance->reset_mutex);
8337
8338         if (event_type && dcmd_ret == DCMD_SUCCESS)
8339                 megasas_add_remove_devices(instance, event_type);
8340
8341         if (dcmd_ret == DCMD_SUCCESS)
8342                 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8343         else
8344                 seq_num = instance->last_seq_num;
8345
8346         /* Register AEN with FW for latest sequence number plus 1 */
8347         class_locale.members.reserved = 0;
8348         class_locale.members.locale = MR_EVT_LOCALE_ALL;
8349         class_locale.members.class = MR_EVT_CLASS_DEBUG;
8350
8351         if (instance->aen_cmd != NULL) {
8352                 kfree(ev);
8353                 return;
8354         }
8355
8356         mutex_lock(&instance->reset_mutex);
8357         error = megasas_register_aen(instance, seq_num,
8358                                         class_locale.word);
8359         if (error)
8360                 dev_err(&instance->pdev->dev,
8361                         "register aen failed error %x\n", error);
8362
8363         mutex_unlock(&instance->reset_mutex);
8364         kfree(ev);
8365 }
8366
8367 /**
8368  * megasas_init - Driver load entry point
8369  */
8370 static int __init megasas_init(void)
8371 {
8372         int rval;
8373
8374         /*
8375          * Booted in kdump kernel, minimize memory footprints by
8376          * disabling few features
8377          */
8378         if (reset_devices) {
8379                 msix_vectors = 1;
8380                 rdpq_enable = 0;
8381                 dual_qdepth_disable = 1;
8382         }
8383
8384         /*
8385          * Announce driver version and other information
8386          */
8387         pr_info("megasas: %s\n", MEGASAS_VERSION);
8388
8389         spin_lock_init(&poll_aen_lock);
8390
8391         support_poll_for_event = 2;
8392         support_device_change = 1;
8393         support_nvme_encapsulation = true;
8394
8395         memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8396
8397         /*
8398          * Register character device node
8399          */
8400         rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8401
8402         if (rval < 0) {
8403                 printk(KERN_DEBUG "megasas: failed to open device node\n");
8404                 return rval;
8405         }
8406
8407         megasas_mgmt_majorno = rval;
8408
8409         /*
8410          * Register ourselves as PCI hotplug module
8411          */
8412         rval = pci_register_driver(&megasas_pci_driver);
8413
8414         if (rval) {
8415                 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8416                 goto err_pcidrv;
8417         }
8418
8419         rval = driver_create_file(&megasas_pci_driver.driver,
8420                                   &driver_attr_version);
8421         if (rval)
8422                 goto err_dcf_attr_ver;
8423
8424         rval = driver_create_file(&megasas_pci_driver.driver,
8425                                   &driver_attr_release_date);
8426         if (rval)
8427                 goto err_dcf_rel_date;
8428
8429         rval = driver_create_file(&megasas_pci_driver.driver,
8430                                 &driver_attr_support_poll_for_event);
8431         if (rval)
8432                 goto err_dcf_support_poll_for_event;
8433
8434         rval = driver_create_file(&megasas_pci_driver.driver,
8435                                   &driver_attr_dbg_lvl);
8436         if (rval)
8437                 goto err_dcf_dbg_lvl;
8438         rval = driver_create_file(&megasas_pci_driver.driver,
8439                                 &driver_attr_support_device_change);
8440         if (rval)
8441                 goto err_dcf_support_device_change;
8442
8443         rval = driver_create_file(&megasas_pci_driver.driver,
8444                                   &driver_attr_support_nvme_encapsulation);
8445         if (rval)
8446                 goto err_dcf_support_nvme_encapsulation;
8447
8448         return rval;
8449
8450 err_dcf_support_nvme_encapsulation:
8451         driver_remove_file(&megasas_pci_driver.driver,
8452                            &driver_attr_support_device_change);
8453
8454 err_dcf_support_device_change:
8455         driver_remove_file(&megasas_pci_driver.driver,
8456                            &driver_attr_dbg_lvl);
8457 err_dcf_dbg_lvl:
8458         driver_remove_file(&megasas_pci_driver.driver,
8459                         &driver_attr_support_poll_for_event);
8460 err_dcf_support_poll_for_event:
8461         driver_remove_file(&megasas_pci_driver.driver,
8462                            &driver_attr_release_date);
8463 err_dcf_rel_date:
8464         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8465 err_dcf_attr_ver:
8466         pci_unregister_driver(&megasas_pci_driver);
8467 err_pcidrv:
8468         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8469         return rval;
8470 }
8471
8472 /**
8473  * megasas_exit - Driver unload entry point
8474  */
8475 static void __exit megasas_exit(void)
8476 {
8477         driver_remove_file(&megasas_pci_driver.driver,
8478                            &driver_attr_dbg_lvl);
8479         driver_remove_file(&megasas_pci_driver.driver,
8480                         &driver_attr_support_poll_for_event);
8481         driver_remove_file(&megasas_pci_driver.driver,
8482                         &driver_attr_support_device_change);
8483         driver_remove_file(&megasas_pci_driver.driver,
8484                            &driver_attr_release_date);
8485         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8486         driver_remove_file(&megasas_pci_driver.driver,
8487                            &driver_attr_support_nvme_encapsulation);
8488
8489         pci_unregister_driver(&megasas_pci_driver);
8490         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8491 }
8492
8493 module_init(megasas_init);
8494 module_exit(megasas_exit);