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