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