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