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