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