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