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