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