GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Linux MegaRAID driver for SAS based RAID controllers
4  *
5  *  Copyright (c) 2009-2013  LSI Corporation
6  *  Copyright (c) 2013-2016  Avago Technologies
7  *  Copyright (c) 2016-2018  Broadcom Inc.
8  *
9  *  FILE: megaraid_sas_fusion.c
10  *
11  *  Authors: Broadcom Inc.
12  *           Sumant Patro
13  *           Adam Radford
14  *           Kashyap Desai <kashyap.desai@broadcom.com>
15  *           Sumit Saxena <sumit.saxena@broadcom.com>
16  *
17  *  Send feedback to: megaraidlinux.pdl@broadcom.com
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/uaccess.h>
31 #include <linux/fs.h>
32 #include <linux/compat.h>
33 #include <linux/blkdev.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/workqueue.h>
38 #include <linux/irq_poll.h>
39
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_dbg.h>
45 #include <linux/dmi.h>
46
47 #include "megaraid_sas_fusion.h"
48 #include "megaraid_sas.h"
49
50
51 extern void
52 megasas_complete_cmd(struct megasas_instance *instance,
53                      struct megasas_cmd *cmd, u8 alt_status);
54 int
55 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
56               int seconds);
57
58 int
59 megasas_clear_intr_fusion(struct megasas_instance *instance);
60
61 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
62
63 extern u32 megasas_dbg_lvl;
64 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
65                                   int initial);
66 extern struct megasas_mgmt_info megasas_mgmt_info;
67 extern unsigned int resetwaittime;
68 extern unsigned int dual_qdepth_disable;
69 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
70 static void megasas_free_reply_fusion(struct megasas_instance *instance);
71 static inline
72 void megasas_configure_queue_sizes(struct megasas_instance *instance);
73 static void megasas_fusion_crash_dump(struct megasas_instance *instance);
74
75 /**
76  * megasas_adp_reset_wait_for_ready -   initiate chip reset and wait for
77  *                                      controller to come to ready state
78  * @instance:                           adapter's soft state
79  * @do_adp_reset:                       If true, do a chip reset
80  * @ocr_context:                        If called from OCR context this will
81  *                                      be set to 1, else 0
82  *
83  * This function initates a chip reset followed by a wait for controller to
84  * transition to ready state.
85  * During this, driver will block all access to PCI config space from userspace
86  */
87 int
88 megasas_adp_reset_wait_for_ready(struct megasas_instance *instance,
89                                  bool do_adp_reset,
90                                  int ocr_context)
91 {
92         int ret = FAILED;
93
94         /*
95          * Block access to PCI config space from userspace
96          * when diag reset is initiated from driver
97          */
98         if (megasas_dbg_lvl & OCR_DEBUG)
99                 dev_info(&instance->pdev->dev,
100                          "Block access to PCI config space %s %d\n",
101                          __func__, __LINE__);
102
103         pci_cfg_access_lock(instance->pdev);
104
105         if (do_adp_reset) {
106                 if (instance->instancet->adp_reset
107                         (instance, instance->reg_set))
108                         goto out;
109         }
110
111         /* Wait for FW to become ready */
112         if (megasas_transition_to_ready(instance, ocr_context)) {
113                 dev_warn(&instance->pdev->dev,
114                          "Failed to transition controller to ready for scsi%d.\n",
115                          instance->host->host_no);
116                 goto out;
117         }
118
119         ret = SUCCESS;
120 out:
121         if (megasas_dbg_lvl & OCR_DEBUG)
122                 dev_info(&instance->pdev->dev,
123                          "Unlock access to PCI config space %s %d\n",
124                          __func__, __LINE__);
125
126         pci_cfg_access_unlock(instance->pdev);
127
128         return ret;
129 }
130
131 /**
132  * megasas_check_same_4gb_region -      check if allocation
133  *                                      crosses same 4GB boundary or not
134  * @instance:                           adapter's soft instance
135  * @start_addr:                         start address of DMA allocation
136  * @size:                               size of allocation in bytes
137  * @return:                             true : allocation does not cross same
138  *                                      4GB boundary
139  *                                      false: allocation crosses same
140  *                                      4GB boundary
141  */
142 static inline bool megasas_check_same_4gb_region
143         (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
144 {
145         dma_addr_t end_addr;
146
147         end_addr = start_addr + size;
148
149         if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
150                 dev_err(&instance->pdev->dev,
151                         "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
152                         (unsigned long long)start_addr,
153                         (unsigned long long)end_addr);
154                 return false;
155         }
156
157         return true;
158 }
159
160 /**
161  * megasas_enable_intr_fusion - Enables interrupts
162  * @instance:   adapter's soft instance
163  */
164 static void
165 megasas_enable_intr_fusion(struct megasas_instance *instance)
166 {
167         struct megasas_register_set __iomem *regs;
168         regs = instance->reg_set;
169
170         instance->mask_interrupts = 0;
171         /* For Thunderbolt/Invader also clear intr on enable */
172         writel(~0, &regs->outbound_intr_status);
173         readl(&regs->outbound_intr_status);
174
175         writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
176
177         /* Dummy readl to force pci flush */
178         dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
179                  __func__, readl(&regs->outbound_intr_mask));
180 }
181
182 /**
183  * megasas_disable_intr_fusion - Disables interrupt
184  * @instance:   adapter's soft instance
185  */
186 static void
187 megasas_disable_intr_fusion(struct megasas_instance *instance)
188 {
189         u32 mask = 0xFFFFFFFF;
190         struct megasas_register_set __iomem *regs;
191         regs = instance->reg_set;
192         instance->mask_interrupts = 1;
193
194         writel(mask, &regs->outbound_intr_mask);
195         /* Dummy readl to force pci flush */
196         dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
197                  __func__, readl(&regs->outbound_intr_mask));
198 }
199
200 int
201 megasas_clear_intr_fusion(struct megasas_instance *instance)
202 {
203         u32 status;
204         struct megasas_register_set __iomem *regs;
205         regs = instance->reg_set;
206         /*
207          * Check if it is our interrupt
208          */
209         status = megasas_readl(instance,
210                                &regs->outbound_intr_status);
211
212         if (status & 1) {
213                 writel(status, &regs->outbound_intr_status);
214                 readl(&regs->outbound_intr_status);
215                 return 1;
216         }
217         if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
218                 return 0;
219
220         return 1;
221 }
222
223 /**
224  * megasas_get_cmd_fusion -     Get a command from the free pool
225  * @instance:           Adapter soft state
226  * @blk_tag:            Command tag
227  *
228  * Returns a blk_tag indexed mpt frame
229  */
230 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
231                                                   *instance, u32 blk_tag)
232 {
233         struct fusion_context *fusion;
234
235         fusion = instance->ctrl_context;
236         return fusion->cmd_list[blk_tag];
237 }
238
239 /**
240  * megasas_return_cmd_fusion -  Return a cmd to free command pool
241  * @instance:           Adapter soft state
242  * @cmd:                Command packet to be returned to free command pool
243  */
244 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
245         struct megasas_cmd_fusion *cmd)
246 {
247         cmd->scmd = NULL;
248         memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
249         cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
250         cmd->cmd_completed = false;
251 }
252
253 /**
254  * megasas_write_64bit_req_desc -       PCI writes 64bit request descriptor
255  * @instance:                           Adapter soft state
256  * @req_desc:                           64bit Request descriptor
257  */
258 static void
259 megasas_write_64bit_req_desc(struct megasas_instance *instance,
260                 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
261 {
262 #if defined(writeq) && defined(CONFIG_64BIT)
263         u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
264                 le32_to_cpu(req_desc->u.low));
265         writeq(req_data, &instance->reg_set->inbound_low_queue_port);
266 #else
267         unsigned long flags;
268         spin_lock_irqsave(&instance->hba_lock, flags);
269         writel(le32_to_cpu(req_desc->u.low),
270                 &instance->reg_set->inbound_low_queue_port);
271         writel(le32_to_cpu(req_desc->u.high),
272                 &instance->reg_set->inbound_high_queue_port);
273         spin_unlock_irqrestore(&instance->hba_lock, flags);
274 #endif
275 }
276
277 /**
278  * megasas_fire_cmd_fusion -    Sends command to the FW
279  * @instance:                   Adapter soft state
280  * @req_desc:                   32bit or 64bit Request descriptor
281  *
282  * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
283  * Prior to AERO_SERIES support 64 bit Descriptor.
284  */
285 static void
286 megasas_fire_cmd_fusion(struct megasas_instance *instance,
287                 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
288 {
289         if (instance->atomic_desc_support)
290                 writel(le32_to_cpu(req_desc->u.low),
291                         &instance->reg_set->inbound_single_queue_port);
292         else
293                 megasas_write_64bit_req_desc(instance, req_desc);
294 }
295
296 /**
297  * megasas_fusion_update_can_queue -    Do all Adapter Queue depth related calculations here
298  * @instance:           Adapter soft state
299  * @fw_boot_context:    Whether this function called during probe or after OCR
300  *
301  * This function is only for fusion controllers.
302  * Update host can queue, if firmware downgrade max supported firmware commands.
303  * Firmware upgrade case will be skiped because underlying firmware has
304  * more resource than exposed to the OS.
305  *
306  */
307 static void
308 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
309 {
310         u16 cur_max_fw_cmds = 0;
311         u16 ldio_threshold = 0;
312
313         /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
314         if (instance->adapter_type < VENTURA_SERIES)
315                 cur_max_fw_cmds =
316                 megasas_readl(instance,
317                               &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
318
319         if (dual_qdepth_disable || !cur_max_fw_cmds)
320                 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
321         else
322                 ldio_threshold =
323                         (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
324
325         dev_info(&instance->pdev->dev,
326                  "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
327                  cur_max_fw_cmds, ldio_threshold);
328
329         if (fw_boot_context == OCR_CONTEXT) {
330                 cur_max_fw_cmds = cur_max_fw_cmds - 1;
331                 if (cur_max_fw_cmds < instance->max_fw_cmds) {
332                         instance->cur_can_queue =
333                                 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
334                                                 MEGASAS_FUSION_IOCTL_CMDS);
335                         instance->host->can_queue = instance->cur_can_queue;
336                         instance->ldio_threshold = ldio_threshold;
337                 }
338         } else {
339                 instance->max_fw_cmds = cur_max_fw_cmds;
340                 instance->ldio_threshold = ldio_threshold;
341
342                 if (reset_devices)
343                         instance->max_fw_cmds = min(instance->max_fw_cmds,
344                                                 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
345                 /*
346                 * Reduce the max supported cmds by 1. This is to ensure that the
347                 * reply_q_sz (1 more than the max cmd that driver may send)
348                 * does not exceed max cmds that the FW can support
349                 */
350                 instance->max_fw_cmds = instance->max_fw_cmds-1;
351         }
352 }
353
354 static inline void
355 megasas_get_msix_index(struct megasas_instance *instance,
356                        struct scsi_cmnd *scmd,
357                        struct megasas_cmd_fusion *cmd,
358                        u8 data_arms)
359 {
360         int sdev_busy;
361
362         /* nr_hw_queue = 1 for MegaRAID */
363         struct blk_mq_hw_ctx *hctx =
364                 scmd->device->request_queue->queue_hw_ctx[0];
365
366         sdev_busy = atomic_read(&hctx->nr_active);
367
368         if (instance->perf_mode == MR_BALANCED_PERF_MODE &&
369             sdev_busy > (data_arms * MR_DEVICE_HIGH_IOPS_DEPTH))
370                 cmd->request_desc->SCSIIO.MSIxIndex =
371                         mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
372                                         MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
373         else if (instance->msix_load_balance)
374                 cmd->request_desc->SCSIIO.MSIxIndex =
375                         (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
376                                 instance->msix_vectors));
377         else
378                 cmd->request_desc->SCSIIO.MSIxIndex =
379                         instance->reply_map[raw_smp_processor_id()];
380 }
381
382 /**
383  * megasas_free_cmds_fusion -   Free all the cmds in the free cmd pool
384  * @instance:           Adapter soft state
385  */
386 void
387 megasas_free_cmds_fusion(struct megasas_instance *instance)
388 {
389         int i;
390         struct fusion_context *fusion = instance->ctrl_context;
391         struct megasas_cmd_fusion *cmd;
392
393         if (fusion->sense)
394                 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
395                               fusion->sense_phys_addr);
396
397         /* SG */
398         if (fusion->cmd_list) {
399                 for (i = 0; i < instance->max_mpt_cmds; i++) {
400                         cmd = fusion->cmd_list[i];
401                         if (cmd) {
402                                 if (cmd->sg_frame)
403                                         dma_pool_free(fusion->sg_dma_pool,
404                                                       cmd->sg_frame,
405                                                       cmd->sg_frame_phys_addr);
406                         }
407                         kfree(cmd);
408                 }
409                 kfree(fusion->cmd_list);
410         }
411
412         if (fusion->sg_dma_pool) {
413                 dma_pool_destroy(fusion->sg_dma_pool);
414                 fusion->sg_dma_pool = NULL;
415         }
416         if (fusion->sense_dma_pool) {
417                 dma_pool_destroy(fusion->sense_dma_pool);
418                 fusion->sense_dma_pool = NULL;
419         }
420
421
422         /* Reply Frame, Desc*/
423         if (instance->is_rdpq)
424                 megasas_free_rdpq_fusion(instance);
425         else
426                 megasas_free_reply_fusion(instance);
427
428         /* Request Frame, Desc*/
429         if (fusion->req_frames_desc)
430                 dma_free_coherent(&instance->pdev->dev,
431                         fusion->request_alloc_sz, fusion->req_frames_desc,
432                         fusion->req_frames_desc_phys);
433         if (fusion->io_request_frames)
434                 dma_pool_free(fusion->io_request_frames_pool,
435                         fusion->io_request_frames,
436                         fusion->io_request_frames_phys);
437         if (fusion->io_request_frames_pool) {
438                 dma_pool_destroy(fusion->io_request_frames_pool);
439                 fusion->io_request_frames_pool = NULL;
440         }
441 }
442
443 /**
444  * megasas_create_sg_sense_fusion -     Creates DMA pool for cmd frames
445  * @instance:                   Adapter soft state
446  *
447  */
448 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
449 {
450         int i;
451         u16 max_cmd;
452         struct fusion_context *fusion;
453         struct megasas_cmd_fusion *cmd;
454         int sense_sz;
455         u32 offset;
456
457         fusion = instance->ctrl_context;
458         max_cmd = instance->max_fw_cmds;
459         sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
460
461         fusion->sg_dma_pool =
462                         dma_pool_create("mr_sg", &instance->pdev->dev,
463                                 instance->max_chain_frame_sz,
464                                 MR_DEFAULT_NVME_PAGE_SIZE, 0);
465         /* SCSI_SENSE_BUFFERSIZE  = 96 bytes */
466         fusion->sense_dma_pool =
467                         dma_pool_create("mr_sense", &instance->pdev->dev,
468                                 sense_sz, 64, 0);
469
470         if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
471                 dev_err(&instance->pdev->dev,
472                         "Failed from %s %d\n",  __func__, __LINE__);
473                 return -ENOMEM;
474         }
475
476         fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
477                                        GFP_KERNEL, &fusion->sense_phys_addr);
478         if (!fusion->sense) {
479                 dev_err(&instance->pdev->dev,
480                         "failed from %s %d\n",  __func__, __LINE__);
481                 return -ENOMEM;
482         }
483
484         /* sense buffer, request frame and reply desc pool requires to be in
485          * same 4 gb region. Below function will check this.
486          * In case of failure, new pci pool will be created with updated
487          * alignment.
488          * Older allocation and pool will be destroyed.
489          * Alignment will be used such a way that next allocation if success,
490          * will always meet same 4gb region requirement.
491          * Actual requirement is not alignment, but we need start and end of
492          * DMA address must have same upper 32 bit address.
493          */
494
495         if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
496                                            sense_sz)) {
497                 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
498                               fusion->sense_phys_addr);
499                 fusion->sense = NULL;
500                 dma_pool_destroy(fusion->sense_dma_pool);
501
502                 fusion->sense_dma_pool =
503                         dma_pool_create("mr_sense_align", &instance->pdev->dev,
504                                         sense_sz, roundup_pow_of_two(sense_sz),
505                                         0);
506                 if (!fusion->sense_dma_pool) {
507                         dev_err(&instance->pdev->dev,
508                                 "Failed from %s %d\n",  __func__, __LINE__);
509                         return -ENOMEM;
510                 }
511                 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
512                                                GFP_KERNEL,
513                                                &fusion->sense_phys_addr);
514                 if (!fusion->sense) {
515                         dev_err(&instance->pdev->dev,
516                                 "failed from %s %d\n",  __func__, __LINE__);
517                         return -ENOMEM;
518                 }
519         }
520
521         /*
522          * Allocate and attach a frame to each of the commands in cmd_list
523          */
524         for (i = 0; i < max_cmd; i++) {
525                 cmd = fusion->cmd_list[i];
526                 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
527                                         GFP_KERNEL, &cmd->sg_frame_phys_addr);
528
529                 offset = SCSI_SENSE_BUFFERSIZE * i;
530                 cmd->sense = (u8 *)fusion->sense + offset;
531                 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
532
533                 if (!cmd->sg_frame) {
534                         dev_err(&instance->pdev->dev,
535                                 "Failed from %s %d\n",  __func__, __LINE__);
536                         return -ENOMEM;
537                 }
538         }
539
540         /* create sense buffer for the raid 1/10 fp */
541         for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
542                 cmd = fusion->cmd_list[i];
543                 offset = SCSI_SENSE_BUFFERSIZE * i;
544                 cmd->sense = (u8 *)fusion->sense + offset;
545                 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
546
547         }
548
549         return 0;
550 }
551
552 static int
553 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
554 {
555         u32 max_mpt_cmd, i, j;
556         struct fusion_context *fusion;
557
558         fusion = instance->ctrl_context;
559
560         max_mpt_cmd = instance->max_mpt_cmds;
561
562         /*
563          * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
564          * Allocate the dynamic array first and then allocate individual
565          * commands.
566          */
567         fusion->cmd_list =
568                 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
569                         GFP_KERNEL);
570         if (!fusion->cmd_list) {
571                 dev_err(&instance->pdev->dev,
572                         "Failed from %s %d\n",  __func__, __LINE__);
573                 return -ENOMEM;
574         }
575
576         for (i = 0; i < max_mpt_cmd; i++) {
577                 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
578                                               GFP_KERNEL);
579                 if (!fusion->cmd_list[i]) {
580                         for (j = 0; j < i; j++)
581                                 kfree(fusion->cmd_list[j]);
582                         kfree(fusion->cmd_list);
583                         dev_err(&instance->pdev->dev,
584                                 "Failed from %s %d\n",  __func__, __LINE__);
585                         return -ENOMEM;
586                 }
587         }
588
589         return 0;
590 }
591
592 static int
593 megasas_alloc_request_fusion(struct megasas_instance *instance)
594 {
595         struct fusion_context *fusion;
596
597         fusion = instance->ctrl_context;
598
599 retry_alloc:
600         fusion->io_request_frames_pool =
601                         dma_pool_create("mr_ioreq", &instance->pdev->dev,
602                                 fusion->io_frames_alloc_sz, 16, 0);
603
604         if (!fusion->io_request_frames_pool) {
605                 dev_err(&instance->pdev->dev,
606                         "Failed from %s %d\n",  __func__, __LINE__);
607                 return -ENOMEM;
608         }
609
610         fusion->io_request_frames =
611                         dma_pool_alloc(fusion->io_request_frames_pool,
612                                 GFP_KERNEL | __GFP_NOWARN,
613                                 &fusion->io_request_frames_phys);
614         if (!fusion->io_request_frames) {
615                 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
616                         instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
617                         dma_pool_destroy(fusion->io_request_frames_pool);
618                         megasas_configure_queue_sizes(instance);
619                         goto retry_alloc;
620                 } else {
621                         dev_err(&instance->pdev->dev,
622                                 "Failed from %s %d\n",  __func__, __LINE__);
623                         return -ENOMEM;
624                 }
625         }
626
627         if (!megasas_check_same_4gb_region(instance,
628                                            fusion->io_request_frames_phys,
629                                            fusion->io_frames_alloc_sz)) {
630                 dma_pool_free(fusion->io_request_frames_pool,
631                               fusion->io_request_frames,
632                               fusion->io_request_frames_phys);
633                 fusion->io_request_frames = NULL;
634                 dma_pool_destroy(fusion->io_request_frames_pool);
635
636                 fusion->io_request_frames_pool =
637                         dma_pool_create("mr_ioreq_align",
638                                         &instance->pdev->dev,
639                                         fusion->io_frames_alloc_sz,
640                                         roundup_pow_of_two(fusion->io_frames_alloc_sz),
641                                         0);
642
643                 if (!fusion->io_request_frames_pool) {
644                         dev_err(&instance->pdev->dev,
645                                 "Failed from %s %d\n",  __func__, __LINE__);
646                         return -ENOMEM;
647                 }
648
649                 fusion->io_request_frames =
650                         dma_pool_alloc(fusion->io_request_frames_pool,
651                                        GFP_KERNEL | __GFP_NOWARN,
652                                        &fusion->io_request_frames_phys);
653
654                 if (!fusion->io_request_frames) {
655                         dev_err(&instance->pdev->dev,
656                                 "Failed from %s %d\n",  __func__, __LINE__);
657                         return -ENOMEM;
658                 }
659         }
660
661         fusion->req_frames_desc =
662                 dma_alloc_coherent(&instance->pdev->dev,
663                                    fusion->request_alloc_sz,
664                                    &fusion->req_frames_desc_phys, GFP_KERNEL);
665         if (!fusion->req_frames_desc) {
666                 dev_err(&instance->pdev->dev,
667                         "Failed from %s %d\n",  __func__, __LINE__);
668                 return -ENOMEM;
669         }
670
671         return 0;
672 }
673
674 static int
675 megasas_alloc_reply_fusion(struct megasas_instance *instance)
676 {
677         int i, count;
678         struct fusion_context *fusion;
679         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
680         fusion = instance->ctrl_context;
681
682         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
683         fusion->reply_frames_desc_pool =
684                         dma_pool_create("mr_reply", &instance->pdev->dev,
685                                 fusion->reply_alloc_sz * count, 16, 0);
686
687         if (!fusion->reply_frames_desc_pool) {
688                 dev_err(&instance->pdev->dev,
689                         "Failed from %s %d\n",  __func__, __LINE__);
690                 return -ENOMEM;
691         }
692
693         fusion->reply_frames_desc[0] =
694                 dma_pool_alloc(fusion->reply_frames_desc_pool,
695                         GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
696         if (!fusion->reply_frames_desc[0]) {
697                 dev_err(&instance->pdev->dev,
698                         "Failed from %s %d\n",  __func__, __LINE__);
699                 return -ENOMEM;
700         }
701
702         if (!megasas_check_same_4gb_region(instance,
703                                            fusion->reply_frames_desc_phys[0],
704                                            (fusion->reply_alloc_sz * count))) {
705                 dma_pool_free(fusion->reply_frames_desc_pool,
706                               fusion->reply_frames_desc[0],
707                               fusion->reply_frames_desc_phys[0]);
708                 fusion->reply_frames_desc[0] = NULL;
709                 dma_pool_destroy(fusion->reply_frames_desc_pool);
710
711                 fusion->reply_frames_desc_pool =
712                         dma_pool_create("mr_reply_align",
713                                         &instance->pdev->dev,
714                                         fusion->reply_alloc_sz * count,
715                                         roundup_pow_of_two(fusion->reply_alloc_sz * count),
716                                         0);
717
718                 if (!fusion->reply_frames_desc_pool) {
719                         dev_err(&instance->pdev->dev,
720                                 "Failed from %s %d\n",  __func__, __LINE__);
721                         return -ENOMEM;
722                 }
723
724                 fusion->reply_frames_desc[0] =
725                         dma_pool_alloc(fusion->reply_frames_desc_pool,
726                                        GFP_KERNEL,
727                                        &fusion->reply_frames_desc_phys[0]);
728
729                 if (!fusion->reply_frames_desc[0]) {
730                         dev_err(&instance->pdev->dev,
731                                 "Failed from %s %d\n",  __func__, __LINE__);
732                         return -ENOMEM;
733                 }
734         }
735
736         reply_desc = fusion->reply_frames_desc[0];
737         for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
738                 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
739
740         /* This is not a rdpq mode, but driver still populate
741          * reply_frame_desc array to use same msix index in ISR path.
742          */
743         for (i = 0; i < (count - 1); i++)
744                 fusion->reply_frames_desc[i + 1] =
745                         fusion->reply_frames_desc[i] +
746                         (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
747
748         return 0;
749 }
750
751 static int
752 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
753 {
754         int i, j, k, msix_count;
755         struct fusion_context *fusion;
756         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
757         union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
758         dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
759         u8 dma_alloc_count, abs_index;
760         u32 chunk_size, array_size, offset;
761
762         fusion = instance->ctrl_context;
763         chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
764         array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
765                      MAX_MSIX_QUEUES_FUSION;
766
767         fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
768                                                array_size, &fusion->rdpq_phys,
769                                                GFP_KERNEL);
770         if (!fusion->rdpq_virt) {
771                 dev_err(&instance->pdev->dev,
772                         "Failed from %s %d\n",  __func__, __LINE__);
773                 return -ENOMEM;
774         }
775
776         msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
777
778         fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
779                                                          &instance->pdev->dev,
780                                                          chunk_size, 16, 0);
781         fusion->reply_frames_desc_pool_align =
782                                 dma_pool_create("mr_rdpq_align",
783                                                 &instance->pdev->dev,
784                                                 chunk_size,
785                                                 roundup_pow_of_two(chunk_size),
786                                                 0);
787
788         if (!fusion->reply_frames_desc_pool ||
789             !fusion->reply_frames_desc_pool_align) {
790                 dev_err(&instance->pdev->dev,
791                         "Failed from %s %d\n",  __func__, __LINE__);
792                 return -ENOMEM;
793         }
794
795 /*
796  * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
797  * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
798  * within 4GB boundary and also reply queues in a set must have same
799  * upper 32-bits in their memory address. so here driver is allocating the
800  * DMA'able memory for reply queues according. Driver uses limitation of
801  * VENTURA_SERIES to manage INVADER_SERIES as well.
802  */
803         dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
804
805         for (i = 0; i < dma_alloc_count; i++) {
806                 rdpq_chunk_virt[i] =
807                         dma_pool_alloc(fusion->reply_frames_desc_pool,
808                                        GFP_KERNEL, &rdpq_chunk_phys[i]);
809                 if (!rdpq_chunk_virt[i]) {
810                         dev_err(&instance->pdev->dev,
811                                 "Failed from %s %d\n",  __func__, __LINE__);
812                         return -ENOMEM;
813                 }
814                 /* reply desc pool requires to be in same 4 gb region.
815                  * Below function will check this.
816                  * In case of failure, new pci pool will be created with updated
817                  * alignment.
818                  * For RDPQ buffers, driver always allocate two separate pci pool.
819                  * Alignment will be used such a way that next allocation if
820                  * success, will always meet same 4gb region requirement.
821                  * rdpq_tracker keep track of each buffer's physical,
822                  * virtual address and pci pool descriptor. It will help driver
823                  * while freeing the resources.
824                  *
825                  */
826                 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
827                                                    chunk_size)) {
828                         dma_pool_free(fusion->reply_frames_desc_pool,
829                                       rdpq_chunk_virt[i],
830                                       rdpq_chunk_phys[i]);
831
832                         rdpq_chunk_virt[i] =
833                                 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
834                                                GFP_KERNEL, &rdpq_chunk_phys[i]);
835                         if (!rdpq_chunk_virt[i]) {
836                                 dev_err(&instance->pdev->dev,
837                                         "Failed from %s %d\n",
838                                         __func__, __LINE__);
839                                 return -ENOMEM;
840                         }
841                         fusion->rdpq_tracker[i].dma_pool_ptr =
842                                         fusion->reply_frames_desc_pool_align;
843                 } else {
844                         fusion->rdpq_tracker[i].dma_pool_ptr =
845                                         fusion->reply_frames_desc_pool;
846                 }
847
848                 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
849                 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
850         }
851
852         for (k = 0; k < dma_alloc_count; k++) {
853                 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
854                         abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
855
856                         if (abs_index == msix_count)
857                                 break;
858                         offset = fusion->reply_alloc_sz * i;
859                         fusion->rdpq_virt[abs_index].RDPQBaseAddress =
860                                         cpu_to_le64(rdpq_chunk_phys[k] + offset);
861                         fusion->reply_frames_desc_phys[abs_index] =
862                                         rdpq_chunk_phys[k] + offset;
863                         fusion->reply_frames_desc[abs_index] =
864                                         (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
865
866                         reply_desc = fusion->reply_frames_desc[abs_index];
867                         for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
868                                 reply_desc->Words = ULLONG_MAX;
869                 }
870         }
871
872         return 0;
873 }
874
875 static void
876 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
877
878         int i;
879         struct fusion_context *fusion;
880
881         fusion = instance->ctrl_context;
882
883         for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
884                 if (fusion->rdpq_tracker[i].pool_entry_virt)
885                         dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
886                                       fusion->rdpq_tracker[i].pool_entry_virt,
887                                       fusion->rdpq_tracker[i].pool_entry_phys);
888
889         }
890
891         dma_pool_destroy(fusion->reply_frames_desc_pool);
892         dma_pool_destroy(fusion->reply_frames_desc_pool_align);
893
894         if (fusion->rdpq_virt)
895                 dma_free_coherent(&instance->pdev->dev,
896                         sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
897                         fusion->rdpq_virt, fusion->rdpq_phys);
898 }
899
900 static void
901 megasas_free_reply_fusion(struct megasas_instance *instance) {
902
903         struct fusion_context *fusion;
904
905         fusion = instance->ctrl_context;
906
907         if (fusion->reply_frames_desc[0])
908                 dma_pool_free(fusion->reply_frames_desc_pool,
909                         fusion->reply_frames_desc[0],
910                         fusion->reply_frames_desc_phys[0]);
911
912         dma_pool_destroy(fusion->reply_frames_desc_pool);
913
914 }
915
916
917 /**
918  * megasas_alloc_cmds_fusion -  Allocates the command packets
919  * @instance:           Adapter soft state
920  *
921  *
922  * Each frame has a 32-bit field called context. This context is used to get
923  * back the megasas_cmd_fusion from the frame when a frame gets completed
924  * In this driver, the 32 bit values are the indices into an array cmd_list.
925  * This array is used only to look up the megasas_cmd_fusion given the context.
926  * The free commands themselves are maintained in a linked list called cmd_pool.
927  *
928  * cmds are formed in the io_request and sg_frame members of the
929  * megasas_cmd_fusion. The context field is used to get a request descriptor
930  * and is used as SMID of the cmd.
931  * SMID value range is from 1 to max_fw_cmds.
932  */
933 static int
934 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
935 {
936         int i;
937         struct fusion_context *fusion;
938         struct megasas_cmd_fusion *cmd;
939         u32 offset;
940         dma_addr_t io_req_base_phys;
941         u8 *io_req_base;
942
943
944         fusion = instance->ctrl_context;
945
946         if (megasas_alloc_request_fusion(instance))
947                 goto fail_exit;
948
949         if (instance->is_rdpq) {
950                 if (megasas_alloc_rdpq_fusion(instance))
951                         goto fail_exit;
952         } else
953                 if (megasas_alloc_reply_fusion(instance))
954                         goto fail_exit;
955
956         if (megasas_alloc_cmdlist_fusion(instance))
957                 goto fail_exit;
958
959         dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
960                  instance->max_fw_cmds);
961
962         /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
963         io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
964         io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
965
966         /*
967          * Add all the commands to command pool (fusion->cmd_pool)
968          */
969
970         /* SMID 0 is reserved. Set SMID/index from 1 */
971         for (i = 0; i < instance->max_mpt_cmds; i++) {
972                 cmd = fusion->cmd_list[i];
973                 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
974                 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
975                 cmd->index = i + 1;
976                 cmd->scmd = NULL;
977                 cmd->sync_cmd_idx =
978                 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
979                                 (i - instance->max_scsi_cmds) :
980                                 (u32)ULONG_MAX; /* Set to Invalid */
981                 cmd->instance = instance;
982                 cmd->io_request =
983                         (struct MPI2_RAID_SCSI_IO_REQUEST *)
984                   (io_req_base + offset);
985                 memset(cmd->io_request, 0,
986                        sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
987                 cmd->io_request_phys_addr = io_req_base_phys + offset;
988                 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
989         }
990
991         if (megasas_create_sg_sense_fusion(instance))
992                 goto fail_exit;
993
994         return 0;
995
996 fail_exit:
997         megasas_free_cmds_fusion(instance);
998         return -ENOMEM;
999 }
1000
1001 /**
1002  * wait_and_poll -      Issues a polling command
1003  * @instance:                   Adapter soft state
1004  * @cmd:                        Command packet to be issued
1005  * @seconds:                    Maximum poll time
1006  *
1007  * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
1008  */
1009 int
1010 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
1011         int seconds)
1012 {
1013         int i;
1014         struct megasas_header *frame_hdr = &cmd->frame->hdr;
1015         u32 status_reg;
1016
1017         u32 msecs = seconds * 1000;
1018
1019         /*
1020          * Wait for cmd_status to change
1021          */
1022         for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
1023                 rmb();
1024                 msleep(20);
1025                 if (!(i % 5000)) {
1026                         status_reg = instance->instancet->read_fw_status_reg(instance)
1027                                         & MFI_STATE_MASK;
1028                         if (status_reg == MFI_STATE_FAULT)
1029                                 break;
1030                 }
1031         }
1032
1033         if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
1034                 return DCMD_TIMEOUT;
1035         else if (frame_hdr->cmd_status == MFI_STAT_OK)
1036                 return DCMD_SUCCESS;
1037         else
1038                 return DCMD_FAILED;
1039 }
1040
1041 /**
1042  * megasas_ioc_init_fusion -    Initializes the FW
1043  * @instance:           Adapter soft state
1044  *
1045  * Issues the IOC Init cmd
1046  */
1047 int
1048 megasas_ioc_init_fusion(struct megasas_instance *instance)
1049 {
1050         struct megasas_init_frame *init_frame;
1051         struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
1052         dma_addr_t      ioc_init_handle;
1053         struct megasas_cmd *cmd;
1054         u8 ret, cur_rdpq_mode;
1055         struct fusion_context *fusion;
1056         union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
1057         int i;
1058         struct megasas_header *frame_hdr;
1059         const char *sys_info;
1060         MFI_CAPABILITIES *drv_ops;
1061         u32 scratch_pad_1;
1062         ktime_t time;
1063         bool cur_fw_64bit_dma_capable;
1064         bool cur_intr_coalescing;
1065
1066         fusion = instance->ctrl_context;
1067
1068         ioc_init_handle = fusion->ioc_init_request_phys;
1069         IOCInitMessage = fusion->ioc_init_request;
1070
1071         cmd = fusion->ioc_init_cmd;
1072
1073         scratch_pad_1 = megasas_readl
1074                 (instance, &instance->reg_set->outbound_scratch_pad_1);
1075
1076         cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
1077
1078         if (instance->adapter_type == INVADER_SERIES) {
1079                 cur_fw_64bit_dma_capable =
1080                         (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
1081
1082                 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1083                         dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1084                                 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1085                         megaraid_sas_kill_hba(instance);
1086                         ret = 1;
1087                         goto fail_fw_init;
1088                 }
1089         }
1090
1091         if (instance->is_rdpq && !cur_rdpq_mode) {
1092                 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1093                         " from RDPQ mode to non RDPQ mode\n");
1094                 ret = 1;
1095                 goto fail_fw_init;
1096         }
1097
1098         cur_intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
1099                                                         true : false;
1100
1101         if ((instance->low_latency_index_start ==
1102                 MR_HIGH_IOPS_QUEUE_COUNT) && cur_intr_coalescing)
1103                 instance->perf_mode = MR_BALANCED_PERF_MODE;
1104
1105         dev_info(&instance->pdev->dev, "Performance mode :%s\n",
1106                 MEGASAS_PERF_MODE_2STR(instance->perf_mode));
1107
1108         instance->fw_sync_cache_support = (scratch_pad_1 &
1109                 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1110         dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1111                  instance->fw_sync_cache_support ? "Yes" : "No");
1112
1113         memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1114
1115         IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1116         IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1117         IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1118         IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1119         IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1120
1121         IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1122         IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1123                         cpu_to_le64(fusion->rdpq_phys) :
1124                         cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1125         IOCInitMessage->MsgFlags = instance->is_rdpq ?
1126                         MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1127         IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1128         IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1129         IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1130         IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1131
1132         time = ktime_get_real();
1133         /* Convert to milliseconds as per FW requirement */
1134         IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1135
1136         init_frame = (struct megasas_init_frame *)cmd->frame;
1137         memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1138
1139         frame_hdr = &cmd->frame->hdr;
1140         frame_hdr->cmd_status = 0xFF;
1141         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1142
1143         init_frame->cmd = MFI_CMD_INIT;
1144         init_frame->cmd_status = 0xFF;
1145
1146         drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1147
1148         /* driver support Extended MSIX */
1149         if (instance->adapter_type >= INVADER_SERIES)
1150                 drv_ops->mfi_capabilities.support_additional_msix = 1;
1151         /* driver supports HA / Remote LUN over Fast Path interface */
1152         drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1153
1154         drv_ops->mfi_capabilities.support_max_255lds = 1;
1155         drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1156         drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1157
1158         if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1159                 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1160
1161         drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1162         if (!dual_qdepth_disable)
1163                 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1164
1165         drv_ops->mfi_capabilities.support_qd_throttling = 1;
1166         drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1167         drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1168         drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
1169
1170         if (instance->consistent_mask_64bit)
1171                 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1172
1173         /* Convert capability to LE32 */
1174         cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1175
1176         sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1177         if (instance->system_info_buf && sys_info) {
1178                 memcpy(instance->system_info_buf->systemId, sys_info,
1179                         strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1180                 instance->system_info_buf->systemIdLength =
1181                         strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1182                 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1183                 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1184         }
1185
1186         init_frame->queue_info_new_phys_addr_hi =
1187                 cpu_to_le32(upper_32_bits(ioc_init_handle));
1188         init_frame->queue_info_new_phys_addr_lo =
1189                 cpu_to_le32(lower_32_bits(ioc_init_handle));
1190         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1191
1192         /*
1193          * Each bit in replyqueue_mask represents one group of MSI-x vectors
1194          * (each group has 8 vectors)
1195          */
1196         switch (instance->perf_mode) {
1197         case MR_BALANCED_PERF_MODE:
1198                 init_frame->replyqueue_mask =
1199                        cpu_to_le16(~(~0 << instance->low_latency_index_start/8));
1200                 break;
1201         case MR_IOPS_PERF_MODE:
1202                 init_frame->replyqueue_mask =
1203                        cpu_to_le16(~(~0 << instance->msix_vectors/8));
1204                 break;
1205         }
1206
1207
1208         req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1209         req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1210         req_desc.MFAIo.RequestFlags =
1211                 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1212                 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1213
1214         /*
1215          * disable the intr before firing the init frame
1216          */
1217         instance->instancet->disable_intr(instance);
1218
1219         for (i = 0; i < (10 * 1000); i += 20) {
1220                 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1221                         msleep(20);
1222                 else
1223                         break;
1224         }
1225
1226         /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1227         megasas_write_64bit_req_desc(instance, &req_desc);
1228
1229         wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1230
1231         frame_hdr = &cmd->frame->hdr;
1232         if (frame_hdr->cmd_status != 0) {
1233                 ret = 1;
1234                 goto fail_fw_init;
1235         }
1236
1237         if (instance->adapter_type >= AERO_SERIES) {
1238                 scratch_pad_1 = megasas_readl
1239                         (instance, &instance->reg_set->outbound_scratch_pad_1);
1240
1241                 instance->atomic_desc_support =
1242                         (scratch_pad_1 & MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
1243
1244                 dev_info(&instance->pdev->dev, "FW supports atomic descriptor\t: %s\n",
1245                         instance->atomic_desc_support ? "Yes" : "No");
1246         }
1247
1248         return 0;
1249
1250 fail_fw_init:
1251         dev_err(&instance->pdev->dev,
1252                 "Init cmd return status FAILED for SCSI host %d\n",
1253                 instance->host->host_no);
1254
1255         return ret;
1256 }
1257
1258 /**
1259  * megasas_sync_pd_seq_num -    JBOD SEQ MAP
1260  * @instance:           Adapter soft state
1261  * @pend:               set to 1, if it is pended jbod map.
1262  *
1263  * Issue Jbod map to the firmware. If it is pended command,
1264  * issue command and return. If it is first instance of jbod map
1265  * issue and receive command.
1266  */
1267 int
1268 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1269         int ret = 0;
1270         size_t pd_seq_map_sz;
1271         struct megasas_cmd *cmd;
1272         struct megasas_dcmd_frame *dcmd;
1273         struct fusion_context *fusion = instance->ctrl_context;
1274         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1275         dma_addr_t pd_seq_h;
1276
1277         pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1278         pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1279         pd_seq_map_sz = struct_size(pd_sync, seq, MAX_PHYSICAL_DEVICES - 1);
1280
1281         cmd = megasas_get_cmd(instance);
1282         if (!cmd) {
1283                 dev_err(&instance->pdev->dev,
1284                         "Could not get mfi cmd. Fail from %s %d\n",
1285                         __func__, __LINE__);
1286                 return -ENOMEM;
1287         }
1288
1289         dcmd = &cmd->frame->dcmd;
1290
1291         memset(pd_sync, 0, pd_seq_map_sz);
1292         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1293
1294         if (pend) {
1295                 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1296                 dcmd->flags = MFI_FRAME_DIR_WRITE;
1297                 instance->jbod_seq_cmd = cmd;
1298         } else {
1299                 dcmd->flags = MFI_FRAME_DIR_READ;
1300         }
1301
1302         dcmd->cmd = MFI_CMD_DCMD;
1303         dcmd->cmd_status = 0xFF;
1304         dcmd->sge_count = 1;
1305         dcmd->timeout = 0;
1306         dcmd->pad_0 = 0;
1307         dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1308         dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1309
1310         megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1311
1312         if (pend) {
1313                 instance->instancet->issue_dcmd(instance, cmd);
1314                 return 0;
1315         }
1316
1317         /* Below code is only for non pended DCMD */
1318         if (!instance->mask_interrupts)
1319                 ret = megasas_issue_blocked_cmd(instance, cmd,
1320                         MFI_IO_TIMEOUT_SECS);
1321         else
1322                 ret = megasas_issue_polled(instance, cmd);
1323
1324         if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1325                 dev_warn(&instance->pdev->dev,
1326                         "driver supports max %d JBOD, but FW reports %d\n",
1327                         MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1328                 ret = -EINVAL;
1329         }
1330
1331         if (ret == DCMD_TIMEOUT)
1332                 dev_warn(&instance->pdev->dev,
1333                          "%s DCMD timed out, continue without JBOD sequence map\n",
1334                          __func__);
1335
1336         if (ret == DCMD_SUCCESS)
1337                 instance->pd_seq_map_id++;
1338
1339         megasas_return_cmd(instance, cmd);
1340         return ret;
1341 }
1342
1343 /*
1344  * megasas_get_ld_map_info -    Returns FW's ld_map structure
1345  * @instance:                           Adapter soft state
1346  * @pend:                               Pend the command or not
1347  * Issues an internal command (DCMD) to get the FW's controller PD
1348  * list structure.  This information is mainly used to find out SYSTEM
1349  * supported by the FW.
1350  * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1351  * dcmd.mbox.b[0]       - number of LDs being sync'd
1352  * dcmd.mbox.b[1]       - 0 - complete command immediately.
1353  *                      - 1 - pend till config change
1354  * dcmd.mbox.b[2]       - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1355  *                      - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1356  *                              uses extended struct MR_FW_RAID_MAP_EXT
1357  */
1358 static int
1359 megasas_get_ld_map_info(struct megasas_instance *instance)
1360 {
1361         int ret = 0;
1362         struct megasas_cmd *cmd;
1363         struct megasas_dcmd_frame *dcmd;
1364         void *ci;
1365         dma_addr_t ci_h = 0;
1366         u32 size_map_info;
1367         struct fusion_context *fusion;
1368
1369         cmd = megasas_get_cmd(instance);
1370
1371         if (!cmd) {
1372                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1373                 return -ENOMEM;
1374         }
1375
1376         fusion = instance->ctrl_context;
1377
1378         if (!fusion) {
1379                 megasas_return_cmd(instance, cmd);
1380                 return -ENXIO;
1381         }
1382
1383         dcmd = &cmd->frame->dcmd;
1384
1385         size_map_info = fusion->current_map_sz;
1386
1387         ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1388         ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1389
1390         if (!ci) {
1391                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1392                 megasas_return_cmd(instance, cmd);
1393                 return -ENOMEM;
1394         }
1395
1396         memset(ci, 0, fusion->max_map_sz);
1397         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1398         dcmd->cmd = MFI_CMD_DCMD;
1399         dcmd->cmd_status = 0xFF;
1400         dcmd->sge_count = 1;
1401         dcmd->flags = MFI_FRAME_DIR_READ;
1402         dcmd->timeout = 0;
1403         dcmd->pad_0 = 0;
1404         dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1405         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1406
1407         megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1408
1409         if (!instance->mask_interrupts)
1410                 ret = megasas_issue_blocked_cmd(instance, cmd,
1411                         MFI_IO_TIMEOUT_SECS);
1412         else
1413                 ret = megasas_issue_polled(instance, cmd);
1414
1415         if (ret == DCMD_TIMEOUT)
1416                 dev_warn(&instance->pdev->dev,
1417                          "%s DCMD timed out, RAID map is disabled\n",
1418                          __func__);
1419
1420         megasas_return_cmd(instance, cmd);
1421
1422         return ret;
1423 }
1424
1425 u8
1426 megasas_get_map_info(struct megasas_instance *instance)
1427 {
1428         struct fusion_context *fusion = instance->ctrl_context;
1429
1430         fusion->fast_path_io = 0;
1431         if (!megasas_get_ld_map_info(instance)) {
1432                 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1433                         fusion->fast_path_io = 1;
1434                         return 0;
1435                 }
1436         }
1437         return 1;
1438 }
1439
1440 /*
1441  * megasas_sync_map_info -      Returns FW's ld_map structure
1442  * @instance:                           Adapter soft state
1443  *
1444  * Issues an internal command (DCMD) to get the FW's controller PD
1445  * list structure.  This information is mainly used to find out SYSTEM
1446  * supported by the FW.
1447  */
1448 int
1449 megasas_sync_map_info(struct megasas_instance *instance)
1450 {
1451         int i;
1452         struct megasas_cmd *cmd;
1453         struct megasas_dcmd_frame *dcmd;
1454         u16 num_lds;
1455         struct fusion_context *fusion;
1456         struct MR_LD_TARGET_SYNC *ci = NULL;
1457         struct MR_DRV_RAID_MAP_ALL *map;
1458         struct MR_LD_RAID  *raid;
1459         struct MR_LD_TARGET_SYNC *ld_sync;
1460         dma_addr_t ci_h = 0;
1461         u32 size_map_info;
1462
1463         cmd = megasas_get_cmd(instance);
1464
1465         if (!cmd) {
1466                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1467                 return -ENOMEM;
1468         }
1469
1470         fusion = instance->ctrl_context;
1471
1472         if (!fusion) {
1473                 megasas_return_cmd(instance, cmd);
1474                 return 1;
1475         }
1476
1477         map = fusion->ld_drv_map[instance->map_id & 1];
1478
1479         num_lds = le16_to_cpu(map->raidMap.ldCount);
1480
1481         dcmd = &cmd->frame->dcmd;
1482
1483         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1484
1485         ci = (struct MR_LD_TARGET_SYNC *)
1486           fusion->ld_map[(instance->map_id - 1) & 1];
1487         memset(ci, 0, fusion->max_map_sz);
1488
1489         ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1490
1491         ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1492
1493         for (i = 0; i < num_lds; i++, ld_sync++) {
1494                 raid = MR_LdRaidGet(i, map);
1495                 ld_sync->targetId = MR_GetLDTgtId(i, map);
1496                 ld_sync->seqNum = raid->seqNum;
1497         }
1498
1499         size_map_info = fusion->current_map_sz;
1500
1501         dcmd->cmd = MFI_CMD_DCMD;
1502         dcmd->cmd_status = 0xFF;
1503         dcmd->sge_count = 1;
1504         dcmd->flags = MFI_FRAME_DIR_WRITE;
1505         dcmd->timeout = 0;
1506         dcmd->pad_0 = 0;
1507         dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1508         dcmd->mbox.b[0] = num_lds;
1509         dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1510         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1511
1512         megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1513
1514         instance->map_update_cmd = cmd;
1515
1516         instance->instancet->issue_dcmd(instance, cmd);
1517
1518         return 0;
1519 }
1520
1521 /*
1522  * meagasas_display_intel_branding - Display branding string
1523  * @instance: per adapter object
1524  *
1525  * Return nothing.
1526  */
1527 static void
1528 megasas_display_intel_branding(struct megasas_instance *instance)
1529 {
1530         if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1531                 return;
1532
1533         switch (instance->pdev->device) {
1534         case PCI_DEVICE_ID_LSI_INVADER:
1535                 switch (instance->pdev->subsystem_device) {
1536                 case MEGARAID_INTEL_RS3DC080_SSDID:
1537                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1538                                 instance->host->host_no,
1539                                 MEGARAID_INTEL_RS3DC080_BRANDING);
1540                         break;
1541                 case MEGARAID_INTEL_RS3DC040_SSDID:
1542                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1543                                 instance->host->host_no,
1544                                 MEGARAID_INTEL_RS3DC040_BRANDING);
1545                         break;
1546                 case MEGARAID_INTEL_RS3SC008_SSDID:
1547                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1548                                 instance->host->host_no,
1549                                 MEGARAID_INTEL_RS3SC008_BRANDING);
1550                         break;
1551                 case MEGARAID_INTEL_RS3MC044_SSDID:
1552                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1553                                 instance->host->host_no,
1554                                 MEGARAID_INTEL_RS3MC044_BRANDING);
1555                         break;
1556                 default:
1557                         break;
1558                 }
1559                 break;
1560         case PCI_DEVICE_ID_LSI_FURY:
1561                 switch (instance->pdev->subsystem_device) {
1562                 case MEGARAID_INTEL_RS3WC080_SSDID:
1563                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1564                                 instance->host->host_no,
1565                                 MEGARAID_INTEL_RS3WC080_BRANDING);
1566                         break;
1567                 case MEGARAID_INTEL_RS3WC040_SSDID:
1568                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1569                                 instance->host->host_no,
1570                                 MEGARAID_INTEL_RS3WC040_BRANDING);
1571                         break;
1572                 default:
1573                         break;
1574                 }
1575                 break;
1576         case PCI_DEVICE_ID_LSI_CUTLASS_52:
1577         case PCI_DEVICE_ID_LSI_CUTLASS_53:
1578                 switch (instance->pdev->subsystem_device) {
1579                 case MEGARAID_INTEL_RMS3BC160_SSDID:
1580                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1581                                 instance->host->host_no,
1582                                 MEGARAID_INTEL_RMS3BC160_BRANDING);
1583                         break;
1584                 default:
1585                         break;
1586                 }
1587                 break;
1588         default:
1589                 break;
1590         }
1591 }
1592
1593 /**
1594  * megasas_allocate_raid_maps - Allocate memory for RAID maps
1595  * @instance:                           Adapter soft state
1596  *
1597  * return:                              if success: return 0
1598  *                                      failed:  return -ENOMEM
1599  */
1600 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1601 {
1602         struct fusion_context *fusion;
1603         int i = 0;
1604
1605         fusion = instance->ctrl_context;
1606
1607         fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1608
1609         for (i = 0; i < 2; i++) {
1610                 fusion->ld_map[i] = NULL;
1611
1612                 fusion->ld_drv_map[i] = (void *)
1613                         __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1614                                          fusion->drv_map_pages);
1615
1616                 if (!fusion->ld_drv_map[i]) {
1617                         fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1618
1619                         if (!fusion->ld_drv_map[i]) {
1620                                 dev_err(&instance->pdev->dev,
1621                                         "Could not allocate memory for local map"
1622                                         " size requested: %d\n",
1623                                         fusion->drv_map_sz);
1624                                 goto ld_drv_map_alloc_fail;
1625                         }
1626                 }
1627         }
1628
1629         for (i = 0; i < 2; i++) {
1630                 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1631                                                        fusion->max_map_sz,
1632                                                        &fusion->ld_map_phys[i],
1633                                                        GFP_KERNEL);
1634                 if (!fusion->ld_map[i]) {
1635                         dev_err(&instance->pdev->dev,
1636                                 "Could not allocate memory for map info %s:%d\n",
1637                                 __func__, __LINE__);
1638                         goto ld_map_alloc_fail;
1639                 }
1640         }
1641
1642         return 0;
1643
1644 ld_map_alloc_fail:
1645         for (i = 0; i < 2; i++) {
1646                 if (fusion->ld_map[i])
1647                         dma_free_coherent(&instance->pdev->dev,
1648                                           fusion->max_map_sz,
1649                                           fusion->ld_map[i],
1650                                           fusion->ld_map_phys[i]);
1651         }
1652
1653 ld_drv_map_alloc_fail:
1654         for (i = 0; i < 2; i++) {
1655                 if (fusion->ld_drv_map[i]) {
1656                         if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1657                                 vfree(fusion->ld_drv_map[i]);
1658                         else
1659                                 free_pages((ulong)fusion->ld_drv_map[i],
1660                                            fusion->drv_map_pages);
1661                 }
1662         }
1663
1664         return -ENOMEM;
1665 }
1666
1667 /**
1668  * megasas_configure_queue_sizes -      Calculate size of request desc queue,
1669  *                                      reply desc queue,
1670  *                                      IO request frame queue, set can_queue.
1671  * @instance:                           Adapter soft state
1672  * @return:                             void
1673  */
1674 static inline
1675 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1676 {
1677         struct fusion_context *fusion;
1678         u16 max_cmd;
1679
1680         fusion = instance->ctrl_context;
1681         max_cmd = instance->max_fw_cmds;
1682
1683         if (instance->adapter_type >= VENTURA_SERIES)
1684                 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1685         else
1686                 instance->max_mpt_cmds = instance->max_fw_cmds;
1687
1688         instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1689         instance->cur_can_queue = instance->max_scsi_cmds;
1690         instance->host->can_queue = instance->cur_can_queue;
1691
1692         fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1693
1694         fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1695                                           instance->max_mpt_cmds;
1696         fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1697                                         (fusion->reply_q_depth);
1698         fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1699                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1700                  * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1701 }
1702
1703 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1704 {
1705         struct fusion_context *fusion;
1706         struct megasas_cmd *cmd;
1707
1708         fusion = instance->ctrl_context;
1709
1710         cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1711
1712         if (!cmd) {
1713                 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1714                         __func__, __LINE__);
1715                 return -ENOMEM;
1716         }
1717
1718         cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1719                                         IOC_INIT_FRAME_SIZE,
1720                                         &cmd->frame_phys_addr, GFP_KERNEL);
1721
1722         if (!cmd->frame) {
1723                 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1724                         __func__, __LINE__);
1725                 kfree(cmd);
1726                 return -ENOMEM;
1727         }
1728
1729         fusion->ioc_init_cmd = cmd;
1730         return 0;
1731 }
1732
1733 /**
1734  * megasas_free_ioc_init_cmd -  Free IOC INIT command frame
1735  * @instance:           Adapter soft state
1736  */
1737 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1738 {
1739         struct fusion_context *fusion;
1740
1741         fusion = instance->ctrl_context;
1742
1743         if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1744                 dma_free_coherent(&instance->pdev->dev,
1745                                   IOC_INIT_FRAME_SIZE,
1746                                   fusion->ioc_init_cmd->frame,
1747                                   fusion->ioc_init_cmd->frame_phys_addr);
1748
1749         kfree(fusion->ioc_init_cmd);
1750 }
1751
1752 /**
1753  * megasas_init_adapter_fusion -        Initializes the FW
1754  * @instance:           Adapter soft state
1755  *
1756  * This is the main function for initializing firmware.
1757  */
1758 static u32
1759 megasas_init_adapter_fusion(struct megasas_instance *instance)
1760 {
1761         struct fusion_context *fusion;
1762         u32 scratch_pad_1;
1763         int i = 0, count;
1764         u32 status_reg;
1765
1766         fusion = instance->ctrl_context;
1767
1768         megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1769
1770         /*
1771          * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1772          */
1773         instance->max_mfi_cmds =
1774                 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1775
1776         megasas_configure_queue_sizes(instance);
1777
1778         scratch_pad_1 = megasas_readl(instance,
1779                                       &instance->reg_set->outbound_scratch_pad_1);
1780         /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1781          * Firmware support extended IO chain frame which is 4 times more than
1782          * legacy Firmware.
1783          * Legacy Firmware - Frame size is (8 * 128) = 1K
1784          * 1M IO Firmware  - Frame size is (8 * 128 * 4)  = 4K
1785          */
1786         if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1787                 instance->max_chain_frame_sz =
1788                         ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1789                         MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1790         else
1791                 instance->max_chain_frame_sz =
1792                         ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1793                         MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1794
1795         if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1796                 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1797                         instance->max_chain_frame_sz,
1798                         MEGASAS_CHAIN_FRAME_SZ_MIN);
1799                 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1800         }
1801
1802         fusion->max_sge_in_main_msg =
1803                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1804                         - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1805
1806         fusion->max_sge_in_chain =
1807                 instance->max_chain_frame_sz
1808                         / sizeof(union MPI2_SGE_IO_UNION);
1809
1810         instance->max_num_sge =
1811                 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1812                         + fusion->max_sge_in_chain - 2);
1813
1814         /* Used for pass thru MFI frame (DCMD) */
1815         fusion->chain_offset_mfi_pthru =
1816                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1817
1818         fusion->chain_offset_io_request =
1819                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1820                  sizeof(union MPI2_SGE_IO_UNION))/16;
1821
1822         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1823         for (i = 0 ; i < count; i++)
1824                 fusion->last_reply_idx[i] = 0;
1825
1826         /*
1827          * For fusion adapters, 3 commands for IOCTL and 8 commands
1828          * for driver's internal DCMDs.
1829          */
1830         instance->max_scsi_cmds = instance->max_fw_cmds -
1831                                 (MEGASAS_FUSION_INTERNAL_CMDS +
1832                                 MEGASAS_FUSION_IOCTL_CMDS);
1833         sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1834
1835         if (megasas_alloc_ioc_init_frame(instance))
1836                 return 1;
1837
1838         /*
1839          * Allocate memory for descriptors
1840          * Create a pool of commands
1841          */
1842         if (megasas_alloc_cmds(instance))
1843                 goto fail_alloc_mfi_cmds;
1844         if (megasas_alloc_cmds_fusion(instance))
1845                 goto fail_alloc_cmds;
1846
1847         if (megasas_ioc_init_fusion(instance)) {
1848                 status_reg = instance->instancet->read_fw_status_reg(instance);
1849                 if (((status_reg & MFI_STATE_MASK) == MFI_STATE_FAULT) &&
1850                     (status_reg & MFI_RESET_ADAPTER)) {
1851                         /* Do a chip reset and then retry IOC INIT once */
1852                         if (megasas_adp_reset_wait_for_ready
1853                                 (instance, true, 0) == FAILED)
1854                                 goto fail_ioc_init;
1855
1856                         if (megasas_ioc_init_fusion(instance))
1857                                 goto fail_ioc_init;
1858                 } else {
1859                         goto fail_ioc_init;
1860                 }
1861         }
1862
1863         megasas_display_intel_branding(instance);
1864         if (megasas_get_ctrl_info(instance)) {
1865                 dev_err(&instance->pdev->dev,
1866                         "Could not get controller info. Fail from %s %d\n",
1867                         __func__, __LINE__);
1868                 goto fail_ioc_init;
1869         }
1870
1871         instance->flag_ieee = 1;
1872         instance->r1_ldio_hint_default =  MR_R1_LDIO_PIGGYBACK_DEFAULT;
1873         instance->threshold_reply_count = instance->max_fw_cmds / 4;
1874         fusion->fast_path_io = 0;
1875
1876         if (megasas_allocate_raid_maps(instance))
1877                 goto fail_ioc_init;
1878
1879         if (!megasas_get_map_info(instance))
1880                 megasas_sync_map_info(instance);
1881
1882         return 0;
1883
1884 fail_ioc_init:
1885         megasas_free_cmds_fusion(instance);
1886 fail_alloc_cmds:
1887         megasas_free_cmds(instance);
1888 fail_alloc_mfi_cmds:
1889         megasas_free_ioc_init_cmd(instance);
1890         return 1;
1891 }
1892
1893 /**
1894  * megasas_fault_detect_work    -       Worker function of
1895  *                                      FW fault handling workqueue.
1896  * @work:       FW fault work struct
1897  */
1898 static void
1899 megasas_fault_detect_work(struct work_struct *work)
1900 {
1901         struct megasas_instance *instance =
1902                 container_of(work, struct megasas_instance,
1903                              fw_fault_work.work);
1904         u32 fw_state, dma_state, status;
1905
1906         /* Check the fw state */
1907         fw_state = instance->instancet->read_fw_status_reg(instance) &
1908                         MFI_STATE_MASK;
1909
1910         if (fw_state == MFI_STATE_FAULT) {
1911                 dma_state = instance->instancet->read_fw_status_reg(instance) &
1912                                 MFI_STATE_DMADONE;
1913                 /* Start collecting crash, if DMA bit is done */
1914                 if (instance->crash_dump_drv_support &&
1915                     instance->crash_dump_app_support && dma_state) {
1916                         megasas_fusion_crash_dump(instance);
1917                 } else {
1918                         if (instance->unload == 0) {
1919                                 status = megasas_reset_fusion(instance->host, 0);
1920                                 if (status != SUCCESS) {
1921                                         dev_err(&instance->pdev->dev,
1922                                                 "Failed from %s %d, do not re-arm timer\n",
1923                                                 __func__, __LINE__);
1924                                         return;
1925                                 }
1926                         }
1927                 }
1928         }
1929
1930         if (instance->fw_fault_work_q)
1931                 queue_delayed_work(instance->fw_fault_work_q,
1932                         &instance->fw_fault_work,
1933                         msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1934 }
1935
1936 int
1937 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1938 {
1939         /* Check if the Fault WQ is already started */
1940         if (instance->fw_fault_work_q)
1941                 return SUCCESS;
1942
1943         INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1944
1945         snprintf(instance->fault_handler_work_q_name,
1946                  sizeof(instance->fault_handler_work_q_name),
1947                  "poll_megasas%d_status", instance->host->host_no);
1948
1949         instance->fw_fault_work_q =
1950                 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1951         if (!instance->fw_fault_work_q) {
1952                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1953                         __func__, __LINE__);
1954                 return FAILED;
1955         }
1956
1957         queue_delayed_work(instance->fw_fault_work_q,
1958                            &instance->fw_fault_work,
1959                            msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1960
1961         return SUCCESS;
1962 }
1963
1964 void
1965 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1966 {
1967         struct workqueue_struct *wq;
1968
1969         if (instance->fw_fault_work_q) {
1970                 wq = instance->fw_fault_work_q;
1971                 instance->fw_fault_work_q = NULL;
1972                 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1973                         flush_workqueue(wq);
1974                 destroy_workqueue(wq);
1975         }
1976 }
1977
1978 /**
1979  * map_cmd_status -     Maps FW cmd status to OS cmd status
1980  * @fusion:             fusion context
1981  * @scmd:               Pointer to cmd
1982  * @status:             status of cmd returned by FW
1983  * @ext_status:         ext status of cmd returned by FW
1984  * @data_length:        command data length
1985  * @sense:              command sense data
1986  */
1987 static void
1988 map_cmd_status(struct fusion_context *fusion,
1989                 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1990                 u32 data_length, u8 *sense)
1991 {
1992         u8 cmd_type;
1993         int resid;
1994
1995         cmd_type = megasas_cmd_type(scmd);
1996         switch (status) {
1997
1998         case MFI_STAT_OK:
1999                 scmd->result = DID_OK << 16;
2000                 break;
2001
2002         case MFI_STAT_SCSI_IO_FAILED:
2003         case MFI_STAT_LD_INIT_IN_PROGRESS:
2004                 scmd->result = (DID_ERROR << 16) | ext_status;
2005                 break;
2006
2007         case MFI_STAT_SCSI_DONE_WITH_ERROR:
2008
2009                 scmd->result = (DID_OK << 16) | ext_status;
2010                 if (ext_status == SAM_STAT_CHECK_CONDITION) {
2011                         memset(scmd->sense_buffer, 0,
2012                                SCSI_SENSE_BUFFERSIZE);
2013                         memcpy(scmd->sense_buffer, sense,
2014                                SCSI_SENSE_BUFFERSIZE);
2015                         scmd->result |= DRIVER_SENSE << 24;
2016                 }
2017
2018                 /*
2019                  * If the  IO request is partially completed, then MR FW will
2020                  * update "io_request->DataLength" field with actual number of
2021                  * bytes transferred.Driver will set residual bytes count in
2022                  * SCSI command structure.
2023                  */
2024                 resid = (scsi_bufflen(scmd) - data_length);
2025                 scsi_set_resid(scmd, resid);
2026
2027                 if (resid &&
2028                         ((cmd_type == READ_WRITE_LDIO) ||
2029                         (cmd_type == READ_WRITE_SYSPDIO)))
2030                         scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
2031                                 " requested/completed 0x%x/0x%x\n",
2032                                 status, scsi_bufflen(scmd), data_length);
2033                 break;
2034
2035         case MFI_STAT_LD_OFFLINE:
2036         case MFI_STAT_DEVICE_NOT_FOUND:
2037                 scmd->result = DID_BAD_TARGET << 16;
2038                 break;
2039         case MFI_STAT_CONFIG_SEQ_MISMATCH:
2040                 scmd->result = DID_IMM_RETRY << 16;
2041                 break;
2042         default:
2043                 scmd->result = DID_ERROR << 16;
2044                 break;
2045         }
2046 }
2047
2048 /**
2049  * megasas_is_prp_possible -
2050  * Checks if native NVMe PRPs can be built for the IO
2051  *
2052  * @instance:           Adapter soft state
2053  * @scmd:               SCSI command from the mid-layer
2054  * @sge_count:          scatter gather element count.
2055  *
2056  * Returns:             true: PRPs can be built
2057  *                      false: IEEE SGLs needs to be built
2058  */
2059 static bool
2060 megasas_is_prp_possible(struct megasas_instance *instance,
2061                         struct scsi_cmnd *scmd, int sge_count)
2062 {
2063         u32 data_length = 0;
2064         struct scatterlist *sg_scmd;
2065         bool build_prp = false;
2066         u32 mr_nvme_pg_size;
2067
2068         mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2069                                 MR_DEFAULT_NVME_PAGE_SIZE);
2070         data_length = scsi_bufflen(scmd);
2071         sg_scmd = scsi_sglist(scmd);
2072
2073         /*
2074          * NVMe uses one PRP for each page (or part of a page)
2075          * look at the data length - if 4 pages or less then IEEE is OK
2076          * if  > 5 pages then we need to build a native SGL
2077          * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2078          * if this first size in the page is >= the residual beyond 4 pages
2079          * then use IEEE, otherwise use native SGL
2080          */
2081
2082         if (data_length > (mr_nvme_pg_size * 5)) {
2083                 build_prp = true;
2084         } else if ((data_length > (mr_nvme_pg_size * 4)) &&
2085                         (data_length <= (mr_nvme_pg_size * 5)))  {
2086                 /* check if 1st SG entry size is < residual beyond 4 pages */
2087                 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
2088                         build_prp = true;
2089         }
2090
2091         return build_prp;
2092 }
2093
2094 /**
2095  * megasas_make_prp_nvme -
2096  * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2097  *
2098  * @instance:           Adapter soft state
2099  * @scmd:               SCSI command from the mid-layer
2100  * @sgl_ptr:            SGL to be filled in
2101  * @cmd:                Fusion command frame
2102  * @sge_count:          scatter gather element count.
2103  *
2104  * Returns:             true: PRPs are built
2105  *                      false: IEEE SGLs needs to be built
2106  */
2107 static bool
2108 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2109                       struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2110                       struct megasas_cmd_fusion *cmd, int sge_count)
2111 {
2112         int sge_len, offset, num_prp_in_chain = 0;
2113         struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2114         u64 *ptr_sgl;
2115         dma_addr_t ptr_sgl_phys;
2116         u64 sge_addr;
2117         u32 page_mask, page_mask_result;
2118         struct scatterlist *sg_scmd;
2119         u32 first_prp_len;
2120         bool build_prp = false;
2121         int data_len = scsi_bufflen(scmd);
2122         u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2123                                         MR_DEFAULT_NVME_PAGE_SIZE);
2124
2125         build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2126
2127         if (!build_prp)
2128                 return false;
2129
2130         /*
2131          * Nvme has a very convoluted prp format.  One prp is required
2132          * for each page or partial page. Driver need to split up OS sg_list
2133          * entries if it is longer than one page or cross a page
2134          * boundary.  Driver also have to insert a PRP list pointer entry as
2135          * the last entry in each physical page of the PRP list.
2136          *
2137          * NOTE: The first PRP "entry" is actually placed in the first
2138          * SGL entry in the main message as IEEE 64 format.  The 2nd
2139          * entry in the main message is the chain element, and the rest
2140          * of the PRP entries are built in the contiguous pcie buffer.
2141          */
2142         page_mask = mr_nvme_pg_size - 1;
2143         ptr_sgl = (u64 *)cmd->sg_frame;
2144         ptr_sgl_phys = cmd->sg_frame_phys_addr;
2145         memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2146
2147         /* Build chain frame element which holds all prps except first*/
2148         main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2149             ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2150
2151         main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2152         main_chain_element->NextChainOffset = 0;
2153         main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2154                                         IEEE_SGE_FLAGS_SYSTEM_ADDR |
2155                                         MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2156
2157         /* Build first prp, sge need not to be page aligned*/
2158         ptr_first_sgl = sgl_ptr;
2159         sg_scmd = scsi_sglist(scmd);
2160         sge_addr = sg_dma_address(sg_scmd);
2161         sge_len = sg_dma_len(sg_scmd);
2162
2163         offset = (u32)(sge_addr & page_mask);
2164         first_prp_len = mr_nvme_pg_size - offset;
2165
2166         ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2167         ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2168
2169         data_len -= first_prp_len;
2170
2171         if (sge_len > first_prp_len) {
2172                 sge_addr += first_prp_len;
2173                 sge_len -= first_prp_len;
2174         } else if (sge_len == first_prp_len) {
2175                 sg_scmd = sg_next(sg_scmd);
2176                 sge_addr = sg_dma_address(sg_scmd);
2177                 sge_len = sg_dma_len(sg_scmd);
2178         }
2179
2180         for (;;) {
2181                 offset = (u32)(sge_addr & page_mask);
2182
2183                 /* Put PRP pointer due to page boundary*/
2184                 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2185                 if (unlikely(!page_mask_result)) {
2186                         scmd_printk(KERN_NOTICE,
2187                                     scmd, "page boundary ptr_sgl: 0x%p\n",
2188                                     ptr_sgl);
2189                         ptr_sgl_phys += 8;
2190                         *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2191                         ptr_sgl++;
2192                         num_prp_in_chain++;
2193                 }
2194
2195                 *ptr_sgl = cpu_to_le64(sge_addr);
2196                 ptr_sgl++;
2197                 ptr_sgl_phys += 8;
2198                 num_prp_in_chain++;
2199
2200                 sge_addr += mr_nvme_pg_size;
2201                 sge_len -= mr_nvme_pg_size;
2202                 data_len -= mr_nvme_pg_size;
2203
2204                 if (data_len <= 0)
2205                         break;
2206
2207                 if (sge_len > 0)
2208                         continue;
2209
2210                 sg_scmd = sg_next(sg_scmd);
2211                 sge_addr = sg_dma_address(sg_scmd);
2212                 sge_len = sg_dma_len(sg_scmd);
2213         }
2214
2215         main_chain_element->Length =
2216                         cpu_to_le32(num_prp_in_chain * sizeof(u64));
2217
2218         return build_prp;
2219 }
2220
2221 /**
2222  * megasas_make_sgl_fusion -    Prepares 32-bit SGL
2223  * @instance:           Adapter soft state
2224  * @scp:                SCSI command from the mid-layer
2225  * @sgl_ptr:            SGL to be filled in
2226  * @cmd:                cmd we are working on
2227  * @sge_count:          sge count
2228  *
2229  */
2230 static void
2231 megasas_make_sgl_fusion(struct megasas_instance *instance,
2232                         struct scsi_cmnd *scp,
2233                         struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2234                         struct megasas_cmd_fusion *cmd, int sge_count)
2235 {
2236         int i, sg_processed;
2237         struct scatterlist *os_sgl;
2238         struct fusion_context *fusion;
2239
2240         fusion = instance->ctrl_context;
2241
2242         if (instance->adapter_type >= INVADER_SERIES) {
2243                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2244                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2245                 sgl_ptr_end->Flags = 0;
2246         }
2247
2248         scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2249                 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2250                 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2251                 sgl_ptr->Flags = 0;
2252                 if (instance->adapter_type >= INVADER_SERIES)
2253                         if (i == sge_count - 1)
2254                                 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2255                 sgl_ptr++;
2256                 sg_processed = i + 1;
2257
2258                 if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
2259                     (sge_count > fusion->max_sge_in_main_msg)) {
2260
2261                         struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2262                         if (instance->adapter_type >= INVADER_SERIES) {
2263                                 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2264                                         MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2265                                         MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2266                                         cmd->io_request->ChainOffset =
2267                                                 fusion->
2268                                                 chain_offset_io_request;
2269                                 else
2270                                         cmd->io_request->ChainOffset = 0;
2271                         } else
2272                                 cmd->io_request->ChainOffset =
2273                                         fusion->chain_offset_io_request;
2274
2275                         sg_chain = sgl_ptr;
2276                         /* Prepare chain element */
2277                         sg_chain->NextChainOffset = 0;
2278                         if (instance->adapter_type >= INVADER_SERIES)
2279                                 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2280                         else
2281                                 sg_chain->Flags =
2282                                         (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2283                                          MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2284                         sg_chain->Length =  cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2285                         sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2286
2287                         sgl_ptr =
2288                           (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2289                         memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2290                 }
2291         }
2292 }
2293
2294 /**
2295  * megasas_make_sgl -   Build Scatter Gather List(SGLs)
2296  * @scp:                SCSI command pointer
2297  * @instance:           Soft instance of controller
2298  * @cmd:                Fusion command pointer
2299  *
2300  * This function will build sgls based on device type.
2301  * For nvme drives, there is different way of building sgls in nvme native
2302  * format- PRPs(Physical Region Page).
2303  *
2304  * Returns the number of sg lists actually used, zero if the sg lists
2305  * is NULL, or -ENOMEM if the mapping failed
2306  */
2307 static
2308 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2309                      struct megasas_cmd_fusion *cmd)
2310 {
2311         int sge_count;
2312         bool build_prp = false;
2313         struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2314
2315         sge_count = scsi_dma_map(scp);
2316
2317         if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2318                 return sge_count;
2319
2320         sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2321         if ((le16_to_cpu(cmd->io_request->IoFlags) &
2322             MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2323             (cmd->pd_interface == NVME_PD))
2324                 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2325                                                   cmd, sge_count);
2326
2327         if (!build_prp)
2328                 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2329                                         cmd, sge_count);
2330
2331         return sge_count;
2332 }
2333
2334 /**
2335  * megasas_set_pd_lba - Sets PD LBA
2336  * @io_request:         IO request
2337  * @cdb_len:            cdb length
2338  * @io_info:            IO information
2339  * @scp:                SCSI command
2340  * @local_map_ptr:      Raid map
2341  * @ref_tag:            Primary reference tag
2342  *
2343  * Used to set the PD LBA in CDB for FP IOs
2344  */
2345 static void
2346 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2347                    struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2348                    struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2349 {
2350         struct MR_LD_RAID *raid;
2351         u16 ld;
2352         u64 start_blk = io_info->pdBlock;
2353         u8 *cdb = io_request->CDB.CDB32;
2354         u32 num_blocks = io_info->numBlocks;
2355         u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2356
2357         /* Check if T10 PI (DIF) is enabled for this LD */
2358         ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2359         raid = MR_LdRaidGet(ld, local_map_ptr);
2360         if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2361                 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2362                 cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2363                 cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;
2364
2365                 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2366                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2367                 else
2368                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2369                 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2370
2371                 /* LBA */
2372                 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2373                 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2374                 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2375                 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2376                 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2377                 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2378                 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2379                 cdb[19] = (u8)(start_blk & 0xff);
2380
2381                 /* Logical block reference tag */
2382                 io_request->CDB.EEDP32.PrimaryReferenceTag =
2383                         cpu_to_be32(ref_tag);
2384                 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2385                 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2386
2387                 /* Transfer length */
2388                 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2389                 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2390                 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2391                 cdb[31] = (u8)(num_blocks & 0xff);
2392
2393                 /* set SCSI IO EEDPFlags */
2394                 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2395                         io_request->EEDPFlags = cpu_to_le16(
2396                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
2397                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2398                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2399                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2400                                 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2401                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2402                 } else {
2403                         io_request->EEDPFlags = cpu_to_le16(
2404                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2405                                 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2406                 }
2407                 io_request->Control |= cpu_to_le32((0x4 << 26));
2408                 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2409         } else {
2410                 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2411                 if (((cdb_len == 12) || (cdb_len == 16)) &&
2412                     (start_blk <= 0xffffffff)) {
2413                         if (cdb_len == 16) {
2414                                 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2415                                 flagvals = cdb[1];
2416                                 groupnum = cdb[14];
2417                                 control = cdb[15];
2418                         } else {
2419                                 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2420                                 flagvals = cdb[1];
2421                                 groupnum = cdb[10];
2422                                 control = cdb[11];
2423                         }
2424
2425                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2426
2427                         cdb[0] = opcode;
2428                         cdb[1] = flagvals;
2429                         cdb[6] = groupnum;
2430                         cdb[9] = control;
2431
2432                         /* Transfer length */
2433                         cdb[8] = (u8)(num_blocks & 0xff);
2434                         cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2435
2436                         io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2437                         cdb_len = 10;
2438                 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2439                         /* Convert to 16 byte CDB for large LBA's */
2440                         switch (cdb_len) {
2441                         case 6:
2442                                 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2443                                 control = cdb[5];
2444                                 break;
2445                         case 10:
2446                                 opcode =
2447                                         cdb[0] == READ_10 ? READ_16 : WRITE_16;
2448                                 flagvals = cdb[1];
2449                                 groupnum = cdb[6];
2450                                 control = cdb[9];
2451                                 break;
2452                         case 12:
2453                                 opcode =
2454                                         cdb[0] == READ_12 ? READ_16 : WRITE_16;
2455                                 flagvals = cdb[1];
2456                                 groupnum = cdb[10];
2457                                 control = cdb[11];
2458                                 break;
2459                         }
2460
2461                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2462
2463                         cdb[0] = opcode;
2464                         cdb[1] = flagvals;
2465                         cdb[14] = groupnum;
2466                         cdb[15] = control;
2467
2468                         /* Transfer length */
2469                         cdb[13] = (u8)(num_blocks & 0xff);
2470                         cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2471                         cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2472                         cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2473
2474                         io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2475                         cdb_len = 16;
2476                 }
2477
2478                 /* Normal case, just load LBA here */
2479                 switch (cdb_len) {
2480                 case 6:
2481                 {
2482                         u8 val = cdb[1] & 0xE0;
2483                         cdb[3] = (u8)(start_blk & 0xff);
2484                         cdb[2] = (u8)((start_blk >> 8) & 0xff);
2485                         cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2486                         break;
2487                 }
2488                 case 10:
2489                         cdb[5] = (u8)(start_blk & 0xff);
2490                         cdb[4] = (u8)((start_blk >> 8) & 0xff);
2491                         cdb[3] = (u8)((start_blk >> 16) & 0xff);
2492                         cdb[2] = (u8)((start_blk >> 24) & 0xff);
2493                         break;
2494                 case 12:
2495                         cdb[5]    = (u8)(start_blk & 0xff);
2496                         cdb[4]    = (u8)((start_blk >> 8) & 0xff);
2497                         cdb[3]    = (u8)((start_blk >> 16) & 0xff);
2498                         cdb[2]    = (u8)((start_blk >> 24) & 0xff);
2499                         break;
2500                 case 16:
2501                         cdb[9]    = (u8)(start_blk & 0xff);
2502                         cdb[8]    = (u8)((start_blk >> 8) & 0xff);
2503                         cdb[7]    = (u8)((start_blk >> 16) & 0xff);
2504                         cdb[6]    = (u8)((start_blk >> 24) & 0xff);
2505                         cdb[5]    = (u8)((start_blk >> 32) & 0xff);
2506                         cdb[4]    = (u8)((start_blk >> 40) & 0xff);
2507                         cdb[3]    = (u8)((start_blk >> 48) & 0xff);
2508                         cdb[2]    = (u8)((start_blk >> 56) & 0xff);
2509                         break;
2510                 }
2511         }
2512 }
2513
2514 /**
2515  * megasas_stream_detect -      stream detection on read and and write IOs
2516  * @instance:           Adapter soft state
2517  * @cmd:                    Command to be prepared
2518  * @io_info:            IO Request info
2519  *
2520  */
2521
2522 /** stream detection on read and and write IOs */
2523 static void megasas_stream_detect(struct megasas_instance *instance,
2524                                   struct megasas_cmd_fusion *cmd,
2525                                   struct IO_REQUEST_INFO *io_info)
2526 {
2527         struct fusion_context *fusion = instance->ctrl_context;
2528         u32 device_id = io_info->ldTgtId;
2529         struct LD_STREAM_DETECT *current_ld_sd
2530                 = fusion->stream_detect_by_ld[device_id];
2531         u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2532         u32 shifted_values, unshifted_values;
2533         u32 index_value_mask, shifted_values_mask;
2534         int i;
2535         bool is_read_ahead = false;
2536         struct STREAM_DETECT *current_sd;
2537         /* find possible stream */
2538         for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2539                 stream_num = (*track_stream >>
2540                         (i * BITS_PER_INDEX_STREAM)) &
2541                         STREAM_MASK;
2542                 current_sd = &current_ld_sd->stream_track[stream_num];
2543                 /* if we found a stream, update the raid
2544                  *  context and also update the mruBitMap
2545                  */
2546                 /*      boundary condition */
2547                 if ((current_sd->next_seq_lba) &&
2548                     (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2549                     (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2550                     (current_sd->is_read == io_info->isRead)) {
2551
2552                         if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2553                             ((!io_info->isRead) || (!is_read_ahead)))
2554                                 /*
2555                                  * Once the API availible we need to change this.
2556                                  * At this point we are not allowing any gap
2557                                  */
2558                                 continue;
2559
2560                         SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2561                         current_sd->next_seq_lba =
2562                         io_info->ldStartBlock + io_info->numBlocks;
2563                         /*
2564                          *      update the mruBitMap LRU
2565                          */
2566                         shifted_values_mask =
2567                                 (1 <<  i * BITS_PER_INDEX_STREAM) - 1;
2568                         shifted_values = ((*track_stream & shifted_values_mask)
2569                                                 << BITS_PER_INDEX_STREAM);
2570                         index_value_mask =
2571                                 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2572                         unshifted_values =
2573                                 *track_stream & ~(shifted_values_mask |
2574                                 index_value_mask);
2575                         *track_stream =
2576                                 unshifted_values | shifted_values | stream_num;
2577                         return;
2578                 }
2579         }
2580         /*
2581          * if we did not find any stream, create a new one
2582          * from the least recently used
2583          */
2584         stream_num = (*track_stream >>
2585                 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2586                 STREAM_MASK;
2587         current_sd = &current_ld_sd->stream_track[stream_num];
2588         current_sd->is_read = io_info->isRead;
2589         current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2590         *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2591         return;
2592 }
2593
2594 /**
2595  * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2596  * affinity (cpu of the controller) and raid_flags in the raid context
2597  * based on IO type.
2598  *
2599  * @fusion:             Fusion context
2600  * @praid_context:      IO RAID context
2601  * @raid:               LD raid map
2602  * @fp_possible:        Is fast path possible?
2603  * @is_read:            Is read IO?
2604  * @scsi_buff_len:      SCSI command buffer length
2605  *
2606  */
2607 static void
2608 megasas_set_raidflag_cpu_affinity(struct fusion_context *fusion,
2609                                 union RAID_CONTEXT_UNION *praid_context,
2610                                 struct MR_LD_RAID *raid, bool fp_possible,
2611                                 u8 is_read, u32 scsi_buff_len)
2612 {
2613         u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2614         struct RAID_CONTEXT_G35 *rctx_g35;
2615
2616         rctx_g35 = &praid_context->raid_context_g35;
2617         if (fp_possible) {
2618                 if (is_read) {
2619                         if ((raid->cpuAffinity.pdRead.cpu0) &&
2620                             (raid->cpuAffinity.pdRead.cpu1))
2621                                 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2622                         else if (raid->cpuAffinity.pdRead.cpu1)
2623                                 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2624                 } else {
2625                         if ((raid->cpuAffinity.pdWrite.cpu0) &&
2626                             (raid->cpuAffinity.pdWrite.cpu1))
2627                                 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2628                         else if (raid->cpuAffinity.pdWrite.cpu1)
2629                                 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2630                         /* Fast path cache by pass capable R0/R1 VD */
2631                         if ((raid->level <= 1) &&
2632                             (raid->capability.fp_cache_bypass_capable)) {
2633                                 rctx_g35->routing_flags |=
2634                                         (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2635                                 rctx_g35->raid_flags =
2636                                         (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2637                                         << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2638                         }
2639                 }
2640         } else {
2641                 if (is_read) {
2642                         if ((raid->cpuAffinity.ldRead.cpu0) &&
2643                             (raid->cpuAffinity.ldRead.cpu1))
2644                                 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2645                         else if (raid->cpuAffinity.ldRead.cpu1)
2646                                 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2647                 } else {
2648                         if ((raid->cpuAffinity.ldWrite.cpu0) &&
2649                             (raid->cpuAffinity.ldWrite.cpu1))
2650                                 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2651                         else if (raid->cpuAffinity.ldWrite.cpu1)
2652                                 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2653
2654                         if (is_stream_detected(rctx_g35) &&
2655                             ((raid->level == 5) || (raid->level == 6)) &&
2656                             (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2657                             (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2658                                 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2659                 }
2660         }
2661
2662         rctx_g35->routing_flags |=
2663                 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2664
2665         /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2666          * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2667          * IO Subtype is not bitmap.
2668          */
2669         if ((fusion->pcie_bw_limitation) && (raid->level == 1) && (!is_read) &&
2670                         (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)) {
2671                 praid_context->raid_context_g35.raid_flags =
2672                         (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2673                         << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2674         }
2675 }
2676
2677 /**
2678  * megasas_build_ldio_fusion -  Prepares IOs to devices
2679  * @instance:           Adapter soft state
2680  * @scp:                SCSI command
2681  * @cmd:                Command to be prepared
2682  *
2683  * Prepares the io_request and chain elements (sg_frame) for IO
2684  * The IO can be for PD (Fast Path) or LD
2685  */
2686 static void
2687 megasas_build_ldio_fusion(struct megasas_instance *instance,
2688                           struct scsi_cmnd *scp,
2689                           struct megasas_cmd_fusion *cmd)
2690 {
2691         bool fp_possible;
2692         u16 ld;
2693         u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2694         u32 scsi_buff_len;
2695         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2696         struct IO_REQUEST_INFO io_info;
2697         struct fusion_context *fusion;
2698         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2699         u8 *raidLUN;
2700         unsigned long spinlock_flags;
2701         struct MR_LD_RAID *raid = NULL;
2702         struct MR_PRIV_DEVICE *mrdev_priv;
2703         struct RAID_CONTEXT *rctx;
2704         struct RAID_CONTEXT_G35 *rctx_g35;
2705
2706         device_id = MEGASAS_DEV_INDEX(scp);
2707
2708         fusion = instance->ctrl_context;
2709
2710         io_request = cmd->io_request;
2711         rctx = &io_request->RaidContext.raid_context;
2712         rctx_g35 = &io_request->RaidContext.raid_context_g35;
2713
2714         rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2715         rctx->status = 0;
2716         rctx->ex_status = 0;
2717
2718         start_lba_lo = 0;
2719         start_lba_hi = 0;
2720         fp_possible = false;
2721
2722         /*
2723          * 6-byte READ(0x08) or WRITE(0x0A) cdb
2724          */
2725         if (scp->cmd_len == 6) {
2726                 datalength = (u32) scp->cmnd[4];
2727                 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2728                         ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2729
2730                 start_lba_lo &= 0x1FFFFF;
2731         }
2732
2733         /*
2734          * 10-byte READ(0x28) or WRITE(0x2A) cdb
2735          */
2736         else if (scp->cmd_len == 10) {
2737                 datalength = (u32) scp->cmnd[8] |
2738                         ((u32) scp->cmnd[7] << 8);
2739                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2740                         ((u32) scp->cmnd[3] << 16) |
2741                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2742         }
2743
2744         /*
2745          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2746          */
2747         else if (scp->cmd_len == 12) {
2748                 datalength = ((u32) scp->cmnd[6] << 24) |
2749                         ((u32) scp->cmnd[7] << 16) |
2750                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2751                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2752                         ((u32) scp->cmnd[3] << 16) |
2753                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2754         }
2755
2756         /*
2757          * 16-byte READ(0x88) or WRITE(0x8A) cdb
2758          */
2759         else if (scp->cmd_len == 16) {
2760                 datalength = ((u32) scp->cmnd[10] << 24) |
2761                         ((u32) scp->cmnd[11] << 16) |
2762                         ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2763                 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2764                         ((u32) scp->cmnd[7] << 16) |
2765                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2766
2767                 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2768                         ((u32) scp->cmnd[3] << 16) |
2769                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2770         }
2771
2772         memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2773         io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2774         io_info.numBlocks = datalength;
2775         io_info.ldTgtId = device_id;
2776         io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2777         scsi_buff_len = scsi_bufflen(scp);
2778         io_request->DataLength = cpu_to_le32(scsi_buff_len);
2779         io_info.data_arms = 1;
2780
2781         if (scp->sc_data_direction == DMA_FROM_DEVICE)
2782                 io_info.isRead = 1;
2783
2784         local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2785         ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2786
2787         if (ld < instance->fw_supported_vd_count)
2788                 raid = MR_LdRaidGet(ld, local_map_ptr);
2789
2790         if (!raid || (!fusion->fast_path_io)) {
2791                 rctx->reg_lock_flags  = 0;
2792                 fp_possible = false;
2793         } else {
2794                 if (MR_BuildRaidContext(instance, &io_info, rctx,
2795                                         local_map_ptr, &raidLUN))
2796                         fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2797         }
2798
2799         megasas_get_msix_index(instance, scp, cmd, io_info.data_arms);
2800
2801         if (instance->adapter_type >= VENTURA_SERIES) {
2802                 /* FP for Optimal raid level 1.
2803                  * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2804                  * are built by the driver as LD I/Os.
2805                  * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2806                  * (there is never a reason to process these as buffered writes)
2807                  * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2808                  * with the SLD bit asserted.
2809                  */
2810                 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2811                         mrdev_priv = scp->device->hostdata;
2812
2813                         if (atomic_inc_return(&instance->fw_outstanding) >
2814                                 (instance->host->can_queue)) {
2815                                 fp_possible = false;
2816                                 atomic_dec(&instance->fw_outstanding);
2817                         } else if (fusion->pcie_bw_limitation &&
2818                                 ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2819                                    (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0))) {
2820                                 fp_possible = false;
2821                                 atomic_dec(&instance->fw_outstanding);
2822                                 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2823                                         atomic_set(&mrdev_priv->r1_ldio_hint,
2824                                                    instance->r1_ldio_hint_default);
2825                         }
2826                 }
2827
2828                 if (!fp_possible ||
2829                     (io_info.isRead && io_info.ra_capable)) {
2830                         spin_lock_irqsave(&instance->stream_lock,
2831                                           spinlock_flags);
2832                         megasas_stream_detect(instance, cmd, &io_info);
2833                         spin_unlock_irqrestore(&instance->stream_lock,
2834                                                spinlock_flags);
2835                         /* In ventura if stream detected for a read and it is
2836                          * read ahead capable make this IO as LDIO
2837                          */
2838                         if (is_stream_detected(rctx_g35))
2839                                 fp_possible = false;
2840                 }
2841
2842                 /* If raid is NULL, set CPU affinity to default CPU0 */
2843                 if (raid)
2844                         megasas_set_raidflag_cpu_affinity(fusion, &io_request->RaidContext,
2845                                 raid, fp_possible, io_info.isRead,
2846                                 scsi_buff_len);
2847                 else
2848                         rctx_g35->routing_flags |=
2849                                 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2850         }
2851
2852         if (fp_possible) {
2853                 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2854                                    local_map_ptr, start_lba_lo);
2855                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2856                 cmd->request_desc->SCSIIO.RequestFlags =
2857                         (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2858                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2859                 if (instance->adapter_type == INVADER_SERIES) {
2860                         rctx->type = MPI2_TYPE_CUDA;
2861                         rctx->nseg = 0x1;
2862                         io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2863                         rctx->reg_lock_flags |=
2864                           (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2865                            MR_RL_FLAGS_SEQ_NUM_ENABLE);
2866                 } else if (instance->adapter_type >= VENTURA_SERIES) {
2867                         rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2868                         rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2869                         rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2870                         io_request->IoFlags |=
2871                                 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2872                 }
2873                 if (fusion->load_balance_info &&
2874                         (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2875                         (io_info.isRead)) {
2876                         io_info.devHandle =
2877                                 get_updated_dev_handle(instance,
2878                                         &fusion->load_balance_info[device_id],
2879                                         &io_info, local_map_ptr);
2880                         scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2881                         cmd->pd_r1_lb = io_info.pd_after_lb;
2882                         if (instance->adapter_type >= VENTURA_SERIES)
2883                                 rctx_g35->span_arm = io_info.span_arm;
2884                         else
2885                                 rctx->span_arm = io_info.span_arm;
2886
2887                 } else
2888                         scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2889
2890                 if (instance->adapter_type >= VENTURA_SERIES)
2891                         cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2892                 else
2893                         cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2894
2895                 if ((raidLUN[0] == 1) &&
2896                         (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2897                         instance->dev_handle = !(instance->dev_handle);
2898                         io_info.devHandle =
2899                                 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2900                 }
2901
2902                 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2903                 io_request->DevHandle = io_info.devHandle;
2904                 cmd->pd_interface = io_info.pd_interface;
2905                 /* populate the LUN field */
2906                 memcpy(io_request->LUN, raidLUN, 8);
2907         } else {
2908                 rctx->timeout_value =
2909                         cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2910                 cmd->request_desc->SCSIIO.RequestFlags =
2911                         (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2912                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2913                 if (instance->adapter_type == INVADER_SERIES) {
2914                         if (io_info.do_fp_rlbypass ||
2915                         (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2916                                 cmd->request_desc->SCSIIO.RequestFlags =
2917                                         (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2918                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2919                         rctx->type = MPI2_TYPE_CUDA;
2920                         rctx->reg_lock_flags |=
2921                                 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2922                                         MR_RL_FLAGS_SEQ_NUM_ENABLE);
2923                         rctx->nseg = 0x1;
2924                 } else if (instance->adapter_type >= VENTURA_SERIES) {
2925                         rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2926                         rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2927                         rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2928                 }
2929                 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2930                 io_request->DevHandle = cpu_to_le16(device_id);
2931
2932         } /* Not FP */
2933 }
2934
2935 /**
2936  * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2937  * @instance:           Adapter soft state
2938  * @scmd:               SCSI command
2939  * @cmd:                Command to be prepared
2940  *
2941  * Prepares the io_request frame for non-rw io cmds for vd.
2942  */
2943 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2944                           struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2945 {
2946         u32 device_id;
2947         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2948         u16 ld;
2949         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2950         struct fusion_context *fusion = instance->ctrl_context;
2951         u8                          span, physArm;
2952         __le16                      devHandle;
2953         u32                         arRef, pd;
2954         struct MR_LD_RAID                  *raid;
2955         struct RAID_CONTEXT                *pRAID_Context;
2956         u8 fp_possible = 1;
2957
2958         io_request = cmd->io_request;
2959         device_id = MEGASAS_DEV_INDEX(scmd);
2960         local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2961         io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2962         /* get RAID_Context pointer */
2963         pRAID_Context = &io_request->RaidContext.raid_context;
2964         /* Check with FW team */
2965         pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2966         pRAID_Context->reg_lock_row_lba    = 0;
2967         pRAID_Context->reg_lock_length    = 0;
2968
2969         if (fusion->fast_path_io && (
2970                 device_id < instance->fw_supported_vd_count)) {
2971
2972                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2973                 if (ld >= instance->fw_supported_vd_count - 1)
2974                         fp_possible = 0;
2975                 else {
2976                         raid = MR_LdRaidGet(ld, local_map_ptr);
2977                         if (!(raid->capability.fpNonRWCapable))
2978                                 fp_possible = 0;
2979                 }
2980         } else
2981                 fp_possible = 0;
2982
2983         if (!fp_possible) {
2984                 io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2985                 io_request->DevHandle = cpu_to_le16(device_id);
2986                 io_request->LUN[1] = scmd->device->lun;
2987                 pRAID_Context->timeout_value =
2988                         cpu_to_le16 (scmd->request->timeout / HZ);
2989                 cmd->request_desc->SCSIIO.RequestFlags =
2990                         (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2991                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2992         } else {
2993
2994                 /* set RAID context values */
2995                 pRAID_Context->config_seq_num = raid->seqNum;
2996                 if (instance->adapter_type < VENTURA_SERIES)
2997                         pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2998                 pRAID_Context->timeout_value =
2999                         cpu_to_le16(raid->fpIoTimeoutForLd);
3000
3001                 /* get the DevHandle for the PD (since this is
3002                    fpNonRWCapable, this is a single disk RAID0) */
3003                 span = physArm = 0;
3004                 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
3005                 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
3006                 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
3007
3008                 /* build request descriptor */
3009                 cmd->request_desc->SCSIIO.RequestFlags =
3010                         (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3011                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3012                 cmd->request_desc->SCSIIO.DevHandle = devHandle;
3013
3014                 /* populate the LUN field */
3015                 memcpy(io_request->LUN, raid->LUN, 8);
3016
3017                 /* build the raidScsiIO structure */
3018                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3019                 io_request->DevHandle = devHandle;
3020         }
3021 }
3022
3023 /**
3024  * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3025  * @instance:           Adapter soft state
3026  * @scmd:               SCSI command
3027  * @cmd:                Command to be prepared
3028  * @fp_possible:        parameter to detect fast path or firmware path io.
3029  *
3030  * Prepares the io_request frame for rw/non-rw io cmds for syspds
3031  */
3032 static void
3033 megasas_build_syspd_fusion(struct megasas_instance *instance,
3034         struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
3035         bool fp_possible)
3036 {
3037         u32 device_id;
3038         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
3039         u16 pd_index = 0;
3040         u16 os_timeout_value;
3041         u16 timeout_limit;
3042         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
3043         struct RAID_CONTEXT     *pRAID_Context;
3044         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3045         struct MR_PRIV_DEVICE *mr_device_priv_data;
3046         struct fusion_context *fusion = instance->ctrl_context;
3047         pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
3048
3049         device_id = MEGASAS_DEV_INDEX(scmd);
3050         pd_index = MEGASAS_PD_INDEX(scmd);
3051         os_timeout_value = scmd->request->timeout / HZ;
3052         mr_device_priv_data = scmd->device->hostdata;
3053         cmd->pd_interface = mr_device_priv_data->interface_type;
3054
3055         io_request = cmd->io_request;
3056         /* get RAID_Context pointer */
3057         pRAID_Context = &io_request->RaidContext.raid_context;
3058         pRAID_Context->reg_lock_flags = 0;
3059         pRAID_Context->reg_lock_row_lba = 0;
3060         pRAID_Context->reg_lock_length = 0;
3061         io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3062         io_request->LUN[1] = scmd->device->lun;
3063         pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
3064                 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
3065
3066         /* If FW supports PD sequence number */
3067         if (instance->support_seqnum_jbod_fp) {
3068                 if (instance->use_seqnum_jbod_fp &&
3069                         instance->pd_list[pd_index].driveType == TYPE_DISK) {
3070
3071                         /* More than 256 PD/JBOD support for Ventura */
3072                         if (instance->support_morethan256jbod)
3073                                 pRAID_Context->virtual_disk_tgt_id =
3074                                         pd_sync->seq[pd_index].pd_target_id;
3075                         else
3076                                 pRAID_Context->virtual_disk_tgt_id =
3077                                         cpu_to_le16(device_id +
3078                                         (MAX_PHYSICAL_DEVICES - 1));
3079                         pRAID_Context->config_seq_num =
3080                                 pd_sync->seq[pd_index].seqNum;
3081                         io_request->DevHandle =
3082                                 pd_sync->seq[pd_index].devHandle;
3083                         if (instance->adapter_type >= VENTURA_SERIES) {
3084                                 io_request->RaidContext.raid_context_g35.routing_flags |=
3085                                         (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3086                                 io_request->RaidContext.raid_context_g35.nseg_type |=
3087                                         (1 << RAID_CONTEXT_NSEG_SHIFT);
3088                                 io_request->RaidContext.raid_context_g35.nseg_type |=
3089                                         (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3090                         } else {
3091                                 pRAID_Context->type = MPI2_TYPE_CUDA;
3092                                 pRAID_Context->nseg = 0x1;
3093                                 pRAID_Context->reg_lock_flags |=
3094                                         (MR_RL_FLAGS_SEQ_NUM_ENABLE |
3095                                          MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3096                         }
3097                 } else {
3098                         pRAID_Context->virtual_disk_tgt_id =
3099                                 cpu_to_le16(device_id +
3100                                 (MAX_PHYSICAL_DEVICES - 1));
3101                         pRAID_Context->config_seq_num = 0;
3102                         io_request->DevHandle = cpu_to_le16(0xFFFF);
3103                 }
3104         } else {
3105                 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3106                 pRAID_Context->config_seq_num = 0;
3107
3108                 if (fusion->fast_path_io) {
3109                         local_map_ptr =
3110                                 fusion->ld_drv_map[(instance->map_id & 1)];
3111                         io_request->DevHandle =
3112                                 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3113                 } else {
3114                         io_request->DevHandle = cpu_to_le16(0xFFFF);
3115                 }
3116         }
3117
3118         cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3119
3120         megasas_get_msix_index(instance, scmd, cmd, 1);
3121
3122         if (!fp_possible) {
3123                 /* system pd firmware path */
3124                 io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3125                 cmd->request_desc->SCSIIO.RequestFlags =
3126                         (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3127                                 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3128                 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3129                 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3130         } else {
3131                 if (os_timeout_value)
3132                         os_timeout_value++;
3133
3134                 /* system pd Fast Path */
3135                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3136                 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3137                                 255 : 0xFFFF;
3138                 pRAID_Context->timeout_value =
3139                         cpu_to_le16((os_timeout_value > timeout_limit) ?
3140                         timeout_limit : os_timeout_value);
3141                 if (instance->adapter_type >= INVADER_SERIES)
3142                         io_request->IoFlags |=
3143                                 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3144
3145                 cmd->request_desc->SCSIIO.RequestFlags =
3146                         (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3147                                 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3148         }
3149 }
3150
3151 /**
3152  * megasas_build_io_fusion -    Prepares IOs to devices
3153  * @instance:           Adapter soft state
3154  * @scp:                SCSI command
3155  * @cmd:                Command to be prepared
3156  *
3157  * Invokes helper functions to prepare request frames
3158  * and sets flags appropriate for IO/Non-IO cmd
3159  */
3160 static int
3161 megasas_build_io_fusion(struct megasas_instance *instance,
3162                         struct scsi_cmnd *scp,
3163                         struct megasas_cmd_fusion *cmd)
3164 {
3165         int sge_count;
3166         u8  cmd_type;
3167         struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3168         struct MR_PRIV_DEVICE *mr_device_priv_data;
3169         mr_device_priv_data = scp->device->hostdata;
3170
3171         /* Zero out some fields so they don't get reused */
3172         memset(io_request->LUN, 0x0, 8);
3173         io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3174         io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3175         io_request->EEDPFlags = 0;
3176         io_request->Control = 0;
3177         io_request->EEDPBlockSize = 0;
3178         io_request->ChainOffset = 0;
3179         io_request->RaidContext.raid_context.raid_flags = 0;
3180         io_request->RaidContext.raid_context.type = 0;
3181         io_request->RaidContext.raid_context.nseg = 0;
3182
3183         memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3184         /*
3185          * Just the CDB length,rest of the Flags are zero
3186          * This will be modified for FP in build_ldio_fusion
3187          */
3188         io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3189
3190         switch (cmd_type = megasas_cmd_type(scp)) {
3191         case READ_WRITE_LDIO:
3192                 megasas_build_ldio_fusion(instance, scp, cmd);
3193                 break;
3194         case NON_READ_WRITE_LDIO:
3195                 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3196                 break;
3197         case READ_WRITE_SYSPDIO:
3198                 megasas_build_syspd_fusion(instance, scp, cmd, true);
3199                 break;
3200         case NON_READ_WRITE_SYSPDIO:
3201                 if (instance->secure_jbod_support ||
3202                     mr_device_priv_data->is_tm_capable)
3203                         megasas_build_syspd_fusion(instance, scp, cmd, false);
3204                 else
3205                         megasas_build_syspd_fusion(instance, scp, cmd, true);
3206                 break;
3207         default:
3208                 break;
3209         }
3210
3211         /*
3212          * Construct SGL
3213          */
3214
3215         sge_count = megasas_make_sgl(instance, scp, cmd);
3216
3217         if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3218                 dev_err(&instance->pdev->dev,
3219                         "%s %d sge_count (%d) is out of range. Range is:  0-%d\n",
3220                         __func__, __LINE__, sge_count, instance->max_num_sge);
3221                 return 1;
3222         }
3223
3224         if (instance->adapter_type >= VENTURA_SERIES) {
3225                 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3226                 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3227                 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3228         } else {
3229                 /* numSGE store lower 8 bit of sge_count.
3230                  * numSGEExt store higher 8 bit of sge_count
3231                  */
3232                 io_request->RaidContext.raid_context.num_sge = sge_count;
3233                 io_request->RaidContext.raid_context.num_sge_ext =
3234                         (u8)(sge_count >> 8);
3235         }
3236
3237         io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3238
3239         if (scp->sc_data_direction == DMA_TO_DEVICE)
3240                 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3241         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3242                 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3243
3244         io_request->SGLOffset0 =
3245                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3246
3247         io_request->SenseBufferLowAddress =
3248                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3249         io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3250
3251         cmd->scmd = scp;
3252         scp->SCp.ptr = (char *)cmd;
3253
3254         return 0;
3255 }
3256
3257 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3258 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3259 {
3260         u8 *p;
3261         struct fusion_context *fusion;
3262
3263         fusion = instance->ctrl_context;
3264         p = fusion->req_frames_desc +
3265                 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3266
3267         return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3268 }
3269
3270
3271 /* megasas_prepate_secondRaid1_IO
3272  *  It prepares the raid 1 second IO
3273  */
3274 static void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3275                                            struct megasas_cmd_fusion *cmd,
3276                                            struct megasas_cmd_fusion *r1_cmd)
3277 {
3278         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3279         struct fusion_context *fusion;
3280         fusion = instance->ctrl_context;
3281         req_desc = cmd->request_desc;
3282         /* copy the io request frame as well as 8 SGEs data for r1 command*/
3283         memcpy(r1_cmd->io_request, cmd->io_request,
3284                (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3285         memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3286                (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3287         /*sense buffer is different for r1 command*/
3288         r1_cmd->io_request->SenseBufferLowAddress =
3289                         cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3290         r1_cmd->scmd = cmd->scmd;
3291         req_desc2 = megasas_get_request_descriptor(instance,
3292                                                    (r1_cmd->index - 1));
3293         req_desc2->Words = 0;
3294         r1_cmd->request_desc = req_desc2;
3295         req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3296         req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3297         r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3298         r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3299         r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3300         cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3301                         cpu_to_le16(r1_cmd->index);
3302         r1_cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3303                         cpu_to_le16(cmd->index);
3304         /*MSIxIndex of both commands request descriptors should be same*/
3305         r1_cmd->request_desc->SCSIIO.MSIxIndex =
3306                         cmd->request_desc->SCSIIO.MSIxIndex;
3307         /*span arm is different for r1 cmd*/
3308         r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3309                         cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3310 }
3311
3312 /**
3313  * megasas_build_and_issue_cmd_fusion -Main routine for building and
3314  *                                     issuing non IOCTL cmd
3315  * @instance:                   Adapter soft state
3316  * @scmd:                       pointer to scsi cmd from OS
3317  */
3318 static u32
3319 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3320                                    struct scsi_cmnd *scmd)
3321 {
3322         struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3323         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3324         u32 index;
3325
3326         if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3327                 instance->ldio_threshold &&
3328                 (atomic_inc_return(&instance->ldio_outstanding) >
3329                 instance->ldio_threshold)) {
3330                 atomic_dec(&instance->ldio_outstanding);
3331                 return SCSI_MLQUEUE_DEVICE_BUSY;
3332         }
3333
3334         if (atomic_inc_return(&instance->fw_outstanding) >
3335                         instance->host->can_queue) {
3336                 atomic_dec(&instance->fw_outstanding);
3337                 return SCSI_MLQUEUE_HOST_BUSY;
3338         }
3339
3340         cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3341
3342         if (!cmd) {
3343                 atomic_dec(&instance->fw_outstanding);
3344                 return SCSI_MLQUEUE_HOST_BUSY;
3345         }
3346
3347         index = cmd->index;
3348
3349         req_desc = megasas_get_request_descriptor(instance, index-1);
3350
3351         req_desc->Words = 0;
3352         cmd->request_desc = req_desc;
3353
3354         if (megasas_build_io_fusion(instance, scmd, cmd)) {
3355                 megasas_return_cmd_fusion(instance, cmd);
3356                 dev_err(&instance->pdev->dev, "Error building command\n");
3357                 cmd->request_desc = NULL;
3358                 atomic_dec(&instance->fw_outstanding);
3359                 return SCSI_MLQUEUE_HOST_BUSY;
3360         }
3361
3362         req_desc = cmd->request_desc;
3363         req_desc->SCSIIO.SMID = cpu_to_le16(index);
3364
3365         if (cmd->io_request->ChainOffset != 0 &&
3366             cmd->io_request->ChainOffset != 0xF)
3367                 dev_err(&instance->pdev->dev, "The chain offset value is not "
3368                        "correct : %x\n", cmd->io_request->ChainOffset);
3369         /*
3370          *      if it is raid 1/10 fp write capable.
3371          *      try to get second command from pool and construct it.
3372          *      From FW, it has confirmed that lba values of two PDs
3373          *      corresponds to single R1/10 LD are always same
3374          *
3375          */
3376         /*      driver side count always should be less than max_fw_cmds
3377          *      to get new command
3378          */
3379         if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3380                 r1_cmd = megasas_get_cmd_fusion(instance,
3381                                 (scmd->request->tag + instance->max_fw_cmds));
3382                 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3383         }
3384
3385
3386         /*
3387          * Issue the command to the FW
3388          */
3389
3390         megasas_fire_cmd_fusion(instance, req_desc);
3391
3392         if (r1_cmd)
3393                 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3394
3395
3396         return 0;
3397 }
3398
3399 /**
3400  * megasas_complete_r1_command -
3401  * completes R1 FP write commands which has valid peer smid
3402  * @instance:                   Adapter soft state
3403  * @cmd:                        MPT command frame
3404  *
3405  */
3406 static inline void
3407 megasas_complete_r1_command(struct megasas_instance *instance,
3408                             struct megasas_cmd_fusion *cmd)
3409 {
3410         u8 *sense, status, ex_status;
3411         u32 data_length;
3412         u16 peer_smid;
3413         struct fusion_context *fusion;
3414         struct megasas_cmd_fusion *r1_cmd = NULL;
3415         struct scsi_cmnd *scmd_local = NULL;
3416         struct RAID_CONTEXT_G35 *rctx_g35;
3417
3418         rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3419         fusion = instance->ctrl_context;
3420         peer_smid = le16_to_cpu(rctx_g35->flow_specific.peer_smid);
3421
3422         r1_cmd = fusion->cmd_list[peer_smid - 1];
3423         scmd_local = cmd->scmd;
3424         status = rctx_g35->status;
3425         ex_status = rctx_g35->ex_status;
3426         data_length = cmd->io_request->DataLength;
3427         sense = cmd->sense;
3428
3429         cmd->cmd_completed = true;
3430
3431         /* Check if peer command is completed or not*/
3432         if (r1_cmd->cmd_completed) {
3433                 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3434                 if (rctx_g35->status != MFI_STAT_OK) {
3435                         status = rctx_g35->status;
3436                         ex_status = rctx_g35->ex_status;
3437                         data_length = r1_cmd->io_request->DataLength;
3438                         sense = r1_cmd->sense;
3439                 }
3440
3441                 megasas_return_cmd_fusion(instance, r1_cmd);
3442                 map_cmd_status(fusion, scmd_local, status, ex_status,
3443                                le32_to_cpu(data_length), sense);
3444                 if (instance->ldio_threshold &&
3445                     megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3446                         atomic_dec(&instance->ldio_outstanding);
3447                 scmd_local->SCp.ptr = NULL;
3448                 megasas_return_cmd_fusion(instance, cmd);
3449                 scsi_dma_unmap(scmd_local);
3450                 scmd_local->scsi_done(scmd_local);
3451         }
3452 }
3453
3454 /**
3455  * complete_cmd_fusion -        Completes command
3456  * @instance:                   Adapter soft state
3457  * @MSIxIndex:                  MSI number
3458  * @irq_context:                IRQ context
3459  *
3460  * Completes all commands that is in reply descriptor queue
3461  */
3462 static int
3463 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex,
3464                     struct megasas_irq_context *irq_context)
3465 {
3466         union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3467         struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3468         struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3469         struct fusion_context *fusion;
3470         struct megasas_cmd *cmd_mfi;
3471         struct megasas_cmd_fusion *cmd_fusion;
3472         u16 smid, num_completed;
3473         u8 reply_descript_type, *sense, status, extStatus;
3474         u32 device_id, data_length;
3475         union desc_value d_val;
3476         struct LD_LOAD_BALANCE_INFO *lbinfo;
3477         int threshold_reply_count = 0;
3478         struct scsi_cmnd *scmd_local = NULL;
3479         struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3480         struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3481
3482         fusion = instance->ctrl_context;
3483
3484         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3485                 return IRQ_HANDLED;
3486
3487         desc = fusion->reply_frames_desc[MSIxIndex] +
3488                                 fusion->last_reply_idx[MSIxIndex];
3489
3490         reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3491
3492         d_val.word = desc->Words;
3493
3494         reply_descript_type = reply_desc->ReplyFlags &
3495                 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3496
3497         if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3498                 return IRQ_NONE;
3499
3500         num_completed = 0;
3501
3502         while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3503                d_val.u.high != cpu_to_le32(UINT_MAX)) {
3504
3505                 smid = le16_to_cpu(reply_desc->SMID);
3506                 cmd_fusion = fusion->cmd_list[smid - 1];
3507                 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3508                                                 cmd_fusion->io_request;
3509
3510                 scmd_local = cmd_fusion->scmd;
3511                 status = scsi_io_req->RaidContext.raid_context.status;
3512                 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3513                 sense = cmd_fusion->sense;
3514                 data_length = scsi_io_req->DataLength;
3515
3516                 switch (scsi_io_req->Function) {
3517                 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3518                         mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3519                                                 cmd_fusion->io_request;
3520                         mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3521                                                 &mr_tm_req->TmRequest;
3522                         dev_dbg(&instance->pdev->dev, "TM completion:"
3523                                 "type: 0x%x TaskMID: 0x%x\n",
3524                                 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3525                         complete(&cmd_fusion->done);
3526                         break;
3527                 case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
3528                         /* Update load balancing info */
3529                         if (fusion->load_balance_info &&
3530                             (cmd_fusion->scmd->SCp.Status &
3531                             MEGASAS_LOAD_BALANCE_FLAG)) {
3532                                 device_id = MEGASAS_DEV_INDEX(scmd_local);
3533                                 lbinfo = &fusion->load_balance_info[device_id];
3534                                 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3535                                 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3536                         }
3537                         fallthrough;    /* and complete IO */
3538                 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3539                         atomic_dec(&instance->fw_outstanding);
3540                         if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3541                                 map_cmd_status(fusion, scmd_local, status,
3542                                                extStatus, le32_to_cpu(data_length),
3543                                                sense);
3544                                 if (instance->ldio_threshold &&
3545                                     (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3546                                         atomic_dec(&instance->ldio_outstanding);
3547                                 scmd_local->SCp.ptr = NULL;
3548                                 megasas_return_cmd_fusion(instance, cmd_fusion);
3549                                 scsi_dma_unmap(scmd_local);
3550                                 scmd_local->scsi_done(scmd_local);
3551                         } else  /* Optimal VD - R1 FP command completion. */
3552                                 megasas_complete_r1_command(instance, cmd_fusion);
3553                         break;
3554                 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3555                         cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3556                         /* Poll mode. Dummy free.
3557                          * In case of Interrupt mode, caller has reverse check.
3558                          */
3559                         if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3560                                 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3561                                 megasas_return_cmd(instance, cmd_mfi);
3562                         } else
3563                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3564                         break;
3565                 }
3566
3567                 fusion->last_reply_idx[MSIxIndex]++;
3568                 if (fusion->last_reply_idx[MSIxIndex] >=
3569                     fusion->reply_q_depth)
3570                         fusion->last_reply_idx[MSIxIndex] = 0;
3571
3572                 desc->Words = cpu_to_le64(ULLONG_MAX);
3573                 num_completed++;
3574                 threshold_reply_count++;
3575
3576                 /* Get the next reply descriptor */
3577                 if (!fusion->last_reply_idx[MSIxIndex])
3578                         desc = fusion->reply_frames_desc[MSIxIndex];
3579                 else
3580                         desc++;
3581
3582                 reply_desc =
3583                   (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3584
3585                 d_val.word = desc->Words;
3586
3587                 reply_descript_type = reply_desc->ReplyFlags &
3588                         MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3589
3590                 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3591                         break;
3592                 /*
3593                  * Write to reply post host index register after completing threshold
3594                  * number of reply counts and still there are more replies in reply queue
3595                  * pending to be completed
3596                  */
3597                 if (threshold_reply_count >= instance->threshold_reply_count) {
3598                         if (instance->msix_combined)
3599                                 writel(((MSIxIndex & 0x7) << 24) |
3600                                         fusion->last_reply_idx[MSIxIndex],
3601                                         instance->reply_post_host_index_addr[MSIxIndex/8]);
3602                         else
3603                                 writel((MSIxIndex << 24) |
3604                                         fusion->last_reply_idx[MSIxIndex],
3605                                         instance->reply_post_host_index_addr[0]);
3606                         threshold_reply_count = 0;
3607                         if (irq_context) {
3608                                 if (!irq_context->irq_poll_scheduled) {
3609                                         irq_context->irq_poll_scheduled = true;
3610                                         irq_context->irq_line_enable = true;
3611                                         irq_poll_sched(&irq_context->irqpoll);
3612                                 }
3613                                 return num_completed;
3614                         }
3615                 }
3616         }
3617
3618         if (num_completed) {
3619                 wmb();
3620                 if (instance->msix_combined)
3621                         writel(((MSIxIndex & 0x7) << 24) |
3622                                 fusion->last_reply_idx[MSIxIndex],
3623                                 instance->reply_post_host_index_addr[MSIxIndex/8]);
3624                 else
3625                         writel((MSIxIndex << 24) |
3626                                 fusion->last_reply_idx[MSIxIndex],
3627                                 instance->reply_post_host_index_addr[0]);
3628                 megasas_check_and_restore_queue_depth(instance);
3629         }
3630         return num_completed;
3631 }
3632
3633 /**
3634  * megasas_enable_irq_poll() - enable irqpoll
3635  * @instance:                   Adapter soft state
3636  */
3637 static void megasas_enable_irq_poll(struct megasas_instance *instance)
3638 {
3639         u32 count, i;
3640         struct megasas_irq_context *irq_ctx;
3641
3642         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3643
3644         for (i = 0; i < count; i++) {
3645                 irq_ctx = &instance->irq_context[i];
3646                 irq_poll_enable(&irq_ctx->irqpoll);
3647         }
3648 }
3649
3650 /**
3651  * megasas_sync_irqs -  Synchronizes all IRQs owned by adapter
3652  * @instance_addr:                      Adapter soft state address
3653  */
3654 static void megasas_sync_irqs(unsigned long instance_addr)
3655 {
3656         u32 count, i;
3657         struct megasas_instance *instance =
3658                 (struct megasas_instance *)instance_addr;
3659         struct megasas_irq_context *irq_ctx;
3660
3661         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3662
3663         for (i = 0; i < count; i++) {
3664                 synchronize_irq(pci_irq_vector(instance->pdev, i));
3665                 irq_ctx = &instance->irq_context[i];
3666                 irq_poll_disable(&irq_ctx->irqpoll);
3667                 if (irq_ctx->irq_poll_scheduled) {
3668                         irq_ctx->irq_poll_scheduled = false;
3669                         enable_irq(irq_ctx->os_irq);
3670                         complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3671                 }
3672         }
3673 }
3674
3675 /**
3676  * megasas_irqpoll() - process a queue for completed reply descriptors
3677  * @irqpoll:    IRQ poll structure associated with queue to poll.
3678  * @budget:     Threshold of reply descriptors to process per poll.
3679  *
3680  * Return: The number of entries processed.
3681  */
3682
3683 int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3684 {
3685         struct megasas_irq_context *irq_ctx;
3686         struct megasas_instance *instance;
3687         int num_entries;
3688
3689         irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3690         instance = irq_ctx->instance;
3691
3692         if (irq_ctx->irq_line_enable) {
3693                 disable_irq_nosync(irq_ctx->os_irq);
3694                 irq_ctx->irq_line_enable = false;
3695         }
3696
3697         num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3698         if (num_entries < budget) {
3699                 irq_poll_complete(irqpoll);
3700                 irq_ctx->irq_poll_scheduled = false;
3701                 enable_irq(irq_ctx->os_irq);
3702                 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3703         }
3704
3705         return num_entries;
3706 }
3707
3708 /**
3709  * megasas_complete_cmd_dpc_fusion -    Completes command
3710  * @instance_addr:                      Adapter soft state address
3711  *
3712  * Tasklet to complete cmds
3713  */
3714 static void
3715 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3716 {
3717         struct megasas_instance *instance =
3718                 (struct megasas_instance *)instance_addr;
3719         struct megasas_irq_context *irq_ctx = NULL;
3720         u32 count, MSIxIndex;
3721
3722         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3723
3724         /* If we have already declared adapter dead, donot complete cmds */
3725         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3726                 return;
3727
3728         for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) {
3729                 irq_ctx = &instance->irq_context[MSIxIndex];
3730                 complete_cmd_fusion(instance, MSIxIndex, irq_ctx);
3731         }
3732 }
3733
3734 /**
3735  * megasas_isr_fusion - isr entry point
3736  * @irq:        IRQ number
3737  * @devp:       IRQ context
3738  */
3739 static irqreturn_t megasas_isr_fusion(int irq, void *devp)
3740 {
3741         struct megasas_irq_context *irq_context = devp;
3742         struct megasas_instance *instance = irq_context->instance;
3743         u32 mfiStatus;
3744
3745         if (instance->mask_interrupts)
3746                 return IRQ_NONE;
3747
3748         if (irq_context->irq_poll_scheduled)
3749                 return IRQ_HANDLED;
3750
3751         if (!instance->msix_vectors) {
3752                 mfiStatus = instance->instancet->clear_intr(instance);
3753                 if (!mfiStatus)
3754                         return IRQ_NONE;
3755         }
3756
3757         /* If we are resetting, bail */
3758         if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3759                 instance->instancet->clear_intr(instance);
3760                 return IRQ_HANDLED;
3761         }
3762
3763         return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3764                         ? IRQ_HANDLED : IRQ_NONE;
3765 }
3766
3767 /**
3768  * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3769  * @instance:                   Adapter soft state
3770  * @mfi_cmd:                    megasas_cmd pointer
3771  *
3772  */
3773 static void
3774 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3775                         struct megasas_cmd *mfi_cmd)
3776 {
3777         struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3778         struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3779         struct megasas_cmd_fusion *cmd;
3780         struct fusion_context *fusion;
3781         struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3782
3783         fusion = instance->ctrl_context;
3784
3785         cmd = megasas_get_cmd_fusion(instance,
3786                         instance->max_scsi_cmds + mfi_cmd->index);
3787
3788         /*  Save the smid. To be used for returning the cmd */
3789         mfi_cmd->context.smid = cmd->index;
3790
3791         /*
3792          * For cmds where the flag is set, store the flag and check
3793          * on completion. For cmds with this flag, don't call
3794          * megasas_complete_cmd
3795          */
3796
3797         if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3798                 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3799
3800         io_req = cmd->io_request;
3801
3802         if (instance->adapter_type >= INVADER_SERIES) {
3803                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3804                         (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3805                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3806                 sgl_ptr_end->Flags = 0;
3807         }
3808
3809         mpi25_ieee_chain =
3810           (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3811
3812         io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3813         io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3814                                        SGL) / 4;
3815         io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3816
3817         mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3818
3819         mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3820                 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3821
3822         mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3823 }
3824
3825 /**
3826  * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3827  * @instance:                   Adapter soft state
3828  * @cmd:                        mfi cmd to build
3829  *
3830  */
3831 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3832 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3833 {
3834         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3835         u16 index;
3836
3837         build_mpt_mfi_pass_thru(instance, cmd);
3838         index = cmd->context.smid;
3839
3840         req_desc = megasas_get_request_descriptor(instance, index - 1);
3841
3842         req_desc->Words = 0;
3843         req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3844                                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3845
3846         req_desc->SCSIIO.SMID = cpu_to_le16(index);
3847
3848         return req_desc;
3849 }
3850
3851 /**
3852  * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3853  * @instance:                   Adapter soft state
3854  * @cmd:                        mfi cmd pointer
3855  *
3856  */
3857 static void
3858 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3859                           struct megasas_cmd *cmd)
3860 {
3861         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3862
3863         req_desc = build_mpt_cmd(instance, cmd);
3864
3865         megasas_fire_cmd_fusion(instance, req_desc);
3866         return;
3867 }
3868
3869 /**
3870  * megasas_release_fusion -     Reverses the FW initialization
3871  * @instance:                   Adapter soft state
3872  */
3873 void
3874 megasas_release_fusion(struct megasas_instance *instance)
3875 {
3876         megasas_free_ioc_init_cmd(instance);
3877         megasas_free_cmds(instance);
3878         megasas_free_cmds_fusion(instance);
3879
3880         iounmap(instance->reg_set);
3881
3882         pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3883 }
3884
3885 /**
3886  * megasas_read_fw_status_reg_fusion - returns the current FW status value
3887  * @instance:                   Adapter soft state
3888  */
3889 static u32
3890 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
3891 {
3892         return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
3893 }
3894
3895 /**
3896  * megasas_alloc_host_crash_buffer -    Host buffers for Crash dump collection from Firmware
3897  * @instance:                           Controller's soft instance
3898  * @return:                             Number of allocated host crash buffers
3899  */
3900 static void
3901 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3902 {
3903         unsigned int i;
3904
3905         for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3906                 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3907                 if (!instance->crash_buf[i]) {
3908                         dev_info(&instance->pdev->dev, "Firmware crash dump "
3909                                 "memory allocation failed at index %d\n", i);
3910                         break;
3911                 }
3912         }
3913         instance->drv_buf_alloc = i;
3914 }
3915
3916 /**
3917  * megasas_free_host_crash_buffer -     Host buffers for Crash dump collection from Firmware
3918  * @instance:                           Controller's soft instance
3919  */
3920 void
3921 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3922 {
3923         unsigned int i;
3924         for (i = 0; i < instance->drv_buf_alloc; i++) {
3925                 if (instance->crash_buf[i])
3926                         vfree(instance->crash_buf[i]);
3927         }
3928         instance->drv_buf_index = 0;
3929         instance->drv_buf_alloc = 0;
3930         instance->fw_crash_state = UNAVAILABLE;
3931         instance->fw_crash_buffer_size = 0;
3932 }
3933
3934 /**
3935  * megasas_adp_reset_fusion -   For controller reset
3936  * @instance:                           Controller's soft instance
3937  * @regs:                               MFI register set
3938  */
3939 static int
3940 megasas_adp_reset_fusion(struct megasas_instance *instance,
3941                          struct megasas_register_set __iomem *regs)
3942 {
3943         u32 host_diag, abs_state, retry;
3944
3945         /* Now try to reset the chip */
3946         writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3947         writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3948         writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3949         writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3950         writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3951         writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3952         writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3953
3954         /* Check that the diag write enable (DRWE) bit is on */
3955         host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3956         retry = 0;
3957         while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3958                 msleep(100);
3959                 host_diag = megasas_readl(instance,
3960                                           &instance->reg_set->fusion_host_diag);
3961                 if (retry++ == 100) {
3962                         dev_warn(&instance->pdev->dev,
3963                                 "Host diag unlock failed from %s %d\n",
3964                                 __func__, __LINE__);
3965                         break;
3966                 }
3967         }
3968         if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3969                 return -1;
3970
3971         /* Send chip reset command */
3972         writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3973                 &instance->reg_set->fusion_host_diag);
3974         msleep(3000);
3975
3976         /* Make sure reset adapter bit is cleared */
3977         host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3978         retry = 0;
3979         while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3980                 msleep(100);
3981                 host_diag = megasas_readl(instance,
3982                                           &instance->reg_set->fusion_host_diag);
3983                 if (retry++ == 1000) {
3984                         dev_warn(&instance->pdev->dev,
3985                                 "Diag reset adapter never cleared %s %d\n",
3986                                 __func__, __LINE__);
3987                         break;
3988                 }
3989         }
3990         if (host_diag & HOST_DIAG_RESET_ADAPTER)
3991                 return -1;
3992
3993         abs_state = instance->instancet->read_fw_status_reg(instance)
3994                         & MFI_STATE_MASK;
3995         retry = 0;
3996
3997         while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3998                 msleep(100);
3999                 abs_state = instance->instancet->
4000                         read_fw_status_reg(instance) & MFI_STATE_MASK;
4001         }
4002         if (abs_state <= MFI_STATE_FW_INIT) {
4003                 dev_warn(&instance->pdev->dev,
4004                         "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4005                         abs_state, __func__, __LINE__);
4006                 return -1;
4007         }
4008
4009         return 0;
4010 }
4011
4012 /**
4013  * megasas_check_reset_fusion - For controller reset check
4014  * @instance:                           Controller's soft instance
4015  * @regs:                               MFI register set
4016  */
4017 static int
4018 megasas_check_reset_fusion(struct megasas_instance *instance,
4019                            struct megasas_register_set __iomem *regs)
4020 {
4021         return 0;
4022 }
4023
4024 /**
4025  * megasas_trigger_snap_dump -  Trigger snap dump in FW
4026  * @instance:                   Soft instance of adapter
4027  */
4028 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4029 {
4030         int j;
4031         u32 fw_state, abs_state;
4032
4033         if (!instance->disableOnlineCtrlReset) {
4034                 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4035                 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4036                        &instance->reg_set->doorbell);
4037                 readl(&instance->reg_set->doorbell);
4038         }
4039
4040         for (j = 0; j < instance->snapdump_wait_time; j++) {
4041                 abs_state = instance->instancet->read_fw_status_reg(instance);
4042                 fw_state = abs_state & MFI_STATE_MASK;
4043                 if (fw_state == MFI_STATE_FAULT) {
4044                         dev_printk(KERN_ERR, &instance->pdev->dev,
4045                                    "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4046                                    abs_state & MFI_STATE_FAULT_CODE,
4047                                    abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4048                         return;
4049                 }
4050                 msleep(1000);
4051         }
4052 }
4053
4054 /* This function waits for outstanding commands on fusion to complete */
4055 static int
4056 megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
4057                                     int reason, int *convert)
4058 {
4059         int i, outstanding, retval = 0, hb_seconds_missed = 0;
4060         u32 fw_state, abs_state;
4061         u32 waittime_for_io_completion;
4062
4063         waittime_for_io_completion =
4064                 min_t(u32, resetwaittime,
4065                         (resetwaittime - instance->snapdump_wait_time));
4066
4067         if (reason == MFI_IO_TIMEOUT_OCR) {
4068                 dev_info(&instance->pdev->dev,
4069                         "MFI command is timed out\n");
4070                 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4071                 if (instance->snapdump_wait_time)
4072                         megasas_trigger_snap_dump(instance);
4073                 retval = 1;
4074                 goto out;
4075         }
4076
4077         for (i = 0; i < waittime_for_io_completion; i++) {
4078                 /* Check if firmware is in fault state */
4079                 abs_state = instance->instancet->read_fw_status_reg(instance);
4080                 fw_state = abs_state & MFI_STATE_MASK;
4081                 if (fw_state == MFI_STATE_FAULT) {
4082                         dev_printk(KERN_ERR, &instance->pdev->dev,
4083                                    "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4084                                    abs_state & MFI_STATE_FAULT_CODE,
4085                                    abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4086                         megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4087                         if (instance->requestorId && reason) {
4088                                 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4089                                 " state while polling during"
4090                                 " I/O timeout handling for %d\n",
4091                                 instance->host->host_no);
4092                                 *convert = 1;
4093                         }
4094
4095                         retval = 1;
4096                         goto out;
4097                 }
4098
4099
4100                 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4101                 if (instance->requestorId && !reason) {
4102                         retval = 1;
4103                         goto out;
4104                 }
4105
4106                 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4107                 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
4108                         if (instance->hb_host_mem->HB.fwCounter !=
4109                             instance->hb_host_mem->HB.driverCounter) {
4110                                 instance->hb_host_mem->HB.driverCounter =
4111                                         instance->hb_host_mem->HB.fwCounter;
4112                                 hb_seconds_missed = 0;
4113                         } else {
4114                                 hb_seconds_missed++;
4115                                 if (hb_seconds_missed ==
4116                                     (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
4117                                         dev_warn(&instance->pdev->dev, "SR-IOV:"
4118                                                " Heartbeat never completed "
4119                                                " while polling during I/O "
4120                                                " timeout handling for "
4121                                                "scsi%d.\n",
4122                                                instance->host->host_no);
4123                                                *convert = 1;
4124                                                retval = 1;
4125                                                goto out;
4126                                 }
4127                         }
4128                 }
4129
4130                 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4131                 outstanding = atomic_read(&instance->fw_outstanding);
4132                 if (!outstanding)
4133                         goto out;
4134
4135                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4136                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
4137                                "commands to complete for scsi%d\n", i,
4138                                outstanding, instance->host->host_no);
4139                 }
4140                 msleep(1000);
4141         }
4142
4143         if (instance->snapdump_wait_time) {
4144                 megasas_trigger_snap_dump(instance);
4145                 retval = 1;
4146                 goto out;
4147         }
4148
4149         if (atomic_read(&instance->fw_outstanding)) {
4150                 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
4151                        "will reset adapter scsi%d.\n",
4152                        instance->host->host_no);
4153                 *convert = 1;
4154                 retval = 1;
4155         }
4156
4157 out:
4158         return retval;
4159 }
4160
4161 void  megasas_reset_reply_desc(struct megasas_instance *instance)
4162 {
4163         int i, j, count;
4164         struct fusion_context *fusion;
4165         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4166
4167         fusion = instance->ctrl_context;
4168         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4169         for (i = 0 ; i < count ; i++) {
4170                 fusion->last_reply_idx[i] = 0;
4171                 reply_desc = fusion->reply_frames_desc[i];
4172                 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4173                         reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4174         }
4175 }
4176
4177 /*
4178  * megasas_refire_mgmt_cmd :    Re-fire management commands
4179  * @instance:                           Controller's soft instance
4180 */
4181 static void megasas_refire_mgmt_cmd(struct megasas_instance *instance,
4182                              bool return_ioctl)
4183 {
4184         int j;
4185         struct megasas_cmd_fusion *cmd_fusion;
4186         struct fusion_context *fusion;
4187         struct megasas_cmd *cmd_mfi;
4188         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4189         struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
4190         u16 smid;
4191         bool refire_cmd = false;
4192         u8 result;
4193         u32 opcode = 0;
4194
4195         fusion = instance->ctrl_context;
4196
4197         /* Re-fire management commands.
4198          * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4199          */
4200         for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4201                 cmd_fusion = fusion->cmd_list[j];
4202                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4203                 smid = le16_to_cpu(cmd_mfi->context.smid);
4204                 result = REFIRE_CMD;
4205
4206                 if (!smid)
4207                         continue;
4208
4209                 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4210
4211                 switch (cmd_mfi->frame->hdr.cmd) {
4212                 case MFI_CMD_DCMD:
4213                         opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4214                          /* Do not refire shutdown command */
4215                         if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4216                                 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4217                                 result = COMPLETE_CMD;
4218                                 break;
4219                         }
4220
4221                         refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4222                                       (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4223                                       !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4224
4225                         if (!refire_cmd)
4226                                 result = RETURN_CMD;
4227
4228                         break;
4229                 case MFI_CMD_NVME:
4230                         if (!instance->support_nvme_passthru) {
4231                                 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4232                                 result = COMPLETE_CMD;
4233                         }
4234
4235                         break;
4236                 case MFI_CMD_TOOLBOX:
4237                         if (!instance->support_pci_lane_margining) {
4238                                 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4239                                 result = COMPLETE_CMD;
4240                         }
4241
4242                         break;
4243                 default:
4244                         break;
4245                 }
4246
4247                 if (return_ioctl && cmd_mfi->sync_cmd &&
4248                     cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT) {
4249                         dev_err(&instance->pdev->dev,
4250                                 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x\n",
4251                                 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4252                                 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4253                         cmd_mfi->cmd_status_drv = DCMD_BUSY;
4254                         result = COMPLETE_CMD;
4255                 }
4256
4257                 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
4258                                 cmd_fusion->io_request;
4259                 if (scsi_io_req->Function == MPI2_FUNCTION_SCSI_TASK_MGMT)
4260                         result = RETURN_CMD;
4261
4262                 switch (result) {
4263                 case REFIRE_CMD:
4264                         megasas_fire_cmd_fusion(instance, req_desc);
4265                         break;
4266                 case RETURN_CMD:
4267                         megasas_return_cmd(instance, cmd_mfi);
4268                         break;
4269                 case COMPLETE_CMD:
4270                         megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4271                         break;
4272                 }
4273         }
4274 }
4275
4276 /*
4277  * megasas_return_polled_cmds: Return polled mode commands back to the pool
4278  *                             before initiating an OCR.
4279  * @instance:                  Controller's soft instance
4280  */
4281 static void
4282 megasas_return_polled_cmds(struct megasas_instance *instance)
4283 {
4284         int i;
4285         struct megasas_cmd_fusion *cmd_fusion;
4286         struct fusion_context *fusion;
4287         struct megasas_cmd *cmd_mfi;
4288
4289         fusion = instance->ctrl_context;
4290
4291         for (i = instance->max_scsi_cmds; i < instance->max_fw_cmds; i++) {
4292                 cmd_fusion = fusion->cmd_list[i];
4293                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4294
4295                 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
4296                         if (megasas_dbg_lvl & OCR_DEBUG)
4297                                 dev_info(&instance->pdev->dev,
4298                                          "%s %d return cmd 0x%x opcode 0x%x\n",
4299                                          __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4300                                          le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4301                         cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
4302                         megasas_return_cmd(instance, cmd_mfi);
4303                 }
4304         }
4305 }
4306
4307 /*
4308  * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4309  * @instance: per adapter struct
4310  * @channel: the channel assigned by the OS
4311  * @id: the id assigned by the OS
4312  *
4313  * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4314  */
4315
4316 static int megasas_track_scsiio(struct megasas_instance *instance,
4317                 int id, int channel)
4318 {
4319         int i, found = 0;
4320         struct megasas_cmd_fusion *cmd_fusion;
4321         struct fusion_context *fusion;
4322         fusion = instance->ctrl_context;
4323
4324         for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4325                 cmd_fusion = fusion->cmd_list[i];
4326                 if (cmd_fusion->scmd &&
4327                         (cmd_fusion->scmd->device->id == id &&
4328                         cmd_fusion->scmd->device->channel == channel)) {
4329                         dev_info(&instance->pdev->dev,
4330                                 "SCSI commands pending to target"
4331                                 "channel %d id %d \tSMID: 0x%x\n",
4332                                 channel, id, cmd_fusion->index);
4333                         scsi_print_command(cmd_fusion->scmd);
4334                         found = 1;
4335                         break;
4336                 }
4337         }
4338
4339         return found ? FAILED : SUCCESS;
4340 }
4341
4342 /**
4343  * megasas_tm_response_code - translation of device response code
4344  * @instance:   Controller's soft instance
4345  * @mpi_reply:  MPI reply returned by firmware
4346  *
4347  * Return nothing.
4348  */
4349 static void
4350 megasas_tm_response_code(struct megasas_instance *instance,
4351                 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4352 {
4353         char *desc;
4354
4355         switch (mpi_reply->ResponseCode) {
4356         case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4357                 desc = "task management request completed";
4358                 break;
4359         case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4360                 desc = "invalid frame";
4361                 break;
4362         case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4363                 desc = "task management request not supported";
4364                 break;
4365         case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4366                 desc = "task management request failed";
4367                 break;
4368         case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4369                 desc = "task management request succeeded";
4370                 break;
4371         case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4372                 desc = "invalid lun";
4373                 break;
4374         case 0xA:
4375                 desc = "overlapped tag attempted";
4376                 break;
4377         case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4378                 desc = "task queued, however not sent to target";
4379                 break;
4380         default:
4381                 desc = "unknown";
4382                 break;
4383         }
4384         dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4385                 mpi_reply->ResponseCode, desc);
4386         dev_dbg(&instance->pdev->dev,
4387                 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4388                 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4389                 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4390                 mpi_reply->Function, mpi_reply->TaskType,
4391                 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4392 }
4393
4394 /**
4395  * megasas_issue_tm - main routine for sending tm requests
4396  * @instance: per adapter struct
4397  * @device_handle: device handle
4398  * @channel: the channel assigned by the OS
4399  * @id: the id assigned by the OS
4400  * @smid_task: smid assigned to the task
4401  * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4402  * @mr_device_priv_data: private data
4403  * Context: user
4404  *
4405  * MegaRaid use MPT interface for Task Magement request.
4406  * A generic API for sending task management requests to firmware.
4407  *
4408  * Return SUCCESS or FAILED.
4409  */
4410 static int
4411 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4412         uint channel, uint id, u16 smid_task, u8 type,
4413         struct MR_PRIV_DEVICE *mr_device_priv_data)
4414 {
4415         struct MR_TASK_MANAGE_REQUEST *mr_request;
4416         struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4417         unsigned long timeleft;
4418         struct megasas_cmd_fusion *cmd_fusion;
4419         struct megasas_cmd *cmd_mfi;
4420         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4421         struct fusion_context *fusion = NULL;
4422         struct megasas_cmd_fusion *scsi_lookup;
4423         int rc;
4424         int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4425         struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4426
4427         fusion = instance->ctrl_context;
4428
4429         cmd_mfi = megasas_get_cmd(instance);
4430
4431         if (!cmd_mfi) {
4432                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4433                         __func__, __LINE__);
4434                 return -ENOMEM;
4435         }
4436
4437         cmd_fusion = megasas_get_cmd_fusion(instance,
4438                         instance->max_scsi_cmds + cmd_mfi->index);
4439
4440         /*  Save the smid. To be used for returning the cmd */
4441         cmd_mfi->context.smid = cmd_fusion->index;
4442
4443         req_desc = megasas_get_request_descriptor(instance,
4444                         (cmd_fusion->index - 1));
4445
4446         cmd_fusion->request_desc = req_desc;
4447         req_desc->Words = 0;
4448
4449         mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4450         memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4451         mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4452         mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4453         mpi_request->DevHandle = cpu_to_le16(device_handle);
4454         mpi_request->TaskType = type;
4455         mpi_request->TaskMID = cpu_to_le16(smid_task);
4456         mpi_request->LUN[1] = 0;
4457
4458
4459         req_desc = cmd_fusion->request_desc;
4460         req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4461         req_desc->HighPriority.RequestFlags =
4462                 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4463                 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4464         req_desc->HighPriority.MSIxIndex =  0;
4465         req_desc->HighPriority.LMID = 0;
4466         req_desc->HighPriority.Reserved1 = 0;
4467
4468         if (channel < MEGASAS_MAX_PD_CHANNELS)
4469                 mr_request->tmReqFlags.isTMForPD = 1;
4470         else
4471                 mr_request->tmReqFlags.isTMForLD = 1;
4472
4473         init_completion(&cmd_fusion->done);
4474         megasas_fire_cmd_fusion(instance, req_desc);
4475
4476         switch (type) {
4477         case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4478                 timeout = mr_device_priv_data->task_abort_tmo;
4479                 break;
4480         case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4481                 timeout = mr_device_priv_data->target_reset_tmo;
4482                 break;
4483         }
4484
4485         timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4486
4487         if (!timeleft) {
4488                 dev_err(&instance->pdev->dev,
4489                         "task mgmt type 0x%x timed out\n", type);
4490                 mutex_unlock(&instance->reset_mutex);
4491                 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4492                 mutex_lock(&instance->reset_mutex);
4493                 return rc;
4494         }
4495
4496         mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4497         megasas_tm_response_code(instance, mpi_reply);
4498
4499         megasas_return_cmd(instance, cmd_mfi);
4500         rc = SUCCESS;
4501         switch (type) {
4502         case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4503                 scsi_lookup = fusion->cmd_list[smid_task - 1];
4504
4505                 if (scsi_lookup->scmd == NULL)
4506                         break;
4507                 else {
4508                         instance->instancet->disable_intr(instance);
4509                         megasas_sync_irqs((unsigned long)instance);
4510                         instance->instancet->enable_intr(instance);
4511                         megasas_enable_irq_poll(instance);
4512                         if (scsi_lookup->scmd == NULL)
4513                                 break;
4514                 }
4515                 rc = FAILED;
4516                 break;
4517
4518         case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4519                 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4520                         break;
4521                 instance->instancet->disable_intr(instance);
4522                 megasas_sync_irqs((unsigned long)instance);
4523                 rc = megasas_track_scsiio(instance, id, channel);
4524                 instance->instancet->enable_intr(instance);
4525                 megasas_enable_irq_poll(instance);
4526
4527                 break;
4528         case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4529         case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4530                 break;
4531         default:
4532                 rc = FAILED;
4533                 break;
4534         }
4535
4536         return rc;
4537
4538 }
4539
4540 /*
4541  * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4542  * @instance: per adapter struct
4543  *
4544  * Return Non Zero index, if SMID found in outstanding commands
4545  */
4546 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4547 {
4548         int i, ret = 0;
4549         struct megasas_instance *instance;
4550         struct megasas_cmd_fusion *cmd_fusion;
4551         struct fusion_context *fusion;
4552
4553         instance = (struct megasas_instance *)scmd->device->host->hostdata;
4554
4555         fusion = instance->ctrl_context;
4556
4557         for (i = 0; i < instance->max_scsi_cmds; i++) {
4558                 cmd_fusion = fusion->cmd_list[i];
4559                 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4560                         scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4561                                 " SMID: %d\n", cmd_fusion->index);
4562                         ret = cmd_fusion->index;
4563                         break;
4564                 }
4565         }
4566
4567         return ret;
4568 }
4569
4570 /*
4571 * megasas_get_tm_devhandle - Get devhandle for TM request
4572 * @sdev-                     OS provided scsi device
4573 *
4574 * Returns-                   devhandle/targetID of SCSI device
4575 */
4576 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4577 {
4578         u16 pd_index = 0;
4579         u32 device_id;
4580         struct megasas_instance *instance;
4581         struct fusion_context *fusion;
4582         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4583         u16 devhandle = (u16)ULONG_MAX;
4584
4585         instance = (struct megasas_instance *)sdev->host->hostdata;
4586         fusion = instance->ctrl_context;
4587
4588         if (!MEGASAS_IS_LOGICAL(sdev)) {
4589                 if (instance->use_seqnum_jbod_fp) {
4590                         pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4591                                     + sdev->id;
4592                         pd_sync = (void *)fusion->pd_seq_sync
4593                                         [(instance->pd_seq_map_id - 1) & 1];
4594                         devhandle = pd_sync->seq[pd_index].devHandle;
4595                 } else
4596                         sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4597                                 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4598         } else {
4599                 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4600                                 + sdev->id;
4601                 devhandle = device_id;
4602         }
4603
4604         return devhandle;
4605 }
4606
4607 /*
4608  * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4609  * @scmd : pointer to scsi command object
4610  *
4611  * Return SUCCESS, if command aborted else FAILED
4612  */
4613
4614 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4615 {
4616         struct megasas_instance *instance;
4617         u16 smid, devhandle;
4618         int ret;
4619         struct MR_PRIV_DEVICE *mr_device_priv_data;
4620         mr_device_priv_data = scmd->device->hostdata;
4621
4622         instance = (struct megasas_instance *)scmd->device->host->hostdata;
4623
4624         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4625                 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4626                 "SCSI host:%d\n", instance->host->host_no);
4627                 ret = FAILED;
4628                 return ret;
4629         }
4630
4631         if (!mr_device_priv_data) {
4632                 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4633                         "scmd(%p)\n", scmd);
4634                 scmd->result = DID_NO_CONNECT << 16;
4635                 ret = SUCCESS;
4636                 goto out;
4637         }
4638
4639         if (!mr_device_priv_data->is_tm_capable) {
4640                 ret = FAILED;
4641                 goto out;
4642         }
4643
4644         mutex_lock(&instance->reset_mutex);
4645
4646         smid = megasas_fusion_smid_lookup(scmd);
4647
4648         if (!smid) {
4649                 ret = SUCCESS;
4650                 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4651                         " issued is not found in outstanding commands\n");
4652                 mutex_unlock(&instance->reset_mutex);
4653                 goto out;
4654         }
4655
4656         devhandle = megasas_get_tm_devhandle(scmd->device);
4657
4658         if (devhandle == (u16)ULONG_MAX) {
4659                 ret = FAILED;
4660                 sdev_printk(KERN_INFO, scmd->device,
4661                         "task abort issued for invalid devhandle\n");
4662                 mutex_unlock(&instance->reset_mutex);
4663                 goto out;
4664         }
4665         sdev_printk(KERN_INFO, scmd->device,
4666                 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4667                 scmd, devhandle);
4668
4669         mr_device_priv_data->tm_busy = true;
4670         ret = megasas_issue_tm(instance, devhandle,
4671                         scmd->device->channel, scmd->device->id, smid,
4672                         MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4673                         mr_device_priv_data);
4674         mr_device_priv_data->tm_busy = false;
4675
4676         mutex_unlock(&instance->reset_mutex);
4677         scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
4678                         ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4679 out:
4680         scsi_print_command(scmd);
4681         if (megasas_dbg_lvl & TM_DEBUG)
4682                 megasas_dump_fusion_io(scmd);
4683
4684         return ret;
4685 }
4686
4687 /*
4688  * megasas_reset_target_fusion : target reset function for fusion adapters
4689  * scmd: SCSI command pointer
4690  *
4691  * Returns SUCCESS if all commands associated with target aborted else FAILED
4692  */
4693
4694 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4695 {
4696
4697         struct megasas_instance *instance;
4698         int ret = FAILED;
4699         u16 devhandle;
4700         struct MR_PRIV_DEVICE *mr_device_priv_data;
4701         mr_device_priv_data = scmd->device->hostdata;
4702
4703         instance = (struct megasas_instance *)scmd->device->host->hostdata;
4704
4705         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4706                 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4707                 "SCSI host:%d\n", instance->host->host_no);
4708                 ret = FAILED;
4709                 return ret;
4710         }
4711
4712         if (!mr_device_priv_data) {
4713                 sdev_printk(KERN_INFO, scmd->device,
4714                             "device been deleted! scmd: (0x%p)\n", scmd);
4715                 scmd->result = DID_NO_CONNECT << 16;
4716                 ret = SUCCESS;
4717                 goto out;
4718         }
4719
4720         if (!mr_device_priv_data->is_tm_capable) {
4721                 ret = FAILED;
4722                 goto out;
4723         }
4724
4725         mutex_lock(&instance->reset_mutex);
4726         devhandle = megasas_get_tm_devhandle(scmd->device);
4727
4728         if (devhandle == (u16)ULONG_MAX) {
4729                 ret = FAILED;
4730                 sdev_printk(KERN_INFO, scmd->device,
4731                         "target reset issued for invalid devhandle\n");
4732                 mutex_unlock(&instance->reset_mutex);
4733                 goto out;
4734         }
4735
4736         sdev_printk(KERN_INFO, scmd->device,
4737                 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4738                 scmd, devhandle);
4739         mr_device_priv_data->tm_busy = true;
4740         ret = megasas_issue_tm(instance, devhandle,
4741                         scmd->device->channel, scmd->device->id, 0,
4742                         MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4743                         mr_device_priv_data);
4744         mr_device_priv_data->tm_busy = false;
4745         mutex_unlock(&instance->reset_mutex);
4746         scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
4747                 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4748
4749 out:
4750         return ret;
4751 }
4752
4753 /*SRIOV get other instance in cluster if any*/
4754 static struct
4755 megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4756 {
4757         int i;
4758
4759         for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4760                 if (megasas_mgmt_info.instance[i] &&
4761                         (megasas_mgmt_info.instance[i] != instance) &&
4762                          megasas_mgmt_info.instance[i]->requestorId &&
4763                          megasas_mgmt_info.instance[i]->peerIsPresent &&
4764                         (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4765                         instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4766                         return megasas_mgmt_info.instance[i];
4767         }
4768         return NULL;
4769 }
4770
4771 /* Check for a second path that is currently UP */
4772 int megasas_check_mpio_paths(struct megasas_instance *instance,
4773         struct scsi_cmnd *scmd)
4774 {
4775         struct megasas_instance *peer_instance = NULL;
4776         int retval = (DID_REQUEUE << 16);
4777
4778         if (instance->peerIsPresent) {
4779                 peer_instance = megasas_get_peer_instance(instance);
4780                 if ((peer_instance) &&
4781                         (atomic_read(&peer_instance->adprecovery) ==
4782                         MEGASAS_HBA_OPERATIONAL))
4783                         retval = (DID_NO_CONNECT << 16);
4784         }
4785         return retval;
4786 }
4787
4788 /* Core fusion reset function */
4789 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4790 {
4791         int retval = SUCCESS, i, j, convert = 0;
4792         struct megasas_instance *instance;
4793         struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4794         struct fusion_context *fusion;
4795         u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
4796         u32 io_timeout_in_crash_mode = 0;
4797         struct scsi_cmnd *scmd_local = NULL;
4798         struct scsi_device *sdev;
4799         int ret_target_prop = DCMD_FAILED;
4800         bool is_target_prop = false;
4801         bool do_adp_reset = true;
4802         int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
4803
4804         instance = (struct megasas_instance *)shost->hostdata;
4805         fusion = instance->ctrl_context;
4806
4807         mutex_lock(&instance->reset_mutex);
4808
4809         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4810                 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4811                        "returning FAILED for scsi%d.\n",
4812                         instance->host->host_no);
4813                 mutex_unlock(&instance->reset_mutex);
4814                 return FAILED;
4815         }
4816         status_reg = instance->instancet->read_fw_status_reg(instance);
4817         abs_state = status_reg & MFI_STATE_MASK;
4818
4819         /* IO timeout detected, forcibly put FW in FAULT state */
4820         if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4821                 instance->crash_dump_app_support && reason) {
4822                 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4823                         "forcibly FAULT Firmware\n");
4824                 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4825                 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4826                 writel(status_reg | MFI_STATE_FORCE_OCR,
4827                         &instance->reg_set->doorbell);
4828                 readl(&instance->reg_set->doorbell);
4829                 mutex_unlock(&instance->reset_mutex);
4830                 do {
4831                         ssleep(3);
4832                         io_timeout_in_crash_mode++;
4833                         dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4834                                 "seconds for crash dump collection and OCR "
4835                                 "to be done\n", (io_timeout_in_crash_mode * 3));
4836                 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4837                         (io_timeout_in_crash_mode < 80));
4838
4839                 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4840                         dev_info(&instance->pdev->dev, "OCR done for IO "
4841                                 "timeout case\n");
4842                         retval = SUCCESS;
4843                 } else {
4844                         dev_info(&instance->pdev->dev, "Controller is not "
4845                                 "operational after 240 seconds wait for IO "
4846                                 "timeout case in FW crash dump mode\n do "
4847                                 "OCR/kill adapter\n");
4848                         retval = megasas_reset_fusion(shost, 0);
4849                 }
4850                 return retval;
4851         }
4852
4853         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4854                 del_timer_sync(&instance->sriov_heartbeat_timer);
4855         set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4856         set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4857         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4858         instance->instancet->disable_intr(instance);
4859         megasas_sync_irqs((unsigned long)instance);
4860
4861         /* First try waiting for commands to complete */
4862         if (megasas_wait_for_outstanding_fusion(instance, reason,
4863                                                 &convert)) {
4864                 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4865                 dev_warn(&instance->pdev->dev, "resetting fusion "
4866                        "adapter scsi%d.\n", instance->host->host_no);
4867                 if (convert)
4868                         reason = 0;
4869
4870                 if (megasas_dbg_lvl & OCR_DEBUG)
4871                         dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4872
4873                 /* Now return commands back to the OS */
4874                 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4875                         cmd_fusion = fusion->cmd_list[i];
4876                         /*check for extra commands issued by driver*/
4877                         if (instance->adapter_type >= VENTURA_SERIES) {
4878                                 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4879                                 megasas_return_cmd_fusion(instance, r1_cmd);
4880                         }
4881                         scmd_local = cmd_fusion->scmd;
4882                         if (cmd_fusion->scmd) {
4883                                 if (megasas_dbg_lvl & OCR_DEBUG) {
4884                                         sdev_printk(KERN_INFO,
4885                                                 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4886                                                 cmd_fusion->index);
4887                                         megasas_dump_fusion_io(cmd_fusion->scmd);
4888                                 }
4889
4890                                 if (cmd_fusion->io_request->Function ==
4891                                         MPI2_FUNCTION_SCSI_IO_REQUEST)
4892                                         fpio_count++;
4893
4894                                 scmd_local->result =
4895                                         megasas_check_mpio_paths(instance,
4896                                                         scmd_local);
4897                                 if (instance->ldio_threshold &&
4898                                         megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4899                                         atomic_dec(&instance->ldio_outstanding);
4900                                 megasas_return_cmd_fusion(instance, cmd_fusion);
4901                                 scsi_dma_unmap(scmd_local);
4902                                 scmd_local->scsi_done(scmd_local);
4903                         }
4904                 }
4905
4906                 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
4907                         fpio_count);
4908
4909                 atomic_set(&instance->fw_outstanding, 0);
4910
4911                 status_reg = instance->instancet->read_fw_status_reg(instance);
4912                 abs_state = status_reg & MFI_STATE_MASK;
4913                 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4914                 if (instance->disableOnlineCtrlReset ||
4915                     (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4916                         /* Reset not supported, kill adapter */
4917                         dev_warn(&instance->pdev->dev, "Reset not supported"
4918                                ", killing adapter scsi%d.\n",
4919                                 instance->host->host_no);
4920                         goto kill_hba;
4921                 }
4922
4923                 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4924                 if (instance->requestorId && !reason) {
4925                         msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4926                         do_adp_reset = false;
4927                         max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
4928                 }
4929
4930                 /* Now try to reset the chip */
4931                 for (i = 0; i < max_reset_tries; i++) {
4932                         /*
4933                          * Do adp reset and wait for
4934                          * controller to transition to ready
4935                          */
4936                         if (megasas_adp_reset_wait_for_ready(instance,
4937                                 do_adp_reset, 1) == FAILED)
4938                                 continue;
4939
4940                         /* Wait for FW to become ready */
4941                         if (megasas_transition_to_ready(instance, 1)) {
4942                                 dev_warn(&instance->pdev->dev,
4943                                         "Failed to transition controller to ready for "
4944                                         "scsi%d.\n", instance->host->host_no);
4945                                 continue;
4946                         }
4947                         megasas_reset_reply_desc(instance);
4948                         megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4949
4950                         if (megasas_ioc_init_fusion(instance)) {
4951                                 continue;
4952                         }
4953
4954                         if (megasas_get_ctrl_info(instance)) {
4955                                 dev_info(&instance->pdev->dev,
4956                                         "Failed from %s %d\n",
4957                                         __func__, __LINE__);
4958                                 goto kill_hba;
4959                         }
4960
4961                         megasas_refire_mgmt_cmd(instance,
4962                                                 (i == (MEGASAS_FUSION_MAX_RESET_TRIES - 1)
4963                                                         ? 1 : 0));
4964
4965                         /* Reset load balance info */
4966                         if (fusion->load_balance_info)
4967                                 memset(fusion->load_balance_info, 0,
4968                                        (sizeof(struct LD_LOAD_BALANCE_INFO) *
4969                                        MAX_LOGICAL_DRIVES_EXT));
4970
4971                         if (!megasas_get_map_info(instance)) {
4972                                 megasas_sync_map_info(instance);
4973                         } else {
4974                                 /*
4975                                  * Return pending polled mode cmds before
4976                                  * retrying OCR
4977                                  */
4978                                 megasas_return_polled_cmds(instance);
4979                                 continue;
4980                         }
4981
4982                         megasas_setup_jbod_map(instance);
4983
4984                         /* reset stream detection array */
4985                         if (instance->adapter_type >= VENTURA_SERIES) {
4986                                 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4987                                         memset(fusion->stream_detect_by_ld[j],
4988                                         0, sizeof(struct LD_STREAM_DETECT));
4989                                  fusion->stream_detect_by_ld[j]->mru_bit_map
4990                                                 = MR_STREAM_BITMAP;
4991                                 }
4992                         }
4993
4994                         clear_bit(MEGASAS_FUSION_IN_RESET,
4995                                   &instance->reset_flags);
4996                         instance->instancet->enable_intr(instance);
4997                         megasas_enable_irq_poll(instance);
4998                         shost_for_each_device(sdev, shost) {
4999                                 if ((instance->tgt_prop) &&
5000                                     (instance->nvme_page_size))
5001                                         ret_target_prop = megasas_get_target_prop(instance, sdev);
5002
5003                                 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
5004                                 megasas_set_dynamic_target_properties(sdev, is_target_prop);
5005                         }
5006
5007                         status_reg = instance->instancet->read_fw_status_reg
5008                                         (instance);
5009                         abs_state = status_reg & MFI_STATE_MASK;
5010                         if (abs_state != MFI_STATE_OPERATIONAL) {
5011                                 dev_info(&instance->pdev->dev,
5012                                          "Adapter is not OPERATIONAL, state 0x%x for scsi:%d\n",
5013                                          abs_state, instance->host->host_no);
5014                                 goto out;
5015                         }
5016                         atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5017
5018                         dev_info(&instance->pdev->dev,
5019                                  "Adapter is OPERATIONAL for scsi:%d\n",
5020                                  instance->host->host_no);
5021
5022                         /* Restart SR-IOV heartbeat */
5023                         if (instance->requestorId) {
5024                                 if (!megasas_sriov_start_heartbeat(instance, 0))
5025                                         megasas_start_timer(instance);
5026                                 else
5027                                         instance->skip_heartbeat_timer_del = 1;
5028                         }
5029
5030                         if (instance->crash_dump_drv_support &&
5031                                 instance->crash_dump_app_support)
5032                                 megasas_set_crash_dump_params(instance,
5033                                         MR_CRASH_BUF_TURN_ON);
5034                         else
5035                                 megasas_set_crash_dump_params(instance,
5036                                         MR_CRASH_BUF_TURN_OFF);
5037
5038                         if (instance->snapdump_wait_time) {
5039                                 megasas_get_snapdump_properties(instance);
5040                                 dev_info(&instance->pdev->dev,
5041                                          "Snap dump wait time\t: %d\n",
5042                                          instance->snapdump_wait_time);
5043                         }
5044
5045                         retval = SUCCESS;
5046
5047                         /* Adapter reset completed successfully */
5048                         dev_warn(&instance->pdev->dev,
5049                                  "Reset successful for scsi%d.\n",
5050                                  instance->host->host_no);
5051
5052                         goto out;
5053                 }
5054                 /* Reset failed, kill the adapter */
5055                 dev_warn(&instance->pdev->dev, "Reset failed, killing "
5056                        "adapter scsi%d.\n", instance->host->host_no);
5057                 goto kill_hba;
5058         } else {
5059                 /* For VF: Restart HB timer if we didn't OCR */
5060                 if (instance->requestorId) {
5061                         megasas_start_timer(instance);
5062                 }
5063                 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5064                 instance->instancet->enable_intr(instance);
5065                 megasas_enable_irq_poll(instance);
5066                 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5067                 goto out;
5068         }
5069 kill_hba:
5070         megaraid_sas_kill_hba(instance);
5071         megasas_enable_irq_poll(instance);
5072         instance->skip_heartbeat_timer_del = 1;
5073         retval = FAILED;
5074 out:
5075         clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
5076         mutex_unlock(&instance->reset_mutex);
5077         return retval;
5078 }
5079
5080 /* Fusion Crash dump collection */
5081 static void  megasas_fusion_crash_dump(struct megasas_instance *instance)
5082 {
5083         u32 status_reg;
5084         u8 partial_copy = 0;
5085         int wait = 0;
5086
5087
5088         status_reg = instance->instancet->read_fw_status_reg(instance);
5089
5090         /*
5091          * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5092          * to host crash buffers
5093          */
5094         if (instance->drv_buf_index == 0) {
5095                 /* Buffer is already allocated for old Crash dump.
5096                  * Do OCR and do not wait for crash dump collection
5097                  */
5098                 if (instance->drv_buf_alloc) {
5099                         dev_info(&instance->pdev->dev, "earlier crash dump is "
5100                                 "not yet copied by application, ignoring this "
5101                                 "crash dump and initiating OCR\n");
5102                         status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5103                         writel(status_reg,
5104                                 &instance->reg_set->outbound_scratch_pad_0);
5105                         readl(&instance->reg_set->outbound_scratch_pad_0);
5106                         return;
5107                 }
5108                 megasas_alloc_host_crash_buffer(instance);
5109                 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5110                         "allocated: %d\n", instance->drv_buf_alloc);
5111         }
5112
5113         while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5114                (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5115                 if (!(status_reg & MFI_STATE_DMADONE)) {
5116                         /*
5117                          * Next crash dump buffer is not yet DMA'd by FW
5118                          * Check after 10ms. Wait for 1 second for FW to
5119                          * post the next buffer. If not bail out.
5120                          */
5121                         wait++;
5122                         msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5123                         status_reg = instance->instancet->read_fw_status_reg(
5124                                         instance);
5125                         continue;
5126                 }
5127
5128                 wait = 0;
5129                 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5130                         dev_info(&instance->pdev->dev,
5131                                  "Driver is done copying the buffer: %d\n",
5132                                  instance->drv_buf_alloc);
5133                         status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5134                         partial_copy = 1;
5135                         break;
5136                 } else {
5137                         memcpy(instance->crash_buf[instance->drv_buf_index],
5138                                instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5139                         instance->drv_buf_index++;
5140                         status_reg &= ~MFI_STATE_DMADONE;
5141                 }
5142
5143                 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5144                 readl(&instance->reg_set->outbound_scratch_pad_0);
5145
5146                 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5147                 status_reg = instance->instancet->read_fw_status_reg(instance);
5148         }
5149
5150         if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5151                 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5152                         "of copied buffers: %d\n", instance->drv_buf_index);
5153                 instance->fw_crash_buffer_size =  instance->drv_buf_index;
5154                 instance->fw_crash_state = AVAILABLE;
5155                 instance->drv_buf_index = 0;
5156                 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5157                 readl(&instance->reg_set->outbound_scratch_pad_0);
5158                 if (!partial_copy)
5159                         megasas_reset_fusion(instance->host, 0);
5160         }
5161 }
5162
5163
5164 /* Fusion OCR work queue */
5165 void megasas_fusion_ocr_wq(struct work_struct *work)
5166 {
5167         struct megasas_instance *instance =
5168                 container_of(work, struct megasas_instance, work_init);
5169
5170         megasas_reset_fusion(instance->host, 0);
5171 }
5172
5173 /* Allocate fusion context */
5174 int
5175 megasas_alloc_fusion_context(struct megasas_instance *instance)
5176 {
5177         struct fusion_context *fusion;
5178
5179         instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
5180                                          GFP_KERNEL);
5181         if (!instance->ctrl_context) {
5182                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5183                         __func__, __LINE__);
5184                 return -ENOMEM;
5185         }
5186
5187         fusion = instance->ctrl_context;
5188
5189         fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5190                                               sizeof(LD_SPAN_INFO));
5191         fusion->log_to_span =
5192                 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5193                                                 fusion->log_to_span_pages);
5194         if (!fusion->log_to_span) {
5195                 fusion->log_to_span =
5196                         vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5197                                            sizeof(LD_SPAN_INFO)));
5198                 if (!fusion->log_to_span) {
5199                         dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5200                                 __func__, __LINE__);
5201                         return -ENOMEM;
5202                 }
5203         }
5204
5205         fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5206                 sizeof(struct LD_LOAD_BALANCE_INFO));
5207         fusion->load_balance_info =
5208                 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5209                 fusion->load_balance_info_pages);
5210         if (!fusion->load_balance_info) {
5211                 fusion->load_balance_info =
5212                         vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5213                                            sizeof(struct LD_LOAD_BALANCE_INFO)));
5214                 if (!fusion->load_balance_info)
5215                         dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5216                                 "continuing without Load Balance support\n");
5217         }
5218
5219         return 0;
5220 }
5221
5222 void
5223 megasas_free_fusion_context(struct megasas_instance *instance)
5224 {
5225         struct fusion_context *fusion = instance->ctrl_context;
5226
5227         if (fusion) {
5228                 if (fusion->load_balance_info) {
5229                         if (is_vmalloc_addr(fusion->load_balance_info))
5230                                 vfree(fusion->load_balance_info);
5231                         else
5232                                 free_pages((ulong)fusion->load_balance_info,
5233                                         fusion->load_balance_info_pages);
5234                 }
5235
5236                 if (fusion->log_to_span) {
5237                         if (is_vmalloc_addr(fusion->log_to_span))
5238                                 vfree(fusion->log_to_span);
5239                         else
5240                                 free_pages((ulong)fusion->log_to_span,
5241                                            fusion->log_to_span_pages);
5242                 }
5243
5244                 kfree(fusion);
5245         }
5246 }
5247
5248 struct megasas_instance_template megasas_instance_template_fusion = {
5249         .enable_intr = megasas_enable_intr_fusion,
5250         .disable_intr = megasas_disable_intr_fusion,
5251         .clear_intr = megasas_clear_intr_fusion,
5252         .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5253         .adp_reset = megasas_adp_reset_fusion,
5254         .check_reset = megasas_check_reset_fusion,
5255         .service_isr = megasas_isr_fusion,
5256         .tasklet = megasas_complete_cmd_dpc_fusion,
5257         .init_adapter = megasas_init_adapter_fusion,
5258         .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5259         .issue_dcmd = megasas_issue_dcmd_fusion,
5260 };