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