xref: /linux/drivers/scsi/megaraid/megaraid_sas_base.c (revision 827634added7f38b7d724cab1dccdb2b004c13c3)
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2003-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  *  Authors: Avago Technologies
21  *           Sreenivas Bagalkote
22  *           Sumant Patro
23  *           Bo Yang
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/slab.h>
45 #include <asm/uaccess.h>
46 #include <linux/fs.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
51 
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
59 
60 /*
61  * Number of sectors per IO command
62  * Will be set in megasas_init_mfi if user does not provide
63  */
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67 	"Maximum number of sectors per IO command");
68 
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72 
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76 
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80 
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84 	"Adapter queue depth when throttled due to I/O timeout. Default: 16");
85 
86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89 		 "before resetting adapter. Default: 180");
90 
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
94 
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION);
97 MODULE_AUTHOR("megaraidlinux@lsi.com");
98 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
99 
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
103 				 u8 query_type);
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106 				u32 seq_num, u32 class_locale_word);
107 /*
108  * PCI ID table for all supported controllers
109  */
110 static struct pci_device_id megasas_pci_table[] = {
111 
112 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
113 	/* xscale IOP */
114 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
115 	/* ppc IOP */
116 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
117 	/* ppc IOP */
118 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
119 	/* gen2*/
120 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
121 	/* gen2*/
122 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
123 	/* skinny*/
124 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
125 	/* skinny*/
126 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127 	/* xscale IOP, vega */
128 	{PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
129 	/* xscale IOP */
130 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131 	/* Fusion */
132 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133 	/* Plasma */
134 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
135 	/* Invader */
136 	{PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
137 	/* Fury */
138 	{}
139 };
140 
141 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
142 
143 static int megasas_mgmt_majorno;
144 struct megasas_mgmt_info megasas_mgmt_info;
145 static struct fasync_struct *megasas_async_queue;
146 static DEFINE_MUTEX(megasas_async_queue_mutex);
147 
148 static int megasas_poll_wait_aen;
149 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
150 static u32 support_poll_for_event;
151 u32 megasas_dbg_lvl;
152 static u32 support_device_change;
153 
154 /* define lock for aen poll */
155 spinlock_t poll_aen_lock;
156 
157 void
158 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
159 		     u8 alt_status);
160 static u32
161 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
162 static int
163 megasas_adp_reset_gen2(struct megasas_instance *instance,
164 		       struct megasas_register_set __iomem *reg_set);
165 static irqreturn_t megasas_isr(int irq, void *devp);
166 static u32
167 megasas_init_adapter_mfi(struct megasas_instance *instance);
168 u32
169 megasas_build_and_issue_cmd(struct megasas_instance *instance,
170 			    struct scsi_cmnd *scmd);
171 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
172 void
173 megasas_release_fusion(struct megasas_instance *instance);
174 int
175 megasas_ioc_init_fusion(struct megasas_instance *instance);
176 void
177 megasas_free_cmds_fusion(struct megasas_instance *instance);
178 u8
179 megasas_get_map_info(struct megasas_instance *instance);
180 int
181 megasas_sync_map_info(struct megasas_instance *instance);
182 int
183 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
184 	int seconds);
185 void megasas_reset_reply_desc(struct megasas_instance *instance);
186 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
187 void megasas_fusion_ocr_wq(struct work_struct *work);
188 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
189 					 int initial);
190 int megasas_check_mpio_paths(struct megasas_instance *instance,
191 			     struct scsi_cmnd *scmd);
192 
193 void
194 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
195 {
196 	instance->instancet->fire_cmd(instance,
197 		cmd->frame_phys_addr, 0, instance->reg_set);
198 }
199 
200 /**
201  * megasas_get_cmd -	Get a command from the free pool
202  * @instance:		Adapter soft state
203  *
204  * Returns a free command from the pool
205  */
206 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
207 						  *instance)
208 {
209 	unsigned long flags;
210 	struct megasas_cmd *cmd = NULL;
211 
212 	spin_lock_irqsave(&instance->mfi_pool_lock, flags);
213 
214 	if (!list_empty(&instance->cmd_pool)) {
215 		cmd = list_entry((&instance->cmd_pool)->next,
216 				 struct megasas_cmd, list);
217 		list_del_init(&cmd->list);
218 		atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_DETACHED);
219 	} else {
220 		printk(KERN_ERR "megasas: Command pool empty!\n");
221 	}
222 
223 	spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
224 	return cmd;
225 }
226 
227 /**
228  * __megasas_return_cmd -	Return a cmd to free command pool
229  * @instance:		Adapter soft state
230  * @cmd:		Command packet to be returned to free command pool
231  */
232 inline void
233 __megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
234 {
235 	/*
236 	 * Don't go ahead and free the MFI frame, if corresponding
237 	 * MPT frame is not freed(valid for only fusion adapters).
238 	 * In case of MFI adapters, anyways for any allocated MFI
239 	 * frame will have cmd->mfi_mpt_mpthr set to MFI_MPT_DETACHED
240 	 */
241 	if (atomic_read(&cmd->mfi_mpt_pthr) != MFI_MPT_DETACHED)
242 		return;
243 
244 	cmd->scmd = NULL;
245 	cmd->frame_count = 0;
246 	cmd->is_wait_event = 0;
247 	cmd->mpt_pthr_cmd_blocked = NULL;
248 
249 	if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
250 	    (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
251 	    (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
252 	    (reset_devices))
253 		cmd->frame->hdr.cmd = MFI_CMD_INVALID;
254 
255 	atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
256 	list_add(&cmd->list, (&instance->cmd_pool)->next);
257 }
258 
259 /**
260  * megasas_return_cmd -	Return a cmd to free command pool
261  * @instance:		Adapter soft state
262  * @cmd:		Command packet to be returned to free command pool
263  */
264 inline void
265 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
266 {
267 	unsigned long flags;
268 
269 	spin_lock_irqsave(&instance->mfi_pool_lock, flags);
270 	__megasas_return_cmd(instance, cmd);
271 	spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
272 }
273 
274 
275 /**
276 *	The following functions are defined for xscale
277 *	(deviceid : 1064R, PERC5) controllers
278 */
279 
280 /**
281  * megasas_enable_intr_xscale -	Enables interrupts
282  * @regs:			MFI register set
283  */
284 static inline void
285 megasas_enable_intr_xscale(struct megasas_instance *instance)
286 {
287 	struct megasas_register_set __iomem *regs;
288 	regs = instance->reg_set;
289 	writel(0, &(regs)->outbound_intr_mask);
290 
291 	/* Dummy readl to force pci flush */
292 	readl(&regs->outbound_intr_mask);
293 }
294 
295 /**
296  * megasas_disable_intr_xscale -Disables interrupt
297  * @regs:			MFI register set
298  */
299 static inline void
300 megasas_disable_intr_xscale(struct megasas_instance *instance)
301 {
302 	struct megasas_register_set __iomem *regs;
303 	u32 mask = 0x1f;
304 	regs = instance->reg_set;
305 	writel(mask, &regs->outbound_intr_mask);
306 	/* Dummy readl to force pci flush */
307 	readl(&regs->outbound_intr_mask);
308 }
309 
310 /**
311  * megasas_read_fw_status_reg_xscale - returns the current FW status value
312  * @regs:			MFI register set
313  */
314 static u32
315 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
316 {
317 	return readl(&(regs)->outbound_msg_0);
318 }
319 /**
320  * megasas_clear_interrupt_xscale -	Check & clear interrupt
321  * @regs:				MFI register set
322  */
323 static int
324 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
325 {
326 	u32 status;
327 	u32 mfiStatus = 0;
328 	/*
329 	 * Check if it is our interrupt
330 	 */
331 	status = readl(&regs->outbound_intr_status);
332 
333 	if (status & MFI_OB_INTR_STATUS_MASK)
334 		mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
335 	if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
336 		mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
337 
338 	/*
339 	 * Clear the interrupt by writing back the same value
340 	 */
341 	if (mfiStatus)
342 		writel(status, &regs->outbound_intr_status);
343 
344 	/* Dummy readl to force pci flush */
345 	readl(&regs->outbound_intr_status);
346 
347 	return mfiStatus;
348 }
349 
350 /**
351  * megasas_fire_cmd_xscale -	Sends command to the FW
352  * @frame_phys_addr :		Physical address of cmd
353  * @frame_count :		Number of frames for the command
354  * @regs :			MFI register set
355  */
356 static inline void
357 megasas_fire_cmd_xscale(struct megasas_instance *instance,
358 		dma_addr_t frame_phys_addr,
359 		u32 frame_count,
360 		struct megasas_register_set __iomem *regs)
361 {
362 	unsigned long flags;
363 	spin_lock_irqsave(&instance->hba_lock, flags);
364 	writel((frame_phys_addr >> 3)|(frame_count),
365 	       &(regs)->inbound_queue_port);
366 	spin_unlock_irqrestore(&instance->hba_lock, flags);
367 }
368 
369 /**
370  * megasas_adp_reset_xscale -  For controller reset
371  * @regs:                              MFI register set
372  */
373 static int
374 megasas_adp_reset_xscale(struct megasas_instance *instance,
375 	struct megasas_register_set __iomem *regs)
376 {
377 	u32 i;
378 	u32 pcidata;
379 	writel(MFI_ADP_RESET, &regs->inbound_doorbell);
380 
381 	for (i = 0; i < 3; i++)
382 		msleep(1000); /* sleep for 3 secs */
383 	pcidata  = 0;
384 	pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
385 	printk(KERN_NOTICE "pcidata = %x\n", pcidata);
386 	if (pcidata & 0x2) {
387 		printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
388 		pcidata &= ~0x2;
389 		pci_write_config_dword(instance->pdev,
390 				MFI_1068_PCSR_OFFSET, pcidata);
391 
392 		for (i = 0; i < 2; i++)
393 			msleep(1000); /* need to wait 2 secs again */
394 
395 		pcidata  = 0;
396 		pci_read_config_dword(instance->pdev,
397 				MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
398 		printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
399 		if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
400 			printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
401 			pcidata = 0;
402 			pci_write_config_dword(instance->pdev,
403 				MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
404 		}
405 	}
406 	return 0;
407 }
408 
409 /**
410  * megasas_check_reset_xscale -	For controller reset check
411  * @regs:				MFI register set
412  */
413 static int
414 megasas_check_reset_xscale(struct megasas_instance *instance,
415 		struct megasas_register_set __iomem *regs)
416 {
417 
418 	if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
419 	    (le32_to_cpu(*instance->consumer) ==
420 		MEGASAS_ADPRESET_INPROG_SIGN))
421 		return 1;
422 	return 0;
423 }
424 
425 static struct megasas_instance_template megasas_instance_template_xscale = {
426 
427 	.fire_cmd = megasas_fire_cmd_xscale,
428 	.enable_intr = megasas_enable_intr_xscale,
429 	.disable_intr = megasas_disable_intr_xscale,
430 	.clear_intr = megasas_clear_intr_xscale,
431 	.read_fw_status_reg = megasas_read_fw_status_reg_xscale,
432 	.adp_reset = megasas_adp_reset_xscale,
433 	.check_reset = megasas_check_reset_xscale,
434 	.service_isr = megasas_isr,
435 	.tasklet = megasas_complete_cmd_dpc,
436 	.init_adapter = megasas_init_adapter_mfi,
437 	.build_and_issue_cmd = megasas_build_and_issue_cmd,
438 	.issue_dcmd = megasas_issue_dcmd,
439 };
440 
441 /**
442 *	This is the end of set of functions & definitions specific
443 *	to xscale (deviceid : 1064R, PERC5) controllers
444 */
445 
446 /**
447 *	The following functions are defined for ppc (deviceid : 0x60)
448 * 	controllers
449 */
450 
451 /**
452  * megasas_enable_intr_ppc -	Enables interrupts
453  * @regs:			MFI register set
454  */
455 static inline void
456 megasas_enable_intr_ppc(struct megasas_instance *instance)
457 {
458 	struct megasas_register_set __iomem *regs;
459 	regs = instance->reg_set;
460 	writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
461 
462 	writel(~0x80000000, &(regs)->outbound_intr_mask);
463 
464 	/* Dummy readl to force pci flush */
465 	readl(&regs->outbound_intr_mask);
466 }
467 
468 /**
469  * megasas_disable_intr_ppc -	Disable interrupt
470  * @regs:			MFI register set
471  */
472 static inline void
473 megasas_disable_intr_ppc(struct megasas_instance *instance)
474 {
475 	struct megasas_register_set __iomem *regs;
476 	u32 mask = 0xFFFFFFFF;
477 	regs = instance->reg_set;
478 	writel(mask, &regs->outbound_intr_mask);
479 	/* Dummy readl to force pci flush */
480 	readl(&regs->outbound_intr_mask);
481 }
482 
483 /**
484  * megasas_read_fw_status_reg_ppc - returns the current FW status value
485  * @regs:			MFI register set
486  */
487 static u32
488 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
489 {
490 	return readl(&(regs)->outbound_scratch_pad);
491 }
492 
493 /**
494  * megasas_clear_interrupt_ppc -	Check & clear interrupt
495  * @regs:				MFI register set
496  */
497 static int
498 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
499 {
500 	u32 status, mfiStatus = 0;
501 
502 	/*
503 	 * Check if it is our interrupt
504 	 */
505 	status = readl(&regs->outbound_intr_status);
506 
507 	if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
508 		mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
509 
510 	if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
511 		mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
512 
513 	/*
514 	 * Clear the interrupt by writing back the same value
515 	 */
516 	writel(status, &regs->outbound_doorbell_clear);
517 
518 	/* Dummy readl to force pci flush */
519 	readl(&regs->outbound_doorbell_clear);
520 
521 	return mfiStatus;
522 }
523 
524 /**
525  * megasas_fire_cmd_ppc -	Sends command to the FW
526  * @frame_phys_addr :		Physical address of cmd
527  * @frame_count :		Number of frames for the command
528  * @regs :			MFI register set
529  */
530 static inline void
531 megasas_fire_cmd_ppc(struct megasas_instance *instance,
532 		dma_addr_t frame_phys_addr,
533 		u32 frame_count,
534 		struct megasas_register_set __iomem *regs)
535 {
536 	unsigned long flags;
537 	spin_lock_irqsave(&instance->hba_lock, flags);
538 	writel((frame_phys_addr | (frame_count<<1))|1,
539 			&(regs)->inbound_queue_port);
540 	spin_unlock_irqrestore(&instance->hba_lock, flags);
541 }
542 
543 /**
544  * megasas_check_reset_ppc -	For controller reset check
545  * @regs:				MFI register set
546  */
547 static int
548 megasas_check_reset_ppc(struct megasas_instance *instance,
549 			struct megasas_register_set __iomem *regs)
550 {
551 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
552 		return 1;
553 
554 	return 0;
555 }
556 
557 static struct megasas_instance_template megasas_instance_template_ppc = {
558 
559 	.fire_cmd = megasas_fire_cmd_ppc,
560 	.enable_intr = megasas_enable_intr_ppc,
561 	.disable_intr = megasas_disable_intr_ppc,
562 	.clear_intr = megasas_clear_intr_ppc,
563 	.read_fw_status_reg = megasas_read_fw_status_reg_ppc,
564 	.adp_reset = megasas_adp_reset_xscale,
565 	.check_reset = megasas_check_reset_ppc,
566 	.service_isr = megasas_isr,
567 	.tasklet = megasas_complete_cmd_dpc,
568 	.init_adapter = megasas_init_adapter_mfi,
569 	.build_and_issue_cmd = megasas_build_and_issue_cmd,
570 	.issue_dcmd = megasas_issue_dcmd,
571 };
572 
573 /**
574  * megasas_enable_intr_skinny -	Enables interrupts
575  * @regs:			MFI register set
576  */
577 static inline void
578 megasas_enable_intr_skinny(struct megasas_instance *instance)
579 {
580 	struct megasas_register_set __iomem *regs;
581 	regs = instance->reg_set;
582 	writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
583 
584 	writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
585 
586 	/* Dummy readl to force pci flush */
587 	readl(&regs->outbound_intr_mask);
588 }
589 
590 /**
591  * megasas_disable_intr_skinny -	Disables interrupt
592  * @regs:			MFI register set
593  */
594 static inline void
595 megasas_disable_intr_skinny(struct megasas_instance *instance)
596 {
597 	struct megasas_register_set __iomem *regs;
598 	u32 mask = 0xFFFFFFFF;
599 	regs = instance->reg_set;
600 	writel(mask, &regs->outbound_intr_mask);
601 	/* Dummy readl to force pci flush */
602 	readl(&regs->outbound_intr_mask);
603 }
604 
605 /**
606  * megasas_read_fw_status_reg_skinny - returns the current FW status value
607  * @regs:			MFI register set
608  */
609 static u32
610 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
611 {
612 	return readl(&(regs)->outbound_scratch_pad);
613 }
614 
615 /**
616  * megasas_clear_interrupt_skinny -	Check & clear interrupt
617  * @regs:				MFI register set
618  */
619 static int
620 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
621 {
622 	u32 status;
623 	u32 mfiStatus = 0;
624 
625 	/*
626 	 * Check if it is our interrupt
627 	 */
628 	status = readl(&regs->outbound_intr_status);
629 
630 	if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
631 		return 0;
632 	}
633 
634 	/*
635 	 * Check if it is our interrupt
636 	 */
637 	if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
638 	    MFI_STATE_FAULT) {
639 		mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
640 	} else
641 		mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
642 
643 	/*
644 	 * Clear the interrupt by writing back the same value
645 	 */
646 	writel(status, &regs->outbound_intr_status);
647 
648 	/*
649 	* dummy read to flush PCI
650 	*/
651 	readl(&regs->outbound_intr_status);
652 
653 	return mfiStatus;
654 }
655 
656 /**
657  * megasas_fire_cmd_skinny -	Sends command to the FW
658  * @frame_phys_addr :		Physical address of cmd
659  * @frame_count :		Number of frames for the command
660  * @regs :			MFI register set
661  */
662 static inline void
663 megasas_fire_cmd_skinny(struct megasas_instance *instance,
664 			dma_addr_t frame_phys_addr,
665 			u32 frame_count,
666 			struct megasas_register_set __iomem *regs)
667 {
668 	unsigned long flags;
669 	spin_lock_irqsave(&instance->hba_lock, flags);
670 	writel(upper_32_bits(frame_phys_addr),
671 	       &(regs)->inbound_high_queue_port);
672 	writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
673 	       &(regs)->inbound_low_queue_port);
674 	spin_unlock_irqrestore(&instance->hba_lock, flags);
675 }
676 
677 /**
678  * megasas_check_reset_skinny -	For controller reset check
679  * @regs:				MFI register set
680  */
681 static int
682 megasas_check_reset_skinny(struct megasas_instance *instance,
683 				struct megasas_register_set __iomem *regs)
684 {
685 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
686 		return 1;
687 
688 	return 0;
689 }
690 
691 static struct megasas_instance_template megasas_instance_template_skinny = {
692 
693 	.fire_cmd = megasas_fire_cmd_skinny,
694 	.enable_intr = megasas_enable_intr_skinny,
695 	.disable_intr = megasas_disable_intr_skinny,
696 	.clear_intr = megasas_clear_intr_skinny,
697 	.read_fw_status_reg = megasas_read_fw_status_reg_skinny,
698 	.adp_reset = megasas_adp_reset_gen2,
699 	.check_reset = megasas_check_reset_skinny,
700 	.service_isr = megasas_isr,
701 	.tasklet = megasas_complete_cmd_dpc,
702 	.init_adapter = megasas_init_adapter_mfi,
703 	.build_and_issue_cmd = megasas_build_and_issue_cmd,
704 	.issue_dcmd = megasas_issue_dcmd,
705 };
706 
707 
708 /**
709 *	The following functions are defined for gen2 (deviceid : 0x78 0x79)
710 *	controllers
711 */
712 
713 /**
714  * megasas_enable_intr_gen2 -  Enables interrupts
715  * @regs:                      MFI register set
716  */
717 static inline void
718 megasas_enable_intr_gen2(struct megasas_instance *instance)
719 {
720 	struct megasas_register_set __iomem *regs;
721 	regs = instance->reg_set;
722 	writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
723 
724 	/* write ~0x00000005 (4 & 1) to the intr mask*/
725 	writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
726 
727 	/* Dummy readl to force pci flush */
728 	readl(&regs->outbound_intr_mask);
729 }
730 
731 /**
732  * megasas_disable_intr_gen2 - Disables interrupt
733  * @regs:                      MFI register set
734  */
735 static inline void
736 megasas_disable_intr_gen2(struct megasas_instance *instance)
737 {
738 	struct megasas_register_set __iomem *regs;
739 	u32 mask = 0xFFFFFFFF;
740 	regs = instance->reg_set;
741 	writel(mask, &regs->outbound_intr_mask);
742 	/* Dummy readl to force pci flush */
743 	readl(&regs->outbound_intr_mask);
744 }
745 
746 /**
747  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
748  * @regs:                      MFI register set
749  */
750 static u32
751 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
752 {
753 	return readl(&(regs)->outbound_scratch_pad);
754 }
755 
756 /**
757  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
758  * @regs:                              MFI register set
759  */
760 static int
761 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
762 {
763 	u32 status;
764 	u32 mfiStatus = 0;
765 	/*
766 	 * Check if it is our interrupt
767 	 */
768 	status = readl(&regs->outbound_intr_status);
769 
770 	if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
771 		mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
772 	}
773 	if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
774 		mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
775 	}
776 
777 	/*
778 	 * Clear the interrupt by writing back the same value
779 	 */
780 	if (mfiStatus)
781 		writel(status, &regs->outbound_doorbell_clear);
782 
783 	/* Dummy readl to force pci flush */
784 	readl(&regs->outbound_intr_status);
785 
786 	return mfiStatus;
787 }
788 /**
789  * megasas_fire_cmd_gen2 -     Sends command to the FW
790  * @frame_phys_addr :          Physical address of cmd
791  * @frame_count :              Number of frames for the command
792  * @regs :                     MFI register set
793  */
794 static inline void
795 megasas_fire_cmd_gen2(struct megasas_instance *instance,
796 			dma_addr_t frame_phys_addr,
797 			u32 frame_count,
798 			struct megasas_register_set __iomem *regs)
799 {
800 	unsigned long flags;
801 	spin_lock_irqsave(&instance->hba_lock, flags);
802 	writel((frame_phys_addr | (frame_count<<1))|1,
803 			&(regs)->inbound_queue_port);
804 	spin_unlock_irqrestore(&instance->hba_lock, flags);
805 }
806 
807 /**
808  * megasas_adp_reset_gen2 -	For controller reset
809  * @regs:				MFI register set
810  */
811 static int
812 megasas_adp_reset_gen2(struct megasas_instance *instance,
813 			struct megasas_register_set __iomem *reg_set)
814 {
815 	u32			retry = 0 ;
816 	u32			HostDiag;
817 	u32			*seq_offset = &reg_set->seq_offset;
818 	u32			*hostdiag_offset = &reg_set->host_diag;
819 
820 	if (instance->instancet == &megasas_instance_template_skinny) {
821 		seq_offset = &reg_set->fusion_seq_offset;
822 		hostdiag_offset = &reg_set->fusion_host_diag;
823 	}
824 
825 	writel(0, seq_offset);
826 	writel(4, seq_offset);
827 	writel(0xb, seq_offset);
828 	writel(2, seq_offset);
829 	writel(7, seq_offset);
830 	writel(0xd, seq_offset);
831 
832 	msleep(1000);
833 
834 	HostDiag = (u32)readl(hostdiag_offset);
835 
836 	while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
837 		msleep(100);
838 		HostDiag = (u32)readl(hostdiag_offset);
839 		printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
840 					retry, HostDiag);
841 
842 		if (retry++ >= 100)
843 			return 1;
844 
845 	}
846 
847 	printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
848 
849 	writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
850 
851 	ssleep(10);
852 
853 	HostDiag = (u32)readl(hostdiag_offset);
854 	while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
855 		msleep(100);
856 		HostDiag = (u32)readl(hostdiag_offset);
857 		printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
858 				retry, HostDiag);
859 
860 		if (retry++ >= 1000)
861 			return 1;
862 
863 	}
864 	return 0;
865 }
866 
867 /**
868  * megasas_check_reset_gen2 -	For controller reset check
869  * @regs:				MFI register set
870  */
871 static int
872 megasas_check_reset_gen2(struct megasas_instance *instance,
873 		struct megasas_register_set __iomem *regs)
874 {
875 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
876 		return 1;
877 	}
878 
879 	return 0;
880 }
881 
882 static struct megasas_instance_template megasas_instance_template_gen2 = {
883 
884 	.fire_cmd = megasas_fire_cmd_gen2,
885 	.enable_intr = megasas_enable_intr_gen2,
886 	.disable_intr = megasas_disable_intr_gen2,
887 	.clear_intr = megasas_clear_intr_gen2,
888 	.read_fw_status_reg = megasas_read_fw_status_reg_gen2,
889 	.adp_reset = megasas_adp_reset_gen2,
890 	.check_reset = megasas_check_reset_gen2,
891 	.service_isr = megasas_isr,
892 	.tasklet = megasas_complete_cmd_dpc,
893 	.init_adapter = megasas_init_adapter_mfi,
894 	.build_and_issue_cmd = megasas_build_and_issue_cmd,
895 	.issue_dcmd = megasas_issue_dcmd,
896 };
897 
898 /**
899 *	This is the end of set of functions & definitions
900 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
901 */
902 
903 /*
904  * Template added for TB (Fusion)
905  */
906 extern struct megasas_instance_template megasas_instance_template_fusion;
907 
908 /**
909  * megasas_issue_polled -	Issues a polling command
910  * @instance:			Adapter soft state
911  * @cmd:			Command packet to be issued
912  *
913  * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
914  */
915 int
916 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
917 {
918 	int seconds;
919 
920 	struct megasas_header *frame_hdr = &cmd->frame->hdr;
921 
922 	frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
923 	frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
924 
925 	/*
926 	 * Issue the frame using inbound queue port
927 	 */
928 	instance->instancet->issue_dcmd(instance, cmd);
929 
930 	/*
931 	 * Wait for cmd_status to change
932 	 */
933 	if (instance->requestorId)
934 		seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
935 	else
936 		seconds = MFI_POLL_TIMEOUT_SECS;
937 	return wait_and_poll(instance, cmd, seconds);
938 }
939 
940 /**
941  * megasas_issue_blocked_cmd -	Synchronous wrapper around regular FW cmds
942  * @instance:			Adapter soft state
943  * @cmd:			Command to be issued
944  * @timeout:			Timeout in seconds
945  *
946  * This function waits on an event for the command to be returned from ISR.
947  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
948  * Used to issue ioctl commands.
949  */
950 int
951 megasas_issue_blocked_cmd(struct megasas_instance *instance,
952 			  struct megasas_cmd *cmd, int timeout)
953 {
954 	int ret = 0;
955 	cmd->cmd_status = ENODATA;
956 
957 	cmd->is_wait_event = 1;
958 	instance->instancet->issue_dcmd(instance, cmd);
959 	if (timeout) {
960 		ret = wait_event_timeout(instance->int_cmd_wait_q,
961 				cmd->cmd_status != ENODATA, timeout * HZ);
962 		if (!ret)
963 			return 1;
964 	} else
965 		wait_event(instance->int_cmd_wait_q,
966 				cmd->cmd_status != ENODATA);
967 
968 	return 0;
969 }
970 
971 /**
972  * megasas_issue_blocked_abort_cmd -	Aborts previously issued cmd
973  * @instance:				Adapter soft state
974  * @cmd_to_abort:			Previously issued cmd to be aborted
975  * @timeout:				Timeout in seconds
976  *
977  * MFI firmware can abort previously issued AEN comamnd (automatic event
978  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
979  * cmd and waits for return status.
980  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
981  */
982 static int
983 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
984 				struct megasas_cmd *cmd_to_abort, int timeout)
985 {
986 	struct megasas_cmd *cmd;
987 	struct megasas_abort_frame *abort_fr;
988 	int ret = 0;
989 
990 	cmd = megasas_get_cmd(instance);
991 
992 	if (!cmd)
993 		return -1;
994 
995 	abort_fr = &cmd->frame->abort;
996 
997 	/*
998 	 * Prepare and issue the abort frame
999 	 */
1000 	abort_fr->cmd = MFI_CMD_ABORT;
1001 	abort_fr->cmd_status = 0xFF;
1002 	abort_fr->flags = cpu_to_le16(0);
1003 	abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1004 	abort_fr->abort_mfi_phys_addr_lo =
1005 		cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1006 	abort_fr->abort_mfi_phys_addr_hi =
1007 		cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1008 
1009 	cmd->sync_cmd = 1;
1010 	cmd->cmd_status = ENODATA;
1011 
1012 	instance->instancet->issue_dcmd(instance, cmd);
1013 
1014 	if (timeout) {
1015 		ret = wait_event_timeout(instance->abort_cmd_wait_q,
1016 				cmd->cmd_status != ENODATA, timeout * HZ);
1017 		if (!ret) {
1018 			dev_err(&instance->pdev->dev, "Command timedout"
1019 				"from %s\n", __func__);
1020 			return 1;
1021 		}
1022 	} else
1023 		wait_event(instance->abort_cmd_wait_q,
1024 				cmd->cmd_status != ENODATA);
1025 
1026 	cmd->sync_cmd = 0;
1027 
1028 	megasas_return_cmd(instance, cmd);
1029 	return 0;
1030 }
1031 
1032 /**
1033  * megasas_make_sgl32 -	Prepares 32-bit SGL
1034  * @instance:		Adapter soft state
1035  * @scp:		SCSI command from the mid-layer
1036  * @mfi_sgl:		SGL to be filled in
1037  *
1038  * If successful, this function returns the number of SG elements. Otherwise,
1039  * it returnes -1.
1040  */
1041 static int
1042 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1043 		   union megasas_sgl *mfi_sgl)
1044 {
1045 	int i;
1046 	int sge_count;
1047 	struct scatterlist *os_sgl;
1048 
1049 	sge_count = scsi_dma_map(scp);
1050 	BUG_ON(sge_count < 0);
1051 
1052 	if (sge_count) {
1053 		scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1054 			mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1055 			mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1056 		}
1057 	}
1058 	return sge_count;
1059 }
1060 
1061 /**
1062  * megasas_make_sgl64 -	Prepares 64-bit SGL
1063  * @instance:		Adapter soft state
1064  * @scp:		SCSI command from the mid-layer
1065  * @mfi_sgl:		SGL to be filled in
1066  *
1067  * If successful, this function returns the number of SG elements. Otherwise,
1068  * it returnes -1.
1069  */
1070 static int
1071 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1072 		   union megasas_sgl *mfi_sgl)
1073 {
1074 	int i;
1075 	int sge_count;
1076 	struct scatterlist *os_sgl;
1077 
1078 	sge_count = scsi_dma_map(scp);
1079 	BUG_ON(sge_count < 0);
1080 
1081 	if (sge_count) {
1082 		scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1083 			mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1084 			mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1085 		}
1086 	}
1087 	return sge_count;
1088 }
1089 
1090 /**
1091  * megasas_make_sgl_skinny - Prepares IEEE SGL
1092  * @instance:           Adapter soft state
1093  * @scp:                SCSI command from the mid-layer
1094  * @mfi_sgl:            SGL to be filled in
1095  *
1096  * If successful, this function returns the number of SG elements. Otherwise,
1097  * it returnes -1.
1098  */
1099 static int
1100 megasas_make_sgl_skinny(struct megasas_instance *instance,
1101 		struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1102 {
1103 	int i;
1104 	int sge_count;
1105 	struct scatterlist *os_sgl;
1106 
1107 	sge_count = scsi_dma_map(scp);
1108 
1109 	if (sge_count) {
1110 		scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1111 			mfi_sgl->sge_skinny[i].length =
1112 				cpu_to_le32(sg_dma_len(os_sgl));
1113 			mfi_sgl->sge_skinny[i].phys_addr =
1114 				cpu_to_le64(sg_dma_address(os_sgl));
1115 			mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1116 		}
1117 	}
1118 	return sge_count;
1119 }
1120 
1121  /**
1122  * megasas_get_frame_count - Computes the number of frames
1123  * @frame_type		: type of frame- io or pthru frame
1124  * @sge_count		: number of sg elements
1125  *
1126  * Returns the number of frames required for numnber of sge's (sge_count)
1127  */
1128 
1129 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1130 			u8 sge_count, u8 frame_type)
1131 {
1132 	int num_cnt;
1133 	int sge_bytes;
1134 	u32 sge_sz;
1135 	u32 frame_count=0;
1136 
1137 	sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1138 	    sizeof(struct megasas_sge32);
1139 
1140 	if (instance->flag_ieee) {
1141 		sge_sz = sizeof(struct megasas_sge_skinny);
1142 	}
1143 
1144 	/*
1145 	 * Main frame can contain 2 SGEs for 64-bit SGLs and
1146 	 * 3 SGEs for 32-bit SGLs for ldio &
1147 	 * 1 SGEs for 64-bit SGLs and
1148 	 * 2 SGEs for 32-bit SGLs for pthru frame
1149 	 */
1150 	if (unlikely(frame_type == PTHRU_FRAME)) {
1151 		if (instance->flag_ieee == 1) {
1152 			num_cnt = sge_count - 1;
1153 		} else if (IS_DMA64)
1154 			num_cnt = sge_count - 1;
1155 		else
1156 			num_cnt = sge_count - 2;
1157 	} else {
1158 		if (instance->flag_ieee == 1) {
1159 			num_cnt = sge_count - 1;
1160 		} else if (IS_DMA64)
1161 			num_cnt = sge_count - 2;
1162 		else
1163 			num_cnt = sge_count - 3;
1164 	}
1165 
1166 	if(num_cnt>0){
1167 		sge_bytes = sge_sz * num_cnt;
1168 
1169 		frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1170 		    ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1171 	}
1172 	/* Main frame */
1173 	frame_count +=1;
1174 
1175 	if (frame_count > 7)
1176 		frame_count = 8;
1177 	return frame_count;
1178 }
1179 
1180 /**
1181  * megasas_build_dcdb -	Prepares a direct cdb (DCDB) command
1182  * @instance:		Adapter soft state
1183  * @scp:		SCSI command
1184  * @cmd:		Command to be prepared in
1185  *
1186  * This function prepares CDB commands. These are typcially pass-through
1187  * commands to the devices.
1188  */
1189 static int
1190 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1191 		   struct megasas_cmd *cmd)
1192 {
1193 	u32 is_logical;
1194 	u32 device_id;
1195 	u16 flags = 0;
1196 	struct megasas_pthru_frame *pthru;
1197 
1198 	is_logical = MEGASAS_IS_LOGICAL(scp);
1199 	device_id = MEGASAS_DEV_INDEX(instance, scp);
1200 	pthru = (struct megasas_pthru_frame *)cmd->frame;
1201 
1202 	if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1203 		flags = MFI_FRAME_DIR_WRITE;
1204 	else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1205 		flags = MFI_FRAME_DIR_READ;
1206 	else if (scp->sc_data_direction == PCI_DMA_NONE)
1207 		flags = MFI_FRAME_DIR_NONE;
1208 
1209 	if (instance->flag_ieee == 1) {
1210 		flags |= MFI_FRAME_IEEE;
1211 	}
1212 
1213 	/*
1214 	 * Prepare the DCDB frame
1215 	 */
1216 	pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1217 	pthru->cmd_status = 0x0;
1218 	pthru->scsi_status = 0x0;
1219 	pthru->target_id = device_id;
1220 	pthru->lun = scp->device->lun;
1221 	pthru->cdb_len = scp->cmd_len;
1222 	pthru->timeout = 0;
1223 	pthru->pad_0 = 0;
1224 	pthru->flags = cpu_to_le16(flags);
1225 	pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1226 
1227 	memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1228 
1229 	/*
1230 	* If the command is for the tape device, set the
1231 	* pthru timeout to the os layer timeout value.
1232 	*/
1233 	if (scp->device->type == TYPE_TAPE) {
1234 		if ((scp->request->timeout / HZ) > 0xFFFF)
1235 			pthru->timeout = 0xFFFF;
1236 		else
1237 			pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1238 	}
1239 
1240 	/*
1241 	 * Construct SGL
1242 	 */
1243 	if (instance->flag_ieee == 1) {
1244 		pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1245 		pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1246 						      &pthru->sgl);
1247 	} else if (IS_DMA64) {
1248 		pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1249 		pthru->sge_count = megasas_make_sgl64(instance, scp,
1250 						      &pthru->sgl);
1251 	} else
1252 		pthru->sge_count = megasas_make_sgl32(instance, scp,
1253 						      &pthru->sgl);
1254 
1255 	if (pthru->sge_count > instance->max_num_sge) {
1256 		printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1257 			pthru->sge_count);
1258 		return 0;
1259 	}
1260 
1261 	/*
1262 	 * Sense info specific
1263 	 */
1264 	pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1265 	pthru->sense_buf_phys_addr_hi =
1266 		cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1267 	pthru->sense_buf_phys_addr_lo =
1268 		cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1269 
1270 	/*
1271 	 * Compute the total number of frames this command consumes. FW uses
1272 	 * this number to pull sufficient number of frames from host memory.
1273 	 */
1274 	cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1275 							PTHRU_FRAME);
1276 
1277 	return cmd->frame_count;
1278 }
1279 
1280 /**
1281  * megasas_build_ldio -	Prepares IOs to logical devices
1282  * @instance:		Adapter soft state
1283  * @scp:		SCSI command
1284  * @cmd:		Command to be prepared
1285  *
1286  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1287  */
1288 static int
1289 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1290 		   struct megasas_cmd *cmd)
1291 {
1292 	u32 device_id;
1293 	u8 sc = scp->cmnd[0];
1294 	u16 flags = 0;
1295 	struct megasas_io_frame *ldio;
1296 
1297 	device_id = MEGASAS_DEV_INDEX(instance, scp);
1298 	ldio = (struct megasas_io_frame *)cmd->frame;
1299 
1300 	if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1301 		flags = MFI_FRAME_DIR_WRITE;
1302 	else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1303 		flags = MFI_FRAME_DIR_READ;
1304 
1305 	if (instance->flag_ieee == 1) {
1306 		flags |= MFI_FRAME_IEEE;
1307 	}
1308 
1309 	/*
1310 	 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1311 	 */
1312 	ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1313 	ldio->cmd_status = 0x0;
1314 	ldio->scsi_status = 0x0;
1315 	ldio->target_id = device_id;
1316 	ldio->timeout = 0;
1317 	ldio->reserved_0 = 0;
1318 	ldio->pad_0 = 0;
1319 	ldio->flags = cpu_to_le16(flags);
1320 	ldio->start_lba_hi = 0;
1321 	ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1322 
1323 	/*
1324 	 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1325 	 */
1326 	if (scp->cmd_len == 6) {
1327 		ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1328 		ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1329 						 ((u32) scp->cmnd[2] << 8) |
1330 						 (u32) scp->cmnd[3]);
1331 
1332 		ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1333 	}
1334 
1335 	/*
1336 	 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1337 	 */
1338 	else if (scp->cmd_len == 10) {
1339 		ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1340 					      ((u32) scp->cmnd[7] << 8));
1341 		ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1342 						 ((u32) scp->cmnd[3] << 16) |
1343 						 ((u32) scp->cmnd[4] << 8) |
1344 						 (u32) scp->cmnd[5]);
1345 	}
1346 
1347 	/*
1348 	 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1349 	 */
1350 	else if (scp->cmd_len == 12) {
1351 		ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1352 					      ((u32) scp->cmnd[7] << 16) |
1353 					      ((u32) scp->cmnd[8] << 8) |
1354 					      (u32) scp->cmnd[9]);
1355 
1356 		ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1357 						 ((u32) scp->cmnd[3] << 16) |
1358 						 ((u32) scp->cmnd[4] << 8) |
1359 						 (u32) scp->cmnd[5]);
1360 	}
1361 
1362 	/*
1363 	 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1364 	 */
1365 	else if (scp->cmd_len == 16) {
1366 		ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1367 					      ((u32) scp->cmnd[11] << 16) |
1368 					      ((u32) scp->cmnd[12] << 8) |
1369 					      (u32) scp->cmnd[13]);
1370 
1371 		ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1372 						 ((u32) scp->cmnd[7] << 16) |
1373 						 ((u32) scp->cmnd[8] << 8) |
1374 						 (u32) scp->cmnd[9]);
1375 
1376 		ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1377 						 ((u32) scp->cmnd[3] << 16) |
1378 						 ((u32) scp->cmnd[4] << 8) |
1379 						 (u32) scp->cmnd[5]);
1380 
1381 	}
1382 
1383 	/*
1384 	 * Construct SGL
1385 	 */
1386 	if (instance->flag_ieee) {
1387 		ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1388 		ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1389 					      &ldio->sgl);
1390 	} else if (IS_DMA64) {
1391 		ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1392 		ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1393 	} else
1394 		ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1395 
1396 	if (ldio->sge_count > instance->max_num_sge) {
1397 		printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1398 			ldio->sge_count);
1399 		return 0;
1400 	}
1401 
1402 	/*
1403 	 * Sense info specific
1404 	 */
1405 	ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1406 	ldio->sense_buf_phys_addr_hi = 0;
1407 	ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1408 
1409 	/*
1410 	 * Compute the total number of frames this command consumes. FW uses
1411 	 * this number to pull sufficient number of frames from host memory.
1412 	 */
1413 	cmd->frame_count = megasas_get_frame_count(instance,
1414 			ldio->sge_count, IO_FRAME);
1415 
1416 	return cmd->frame_count;
1417 }
1418 
1419 /**
1420  * megasas_cmd_type -		Checks if the cmd is for logical drive/sysPD
1421  *				and whether it's RW or non RW
1422  * @scmd:			SCSI command
1423  *
1424  */
1425 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1426 {
1427 	int ret;
1428 
1429 	switch (cmd->cmnd[0]) {
1430 	case READ_10:
1431 	case WRITE_10:
1432 	case READ_12:
1433 	case WRITE_12:
1434 	case READ_6:
1435 	case WRITE_6:
1436 	case READ_16:
1437 	case WRITE_16:
1438 		ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1439 			READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1440 		break;
1441 	default:
1442 		ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1443 			NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1444 	}
1445 	return ret;
1446 }
1447 
1448  /**
1449  * megasas_dump_pending_frames -	Dumps the frame address of all pending cmds
1450  *                              	in FW
1451  * @instance:				Adapter soft state
1452  */
1453 static inline void
1454 megasas_dump_pending_frames(struct megasas_instance *instance)
1455 {
1456 	struct megasas_cmd *cmd;
1457 	int i,n;
1458 	union megasas_sgl *mfi_sgl;
1459 	struct megasas_io_frame *ldio;
1460 	struct megasas_pthru_frame *pthru;
1461 	u32 sgcount;
1462 	u32 max_cmd = instance->max_fw_cmds;
1463 
1464 	printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1465 	printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1466 	if (IS_DMA64)
1467 		printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1468 	else
1469 		printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1470 
1471 	printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1472 	for (i = 0; i < max_cmd; i++) {
1473 		cmd = instance->cmd_list[i];
1474 		if(!cmd->scmd)
1475 			continue;
1476 		printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1477 		if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1478 			ldio = (struct megasas_io_frame *)cmd->frame;
1479 			mfi_sgl = &ldio->sgl;
1480 			sgcount = ldio->sge_count;
1481 			printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1482 			" lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1483 			instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1484 			le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1485 			le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1486 		}
1487 		else {
1488 			pthru = (struct megasas_pthru_frame *) cmd->frame;
1489 			mfi_sgl = &pthru->sgl;
1490 			sgcount = pthru->sge_count;
1491 			printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1492 			"lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1493 			instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1494 			pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1495 			le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1496 		}
1497 	if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1498 		for (n = 0; n < sgcount; n++){
1499 			if (IS_DMA64)
1500 				printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%llx ",
1501 					le32_to_cpu(mfi_sgl->sge64[n].length),
1502 					le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1503 			else
1504 				printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",
1505 					le32_to_cpu(mfi_sgl->sge32[n].length),
1506 					le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1507 			}
1508 		}
1509 		printk(KERN_ERR "\n");
1510 	} /*for max_cmd*/
1511 	printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1512 	for (i = 0; i < max_cmd; i++) {
1513 
1514 		cmd = instance->cmd_list[i];
1515 
1516 		if(cmd->sync_cmd == 1){
1517 			printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1518 		}
1519 	}
1520 	printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1521 }
1522 
1523 u32
1524 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1525 			    struct scsi_cmnd *scmd)
1526 {
1527 	struct megasas_cmd *cmd;
1528 	u32 frame_count;
1529 
1530 	cmd = megasas_get_cmd(instance);
1531 	if (!cmd)
1532 		return SCSI_MLQUEUE_HOST_BUSY;
1533 
1534 	/*
1535 	 * Logical drive command
1536 	 */
1537 	if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1538 		frame_count = megasas_build_ldio(instance, scmd, cmd);
1539 	else
1540 		frame_count = megasas_build_dcdb(instance, scmd, cmd);
1541 
1542 	if (!frame_count)
1543 		goto out_return_cmd;
1544 
1545 	cmd->scmd = scmd;
1546 	scmd->SCp.ptr = (char *)cmd;
1547 
1548 	/*
1549 	 * Issue the command to the FW
1550 	 */
1551 	atomic_inc(&instance->fw_outstanding);
1552 
1553 	instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1554 				cmd->frame_count-1, instance->reg_set);
1555 
1556 	return 0;
1557 out_return_cmd:
1558 	megasas_return_cmd(instance, cmd);
1559 	return 1;
1560 }
1561 
1562 
1563 /**
1564  * megasas_queue_command -	Queue entry point
1565  * @scmd:			SCSI command to be queued
1566  * @done:			Callback entry point
1567  */
1568 static int
1569 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1570 {
1571 	struct megasas_instance *instance;
1572 	unsigned long flags;
1573 
1574 	instance = (struct megasas_instance *)
1575 	    scmd->device->host->hostdata;
1576 
1577 	if (instance->unload == 1) {
1578 		scmd->result = DID_NO_CONNECT << 16;
1579 		scmd->scsi_done(scmd);
1580 		return 0;
1581 	}
1582 
1583 	if (instance->issuepend_done == 0)
1584 		return SCSI_MLQUEUE_HOST_BUSY;
1585 
1586 	spin_lock_irqsave(&instance->hba_lock, flags);
1587 
1588 	/* Check for an mpio path and adjust behavior */
1589 	if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1590 		if (megasas_check_mpio_paths(instance, scmd) ==
1591 		    (DID_RESET << 16)) {
1592 			spin_unlock_irqrestore(&instance->hba_lock, flags);
1593 			return SCSI_MLQUEUE_HOST_BUSY;
1594 		} else {
1595 			spin_unlock_irqrestore(&instance->hba_lock, flags);
1596 			scmd->result = DID_NO_CONNECT << 16;
1597 			scmd->scsi_done(scmd);
1598 			return 0;
1599 		}
1600 	}
1601 
1602 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1603 		spin_unlock_irqrestore(&instance->hba_lock, flags);
1604 		scmd->result = DID_NO_CONNECT << 16;
1605 		scmd->scsi_done(scmd);
1606 		return 0;
1607 	}
1608 
1609 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1610 		spin_unlock_irqrestore(&instance->hba_lock, flags);
1611 		return SCSI_MLQUEUE_HOST_BUSY;
1612 	}
1613 
1614 	spin_unlock_irqrestore(&instance->hba_lock, flags);
1615 
1616 	scmd->result = 0;
1617 
1618 	if (MEGASAS_IS_LOGICAL(scmd) &&
1619 	    (scmd->device->id >= instance->fw_supported_vd_count ||
1620 		scmd->device->lun)) {
1621 		scmd->result = DID_BAD_TARGET << 16;
1622 		goto out_done;
1623 	}
1624 
1625 	switch (scmd->cmnd[0]) {
1626 	case SYNCHRONIZE_CACHE:
1627 		/*
1628 		 * FW takes care of flush cache on its own
1629 		 * No need to send it down
1630 		 */
1631 		scmd->result = DID_OK << 16;
1632 		goto out_done;
1633 	default:
1634 		break;
1635 	}
1636 
1637 	if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1638 		printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1639 		return SCSI_MLQUEUE_HOST_BUSY;
1640 	}
1641 
1642 	return 0;
1643 
1644  out_done:
1645 	scmd->scsi_done(scmd);
1646 	return 0;
1647 }
1648 
1649 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1650 {
1651 	int i;
1652 
1653 	for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1654 
1655 		if ((megasas_mgmt_info.instance[i]) &&
1656 		    (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1657 			return megasas_mgmt_info.instance[i];
1658 	}
1659 
1660 	return NULL;
1661 }
1662 
1663 static int megasas_slave_configure(struct scsi_device *sdev)
1664 {
1665 	/*
1666 	* The RAID firmware may require extended timeouts.
1667 	*/
1668 	blk_queue_rq_timeout(sdev->request_queue,
1669 		MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1670 
1671 	return 0;
1672 }
1673 
1674 static int megasas_slave_alloc(struct scsi_device *sdev)
1675 {
1676 	u16             pd_index = 0;
1677 	struct megasas_instance *instance ;
1678 	instance = megasas_lookup_instance(sdev->host->host_no);
1679 	if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1680 		/*
1681 		 * Open the OS scan to the SYSTEM PD
1682 		 */
1683 		pd_index =
1684 			(sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1685 			sdev->id;
1686 		if (instance->pd_list[pd_index].driveState ==
1687 					MR_PD_STATE_SYSTEM) {
1688 			return 0;
1689 		}
1690 		return -ENXIO;
1691 	}
1692 	return 0;
1693 }
1694 
1695 /*
1696 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1697 *                                       kill adapter
1698 * @instance:				Adapter soft state
1699 *
1700 */
1701 void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1702 {
1703 	int i;
1704 	struct megasas_cmd *cmd_mfi;
1705 	struct megasas_cmd_fusion *cmd_fusion;
1706 	struct fusion_context *fusion = instance->ctrl_context;
1707 
1708 	/* Find all outstanding ioctls */
1709 	if (fusion) {
1710 		for (i = 0; i < instance->max_fw_cmds; i++) {
1711 			cmd_fusion = fusion->cmd_list[i];
1712 			if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1713 				cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1714 				if (cmd_mfi->sync_cmd &&
1715 					cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1716 					megasas_complete_cmd(instance,
1717 							     cmd_mfi, DID_OK);
1718 			}
1719 		}
1720 	} else {
1721 		for (i = 0; i < instance->max_fw_cmds; i++) {
1722 			cmd_mfi = instance->cmd_list[i];
1723 			if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1724 				MFI_CMD_ABORT)
1725 				megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1726 		}
1727 	}
1728 }
1729 
1730 
1731 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1732 {
1733 	/* Set critical error to block I/O & ioctls in case caller didn't */
1734 	instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1735 	/* Wait 1 second to ensure IO or ioctls in build have posted */
1736 	msleep(1000);
1737 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1738 		(instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1739 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1740 		(instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
1741 		(instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1742 		(instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1743 		writel(MFI_STOP_ADP,
1744 			&instance->reg_set->doorbell);
1745 		/* Flush */
1746 		readl(&instance->reg_set->doorbell);
1747 		if (instance->mpio && instance->requestorId)
1748 			memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1749 	} else {
1750 		writel(MFI_STOP_ADP,
1751 			&instance->reg_set->inbound_doorbell);
1752 	}
1753 	/* Complete outstanding ioctls when adapter is killed */
1754 	megasas_complete_outstanding_ioctls(instance);
1755 }
1756 
1757  /**
1758   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1759   *					restored to max value
1760   * @instance:			Adapter soft state
1761   *
1762   */
1763 void
1764 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1765 {
1766 	unsigned long flags;
1767 
1768 	if (instance->flag & MEGASAS_FW_BUSY
1769 	    && time_after(jiffies, instance->last_time + 5 * HZ)
1770 	    && atomic_read(&instance->fw_outstanding) <
1771 	    instance->throttlequeuedepth + 1) {
1772 
1773 		spin_lock_irqsave(instance->host->host_lock, flags);
1774 		instance->flag &= ~MEGASAS_FW_BUSY;
1775 
1776 		instance->host->can_queue = instance->max_scsi_cmds;
1777 		spin_unlock_irqrestore(instance->host->host_lock, flags);
1778 	}
1779 }
1780 
1781 /**
1782  * megasas_complete_cmd_dpc	 -	Returns FW's controller structure
1783  * @instance_addr:			Address of adapter soft state
1784  *
1785  * Tasklet to complete cmds
1786  */
1787 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1788 {
1789 	u32 producer;
1790 	u32 consumer;
1791 	u32 context;
1792 	struct megasas_cmd *cmd;
1793 	struct megasas_instance *instance =
1794 				(struct megasas_instance *)instance_addr;
1795 	unsigned long flags;
1796 
1797 	/* If we have already declared adapter dead, donot complete cmds */
1798 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1799 		return;
1800 
1801 	spin_lock_irqsave(&instance->completion_lock, flags);
1802 
1803 	producer = le32_to_cpu(*instance->producer);
1804 	consumer = le32_to_cpu(*instance->consumer);
1805 
1806 	while (consumer != producer) {
1807 		context = le32_to_cpu(instance->reply_queue[consumer]);
1808 		if (context >= instance->max_fw_cmds) {
1809 			printk(KERN_ERR "Unexpected context value %x\n",
1810 				context);
1811 			BUG();
1812 		}
1813 
1814 		cmd = instance->cmd_list[context];
1815 
1816 		megasas_complete_cmd(instance, cmd, DID_OK);
1817 
1818 		consumer++;
1819 		if (consumer == (instance->max_fw_cmds + 1)) {
1820 			consumer = 0;
1821 		}
1822 	}
1823 
1824 	*instance->consumer = cpu_to_le32(producer);
1825 
1826 	spin_unlock_irqrestore(&instance->completion_lock, flags);
1827 
1828 	/*
1829 	 * Check if we can restore can_queue
1830 	 */
1831 	megasas_check_and_restore_queue_depth(instance);
1832 }
1833 
1834 /**
1835  * megasas_start_timer - Initializes a timer object
1836  * @instance:		Adapter soft state
1837  * @timer:		timer object to be initialized
1838  * @fn:			timer function
1839  * @interval:		time interval between timer function call
1840  *
1841  */
1842 void megasas_start_timer(struct megasas_instance *instance,
1843 			struct timer_list *timer,
1844 			void *fn, unsigned long interval)
1845 {
1846 	init_timer(timer);
1847 	timer->expires = jiffies + interval;
1848 	timer->data = (unsigned long)instance;
1849 	timer->function = fn;
1850 	add_timer(timer);
1851 }
1852 
1853 static void
1854 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1855 
1856 static void
1857 process_fw_state_change_wq(struct work_struct *work);
1858 
1859 void megasas_do_ocr(struct megasas_instance *instance)
1860 {
1861 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1862 	(instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1863 	(instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1864 		*instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1865 	}
1866 	instance->instancet->disable_intr(instance);
1867 	instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
1868 	instance->issuepend_done = 0;
1869 
1870 	atomic_set(&instance->fw_outstanding, 0);
1871 	megasas_internal_reset_defer_cmds(instance);
1872 	process_fw_state_change_wq(&instance->work_init);
1873 }
1874 
1875 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1876 					    int initial)
1877 {
1878 	struct megasas_cmd *cmd;
1879 	struct megasas_dcmd_frame *dcmd;
1880 	struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1881 	dma_addr_t new_affiliation_111_h;
1882 	int ld, retval = 0;
1883 	u8 thisVf;
1884 
1885 	cmd = megasas_get_cmd(instance);
1886 
1887 	if (!cmd) {
1888 		printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation_111:"
1889 		       "Failed to get cmd for scsi%d.\n",
1890 			instance->host->host_no);
1891 		return -ENOMEM;
1892 	}
1893 
1894 	dcmd = &cmd->frame->dcmd;
1895 
1896 	if (!instance->vf_affiliation_111) {
1897 		printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
1898 		       "affiliation for scsi%d.\n", instance->host->host_no);
1899 		megasas_return_cmd(instance, cmd);
1900 		return -ENOMEM;
1901 	}
1902 
1903 	if (initial)
1904 			memset(instance->vf_affiliation_111, 0,
1905 			       sizeof(struct MR_LD_VF_AFFILIATION_111));
1906 	else {
1907 		new_affiliation_111 =
1908 			pci_alloc_consistent(instance->pdev,
1909 					     sizeof(struct MR_LD_VF_AFFILIATION_111),
1910 					     &new_affiliation_111_h);
1911 		if (!new_affiliation_111) {
1912 			printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
1913 			       "memory for new affiliation for scsi%d.\n",
1914 			       instance->host->host_no);
1915 			megasas_return_cmd(instance, cmd);
1916 			return -ENOMEM;
1917 		}
1918 		memset(new_affiliation_111, 0,
1919 		       sizeof(struct MR_LD_VF_AFFILIATION_111));
1920 	}
1921 
1922 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1923 
1924 	dcmd->cmd = MFI_CMD_DCMD;
1925 	dcmd->cmd_status = 0xFF;
1926 	dcmd->sge_count = 1;
1927 	dcmd->flags = MFI_FRAME_DIR_BOTH;
1928 	dcmd->timeout = 0;
1929 	dcmd->pad_0 = 0;
1930 	dcmd->data_xfer_len = sizeof(struct MR_LD_VF_AFFILIATION_111);
1931 	dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111;
1932 
1933 	if (initial)
1934 		dcmd->sgl.sge32[0].phys_addr =
1935 			instance->vf_affiliation_111_h;
1936 	else
1937 		dcmd->sgl.sge32[0].phys_addr = new_affiliation_111_h;
1938 
1939 	dcmd->sgl.sge32[0].length =
1940 		sizeof(struct MR_LD_VF_AFFILIATION_111);
1941 
1942 	printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
1943 	       "scsi%d\n", instance->host->host_no);
1944 
1945 	megasas_issue_blocked_cmd(instance, cmd, 0);
1946 
1947 	if (dcmd->cmd_status) {
1948 		printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
1949 		       " failed with status 0x%x for scsi%d.\n",
1950 		       dcmd->cmd_status, instance->host->host_no);
1951 		retval = 1; /* Do a scan if we couldn't get affiliation */
1952 		goto out;
1953 	}
1954 
1955 	if (!initial) {
1956 		thisVf = new_affiliation_111->thisVf;
1957 		for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1958 			if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1959 			    new_affiliation_111->map[ld].policy[thisVf]) {
1960 				printk(KERN_WARNING "megasas: SR-IOV: "
1961 				       "Got new LD/VF affiliation "
1962 				       "for scsi%d.\n",
1963 				       instance->host->host_no);
1964 				memcpy(instance->vf_affiliation_111,
1965 				       new_affiliation_111,
1966 				       sizeof(struct MR_LD_VF_AFFILIATION_111));
1967 				retval = 1;
1968 				goto out;
1969 			}
1970 	}
1971 out:
1972 	if (new_affiliation_111) {
1973 		pci_free_consistent(instance->pdev,
1974 				    sizeof(struct MR_LD_VF_AFFILIATION_111),
1975 				    new_affiliation_111,
1976 				    new_affiliation_111_h);
1977 	}
1978 
1979 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
1980 		megasas_return_mfi_mpt_pthr(instance, cmd,
1981 			cmd->mpt_pthr_cmd_blocked);
1982 	else
1983 		megasas_return_cmd(instance, cmd);
1984 
1985 	return retval;
1986 }
1987 
1988 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1989 					    int initial)
1990 {
1991 	struct megasas_cmd *cmd;
1992 	struct megasas_dcmd_frame *dcmd;
1993 	struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1994 	struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1995 	dma_addr_t new_affiliation_h;
1996 	int i, j, retval = 0, found = 0, doscan = 0;
1997 	u8 thisVf;
1998 
1999 	cmd = megasas_get_cmd(instance);
2000 
2001 	if (!cmd) {
2002 		printk(KERN_DEBUG "megasas: megasas_get_ld_vf_affiliation12: "
2003 		       "Failed to get cmd for scsi%d.\n",
2004 		       instance->host->host_no);
2005 		return -ENOMEM;
2006 	}
2007 
2008 	dcmd = &cmd->frame->dcmd;
2009 
2010 	if (!instance->vf_affiliation) {
2011 		printk(KERN_WARNING "megasas: SR-IOV: Couldn't get LD/VF "
2012 		       "affiliation for scsi%d.\n", instance->host->host_no);
2013 		megasas_return_cmd(instance, cmd);
2014 		return -ENOMEM;
2015 	}
2016 
2017 	if (initial)
2018 		memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2019 		       sizeof(struct MR_LD_VF_AFFILIATION));
2020 	else {
2021 		new_affiliation =
2022 			pci_alloc_consistent(instance->pdev,
2023 					     (MAX_LOGICAL_DRIVES + 1) *
2024 					     sizeof(struct MR_LD_VF_AFFILIATION),
2025 					     &new_affiliation_h);
2026 		if (!new_affiliation) {
2027 			printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate "
2028 			       "memory for new affiliation for scsi%d.\n",
2029 			       instance->host->host_no);
2030 			megasas_return_cmd(instance, cmd);
2031 			return -ENOMEM;
2032 		}
2033 		memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2034 		       sizeof(struct MR_LD_VF_AFFILIATION));
2035 	}
2036 
2037 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2038 
2039 	dcmd->cmd = MFI_CMD_DCMD;
2040 	dcmd->cmd_status = 0xFF;
2041 	dcmd->sge_count = 1;
2042 	dcmd->flags = MFI_FRAME_DIR_BOTH;
2043 	dcmd->timeout = 0;
2044 	dcmd->pad_0 = 0;
2045 	dcmd->data_xfer_len = (MAX_LOGICAL_DRIVES + 1) *
2046 		sizeof(struct MR_LD_VF_AFFILIATION);
2047 	dcmd->opcode = MR_DCMD_LD_VF_MAP_GET_ALL_LDS;
2048 
2049 	if (initial)
2050 		dcmd->sgl.sge32[0].phys_addr = instance->vf_affiliation_h;
2051 	else
2052 		dcmd->sgl.sge32[0].phys_addr = new_affiliation_h;
2053 
2054 	dcmd->sgl.sge32[0].length = (MAX_LOGICAL_DRIVES + 1) *
2055 		sizeof(struct MR_LD_VF_AFFILIATION);
2056 
2057 	printk(KERN_WARNING "megasas: SR-IOV: Getting LD/VF affiliation for "
2058 	       "scsi%d\n", instance->host->host_no);
2059 
2060 	megasas_issue_blocked_cmd(instance, cmd, 0);
2061 
2062 	if (dcmd->cmd_status) {
2063 		printk(KERN_WARNING "megasas: SR-IOV: LD/VF affiliation DCMD"
2064 		       " failed with status 0x%x for scsi%d.\n",
2065 		       dcmd->cmd_status, instance->host->host_no);
2066 		retval = 1; /* Do a scan if we couldn't get affiliation */
2067 		goto out;
2068 	}
2069 
2070 	if (!initial) {
2071 		if (!new_affiliation->ldCount) {
2072 			printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2073 			       "affiliation for passive path for scsi%d.\n",
2074 			       instance->host->host_no);
2075 			retval = 1;
2076 			goto out;
2077 		}
2078 		newmap = new_affiliation->map;
2079 		savedmap = instance->vf_affiliation->map;
2080 		thisVf = new_affiliation->thisVf;
2081 		for (i = 0 ; i < new_affiliation->ldCount; i++) {
2082 			found = 0;
2083 			for (j = 0; j < instance->vf_affiliation->ldCount;
2084 			     j++) {
2085 				if (newmap->ref.targetId ==
2086 				    savedmap->ref.targetId) {
2087 					found = 1;
2088 					if (newmap->policy[thisVf] !=
2089 					    savedmap->policy[thisVf]) {
2090 						doscan = 1;
2091 						goto out;
2092 					}
2093 				}
2094 				savedmap = (struct MR_LD_VF_MAP *)
2095 					((unsigned char *)savedmap +
2096 					 savedmap->size);
2097 			}
2098 			if (!found && newmap->policy[thisVf] !=
2099 			    MR_LD_ACCESS_HIDDEN) {
2100 				doscan = 1;
2101 				goto out;
2102 			}
2103 			newmap = (struct MR_LD_VF_MAP *)
2104 				((unsigned char *)newmap + newmap->size);
2105 		}
2106 
2107 		newmap = new_affiliation->map;
2108 		savedmap = instance->vf_affiliation->map;
2109 
2110 		for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2111 			found = 0;
2112 			for (j = 0 ; j < new_affiliation->ldCount; j++) {
2113 				if (savedmap->ref.targetId ==
2114 				    newmap->ref.targetId) {
2115 					found = 1;
2116 					if (savedmap->policy[thisVf] !=
2117 					    newmap->policy[thisVf]) {
2118 						doscan = 1;
2119 						goto out;
2120 					}
2121 				}
2122 				newmap = (struct MR_LD_VF_MAP *)
2123 					((unsigned char *)newmap +
2124 					 newmap->size);
2125 			}
2126 			if (!found && savedmap->policy[thisVf] !=
2127 			    MR_LD_ACCESS_HIDDEN) {
2128 				doscan = 1;
2129 				goto out;
2130 			}
2131 			savedmap = (struct MR_LD_VF_MAP *)
2132 				((unsigned char *)savedmap +
2133 				 savedmap->size);
2134 		}
2135 	}
2136 out:
2137 	if (doscan) {
2138 		printk(KERN_WARNING "megasas: SR-IOV: Got new LD/VF "
2139 		       "affiliation for scsi%d.\n", instance->host->host_no);
2140 		memcpy(instance->vf_affiliation, new_affiliation,
2141 		       new_affiliation->size);
2142 		retval = 1;
2143 	}
2144 
2145 	if (new_affiliation)
2146 		pci_free_consistent(instance->pdev,
2147 				    (MAX_LOGICAL_DRIVES + 1) *
2148 				    sizeof(struct MR_LD_VF_AFFILIATION),
2149 				    new_affiliation, new_affiliation_h);
2150 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2151 		megasas_return_mfi_mpt_pthr(instance, cmd,
2152 			cmd->mpt_pthr_cmd_blocked);
2153 	else
2154 		megasas_return_cmd(instance, cmd);
2155 
2156 	return retval;
2157 }
2158 
2159 /* This function will get the current SR-IOV LD/VF affiliation */
2160 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2161 	int initial)
2162 {
2163 	int retval;
2164 
2165 	if (instance->PlasmaFW111)
2166 		retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2167 	else
2168 		retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2169 	return retval;
2170 }
2171 
2172 /* This function will tell FW to start the SR-IOV heartbeat */
2173 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2174 					 int initial)
2175 {
2176 	struct megasas_cmd *cmd;
2177 	struct megasas_dcmd_frame *dcmd;
2178 	int retval = 0;
2179 
2180 	cmd = megasas_get_cmd(instance);
2181 
2182 	if (!cmd) {
2183 		printk(KERN_DEBUG "megasas: megasas_sriov_start_heartbeat: "
2184 		       "Failed to get cmd for scsi%d.\n",
2185 		       instance->host->host_no);
2186 		return -ENOMEM;
2187 	}
2188 
2189 	dcmd = &cmd->frame->dcmd;
2190 
2191 	if (initial) {
2192 		instance->hb_host_mem =
2193 			pci_zalloc_consistent(instance->pdev,
2194 					      sizeof(struct MR_CTRL_HB_HOST_MEM),
2195 					      &instance->hb_host_mem_h);
2196 		if (!instance->hb_host_mem) {
2197 			printk(KERN_DEBUG "megasas: SR-IOV: Couldn't allocate"
2198 			       " memory for heartbeat host memory for "
2199 			       "scsi%d.\n", instance->host->host_no);
2200 			retval = -ENOMEM;
2201 			goto out;
2202 		}
2203 	}
2204 
2205 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2206 
2207 	dcmd->mbox.s[0] = sizeof(struct MR_CTRL_HB_HOST_MEM);
2208 	dcmd->cmd = MFI_CMD_DCMD;
2209 	dcmd->cmd_status = 0xFF;
2210 	dcmd->sge_count = 1;
2211 	dcmd->flags = MFI_FRAME_DIR_BOTH;
2212 	dcmd->timeout = 0;
2213 	dcmd->pad_0 = 0;
2214 	dcmd->data_xfer_len = sizeof(struct MR_CTRL_HB_HOST_MEM);
2215 	dcmd->opcode = MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC;
2216 	dcmd->sgl.sge32[0].phys_addr = instance->hb_host_mem_h;
2217 	dcmd->sgl.sge32[0].length = sizeof(struct MR_CTRL_HB_HOST_MEM);
2218 
2219 	printk(KERN_WARNING "megasas: SR-IOV: Starting heartbeat for scsi%d\n",
2220 	       instance->host->host_no);
2221 
2222 	if (!megasas_issue_polled(instance, cmd)) {
2223 		retval = 0;
2224 	} else {
2225 		printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2226 		       "_MEM_ALLOC DCMD timed out for scsi%d\n",
2227 		       instance->host->host_no);
2228 		retval = 1;
2229 		goto out;
2230 	}
2231 
2232 
2233 	if (dcmd->cmd_status) {
2234 		printk(KERN_WARNING "megasas: SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2235 		       "_MEM_ALLOC DCMD failed with status 0x%x for scsi%d\n",
2236 		       dcmd->cmd_status,
2237 		       instance->host->host_no);
2238 		retval = 1;
2239 		goto out;
2240 	}
2241 
2242 out:
2243 	megasas_return_cmd(instance, cmd);
2244 
2245 	return retval;
2246 }
2247 
2248 /* Handler for SR-IOV heartbeat */
2249 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2250 {
2251 	struct megasas_instance *instance =
2252 		(struct megasas_instance *)instance_addr;
2253 
2254 	if (instance->hb_host_mem->HB.fwCounter !=
2255 	    instance->hb_host_mem->HB.driverCounter) {
2256 		instance->hb_host_mem->HB.driverCounter =
2257 			instance->hb_host_mem->HB.fwCounter;
2258 		mod_timer(&instance->sriov_heartbeat_timer,
2259 			  jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2260 	} else {
2261 		printk(KERN_WARNING "megasas: SR-IOV: Heartbeat never "
2262 		       "completed for scsi%d\n", instance->host->host_no);
2263 		schedule_work(&instance->work_init);
2264 	}
2265 }
2266 
2267 /**
2268  * megasas_wait_for_outstanding -	Wait for all outstanding cmds
2269  * @instance:				Adapter soft state
2270  *
2271  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2272  * complete all its outstanding commands. Returns error if one or more IOs
2273  * are pending after this time period. It also marks the controller dead.
2274  */
2275 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2276 {
2277 	int i;
2278 	u32 reset_index;
2279 	u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2280 	u8 adprecovery;
2281 	unsigned long flags;
2282 	struct list_head clist_local;
2283 	struct megasas_cmd *reset_cmd;
2284 	u32 fw_state;
2285 	u8 kill_adapter_flag;
2286 
2287 	spin_lock_irqsave(&instance->hba_lock, flags);
2288 	adprecovery = instance->adprecovery;
2289 	spin_unlock_irqrestore(&instance->hba_lock, flags);
2290 
2291 	if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2292 
2293 		INIT_LIST_HEAD(&clist_local);
2294 		spin_lock_irqsave(&instance->hba_lock, flags);
2295 		list_splice_init(&instance->internal_reset_pending_q,
2296 				&clist_local);
2297 		spin_unlock_irqrestore(&instance->hba_lock, flags);
2298 
2299 		printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
2300 		for (i = 0; i < wait_time; i++) {
2301 			msleep(1000);
2302 			spin_lock_irqsave(&instance->hba_lock, flags);
2303 			adprecovery = instance->adprecovery;
2304 			spin_unlock_irqrestore(&instance->hba_lock, flags);
2305 			if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2306 				break;
2307 		}
2308 
2309 		if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2310 			printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
2311 			spin_lock_irqsave(&instance->hba_lock, flags);
2312 			instance->adprecovery	= MEGASAS_HW_CRITICAL_ERROR;
2313 			spin_unlock_irqrestore(&instance->hba_lock, flags);
2314 			return FAILED;
2315 		}
2316 
2317 		reset_index	= 0;
2318 		while (!list_empty(&clist_local)) {
2319 			reset_cmd	= list_entry((&clist_local)->next,
2320 						struct megasas_cmd, list);
2321 			list_del_init(&reset_cmd->list);
2322 			if (reset_cmd->scmd) {
2323 				reset_cmd->scmd->result = DID_RESET << 16;
2324 				printk(KERN_NOTICE "%d:%p reset [%02x]\n",
2325 					reset_index, reset_cmd,
2326 					reset_cmd->scmd->cmnd[0]);
2327 
2328 				reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2329 				megasas_return_cmd(instance, reset_cmd);
2330 			} else if (reset_cmd->sync_cmd) {
2331 				printk(KERN_NOTICE "megasas:%p synch cmds"
2332 						"reset queue\n",
2333 						reset_cmd);
2334 
2335 				reset_cmd->cmd_status = ENODATA;
2336 				instance->instancet->fire_cmd(instance,
2337 						reset_cmd->frame_phys_addr,
2338 						0, instance->reg_set);
2339 			} else {
2340 				printk(KERN_NOTICE "megasas: %p unexpected"
2341 					"cmds lst\n",
2342 					reset_cmd);
2343 			}
2344 			reset_index++;
2345 		}
2346 
2347 		return SUCCESS;
2348 	}
2349 
2350 	for (i = 0; i < resetwaittime; i++) {
2351 
2352 		int outstanding = atomic_read(&instance->fw_outstanding);
2353 
2354 		if (!outstanding)
2355 			break;
2356 
2357 		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2358 			printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2359 			       "commands to complete\n",i,outstanding);
2360 			/*
2361 			 * Call cmd completion routine. Cmd to be
2362 			 * be completed directly without depending on isr.
2363 			 */
2364 			megasas_complete_cmd_dpc((unsigned long)instance);
2365 		}
2366 
2367 		msleep(1000);
2368 	}
2369 
2370 	i = 0;
2371 	kill_adapter_flag = 0;
2372 	do {
2373 		fw_state = instance->instancet->read_fw_status_reg(
2374 					instance->reg_set) & MFI_STATE_MASK;
2375 		if ((fw_state == MFI_STATE_FAULT) &&
2376 			(instance->disableOnlineCtrlReset == 0)) {
2377 			if (i == 3) {
2378 				kill_adapter_flag = 2;
2379 				break;
2380 			}
2381 			megasas_do_ocr(instance);
2382 			kill_adapter_flag = 1;
2383 
2384 			/* wait for 1 secs to let FW finish the pending cmds */
2385 			msleep(1000);
2386 		}
2387 		i++;
2388 	} while (i <= 3);
2389 
2390 	if (atomic_read(&instance->fw_outstanding) &&
2391 					!kill_adapter_flag) {
2392 		if (instance->disableOnlineCtrlReset == 0) {
2393 
2394 			megasas_do_ocr(instance);
2395 
2396 			/* wait for 5 secs to let FW finish the pending cmds */
2397 			for (i = 0; i < wait_time; i++) {
2398 				int outstanding =
2399 					atomic_read(&instance->fw_outstanding);
2400 				if (!outstanding)
2401 					return SUCCESS;
2402 				msleep(1000);
2403 			}
2404 		}
2405 	}
2406 
2407 	if (atomic_read(&instance->fw_outstanding) ||
2408 					(kill_adapter_flag == 2)) {
2409 		printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
2410 		/*
2411 		* Send signal to FW to stop processing any pending cmds.
2412 		* The controller will be taken offline by the OS now.
2413 		*/
2414 		if ((instance->pdev->device ==
2415 			PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2416 			(instance->pdev->device ==
2417 			PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2418 			writel(MFI_STOP_ADP,
2419 				&instance->reg_set->doorbell);
2420 		} else {
2421 			writel(MFI_STOP_ADP,
2422 				&instance->reg_set->inbound_doorbell);
2423 		}
2424 		megasas_dump_pending_frames(instance);
2425 		spin_lock_irqsave(&instance->hba_lock, flags);
2426 		instance->adprecovery	= MEGASAS_HW_CRITICAL_ERROR;
2427 		spin_unlock_irqrestore(&instance->hba_lock, flags);
2428 		return FAILED;
2429 	}
2430 
2431 	printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
2432 
2433 	return SUCCESS;
2434 }
2435 
2436 /**
2437  * megasas_generic_reset -	Generic reset routine
2438  * @scmd:			Mid-layer SCSI command
2439  *
2440  * This routine implements a generic reset handler for device, bus and host
2441  * reset requests. Device, bus and host specific reset handlers can use this
2442  * function after they do their specific tasks.
2443  */
2444 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2445 {
2446 	int ret_val;
2447 	struct megasas_instance *instance;
2448 
2449 	instance = (struct megasas_instance *)scmd->device->host->hostdata;
2450 
2451 	scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2452 		 scmd->cmnd[0], scmd->retries);
2453 
2454 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2455 		printk(KERN_ERR "megasas: cannot recover from previous reset "
2456 		       "failures\n");
2457 		return FAILED;
2458 	}
2459 
2460 	ret_val = megasas_wait_for_outstanding(instance);
2461 	if (ret_val == SUCCESS)
2462 		printk(KERN_NOTICE "megasas: reset successful \n");
2463 	else
2464 		printk(KERN_ERR "megasas: failed to do reset\n");
2465 
2466 	return ret_val;
2467 }
2468 
2469 /**
2470  * megasas_reset_timer - quiesce the adapter if required
2471  * @scmd:		scsi cmnd
2472  *
2473  * Sets the FW busy flag and reduces the host->can_queue if the
2474  * cmd has not been completed within the timeout period.
2475  */
2476 static enum
2477 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2478 {
2479 	struct megasas_instance *instance;
2480 	unsigned long flags;
2481 
2482 	if (time_after(jiffies, scmd->jiffies_at_alloc +
2483 				(MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2484 		return BLK_EH_NOT_HANDLED;
2485 	}
2486 
2487 	instance = (struct megasas_instance *)scmd->device->host->hostdata;
2488 	if (!(instance->flag & MEGASAS_FW_BUSY)) {
2489 		/* FW is busy, throttle IO */
2490 		spin_lock_irqsave(instance->host->host_lock, flags);
2491 
2492 		instance->host->can_queue = instance->throttlequeuedepth;
2493 		instance->last_time = jiffies;
2494 		instance->flag |= MEGASAS_FW_BUSY;
2495 
2496 		spin_unlock_irqrestore(instance->host->host_lock, flags);
2497 	}
2498 	return BLK_EH_RESET_TIMER;
2499 }
2500 
2501 /**
2502  * megasas_reset_device -	Device reset handler entry point
2503  */
2504 static int megasas_reset_device(struct scsi_cmnd *scmd)
2505 {
2506 	int ret;
2507 
2508 	/*
2509 	 * First wait for all commands to complete
2510 	 */
2511 	ret = megasas_generic_reset(scmd);
2512 
2513 	return ret;
2514 }
2515 
2516 /**
2517  * megasas_reset_bus_host -	Bus & host reset handler entry point
2518  */
2519 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2520 {
2521 	int ret;
2522 	struct megasas_instance *instance;
2523 	instance = (struct megasas_instance *)scmd->device->host->hostdata;
2524 
2525 	/*
2526 	 * First wait for all commands to complete
2527 	 */
2528 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
2529 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
2530 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2531 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2532 		ret = megasas_reset_fusion(scmd->device->host, 1);
2533 	else
2534 		ret = megasas_generic_reset(scmd);
2535 
2536 	return ret;
2537 }
2538 
2539 /**
2540  * megasas_bios_param - Returns disk geometry for a disk
2541  * @sdev: 		device handle
2542  * @bdev:		block device
2543  * @capacity:		drive capacity
2544  * @geom:		geometry parameters
2545  */
2546 static int
2547 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2548 		 sector_t capacity, int geom[])
2549 {
2550 	int heads;
2551 	int sectors;
2552 	sector_t cylinders;
2553 	unsigned long tmp;
2554 	/* Default heads (64) & sectors (32) */
2555 	heads = 64;
2556 	sectors = 32;
2557 
2558 	tmp = heads * sectors;
2559 	cylinders = capacity;
2560 
2561 	sector_div(cylinders, tmp);
2562 
2563 	/*
2564 	 * Handle extended translation size for logical drives > 1Gb
2565 	 */
2566 
2567 	if (capacity >= 0x200000) {
2568 		heads = 255;
2569 		sectors = 63;
2570 		tmp = heads*sectors;
2571 		cylinders = capacity;
2572 		sector_div(cylinders, tmp);
2573 	}
2574 
2575 	geom[0] = heads;
2576 	geom[1] = sectors;
2577 	geom[2] = cylinders;
2578 
2579 	return 0;
2580 }
2581 
2582 static void megasas_aen_polling(struct work_struct *work);
2583 
2584 /**
2585  * megasas_service_aen -	Processes an event notification
2586  * @instance:			Adapter soft state
2587  * @cmd:			AEN command completed by the ISR
2588  *
2589  * For AEN, driver sends a command down to FW that is held by the FW till an
2590  * event occurs. When an event of interest occurs, FW completes the command
2591  * that it was previously holding.
2592  *
2593  * This routines sends SIGIO signal to processes that have registered with the
2594  * driver for AEN.
2595  */
2596 static void
2597 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2598 {
2599 	unsigned long flags;
2600 	/*
2601 	 * Don't signal app if it is just an aborted previously registered aen
2602 	 */
2603 	if ((!cmd->abort_aen) && (instance->unload == 0)) {
2604 		spin_lock_irqsave(&poll_aen_lock, flags);
2605 		megasas_poll_wait_aen = 1;
2606 		spin_unlock_irqrestore(&poll_aen_lock, flags);
2607 		wake_up(&megasas_poll_wait);
2608 		kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2609 	}
2610 	else
2611 		cmd->abort_aen = 0;
2612 
2613 	instance->aen_cmd = NULL;
2614 
2615 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
2616 		megasas_return_mfi_mpt_pthr(instance, cmd,
2617 			cmd->mpt_pthr_cmd_blocked);
2618 	else
2619 		megasas_return_cmd(instance, cmd);
2620 
2621 	if ((instance->unload == 0) &&
2622 		((instance->issuepend_done == 1))) {
2623 		struct megasas_aen_event *ev;
2624 		ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2625 		if (!ev) {
2626 			printk(KERN_ERR "megasas_service_aen: out of memory\n");
2627 		} else {
2628 			ev->instance = instance;
2629 			instance->ev = ev;
2630 			INIT_DELAYED_WORK(&ev->hotplug_work,
2631 					  megasas_aen_polling);
2632 			schedule_delayed_work(&ev->hotplug_work, 0);
2633 		}
2634 	}
2635 }
2636 
2637 static ssize_t
2638 megasas_fw_crash_buffer_store(struct device *cdev,
2639 	struct device_attribute *attr, const char *buf, size_t count)
2640 {
2641 	struct Scsi_Host *shost = class_to_shost(cdev);
2642 	struct megasas_instance *instance =
2643 		(struct megasas_instance *) shost->hostdata;
2644 	int val = 0;
2645 	unsigned long flags;
2646 
2647 	if (kstrtoint(buf, 0, &val) != 0)
2648 		return -EINVAL;
2649 
2650 	spin_lock_irqsave(&instance->crashdump_lock, flags);
2651 	instance->fw_crash_buffer_offset = val;
2652 	spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2653 	return strlen(buf);
2654 }
2655 
2656 static ssize_t
2657 megasas_fw_crash_buffer_show(struct device *cdev,
2658 	struct device_attribute *attr, char *buf)
2659 {
2660 	struct Scsi_Host *shost = class_to_shost(cdev);
2661 	struct megasas_instance *instance =
2662 		(struct megasas_instance *) shost->hostdata;
2663 	u32 size;
2664 	unsigned long buff_addr;
2665 	unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2666 	unsigned long src_addr;
2667 	unsigned long flags;
2668 	u32 buff_offset;
2669 
2670 	spin_lock_irqsave(&instance->crashdump_lock, flags);
2671 	buff_offset = instance->fw_crash_buffer_offset;
2672 	if (!instance->crash_dump_buf &&
2673 		!((instance->fw_crash_state == AVAILABLE) ||
2674 		(instance->fw_crash_state == COPYING))) {
2675 		dev_err(&instance->pdev->dev,
2676 			"Firmware crash dump is not available\n");
2677 		spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2678 		return -EINVAL;
2679 	}
2680 
2681 	buff_addr = (unsigned long) buf;
2682 
2683 	if (buff_offset >
2684 		(instance->fw_crash_buffer_size * dmachunk)) {
2685 		dev_err(&instance->pdev->dev,
2686 			"Firmware crash dump offset is out of range\n");
2687 		spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2688 		return 0;
2689 	}
2690 
2691 	size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2692 	size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2693 
2694 	src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2695 		(buff_offset % dmachunk);
2696 	memcpy(buf, (void *)src_addr,  size);
2697 	spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2698 
2699 	return size;
2700 }
2701 
2702 static ssize_t
2703 megasas_fw_crash_buffer_size_show(struct device *cdev,
2704 	struct device_attribute *attr, char *buf)
2705 {
2706 	struct Scsi_Host *shost = class_to_shost(cdev);
2707 	struct megasas_instance *instance =
2708 		(struct megasas_instance *) shost->hostdata;
2709 
2710 	return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2711 		((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2712 }
2713 
2714 static ssize_t
2715 megasas_fw_crash_state_store(struct device *cdev,
2716 	struct device_attribute *attr, const char *buf, size_t count)
2717 {
2718 	struct Scsi_Host *shost = class_to_shost(cdev);
2719 	struct megasas_instance *instance =
2720 		(struct megasas_instance *) shost->hostdata;
2721 	int val = 0;
2722 	unsigned long flags;
2723 
2724 	if (kstrtoint(buf, 0, &val) != 0)
2725 		return -EINVAL;
2726 
2727 	if ((val <= AVAILABLE || val > COPY_ERROR)) {
2728 		dev_err(&instance->pdev->dev, "application updates invalid "
2729 			"firmware crash state\n");
2730 		return -EINVAL;
2731 	}
2732 
2733 	instance->fw_crash_state = val;
2734 
2735 	if ((val == COPIED) || (val == COPY_ERROR)) {
2736 		spin_lock_irqsave(&instance->crashdump_lock, flags);
2737 		megasas_free_host_crash_buffer(instance);
2738 		spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2739 		if (val == COPY_ERROR)
2740 			dev_info(&instance->pdev->dev, "application failed to "
2741 				"copy Firmware crash dump\n");
2742 		else
2743 			dev_info(&instance->pdev->dev, "Firmware crash dump "
2744 				"copied successfully\n");
2745 	}
2746 	return strlen(buf);
2747 }
2748 
2749 static ssize_t
2750 megasas_fw_crash_state_show(struct device *cdev,
2751 	struct device_attribute *attr, char *buf)
2752 {
2753 	struct Scsi_Host *shost = class_to_shost(cdev);
2754 	struct megasas_instance *instance =
2755 		(struct megasas_instance *) shost->hostdata;
2756 	return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2757 }
2758 
2759 static ssize_t
2760 megasas_page_size_show(struct device *cdev,
2761 	struct device_attribute *attr, char *buf)
2762 {
2763 	return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2764 }
2765 
2766 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2767 	megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2768 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2769 	megasas_fw_crash_buffer_size_show, NULL);
2770 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2771 	megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2772 static DEVICE_ATTR(page_size, S_IRUGO,
2773 	megasas_page_size_show, NULL);
2774 
2775 struct device_attribute *megaraid_host_attrs[] = {
2776 	&dev_attr_fw_crash_buffer_size,
2777 	&dev_attr_fw_crash_buffer,
2778 	&dev_attr_fw_crash_state,
2779 	&dev_attr_page_size,
2780 	NULL,
2781 };
2782 
2783 /*
2784  * Scsi host template for megaraid_sas driver
2785  */
2786 static struct scsi_host_template megasas_template = {
2787 
2788 	.module = THIS_MODULE,
2789 	.name = "LSI SAS based MegaRAID driver",
2790 	.proc_name = "megaraid_sas",
2791 	.slave_configure = megasas_slave_configure,
2792 	.slave_alloc = megasas_slave_alloc,
2793 	.queuecommand = megasas_queue_command,
2794 	.eh_device_reset_handler = megasas_reset_device,
2795 	.eh_bus_reset_handler = megasas_reset_bus_host,
2796 	.eh_host_reset_handler = megasas_reset_bus_host,
2797 	.eh_timed_out = megasas_reset_timer,
2798 	.shost_attrs = megaraid_host_attrs,
2799 	.bios_param = megasas_bios_param,
2800 	.use_clustering = ENABLE_CLUSTERING,
2801 	.change_queue_depth = scsi_change_queue_depth,
2802 	.no_write_same = 1,
2803 };
2804 
2805 /**
2806  * megasas_complete_int_cmd -	Completes an internal command
2807  * @instance:			Adapter soft state
2808  * @cmd:			Command to be completed
2809  *
2810  * The megasas_issue_blocked_cmd() function waits for a command to complete
2811  * after it issues a command. This function wakes up that waiting routine by
2812  * calling wake_up() on the wait queue.
2813  */
2814 static void
2815 megasas_complete_int_cmd(struct megasas_instance *instance,
2816 			 struct megasas_cmd *cmd)
2817 {
2818 	cmd->cmd_status = cmd->frame->io.cmd_status;
2819 
2820 	if (cmd->cmd_status == ENODATA) {
2821 		cmd->cmd_status = 0;
2822 	}
2823 	wake_up(&instance->int_cmd_wait_q);
2824 }
2825 
2826 /**
2827  * megasas_complete_abort -	Completes aborting a command
2828  * @instance:			Adapter soft state
2829  * @cmd:			Cmd that was issued to abort another cmd
2830  *
2831  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2832  * after it issues an abort on a previously issued command. This function
2833  * wakes up all functions waiting on the same wait queue.
2834  */
2835 static void
2836 megasas_complete_abort(struct megasas_instance *instance,
2837 		       struct megasas_cmd *cmd)
2838 {
2839 	if (cmd->sync_cmd) {
2840 		cmd->sync_cmd = 0;
2841 		cmd->cmd_status = 0;
2842 		wake_up(&instance->abort_cmd_wait_q);
2843 	}
2844 
2845 	return;
2846 }
2847 
2848 /**
2849  * megasas_complete_cmd -	Completes a command
2850  * @instance:			Adapter soft state
2851  * @cmd:			Command to be completed
2852  * @alt_status:			If non-zero, use this value as status to
2853  * 				SCSI mid-layer instead of the value returned
2854  * 				by the FW. This should be used if caller wants
2855  * 				an alternate status (as in the case of aborted
2856  * 				commands)
2857  */
2858 void
2859 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2860 		     u8 alt_status)
2861 {
2862 	int exception = 0;
2863 	struct megasas_header *hdr = &cmd->frame->hdr;
2864 	unsigned long flags;
2865 	struct fusion_context *fusion = instance->ctrl_context;
2866 	u32 opcode;
2867 
2868 	/* flag for the retry reset */
2869 	cmd->retry_for_fw_reset = 0;
2870 
2871 	if (cmd->scmd)
2872 		cmd->scmd->SCp.ptr = NULL;
2873 
2874 	switch (hdr->cmd) {
2875 	case MFI_CMD_INVALID:
2876 		/* Some older 1068 controller FW may keep a pended
2877 		   MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2878 		   when booting the kdump kernel.  Ignore this command to
2879 		   prevent a kernel panic on shutdown of the kdump kernel. */
2880 		printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2881 		       "completed.\n");
2882 		printk(KERN_WARNING "megaraid_sas: If you have a controller "
2883 		       "other than PERC5, please upgrade your firmware.\n");
2884 		break;
2885 	case MFI_CMD_PD_SCSI_IO:
2886 	case MFI_CMD_LD_SCSI_IO:
2887 
2888 		/*
2889 		 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2890 		 * issued either through an IO path or an IOCTL path. If it
2891 		 * was via IOCTL, we will send it to internal completion.
2892 		 */
2893 		if (cmd->sync_cmd) {
2894 			cmd->sync_cmd = 0;
2895 			megasas_complete_int_cmd(instance, cmd);
2896 			break;
2897 		}
2898 
2899 	case MFI_CMD_LD_READ:
2900 	case MFI_CMD_LD_WRITE:
2901 
2902 		if (alt_status) {
2903 			cmd->scmd->result = alt_status << 16;
2904 			exception = 1;
2905 		}
2906 
2907 		if (exception) {
2908 
2909 			atomic_dec(&instance->fw_outstanding);
2910 
2911 			scsi_dma_unmap(cmd->scmd);
2912 			cmd->scmd->scsi_done(cmd->scmd);
2913 			megasas_return_cmd(instance, cmd);
2914 
2915 			break;
2916 		}
2917 
2918 		switch (hdr->cmd_status) {
2919 
2920 		case MFI_STAT_OK:
2921 			cmd->scmd->result = DID_OK << 16;
2922 			break;
2923 
2924 		case MFI_STAT_SCSI_IO_FAILED:
2925 		case MFI_STAT_LD_INIT_IN_PROGRESS:
2926 			cmd->scmd->result =
2927 			    (DID_ERROR << 16) | hdr->scsi_status;
2928 			break;
2929 
2930 		case MFI_STAT_SCSI_DONE_WITH_ERROR:
2931 
2932 			cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2933 
2934 			if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2935 				memset(cmd->scmd->sense_buffer, 0,
2936 				       SCSI_SENSE_BUFFERSIZE);
2937 				memcpy(cmd->scmd->sense_buffer, cmd->sense,
2938 				       hdr->sense_len);
2939 
2940 				cmd->scmd->result |= DRIVER_SENSE << 24;
2941 			}
2942 
2943 			break;
2944 
2945 		case MFI_STAT_LD_OFFLINE:
2946 		case MFI_STAT_DEVICE_NOT_FOUND:
2947 			cmd->scmd->result = DID_BAD_TARGET << 16;
2948 			break;
2949 
2950 		default:
2951 			printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2952 			       hdr->cmd_status);
2953 			cmd->scmd->result = DID_ERROR << 16;
2954 			break;
2955 		}
2956 
2957 		atomic_dec(&instance->fw_outstanding);
2958 
2959 		scsi_dma_unmap(cmd->scmd);
2960 		cmd->scmd->scsi_done(cmd->scmd);
2961 		megasas_return_cmd(instance, cmd);
2962 
2963 		break;
2964 
2965 	case MFI_CMD_SMP:
2966 	case MFI_CMD_STP:
2967 	case MFI_CMD_DCMD:
2968 		opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
2969 		/* Check for LD map update */
2970 		if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2971 			&& (cmd->frame->dcmd.mbox.b[1] == 1)) {
2972 			fusion->fast_path_io = 0;
2973 			spin_lock_irqsave(instance->host->host_lock, flags);
2974 			if (cmd->frame->hdr.cmd_status != 0) {
2975 				if (cmd->frame->hdr.cmd_status !=
2976 				    MFI_STAT_NOT_FOUND)
2977 					printk(KERN_WARNING "megasas: map sync"
2978 					       "failed, status = 0x%x.\n",
2979 					       cmd->frame->hdr.cmd_status);
2980 				else {
2981 					megasas_return_mfi_mpt_pthr(instance,
2982 						cmd, cmd->mpt_pthr_cmd_blocked);
2983 					spin_unlock_irqrestore(
2984 						instance->host->host_lock,
2985 						flags);
2986 					break;
2987 				}
2988 			} else
2989 				instance->map_id++;
2990 			megasas_return_mfi_mpt_pthr(instance, cmd,
2991 				cmd->mpt_pthr_cmd_blocked);
2992 
2993 			/*
2994 			 * Set fast path IO to ZERO.
2995 			 * Validate Map will set proper value.
2996 			 * Meanwhile all IOs will go as LD IO.
2997 			 */
2998 			if (MR_ValidateMapInfo(instance))
2999 				fusion->fast_path_io = 1;
3000 			else
3001 				fusion->fast_path_io = 0;
3002 			megasas_sync_map_info(instance);
3003 			spin_unlock_irqrestore(instance->host->host_lock,
3004 					       flags);
3005 			break;
3006 		}
3007 		if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3008 		    opcode == MR_DCMD_CTRL_EVENT_GET) {
3009 			spin_lock_irqsave(&poll_aen_lock, flags);
3010 			megasas_poll_wait_aen = 0;
3011 			spin_unlock_irqrestore(&poll_aen_lock, flags);
3012 		}
3013 
3014 		/*
3015 		 * See if got an event notification
3016 		 */
3017 		if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3018 			megasas_service_aen(instance, cmd);
3019 		else
3020 			megasas_complete_int_cmd(instance, cmd);
3021 
3022 		break;
3023 
3024 	case MFI_CMD_ABORT:
3025 		/*
3026 		 * Cmd issued to abort another cmd returned
3027 		 */
3028 		megasas_complete_abort(instance, cmd);
3029 		break;
3030 
3031 	default:
3032 		printk("megasas: Unknown command completed! [0x%X]\n",
3033 		       hdr->cmd);
3034 		break;
3035 	}
3036 }
3037 
3038 /**
3039  * megasas_issue_pending_cmds_again -	issue all pending cmds
3040  *                              	in FW again because of the fw reset
3041  * @instance:				Adapter soft state
3042  */
3043 static inline void
3044 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3045 {
3046 	struct megasas_cmd *cmd;
3047 	struct list_head clist_local;
3048 	union megasas_evt_class_locale class_locale;
3049 	unsigned long flags;
3050 	u32 seq_num;
3051 
3052 	INIT_LIST_HEAD(&clist_local);
3053 	spin_lock_irqsave(&instance->hba_lock, flags);
3054 	list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3055 	spin_unlock_irqrestore(&instance->hba_lock, flags);
3056 
3057 	while (!list_empty(&clist_local)) {
3058 		cmd	= list_entry((&clist_local)->next,
3059 					struct megasas_cmd, list);
3060 		list_del_init(&cmd->list);
3061 
3062 		if (cmd->sync_cmd || cmd->scmd) {
3063 			printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
3064 				"detected to be pending while HBA reset.\n",
3065 					cmd, cmd->scmd, cmd->sync_cmd);
3066 
3067 			cmd->retry_for_fw_reset++;
3068 
3069 			if (cmd->retry_for_fw_reset == 3) {
3070 				printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
3071 					"was tried multiple times during reset."
3072 					"Shutting down the HBA\n",
3073 					cmd, cmd->scmd, cmd->sync_cmd);
3074 				instance->instancet->disable_intr(instance);
3075 				atomic_set(&instance->fw_reset_no_pci_access, 1);
3076 				megaraid_sas_kill_hba(instance);
3077 				return;
3078 			}
3079 		}
3080 
3081 		if (cmd->sync_cmd == 1) {
3082 			if (cmd->scmd) {
3083 				printk(KERN_NOTICE "megaraid_sas: unexpected"
3084 					"cmd attached to internal command!\n");
3085 			}
3086 			printk(KERN_NOTICE "megasas: %p synchronous cmd"
3087 						"on the internal reset queue,"
3088 						"issue it again.\n", cmd);
3089 			cmd->cmd_status = ENODATA;
3090 			instance->instancet->fire_cmd(instance,
3091 							cmd->frame_phys_addr ,
3092 							0, instance->reg_set);
3093 		} else if (cmd->scmd) {
3094 			printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
3095 			"detected on the internal queue, issue again.\n",
3096 			cmd, cmd->scmd->cmnd[0]);
3097 
3098 			atomic_inc(&instance->fw_outstanding);
3099 			instance->instancet->fire_cmd(instance,
3100 					cmd->frame_phys_addr,
3101 					cmd->frame_count-1, instance->reg_set);
3102 		} else {
3103 			printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
3104 				"internal reset defer list while re-issue!!\n",
3105 				cmd);
3106 		}
3107 	}
3108 
3109 	if (instance->aen_cmd) {
3110 		printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
3111 		megasas_return_cmd(instance, instance->aen_cmd);
3112 
3113 		instance->aen_cmd	= NULL;
3114 	}
3115 
3116 	/*
3117 	* Initiate AEN (Asynchronous Event Notification)
3118 	*/
3119 	seq_num = instance->last_seq_num;
3120 	class_locale.members.reserved = 0;
3121 	class_locale.members.locale = MR_EVT_LOCALE_ALL;
3122 	class_locale.members.class = MR_EVT_CLASS_DEBUG;
3123 
3124 	megasas_register_aen(instance, seq_num, class_locale.word);
3125 }
3126 
3127 /**
3128  * Move the internal reset pending commands to a deferred queue.
3129  *
3130  * We move the commands pending at internal reset time to a
3131  * pending queue. This queue would be flushed after successful
3132  * completion of the internal reset sequence. if the internal reset
3133  * did not complete in time, the kernel reset handler would flush
3134  * these commands.
3135  **/
3136 static void
3137 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3138 {
3139 	struct megasas_cmd *cmd;
3140 	int i;
3141 	u32 max_cmd = instance->max_fw_cmds;
3142 	u32 defer_index;
3143 	unsigned long flags;
3144 
3145 	defer_index     = 0;
3146 	spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3147 	for (i = 0; i < max_cmd; i++) {
3148 		cmd = instance->cmd_list[i];
3149 		if (cmd->sync_cmd == 1 || cmd->scmd) {
3150 			printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
3151 					"on the defer queue as internal\n",
3152 				defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3153 
3154 			if (!list_empty(&cmd->list)) {
3155 				printk(KERN_NOTICE "megaraid_sas: ERROR while"
3156 					" moving this cmd:%p, %d %p, it was"
3157 					"discovered on some list?\n",
3158 					cmd, cmd->sync_cmd, cmd->scmd);
3159 
3160 				list_del_init(&cmd->list);
3161 			}
3162 			defer_index++;
3163 			list_add_tail(&cmd->list,
3164 				&instance->internal_reset_pending_q);
3165 		}
3166 	}
3167 	spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3168 }
3169 
3170 
3171 static void
3172 process_fw_state_change_wq(struct work_struct *work)
3173 {
3174 	struct megasas_instance *instance =
3175 		container_of(work, struct megasas_instance, work_init);
3176 	u32 wait;
3177 	unsigned long flags;
3178 
3179 	if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3180 		printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
3181 				instance->adprecovery);
3182 		return ;
3183 	}
3184 
3185 	if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3186 		printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
3187 					"state, restarting it...\n");
3188 
3189 		instance->instancet->disable_intr(instance);
3190 		atomic_set(&instance->fw_outstanding, 0);
3191 
3192 		atomic_set(&instance->fw_reset_no_pci_access, 1);
3193 		instance->instancet->adp_reset(instance, instance->reg_set);
3194 		atomic_set(&instance->fw_reset_no_pci_access, 0 );
3195 
3196 		printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
3197 					"initiating next stage...\n");
3198 
3199 		printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
3200 					"state 2 starting...\n");
3201 
3202 		/*waitting for about 20 second before start the second init*/
3203 		for (wait = 0; wait < 30; wait++) {
3204 			msleep(1000);
3205 		}
3206 
3207 		if (megasas_transition_to_ready(instance, 1)) {
3208 			printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
3209 
3210 			atomic_set(&instance->fw_reset_no_pci_access, 1);
3211 			megaraid_sas_kill_hba(instance);
3212 			return ;
3213 		}
3214 
3215 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3216 			(instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3217 			(instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3218 			) {
3219 			*instance->consumer = *instance->producer;
3220 		} else {
3221 			*instance->consumer = 0;
3222 			*instance->producer = 0;
3223 		}
3224 
3225 		megasas_issue_init_mfi(instance);
3226 
3227 		spin_lock_irqsave(&instance->hba_lock, flags);
3228 		instance->adprecovery	= MEGASAS_HBA_OPERATIONAL;
3229 		spin_unlock_irqrestore(&instance->hba_lock, flags);
3230 		instance->instancet->enable_intr(instance);
3231 
3232 		megasas_issue_pending_cmds_again(instance);
3233 		instance->issuepend_done = 1;
3234 	}
3235 	return ;
3236 }
3237 
3238 /**
3239  * megasas_deplete_reply_queue -	Processes all completed commands
3240  * @instance:				Adapter soft state
3241  * @alt_status:				Alternate status to be returned to
3242  * 					SCSI mid-layer instead of the status
3243  * 					returned by the FW
3244  * Note: this must be called with hba lock held
3245  */
3246 static int
3247 megasas_deplete_reply_queue(struct megasas_instance *instance,
3248 					u8 alt_status)
3249 {
3250 	u32 mfiStatus;
3251 	u32 fw_state;
3252 
3253 	if ((mfiStatus = instance->instancet->check_reset(instance,
3254 					instance->reg_set)) == 1) {
3255 		return IRQ_HANDLED;
3256 	}
3257 
3258 	if ((mfiStatus = instance->instancet->clear_intr(
3259 						instance->reg_set)
3260 						) == 0) {
3261 		/* Hardware may not set outbound_intr_status in MSI-X mode */
3262 		if (!instance->msix_vectors)
3263 			return IRQ_NONE;
3264 	}
3265 
3266 	instance->mfiStatus = mfiStatus;
3267 
3268 	if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3269 		fw_state = instance->instancet->read_fw_status_reg(
3270 				instance->reg_set) & MFI_STATE_MASK;
3271 
3272 		if (fw_state != MFI_STATE_FAULT) {
3273 			printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
3274 						fw_state);
3275 		}
3276 
3277 		if ((fw_state == MFI_STATE_FAULT) &&
3278 				(instance->disableOnlineCtrlReset == 0)) {
3279 			printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
3280 
3281 			if ((instance->pdev->device ==
3282 					PCI_DEVICE_ID_LSI_SAS1064R) ||
3283 				(instance->pdev->device ==
3284 					PCI_DEVICE_ID_DELL_PERC5) ||
3285 				(instance->pdev->device ==
3286 					PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3287 
3288 				*instance->consumer =
3289 					cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3290 			}
3291 
3292 
3293 			instance->instancet->disable_intr(instance);
3294 			instance->adprecovery	= MEGASAS_ADPRESET_SM_INFAULT;
3295 			instance->issuepend_done = 0;
3296 
3297 			atomic_set(&instance->fw_outstanding, 0);
3298 			megasas_internal_reset_defer_cmds(instance);
3299 
3300 			printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
3301 					fw_state, instance->adprecovery);
3302 
3303 			schedule_work(&instance->work_init);
3304 			return IRQ_HANDLED;
3305 
3306 		} else {
3307 			printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
3308 				fw_state, instance->disableOnlineCtrlReset);
3309 		}
3310 	}
3311 
3312 	tasklet_schedule(&instance->isr_tasklet);
3313 	return IRQ_HANDLED;
3314 }
3315 /**
3316  * megasas_isr - isr entry point
3317  */
3318 static irqreturn_t megasas_isr(int irq, void *devp)
3319 {
3320 	struct megasas_irq_context *irq_context = devp;
3321 	struct megasas_instance *instance = irq_context->instance;
3322 	unsigned long flags;
3323 	irqreturn_t	rc;
3324 
3325 	if (atomic_read(&instance->fw_reset_no_pci_access))
3326 		return IRQ_HANDLED;
3327 
3328 	spin_lock_irqsave(&instance->hba_lock, flags);
3329 	rc =  megasas_deplete_reply_queue(instance, DID_OK);
3330 	spin_unlock_irqrestore(&instance->hba_lock, flags);
3331 
3332 	return rc;
3333 }
3334 
3335 /**
3336  * megasas_transition_to_ready -	Move the FW to READY state
3337  * @instance:				Adapter soft state
3338  *
3339  * During the initialization, FW passes can potentially be in any one of
3340  * several possible states. If the FW in operational, waiting-for-handshake
3341  * states, driver must take steps to bring it to ready state. Otherwise, it
3342  * has to wait for the ready state.
3343  */
3344 int
3345 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3346 {
3347 	int i;
3348 	u8 max_wait;
3349 	u32 fw_state;
3350 	u32 cur_state;
3351 	u32 abs_state, curr_abs_state;
3352 
3353 	abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3354 	fw_state = abs_state & MFI_STATE_MASK;
3355 
3356 	if (fw_state != MFI_STATE_READY)
3357 		printk(KERN_INFO "megasas: Waiting for FW to come to ready"
3358 		       " state\n");
3359 
3360 	while (fw_state != MFI_STATE_READY) {
3361 
3362 		switch (fw_state) {
3363 
3364 		case MFI_STATE_FAULT:
3365 			printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
3366 			if (ocr) {
3367 				max_wait = MEGASAS_RESET_WAIT_TIME;
3368 				cur_state = MFI_STATE_FAULT;
3369 				break;
3370 			} else
3371 				return -ENODEV;
3372 
3373 		case MFI_STATE_WAIT_HANDSHAKE:
3374 			/*
3375 			 * Set the CLR bit in inbound doorbell
3376 			 */
3377 			if ((instance->pdev->device ==
3378 				PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3379 				(instance->pdev->device ==
3380 				 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3381 				(instance->pdev->device ==
3382 				PCI_DEVICE_ID_LSI_FUSION) ||
3383 				(instance->pdev->device ==
3384 				PCI_DEVICE_ID_LSI_PLASMA) ||
3385 				(instance->pdev->device ==
3386 				PCI_DEVICE_ID_LSI_INVADER) ||
3387 				(instance->pdev->device ==
3388 				PCI_DEVICE_ID_LSI_FURY)) {
3389 				writel(
3390 				  MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3391 				  &instance->reg_set->doorbell);
3392 			} else {
3393 				writel(
3394 				    MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3395 					&instance->reg_set->inbound_doorbell);
3396 			}
3397 
3398 			max_wait = MEGASAS_RESET_WAIT_TIME;
3399 			cur_state = MFI_STATE_WAIT_HANDSHAKE;
3400 			break;
3401 
3402 		case MFI_STATE_BOOT_MESSAGE_PENDING:
3403 			if ((instance->pdev->device ==
3404 			     PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3405 				(instance->pdev->device ==
3406 				 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3407 			    (instance->pdev->device ==
3408 			     PCI_DEVICE_ID_LSI_FUSION) ||
3409 			    (instance->pdev->device ==
3410 			     PCI_DEVICE_ID_LSI_PLASMA) ||
3411 			    (instance->pdev->device ==
3412 			     PCI_DEVICE_ID_LSI_INVADER) ||
3413 			    (instance->pdev->device ==
3414 			     PCI_DEVICE_ID_LSI_FURY)) {
3415 				writel(MFI_INIT_HOTPLUG,
3416 				       &instance->reg_set->doorbell);
3417 			} else
3418 				writel(MFI_INIT_HOTPLUG,
3419 					&instance->reg_set->inbound_doorbell);
3420 
3421 			max_wait = MEGASAS_RESET_WAIT_TIME;
3422 			cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3423 			break;
3424 
3425 		case MFI_STATE_OPERATIONAL:
3426 			/*
3427 			 * Bring it to READY state; assuming max wait 10 secs
3428 			 */
3429 			instance->instancet->disable_intr(instance);
3430 			if ((instance->pdev->device ==
3431 				PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3432 				(instance->pdev->device ==
3433 				PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
3434 				(instance->pdev->device
3435 					== PCI_DEVICE_ID_LSI_FUSION) ||
3436 				(instance->pdev->device
3437 					== PCI_DEVICE_ID_LSI_PLASMA) ||
3438 				(instance->pdev->device
3439 					== PCI_DEVICE_ID_LSI_INVADER) ||
3440 				(instance->pdev->device
3441 					== PCI_DEVICE_ID_LSI_FURY)) {
3442 				writel(MFI_RESET_FLAGS,
3443 					&instance->reg_set->doorbell);
3444 				if ((instance->pdev->device ==
3445 					PCI_DEVICE_ID_LSI_FUSION) ||
3446 					(instance->pdev->device ==
3447 					PCI_DEVICE_ID_LSI_PLASMA) ||
3448 					(instance->pdev->device ==
3449 					PCI_DEVICE_ID_LSI_INVADER) ||
3450 					(instance->pdev->device ==
3451 					PCI_DEVICE_ID_LSI_FURY)) {
3452 					for (i = 0; i < (10 * 1000); i += 20) {
3453 						if (readl(
3454 							    &instance->
3455 							    reg_set->
3456 							    doorbell) & 1)
3457 							msleep(20);
3458 						else
3459 							break;
3460 					}
3461 				}
3462 			} else
3463 				writel(MFI_RESET_FLAGS,
3464 					&instance->reg_set->inbound_doorbell);
3465 
3466 			max_wait = MEGASAS_RESET_WAIT_TIME;
3467 			cur_state = MFI_STATE_OPERATIONAL;
3468 			break;
3469 
3470 		case MFI_STATE_UNDEFINED:
3471 			/*
3472 			 * This state should not last for more than 2 seconds
3473 			 */
3474 			max_wait = MEGASAS_RESET_WAIT_TIME;
3475 			cur_state = MFI_STATE_UNDEFINED;
3476 			break;
3477 
3478 		case MFI_STATE_BB_INIT:
3479 			max_wait = MEGASAS_RESET_WAIT_TIME;
3480 			cur_state = MFI_STATE_BB_INIT;
3481 			break;
3482 
3483 		case MFI_STATE_FW_INIT:
3484 			max_wait = MEGASAS_RESET_WAIT_TIME;
3485 			cur_state = MFI_STATE_FW_INIT;
3486 			break;
3487 
3488 		case MFI_STATE_FW_INIT_2:
3489 			max_wait = MEGASAS_RESET_WAIT_TIME;
3490 			cur_state = MFI_STATE_FW_INIT_2;
3491 			break;
3492 
3493 		case MFI_STATE_DEVICE_SCAN:
3494 			max_wait = MEGASAS_RESET_WAIT_TIME;
3495 			cur_state = MFI_STATE_DEVICE_SCAN;
3496 			break;
3497 
3498 		case MFI_STATE_FLUSH_CACHE:
3499 			max_wait = MEGASAS_RESET_WAIT_TIME;
3500 			cur_state = MFI_STATE_FLUSH_CACHE;
3501 			break;
3502 
3503 		default:
3504 			printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
3505 			       fw_state);
3506 			return -ENODEV;
3507 		}
3508 
3509 		/*
3510 		 * The cur_state should not last for more than max_wait secs
3511 		 */
3512 		for (i = 0; i < (max_wait * 1000); i++) {
3513 			curr_abs_state = instance->instancet->
3514 				read_fw_status_reg(instance->reg_set);
3515 
3516 			if (abs_state == curr_abs_state) {
3517 				msleep(1);
3518 			} else
3519 				break;
3520 		}
3521 
3522 		/*
3523 		 * Return error if fw_state hasn't changed after max_wait
3524 		 */
3525 		if (curr_abs_state == abs_state) {
3526 			printk(KERN_DEBUG "FW state [%d] hasn't changed "
3527 			       "in %d secs\n", fw_state, max_wait);
3528 			return -ENODEV;
3529 		}
3530 
3531 		abs_state = curr_abs_state;
3532 		fw_state = curr_abs_state & MFI_STATE_MASK;
3533 	}
3534 	printk(KERN_INFO "megasas: FW now in Ready state\n");
3535 
3536 	return 0;
3537 }
3538 
3539 /**
3540  * megasas_teardown_frame_pool -	Destroy the cmd frame DMA pool
3541  * @instance:				Adapter soft state
3542  */
3543 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3544 {
3545 	int i;
3546 	u32 max_cmd = instance->max_mfi_cmds;
3547 	struct megasas_cmd *cmd;
3548 
3549 	if (!instance->frame_dma_pool)
3550 		return;
3551 
3552 	/*
3553 	 * Return all frames to pool
3554 	 */
3555 	for (i = 0; i < max_cmd; i++) {
3556 
3557 		cmd = instance->cmd_list[i];
3558 
3559 		if (cmd->frame)
3560 			pci_pool_free(instance->frame_dma_pool, cmd->frame,
3561 				      cmd->frame_phys_addr);
3562 
3563 		if (cmd->sense)
3564 			pci_pool_free(instance->sense_dma_pool, cmd->sense,
3565 				      cmd->sense_phys_addr);
3566 	}
3567 
3568 	/*
3569 	 * Now destroy the pool itself
3570 	 */
3571 	pci_pool_destroy(instance->frame_dma_pool);
3572 	pci_pool_destroy(instance->sense_dma_pool);
3573 
3574 	instance->frame_dma_pool = NULL;
3575 	instance->sense_dma_pool = NULL;
3576 }
3577 
3578 /**
3579  * megasas_create_frame_pool -	Creates DMA pool for cmd frames
3580  * @instance:			Adapter soft state
3581  *
3582  * Each command packet has an embedded DMA memory buffer that is used for
3583  * filling MFI frame and the SG list that immediately follows the frame. This
3584  * function creates those DMA memory buffers for each command packet by using
3585  * PCI pool facility.
3586  */
3587 static int megasas_create_frame_pool(struct megasas_instance *instance)
3588 {
3589 	int i;
3590 	u32 max_cmd;
3591 	u32 sge_sz;
3592 	u32 total_sz;
3593 	u32 frame_count;
3594 	struct megasas_cmd *cmd;
3595 
3596 	max_cmd = instance->max_mfi_cmds;
3597 
3598 	/*
3599 	 * Size of our frame is 64 bytes for MFI frame, followed by max SG
3600 	 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3601 	 */
3602 	sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3603 	    sizeof(struct megasas_sge32);
3604 
3605 	if (instance->flag_ieee) {
3606 		sge_sz = sizeof(struct megasas_sge_skinny);
3607 	}
3608 
3609 	/*
3610 	 * For MFI controllers.
3611 	 * max_num_sge = 60
3612 	 * max_sge_sz  = 16 byte (sizeof megasas_sge_skinny)
3613 	 * Total 960 byte (15 MFI frame of 64 byte)
3614 	 *
3615 	 * Fusion adapter require only 3 extra frame.
3616 	 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3617 	 * max_sge_sz  = 12 byte (sizeof  megasas_sge64)
3618 	 * Total 192 byte (3 MFI frame of 64 byte)
3619 	 */
3620 	frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3621 	total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3622 	/*
3623 	 * Use DMA pool facility provided by PCI layer
3624 	 */
3625 	instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3626 					instance->pdev, total_sz, 256, 0);
3627 
3628 	if (!instance->frame_dma_pool) {
3629 		printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
3630 		return -ENOMEM;
3631 	}
3632 
3633 	instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3634 						   instance->pdev, 128, 4, 0);
3635 
3636 	if (!instance->sense_dma_pool) {
3637 		printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
3638 
3639 		pci_pool_destroy(instance->frame_dma_pool);
3640 		instance->frame_dma_pool = NULL;
3641 
3642 		return -ENOMEM;
3643 	}
3644 
3645 	/*
3646 	 * Allocate and attach a frame to each of the commands in cmd_list.
3647 	 * By making cmd->index as the context instead of the &cmd, we can
3648 	 * always use 32bit context regardless of the architecture
3649 	 */
3650 	for (i = 0; i < max_cmd; i++) {
3651 
3652 		cmd = instance->cmd_list[i];
3653 
3654 		cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3655 					    GFP_KERNEL, &cmd->frame_phys_addr);
3656 
3657 		cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3658 					    GFP_KERNEL, &cmd->sense_phys_addr);
3659 
3660 		/*
3661 		 * megasas_teardown_frame_pool() takes care of freeing
3662 		 * whatever has been allocated
3663 		 */
3664 		if (!cmd->frame || !cmd->sense) {
3665 			printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
3666 			megasas_teardown_frame_pool(instance);
3667 			return -ENOMEM;
3668 		}
3669 
3670 		memset(cmd->frame, 0, total_sz);
3671 		cmd->frame->io.context = cpu_to_le32(cmd->index);
3672 		cmd->frame->io.pad_0 = 0;
3673 		if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
3674 		    (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
3675 		    (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
3676 			(instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
3677 		    (reset_devices))
3678 			cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3679 	}
3680 
3681 	return 0;
3682 }
3683 
3684 /**
3685  * megasas_free_cmds -	Free all the cmds in the free cmd pool
3686  * @instance:		Adapter soft state
3687  */
3688 void megasas_free_cmds(struct megasas_instance *instance)
3689 {
3690 	int i;
3691 	/* First free the MFI frame pool */
3692 	megasas_teardown_frame_pool(instance);
3693 
3694 	/* Free all the commands in the cmd_list */
3695 	for (i = 0; i < instance->max_mfi_cmds; i++)
3696 
3697 		kfree(instance->cmd_list[i]);
3698 
3699 	/* Free the cmd_list buffer itself */
3700 	kfree(instance->cmd_list);
3701 	instance->cmd_list = NULL;
3702 
3703 	INIT_LIST_HEAD(&instance->cmd_pool);
3704 }
3705 
3706 /**
3707  * megasas_alloc_cmds -	Allocates the command packets
3708  * @instance:		Adapter soft state
3709  *
3710  * Each command that is issued to the FW, whether IO commands from the OS or
3711  * internal commands like IOCTLs, are wrapped in local data structure called
3712  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3713  * the FW.
3714  *
3715  * Each frame has a 32-bit field called context (tag). This context is used
3716  * to get back the megasas_cmd from the frame when a frame gets completed in
3717  * the ISR. Typically the address of the megasas_cmd itself would be used as
3718  * the context. But we wanted to keep the differences between 32 and 64 bit
3719  * systems to the mininum. We always use 32 bit integers for the context. In
3720  * this driver, the 32 bit values are the indices into an array cmd_list.
3721  * This array is used only to look up the megasas_cmd given the context. The
3722  * free commands themselves are maintained in a linked list called cmd_pool.
3723  */
3724 int megasas_alloc_cmds(struct megasas_instance *instance)
3725 {
3726 	int i;
3727 	int j;
3728 	u32 max_cmd;
3729 	struct megasas_cmd *cmd;
3730 	struct fusion_context *fusion;
3731 
3732 	fusion = instance->ctrl_context;
3733 	max_cmd = instance->max_mfi_cmds;
3734 
3735 	/*
3736 	 * instance->cmd_list is an array of struct megasas_cmd pointers.
3737 	 * Allocate the dynamic array first and then allocate individual
3738 	 * commands.
3739 	 */
3740 	instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3741 
3742 	if (!instance->cmd_list) {
3743 		printk(KERN_DEBUG "megasas: out of memory\n");
3744 		return -ENOMEM;
3745 	}
3746 
3747 	memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3748 
3749 	for (i = 0; i < max_cmd; i++) {
3750 		instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3751 						GFP_KERNEL);
3752 
3753 		if (!instance->cmd_list[i]) {
3754 
3755 			for (j = 0; j < i; j++)
3756 				kfree(instance->cmd_list[j]);
3757 
3758 			kfree(instance->cmd_list);
3759 			instance->cmd_list = NULL;
3760 
3761 			return -ENOMEM;
3762 		}
3763 	}
3764 
3765 	for (i = 0; i < max_cmd; i++) {
3766 		cmd = instance->cmd_list[i];
3767 		memset(cmd, 0, sizeof(struct megasas_cmd));
3768 		cmd->index = i;
3769 		atomic_set(&cmd->mfi_mpt_pthr, MFI_LIST_ADDED);
3770 		cmd->scmd = NULL;
3771 		cmd->instance = instance;
3772 
3773 		list_add_tail(&cmd->list, &instance->cmd_pool);
3774 	}
3775 
3776 	/*
3777 	 * Create a frame pool and assign one frame to each cmd
3778 	 */
3779 	if (megasas_create_frame_pool(instance)) {
3780 		printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3781 		megasas_free_cmds(instance);
3782 	}
3783 
3784 	return 0;
3785 }
3786 
3787 /*
3788  * megasas_get_pd_list_info -	Returns FW's pd_list structure
3789  * @instance:				Adapter soft state
3790  * @pd_list:				pd_list structure
3791  *
3792  * Issues an internal command (DCMD) to get the FW's controller PD
3793  * list structure.  This information is mainly used to find out SYSTEM
3794  * supported by the FW.
3795  */
3796 static int
3797 megasas_get_pd_list(struct megasas_instance *instance)
3798 {
3799 	int ret = 0, pd_index = 0;
3800 	struct megasas_cmd *cmd;
3801 	struct megasas_dcmd_frame *dcmd;
3802 	struct MR_PD_LIST *ci;
3803 	struct MR_PD_ADDRESS *pd_addr;
3804 	dma_addr_t ci_h = 0;
3805 
3806 	cmd = megasas_get_cmd(instance);
3807 
3808 	if (!cmd) {
3809 		printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3810 		return -ENOMEM;
3811 	}
3812 
3813 	dcmd = &cmd->frame->dcmd;
3814 
3815 	ci = pci_alloc_consistent(instance->pdev,
3816 		  MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3817 
3818 	if (!ci) {
3819 		printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3820 		megasas_return_cmd(instance, cmd);
3821 		return -ENOMEM;
3822 	}
3823 
3824 	memset(ci, 0, sizeof(*ci));
3825 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3826 
3827 	dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3828 	dcmd->mbox.b[1] = 0;
3829 	dcmd->cmd = MFI_CMD_DCMD;
3830 	dcmd->cmd_status = 0xFF;
3831 	dcmd->sge_count = 1;
3832 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3833 	dcmd->timeout = 0;
3834 	dcmd->pad_0 = 0;
3835 	dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3836 	dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3837 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3838 	dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3839 
3840 	if (instance->ctrl_context && !instance->mask_interrupts)
3841 		ret = megasas_issue_blocked_cmd(instance, cmd,
3842 			MEGASAS_BLOCKED_CMD_TIMEOUT);
3843 	else
3844 		ret = megasas_issue_polled(instance, cmd);
3845 
3846 	/*
3847 	* the following function will get the instance PD LIST.
3848 	*/
3849 
3850 	pd_addr = ci->addr;
3851 
3852 	if ( ret == 0 &&
3853 	     (le32_to_cpu(ci->count) <
3854 		  (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3855 
3856 		memset(instance->local_pd_list, 0,
3857 			MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3858 
3859 		for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3860 
3861 			instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid	=
3862 				le16_to_cpu(pd_addr->deviceId);
3863 			instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType	=
3864 							pd_addr->scsiDevType;
3865 			instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState	=
3866 							MR_PD_STATE_SYSTEM;
3867 			pd_addr++;
3868 		}
3869 		memcpy(instance->pd_list, instance->local_pd_list,
3870 			sizeof(instance->pd_list));
3871 	}
3872 
3873 	pci_free_consistent(instance->pdev,
3874 				MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3875 				ci, ci_h);
3876 
3877 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3878 		megasas_return_mfi_mpt_pthr(instance, cmd,
3879 			cmd->mpt_pthr_cmd_blocked);
3880 	else
3881 		megasas_return_cmd(instance, cmd);
3882 
3883 	return ret;
3884 }
3885 
3886 /*
3887  * megasas_get_ld_list_info -	Returns FW's ld_list structure
3888  * @instance:				Adapter soft state
3889  * @ld_list:				ld_list structure
3890  *
3891  * Issues an internal command (DCMD) to get the FW's controller PD
3892  * list structure.  This information is mainly used to find out SYSTEM
3893  * supported by the FW.
3894  */
3895 static int
3896 megasas_get_ld_list(struct megasas_instance *instance)
3897 {
3898 	int ret = 0, ld_index = 0, ids = 0;
3899 	struct megasas_cmd *cmd;
3900 	struct megasas_dcmd_frame *dcmd;
3901 	struct MR_LD_LIST *ci;
3902 	dma_addr_t ci_h = 0;
3903 	u32 ld_count;
3904 
3905 	cmd = megasas_get_cmd(instance);
3906 
3907 	if (!cmd) {
3908 		printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3909 		return -ENOMEM;
3910 	}
3911 
3912 	dcmd = &cmd->frame->dcmd;
3913 
3914 	ci = pci_alloc_consistent(instance->pdev,
3915 				sizeof(struct MR_LD_LIST),
3916 				&ci_h);
3917 
3918 	if (!ci) {
3919 		printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3920 		megasas_return_cmd(instance, cmd);
3921 		return -ENOMEM;
3922 	}
3923 
3924 	memset(ci, 0, sizeof(*ci));
3925 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3926 
3927 	if (instance->supportmax256vd)
3928 		dcmd->mbox.b[0] = 1;
3929 	dcmd->cmd = MFI_CMD_DCMD;
3930 	dcmd->cmd_status = 0xFF;
3931 	dcmd->sge_count = 1;
3932 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3933 	dcmd->timeout = 0;
3934 	dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3935 	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3936 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3937 	dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
3938 	dcmd->pad_0  = 0;
3939 
3940 	if (instance->ctrl_context && !instance->mask_interrupts)
3941 		ret = megasas_issue_blocked_cmd(instance, cmd,
3942 			MEGASAS_BLOCKED_CMD_TIMEOUT);
3943 	else
3944 		ret = megasas_issue_polled(instance, cmd);
3945 
3946 
3947 	ld_count = le32_to_cpu(ci->ldCount);
3948 
3949 	/* the following function will get the instance PD LIST */
3950 
3951 	if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3952 		memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
3953 
3954 		for (ld_index = 0; ld_index < ld_count; ld_index++) {
3955 			if (ci->ldList[ld_index].state != 0) {
3956 				ids = ci->ldList[ld_index].ref.targetId;
3957 				instance->ld_ids[ids] =
3958 					ci->ldList[ld_index].ref.targetId;
3959 			}
3960 		}
3961 	}
3962 
3963 	pci_free_consistent(instance->pdev,
3964 				sizeof(struct MR_LD_LIST),
3965 				ci,
3966 				ci_h);
3967 
3968 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
3969 		megasas_return_mfi_mpt_pthr(instance, cmd,
3970 			cmd->mpt_pthr_cmd_blocked);
3971 	else
3972 		megasas_return_cmd(instance, cmd);
3973 	return ret;
3974 }
3975 
3976 /**
3977  * megasas_ld_list_query -	Returns FW's ld_list structure
3978  * @instance:				Adapter soft state
3979  * @ld_list:				ld_list structure
3980  *
3981  * Issues an internal command (DCMD) to get the FW's controller PD
3982  * list structure.  This information is mainly used to find out SYSTEM
3983  * supported by the FW.
3984  */
3985 static int
3986 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3987 {
3988 	int ret = 0, ld_index = 0, ids = 0;
3989 	struct megasas_cmd *cmd;
3990 	struct megasas_dcmd_frame *dcmd;
3991 	struct MR_LD_TARGETID_LIST *ci;
3992 	dma_addr_t ci_h = 0;
3993 	u32 tgtid_count;
3994 
3995 	cmd = megasas_get_cmd(instance);
3996 
3997 	if (!cmd) {
3998 		printk(KERN_WARNING
3999 		       "megasas:(megasas_ld_list_query): Failed to get cmd\n");
4000 		return -ENOMEM;
4001 	}
4002 
4003 	dcmd = &cmd->frame->dcmd;
4004 
4005 	ci = pci_alloc_consistent(instance->pdev,
4006 				  sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4007 
4008 	if (!ci) {
4009 		printk(KERN_WARNING
4010 		       "megasas: Failed to alloc mem for ld_list_query\n");
4011 		megasas_return_cmd(instance, cmd);
4012 		return -ENOMEM;
4013 	}
4014 
4015 	memset(ci, 0, sizeof(*ci));
4016 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4017 
4018 	dcmd->mbox.b[0] = query_type;
4019 	if (instance->supportmax256vd)
4020 		dcmd->mbox.b[2] = 1;
4021 
4022 	dcmd->cmd = MFI_CMD_DCMD;
4023 	dcmd->cmd_status = 0xFF;
4024 	dcmd->sge_count = 1;
4025 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4026 	dcmd->timeout = 0;
4027 	dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4028 	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4029 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4030 	dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4031 	dcmd->pad_0  = 0;
4032 
4033 	if (instance->ctrl_context && !instance->mask_interrupts)
4034 		ret = megasas_issue_blocked_cmd(instance, cmd,
4035 			MEGASAS_BLOCKED_CMD_TIMEOUT);
4036 	else
4037 		ret = megasas_issue_polled(instance, cmd);
4038 
4039 	tgtid_count = le32_to_cpu(ci->count);
4040 
4041 	if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4042 		memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4043 		for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4044 			ids = ci->targetId[ld_index];
4045 			instance->ld_ids[ids] = ci->targetId[ld_index];
4046 		}
4047 
4048 	}
4049 
4050 	pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4051 			    ci, ci_h);
4052 
4053 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4054 		megasas_return_mfi_mpt_pthr(instance, cmd,
4055 			cmd->mpt_pthr_cmd_blocked);
4056 	else
4057 		megasas_return_cmd(instance, cmd);
4058 
4059 	return ret;
4060 }
4061 
4062 /*
4063  * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4064  * instance			 : Controller's instance
4065 */
4066 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4067 {
4068 	struct fusion_context *fusion;
4069 	u32 old_map_sz;
4070 	u32 new_map_sz;
4071 
4072 	fusion = instance->ctrl_context;
4073 	/* For MFI based controllers return dummy success */
4074 	if (!fusion)
4075 		return;
4076 
4077 	instance->supportmax256vd =
4078 		instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4079 	/* Below is additional check to address future FW enhancement */
4080 	if (instance->ctrl_info->max_lds > 64)
4081 		instance->supportmax256vd = 1;
4082 
4083 	instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4084 					* MEGASAS_MAX_DEV_PER_CHANNEL;
4085 	instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4086 					* MEGASAS_MAX_DEV_PER_CHANNEL;
4087 	if (instance->supportmax256vd) {
4088 		instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4089 		instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4090 	} else {
4091 		instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4092 		instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4093 	}
4094 	dev_info(&instance->pdev->dev, "Firmware supports %d VD %d PD\n",
4095 		instance->fw_supported_vd_count,
4096 		instance->fw_supported_pd_count);
4097 	dev_info(&instance->pdev->dev, "Driver supports %d VD  %d PD\n",
4098 		instance->drv_supported_vd_count,
4099 		instance->drv_supported_pd_count);
4100 
4101 	old_map_sz =  sizeof(struct MR_FW_RAID_MAP) +
4102 				(sizeof(struct MR_LD_SPAN_MAP) *
4103 				(instance->fw_supported_vd_count - 1));
4104 	new_map_sz =  sizeof(struct MR_FW_RAID_MAP_EXT);
4105 	fusion->drv_map_sz =  sizeof(struct MR_DRV_RAID_MAP) +
4106 				(sizeof(struct MR_LD_SPAN_MAP) *
4107 				(instance->drv_supported_vd_count - 1));
4108 
4109 	fusion->max_map_sz = max(old_map_sz, new_map_sz);
4110 
4111 
4112 	if (instance->supportmax256vd)
4113 		fusion->current_map_sz = new_map_sz;
4114 	else
4115 		fusion->current_map_sz = old_map_sz;
4116 
4117 }
4118 
4119 /**
4120  * megasas_get_controller_info -	Returns FW's controller structure
4121  * @instance:				Adapter soft state
4122  *
4123  * Issues an internal command (DCMD) to get the FW's controller structure.
4124  * This information is mainly used to find out the maximum IO transfer per
4125  * command supported by the FW.
4126  */
4127 int
4128 megasas_get_ctrl_info(struct megasas_instance *instance)
4129 {
4130 	int ret = 0;
4131 	struct megasas_cmd *cmd;
4132 	struct megasas_dcmd_frame *dcmd;
4133 	struct megasas_ctrl_info *ci;
4134 	struct megasas_ctrl_info *ctrl_info;
4135 	dma_addr_t ci_h = 0;
4136 
4137 	ctrl_info = instance->ctrl_info;
4138 
4139 	cmd = megasas_get_cmd(instance);
4140 
4141 	if (!cmd) {
4142 		printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
4143 		return -ENOMEM;
4144 	}
4145 
4146 	dcmd = &cmd->frame->dcmd;
4147 
4148 	ci = pci_alloc_consistent(instance->pdev,
4149 				  sizeof(struct megasas_ctrl_info), &ci_h);
4150 
4151 	if (!ci) {
4152 		printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
4153 		megasas_return_cmd(instance, cmd);
4154 		return -ENOMEM;
4155 	}
4156 
4157 	memset(ci, 0, sizeof(*ci));
4158 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4159 
4160 	dcmd->cmd = MFI_CMD_DCMD;
4161 	dcmd->cmd_status = 0xFF;
4162 	dcmd->sge_count = 1;
4163 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4164 	dcmd->timeout = 0;
4165 	dcmd->pad_0 = 0;
4166 	dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4167 	dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4168 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4169 	dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4170 	dcmd->mbox.b[0] = 1;
4171 
4172 	if (instance->ctrl_context && !instance->mask_interrupts)
4173 		ret = megasas_issue_blocked_cmd(instance, cmd,
4174 			MEGASAS_BLOCKED_CMD_TIMEOUT);
4175 	else
4176 		ret = megasas_issue_polled(instance, cmd);
4177 
4178 	if (!ret) {
4179 		memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4180 		le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4181 		le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4182 		le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4183 		megasas_update_ext_vd_details(instance);
4184 	}
4185 
4186 	pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4187 			    ci, ci_h);
4188 
4189 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4190 		megasas_return_mfi_mpt_pthr(instance, cmd,
4191 			cmd->mpt_pthr_cmd_blocked);
4192 	else
4193 		megasas_return_cmd(instance, cmd);
4194 	return ret;
4195 }
4196 
4197 /*
4198  * megasas_set_crash_dump_params -	Sends address of crash dump DMA buffer
4199  *					to firmware
4200  *
4201  * @instance:				Adapter soft state
4202  * @crash_buf_state		-	tell FW to turn ON/OFF crash dump feature
4203 					MR_CRASH_BUF_TURN_OFF = 0
4204 					MR_CRASH_BUF_TURN_ON = 1
4205  * @return 0 on success non-zero on failure.
4206  * Issues an internal command (DCMD) to set parameters for crash dump feature.
4207  * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4208  * that driver supports crash dump feature. This DCMD will be sent only if
4209  * crash dump feature is supported by the FW.
4210  *
4211  */
4212 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4213 	u8 crash_buf_state)
4214 {
4215 	int ret = 0;
4216 	struct megasas_cmd *cmd;
4217 	struct megasas_dcmd_frame *dcmd;
4218 
4219 	cmd = megasas_get_cmd(instance);
4220 
4221 	if (!cmd) {
4222 		dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4223 		return -ENOMEM;
4224 	}
4225 
4226 
4227 	dcmd = &cmd->frame->dcmd;
4228 
4229 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4230 	dcmd->mbox.b[0] = crash_buf_state;
4231 	dcmd->cmd = MFI_CMD_DCMD;
4232 	dcmd->cmd_status = 0xFF;
4233 	dcmd->sge_count = 1;
4234 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4235 	dcmd->timeout = 0;
4236 	dcmd->pad_0 = 0;
4237 	dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4238 	dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4239 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4240 	dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4241 
4242 	if (instance->ctrl_context && !instance->mask_interrupts)
4243 		ret = megasas_issue_blocked_cmd(instance, cmd,
4244 			MEGASAS_BLOCKED_CMD_TIMEOUT);
4245 	else
4246 		ret = megasas_issue_polled(instance, cmd);
4247 
4248 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4249 		megasas_return_mfi_mpt_pthr(instance, cmd,
4250 			cmd->mpt_pthr_cmd_blocked);
4251 	else
4252 		megasas_return_cmd(instance, cmd);
4253 	return ret;
4254 }
4255 
4256 /**
4257  * megasas_issue_init_mfi -	Initializes the FW
4258  * @instance:		Adapter soft state
4259  *
4260  * Issues the INIT MFI cmd
4261  */
4262 static int
4263 megasas_issue_init_mfi(struct megasas_instance *instance)
4264 {
4265 	u32 context;
4266 
4267 	struct megasas_cmd *cmd;
4268 
4269 	struct megasas_init_frame *init_frame;
4270 	struct megasas_init_queue_info *initq_info;
4271 	dma_addr_t init_frame_h;
4272 	dma_addr_t initq_info_h;
4273 
4274 	/*
4275 	 * Prepare a init frame. Note the init frame points to queue info
4276 	 * structure. Each frame has SGL allocated after first 64 bytes. For
4277 	 * this frame - since we don't need any SGL - we use SGL's space as
4278 	 * queue info structure
4279 	 *
4280 	 * We will not get a NULL command below. We just created the pool.
4281 	 */
4282 	cmd = megasas_get_cmd(instance);
4283 
4284 	init_frame = (struct megasas_init_frame *)cmd->frame;
4285 	initq_info = (struct megasas_init_queue_info *)
4286 		((unsigned long)init_frame + 64);
4287 
4288 	init_frame_h = cmd->frame_phys_addr;
4289 	initq_info_h = init_frame_h + 64;
4290 
4291 	context = init_frame->context;
4292 	memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4293 	memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4294 	init_frame->context = context;
4295 
4296 	initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4297 	initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4298 
4299 	initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4300 	initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4301 
4302 	init_frame->cmd = MFI_CMD_INIT;
4303 	init_frame->cmd_status = 0xFF;
4304 	init_frame->queue_info_new_phys_addr_lo =
4305 		cpu_to_le32(lower_32_bits(initq_info_h));
4306 	init_frame->queue_info_new_phys_addr_hi =
4307 		cpu_to_le32(upper_32_bits(initq_info_h));
4308 
4309 	init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4310 
4311 	/*
4312 	 * disable the intr before firing the init frame to FW
4313 	 */
4314 	instance->instancet->disable_intr(instance);
4315 
4316 	/*
4317 	 * Issue the init frame in polled mode
4318 	 */
4319 
4320 	if (megasas_issue_polled(instance, cmd)) {
4321 		printk(KERN_ERR "megasas: Failed to init firmware\n");
4322 		megasas_return_cmd(instance, cmd);
4323 		goto fail_fw_init;
4324 	}
4325 
4326 	megasas_return_cmd(instance, cmd);
4327 
4328 	return 0;
4329 
4330 fail_fw_init:
4331 	return -EINVAL;
4332 }
4333 
4334 static u32
4335 megasas_init_adapter_mfi(struct megasas_instance *instance)
4336 {
4337 	struct megasas_register_set __iomem *reg_set;
4338 	u32 context_sz;
4339 	u32 reply_q_sz;
4340 
4341 	reg_set = instance->reg_set;
4342 
4343 	/*
4344 	 * Get various operational parameters from status register
4345 	 */
4346 	instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4347 	/*
4348 	 * Reduce the max supported cmds by 1. This is to ensure that the
4349 	 * reply_q_sz (1 more than the max cmd that driver may send)
4350 	 * does not exceed max cmds that the FW can support
4351 	 */
4352 	instance->max_fw_cmds = instance->max_fw_cmds-1;
4353 	instance->max_mfi_cmds = instance->max_fw_cmds;
4354 	instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4355 					0x10;
4356 	/*
4357 	 * Create a pool of commands
4358 	 */
4359 	if (megasas_alloc_cmds(instance))
4360 		goto fail_alloc_cmds;
4361 
4362 	/*
4363 	 * Allocate memory for reply queue. Length of reply queue should
4364 	 * be _one_ more than the maximum commands handled by the firmware.
4365 	 *
4366 	 * Note: When FW completes commands, it places corresponding contex
4367 	 * values in this circular reply queue. This circular queue is a fairly
4368 	 * typical producer-consumer queue. FW is the producer (of completed
4369 	 * commands) and the driver is the consumer.
4370 	 */
4371 	context_sz = sizeof(u32);
4372 	reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4373 
4374 	instance->reply_queue = pci_alloc_consistent(instance->pdev,
4375 						     reply_q_sz,
4376 						     &instance->reply_queue_h);
4377 
4378 	if (!instance->reply_queue) {
4379 		printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
4380 		goto fail_reply_queue;
4381 	}
4382 
4383 	if (megasas_issue_init_mfi(instance))
4384 		goto fail_fw_init;
4385 
4386 	if (megasas_get_ctrl_info(instance)) {
4387 		dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4388 			"Fail from %s %d\n", instance->unique_id,
4389 			__func__, __LINE__);
4390 		goto fail_fw_init;
4391 	}
4392 
4393 	instance->fw_support_ieee = 0;
4394 	instance->fw_support_ieee =
4395 		(instance->instancet->read_fw_status_reg(reg_set) &
4396 		0x04000000);
4397 
4398 	printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
4399 			instance->fw_support_ieee);
4400 
4401 	if (instance->fw_support_ieee)
4402 		instance->flag_ieee = 1;
4403 
4404 	return 0;
4405 
4406 fail_fw_init:
4407 
4408 	pci_free_consistent(instance->pdev, reply_q_sz,
4409 			    instance->reply_queue, instance->reply_queue_h);
4410 fail_reply_queue:
4411 	megasas_free_cmds(instance);
4412 
4413 fail_alloc_cmds:
4414 	return 1;
4415 }
4416 
4417 /**
4418  * megasas_init_fw -	Initializes the FW
4419  * @instance:		Adapter soft state
4420  *
4421  * This is the main function for initializing firmware
4422  */
4423 
4424 static int megasas_init_fw(struct megasas_instance *instance)
4425 {
4426 	u32 max_sectors_1;
4427 	u32 max_sectors_2;
4428 	u32 tmp_sectors, msix_enable, scratch_pad_2;
4429 	resource_size_t base_addr;
4430 	struct megasas_register_set __iomem *reg_set;
4431 	struct megasas_ctrl_info *ctrl_info = NULL;
4432 	unsigned long bar_list;
4433 	int i, loop, fw_msix_count = 0;
4434 	struct IOV_111 *iovPtr;
4435 
4436 	/* Find first memory bar */
4437 	bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4438 	instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4439 	if (pci_request_selected_regions(instance->pdev, instance->bar,
4440 					 "megasas: LSI")) {
4441 		printk(KERN_DEBUG "megasas: IO memory region busy!\n");
4442 		return -EBUSY;
4443 	}
4444 
4445 	base_addr = pci_resource_start(instance->pdev, instance->bar);
4446 	instance->reg_set = ioremap_nocache(base_addr, 8192);
4447 
4448 	if (!instance->reg_set) {
4449 		printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
4450 		goto fail_ioremap;
4451 	}
4452 
4453 	reg_set = instance->reg_set;
4454 
4455 	switch (instance->pdev->device) {
4456 	case PCI_DEVICE_ID_LSI_FUSION:
4457 	case PCI_DEVICE_ID_LSI_PLASMA:
4458 	case PCI_DEVICE_ID_LSI_INVADER:
4459 	case PCI_DEVICE_ID_LSI_FURY:
4460 		instance->instancet = &megasas_instance_template_fusion;
4461 		break;
4462 	case PCI_DEVICE_ID_LSI_SAS1078R:
4463 	case PCI_DEVICE_ID_LSI_SAS1078DE:
4464 		instance->instancet = &megasas_instance_template_ppc;
4465 		break;
4466 	case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4467 	case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4468 		instance->instancet = &megasas_instance_template_gen2;
4469 		break;
4470 	case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4471 	case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4472 		instance->instancet = &megasas_instance_template_skinny;
4473 		break;
4474 	case PCI_DEVICE_ID_LSI_SAS1064R:
4475 	case PCI_DEVICE_ID_DELL_PERC5:
4476 	default:
4477 		instance->instancet = &megasas_instance_template_xscale;
4478 		break;
4479 	}
4480 
4481 	if (megasas_transition_to_ready(instance, 0)) {
4482 		atomic_set(&instance->fw_reset_no_pci_access, 1);
4483 		instance->instancet->adp_reset
4484 			(instance, instance->reg_set);
4485 		atomic_set(&instance->fw_reset_no_pci_access, 0);
4486 		dev_info(&instance->pdev->dev,
4487 			"megasas: FW restarted successfully from %s!\n",
4488 			__func__);
4489 
4490 		/*waitting for about 30 second before retry*/
4491 		ssleep(30);
4492 
4493 		if (megasas_transition_to_ready(instance, 0))
4494 			goto fail_ready_state;
4495 	}
4496 
4497 	/*
4498 	 * MSI-X host index 0 is common for all adapter.
4499 	 * It is used for all MPT based Adapters.
4500 	 */
4501 	instance->reply_post_host_index_addr[0] =
4502 		(u32 *)((u8 *)instance->reg_set +
4503 		MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4504 
4505 	/* Check if MSI-X is supported while in ready state */
4506 	msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4507 		       0x4000000) >> 0x1a;
4508 	if (msix_enable && !msix_disable) {
4509 		scratch_pad_2 = readl
4510 			(&instance->reg_set->outbound_scratch_pad_2);
4511 		/* Check max MSI-X vectors */
4512 		if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4513 		    (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
4514 			instance->msix_vectors = (scratch_pad_2
4515 				& MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4516 			fw_msix_count = instance->msix_vectors;
4517 			if (msix_vectors)
4518 				instance->msix_vectors =
4519 					min(msix_vectors,
4520 					    instance->msix_vectors);
4521 		} else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4522 			|| (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4523 			/* Invader/Fury supports more than 8 MSI-X */
4524 			instance->msix_vectors = ((scratch_pad_2
4525 				& MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4526 				>> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4527 			fw_msix_count = instance->msix_vectors;
4528 			/* Save 1-15 reply post index address to local memory
4529 			 * Index 0 is already saved from reg offset
4530 			 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4531 			 */
4532 			for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4533 				instance->reply_post_host_index_addr[loop] =
4534 					(u32 *)((u8 *)instance->reg_set +
4535 					MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4536 					+ (loop * 0x10));
4537 			}
4538 			if (msix_vectors)
4539 				instance->msix_vectors = min(msix_vectors,
4540 					instance->msix_vectors);
4541 		} else
4542 			instance->msix_vectors = 1;
4543 		/* Don't bother allocating more MSI-X vectors than cpus */
4544 		instance->msix_vectors = min(instance->msix_vectors,
4545 					     (unsigned int)num_online_cpus());
4546 		for (i = 0; i < instance->msix_vectors; i++)
4547 			instance->msixentry[i].entry = i;
4548 		i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4549 					  1, instance->msix_vectors);
4550 		if (i > 0)
4551 			instance->msix_vectors = i;
4552 		else
4553 			instance->msix_vectors = 0;
4554 
4555 		dev_info(&instance->pdev->dev, "[scsi%d]: FW supports"
4556 			"<%d> MSIX vector,Online CPUs: <%d>,"
4557 			"Current MSIX <%d>\n", instance->host->host_no,
4558 			fw_msix_count, (unsigned int)num_online_cpus(),
4559 			instance->msix_vectors);
4560 	}
4561 
4562 	instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4563 				GFP_KERNEL);
4564 	if (instance->ctrl_info == NULL)
4565 		goto fail_init_adapter;
4566 
4567 	/*
4568 	 * Below are default value for legacy Firmware.
4569 	 * non-fusion based controllers
4570 	 */
4571 	instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4572 	instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4573 	/* Get operational params, sge flags, send init cmd to controller */
4574 	if (instance->instancet->init_adapter(instance))
4575 		goto fail_init_adapter;
4576 
4577 	printk(KERN_ERR "megasas: INIT adapter done\n");
4578 
4579 	/** for passthrough
4580 	* the following function will get the PD LIST.
4581 	*/
4582 
4583 	memset(instance->pd_list, 0 ,
4584 		(MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4585 	if (megasas_get_pd_list(instance) < 0) {
4586 		printk(KERN_ERR "megasas: failed to get PD list\n");
4587 		goto fail_init_adapter;
4588 	}
4589 
4590 	memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4591 	if (megasas_ld_list_query(instance,
4592 				  MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4593 		megasas_get_ld_list(instance);
4594 
4595 	/*
4596 	 * Compute the max allowed sectors per IO: The controller info has two
4597 	 * limits on max sectors. Driver should use the minimum of these two.
4598 	 *
4599 	 * 1 << stripe_sz_ops.min = max sectors per strip
4600 	 *
4601 	 * Note that older firmwares ( < FW ver 30) didn't report information
4602 	 * to calculate max_sectors_1. So the number ended up as zero always.
4603 	 */
4604 	tmp_sectors = 0;
4605 	ctrl_info = instance->ctrl_info;
4606 
4607 	max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4608 		le16_to_cpu(ctrl_info->max_strips_per_io);
4609 	max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4610 
4611 	tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
4612 
4613 	/*Check whether controller is iMR or MR */
4614 	if (ctrl_info->memory_size) {
4615 		instance->is_imr = 0;
4616 		dev_info(&instance->pdev->dev, "Controller type: MR,"
4617 			"Memory size is: %dMB\n",
4618 			le16_to_cpu(ctrl_info->memory_size));
4619 	} else {
4620 		instance->is_imr = 1;
4621 		dev_info(&instance->pdev->dev,
4622 			"Controller type: iMR\n");
4623 	}
4624 	instance->disableOnlineCtrlReset =
4625 	ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4626 	instance->mpio = ctrl_info->adapterOperations2.mpio;
4627 	instance->UnevenSpanSupport =
4628 		ctrl_info->adapterOperations2.supportUnevenSpans;
4629 	if (instance->UnevenSpanSupport) {
4630 		struct fusion_context *fusion = instance->ctrl_context;
4631 
4632 		dev_info(&instance->pdev->dev, "FW supports: "
4633 		"UnevenSpanSupport=%x\n", instance->UnevenSpanSupport);
4634 		if (MR_ValidateMapInfo(instance))
4635 			fusion->fast_path_io = 1;
4636 		else
4637 			fusion->fast_path_io = 0;
4638 
4639 	}
4640 	if (ctrl_info->host_interface.SRIOV) {
4641 		if (!ctrl_info->adapterOperations2.activePassive)
4642 			instance->PlasmaFW111 = 1;
4643 
4644 		if (!instance->PlasmaFW111)
4645 			instance->requestorId =
4646 				ctrl_info->iov.requestorId;
4647 		else {
4648 			iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4649 			instance->requestorId = iovPtr->requestorId;
4650 		}
4651 		dev_warn(&instance->pdev->dev, "I am VF "
4652 		       "requestorId %d\n", instance->requestorId);
4653 	}
4654 
4655 	instance->crash_dump_fw_support =
4656 		ctrl_info->adapterOperations3.supportCrashDump;
4657 	instance->crash_dump_drv_support =
4658 		(instance->crash_dump_fw_support &&
4659 		instance->crash_dump_buf);
4660 	if (instance->crash_dump_drv_support) {
4661 		dev_info(&instance->pdev->dev, "Firmware Crash dump "
4662 			"feature is supported\n");
4663 		megasas_set_crash_dump_params(instance,
4664 			MR_CRASH_BUF_TURN_OFF);
4665 
4666 	} else {
4667 		if (instance->crash_dump_buf)
4668 			pci_free_consistent(instance->pdev,
4669 				CRASH_DMA_BUF_SIZE,
4670 				instance->crash_dump_buf,
4671 				instance->crash_dump_h);
4672 		instance->crash_dump_buf = NULL;
4673 	}
4674 
4675 	instance->secure_jbod_support =
4676 		ctrl_info->adapterOperations3.supportSecurityonJBOD;
4677 	if (instance->secure_jbod_support)
4678 		dev_info(&instance->pdev->dev, "Firmware supports Secure JBOD\n");
4679 	instance->max_sectors_per_req = instance->max_num_sge *
4680 						PAGE_SIZE / 512;
4681 	if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4682 		instance->max_sectors_per_req = tmp_sectors;
4683 
4684 	/*
4685 	 * 1. For fusion adapters, 3 commands for IOCTL and 5 commands
4686 	 *    for driver's internal DCMDs.
4687 	 * 2. For MFI skinny adapters, 5 commands for IOCTL + driver's
4688 	 *    internal DCMDs.
4689 	 * 3. For rest of MFI adapters, 27 commands reserved for IOCTLs
4690 	 *    and 5 commands for drivers's internal DCMD.
4691 	 */
4692 	if (instance->ctrl_context) {
4693 		instance->max_scsi_cmds = instance->max_fw_cmds -
4694 					(MEGASAS_FUSION_INTERNAL_CMDS +
4695 					MEGASAS_FUSION_IOCTL_CMDS);
4696 		sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
4697 	} else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4698 		(instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4699 		instance->max_scsi_cmds = instance->max_fw_cmds -
4700 						MEGASAS_SKINNY_INT_CMDS;
4701 		sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4702 	} else {
4703 		instance->max_scsi_cmds = instance->max_fw_cmds -
4704 						MEGASAS_INT_CMDS;
4705 		sema_init(&instance->ioctl_sem, (MEGASAS_INT_CMDS - 5));
4706 	}
4707 
4708 	/* Check for valid throttlequeuedepth module parameter */
4709 	if (throttlequeuedepth &&
4710 			throttlequeuedepth <= instance->max_scsi_cmds)
4711 		instance->throttlequeuedepth = throttlequeuedepth;
4712 	else
4713 		instance->throttlequeuedepth =
4714 				MEGASAS_THROTTLE_QUEUE_DEPTH;
4715 
4716         /*
4717 	* Setup tasklet for cmd completion
4718 	*/
4719 
4720 	tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4721 		(unsigned long)instance);
4722 
4723 	/* Launch SR-IOV heartbeat timer */
4724 	if (instance->requestorId) {
4725 		if (!megasas_sriov_start_heartbeat(instance, 1))
4726 			megasas_start_timer(instance,
4727 					    &instance->sriov_heartbeat_timer,
4728 					    megasas_sriov_heartbeat_handler,
4729 					    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4730 		else
4731 			instance->skip_heartbeat_timer_del = 1;
4732 	}
4733 
4734 	return 0;
4735 
4736 fail_init_adapter:
4737 fail_ready_state:
4738 	kfree(instance->ctrl_info);
4739 	instance->ctrl_info = NULL;
4740 	iounmap(instance->reg_set);
4741 
4742       fail_ioremap:
4743 	pci_release_selected_regions(instance->pdev, instance->bar);
4744 
4745 	return -EINVAL;
4746 }
4747 
4748 /**
4749  * megasas_release_mfi -	Reverses the FW initialization
4750  * @intance:			Adapter soft state
4751  */
4752 static void megasas_release_mfi(struct megasas_instance *instance)
4753 {
4754 	u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4755 
4756 	if (instance->reply_queue)
4757 		pci_free_consistent(instance->pdev, reply_q_sz,
4758 			    instance->reply_queue, instance->reply_queue_h);
4759 
4760 	megasas_free_cmds(instance);
4761 
4762 	iounmap(instance->reg_set);
4763 
4764 	pci_release_selected_regions(instance->pdev, instance->bar);
4765 }
4766 
4767 /**
4768  * megasas_get_seq_num -	Gets latest event sequence numbers
4769  * @instance:			Adapter soft state
4770  * @eli:			FW event log sequence numbers information
4771  *
4772  * FW maintains a log of all events in a non-volatile area. Upper layers would
4773  * usually find out the latest sequence number of the events, the seq number at
4774  * the boot etc. They would "read" all the events below the latest seq number
4775  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4776  * number), they would subsribe to AEN (asynchronous event notification) and
4777  * wait for the events to happen.
4778  */
4779 static int
4780 megasas_get_seq_num(struct megasas_instance *instance,
4781 		    struct megasas_evt_log_info *eli)
4782 {
4783 	struct megasas_cmd *cmd;
4784 	struct megasas_dcmd_frame *dcmd;
4785 	struct megasas_evt_log_info *el_info;
4786 	dma_addr_t el_info_h = 0;
4787 
4788 	cmd = megasas_get_cmd(instance);
4789 
4790 	if (!cmd) {
4791 		return -ENOMEM;
4792 	}
4793 
4794 	dcmd = &cmd->frame->dcmd;
4795 	el_info = pci_alloc_consistent(instance->pdev,
4796 				       sizeof(struct megasas_evt_log_info),
4797 				       &el_info_h);
4798 
4799 	if (!el_info) {
4800 		megasas_return_cmd(instance, cmd);
4801 		return -ENOMEM;
4802 	}
4803 
4804 	memset(el_info, 0, sizeof(*el_info));
4805 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4806 
4807 	dcmd->cmd = MFI_CMD_DCMD;
4808 	dcmd->cmd_status = 0x0;
4809 	dcmd->sge_count = 1;
4810 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4811 	dcmd->timeout = 0;
4812 	dcmd->pad_0 = 0;
4813 	dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4814 	dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4815 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4816 	dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4817 
4818 	if (megasas_issue_blocked_cmd(instance, cmd, 30))
4819 		dev_err(&instance->pdev->dev, "Command timedout"
4820 			"from %s\n", __func__);
4821 	else {
4822 		/*
4823 		 * Copy the data back into callers buffer
4824 		 */
4825 		eli->newest_seq_num = le32_to_cpu(el_info->newest_seq_num);
4826 		eli->oldest_seq_num = le32_to_cpu(el_info->oldest_seq_num);
4827 		eli->clear_seq_num = le32_to_cpu(el_info->clear_seq_num);
4828 		eli->shutdown_seq_num = le32_to_cpu(el_info->shutdown_seq_num);
4829 		eli->boot_seq_num = le32_to_cpu(el_info->boot_seq_num);
4830 	}
4831 
4832 	pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4833 			    el_info, el_info_h);
4834 
4835 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
4836 		megasas_return_mfi_mpt_pthr(instance, cmd,
4837 			cmd->mpt_pthr_cmd_blocked);
4838 	else
4839 		megasas_return_cmd(instance, cmd);
4840 
4841 	return 0;
4842 }
4843 
4844 /**
4845  * megasas_register_aen -	Registers for asynchronous event notification
4846  * @instance:			Adapter soft state
4847  * @seq_num:			The starting sequence number
4848  * @class_locale:		Class of the event
4849  *
4850  * This function subscribes for AEN for events beyond the @seq_num. It requests
4851  * to be notified if and only if the event is of type @class_locale
4852  */
4853 static int
4854 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4855 		     u32 class_locale_word)
4856 {
4857 	int ret_val;
4858 	struct megasas_cmd *cmd;
4859 	struct megasas_dcmd_frame *dcmd;
4860 	union megasas_evt_class_locale curr_aen;
4861 	union megasas_evt_class_locale prev_aen;
4862 
4863 	/*
4864 	 * If there an AEN pending already (aen_cmd), check if the
4865 	 * class_locale of that pending AEN is inclusive of the new
4866 	 * AEN request we currently have. If it is, then we don't have
4867 	 * to do anything. In other words, whichever events the current
4868 	 * AEN request is subscribing to, have already been subscribed
4869 	 * to.
4870 	 *
4871 	 * If the old_cmd is _not_ inclusive, then we have to abort
4872 	 * that command, form a class_locale that is superset of both
4873 	 * old and current and re-issue to the FW
4874 	 */
4875 
4876 	curr_aen.word = class_locale_word;
4877 
4878 	if (instance->aen_cmd) {
4879 
4880 		prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
4881 		prev_aen.members.locale = le16_to_cpu(prev_aen.members.locale);
4882 
4883 		/*
4884 		 * A class whose enum value is smaller is inclusive of all
4885 		 * higher values. If a PROGRESS (= -1) was previously
4886 		 * registered, then a new registration requests for higher
4887 		 * classes need not be sent to FW. They are automatically
4888 		 * included.
4889 		 *
4890 		 * Locale numbers don't have such hierarchy. They are bitmap
4891 		 * values
4892 		 */
4893 		if ((prev_aen.members.class <= curr_aen.members.class) &&
4894 		    !((prev_aen.members.locale & curr_aen.members.locale) ^
4895 		      curr_aen.members.locale)) {
4896 			/*
4897 			 * Previously issued event registration includes
4898 			 * current request. Nothing to do.
4899 			 */
4900 			return 0;
4901 		} else {
4902 			curr_aen.members.locale |= prev_aen.members.locale;
4903 
4904 			if (prev_aen.members.class < curr_aen.members.class)
4905 				curr_aen.members.class = prev_aen.members.class;
4906 
4907 			instance->aen_cmd->abort_aen = 1;
4908 			ret_val = megasas_issue_blocked_abort_cmd(instance,
4909 								  instance->
4910 								  aen_cmd, 30);
4911 
4912 			if (ret_val) {
4913 				printk(KERN_DEBUG "megasas: Failed to abort "
4914 				       "previous AEN command\n");
4915 				return ret_val;
4916 			}
4917 		}
4918 	}
4919 
4920 	cmd = megasas_get_cmd(instance);
4921 
4922 	if (!cmd)
4923 		return -ENOMEM;
4924 
4925 	dcmd = &cmd->frame->dcmd;
4926 
4927 	memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4928 
4929 	/*
4930 	 * Prepare DCMD for aen registration
4931 	 */
4932 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4933 
4934 	dcmd->cmd = MFI_CMD_DCMD;
4935 	dcmd->cmd_status = 0x0;
4936 	dcmd->sge_count = 1;
4937 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4938 	dcmd->timeout = 0;
4939 	dcmd->pad_0 = 0;
4940 	dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4941 	dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4942 	dcmd->mbox.w[0] = cpu_to_le32(seq_num);
4943 	instance->last_seq_num = seq_num;
4944 	dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4945 	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4946 	dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
4947 
4948 	if (instance->aen_cmd != NULL) {
4949 		megasas_return_cmd(instance, cmd);
4950 		return 0;
4951 	}
4952 
4953 	/*
4954 	 * Store reference to the cmd used to register for AEN. When an
4955 	 * application wants us to register for AEN, we have to abort this
4956 	 * cmd and re-register with a new EVENT LOCALE supplied by that app
4957 	 */
4958 	instance->aen_cmd = cmd;
4959 
4960 	/*
4961 	 * Issue the aen registration frame
4962 	 */
4963 	instance->instancet->issue_dcmd(instance, cmd);
4964 
4965 	return 0;
4966 }
4967 
4968 /**
4969  * megasas_start_aen -	Subscribes to AEN during driver load time
4970  * @instance:		Adapter soft state
4971  */
4972 static int megasas_start_aen(struct megasas_instance *instance)
4973 {
4974 	struct megasas_evt_log_info eli;
4975 	union megasas_evt_class_locale class_locale;
4976 
4977 	/*
4978 	 * Get the latest sequence number from FW
4979 	 */
4980 	memset(&eli, 0, sizeof(eli));
4981 
4982 	if (megasas_get_seq_num(instance, &eli))
4983 		return -1;
4984 
4985 	/*
4986 	 * Register AEN with FW for latest sequence number plus 1
4987 	 */
4988 	class_locale.members.reserved = 0;
4989 	class_locale.members.locale = MR_EVT_LOCALE_ALL;
4990 	class_locale.members.class = MR_EVT_CLASS_DEBUG;
4991 
4992 	return megasas_register_aen(instance,
4993 			eli.newest_seq_num + 1,
4994 			class_locale.word);
4995 }
4996 
4997 /**
4998  * megasas_io_attach -	Attaches this driver to SCSI mid-layer
4999  * @instance:		Adapter soft state
5000  */
5001 static int megasas_io_attach(struct megasas_instance *instance)
5002 {
5003 	struct Scsi_Host *host = instance->host;
5004 
5005 	/*
5006 	 * Export parameters required by SCSI mid-layer
5007 	 */
5008 	host->irq = instance->pdev->irq;
5009 	host->unique_id = instance->unique_id;
5010 	host->can_queue = instance->max_scsi_cmds;
5011 	host->this_id = instance->init_id;
5012 	host->sg_tablesize = instance->max_num_sge;
5013 
5014 	if (instance->fw_support_ieee)
5015 		instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5016 
5017 	/*
5018 	 * Check if the module parameter value for max_sectors can be used
5019 	 */
5020 	if (max_sectors && max_sectors < instance->max_sectors_per_req)
5021 		instance->max_sectors_per_req = max_sectors;
5022 	else {
5023 		if (max_sectors) {
5024 			if (((instance->pdev->device ==
5025 				PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5026 				(instance->pdev->device ==
5027 				PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5028 				(max_sectors <= MEGASAS_MAX_SECTORS)) {
5029 				instance->max_sectors_per_req = max_sectors;
5030 			} else {
5031 			printk(KERN_INFO "megasas: max_sectors should be > 0"
5032 				"and <= %d (or < 1MB for GEN2 controller)\n",
5033 				instance->max_sectors_per_req);
5034 			}
5035 		}
5036 	}
5037 
5038 	host->max_sectors = instance->max_sectors_per_req;
5039 	host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5040 	host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5041 	host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5042 	host->max_lun = MEGASAS_MAX_LUN;
5043 	host->max_cmd_len = 16;
5044 
5045 	/* Fusion only supports host reset */
5046 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5047 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5048 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5049 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5050 		host->hostt->eh_device_reset_handler = NULL;
5051 		host->hostt->eh_bus_reset_handler = NULL;
5052 	}
5053 
5054 	/*
5055 	 * Notify the mid-layer about the new controller
5056 	 */
5057 	if (scsi_add_host(host, &instance->pdev->dev)) {
5058 		printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
5059 		return -ENODEV;
5060 	}
5061 
5062 	return 0;
5063 }
5064 
5065 static int
5066 megasas_set_dma_mask(struct pci_dev *pdev)
5067 {
5068 	/*
5069 	 * All our contollers are capable of performing 64-bit DMA
5070 	 */
5071 	if (IS_DMA64) {
5072 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5073 
5074 			if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5075 				goto fail_set_dma_mask;
5076 		}
5077 	} else {
5078 		if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5079 			goto fail_set_dma_mask;
5080 	}
5081 	/*
5082 	 * Ensure that all data structures are allocated in 32-bit
5083 	 * memory.
5084 	 */
5085 	if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5086 		/* Try 32bit DMA mask and 32 bit Consistent dma mask */
5087 		if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5088 			&& !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5089 			dev_info(&pdev->dev, "set 32bit DMA mask"
5090 				"and 32 bit consistent mask\n");
5091 		else
5092 			goto fail_set_dma_mask;
5093 	}
5094 
5095 	return 0;
5096 
5097 fail_set_dma_mask:
5098 	return 1;
5099 }
5100 
5101 /**
5102  * megasas_probe_one -	PCI hotplug entry point
5103  * @pdev:		PCI device structure
5104  * @id:			PCI ids of supported hotplugged adapter
5105  */
5106 static int megasas_probe_one(struct pci_dev *pdev,
5107 			     const struct pci_device_id *id)
5108 {
5109 	int rval, pos, i, j, cpu;
5110 	struct Scsi_Host *host;
5111 	struct megasas_instance *instance;
5112 	u16 control = 0;
5113 	struct fusion_context *fusion = NULL;
5114 
5115 	/* Reset MSI-X in the kdump kernel */
5116 	if (reset_devices) {
5117 		pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5118 		if (pos) {
5119 			pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5120 					     &control);
5121 			if (control & PCI_MSIX_FLAGS_ENABLE) {
5122 				dev_info(&pdev->dev, "resetting MSI-X\n");
5123 				pci_write_config_word(pdev,
5124 						      pos + PCI_MSIX_FLAGS,
5125 						      control &
5126 						      ~PCI_MSIX_FLAGS_ENABLE);
5127 			}
5128 		}
5129 	}
5130 
5131 	/*
5132 	 * Announce PCI information
5133 	 */
5134 	printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
5135 	       pdev->vendor, pdev->device, pdev->subsystem_vendor,
5136 	       pdev->subsystem_device);
5137 
5138 	printk("bus %d:slot %d:func %d\n",
5139 	       pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
5140 
5141 	/*
5142 	 * PCI prepping: enable device set bus mastering and dma mask
5143 	 */
5144 	rval = pci_enable_device_mem(pdev);
5145 
5146 	if (rval) {
5147 		return rval;
5148 	}
5149 
5150 	pci_set_master(pdev);
5151 
5152 	if (megasas_set_dma_mask(pdev))
5153 		goto fail_set_dma_mask;
5154 
5155 	host = scsi_host_alloc(&megasas_template,
5156 			       sizeof(struct megasas_instance));
5157 
5158 	if (!host) {
5159 		printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
5160 		goto fail_alloc_instance;
5161 	}
5162 
5163 	instance = (struct megasas_instance *)host->hostdata;
5164 	memset(instance, 0, sizeof(*instance));
5165 	atomic_set( &instance->fw_reset_no_pci_access, 0 );
5166 	instance->pdev = pdev;
5167 
5168 	switch (instance->pdev->device) {
5169 	case PCI_DEVICE_ID_LSI_FUSION:
5170 	case PCI_DEVICE_ID_LSI_PLASMA:
5171 	case PCI_DEVICE_ID_LSI_INVADER:
5172 	case PCI_DEVICE_ID_LSI_FURY:
5173 	{
5174 		instance->ctrl_context_pages =
5175 			get_order(sizeof(struct fusion_context));
5176 		instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5177 				instance->ctrl_context_pages);
5178 		if (!instance->ctrl_context) {
5179 			printk(KERN_DEBUG "megasas: Failed to allocate "
5180 			       "memory for Fusion context info\n");
5181 			goto fail_alloc_dma_buf;
5182 		}
5183 		fusion = instance->ctrl_context;
5184 		memset(fusion, 0,
5185 			((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5186 		INIT_LIST_HEAD(&fusion->cmd_pool);
5187 		spin_lock_init(&fusion->mpt_pool_lock);
5188 	}
5189 	break;
5190 	default: /* For all other supported controllers */
5191 
5192 		instance->producer =
5193 			pci_alloc_consistent(pdev, sizeof(u32),
5194 					     &instance->producer_h);
5195 		instance->consumer =
5196 			pci_alloc_consistent(pdev, sizeof(u32),
5197 					     &instance->consumer_h);
5198 
5199 		if (!instance->producer || !instance->consumer) {
5200 			printk(KERN_DEBUG "megasas: Failed to allocate"
5201 			       "memory for producer, consumer\n");
5202 			goto fail_alloc_dma_buf;
5203 		}
5204 
5205 		*instance->producer = 0;
5206 		*instance->consumer = 0;
5207 		break;
5208 	}
5209 
5210 	/* Crash dump feature related initialisation*/
5211 	instance->drv_buf_index = 0;
5212 	instance->drv_buf_alloc = 0;
5213 	instance->crash_dump_fw_support = 0;
5214 	instance->crash_dump_app_support = 0;
5215 	instance->fw_crash_state = UNAVAILABLE;
5216 	spin_lock_init(&instance->crashdump_lock);
5217 	instance->crash_dump_buf = NULL;
5218 
5219 	if (!reset_devices)
5220 		instance->crash_dump_buf = pci_alloc_consistent(pdev,
5221 						CRASH_DMA_BUF_SIZE,
5222 						&instance->crash_dump_h);
5223 	if (!instance->crash_dump_buf)
5224 		dev_err(&instance->pdev->dev, "Can't allocate Firmware "
5225 			"crash dump DMA buffer\n");
5226 
5227 	megasas_poll_wait_aen = 0;
5228 	instance->flag_ieee = 0;
5229 	instance->ev = NULL;
5230 	instance->issuepend_done = 1;
5231 	instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5232 	instance->is_imr = 0;
5233 
5234 	instance->evt_detail = pci_alloc_consistent(pdev,
5235 						    sizeof(struct
5236 							   megasas_evt_detail),
5237 						    &instance->evt_detail_h);
5238 
5239 	if (!instance->evt_detail) {
5240 		printk(KERN_DEBUG "megasas: Failed to allocate memory for "
5241 		       "event detail structure\n");
5242 		goto fail_alloc_dma_buf;
5243 	}
5244 
5245 	/*
5246 	 * Initialize locks and queues
5247 	 */
5248 	INIT_LIST_HEAD(&instance->cmd_pool);
5249 	INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5250 
5251 	atomic_set(&instance->fw_outstanding,0);
5252 
5253 	init_waitqueue_head(&instance->int_cmd_wait_q);
5254 	init_waitqueue_head(&instance->abort_cmd_wait_q);
5255 
5256 	spin_lock_init(&instance->mfi_pool_lock);
5257 	spin_lock_init(&instance->hba_lock);
5258 	spin_lock_init(&instance->completion_lock);
5259 
5260 	mutex_init(&instance->aen_mutex);
5261 	mutex_init(&instance->reset_mutex);
5262 
5263 	/*
5264 	 * Initialize PCI related and misc parameters
5265 	 */
5266 	instance->host = host;
5267 	instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5268 	instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5269 	instance->ctrl_info = NULL;
5270 
5271 
5272 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5273 		(instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5274 		instance->flag_ieee = 1;
5275 
5276 	megasas_dbg_lvl = 0;
5277 	instance->flag = 0;
5278 	instance->unload = 1;
5279 	instance->last_time = 0;
5280 	instance->disableOnlineCtrlReset = 1;
5281 	instance->UnevenSpanSupport = 0;
5282 
5283 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5284 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5285 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5286 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5287 		INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5288 		INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5289 	} else
5290 		INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5291 
5292 	/*
5293 	 * Initialize MFI Firmware
5294 	 */
5295 	if (megasas_init_fw(instance))
5296 		goto fail_init_mfi;
5297 
5298 	if (instance->requestorId) {
5299 		if (instance->PlasmaFW111) {
5300 			instance->vf_affiliation_111 =
5301 				pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5302 						     &instance->vf_affiliation_111_h);
5303 			if (!instance->vf_affiliation_111)
5304 				printk(KERN_WARNING "megasas: Can't allocate "
5305 				       "memory for VF affiliation buffer\n");
5306 		} else {
5307 			instance->vf_affiliation =
5308 				pci_alloc_consistent(pdev,
5309 						     (MAX_LOGICAL_DRIVES + 1) *
5310 						     sizeof(struct MR_LD_VF_AFFILIATION),
5311 						     &instance->vf_affiliation_h);
5312 			if (!instance->vf_affiliation)
5313 				printk(KERN_WARNING "megasas: Can't allocate "
5314 				       "memory for VF affiliation buffer\n");
5315 		}
5316 	}
5317 
5318 retry_irq_register:
5319 	/*
5320 	 * Register IRQ
5321 	 */
5322 	if (instance->msix_vectors) {
5323 		cpu = cpumask_first(cpu_online_mask);
5324 		for (i = 0; i < instance->msix_vectors; i++) {
5325 			instance->irq_context[i].instance = instance;
5326 			instance->irq_context[i].MSIxIndex = i;
5327 			if (request_irq(instance->msixentry[i].vector,
5328 					instance->instancet->service_isr, 0,
5329 					"megasas",
5330 					&instance->irq_context[i])) {
5331 				printk(KERN_DEBUG "megasas: Failed to "
5332 				       "register IRQ for vector %d.\n", i);
5333 				for (j = 0; j < i; j++) {
5334 					if (smp_affinity_enable)
5335 						irq_set_affinity_hint(
5336 							instance->msixentry[j].vector, NULL);
5337 					free_irq(
5338 						instance->msixentry[j].vector,
5339 						&instance->irq_context[j]);
5340 				}
5341 				/* Retry irq register for IO_APIC */
5342 				instance->msix_vectors = 0;
5343 				goto retry_irq_register;
5344 			}
5345 			if (smp_affinity_enable) {
5346 				if (irq_set_affinity_hint(instance->msixentry[i].vector,
5347 					get_cpu_mask(cpu)))
5348 					dev_err(&instance->pdev->dev,
5349 						"Error setting affinity hint "
5350 						"for cpu %d\n", cpu);
5351 				cpu = cpumask_next(cpu, cpu_online_mask);
5352 			}
5353 		}
5354 	} else {
5355 		instance->irq_context[0].instance = instance;
5356 		instance->irq_context[0].MSIxIndex = 0;
5357 		if (request_irq(pdev->irq, instance->instancet->service_isr,
5358 				IRQF_SHARED, "megasas",
5359 				&instance->irq_context[0])) {
5360 			printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5361 			goto fail_irq;
5362 		}
5363 	}
5364 
5365 	instance->instancet->enable_intr(instance);
5366 
5367 	/*
5368 	 * Store instance in PCI softstate
5369 	 */
5370 	pci_set_drvdata(pdev, instance);
5371 
5372 	/*
5373 	 * Add this controller to megasas_mgmt_info structure so that it
5374 	 * can be exported to management applications
5375 	 */
5376 	megasas_mgmt_info.count++;
5377 	megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5378 	megasas_mgmt_info.max_index++;
5379 
5380 	/*
5381 	 * Register with SCSI mid-layer
5382 	 */
5383 	if (megasas_io_attach(instance))
5384 		goto fail_io_attach;
5385 
5386 	instance->unload = 0;
5387 	/*
5388 	 * Trigger SCSI to scan our drives
5389 	 */
5390 	scsi_scan_host(host);
5391 
5392 	/*
5393 	 * Initiate AEN (Asynchronous Event Notification)
5394 	 */
5395 	if (megasas_start_aen(instance)) {
5396 		printk(KERN_DEBUG "megasas: start aen failed\n");
5397 		goto fail_start_aen;
5398 	}
5399 
5400 	/* Get current SR-IOV LD/VF affiliation */
5401 	if (instance->requestorId)
5402 		megasas_get_ld_vf_affiliation(instance, 1);
5403 
5404 	return 0;
5405 
5406       fail_start_aen:
5407       fail_io_attach:
5408 	megasas_mgmt_info.count--;
5409 	megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5410 	megasas_mgmt_info.max_index--;
5411 
5412 	instance->instancet->disable_intr(instance);
5413 	if (instance->msix_vectors)
5414 		for (i = 0; i < instance->msix_vectors; i++) {
5415 			if (smp_affinity_enable)
5416 				irq_set_affinity_hint(
5417 					instance->msixentry[i].vector, NULL);
5418 			free_irq(instance->msixentry[i].vector,
5419 				 &instance->irq_context[i]);
5420 		}
5421 	else
5422 		free_irq(instance->pdev->irq, &instance->irq_context[0]);
5423 fail_irq:
5424 	if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5425 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5426 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5427 	    (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
5428 		megasas_release_fusion(instance);
5429 	else
5430 		megasas_release_mfi(instance);
5431       fail_init_mfi:
5432 	if (instance->msix_vectors)
5433 		pci_disable_msix(instance->pdev);
5434       fail_alloc_dma_buf:
5435 	if (instance->evt_detail)
5436 		pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5437 				    instance->evt_detail,
5438 				    instance->evt_detail_h);
5439 
5440 	if (instance->producer)
5441 		pci_free_consistent(pdev, sizeof(u32), instance->producer,
5442 				    instance->producer_h);
5443 	if (instance->consumer)
5444 		pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5445 				    instance->consumer_h);
5446 	scsi_host_put(host);
5447 
5448       fail_alloc_instance:
5449       fail_set_dma_mask:
5450 	pci_disable_device(pdev);
5451 
5452 	return -ENODEV;
5453 }
5454 
5455 /**
5456  * megasas_flush_cache -	Requests FW to flush all its caches
5457  * @instance:			Adapter soft state
5458  */
5459 static void megasas_flush_cache(struct megasas_instance *instance)
5460 {
5461 	struct megasas_cmd *cmd;
5462 	struct megasas_dcmd_frame *dcmd;
5463 
5464 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5465 		return;
5466 
5467 	cmd = megasas_get_cmd(instance);
5468 
5469 	if (!cmd)
5470 		return;
5471 
5472 	dcmd = &cmd->frame->dcmd;
5473 
5474 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5475 
5476 	dcmd->cmd = MFI_CMD_DCMD;
5477 	dcmd->cmd_status = 0x0;
5478 	dcmd->sge_count = 0;
5479 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5480 	dcmd->timeout = 0;
5481 	dcmd->pad_0 = 0;
5482 	dcmd->data_xfer_len = 0;
5483 	dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5484 	dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5485 
5486 	if (megasas_issue_blocked_cmd(instance, cmd, 30))
5487 		dev_err(&instance->pdev->dev, "Command timedout"
5488 			" from %s\n", __func__);
5489 
5490 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5491 		megasas_return_mfi_mpt_pthr(instance, cmd,
5492 			cmd->mpt_pthr_cmd_blocked);
5493 	else
5494 		megasas_return_cmd(instance, cmd);
5495 
5496 	return;
5497 }
5498 
5499 /**
5500  * megasas_shutdown_controller -	Instructs FW to shutdown the controller
5501  * @instance:				Adapter soft state
5502  * @opcode:				Shutdown/Hibernate
5503  */
5504 static void megasas_shutdown_controller(struct megasas_instance *instance,
5505 					u32 opcode)
5506 {
5507 	struct megasas_cmd *cmd;
5508 	struct megasas_dcmd_frame *dcmd;
5509 
5510 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5511 		return;
5512 
5513 	cmd = megasas_get_cmd(instance);
5514 
5515 	if (!cmd)
5516 		return;
5517 
5518 	if (instance->aen_cmd)
5519 		megasas_issue_blocked_abort_cmd(instance,
5520 			instance->aen_cmd, 30);
5521 	if (instance->map_update_cmd)
5522 		megasas_issue_blocked_abort_cmd(instance,
5523 			instance->map_update_cmd, 30);
5524 	dcmd = &cmd->frame->dcmd;
5525 
5526 	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5527 
5528 	dcmd->cmd = MFI_CMD_DCMD;
5529 	dcmd->cmd_status = 0x0;
5530 	dcmd->sge_count = 0;
5531 	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5532 	dcmd->timeout = 0;
5533 	dcmd->pad_0 = 0;
5534 	dcmd->data_xfer_len = 0;
5535 	dcmd->opcode = cpu_to_le32(opcode);
5536 
5537 	if (megasas_issue_blocked_cmd(instance, cmd, 30))
5538 		dev_err(&instance->pdev->dev, "Command timedout"
5539 			"from %s\n", __func__);
5540 
5541 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
5542 		megasas_return_mfi_mpt_pthr(instance, cmd,
5543 			cmd->mpt_pthr_cmd_blocked);
5544 	else
5545 		megasas_return_cmd(instance, cmd);
5546 
5547 	return;
5548 }
5549 
5550 #ifdef CONFIG_PM
5551 /**
5552  * megasas_suspend -	driver suspend entry point
5553  * @pdev:		PCI device structure
5554  * @state:		PCI power state to suspend routine
5555  */
5556 static int
5557 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5558 {
5559 	struct Scsi_Host *host;
5560 	struct megasas_instance *instance;
5561 	int i;
5562 
5563 	instance = pci_get_drvdata(pdev);
5564 	host = instance->host;
5565 	instance->unload = 1;
5566 
5567 	/* Shutdown SR-IOV heartbeat timer */
5568 	if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5569 		del_timer_sync(&instance->sriov_heartbeat_timer);
5570 
5571 	megasas_flush_cache(instance);
5572 	megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5573 
5574 	/* cancel the delayed work if this work still in queue */
5575 	if (instance->ev != NULL) {
5576 		struct megasas_aen_event *ev = instance->ev;
5577 		cancel_delayed_work_sync(&ev->hotplug_work);
5578 		instance->ev = NULL;
5579 	}
5580 
5581 	tasklet_kill(&instance->isr_tasklet);
5582 
5583 	pci_set_drvdata(instance->pdev, instance);
5584 	instance->instancet->disable_intr(instance);
5585 
5586 	if (instance->msix_vectors)
5587 		for (i = 0; i < instance->msix_vectors; i++) {
5588 			if (smp_affinity_enable)
5589 				irq_set_affinity_hint(
5590 					instance->msixentry[i].vector, NULL);
5591 			free_irq(instance->msixentry[i].vector,
5592 				 &instance->irq_context[i]);
5593 		}
5594 	else
5595 		free_irq(instance->pdev->irq, &instance->irq_context[0]);
5596 	if (instance->msix_vectors)
5597 		pci_disable_msix(instance->pdev);
5598 
5599 	pci_save_state(pdev);
5600 	pci_disable_device(pdev);
5601 
5602 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
5603 
5604 	return 0;
5605 }
5606 
5607 /**
5608  * megasas_resume-      driver resume entry point
5609  * @pdev:               PCI device structure
5610  */
5611 static int
5612 megasas_resume(struct pci_dev *pdev)
5613 {
5614 	int rval, i, j, cpu;
5615 	struct Scsi_Host *host;
5616 	struct megasas_instance *instance;
5617 
5618 	instance = pci_get_drvdata(pdev);
5619 	host = instance->host;
5620 	pci_set_power_state(pdev, PCI_D0);
5621 	pci_enable_wake(pdev, PCI_D0, 0);
5622 	pci_restore_state(pdev);
5623 
5624 	/*
5625 	 * PCI prepping: enable device set bus mastering and dma mask
5626 	 */
5627 	rval = pci_enable_device_mem(pdev);
5628 
5629 	if (rval) {
5630 		printk(KERN_ERR "megasas: Enable device failed\n");
5631 		return rval;
5632 	}
5633 
5634 	pci_set_master(pdev);
5635 
5636 	if (megasas_set_dma_mask(pdev))
5637 		goto fail_set_dma_mask;
5638 
5639 	/*
5640 	 * Initialize MFI Firmware
5641 	 */
5642 
5643 	atomic_set(&instance->fw_outstanding, 0);
5644 
5645 	/*
5646 	 * We expect the FW state to be READY
5647 	 */
5648 	if (megasas_transition_to_ready(instance, 0))
5649 		goto fail_ready_state;
5650 
5651 	/* Now re-enable MSI-X */
5652 	if (instance->msix_vectors &&
5653 	    pci_enable_msix_exact(instance->pdev, instance->msixentry,
5654 				  instance->msix_vectors))
5655 		goto fail_reenable_msix;
5656 
5657 	switch (instance->pdev->device) {
5658 	case PCI_DEVICE_ID_LSI_FUSION:
5659 	case PCI_DEVICE_ID_LSI_PLASMA:
5660 	case PCI_DEVICE_ID_LSI_INVADER:
5661 	case PCI_DEVICE_ID_LSI_FURY:
5662 	{
5663 		megasas_reset_reply_desc(instance);
5664 		if (megasas_ioc_init_fusion(instance)) {
5665 			megasas_free_cmds(instance);
5666 			megasas_free_cmds_fusion(instance);
5667 			goto fail_init_mfi;
5668 		}
5669 		if (!megasas_get_map_info(instance))
5670 			megasas_sync_map_info(instance);
5671 	}
5672 	break;
5673 	default:
5674 		*instance->producer = 0;
5675 		*instance->consumer = 0;
5676 		if (megasas_issue_init_mfi(instance))
5677 			goto fail_init_mfi;
5678 		break;
5679 	}
5680 
5681 	tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5682 		     (unsigned long)instance);
5683 
5684 	/*
5685 	 * Register IRQ
5686 	 */
5687 	if (instance->msix_vectors) {
5688 		cpu = cpumask_first(cpu_online_mask);
5689 		for (i = 0 ; i < instance->msix_vectors; i++) {
5690 			instance->irq_context[i].instance = instance;
5691 			instance->irq_context[i].MSIxIndex = i;
5692 			if (request_irq(instance->msixentry[i].vector,
5693 					instance->instancet->service_isr, 0,
5694 					"megasas",
5695 					&instance->irq_context[i])) {
5696 				printk(KERN_DEBUG "megasas: Failed to "
5697 				       "register IRQ for vector %d.\n", i);
5698 				for (j = 0; j < i; j++) {
5699 					if (smp_affinity_enable)
5700 						irq_set_affinity_hint(
5701 							instance->msixentry[j].vector, NULL);
5702 					free_irq(
5703 						instance->msixentry[j].vector,
5704 						&instance->irq_context[j]);
5705 				}
5706 				goto fail_irq;
5707 			}
5708 
5709 			if (smp_affinity_enable) {
5710 				if (irq_set_affinity_hint(instance->msixentry[i].vector,
5711 					get_cpu_mask(cpu)))
5712 					dev_err(&instance->pdev->dev, "Error "
5713 						"setting affinity hint for cpu "
5714 						"%d\n", cpu);
5715 				cpu = cpumask_next(cpu, cpu_online_mask);
5716 			}
5717 		}
5718 	} else {
5719 		instance->irq_context[0].instance = instance;
5720 		instance->irq_context[0].MSIxIndex = 0;
5721 		if (request_irq(pdev->irq, instance->instancet->service_isr,
5722 				IRQF_SHARED, "megasas",
5723 				&instance->irq_context[0])) {
5724 			printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
5725 			goto fail_irq;
5726 		}
5727 	}
5728 
5729 	/* Re-launch SR-IOV heartbeat timer */
5730 	if (instance->requestorId) {
5731 		if (!megasas_sriov_start_heartbeat(instance, 0))
5732 			megasas_start_timer(instance,
5733 					    &instance->sriov_heartbeat_timer,
5734 					    megasas_sriov_heartbeat_handler,
5735 					    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5736 		else
5737 			instance->skip_heartbeat_timer_del = 1;
5738 	}
5739 
5740 	instance->instancet->enable_intr(instance);
5741 	instance->unload = 0;
5742 
5743 	/*
5744 	 * Initiate AEN (Asynchronous Event Notification)
5745 	 */
5746 	if (megasas_start_aen(instance))
5747 		printk(KERN_ERR "megasas: Start AEN failed\n");
5748 
5749 	return 0;
5750 
5751 fail_irq:
5752 fail_init_mfi:
5753 	if (instance->evt_detail)
5754 		pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5755 				instance->evt_detail,
5756 				instance->evt_detail_h);
5757 
5758 	if (instance->producer)
5759 		pci_free_consistent(pdev, sizeof(u32), instance->producer,
5760 				instance->producer_h);
5761 	if (instance->consumer)
5762 		pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5763 				instance->consumer_h);
5764 	scsi_host_put(host);
5765 
5766 fail_set_dma_mask:
5767 fail_ready_state:
5768 fail_reenable_msix:
5769 
5770 	pci_disable_device(pdev);
5771 
5772 	return -ENODEV;
5773 }
5774 #else
5775 #define megasas_suspend	NULL
5776 #define megasas_resume	NULL
5777 #endif
5778 
5779 /**
5780  * megasas_detach_one -	PCI hot"un"plug entry point
5781  * @pdev:		PCI device structure
5782  */
5783 static void megasas_detach_one(struct pci_dev *pdev)
5784 {
5785 	int i;
5786 	struct Scsi_Host *host;
5787 	struct megasas_instance *instance;
5788 	struct fusion_context *fusion;
5789 
5790 	instance = pci_get_drvdata(pdev);
5791 	instance->unload = 1;
5792 	host = instance->host;
5793 	fusion = instance->ctrl_context;
5794 
5795 	/* Shutdown SR-IOV heartbeat timer */
5796 	if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5797 		del_timer_sync(&instance->sriov_heartbeat_timer);
5798 
5799 	if (instance->fw_crash_state != UNAVAILABLE)
5800 		megasas_free_host_crash_buffer(instance);
5801 	scsi_remove_host(instance->host);
5802 	megasas_flush_cache(instance);
5803 	megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5804 
5805 	/* cancel the delayed work if this work still in queue*/
5806 	if (instance->ev != NULL) {
5807 		struct megasas_aen_event *ev = instance->ev;
5808 		cancel_delayed_work_sync(&ev->hotplug_work);
5809 		instance->ev = NULL;
5810 	}
5811 
5812 	/* cancel all wait events */
5813 	wake_up_all(&instance->int_cmd_wait_q);
5814 
5815 	tasklet_kill(&instance->isr_tasklet);
5816 
5817 	/*
5818 	 * Take the instance off the instance array. Note that we will not
5819 	 * decrement the max_index. We let this array be sparse array
5820 	 */
5821 	for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5822 		if (megasas_mgmt_info.instance[i] == instance) {
5823 			megasas_mgmt_info.count--;
5824 			megasas_mgmt_info.instance[i] = NULL;
5825 
5826 			break;
5827 		}
5828 	}
5829 
5830 	instance->instancet->disable_intr(instance);
5831 
5832 	if (instance->msix_vectors)
5833 		for (i = 0; i < instance->msix_vectors; i++) {
5834 			if (smp_affinity_enable)
5835 				irq_set_affinity_hint(
5836 					instance->msixentry[i].vector, NULL);
5837 			free_irq(instance->msixentry[i].vector,
5838 				 &instance->irq_context[i]);
5839 		}
5840 	else
5841 		free_irq(instance->pdev->irq, &instance->irq_context[0]);
5842 	if (instance->msix_vectors)
5843 		pci_disable_msix(instance->pdev);
5844 
5845 	switch (instance->pdev->device) {
5846 	case PCI_DEVICE_ID_LSI_FUSION:
5847 	case PCI_DEVICE_ID_LSI_PLASMA:
5848 	case PCI_DEVICE_ID_LSI_INVADER:
5849 	case PCI_DEVICE_ID_LSI_FURY:
5850 		megasas_release_fusion(instance);
5851 		for (i = 0; i < 2 ; i++) {
5852 			if (fusion->ld_map[i])
5853 				dma_free_coherent(&instance->pdev->dev,
5854 						  fusion->max_map_sz,
5855 						  fusion->ld_map[i],
5856 						  fusion->ld_map_phys[i]);
5857 			if (fusion->ld_drv_map[i])
5858 				free_pages((ulong)fusion->ld_drv_map[i],
5859 					fusion->drv_map_pages);
5860 		}
5861 		free_pages((ulong)instance->ctrl_context,
5862 			instance->ctrl_context_pages);
5863 		break;
5864 	default:
5865 		megasas_release_mfi(instance);
5866 		pci_free_consistent(pdev, sizeof(u32),
5867 				    instance->producer,
5868 				    instance->producer_h);
5869 		pci_free_consistent(pdev, sizeof(u32),
5870 				    instance->consumer,
5871 				    instance->consumer_h);
5872 		break;
5873 	}
5874 
5875 	kfree(instance->ctrl_info);
5876 
5877 	if (instance->evt_detail)
5878 		pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5879 				instance->evt_detail, instance->evt_detail_h);
5880 
5881 	if (instance->vf_affiliation)
5882 		pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5883 				    sizeof(struct MR_LD_VF_AFFILIATION),
5884 				    instance->vf_affiliation,
5885 				    instance->vf_affiliation_h);
5886 
5887 	if (instance->vf_affiliation_111)
5888 		pci_free_consistent(pdev,
5889 				    sizeof(struct MR_LD_VF_AFFILIATION_111),
5890 				    instance->vf_affiliation_111,
5891 				    instance->vf_affiliation_111_h);
5892 
5893 	if (instance->hb_host_mem)
5894 		pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5895 				    instance->hb_host_mem,
5896 				    instance->hb_host_mem_h);
5897 
5898 	if (instance->crash_dump_buf)
5899 		pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5900 			    instance->crash_dump_buf, instance->crash_dump_h);
5901 
5902 	scsi_host_put(host);
5903 
5904 	pci_disable_device(pdev);
5905 
5906 	return;
5907 }
5908 
5909 /**
5910  * megasas_shutdown -	Shutdown entry point
5911  * @device:		Generic device structure
5912  */
5913 static void megasas_shutdown(struct pci_dev *pdev)
5914 {
5915 	int i;
5916 	struct megasas_instance *instance = pci_get_drvdata(pdev);
5917 
5918 	instance->unload = 1;
5919 	megasas_flush_cache(instance);
5920 	megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5921 	instance->instancet->disable_intr(instance);
5922 	if (instance->msix_vectors)
5923 		for (i = 0; i < instance->msix_vectors; i++) {
5924 			if (smp_affinity_enable)
5925 				irq_set_affinity_hint(
5926 					instance->msixentry[i].vector, NULL);
5927 			free_irq(instance->msixentry[i].vector,
5928 				 &instance->irq_context[i]);
5929 		}
5930 	else
5931 		free_irq(instance->pdev->irq, &instance->irq_context[0]);
5932 	if (instance->msix_vectors)
5933 		pci_disable_msix(instance->pdev);
5934 }
5935 
5936 /**
5937  * megasas_mgmt_open -	char node "open" entry point
5938  */
5939 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5940 {
5941 	/*
5942 	 * Allow only those users with admin rights
5943 	 */
5944 	if (!capable(CAP_SYS_ADMIN))
5945 		return -EACCES;
5946 
5947 	return 0;
5948 }
5949 
5950 /**
5951  * megasas_mgmt_fasync -	Async notifier registration from applications
5952  *
5953  * This function adds the calling process to a driver global queue. When an
5954  * event occurs, SIGIO will be sent to all processes in this queue.
5955  */
5956 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5957 {
5958 	int rc;
5959 
5960 	mutex_lock(&megasas_async_queue_mutex);
5961 
5962 	rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5963 
5964 	mutex_unlock(&megasas_async_queue_mutex);
5965 
5966 	if (rc >= 0) {
5967 		/* For sanity check when we get ioctl */
5968 		filep->private_data = filep;
5969 		return 0;
5970 	}
5971 
5972 	printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5973 
5974 	return rc;
5975 }
5976 
5977 /**
5978  * megasas_mgmt_poll -  char node "poll" entry point
5979  * */
5980 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5981 {
5982 	unsigned int mask;
5983 	unsigned long flags;
5984 	poll_wait(file, &megasas_poll_wait, wait);
5985 	spin_lock_irqsave(&poll_aen_lock, flags);
5986 	if (megasas_poll_wait_aen)
5987 		mask =   (POLLIN | POLLRDNORM);
5988 
5989 	else
5990 		mask = 0;
5991 	megasas_poll_wait_aen = 0;
5992 	spin_unlock_irqrestore(&poll_aen_lock, flags);
5993 	return mask;
5994 }
5995 
5996 /*
5997  * megasas_set_crash_dump_params_ioctl:
5998  *		Send CRASH_DUMP_MODE DCMD to all controllers
5999  * @cmd:	MFI command frame
6000  */
6001 
6002 static int megasas_set_crash_dump_params_ioctl(
6003 	struct megasas_cmd *cmd)
6004 {
6005 	struct megasas_instance *local_instance;
6006 	int i, error = 0;
6007 	int crash_support;
6008 
6009 	crash_support = cmd->frame->dcmd.mbox.w[0];
6010 
6011 	for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6012 		local_instance = megasas_mgmt_info.instance[i];
6013 		if (local_instance && local_instance->crash_dump_drv_support) {
6014 			if ((local_instance->adprecovery ==
6015 				MEGASAS_HBA_OPERATIONAL) &&
6016 				!megasas_set_crash_dump_params(local_instance,
6017 					crash_support)) {
6018 				local_instance->crash_dump_app_support =
6019 					crash_support;
6020 				dev_info(&local_instance->pdev->dev,
6021 					"Application firmware crash "
6022 					"dump mode set success\n");
6023 				error = 0;
6024 			} else {
6025 				dev_info(&local_instance->pdev->dev,
6026 					"Application firmware crash "
6027 					"dump mode set failed\n");
6028 				error = -1;
6029 			}
6030 		}
6031 	}
6032 	return error;
6033 }
6034 
6035 /**
6036  * megasas_mgmt_fw_ioctl -	Issues management ioctls to FW
6037  * @instance:			Adapter soft state
6038  * @argp:			User's ioctl packet
6039  */
6040 static int
6041 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6042 		      struct megasas_iocpacket __user * user_ioc,
6043 		      struct megasas_iocpacket *ioc)
6044 {
6045 	struct megasas_sge32 *kern_sge32;
6046 	struct megasas_cmd *cmd;
6047 	void *kbuff_arr[MAX_IOCTL_SGE];
6048 	dma_addr_t buf_handle = 0;
6049 	int error = 0, i;
6050 	void *sense = NULL;
6051 	dma_addr_t sense_handle;
6052 	unsigned long *sense_ptr;
6053 
6054 	memset(kbuff_arr, 0, sizeof(kbuff_arr));
6055 
6056 	if (ioc->sge_count > MAX_IOCTL_SGE) {
6057 		printk(KERN_DEBUG "megasas: SGE count [%d] >  max limit [%d]\n",
6058 		       ioc->sge_count, MAX_IOCTL_SGE);
6059 		return -EINVAL;
6060 	}
6061 
6062 	cmd = megasas_get_cmd(instance);
6063 	if (!cmd) {
6064 		printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
6065 		return -ENOMEM;
6066 	}
6067 
6068 	/*
6069 	 * User's IOCTL packet has 2 frames (maximum). Copy those two
6070 	 * frames into our cmd's frames. cmd->frame's context will get
6071 	 * overwritten when we copy from user's frames. So set that value
6072 	 * alone separately
6073 	 */
6074 	memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6075 	cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6076 	cmd->frame->hdr.pad_0 = 0;
6077 	cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6078 					       MFI_FRAME_SGL64 |
6079 					       MFI_FRAME_SENSE64));
6080 
6081 	if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6082 		error = megasas_set_crash_dump_params_ioctl(cmd);
6083 		megasas_return_cmd(instance, cmd);
6084 		return error;
6085 	}
6086 
6087 	/*
6088 	 * The management interface between applications and the fw uses
6089 	 * MFI frames. E.g, RAID configuration changes, LD property changes
6090 	 * etc are accomplishes through different kinds of MFI frames. The
6091 	 * driver needs to care only about substituting user buffers with
6092 	 * kernel buffers in SGLs. The location of SGL is embedded in the
6093 	 * struct iocpacket itself.
6094 	 */
6095 	kern_sge32 = (struct megasas_sge32 *)
6096 	    ((unsigned long)cmd->frame + ioc->sgl_off);
6097 
6098 	/*
6099 	 * For each user buffer, create a mirror buffer and copy in
6100 	 */
6101 	for (i = 0; i < ioc->sge_count; i++) {
6102 		if (!ioc->sgl[i].iov_len)
6103 			continue;
6104 
6105 		kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6106 						    ioc->sgl[i].iov_len,
6107 						    &buf_handle, GFP_KERNEL);
6108 		if (!kbuff_arr[i]) {
6109 			printk(KERN_DEBUG "megasas: Failed to alloc "
6110 			       "kernel SGL buffer for IOCTL \n");
6111 			error = -ENOMEM;
6112 			goto out;
6113 		}
6114 
6115 		/*
6116 		 * We don't change the dma_coherent_mask, so
6117 		 * pci_alloc_consistent only returns 32bit addresses
6118 		 */
6119 		kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6120 		kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6121 
6122 		/*
6123 		 * We created a kernel buffer corresponding to the
6124 		 * user buffer. Now copy in from the user buffer
6125 		 */
6126 		if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6127 				   (u32) (ioc->sgl[i].iov_len))) {
6128 			error = -EFAULT;
6129 			goto out;
6130 		}
6131 	}
6132 
6133 	if (ioc->sense_len) {
6134 		sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6135 					     &sense_handle, GFP_KERNEL);
6136 		if (!sense) {
6137 			error = -ENOMEM;
6138 			goto out;
6139 		}
6140 
6141 		sense_ptr =
6142 		(unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6143 		*sense_ptr = cpu_to_le32(sense_handle);
6144 	}
6145 
6146 	/*
6147 	 * Set the sync_cmd flag so that the ISR knows not to complete this
6148 	 * cmd to the SCSI mid-layer
6149 	 */
6150 	cmd->sync_cmd = 1;
6151 	megasas_issue_blocked_cmd(instance, cmd, 0);
6152 	cmd->sync_cmd = 0;
6153 
6154 	if (instance->unload == 1) {
6155 		dev_info(&instance->pdev->dev, "Driver unload is in progress "
6156 			"don't submit data to application\n");
6157 		goto out;
6158 	}
6159 	/*
6160 	 * copy out the kernel buffers to user buffers
6161 	 */
6162 	for (i = 0; i < ioc->sge_count; i++) {
6163 		if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6164 				 ioc->sgl[i].iov_len)) {
6165 			error = -EFAULT;
6166 			goto out;
6167 		}
6168 	}
6169 
6170 	/*
6171 	 * copy out the sense
6172 	 */
6173 	if (ioc->sense_len) {
6174 		/*
6175 		 * sense_ptr points to the location that has the user
6176 		 * sense buffer address
6177 		 */
6178 		sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6179 				ioc->sense_off);
6180 
6181 		if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6182 				 sense, ioc->sense_len)) {
6183 			printk(KERN_ERR "megasas: Failed to copy out to user "
6184 					"sense data\n");
6185 			error = -EFAULT;
6186 			goto out;
6187 		}
6188 	}
6189 
6190 	/*
6191 	 * copy the status codes returned by the fw
6192 	 */
6193 	if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6194 			 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6195 		printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
6196 		error = -EFAULT;
6197 	}
6198 
6199       out:
6200 	if (sense) {
6201 		dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6202 				    sense, sense_handle);
6203 	}
6204 
6205 	for (i = 0; i < ioc->sge_count; i++) {
6206 		if (kbuff_arr[i])
6207 			dma_free_coherent(&instance->pdev->dev,
6208 					  le32_to_cpu(kern_sge32[i].length),
6209 					  kbuff_arr[i],
6210 					  le32_to_cpu(kern_sge32[i].phys_addr));
6211 			kbuff_arr[i] = NULL;
6212 	}
6213 
6214 	if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
6215 		megasas_return_mfi_mpt_pthr(instance, cmd,
6216 			cmd->mpt_pthr_cmd_blocked);
6217 	else
6218 		megasas_return_cmd(instance, cmd);
6219 	return error;
6220 }
6221 
6222 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6223 {
6224 	struct megasas_iocpacket __user *user_ioc =
6225 	    (struct megasas_iocpacket __user *)arg;
6226 	struct megasas_iocpacket *ioc;
6227 	struct megasas_instance *instance;
6228 	int error;
6229 	int i;
6230 	unsigned long flags;
6231 	u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6232 
6233 	ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6234 	if (!ioc)
6235 		return -ENOMEM;
6236 
6237 	if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6238 		error = -EFAULT;
6239 		goto out_kfree_ioc;
6240 	}
6241 
6242 	instance = megasas_lookup_instance(ioc->host_no);
6243 	if (!instance) {
6244 		error = -ENODEV;
6245 		goto out_kfree_ioc;
6246 	}
6247 
6248 	/* Adjust ioctl wait time for VF mode */
6249 	if (instance->requestorId)
6250 		wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6251 
6252 	/* Block ioctls in VF mode */
6253 	if (instance->requestorId && !allow_vf_ioctls) {
6254 		error = -ENODEV;
6255 		goto out_kfree_ioc;
6256 	}
6257 
6258 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6259 		printk(KERN_ERR "Controller in crit error\n");
6260 		error = -ENODEV;
6261 		goto out_kfree_ioc;
6262 	}
6263 
6264 	if (instance->unload == 1) {
6265 		error = -ENODEV;
6266 		goto out_kfree_ioc;
6267 	}
6268 
6269 	if (down_interruptible(&instance->ioctl_sem)) {
6270 		error = -ERESTARTSYS;
6271 		goto out_kfree_ioc;
6272 	}
6273 
6274 	for (i = 0; i < wait_time; i++) {
6275 
6276 		spin_lock_irqsave(&instance->hba_lock, flags);
6277 		if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6278 			spin_unlock_irqrestore(&instance->hba_lock, flags);
6279 			break;
6280 		}
6281 		spin_unlock_irqrestore(&instance->hba_lock, flags);
6282 
6283 		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6284 			printk(KERN_NOTICE "megasas: waiting"
6285 				"for controller reset to finish\n");
6286 		}
6287 
6288 		msleep(1000);
6289 	}
6290 
6291 	spin_lock_irqsave(&instance->hba_lock, flags);
6292 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6293 		spin_unlock_irqrestore(&instance->hba_lock, flags);
6294 
6295 		printk(KERN_ERR "megaraid_sas: timed out while"
6296 			"waiting for HBA to recover\n");
6297 		error = -ENODEV;
6298 		goto out_up;
6299 	}
6300 	spin_unlock_irqrestore(&instance->hba_lock, flags);
6301 
6302 	error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6303       out_up:
6304 	up(&instance->ioctl_sem);
6305 
6306       out_kfree_ioc:
6307 	kfree(ioc);
6308 	return error;
6309 }
6310 
6311 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6312 {
6313 	struct megasas_instance *instance;
6314 	struct megasas_aen aen;
6315 	int error;
6316 	int i;
6317 	unsigned long flags;
6318 	u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6319 
6320 	if (file->private_data != file) {
6321 		printk(KERN_DEBUG "megasas: fasync_helper was not "
6322 		       "called first\n");
6323 		return -EINVAL;
6324 	}
6325 
6326 	if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6327 		return -EFAULT;
6328 
6329 	instance = megasas_lookup_instance(aen.host_no);
6330 
6331 	if (!instance)
6332 		return -ENODEV;
6333 
6334 	if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6335 		return -ENODEV;
6336 	}
6337 
6338 	if (instance->unload == 1) {
6339 		return -ENODEV;
6340 	}
6341 
6342 	for (i = 0; i < wait_time; i++) {
6343 
6344 		spin_lock_irqsave(&instance->hba_lock, flags);
6345 		if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6346 			spin_unlock_irqrestore(&instance->hba_lock,
6347 						flags);
6348 			break;
6349 		}
6350 
6351 		spin_unlock_irqrestore(&instance->hba_lock, flags);
6352 
6353 		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6354 			printk(KERN_NOTICE "megasas: waiting for"
6355 				"controller reset to finish\n");
6356 		}
6357 
6358 		msleep(1000);
6359 	}
6360 
6361 	spin_lock_irqsave(&instance->hba_lock, flags);
6362 	if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6363 		spin_unlock_irqrestore(&instance->hba_lock, flags);
6364 		printk(KERN_ERR "megaraid_sas: timed out while waiting"
6365 				"for HBA to recover.\n");
6366 		return -ENODEV;
6367 	}
6368 	spin_unlock_irqrestore(&instance->hba_lock, flags);
6369 
6370 	mutex_lock(&instance->aen_mutex);
6371 	error = megasas_register_aen(instance, aen.seq_num,
6372 				     aen.class_locale_word);
6373 	mutex_unlock(&instance->aen_mutex);
6374 	return error;
6375 }
6376 
6377 /**
6378  * megasas_mgmt_ioctl -	char node ioctl entry point
6379  */
6380 static long
6381 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6382 {
6383 	switch (cmd) {
6384 	case MEGASAS_IOC_FIRMWARE:
6385 		return megasas_mgmt_ioctl_fw(file, arg);
6386 
6387 	case MEGASAS_IOC_GET_AEN:
6388 		return megasas_mgmt_ioctl_aen(file, arg);
6389 	}
6390 
6391 	return -ENOTTY;
6392 }
6393 
6394 #ifdef CONFIG_COMPAT
6395 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6396 {
6397 	struct compat_megasas_iocpacket __user *cioc =
6398 	    (struct compat_megasas_iocpacket __user *)arg;
6399 	struct megasas_iocpacket __user *ioc =
6400 	    compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6401 	int i;
6402 	int error = 0;
6403 	compat_uptr_t ptr;
6404 
6405 	if (clear_user(ioc, sizeof(*ioc)))
6406 		return -EFAULT;
6407 
6408 	if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6409 	    copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6410 	    copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6411 	    copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6412 	    copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6413 	    copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6414 		return -EFAULT;
6415 
6416 	/*
6417 	 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6418 	 * sense_len is not null, so prepare the 64bit value under
6419 	 * the same condition.
6420 	 */
6421 	if (ioc->sense_len) {
6422 		void __user **sense_ioc_ptr =
6423 			(void __user **)(ioc->frame.raw + ioc->sense_off);
6424 		compat_uptr_t *sense_cioc_ptr =
6425 			(compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6426 		if (get_user(ptr, sense_cioc_ptr) ||
6427 		    put_user(compat_ptr(ptr), sense_ioc_ptr))
6428 			return -EFAULT;
6429 	}
6430 
6431 	for (i = 0; i < MAX_IOCTL_SGE; i++) {
6432 		if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6433 		    put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6434 		    copy_in_user(&ioc->sgl[i].iov_len,
6435 				 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6436 			return -EFAULT;
6437 	}
6438 
6439 	error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6440 
6441 	if (copy_in_user(&cioc->frame.hdr.cmd_status,
6442 			 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6443 		printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6444 		return -EFAULT;
6445 	}
6446 	return error;
6447 }
6448 
6449 static long
6450 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6451 			  unsigned long arg)
6452 {
6453 	switch (cmd) {
6454 	case MEGASAS_IOC_FIRMWARE32:
6455 		return megasas_mgmt_compat_ioctl_fw(file, arg);
6456 	case MEGASAS_IOC_GET_AEN:
6457 		return megasas_mgmt_ioctl_aen(file, arg);
6458 	}
6459 
6460 	return -ENOTTY;
6461 }
6462 #endif
6463 
6464 /*
6465  * File operations structure for management interface
6466  */
6467 static const struct file_operations megasas_mgmt_fops = {
6468 	.owner = THIS_MODULE,
6469 	.open = megasas_mgmt_open,
6470 	.fasync = megasas_mgmt_fasync,
6471 	.unlocked_ioctl = megasas_mgmt_ioctl,
6472 	.poll = megasas_mgmt_poll,
6473 #ifdef CONFIG_COMPAT
6474 	.compat_ioctl = megasas_mgmt_compat_ioctl,
6475 #endif
6476 	.llseek = noop_llseek,
6477 };
6478 
6479 /*
6480  * PCI hotplug support registration structure
6481  */
6482 static struct pci_driver megasas_pci_driver = {
6483 
6484 	.name = "megaraid_sas",
6485 	.id_table = megasas_pci_table,
6486 	.probe = megasas_probe_one,
6487 	.remove = megasas_detach_one,
6488 	.suspend = megasas_suspend,
6489 	.resume = megasas_resume,
6490 	.shutdown = megasas_shutdown,
6491 };
6492 
6493 /*
6494  * Sysfs driver attributes
6495  */
6496 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6497 {
6498 	return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6499 			MEGASAS_VERSION);
6500 }
6501 
6502 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6503 
6504 static ssize_t
6505 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6506 {
6507 	return sprintf(buf, "%u\n", support_poll_for_event);
6508 }
6509 
6510 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6511 			megasas_sysfs_show_support_poll_for_event, NULL);
6512 
6513  static ssize_t
6514 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6515 {
6516 	return sprintf(buf, "%u\n", support_device_change);
6517 }
6518 
6519 static DRIVER_ATTR(support_device_change, S_IRUGO,
6520 			megasas_sysfs_show_support_device_change, NULL);
6521 
6522 static ssize_t
6523 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6524 {
6525 	return sprintf(buf, "%u\n", megasas_dbg_lvl);
6526 }
6527 
6528 static ssize_t
6529 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6530 {
6531 	int retval = count;
6532 	if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6533 		printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6534 		retval = -EINVAL;
6535 	}
6536 	return retval;
6537 }
6538 
6539 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6540 		megasas_sysfs_set_dbg_lvl);
6541 
6542 static void
6543 megasas_aen_polling(struct work_struct *work)
6544 {
6545 	struct megasas_aen_event *ev =
6546 		container_of(work, struct megasas_aen_event, hotplug_work.work);
6547 	struct megasas_instance *instance = ev->instance;
6548 	union megasas_evt_class_locale class_locale;
6549 	struct  Scsi_Host *host;
6550 	struct  scsi_device *sdev1;
6551 	u16     pd_index = 0;
6552 	u16	ld_index = 0;
6553 	int     i, j, doscan = 0;
6554 	u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6555 	int error;
6556 
6557 	if (!instance) {
6558 		printk(KERN_ERR "invalid instance!\n");
6559 		kfree(ev);
6560 		return;
6561 	}
6562 
6563 	/* Adjust event workqueue thread wait time for VF mode */
6564 	if (instance->requestorId)
6565 		wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6566 
6567 	/* Don't run the event workqueue thread if OCR is running */
6568 	for (i = 0; i < wait_time; i++) {
6569 		if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6570 			break;
6571 		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6572 			printk(KERN_NOTICE "megasas: %s waiting for "
6573 			       "controller reset to finish for scsi%d\n",
6574 			       __func__, instance->host->host_no);
6575 		}
6576 		msleep(1000);
6577 	}
6578 
6579 	instance->ev = NULL;
6580 	host = instance->host;
6581 	if (instance->evt_detail) {
6582 
6583 		switch (le32_to_cpu(instance->evt_detail->code)) {
6584 		case MR_EVT_PD_INSERTED:
6585 			if (megasas_get_pd_list(instance) == 0) {
6586 			for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6587 				for (j = 0;
6588 				j < MEGASAS_MAX_DEV_PER_CHANNEL;
6589 				j++) {
6590 
6591 				pd_index =
6592 				(i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6593 
6594 				sdev1 =
6595 				scsi_device_lookup(host, i, j, 0);
6596 
6597 				if (instance->pd_list[pd_index].driveState
6598 						== MR_PD_STATE_SYSTEM) {
6599 						if (!sdev1) {
6600 						scsi_add_device(host, i, j, 0);
6601 						}
6602 
6603 					if (sdev1)
6604 						scsi_device_put(sdev1);
6605 					}
6606 				}
6607 			}
6608 			}
6609 			doscan = 0;
6610 			break;
6611 
6612 		case MR_EVT_PD_REMOVED:
6613 			if (megasas_get_pd_list(instance) == 0) {
6614 			for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6615 				for (j = 0;
6616 				j < MEGASAS_MAX_DEV_PER_CHANNEL;
6617 				j++) {
6618 
6619 				pd_index =
6620 				(i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6621 
6622 				sdev1 =
6623 				scsi_device_lookup(host, i, j, 0);
6624 
6625 				if (instance->pd_list[pd_index].driveState
6626 					== MR_PD_STATE_SYSTEM) {
6627 					if (sdev1) {
6628 						scsi_device_put(sdev1);
6629 					}
6630 				} else {
6631 					if (sdev1) {
6632 						scsi_remove_device(sdev1);
6633 						scsi_device_put(sdev1);
6634 					}
6635 				}
6636 				}
6637 			}
6638 			}
6639 			doscan = 0;
6640 			break;
6641 
6642 		case MR_EVT_LD_OFFLINE:
6643 		case MR_EVT_CFG_CLEARED:
6644 		case MR_EVT_LD_DELETED:
6645 			if (!instance->requestorId ||
6646 			    (instance->requestorId &&
6647 			     megasas_get_ld_vf_affiliation(instance, 0))) {
6648 				if (megasas_ld_list_query(instance,
6649 							  MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6650 					megasas_get_ld_list(instance);
6651 				for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6652 					for (j = 0;
6653 					     j < MEGASAS_MAX_DEV_PER_CHANNEL;
6654 					     j++) {
6655 
6656 						ld_index =
6657 							(i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6658 
6659 						sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6660 
6661 						if (instance->ld_ids[ld_index]
6662 						    != 0xff) {
6663 							if (sdev1)
6664 								scsi_device_put(sdev1);
6665 						} else {
6666 							if (sdev1) {
6667 								scsi_remove_device(sdev1);
6668 								scsi_device_put(sdev1);
6669 							}
6670 						}
6671 					}
6672 				}
6673 				doscan = 0;
6674 			}
6675 			break;
6676 		case MR_EVT_LD_CREATED:
6677 			if (!instance->requestorId ||
6678 			    (instance->requestorId &&
6679 			     megasas_get_ld_vf_affiliation(instance, 0))) {
6680 				if (megasas_ld_list_query(instance,
6681 							  MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6682 					megasas_get_ld_list(instance);
6683 				for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6684 					for (j = 0;
6685 					     j < MEGASAS_MAX_DEV_PER_CHANNEL;
6686 					     j++) {
6687 						ld_index =
6688 							(i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6689 
6690 						sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6691 
6692 						if (instance->ld_ids[ld_index]
6693 						    != 0xff) {
6694 							if (!sdev1)
6695 								scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6696 						}
6697 						if (sdev1)
6698 							scsi_device_put(sdev1);
6699 					}
6700 				}
6701 				doscan = 0;
6702 			}
6703 			break;
6704 		case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6705 		case MR_EVT_FOREIGN_CFG_IMPORTED:
6706 		case MR_EVT_LD_STATE_CHANGE:
6707 			doscan = 1;
6708 			break;
6709 		default:
6710 			doscan = 0;
6711 			break;
6712 		}
6713 	} else {
6714 		printk(KERN_ERR "invalid evt_detail!\n");
6715 		kfree(ev);
6716 		return;
6717 	}
6718 
6719 	if (doscan) {
6720 		printk(KERN_INFO "megaraid_sas: scanning for scsi%d...\n",
6721 		       instance->host->host_no);
6722 		if (megasas_get_pd_list(instance) == 0) {
6723 			for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6724 				for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6725 					pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6726 					sdev1 = scsi_device_lookup(host, i, j, 0);
6727 					if (instance->pd_list[pd_index].driveState ==
6728 					    MR_PD_STATE_SYSTEM) {
6729 						if (!sdev1) {
6730 							scsi_add_device(host, i, j, 0);
6731 						}
6732 						if (sdev1)
6733 							scsi_device_put(sdev1);
6734 					} else {
6735 						if (sdev1) {
6736 							scsi_remove_device(sdev1);
6737 							scsi_device_put(sdev1);
6738 						}
6739 					}
6740 				}
6741 			}
6742 		}
6743 
6744 		if (!instance->requestorId ||
6745 		    (instance->requestorId &&
6746 		     megasas_get_ld_vf_affiliation(instance, 0))) {
6747 			if (megasas_ld_list_query(instance,
6748 						  MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6749 				megasas_get_ld_list(instance);
6750 			for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6751 				for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6752 				     j++) {
6753 					ld_index =
6754 						(i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6755 
6756 					sdev1 = scsi_device_lookup(host,
6757 								   MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6758 					if (instance->ld_ids[ld_index]
6759 					    != 0xff) {
6760 						if (!sdev1)
6761 							scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6762 						else
6763 							scsi_device_put(sdev1);
6764 					} else {
6765 						if (sdev1) {
6766 							scsi_remove_device(sdev1);
6767 							scsi_device_put(sdev1);
6768 						}
6769 					}
6770 				}
6771 			}
6772 		}
6773 	}
6774 
6775 	if ( instance->aen_cmd != NULL ) {
6776 		kfree(ev);
6777 		return ;
6778 	}
6779 
6780 	seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6781 
6782 	/* Register AEN with FW for latest sequence number plus 1 */
6783 	class_locale.members.reserved = 0;
6784 	class_locale.members.locale = MR_EVT_LOCALE_ALL;
6785 	class_locale.members.class = MR_EVT_CLASS_DEBUG;
6786 	mutex_lock(&instance->aen_mutex);
6787 	error = megasas_register_aen(instance, seq_num,
6788 					class_locale.word);
6789 	mutex_unlock(&instance->aen_mutex);
6790 
6791 	if (error)
6792 		printk(KERN_ERR "register aen failed error %x\n", error);
6793 
6794 	kfree(ev);
6795 }
6796 
6797 /**
6798  * megasas_init - Driver load entry point
6799  */
6800 static int __init megasas_init(void)
6801 {
6802 	int rval;
6803 
6804 	/*
6805 	 * Announce driver version and other information
6806 	 */
6807 	pr_info("megasas: %s\n", MEGASAS_VERSION);
6808 
6809 	spin_lock_init(&poll_aen_lock);
6810 
6811 	support_poll_for_event = 2;
6812 	support_device_change = 1;
6813 
6814 	memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6815 
6816 	/*
6817 	 * Register character device node
6818 	 */
6819 	rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6820 
6821 	if (rval < 0) {
6822 		printk(KERN_DEBUG "megasas: failed to open device node\n");
6823 		return rval;
6824 	}
6825 
6826 	megasas_mgmt_majorno = rval;
6827 
6828 	/*
6829 	 * Register ourselves as PCI hotplug module
6830 	 */
6831 	rval = pci_register_driver(&megasas_pci_driver);
6832 
6833 	if (rval) {
6834 		printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6835 		goto err_pcidrv;
6836 	}
6837 
6838 	rval = driver_create_file(&megasas_pci_driver.driver,
6839 				  &driver_attr_version);
6840 	if (rval)
6841 		goto err_dcf_attr_ver;
6842 
6843 	rval = driver_create_file(&megasas_pci_driver.driver,
6844 				&driver_attr_support_poll_for_event);
6845 	if (rval)
6846 		goto err_dcf_support_poll_for_event;
6847 
6848 	rval = driver_create_file(&megasas_pci_driver.driver,
6849 				  &driver_attr_dbg_lvl);
6850 	if (rval)
6851 		goto err_dcf_dbg_lvl;
6852 	rval = driver_create_file(&megasas_pci_driver.driver,
6853 				&driver_attr_support_device_change);
6854 	if (rval)
6855 		goto err_dcf_support_device_change;
6856 
6857 	return rval;
6858 
6859 err_dcf_support_device_change:
6860 	driver_remove_file(&megasas_pci_driver.driver,
6861 			   &driver_attr_dbg_lvl);
6862 err_dcf_dbg_lvl:
6863 	driver_remove_file(&megasas_pci_driver.driver,
6864 			&driver_attr_support_poll_for_event);
6865 err_dcf_support_poll_for_event:
6866 	driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6867 err_dcf_attr_ver:
6868 	pci_unregister_driver(&megasas_pci_driver);
6869 err_pcidrv:
6870 	unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6871 	return rval;
6872 }
6873 
6874 /**
6875  * megasas_exit - Driver unload entry point
6876  */
6877 static void __exit megasas_exit(void)
6878 {
6879 	driver_remove_file(&megasas_pci_driver.driver,
6880 			   &driver_attr_dbg_lvl);
6881 	driver_remove_file(&megasas_pci_driver.driver,
6882 			&driver_attr_support_poll_for_event);
6883 	driver_remove_file(&megasas_pci_driver.driver,
6884 			&driver_attr_support_device_change);
6885 	driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6886 
6887 	pci_unregister_driver(&megasas_pci_driver);
6888 	unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6889 }
6890 
6891 module_init(megasas_init);
6892 module_exit(megasas_exit);
6893