]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/megaraid/megaraid_sas.c
SCSI host lock push-down
[mv-sheeva.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_lck(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 DEF_SCSI_QCMD(megasas_queue_command)
1421
1422 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1423 {
1424         int i;
1425
1426         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1427
1428                 if ((megasas_mgmt_info.instance[i]) &&
1429                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1430                         return megasas_mgmt_info.instance[i];
1431         }
1432
1433         return NULL;
1434 }
1435
1436 static int megasas_slave_configure(struct scsi_device *sdev)
1437 {
1438         u16             pd_index = 0;
1439         struct  megasas_instance *instance ;
1440
1441         instance = megasas_lookup_instance(sdev->host->host_no);
1442
1443         /*
1444         * Don't export physical disk devices to the disk driver.
1445         *
1446         * FIXME: Currently we don't export them to the midlayer at all.
1447         *        That will be fixed once LSI engineers have audited the
1448         *        firmware for possible issues.
1449         */
1450         if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1451                                 sdev->type == TYPE_DISK) {
1452                 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1453                                                                 sdev->id;
1454                 if (instance->pd_list[pd_index].driveState ==
1455                                                 MR_PD_STATE_SYSTEM) {
1456                         blk_queue_rq_timeout(sdev->request_queue,
1457                                 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1458                         return 0;
1459                 }
1460                 return -ENXIO;
1461         }
1462
1463         /*
1464         * The RAID firmware may require extended timeouts.
1465         */
1466         blk_queue_rq_timeout(sdev->request_queue,
1467                 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1468         return 0;
1469 }
1470
1471 static int megasas_slave_alloc(struct scsi_device *sdev)
1472 {
1473         u16             pd_index = 0;
1474         struct megasas_instance *instance ;
1475         instance = megasas_lookup_instance(sdev->host->host_no);
1476         if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1477                                 (sdev->type == TYPE_DISK)) {
1478                 /*
1479                  * Open the OS scan to the SYSTEM PD
1480                  */
1481                 pd_index =
1482                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1483                         sdev->id;
1484                 if ((instance->pd_list[pd_index].driveState ==
1485                                         MR_PD_STATE_SYSTEM) &&
1486                         (instance->pd_list[pd_index].driveType ==
1487                                                 TYPE_DISK)) {
1488                         return 0;
1489                 }
1490                 return -ENXIO;
1491         }
1492         return 0;
1493 }
1494
1495 static void megaraid_sas_kill_hba(struct megasas_instance *instance)
1496 {
1497         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1498                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1499                 writel(MFI_STOP_ADP,
1500                         &instance->reg_set->reserved_0[0]);
1501         } else {
1502                 writel(MFI_STOP_ADP,
1503                         &instance->reg_set->inbound_doorbell);
1504         }
1505 }
1506
1507 /**
1508  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
1509  * @instance_addr:                      Address of adapter soft state
1510  *
1511  * Tasklet to complete cmds
1512  */
1513 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1514 {
1515         u32 producer;
1516         u32 consumer;
1517         u32 context;
1518         struct megasas_cmd *cmd;
1519         struct megasas_instance *instance =
1520                                 (struct megasas_instance *)instance_addr;
1521         unsigned long flags;
1522
1523         /* If we have already declared adapter dead, donot complete cmds */
1524         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1525                 return;
1526
1527         spin_lock_irqsave(&instance->completion_lock, flags);
1528
1529         producer = *instance->producer;
1530         consumer = *instance->consumer;
1531
1532         while (consumer != producer) {
1533                 context = instance->reply_queue[consumer];
1534                 if (context >= instance->max_fw_cmds) {
1535                         printk(KERN_ERR "Unexpected context value %x\n",
1536                                 context);
1537                         BUG();
1538                 }
1539
1540                 cmd = instance->cmd_list[context];
1541
1542                 megasas_complete_cmd(instance, cmd, DID_OK);
1543
1544                 consumer++;
1545                 if (consumer == (instance->max_fw_cmds + 1)) {
1546                         consumer = 0;
1547                 }
1548         }
1549
1550         *instance->consumer = producer;
1551
1552         spin_unlock_irqrestore(&instance->completion_lock, flags);
1553
1554         /*
1555          * Check if we can restore can_queue
1556          */
1557         if (instance->flag & MEGASAS_FW_BUSY
1558                 && time_after(jiffies, instance->last_time + 5 * HZ)
1559                 && atomic_read(&instance->fw_outstanding) < 17) {
1560
1561                 spin_lock_irqsave(instance->host->host_lock, flags);
1562                 instance->flag &= ~MEGASAS_FW_BUSY;
1563                 if ((instance->pdev->device ==
1564                         PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1565                         (instance->pdev->device ==
1566                         PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1567                         instance->host->can_queue =
1568                                 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1569                 } else
1570                         instance->host->can_queue =
1571                                 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1572
1573                 spin_unlock_irqrestore(instance->host->host_lock, flags);
1574         }
1575 }
1576
1577 static void
1578 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1579
1580 static void
1581 process_fw_state_change_wq(struct work_struct *work);
1582
1583 void megasas_do_ocr(struct megasas_instance *instance)
1584 {
1585         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1586         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1587         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1588                 *instance->consumer     = MEGASAS_ADPRESET_INPROG_SIGN;
1589         }
1590         instance->instancet->disable_intr(instance->reg_set);
1591         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
1592         instance->issuepend_done = 0;
1593
1594         atomic_set(&instance->fw_outstanding, 0);
1595         megasas_internal_reset_defer_cmds(instance);
1596         process_fw_state_change_wq(&instance->work_init);
1597 }
1598
1599 /**
1600  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
1601  * @instance:                           Adapter soft state
1602  *
1603  * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
1604  * complete all its outstanding commands. Returns error if one or more IOs
1605  * are pending after this time period. It also marks the controller dead.
1606  */
1607 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1608 {
1609         int i;
1610         u32 reset_index;
1611         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1612         u8 adprecovery;
1613         unsigned long flags;
1614         struct list_head clist_local;
1615         struct megasas_cmd *reset_cmd;
1616         u32 fw_state;
1617         u8 kill_adapter_flag;
1618
1619         spin_lock_irqsave(&instance->hba_lock, flags);
1620         adprecovery = instance->adprecovery;
1621         spin_unlock_irqrestore(&instance->hba_lock, flags);
1622
1623         if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1624
1625                 INIT_LIST_HEAD(&clist_local);
1626                 spin_lock_irqsave(&instance->hba_lock, flags);
1627                 list_splice_init(&instance->internal_reset_pending_q,
1628                                 &clist_local);
1629                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1630
1631                 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1632                 for (i = 0; i < wait_time; i++) {
1633                         msleep(1000);
1634                         spin_lock_irqsave(&instance->hba_lock, flags);
1635                         adprecovery = instance->adprecovery;
1636                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1637                         if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1638                                 break;
1639                 }
1640
1641                 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1642                         printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1643                         spin_lock_irqsave(&instance->hba_lock, flags);
1644                         instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
1645                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1646                         return FAILED;
1647                 }
1648
1649                 reset_index     = 0;
1650                 while (!list_empty(&clist_local)) {
1651                         reset_cmd       = list_entry((&clist_local)->next,
1652                                                 struct megasas_cmd, list);
1653                         list_del_init(&reset_cmd->list);
1654                         if (reset_cmd->scmd) {
1655                                 reset_cmd->scmd->result = DID_RESET << 16;
1656                                 printk(KERN_NOTICE "%d:%p reset [%02x], %#lx\n",
1657                                         reset_index, reset_cmd,
1658                                         reset_cmd->scmd->cmnd[0],
1659                                         reset_cmd->scmd->serial_number);
1660
1661                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1662                                 megasas_return_cmd(instance, reset_cmd);
1663                         } else if (reset_cmd->sync_cmd) {
1664                                 printk(KERN_NOTICE "megasas:%p synch cmds"
1665                                                 "reset queue\n",
1666                                                 reset_cmd);
1667
1668                                 reset_cmd->cmd_status = ENODATA;
1669                                 instance->instancet->fire_cmd(instance,
1670                                                 reset_cmd->frame_phys_addr,
1671                                                 0, instance->reg_set);
1672                         } else {
1673                                 printk(KERN_NOTICE "megasas: %p unexpected"
1674                                         "cmds lst\n",
1675                                         reset_cmd);
1676                         }
1677                         reset_index++;
1678                 }
1679
1680                 return SUCCESS;
1681         }
1682
1683         for (i = 0; i < wait_time; i++) {
1684
1685                 int outstanding = atomic_read(&instance->fw_outstanding);
1686
1687                 if (!outstanding)
1688                         break;
1689
1690                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1691                         printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1692                                "commands to complete\n",i,outstanding);
1693                         /*
1694                          * Call cmd completion routine. Cmd to be
1695                          * be completed directly without depending on isr.
1696                          */
1697                         megasas_complete_cmd_dpc((unsigned long)instance);
1698                 }
1699
1700                 msleep(1000);
1701         }
1702
1703         i = 0;
1704         kill_adapter_flag = 0;
1705         do {
1706                 fw_state = instance->instancet->read_fw_status_reg(
1707                                         instance->reg_set) & MFI_STATE_MASK;
1708                 if ((fw_state == MFI_STATE_FAULT) &&
1709                         (instance->disableOnlineCtrlReset == 0)) {
1710                         if (i == 3) {
1711                                 kill_adapter_flag = 2;
1712                                 break;
1713                         }
1714                         megasas_do_ocr(instance);
1715                         kill_adapter_flag = 1;
1716
1717                         /* wait for 1 secs to let FW finish the pending cmds */
1718                         msleep(1000);
1719                 }
1720                 i++;
1721         } while (i <= 3);
1722
1723         if (atomic_read(&instance->fw_outstanding) &&
1724                                         !kill_adapter_flag) {
1725                 if (instance->disableOnlineCtrlReset == 0) {
1726
1727                         megasas_do_ocr(instance);
1728
1729                         /* wait for 5 secs to let FW finish the pending cmds */
1730                         for (i = 0; i < wait_time; i++) {
1731                                 int outstanding =
1732                                         atomic_read(&instance->fw_outstanding);
1733                                 if (!outstanding)
1734                                         return SUCCESS;
1735                                 msleep(1000);
1736                         }
1737                 }
1738         }
1739
1740         if (atomic_read(&instance->fw_outstanding) ||
1741                                         (kill_adapter_flag == 2)) {
1742                 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1743                 /*
1744                 * Send signal to FW to stop processing any pending cmds.
1745                 * The controller will be taken offline by the OS now.
1746                 */
1747                 if ((instance->pdev->device ==
1748                         PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1749                         (instance->pdev->device ==
1750                         PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1751                         writel(MFI_STOP_ADP,
1752                                 &instance->reg_set->reserved_0[0]);
1753                 } else {
1754                         writel(MFI_STOP_ADP,
1755                                 &instance->reg_set->inbound_doorbell);
1756                 }
1757                 megasas_dump_pending_frames(instance);
1758                 spin_lock_irqsave(&instance->hba_lock, flags);
1759                 instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
1760                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1761                 return FAILED;
1762         }
1763
1764         printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1765
1766         return SUCCESS;
1767 }
1768
1769 /**
1770  * megasas_generic_reset -      Generic reset routine
1771  * @scmd:                       Mid-layer SCSI command
1772  *
1773  * This routine implements a generic reset handler for device, bus and host
1774  * reset requests. Device, bus and host specific reset handlers can use this
1775  * function after they do their specific tasks.
1776  */
1777 static int megasas_generic_reset(struct scsi_cmnd *scmd)
1778 {
1779         int ret_val;
1780         struct megasas_instance *instance;
1781
1782         instance = (struct megasas_instance *)scmd->device->host->hostdata;
1783
1784         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n",
1785                  scmd->serial_number, scmd->cmnd[0], scmd->retries);
1786
1787         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1788                 printk(KERN_ERR "megasas: cannot recover from previous reset "
1789                        "failures\n");
1790                 return FAILED;
1791         }
1792
1793         ret_val = megasas_wait_for_outstanding(instance);
1794         if (ret_val == SUCCESS)
1795                 printk(KERN_NOTICE "megasas: reset successful \n");
1796         else
1797                 printk(KERN_ERR "megasas: failed to do reset\n");
1798
1799         return ret_val;
1800 }
1801
1802 /**
1803  * megasas_reset_timer - quiesce the adapter if required
1804  * @scmd:               scsi cmnd
1805  *
1806  * Sets the FW busy flag and reduces the host->can_queue if the
1807  * cmd has not been completed within the timeout period.
1808  */
1809 static enum
1810 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1811 {
1812         struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr;
1813         struct megasas_instance *instance;
1814         unsigned long flags;
1815
1816         if (time_after(jiffies, scmd->jiffies_at_alloc +
1817                                 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1818                 return BLK_EH_NOT_HANDLED;
1819         }
1820
1821         instance = cmd->instance;
1822         if (!(instance->flag & MEGASAS_FW_BUSY)) {
1823                 /* FW is busy, throttle IO */
1824                 spin_lock_irqsave(instance->host->host_lock, flags);
1825
1826                 instance->host->can_queue = 16;
1827                 instance->last_time = jiffies;
1828                 instance->flag |= MEGASAS_FW_BUSY;
1829
1830                 spin_unlock_irqrestore(instance->host->host_lock, flags);
1831         }
1832         return BLK_EH_RESET_TIMER;
1833 }
1834
1835 /**
1836  * megasas_reset_device -       Device reset handler entry point
1837  */
1838 static int megasas_reset_device(struct scsi_cmnd *scmd)
1839 {
1840         int ret;
1841
1842         /*
1843          * First wait for all commands to complete
1844          */
1845         ret = megasas_generic_reset(scmd);
1846
1847         return ret;
1848 }
1849
1850 /**
1851  * megasas_reset_bus_host -     Bus & host reset handler entry point
1852  */
1853 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1854 {
1855         int ret;
1856
1857         /*
1858          * First wait for all commands to complete
1859          */
1860         ret = megasas_generic_reset(scmd);
1861
1862         return ret;
1863 }
1864
1865 /**
1866  * megasas_bios_param - Returns disk geometry for a disk
1867  * @sdev:               device handle
1868  * @bdev:               block device
1869  * @capacity:           drive capacity
1870  * @geom:               geometry parameters
1871  */
1872 static int
1873 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1874                  sector_t capacity, int geom[])
1875 {
1876         int heads;
1877         int sectors;
1878         sector_t cylinders;
1879         unsigned long tmp;
1880         /* Default heads (64) & sectors (32) */
1881         heads = 64;
1882         sectors = 32;
1883
1884         tmp = heads * sectors;
1885         cylinders = capacity;
1886
1887         sector_div(cylinders, tmp);
1888
1889         /*
1890          * Handle extended translation size for logical drives > 1Gb
1891          */
1892
1893         if (capacity >= 0x200000) {
1894                 heads = 255;
1895                 sectors = 63;
1896                 tmp = heads*sectors;
1897                 cylinders = capacity;
1898                 sector_div(cylinders, tmp);
1899         }
1900
1901         geom[0] = heads;
1902         geom[1] = sectors;
1903         geom[2] = cylinders;
1904
1905         return 0;
1906 }
1907
1908 static void megasas_aen_polling(struct work_struct *work);
1909
1910 /**
1911  * megasas_service_aen -        Processes an event notification
1912  * @instance:                   Adapter soft state
1913  * @cmd:                        AEN command completed by the ISR
1914  *
1915  * For AEN, driver sends a command down to FW that is held by the FW till an
1916  * event occurs. When an event of interest occurs, FW completes the command
1917  * that it was previously holding.
1918  *
1919  * This routines sends SIGIO signal to processes that have registered with the
1920  * driver for AEN.
1921  */
1922 static void
1923 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
1924 {
1925         unsigned long flags;
1926         /*
1927          * Don't signal app if it is just an aborted previously registered aen
1928          */
1929         if ((!cmd->abort_aen) && (instance->unload == 0)) {
1930                 spin_lock_irqsave(&poll_aen_lock, flags);
1931                 megasas_poll_wait_aen = 1;
1932                 spin_unlock_irqrestore(&poll_aen_lock, flags);
1933                 wake_up(&megasas_poll_wait);
1934                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
1935         }
1936         else
1937                 cmd->abort_aen = 0;
1938
1939         instance->aen_cmd = NULL;
1940         megasas_return_cmd(instance, cmd);
1941
1942         if ((instance->unload == 0) &&
1943                 ((instance->issuepend_done == 1))) {
1944                 struct megasas_aen_event *ev;
1945                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
1946                 if (!ev) {
1947                         printk(KERN_ERR "megasas_service_aen: out of memory\n");
1948                 } else {
1949                         ev->instance = instance;
1950                         instance->ev = ev;
1951                         INIT_WORK(&ev->hotplug_work, megasas_aen_polling);
1952                         schedule_delayed_work(
1953                                 (struct delayed_work *)&ev->hotplug_work, 0);
1954                 }
1955         }
1956 }
1957
1958 /*
1959  * Scsi host template for megaraid_sas driver
1960  */
1961 static struct scsi_host_template megasas_template = {
1962
1963         .module = THIS_MODULE,
1964         .name = "LSI SAS based MegaRAID driver",
1965         .proc_name = "megaraid_sas",
1966         .slave_configure = megasas_slave_configure,
1967         .slave_alloc = megasas_slave_alloc,
1968         .queuecommand = megasas_queue_command,
1969         .eh_device_reset_handler = megasas_reset_device,
1970         .eh_bus_reset_handler = megasas_reset_bus_host,
1971         .eh_host_reset_handler = megasas_reset_bus_host,
1972         .eh_timed_out = megasas_reset_timer,
1973         .bios_param = megasas_bios_param,
1974         .use_clustering = ENABLE_CLUSTERING,
1975 };
1976
1977 /**
1978  * megasas_complete_int_cmd -   Completes an internal command
1979  * @instance:                   Adapter soft state
1980  * @cmd:                        Command to be completed
1981  *
1982  * The megasas_issue_blocked_cmd() function waits for a command to complete
1983  * after it issues a command. This function wakes up that waiting routine by
1984  * calling wake_up() on the wait queue.
1985  */
1986 static void
1987 megasas_complete_int_cmd(struct megasas_instance *instance,
1988                          struct megasas_cmd *cmd)
1989 {
1990         cmd->cmd_status = cmd->frame->io.cmd_status;
1991
1992         if (cmd->cmd_status == ENODATA) {
1993                 cmd->cmd_status = 0;
1994         }
1995         wake_up(&instance->int_cmd_wait_q);
1996 }
1997
1998 /**
1999  * megasas_complete_abort -     Completes aborting a command
2000  * @instance:                   Adapter soft state
2001  * @cmd:                        Cmd that was issued to abort another cmd
2002  *
2003  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q 
2004  * after it issues an abort on a previously issued command. This function 
2005  * wakes up all functions waiting on the same wait queue.
2006  */
2007 static void
2008 megasas_complete_abort(struct megasas_instance *instance,
2009                        struct megasas_cmd *cmd)
2010 {
2011         if (cmd->sync_cmd) {
2012                 cmd->sync_cmd = 0;
2013                 cmd->cmd_status = 0;
2014                 wake_up(&instance->abort_cmd_wait_q);
2015         }
2016
2017         return;
2018 }
2019
2020 /**
2021  * megasas_complete_cmd -       Completes a command
2022  * @instance:                   Adapter soft state
2023  * @cmd:                        Command to be completed
2024  * @alt_status:                 If non-zero, use this value as status to 
2025  *                              SCSI mid-layer instead of the value returned
2026  *                              by the FW. This should be used if caller wants
2027  *                              an alternate status (as in the case of aborted
2028  *                              commands)
2029  */
2030 static void
2031 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2032                      u8 alt_status)
2033 {
2034         int exception = 0;
2035         struct megasas_header *hdr = &cmd->frame->hdr;
2036         unsigned long flags;
2037
2038         /* flag for the retry reset */
2039         cmd->retry_for_fw_reset = 0;
2040
2041         if (cmd->scmd)
2042                 cmd->scmd->SCp.ptr = NULL;
2043
2044         switch (hdr->cmd) {
2045
2046         case MFI_CMD_PD_SCSI_IO:
2047         case MFI_CMD_LD_SCSI_IO:
2048
2049                 /*
2050                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2051                  * issued either through an IO path or an IOCTL path. If it
2052                  * was via IOCTL, we will send it to internal completion.
2053                  */
2054                 if (cmd->sync_cmd) {
2055                         cmd->sync_cmd = 0;
2056                         megasas_complete_int_cmd(instance, cmd);
2057                         break;
2058                 }
2059
2060         case MFI_CMD_LD_READ:
2061         case MFI_CMD_LD_WRITE:
2062
2063                 if (alt_status) {
2064                         cmd->scmd->result = alt_status << 16;
2065                         exception = 1;
2066                 }
2067
2068                 if (exception) {
2069
2070                         atomic_dec(&instance->fw_outstanding);
2071
2072                         scsi_dma_unmap(cmd->scmd);
2073                         cmd->scmd->scsi_done(cmd->scmd);
2074                         megasas_return_cmd(instance, cmd);
2075
2076                         break;
2077                 }
2078
2079                 switch (hdr->cmd_status) {
2080
2081                 case MFI_STAT_OK:
2082                         cmd->scmd->result = DID_OK << 16;
2083                         break;
2084
2085                 case MFI_STAT_SCSI_IO_FAILED:
2086                 case MFI_STAT_LD_INIT_IN_PROGRESS:
2087                         cmd->scmd->result =
2088                             (DID_ERROR << 16) | hdr->scsi_status;
2089                         break;
2090
2091                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2092
2093                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2094
2095                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2096                                 memset(cmd->scmd->sense_buffer, 0,
2097                                        SCSI_SENSE_BUFFERSIZE);
2098                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2099                                        hdr->sense_len);
2100
2101                                 cmd->scmd->result |= DRIVER_SENSE << 24;
2102                         }
2103
2104                         break;
2105
2106                 case MFI_STAT_LD_OFFLINE:
2107                 case MFI_STAT_DEVICE_NOT_FOUND:
2108                         cmd->scmd->result = DID_BAD_TARGET << 16;
2109                         break;
2110
2111                 default:
2112                         printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2113                                hdr->cmd_status);
2114                         cmd->scmd->result = DID_ERROR << 16;
2115                         break;
2116                 }
2117
2118                 atomic_dec(&instance->fw_outstanding);
2119
2120                 scsi_dma_unmap(cmd->scmd);
2121                 cmd->scmd->scsi_done(cmd->scmd);
2122                 megasas_return_cmd(instance, cmd);
2123
2124                 break;
2125
2126         case MFI_CMD_SMP:
2127         case MFI_CMD_STP:
2128         case MFI_CMD_DCMD:
2129                 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2130                         cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2131                         spin_lock_irqsave(&poll_aen_lock, flags);
2132                         megasas_poll_wait_aen = 0;
2133                         spin_unlock_irqrestore(&poll_aen_lock, flags);
2134                 }
2135
2136                 /*
2137                  * See if got an event notification
2138                  */
2139                 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2140                         megasas_service_aen(instance, cmd);
2141                 else
2142                         megasas_complete_int_cmd(instance, cmd);
2143
2144                 break;
2145
2146         case MFI_CMD_ABORT:
2147                 /*
2148                  * Cmd issued to abort another cmd returned
2149                  */
2150                 megasas_complete_abort(instance, cmd);
2151                 break;
2152
2153         default:
2154                 printk("megasas: Unknown command completed! [0x%X]\n",
2155                        hdr->cmd);
2156                 break;
2157         }
2158 }
2159
2160 /**
2161  * megasas_issue_pending_cmds_again -   issue all pending cmds
2162  *                                      in FW again because of the fw reset
2163  * @instance:                           Adapter soft state
2164  */
2165 static inline void
2166 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2167 {
2168         struct megasas_cmd *cmd;
2169         struct list_head clist_local;
2170         union megasas_evt_class_locale class_locale;
2171         unsigned long flags;
2172         u32 seq_num;
2173
2174         INIT_LIST_HEAD(&clist_local);
2175         spin_lock_irqsave(&instance->hba_lock, flags);
2176         list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2177         spin_unlock_irqrestore(&instance->hba_lock, flags);
2178
2179         while (!list_empty(&clist_local)) {
2180                 cmd     = list_entry((&clist_local)->next,
2181                                         struct megasas_cmd, list);
2182                 list_del_init(&cmd->list);
2183
2184                 if (cmd->sync_cmd || cmd->scmd) {
2185                         printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2186                                 "detected to be pending while HBA reset.\n",
2187                                         cmd, cmd->scmd, cmd->sync_cmd);
2188
2189                         cmd->retry_for_fw_reset++;
2190
2191                         if (cmd->retry_for_fw_reset == 3) {
2192                                 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2193                                         "was tried multiple times during reset."
2194                                         "Shutting down the HBA\n",
2195                                         cmd, cmd->scmd, cmd->sync_cmd);
2196                                 megaraid_sas_kill_hba(instance);
2197
2198                                 instance->adprecovery =
2199                                                 MEGASAS_HW_CRITICAL_ERROR;
2200                                 return;
2201                         }
2202                 }
2203
2204                 if (cmd->sync_cmd == 1) {
2205                         if (cmd->scmd) {
2206                                 printk(KERN_NOTICE "megaraid_sas: unexpected"
2207                                         "cmd attached to internal command!\n");
2208                         }
2209                         printk(KERN_NOTICE "megasas: %p synchronous cmd"
2210                                                 "on the internal reset queue,"
2211                                                 "issue it again.\n", cmd);
2212                         cmd->cmd_status = ENODATA;
2213                         instance->instancet->fire_cmd(instance,
2214                                                         cmd->frame_phys_addr ,
2215                                                         0, instance->reg_set);
2216                 } else if (cmd->scmd) {
2217                         printk(KERN_NOTICE "megasas: %p scsi cmd [%02x],%#lx"
2218                         "detected on the internal queue, issue again.\n",
2219                         cmd, cmd->scmd->cmnd[0], cmd->scmd->serial_number);
2220
2221                         atomic_inc(&instance->fw_outstanding);
2222                         instance->instancet->fire_cmd(instance,
2223                                         cmd->frame_phys_addr,
2224                                         cmd->frame_count-1, instance->reg_set);
2225                 } else {
2226                         printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2227                                 "internal reset defer list while re-issue!!\n",
2228                                 cmd);
2229                 }
2230         }
2231
2232         if (instance->aen_cmd) {
2233                 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2234                 megasas_return_cmd(instance, instance->aen_cmd);
2235
2236                 instance->aen_cmd       = NULL;
2237         }
2238
2239         /*
2240         * Initiate AEN (Asynchronous Event Notification)
2241         */
2242         seq_num = instance->last_seq_num;
2243         class_locale.members.reserved = 0;
2244         class_locale.members.locale = MR_EVT_LOCALE_ALL;
2245         class_locale.members.class = MR_EVT_CLASS_DEBUG;
2246
2247         megasas_register_aen(instance, seq_num, class_locale.word);
2248 }
2249
2250 /**
2251  * Move the internal reset pending commands to a deferred queue.
2252  *
2253  * We move the commands pending at internal reset time to a
2254  * pending queue. This queue would be flushed after successful
2255  * completion of the internal reset sequence. if the internal reset
2256  * did not complete in time, the kernel reset handler would flush
2257  * these commands.
2258  **/
2259 static void
2260 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2261 {
2262         struct megasas_cmd *cmd;
2263         int i;
2264         u32 max_cmd = instance->max_fw_cmds;
2265         u32 defer_index;
2266         unsigned long flags;
2267
2268         defer_index     = 0;
2269         spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2270         for (i = 0; i < max_cmd; i++) {
2271                 cmd = instance->cmd_list[i];
2272                 if (cmd->sync_cmd == 1 || cmd->scmd) {
2273                         printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2274                                         "on the defer queue as internal\n",
2275                                 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2276
2277                         if (!list_empty(&cmd->list)) {
2278                                 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2279                                         " moving this cmd:%p, %d %p, it was"
2280                                         "discovered on some list?\n",
2281                                         cmd, cmd->sync_cmd, cmd->scmd);
2282
2283                                 list_del_init(&cmd->list);
2284                         }
2285                         defer_index++;
2286                         list_add_tail(&cmd->list,
2287                                 &instance->internal_reset_pending_q);
2288                 }
2289         }
2290         spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2291 }
2292
2293
2294 static void
2295 process_fw_state_change_wq(struct work_struct *work)
2296 {
2297         struct megasas_instance *instance =
2298                 container_of(work, struct megasas_instance, work_init);
2299         u32 wait;
2300         unsigned long flags;
2301
2302         if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2303                 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2304                                 instance->adprecovery);
2305                 return ;
2306         }
2307
2308         if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2309                 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2310                                         "state, restarting it...\n");
2311
2312                 instance->instancet->disable_intr(instance->reg_set);
2313                 atomic_set(&instance->fw_outstanding, 0);
2314
2315                 atomic_set(&instance->fw_reset_no_pci_access, 1);
2316                 instance->instancet->adp_reset(instance, instance->reg_set);
2317                 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2318
2319                 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2320                                         "initiating next stage...\n");
2321
2322                 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2323                                         "state 2 starting...\n");
2324
2325                 /*waitting for about 20 second before start the second init*/
2326                 for (wait = 0; wait < 30; wait++) {
2327                         msleep(1000);
2328                 }
2329
2330                 if (megasas_transition_to_ready(instance)) {
2331                         printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2332
2333                         megaraid_sas_kill_hba(instance);
2334                         instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
2335                         return ;
2336                 }
2337
2338                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2339                         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2340                         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2341                         ) {
2342                         *instance->consumer = *instance->producer;
2343                 } else {
2344                         *instance->consumer = 0;
2345                         *instance->producer = 0;
2346                 }
2347
2348                 megasas_issue_init_mfi(instance);
2349
2350                 spin_lock_irqsave(&instance->hba_lock, flags);
2351                 instance->adprecovery   = MEGASAS_HBA_OPERATIONAL;
2352                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2353                 instance->instancet->enable_intr(instance->reg_set);
2354
2355                 megasas_issue_pending_cmds_again(instance);
2356                 instance->issuepend_done = 1;
2357         }
2358         return ;
2359 }
2360
2361 /**
2362  * megasas_deplete_reply_queue -        Processes all completed commands
2363  * @instance:                           Adapter soft state
2364  * @alt_status:                         Alternate status to be returned to
2365  *                                      SCSI mid-layer instead of the status
2366  *                                      returned by the FW
2367  * Note: this must be called with hba lock held
2368  */
2369 static int
2370 megasas_deplete_reply_queue(struct megasas_instance *instance,
2371                                         u8 alt_status)
2372 {
2373         u32 mfiStatus;
2374         u32 fw_state;
2375
2376         if ((mfiStatus = instance->instancet->check_reset(instance,
2377                                         instance->reg_set)) == 1) {
2378                 return IRQ_HANDLED;
2379         }
2380
2381         if ((mfiStatus = instance->instancet->clear_intr(
2382                                                 instance->reg_set)
2383                                                 ) == 0) {
2384                 return IRQ_NONE;
2385         }
2386
2387         instance->mfiStatus = mfiStatus;
2388
2389         if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2390                 fw_state = instance->instancet->read_fw_status_reg(
2391                                 instance->reg_set) & MFI_STATE_MASK;
2392
2393                 if (fw_state != MFI_STATE_FAULT) {
2394                         printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2395                                                 fw_state);
2396                 }
2397
2398                 if ((fw_state == MFI_STATE_FAULT) &&
2399                                 (instance->disableOnlineCtrlReset == 0)) {
2400                         printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2401
2402                         if ((instance->pdev->device ==
2403                                         PCI_DEVICE_ID_LSI_SAS1064R) ||
2404                                 (instance->pdev->device ==
2405                                         PCI_DEVICE_ID_DELL_PERC5) ||
2406                                 (instance->pdev->device ==
2407                                         PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2408
2409                                 *instance->consumer =
2410                                         MEGASAS_ADPRESET_INPROG_SIGN;
2411                         }
2412
2413
2414                         instance->instancet->disable_intr(instance->reg_set);
2415                         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
2416                         instance->issuepend_done = 0;
2417
2418                         atomic_set(&instance->fw_outstanding, 0);
2419                         megasas_internal_reset_defer_cmds(instance);
2420
2421                         printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2422                                         fw_state, instance->adprecovery);
2423
2424                         schedule_work(&instance->work_init);
2425                         return IRQ_HANDLED;
2426
2427                 } else {
2428                         printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2429                                 fw_state, instance->disableOnlineCtrlReset);
2430                 }
2431         }
2432
2433         tasklet_schedule(&instance->isr_tasklet);
2434         return IRQ_HANDLED;
2435 }
2436 /**
2437  * megasas_isr - isr entry point
2438  */
2439 static irqreturn_t megasas_isr(int irq, void *devp)
2440 {
2441         struct megasas_instance *instance;
2442         unsigned long flags;
2443         irqreturn_t     rc;
2444
2445         if (atomic_read(
2446                 &(((struct megasas_instance *)devp)->fw_reset_no_pci_access)))
2447                 return IRQ_HANDLED;
2448
2449         instance = (struct megasas_instance *)devp;
2450
2451         spin_lock_irqsave(&instance->hba_lock, flags);
2452         rc =  megasas_deplete_reply_queue(instance, DID_OK);
2453         spin_unlock_irqrestore(&instance->hba_lock, flags);
2454
2455         return rc;
2456 }
2457
2458 /**
2459  * megasas_transition_to_ready -        Move the FW to READY state
2460  * @instance:                           Adapter soft state
2461  *
2462  * During the initialization, FW passes can potentially be in any one of
2463  * several possible states. If the FW in operational, waiting-for-handshake
2464  * states, driver must take steps to bring it to ready state. Otherwise, it
2465  * has to wait for the ready state.
2466  */
2467 static int
2468 megasas_transition_to_ready(struct megasas_instance* instance)
2469 {
2470         int i;
2471         u8 max_wait;
2472         u32 fw_state;
2473         u32 cur_state;
2474         u32 abs_state, curr_abs_state;
2475
2476         fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2477
2478         if (fw_state != MFI_STATE_READY)
2479                 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2480                        " state\n");
2481
2482         while (fw_state != MFI_STATE_READY) {
2483
2484                 abs_state =
2485                 instance->instancet->read_fw_status_reg(instance->reg_set);
2486
2487                 switch (fw_state) {
2488
2489                 case MFI_STATE_FAULT:
2490
2491                         printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2492                         return -ENODEV;
2493
2494                 case MFI_STATE_WAIT_HANDSHAKE:
2495                         /*
2496                          * Set the CLR bit in inbound doorbell
2497                          */
2498                         if ((instance->pdev->device ==
2499                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2500                                 (instance->pdev->device ==
2501                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2502
2503                                 writel(
2504                                   MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2505                                   &instance->reg_set->reserved_0[0]);
2506                         } else {
2507                                 writel(
2508                                     MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2509                                         &instance->reg_set->inbound_doorbell);
2510                         }
2511
2512                         max_wait = MEGASAS_RESET_WAIT_TIME;
2513                         cur_state = MFI_STATE_WAIT_HANDSHAKE;
2514                         break;
2515
2516                 case MFI_STATE_BOOT_MESSAGE_PENDING:
2517                         if ((instance->pdev->device ==
2518                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2519                         (instance->pdev->device ==
2520                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2521                                 writel(MFI_INIT_HOTPLUG,
2522                                 &instance->reg_set->reserved_0[0]);
2523                         } else
2524                                 writel(MFI_INIT_HOTPLUG,
2525                                         &instance->reg_set->inbound_doorbell);
2526
2527                         max_wait = MEGASAS_RESET_WAIT_TIME;
2528                         cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2529                         break;
2530
2531                 case MFI_STATE_OPERATIONAL:
2532                         /*
2533                          * Bring it to READY state; assuming max wait 10 secs
2534                          */
2535                         instance->instancet->disable_intr(instance->reg_set);
2536                         if ((instance->pdev->device ==
2537                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2538                                 (instance->pdev->device ==
2539                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2540                                 writel(MFI_RESET_FLAGS,
2541                                         &instance->reg_set->reserved_0[0]);
2542                         } else
2543                                 writel(MFI_RESET_FLAGS,
2544                                         &instance->reg_set->inbound_doorbell);
2545
2546                         max_wait = MEGASAS_RESET_WAIT_TIME;
2547                         cur_state = MFI_STATE_OPERATIONAL;
2548                         break;
2549
2550                 case MFI_STATE_UNDEFINED:
2551                         /*
2552                          * This state should not last for more than 2 seconds
2553                          */
2554                         max_wait = MEGASAS_RESET_WAIT_TIME;
2555                         cur_state = MFI_STATE_UNDEFINED;
2556                         break;
2557
2558                 case MFI_STATE_BB_INIT:
2559                         max_wait = MEGASAS_RESET_WAIT_TIME;
2560                         cur_state = MFI_STATE_BB_INIT;
2561                         break;
2562
2563                 case MFI_STATE_FW_INIT:
2564                         max_wait = MEGASAS_RESET_WAIT_TIME;
2565                         cur_state = MFI_STATE_FW_INIT;
2566                         break;
2567
2568                 case MFI_STATE_FW_INIT_2:
2569                         max_wait = MEGASAS_RESET_WAIT_TIME;
2570                         cur_state = MFI_STATE_FW_INIT_2;
2571                         break;
2572
2573                 case MFI_STATE_DEVICE_SCAN:
2574                         max_wait = MEGASAS_RESET_WAIT_TIME;
2575                         cur_state = MFI_STATE_DEVICE_SCAN;
2576                         break;
2577
2578                 case MFI_STATE_FLUSH_CACHE:
2579                         max_wait = MEGASAS_RESET_WAIT_TIME;
2580                         cur_state = MFI_STATE_FLUSH_CACHE;
2581                         break;
2582
2583                 default:
2584                         printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2585                                fw_state);
2586                         return -ENODEV;
2587                 }
2588
2589                 /*
2590                  * The cur_state should not last for more than max_wait secs
2591                  */
2592                 for (i = 0; i < (max_wait * 1000); i++) {
2593                         fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &  
2594                                         MFI_STATE_MASK ;
2595                 curr_abs_state =
2596                 instance->instancet->read_fw_status_reg(instance->reg_set);
2597
2598                         if (abs_state == curr_abs_state) {
2599                                 msleep(1);
2600                         } else
2601                                 break;
2602                 }
2603
2604                 /*
2605                  * Return error if fw_state hasn't changed after max_wait
2606                  */
2607                 if (curr_abs_state == abs_state) {
2608                         printk(KERN_DEBUG "FW state [%d] hasn't changed "
2609                                "in %d secs\n", fw_state, max_wait);
2610                         return -ENODEV;
2611                 }
2612         }
2613         printk(KERN_INFO "megasas: FW now in Ready state\n");
2614
2615         return 0;
2616 }
2617
2618 /**
2619  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
2620  * @instance:                           Adapter soft state
2621  */
2622 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2623 {
2624         int i;
2625         u32 max_cmd = instance->max_fw_cmds;
2626         struct megasas_cmd *cmd;
2627
2628         if (!instance->frame_dma_pool)
2629                 return;
2630
2631         /*
2632          * Return all frames to pool
2633          */
2634         for (i = 0; i < max_cmd; i++) {
2635
2636                 cmd = instance->cmd_list[i];
2637
2638                 if (cmd->frame)
2639                         pci_pool_free(instance->frame_dma_pool, cmd->frame,
2640                                       cmd->frame_phys_addr);
2641
2642                 if (cmd->sense)
2643                         pci_pool_free(instance->sense_dma_pool, cmd->sense,
2644                                       cmd->sense_phys_addr);
2645         }
2646
2647         /*
2648          * Now destroy the pool itself
2649          */
2650         pci_pool_destroy(instance->frame_dma_pool);
2651         pci_pool_destroy(instance->sense_dma_pool);
2652
2653         instance->frame_dma_pool = NULL;
2654         instance->sense_dma_pool = NULL;
2655 }
2656
2657 /**
2658  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
2659  * @instance:                   Adapter soft state
2660  *
2661  * Each command packet has an embedded DMA memory buffer that is used for
2662  * filling MFI frame and the SG list that immediately follows the frame. This
2663  * function creates those DMA memory buffers for each command packet by using
2664  * PCI pool facility.
2665  */
2666 static int megasas_create_frame_pool(struct megasas_instance *instance)
2667 {
2668         int i;
2669         u32 max_cmd;
2670         u32 sge_sz;
2671         u32 sgl_sz;
2672         u32 total_sz;
2673         u32 frame_count;
2674         struct megasas_cmd *cmd;
2675
2676         max_cmd = instance->max_fw_cmds;
2677
2678         /*
2679          * Size of our frame is 64 bytes for MFI frame, followed by max SG
2680          * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
2681          */
2682         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2683             sizeof(struct megasas_sge32);
2684
2685         if (instance->flag_ieee) {
2686                 sge_sz = sizeof(struct megasas_sge_skinny);
2687         }
2688
2689         /*
2690          * Calculated the number of 64byte frames required for SGL
2691          */
2692         sgl_sz = sge_sz * instance->max_num_sge;
2693         frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2694         frame_count = 15;
2695
2696         /*
2697          * We need one extra frame for the MFI command
2698          */
2699         frame_count++;
2700
2701         total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2702         /*
2703          * Use DMA pool facility provided by PCI layer
2704          */
2705         instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2706                                                    instance->pdev, total_sz, 64,
2707                                                    0);
2708
2709         if (!instance->frame_dma_pool) {
2710                 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2711                 return -ENOMEM;
2712         }
2713
2714         instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2715                                                    instance->pdev, 128, 4, 0);
2716
2717         if (!instance->sense_dma_pool) {
2718                 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2719
2720                 pci_pool_destroy(instance->frame_dma_pool);
2721                 instance->frame_dma_pool = NULL;
2722
2723                 return -ENOMEM;
2724         }
2725
2726         /*
2727          * Allocate and attach a frame to each of the commands in cmd_list.
2728          * By making cmd->index as the context instead of the &cmd, we can
2729          * always use 32bit context regardless of the architecture
2730          */
2731         for (i = 0; i < max_cmd; i++) {
2732
2733                 cmd = instance->cmd_list[i];
2734
2735                 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2736                                             GFP_KERNEL, &cmd->frame_phys_addr);
2737
2738                 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2739                                             GFP_KERNEL, &cmd->sense_phys_addr);
2740
2741                 /*
2742                  * megasas_teardown_frame_pool() takes care of freeing
2743                  * whatever has been allocated
2744                  */
2745                 if (!cmd->frame || !cmd->sense) {
2746                         printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2747                         megasas_teardown_frame_pool(instance);
2748                         return -ENOMEM;
2749                 }
2750
2751                 memset(cmd->frame, 0, total_sz);
2752                 cmd->frame->io.context = cmd->index;
2753                 cmd->frame->io.pad_0 = 0;
2754         }
2755
2756         return 0;
2757 }
2758
2759 /**
2760  * megasas_free_cmds -  Free all the cmds in the free cmd pool
2761  * @instance:           Adapter soft state
2762  */
2763 static void megasas_free_cmds(struct megasas_instance *instance)
2764 {
2765         int i;
2766         /* First free the MFI frame pool */
2767         megasas_teardown_frame_pool(instance);
2768
2769         /* Free all the commands in the cmd_list */
2770         for (i = 0; i < instance->max_fw_cmds; i++)
2771                 kfree(instance->cmd_list[i]);
2772
2773         /* Free the cmd_list buffer itself */
2774         kfree(instance->cmd_list);
2775         instance->cmd_list = NULL;
2776
2777         INIT_LIST_HEAD(&instance->cmd_pool);
2778 }
2779
2780 /**
2781  * megasas_alloc_cmds - Allocates the command packets
2782  * @instance:           Adapter soft state
2783  *
2784  * Each command that is issued to the FW, whether IO commands from the OS or
2785  * internal commands like IOCTLs, are wrapped in local data structure called
2786  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
2787  * the FW.
2788  *
2789  * Each frame has a 32-bit field called context (tag). This context is used
2790  * to get back the megasas_cmd from the frame when a frame gets completed in
2791  * the ISR. Typically the address of the megasas_cmd itself would be used as
2792  * the context. But we wanted to keep the differences between 32 and 64 bit
2793  * systems to the mininum. We always use 32 bit integers for the context. In
2794  * this driver, the 32 bit values are the indices into an array cmd_list.
2795  * This array is used only to look up the megasas_cmd given the context. The
2796  * free commands themselves are maintained in a linked list called cmd_pool.
2797  */
2798 static int megasas_alloc_cmds(struct megasas_instance *instance)
2799 {
2800         int i;
2801         int j;
2802         u32 max_cmd;
2803         struct megasas_cmd *cmd;
2804
2805         max_cmd = instance->max_fw_cmds;
2806
2807         /*
2808          * instance->cmd_list is an array of struct megasas_cmd pointers.
2809          * Allocate the dynamic array first and then allocate individual
2810          * commands.
2811          */
2812         instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
2813
2814         if (!instance->cmd_list) {
2815                 printk(KERN_DEBUG "megasas: out of memory\n");
2816                 return -ENOMEM;
2817         }
2818
2819
2820         for (i = 0; i < max_cmd; i++) {
2821                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
2822                                                 GFP_KERNEL);
2823
2824                 if (!instance->cmd_list[i]) {
2825
2826                         for (j = 0; j < i; j++)
2827                                 kfree(instance->cmd_list[j]);
2828
2829                         kfree(instance->cmd_list);
2830                         instance->cmd_list = NULL;
2831
2832                         return -ENOMEM;
2833                 }
2834         }
2835
2836         /*
2837          * Add all the commands to command pool (instance->cmd_pool)
2838          */
2839         for (i = 0; i < max_cmd; i++) {
2840                 cmd = instance->cmd_list[i];
2841                 memset(cmd, 0, sizeof(struct megasas_cmd));
2842                 cmd->index = i;
2843                 cmd->scmd = NULL;
2844                 cmd->instance = instance;
2845
2846                 list_add_tail(&cmd->list, &instance->cmd_pool);
2847         }
2848
2849         /*
2850          * Create a frame pool and assign one frame to each cmd
2851          */
2852         if (megasas_create_frame_pool(instance)) {
2853                 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
2854                 megasas_free_cmds(instance);
2855         }
2856
2857         return 0;
2858 }
2859
2860 /*
2861  * megasas_get_pd_list_info -   Returns FW's pd_list structure
2862  * @instance:                           Adapter soft state
2863  * @pd_list:                            pd_list structure
2864  *
2865  * Issues an internal command (DCMD) to get the FW's controller PD
2866  * list structure.  This information is mainly used to find out SYSTEM
2867  * supported by the FW.
2868  */
2869 static int
2870 megasas_get_pd_list(struct megasas_instance *instance)
2871 {
2872         int ret = 0, pd_index = 0;
2873         struct megasas_cmd *cmd;
2874         struct megasas_dcmd_frame *dcmd;
2875         struct MR_PD_LIST *ci;
2876         struct MR_PD_ADDRESS *pd_addr;
2877         dma_addr_t ci_h = 0;
2878
2879         cmd = megasas_get_cmd(instance);
2880
2881         if (!cmd) {
2882                 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
2883                 return -ENOMEM;
2884         }
2885
2886         dcmd = &cmd->frame->dcmd;
2887
2888         ci = pci_alloc_consistent(instance->pdev,
2889                   MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
2890
2891         if (!ci) {
2892                 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
2893                 megasas_return_cmd(instance, cmd);
2894                 return -ENOMEM;
2895         }
2896
2897         memset(ci, 0, sizeof(*ci));
2898         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2899
2900         dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
2901         dcmd->mbox.b[1] = 0;
2902         dcmd->cmd = MFI_CMD_DCMD;
2903         dcmd->cmd_status = 0xFF;
2904         dcmd->sge_count = 1;
2905         dcmd->flags = MFI_FRAME_DIR_READ;
2906         dcmd->timeout = 0;
2907         dcmd->pad_0 = 0;
2908         dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2909         dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
2910         dcmd->sgl.sge32[0].phys_addr = ci_h;
2911         dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
2912
2913         if (!megasas_issue_polled(instance, cmd)) {
2914                 ret = 0;
2915         } else {
2916                 ret = -1;
2917         }
2918
2919         /*
2920         * the following function will get the instance PD LIST.
2921         */
2922
2923         pd_addr = ci->addr;
2924
2925         if ( ret == 0 &&
2926                 (ci->count <
2927                   (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
2928
2929                 memset(instance->pd_list, 0,
2930                         MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
2931
2932                 for (pd_index = 0; pd_index < ci->count; pd_index++) {
2933
2934                         instance->pd_list[pd_addr->deviceId].tid        =
2935                                                         pd_addr->deviceId;
2936                         instance->pd_list[pd_addr->deviceId].driveType  =
2937                                                         pd_addr->scsiDevType;
2938                         instance->pd_list[pd_addr->deviceId].driveState =
2939                                                         MR_PD_STATE_SYSTEM;
2940                         pd_addr++;
2941                 }
2942         }
2943
2944         pci_free_consistent(instance->pdev,
2945                                 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
2946                                 ci, ci_h);
2947         megasas_return_cmd(instance, cmd);
2948
2949         return ret;
2950 }
2951
2952 /*
2953  * megasas_get_ld_list_info -   Returns FW's ld_list structure
2954  * @instance:                           Adapter soft state
2955  * @ld_list:                            ld_list structure
2956  *
2957  * Issues an internal command (DCMD) to get the FW's controller PD
2958  * list structure.  This information is mainly used to find out SYSTEM
2959  * supported by the FW.
2960  */
2961 static int
2962 megasas_get_ld_list(struct megasas_instance *instance)
2963 {
2964         int ret = 0, ld_index = 0, ids = 0;
2965         struct megasas_cmd *cmd;
2966         struct megasas_dcmd_frame *dcmd;
2967         struct MR_LD_LIST *ci;
2968         dma_addr_t ci_h = 0;
2969
2970         cmd = megasas_get_cmd(instance);
2971
2972         if (!cmd) {
2973                 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
2974                 return -ENOMEM;
2975         }
2976
2977         dcmd = &cmd->frame->dcmd;
2978
2979         ci = pci_alloc_consistent(instance->pdev,
2980                                 sizeof(struct MR_LD_LIST),
2981                                 &ci_h);
2982
2983         if (!ci) {
2984                 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
2985                 megasas_return_cmd(instance, cmd);
2986                 return -ENOMEM;
2987         }
2988
2989         memset(ci, 0, sizeof(*ci));
2990         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2991
2992         dcmd->cmd = MFI_CMD_DCMD;
2993         dcmd->cmd_status = 0xFF;
2994         dcmd->sge_count = 1;
2995         dcmd->flags = MFI_FRAME_DIR_READ;
2996         dcmd->timeout = 0;
2997         dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
2998         dcmd->opcode = MR_DCMD_LD_GET_LIST;
2999         dcmd->sgl.sge32[0].phys_addr = ci_h;
3000         dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3001         dcmd->pad_0  = 0;
3002
3003         if (!megasas_issue_polled(instance, cmd)) {
3004                 ret = 0;
3005         } else {
3006                 ret = -1;
3007         }
3008
3009         /* the following function will get the instance PD LIST */
3010
3011         if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3012                 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3013
3014                 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3015                         if (ci->ldList[ld_index].state != 0) {
3016                                 ids = ci->ldList[ld_index].ref.targetId;
3017                                 instance->ld_ids[ids] =
3018                                         ci->ldList[ld_index].ref.targetId;
3019                         }
3020                 }
3021         }
3022
3023         pci_free_consistent(instance->pdev,
3024                                 sizeof(struct MR_LD_LIST),
3025                                 ci,
3026                                 ci_h);
3027
3028         megasas_return_cmd(instance, cmd);
3029         return ret;
3030 }
3031
3032 /**
3033  * megasas_get_controller_info -        Returns FW's controller structure
3034  * @instance:                           Adapter soft state
3035  * @ctrl_info:                          Controller information structure
3036  *
3037  * Issues an internal command (DCMD) to get the FW's controller structure.
3038  * This information is mainly used to find out the maximum IO transfer per
3039  * command supported by the FW.
3040  */
3041 static int
3042 megasas_get_ctrl_info(struct megasas_instance *instance,
3043                       struct megasas_ctrl_info *ctrl_info)
3044 {
3045         int ret = 0;
3046         struct megasas_cmd *cmd;
3047         struct megasas_dcmd_frame *dcmd;
3048         struct megasas_ctrl_info *ci;
3049         dma_addr_t ci_h = 0;
3050
3051         cmd = megasas_get_cmd(instance);
3052
3053         if (!cmd) {
3054                 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3055                 return -ENOMEM;
3056         }
3057
3058         dcmd = &cmd->frame->dcmd;
3059
3060         ci = pci_alloc_consistent(instance->pdev,
3061                                   sizeof(struct megasas_ctrl_info), &ci_h);
3062
3063         if (!ci) {
3064                 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3065                 megasas_return_cmd(instance, cmd);
3066                 return -ENOMEM;
3067         }
3068
3069         memset(ci, 0, sizeof(*ci));
3070         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3071
3072         dcmd->cmd = MFI_CMD_DCMD;
3073         dcmd->cmd_status = 0xFF;
3074         dcmd->sge_count = 1;
3075         dcmd->flags = MFI_FRAME_DIR_READ;
3076         dcmd->timeout = 0;
3077         dcmd->pad_0 = 0;
3078         dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3079         dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3080         dcmd->sgl.sge32[0].phys_addr = ci_h;
3081         dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3082
3083         if (!megasas_issue_polled(instance, cmd)) {
3084                 ret = 0;
3085                 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3086         } else {
3087                 ret = -1;
3088         }
3089
3090         pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3091                             ci, ci_h);
3092
3093         megasas_return_cmd(instance, cmd);
3094         return ret;
3095 }
3096
3097 /**
3098  * megasas_issue_init_mfi -     Initializes the FW
3099  * @instance:           Adapter soft state
3100  *
3101  * Issues the INIT MFI cmd
3102  */
3103 static int
3104 megasas_issue_init_mfi(struct megasas_instance *instance)
3105 {
3106         u32 context;
3107
3108         struct megasas_cmd *cmd;
3109
3110         struct megasas_init_frame *init_frame;
3111         struct megasas_init_queue_info *initq_info;
3112         dma_addr_t init_frame_h;
3113         dma_addr_t initq_info_h;
3114
3115         /*
3116          * Prepare a init frame. Note the init frame points to queue info
3117          * structure. Each frame has SGL allocated after first 64 bytes. For
3118          * this frame - since we don't need any SGL - we use SGL's space as
3119          * queue info structure
3120          *
3121          * We will not get a NULL command below. We just created the pool.
3122          */
3123         cmd = megasas_get_cmd(instance);
3124
3125         init_frame = (struct megasas_init_frame *)cmd->frame;
3126         initq_info = (struct megasas_init_queue_info *)
3127                 ((unsigned long)init_frame + 64);
3128
3129         init_frame_h = cmd->frame_phys_addr;
3130         initq_info_h = init_frame_h + 64;
3131
3132         context = init_frame->context;
3133         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3134         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3135         init_frame->context = context;
3136
3137         initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3138         initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3139
3140         initq_info->producer_index_phys_addr_lo = instance->producer_h;
3141         initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3142
3143         init_frame->cmd = MFI_CMD_INIT;
3144         init_frame->cmd_status = 0xFF;
3145         init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3146
3147         init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3148
3149         /*
3150          * disable the intr before firing the init frame to FW
3151          */
3152         instance->instancet->disable_intr(instance->reg_set);
3153
3154         /*
3155          * Issue the init frame in polled mode
3156          */
3157
3158         if (megasas_issue_polled(instance, cmd)) {
3159                 printk(KERN_ERR "megasas: Failed to init firmware\n");
3160                 megasas_return_cmd(instance, cmd);
3161                 goto fail_fw_init;
3162         }
3163
3164         megasas_return_cmd(instance, cmd);
3165
3166         return 0;
3167
3168 fail_fw_init:
3169         return -EINVAL;
3170 }
3171
3172 /**
3173  * megasas_start_timer - Initializes a timer object
3174  * @instance:           Adapter soft state
3175  * @timer:              timer object to be initialized
3176  * @fn:                 timer function
3177  * @interval:           time interval between timer function call
3178  */
3179 static inline void
3180 megasas_start_timer(struct megasas_instance *instance,
3181                         struct timer_list *timer,
3182                         void *fn, unsigned long interval)
3183 {
3184         init_timer(timer);
3185         timer->expires = jiffies + interval;
3186         timer->data = (unsigned long)instance;
3187         timer->function = fn;
3188         add_timer(timer);
3189 }
3190
3191 /**
3192  * megasas_io_completion_timer - Timer fn
3193  * @instance_addr:      Address of adapter soft state
3194  *
3195  * Schedules tasklet for cmd completion
3196  * if poll_mode_io is set
3197  */
3198 static void
3199 megasas_io_completion_timer(unsigned long instance_addr)
3200 {
3201         struct megasas_instance *instance =
3202                         (struct megasas_instance *)instance_addr;
3203
3204         if (atomic_read(&instance->fw_outstanding))
3205                 tasklet_schedule(&instance->isr_tasklet);
3206
3207         /* Restart timer */
3208         if (poll_mode_io)
3209                 mod_timer(&instance->io_completion_timer,
3210                         jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL);
3211 }
3212
3213 /**
3214  * megasas_init_mfi -   Initializes the FW
3215  * @instance:           Adapter soft state
3216  *
3217  * This is the main function for initializing MFI firmware.
3218  */
3219 static int megasas_init_mfi(struct megasas_instance *instance)
3220 {
3221         u32 context_sz;
3222         u32 reply_q_sz;
3223         u32 max_sectors_1;
3224         u32 max_sectors_2;
3225         u32 tmp_sectors;
3226         struct megasas_register_set __iomem *reg_set;
3227         struct megasas_ctrl_info *ctrl_info;
3228         /*
3229          * Map the message registers
3230          */
3231         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3232                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3233                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3234                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) {
3235                 instance->base_addr = pci_resource_start(instance->pdev, 1);
3236         } else {
3237                 instance->base_addr = pci_resource_start(instance->pdev, 0);
3238         }
3239
3240         if (pci_request_selected_regions(instance->pdev,
3241                 pci_select_bars(instance->pdev, IORESOURCE_MEM),
3242                 "megasas: LSI")) {
3243                 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3244                 return -EBUSY;
3245         }
3246
3247         instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3248
3249         if (!instance->reg_set) {
3250                 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3251                 goto fail_ioremap;
3252         }
3253
3254         reg_set = instance->reg_set;
3255
3256         switch(instance->pdev->device)
3257         {
3258                 case PCI_DEVICE_ID_LSI_SAS1078R:
3259                 case PCI_DEVICE_ID_LSI_SAS1078DE:
3260                         instance->instancet = &megasas_instance_template_ppc;
3261                         break;
3262                 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3263                 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3264                         instance->instancet = &megasas_instance_template_gen2;
3265                         break;
3266                 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3267                 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3268                         instance->instancet = &megasas_instance_template_skinny;
3269                         break;
3270                 case PCI_DEVICE_ID_LSI_SAS1064R:
3271                 case PCI_DEVICE_ID_DELL_PERC5:
3272                 default:
3273                         instance->instancet = &megasas_instance_template_xscale;
3274                         break;
3275         }
3276
3277         /*
3278          * We expect the FW state to be READY
3279          */
3280         if (megasas_transition_to_ready(instance))
3281                 goto fail_ready_state;
3282
3283         /*
3284          * Get various operational parameters from status register
3285          */
3286         instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3287         /*
3288          * Reduce the max supported cmds by 1. This is to ensure that the
3289          * reply_q_sz (1 more than the max cmd that driver may send)
3290          * does not exceed max cmds that the FW can support
3291          */
3292         instance->max_fw_cmds = instance->max_fw_cmds-1;
3293         instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >> 
3294                                         0x10;
3295         /*
3296          * Create a pool of commands
3297          */
3298         if (megasas_alloc_cmds(instance))
3299                 goto fail_alloc_cmds;
3300
3301         /*
3302          * Allocate memory for reply queue. Length of reply queue should
3303          * be _one_ more than the maximum commands handled by the firmware.
3304          *
3305          * Note: When FW completes commands, it places corresponding contex
3306          * values in this circular reply queue. This circular queue is a fairly
3307          * typical producer-consumer queue. FW is the producer (of completed
3308          * commands) and the driver is the consumer.
3309          */
3310         context_sz = sizeof(u32);
3311         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3312
3313         instance->reply_queue = pci_alloc_consistent(instance->pdev,
3314                                                      reply_q_sz,
3315                                                      &instance->reply_queue_h);
3316
3317         if (!instance->reply_queue) {
3318                 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3319                 goto fail_reply_queue;
3320         }
3321
3322         if (megasas_issue_init_mfi(instance))
3323                 goto fail_fw_init;
3324
3325         instance->fw_support_ieee = 0;
3326         instance->fw_support_ieee =
3327                 (instance->instancet->read_fw_status_reg(reg_set) &
3328                 0x04000000);
3329
3330         printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3331                         instance->fw_support_ieee);
3332
3333         if (instance->fw_support_ieee)
3334                 instance->flag_ieee = 1;
3335
3336         /** for passthrough
3337         * the following function will get the PD LIST.
3338         */
3339
3340         memset(instance->pd_list, 0 ,
3341                 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3342         megasas_get_pd_list(instance);
3343
3344         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3345         megasas_get_ld_list(instance);
3346
3347         ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3348
3349         /*
3350          * Compute the max allowed sectors per IO: The controller info has two
3351          * limits on max sectors. Driver should use the minimum of these two.
3352          *
3353          * 1 << stripe_sz_ops.min = max sectors per strip
3354          *
3355          * Note that older firmwares ( < FW ver 30) didn't report information
3356          * to calculate max_sectors_1. So the number ended up as zero always.
3357          */
3358         tmp_sectors = 0;
3359         if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3360
3361                 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3362                     ctrl_info->max_strips_per_io;
3363                 max_sectors_2 = ctrl_info->max_request_size;
3364
3365                 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3366                 instance->disableOnlineCtrlReset =
3367                 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3368         }
3369
3370         instance->max_sectors_per_req = instance->max_num_sge *
3371                                                 PAGE_SIZE / 512;
3372         if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3373                 instance->max_sectors_per_req = tmp_sectors;
3374
3375         kfree(ctrl_info);
3376
3377         /*
3378         * Setup tasklet for cmd completion
3379         */
3380
3381         tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
3382                 (unsigned long)instance);
3383
3384         /* Initialize the cmd completion timer */
3385         if (poll_mode_io)
3386                 megasas_start_timer(instance, &instance->io_completion_timer,
3387                                 megasas_io_completion_timer,
3388                                 MEGASAS_COMPLETION_TIMER_INTERVAL);
3389         return 0;
3390
3391       fail_fw_init:
3392
3393         pci_free_consistent(instance->pdev, reply_q_sz,
3394                             instance->reply_queue, instance->reply_queue_h);
3395       fail_reply_queue:
3396         megasas_free_cmds(instance);
3397
3398       fail_alloc_cmds:
3399       fail_ready_state:
3400         iounmap(instance->reg_set);
3401
3402       fail_ioremap:
3403         pci_release_selected_regions(instance->pdev,
3404                 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3405
3406         return -EINVAL;
3407 }
3408
3409 /**
3410  * megasas_release_mfi -        Reverses the FW initialization
3411  * @intance:                    Adapter soft state
3412  */
3413 static void megasas_release_mfi(struct megasas_instance *instance)
3414 {
3415         u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
3416
3417         pci_free_consistent(instance->pdev, reply_q_sz,
3418                             instance->reply_queue, instance->reply_queue_h);
3419
3420         megasas_free_cmds(instance);
3421
3422         iounmap(instance->reg_set);
3423
3424         pci_release_selected_regions(instance->pdev,
3425                 pci_select_bars(instance->pdev, IORESOURCE_MEM));
3426 }
3427
3428 /**
3429  * megasas_get_seq_num -        Gets latest event sequence numbers
3430  * @instance:                   Adapter soft state
3431  * @eli:                        FW event log sequence numbers information
3432  *
3433  * FW maintains a log of all events in a non-volatile area. Upper layers would
3434  * usually find out the latest sequence number of the events, the seq number at
3435  * the boot etc. They would "read" all the events below the latest seq number
3436  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
3437  * number), they would subsribe to AEN (asynchronous event notification) and
3438  * wait for the events to happen.
3439  */
3440 static int
3441 megasas_get_seq_num(struct megasas_instance *instance,
3442                     struct megasas_evt_log_info *eli)
3443 {
3444         struct megasas_cmd *cmd;
3445         struct megasas_dcmd_frame *dcmd;
3446         struct megasas_evt_log_info *el_info;
3447         dma_addr_t el_info_h = 0;
3448
3449         cmd = megasas_get_cmd(instance);
3450
3451         if (!cmd) {
3452                 return -ENOMEM;
3453         }
3454
3455         dcmd = &cmd->frame->dcmd;
3456         el_info = pci_alloc_consistent(instance->pdev,
3457                                        sizeof(struct megasas_evt_log_info),
3458                                        &el_info_h);
3459
3460         if (!el_info) {
3461                 megasas_return_cmd(instance, cmd);
3462                 return -ENOMEM;
3463         }
3464
3465         memset(el_info, 0, sizeof(*el_info));
3466         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3467
3468         dcmd->cmd = MFI_CMD_DCMD;
3469         dcmd->cmd_status = 0x0;
3470         dcmd->sge_count = 1;
3471         dcmd->flags = MFI_FRAME_DIR_READ;
3472         dcmd->timeout = 0;
3473         dcmd->pad_0 = 0;
3474         dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3475         dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3476         dcmd->sgl.sge32[0].phys_addr = el_info_h;
3477         dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3478
3479         megasas_issue_blocked_cmd(instance, cmd);
3480
3481         /*
3482          * Copy the data back into callers buffer
3483          */
3484         memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3485
3486         pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3487                             el_info, el_info_h);
3488
3489         megasas_return_cmd(instance, cmd);
3490
3491         return 0;
3492 }
3493
3494 /**
3495  * megasas_register_aen -       Registers for asynchronous event notification
3496  * @instance:                   Adapter soft state
3497  * @seq_num:                    The starting sequence number
3498  * @class_locale:               Class of the event
3499  *
3500  * This function subscribes for AEN for events beyond the @seq_num. It requests
3501  * to be notified if and only if the event is of type @class_locale
3502  */
3503 static int
3504 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3505                      u32 class_locale_word)
3506 {
3507         int ret_val;
3508         struct megasas_cmd *cmd;
3509         struct megasas_dcmd_frame *dcmd;
3510         union megasas_evt_class_locale curr_aen;
3511         union megasas_evt_class_locale prev_aen;
3512
3513         /*
3514          * If there an AEN pending already (aen_cmd), check if the
3515          * class_locale of that pending AEN is inclusive of the new
3516          * AEN request we currently have. If it is, then we don't have
3517          * to do anything. In other words, whichever events the current
3518          * AEN request is subscribing to, have already been subscribed
3519          * to.
3520          *
3521          * If the old_cmd is _not_ inclusive, then we have to abort
3522          * that command, form a class_locale that is superset of both
3523          * old and current and re-issue to the FW
3524          */
3525
3526         curr_aen.word = class_locale_word;
3527
3528         if (instance->aen_cmd) {
3529
3530                 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3531
3532                 /*
3533                  * A class whose enum value is smaller is inclusive of all
3534                  * higher values. If a PROGRESS (= -1) was previously
3535                  * registered, then a new registration requests for higher
3536                  * classes need not be sent to FW. They are automatically
3537                  * included.
3538                  *
3539                  * Locale numbers don't have such hierarchy. They are bitmap
3540                  * values
3541                  */
3542                 if ((prev_aen.members.class <= curr_aen.members.class) &&
3543                     !((prev_aen.members.locale & curr_aen.members.locale) ^
3544                       curr_aen.members.locale)) {
3545                         /*
3546                          * Previously issued event registration includes
3547                          * current request. Nothing to do.
3548                          */
3549                         return 0;
3550                 } else {
3551                         curr_aen.members.locale |= prev_aen.members.locale;
3552
3553                         if (prev_aen.members.class < curr_aen.members.class)
3554                                 curr_aen.members.class = prev_aen.members.class;
3555
3556                         instance->aen_cmd->abort_aen = 1;
3557                         ret_val = megasas_issue_blocked_abort_cmd(instance,
3558                                                                   instance->
3559                                                                   aen_cmd);
3560
3561                         if (ret_val) {
3562                                 printk(KERN_DEBUG "megasas: Failed to abort "
3563                                        "previous AEN command\n");
3564                                 return ret_val;
3565                         }
3566                 }
3567         }
3568
3569         cmd = megasas_get_cmd(instance);
3570
3571         if (!cmd)
3572                 return -ENOMEM;
3573
3574         dcmd = &cmd->frame->dcmd;
3575
3576         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3577
3578         /*
3579          * Prepare DCMD for aen registration
3580          */
3581         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3582
3583         dcmd->cmd = MFI_CMD_DCMD;
3584         dcmd->cmd_status = 0x0;
3585         dcmd->sge_count = 1;
3586         dcmd->flags = MFI_FRAME_DIR_READ;
3587         dcmd->timeout = 0;
3588         dcmd->pad_0 = 0;
3589         instance->last_seq_num = seq_num;
3590         dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3591         dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3592         dcmd->mbox.w[0] = seq_num;
3593         dcmd->mbox.w[1] = curr_aen.word;
3594         dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3595         dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3596
3597         if (instance->aen_cmd != NULL) {
3598                 megasas_return_cmd(instance, cmd);
3599                 return 0;
3600         }
3601
3602         /*
3603          * Store reference to the cmd used to register for AEN. When an
3604          * application wants us to register for AEN, we have to abort this
3605          * cmd and re-register with a new EVENT LOCALE supplied by that app
3606          */
3607         instance->aen_cmd = cmd;
3608
3609         /*
3610          * Issue the aen registration frame
3611          */
3612         instance->instancet->fire_cmd(instance,
3613                         cmd->frame_phys_addr, 0, instance->reg_set);
3614
3615         return 0;
3616 }
3617
3618 /**
3619  * megasas_start_aen -  Subscribes to AEN during driver load time
3620  * @instance:           Adapter soft state
3621  */
3622 static int megasas_start_aen(struct megasas_instance *instance)
3623 {
3624         struct megasas_evt_log_info eli;
3625         union megasas_evt_class_locale class_locale;
3626
3627         /*
3628          * Get the latest sequence number from FW
3629          */
3630         memset(&eli, 0, sizeof(eli));
3631
3632         if (megasas_get_seq_num(instance, &eli))
3633                 return -1;
3634
3635         /*
3636          * Register AEN with FW for latest sequence number plus 1
3637          */
3638         class_locale.members.reserved = 0;
3639         class_locale.members.locale = MR_EVT_LOCALE_ALL;
3640         class_locale.members.class = MR_EVT_CLASS_DEBUG;
3641
3642         return megasas_register_aen(instance, eli.newest_seq_num + 1,
3643                                     class_locale.word);
3644 }
3645
3646 /**
3647  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
3648  * @instance:           Adapter soft state
3649  */
3650 static int megasas_io_attach(struct megasas_instance *instance)
3651 {
3652         struct Scsi_Host *host = instance->host;
3653
3654         /*
3655          * Export parameters required by SCSI mid-layer
3656          */
3657         host->irq = instance->pdev->irq;
3658         host->unique_id = instance->unique_id;
3659         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3660                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3661                 host->can_queue =
3662                         instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3663         } else
3664                 host->can_queue =
3665                         instance->max_fw_cmds - MEGASAS_INT_CMDS;
3666         host->this_id = instance->init_id;
3667         host->sg_tablesize = instance->max_num_sge;
3668         /*
3669          * Check if the module parameter value for max_sectors can be used
3670          */
3671         if (max_sectors && max_sectors < instance->max_sectors_per_req)
3672                 instance->max_sectors_per_req = max_sectors;
3673         else {
3674                 if (max_sectors) {
3675                         if (((instance->pdev->device ==
3676                                 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3677                                 (instance->pdev->device ==
3678                                 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3679                                 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3680                                 instance->max_sectors_per_req = max_sectors;
3681                         } else {
3682                         printk(KERN_INFO "megasas: max_sectors should be > 0"
3683                                 "and <= %d (or < 1MB for GEN2 controller)\n",
3684                                 instance->max_sectors_per_req);
3685                         }
3686                 }
3687         }
3688
3689         host->max_sectors = instance->max_sectors_per_req;
3690         host->cmd_per_lun = 128;
3691         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3692         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3693         host->max_lun = MEGASAS_MAX_LUN;
3694         host->max_cmd_len = 16;
3695
3696         /*
3697          * Notify the mid-layer about the new controller
3698          */
3699         if (scsi_add_host(host, &instance->pdev->dev)) {
3700                 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3701                 return -ENODEV;
3702         }
3703
3704         /*
3705          * Trigger SCSI to scan our drives
3706          */
3707         scsi_scan_host(host);
3708         return 0;
3709 }
3710
3711 static int
3712 megasas_set_dma_mask(struct pci_dev *pdev)
3713 {
3714         /*
3715          * All our contollers are capable of performing 64-bit DMA
3716          */
3717         if (IS_DMA64) {
3718                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3719
3720                         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3721                                 goto fail_set_dma_mask;
3722                 }
3723         } else {
3724                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3725                         goto fail_set_dma_mask;
3726         }
3727         return 0;
3728
3729 fail_set_dma_mask:
3730         return 1;
3731 }
3732
3733 /**
3734  * megasas_probe_one -  PCI hotplug entry point
3735  * @pdev:               PCI device structure
3736  * @id:                 PCI ids of supported hotplugged adapter 
3737  */
3738 static int __devinit
3739 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
3740 {
3741         int rval;
3742         struct Scsi_Host *host;
3743         struct megasas_instance *instance;
3744
3745         /*
3746          * Announce PCI information
3747          */
3748         printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
3749                pdev->vendor, pdev->device, pdev->subsystem_vendor,
3750                pdev->subsystem_device);
3751
3752         printk("bus %d:slot %d:func %d\n",
3753                pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
3754
3755         /*
3756          * PCI prepping: enable device set bus mastering and dma mask
3757          */
3758         rval = pci_enable_device_mem(pdev);
3759
3760         if (rval) {
3761                 return rval;
3762         }
3763
3764         pci_set_master(pdev);
3765
3766         if (megasas_set_dma_mask(pdev))
3767                 goto fail_set_dma_mask;
3768
3769         host = scsi_host_alloc(&megasas_template,
3770                                sizeof(struct megasas_instance));
3771
3772         if (!host) {
3773                 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
3774                 goto fail_alloc_instance;
3775         }
3776
3777         instance = (struct megasas_instance *)host->hostdata;
3778         memset(instance, 0, sizeof(*instance));
3779         atomic_set( &instance->fw_reset_no_pci_access, 0 );
3780
3781         instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
3782                                                   &instance->producer_h);
3783         instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
3784                                                   &instance->consumer_h);
3785
3786         if (!instance->producer || !instance->consumer) {
3787                 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3788                        "producer, consumer\n");
3789                 goto fail_alloc_dma_buf;
3790         }
3791
3792         *instance->producer = 0;
3793         *instance->consumer = 0;
3794         megasas_poll_wait_aen = 0;
3795         instance->flag_ieee = 0;
3796         instance->ev = NULL;
3797         instance->issuepend_done = 1;
3798         instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
3799         megasas_poll_wait_aen = 0;
3800
3801         instance->evt_detail = pci_alloc_consistent(pdev,
3802                                                     sizeof(struct
3803                                                            megasas_evt_detail),
3804                                                     &instance->evt_detail_h);
3805
3806         if (!instance->evt_detail) {
3807                 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
3808                        "event detail structure\n");
3809                 goto fail_alloc_dma_buf;
3810         }
3811
3812         /*
3813          * Initialize locks and queues
3814          */
3815         INIT_LIST_HEAD(&instance->cmd_pool);
3816         INIT_LIST_HEAD(&instance->internal_reset_pending_q);
3817
3818         atomic_set(&instance->fw_outstanding,0);
3819
3820         init_waitqueue_head(&instance->int_cmd_wait_q);
3821         init_waitqueue_head(&instance->abort_cmd_wait_q);
3822
3823         spin_lock_init(&instance->cmd_pool_lock);
3824         spin_lock_init(&instance->hba_lock);
3825         spin_lock_init(&instance->completion_lock);
3826         spin_lock_init(&poll_aen_lock);
3827
3828         mutex_init(&instance->aen_mutex);
3829
3830         /*
3831          * Initialize PCI related and misc parameters
3832          */
3833         instance->pdev = pdev;
3834         instance->host = host;
3835         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
3836         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
3837
3838         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3839                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3840                 instance->flag_ieee = 1;
3841                 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
3842         } else
3843                 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
3844
3845         megasas_dbg_lvl = 0;
3846         instance->flag = 0;
3847         instance->unload = 1;
3848         instance->last_time = 0;
3849         instance->disableOnlineCtrlReset = 1;
3850
3851         INIT_WORK(&instance->work_init, process_fw_state_change_wq);
3852
3853         /*
3854          * Initialize MFI Firmware
3855          */
3856         if (megasas_init_mfi(instance))
3857                 goto fail_init_mfi;
3858
3859         /*
3860          * Register IRQ
3861          */
3862         if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) {
3863                 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
3864                 goto fail_irq;
3865         }
3866
3867         instance->instancet->enable_intr(instance->reg_set);
3868
3869         /*
3870          * Store instance in PCI softstate
3871          */
3872         pci_set_drvdata(pdev, instance);
3873
3874         /*
3875          * Add this controller to megasas_mgmt_info structure so that it
3876          * can be exported to management applications
3877          */
3878         megasas_mgmt_info.count++;
3879         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
3880         megasas_mgmt_info.max_index++;
3881
3882         /*
3883          * Initiate AEN (Asynchronous Event Notification)
3884          */
3885         if (megasas_start_aen(instance)) {
3886                 printk(KERN_DEBUG "megasas: start aen failed\n");
3887                 goto fail_start_aen;
3888         }
3889
3890         /*
3891          * Register with SCSI mid-layer
3892          */
3893         if (megasas_io_attach(instance))
3894                 goto fail_io_attach;
3895
3896         instance->unload = 0;
3897         return 0;
3898
3899       fail_start_aen:
3900       fail_io_attach:
3901         megasas_mgmt_info.count--;
3902         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
3903         megasas_mgmt_info.max_index--;
3904
3905         pci_set_drvdata(pdev, NULL);
3906         instance->instancet->disable_intr(instance->reg_set);
3907         free_irq(instance->pdev->irq, instance);
3908
3909         megasas_release_mfi(instance);
3910
3911       fail_irq:
3912       fail_init_mfi:
3913       fail_alloc_dma_buf:
3914         if (instance->evt_detail)
3915                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
3916                                     instance->evt_detail,
3917                                     instance->evt_detail_h);
3918
3919         if (instance->producer)
3920                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
3921                                     instance->producer_h);
3922         if (instance->consumer)
3923                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
3924                                     instance->consumer_h);
3925         scsi_host_put(host);
3926
3927       fail_alloc_instance:
3928       fail_set_dma_mask:
3929         pci_disable_device(pdev);
3930
3931         return -ENODEV;
3932 }
3933
3934 /**
3935  * megasas_flush_cache -        Requests FW to flush all its caches
3936  * @instance:                   Adapter soft state
3937  */
3938 static void megasas_flush_cache(struct megasas_instance *instance)
3939 {
3940         struct megasas_cmd *cmd;
3941         struct megasas_dcmd_frame *dcmd;
3942
3943         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3944                 return;
3945
3946         cmd = megasas_get_cmd(instance);
3947
3948         if (!cmd)
3949                 return;
3950
3951         dcmd = &cmd->frame->dcmd;
3952
3953         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3954
3955         dcmd->cmd = MFI_CMD_DCMD;
3956         dcmd->cmd_status = 0x0;
3957         dcmd->sge_count = 0;
3958         dcmd->flags = MFI_FRAME_DIR_NONE;
3959         dcmd->timeout = 0;
3960         dcmd->pad_0 = 0;
3961         dcmd->data_xfer_len = 0;
3962         dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
3963         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
3964
3965         megasas_issue_blocked_cmd(instance, cmd);
3966
3967         megasas_return_cmd(instance, cmd);
3968
3969         return;
3970 }
3971
3972 /**
3973  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
3974  * @instance:                           Adapter soft state
3975  * @opcode:                             Shutdown/Hibernate
3976  */
3977 static void megasas_shutdown_controller(struct megasas_instance *instance,
3978                                         u32 opcode)
3979 {
3980         struct megasas_cmd *cmd;
3981         struct megasas_dcmd_frame *dcmd;
3982
3983         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
3984                 return;
3985
3986         cmd = megasas_get_cmd(instance);
3987
3988         if (!cmd)
3989                 return;
3990
3991         if (instance->aen_cmd)
3992                 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
3993
3994         dcmd = &cmd->frame->dcmd;
3995
3996         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3997
3998         dcmd->cmd = MFI_CMD_DCMD;
3999         dcmd->cmd_status = 0x0;
4000         dcmd->sge_count = 0;
4001         dcmd->flags = MFI_FRAME_DIR_NONE;
4002         dcmd->timeout = 0;
4003         dcmd->pad_0 = 0;
4004         dcmd->data_xfer_len = 0;
4005         dcmd->opcode = opcode;
4006
4007         megasas_issue_blocked_cmd(instance, cmd);
4008
4009         megasas_return_cmd(instance, cmd);
4010
4011         return;
4012 }
4013
4014 #ifdef CONFIG_PM
4015 /**
4016  * megasas_suspend -    driver suspend entry point
4017  * @pdev:               PCI device structure
4018  * @state:              PCI power state to suspend routine
4019  */
4020 static int
4021 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4022 {
4023         struct Scsi_Host *host;
4024         struct megasas_instance *instance;
4025
4026         instance = pci_get_drvdata(pdev);
4027         host = instance->host;
4028         instance->unload = 1;
4029
4030         if (poll_mode_io)
4031                 del_timer_sync(&instance->io_completion_timer);
4032
4033         megasas_flush_cache(instance);
4034         megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4035
4036         /* cancel the delayed work if this work still in queue */
4037         if (instance->ev != NULL) {
4038                 struct megasas_aen_event *ev = instance->ev;
4039                 cancel_delayed_work(
4040                         (struct delayed_work *)&ev->hotplug_work);
4041                 flush_scheduled_work();
4042                 instance->ev = NULL;
4043         }
4044
4045         tasklet_kill(&instance->isr_tasklet);
4046
4047         pci_set_drvdata(instance->pdev, instance);
4048         instance->instancet->disable_intr(instance->reg_set);
4049         free_irq(instance->pdev->irq, instance);
4050
4051         pci_save_state(pdev);
4052         pci_disable_device(pdev);
4053
4054         pci_set_power_state(pdev, pci_choose_state(pdev, state));
4055
4056         return 0;
4057 }
4058
4059 /**
4060  * megasas_resume-      driver resume entry point
4061  * @pdev:               PCI device structure
4062  */
4063 static int
4064 megasas_resume(struct pci_dev *pdev)
4065 {
4066         int rval;
4067         struct Scsi_Host *host;
4068         struct megasas_instance *instance;
4069
4070         instance = pci_get_drvdata(pdev);
4071         host = instance->host;
4072         pci_set_power_state(pdev, PCI_D0);
4073         pci_enable_wake(pdev, PCI_D0, 0);
4074         pci_restore_state(pdev);
4075
4076         /*
4077          * PCI prepping: enable device set bus mastering and dma mask
4078          */
4079         rval = pci_enable_device_mem(pdev);
4080
4081         if (rval) {
4082                 printk(KERN_ERR "megasas: Enable device failed\n");
4083                 return rval;
4084         }
4085
4086         pci_set_master(pdev);
4087
4088         if (megasas_set_dma_mask(pdev))
4089                 goto fail_set_dma_mask;
4090
4091         /*
4092          * Initialize MFI Firmware
4093          */
4094
4095         *instance->producer = 0;
4096         *instance->consumer = 0;
4097
4098         atomic_set(&instance->fw_outstanding, 0);
4099
4100         /*
4101          * We expect the FW state to be READY
4102          */
4103         if (megasas_transition_to_ready(instance))
4104                 goto fail_ready_state;
4105
4106         if (megasas_issue_init_mfi(instance))
4107                 goto fail_init_mfi;
4108
4109         tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc,
4110                         (unsigned long)instance);
4111
4112         /*
4113          * Register IRQ
4114          */
4115         if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED,
4116                 "megasas", instance)) {
4117                 printk(KERN_ERR "megasas: Failed to register IRQ\n");
4118                 goto fail_irq;
4119         }
4120
4121         instance->instancet->enable_intr(instance->reg_set);
4122
4123         /*
4124          * Initiate AEN (Asynchronous Event Notification)
4125          */
4126         if (megasas_start_aen(instance))
4127                 printk(KERN_ERR "megasas: Start AEN failed\n");
4128
4129         /* Initialize the cmd completion timer */
4130         if (poll_mode_io)
4131                 megasas_start_timer(instance, &instance->io_completion_timer,
4132                                 megasas_io_completion_timer,
4133                                 MEGASAS_COMPLETION_TIMER_INTERVAL);
4134         instance->unload = 0;
4135
4136         return 0;
4137
4138 fail_irq:
4139 fail_init_mfi:
4140         if (instance->evt_detail)
4141                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4142                                 instance->evt_detail,
4143                                 instance->evt_detail_h);
4144
4145         if (instance->producer)
4146                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4147                                 instance->producer_h);
4148         if (instance->consumer)
4149                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4150                                 instance->consumer_h);
4151         scsi_host_put(host);
4152
4153 fail_set_dma_mask:
4154 fail_ready_state:
4155
4156         pci_disable_device(pdev);
4157
4158         return -ENODEV;
4159 }
4160 #else
4161 #define megasas_suspend NULL
4162 #define megasas_resume  NULL
4163 #endif
4164
4165 /**
4166  * megasas_detach_one - PCI hot"un"plug entry point
4167  * @pdev:               PCI device structure
4168  */
4169 static void __devexit megasas_detach_one(struct pci_dev *pdev)
4170 {
4171         int i;
4172         struct Scsi_Host *host;
4173         struct megasas_instance *instance;
4174
4175         instance = pci_get_drvdata(pdev);
4176         instance->unload = 1;
4177         host = instance->host;
4178
4179         if (poll_mode_io)
4180                 del_timer_sync(&instance->io_completion_timer);
4181
4182         scsi_remove_host(instance->host);
4183         megasas_flush_cache(instance);
4184         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4185
4186         /* cancel the delayed work if this work still in queue*/
4187         if (instance->ev != NULL) {
4188                 struct megasas_aen_event *ev = instance->ev;
4189                 cancel_delayed_work(
4190                         (struct delayed_work *)&ev->hotplug_work);
4191                 flush_scheduled_work();
4192                 instance->ev = NULL;
4193         }
4194
4195         tasklet_kill(&instance->isr_tasklet);
4196
4197         /*
4198          * Take the instance off the instance array. Note that we will not
4199          * decrement the max_index. We let this array be sparse array
4200          */
4201         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4202                 if (megasas_mgmt_info.instance[i] == instance) {
4203                         megasas_mgmt_info.count--;
4204                         megasas_mgmt_info.instance[i] = NULL;
4205
4206                         break;
4207                 }
4208         }
4209
4210         pci_set_drvdata(instance->pdev, NULL);
4211
4212         instance->instancet->disable_intr(instance->reg_set);
4213
4214         free_irq(instance->pdev->irq, instance);
4215
4216         megasas_release_mfi(instance);
4217
4218         pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4219                             instance->evt_detail, instance->evt_detail_h);
4220
4221         pci_free_consistent(pdev, sizeof(u32), instance->producer,
4222                             instance->producer_h);
4223
4224         pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4225                             instance->consumer_h);
4226
4227         scsi_host_put(host);
4228
4229         pci_set_drvdata(pdev, NULL);
4230
4231         pci_disable_device(pdev);
4232
4233         return;
4234 }
4235
4236 /**
4237  * megasas_shutdown -   Shutdown entry point
4238  * @device:             Generic device structure
4239  */
4240 static void megasas_shutdown(struct pci_dev *pdev)
4241 {
4242         struct megasas_instance *instance = pci_get_drvdata(pdev);
4243         instance->unload = 1;
4244         megasas_flush_cache(instance);
4245         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4246 }
4247
4248 /**
4249  * megasas_mgmt_open -  char node "open" entry point
4250  */
4251 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4252 {
4253         /*
4254          * Allow only those users with admin rights
4255          */
4256         if (!capable(CAP_SYS_ADMIN))
4257                 return -EACCES;
4258
4259         return 0;
4260 }
4261
4262 /**
4263  * megasas_mgmt_fasync -        Async notifier registration from applications
4264  *
4265  * This function adds the calling process to a driver global queue. When an
4266  * event occurs, SIGIO will be sent to all processes in this queue.
4267  */
4268 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4269 {
4270         int rc;
4271
4272         mutex_lock(&megasas_async_queue_mutex);
4273
4274         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4275
4276         mutex_unlock(&megasas_async_queue_mutex);
4277
4278         if (rc >= 0) {
4279                 /* For sanity check when we get ioctl */
4280                 filep->private_data = filep;
4281                 return 0;
4282         }
4283
4284         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4285
4286         return rc;
4287 }
4288
4289 /**
4290  * megasas_mgmt_poll -  char node "poll" entry point
4291  * */
4292 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4293 {
4294         unsigned int mask;
4295         unsigned long flags;
4296         poll_wait(file, &megasas_poll_wait, wait);
4297         spin_lock_irqsave(&poll_aen_lock, flags);
4298         if (megasas_poll_wait_aen)
4299                 mask =   (POLLIN | POLLRDNORM);
4300         else
4301                 mask = 0;
4302         spin_unlock_irqrestore(&poll_aen_lock, flags);
4303         return mask;
4304 }
4305
4306 /**
4307  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
4308  * @instance:                   Adapter soft state
4309  * @argp:                       User's ioctl packet
4310  */
4311 static int
4312 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4313                       struct megasas_iocpacket __user * user_ioc,
4314                       struct megasas_iocpacket *ioc)
4315 {
4316         struct megasas_sge32 *kern_sge32;
4317         struct megasas_cmd *cmd;
4318         void *kbuff_arr[MAX_IOCTL_SGE];
4319         dma_addr_t buf_handle = 0;
4320         int error = 0, i;
4321         void *sense = NULL;
4322         dma_addr_t sense_handle;
4323         unsigned long *sense_ptr;
4324
4325         memset(kbuff_arr, 0, sizeof(kbuff_arr));
4326
4327         if (ioc->sge_count > MAX_IOCTL_SGE) {
4328                 printk(KERN_DEBUG "megasas: SGE count [%d] >  max limit [%d]\n",
4329                        ioc->sge_count, MAX_IOCTL_SGE);
4330                 return -EINVAL;
4331         }
4332
4333         cmd = megasas_get_cmd(instance);
4334         if (!cmd) {
4335                 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4336                 return -ENOMEM;
4337         }
4338
4339         /*
4340          * User's IOCTL packet has 2 frames (maximum). Copy those two
4341          * frames into our cmd's frames. cmd->frame's context will get
4342          * overwritten when we copy from user's frames. So set that value
4343          * alone separately
4344          */
4345         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4346         cmd->frame->hdr.context = cmd->index;
4347         cmd->frame->hdr.pad_0 = 0;
4348
4349         /*
4350          * The management interface between applications and the fw uses
4351          * MFI frames. E.g, RAID configuration changes, LD property changes
4352          * etc are accomplishes through different kinds of MFI frames. The
4353          * driver needs to care only about substituting user buffers with
4354          * kernel buffers in SGLs. The location of SGL is embedded in the
4355          * struct iocpacket itself.
4356          */
4357         kern_sge32 = (struct megasas_sge32 *)
4358             ((unsigned long)cmd->frame + ioc->sgl_off);
4359
4360         /*
4361          * For each user buffer, create a mirror buffer and copy in
4362          */
4363         for (i = 0; i < ioc->sge_count; i++) {
4364                 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4365                                                     ioc->sgl[i].iov_len,
4366                                                     &buf_handle, GFP_KERNEL);
4367                 if (!kbuff_arr[i]) {
4368                         printk(KERN_DEBUG "megasas: Failed to alloc "
4369                                "kernel SGL buffer for IOCTL \n");
4370                         error = -ENOMEM;
4371                         goto out;
4372                 }
4373
4374                 /*
4375                  * We don't change the dma_coherent_mask, so
4376                  * pci_alloc_consistent only returns 32bit addresses
4377                  */
4378                 kern_sge32[i].phys_addr = (u32) buf_handle;
4379                 kern_sge32[i].length = ioc->sgl[i].iov_len;
4380
4381                 /*
4382                  * We created a kernel buffer corresponding to the
4383                  * user buffer. Now copy in from the user buffer
4384                  */
4385                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4386                                    (u32) (ioc->sgl[i].iov_len))) {
4387                         error = -EFAULT;
4388                         goto out;
4389                 }
4390         }
4391
4392         if (ioc->sense_len) {
4393                 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4394                                              &sense_handle, GFP_KERNEL);
4395                 if (!sense) {
4396                         error = -ENOMEM;
4397                         goto out;
4398                 }
4399
4400                 sense_ptr =
4401                 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4402                 *sense_ptr = sense_handle;
4403         }
4404
4405         /*
4406          * Set the sync_cmd flag so that the ISR knows not to complete this
4407          * cmd to the SCSI mid-layer
4408          */
4409         cmd->sync_cmd = 1;
4410         megasas_issue_blocked_cmd(instance, cmd);
4411         cmd->sync_cmd = 0;
4412
4413         /*
4414          * copy out the kernel buffers to user buffers
4415          */
4416         for (i = 0; i < ioc->sge_count; i++) {
4417                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4418                                  ioc->sgl[i].iov_len)) {
4419                         error = -EFAULT;
4420                         goto out;
4421                 }
4422         }
4423
4424         /*
4425          * copy out the sense
4426          */
4427         if (ioc->sense_len) {
4428                 /*
4429                  * sense_ptr points to the location that has the user
4430                  * sense buffer address
4431                  */
4432                 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4433                                 ioc->sense_off);
4434
4435                 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4436                                  sense, ioc->sense_len)) {
4437                         printk(KERN_ERR "megasas: Failed to copy out to user "
4438                                         "sense data\n");
4439                         error = -EFAULT;
4440                         goto out;
4441                 }
4442         }
4443
4444         /*
4445          * copy the status codes returned by the fw
4446          */
4447         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4448                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4449                 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4450                 error = -EFAULT;
4451         }
4452
4453       out:
4454         if (sense) {
4455                 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4456                                     sense, sense_handle);
4457         }
4458
4459         for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4460                 dma_free_coherent(&instance->pdev->dev,
4461                                     kern_sge32[i].length,
4462                                     kbuff_arr[i], kern_sge32[i].phys_addr);
4463         }
4464
4465         megasas_return_cmd(instance, cmd);
4466         return error;
4467 }
4468
4469 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4470 {
4471         struct megasas_iocpacket __user *user_ioc =
4472             (struct megasas_iocpacket __user *)arg;
4473         struct megasas_iocpacket *ioc;
4474         struct megasas_instance *instance;
4475         int error;
4476         int i;
4477         unsigned long flags;
4478         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4479
4480         ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4481         if (!ioc)
4482                 return -ENOMEM;
4483
4484         if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4485                 error = -EFAULT;
4486                 goto out_kfree_ioc;
4487         }
4488
4489         instance = megasas_lookup_instance(ioc->host_no);
4490         if (!instance) {
4491                 error = -ENODEV;
4492                 goto out_kfree_ioc;
4493         }
4494
4495         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4496                 printk(KERN_ERR "Controller in crit error\n");
4497                 error = -ENODEV;
4498                 goto out_kfree_ioc;
4499         }
4500
4501         if (instance->unload == 1) {
4502                 error = -ENODEV;
4503                 goto out_kfree_ioc;
4504         }
4505
4506         /*
4507          * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4508          */
4509         if (down_interruptible(&instance->ioctl_sem)) {
4510                 error = -ERESTARTSYS;
4511                 goto out_kfree_ioc;
4512         }
4513
4514         for (i = 0; i < wait_time; i++) {
4515
4516                 spin_lock_irqsave(&instance->hba_lock, flags);
4517                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4518                         spin_unlock_irqrestore(&instance->hba_lock, flags);
4519                         break;
4520                 }
4521                 spin_unlock_irqrestore(&instance->hba_lock, flags);
4522
4523                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4524                         printk(KERN_NOTICE "megasas: waiting"
4525                                 "for controller reset to finish\n");
4526                 }
4527
4528                 msleep(1000);
4529         }
4530
4531         spin_lock_irqsave(&instance->hba_lock, flags);
4532         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4533                 spin_unlock_irqrestore(&instance->hba_lock, flags);
4534
4535                 printk(KERN_ERR "megaraid_sas: timed out while"
4536                         "waiting for HBA to recover\n");
4537                 error = -ENODEV;
4538                 goto out_kfree_ioc;
4539         }
4540         spin_unlock_irqrestore(&instance->hba_lock, flags);
4541
4542         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4543         up(&instance->ioctl_sem);
4544
4545       out_kfree_ioc:
4546         kfree(ioc);
4547         return error;
4548 }
4549
4550 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4551 {
4552         struct megasas_instance *instance;
4553         struct megasas_aen aen;
4554         int error;
4555         int i;
4556         unsigned long flags;
4557         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4558
4559         if (file->private_data != file) {
4560                 printk(KERN_DEBUG "megasas: fasync_helper was not "
4561                        "called first\n");
4562                 return -EINVAL;
4563         }
4564
4565         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4566                 return -EFAULT;
4567
4568         instance = megasas_lookup_instance(aen.host_no);
4569
4570         if (!instance)
4571                 return -ENODEV;
4572
4573         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4574                 return -ENODEV;
4575         }
4576
4577         if (instance->unload == 1) {
4578                 return -ENODEV;
4579         }
4580
4581         for (i = 0; i < wait_time; i++) {
4582
4583                 spin_lock_irqsave(&instance->hba_lock, flags);
4584                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4585                         spin_unlock_irqrestore(&instance->hba_lock,
4586                                                 flags);
4587                         break;
4588                 }
4589
4590                 spin_unlock_irqrestore(&instance->hba_lock, flags);
4591
4592                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4593                         printk(KERN_NOTICE "megasas: waiting for"
4594                                 "controller reset to finish\n");
4595                 }
4596
4597                 msleep(1000);
4598         }
4599
4600         spin_lock_irqsave(&instance->hba_lock, flags);
4601         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4602                 spin_unlock_irqrestore(&instance->hba_lock, flags);
4603                 printk(KERN_ERR "megaraid_sas: timed out while waiting"
4604                                 "for HBA to recover.\n");
4605                 return -ENODEV;
4606         }
4607         spin_unlock_irqrestore(&instance->hba_lock, flags);
4608
4609         mutex_lock(&instance->aen_mutex);
4610         error = megasas_register_aen(instance, aen.seq_num,
4611                                      aen.class_locale_word);
4612         mutex_unlock(&instance->aen_mutex);
4613         return error;
4614 }
4615
4616 /**
4617  * megasas_mgmt_ioctl - char node ioctl entry point
4618  */
4619 static long
4620 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
4621 {
4622         switch (cmd) {
4623         case MEGASAS_IOC_FIRMWARE:
4624                 return megasas_mgmt_ioctl_fw(file, arg);
4625
4626         case MEGASAS_IOC_GET_AEN:
4627                 return megasas_mgmt_ioctl_aen(file, arg);
4628         }
4629
4630         return -ENOTTY;
4631 }
4632
4633 #ifdef CONFIG_COMPAT
4634 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
4635 {
4636         struct compat_megasas_iocpacket __user *cioc =
4637             (struct compat_megasas_iocpacket __user *)arg;
4638         struct megasas_iocpacket __user *ioc =
4639             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
4640         int i;
4641         int error = 0;
4642         compat_uptr_t ptr;
4643
4644         if (clear_user(ioc, sizeof(*ioc)))
4645                 return -EFAULT;
4646
4647         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
4648             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
4649             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
4650             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
4651             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
4652             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
4653                 return -EFAULT;
4654
4655         /*
4656          * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
4657          * sense_len is not null, so prepare the 64bit value under
4658          * the same condition.
4659          */
4660         if (ioc->sense_len) {
4661                 void __user **sense_ioc_ptr =
4662                         (void __user **)(ioc->frame.raw + ioc->sense_off);
4663                 compat_uptr_t *sense_cioc_ptr =
4664                         (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
4665                 if (get_user(ptr, sense_cioc_ptr) ||
4666                     put_user(compat_ptr(ptr), sense_ioc_ptr))
4667                         return -EFAULT;
4668         }
4669
4670         for (i = 0; i < MAX_IOCTL_SGE; i++) {
4671                 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
4672                     put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
4673                     copy_in_user(&ioc->sgl[i].iov_len,
4674                                  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
4675                         return -EFAULT;
4676         }
4677
4678         error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
4679
4680         if (copy_in_user(&cioc->frame.hdr.cmd_status,
4681                          &ioc->frame.hdr.cmd_status, sizeof(u8))) {
4682                 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
4683                 return -EFAULT;
4684         }
4685         return error;
4686 }
4687
4688 static long
4689 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
4690                           unsigned long arg)
4691 {
4692         switch (cmd) {
4693         case MEGASAS_IOC_FIRMWARE32:
4694                 return megasas_mgmt_compat_ioctl_fw(file, arg);
4695         case MEGASAS_IOC_GET_AEN:
4696                 return megasas_mgmt_ioctl_aen(file, arg);
4697         }
4698
4699         return -ENOTTY;
4700 }
4701 #endif
4702
4703 /*
4704  * File operations structure for management interface
4705  */
4706 static const struct file_operations megasas_mgmt_fops = {
4707         .owner = THIS_MODULE,
4708         .open = megasas_mgmt_open,
4709         .fasync = megasas_mgmt_fasync,
4710         .unlocked_ioctl = megasas_mgmt_ioctl,
4711         .poll = megasas_mgmt_poll,
4712 #ifdef CONFIG_COMPAT
4713         .compat_ioctl = megasas_mgmt_compat_ioctl,
4714 #endif
4715         .llseek = noop_llseek,
4716 };
4717
4718 /*
4719  * PCI hotplug support registration structure
4720  */
4721 static struct pci_driver megasas_pci_driver = {
4722
4723         .name = "megaraid_sas",
4724         .id_table = megasas_pci_table,
4725         .probe = megasas_probe_one,
4726         .remove = __devexit_p(megasas_detach_one),
4727         .suspend = megasas_suspend,
4728         .resume = megasas_resume,
4729         .shutdown = megasas_shutdown,
4730 };
4731
4732 /*
4733  * Sysfs driver attributes
4734  */
4735 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
4736 {
4737         return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
4738                         MEGASAS_VERSION);
4739 }
4740
4741 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
4742
4743 static ssize_t
4744 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
4745 {
4746         return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
4747                         MEGASAS_RELDATE);
4748 }
4749
4750 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
4751                    NULL);
4752
4753 static ssize_t
4754 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
4755 {
4756         return sprintf(buf, "%u\n", support_poll_for_event);
4757 }
4758
4759 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
4760                         megasas_sysfs_show_support_poll_for_event, NULL);
4761
4762  static ssize_t
4763 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
4764 {
4765         return sprintf(buf, "%u\n", support_device_change);
4766 }
4767
4768 static DRIVER_ATTR(support_device_change, S_IRUGO,
4769                         megasas_sysfs_show_support_device_change, NULL);
4770
4771 static ssize_t
4772 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
4773 {
4774         return sprintf(buf, "%u\n", megasas_dbg_lvl);
4775 }
4776
4777 static ssize_t
4778 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
4779 {
4780         int retval = count;
4781         if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
4782                 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
4783                 retval = -EINVAL;
4784         }
4785         return retval;
4786 }
4787
4788 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
4789                 megasas_sysfs_set_dbg_lvl);
4790
4791 static ssize_t
4792 megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf)
4793 {
4794         return sprintf(buf, "%u\n", poll_mode_io);
4795 }
4796
4797 static ssize_t
4798 megasas_sysfs_set_poll_mode_io(struct device_driver *dd,
4799                                 const char *buf, size_t count)
4800 {
4801         int retval = count;
4802         int tmp = poll_mode_io;
4803         int i;
4804         struct megasas_instance *instance;
4805
4806         if (sscanf(buf, "%u", &poll_mode_io) < 1) {
4807                 printk(KERN_ERR "megasas: could not set poll_mode_io\n");
4808                 retval = -EINVAL;
4809         }
4810
4811         /*
4812          * Check if poll_mode_io is already set or is same as previous value
4813          */
4814         if ((tmp && poll_mode_io) || (tmp == poll_mode_io))
4815                 goto out;
4816
4817         if (poll_mode_io) {
4818                 /*
4819                  * Start timers for all adapters
4820                  */
4821                 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4822                         instance = megasas_mgmt_info.instance[i];
4823                         if (instance) {
4824                                 megasas_start_timer(instance,
4825                                         &instance->io_completion_timer,
4826                                         megasas_io_completion_timer,
4827                                         MEGASAS_COMPLETION_TIMER_INTERVAL);
4828                         }
4829                 }
4830         } else {
4831                 /*
4832                  * Delete timers for all adapters
4833                  */
4834                 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4835                         instance = megasas_mgmt_info.instance[i];
4836                         if (instance)
4837                                 del_timer_sync(&instance->io_completion_timer);
4838                 }
4839         }
4840
4841 out:
4842         return retval;
4843 }
4844
4845 static void
4846 megasas_aen_polling(struct work_struct *work)
4847 {
4848         struct megasas_aen_event *ev =
4849                 container_of(work, struct megasas_aen_event, hotplug_work);
4850         struct megasas_instance *instance = ev->instance;
4851         union megasas_evt_class_locale class_locale;
4852         struct  Scsi_Host *host;
4853         struct  scsi_device *sdev1;
4854         u16     pd_index = 0;
4855         u16     ld_index = 0;
4856         int     i, j, doscan = 0;
4857         u32 seq_num;
4858         int error;
4859
4860         if (!instance) {
4861                 printk(KERN_ERR "invalid instance!\n");
4862                 kfree(ev);
4863                 return;
4864         }
4865         instance->ev = NULL;
4866         host = instance->host;
4867         if (instance->evt_detail) {
4868
4869                 switch (instance->evt_detail->code) {
4870                 case MR_EVT_PD_INSERTED:
4871                         if (megasas_get_pd_list(instance) == 0) {
4872                         for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4873                                 for (j = 0;
4874                                 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4875                                 j++) {
4876
4877                                 pd_index =
4878                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4879
4880                                 sdev1 =
4881                                 scsi_device_lookup(host, i, j, 0);
4882
4883                                 if (instance->pd_list[pd_index].driveState
4884                                                 == MR_PD_STATE_SYSTEM) {
4885                                                 if (!sdev1) {
4886                                                 scsi_add_device(host, i, j, 0);
4887                                                 }
4888
4889                                         if (sdev1)
4890                                                 scsi_device_put(sdev1);
4891                                         }
4892                                 }
4893                         }
4894                         }
4895                         doscan = 0;
4896                         break;
4897
4898                 case MR_EVT_PD_REMOVED:
4899                         if (megasas_get_pd_list(instance) == 0) {
4900                         megasas_get_pd_list(instance);
4901                         for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
4902                                 for (j = 0;
4903                                 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4904                                 j++) {
4905
4906                                 pd_index =
4907                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4908
4909                                 sdev1 =
4910                                 scsi_device_lookup(host, i, j, 0);
4911
4912                                 if (instance->pd_list[pd_index].driveState
4913                                         == MR_PD_STATE_SYSTEM) {
4914                                         if (sdev1) {
4915                                                 scsi_device_put(sdev1);
4916                                         }
4917                                 } else {
4918                                         if (sdev1) {
4919                                                 scsi_remove_device(sdev1);
4920                                                 scsi_device_put(sdev1);
4921                                         }
4922                                 }
4923                                 }
4924                         }
4925                         }
4926                         doscan = 0;
4927                         break;
4928
4929                 case MR_EVT_LD_OFFLINE:
4930                 case MR_EVT_LD_DELETED:
4931                         megasas_get_ld_list(instance);
4932                         for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4933                                 for (j = 0;
4934                                 j < MEGASAS_MAX_DEV_PER_CHANNEL;
4935                                 j++) {
4936
4937                                 ld_index =
4938                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4939
4940                                 sdev1 = scsi_device_lookup(host,
4941                                         i + MEGASAS_MAX_LD_CHANNELS,
4942                                         j,
4943                                         0);
4944
4945                                 if (instance->ld_ids[ld_index] != 0xff) {
4946                                         if (sdev1) {
4947                                                 scsi_device_put(sdev1);
4948                                         }
4949                                 } else {
4950                                         if (sdev1) {
4951                                                 scsi_remove_device(sdev1);
4952                                                 scsi_device_put(sdev1);
4953                                         }
4954                                 }
4955                                 }
4956                         }
4957                         doscan = 0;
4958                         break;
4959                 case MR_EVT_LD_CREATED:
4960                         megasas_get_ld_list(instance);
4961                         for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
4962                                 for (j = 0;
4963                                         j < MEGASAS_MAX_DEV_PER_CHANNEL;
4964                                         j++) {
4965                                         ld_index =
4966                                         (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
4967
4968                                         sdev1 = scsi_device_lookup(host,
4969                                                 i+MEGASAS_MAX_LD_CHANNELS,
4970                                                 j, 0);
4971
4972                                         if (instance->ld_ids[ld_index] !=
4973                                                                 0xff) {
4974                                                 if (!sdev1) {
4975                                                         scsi_add_device(host,
4976                                                                 i + 2,
4977                                                                 j, 0);
4978                                                 }
4979                                         }
4980                                         if (sdev1) {
4981                                                 scsi_device_put(sdev1);
4982                                         }
4983                                 }
4984                         }
4985                         doscan = 0;
4986                         break;
4987                 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
4988                 case MR_EVT_FOREIGN_CFG_IMPORTED:
4989                         doscan = 1;
4990                         break;
4991                 default:
4992                         doscan = 0;
4993                         break;
4994                 }
4995         } else {
4996                 printk(KERN_ERR "invalid evt_detail!\n");
4997                 kfree(ev);
4998                 return;
4999         }
5000
5001         if (doscan) {
5002                 printk(KERN_INFO "scanning ...\n");
5003                 megasas_get_pd_list(instance);
5004                 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5005                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5006                                 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5007                                 sdev1 = scsi_device_lookup(host, i, j, 0);
5008                                 if (instance->pd_list[pd_index].driveState ==
5009                                                         MR_PD_STATE_SYSTEM) {
5010                                         if (!sdev1) {
5011                                                 scsi_add_device(host, i, j, 0);
5012                                         }
5013                                         if (sdev1)
5014                                                 scsi_device_put(sdev1);
5015                                 } else {
5016                                         if (sdev1) {
5017                                                 scsi_remove_device(sdev1);
5018                                                 scsi_device_put(sdev1);
5019                                         }
5020                                 }
5021                         }
5022                 }
5023
5024                 megasas_get_ld_list(instance);
5025                 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5026                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5027                                 ld_index =
5028                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5029
5030                                 sdev1 = scsi_device_lookup(host,
5031                                         i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5032                                 if (instance->ld_ids[ld_index] != 0xff) {
5033                                         if (!sdev1) {
5034                                                 scsi_add_device(host,
5035                                                                 i+2,
5036                                                                 j, 0);
5037                                         } else {
5038                                                 scsi_device_put(sdev1);
5039                                         }
5040                                 } else {
5041                                         if (sdev1) {
5042                                                 scsi_remove_device(sdev1);
5043                                                 scsi_device_put(sdev1);
5044                                         }
5045                                 }
5046                         }
5047                 }
5048         }
5049
5050         if ( instance->aen_cmd != NULL ) {
5051                 kfree(ev);
5052                 return ;
5053         }
5054
5055         seq_num = instance->evt_detail->seq_num + 1;
5056
5057         /* Register AEN with FW for latest sequence number plus 1 */
5058         class_locale.members.reserved = 0;
5059         class_locale.members.locale = MR_EVT_LOCALE_ALL;
5060         class_locale.members.class = MR_EVT_CLASS_DEBUG;
5061         mutex_lock(&instance->aen_mutex);
5062         error = megasas_register_aen(instance, seq_num,
5063                                         class_locale.word);
5064         mutex_unlock(&instance->aen_mutex);
5065
5066         if (error)
5067                 printk(KERN_ERR "register aen failed error %x\n", error);
5068
5069         kfree(ev);
5070 }
5071
5072
5073 static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR,
5074                 megasas_sysfs_show_poll_mode_io,
5075                 megasas_sysfs_set_poll_mode_io);
5076
5077 /**
5078  * megasas_init - Driver load entry point
5079  */
5080 static int __init megasas_init(void)
5081 {
5082         int rval;
5083
5084         /*
5085          * Announce driver version and other information
5086          */
5087         printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5088                MEGASAS_EXT_VERSION);
5089
5090         support_poll_for_event = 2;
5091         support_device_change = 1;
5092
5093         memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
5094
5095         /*
5096          * Register character device node
5097          */
5098         rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5099
5100         if (rval < 0) {
5101                 printk(KERN_DEBUG "megasas: failed to open device node\n");
5102                 return rval;
5103         }
5104
5105         megasas_mgmt_majorno = rval;
5106
5107         /*
5108          * Register ourselves as PCI hotplug module
5109          */
5110         rval = pci_register_driver(&megasas_pci_driver);
5111
5112         if (rval) {
5113                 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5114                 goto err_pcidrv;
5115         }
5116
5117         rval = driver_create_file(&megasas_pci_driver.driver,
5118                                   &driver_attr_version);
5119         if (rval)
5120                 goto err_dcf_attr_ver;
5121         rval = driver_create_file(&megasas_pci_driver.driver,
5122                                   &driver_attr_release_date);
5123         if (rval)
5124                 goto err_dcf_rel_date;
5125
5126         rval = driver_create_file(&megasas_pci_driver.driver,
5127                                 &driver_attr_support_poll_for_event);
5128         if (rval)
5129                 goto err_dcf_support_poll_for_event;
5130
5131         rval = driver_create_file(&megasas_pci_driver.driver,
5132                                   &driver_attr_dbg_lvl);
5133         if (rval)
5134                 goto err_dcf_dbg_lvl;
5135         rval = driver_create_file(&megasas_pci_driver.driver,
5136                                   &driver_attr_poll_mode_io);
5137         if (rval)
5138                 goto err_dcf_poll_mode_io;
5139
5140         rval = driver_create_file(&megasas_pci_driver.driver,
5141                                 &driver_attr_support_device_change);
5142         if (rval)
5143                 goto err_dcf_support_device_change;
5144
5145         return rval;
5146
5147 err_dcf_support_device_change:
5148         driver_remove_file(&megasas_pci_driver.driver,
5149                   &driver_attr_poll_mode_io);
5150
5151 err_dcf_poll_mode_io:
5152         driver_remove_file(&megasas_pci_driver.driver,
5153                            &driver_attr_dbg_lvl);
5154 err_dcf_dbg_lvl:
5155         driver_remove_file(&megasas_pci_driver.driver,
5156                         &driver_attr_support_poll_for_event);
5157
5158 err_dcf_support_poll_for_event:
5159         driver_remove_file(&megasas_pci_driver.driver,
5160                            &driver_attr_release_date);
5161
5162 err_dcf_rel_date:
5163         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5164 err_dcf_attr_ver:
5165         pci_unregister_driver(&megasas_pci_driver);
5166 err_pcidrv:
5167         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5168         return rval;
5169 }
5170
5171 /**
5172  * megasas_exit - Driver unload entry point
5173  */
5174 static void __exit megasas_exit(void)
5175 {
5176         driver_remove_file(&megasas_pci_driver.driver,
5177                            &driver_attr_poll_mode_io);
5178         driver_remove_file(&megasas_pci_driver.driver,
5179                            &driver_attr_dbg_lvl);
5180         driver_remove_file(&megasas_pci_driver.driver,
5181                         &driver_attr_support_poll_for_event);
5182         driver_remove_file(&megasas_pci_driver.driver,
5183                         &driver_attr_support_device_change);
5184         driver_remove_file(&megasas_pci_driver.driver,
5185                            &driver_attr_release_date);
5186         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5187
5188         pci_unregister_driver(&megasas_pci_driver);
5189         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5190 }
5191
5192 module_init(megasas_init);
5193 module_exit(megasas_exit);