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 
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_dbg.h>
56 #include <linux/dmi.h>
57 
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
60 
61 
62 extern void megasas_free_cmds(struct megasas_instance *instance);
63 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
64 					   *instance);
65 extern void
66 megasas_complete_cmd(struct megasas_instance *instance,
67 		     struct megasas_cmd *cmd, u8 alt_status);
68 int
69 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
70 	      int seconds);
71 
72 void
73 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
74 int megasas_alloc_cmds(struct megasas_instance *instance);
75 int
76 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
77 int
78 megasas_issue_polled(struct megasas_instance *instance,
79 		     struct megasas_cmd *cmd);
80 void
81 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
82 
83 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
84 void megaraid_sas_kill_hba(struct megasas_instance *instance);
85 
86 extern u32 megasas_dbg_lvl;
87 void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
88 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
89 				  int initial);
90 void megasas_start_timer(struct megasas_instance *instance,
91 			struct timer_list *timer,
92 			 void *fn, unsigned long interval);
93 extern struct megasas_mgmt_info megasas_mgmt_info;
94 extern int resetwaittime;
95 
96 
97 
98 /**
99  * megasas_enable_intr_fusion -	Enables interrupts
100  * @regs:			MFI register set
101  */
102 void
103 megasas_enable_intr_fusion(struct megasas_instance *instance)
104 {
105 	struct megasas_register_set __iomem *regs;
106 	regs = instance->reg_set;
107 
108 	instance->mask_interrupts = 0;
109 	/* For Thunderbolt/Invader also clear intr on enable */
110 	writel(~0, &regs->outbound_intr_status);
111 	readl(&regs->outbound_intr_status);
112 
113 	writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
114 
115 	/* Dummy readl to force pci flush */
116 	readl(&regs->outbound_intr_mask);
117 }
118 
119 /**
120  * megasas_disable_intr_fusion - Disables interrupt
121  * @regs:			 MFI register set
122  */
123 void
124 megasas_disable_intr_fusion(struct megasas_instance *instance)
125 {
126 	u32 mask = 0xFFFFFFFF;
127 	u32 status;
128 	struct megasas_register_set __iomem *regs;
129 	regs = instance->reg_set;
130 	instance->mask_interrupts = 1;
131 
132 	writel(mask, &regs->outbound_intr_mask);
133 	/* Dummy readl to force pci flush */
134 	status = readl(&regs->outbound_intr_mask);
135 }
136 
137 int
138 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
139 {
140 	u32 status;
141 	/*
142 	 * Check if it is our interrupt
143 	 */
144 	status = readl(&regs->outbound_intr_status);
145 
146 	if (status & 1) {
147 		writel(status, &regs->outbound_intr_status);
148 		readl(&regs->outbound_intr_status);
149 		return 1;
150 	}
151 	if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
152 		return 0;
153 
154 	return 1;
155 }
156 
157 /**
158  * megasas_get_cmd_fusion -	Get a command from the free pool
159  * @instance:		Adapter soft state
160  *
161  * Returns a blk_tag indexed mpt frame
162  */
163 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
164 						  *instance, u32 blk_tag)
165 {
166 	struct fusion_context *fusion;
167 
168 	fusion = instance->ctrl_context;
169 	return fusion->cmd_list[blk_tag];
170 }
171 
172 /**
173  * megasas_return_cmd_fusion -	Return a cmd to free command pool
174  * @instance:		Adapter soft state
175  * @cmd:		Command packet to be returned to free command pool
176  */
177 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
178 	struct megasas_cmd_fusion *cmd)
179 {
180 	cmd->scmd = NULL;
181 	memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
182 }
183 
184 /**
185  * megasas_fire_cmd_fusion -	Sends command to the FW
186  */
187 static void
188 megasas_fire_cmd_fusion(struct megasas_instance *instance,
189 		union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
190 {
191 #if defined(writeq) && defined(CONFIG_64BIT)
192 	u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
193 			le32_to_cpu(req_desc->u.low));
194 
195 	writeq(req_data, &instance->reg_set->inbound_low_queue_port);
196 #else
197 	unsigned long flags;
198 
199 	spin_lock_irqsave(&instance->hba_lock, flags);
200 	writel(le32_to_cpu(req_desc->u.low),
201 		&instance->reg_set->inbound_low_queue_port);
202 	writel(le32_to_cpu(req_desc->u.high),
203 		&instance->reg_set->inbound_high_queue_port);
204 	spin_unlock_irqrestore(&instance->hba_lock, flags);
205 #endif
206 }
207 
208 
209 /**
210  * megasas_teardown_frame_pool_fusion -	Destroy the cmd frame DMA pool
211  * @instance:				Adapter soft state
212  */
213 static void megasas_teardown_frame_pool_fusion(
214 	struct megasas_instance *instance)
215 {
216 	int i;
217 	struct fusion_context *fusion = instance->ctrl_context;
218 
219 	u16 max_cmd = instance->max_fw_cmds;
220 
221 	struct megasas_cmd_fusion *cmd;
222 
223 	if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
224 		printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
225 		       "sense pool : %p\n", fusion->sg_dma_pool,
226 		       fusion->sense_dma_pool);
227 		return;
228 	}
229 
230 	/*
231 	 * Return all frames to pool
232 	 */
233 	for (i = 0; i < max_cmd; i++) {
234 
235 		cmd = fusion->cmd_list[i];
236 
237 		if (cmd->sg_frame)
238 			pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
239 				      cmd->sg_frame_phys_addr);
240 
241 		if (cmd->sense)
242 			pci_pool_free(fusion->sense_dma_pool, cmd->sense,
243 				      cmd->sense_phys_addr);
244 	}
245 
246 	/*
247 	 * Now destroy the pool itself
248 	 */
249 	pci_pool_destroy(fusion->sg_dma_pool);
250 	pci_pool_destroy(fusion->sense_dma_pool);
251 
252 	fusion->sg_dma_pool = NULL;
253 	fusion->sense_dma_pool = NULL;
254 }
255 
256 /**
257  * megasas_free_cmds_fusion -	Free all the cmds in the free cmd pool
258  * @instance:		Adapter soft state
259  */
260 void
261 megasas_free_cmds_fusion(struct megasas_instance *instance)
262 {
263 	int i;
264 	struct fusion_context *fusion = instance->ctrl_context;
265 
266 	u32 max_cmds, req_sz, reply_sz, io_frames_sz;
267 
268 
269 	req_sz = fusion->request_alloc_sz;
270 	reply_sz = fusion->reply_alloc_sz;
271 	io_frames_sz = fusion->io_frames_alloc_sz;
272 
273 	max_cmds = instance->max_fw_cmds;
274 
275 	/* Free descriptors and request Frames memory */
276 	if (fusion->req_frames_desc)
277 		dma_free_coherent(&instance->pdev->dev, req_sz,
278 				  fusion->req_frames_desc,
279 				  fusion->req_frames_desc_phys);
280 
281 	if (fusion->reply_frames_desc) {
282 		pci_pool_free(fusion->reply_frames_desc_pool,
283 			      fusion->reply_frames_desc,
284 			      fusion->reply_frames_desc_phys);
285 		pci_pool_destroy(fusion->reply_frames_desc_pool);
286 	}
287 
288 	if (fusion->io_request_frames) {
289 		pci_pool_free(fusion->io_request_frames_pool,
290 			      fusion->io_request_frames,
291 			      fusion->io_request_frames_phys);
292 		pci_pool_destroy(fusion->io_request_frames_pool);
293 	}
294 
295 	/* Free the Fusion frame pool */
296 	megasas_teardown_frame_pool_fusion(instance);
297 
298 	/* Free all the commands in the cmd_list */
299 	for (i = 0; i < max_cmds; i++)
300 		kfree(fusion->cmd_list[i]);
301 
302 	/* Free the cmd_list buffer itself */
303 	kfree(fusion->cmd_list);
304 	fusion->cmd_list = NULL;
305 
306 }
307 
308 /**
309  * megasas_create_frame_pool_fusion -	Creates DMA pool for cmd frames
310  * @instance:			Adapter soft state
311  *
312  */
313 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
314 {
315 	int i;
316 	u32 max_cmd;
317 	struct fusion_context *fusion;
318 	struct megasas_cmd_fusion *cmd;
319 	u32 total_sz_chain_frame;
320 
321 	fusion = instance->ctrl_context;
322 	max_cmd = instance->max_fw_cmds;
323 
324 	total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
325 
326 	/*
327 	 * Use DMA pool facility provided by PCI layer
328 	 */
329 
330 	fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
331 					      instance->pdev,
332 					      total_sz_chain_frame, 4,
333 					      0);
334 	if (!fusion->sg_dma_pool) {
335 		printk(KERN_DEBUG "megasas: failed to setup request pool "
336 		       "fusion\n");
337 		return -ENOMEM;
338 	}
339 	fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
340 						 instance->pdev,
341 						 SCSI_SENSE_BUFFERSIZE, 64, 0);
342 
343 	if (!fusion->sense_dma_pool) {
344 		printk(KERN_DEBUG "megasas: failed to setup sense pool "
345 		       "fusion\n");
346 		pci_pool_destroy(fusion->sg_dma_pool);
347 		fusion->sg_dma_pool = NULL;
348 		return -ENOMEM;
349 	}
350 
351 	/*
352 	 * Allocate and attach a frame to each of the commands in cmd_list
353 	 */
354 	for (i = 0; i < max_cmd; i++) {
355 
356 		cmd = fusion->cmd_list[i];
357 
358 		cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
359 					       GFP_KERNEL,
360 					       &cmd->sg_frame_phys_addr);
361 
362 		cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
363 					    GFP_KERNEL, &cmd->sense_phys_addr);
364 		/*
365 		 * megasas_teardown_frame_pool_fusion() takes care of freeing
366 		 * whatever has been allocated
367 		 */
368 		if (!cmd->sg_frame || !cmd->sense) {
369 			printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
370 			megasas_teardown_frame_pool_fusion(instance);
371 			return -ENOMEM;
372 		}
373 	}
374 	return 0;
375 }
376 
377 /**
378  * megasas_alloc_cmds_fusion -	Allocates the command packets
379  * @instance:		Adapter soft state
380  *
381  *
382  * Each frame has a 32-bit field called context. This context is used to get
383  * back the megasas_cmd_fusion from the frame when a frame gets completed
384  * In this driver, the 32 bit values are the indices into an array cmd_list.
385  * This array is used only to look up the megasas_cmd_fusion given the context.
386  * The free commands themselves are maintained in a linked list called cmd_pool.
387  *
388  * cmds are formed in the io_request and sg_frame members of the
389  * megasas_cmd_fusion. The context field is used to get a request descriptor
390  * and is used as SMID of the cmd.
391  * SMID value range is from 1 to max_fw_cmds.
392  */
393 int
394 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
395 {
396 	int i, j, count;
397 	u32 max_cmd, io_frames_sz;
398 	struct fusion_context *fusion;
399 	struct megasas_cmd_fusion *cmd;
400 	union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
401 	u32 offset;
402 	dma_addr_t io_req_base_phys;
403 	u8 *io_req_base;
404 
405 	fusion = instance->ctrl_context;
406 
407 	max_cmd = instance->max_fw_cmds;
408 
409 	fusion->req_frames_desc =
410 		dma_alloc_coherent(&instance->pdev->dev,
411 				   fusion->request_alloc_sz,
412 				   &fusion->req_frames_desc_phys, GFP_KERNEL);
413 
414 	if (!fusion->req_frames_desc) {
415 		printk(KERN_ERR "megasas; Could not allocate memory for "
416 		       "request_frames\n");
417 		goto fail_req_desc;
418 	}
419 
420 	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
421 	fusion->reply_frames_desc_pool =
422 		pci_pool_create("reply_frames pool", instance->pdev,
423 				fusion->reply_alloc_sz * count, 16, 0);
424 
425 	if (!fusion->reply_frames_desc_pool) {
426 		printk(KERN_ERR "megasas; Could not allocate memory for "
427 		       "reply_frame pool\n");
428 		goto fail_reply_desc;
429 	}
430 
431 	fusion->reply_frames_desc =
432 		pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
433 			       &fusion->reply_frames_desc_phys);
434 	if (!fusion->reply_frames_desc) {
435 		printk(KERN_ERR "megasas; Could not allocate memory for "
436 		       "reply_frame pool\n");
437 		pci_pool_destroy(fusion->reply_frames_desc_pool);
438 		goto fail_reply_desc;
439 	}
440 
441 	reply_desc = fusion->reply_frames_desc;
442 	for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
443 		reply_desc->Words = cpu_to_le64(ULLONG_MAX);
444 
445 	io_frames_sz = fusion->io_frames_alloc_sz;
446 
447 	fusion->io_request_frames_pool =
448 		pci_pool_create("io_request_frames pool", instance->pdev,
449 				fusion->io_frames_alloc_sz, 16, 0);
450 
451 	if (!fusion->io_request_frames_pool) {
452 		printk(KERN_ERR "megasas: Could not allocate memory for "
453 		       "io_request_frame pool\n");
454 		goto fail_io_frames;
455 	}
456 
457 	fusion->io_request_frames =
458 		pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
459 			       &fusion->io_request_frames_phys);
460 	if (!fusion->io_request_frames) {
461 		printk(KERN_ERR "megasas: Could not allocate memory for "
462 		       "io_request_frames frames\n");
463 		pci_pool_destroy(fusion->io_request_frames_pool);
464 		goto fail_io_frames;
465 	}
466 
467 	/*
468 	 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
469 	 * Allocate the dynamic array first and then allocate individual
470 	 * commands.
471 	 */
472 	fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
473 				   * max_cmd, GFP_KERNEL);
474 
475 	if (!fusion->cmd_list) {
476 		printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
477 		       "memory for cmd_list_fusion\n");
478 		goto fail_cmd_list;
479 	}
480 
481 	max_cmd = instance->max_fw_cmds;
482 	for (i = 0; i < max_cmd; i++) {
483 		fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
484 					      GFP_KERNEL);
485 		if (!fusion->cmd_list[i]) {
486 			printk(KERN_ERR "Could not alloc cmd list fusion\n");
487 
488 			for (j = 0; j < i; j++)
489 				kfree(fusion->cmd_list[j]);
490 
491 			kfree(fusion->cmd_list);
492 			fusion->cmd_list = NULL;
493 			goto fail_cmd_list;
494 		}
495 	}
496 
497 	/* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
498 	io_req_base = fusion->io_request_frames +
499 		MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
500 	io_req_base_phys = fusion->io_request_frames_phys +
501 		MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
502 
503 	/*
504 	 * Add all the commands to command pool (fusion->cmd_pool)
505 	 */
506 
507 	/* SMID 0 is reserved. Set SMID/index from 1 */
508 	for (i = 0; i < max_cmd; i++) {
509 		cmd = fusion->cmd_list[i];
510 		offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
511 		memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
512 		cmd->index = i + 1;
513 		cmd->scmd = NULL;
514 		cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
515 				(i - instance->max_scsi_cmds) :
516 				(u32)ULONG_MAX; /* Set to Invalid */
517 		cmd->instance = instance;
518 		cmd->io_request =
519 			(struct MPI2_RAID_SCSI_IO_REQUEST *)
520 		  (io_req_base + offset);
521 		memset(cmd->io_request, 0,
522 		       sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
523 		cmd->io_request_phys_addr = io_req_base_phys + offset;
524 	}
525 
526 	/*
527 	 * Create a frame pool and assign one frame to each cmd
528 	 */
529 	if (megasas_create_frame_pool_fusion(instance)) {
530 		printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
531 		megasas_free_cmds_fusion(instance);
532 		goto fail_req_desc;
533 	}
534 
535 	return 0;
536 
537 fail_cmd_list:
538 	pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
539 		      fusion->io_request_frames_phys);
540 	pci_pool_destroy(fusion->io_request_frames_pool);
541 fail_io_frames:
542 	dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
543 			  fusion->reply_frames_desc,
544 			  fusion->reply_frames_desc_phys);
545 	pci_pool_free(fusion->reply_frames_desc_pool,
546 		      fusion->reply_frames_desc,
547 		      fusion->reply_frames_desc_phys);
548 	pci_pool_destroy(fusion->reply_frames_desc_pool);
549 
550 fail_reply_desc:
551 	dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
552 			  fusion->req_frames_desc,
553 			  fusion->req_frames_desc_phys);
554 fail_req_desc:
555 	return -ENOMEM;
556 }
557 
558 /**
559  * wait_and_poll -	Issues a polling command
560  * @instance:			Adapter soft state
561  * @cmd:			Command packet to be issued
562  *
563  * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
564  */
565 int
566 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
567 	int seconds)
568 {
569 	int i;
570 	struct megasas_header *frame_hdr = &cmd->frame->hdr;
571 	struct fusion_context *fusion;
572 
573 	u32 msecs = seconds * 1000;
574 
575 	fusion = instance->ctrl_context;
576 	/*
577 	 * Wait for cmd_status to change
578 	 */
579 	for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
580 		rmb();
581 		msleep(20);
582 	}
583 
584 	if (frame_hdr->cmd_status == 0xff)
585 		return -ETIME;
586 
587 	return (frame_hdr->cmd_status == MFI_STAT_OK) ?
588 		0 : 1;
589 }
590 
591 /**
592  * megasas_ioc_init_fusion -	Initializes the FW
593  * @instance:		Adapter soft state
594  *
595  * Issues the IOC Init cmd
596  */
597 int
598 megasas_ioc_init_fusion(struct megasas_instance *instance)
599 {
600 	struct megasas_init_frame *init_frame;
601 	struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
602 	dma_addr_t	ioc_init_handle;
603 	struct megasas_cmd *cmd;
604 	u8 ret;
605 	struct fusion_context *fusion;
606 	union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
607 	int i;
608 	struct megasas_header *frame_hdr;
609 	const char *sys_info;
610 
611 	fusion = instance->ctrl_context;
612 
613 	cmd = megasas_get_cmd(instance);
614 
615 	if (!cmd) {
616 		printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
617 		ret = 1;
618 		goto fail_get_cmd;
619 	}
620 
621 	IOCInitMessage =
622 	  dma_alloc_coherent(&instance->pdev->dev,
623 			     sizeof(struct MPI2_IOC_INIT_REQUEST),
624 			     &ioc_init_handle, GFP_KERNEL);
625 
626 	if (!IOCInitMessage) {
627 		printk(KERN_ERR "Could not allocate memory for "
628 		       "IOCInitMessage\n");
629 		ret = 1;
630 		goto fail_fw_init;
631 	}
632 
633 	memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
634 
635 	IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
636 	IOCInitMessage->WhoInit	= MPI2_WHOINIT_HOST_DRIVER;
637 	IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
638 	IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
639 	IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
640 
641 	IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
642 	IOCInitMessage->ReplyDescriptorPostQueueAddress	= cpu_to_le64(fusion->reply_frames_desc_phys);
643 	IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
644 	IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
645 	init_frame = (struct megasas_init_frame *)cmd->frame;
646 	memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
647 
648 	frame_hdr = &cmd->frame->hdr;
649 	frame_hdr->cmd_status = 0xFF;
650 	frame_hdr->flags = cpu_to_le16(
651 		le16_to_cpu(frame_hdr->flags) |
652 		MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
653 
654 	init_frame->cmd	= MFI_CMD_INIT;
655 	init_frame->cmd_status = 0xFF;
656 
657 	/* driver support Extended MSIX */
658 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
659 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
660 		init_frame->driver_operations.
661 			mfi_capabilities.support_additional_msix = 1;
662 	/* driver supports HA / Remote LUN over Fast Path interface */
663 	init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
664 		= 1;
665 	init_frame->driver_operations.mfi_capabilities.support_max_255lds
666 		= 1;
667 	init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
668 		= 1;
669 	init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw
670 		= 1;
671 	/* Convert capability to LE32 */
672 	cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
673 
674 	sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
675 	if (instance->system_info_buf && sys_info) {
676 		memcpy(instance->system_info_buf->systemId, sys_info,
677 			strlen(sys_info) > 64 ? 64 : strlen(sys_info));
678 		instance->system_info_buf->systemIdLength =
679 			strlen(sys_info) > 64 ? 64 : strlen(sys_info);
680 		init_frame->system_info_lo = instance->system_info_h;
681 		init_frame->system_info_hi = 0;
682 	}
683 
684 	init_frame->queue_info_new_phys_addr_hi =
685 		cpu_to_le32(upper_32_bits(ioc_init_handle));
686 	init_frame->queue_info_new_phys_addr_lo =
687 		cpu_to_le32(lower_32_bits(ioc_init_handle));
688 	init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
689 
690 	req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
691 	req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
692 	req_desc.MFAIo.RequestFlags =
693 		(MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
694 		MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
695 
696 	/*
697 	 * disable the intr before firing the init frame
698 	 */
699 	instance->instancet->disable_intr(instance);
700 
701 	for (i = 0; i < (10 * 1000); i += 20) {
702 		if (readl(&instance->reg_set->doorbell) & 1)
703 			msleep(20);
704 		else
705 			break;
706 	}
707 
708 	megasas_fire_cmd_fusion(instance, &req_desc);
709 
710 	wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
711 
712 	frame_hdr = &cmd->frame->hdr;
713 	if (frame_hdr->cmd_status != 0) {
714 		ret = 1;
715 		goto fail_fw_init;
716 	}
717 	printk(KERN_ERR "megasas:IOC Init cmd success\n");
718 
719 	ret = 0;
720 
721 fail_fw_init:
722 	megasas_return_cmd(instance, cmd);
723 	if (IOCInitMessage)
724 		dma_free_coherent(&instance->pdev->dev,
725 				  sizeof(struct MPI2_IOC_INIT_REQUEST),
726 				  IOCInitMessage, ioc_init_handle);
727 fail_get_cmd:
728 	return ret;
729 }
730 
731 /*
732  * megasas_get_ld_map_info -	Returns FW's ld_map structure
733  * @instance:				Adapter soft state
734  * @pend:				Pend the command or not
735  * Issues an internal command (DCMD) to get the FW's controller PD
736  * list structure.  This information is mainly used to find out SYSTEM
737  * supported by the FW.
738  * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
739  * dcmd.mbox.b[0]	- number of LDs being sync'd
740  * dcmd.mbox.b[1]	- 0 - complete command immediately.
741  *			- 1 - pend till config change
742  * dcmd.mbox.b[2]	- 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
743  *			- 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
744  *				uses extended struct MR_FW_RAID_MAP_EXT
745  */
746 static int
747 megasas_get_ld_map_info(struct megasas_instance *instance)
748 {
749 	int ret = 0;
750 	struct megasas_cmd *cmd;
751 	struct megasas_dcmd_frame *dcmd;
752 	void *ci;
753 	dma_addr_t ci_h = 0;
754 	u32 size_map_info;
755 	struct fusion_context *fusion;
756 
757 	cmd = megasas_get_cmd(instance);
758 
759 	if (!cmd) {
760 		printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
761 		return -ENOMEM;
762 	}
763 
764 	fusion = instance->ctrl_context;
765 
766 	if (!fusion) {
767 		megasas_return_cmd(instance, cmd);
768 		return -ENXIO;
769 	}
770 
771 	dcmd = &cmd->frame->dcmd;
772 
773 	size_map_info = fusion->current_map_sz;
774 
775 	ci = (void *) fusion->ld_map[(instance->map_id & 1)];
776 	ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
777 
778 	if (!ci) {
779 		printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
780 		megasas_return_cmd(instance, cmd);
781 		return -ENOMEM;
782 	}
783 
784 	memset(ci, 0, fusion->max_map_sz);
785 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
786 #if VD_EXT_DEBUG
787 	dev_dbg(&instance->pdev->dev,
788 		"%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
789 		__func__, cpu_to_le32(size_map_info));
790 #endif
791 	dcmd->cmd = MFI_CMD_DCMD;
792 	dcmd->cmd_status = 0xFF;
793 	dcmd->sge_count = 1;
794 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
795 	dcmd->timeout = 0;
796 	dcmd->pad_0 = 0;
797 	dcmd->data_xfer_len = cpu_to_le32(size_map_info);
798 	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
799 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
800 	dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
801 
802 	if (instance->ctrl_context && !instance->mask_interrupts)
803 		ret = megasas_issue_blocked_cmd(instance, cmd,
804 			MEGASAS_BLOCKED_CMD_TIMEOUT);
805 	else
806 		ret = megasas_issue_polled(instance, cmd);
807 
808 	megasas_return_cmd(instance, cmd);
809 
810 	return ret;
811 }
812 
813 u8
814 megasas_get_map_info(struct megasas_instance *instance)
815 {
816 	struct fusion_context *fusion = instance->ctrl_context;
817 
818 	fusion->fast_path_io = 0;
819 	if (!megasas_get_ld_map_info(instance)) {
820 		if (MR_ValidateMapInfo(instance)) {
821 			fusion->fast_path_io = 1;
822 			return 0;
823 		}
824 	}
825 	return 1;
826 }
827 
828 /*
829  * megasas_sync_map_info -	Returns FW's ld_map structure
830  * @instance:				Adapter soft state
831  *
832  * Issues an internal command (DCMD) to get the FW's controller PD
833  * list structure.  This information is mainly used to find out SYSTEM
834  * supported by the FW.
835  */
836 int
837 megasas_sync_map_info(struct megasas_instance *instance)
838 {
839 	int ret = 0, i;
840 	struct megasas_cmd *cmd;
841 	struct megasas_dcmd_frame *dcmd;
842 	u32 size_sync_info, num_lds;
843 	struct fusion_context *fusion;
844 	struct MR_LD_TARGET_SYNC *ci = NULL;
845 	struct MR_DRV_RAID_MAP_ALL *map;
846 	struct MR_LD_RAID  *raid;
847 	struct MR_LD_TARGET_SYNC *ld_sync;
848 	dma_addr_t ci_h = 0;
849 	u32 size_map_info;
850 
851 	cmd = megasas_get_cmd(instance);
852 
853 	if (!cmd) {
854 		printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
855 		       "info.\n");
856 		return -ENOMEM;
857 	}
858 
859 	fusion = instance->ctrl_context;
860 
861 	if (!fusion) {
862 		megasas_return_cmd(instance, cmd);
863 		return 1;
864 	}
865 
866 	map = fusion->ld_drv_map[instance->map_id & 1];
867 
868 	num_lds = le16_to_cpu(map->raidMap.ldCount);
869 
870 	dcmd = &cmd->frame->dcmd;
871 
872 	size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
873 
874 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
875 
876 	ci = (struct MR_LD_TARGET_SYNC *)
877 	  fusion->ld_map[(instance->map_id - 1) & 1];
878 	memset(ci, 0, fusion->max_map_sz);
879 
880 	ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
881 
882 	ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
883 
884 	for (i = 0; i < num_lds; i++, ld_sync++) {
885 		raid = MR_LdRaidGet(i, map);
886 		ld_sync->targetId = MR_GetLDTgtId(i, map);
887 		ld_sync->seqNum = raid->seqNum;
888 	}
889 
890 	size_map_info = fusion->current_map_sz;
891 
892 	dcmd->cmd = MFI_CMD_DCMD;
893 	dcmd->cmd_status = 0xFF;
894 	dcmd->sge_count = 1;
895 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
896 	dcmd->timeout = 0;
897 	dcmd->pad_0 = 0;
898 	dcmd->data_xfer_len = cpu_to_le32(size_map_info);
899 	dcmd->mbox.b[0] = num_lds;
900 	dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
901 	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
902 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
903 	dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
904 
905 	instance->map_update_cmd = cmd;
906 
907 	instance->instancet->issue_dcmd(instance, cmd);
908 
909 	return ret;
910 }
911 
912 /*
913  * meagasas_display_intel_branding - Display branding string
914  * @instance: per adapter object
915  *
916  * Return nothing.
917  */
918 static void
919 megasas_display_intel_branding(struct megasas_instance *instance)
920 {
921 	if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
922 		return;
923 
924 	switch (instance->pdev->device) {
925 	case PCI_DEVICE_ID_LSI_INVADER:
926 		switch (instance->pdev->subsystem_device) {
927 		case MEGARAID_INTEL_RS3DC080_SSDID:
928 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
929 				instance->host->host_no,
930 				MEGARAID_INTEL_RS3DC080_BRANDING);
931 			break;
932 		case MEGARAID_INTEL_RS3DC040_SSDID:
933 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
934 				instance->host->host_no,
935 				MEGARAID_INTEL_RS3DC040_BRANDING);
936 			break;
937 		case MEGARAID_INTEL_RS3SC008_SSDID:
938 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
939 				instance->host->host_no,
940 				MEGARAID_INTEL_RS3SC008_BRANDING);
941 			break;
942 		case MEGARAID_INTEL_RS3MC044_SSDID:
943 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
944 				instance->host->host_no,
945 				MEGARAID_INTEL_RS3MC044_BRANDING);
946 			break;
947 		default:
948 			break;
949 		}
950 		break;
951 	case PCI_DEVICE_ID_LSI_FURY:
952 		switch (instance->pdev->subsystem_device) {
953 		case MEGARAID_INTEL_RS3WC080_SSDID:
954 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
955 				instance->host->host_no,
956 				MEGARAID_INTEL_RS3WC080_BRANDING);
957 			break;
958 		case MEGARAID_INTEL_RS3WC040_SSDID:
959 			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
960 				instance->host->host_no,
961 				MEGARAID_INTEL_RS3WC040_BRANDING);
962 			break;
963 		default:
964 			break;
965 		}
966 		break;
967 	default:
968 		break;
969 	}
970 }
971 
972 /**
973  * megasas_init_adapter_fusion -	Initializes the FW
974  * @instance:		Adapter soft state
975  *
976  * This is the main function for initializing firmware.
977  */
978 u32
979 megasas_init_adapter_fusion(struct megasas_instance *instance)
980 {
981 	struct megasas_register_set __iomem *reg_set;
982 	struct fusion_context *fusion;
983 	u32 max_cmd;
984 	int i = 0, count;
985 
986 	fusion = instance->ctrl_context;
987 
988 	reg_set = instance->reg_set;
989 
990 	/*
991 	 * Get various operational parameters from status register
992 	 */
993 	instance->max_fw_cmds =
994 		instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
995 	instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
996 
997 	/*
998 	 * Reduce the max supported cmds by 1. This is to ensure that the
999 	 * reply_q_sz (1 more than the max cmd that driver may send)
1000 	 * does not exceed max cmds that the FW can support
1001 	 */
1002 	instance->max_fw_cmds = instance->max_fw_cmds-1;
1003 
1004 	/*
1005 	 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1006 	 */
1007 	instance->max_mfi_cmds =
1008 		MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1009 
1010 	max_cmd = instance->max_fw_cmds;
1011 
1012 	fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
1013 
1014 	fusion->request_alloc_sz =
1015 		sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1016 	fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1017 		*(fusion->reply_q_depth);
1018 	fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1019 		(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1020 		 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1021 
1022 	fusion->max_sge_in_main_msg =
1023 	  (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1024 	   offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1025 
1026 	fusion->max_sge_in_chain =
1027 		MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
1028 
1029 	instance->max_num_sge = rounddown_pow_of_two(
1030 		fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
1031 
1032 	/* Used for pass thru MFI frame (DCMD) */
1033 	fusion->chain_offset_mfi_pthru =
1034 		offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1035 
1036 	fusion->chain_offset_io_request =
1037 		(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1038 		 sizeof(union MPI2_SGE_IO_UNION))/16;
1039 
1040 	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1041 	for (i = 0 ; i < count; i++)
1042 		fusion->last_reply_idx[i] = 0;
1043 
1044 	/*
1045 	 * For fusion adapters, 3 commands for IOCTL and 5 commands
1046 	 * for driver's internal DCMDs.
1047 	 */
1048 	instance->max_scsi_cmds = instance->max_fw_cmds -
1049 				(MEGASAS_FUSION_INTERNAL_CMDS +
1050 				MEGASAS_FUSION_IOCTL_CMDS);
1051 	sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1052 
1053 	/*
1054 	 * Allocate memory for descriptors
1055 	 * Create a pool of commands
1056 	 */
1057 	if (megasas_alloc_cmds(instance))
1058 		goto fail_alloc_mfi_cmds;
1059 	if (megasas_alloc_cmds_fusion(instance))
1060 		goto fail_alloc_cmds;
1061 
1062 	if (megasas_ioc_init_fusion(instance))
1063 		goto fail_ioc_init;
1064 
1065 	megasas_display_intel_branding(instance);
1066 	if (megasas_get_ctrl_info(instance)) {
1067 		dev_err(&instance->pdev->dev,
1068 			"Could not get controller info. Fail from %s %d\n",
1069 			__func__, __LINE__);
1070 		goto fail_ioc_init;
1071 	}
1072 
1073 	instance->flag_ieee = 1;
1074 	fusion->fast_path_io = 0;
1075 
1076 	fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1077 	for (i = 0; i < 2; i++) {
1078 		fusion->ld_map[i] = NULL;
1079 		fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1080 			fusion->drv_map_pages);
1081 		if (!fusion->ld_drv_map[i]) {
1082 			dev_err(&instance->pdev->dev, "Could not allocate "
1083 				"memory for local map info for %d pages\n",
1084 				fusion->drv_map_pages);
1085 			if (i == 1)
1086 				free_pages((ulong)fusion->ld_drv_map[0],
1087 					fusion->drv_map_pages);
1088 			goto fail_ioc_init;
1089 		}
1090 		memset(fusion->ld_drv_map[i], 0,
1091 			((1 << PAGE_SHIFT) << fusion->drv_map_pages));
1092 	}
1093 
1094 	for (i = 0; i < 2; i++) {
1095 		fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1096 						       fusion->max_map_sz,
1097 						       &fusion->ld_map_phys[i],
1098 						       GFP_KERNEL);
1099 		if (!fusion->ld_map[i]) {
1100 			printk(KERN_ERR "megasas: Could not allocate memory "
1101 			       "for map info\n");
1102 			goto fail_map_info;
1103 		}
1104 	}
1105 
1106 	if (!megasas_get_map_info(instance))
1107 		megasas_sync_map_info(instance);
1108 
1109 	return 0;
1110 
1111 fail_map_info:
1112 	if (i == 1)
1113 		dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
1114 				  fusion->ld_map[0], fusion->ld_map_phys[0]);
1115 fail_ioc_init:
1116 	megasas_free_cmds_fusion(instance);
1117 fail_alloc_cmds:
1118 	megasas_free_cmds(instance);
1119 fail_alloc_mfi_cmds:
1120 	return 1;
1121 }
1122 
1123 /**
1124  * map_cmd_status -	Maps FW cmd status to OS cmd status
1125  * @cmd :		Pointer to cmd
1126  * @status :		status of cmd returned by FW
1127  * @ext_status :	ext status of cmd returned by FW
1128  */
1129 
1130 void
1131 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1132 {
1133 
1134 	switch (status) {
1135 
1136 	case MFI_STAT_OK:
1137 		cmd->scmd->result = DID_OK << 16;
1138 		break;
1139 
1140 	case MFI_STAT_SCSI_IO_FAILED:
1141 	case MFI_STAT_LD_INIT_IN_PROGRESS:
1142 		cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1143 		break;
1144 
1145 	case MFI_STAT_SCSI_DONE_WITH_ERROR:
1146 
1147 		cmd->scmd->result = (DID_OK << 16) | ext_status;
1148 		if (ext_status == SAM_STAT_CHECK_CONDITION) {
1149 			memset(cmd->scmd->sense_buffer, 0,
1150 			       SCSI_SENSE_BUFFERSIZE);
1151 			memcpy(cmd->scmd->sense_buffer, cmd->sense,
1152 			       SCSI_SENSE_BUFFERSIZE);
1153 			cmd->scmd->result |= DRIVER_SENSE << 24;
1154 		}
1155 		break;
1156 
1157 	case MFI_STAT_LD_OFFLINE:
1158 	case MFI_STAT_DEVICE_NOT_FOUND:
1159 		cmd->scmd->result = DID_BAD_TARGET << 16;
1160 		break;
1161 	case MFI_STAT_CONFIG_SEQ_MISMATCH:
1162 		cmd->scmd->result = DID_IMM_RETRY << 16;
1163 		break;
1164 	default:
1165 		printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1166 		cmd->scmd->result = DID_ERROR << 16;
1167 		break;
1168 	}
1169 }
1170 
1171 /**
1172  * megasas_make_sgl_fusion -	Prepares 32-bit SGL
1173  * @instance:		Adapter soft state
1174  * @scp:		SCSI command from the mid-layer
1175  * @sgl_ptr:		SGL to be filled in
1176  * @cmd:		cmd we are working on
1177  *
1178  * If successful, this function returns the number of SG elements.
1179  */
1180 static int
1181 megasas_make_sgl_fusion(struct megasas_instance *instance,
1182 			struct scsi_cmnd *scp,
1183 			struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1184 			struct megasas_cmd_fusion *cmd)
1185 {
1186 	int i, sg_processed, sge_count;
1187 	struct scatterlist *os_sgl;
1188 	struct fusion_context *fusion;
1189 
1190 	fusion = instance->ctrl_context;
1191 
1192 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1193 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1194 		struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1195 		sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1196 		sgl_ptr_end->Flags = 0;
1197 	}
1198 
1199 	sge_count = scsi_dma_map(scp);
1200 
1201 	BUG_ON(sge_count < 0);
1202 
1203 	if (sge_count > instance->max_num_sge || !sge_count)
1204 		return sge_count;
1205 
1206 	scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1207 		sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1208 		sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1209 		sgl_ptr->Flags = 0;
1210 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1211 			(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1212 			if (i == sge_count - 1)
1213 				sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1214 		}
1215 		sgl_ptr++;
1216 
1217 		sg_processed = i + 1;
1218 
1219 		if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
1220 		    (sge_count > fusion->max_sge_in_main_msg)) {
1221 
1222 			struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1223 			if ((instance->pdev->device ==
1224 				PCI_DEVICE_ID_LSI_INVADER) ||
1225 				(instance->pdev->device ==
1226 				PCI_DEVICE_ID_LSI_FURY)) {
1227 				if ((le16_to_cpu(cmd->io_request->IoFlags) &
1228 					MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1229 					MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1230 					cmd->io_request->ChainOffset =
1231 						fusion->
1232 						chain_offset_io_request;
1233 				else
1234 					cmd->io_request->ChainOffset = 0;
1235 			} else
1236 				cmd->io_request->ChainOffset =
1237 					fusion->chain_offset_io_request;
1238 
1239 			sg_chain = sgl_ptr;
1240 			/* Prepare chain element */
1241 			sg_chain->NextChainOffset = 0;
1242 			if ((instance->pdev->device ==
1243 				PCI_DEVICE_ID_LSI_INVADER) ||
1244 				(instance->pdev->device ==
1245 				PCI_DEVICE_ID_LSI_FURY))
1246 				sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1247 			else
1248 				sg_chain->Flags =
1249 					(IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1250 					 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1251 			sg_chain->Length =  cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1252 			sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1253 
1254 			sgl_ptr =
1255 			  (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1256 			memset(sgl_ptr, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
1257 		}
1258 	}
1259 
1260 	return sge_count;
1261 }
1262 
1263 /**
1264  * megasas_set_pd_lba -	Sets PD LBA
1265  * @cdb:		CDB
1266  * @cdb_len:		cdb length
1267  * @start_blk:		Start block of IO
1268  *
1269  * Used to set the PD LBA in CDB for FP IOs
1270  */
1271 void
1272 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1273 		   struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1274 		   struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1275 {
1276 	struct MR_LD_RAID *raid;
1277 	u32 ld;
1278 	u64 start_blk = io_info->pdBlock;
1279 	u8 *cdb = io_request->CDB.CDB32;
1280 	u32 num_blocks = io_info->numBlocks;
1281 	u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1282 
1283 	/* Check if T10 PI (DIF) is enabled for this LD */
1284 	ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1285 	raid = MR_LdRaidGet(ld, local_map_ptr);
1286 	if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1287 		memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1288 		cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1289 		cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;
1290 
1291 		if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1292 			cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1293 		else
1294 			cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1295 		cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1296 
1297 		/* LBA */
1298 		cdb[12] = (u8)((start_blk >> 56) & 0xff);
1299 		cdb[13] = (u8)((start_blk >> 48) & 0xff);
1300 		cdb[14] = (u8)((start_blk >> 40) & 0xff);
1301 		cdb[15] = (u8)((start_blk >> 32) & 0xff);
1302 		cdb[16] = (u8)((start_blk >> 24) & 0xff);
1303 		cdb[17] = (u8)((start_blk >> 16) & 0xff);
1304 		cdb[18] = (u8)((start_blk >> 8) & 0xff);
1305 		cdb[19] = (u8)(start_blk & 0xff);
1306 
1307 		/* Logical block reference tag */
1308 		io_request->CDB.EEDP32.PrimaryReferenceTag =
1309 			cpu_to_be32(ref_tag);
1310 		io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
1311 		io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1312 
1313 		/* Transfer length */
1314 		cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1315 		cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1316 		cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1317 		cdb[31] = (u8)(num_blocks & 0xff);
1318 
1319 		/* set SCSI IO EEDPFlags */
1320 		if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1321 			io_request->EEDPFlags = cpu_to_le16(
1322 				MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
1323 				MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1324 				MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1325 				MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1326 				MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1327 		} else {
1328 			io_request->EEDPFlags = cpu_to_le16(
1329 				MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1330 				MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1331 		}
1332 		io_request->Control |= cpu_to_le32((0x4 << 26));
1333 		io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1334 	} else {
1335 		/* Some drives don't support 16/12 byte CDB's, convert to 10 */
1336 		if (((cdb_len == 12) || (cdb_len == 16)) &&
1337 		    (start_blk <= 0xffffffff)) {
1338 			if (cdb_len == 16) {
1339 				opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1340 				flagvals = cdb[1];
1341 				groupnum = cdb[14];
1342 				control = cdb[15];
1343 			} else {
1344 				opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1345 				flagvals = cdb[1];
1346 				groupnum = cdb[10];
1347 				control = cdb[11];
1348 			}
1349 
1350 			memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1351 
1352 			cdb[0] = opcode;
1353 			cdb[1] = flagvals;
1354 			cdb[6] = groupnum;
1355 			cdb[9] = control;
1356 
1357 			/* Transfer length */
1358 			cdb[8] = (u8)(num_blocks & 0xff);
1359 			cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1360 
1361 			io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1362 			cdb_len = 10;
1363 		} else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1364 			/* Convert to 16 byte CDB for large LBA's */
1365 			switch (cdb_len) {
1366 			case 6:
1367 				opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1368 				control = cdb[5];
1369 				break;
1370 			case 10:
1371 				opcode =
1372 					cdb[0] == READ_10 ? READ_16 : WRITE_16;
1373 				flagvals = cdb[1];
1374 				groupnum = cdb[6];
1375 				control = cdb[9];
1376 				break;
1377 			case 12:
1378 				opcode =
1379 					cdb[0] == READ_12 ? READ_16 : WRITE_16;
1380 				flagvals = cdb[1];
1381 				groupnum = cdb[10];
1382 				control = cdb[11];
1383 				break;
1384 			}
1385 
1386 			memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1387 
1388 			cdb[0] = opcode;
1389 			cdb[1] = flagvals;
1390 			cdb[14] = groupnum;
1391 			cdb[15] = control;
1392 
1393 			/* Transfer length */
1394 			cdb[13] = (u8)(num_blocks & 0xff);
1395 			cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1396 			cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1397 			cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1398 
1399 			io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1400 			cdb_len = 16;
1401 		}
1402 
1403 		/* Normal case, just load LBA here */
1404 		switch (cdb_len) {
1405 		case 6:
1406 		{
1407 			u8 val = cdb[1] & 0xE0;
1408 			cdb[3] = (u8)(start_blk & 0xff);
1409 			cdb[2] = (u8)((start_blk >> 8) & 0xff);
1410 			cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1411 			break;
1412 		}
1413 		case 10:
1414 			cdb[5] = (u8)(start_blk & 0xff);
1415 			cdb[4] = (u8)((start_blk >> 8) & 0xff);
1416 			cdb[3] = (u8)((start_blk >> 16) & 0xff);
1417 			cdb[2] = (u8)((start_blk >> 24) & 0xff);
1418 			break;
1419 		case 12:
1420 			cdb[5]    = (u8)(start_blk & 0xff);
1421 			cdb[4]    = (u8)((start_blk >> 8) & 0xff);
1422 			cdb[3]    = (u8)((start_blk >> 16) & 0xff);
1423 			cdb[2]    = (u8)((start_blk >> 24) & 0xff);
1424 			break;
1425 		case 16:
1426 			cdb[9]    = (u8)(start_blk & 0xff);
1427 			cdb[8]    = (u8)((start_blk >> 8) & 0xff);
1428 			cdb[7]    = (u8)((start_blk >> 16) & 0xff);
1429 			cdb[6]    = (u8)((start_blk >> 24) & 0xff);
1430 			cdb[5]    = (u8)((start_blk >> 32) & 0xff);
1431 			cdb[4]    = (u8)((start_blk >> 40) & 0xff);
1432 			cdb[3]    = (u8)((start_blk >> 48) & 0xff);
1433 			cdb[2]    = (u8)((start_blk >> 56) & 0xff);
1434 			break;
1435 		}
1436 	}
1437 }
1438 
1439 /**
1440  * megasas_build_ldio_fusion -	Prepares IOs to devices
1441  * @instance:		Adapter soft state
1442  * @scp:		SCSI command
1443  * @cmd:		Command to be prepared
1444  *
1445  * Prepares the io_request and chain elements (sg_frame) for IO
1446  * The IO can be for PD (Fast Path) or LD
1447  */
1448 void
1449 megasas_build_ldio_fusion(struct megasas_instance *instance,
1450 			  struct scsi_cmnd *scp,
1451 			  struct megasas_cmd_fusion *cmd)
1452 {
1453 	u8 fp_possible;
1454 	u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1455 	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1456 	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1457 	struct IO_REQUEST_INFO io_info;
1458 	struct fusion_context *fusion;
1459 	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1460 	u8 *raidLUN;
1461 
1462 	device_id = MEGASAS_DEV_INDEX(scp);
1463 
1464 	fusion = instance->ctrl_context;
1465 
1466 	io_request = cmd->io_request;
1467 	io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1468 	io_request->RaidContext.status = 0;
1469 	io_request->RaidContext.exStatus = 0;
1470 
1471 	req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1472 
1473 	start_lba_lo = 0;
1474 	start_lba_hi = 0;
1475 	fp_possible = 0;
1476 
1477 	/*
1478 	 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1479 	 */
1480 	if (scp->cmd_len == 6) {
1481 		datalength = (u32) scp->cmnd[4];
1482 		start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1483 			((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1484 
1485 		start_lba_lo &= 0x1FFFFF;
1486 	}
1487 
1488 	/*
1489 	 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1490 	 */
1491 	else if (scp->cmd_len == 10) {
1492 		datalength = (u32) scp->cmnd[8] |
1493 			((u32) scp->cmnd[7] << 8);
1494 		start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1495 			((u32) scp->cmnd[3] << 16) |
1496 			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1497 	}
1498 
1499 	/*
1500 	 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1501 	 */
1502 	else if (scp->cmd_len == 12) {
1503 		datalength = ((u32) scp->cmnd[6] << 24) |
1504 			((u32) scp->cmnd[7] << 16) |
1505 			((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1506 		start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1507 			((u32) scp->cmnd[3] << 16) |
1508 			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1509 	}
1510 
1511 	/*
1512 	 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1513 	 */
1514 	else if (scp->cmd_len == 16) {
1515 		datalength = ((u32) scp->cmnd[10] << 24) |
1516 			((u32) scp->cmnd[11] << 16) |
1517 			((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1518 		start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1519 			((u32) scp->cmnd[7] << 16) |
1520 			((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1521 
1522 		start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1523 			((u32) scp->cmnd[3] << 16) |
1524 			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1525 	}
1526 
1527 	memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1528 	io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1529 	io_info.numBlocks = datalength;
1530 	io_info.ldTgtId = device_id;
1531 	io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1532 
1533 	if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1534 		io_info.isRead = 1;
1535 
1536 	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1537 
1538 	if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1539 		instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
1540 		io_request->RaidContext.regLockFlags  = 0;
1541 		fp_possible = 0;
1542 	} else {
1543 		if (MR_BuildRaidContext(instance, &io_info,
1544 					&io_request->RaidContext,
1545 					local_map_ptr, &raidLUN))
1546 			fp_possible = io_info.fpOkForIo;
1547 	}
1548 
1549 	/* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
1550 	   id by default, not CPU group id, otherwise all MSI-X queues won't
1551 	   be utilized */
1552 	cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1553 		raw_smp_processor_id() % instance->msix_vectors : 0;
1554 
1555 	if (fp_possible) {
1556 		megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1557 				   local_map_ptr, start_lba_lo);
1558 		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1559 		cmd->request_desc->SCSIIO.RequestFlags =
1560 			(MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1561 			 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1562 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1563 			(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1564 			if (io_request->RaidContext.regLockFlags ==
1565 			    REGION_TYPE_UNUSED)
1566 				cmd->request_desc->SCSIIO.RequestFlags =
1567 					(MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1568 					MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1569 			io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1570 			io_request->RaidContext.nseg = 0x1;
1571 			io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1572 			io_request->RaidContext.regLockFlags |=
1573 			  (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1574 			   MR_RL_FLAGS_SEQ_NUM_ENABLE);
1575 		}
1576 		if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1577 		    (io_info.isRead)) {
1578 			io_info.devHandle =
1579 				get_updated_dev_handle(instance,
1580 					&fusion->load_balance_info[device_id],
1581 					&io_info);
1582 			scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1583 			cmd->pd_r1_lb = io_info.pd_after_lb;
1584 		} else
1585 			scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1586 
1587 		if ((raidLUN[0] == 1) &&
1588 			(local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 2)) {
1589 			instance->dev_handle = !(instance->dev_handle);
1590 			io_info.devHandle =
1591 				local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
1592 		}
1593 
1594 		cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1595 		io_request->DevHandle = io_info.devHandle;
1596 		/* populate the LUN field */
1597 		memcpy(io_request->LUN, raidLUN, 8);
1598 	} else {
1599 		io_request->RaidContext.timeoutValue =
1600 			cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
1601 		cmd->request_desc->SCSIIO.RequestFlags =
1602 			(MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1603 			 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1604 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1605 			(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1606 			if (io_request->RaidContext.regLockFlags ==
1607 			    REGION_TYPE_UNUSED)
1608 				cmd->request_desc->SCSIIO.RequestFlags =
1609 					(MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1610 					MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1611 			io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1612 			io_request->RaidContext.regLockFlags |=
1613 				(MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1614 				 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1615 			io_request->RaidContext.nseg = 0x1;
1616 		}
1617 		io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1618 		io_request->DevHandle = cpu_to_le16(device_id);
1619 	} /* Not FP */
1620 }
1621 
1622 /**
1623  * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
1624  * @instance:		Adapter soft state
1625  * @scp:		SCSI command
1626  * @cmd:		Command to be prepared
1627  *
1628  * Prepares the io_request frame for non-rw io cmds for vd.
1629  */
1630 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1631 			  struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
1632 {
1633 	u32 device_id;
1634 	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1635 	u16 pd_index = 0;
1636 	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1637 	struct fusion_context *fusion = instance->ctrl_context;
1638 	u8                          span, physArm;
1639 	__le16                      devHandle;
1640 	u32                         ld, arRef, pd;
1641 	struct MR_LD_RAID                  *raid;
1642 	struct RAID_CONTEXT                *pRAID_Context;
1643 	u8 fp_possible = 1;
1644 
1645 	io_request = cmd->io_request;
1646 	device_id = MEGASAS_DEV_INDEX(scmd);
1647 	pd_index = MEGASAS_PD_INDEX(scmd);
1648 	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1649 	io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1650 	/* get RAID_Context pointer */
1651 	pRAID_Context = &io_request->RaidContext;
1652 	/* Check with FW team */
1653 	pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1654 	pRAID_Context->regLockRowLBA    = 0;
1655 	pRAID_Context->regLockLength    = 0;
1656 
1657 	if (fusion->fast_path_io && (
1658 		device_id < instance->fw_supported_vd_count)) {
1659 
1660 		ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1661 		if (ld >= instance->fw_supported_vd_count)
1662 			fp_possible = 0;
1663 
1664 		raid = MR_LdRaidGet(ld, local_map_ptr);
1665 		if (!(raid->capability.fpNonRWCapable))
1666 			fp_possible = 0;
1667 	} else
1668 		fp_possible = 0;
1669 
1670 	if (!fp_possible) {
1671 		io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1672 		io_request->DevHandle = cpu_to_le16(device_id);
1673 		io_request->LUN[1] = scmd->device->lun;
1674 		pRAID_Context->timeoutValue =
1675 			cpu_to_le16 (scmd->request->timeout / HZ);
1676 		cmd->request_desc->SCSIIO.RequestFlags =
1677 			(MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1678 			MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1679 	} else {
1680 
1681 		/* set RAID context values */
1682 		pRAID_Context->configSeqNum = raid->seqNum;
1683 		pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1684 		pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
1685 
1686 		/* get the DevHandle for the PD (since this is
1687 		   fpNonRWCapable, this is a single disk RAID0) */
1688 		span = physArm = 0;
1689 		arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1690 		pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1691 		devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1692 
1693 		/* build request descriptor */
1694 		cmd->request_desc->SCSIIO.RequestFlags =
1695 			(MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1696 			MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1697 		cmd->request_desc->SCSIIO.DevHandle = devHandle;
1698 
1699 		/* populate the LUN field */
1700 		memcpy(io_request->LUN, raid->LUN, 8);
1701 
1702 		/* build the raidScsiIO structure */
1703 		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1704 		io_request->DevHandle = devHandle;
1705 	}
1706 }
1707 
1708 /**
1709  * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1710  * @instance:		Adapter soft state
1711  * @scp:		SCSI command
1712  * @cmd:		Command to be prepared
1713  * @fp_possible:	parameter to detect fast path or firmware path io.
1714  *
1715  * Prepares the io_request frame for rw/non-rw io cmds for syspds
1716  */
1717 static void
1718 megasas_build_syspd_fusion(struct megasas_instance *instance,
1719 	struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1720 {
1721 	u32 device_id;
1722 	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1723 	u16 pd_index = 0;
1724 	u16 os_timeout_value;
1725 	u16 timeout_limit;
1726 	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1727 	struct RAID_CONTEXT	*pRAID_Context;
1728 	struct fusion_context *fusion = instance->ctrl_context;
1729 
1730 	device_id = MEGASAS_DEV_INDEX(scmd);
1731 	pd_index = MEGASAS_PD_INDEX(scmd);
1732 	os_timeout_value = scmd->request->timeout / HZ;
1733 
1734 	io_request = cmd->io_request;
1735 	/* get RAID_Context pointer */
1736 	pRAID_Context = &io_request->RaidContext;
1737 	io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1738 	io_request->LUN[1] = scmd->device->lun;
1739 	pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1740 		<< MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1741 
1742 	pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1743 	pRAID_Context->configSeqNum = 0;
1744 	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1745 	io_request->DevHandle =
1746 		local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1747 
1748 	cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1749 	cmd->request_desc->SCSIIO.MSIxIndex =
1750 		instance->msix_vectors ?
1751 		(raw_smp_processor_id() % instance->msix_vectors) : 0;
1752 
1753 
1754 	if (!fp_possible) {
1755 		/* system pd firmware path */
1756 		io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1757 		cmd->request_desc->SCSIIO.RequestFlags =
1758 			(MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1759 				MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1760 		pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
1761 	} else {
1762 		/* system pd Fast Path */
1763 		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1764 		pRAID_Context->regLockFlags = 0;
1765 		pRAID_Context->regLockRowLBA = 0;
1766 		pRAID_Context->regLockLength = 0;
1767 		timeout_limit = (scmd->device->type == TYPE_DISK) ?
1768 				255 : 0xFFFF;
1769 		pRAID_Context->timeoutValue =
1770 			cpu_to_le16((os_timeout_value > timeout_limit) ?
1771 			timeout_limit : os_timeout_value);
1772 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1773 			(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1774 			cmd->request_desc->SCSIIO.RequestFlags |=
1775 				(MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1776 				MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1777 			pRAID_Context->Type = MPI2_TYPE_CUDA;
1778 			pRAID_Context->nseg = 0x1;
1779 			io_request->IoFlags |=
1780 				cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1781 		}
1782 		cmd->request_desc->SCSIIO.RequestFlags =
1783 			(MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1784 				MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1785 	}
1786 }
1787 
1788 /**
1789  * megasas_build_io_fusion -	Prepares IOs to devices
1790  * @instance:		Adapter soft state
1791  * @scp:		SCSI command
1792  * @cmd:		Command to be prepared
1793  *
1794  * Invokes helper functions to prepare request frames
1795  * and sets flags appropriate for IO/Non-IO cmd
1796  */
1797 int
1798 megasas_build_io_fusion(struct megasas_instance *instance,
1799 			struct scsi_cmnd *scp,
1800 			struct megasas_cmd_fusion *cmd)
1801 {
1802 	u32 sge_count;
1803 	u8  cmd_type;
1804 	struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1805 
1806 	/* Zero out some fields so they don't get reused */
1807 	memset(io_request->LUN, 0x0, 8);
1808 	io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1809 	io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1810 	io_request->EEDPFlags = 0;
1811 	io_request->Control = 0;
1812 	io_request->EEDPBlockSize = 0;
1813 	io_request->ChainOffset = 0;
1814 	io_request->RaidContext.RAIDFlags = 0;
1815 	io_request->RaidContext.Type = 0;
1816 	io_request->RaidContext.nseg = 0;
1817 
1818 	memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1819 	/*
1820 	 * Just the CDB length,rest of the Flags are zero
1821 	 * This will be modified for FP in build_ldio_fusion
1822 	 */
1823 	io_request->IoFlags = cpu_to_le16(scp->cmd_len);
1824 
1825 	switch (cmd_type = megasas_cmd_type(scp)) {
1826 	case READ_WRITE_LDIO:
1827 		megasas_build_ldio_fusion(instance, scp, cmd);
1828 		break;
1829 	case NON_READ_WRITE_LDIO:
1830 		megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1831 		break;
1832 	case READ_WRITE_SYSPDIO:
1833 	case NON_READ_WRITE_SYSPDIO:
1834 		if (instance->secure_jbod_support &&
1835 			(cmd_type == NON_READ_WRITE_SYSPDIO))
1836 			megasas_build_syspd_fusion(instance, scp, cmd, 0);
1837 		else
1838 			megasas_build_syspd_fusion(instance, scp, cmd, 1);
1839 		break;
1840 	default:
1841 		break;
1842 	}
1843 
1844 	/*
1845 	 * Construct SGL
1846 	 */
1847 
1848 	sge_count =
1849 		megasas_make_sgl_fusion(instance, scp,
1850 					(struct MPI25_IEEE_SGE_CHAIN64 *)
1851 					&io_request->SGL, cmd);
1852 
1853 	if (sge_count > instance->max_num_sge) {
1854 		printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1855 		       "max (0x%x) allowed\n", sge_count,
1856 		       instance->max_num_sge);
1857 		return 1;
1858 	}
1859 
1860 	io_request->RaidContext.numSGE = sge_count;
1861 
1862 	io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
1863 
1864 	if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1865 		io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
1866 	else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1867 		io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
1868 
1869 	io_request->SGLOffset0 =
1870 		offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1871 
1872 	io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
1873 	io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1874 
1875 	cmd->scmd = scp;
1876 	scp->SCp.ptr = (char *)cmd;
1877 
1878 	return 0;
1879 }
1880 
1881 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1882 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1883 {
1884 	u8 *p;
1885 	struct fusion_context *fusion;
1886 
1887 	if (index >= instance->max_fw_cmds) {
1888 		printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1889 		       "descriptor for scsi%d\n", index,
1890 			instance->host->host_no);
1891 		return NULL;
1892 	}
1893 	fusion = instance->ctrl_context;
1894 	p = fusion->req_frames_desc
1895 		+sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1896 
1897 	return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1898 }
1899 
1900 /**
1901  * megasas_build_and_issue_cmd_fusion -Main routine for building and
1902  *                                     issuing non IOCTL cmd
1903  * @instance:			Adapter soft state
1904  * @scmd:			pointer to scsi cmd from OS
1905  */
1906 static u32
1907 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1908 				   struct scsi_cmnd *scmd)
1909 {
1910 	struct megasas_cmd_fusion *cmd;
1911 	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1912 	u32 index;
1913 	struct fusion_context *fusion;
1914 
1915 	fusion = instance->ctrl_context;
1916 
1917 	cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
1918 
1919 	index = cmd->index;
1920 
1921 	req_desc = megasas_get_request_descriptor(instance, index-1);
1922 	if (!req_desc)
1923 		return 1;
1924 
1925 	req_desc->Words = 0;
1926 	cmd->request_desc = req_desc;
1927 
1928 	if (megasas_build_io_fusion(instance, scmd, cmd)) {
1929 		megasas_return_cmd_fusion(instance, cmd);
1930 		printk(KERN_ERR "megasas: Error building command.\n");
1931 		cmd->request_desc = NULL;
1932 		return 1;
1933 	}
1934 
1935 	req_desc = cmd->request_desc;
1936 	req_desc->SCSIIO.SMID = cpu_to_le16(index);
1937 
1938 	if (cmd->io_request->ChainOffset != 0 &&
1939 	    cmd->io_request->ChainOffset != 0xF)
1940 		printk(KERN_ERR "megasas: The chain offset value is not "
1941 		       "correct : %x\n", cmd->io_request->ChainOffset);
1942 
1943 	/*
1944 	 * Issue the command to the FW
1945 	 */
1946 	atomic_inc(&instance->fw_outstanding);
1947 
1948 	megasas_fire_cmd_fusion(instance, req_desc);
1949 
1950 	return 0;
1951 }
1952 
1953 /**
1954  * complete_cmd_fusion -	Completes command
1955  * @instance:			Adapter soft state
1956  * Completes all commands that is in reply descriptor queue
1957  */
1958 int
1959 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1960 {
1961 	union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1962 	struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1963 	struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1964 	struct fusion_context *fusion;
1965 	struct megasas_cmd *cmd_mfi;
1966 	struct megasas_cmd_fusion *cmd_fusion;
1967 	u16 smid, num_completed;
1968 	u8 reply_descript_type;
1969 	u32 status, extStatus, device_id;
1970 	union desc_value d_val;
1971 	struct LD_LOAD_BALANCE_INFO *lbinfo;
1972 	int threshold_reply_count = 0;
1973 	struct scsi_cmnd *scmd_local = NULL;
1974 
1975 	fusion = instance->ctrl_context;
1976 
1977 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1978 		return IRQ_HANDLED;
1979 
1980 	desc = fusion->reply_frames_desc;
1981 	desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1982 		 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1983 		fusion->last_reply_idx[MSIxIndex];
1984 
1985 	reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1986 
1987 	d_val.word = desc->Words;
1988 
1989 	reply_descript_type = reply_desc->ReplyFlags &
1990 		MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1991 
1992 	if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1993 		return IRQ_NONE;
1994 
1995 	num_completed = 0;
1996 
1997 	while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
1998 	       d_val.u.high != cpu_to_le32(UINT_MAX)) {
1999 		smid = le16_to_cpu(reply_desc->SMID);
2000 
2001 		cmd_fusion = fusion->cmd_list[smid - 1];
2002 
2003 		scsi_io_req =
2004 			(struct MPI2_RAID_SCSI_IO_REQUEST *)
2005 		  cmd_fusion->io_request;
2006 
2007 		if (cmd_fusion->scmd)
2008 			cmd_fusion->scmd->SCp.ptr = NULL;
2009 
2010 		scmd_local = cmd_fusion->scmd;
2011 		status = scsi_io_req->RaidContext.status;
2012 		extStatus = scsi_io_req->RaidContext.exStatus;
2013 
2014 		switch (scsi_io_req->Function) {
2015 		case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
2016 			/* Update load balancing info */
2017 			device_id = MEGASAS_DEV_INDEX(scmd_local);
2018 			lbinfo = &fusion->load_balance_info[device_id];
2019 			if (cmd_fusion->scmd->SCp.Status &
2020 			    MEGASAS_LOAD_BALANCE_FLAG) {
2021 				atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
2022 				cmd_fusion->scmd->SCp.Status &=
2023 					~MEGASAS_LOAD_BALANCE_FLAG;
2024 			}
2025 			if (reply_descript_type ==
2026 			    MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2027 				if (megasas_dbg_lvl == 5)
2028 					printk(KERN_ERR "\nmegasas: FAST Path "
2029 					       "IO Success\n");
2030 			}
2031 			/* Fall thru and complete IO */
2032 		case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2033 			/* Map the FW Cmd Status */
2034 			map_cmd_status(cmd_fusion, status, extStatus);
2035 			scsi_io_req->RaidContext.status = 0;
2036 			scsi_io_req->RaidContext.exStatus = 0;
2037 			megasas_return_cmd_fusion(instance, cmd_fusion);
2038 			scsi_dma_unmap(scmd_local);
2039 			scmd_local->scsi_done(scmd_local);
2040 			atomic_dec(&instance->fw_outstanding);
2041 
2042 			break;
2043 		case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2044 			cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2045 
2046 			/* Poll mode. Dummy free.
2047 			 * In case of Interrupt mode, caller has reverse check.
2048 			 */
2049 			if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2050 				cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2051 				megasas_return_cmd(instance, cmd_mfi);
2052 			} else
2053 				megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2054 			break;
2055 		}
2056 
2057 		fusion->last_reply_idx[MSIxIndex]++;
2058 		if (fusion->last_reply_idx[MSIxIndex] >=
2059 		    fusion->reply_q_depth)
2060 			fusion->last_reply_idx[MSIxIndex] = 0;
2061 
2062 		desc->Words = cpu_to_le64(ULLONG_MAX);
2063 		num_completed++;
2064 		threshold_reply_count++;
2065 
2066 		/* Get the next reply descriptor */
2067 		if (!fusion->last_reply_idx[MSIxIndex])
2068 			desc = fusion->reply_frames_desc +
2069 				((MSIxIndex * fusion->reply_alloc_sz)/
2070 				 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
2071 		else
2072 			desc++;
2073 
2074 		reply_desc =
2075 		  (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2076 
2077 		d_val.word = desc->Words;
2078 
2079 		reply_descript_type = reply_desc->ReplyFlags &
2080 			MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2081 
2082 		if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2083 			break;
2084 		/*
2085 		 * Write to reply post host index register after completing threshold
2086 		 * number of reply counts and still there are more replies in reply queue
2087 		 * pending to be completed
2088 		 */
2089 		if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2090 			if ((instance->pdev->device ==
2091 				PCI_DEVICE_ID_LSI_INVADER) ||
2092 				(instance->pdev->device ==
2093 				PCI_DEVICE_ID_LSI_FURY))
2094 				writel(((MSIxIndex & 0x7) << 24) |
2095 					fusion->last_reply_idx[MSIxIndex],
2096 					instance->reply_post_host_index_addr[MSIxIndex/8]);
2097 			else
2098 				writel((MSIxIndex << 24) |
2099 					fusion->last_reply_idx[MSIxIndex],
2100 					instance->reply_post_host_index_addr[0]);
2101 			threshold_reply_count = 0;
2102 		}
2103 	}
2104 
2105 	if (!num_completed)
2106 		return IRQ_NONE;
2107 
2108 	wmb();
2109 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2110 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2111 		writel(((MSIxIndex & 0x7) << 24) |
2112 			fusion->last_reply_idx[MSIxIndex],
2113 			instance->reply_post_host_index_addr[MSIxIndex/8]);
2114 	else
2115 		writel((MSIxIndex << 24) |
2116 			fusion->last_reply_idx[MSIxIndex],
2117 			instance->reply_post_host_index_addr[0]);
2118 	megasas_check_and_restore_queue_depth(instance);
2119 	return IRQ_HANDLED;
2120 }
2121 
2122 /**
2123  * megasas_complete_cmd_dpc_fusion -	Completes command
2124  * @instance:			Adapter soft state
2125  *
2126  * Tasklet to complete cmds
2127  */
2128 void
2129 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2130 {
2131 	struct megasas_instance *instance =
2132 		(struct megasas_instance *)instance_addr;
2133 	unsigned long flags;
2134 	u32 count, MSIxIndex;
2135 
2136 	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2137 
2138 	/* If we have already declared adapter dead, donot complete cmds */
2139 	spin_lock_irqsave(&instance->hba_lock, flags);
2140 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2141 		spin_unlock_irqrestore(&instance->hba_lock, flags);
2142 		return;
2143 	}
2144 	spin_unlock_irqrestore(&instance->hba_lock, flags);
2145 
2146 	for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2147 		complete_cmd_fusion(instance, MSIxIndex);
2148 }
2149 
2150 /**
2151  * megasas_isr_fusion - isr entry point
2152  */
2153 irqreturn_t megasas_isr_fusion(int irq, void *devp)
2154 {
2155 	struct megasas_irq_context *irq_context = devp;
2156 	struct megasas_instance *instance = irq_context->instance;
2157 	u32 mfiStatus, fw_state, dma_state;
2158 
2159 	if (instance->mask_interrupts)
2160 		return IRQ_NONE;
2161 
2162 	if (!instance->msix_vectors) {
2163 		mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2164 		if (!mfiStatus)
2165 			return IRQ_NONE;
2166 	}
2167 
2168 	/* If we are resetting, bail */
2169 	if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2170 		instance->instancet->clear_intr(instance->reg_set);
2171 		return IRQ_HANDLED;
2172 	}
2173 
2174 	if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2175 		instance->instancet->clear_intr(instance->reg_set);
2176 		/* If we didn't complete any commands, check for FW fault */
2177 		fw_state = instance->instancet->read_fw_status_reg(
2178 			instance->reg_set) & MFI_STATE_MASK;
2179 		dma_state = instance->instancet->read_fw_status_reg
2180 			(instance->reg_set) & MFI_STATE_DMADONE;
2181 		if (instance->crash_dump_drv_support &&
2182 			instance->crash_dump_app_support) {
2183 			/* Start collecting crash, if DMA bit is done */
2184 			if ((fw_state == MFI_STATE_FAULT) && dma_state)
2185 				schedule_work(&instance->crash_init);
2186 			else if (fw_state == MFI_STATE_FAULT)
2187 				schedule_work(&instance->work_init);
2188 		} else if (fw_state == MFI_STATE_FAULT) {
2189 			printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2190 			       "for scsi%d\n", instance->host->host_no);
2191 			schedule_work(&instance->work_init);
2192 		}
2193 	}
2194 
2195 	return IRQ_HANDLED;
2196 }
2197 
2198 /**
2199  * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2200  * @instance:			Adapter soft state
2201  * mfi_cmd:			megasas_cmd pointer
2202  *
2203  */
2204 u8
2205 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2206 			struct megasas_cmd *mfi_cmd)
2207 {
2208 	struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2209 	struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2210 	struct megasas_cmd_fusion *cmd;
2211 	struct fusion_context *fusion;
2212 	struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2213 
2214 	fusion = instance->ctrl_context;
2215 
2216 	cmd = megasas_get_cmd_fusion(instance,
2217 			instance->max_scsi_cmds + mfi_cmd->index);
2218 
2219 	/*  Save the smid. To be used for returning the cmd */
2220 	mfi_cmd->context.smid = cmd->index;
2221 
2222 	/*
2223 	 * For cmds where the flag is set, store the flag and check
2224 	 * on completion. For cmds with this flag, don't call
2225 	 * megasas_complete_cmd
2226 	 */
2227 
2228 	if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
2229 		mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
2230 
2231 	io_req = cmd->io_request;
2232 
2233 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2234 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2235 		struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2236 			(struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2237 		sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2238 		sgl_ptr_end->Flags = 0;
2239 	}
2240 
2241 	mpi25_ieee_chain =
2242 	  (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2243 
2244 	io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2245 	io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2246 				       SGL) / 4;
2247 	io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2248 
2249 	mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
2250 
2251 	mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2252 		MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2253 
2254 	mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
2255 
2256 	return 0;
2257 }
2258 
2259 /**
2260  * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2261  * @instance:			Adapter soft state
2262  * @cmd:			mfi cmd to build
2263  *
2264  */
2265 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2266 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2267 {
2268 	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2269 	u16 index;
2270 
2271 	if (build_mpt_mfi_pass_thru(instance, cmd)) {
2272 		printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2273 		return NULL;
2274 	}
2275 
2276 	index = cmd->context.smid;
2277 
2278 	req_desc = megasas_get_request_descriptor(instance, index - 1);
2279 
2280 	if (!req_desc)
2281 		return NULL;
2282 
2283 	req_desc->Words = 0;
2284 	req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2285 					 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2286 
2287 	req_desc->SCSIIO.SMID = cpu_to_le16(index);
2288 
2289 	return req_desc;
2290 }
2291 
2292 /**
2293  * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2294  * @instance:			Adapter soft state
2295  * @cmd:			mfi cmd pointer
2296  *
2297  */
2298 void
2299 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2300 			  struct megasas_cmd *cmd)
2301 {
2302 	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2303 
2304 	req_desc = build_mpt_cmd(instance, cmd);
2305 	if (!req_desc) {
2306 		printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2307 		return;
2308 	}
2309 	megasas_fire_cmd_fusion(instance, req_desc);
2310 }
2311 
2312 /**
2313  * megasas_release_fusion -	Reverses the FW initialization
2314  * @instance:			Adapter soft state
2315  */
2316 void
2317 megasas_release_fusion(struct megasas_instance *instance)
2318 {
2319 	megasas_free_cmds(instance);
2320 	megasas_free_cmds_fusion(instance);
2321 
2322 	iounmap(instance->reg_set);
2323 
2324 	pci_release_selected_regions(instance->pdev, instance->bar);
2325 }
2326 
2327 /**
2328  * megasas_read_fw_status_reg_fusion - returns the current FW status value
2329  * @regs:			MFI register set
2330  */
2331 static u32
2332 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2333 {
2334 	return readl(&(regs)->outbound_scratch_pad);
2335 }
2336 
2337 /**
2338  * megasas_alloc_host_crash_buffer -	Host buffers for Crash dump collection from Firmware
2339  * @instance:				Controller's soft instance
2340  * return:			        Number of allocated host crash buffers
2341  */
2342 static void
2343 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2344 {
2345 	unsigned int i;
2346 
2347 	instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2348 	for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2349 		instance->crash_buf[i] = (void	*)__get_free_pages(GFP_KERNEL,
2350 				instance->crash_buf_pages);
2351 		if (!instance->crash_buf[i]) {
2352 			dev_info(&instance->pdev->dev, "Firmware crash dump "
2353 				"memory allocation failed at index %d\n", i);
2354 			break;
2355 		}
2356 		memset(instance->crash_buf[i], 0,
2357 			((1 << PAGE_SHIFT) << instance->crash_buf_pages));
2358 	}
2359 	instance->drv_buf_alloc = i;
2360 }
2361 
2362 /**
2363  * megasas_free_host_crash_buffer -	Host buffers for Crash dump collection from Firmware
2364  * @instance:				Controller's soft instance
2365  */
2366 void
2367 megasas_free_host_crash_buffer(struct megasas_instance *instance)
2368 {
2369 	unsigned int i
2370 ;
2371 	for (i = 0; i < instance->drv_buf_alloc; i++) {
2372 		if (instance->crash_buf[i])
2373 			free_pages((ulong)instance->crash_buf[i],
2374 					instance->crash_buf_pages);
2375 	}
2376 	instance->drv_buf_index = 0;
2377 	instance->drv_buf_alloc = 0;
2378 	instance->fw_crash_state = UNAVAILABLE;
2379 	instance->fw_crash_buffer_size = 0;
2380 }
2381 
2382 /**
2383  * megasas_adp_reset_fusion -	For controller reset
2384  * @regs:				MFI register set
2385  */
2386 static int
2387 megasas_adp_reset_fusion(struct megasas_instance *instance,
2388 			 struct megasas_register_set __iomem *regs)
2389 {
2390 	return 0;
2391 }
2392 
2393 /**
2394  * megasas_check_reset_fusion -	For controller reset check
2395  * @regs:				MFI register set
2396  */
2397 static int
2398 megasas_check_reset_fusion(struct megasas_instance *instance,
2399 			   struct megasas_register_set __iomem *regs)
2400 {
2401 	return 0;
2402 }
2403 
2404 /* This function waits for outstanding commands on fusion to complete */
2405 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2406 					int iotimeout, int *convert)
2407 {
2408 	int i, outstanding, retval = 0, hb_seconds_missed = 0;
2409 	u32 fw_state;
2410 
2411 	for (i = 0; i < resetwaittime; i++) {
2412 		/* Check if firmware is in fault state */
2413 		fw_state = instance->instancet->read_fw_status_reg(
2414 			instance->reg_set) & MFI_STATE_MASK;
2415 		if (fw_state == MFI_STATE_FAULT) {
2416 			printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2417 			       " will reset adapter scsi%d.\n",
2418 				instance->host->host_no);
2419 			retval = 1;
2420 			goto out;
2421 		}
2422 		/* If SR-IOV VF mode & heartbeat timeout, don't wait */
2423 		if (instance->requestorId && !iotimeout) {
2424 			retval = 1;
2425 			goto out;
2426 		}
2427 
2428 		/* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2429 		if (instance->requestorId && iotimeout) {
2430 			if (instance->hb_host_mem->HB.fwCounter !=
2431 			    instance->hb_host_mem->HB.driverCounter) {
2432 				instance->hb_host_mem->HB.driverCounter =
2433 					instance->hb_host_mem->HB.fwCounter;
2434 				hb_seconds_missed = 0;
2435 			} else {
2436 				hb_seconds_missed++;
2437 				if (hb_seconds_missed ==
2438 				    (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2439 					printk(KERN_WARNING "megasas: SR-IOV:"
2440 					       " Heartbeat never completed "
2441 					       " while polling during I/O "
2442 					       " timeout handling for "
2443 					       "scsi%d.\n",
2444 					       instance->host->host_no);
2445 					       *convert = 1;
2446 					       retval = 1;
2447 					       goto out;
2448 				}
2449 			}
2450 		}
2451 
2452 		outstanding = atomic_read(&instance->fw_outstanding);
2453 		if (!outstanding)
2454 			goto out;
2455 
2456 		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2457 			printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2458 			       "commands to complete for scsi%d\n", i,
2459 			       outstanding, instance->host->host_no);
2460 			megasas_complete_cmd_dpc_fusion(
2461 				(unsigned long)instance);
2462 		}
2463 		msleep(1000);
2464 	}
2465 
2466 	if (atomic_read(&instance->fw_outstanding)) {
2467 		printk("megaraid_sas: pending commands remain after waiting, "
2468 		       "will reset adapter scsi%d.\n",
2469 		       instance->host->host_no);
2470 		retval = 1;
2471 	}
2472 out:
2473 	return retval;
2474 }
2475 
2476 void  megasas_reset_reply_desc(struct megasas_instance *instance)
2477 {
2478 	int i, count;
2479 	struct fusion_context *fusion;
2480 	union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2481 
2482 	fusion = instance->ctrl_context;
2483 	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2484 	for (i = 0 ; i < count ; i++)
2485 		fusion->last_reply_idx[i] = 0;
2486 	reply_desc = fusion->reply_frames_desc;
2487 	for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2488 		reply_desc->Words = cpu_to_le64(ULLONG_MAX);
2489 }
2490 
2491 /*
2492  * megasas_refire_mgmt_cmd :	Re-fire management commands
2493  * @instance:				Controller's soft instance
2494 */
2495 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2496 {
2497 	int j;
2498 	struct megasas_cmd_fusion *cmd_fusion;
2499 	struct fusion_context *fusion;
2500 	struct megasas_cmd *cmd_mfi;
2501 	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2502 	u16 smid;
2503 
2504 	fusion = instance->ctrl_context;
2505 
2506 	/* Re-fire management commands.
2507 	 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2508 	 */
2509 	for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2510 		cmd_fusion = fusion->cmd_list[j];
2511 		cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2512 		smid = le16_to_cpu(cmd_mfi->context.smid);
2513 
2514 		if (!smid)
2515 			continue;
2516 		req_desc = megasas_get_request_descriptor
2517 					(instance, smid - 1);
2518 		if (req_desc && (cmd_mfi->frame->dcmd.opcode !=
2519 				cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)))
2520 			megasas_fire_cmd_fusion(instance, req_desc);
2521 		else
2522 			megasas_return_cmd(instance, cmd_mfi);
2523 	}
2524 }
2525 
2526 /* Check for a second path that is currently UP */
2527 int megasas_check_mpio_paths(struct megasas_instance *instance,
2528 	struct scsi_cmnd *scmd)
2529 {
2530 	int i, j, retval = (DID_RESET << 16);
2531 
2532 	if (instance->mpio && instance->requestorId) {
2533 		for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2534 			for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2535 				if (megasas_mgmt_info.instance[i] &&
2536 				    (megasas_mgmt_info.instance[i] != instance) &&
2537 				    megasas_mgmt_info.instance[i]->mpio &&
2538 				    megasas_mgmt_info.instance[i]->requestorId
2539 				    &&
2540 				    (megasas_mgmt_info.instance[i]->ld_ids[j]
2541 				     == scmd->device->id)) {
2542 					    retval = (DID_NO_CONNECT << 16);
2543 					    goto out;
2544 				}
2545 	}
2546 out:
2547 	return retval;
2548 }
2549 
2550 /* Core fusion reset function */
2551 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2552 {
2553 	int retval = SUCCESS, i, retry = 0, convert = 0;
2554 	struct megasas_instance *instance;
2555 	struct megasas_cmd_fusion *cmd_fusion;
2556 	struct fusion_context *fusion;
2557 	u32 host_diag, abs_state, status_reg, reset_adapter;
2558 	u32 io_timeout_in_crash_mode = 0;
2559 	struct scsi_cmnd *scmd_local = NULL;
2560 
2561 	instance = (struct megasas_instance *)shost->hostdata;
2562 	fusion = instance->ctrl_context;
2563 
2564 	mutex_lock(&instance->reset_mutex);
2565 
2566 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2567 		printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2568 		       "returning FAILED for scsi%d.\n",
2569 			instance->host->host_no);
2570 		mutex_unlock(&instance->reset_mutex);
2571 		return FAILED;
2572 	}
2573 	status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2574 	abs_state = status_reg & MFI_STATE_MASK;
2575 
2576 	/* IO timeout detected, forcibly put FW in FAULT state */
2577 	if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2578 		instance->crash_dump_app_support && iotimeout) {
2579 		dev_info(&instance->pdev->dev, "IO timeout is detected, "
2580 			"forcibly FAULT Firmware\n");
2581 		instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2582 		status_reg = readl(&instance->reg_set->doorbell);
2583 		writel(status_reg | MFI_STATE_FORCE_OCR,
2584 			&instance->reg_set->doorbell);
2585 		readl(&instance->reg_set->doorbell);
2586 		mutex_unlock(&instance->reset_mutex);
2587 		do {
2588 			ssleep(3);
2589 			io_timeout_in_crash_mode++;
2590 			dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2591 				"seconds for crash dump collection and OCR "
2592 				"to be done\n", (io_timeout_in_crash_mode * 3));
2593 		} while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2594 			(io_timeout_in_crash_mode < 80));
2595 
2596 		if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2597 			dev_info(&instance->pdev->dev, "OCR done for IO "
2598 				"timeout case\n");
2599 			retval = SUCCESS;
2600 		} else {
2601 			dev_info(&instance->pdev->dev, "Controller is not "
2602 				"operational after 240 seconds wait for IO "
2603 				"timeout case in FW crash dump mode\n do "
2604 				"OCR/kill adapter\n");
2605 			retval = megasas_reset_fusion(shost, 0);
2606 		}
2607 		return retval;
2608 	}
2609 
2610 	if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2611 		del_timer_sync(&instance->sriov_heartbeat_timer);
2612 	set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2613 	instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
2614 	instance->instancet->disable_intr(instance);
2615 	msleep(1000);
2616 
2617 	/* First try waiting for commands to complete */
2618 	if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2619 						&convert)) {
2620 		instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2621 		printk(KERN_WARNING "megaraid_sas: resetting fusion "
2622 		       "adapter scsi%d.\n", instance->host->host_no);
2623 		if (convert)
2624 			iotimeout = 0;
2625 
2626 		/* Now return commands back to the OS */
2627 		for (i = 0 ; i < instance->max_scsi_cmds; i++) {
2628 			cmd_fusion = fusion->cmd_list[i];
2629 			scmd_local = cmd_fusion->scmd;
2630 			if (cmd_fusion->scmd) {
2631 				scmd_local->result =
2632 					megasas_check_mpio_paths(instance,
2633 							scmd_local);
2634 				megasas_return_cmd_fusion(instance, cmd_fusion);
2635 				scsi_dma_unmap(scmd_local);
2636 				scmd_local->scsi_done(scmd_local);
2637 				atomic_dec(&instance->fw_outstanding);
2638 			}
2639 		}
2640 
2641 		status_reg = instance->instancet->read_fw_status_reg(
2642 			instance->reg_set);
2643 		abs_state = status_reg & MFI_STATE_MASK;
2644 		reset_adapter = status_reg & MFI_RESET_ADAPTER;
2645 		if (instance->disableOnlineCtrlReset ||
2646 		    (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2647 			/* Reset not supported, kill adapter */
2648 			printk(KERN_WARNING "megaraid_sas: Reset not supported"
2649 			       ", killing adapter scsi%d.\n",
2650 				instance->host->host_no);
2651 			megaraid_sas_kill_hba(instance);
2652 			instance->skip_heartbeat_timer_del = 1;
2653 			retval = FAILED;
2654 			goto out;
2655 		}
2656 
2657 		/* Let SR-IOV VF & PF sync up if there was a HB failure */
2658 		if (instance->requestorId && !iotimeout) {
2659 			msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2660 			/* Look for a late HB update after VF settle time */
2661 			if (abs_state == MFI_STATE_OPERATIONAL &&
2662 			    (instance->hb_host_mem->HB.fwCounter !=
2663 			     instance->hb_host_mem->HB.driverCounter)) {
2664 					instance->hb_host_mem->HB.driverCounter =
2665 						instance->hb_host_mem->HB.fwCounter;
2666 					printk(KERN_WARNING "megasas: SR-IOV:"
2667 					       "Late FW heartbeat update for "
2668 					       "scsi%d.\n",
2669 					       instance->host->host_no);
2670 			} else {
2671 				/* In VF mode, first poll for FW ready */
2672 				for (i = 0;
2673 				     i < (MEGASAS_RESET_WAIT_TIME * 1000);
2674 				     i += 20) {
2675 					status_reg =
2676 						instance->instancet->
2677 						read_fw_status_reg(
2678 							instance->reg_set);
2679 					abs_state = status_reg &
2680 						MFI_STATE_MASK;
2681 					if (abs_state == MFI_STATE_READY) {
2682 						printk(KERN_WARNING "megasas"
2683 						       ": SR-IOV: FW was found"
2684 						       "to be in ready state "
2685 						       "for scsi%d.\n",
2686 						       instance->host->host_no);
2687 						break;
2688 					}
2689 					msleep(20);
2690 				}
2691 				if (abs_state != MFI_STATE_READY) {
2692 					printk(KERN_WARNING "megasas: SR-IOV: "
2693 					       "FW not in ready state after %d"
2694 					       " seconds for scsi%d, status_reg = "
2695 					       "0x%x.\n",
2696 					       MEGASAS_RESET_WAIT_TIME,
2697 					       instance->host->host_no,
2698 					       status_reg);
2699 					megaraid_sas_kill_hba(instance);
2700 					instance->skip_heartbeat_timer_del = 1;
2701 					instance->adprecovery =
2702 						MEGASAS_HW_CRITICAL_ERROR;
2703 					retval = FAILED;
2704 					goto out;
2705 				}
2706 			}
2707 		}
2708 
2709 		/* Now try to reset the chip */
2710 		for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2711 			writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2712 			       &instance->reg_set->fusion_seq_offset);
2713 			writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2714 			       &instance->reg_set->fusion_seq_offset);
2715 			writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2716 			       &instance->reg_set->fusion_seq_offset);
2717 			writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2718 			       &instance->reg_set->fusion_seq_offset);
2719 			writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2720 			       &instance->reg_set->fusion_seq_offset);
2721 			writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2722 			       &instance->reg_set->fusion_seq_offset);
2723 			writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2724 			       &instance->reg_set->fusion_seq_offset);
2725 
2726 			/* Check that the diag write enable (DRWE) bit is on */
2727 			host_diag = readl(&instance->reg_set->fusion_host_diag);
2728 			retry = 0;
2729 			while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2730 				msleep(100);
2731 				host_diag =
2732 				readl(&instance->reg_set->fusion_host_diag);
2733 				if (retry++ == 100) {
2734 					printk(KERN_WARNING "megaraid_sas: "
2735 					       "Host diag unlock failed! "
2736 					       "for scsi%d\n",
2737 						instance->host->host_no);
2738 					break;
2739 				}
2740 			}
2741 			if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2742 				continue;
2743 
2744 			/* Send chip reset command */
2745 			writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2746 			       &instance->reg_set->fusion_host_diag);
2747 			msleep(3000);
2748 
2749 			/* Make sure reset adapter bit is cleared */
2750 			host_diag = readl(&instance->reg_set->fusion_host_diag);
2751 			retry = 0;
2752 			while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2753 				msleep(100);
2754 				host_diag =
2755 				readl(&instance->reg_set->fusion_host_diag);
2756 				if (retry++ == 1000) {
2757 					printk(KERN_WARNING "megaraid_sas: "
2758 					       "Diag reset adapter never "
2759 					       "cleared for scsi%d!\n",
2760 						instance->host->host_no);
2761 					break;
2762 				}
2763 			}
2764 			if (host_diag & HOST_DIAG_RESET_ADAPTER)
2765 				continue;
2766 
2767 			abs_state =
2768 				instance->instancet->read_fw_status_reg(
2769 					instance->reg_set) & MFI_STATE_MASK;
2770 			retry = 0;
2771 
2772 			while ((abs_state <= MFI_STATE_FW_INIT) &&
2773 			       (retry++ < 1000)) {
2774 				msleep(100);
2775 				abs_state =
2776 				instance->instancet->read_fw_status_reg(
2777 					instance->reg_set) & MFI_STATE_MASK;
2778 			}
2779 			if (abs_state <= MFI_STATE_FW_INIT) {
2780 				printk(KERN_WARNING "megaraid_sas: firmware "
2781 				       "state < MFI_STATE_FW_INIT, state = "
2782 				       "0x%x for scsi%d\n", abs_state,
2783 					instance->host->host_no);
2784 				continue;
2785 			}
2786 
2787 			/* Wait for FW to become ready */
2788 			if (megasas_transition_to_ready(instance, 1)) {
2789 				printk(KERN_WARNING "megaraid_sas: Failed to "
2790 				       "transition controller to ready "
2791 				       "for scsi%d.\n",
2792 				       instance->host->host_no);
2793 				continue;
2794 			}
2795 
2796 			megasas_reset_reply_desc(instance);
2797 			if (megasas_ioc_init_fusion(instance)) {
2798 				printk(KERN_WARNING "megaraid_sas: "
2799 				       "megasas_ioc_init_fusion() failed!"
2800 				       " for scsi%d\n",
2801 				       instance->host->host_no);
2802 				continue;
2803 			}
2804 
2805 			megasas_refire_mgmt_cmd(instance);
2806 
2807 			if (megasas_get_ctrl_info(instance)) {
2808 				dev_info(&instance->pdev->dev,
2809 					"Failed from %s %d\n",
2810 					__func__, __LINE__);
2811 				megaraid_sas_kill_hba(instance);
2812 				retval = FAILED;
2813 			}
2814 			/* Reset load balance info */
2815 			memset(fusion->load_balance_info, 0,
2816 			       sizeof(struct LD_LOAD_BALANCE_INFO)
2817 			       *MAX_LOGICAL_DRIVES_EXT);
2818 
2819 			if (!megasas_get_map_info(instance))
2820 				megasas_sync_map_info(instance);
2821 
2822 			clear_bit(MEGASAS_FUSION_IN_RESET,
2823 				  &instance->reset_flags);
2824 			instance->instancet->enable_intr(instance);
2825 			instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2826 
2827 			/* Restart SR-IOV heartbeat */
2828 			if (instance->requestorId) {
2829 				if (!megasas_sriov_start_heartbeat(instance, 0))
2830 					megasas_start_timer(instance,
2831 							    &instance->sriov_heartbeat_timer,
2832 							    megasas_sriov_heartbeat_handler,
2833 							    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2834 				else
2835 					instance->skip_heartbeat_timer_del = 1;
2836 			}
2837 
2838 			/* Adapter reset completed successfully */
2839 			printk(KERN_WARNING "megaraid_sas: Reset "
2840 			       "successful for scsi%d.\n",
2841 				instance->host->host_no);
2842 
2843 			if (instance->crash_dump_drv_support &&
2844 				instance->crash_dump_app_support)
2845 				megasas_set_crash_dump_params(instance,
2846 					MR_CRASH_BUF_TURN_ON);
2847 			else
2848 				megasas_set_crash_dump_params(instance,
2849 					MR_CRASH_BUF_TURN_OFF);
2850 
2851 			retval = SUCCESS;
2852 			goto out;
2853 		}
2854 		/* Reset failed, kill the adapter */
2855 		printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2856 		       "adapter scsi%d.\n", instance->host->host_no);
2857 		megaraid_sas_kill_hba(instance);
2858 		instance->skip_heartbeat_timer_del = 1;
2859 		retval = FAILED;
2860 	} else {
2861 		/* For VF: Restart HB timer if we didn't OCR */
2862 		if (instance->requestorId) {
2863 			megasas_start_timer(instance,
2864 					    &instance->sriov_heartbeat_timer,
2865 					    megasas_sriov_heartbeat_handler,
2866 					    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2867 		}
2868 		clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2869 		instance->instancet->enable_intr(instance);
2870 		instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2871 	}
2872 out:
2873 	clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2874 	mutex_unlock(&instance->reset_mutex);
2875 	return retval;
2876 }
2877 
2878 /* Fusion Crash dump collection work queue */
2879 void  megasas_fusion_crash_dump_wq(struct work_struct *work)
2880 {
2881 	struct megasas_instance *instance =
2882 		container_of(work, struct megasas_instance, crash_init);
2883 	u32 status_reg;
2884 	u8 partial_copy = 0;
2885 
2886 
2887 	status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2888 
2889 	/*
2890 	 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2891 	 * to host crash buffers
2892 	 */
2893 	if (instance->drv_buf_index == 0) {
2894 		/* Buffer is already allocated for old Crash dump.
2895 		 * Do OCR and do not wait for crash dump collection
2896 		 */
2897 		if (instance->drv_buf_alloc) {
2898 			dev_info(&instance->pdev->dev, "earlier crash dump is "
2899 				"not yet copied by application, ignoring this "
2900 				"crash dump and initiating OCR\n");
2901 			status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2902 			writel(status_reg,
2903 				&instance->reg_set->outbound_scratch_pad);
2904 			readl(&instance->reg_set->outbound_scratch_pad);
2905 			return;
2906 		}
2907 		megasas_alloc_host_crash_buffer(instance);
2908 		dev_info(&instance->pdev->dev, "Number of host crash buffers "
2909 			"allocated: %d\n", instance->drv_buf_alloc);
2910 	}
2911 
2912 	/*
2913 	 * Driver has allocated max buffers, which can be allocated
2914 	 * and FW has more crash dump data, then driver will
2915 	 * ignore the data.
2916 	 */
2917 	if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2918 		dev_info(&instance->pdev->dev, "Driver is done copying "
2919 			"the buffer: %d\n", instance->drv_buf_alloc);
2920 		status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2921 		partial_copy = 1;
2922 	} else {
2923 		memcpy(instance->crash_buf[instance->drv_buf_index],
2924 			instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2925 		instance->drv_buf_index++;
2926 		status_reg &= ~MFI_STATE_DMADONE;
2927 	}
2928 
2929 	if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2930 		dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2931 			"of copied buffers: %d\n", instance->drv_buf_index);
2932 		instance->fw_crash_buffer_size =  instance->drv_buf_index;
2933 		instance->fw_crash_state = AVAILABLE;
2934 		instance->drv_buf_index = 0;
2935 		writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2936 		readl(&instance->reg_set->outbound_scratch_pad);
2937 		if (!partial_copy)
2938 			megasas_reset_fusion(instance->host, 0);
2939 	} else {
2940 		writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2941 		readl(&instance->reg_set->outbound_scratch_pad);
2942 	}
2943 }
2944 
2945 
2946 /* Fusion OCR work queue */
2947 void megasas_fusion_ocr_wq(struct work_struct *work)
2948 {
2949 	struct megasas_instance *instance =
2950 		container_of(work, struct megasas_instance, work_init);
2951 
2952 	megasas_reset_fusion(instance->host, 0);
2953 }
2954 
2955 struct megasas_instance_template megasas_instance_template_fusion = {
2956 	.enable_intr = megasas_enable_intr_fusion,
2957 	.disable_intr = megasas_disable_intr_fusion,
2958 	.clear_intr = megasas_clear_intr_fusion,
2959 	.read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2960 	.adp_reset = megasas_adp_reset_fusion,
2961 	.check_reset = megasas_check_reset_fusion,
2962 	.service_isr = megasas_isr_fusion,
2963 	.tasklet = megasas_complete_cmd_dpc_fusion,
2964 	.init_adapter = megasas_init_adapter_fusion,
2965 	.build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2966 	.issue_dcmd = megasas_issue_dcmd_fusion,
2967 };
2968