]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/scsi/megaraid/megaraid_sas_base.c
megaraid_sas: use dev_printk when possible
[karo-tx-linux.git] / drivers / scsi / megaraid / megaraid_sas_base.c
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2003-2013  LSI Corporation
5  *  Copyright (c) 2013-2014  Avago Technologies
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  *  Authors: Avago Technologies
21  *           Sreenivas Bagalkote
22  *           Sumant Patro
23  *           Bo Yang
24  *           Adam Radford
25  *           Kashyap Desai <kashyap.desai@avagotech.com>
26  *           Sumit Saxena <sumit.saxena@avagotech.com>
27  *
28  *  Send feedback to: megaraidlinux.pdl@avagotech.com
29  *
30  *  Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31  *  San Jose, California 95131
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/slab.h>
45 #include <asm/uaccess.h>
46 #include <linux/fs.h>
47 #include <linux/compat.h>
48 #include <linux/blkdev.h>
49 #include <linux/mutex.h>
50 #include <linux/poll.h>
51
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_tcq.h>
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
59
60 /*
61  * Number of sectors per IO command
62  * Will be set in megasas_init_mfi if user does not provide
63  */
64 static unsigned int max_sectors;
65 module_param_named(max_sectors, max_sectors, int, 0);
66 MODULE_PARM_DESC(max_sectors,
67         "Maximum number of sectors per IO command");
68
69 static int msix_disable;
70 module_param(msix_disable, int, S_IRUGO);
71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
72
73 static unsigned int msix_vectors;
74 module_param(msix_vectors, int, S_IRUGO);
75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
76
77 static int allow_vf_ioctls;
78 module_param(allow_vf_ioctls, int, S_IRUGO);
79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
80
81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
82 module_param(throttlequeuedepth, int, S_IRUGO);
83 MODULE_PARM_DESC(throttlequeuedepth,
84         "Adapter queue depth when throttled due to I/O timeout. Default: 16");
85
86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
87 module_param(resetwaittime, int, S_IRUGO);
88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
89                  "before resetting adapter. Default: 180");
90
91 int smp_affinity_enable = 1;
92 module_param(smp_affinity_enable, int, S_IRUGO);
93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
94
95 MODULE_LICENSE("GPL");
96 MODULE_VERSION(MEGASAS_VERSION);
97 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
98 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
99
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
103                                  u8 query_type);
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106                                 u32 seq_num, u32 class_locale_word);
107 /*
108  * PCI ID table for all supported controllers
109  */
110 static struct pci_device_id megasas_pci_table[] = {
111
112         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
113         /* xscale IOP */
114         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
115         /* ppc IOP */
116         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
117         /* ppc IOP */
118         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
119         /* gen2*/
120         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
121         /* gen2*/
122         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
123         /* skinny*/
124         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
125         /* skinny*/
126         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127         /* xscale IOP, vega */
128         {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
129         /* xscale IOP */
130         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131         /* Fusion */
132         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133         /* Plasma */
134         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
135         /* Invader */
136         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
137         /* Fury */
138         {}
139 };
140
141 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
142
143 static int megasas_mgmt_majorno;
144 struct megasas_mgmt_info megasas_mgmt_info;
145 static struct fasync_struct *megasas_async_queue;
146 static DEFINE_MUTEX(megasas_async_queue_mutex);
147
148 static int megasas_poll_wait_aen;
149 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
150 static u32 support_poll_for_event;
151 u32 megasas_dbg_lvl;
152 static u32 support_device_change;
153
154 /* define lock for aen poll */
155 spinlock_t poll_aen_lock;
156
157 void
158 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
159                      u8 alt_status);
160 static u32
161 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
162 static int
163 megasas_adp_reset_gen2(struct megasas_instance *instance,
164                        struct megasas_register_set __iomem *reg_set);
165 static irqreturn_t megasas_isr(int irq, void *devp);
166 static u32
167 megasas_init_adapter_mfi(struct megasas_instance *instance);
168 u32
169 megasas_build_and_issue_cmd(struct megasas_instance *instance,
170                             struct scsi_cmnd *scmd);
171 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
172 void
173 megasas_release_fusion(struct megasas_instance *instance);
174 int
175 megasas_ioc_init_fusion(struct megasas_instance *instance);
176 void
177 megasas_free_cmds_fusion(struct megasas_instance *instance);
178 u8
179 megasas_get_map_info(struct megasas_instance *instance);
180 int
181 megasas_sync_map_info(struct megasas_instance *instance);
182 int
183 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
184         int seconds);
185 void megasas_reset_reply_desc(struct megasas_instance *instance);
186 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
187 void megasas_fusion_ocr_wq(struct work_struct *work);
188 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
189                                          int initial);
190 int megasas_check_mpio_paths(struct megasas_instance *instance,
191                              struct scsi_cmnd *scmd);
192
193 void
194 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
195 {
196         instance->instancet->fire_cmd(instance,
197                 cmd->frame_phys_addr, 0, instance->reg_set);
198 }
199
200 /**
201  * megasas_get_cmd -    Get a command from the free pool
202  * @instance:           Adapter soft state
203  *
204  * Returns a free command from the pool
205  */
206 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
207                                                   *instance)
208 {
209         unsigned long flags;
210         struct megasas_cmd *cmd = NULL;
211
212         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
213
214         if (!list_empty(&instance->cmd_pool)) {
215                 cmd = list_entry((&instance->cmd_pool)->next,
216                                  struct megasas_cmd, list);
217                 list_del_init(&cmd->list);
218         } else {
219                 dev_err(&instance->pdev->dev, "Command pool empty!\n");
220         }
221
222         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
223         return cmd;
224 }
225
226 /**
227  * megasas_return_cmd - Return a cmd to free command pool
228  * @instance:           Adapter soft state
229  * @cmd:                Command packet to be returned to free command pool
230  */
231 inline void
232 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
233 {
234         unsigned long flags;
235         u32 blk_tags;
236         struct megasas_cmd_fusion *cmd_fusion;
237         struct fusion_context *fusion = instance->ctrl_context;
238
239         /* This flag is used only for fusion adapter.
240          * Wait for Interrupt for Polled mode DCMD
241          */
242         if (cmd->flags & DRV_DCMD_POLLED_MODE)
243                 return;
244
245         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
246
247         if (fusion) {
248                 blk_tags = instance->max_scsi_cmds + cmd->index;
249                 cmd_fusion = fusion->cmd_list[blk_tags];
250                 megasas_return_cmd_fusion(instance, cmd_fusion);
251         }
252         cmd->scmd = NULL;
253         cmd->frame_count = 0;
254         cmd->flags = 0;
255         if (!fusion && reset_devices)
256                 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
257         list_add(&cmd->list, (&instance->cmd_pool)->next);
258
259         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
260
261 }
262
263 /**
264 *       The following functions are defined for xscale
265 *       (deviceid : 1064R, PERC5) controllers
266 */
267
268 /**
269  * megasas_enable_intr_xscale - Enables interrupts
270  * @regs:                       MFI register set
271  */
272 static inline void
273 megasas_enable_intr_xscale(struct megasas_instance *instance)
274 {
275         struct megasas_register_set __iomem *regs;
276         regs = instance->reg_set;
277         writel(0, &(regs)->outbound_intr_mask);
278
279         /* Dummy readl to force pci flush */
280         readl(&regs->outbound_intr_mask);
281 }
282
283 /**
284  * megasas_disable_intr_xscale -Disables interrupt
285  * @regs:                       MFI register set
286  */
287 static inline void
288 megasas_disable_intr_xscale(struct megasas_instance *instance)
289 {
290         struct megasas_register_set __iomem *regs;
291         u32 mask = 0x1f;
292         regs = instance->reg_set;
293         writel(mask, &regs->outbound_intr_mask);
294         /* Dummy readl to force pci flush */
295         readl(&regs->outbound_intr_mask);
296 }
297
298 /**
299  * megasas_read_fw_status_reg_xscale - returns the current FW status value
300  * @regs:                       MFI register set
301  */
302 static u32
303 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
304 {
305         return readl(&(regs)->outbound_msg_0);
306 }
307 /**
308  * megasas_clear_interrupt_xscale -     Check & clear interrupt
309  * @regs:                               MFI register set
310  */
311 static int
312 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
313 {
314         u32 status;
315         u32 mfiStatus = 0;
316         /*
317          * Check if it is our interrupt
318          */
319         status = readl(&regs->outbound_intr_status);
320
321         if (status & MFI_OB_INTR_STATUS_MASK)
322                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
323         if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
324                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
325
326         /*
327          * Clear the interrupt by writing back the same value
328          */
329         if (mfiStatus)
330                 writel(status, &regs->outbound_intr_status);
331
332         /* Dummy readl to force pci flush */
333         readl(&regs->outbound_intr_status);
334
335         return mfiStatus;
336 }
337
338 /**
339  * megasas_fire_cmd_xscale -    Sends command to the FW
340  * @frame_phys_addr :           Physical address of cmd
341  * @frame_count :               Number of frames for the command
342  * @regs :                      MFI register set
343  */
344 static inline void
345 megasas_fire_cmd_xscale(struct megasas_instance *instance,
346                 dma_addr_t frame_phys_addr,
347                 u32 frame_count,
348                 struct megasas_register_set __iomem *regs)
349 {
350         unsigned long flags;
351         spin_lock_irqsave(&instance->hba_lock, flags);
352         writel((frame_phys_addr >> 3)|(frame_count),
353                &(regs)->inbound_queue_port);
354         spin_unlock_irqrestore(&instance->hba_lock, flags);
355 }
356
357 /**
358  * megasas_adp_reset_xscale -  For controller reset
359  * @regs:                              MFI register set
360  */
361 static int
362 megasas_adp_reset_xscale(struct megasas_instance *instance,
363         struct megasas_register_set __iomem *regs)
364 {
365         u32 i;
366         u32 pcidata;
367         writel(MFI_ADP_RESET, &regs->inbound_doorbell);
368
369         for (i = 0; i < 3; i++)
370                 msleep(1000); /* sleep for 3 secs */
371         pcidata  = 0;
372         pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
373         dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
374         if (pcidata & 0x2) {
375                 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
376                 pcidata &= ~0x2;
377                 pci_write_config_dword(instance->pdev,
378                                 MFI_1068_PCSR_OFFSET, pcidata);
379
380                 for (i = 0; i < 2; i++)
381                         msleep(1000); /* need to wait 2 secs again */
382
383                 pcidata  = 0;
384                 pci_read_config_dword(instance->pdev,
385                                 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
386                 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
387                 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
388                         dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
389                         pcidata = 0;
390                         pci_write_config_dword(instance->pdev,
391                                 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
392                 }
393         }
394         return 0;
395 }
396
397 /**
398  * megasas_check_reset_xscale - For controller reset check
399  * @regs:                               MFI register set
400  */
401 static int
402 megasas_check_reset_xscale(struct megasas_instance *instance,
403                 struct megasas_register_set __iomem *regs)
404 {
405
406         if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
407             (le32_to_cpu(*instance->consumer) ==
408                 MEGASAS_ADPRESET_INPROG_SIGN))
409                 return 1;
410         return 0;
411 }
412
413 static struct megasas_instance_template megasas_instance_template_xscale = {
414
415         .fire_cmd = megasas_fire_cmd_xscale,
416         .enable_intr = megasas_enable_intr_xscale,
417         .disable_intr = megasas_disable_intr_xscale,
418         .clear_intr = megasas_clear_intr_xscale,
419         .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
420         .adp_reset = megasas_adp_reset_xscale,
421         .check_reset = megasas_check_reset_xscale,
422         .service_isr = megasas_isr,
423         .tasklet = megasas_complete_cmd_dpc,
424         .init_adapter = megasas_init_adapter_mfi,
425         .build_and_issue_cmd = megasas_build_and_issue_cmd,
426         .issue_dcmd = megasas_issue_dcmd,
427 };
428
429 /**
430 *       This is the end of set of functions & definitions specific
431 *       to xscale (deviceid : 1064R, PERC5) controllers
432 */
433
434 /**
435 *       The following functions are defined for ppc (deviceid : 0x60)
436 *       controllers
437 */
438
439 /**
440  * megasas_enable_intr_ppc -    Enables interrupts
441  * @regs:                       MFI register set
442  */
443 static inline void
444 megasas_enable_intr_ppc(struct megasas_instance *instance)
445 {
446         struct megasas_register_set __iomem *regs;
447         regs = instance->reg_set;
448         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
449
450         writel(~0x80000000, &(regs)->outbound_intr_mask);
451
452         /* Dummy readl to force pci flush */
453         readl(&regs->outbound_intr_mask);
454 }
455
456 /**
457  * megasas_disable_intr_ppc -   Disable interrupt
458  * @regs:                       MFI register set
459  */
460 static inline void
461 megasas_disable_intr_ppc(struct megasas_instance *instance)
462 {
463         struct megasas_register_set __iomem *regs;
464         u32 mask = 0xFFFFFFFF;
465         regs = instance->reg_set;
466         writel(mask, &regs->outbound_intr_mask);
467         /* Dummy readl to force pci flush */
468         readl(&regs->outbound_intr_mask);
469 }
470
471 /**
472  * megasas_read_fw_status_reg_ppc - returns the current FW status value
473  * @regs:                       MFI register set
474  */
475 static u32
476 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
477 {
478         return readl(&(regs)->outbound_scratch_pad);
479 }
480
481 /**
482  * megasas_clear_interrupt_ppc -        Check & clear interrupt
483  * @regs:                               MFI register set
484  */
485 static int
486 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
487 {
488         u32 status, mfiStatus = 0;
489
490         /*
491          * Check if it is our interrupt
492          */
493         status = readl(&regs->outbound_intr_status);
494
495         if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
496                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
497
498         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
499                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
500
501         /*
502          * Clear the interrupt by writing back the same value
503          */
504         writel(status, &regs->outbound_doorbell_clear);
505
506         /* Dummy readl to force pci flush */
507         readl(&regs->outbound_doorbell_clear);
508
509         return mfiStatus;
510 }
511
512 /**
513  * megasas_fire_cmd_ppc -       Sends command to the FW
514  * @frame_phys_addr :           Physical address of cmd
515  * @frame_count :               Number of frames for the command
516  * @regs :                      MFI register set
517  */
518 static inline void
519 megasas_fire_cmd_ppc(struct megasas_instance *instance,
520                 dma_addr_t frame_phys_addr,
521                 u32 frame_count,
522                 struct megasas_register_set __iomem *regs)
523 {
524         unsigned long flags;
525         spin_lock_irqsave(&instance->hba_lock, flags);
526         writel((frame_phys_addr | (frame_count<<1))|1,
527                         &(regs)->inbound_queue_port);
528         spin_unlock_irqrestore(&instance->hba_lock, flags);
529 }
530
531 /**
532  * megasas_check_reset_ppc -    For controller reset check
533  * @regs:                               MFI register set
534  */
535 static int
536 megasas_check_reset_ppc(struct megasas_instance *instance,
537                         struct megasas_register_set __iomem *regs)
538 {
539         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
540                 return 1;
541
542         return 0;
543 }
544
545 static struct megasas_instance_template megasas_instance_template_ppc = {
546
547         .fire_cmd = megasas_fire_cmd_ppc,
548         .enable_intr = megasas_enable_intr_ppc,
549         .disable_intr = megasas_disable_intr_ppc,
550         .clear_intr = megasas_clear_intr_ppc,
551         .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
552         .adp_reset = megasas_adp_reset_xscale,
553         .check_reset = megasas_check_reset_ppc,
554         .service_isr = megasas_isr,
555         .tasklet = megasas_complete_cmd_dpc,
556         .init_adapter = megasas_init_adapter_mfi,
557         .build_and_issue_cmd = megasas_build_and_issue_cmd,
558         .issue_dcmd = megasas_issue_dcmd,
559 };
560
561 /**
562  * megasas_enable_intr_skinny - Enables interrupts
563  * @regs:                       MFI register set
564  */
565 static inline void
566 megasas_enable_intr_skinny(struct megasas_instance *instance)
567 {
568         struct megasas_register_set __iomem *regs;
569         regs = instance->reg_set;
570         writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
571
572         writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
573
574         /* Dummy readl to force pci flush */
575         readl(&regs->outbound_intr_mask);
576 }
577
578 /**
579  * megasas_disable_intr_skinny -        Disables interrupt
580  * @regs:                       MFI register set
581  */
582 static inline void
583 megasas_disable_intr_skinny(struct megasas_instance *instance)
584 {
585         struct megasas_register_set __iomem *regs;
586         u32 mask = 0xFFFFFFFF;
587         regs = instance->reg_set;
588         writel(mask, &regs->outbound_intr_mask);
589         /* Dummy readl to force pci flush */
590         readl(&regs->outbound_intr_mask);
591 }
592
593 /**
594  * megasas_read_fw_status_reg_skinny - returns the current FW status value
595  * @regs:                       MFI register set
596  */
597 static u32
598 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
599 {
600         return readl(&(regs)->outbound_scratch_pad);
601 }
602
603 /**
604  * megasas_clear_interrupt_skinny -     Check & clear interrupt
605  * @regs:                               MFI register set
606  */
607 static int
608 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
609 {
610         u32 status;
611         u32 mfiStatus = 0;
612
613         /*
614          * Check if it is our interrupt
615          */
616         status = readl(&regs->outbound_intr_status);
617
618         if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
619                 return 0;
620         }
621
622         /*
623          * Check if it is our interrupt
624          */
625         if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
626             MFI_STATE_FAULT) {
627                 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
628         } else
629                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
630
631         /*
632          * Clear the interrupt by writing back the same value
633          */
634         writel(status, &regs->outbound_intr_status);
635
636         /*
637         * dummy read to flush PCI
638         */
639         readl(&regs->outbound_intr_status);
640
641         return mfiStatus;
642 }
643
644 /**
645  * megasas_fire_cmd_skinny -    Sends command to the FW
646  * @frame_phys_addr :           Physical address of cmd
647  * @frame_count :               Number of frames for the command
648  * @regs :                      MFI register set
649  */
650 static inline void
651 megasas_fire_cmd_skinny(struct megasas_instance *instance,
652                         dma_addr_t frame_phys_addr,
653                         u32 frame_count,
654                         struct megasas_register_set __iomem *regs)
655 {
656         unsigned long flags;
657         spin_lock_irqsave(&instance->hba_lock, flags);
658         writel(upper_32_bits(frame_phys_addr),
659                &(regs)->inbound_high_queue_port);
660         writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
661                &(regs)->inbound_low_queue_port);
662         spin_unlock_irqrestore(&instance->hba_lock, flags);
663 }
664
665 /**
666  * megasas_check_reset_skinny - For controller reset check
667  * @regs:                               MFI register set
668  */
669 static int
670 megasas_check_reset_skinny(struct megasas_instance *instance,
671                                 struct megasas_register_set __iomem *regs)
672 {
673         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
674                 return 1;
675
676         return 0;
677 }
678
679 static struct megasas_instance_template megasas_instance_template_skinny = {
680
681         .fire_cmd = megasas_fire_cmd_skinny,
682         .enable_intr = megasas_enable_intr_skinny,
683         .disable_intr = megasas_disable_intr_skinny,
684         .clear_intr = megasas_clear_intr_skinny,
685         .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
686         .adp_reset = megasas_adp_reset_gen2,
687         .check_reset = megasas_check_reset_skinny,
688         .service_isr = megasas_isr,
689         .tasklet = megasas_complete_cmd_dpc,
690         .init_adapter = megasas_init_adapter_mfi,
691         .build_and_issue_cmd = megasas_build_and_issue_cmd,
692         .issue_dcmd = megasas_issue_dcmd,
693 };
694
695
696 /**
697 *       The following functions are defined for gen2 (deviceid : 0x78 0x79)
698 *       controllers
699 */
700
701 /**
702  * megasas_enable_intr_gen2 -  Enables interrupts
703  * @regs:                      MFI register set
704  */
705 static inline void
706 megasas_enable_intr_gen2(struct megasas_instance *instance)
707 {
708         struct megasas_register_set __iomem *regs;
709         regs = instance->reg_set;
710         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
711
712         /* write ~0x00000005 (4 & 1) to the intr mask*/
713         writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
714
715         /* Dummy readl to force pci flush */
716         readl(&regs->outbound_intr_mask);
717 }
718
719 /**
720  * megasas_disable_intr_gen2 - Disables interrupt
721  * @regs:                      MFI register set
722  */
723 static inline void
724 megasas_disable_intr_gen2(struct megasas_instance *instance)
725 {
726         struct megasas_register_set __iomem *regs;
727         u32 mask = 0xFFFFFFFF;
728         regs = instance->reg_set;
729         writel(mask, &regs->outbound_intr_mask);
730         /* Dummy readl to force pci flush */
731         readl(&regs->outbound_intr_mask);
732 }
733
734 /**
735  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
736  * @regs:                      MFI register set
737  */
738 static u32
739 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
740 {
741         return readl(&(regs)->outbound_scratch_pad);
742 }
743
744 /**
745  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
746  * @regs:                              MFI register set
747  */
748 static int
749 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
750 {
751         u32 status;
752         u32 mfiStatus = 0;
753         /*
754          * Check if it is our interrupt
755          */
756         status = readl(&regs->outbound_intr_status);
757
758         if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
759                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
760         }
761         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
762                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
763         }
764
765         /*
766          * Clear the interrupt by writing back the same value
767          */
768         if (mfiStatus)
769                 writel(status, &regs->outbound_doorbell_clear);
770
771         /* Dummy readl to force pci flush */
772         readl(&regs->outbound_intr_status);
773
774         return mfiStatus;
775 }
776 /**
777  * megasas_fire_cmd_gen2 -     Sends command to the FW
778  * @frame_phys_addr :          Physical address of cmd
779  * @frame_count :              Number of frames for the command
780  * @regs :                     MFI register set
781  */
782 static inline void
783 megasas_fire_cmd_gen2(struct megasas_instance *instance,
784                         dma_addr_t frame_phys_addr,
785                         u32 frame_count,
786                         struct megasas_register_set __iomem *regs)
787 {
788         unsigned long flags;
789         spin_lock_irqsave(&instance->hba_lock, flags);
790         writel((frame_phys_addr | (frame_count<<1))|1,
791                         &(regs)->inbound_queue_port);
792         spin_unlock_irqrestore(&instance->hba_lock, flags);
793 }
794
795 /**
796  * megasas_adp_reset_gen2 -     For controller reset
797  * @regs:                               MFI register set
798  */
799 static int
800 megasas_adp_reset_gen2(struct megasas_instance *instance,
801                         struct megasas_register_set __iomem *reg_set)
802 {
803         u32                     retry = 0 ;
804         u32                     HostDiag;
805         u32 __iomem             *seq_offset = &reg_set->seq_offset;
806         u32 __iomem             *hostdiag_offset = &reg_set->host_diag;
807
808         if (instance->instancet == &megasas_instance_template_skinny) {
809                 seq_offset = &reg_set->fusion_seq_offset;
810                 hostdiag_offset = &reg_set->fusion_host_diag;
811         }
812
813         writel(0, seq_offset);
814         writel(4, seq_offset);
815         writel(0xb, seq_offset);
816         writel(2, seq_offset);
817         writel(7, seq_offset);
818         writel(0xd, seq_offset);
819
820         msleep(1000);
821
822         HostDiag = (u32)readl(hostdiag_offset);
823
824         while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
825                 msleep(100);
826                 HostDiag = (u32)readl(hostdiag_offset);
827                 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
828                                         retry, HostDiag);
829
830                 if (retry++ >= 100)
831                         return 1;
832
833         }
834
835         dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
836
837         writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
838
839         ssleep(10);
840
841         HostDiag = (u32)readl(hostdiag_offset);
842         while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
843                 msleep(100);
844                 HostDiag = (u32)readl(hostdiag_offset);
845                 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
846                                 retry, HostDiag);
847
848                 if (retry++ >= 1000)
849                         return 1;
850
851         }
852         return 0;
853 }
854
855 /**
856  * megasas_check_reset_gen2 -   For controller reset check
857  * @regs:                               MFI register set
858  */
859 static int
860 megasas_check_reset_gen2(struct megasas_instance *instance,
861                 struct megasas_register_set __iomem *regs)
862 {
863         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
864                 return 1;
865         }
866
867         return 0;
868 }
869
870 static struct megasas_instance_template megasas_instance_template_gen2 = {
871
872         .fire_cmd = megasas_fire_cmd_gen2,
873         .enable_intr = megasas_enable_intr_gen2,
874         .disable_intr = megasas_disable_intr_gen2,
875         .clear_intr = megasas_clear_intr_gen2,
876         .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
877         .adp_reset = megasas_adp_reset_gen2,
878         .check_reset = megasas_check_reset_gen2,
879         .service_isr = megasas_isr,
880         .tasklet = megasas_complete_cmd_dpc,
881         .init_adapter = megasas_init_adapter_mfi,
882         .build_and_issue_cmd = megasas_build_and_issue_cmd,
883         .issue_dcmd = megasas_issue_dcmd,
884 };
885
886 /**
887 *       This is the end of set of functions & definitions
888 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
889 */
890
891 /*
892  * Template added for TB (Fusion)
893  */
894 extern struct megasas_instance_template megasas_instance_template_fusion;
895
896 /**
897  * megasas_issue_polled -       Issues a polling command
898  * @instance:                   Adapter soft state
899  * @cmd:                        Command packet to be issued
900  *
901  * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
902  */
903 int
904 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
905 {
906         int seconds;
907
908         struct megasas_header *frame_hdr = &cmd->frame->hdr;
909
910         frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
911         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
912
913         /*
914          * Issue the frame using inbound queue port
915          */
916         instance->instancet->issue_dcmd(instance, cmd);
917
918         /*
919          * Wait for cmd_status to change
920          */
921         if (instance->requestorId)
922                 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
923         else
924                 seconds = MFI_POLL_TIMEOUT_SECS;
925         return wait_and_poll(instance, cmd, seconds);
926 }
927
928 /**
929  * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
930  * @instance:                   Adapter soft state
931  * @cmd:                        Command to be issued
932  * @timeout:                    Timeout in seconds
933  *
934  * This function waits on an event for the command to be returned from ISR.
935  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
936  * Used to issue ioctl commands.
937  */
938 int
939 megasas_issue_blocked_cmd(struct megasas_instance *instance,
940                           struct megasas_cmd *cmd, int timeout)
941 {
942         int ret = 0;
943         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
944
945         instance->instancet->issue_dcmd(instance, cmd);
946         if (timeout) {
947                 ret = wait_event_timeout(instance->int_cmd_wait_q,
948                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
949                 if (!ret)
950                         return 1;
951         } else
952                 wait_event(instance->int_cmd_wait_q,
953                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
954
955         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
956                 0 : 1;
957 }
958
959 /**
960  * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
961  * @instance:                           Adapter soft state
962  * @cmd_to_abort:                       Previously issued cmd to be aborted
963  * @timeout:                            Timeout in seconds
964  *
965  * MFI firmware can abort previously issued AEN comamnd (automatic event
966  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
967  * cmd and waits for return status.
968  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
969  */
970 static int
971 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
972                                 struct megasas_cmd *cmd_to_abort, int timeout)
973 {
974         struct megasas_cmd *cmd;
975         struct megasas_abort_frame *abort_fr;
976         int ret = 0;
977
978         cmd = megasas_get_cmd(instance);
979
980         if (!cmd)
981                 return -1;
982
983         abort_fr = &cmd->frame->abort;
984
985         /*
986          * Prepare and issue the abort frame
987          */
988         abort_fr->cmd = MFI_CMD_ABORT;
989         abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
990         abort_fr->flags = cpu_to_le16(0);
991         abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
992         abort_fr->abort_mfi_phys_addr_lo =
993                 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
994         abort_fr->abort_mfi_phys_addr_hi =
995                 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
996
997         cmd->sync_cmd = 1;
998         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
999
1000         instance->instancet->issue_dcmd(instance, cmd);
1001
1002         if (timeout) {
1003                 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1004                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1005                 if (!ret) {
1006                         dev_err(&instance->pdev->dev, "Command timedout"
1007                                 "from %s\n", __func__);
1008                         return 1;
1009                 }
1010         } else
1011                 wait_event(instance->abort_cmd_wait_q,
1012                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1013
1014         cmd->sync_cmd = 0;
1015
1016         megasas_return_cmd(instance, cmd);
1017         return 0;
1018 }
1019
1020 /**
1021  * megasas_make_sgl32 - Prepares 32-bit SGL
1022  * @instance:           Adapter soft state
1023  * @scp:                SCSI command from the mid-layer
1024  * @mfi_sgl:            SGL to be filled in
1025  *
1026  * If successful, this function returns the number of SG elements. Otherwise,
1027  * it returnes -1.
1028  */
1029 static int
1030 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1031                    union megasas_sgl *mfi_sgl)
1032 {
1033         int i;
1034         int sge_count;
1035         struct scatterlist *os_sgl;
1036
1037         sge_count = scsi_dma_map(scp);
1038         BUG_ON(sge_count < 0);
1039
1040         if (sge_count) {
1041                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1042                         mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1043                         mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1044                 }
1045         }
1046         return sge_count;
1047 }
1048
1049 /**
1050  * megasas_make_sgl64 - Prepares 64-bit SGL
1051  * @instance:           Adapter soft state
1052  * @scp:                SCSI command from the mid-layer
1053  * @mfi_sgl:            SGL to be filled in
1054  *
1055  * If successful, this function returns the number of SG elements. Otherwise,
1056  * it returnes -1.
1057  */
1058 static int
1059 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1060                    union megasas_sgl *mfi_sgl)
1061 {
1062         int i;
1063         int sge_count;
1064         struct scatterlist *os_sgl;
1065
1066         sge_count = scsi_dma_map(scp);
1067         BUG_ON(sge_count < 0);
1068
1069         if (sge_count) {
1070                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1071                         mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1072                         mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1073                 }
1074         }
1075         return sge_count;
1076 }
1077
1078 /**
1079  * megasas_make_sgl_skinny - Prepares IEEE SGL
1080  * @instance:           Adapter soft state
1081  * @scp:                SCSI command from the mid-layer
1082  * @mfi_sgl:            SGL to be filled in
1083  *
1084  * If successful, this function returns the number of SG elements. Otherwise,
1085  * it returnes -1.
1086  */
1087 static int
1088 megasas_make_sgl_skinny(struct megasas_instance *instance,
1089                 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1090 {
1091         int i;
1092         int sge_count;
1093         struct scatterlist *os_sgl;
1094
1095         sge_count = scsi_dma_map(scp);
1096
1097         if (sge_count) {
1098                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1099                         mfi_sgl->sge_skinny[i].length =
1100                                 cpu_to_le32(sg_dma_len(os_sgl));
1101                         mfi_sgl->sge_skinny[i].phys_addr =
1102                                 cpu_to_le64(sg_dma_address(os_sgl));
1103                         mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1104                 }
1105         }
1106         return sge_count;
1107 }
1108
1109  /**
1110  * megasas_get_frame_count - Computes the number of frames
1111  * @frame_type          : type of frame- io or pthru frame
1112  * @sge_count           : number of sg elements
1113  *
1114  * Returns the number of frames required for numnber of sge's (sge_count)
1115  */
1116
1117 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1118                         u8 sge_count, u8 frame_type)
1119 {
1120         int num_cnt;
1121         int sge_bytes;
1122         u32 sge_sz;
1123         u32 frame_count=0;
1124
1125         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1126             sizeof(struct megasas_sge32);
1127
1128         if (instance->flag_ieee) {
1129                 sge_sz = sizeof(struct megasas_sge_skinny);
1130         }
1131
1132         /*
1133          * Main frame can contain 2 SGEs for 64-bit SGLs and
1134          * 3 SGEs for 32-bit SGLs for ldio &
1135          * 1 SGEs for 64-bit SGLs and
1136          * 2 SGEs for 32-bit SGLs for pthru frame
1137          */
1138         if (unlikely(frame_type == PTHRU_FRAME)) {
1139                 if (instance->flag_ieee == 1) {
1140                         num_cnt = sge_count - 1;
1141                 } else if (IS_DMA64)
1142                         num_cnt = sge_count - 1;
1143                 else
1144                         num_cnt = sge_count - 2;
1145         } else {
1146                 if (instance->flag_ieee == 1) {
1147                         num_cnt = sge_count - 1;
1148                 } else if (IS_DMA64)
1149                         num_cnt = sge_count - 2;
1150                 else
1151                         num_cnt = sge_count - 3;
1152         }
1153
1154         if(num_cnt>0){
1155                 sge_bytes = sge_sz * num_cnt;
1156
1157                 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1158                     ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1159         }
1160         /* Main frame */
1161         frame_count +=1;
1162
1163         if (frame_count > 7)
1164                 frame_count = 8;
1165         return frame_count;
1166 }
1167
1168 /**
1169  * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1170  * @instance:           Adapter soft state
1171  * @scp:                SCSI command
1172  * @cmd:                Command to be prepared in
1173  *
1174  * This function prepares CDB commands. These are typcially pass-through
1175  * commands to the devices.
1176  */
1177 static int
1178 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1179                    struct megasas_cmd *cmd)
1180 {
1181         u32 is_logical;
1182         u32 device_id;
1183         u16 flags = 0;
1184         struct megasas_pthru_frame *pthru;
1185
1186         is_logical = MEGASAS_IS_LOGICAL(scp);
1187         device_id = MEGASAS_DEV_INDEX(scp);
1188         pthru = (struct megasas_pthru_frame *)cmd->frame;
1189
1190         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1191                 flags = MFI_FRAME_DIR_WRITE;
1192         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1193                 flags = MFI_FRAME_DIR_READ;
1194         else if (scp->sc_data_direction == PCI_DMA_NONE)
1195                 flags = MFI_FRAME_DIR_NONE;
1196
1197         if (instance->flag_ieee == 1) {
1198                 flags |= MFI_FRAME_IEEE;
1199         }
1200
1201         /*
1202          * Prepare the DCDB frame
1203          */
1204         pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1205         pthru->cmd_status = 0x0;
1206         pthru->scsi_status = 0x0;
1207         pthru->target_id = device_id;
1208         pthru->lun = scp->device->lun;
1209         pthru->cdb_len = scp->cmd_len;
1210         pthru->timeout = 0;
1211         pthru->pad_0 = 0;
1212         pthru->flags = cpu_to_le16(flags);
1213         pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1214
1215         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1216
1217         /*
1218         * If the command is for the tape device, set the
1219         * pthru timeout to the os layer timeout value.
1220         */
1221         if (scp->device->type == TYPE_TAPE) {
1222                 if ((scp->request->timeout / HZ) > 0xFFFF)
1223                         pthru->timeout = cpu_to_le16(0xFFFF);
1224                 else
1225                         pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1226         }
1227
1228         /*
1229          * Construct SGL
1230          */
1231         if (instance->flag_ieee == 1) {
1232                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1233                 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1234                                                       &pthru->sgl);
1235         } else if (IS_DMA64) {
1236                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1237                 pthru->sge_count = megasas_make_sgl64(instance, scp,
1238                                                       &pthru->sgl);
1239         } else
1240                 pthru->sge_count = megasas_make_sgl32(instance, scp,
1241                                                       &pthru->sgl);
1242
1243         if (pthru->sge_count > instance->max_num_sge) {
1244                 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1245                         pthru->sge_count);
1246                 return 0;
1247         }
1248
1249         /*
1250          * Sense info specific
1251          */
1252         pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1253         pthru->sense_buf_phys_addr_hi =
1254                 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1255         pthru->sense_buf_phys_addr_lo =
1256                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1257
1258         /*
1259          * Compute the total number of frames this command consumes. FW uses
1260          * this number to pull sufficient number of frames from host memory.
1261          */
1262         cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1263                                                         PTHRU_FRAME);
1264
1265         return cmd->frame_count;
1266 }
1267
1268 /**
1269  * megasas_build_ldio - Prepares IOs to logical devices
1270  * @instance:           Adapter soft state
1271  * @scp:                SCSI command
1272  * @cmd:                Command to be prepared
1273  *
1274  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1275  */
1276 static int
1277 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1278                    struct megasas_cmd *cmd)
1279 {
1280         u32 device_id;
1281         u8 sc = scp->cmnd[0];
1282         u16 flags = 0;
1283         struct megasas_io_frame *ldio;
1284
1285         device_id = MEGASAS_DEV_INDEX(scp);
1286         ldio = (struct megasas_io_frame *)cmd->frame;
1287
1288         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1289                 flags = MFI_FRAME_DIR_WRITE;
1290         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1291                 flags = MFI_FRAME_DIR_READ;
1292
1293         if (instance->flag_ieee == 1) {
1294                 flags |= MFI_FRAME_IEEE;
1295         }
1296
1297         /*
1298          * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1299          */
1300         ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1301         ldio->cmd_status = 0x0;
1302         ldio->scsi_status = 0x0;
1303         ldio->target_id = device_id;
1304         ldio->timeout = 0;
1305         ldio->reserved_0 = 0;
1306         ldio->pad_0 = 0;
1307         ldio->flags = cpu_to_le16(flags);
1308         ldio->start_lba_hi = 0;
1309         ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1310
1311         /*
1312          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1313          */
1314         if (scp->cmd_len == 6) {
1315                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1316                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1317                                                  ((u32) scp->cmnd[2] << 8) |
1318                                                  (u32) scp->cmnd[3]);
1319
1320                 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1321         }
1322
1323         /*
1324          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1325          */
1326         else if (scp->cmd_len == 10) {
1327                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1328                                               ((u32) scp->cmnd[7] << 8));
1329                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1330                                                  ((u32) scp->cmnd[3] << 16) |
1331                                                  ((u32) scp->cmnd[4] << 8) |
1332                                                  (u32) scp->cmnd[5]);
1333         }
1334
1335         /*
1336          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1337          */
1338         else if (scp->cmd_len == 12) {
1339                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1340                                               ((u32) scp->cmnd[7] << 16) |
1341                                               ((u32) scp->cmnd[8] << 8) |
1342                                               (u32) scp->cmnd[9]);
1343
1344                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1345                                                  ((u32) scp->cmnd[3] << 16) |
1346                                                  ((u32) scp->cmnd[4] << 8) |
1347                                                  (u32) scp->cmnd[5]);
1348         }
1349
1350         /*
1351          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1352          */
1353         else if (scp->cmd_len == 16) {
1354                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1355                                               ((u32) scp->cmnd[11] << 16) |
1356                                               ((u32) scp->cmnd[12] << 8) |
1357                                               (u32) scp->cmnd[13]);
1358
1359                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1360                                                  ((u32) scp->cmnd[7] << 16) |
1361                                                  ((u32) scp->cmnd[8] << 8) |
1362                                                  (u32) scp->cmnd[9]);
1363
1364                 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1365                                                  ((u32) scp->cmnd[3] << 16) |
1366                                                  ((u32) scp->cmnd[4] << 8) |
1367                                                  (u32) scp->cmnd[5]);
1368
1369         }
1370
1371         /*
1372          * Construct SGL
1373          */
1374         if (instance->flag_ieee) {
1375                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1376                 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1377                                               &ldio->sgl);
1378         } else if (IS_DMA64) {
1379                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1380                 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1381         } else
1382                 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1383
1384         if (ldio->sge_count > instance->max_num_sge) {
1385                 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1386                         ldio->sge_count);
1387                 return 0;
1388         }
1389
1390         /*
1391          * Sense info specific
1392          */
1393         ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1394         ldio->sense_buf_phys_addr_hi = 0;
1395         ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1396
1397         /*
1398          * Compute the total number of frames this command consumes. FW uses
1399          * this number to pull sufficient number of frames from host memory.
1400          */
1401         cmd->frame_count = megasas_get_frame_count(instance,
1402                         ldio->sge_count, IO_FRAME);
1403
1404         return cmd->frame_count;
1405 }
1406
1407 /**
1408  * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1409  *                              and whether it's RW or non RW
1410  * @scmd:                       SCSI command
1411  *
1412  */
1413 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1414 {
1415         int ret;
1416
1417         switch (cmd->cmnd[0]) {
1418         case READ_10:
1419         case WRITE_10:
1420         case READ_12:
1421         case WRITE_12:
1422         case READ_6:
1423         case WRITE_6:
1424         case READ_16:
1425         case WRITE_16:
1426                 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1427                         READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1428                 break;
1429         default:
1430                 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1431                         NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1432         }
1433         return ret;
1434 }
1435
1436  /**
1437  * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1438  *                                      in FW
1439  * @instance:                           Adapter soft state
1440  */
1441 static inline void
1442 megasas_dump_pending_frames(struct megasas_instance *instance)
1443 {
1444         struct megasas_cmd *cmd;
1445         int i,n;
1446         union megasas_sgl *mfi_sgl;
1447         struct megasas_io_frame *ldio;
1448         struct megasas_pthru_frame *pthru;
1449         u32 sgcount;
1450         u32 max_cmd = instance->max_fw_cmds;
1451
1452         dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1453         dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1454         if (IS_DMA64)
1455                 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1456         else
1457                 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1458
1459         dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1460         for (i = 0; i < max_cmd; i++) {
1461                 cmd = instance->cmd_list[i];
1462                 if(!cmd->scmd)
1463                         continue;
1464                 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1465                 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1466                         ldio = (struct megasas_io_frame *)cmd->frame;
1467                         mfi_sgl = &ldio->sgl;
1468                         sgcount = ldio->sge_count;
1469                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1470                         " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1471                         instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1472                         le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1473                         le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1474                 }
1475                 else {
1476                         pthru = (struct megasas_pthru_frame *) cmd->frame;
1477                         mfi_sgl = &pthru->sgl;
1478                         sgcount = pthru->sge_count;
1479                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1480                         "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1481                         instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1482                         pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1483                         le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1484                 }
1485         if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1486                 for (n = 0; n < sgcount; n++){
1487                         if (IS_DMA64)
1488                                 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1489                                         le32_to_cpu(mfi_sgl->sge64[n].length),
1490                                         le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1491                         else
1492                                 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1493                                         le32_to_cpu(mfi_sgl->sge32[n].length),
1494                                         le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1495                         }
1496                 }
1497         } /*for max_cmd*/
1498         dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1499         for (i = 0; i < max_cmd; i++) {
1500
1501                 cmd = instance->cmd_list[i];
1502
1503                 if(cmd->sync_cmd == 1){
1504                         dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1505                 }
1506         }
1507         dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1508 }
1509
1510 u32
1511 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1512                             struct scsi_cmnd *scmd)
1513 {
1514         struct megasas_cmd *cmd;
1515         u32 frame_count;
1516
1517         cmd = megasas_get_cmd(instance);
1518         if (!cmd)
1519                 return SCSI_MLQUEUE_HOST_BUSY;
1520
1521         /*
1522          * Logical drive command
1523          */
1524         if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1525                 frame_count = megasas_build_ldio(instance, scmd, cmd);
1526         else
1527                 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1528
1529         if (!frame_count)
1530                 goto out_return_cmd;
1531
1532         cmd->scmd = scmd;
1533         scmd->SCp.ptr = (char *)cmd;
1534
1535         /*
1536          * Issue the command to the FW
1537          */
1538         atomic_inc(&instance->fw_outstanding);
1539
1540         instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1541                                 cmd->frame_count-1, instance->reg_set);
1542
1543         return 0;
1544 out_return_cmd:
1545         megasas_return_cmd(instance, cmd);
1546         return 1;
1547 }
1548
1549
1550 /**
1551  * megasas_queue_command -      Queue entry point
1552  * @scmd:                       SCSI command to be queued
1553  * @done:                       Callback entry point
1554  */
1555 static int
1556 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1557 {
1558         struct megasas_instance *instance;
1559         unsigned long flags;
1560
1561         instance = (struct megasas_instance *)
1562             scmd->device->host->hostdata;
1563
1564         if (instance->unload == 1) {
1565                 scmd->result = DID_NO_CONNECT << 16;
1566                 scmd->scsi_done(scmd);
1567                 return 0;
1568         }
1569
1570         if (instance->issuepend_done == 0)
1571                 return SCSI_MLQUEUE_HOST_BUSY;
1572
1573         spin_lock_irqsave(&instance->hba_lock, flags);
1574
1575         /* Check for an mpio path and adjust behavior */
1576         if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1577                 if (megasas_check_mpio_paths(instance, scmd) ==
1578                     (DID_RESET << 16)) {
1579                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1580                         return SCSI_MLQUEUE_HOST_BUSY;
1581                 } else {
1582                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1583                         scmd->result = DID_NO_CONNECT << 16;
1584                         scmd->scsi_done(scmd);
1585                         return 0;
1586                 }
1587         }
1588
1589         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1590                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1591                 scmd->result = DID_NO_CONNECT << 16;
1592                 scmd->scsi_done(scmd);
1593                 return 0;
1594         }
1595
1596         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1597                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1598                 return SCSI_MLQUEUE_HOST_BUSY;
1599         }
1600
1601         spin_unlock_irqrestore(&instance->hba_lock, flags);
1602
1603         scmd->result = 0;
1604
1605         if (MEGASAS_IS_LOGICAL(scmd) &&
1606             (scmd->device->id >= instance->fw_supported_vd_count ||
1607                 scmd->device->lun)) {
1608                 scmd->result = DID_BAD_TARGET << 16;
1609                 goto out_done;
1610         }
1611
1612         switch (scmd->cmnd[0]) {
1613         case SYNCHRONIZE_CACHE:
1614                 /*
1615                  * FW takes care of flush cache on its own
1616                  * No need to send it down
1617                  */
1618                 scmd->result = DID_OK << 16;
1619                 goto out_done;
1620         default:
1621                 break;
1622         }
1623
1624         if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1625                 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1626                 return SCSI_MLQUEUE_HOST_BUSY;
1627         }
1628
1629         return 0;
1630
1631  out_done:
1632         scmd->scsi_done(scmd);
1633         return 0;
1634 }
1635
1636 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1637 {
1638         int i;
1639
1640         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1641
1642                 if ((megasas_mgmt_info.instance[i]) &&
1643                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1644                         return megasas_mgmt_info.instance[i];
1645         }
1646
1647         return NULL;
1648 }
1649
1650 static int megasas_slave_configure(struct scsi_device *sdev)
1651 {
1652         /*
1653         * The RAID firmware may require extended timeouts.
1654         */
1655         blk_queue_rq_timeout(sdev->request_queue,
1656                 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1657
1658         return 0;
1659 }
1660
1661 static int megasas_slave_alloc(struct scsi_device *sdev)
1662 {
1663         u16             pd_index = 0;
1664         struct megasas_instance *instance ;
1665         instance = megasas_lookup_instance(sdev->host->host_no);
1666         if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1667                 /*
1668                  * Open the OS scan to the SYSTEM PD
1669                  */
1670                 pd_index =
1671                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1672                         sdev->id;
1673                 if (instance->pd_list[pd_index].driveState ==
1674                                         MR_PD_STATE_SYSTEM) {
1675                         return 0;
1676                 }
1677                 return -ENXIO;
1678         }
1679         return 0;
1680 }
1681
1682 /*
1683 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1684 *                                       kill adapter
1685 * @instance:                            Adapter soft state
1686 *
1687 */
1688 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1689 {
1690         int i;
1691         struct megasas_cmd *cmd_mfi;
1692         struct megasas_cmd_fusion *cmd_fusion;
1693         struct fusion_context *fusion = instance->ctrl_context;
1694
1695         /* Find all outstanding ioctls */
1696         if (fusion) {
1697                 for (i = 0; i < instance->max_fw_cmds; i++) {
1698                         cmd_fusion = fusion->cmd_list[i];
1699                         if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1700                                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1701                                 if (cmd_mfi->sync_cmd &&
1702                                         cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1703                                         megasas_complete_cmd(instance,
1704                                                              cmd_mfi, DID_OK);
1705                         }
1706                 }
1707         } else {
1708                 for (i = 0; i < instance->max_fw_cmds; i++) {
1709                         cmd_mfi = instance->cmd_list[i];
1710                         if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1711                                 MFI_CMD_ABORT)
1712                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1713                 }
1714         }
1715 }
1716
1717
1718 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1719 {
1720         /* Set critical error to block I/O & ioctls in case caller didn't */
1721         instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1722         /* Wait 1 second to ensure IO or ioctls in build have posted */
1723         msleep(1000);
1724         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1725                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1726                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1727                 (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
1728                 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1729                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1730                 writel(MFI_STOP_ADP,
1731                         &instance->reg_set->doorbell);
1732                 /* Flush */
1733                 readl(&instance->reg_set->doorbell);
1734                 if (instance->mpio && instance->requestorId)
1735                         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1736         } else {
1737                 writel(MFI_STOP_ADP,
1738                         &instance->reg_set->inbound_doorbell);
1739         }
1740         /* Complete outstanding ioctls when adapter is killed */
1741         megasas_complete_outstanding_ioctls(instance);
1742 }
1743
1744  /**
1745   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1746   *                                     restored to max value
1747   * @instance:                  Adapter soft state
1748   *
1749   */
1750 void
1751 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1752 {
1753         unsigned long flags;
1754
1755         if (instance->flag & MEGASAS_FW_BUSY
1756             && time_after(jiffies, instance->last_time + 5 * HZ)
1757             && atomic_read(&instance->fw_outstanding) <
1758             instance->throttlequeuedepth + 1) {
1759
1760                 spin_lock_irqsave(instance->host->host_lock, flags);
1761                 instance->flag &= ~MEGASAS_FW_BUSY;
1762
1763                 instance->host->can_queue = instance->max_scsi_cmds;
1764                 spin_unlock_irqrestore(instance->host->host_lock, flags);
1765         }
1766 }
1767
1768 /**
1769  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
1770  * @instance_addr:                      Address of adapter soft state
1771  *
1772  * Tasklet to complete cmds
1773  */
1774 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1775 {
1776         u32 producer;
1777         u32 consumer;
1778         u32 context;
1779         struct megasas_cmd *cmd;
1780         struct megasas_instance *instance =
1781                                 (struct megasas_instance *)instance_addr;
1782         unsigned long flags;
1783
1784         /* If we have already declared adapter dead, donot complete cmds */
1785         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1786                 return;
1787
1788         spin_lock_irqsave(&instance->completion_lock, flags);
1789
1790         producer = le32_to_cpu(*instance->producer);
1791         consumer = le32_to_cpu(*instance->consumer);
1792
1793         while (consumer != producer) {
1794                 context = le32_to_cpu(instance->reply_queue[consumer]);
1795                 if (context >= instance->max_fw_cmds) {
1796                         dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1797                                 context);
1798                         BUG();
1799                 }
1800
1801                 cmd = instance->cmd_list[context];
1802
1803                 megasas_complete_cmd(instance, cmd, DID_OK);
1804
1805                 consumer++;
1806                 if (consumer == (instance->max_fw_cmds + 1)) {
1807                         consumer = 0;
1808                 }
1809         }
1810
1811         *instance->consumer = cpu_to_le32(producer);
1812
1813         spin_unlock_irqrestore(&instance->completion_lock, flags);
1814
1815         /*
1816          * Check if we can restore can_queue
1817          */
1818         megasas_check_and_restore_queue_depth(instance);
1819 }
1820
1821 /**
1822  * megasas_start_timer - Initializes a timer object
1823  * @instance:           Adapter soft state
1824  * @timer:              timer object to be initialized
1825  * @fn:                 timer function
1826  * @interval:           time interval between timer function call
1827  *
1828  */
1829 void megasas_start_timer(struct megasas_instance *instance,
1830                         struct timer_list *timer,
1831                         void *fn, unsigned long interval)
1832 {
1833         init_timer(timer);
1834         timer->expires = jiffies + interval;
1835         timer->data = (unsigned long)instance;
1836         timer->function = fn;
1837         add_timer(timer);
1838 }
1839
1840 static void
1841 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1842
1843 static void
1844 process_fw_state_change_wq(struct work_struct *work);
1845
1846 void megasas_do_ocr(struct megasas_instance *instance)
1847 {
1848         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1849         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1850         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1851                 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1852         }
1853         instance->instancet->disable_intr(instance);
1854         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
1855         instance->issuepend_done = 0;
1856
1857         atomic_set(&instance->fw_outstanding, 0);
1858         megasas_internal_reset_defer_cmds(instance);
1859         process_fw_state_change_wq(&instance->work_init);
1860 }
1861
1862 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1863                                             int initial)
1864 {
1865         struct megasas_cmd *cmd;
1866         struct megasas_dcmd_frame *dcmd;
1867         struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1868         dma_addr_t new_affiliation_111_h;
1869         int ld, retval = 0;
1870         u8 thisVf;
1871
1872         cmd = megasas_get_cmd(instance);
1873
1874         if (!cmd) {
1875                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
1876                        "Failed to get cmd for scsi%d\n",
1877                         instance->host->host_no);
1878                 return -ENOMEM;
1879         }
1880
1881         dcmd = &cmd->frame->dcmd;
1882
1883         if (!instance->vf_affiliation_111) {
1884                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
1885                        "affiliation for scsi%d\n", instance->host->host_no);
1886                 megasas_return_cmd(instance, cmd);
1887                 return -ENOMEM;
1888         }
1889
1890         if (initial)
1891                         memset(instance->vf_affiliation_111, 0,
1892                                sizeof(struct MR_LD_VF_AFFILIATION_111));
1893         else {
1894                 new_affiliation_111 =
1895                         pci_alloc_consistent(instance->pdev,
1896                                              sizeof(struct MR_LD_VF_AFFILIATION_111),
1897                                              &new_affiliation_111_h);
1898                 if (!new_affiliation_111) {
1899                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
1900                                "memory for new affiliation for scsi%d\n",
1901                                instance->host->host_no);
1902                         megasas_return_cmd(instance, cmd);
1903                         return -ENOMEM;
1904                 }
1905                 memset(new_affiliation_111, 0,
1906                        sizeof(struct MR_LD_VF_AFFILIATION_111));
1907         }
1908
1909         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1910
1911         dcmd->cmd = MFI_CMD_DCMD;
1912         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
1913         dcmd->sge_count = 1;
1914         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
1915         dcmd->timeout = 0;
1916         dcmd->pad_0 = 0;
1917         dcmd->data_xfer_len =
1918                 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
1919         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
1920
1921         if (initial)
1922                 dcmd->sgl.sge32[0].phys_addr =
1923                         cpu_to_le32(instance->vf_affiliation_111_h);
1924         else
1925                 dcmd->sgl.sge32[0].phys_addr =
1926                         cpu_to_le32(new_affiliation_111_h);
1927
1928         dcmd->sgl.sge32[0].length = cpu_to_le32(
1929                 sizeof(struct MR_LD_VF_AFFILIATION_111));
1930
1931         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
1932                "scsi%d\n", instance->host->host_no);
1933
1934         megasas_issue_blocked_cmd(instance, cmd, 0);
1935
1936         if (dcmd->cmd_status) {
1937                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
1938                        " failed with status 0x%x for scsi%d\n",
1939                        dcmd->cmd_status, instance->host->host_no);
1940                 retval = 1; /* Do a scan if we couldn't get affiliation */
1941                 goto out;
1942         }
1943
1944         if (!initial) {
1945                 thisVf = new_affiliation_111->thisVf;
1946                 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
1947                         if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
1948                             new_affiliation_111->map[ld].policy[thisVf]) {
1949                                 dev_warn(&instance->pdev->dev, "SR-IOV: "
1950                                        "Got new LD/VF affiliation for scsi%d\n",
1951                                        instance->host->host_no);
1952                                 memcpy(instance->vf_affiliation_111,
1953                                        new_affiliation_111,
1954                                        sizeof(struct MR_LD_VF_AFFILIATION_111));
1955                                 retval = 1;
1956                                 goto out;
1957                         }
1958         }
1959 out:
1960         if (new_affiliation_111) {
1961                 pci_free_consistent(instance->pdev,
1962                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
1963                                     new_affiliation_111,
1964                                     new_affiliation_111_h);
1965         }
1966
1967         megasas_return_cmd(instance, cmd);
1968
1969         return retval;
1970 }
1971
1972 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
1973                                             int initial)
1974 {
1975         struct megasas_cmd *cmd;
1976         struct megasas_dcmd_frame *dcmd;
1977         struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
1978         struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
1979         dma_addr_t new_affiliation_h;
1980         int i, j, retval = 0, found = 0, doscan = 0;
1981         u8 thisVf;
1982
1983         cmd = megasas_get_cmd(instance);
1984
1985         if (!cmd) {
1986                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
1987                        "Failed to get cmd for scsi%d\n",
1988                        instance->host->host_no);
1989                 return -ENOMEM;
1990         }
1991
1992         dcmd = &cmd->frame->dcmd;
1993
1994         if (!instance->vf_affiliation) {
1995                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
1996                        "affiliation for scsi%d\n", instance->host->host_no);
1997                 megasas_return_cmd(instance, cmd);
1998                 return -ENOMEM;
1999         }
2000
2001         if (initial)
2002                 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2003                        sizeof(struct MR_LD_VF_AFFILIATION));
2004         else {
2005                 new_affiliation =
2006                         pci_alloc_consistent(instance->pdev,
2007                                              (MAX_LOGICAL_DRIVES + 1) *
2008                                              sizeof(struct MR_LD_VF_AFFILIATION),
2009                                              &new_affiliation_h);
2010                 if (!new_affiliation) {
2011                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2012                                "memory for new affiliation for scsi%d\n",
2013                                instance->host->host_no);
2014                         megasas_return_cmd(instance, cmd);
2015                         return -ENOMEM;
2016                 }
2017                 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2018                        sizeof(struct MR_LD_VF_AFFILIATION));
2019         }
2020
2021         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2022
2023         dcmd->cmd = MFI_CMD_DCMD;
2024         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2025         dcmd->sge_count = 1;
2026         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2027         dcmd->timeout = 0;
2028         dcmd->pad_0 = 0;
2029         dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2030                 sizeof(struct MR_LD_VF_AFFILIATION));
2031         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2032
2033         if (initial)
2034                 dcmd->sgl.sge32[0].phys_addr =
2035                         cpu_to_le32(instance->vf_affiliation_h);
2036         else
2037                 dcmd->sgl.sge32[0].phys_addr =
2038                         cpu_to_le32(new_affiliation_h);
2039
2040         dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2041                 sizeof(struct MR_LD_VF_AFFILIATION));
2042
2043         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2044                "scsi%d\n", instance->host->host_no);
2045
2046         megasas_issue_blocked_cmd(instance, cmd, 0);
2047
2048         if (dcmd->cmd_status) {
2049                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2050                        " failed with status 0x%x for scsi%d\n",
2051                        dcmd->cmd_status, instance->host->host_no);
2052                 retval = 1; /* Do a scan if we couldn't get affiliation */
2053                 goto out;
2054         }
2055
2056         if (!initial) {
2057                 if (!new_affiliation->ldCount) {
2058                         dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2059                                "affiliation for passive path for scsi%d\n",
2060                                instance->host->host_no);
2061                         retval = 1;
2062                         goto out;
2063                 }
2064                 newmap = new_affiliation->map;
2065                 savedmap = instance->vf_affiliation->map;
2066                 thisVf = new_affiliation->thisVf;
2067                 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2068                         found = 0;
2069                         for (j = 0; j < instance->vf_affiliation->ldCount;
2070                              j++) {
2071                                 if (newmap->ref.targetId ==
2072                                     savedmap->ref.targetId) {
2073                                         found = 1;
2074                                         if (newmap->policy[thisVf] !=
2075                                             savedmap->policy[thisVf]) {
2076                                                 doscan = 1;
2077                                                 goto out;
2078                                         }
2079                                 }
2080                                 savedmap = (struct MR_LD_VF_MAP *)
2081                                         ((unsigned char *)savedmap +
2082                                          savedmap->size);
2083                         }
2084                         if (!found && newmap->policy[thisVf] !=
2085                             MR_LD_ACCESS_HIDDEN) {
2086                                 doscan = 1;
2087                                 goto out;
2088                         }
2089                         newmap = (struct MR_LD_VF_MAP *)
2090                                 ((unsigned char *)newmap + newmap->size);
2091                 }
2092
2093                 newmap = new_affiliation->map;
2094                 savedmap = instance->vf_affiliation->map;
2095
2096                 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2097                         found = 0;
2098                         for (j = 0 ; j < new_affiliation->ldCount; j++) {
2099                                 if (savedmap->ref.targetId ==
2100                                     newmap->ref.targetId) {
2101                                         found = 1;
2102                                         if (savedmap->policy[thisVf] !=
2103                                             newmap->policy[thisVf]) {
2104                                                 doscan = 1;
2105                                                 goto out;
2106                                         }
2107                                 }
2108                                 newmap = (struct MR_LD_VF_MAP *)
2109                                         ((unsigned char *)newmap +
2110                                          newmap->size);
2111                         }
2112                         if (!found && savedmap->policy[thisVf] !=
2113                             MR_LD_ACCESS_HIDDEN) {
2114                                 doscan = 1;
2115                                 goto out;
2116                         }
2117                         savedmap = (struct MR_LD_VF_MAP *)
2118                                 ((unsigned char *)savedmap +
2119                                  savedmap->size);
2120                 }
2121         }
2122 out:
2123         if (doscan) {
2124                 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2125                        "affiliation for scsi%d\n", instance->host->host_no);
2126                 memcpy(instance->vf_affiliation, new_affiliation,
2127                        new_affiliation->size);
2128                 retval = 1;
2129         }
2130
2131         if (new_affiliation)
2132                 pci_free_consistent(instance->pdev,
2133                                     (MAX_LOGICAL_DRIVES + 1) *
2134                                     sizeof(struct MR_LD_VF_AFFILIATION),
2135                                     new_affiliation, new_affiliation_h);
2136         megasas_return_cmd(instance, cmd);
2137
2138         return retval;
2139 }
2140
2141 /* This function will get the current SR-IOV LD/VF affiliation */
2142 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2143         int initial)
2144 {
2145         int retval;
2146
2147         if (instance->PlasmaFW111)
2148                 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2149         else
2150                 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2151         return retval;
2152 }
2153
2154 /* This function will tell FW to start the SR-IOV heartbeat */
2155 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2156                                          int initial)
2157 {
2158         struct megasas_cmd *cmd;
2159         struct megasas_dcmd_frame *dcmd;
2160         int retval = 0;
2161
2162         cmd = megasas_get_cmd(instance);
2163
2164         if (!cmd) {
2165                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2166                        "Failed to get cmd for scsi%d\n",
2167                        instance->host->host_no);
2168                 return -ENOMEM;
2169         }
2170
2171         dcmd = &cmd->frame->dcmd;
2172
2173         if (initial) {
2174                 instance->hb_host_mem =
2175                         pci_zalloc_consistent(instance->pdev,
2176                                               sizeof(struct MR_CTRL_HB_HOST_MEM),
2177                                               &instance->hb_host_mem_h);
2178                 if (!instance->hb_host_mem) {
2179                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2180                                " memory for heartbeat host memory for scsi%d\n",
2181                                instance->host->host_no);
2182                         retval = -ENOMEM;
2183                         goto out;
2184                 }
2185         }
2186
2187         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2188
2189         dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2190         dcmd->cmd = MFI_CMD_DCMD;
2191         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2192         dcmd->sge_count = 1;
2193         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2194         dcmd->timeout = 0;
2195         dcmd->pad_0 = 0;
2196         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2197         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2198         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2199         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2200
2201         dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2202                instance->host->host_no);
2203
2204         if (instance->ctrl_context && !instance->mask_interrupts)
2205                 retval = megasas_issue_blocked_cmd(instance, cmd,
2206                         MEGASAS_ROUTINE_WAIT_TIME_VF);
2207         else
2208                 retval = megasas_issue_polled(instance, cmd);
2209
2210         if (retval) {
2211                 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2212                         "_MEM_ALLOC DCMD %s for scsi%d\n",
2213                         (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2214                         "timed out" : "failed", instance->host->host_no);
2215                 retval = 1;
2216         }
2217
2218 out:
2219         megasas_return_cmd(instance, cmd);
2220
2221         return retval;
2222 }
2223
2224 /* Handler for SR-IOV heartbeat */
2225 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2226 {
2227         struct megasas_instance *instance =
2228                 (struct megasas_instance *)instance_addr;
2229
2230         if (instance->hb_host_mem->HB.fwCounter !=
2231             instance->hb_host_mem->HB.driverCounter) {
2232                 instance->hb_host_mem->HB.driverCounter =
2233                         instance->hb_host_mem->HB.fwCounter;
2234                 mod_timer(&instance->sriov_heartbeat_timer,
2235                           jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2236         } else {
2237                 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2238                        "completed for scsi%d\n", instance->host->host_no);
2239                 schedule_work(&instance->work_init);
2240         }
2241 }
2242
2243 /**
2244  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2245  * @instance:                           Adapter soft state
2246  *
2247  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2248  * complete all its outstanding commands. Returns error if one or more IOs
2249  * are pending after this time period. It also marks the controller dead.
2250  */
2251 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2252 {
2253         int i;
2254         u32 reset_index;
2255         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2256         u8 adprecovery;
2257         unsigned long flags;
2258         struct list_head clist_local;
2259         struct megasas_cmd *reset_cmd;
2260         u32 fw_state;
2261         u8 kill_adapter_flag;
2262
2263         spin_lock_irqsave(&instance->hba_lock, flags);
2264         adprecovery = instance->adprecovery;
2265         spin_unlock_irqrestore(&instance->hba_lock, flags);
2266
2267         if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2268
2269                 INIT_LIST_HEAD(&clist_local);
2270                 spin_lock_irqsave(&instance->hba_lock, flags);
2271                 list_splice_init(&instance->internal_reset_pending_q,
2272                                 &clist_local);
2273                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2274
2275                 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2276                 for (i = 0; i < wait_time; i++) {
2277                         msleep(1000);
2278                         spin_lock_irqsave(&instance->hba_lock, flags);
2279                         adprecovery = instance->adprecovery;
2280                         spin_unlock_irqrestore(&instance->hba_lock, flags);
2281                         if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2282                                 break;
2283                 }
2284
2285                 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2286                         dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2287                         spin_lock_irqsave(&instance->hba_lock, flags);
2288                         instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
2289                         spin_unlock_irqrestore(&instance->hba_lock, flags);
2290                         return FAILED;
2291                 }
2292
2293                 reset_index     = 0;
2294                 while (!list_empty(&clist_local)) {
2295                         reset_cmd       = list_entry((&clist_local)->next,
2296                                                 struct megasas_cmd, list);
2297                         list_del_init(&reset_cmd->list);
2298                         if (reset_cmd->scmd) {
2299                                 reset_cmd->scmd->result = DID_RESET << 16;
2300                                 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2301                                         reset_index, reset_cmd,
2302                                         reset_cmd->scmd->cmnd[0]);
2303
2304                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2305                                 megasas_return_cmd(instance, reset_cmd);
2306                         } else if (reset_cmd->sync_cmd) {
2307                                 dev_notice(&instance->pdev->dev, "%p synch cmds"
2308                                                 "reset queue\n",
2309                                                 reset_cmd);
2310
2311                                 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2312                                 instance->instancet->fire_cmd(instance,
2313                                                 reset_cmd->frame_phys_addr,
2314                                                 0, instance->reg_set);
2315                         } else {
2316                                 dev_notice(&instance->pdev->dev, "%p unexpected"
2317                                         "cmds lst\n",
2318                                         reset_cmd);
2319                         }
2320                         reset_index++;
2321                 }
2322
2323                 return SUCCESS;
2324         }
2325
2326         for (i = 0; i < resetwaittime; i++) {
2327
2328                 int outstanding = atomic_read(&instance->fw_outstanding);
2329
2330                 if (!outstanding)
2331                         break;
2332
2333                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2334                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2335                                "commands to complete\n",i,outstanding);
2336                         /*
2337                          * Call cmd completion routine. Cmd to be
2338                          * be completed directly without depending on isr.
2339                          */
2340                         megasas_complete_cmd_dpc((unsigned long)instance);
2341                 }
2342
2343                 msleep(1000);
2344         }
2345
2346         i = 0;
2347         kill_adapter_flag = 0;
2348         do {
2349                 fw_state = instance->instancet->read_fw_status_reg(
2350                                         instance->reg_set) & MFI_STATE_MASK;
2351                 if ((fw_state == MFI_STATE_FAULT) &&
2352                         (instance->disableOnlineCtrlReset == 0)) {
2353                         if (i == 3) {
2354                                 kill_adapter_flag = 2;
2355                                 break;
2356                         }
2357                         megasas_do_ocr(instance);
2358                         kill_adapter_flag = 1;
2359
2360                         /* wait for 1 secs to let FW finish the pending cmds */
2361                         msleep(1000);
2362                 }
2363                 i++;
2364         } while (i <= 3);
2365
2366         if (atomic_read(&instance->fw_outstanding) &&
2367                                         !kill_adapter_flag) {
2368                 if (instance->disableOnlineCtrlReset == 0) {
2369
2370                         megasas_do_ocr(instance);
2371
2372                         /* wait for 5 secs to let FW finish the pending cmds */
2373                         for (i = 0; i < wait_time; i++) {
2374                                 int outstanding =
2375                                         atomic_read(&instance->fw_outstanding);
2376                                 if (!outstanding)
2377                                         return SUCCESS;
2378                                 msleep(1000);
2379                         }
2380                 }
2381         }
2382
2383         if (atomic_read(&instance->fw_outstanding) ||
2384                                         (kill_adapter_flag == 2)) {
2385                 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2386                 /*
2387                 * Send signal to FW to stop processing any pending cmds.
2388                 * The controller will be taken offline by the OS now.
2389                 */
2390                 if ((instance->pdev->device ==
2391                         PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2392                         (instance->pdev->device ==
2393                         PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2394                         writel(MFI_STOP_ADP,
2395                                 &instance->reg_set->doorbell);
2396                 } else {
2397                         writel(MFI_STOP_ADP,
2398                                 &instance->reg_set->inbound_doorbell);
2399                 }
2400                 megasas_dump_pending_frames(instance);
2401                 spin_lock_irqsave(&instance->hba_lock, flags);
2402                 instance->adprecovery   = MEGASAS_HW_CRITICAL_ERROR;
2403                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2404                 return FAILED;
2405         }
2406
2407         dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2408
2409         return SUCCESS;
2410 }
2411
2412 /**
2413  * megasas_generic_reset -      Generic reset routine
2414  * @scmd:                       Mid-layer SCSI command
2415  *
2416  * This routine implements a generic reset handler for device, bus and host
2417  * reset requests. Device, bus and host specific reset handlers can use this
2418  * function after they do their specific tasks.
2419  */
2420 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2421 {
2422         int ret_val;
2423         struct megasas_instance *instance;
2424
2425         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2426
2427         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2428                  scmd->cmnd[0], scmd->retries);
2429
2430         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2431                 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2432                 return FAILED;
2433         }
2434
2435         ret_val = megasas_wait_for_outstanding(instance);
2436         if (ret_val == SUCCESS)
2437                 dev_notice(&instance->pdev->dev, "reset successful\n");
2438         else
2439                 dev_err(&instance->pdev->dev, "failed to do reset\n");
2440
2441         return ret_val;
2442 }
2443
2444 /**
2445  * megasas_reset_timer - quiesce the adapter if required
2446  * @scmd:               scsi cmnd
2447  *
2448  * Sets the FW busy flag and reduces the host->can_queue if the
2449  * cmd has not been completed within the timeout period.
2450  */
2451 static enum
2452 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2453 {
2454         struct megasas_instance *instance;
2455         unsigned long flags;
2456
2457         if (time_after(jiffies, scmd->jiffies_at_alloc +
2458                                 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2459                 return BLK_EH_NOT_HANDLED;
2460         }
2461
2462         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2463         if (!(instance->flag & MEGASAS_FW_BUSY)) {
2464                 /* FW is busy, throttle IO */
2465                 spin_lock_irqsave(instance->host->host_lock, flags);
2466
2467                 instance->host->can_queue = instance->throttlequeuedepth;
2468                 instance->last_time = jiffies;
2469                 instance->flag |= MEGASAS_FW_BUSY;
2470
2471                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2472         }
2473         return BLK_EH_RESET_TIMER;
2474 }
2475
2476 /**
2477  * megasas_reset_device -       Device reset handler entry point
2478  */
2479 static int megasas_reset_device(struct scsi_cmnd *scmd)
2480 {
2481         int ret;
2482
2483         /*
2484          * First wait for all commands to complete
2485          */
2486         ret = megasas_generic_reset(scmd);
2487
2488         return ret;
2489 }
2490
2491 /**
2492  * megasas_reset_bus_host -     Bus & host reset handler entry point
2493  */
2494 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2495 {
2496         int ret;
2497         struct megasas_instance *instance;
2498         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2499
2500         /*
2501          * First wait for all commands to complete
2502          */
2503         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
2504             (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
2505             (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2506             (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2507                 ret = megasas_reset_fusion(scmd->device->host, 1);
2508         else
2509                 ret = megasas_generic_reset(scmd);
2510
2511         return ret;
2512 }
2513
2514 /**
2515  * megasas_bios_param - Returns disk geometry for a disk
2516  * @sdev:               device handle
2517  * @bdev:               block device
2518  * @capacity:           drive capacity
2519  * @geom:               geometry parameters
2520  */
2521 static int
2522 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2523                  sector_t capacity, int geom[])
2524 {
2525         int heads;
2526         int sectors;
2527         sector_t cylinders;
2528         unsigned long tmp;
2529         /* Default heads (64) & sectors (32) */
2530         heads = 64;
2531         sectors = 32;
2532
2533         tmp = heads * sectors;
2534         cylinders = capacity;
2535
2536         sector_div(cylinders, tmp);
2537
2538         /*
2539          * Handle extended translation size for logical drives > 1Gb
2540          */
2541
2542         if (capacity >= 0x200000) {
2543                 heads = 255;
2544                 sectors = 63;
2545                 tmp = heads*sectors;
2546                 cylinders = capacity;
2547                 sector_div(cylinders, tmp);
2548         }
2549
2550         geom[0] = heads;
2551         geom[1] = sectors;
2552         geom[2] = cylinders;
2553
2554         return 0;
2555 }
2556
2557 static void megasas_aen_polling(struct work_struct *work);
2558
2559 /**
2560  * megasas_service_aen -        Processes an event notification
2561  * @instance:                   Adapter soft state
2562  * @cmd:                        AEN command completed by the ISR
2563  *
2564  * For AEN, driver sends a command down to FW that is held by the FW till an
2565  * event occurs. When an event of interest occurs, FW completes the command
2566  * that it was previously holding.
2567  *
2568  * This routines sends SIGIO signal to processes that have registered with the
2569  * driver for AEN.
2570  */
2571 static void
2572 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2573 {
2574         unsigned long flags;
2575         /*
2576          * Don't signal app if it is just an aborted previously registered aen
2577          */
2578         if ((!cmd->abort_aen) && (instance->unload == 0)) {
2579                 spin_lock_irqsave(&poll_aen_lock, flags);
2580                 megasas_poll_wait_aen = 1;
2581                 spin_unlock_irqrestore(&poll_aen_lock, flags);
2582                 wake_up(&megasas_poll_wait);
2583                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2584         }
2585         else
2586                 cmd->abort_aen = 0;
2587
2588         instance->aen_cmd = NULL;
2589
2590         megasas_return_cmd(instance, cmd);
2591
2592         if ((instance->unload == 0) &&
2593                 ((instance->issuepend_done == 1))) {
2594                 struct megasas_aen_event *ev;
2595                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2596                 if (!ev) {
2597                         dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2598                 } else {
2599                         ev->instance = instance;
2600                         instance->ev = ev;
2601                         INIT_DELAYED_WORK(&ev->hotplug_work,
2602                                           megasas_aen_polling);
2603                         schedule_delayed_work(&ev->hotplug_work, 0);
2604                 }
2605         }
2606 }
2607
2608 static ssize_t
2609 megasas_fw_crash_buffer_store(struct device *cdev,
2610         struct device_attribute *attr, const char *buf, size_t count)
2611 {
2612         struct Scsi_Host *shost = class_to_shost(cdev);
2613         struct megasas_instance *instance =
2614                 (struct megasas_instance *) shost->hostdata;
2615         int val = 0;
2616         unsigned long flags;
2617
2618         if (kstrtoint(buf, 0, &val) != 0)
2619                 return -EINVAL;
2620
2621         spin_lock_irqsave(&instance->crashdump_lock, flags);
2622         instance->fw_crash_buffer_offset = val;
2623         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2624         return strlen(buf);
2625 }
2626
2627 static ssize_t
2628 megasas_fw_crash_buffer_show(struct device *cdev,
2629         struct device_attribute *attr, char *buf)
2630 {
2631         struct Scsi_Host *shost = class_to_shost(cdev);
2632         struct megasas_instance *instance =
2633                 (struct megasas_instance *) shost->hostdata;
2634         u32 size;
2635         unsigned long buff_addr;
2636         unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2637         unsigned long src_addr;
2638         unsigned long flags;
2639         u32 buff_offset;
2640
2641         spin_lock_irqsave(&instance->crashdump_lock, flags);
2642         buff_offset = instance->fw_crash_buffer_offset;
2643         if (!instance->crash_dump_buf &&
2644                 !((instance->fw_crash_state == AVAILABLE) ||
2645                 (instance->fw_crash_state == COPYING))) {
2646                 dev_err(&instance->pdev->dev,
2647                         "Firmware crash dump is not available\n");
2648                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2649                 return -EINVAL;
2650         }
2651
2652         buff_addr = (unsigned long) buf;
2653
2654         if (buff_offset >
2655                 (instance->fw_crash_buffer_size * dmachunk)) {
2656                 dev_err(&instance->pdev->dev,
2657                         "Firmware crash dump offset is out of range\n");
2658                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2659                 return 0;
2660         }
2661
2662         size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2663         size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2664
2665         src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2666                 (buff_offset % dmachunk);
2667         memcpy(buf, (void *)src_addr,  size);
2668         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2669
2670         return size;
2671 }
2672
2673 static ssize_t
2674 megasas_fw_crash_buffer_size_show(struct device *cdev,
2675         struct device_attribute *attr, char *buf)
2676 {
2677         struct Scsi_Host *shost = class_to_shost(cdev);
2678         struct megasas_instance *instance =
2679                 (struct megasas_instance *) shost->hostdata;
2680
2681         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2682                 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2683 }
2684
2685 static ssize_t
2686 megasas_fw_crash_state_store(struct device *cdev,
2687         struct device_attribute *attr, const char *buf, size_t count)
2688 {
2689         struct Scsi_Host *shost = class_to_shost(cdev);
2690         struct megasas_instance *instance =
2691                 (struct megasas_instance *) shost->hostdata;
2692         int val = 0;
2693         unsigned long flags;
2694
2695         if (kstrtoint(buf, 0, &val) != 0)
2696                 return -EINVAL;
2697
2698         if ((val <= AVAILABLE || val > COPY_ERROR)) {
2699                 dev_err(&instance->pdev->dev, "application updates invalid "
2700                         "firmware crash state\n");
2701                 return -EINVAL;
2702         }
2703
2704         instance->fw_crash_state = val;
2705
2706         if ((val == COPIED) || (val == COPY_ERROR)) {
2707                 spin_lock_irqsave(&instance->crashdump_lock, flags);
2708                 megasas_free_host_crash_buffer(instance);
2709                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2710                 if (val == COPY_ERROR)
2711                         dev_info(&instance->pdev->dev, "application failed to "
2712                                 "copy Firmware crash dump\n");
2713                 else
2714                         dev_info(&instance->pdev->dev, "Firmware crash dump "
2715                                 "copied successfully\n");
2716         }
2717         return strlen(buf);
2718 }
2719
2720 static ssize_t
2721 megasas_fw_crash_state_show(struct device *cdev,
2722         struct device_attribute *attr, char *buf)
2723 {
2724         struct Scsi_Host *shost = class_to_shost(cdev);
2725         struct megasas_instance *instance =
2726                 (struct megasas_instance *) shost->hostdata;
2727         return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2728 }
2729
2730 static ssize_t
2731 megasas_page_size_show(struct device *cdev,
2732         struct device_attribute *attr, char *buf)
2733 {
2734         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2735 }
2736
2737 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2738         megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2739 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2740         megasas_fw_crash_buffer_size_show, NULL);
2741 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2742         megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2743 static DEVICE_ATTR(page_size, S_IRUGO,
2744         megasas_page_size_show, NULL);
2745
2746 struct device_attribute *megaraid_host_attrs[] = {
2747         &dev_attr_fw_crash_buffer_size,
2748         &dev_attr_fw_crash_buffer,
2749         &dev_attr_fw_crash_state,
2750         &dev_attr_page_size,
2751         NULL,
2752 };
2753
2754 /*
2755  * Scsi host template for megaraid_sas driver
2756  */
2757 static struct scsi_host_template megasas_template = {
2758
2759         .module = THIS_MODULE,
2760         .name = "Avago SAS based MegaRAID driver",
2761         .proc_name = "megaraid_sas",
2762         .slave_configure = megasas_slave_configure,
2763         .slave_alloc = megasas_slave_alloc,
2764         .queuecommand = megasas_queue_command,
2765         .eh_device_reset_handler = megasas_reset_device,
2766         .eh_bus_reset_handler = megasas_reset_bus_host,
2767         .eh_host_reset_handler = megasas_reset_bus_host,
2768         .eh_timed_out = megasas_reset_timer,
2769         .shost_attrs = megaraid_host_attrs,
2770         .bios_param = megasas_bios_param,
2771         .use_clustering = ENABLE_CLUSTERING,
2772         .change_queue_depth = scsi_change_queue_depth,
2773         .no_write_same = 1,
2774 };
2775
2776 /**
2777  * megasas_complete_int_cmd -   Completes an internal command
2778  * @instance:                   Adapter soft state
2779  * @cmd:                        Command to be completed
2780  *
2781  * The megasas_issue_blocked_cmd() function waits for a command to complete
2782  * after it issues a command. This function wakes up that waiting routine by
2783  * calling wake_up() on the wait queue.
2784  */
2785 static void
2786 megasas_complete_int_cmd(struct megasas_instance *instance,
2787                          struct megasas_cmd *cmd)
2788 {
2789         cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2790         wake_up(&instance->int_cmd_wait_q);
2791 }
2792
2793 /**
2794  * megasas_complete_abort -     Completes aborting a command
2795  * @instance:                   Adapter soft state
2796  * @cmd:                        Cmd that was issued to abort another cmd
2797  *
2798  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2799  * after it issues an abort on a previously issued command. This function
2800  * wakes up all functions waiting on the same wait queue.
2801  */
2802 static void
2803 megasas_complete_abort(struct megasas_instance *instance,
2804                        struct megasas_cmd *cmd)
2805 {
2806         if (cmd->sync_cmd) {
2807                 cmd->sync_cmd = 0;
2808                 cmd->cmd_status_drv = 0;
2809                 wake_up(&instance->abort_cmd_wait_q);
2810         }
2811
2812         return;
2813 }
2814
2815 /**
2816  * megasas_complete_cmd -       Completes a command
2817  * @instance:                   Adapter soft state
2818  * @cmd:                        Command to be completed
2819  * @alt_status:                 If non-zero, use this value as status to
2820  *                              SCSI mid-layer instead of the value returned
2821  *                              by the FW. This should be used if caller wants
2822  *                              an alternate status (as in the case of aborted
2823  *                              commands)
2824  */
2825 void
2826 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2827                      u8 alt_status)
2828 {
2829         int exception = 0;
2830         struct megasas_header *hdr = &cmd->frame->hdr;
2831         unsigned long flags;
2832         struct fusion_context *fusion = instance->ctrl_context;
2833         u32 opcode;
2834
2835         /* flag for the retry reset */
2836         cmd->retry_for_fw_reset = 0;
2837
2838         if (cmd->scmd)
2839                 cmd->scmd->SCp.ptr = NULL;
2840
2841         switch (hdr->cmd) {
2842         case MFI_CMD_INVALID:
2843                 /* Some older 1068 controller FW may keep a pended
2844                    MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2845                    when booting the kdump kernel.  Ignore this command to
2846                    prevent a kernel panic on shutdown of the kdump kernel. */
2847                 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2848                        "completed\n");
2849                 dev_warn(&instance->pdev->dev, "If you have a controller "
2850                        "other than PERC5, please upgrade your firmware\n");
2851                 break;
2852         case MFI_CMD_PD_SCSI_IO:
2853         case MFI_CMD_LD_SCSI_IO:
2854
2855                 /*
2856                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2857                  * issued either through an IO path or an IOCTL path. If it
2858                  * was via IOCTL, we will send it to internal completion.
2859                  */
2860                 if (cmd->sync_cmd) {
2861                         cmd->sync_cmd = 0;
2862                         megasas_complete_int_cmd(instance, cmd);
2863                         break;
2864                 }
2865
2866         case MFI_CMD_LD_READ:
2867         case MFI_CMD_LD_WRITE:
2868
2869                 if (alt_status) {
2870                         cmd->scmd->result = alt_status << 16;
2871                         exception = 1;
2872                 }
2873
2874                 if (exception) {
2875
2876                         atomic_dec(&instance->fw_outstanding);
2877
2878                         scsi_dma_unmap(cmd->scmd);
2879                         cmd->scmd->scsi_done(cmd->scmd);
2880                         megasas_return_cmd(instance, cmd);
2881
2882                         break;
2883                 }
2884
2885                 switch (hdr->cmd_status) {
2886
2887                 case MFI_STAT_OK:
2888                         cmd->scmd->result = DID_OK << 16;
2889                         break;
2890
2891                 case MFI_STAT_SCSI_IO_FAILED:
2892                 case MFI_STAT_LD_INIT_IN_PROGRESS:
2893                         cmd->scmd->result =
2894                             (DID_ERROR << 16) | hdr->scsi_status;
2895                         break;
2896
2897                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2898
2899                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2900
2901                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2902                                 memset(cmd->scmd->sense_buffer, 0,
2903                                        SCSI_SENSE_BUFFERSIZE);
2904                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2905                                        hdr->sense_len);
2906
2907                                 cmd->scmd->result |= DRIVER_SENSE << 24;
2908                         }
2909
2910                         break;
2911
2912                 case MFI_STAT_LD_OFFLINE:
2913                 case MFI_STAT_DEVICE_NOT_FOUND:
2914                         cmd->scmd->result = DID_BAD_TARGET << 16;
2915                         break;
2916
2917                 default:
2918                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
2919                                hdr->cmd_status);
2920                         cmd->scmd->result = DID_ERROR << 16;
2921                         break;
2922                 }
2923
2924                 atomic_dec(&instance->fw_outstanding);
2925
2926                 scsi_dma_unmap(cmd->scmd);
2927                 cmd->scmd->scsi_done(cmd->scmd);
2928                 megasas_return_cmd(instance, cmd);
2929
2930                 break;
2931
2932         case MFI_CMD_SMP:
2933         case MFI_CMD_STP:
2934         case MFI_CMD_DCMD:
2935                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
2936                 /* Check for LD map update */
2937                 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2938                         && (cmd->frame->dcmd.mbox.b[1] == 1)) {
2939                         fusion->fast_path_io = 0;
2940                         spin_lock_irqsave(instance->host->host_lock, flags);
2941                         if (cmd->frame->hdr.cmd_status != 0) {
2942                                 if (cmd->frame->hdr.cmd_status !=
2943                                     MFI_STAT_NOT_FOUND)
2944                                         dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
2945                                                cmd->frame->hdr.cmd_status);
2946                                 else {
2947                                         megasas_return_cmd(instance, cmd);
2948                                         spin_unlock_irqrestore(
2949                                                 instance->host->host_lock,
2950                                                 flags);
2951                                         break;
2952                                 }
2953                         } else
2954                                 instance->map_id++;
2955                         megasas_return_cmd(instance, cmd);
2956
2957                         /*
2958                          * Set fast path IO to ZERO.
2959                          * Validate Map will set proper value.
2960                          * Meanwhile all IOs will go as LD IO.
2961                          */
2962                         if (MR_ValidateMapInfo(instance))
2963                                 fusion->fast_path_io = 1;
2964                         else
2965                                 fusion->fast_path_io = 0;
2966                         megasas_sync_map_info(instance);
2967                         spin_unlock_irqrestore(instance->host->host_lock,
2968                                                flags);
2969                         break;
2970                 }
2971                 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2972                     opcode == MR_DCMD_CTRL_EVENT_GET) {
2973                         spin_lock_irqsave(&poll_aen_lock, flags);
2974                         megasas_poll_wait_aen = 0;
2975                         spin_unlock_irqrestore(&poll_aen_lock, flags);
2976                 }
2977
2978                 /*
2979                  * See if got an event notification
2980                  */
2981                 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
2982                         megasas_service_aen(instance, cmd);
2983                 else
2984                         megasas_complete_int_cmd(instance, cmd);
2985
2986                 break;
2987
2988         case MFI_CMD_ABORT:
2989                 /*
2990                  * Cmd issued to abort another cmd returned
2991                  */
2992                 megasas_complete_abort(instance, cmd);
2993                 break;
2994
2995         default:
2996                 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
2997                        hdr->cmd);
2998                 break;
2999         }
3000 }
3001
3002 /**
3003  * megasas_issue_pending_cmds_again -   issue all pending cmds
3004  *                                      in FW again because of the fw reset
3005  * @instance:                           Adapter soft state
3006  */
3007 static inline void
3008 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3009 {
3010         struct megasas_cmd *cmd;
3011         struct list_head clist_local;
3012         union megasas_evt_class_locale class_locale;
3013         unsigned long flags;
3014         u32 seq_num;
3015
3016         INIT_LIST_HEAD(&clist_local);
3017         spin_lock_irqsave(&instance->hba_lock, flags);
3018         list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3019         spin_unlock_irqrestore(&instance->hba_lock, flags);
3020
3021         while (!list_empty(&clist_local)) {
3022                 cmd     = list_entry((&clist_local)->next,
3023                                         struct megasas_cmd, list);
3024                 list_del_init(&cmd->list);
3025
3026                 if (cmd->sync_cmd || cmd->scmd) {
3027                         dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3028                                 "detected to be pending while HBA reset\n",
3029                                         cmd, cmd->scmd, cmd->sync_cmd);
3030
3031                         cmd->retry_for_fw_reset++;
3032
3033                         if (cmd->retry_for_fw_reset == 3) {
3034                                 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3035                                         "was tried multiple times during reset."
3036                                         "Shutting down the HBA\n",
3037                                         cmd, cmd->scmd, cmd->sync_cmd);
3038                                 instance->instancet->disable_intr(instance);
3039                                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3040                                 megaraid_sas_kill_hba(instance);
3041                                 return;
3042                         }
3043                 }
3044
3045                 if (cmd->sync_cmd == 1) {
3046                         if (cmd->scmd) {
3047                                 dev_notice(&instance->pdev->dev, "unexpected"
3048                                         "cmd attached to internal command!\n");
3049                         }
3050                         dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3051                                                 "on the internal reset queue,"
3052                                                 "issue it again.\n", cmd);
3053                         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3054                         instance->instancet->fire_cmd(instance,
3055                                                         cmd->frame_phys_addr ,
3056                                                         0, instance->reg_set);
3057                 } else if (cmd->scmd) {
3058                         dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3059                         "detected on the internal queue, issue again.\n",
3060                         cmd, cmd->scmd->cmnd[0]);
3061
3062                         atomic_inc(&instance->fw_outstanding);
3063                         instance->instancet->fire_cmd(instance,
3064                                         cmd->frame_phys_addr,
3065                                         cmd->frame_count-1, instance->reg_set);
3066                 } else {
3067                         dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3068                                 "internal reset defer list while re-issue!!\n",
3069                                 cmd);
3070                 }
3071         }
3072
3073         if (instance->aen_cmd) {
3074                 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3075                 megasas_return_cmd(instance, instance->aen_cmd);
3076
3077                 instance->aen_cmd       = NULL;
3078         }
3079
3080         /*
3081         * Initiate AEN (Asynchronous Event Notification)
3082         */
3083         seq_num = instance->last_seq_num;
3084         class_locale.members.reserved = 0;
3085         class_locale.members.locale = MR_EVT_LOCALE_ALL;
3086         class_locale.members.class = MR_EVT_CLASS_DEBUG;
3087
3088         megasas_register_aen(instance, seq_num, class_locale.word);
3089 }
3090
3091 /**
3092  * Move the internal reset pending commands to a deferred queue.
3093  *
3094  * We move the commands pending at internal reset time to a
3095  * pending queue. This queue would be flushed after successful
3096  * completion of the internal reset sequence. if the internal reset
3097  * did not complete in time, the kernel reset handler would flush
3098  * these commands.
3099  **/
3100 static void
3101 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3102 {
3103         struct megasas_cmd *cmd;
3104         int i;
3105         u32 max_cmd = instance->max_fw_cmds;
3106         u32 defer_index;
3107         unsigned long flags;
3108
3109         defer_index     = 0;
3110         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3111         for (i = 0; i < max_cmd; i++) {
3112                 cmd = instance->cmd_list[i];
3113                 if (cmd->sync_cmd == 1 || cmd->scmd) {
3114                         dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3115                                         "on the defer queue as internal\n",
3116                                 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3117
3118                         if (!list_empty(&cmd->list)) {
3119                                 dev_notice(&instance->pdev->dev, "ERROR while"
3120                                         " moving this cmd:%p, %d %p, it was"
3121                                         "discovered on some list?\n",
3122                                         cmd, cmd->sync_cmd, cmd->scmd);
3123
3124                                 list_del_init(&cmd->list);
3125                         }
3126                         defer_index++;
3127                         list_add_tail(&cmd->list,
3128                                 &instance->internal_reset_pending_q);
3129                 }
3130         }
3131         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3132 }
3133
3134
3135 static void
3136 process_fw_state_change_wq(struct work_struct *work)
3137 {
3138         struct megasas_instance *instance =
3139                 container_of(work, struct megasas_instance, work_init);
3140         u32 wait;
3141         unsigned long flags;
3142
3143         if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3144                 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3145                                 instance->adprecovery);
3146                 return ;
3147         }
3148
3149         if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3150                 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3151                                         "state, restarting it...\n");
3152
3153                 instance->instancet->disable_intr(instance);
3154                 atomic_set(&instance->fw_outstanding, 0);
3155
3156                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3157                 instance->instancet->adp_reset(instance, instance->reg_set);
3158                 atomic_set(&instance->fw_reset_no_pci_access, 0 );
3159
3160                 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3161                                         "initiating next stage...\n");
3162
3163                 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3164                                         "state 2 starting...\n");
3165
3166                 /*waitting for about 20 second before start the second init*/
3167                 for (wait = 0; wait < 30; wait++) {
3168                         msleep(1000);
3169                 }
3170
3171                 if (megasas_transition_to_ready(instance, 1)) {
3172                         dev_notice(&instance->pdev->dev, "adapter not ready\n");
3173
3174                         atomic_set(&instance->fw_reset_no_pci_access, 1);
3175                         megaraid_sas_kill_hba(instance);
3176                         return ;
3177                 }
3178
3179                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3180                         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3181                         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3182                         ) {
3183                         *instance->consumer = *instance->producer;
3184                 } else {
3185                         *instance->consumer = 0;
3186                         *instance->producer = 0;
3187                 }
3188
3189                 megasas_issue_init_mfi(instance);
3190
3191                 spin_lock_irqsave(&instance->hba_lock, flags);
3192                 instance->adprecovery   = MEGASAS_HBA_OPERATIONAL;
3193                 spin_unlock_irqrestore(&instance->hba_lock, flags);
3194                 instance->instancet->enable_intr(instance);
3195
3196                 megasas_issue_pending_cmds_again(instance);
3197                 instance->issuepend_done = 1;
3198         }
3199         return ;
3200 }
3201
3202 /**
3203  * megasas_deplete_reply_queue -        Processes all completed commands
3204  * @instance:                           Adapter soft state
3205  * @alt_status:                         Alternate status to be returned to
3206  *                                      SCSI mid-layer instead of the status
3207  *                                      returned by the FW
3208  * Note: this must be called with hba lock held
3209  */
3210 static int
3211 megasas_deplete_reply_queue(struct megasas_instance *instance,
3212                                         u8 alt_status)
3213 {
3214         u32 mfiStatus;
3215         u32 fw_state;
3216
3217         if ((mfiStatus = instance->instancet->check_reset(instance,
3218                                         instance->reg_set)) == 1) {
3219                 return IRQ_HANDLED;
3220         }
3221
3222         if ((mfiStatus = instance->instancet->clear_intr(
3223                                                 instance->reg_set)
3224                                                 ) == 0) {
3225                 /* Hardware may not set outbound_intr_status in MSI-X mode */
3226                 if (!instance->msix_vectors)
3227                         return IRQ_NONE;
3228         }
3229
3230         instance->mfiStatus = mfiStatus;
3231
3232         if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3233                 fw_state = instance->instancet->read_fw_status_reg(
3234                                 instance->reg_set) & MFI_STATE_MASK;
3235
3236                 if (fw_state != MFI_STATE_FAULT) {
3237                         dev_notice(&instance->pdev->dev, "fw state:%x\n",
3238                                                 fw_state);
3239                 }
3240
3241                 if ((fw_state == MFI_STATE_FAULT) &&
3242                                 (instance->disableOnlineCtrlReset == 0)) {
3243                         dev_notice(&instance->pdev->dev, "wait adp restart\n");
3244
3245                         if ((instance->pdev->device ==
3246                                         PCI_DEVICE_ID_LSI_SAS1064R) ||
3247                                 (instance->pdev->device ==
3248                                         PCI_DEVICE_ID_DELL_PERC5) ||
3249                                 (instance->pdev->device ==
3250                                         PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3251
3252                                 *instance->consumer =
3253                                         cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3254                         }
3255
3256
3257                         instance->instancet->disable_intr(instance);
3258                         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
3259                         instance->issuepend_done = 0;
3260
3261                         atomic_set(&instance->fw_outstanding, 0);
3262                         megasas_internal_reset_defer_cmds(instance);
3263
3264                         dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3265                                         fw_state, instance->adprecovery);
3266
3267                         schedule_work(&instance->work_init);
3268                         return IRQ_HANDLED;
3269
3270                 } else {
3271                         dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3272                                 fw_state, instance->disableOnlineCtrlReset);
3273                 }
3274         }
3275
3276         tasklet_schedule(&instance->isr_tasklet);
3277         return IRQ_HANDLED;
3278 }
3279 /**
3280  * megasas_isr - isr entry point
3281  */
3282 static irqreturn_t megasas_isr(int irq, void *devp)
3283 {
3284         struct megasas_irq_context *irq_context = devp;
3285         struct megasas_instance *instance = irq_context->instance;
3286         unsigned long flags;
3287         irqreturn_t     rc;
3288
3289         if (atomic_read(&instance->fw_reset_no_pci_access))
3290                 return IRQ_HANDLED;
3291
3292         spin_lock_irqsave(&instance->hba_lock, flags);
3293         rc =  megasas_deplete_reply_queue(instance, DID_OK);
3294         spin_unlock_irqrestore(&instance->hba_lock, flags);
3295
3296         return rc;
3297 }
3298
3299 /**
3300  * megasas_transition_to_ready -        Move the FW to READY state
3301  * @instance:                           Adapter soft state
3302  *
3303  * During the initialization, FW passes can potentially be in any one of
3304  * several possible states. If the FW in operational, waiting-for-handshake
3305  * states, driver must take steps to bring it to ready state. Otherwise, it
3306  * has to wait for the ready state.
3307  */
3308 int
3309 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3310 {
3311         int i;
3312         u8 max_wait;
3313         u32 fw_state;
3314         u32 cur_state;
3315         u32 abs_state, curr_abs_state;
3316
3317         abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3318         fw_state = abs_state & MFI_STATE_MASK;
3319
3320         if (fw_state != MFI_STATE_READY)
3321                 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3322                        " state\n");
3323
3324         while (fw_state != MFI_STATE_READY) {
3325
3326                 switch (fw_state) {
3327
3328                 case MFI_STATE_FAULT:
3329                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3330                         if (ocr) {
3331                                 max_wait = MEGASAS_RESET_WAIT_TIME;
3332                                 cur_state = MFI_STATE_FAULT;
3333                                 break;
3334                         } else
3335                                 return -ENODEV;
3336
3337                 case MFI_STATE_WAIT_HANDSHAKE:
3338                         /*
3339                          * Set the CLR bit in inbound doorbell
3340                          */
3341                         if ((instance->pdev->device ==
3342                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3343                                 (instance->pdev->device ==
3344                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3345                                 (instance->pdev->device ==
3346                                 PCI_DEVICE_ID_LSI_FUSION) ||
3347                                 (instance->pdev->device ==
3348                                 PCI_DEVICE_ID_LSI_PLASMA) ||
3349                                 (instance->pdev->device ==
3350                                 PCI_DEVICE_ID_LSI_INVADER) ||
3351                                 (instance->pdev->device ==
3352                                 PCI_DEVICE_ID_LSI_FURY)) {
3353                                 writel(
3354                                   MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3355                                   &instance->reg_set->doorbell);
3356                         } else {
3357                                 writel(
3358                                     MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3359                                         &instance->reg_set->inbound_doorbell);
3360                         }
3361
3362                         max_wait = MEGASAS_RESET_WAIT_TIME;
3363                         cur_state = MFI_STATE_WAIT_HANDSHAKE;
3364                         break;
3365
3366                 case MFI_STATE_BOOT_MESSAGE_PENDING:
3367                         if ((instance->pdev->device ==
3368                              PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3369                                 (instance->pdev->device ==
3370                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3371                             (instance->pdev->device ==
3372                              PCI_DEVICE_ID_LSI_FUSION) ||
3373                             (instance->pdev->device ==
3374                              PCI_DEVICE_ID_LSI_PLASMA) ||
3375                             (instance->pdev->device ==
3376                              PCI_DEVICE_ID_LSI_INVADER) ||
3377                             (instance->pdev->device ==
3378                              PCI_DEVICE_ID_LSI_FURY)) {
3379                                 writel(MFI_INIT_HOTPLUG,
3380                                        &instance->reg_set->doorbell);
3381                         } else
3382                                 writel(MFI_INIT_HOTPLUG,
3383                                         &instance->reg_set->inbound_doorbell);
3384
3385                         max_wait = MEGASAS_RESET_WAIT_TIME;
3386                         cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3387                         break;
3388
3389                 case MFI_STATE_OPERATIONAL:
3390                         /*
3391                          * Bring it to READY state; assuming max wait 10 secs
3392                          */
3393                         instance->instancet->disable_intr(instance);
3394                         if ((instance->pdev->device ==
3395                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3396                                 (instance->pdev->device ==
3397                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
3398                                 (instance->pdev->device
3399                                         == PCI_DEVICE_ID_LSI_FUSION) ||
3400                                 (instance->pdev->device
3401                                         == PCI_DEVICE_ID_LSI_PLASMA) ||
3402                                 (instance->pdev->device
3403                                         == PCI_DEVICE_ID_LSI_INVADER) ||
3404                                 (instance->pdev->device
3405                                         == PCI_DEVICE_ID_LSI_FURY)) {
3406                                 writel(MFI_RESET_FLAGS,
3407                                         &instance->reg_set->doorbell);
3408                                 if ((instance->pdev->device ==
3409                                         PCI_DEVICE_ID_LSI_FUSION) ||
3410                                         (instance->pdev->device ==
3411                                         PCI_DEVICE_ID_LSI_PLASMA) ||
3412                                         (instance->pdev->device ==
3413                                         PCI_DEVICE_ID_LSI_INVADER) ||
3414                                         (instance->pdev->device ==
3415                                         PCI_DEVICE_ID_LSI_FURY)) {
3416                                         for (i = 0; i < (10 * 1000); i += 20) {
3417                                                 if (readl(
3418                                                             &instance->
3419                                                             reg_set->
3420                                                             doorbell) & 1)
3421                                                         msleep(20);
3422                                                 else
3423                                                         break;
3424                                         }
3425                                 }
3426                         } else
3427                                 writel(MFI_RESET_FLAGS,
3428                                         &instance->reg_set->inbound_doorbell);
3429
3430                         max_wait = MEGASAS_RESET_WAIT_TIME;
3431                         cur_state = MFI_STATE_OPERATIONAL;
3432                         break;
3433
3434                 case MFI_STATE_UNDEFINED:
3435                         /*
3436                          * This state should not last for more than 2 seconds
3437                          */
3438                         max_wait = MEGASAS_RESET_WAIT_TIME;
3439                         cur_state = MFI_STATE_UNDEFINED;
3440                         break;
3441
3442                 case MFI_STATE_BB_INIT:
3443                         max_wait = MEGASAS_RESET_WAIT_TIME;
3444                         cur_state = MFI_STATE_BB_INIT;
3445                         break;
3446
3447                 case MFI_STATE_FW_INIT:
3448                         max_wait = MEGASAS_RESET_WAIT_TIME;
3449                         cur_state = MFI_STATE_FW_INIT;
3450                         break;
3451
3452                 case MFI_STATE_FW_INIT_2:
3453                         max_wait = MEGASAS_RESET_WAIT_TIME;
3454                         cur_state = MFI_STATE_FW_INIT_2;
3455                         break;
3456
3457                 case MFI_STATE_DEVICE_SCAN:
3458                         max_wait = MEGASAS_RESET_WAIT_TIME;
3459                         cur_state = MFI_STATE_DEVICE_SCAN;
3460                         break;
3461
3462                 case MFI_STATE_FLUSH_CACHE:
3463                         max_wait = MEGASAS_RESET_WAIT_TIME;
3464                         cur_state = MFI_STATE_FLUSH_CACHE;
3465                         break;
3466
3467                 default:
3468                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3469                                fw_state);
3470                         return -ENODEV;
3471                 }
3472
3473                 /*
3474                  * The cur_state should not last for more than max_wait secs
3475                  */
3476                 for (i = 0; i < (max_wait * 1000); i++) {
3477                         curr_abs_state = instance->instancet->
3478                                 read_fw_status_reg(instance->reg_set);
3479
3480                         if (abs_state == curr_abs_state) {
3481                                 msleep(1);
3482                         } else
3483                                 break;
3484                 }
3485
3486                 /*
3487                  * Return error if fw_state hasn't changed after max_wait
3488                  */
3489                 if (curr_abs_state == abs_state) {
3490                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3491                                "in %d secs\n", fw_state, max_wait);
3492                         return -ENODEV;
3493                 }
3494
3495                 abs_state = curr_abs_state;
3496                 fw_state = curr_abs_state & MFI_STATE_MASK;
3497         }
3498         dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3499
3500         return 0;
3501 }
3502
3503 /**
3504  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
3505  * @instance:                           Adapter soft state
3506  */
3507 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3508 {
3509         int i;
3510         u32 max_cmd = instance->max_mfi_cmds;
3511         struct megasas_cmd *cmd;
3512
3513         if (!instance->frame_dma_pool)
3514                 return;
3515
3516         /*
3517          * Return all frames to pool
3518          */
3519         for (i = 0; i < max_cmd; i++) {
3520
3521                 cmd = instance->cmd_list[i];
3522
3523                 if (cmd->frame)
3524                         pci_pool_free(instance->frame_dma_pool, cmd->frame,
3525                                       cmd->frame_phys_addr);
3526
3527                 if (cmd->sense)
3528                         pci_pool_free(instance->sense_dma_pool, cmd->sense,
3529                                       cmd->sense_phys_addr);
3530         }
3531
3532         /*
3533          * Now destroy the pool itself
3534          */
3535         pci_pool_destroy(instance->frame_dma_pool);
3536         pci_pool_destroy(instance->sense_dma_pool);
3537
3538         instance->frame_dma_pool = NULL;
3539         instance->sense_dma_pool = NULL;
3540 }
3541
3542 /**
3543  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
3544  * @instance:                   Adapter soft state
3545  *
3546  * Each command packet has an embedded DMA memory buffer that is used for
3547  * filling MFI frame and the SG list that immediately follows the frame. This
3548  * function creates those DMA memory buffers for each command packet by using
3549  * PCI pool facility.
3550  */
3551 static int megasas_create_frame_pool(struct megasas_instance *instance)
3552 {
3553         int i;
3554         u32 max_cmd;
3555         u32 sge_sz;
3556         u32 total_sz;
3557         u32 frame_count;
3558         struct megasas_cmd *cmd;
3559
3560         max_cmd = instance->max_mfi_cmds;
3561
3562         /*
3563          * Size of our frame is 64 bytes for MFI frame, followed by max SG
3564          * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3565          */
3566         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3567             sizeof(struct megasas_sge32);
3568
3569         if (instance->flag_ieee) {
3570                 sge_sz = sizeof(struct megasas_sge_skinny);
3571         }
3572
3573         /*
3574          * For MFI controllers.
3575          * max_num_sge = 60
3576          * max_sge_sz  = 16 byte (sizeof megasas_sge_skinny)
3577          * Total 960 byte (15 MFI frame of 64 byte)
3578          *
3579          * Fusion adapter require only 3 extra frame.
3580          * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3581          * max_sge_sz  = 12 byte (sizeof  megasas_sge64)
3582          * Total 192 byte (3 MFI frame of 64 byte)
3583          */
3584         frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3585         total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3586         /*
3587          * Use DMA pool facility provided by PCI layer
3588          */
3589         instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3590                                         instance->pdev, total_sz, 256, 0);
3591
3592         if (!instance->frame_dma_pool) {
3593                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3594                 return -ENOMEM;
3595         }
3596
3597         instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3598                                                    instance->pdev, 128, 4, 0);
3599
3600         if (!instance->sense_dma_pool) {
3601                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3602
3603                 pci_pool_destroy(instance->frame_dma_pool);
3604                 instance->frame_dma_pool = NULL;
3605
3606                 return -ENOMEM;
3607         }
3608
3609         /*
3610          * Allocate and attach a frame to each of the commands in cmd_list.
3611          * By making cmd->index as the context instead of the &cmd, we can
3612          * always use 32bit context regardless of the architecture
3613          */
3614         for (i = 0; i < max_cmd; i++) {
3615
3616                 cmd = instance->cmd_list[i];
3617
3618                 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3619                                             GFP_KERNEL, &cmd->frame_phys_addr);
3620
3621                 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3622                                             GFP_KERNEL, &cmd->sense_phys_addr);
3623
3624                 /*
3625                  * megasas_teardown_frame_pool() takes care of freeing
3626                  * whatever has been allocated
3627                  */
3628                 if (!cmd->frame || !cmd->sense) {
3629                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3630                         megasas_teardown_frame_pool(instance);
3631                         return -ENOMEM;
3632                 }
3633
3634                 memset(cmd->frame, 0, total_sz);
3635                 cmd->frame->io.context = cpu_to_le32(cmd->index);
3636                 cmd->frame->io.pad_0 = 0;
3637                 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
3638                     (instance->pdev->device != PCI_DEVICE_ID_LSI_PLASMA) &&
3639                     (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
3640                         (instance->pdev->device != PCI_DEVICE_ID_LSI_FURY) &&
3641                     (reset_devices))
3642                         cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3643         }
3644
3645         return 0;
3646 }
3647
3648 /**
3649  * megasas_free_cmds -  Free all the cmds in the free cmd pool
3650  * @instance:           Adapter soft state
3651  */
3652 void megasas_free_cmds(struct megasas_instance *instance)
3653 {
3654         int i;
3655         /* First free the MFI frame pool */
3656         megasas_teardown_frame_pool(instance);
3657
3658         /* Free all the commands in the cmd_list */
3659         for (i = 0; i < instance->max_mfi_cmds; i++)
3660
3661                 kfree(instance->cmd_list[i]);
3662
3663         /* Free the cmd_list buffer itself */
3664         kfree(instance->cmd_list);
3665         instance->cmd_list = NULL;
3666
3667         INIT_LIST_HEAD(&instance->cmd_pool);
3668 }
3669
3670 /**
3671  * megasas_alloc_cmds - Allocates the command packets
3672  * @instance:           Adapter soft state
3673  *
3674  * Each command that is issued to the FW, whether IO commands from the OS or
3675  * internal commands like IOCTLs, are wrapped in local data structure called
3676  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3677  * the FW.
3678  *
3679  * Each frame has a 32-bit field called context (tag). This context is used
3680  * to get back the megasas_cmd from the frame when a frame gets completed in
3681  * the ISR. Typically the address of the megasas_cmd itself would be used as
3682  * the context. But we wanted to keep the differences between 32 and 64 bit
3683  * systems to the mininum. We always use 32 bit integers for the context. In
3684  * this driver, the 32 bit values are the indices into an array cmd_list.
3685  * This array is used only to look up the megasas_cmd given the context. The
3686  * free commands themselves are maintained in a linked list called cmd_pool.
3687  */
3688 int megasas_alloc_cmds(struct megasas_instance *instance)
3689 {
3690         int i;
3691         int j;
3692         u32 max_cmd;
3693         struct megasas_cmd *cmd;
3694         struct fusion_context *fusion;
3695
3696         fusion = instance->ctrl_context;
3697         max_cmd = instance->max_mfi_cmds;
3698
3699         /*
3700          * instance->cmd_list is an array of struct megasas_cmd pointers.
3701          * Allocate the dynamic array first and then allocate individual
3702          * commands.
3703          */
3704         instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3705
3706         if (!instance->cmd_list) {
3707                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3708                 return -ENOMEM;
3709         }
3710
3711         memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3712
3713         for (i = 0; i < max_cmd; i++) {
3714                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3715                                                 GFP_KERNEL);
3716
3717                 if (!instance->cmd_list[i]) {
3718
3719                         for (j = 0; j < i; j++)
3720                                 kfree(instance->cmd_list[j]);
3721
3722                         kfree(instance->cmd_list);
3723                         instance->cmd_list = NULL;
3724
3725                         return -ENOMEM;
3726                 }
3727         }
3728
3729         for (i = 0; i < max_cmd; i++) {
3730                 cmd = instance->cmd_list[i];
3731                 memset(cmd, 0, sizeof(struct megasas_cmd));
3732                 cmd->index = i;
3733                 cmd->scmd = NULL;
3734                 cmd->instance = instance;
3735
3736                 list_add_tail(&cmd->list, &instance->cmd_pool);
3737         }
3738
3739         /*
3740          * Create a frame pool and assign one frame to each cmd
3741          */
3742         if (megasas_create_frame_pool(instance)) {
3743                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3744                 megasas_free_cmds(instance);
3745         }
3746
3747         return 0;
3748 }
3749
3750 /*
3751  * megasas_get_pd_list_info -   Returns FW's pd_list structure
3752  * @instance:                           Adapter soft state
3753  * @pd_list:                            pd_list structure
3754  *
3755  * Issues an internal command (DCMD) to get the FW's controller PD
3756  * list structure.  This information is mainly used to find out SYSTEM
3757  * supported by the FW.
3758  */
3759 static int
3760 megasas_get_pd_list(struct megasas_instance *instance)
3761 {
3762         int ret = 0, pd_index = 0;
3763         struct megasas_cmd *cmd;
3764         struct megasas_dcmd_frame *dcmd;
3765         struct MR_PD_LIST *ci;
3766         struct MR_PD_ADDRESS *pd_addr;
3767         dma_addr_t ci_h = 0;
3768
3769         cmd = megasas_get_cmd(instance);
3770
3771         if (!cmd) {
3772                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3773                 return -ENOMEM;
3774         }
3775
3776         dcmd = &cmd->frame->dcmd;
3777
3778         ci = pci_alloc_consistent(instance->pdev,
3779                   MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3780
3781         if (!ci) {
3782                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3783                 megasas_return_cmd(instance, cmd);
3784                 return -ENOMEM;
3785         }
3786
3787         memset(ci, 0, sizeof(*ci));
3788         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3789
3790         dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3791         dcmd->mbox.b[1] = 0;
3792         dcmd->cmd = MFI_CMD_DCMD;
3793         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3794         dcmd->sge_count = 1;
3795         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3796         dcmd->timeout = 0;
3797         dcmd->pad_0 = 0;
3798         dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3799         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3800         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3801         dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3802
3803         if (instance->ctrl_context && !instance->mask_interrupts)
3804                 ret = megasas_issue_blocked_cmd(instance, cmd,
3805                         MEGASAS_BLOCKED_CMD_TIMEOUT);
3806         else
3807                 ret = megasas_issue_polled(instance, cmd);
3808
3809         /*
3810         * the following function will get the instance PD LIST.
3811         */
3812
3813         pd_addr = ci->addr;
3814
3815         if ( ret == 0 &&
3816              (le32_to_cpu(ci->count) <
3817                   (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3818
3819                 memset(instance->local_pd_list, 0,
3820                         MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3821
3822                 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3823
3824                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid     =
3825                                 le16_to_cpu(pd_addr->deviceId);
3826                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType       =
3827                                                         pd_addr->scsiDevType;
3828                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState      =
3829                                                         MR_PD_STATE_SYSTEM;
3830                         pd_addr++;
3831                 }
3832                 memcpy(instance->pd_list, instance->local_pd_list,
3833                         sizeof(instance->pd_list));
3834         }
3835
3836         pci_free_consistent(instance->pdev,
3837                                 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3838                                 ci, ci_h);
3839
3840         megasas_return_cmd(instance, cmd);
3841
3842         return ret;
3843 }
3844
3845 /*
3846  * megasas_get_ld_list_info -   Returns FW's ld_list structure
3847  * @instance:                           Adapter soft state
3848  * @ld_list:                            ld_list structure
3849  *
3850  * Issues an internal command (DCMD) to get the FW's controller PD
3851  * list structure.  This information is mainly used to find out SYSTEM
3852  * supported by the FW.
3853  */
3854 static int
3855 megasas_get_ld_list(struct megasas_instance *instance)
3856 {
3857         int ret = 0, ld_index = 0, ids = 0;
3858         struct megasas_cmd *cmd;
3859         struct megasas_dcmd_frame *dcmd;
3860         struct MR_LD_LIST *ci;
3861         dma_addr_t ci_h = 0;
3862         u32 ld_count;
3863
3864         cmd = megasas_get_cmd(instance);
3865
3866         if (!cmd) {
3867                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3868                 return -ENOMEM;
3869         }
3870
3871         dcmd = &cmd->frame->dcmd;
3872
3873         ci = pci_alloc_consistent(instance->pdev,
3874                                 sizeof(struct MR_LD_LIST),
3875                                 &ci_h);
3876
3877         if (!ci) {
3878                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3879                 megasas_return_cmd(instance, cmd);
3880                 return -ENOMEM;
3881         }
3882
3883         memset(ci, 0, sizeof(*ci));
3884         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3885
3886         if (instance->supportmax256vd)
3887                 dcmd->mbox.b[0] = 1;
3888         dcmd->cmd = MFI_CMD_DCMD;
3889         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3890         dcmd->sge_count = 1;
3891         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3892         dcmd->timeout = 0;
3893         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3894         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3895         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3896         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
3897         dcmd->pad_0  = 0;
3898
3899         if (instance->ctrl_context && !instance->mask_interrupts)
3900                 ret = megasas_issue_blocked_cmd(instance, cmd,
3901                         MEGASAS_BLOCKED_CMD_TIMEOUT);
3902         else
3903                 ret = megasas_issue_polled(instance, cmd);
3904
3905
3906         ld_count = le32_to_cpu(ci->ldCount);
3907
3908         /* the following function will get the instance PD LIST */
3909
3910         if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
3911                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
3912
3913                 for (ld_index = 0; ld_index < ld_count; ld_index++) {
3914                         if (ci->ldList[ld_index].state != 0) {
3915                                 ids = ci->ldList[ld_index].ref.targetId;
3916                                 instance->ld_ids[ids] =
3917                                         ci->ldList[ld_index].ref.targetId;
3918                         }
3919                 }
3920         }
3921
3922         pci_free_consistent(instance->pdev,
3923                                 sizeof(struct MR_LD_LIST),
3924                                 ci,
3925                                 ci_h);
3926
3927         megasas_return_cmd(instance, cmd);
3928         return ret;
3929 }
3930
3931 /**
3932  * megasas_ld_list_query -      Returns FW's ld_list structure
3933  * @instance:                           Adapter soft state
3934  * @ld_list:                            ld_list structure
3935  *
3936  * Issues an internal command (DCMD) to get the FW's controller PD
3937  * list structure.  This information is mainly used to find out SYSTEM
3938  * supported by the FW.
3939  */
3940 static int
3941 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
3942 {
3943         int ret = 0, ld_index = 0, ids = 0;
3944         struct megasas_cmd *cmd;
3945         struct megasas_dcmd_frame *dcmd;
3946         struct MR_LD_TARGETID_LIST *ci;
3947         dma_addr_t ci_h = 0;
3948         u32 tgtid_count;
3949
3950         cmd = megasas_get_cmd(instance);
3951
3952         if (!cmd) {
3953                 dev_warn(&instance->pdev->dev,
3954                          "megasas_ld_list_query: Failed to get cmd\n");
3955                 return -ENOMEM;
3956         }
3957
3958         dcmd = &cmd->frame->dcmd;
3959
3960         ci = pci_alloc_consistent(instance->pdev,
3961                                   sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
3962
3963         if (!ci) {
3964                 dev_warn(&instance->pdev->dev,
3965                          "Failed to alloc mem for ld_list_query\n");
3966                 megasas_return_cmd(instance, cmd);
3967                 return -ENOMEM;
3968         }
3969
3970         memset(ci, 0, sizeof(*ci));
3971         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3972
3973         dcmd->mbox.b[0] = query_type;
3974         if (instance->supportmax256vd)
3975                 dcmd->mbox.b[2] = 1;
3976
3977         dcmd->cmd = MFI_CMD_DCMD;
3978         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3979         dcmd->sge_count = 1;
3980         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3981         dcmd->timeout = 0;
3982         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3983         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
3984         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3985         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
3986         dcmd->pad_0  = 0;
3987
3988         if (instance->ctrl_context && !instance->mask_interrupts)
3989                 ret = megasas_issue_blocked_cmd(instance, cmd,
3990                         MEGASAS_BLOCKED_CMD_TIMEOUT);
3991         else
3992                 ret = megasas_issue_polled(instance, cmd);
3993
3994         tgtid_count = le32_to_cpu(ci->count);
3995
3996         if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
3997                 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3998                 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
3999                         ids = ci->targetId[ld_index];
4000                         instance->ld_ids[ids] = ci->targetId[ld_index];
4001                 }
4002
4003         }
4004
4005         pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4006                             ci, ci_h);
4007
4008         megasas_return_cmd(instance, cmd);
4009
4010         return ret;
4011 }
4012
4013 /*
4014  * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4015  * instance                      : Controller's instance
4016 */
4017 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4018 {
4019         struct fusion_context *fusion;
4020         u32 old_map_sz;
4021         u32 new_map_sz;
4022
4023         fusion = instance->ctrl_context;
4024         /* For MFI based controllers return dummy success */
4025         if (!fusion)
4026                 return;
4027
4028         instance->supportmax256vd =
4029                 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4030         /* Below is additional check to address future FW enhancement */
4031         if (instance->ctrl_info->max_lds > 64)
4032                 instance->supportmax256vd = 1;
4033
4034         instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4035                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4036         instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4037                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4038         if (instance->supportmax256vd) {
4039                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4040                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4041         } else {
4042                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4043                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4044         }
4045
4046         dev_info(&instance->pdev->dev,
4047                 "firmware type\t: %s\n",
4048                 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4049                 "Legacy(64 VD) firmware");
4050
4051         old_map_sz =  sizeof(struct MR_FW_RAID_MAP) +
4052                                 (sizeof(struct MR_LD_SPAN_MAP) *
4053                                 (instance->fw_supported_vd_count - 1));
4054         new_map_sz =  sizeof(struct MR_FW_RAID_MAP_EXT);
4055         fusion->drv_map_sz =  sizeof(struct MR_DRV_RAID_MAP) +
4056                                 (sizeof(struct MR_LD_SPAN_MAP) *
4057                                 (instance->drv_supported_vd_count - 1));
4058
4059         fusion->max_map_sz = max(old_map_sz, new_map_sz);
4060
4061
4062         if (instance->supportmax256vd)
4063                 fusion->current_map_sz = new_map_sz;
4064         else
4065                 fusion->current_map_sz = old_map_sz;
4066
4067 }
4068
4069 /**
4070  * megasas_get_controller_info -        Returns FW's controller structure
4071  * @instance:                           Adapter soft state
4072  *
4073  * Issues an internal command (DCMD) to get the FW's controller structure.
4074  * This information is mainly used to find out the maximum IO transfer per
4075  * command supported by the FW.
4076  */
4077 int
4078 megasas_get_ctrl_info(struct megasas_instance *instance)
4079 {
4080         int ret = 0;
4081         struct megasas_cmd *cmd;
4082         struct megasas_dcmd_frame *dcmd;
4083         struct megasas_ctrl_info *ci;
4084         struct megasas_ctrl_info *ctrl_info;
4085         dma_addr_t ci_h = 0;
4086
4087         ctrl_info = instance->ctrl_info;
4088
4089         cmd = megasas_get_cmd(instance);
4090
4091         if (!cmd) {
4092                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4093                 return -ENOMEM;
4094         }
4095
4096         dcmd = &cmd->frame->dcmd;
4097
4098         ci = pci_alloc_consistent(instance->pdev,
4099                                   sizeof(struct megasas_ctrl_info), &ci_h);
4100
4101         if (!ci) {
4102                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4103                 megasas_return_cmd(instance, cmd);
4104                 return -ENOMEM;
4105         }
4106
4107         memset(ci, 0, sizeof(*ci));
4108         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4109
4110         dcmd->cmd = MFI_CMD_DCMD;
4111         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4112         dcmd->sge_count = 1;
4113         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4114         dcmd->timeout = 0;
4115         dcmd->pad_0 = 0;
4116         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4117         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4118         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4119         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4120         dcmd->mbox.b[0] = 1;
4121
4122         if (instance->ctrl_context && !instance->mask_interrupts)
4123                 ret = megasas_issue_blocked_cmd(instance, cmd,
4124                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4125         else
4126                 ret = megasas_issue_polled(instance, cmd);
4127
4128         if (!ret) {
4129                 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4130                 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4131                 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4132                 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4133                 megasas_update_ext_vd_details(instance);
4134                 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4135                 dev_info(&instance->pdev->dev,
4136                                 "controller type\t: %s(%dMB)\n",
4137                                 instance->is_imr ? "iMR" : "MR",
4138                                 le16_to_cpu(ctrl_info->memory_size));
4139         }
4140
4141         pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4142                             ci, ci_h);
4143
4144         megasas_return_cmd(instance, cmd);
4145         return ret;
4146 }
4147
4148 /*
4149  * megasas_set_crash_dump_params -      Sends address of crash dump DMA buffer
4150  *                                      to firmware
4151  *
4152  * @instance:                           Adapter soft state
4153  * @crash_buf_state             -       tell FW to turn ON/OFF crash dump feature
4154                                         MR_CRASH_BUF_TURN_OFF = 0
4155                                         MR_CRASH_BUF_TURN_ON = 1
4156  * @return 0 on success non-zero on failure.
4157  * Issues an internal command (DCMD) to set parameters for crash dump feature.
4158  * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4159  * that driver supports crash dump feature. This DCMD will be sent only if
4160  * crash dump feature is supported by the FW.
4161  *
4162  */
4163 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4164         u8 crash_buf_state)
4165 {
4166         int ret = 0;
4167         struct megasas_cmd *cmd;
4168         struct megasas_dcmd_frame *dcmd;
4169
4170         cmd = megasas_get_cmd(instance);
4171
4172         if (!cmd) {
4173                 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4174                 return -ENOMEM;
4175         }
4176
4177
4178         dcmd = &cmd->frame->dcmd;
4179
4180         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4181         dcmd->mbox.b[0] = crash_buf_state;
4182         dcmd->cmd = MFI_CMD_DCMD;
4183         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4184         dcmd->sge_count = 1;
4185         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4186         dcmd->timeout = 0;
4187         dcmd->pad_0 = 0;
4188         dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4189         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4190         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4191         dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4192
4193         if (instance->ctrl_context && !instance->mask_interrupts)
4194                 ret = megasas_issue_blocked_cmd(instance, cmd,
4195                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4196         else
4197                 ret = megasas_issue_polled(instance, cmd);
4198
4199         megasas_return_cmd(instance, cmd);
4200         return ret;
4201 }
4202
4203 /**
4204  * megasas_issue_init_mfi -     Initializes the FW
4205  * @instance:           Adapter soft state
4206  *
4207  * Issues the INIT MFI cmd
4208  */
4209 static int
4210 megasas_issue_init_mfi(struct megasas_instance *instance)
4211 {
4212         __le32 context;
4213
4214         struct megasas_cmd *cmd;
4215
4216         struct megasas_init_frame *init_frame;
4217         struct megasas_init_queue_info *initq_info;
4218         dma_addr_t init_frame_h;
4219         dma_addr_t initq_info_h;
4220
4221         /*
4222          * Prepare a init frame. Note the init frame points to queue info
4223          * structure. Each frame has SGL allocated after first 64 bytes. For
4224          * this frame - since we don't need any SGL - we use SGL's space as
4225          * queue info structure
4226          *
4227          * We will not get a NULL command below. We just created the pool.
4228          */
4229         cmd = megasas_get_cmd(instance);
4230
4231         init_frame = (struct megasas_init_frame *)cmd->frame;
4232         initq_info = (struct megasas_init_queue_info *)
4233                 ((unsigned long)init_frame + 64);
4234
4235         init_frame_h = cmd->frame_phys_addr;
4236         initq_info_h = init_frame_h + 64;
4237
4238         context = init_frame->context;
4239         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4240         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4241         init_frame->context = context;
4242
4243         initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4244         initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4245
4246         initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4247         initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4248
4249         init_frame->cmd = MFI_CMD_INIT;
4250         init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4251         init_frame->queue_info_new_phys_addr_lo =
4252                 cpu_to_le32(lower_32_bits(initq_info_h));
4253         init_frame->queue_info_new_phys_addr_hi =
4254                 cpu_to_le32(upper_32_bits(initq_info_h));
4255
4256         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4257
4258         /*
4259          * disable the intr before firing the init frame to FW
4260          */
4261         instance->instancet->disable_intr(instance);
4262
4263         /*
4264          * Issue the init frame in polled mode
4265          */
4266
4267         if (megasas_issue_polled(instance, cmd)) {
4268                 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4269                 megasas_return_cmd(instance, cmd);
4270                 goto fail_fw_init;
4271         }
4272
4273         megasas_return_cmd(instance, cmd);
4274
4275         return 0;
4276
4277 fail_fw_init:
4278         return -EINVAL;
4279 }
4280
4281 static u32
4282 megasas_init_adapter_mfi(struct megasas_instance *instance)
4283 {
4284         struct megasas_register_set __iomem *reg_set;
4285         u32 context_sz;
4286         u32 reply_q_sz;
4287
4288         reg_set = instance->reg_set;
4289
4290         /*
4291          * Get various operational parameters from status register
4292          */
4293         instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4294         /*
4295          * Reduce the max supported cmds by 1. This is to ensure that the
4296          * reply_q_sz (1 more than the max cmd that driver may send)
4297          * does not exceed max cmds that the FW can support
4298          */
4299         instance->max_fw_cmds = instance->max_fw_cmds-1;
4300         instance->max_mfi_cmds = instance->max_fw_cmds;
4301         instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4302                                         0x10;
4303         /*
4304          * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4305          * are reserved for IOCTL + driver's internal DCMDs.
4306          */
4307         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4308                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4309                 instance->max_scsi_cmds = (instance->max_fw_cmds -
4310                         MEGASAS_SKINNY_INT_CMDS);
4311                 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4312         } else {
4313                 instance->max_scsi_cmds = (instance->max_fw_cmds -
4314                         MEGASAS_INT_CMDS);
4315                 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4316         }
4317
4318         /*
4319          * Create a pool of commands
4320          */
4321         if (megasas_alloc_cmds(instance))
4322                 goto fail_alloc_cmds;
4323
4324         /*
4325          * Allocate memory for reply queue. Length of reply queue should
4326          * be _one_ more than the maximum commands handled by the firmware.
4327          *
4328          * Note: When FW completes commands, it places corresponding contex
4329          * values in this circular reply queue. This circular queue is a fairly
4330          * typical producer-consumer queue. FW is the producer (of completed
4331          * commands) and the driver is the consumer.
4332          */
4333         context_sz = sizeof(u32);
4334         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4335
4336         instance->reply_queue = pci_alloc_consistent(instance->pdev,
4337                                                      reply_q_sz,
4338                                                      &instance->reply_queue_h);
4339
4340         if (!instance->reply_queue) {
4341                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4342                 goto fail_reply_queue;
4343         }
4344
4345         if (megasas_issue_init_mfi(instance))
4346                 goto fail_fw_init;
4347
4348         if (megasas_get_ctrl_info(instance)) {
4349                 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4350                         "Fail from %s %d\n", instance->unique_id,
4351                         __func__, __LINE__);
4352                 goto fail_fw_init;
4353         }
4354
4355         instance->fw_support_ieee = 0;
4356         instance->fw_support_ieee =
4357                 (instance->instancet->read_fw_status_reg(reg_set) &
4358                 0x04000000);
4359
4360         dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4361                         instance->fw_support_ieee);
4362
4363         if (instance->fw_support_ieee)
4364                 instance->flag_ieee = 1;
4365
4366         return 0;
4367
4368 fail_fw_init:
4369
4370         pci_free_consistent(instance->pdev, reply_q_sz,
4371                             instance->reply_queue, instance->reply_queue_h);
4372 fail_reply_queue:
4373         megasas_free_cmds(instance);
4374
4375 fail_alloc_cmds:
4376         return 1;
4377 }
4378
4379 /*
4380  * megasas_setup_irqs_msix -            register legacy interrupts.
4381  * @instance:                           Adapter soft state
4382  *
4383  * Do not enable interrupt, only setup ISRs.
4384  *
4385  * Return 0 on success.
4386  */
4387 static int
4388 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4389 {
4390         struct pci_dev *pdev;
4391
4392         pdev = instance->pdev;
4393         instance->irq_context[0].instance = instance;
4394         instance->irq_context[0].MSIxIndex = 0;
4395         if (request_irq(pdev->irq, instance->instancet->service_isr,
4396                 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4397                 dev_err(&instance->pdev->dev,
4398                                 "Failed to register IRQ from %s %d\n",
4399                                 __func__, __LINE__);
4400                 return -1;
4401         }
4402         return 0;
4403 }
4404
4405 /**
4406  * megasas_setup_irqs_msix -            register MSI-x interrupts.
4407  * @instance:                           Adapter soft state
4408  * @is_probe:                           Driver probe check
4409  *
4410  * Do not enable interrupt, only setup ISRs.
4411  *
4412  * Return 0 on success.
4413  */
4414 static int
4415 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4416 {
4417         int i, j, cpu;
4418         struct pci_dev *pdev;
4419
4420         pdev = instance->pdev;
4421
4422         /* Try MSI-x */
4423         cpu = cpumask_first(cpu_online_mask);
4424         for (i = 0; i < instance->msix_vectors; i++) {
4425                 instance->irq_context[i].instance = instance;
4426                 instance->irq_context[i].MSIxIndex = i;
4427                 if (request_irq(instance->msixentry[i].vector,
4428                         instance->instancet->service_isr, 0, "megasas",
4429                         &instance->irq_context[i])) {
4430                         dev_err(&instance->pdev->dev,
4431                                 "Failed to register IRQ for vector %d.\n", i);
4432                         for (j = 0; j < i; j++) {
4433                                 if (smp_affinity_enable)
4434                                         irq_set_affinity_hint(
4435                                                 instance->msixentry[j].vector, NULL);
4436                                 free_irq(instance->msixentry[j].vector,
4437                                         &instance->irq_context[j]);
4438                         }
4439                         /* Retry irq register for IO_APIC*/
4440                         instance->msix_vectors = 0;
4441                         if (is_probe)
4442                                 return megasas_setup_irqs_ioapic(instance);
4443                         else
4444                                 return -1;
4445                 }
4446                 if (smp_affinity_enable) {
4447                         if (irq_set_affinity_hint(instance->msixentry[i].vector,
4448                                 get_cpu_mask(cpu)))
4449                                 dev_err(&instance->pdev->dev,
4450                                         "Failed to set affinity hint"
4451                                         " for cpu %d\n", cpu);
4452                         cpu = cpumask_next(cpu, cpu_online_mask);
4453                 }
4454         }
4455         return 0;
4456 }
4457
4458 /*
4459  * megasas_destroy_irqs-                unregister interrupts.
4460  * @instance:                           Adapter soft state
4461  * return:                              void
4462  */
4463 static void
4464 megasas_destroy_irqs(struct megasas_instance *instance) {
4465
4466         int i;
4467
4468         if (instance->msix_vectors)
4469                 for (i = 0; i < instance->msix_vectors; i++) {
4470                         if (smp_affinity_enable)
4471                                 irq_set_affinity_hint(
4472                                         instance->msixentry[i].vector, NULL);
4473                         free_irq(instance->msixentry[i].vector,
4474                                  &instance->irq_context[i]);
4475                 }
4476         else
4477                 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4478 }
4479
4480 /**
4481  * megasas_init_fw -    Initializes the FW
4482  * @instance:           Adapter soft state
4483  *
4484  * This is the main function for initializing firmware
4485  */
4486
4487 static int megasas_init_fw(struct megasas_instance *instance)
4488 {
4489         u32 max_sectors_1;
4490         u32 max_sectors_2;
4491         u32 tmp_sectors, msix_enable, scratch_pad_2;
4492         resource_size_t base_addr;
4493         struct megasas_register_set __iomem *reg_set;
4494         struct megasas_ctrl_info *ctrl_info = NULL;
4495         unsigned long bar_list;
4496         int i, loop, fw_msix_count = 0;
4497         struct IOV_111 *iovPtr;
4498
4499         /* Find first memory bar */
4500         bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4501         instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4502         if (pci_request_selected_regions(instance->pdev, instance->bar,
4503                                          "megasas: LSI")) {
4504                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4505                 return -EBUSY;
4506         }
4507
4508         base_addr = pci_resource_start(instance->pdev, instance->bar);
4509         instance->reg_set = ioremap_nocache(base_addr, 8192);
4510
4511         if (!instance->reg_set) {
4512                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4513                 goto fail_ioremap;
4514         }
4515
4516         reg_set = instance->reg_set;
4517
4518         switch (instance->pdev->device) {
4519         case PCI_DEVICE_ID_LSI_FUSION:
4520         case PCI_DEVICE_ID_LSI_PLASMA:
4521         case PCI_DEVICE_ID_LSI_INVADER:
4522         case PCI_DEVICE_ID_LSI_FURY:
4523                 instance->instancet = &megasas_instance_template_fusion;
4524                 break;
4525         case PCI_DEVICE_ID_LSI_SAS1078R:
4526         case PCI_DEVICE_ID_LSI_SAS1078DE:
4527                 instance->instancet = &megasas_instance_template_ppc;
4528                 break;
4529         case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4530         case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4531                 instance->instancet = &megasas_instance_template_gen2;
4532                 break;
4533         case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4534         case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4535                 instance->instancet = &megasas_instance_template_skinny;
4536                 break;
4537         case PCI_DEVICE_ID_LSI_SAS1064R:
4538         case PCI_DEVICE_ID_DELL_PERC5:
4539         default:
4540                 instance->instancet = &megasas_instance_template_xscale;
4541                 break;
4542         }
4543
4544         if (megasas_transition_to_ready(instance, 0)) {
4545                 atomic_set(&instance->fw_reset_no_pci_access, 1);
4546                 instance->instancet->adp_reset
4547                         (instance, instance->reg_set);
4548                 atomic_set(&instance->fw_reset_no_pci_access, 0);
4549                 dev_info(&instance->pdev->dev,
4550                         "FW restarted successfully from %s!\n",
4551                         __func__);
4552
4553                 /*waitting for about 30 second before retry*/
4554                 ssleep(30);
4555
4556                 if (megasas_transition_to_ready(instance, 0))
4557                         goto fail_ready_state;
4558         }
4559
4560         /*
4561          * MSI-X host index 0 is common for all adapter.
4562          * It is used for all MPT based Adapters.
4563          */
4564         instance->reply_post_host_index_addr[0] =
4565                 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4566                 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4567
4568         /* Check if MSI-X is supported while in ready state */
4569         msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4570                        0x4000000) >> 0x1a;
4571         if (msix_enable && !msix_disable) {
4572                 scratch_pad_2 = readl
4573                         (&instance->reg_set->outbound_scratch_pad_2);
4574                 /* Check max MSI-X vectors */
4575                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4576                     (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA)) {
4577                         instance->msix_vectors = (scratch_pad_2
4578                                 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4579                         fw_msix_count = instance->msix_vectors;
4580                         if (msix_vectors)
4581                                 instance->msix_vectors =
4582                                         min(msix_vectors,
4583                                             instance->msix_vectors);
4584                 } else if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)
4585                         || (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
4586                         /* Invader/Fury supports more than 8 MSI-X */
4587                         instance->msix_vectors = ((scratch_pad_2
4588                                 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4589                                 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4590                         fw_msix_count = instance->msix_vectors;
4591                         /* Save 1-15 reply post index address to local memory
4592                          * Index 0 is already saved from reg offset
4593                          * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4594                          */
4595                         for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4596                                 instance->reply_post_host_index_addr[loop] =
4597                                         (u32 __iomem *)
4598                                         ((u8 __iomem *)instance->reg_set +
4599                                         MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4600                                         + (loop * 0x10));
4601                         }
4602                         if (msix_vectors)
4603                                 instance->msix_vectors = min(msix_vectors,
4604                                         instance->msix_vectors);
4605                 } else
4606                         instance->msix_vectors = 1;
4607                 /* Don't bother allocating more MSI-X vectors than cpus */
4608                 instance->msix_vectors = min(instance->msix_vectors,
4609                                              (unsigned int)num_online_cpus());
4610                 for (i = 0; i < instance->msix_vectors; i++)
4611                         instance->msixentry[i].entry = i;
4612                 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4613                                           1, instance->msix_vectors);
4614                 if (i > 0)
4615                         instance->msix_vectors = i;
4616                 else
4617                         instance->msix_vectors = 0;
4618         }
4619
4620         dev_info(&instance->pdev->dev,
4621                 "firmware supports msix\t: (%d)", fw_msix_count);
4622         dev_info(&instance->pdev->dev,
4623                 "current msix/online cpus\t: (%d/%d)\n",
4624                 instance->msix_vectors, (unsigned int)num_online_cpus());
4625
4626         if (instance->msix_vectors ?
4627                 megasas_setup_irqs_msix(instance, 1) :
4628                 megasas_setup_irqs_ioapic(instance))
4629                 goto fail_setup_irqs;
4630
4631         instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4632                                 GFP_KERNEL);
4633         if (instance->ctrl_info == NULL)
4634                 goto fail_init_adapter;
4635
4636         /*
4637          * Below are default value for legacy Firmware.
4638          * non-fusion based controllers
4639          */
4640         instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4641         instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4642         /* Get operational params, sge flags, send init cmd to controller */
4643         if (instance->instancet->init_adapter(instance))
4644                 goto fail_init_adapter;
4645
4646         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4647                 (unsigned long)instance);
4648
4649         instance->instancet->enable_intr(instance);
4650
4651         dev_err(&instance->pdev->dev, "INIT adapter done\n");
4652
4653         /** for passthrough
4654         * the following function will get the PD LIST.
4655         */
4656
4657         memset(instance->pd_list, 0 ,
4658                 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4659         if (megasas_get_pd_list(instance) < 0) {
4660                 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4661                 goto fail_get_pd_list;
4662         }
4663
4664         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4665         if (megasas_ld_list_query(instance,
4666                                   MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4667                 megasas_get_ld_list(instance);
4668
4669         /*
4670          * Compute the max allowed sectors per IO: The controller info has two
4671          * limits on max sectors. Driver should use the minimum of these two.
4672          *
4673          * 1 << stripe_sz_ops.min = max sectors per strip
4674          *
4675          * Note that older firmwares ( < FW ver 30) didn't report information
4676          * to calculate max_sectors_1. So the number ended up as zero always.
4677          */
4678         tmp_sectors = 0;
4679         ctrl_info = instance->ctrl_info;
4680
4681         max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4682                 le16_to_cpu(ctrl_info->max_strips_per_io);
4683         max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4684
4685         tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
4686
4687         instance->disableOnlineCtrlReset =
4688         ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4689         instance->mpio = ctrl_info->adapterOperations2.mpio;
4690         instance->UnevenSpanSupport =
4691                 ctrl_info->adapterOperations2.supportUnevenSpans;
4692         if (instance->UnevenSpanSupport) {
4693                 struct fusion_context *fusion = instance->ctrl_context;
4694                 if (MR_ValidateMapInfo(instance))
4695                         fusion->fast_path_io = 1;
4696                 else
4697                         fusion->fast_path_io = 0;
4698
4699         }
4700         if (ctrl_info->host_interface.SRIOV) {
4701                 if (!ctrl_info->adapterOperations2.activePassive)
4702                         instance->PlasmaFW111 = 1;
4703
4704                 if (!instance->PlasmaFW111)
4705                         instance->requestorId =
4706                                 ctrl_info->iov.requestorId;
4707                 else {
4708                         iovPtr = (struct IOV_111 *)((unsigned char *)ctrl_info + IOV_111_OFFSET);
4709                         instance->requestorId = iovPtr->requestorId;
4710                 }
4711                 dev_warn(&instance->pdev->dev, "I am VF "
4712                        "requestorId %d\n", instance->requestorId);
4713         }
4714
4715         instance->crash_dump_fw_support =
4716                 ctrl_info->adapterOperations3.supportCrashDump;
4717         instance->crash_dump_drv_support =
4718                 (instance->crash_dump_fw_support &&
4719                 instance->crash_dump_buf);
4720         if (instance->crash_dump_drv_support)
4721                 megasas_set_crash_dump_params(instance,
4722                         MR_CRASH_BUF_TURN_OFF);
4723
4724         else {
4725                 if (instance->crash_dump_buf)
4726                         pci_free_consistent(instance->pdev,
4727                                 CRASH_DMA_BUF_SIZE,
4728                                 instance->crash_dump_buf,
4729                                 instance->crash_dump_h);
4730                 instance->crash_dump_buf = NULL;
4731         }
4732
4733         instance->secure_jbod_support =
4734                 ctrl_info->adapterOperations3.supportSecurityonJBOD;
4735
4736         dev_info(&instance->pdev->dev,
4737                 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4738                 le16_to_cpu(ctrl_info->pci.vendor_id),
4739                 le16_to_cpu(ctrl_info->pci.device_id),
4740                 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4741                 le16_to_cpu(ctrl_info->pci.sub_device_id));
4742         dev_info(&instance->pdev->dev, "unevenspan support      : %s\n",
4743                 instance->UnevenSpanSupport ? "yes" : "no");
4744         dev_info(&instance->pdev->dev, "disable ocr             : %s\n",
4745                 instance->disableOnlineCtrlReset ? "yes" : "no");
4746         dev_info(&instance->pdev->dev, "firmware crash dump     : %s\n",
4747                 instance->crash_dump_drv_support ? "yes" : "no");
4748         dev_info(&instance->pdev->dev, "secure jbod             : %s\n",
4749                 instance->secure_jbod_support ? "yes" : "no");
4750
4751
4752         instance->max_sectors_per_req = instance->max_num_sge *
4753                                                 PAGE_SIZE / 512;
4754         if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4755                 instance->max_sectors_per_req = tmp_sectors;
4756
4757         /* Check for valid throttlequeuedepth module parameter */
4758         if (throttlequeuedepth &&
4759                         throttlequeuedepth <= instance->max_scsi_cmds)
4760                 instance->throttlequeuedepth = throttlequeuedepth;
4761         else
4762                 instance->throttlequeuedepth =
4763                                 MEGASAS_THROTTLE_QUEUE_DEPTH;
4764
4765
4766         /* Launch SR-IOV heartbeat timer */
4767         if (instance->requestorId) {
4768                 if (!megasas_sriov_start_heartbeat(instance, 1))
4769                         megasas_start_timer(instance,
4770                                             &instance->sriov_heartbeat_timer,
4771                                             megasas_sriov_heartbeat_handler,
4772                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4773                 else
4774                         instance->skip_heartbeat_timer_del = 1;
4775         }
4776
4777         return 0;
4778
4779 fail_get_pd_list:
4780         instance->instancet->disable_intr(instance);
4781 fail_init_adapter:
4782         megasas_destroy_irqs(instance);
4783 fail_setup_irqs:
4784         if (instance->msix_vectors)
4785                 pci_disable_msix(instance->pdev);
4786         instance->msix_vectors = 0;
4787 fail_ready_state:
4788         kfree(instance->ctrl_info);
4789         instance->ctrl_info = NULL;
4790         iounmap(instance->reg_set);
4791
4792       fail_ioremap:
4793         pci_release_selected_regions(instance->pdev, instance->bar);
4794
4795         return -EINVAL;
4796 }
4797
4798 /**
4799  * megasas_release_mfi -        Reverses the FW initialization
4800  * @instance:                   Adapter soft state
4801  */
4802 static void megasas_release_mfi(struct megasas_instance *instance)
4803 {
4804         u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4805
4806         if (instance->reply_queue)
4807                 pci_free_consistent(instance->pdev, reply_q_sz,
4808                             instance->reply_queue, instance->reply_queue_h);
4809
4810         megasas_free_cmds(instance);
4811
4812         iounmap(instance->reg_set);
4813
4814         pci_release_selected_regions(instance->pdev, instance->bar);
4815 }
4816
4817 /**
4818  * megasas_get_seq_num -        Gets latest event sequence numbers
4819  * @instance:                   Adapter soft state
4820  * @eli:                        FW event log sequence numbers information
4821  *
4822  * FW maintains a log of all events in a non-volatile area. Upper layers would
4823  * usually find out the latest sequence number of the events, the seq number at
4824  * the boot etc. They would "read" all the events below the latest seq number
4825  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4826  * number), they would subsribe to AEN (asynchronous event notification) and
4827  * wait for the events to happen.
4828  */
4829 static int
4830 megasas_get_seq_num(struct megasas_instance *instance,
4831                     struct megasas_evt_log_info *eli)
4832 {
4833         struct megasas_cmd *cmd;
4834         struct megasas_dcmd_frame *dcmd;
4835         struct megasas_evt_log_info *el_info;
4836         dma_addr_t el_info_h = 0;
4837
4838         cmd = megasas_get_cmd(instance);
4839
4840         if (!cmd) {
4841                 return -ENOMEM;
4842         }
4843
4844         dcmd = &cmd->frame->dcmd;
4845         el_info = pci_alloc_consistent(instance->pdev,
4846                                        sizeof(struct megasas_evt_log_info),
4847                                        &el_info_h);
4848
4849         if (!el_info) {
4850                 megasas_return_cmd(instance, cmd);
4851                 return -ENOMEM;
4852         }
4853
4854         memset(el_info, 0, sizeof(*el_info));
4855         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4856
4857         dcmd->cmd = MFI_CMD_DCMD;
4858         dcmd->cmd_status = 0x0;
4859         dcmd->sge_count = 1;
4860         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4861         dcmd->timeout = 0;
4862         dcmd->pad_0 = 0;
4863         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4864         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
4865         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
4866         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
4867
4868         if (megasas_issue_blocked_cmd(instance, cmd, 30))
4869                 dev_err(&instance->pdev->dev, "Command timedout"
4870                         "from %s\n", __func__);
4871         else {
4872                 /*
4873                  * Copy the data back into callers buffer
4874                  */
4875                 eli->newest_seq_num = el_info->newest_seq_num;
4876                 eli->oldest_seq_num = el_info->oldest_seq_num;
4877                 eli->clear_seq_num = el_info->clear_seq_num;
4878                 eli->shutdown_seq_num = el_info->shutdown_seq_num;
4879                 eli->boot_seq_num = el_info->boot_seq_num;
4880         }
4881
4882         pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
4883                             el_info, el_info_h);
4884
4885         megasas_return_cmd(instance, cmd);
4886
4887         return 0;
4888 }
4889
4890 /**
4891  * megasas_register_aen -       Registers for asynchronous event notification
4892  * @instance:                   Adapter soft state
4893  * @seq_num:                    The starting sequence number
4894  * @class_locale:               Class of the event
4895  *
4896  * This function subscribes for AEN for events beyond the @seq_num. It requests
4897  * to be notified if and only if the event is of type @class_locale
4898  */
4899 static int
4900 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
4901                      u32 class_locale_word)
4902 {
4903         int ret_val;
4904         struct megasas_cmd *cmd;
4905         struct megasas_dcmd_frame *dcmd;
4906         union megasas_evt_class_locale curr_aen;
4907         union megasas_evt_class_locale prev_aen;
4908
4909         /*
4910          * If there an AEN pending already (aen_cmd), check if the
4911          * class_locale of that pending AEN is inclusive of the new
4912          * AEN request we currently have. If it is, then we don't have
4913          * to do anything. In other words, whichever events the current
4914          * AEN request is subscribing to, have already been subscribed
4915          * to.
4916          *
4917          * If the old_cmd is _not_ inclusive, then we have to abort
4918          * that command, form a class_locale that is superset of both
4919          * old and current and re-issue to the FW
4920          */
4921
4922         curr_aen.word = class_locale_word;
4923
4924         if (instance->aen_cmd) {
4925
4926                 prev_aen.word =
4927                         le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
4928
4929                 /*
4930                  * A class whose enum value is smaller is inclusive of all
4931                  * higher values. If a PROGRESS (= -1) was previously
4932                  * registered, then a new registration requests for higher
4933                  * classes need not be sent to FW. They are automatically
4934                  * included.
4935                  *
4936                  * Locale numbers don't have such hierarchy. They are bitmap
4937                  * values
4938                  */
4939                 if ((prev_aen.members.class <= curr_aen.members.class) &&
4940                     !((prev_aen.members.locale & curr_aen.members.locale) ^
4941                       curr_aen.members.locale)) {
4942                         /*
4943                          * Previously issued event registration includes
4944                          * current request. Nothing to do.
4945                          */
4946                         return 0;
4947                 } else {
4948                         curr_aen.members.locale |= prev_aen.members.locale;
4949
4950                         if (prev_aen.members.class < curr_aen.members.class)
4951                                 curr_aen.members.class = prev_aen.members.class;
4952
4953                         instance->aen_cmd->abort_aen = 1;
4954                         ret_val = megasas_issue_blocked_abort_cmd(instance,
4955                                                                   instance->
4956                                                                   aen_cmd, 30);
4957
4958                         if (ret_val) {
4959                                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
4960                                        "previous AEN command\n");
4961                                 return ret_val;
4962                         }
4963                 }
4964         }
4965
4966         cmd = megasas_get_cmd(instance);
4967
4968         if (!cmd)
4969                 return -ENOMEM;
4970
4971         dcmd = &cmd->frame->dcmd;
4972
4973         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
4974
4975         /*
4976          * Prepare DCMD for aen registration
4977          */
4978         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4979
4980         dcmd->cmd = MFI_CMD_DCMD;
4981         dcmd->cmd_status = 0x0;
4982         dcmd->sge_count = 1;
4983         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4984         dcmd->timeout = 0;
4985         dcmd->pad_0 = 0;
4986         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
4987         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
4988         dcmd->mbox.w[0] = cpu_to_le32(seq_num);
4989         instance->last_seq_num = seq_num;
4990         dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
4991         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
4992         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
4993
4994         if (instance->aen_cmd != NULL) {
4995                 megasas_return_cmd(instance, cmd);
4996                 return 0;
4997         }
4998
4999         /*
5000          * Store reference to the cmd used to register for AEN. When an
5001          * application wants us to register for AEN, we have to abort this
5002          * cmd and re-register with a new EVENT LOCALE supplied by that app
5003          */
5004         instance->aen_cmd = cmd;
5005
5006         /*
5007          * Issue the aen registration frame
5008          */
5009         instance->instancet->issue_dcmd(instance, cmd);
5010
5011         return 0;
5012 }
5013
5014 /**
5015  * megasas_start_aen -  Subscribes to AEN during driver load time
5016  * @instance:           Adapter soft state
5017  */
5018 static int megasas_start_aen(struct megasas_instance *instance)
5019 {
5020         struct megasas_evt_log_info eli;
5021         union megasas_evt_class_locale class_locale;
5022
5023         /*
5024          * Get the latest sequence number from FW
5025          */
5026         memset(&eli, 0, sizeof(eli));
5027
5028         if (megasas_get_seq_num(instance, &eli))
5029                 return -1;
5030
5031         /*
5032          * Register AEN with FW for latest sequence number plus 1
5033          */
5034         class_locale.members.reserved = 0;
5035         class_locale.members.locale = MR_EVT_LOCALE_ALL;
5036         class_locale.members.class = MR_EVT_CLASS_DEBUG;
5037
5038         return megasas_register_aen(instance,
5039                         le32_to_cpu(eli.newest_seq_num) + 1,
5040                         class_locale.word);
5041 }
5042
5043 /**
5044  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
5045  * @instance:           Adapter soft state
5046  */
5047 static int megasas_io_attach(struct megasas_instance *instance)
5048 {
5049         struct Scsi_Host *host = instance->host;
5050         u32             error;
5051
5052         /*
5053          * Export parameters required by SCSI mid-layer
5054          */
5055         host->irq = instance->pdev->irq;
5056         host->unique_id = instance->unique_id;
5057         host->can_queue = instance->max_scsi_cmds;
5058         host->this_id = instance->init_id;
5059         host->sg_tablesize = instance->max_num_sge;
5060
5061         if (instance->fw_support_ieee)
5062                 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5063
5064         /*
5065          * Check if the module parameter value for max_sectors can be used
5066          */
5067         if (max_sectors && max_sectors < instance->max_sectors_per_req)
5068                 instance->max_sectors_per_req = max_sectors;
5069         else {
5070                 if (max_sectors) {
5071                         if (((instance->pdev->device ==
5072                                 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5073                                 (instance->pdev->device ==
5074                                 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5075                                 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5076                                 instance->max_sectors_per_req = max_sectors;
5077                         } else {
5078                         dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5079                                 "and <= %d (or < 1MB for GEN2 controller)\n",
5080                                 instance->max_sectors_per_req);
5081                         }
5082                 }
5083         }
5084
5085         host->max_sectors = instance->max_sectors_per_req;
5086         host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5087         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5088         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5089         host->max_lun = MEGASAS_MAX_LUN;
5090         host->max_cmd_len = 16;
5091
5092         /* Fusion only supports host reset */
5093         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5094             (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5095             (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5096             (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5097                 host->hostt->eh_device_reset_handler = NULL;
5098                 host->hostt->eh_bus_reset_handler = NULL;
5099         }
5100         error = scsi_init_shared_tag_map(host, host->can_queue);
5101         if (error) {
5102                 dev_err(&instance->pdev->dev,
5103                         "Failed to shared tag from %s %d\n",
5104                         __func__, __LINE__);
5105                 return -ENODEV;
5106         }
5107
5108         /*
5109          * Notify the mid-layer about the new controller
5110          */
5111         if (scsi_add_host(host, &instance->pdev->dev)) {
5112                 dev_err(&instance->pdev->dev,
5113                         "Failed to add host from %s %d\n",
5114                         __func__, __LINE__);
5115                 return -ENODEV;
5116         }
5117
5118         return 0;
5119 }
5120
5121 static int
5122 megasas_set_dma_mask(struct pci_dev *pdev)
5123 {
5124         /*
5125          * All our contollers are capable of performing 64-bit DMA
5126          */
5127         if (IS_DMA64) {
5128                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5129
5130                         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5131                                 goto fail_set_dma_mask;
5132                 }
5133         } else {
5134                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5135                         goto fail_set_dma_mask;
5136         }
5137         /*
5138          * Ensure that all data structures are allocated in 32-bit
5139          * memory.
5140          */
5141         if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5142                 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5143                 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5144                         && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5145                         dev_info(&pdev->dev, "set 32bit DMA mask"
5146                                 "and 32 bit consistent mask\n");
5147                 else
5148                         goto fail_set_dma_mask;
5149         }
5150
5151         return 0;
5152
5153 fail_set_dma_mask:
5154         return 1;
5155 }
5156
5157 /**
5158  * megasas_probe_one -  PCI hotplug entry point
5159  * @pdev:               PCI device structure
5160  * @id:                 PCI ids of supported hotplugged adapter
5161  */
5162 static int megasas_probe_one(struct pci_dev *pdev,
5163                              const struct pci_device_id *id)
5164 {
5165         int rval, pos;
5166         struct Scsi_Host *host;
5167         struct megasas_instance *instance;
5168         u16 control = 0;
5169         struct fusion_context *fusion = NULL;
5170
5171         /* Reset MSI-X in the kdump kernel */
5172         if (reset_devices) {
5173                 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5174                 if (pos) {
5175                         pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5176                                              &control);
5177                         if (control & PCI_MSIX_FLAGS_ENABLE) {
5178                                 dev_info(&pdev->dev, "resetting MSI-X\n");
5179                                 pci_write_config_word(pdev,
5180                                                       pos + PCI_MSIX_FLAGS,
5181                                                       control &
5182                                                       ~PCI_MSIX_FLAGS_ENABLE);
5183                         }
5184                 }
5185         }
5186
5187         /*
5188          * PCI prepping: enable device set bus mastering and dma mask
5189          */
5190         rval = pci_enable_device_mem(pdev);
5191
5192         if (rval) {
5193                 return rval;
5194         }
5195
5196         pci_set_master(pdev);
5197
5198         if (megasas_set_dma_mask(pdev))
5199                 goto fail_set_dma_mask;
5200
5201         host = scsi_host_alloc(&megasas_template,
5202                                sizeof(struct megasas_instance));
5203
5204         if (!host) {
5205                 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5206                 goto fail_alloc_instance;
5207         }
5208
5209         instance = (struct megasas_instance *)host->hostdata;
5210         memset(instance, 0, sizeof(*instance));
5211         atomic_set( &instance->fw_reset_no_pci_access, 0 );
5212         instance->pdev = pdev;
5213
5214         switch (instance->pdev->device) {
5215         case PCI_DEVICE_ID_LSI_FUSION:
5216         case PCI_DEVICE_ID_LSI_PLASMA:
5217         case PCI_DEVICE_ID_LSI_INVADER:
5218         case PCI_DEVICE_ID_LSI_FURY:
5219         {
5220                 instance->ctrl_context_pages =
5221                         get_order(sizeof(struct fusion_context));
5222                 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5223                                 instance->ctrl_context_pages);
5224                 if (!instance->ctrl_context) {
5225                         dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5226                                "memory for Fusion context info\n");
5227                         goto fail_alloc_dma_buf;
5228                 }
5229                 fusion = instance->ctrl_context;
5230                 memset(fusion, 0,
5231                         ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5232         }
5233         break;
5234         default: /* For all other supported controllers */
5235
5236                 instance->producer =
5237                         pci_alloc_consistent(pdev, sizeof(u32),
5238                                              &instance->producer_h);
5239                 instance->consumer =
5240                         pci_alloc_consistent(pdev, sizeof(u32),
5241                                              &instance->consumer_h);
5242
5243                 if (!instance->producer || !instance->consumer) {
5244                         dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5245                                "memory for producer, consumer\n");
5246                         goto fail_alloc_dma_buf;
5247                 }
5248
5249                 *instance->producer = 0;
5250                 *instance->consumer = 0;
5251                 break;
5252         }
5253
5254         instance->system_info_buf = pci_zalloc_consistent(pdev,
5255                                         sizeof(struct MR_DRV_SYSTEM_INFO),
5256                                         &instance->system_info_h);
5257
5258         if (!instance->system_info_buf)
5259                 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5260
5261         /* Crash dump feature related initialisation*/
5262         instance->drv_buf_index = 0;
5263         instance->drv_buf_alloc = 0;
5264         instance->crash_dump_fw_support = 0;
5265         instance->crash_dump_app_support = 0;
5266         instance->fw_crash_state = UNAVAILABLE;
5267         spin_lock_init(&instance->crashdump_lock);
5268         instance->crash_dump_buf = NULL;
5269
5270         if (!reset_devices)
5271                 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5272                                                 CRASH_DMA_BUF_SIZE,
5273                                                 &instance->crash_dump_h);
5274         if (!instance->crash_dump_buf)
5275                 dev_err(&pdev->dev, "Can't allocate Firmware "
5276                         "crash dump DMA buffer\n");
5277
5278         megasas_poll_wait_aen = 0;
5279         instance->flag_ieee = 0;
5280         instance->ev = NULL;
5281         instance->issuepend_done = 1;
5282         instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5283         instance->is_imr = 0;
5284
5285         instance->evt_detail = pci_alloc_consistent(pdev,
5286                                                     sizeof(struct
5287                                                            megasas_evt_detail),
5288                                                     &instance->evt_detail_h);
5289
5290         if (!instance->evt_detail) {
5291                 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5292                        "event detail structure\n");
5293                 goto fail_alloc_dma_buf;
5294         }
5295
5296         /*
5297          * Initialize locks and queues
5298          */
5299         INIT_LIST_HEAD(&instance->cmd_pool);
5300         INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5301
5302         atomic_set(&instance->fw_outstanding,0);
5303
5304         init_waitqueue_head(&instance->int_cmd_wait_q);
5305         init_waitqueue_head(&instance->abort_cmd_wait_q);
5306
5307         spin_lock_init(&instance->mfi_pool_lock);
5308         spin_lock_init(&instance->hba_lock);
5309         spin_lock_init(&instance->completion_lock);
5310
5311         mutex_init(&instance->aen_mutex);
5312         mutex_init(&instance->reset_mutex);
5313
5314         /*
5315          * Initialize PCI related and misc parameters
5316          */
5317         instance->host = host;
5318         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5319         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5320         instance->ctrl_info = NULL;
5321
5322
5323         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5324                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5325                 instance->flag_ieee = 1;
5326
5327         megasas_dbg_lvl = 0;
5328         instance->flag = 0;
5329         instance->unload = 1;
5330         instance->last_time = 0;
5331         instance->disableOnlineCtrlReset = 1;
5332         instance->UnevenSpanSupport = 0;
5333
5334         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5335             (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5336             (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5337             (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
5338                 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5339                 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5340         } else
5341                 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5342
5343         /*
5344          * Initialize MFI Firmware
5345          */
5346         if (megasas_init_fw(instance))
5347                 goto fail_init_mfi;
5348
5349         if (instance->requestorId) {
5350                 if (instance->PlasmaFW111) {
5351                         instance->vf_affiliation_111 =
5352                                 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5353                                                      &instance->vf_affiliation_111_h);
5354                         if (!instance->vf_affiliation_111)
5355                                 dev_warn(&pdev->dev, "Can't allocate "
5356                                        "memory for VF affiliation buffer\n");
5357                 } else {
5358                         instance->vf_affiliation =
5359                                 pci_alloc_consistent(pdev,
5360                                                      (MAX_LOGICAL_DRIVES + 1) *
5361                                                      sizeof(struct MR_LD_VF_AFFILIATION),
5362                                                      &instance->vf_affiliation_h);
5363                         if (!instance->vf_affiliation)
5364                                 dev_warn(&pdev->dev, "Can't allocate "
5365                                        "memory for VF affiliation buffer\n");
5366                 }
5367         }
5368
5369         /*
5370          * Store instance in PCI softstate
5371          */
5372         pci_set_drvdata(pdev, instance);
5373
5374         /*
5375          * Add this controller to megasas_mgmt_info structure so that it
5376          * can be exported to management applications
5377          */
5378         megasas_mgmt_info.count++;
5379         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5380         megasas_mgmt_info.max_index++;
5381
5382         /*
5383          * Register with SCSI mid-layer
5384          */
5385         if (megasas_io_attach(instance))
5386                 goto fail_io_attach;
5387
5388         instance->unload = 0;
5389         /*
5390          * Trigger SCSI to scan our drives
5391          */
5392         scsi_scan_host(host);
5393
5394         /*
5395          * Initiate AEN (Asynchronous Event Notification)
5396          */
5397         if (megasas_start_aen(instance)) {
5398                 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5399                 goto fail_start_aen;
5400         }
5401
5402         /* Get current SR-IOV LD/VF affiliation */
5403         if (instance->requestorId)
5404                 megasas_get_ld_vf_affiliation(instance, 1);
5405
5406         return 0;
5407
5408       fail_start_aen:
5409       fail_io_attach:
5410         megasas_mgmt_info.count--;
5411         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5412         megasas_mgmt_info.max_index--;
5413
5414         instance->instancet->disable_intr(instance);
5415         megasas_destroy_irqs(instance);
5416
5417         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5418             (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) ||
5419             (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
5420             (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
5421                 megasas_release_fusion(instance);
5422         else
5423                 megasas_release_mfi(instance);
5424         if (instance->msix_vectors)
5425                 pci_disable_msix(instance->pdev);
5426 fail_init_mfi:
5427       fail_alloc_dma_buf:
5428         if (instance->evt_detail)
5429                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5430                                     instance->evt_detail,
5431                                     instance->evt_detail_h);
5432
5433         if (instance->producer)
5434                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5435                                     instance->producer_h);
5436         if (instance->consumer)
5437                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5438                                     instance->consumer_h);
5439         scsi_host_put(host);
5440
5441       fail_alloc_instance:
5442       fail_set_dma_mask:
5443         pci_disable_device(pdev);
5444
5445         return -ENODEV;
5446 }
5447
5448 /**
5449  * megasas_flush_cache -        Requests FW to flush all its caches
5450  * @instance:                   Adapter soft state
5451  */
5452 static void megasas_flush_cache(struct megasas_instance *instance)
5453 {
5454         struct megasas_cmd *cmd;
5455         struct megasas_dcmd_frame *dcmd;
5456
5457         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5458                 return;
5459
5460         cmd = megasas_get_cmd(instance);
5461
5462         if (!cmd)
5463                 return;
5464
5465         dcmd = &cmd->frame->dcmd;
5466
5467         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5468
5469         dcmd->cmd = MFI_CMD_DCMD;
5470         dcmd->cmd_status = 0x0;
5471         dcmd->sge_count = 0;
5472         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5473         dcmd->timeout = 0;
5474         dcmd->pad_0 = 0;
5475         dcmd->data_xfer_len = 0;
5476         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5477         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5478
5479         if (megasas_issue_blocked_cmd(instance, cmd, 30))
5480                 dev_err(&instance->pdev->dev, "Command timedout"
5481                         " from %s\n", __func__);
5482
5483         megasas_return_cmd(instance, cmd);
5484
5485         return;
5486 }
5487
5488 /**
5489  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
5490  * @instance:                           Adapter soft state
5491  * @opcode:                             Shutdown/Hibernate
5492  */
5493 static void megasas_shutdown_controller(struct megasas_instance *instance,
5494                                         u32 opcode)
5495 {
5496         struct megasas_cmd *cmd;
5497         struct megasas_dcmd_frame *dcmd;
5498
5499         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5500                 return;
5501
5502         cmd = megasas_get_cmd(instance);
5503
5504         if (!cmd)
5505                 return;
5506
5507         if (instance->aen_cmd)
5508                 megasas_issue_blocked_abort_cmd(instance,
5509                         instance->aen_cmd, 30);
5510         if (instance->map_update_cmd)
5511                 megasas_issue_blocked_abort_cmd(instance,
5512                         instance->map_update_cmd, 30);
5513         dcmd = &cmd->frame->dcmd;
5514
5515         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5516
5517         dcmd->cmd = MFI_CMD_DCMD;
5518         dcmd->cmd_status = 0x0;
5519         dcmd->sge_count = 0;
5520         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5521         dcmd->timeout = 0;
5522         dcmd->pad_0 = 0;
5523         dcmd->data_xfer_len = 0;
5524         dcmd->opcode = cpu_to_le32(opcode);
5525
5526         if (megasas_issue_blocked_cmd(instance, cmd, 30))
5527                 dev_err(&instance->pdev->dev, "Command timedout"
5528                         "from %s\n", __func__);
5529
5530         megasas_return_cmd(instance, cmd);
5531
5532         return;
5533 }
5534
5535 #ifdef CONFIG_PM
5536 /**
5537  * megasas_suspend -    driver suspend entry point
5538  * @pdev:               PCI device structure
5539  * @state:              PCI power state to suspend routine
5540  */
5541 static int
5542 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5543 {
5544         struct Scsi_Host *host;
5545         struct megasas_instance *instance;
5546
5547         instance = pci_get_drvdata(pdev);
5548         host = instance->host;
5549         instance->unload = 1;
5550
5551         /* Shutdown SR-IOV heartbeat timer */
5552         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5553                 del_timer_sync(&instance->sriov_heartbeat_timer);
5554
5555         megasas_flush_cache(instance);
5556         megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5557
5558         /* cancel the delayed work if this work still in queue */
5559         if (instance->ev != NULL) {
5560                 struct megasas_aen_event *ev = instance->ev;
5561                 cancel_delayed_work_sync(&ev->hotplug_work);
5562                 instance->ev = NULL;
5563         }
5564
5565         tasklet_kill(&instance->isr_tasklet);
5566
5567         pci_set_drvdata(instance->pdev, instance);
5568         instance->instancet->disable_intr(instance);
5569
5570         megasas_destroy_irqs(instance);
5571
5572         if (instance->msix_vectors)
5573                 pci_disable_msix(instance->pdev);
5574
5575         pci_save_state(pdev);
5576         pci_disable_device(pdev);
5577
5578         pci_set_power_state(pdev, pci_choose_state(pdev, state));
5579
5580         return 0;
5581 }
5582
5583 /**
5584  * megasas_resume-      driver resume entry point
5585  * @pdev:               PCI device structure
5586  */
5587 static int
5588 megasas_resume(struct pci_dev *pdev)
5589 {
5590         int rval;
5591         struct Scsi_Host *host;
5592         struct megasas_instance *instance;
5593
5594         instance = pci_get_drvdata(pdev);
5595         host = instance->host;
5596         pci_set_power_state(pdev, PCI_D0);
5597         pci_enable_wake(pdev, PCI_D0, 0);
5598         pci_restore_state(pdev);
5599
5600         /*
5601          * PCI prepping: enable device set bus mastering and dma mask
5602          */
5603         rval = pci_enable_device_mem(pdev);
5604
5605         if (rval) {
5606                 dev_err(&pdev->dev, "Enable device failed\n");
5607                 return rval;
5608         }
5609
5610         pci_set_master(pdev);
5611
5612         if (megasas_set_dma_mask(pdev))
5613                 goto fail_set_dma_mask;
5614
5615         /*
5616          * Initialize MFI Firmware
5617          */
5618
5619         atomic_set(&instance->fw_outstanding, 0);
5620
5621         /*
5622          * We expect the FW state to be READY
5623          */
5624         if (megasas_transition_to_ready(instance, 0))
5625                 goto fail_ready_state;
5626
5627         /* Now re-enable MSI-X */
5628         if (instance->msix_vectors &&
5629             pci_enable_msix_exact(instance->pdev, instance->msixentry,
5630                                   instance->msix_vectors))
5631                 goto fail_reenable_msix;
5632
5633         switch (instance->pdev->device) {
5634         case PCI_DEVICE_ID_LSI_FUSION:
5635         case PCI_DEVICE_ID_LSI_PLASMA:
5636         case PCI_DEVICE_ID_LSI_INVADER:
5637         case PCI_DEVICE_ID_LSI_FURY:
5638         {
5639                 megasas_reset_reply_desc(instance);
5640                 if (megasas_ioc_init_fusion(instance)) {
5641                         megasas_free_cmds(instance);
5642                         megasas_free_cmds_fusion(instance);
5643                         goto fail_init_mfi;
5644                 }
5645                 if (!megasas_get_map_info(instance))
5646                         megasas_sync_map_info(instance);
5647         }
5648         break;
5649         default:
5650                 *instance->producer = 0;
5651                 *instance->consumer = 0;
5652                 if (megasas_issue_init_mfi(instance))
5653                         goto fail_init_mfi;
5654                 break;
5655         }
5656
5657         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5658                      (unsigned long)instance);
5659
5660         if (instance->msix_vectors ?
5661                         megasas_setup_irqs_msix(instance, 0) :
5662                         megasas_setup_irqs_ioapic(instance))
5663                 goto fail_init_mfi;
5664
5665         /* Re-launch SR-IOV heartbeat timer */
5666         if (instance->requestorId) {
5667                 if (!megasas_sriov_start_heartbeat(instance, 0))
5668                         megasas_start_timer(instance,
5669                                             &instance->sriov_heartbeat_timer,
5670                                             megasas_sriov_heartbeat_handler,
5671                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5672                 else {
5673                         instance->skip_heartbeat_timer_del = 1;
5674                         goto fail_init_mfi;
5675                 }
5676         }
5677
5678         instance->instancet->enable_intr(instance);
5679         instance->unload = 0;
5680
5681         /*
5682          * Initiate AEN (Asynchronous Event Notification)
5683          */
5684         if (megasas_start_aen(instance))
5685                 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5686
5687         return 0;
5688
5689 fail_init_mfi:
5690         if (instance->evt_detail)
5691                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5692                                 instance->evt_detail,
5693                                 instance->evt_detail_h);
5694
5695         if (instance->producer)
5696                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5697                                 instance->producer_h);
5698         if (instance->consumer)
5699                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5700                                 instance->consumer_h);
5701         scsi_host_put(host);
5702
5703 fail_set_dma_mask:
5704 fail_ready_state:
5705 fail_reenable_msix:
5706
5707         pci_disable_device(pdev);
5708
5709         return -ENODEV;
5710 }
5711 #else
5712 #define megasas_suspend NULL
5713 #define megasas_resume  NULL
5714 #endif
5715
5716 /**
5717  * megasas_detach_one - PCI hot"un"plug entry point
5718  * @pdev:               PCI device structure
5719  */
5720 static void megasas_detach_one(struct pci_dev *pdev)
5721 {
5722         int i;
5723         struct Scsi_Host *host;
5724         struct megasas_instance *instance;
5725         struct fusion_context *fusion;
5726
5727         instance = pci_get_drvdata(pdev);
5728         instance->unload = 1;
5729         host = instance->host;
5730         fusion = instance->ctrl_context;
5731
5732         /* Shutdown SR-IOV heartbeat timer */
5733         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5734                 del_timer_sync(&instance->sriov_heartbeat_timer);
5735
5736         if (instance->fw_crash_state != UNAVAILABLE)
5737                 megasas_free_host_crash_buffer(instance);
5738         scsi_remove_host(instance->host);
5739         megasas_flush_cache(instance);
5740         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5741
5742         /* cancel the delayed work if this work still in queue*/
5743         if (instance->ev != NULL) {
5744                 struct megasas_aen_event *ev = instance->ev;
5745                 cancel_delayed_work_sync(&ev->hotplug_work);
5746                 instance->ev = NULL;
5747         }
5748
5749         /* cancel all wait events */
5750         wake_up_all(&instance->int_cmd_wait_q);
5751
5752         tasklet_kill(&instance->isr_tasklet);
5753
5754         /*
5755          * Take the instance off the instance array. Note that we will not
5756          * decrement the max_index. We let this array be sparse array
5757          */
5758         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5759                 if (megasas_mgmt_info.instance[i] == instance) {
5760                         megasas_mgmt_info.count--;
5761                         megasas_mgmt_info.instance[i] = NULL;
5762
5763                         break;
5764                 }
5765         }
5766
5767         instance->instancet->disable_intr(instance);
5768
5769         megasas_destroy_irqs(instance);
5770
5771         if (instance->msix_vectors)
5772                 pci_disable_msix(instance->pdev);
5773
5774         switch (instance->pdev->device) {
5775         case PCI_DEVICE_ID_LSI_FUSION:
5776         case PCI_DEVICE_ID_LSI_PLASMA:
5777         case PCI_DEVICE_ID_LSI_INVADER:
5778         case PCI_DEVICE_ID_LSI_FURY:
5779                 megasas_release_fusion(instance);
5780                 for (i = 0; i < 2 ; i++) {
5781                         if (fusion->ld_map[i])
5782                                 dma_free_coherent(&instance->pdev->dev,
5783                                                   fusion->max_map_sz,
5784                                                   fusion->ld_map[i],
5785                                                   fusion->ld_map_phys[i]);
5786                         if (fusion->ld_drv_map[i])
5787                                 free_pages((ulong)fusion->ld_drv_map[i],
5788                                         fusion->drv_map_pages);
5789                 }
5790                 free_pages((ulong)instance->ctrl_context,
5791                         instance->ctrl_context_pages);
5792                 break;
5793         default:
5794                 megasas_release_mfi(instance);
5795                 pci_free_consistent(pdev, sizeof(u32),
5796                                     instance->producer,
5797                                     instance->producer_h);
5798                 pci_free_consistent(pdev, sizeof(u32),
5799                                     instance->consumer,
5800                                     instance->consumer_h);
5801                 break;
5802         }
5803
5804         kfree(instance->ctrl_info);
5805
5806         if (instance->evt_detail)
5807                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5808                                 instance->evt_detail, instance->evt_detail_h);
5809
5810         if (instance->vf_affiliation)
5811                 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5812                                     sizeof(struct MR_LD_VF_AFFILIATION),
5813                                     instance->vf_affiliation,
5814                                     instance->vf_affiliation_h);
5815
5816         if (instance->vf_affiliation_111)
5817                 pci_free_consistent(pdev,
5818                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
5819                                     instance->vf_affiliation_111,
5820                                     instance->vf_affiliation_111_h);
5821
5822         if (instance->hb_host_mem)
5823                 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5824                                     instance->hb_host_mem,
5825                                     instance->hb_host_mem_h);
5826
5827         if (instance->crash_dump_buf)
5828                 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5829                             instance->crash_dump_buf, instance->crash_dump_h);
5830
5831         if (instance->system_info_buf)
5832                 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5833                                     instance->system_info_buf, instance->system_info_h);
5834
5835         scsi_host_put(host);
5836
5837         pci_disable_device(pdev);
5838
5839         return;
5840 }
5841
5842 /**
5843  * megasas_shutdown -   Shutdown entry point
5844  * @device:             Generic device structure
5845  */
5846 static void megasas_shutdown(struct pci_dev *pdev)
5847 {
5848         struct megasas_instance *instance = pci_get_drvdata(pdev);
5849
5850         instance->unload = 1;
5851         megasas_flush_cache(instance);
5852         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5853         instance->instancet->disable_intr(instance);
5854         megasas_destroy_irqs(instance);
5855
5856         if (instance->msix_vectors)
5857                 pci_disable_msix(instance->pdev);
5858 }
5859
5860 /**
5861  * megasas_mgmt_open -  char node "open" entry point
5862  */
5863 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
5864 {
5865         /*
5866          * Allow only those users with admin rights
5867          */
5868         if (!capable(CAP_SYS_ADMIN))
5869                 return -EACCES;
5870
5871         return 0;
5872 }
5873
5874 /**
5875  * megasas_mgmt_fasync -        Async notifier registration from applications
5876  *
5877  * This function adds the calling process to a driver global queue. When an
5878  * event occurs, SIGIO will be sent to all processes in this queue.
5879  */
5880 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
5881 {
5882         int rc;
5883
5884         mutex_lock(&megasas_async_queue_mutex);
5885
5886         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
5887
5888         mutex_unlock(&megasas_async_queue_mutex);
5889
5890         if (rc >= 0) {
5891                 /* For sanity check when we get ioctl */
5892                 filep->private_data = filep;
5893                 return 0;
5894         }
5895
5896         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
5897
5898         return rc;
5899 }
5900
5901 /**
5902  * megasas_mgmt_poll -  char node "poll" entry point
5903  * */
5904 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
5905 {
5906         unsigned int mask;
5907         unsigned long flags;
5908         poll_wait(file, &megasas_poll_wait, wait);
5909         spin_lock_irqsave(&poll_aen_lock, flags);
5910         if (megasas_poll_wait_aen)
5911                 mask =   (POLLIN | POLLRDNORM);
5912
5913         else
5914                 mask = 0;
5915         megasas_poll_wait_aen = 0;
5916         spin_unlock_irqrestore(&poll_aen_lock, flags);
5917         return mask;
5918 }
5919
5920 /*
5921  * megasas_set_crash_dump_params_ioctl:
5922  *              Send CRASH_DUMP_MODE DCMD to all controllers
5923  * @cmd:        MFI command frame
5924  */
5925
5926 static int megasas_set_crash_dump_params_ioctl(
5927         struct megasas_cmd *cmd)
5928 {
5929         struct megasas_instance *local_instance;
5930         int i, error = 0;
5931         int crash_support;
5932
5933         crash_support = cmd->frame->dcmd.mbox.w[0];
5934
5935         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5936                 local_instance = megasas_mgmt_info.instance[i];
5937                 if (local_instance && local_instance->crash_dump_drv_support) {
5938                         if ((local_instance->adprecovery ==
5939                                 MEGASAS_HBA_OPERATIONAL) &&
5940                                 !megasas_set_crash_dump_params(local_instance,
5941                                         crash_support)) {
5942                                 local_instance->crash_dump_app_support =
5943                                         crash_support;
5944                                 dev_info(&local_instance->pdev->dev,
5945                                         "Application firmware crash "
5946                                         "dump mode set success\n");
5947                                 error = 0;
5948                         } else {
5949                                 dev_info(&local_instance->pdev->dev,
5950                                         "Application firmware crash "
5951                                         "dump mode set failed\n");
5952                                 error = -1;
5953                         }
5954                 }
5955         }
5956         return error;
5957 }
5958
5959 /**
5960  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
5961  * @instance:                   Adapter soft state
5962  * @argp:                       User's ioctl packet
5963  */
5964 static int
5965 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
5966                       struct megasas_iocpacket __user * user_ioc,
5967                       struct megasas_iocpacket *ioc)
5968 {
5969         struct megasas_sge32 *kern_sge32;
5970         struct megasas_cmd *cmd;
5971         void *kbuff_arr[MAX_IOCTL_SGE];
5972         dma_addr_t buf_handle = 0;
5973         int error = 0, i;
5974         void *sense = NULL;
5975         dma_addr_t sense_handle;
5976         unsigned long *sense_ptr;
5977
5978         memset(kbuff_arr, 0, sizeof(kbuff_arr));
5979
5980         if (ioc->sge_count > MAX_IOCTL_SGE) {
5981                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] >  max limit [%d]\n",
5982                        ioc->sge_count, MAX_IOCTL_SGE);
5983                 return -EINVAL;
5984         }
5985
5986         cmd = megasas_get_cmd(instance);
5987         if (!cmd) {
5988                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
5989                 return -ENOMEM;
5990         }
5991
5992         /*
5993          * User's IOCTL packet has 2 frames (maximum). Copy those two
5994          * frames into our cmd's frames. cmd->frame's context will get
5995          * overwritten when we copy from user's frames. So set that value
5996          * alone separately
5997          */
5998         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
5999         cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6000         cmd->frame->hdr.pad_0 = 0;
6001         cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6002                                                MFI_FRAME_SGL64 |
6003                                                MFI_FRAME_SENSE64));
6004
6005         if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6006                 error = megasas_set_crash_dump_params_ioctl(cmd);
6007                 megasas_return_cmd(instance, cmd);
6008                 return error;
6009         }
6010
6011         /*
6012          * The management interface between applications and the fw uses
6013          * MFI frames. E.g, RAID configuration changes, LD property changes
6014          * etc are accomplishes through different kinds of MFI frames. The
6015          * driver needs to care only about substituting user buffers with
6016          * kernel buffers in SGLs. The location of SGL is embedded in the
6017          * struct iocpacket itself.
6018          */
6019         kern_sge32 = (struct megasas_sge32 *)
6020             ((unsigned long)cmd->frame + ioc->sgl_off);
6021
6022         /*
6023          * For each user buffer, create a mirror buffer and copy in
6024          */
6025         for (i = 0; i < ioc->sge_count; i++) {
6026                 if (!ioc->sgl[i].iov_len)
6027                         continue;
6028
6029                 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6030                                                     ioc->sgl[i].iov_len,
6031                                                     &buf_handle, GFP_KERNEL);
6032                 if (!kbuff_arr[i]) {
6033                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6034                                "kernel SGL buffer for IOCTL\n");
6035                         error = -ENOMEM;
6036                         goto out;
6037                 }
6038
6039                 /*
6040                  * We don't change the dma_coherent_mask, so
6041                  * pci_alloc_consistent only returns 32bit addresses
6042                  */
6043                 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6044                 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6045
6046                 /*
6047                  * We created a kernel buffer corresponding to the
6048                  * user buffer. Now copy in from the user buffer
6049                  */
6050                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6051                                    (u32) (ioc->sgl[i].iov_len))) {
6052                         error = -EFAULT;
6053                         goto out;
6054                 }
6055         }
6056
6057         if (ioc->sense_len) {
6058                 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6059                                              &sense_handle, GFP_KERNEL);
6060                 if (!sense) {
6061                         error = -ENOMEM;
6062                         goto out;
6063                 }
6064
6065                 sense_ptr =
6066                 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6067                 *sense_ptr = cpu_to_le32(sense_handle);
6068         }
6069
6070         /*
6071          * Set the sync_cmd flag so that the ISR knows not to complete this
6072          * cmd to the SCSI mid-layer
6073          */
6074         cmd->sync_cmd = 1;
6075         megasas_issue_blocked_cmd(instance, cmd, 0);
6076         cmd->sync_cmd = 0;
6077
6078         if (instance->unload == 1) {
6079                 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6080                         "don't submit data to application\n");
6081                 goto out;
6082         }
6083         /*
6084          * copy out the kernel buffers to user buffers
6085          */
6086         for (i = 0; i < ioc->sge_count; i++) {
6087                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6088                                  ioc->sgl[i].iov_len)) {
6089                         error = -EFAULT;
6090                         goto out;
6091                 }
6092         }
6093
6094         /*
6095          * copy out the sense
6096          */
6097         if (ioc->sense_len) {
6098                 /*
6099                  * sense_ptr points to the location that has the user
6100                  * sense buffer address
6101                  */
6102                 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6103                                 ioc->sense_off);
6104
6105                 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6106                                  sense, ioc->sense_len)) {
6107                         dev_err(&instance->pdev->dev, "Failed to copy out to user "
6108                                         "sense data\n");
6109                         error = -EFAULT;
6110                         goto out;
6111                 }
6112         }
6113
6114         /*
6115          * copy the status codes returned by the fw
6116          */
6117         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6118                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6119                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6120                 error = -EFAULT;
6121         }
6122
6123       out:
6124         if (sense) {
6125                 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6126                                     sense, sense_handle);
6127         }
6128
6129         for (i = 0; i < ioc->sge_count; i++) {
6130                 if (kbuff_arr[i])
6131                         dma_free_coherent(&instance->pdev->dev,
6132                                           le32_to_cpu(kern_sge32[i].length),
6133                                           kbuff_arr[i],
6134                                           le32_to_cpu(kern_sge32[i].phys_addr));
6135                         kbuff_arr[i] = NULL;
6136         }
6137
6138         megasas_return_cmd(instance, cmd);
6139         return error;
6140 }
6141
6142 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6143 {
6144         struct megasas_iocpacket __user *user_ioc =
6145             (struct megasas_iocpacket __user *)arg;
6146         struct megasas_iocpacket *ioc;
6147         struct megasas_instance *instance;
6148         int error;
6149         int i;
6150         unsigned long flags;
6151         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6152
6153         ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6154         if (!ioc)
6155                 return -ENOMEM;
6156
6157         if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6158                 error = -EFAULT;
6159                 goto out_kfree_ioc;
6160         }
6161
6162         instance = megasas_lookup_instance(ioc->host_no);
6163         if (!instance) {
6164                 error = -ENODEV;
6165                 goto out_kfree_ioc;
6166         }
6167
6168         /* Adjust ioctl wait time for VF mode */
6169         if (instance->requestorId)
6170                 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6171
6172         /* Block ioctls in VF mode */
6173         if (instance->requestorId && !allow_vf_ioctls) {
6174                 error = -ENODEV;
6175                 goto out_kfree_ioc;
6176         }
6177
6178         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6179                 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6180                 error = -ENODEV;
6181                 goto out_kfree_ioc;
6182         }
6183
6184         if (instance->unload == 1) {
6185                 error = -ENODEV;
6186                 goto out_kfree_ioc;
6187         }
6188
6189         if (down_interruptible(&instance->ioctl_sem)) {
6190                 error = -ERESTARTSYS;
6191                 goto out_kfree_ioc;
6192         }
6193
6194         for (i = 0; i < wait_time; i++) {
6195
6196                 spin_lock_irqsave(&instance->hba_lock, flags);
6197                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6198                         spin_unlock_irqrestore(&instance->hba_lock, flags);
6199                         break;
6200                 }
6201                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6202
6203                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6204                         dev_notice(&instance->pdev->dev, "waiting"
6205                                 "for controller reset to finish\n");
6206                 }
6207
6208                 msleep(1000);
6209         }
6210
6211         spin_lock_irqsave(&instance->hba_lock, flags);
6212         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6213                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6214
6215                 dev_err(&instance->pdev->dev, "timed out while"
6216                         "waiting for HBA to recover\n");
6217                 error = -ENODEV;
6218                 goto out_up;
6219         }
6220         spin_unlock_irqrestore(&instance->hba_lock, flags);
6221
6222         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6223       out_up:
6224         up(&instance->ioctl_sem);
6225
6226       out_kfree_ioc:
6227         kfree(ioc);
6228         return error;
6229 }
6230
6231 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6232 {
6233         struct megasas_instance *instance;
6234         struct megasas_aen aen;
6235         int error;
6236         int i;
6237         unsigned long flags;
6238         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6239
6240         if (file->private_data != file) {
6241                 printk(KERN_DEBUG "megasas: fasync_helper was not "
6242                        "called first\n");
6243                 return -EINVAL;
6244         }
6245
6246         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6247                 return -EFAULT;
6248
6249         instance = megasas_lookup_instance(aen.host_no);
6250
6251         if (!instance)
6252                 return -ENODEV;
6253
6254         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6255                 return -ENODEV;
6256         }
6257
6258         if (instance->unload == 1) {
6259                 return -ENODEV;
6260         }
6261
6262         for (i = 0; i < wait_time; i++) {
6263
6264                 spin_lock_irqsave(&instance->hba_lock, flags);
6265                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6266                         spin_unlock_irqrestore(&instance->hba_lock,
6267                                                 flags);
6268                         break;
6269                 }
6270
6271                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6272
6273                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6274                         dev_notice(&instance->pdev->dev, "waiting for"
6275                                 "controller reset to finish\n");
6276                 }
6277
6278                 msleep(1000);
6279         }
6280
6281         spin_lock_irqsave(&instance->hba_lock, flags);
6282         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6283                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6284                 dev_err(&instance->pdev->dev, "timed out while waiting"
6285                                 "for HBA to recover\n");
6286                 return -ENODEV;
6287         }
6288         spin_unlock_irqrestore(&instance->hba_lock, flags);
6289
6290         mutex_lock(&instance->aen_mutex);
6291         error = megasas_register_aen(instance, aen.seq_num,
6292                                      aen.class_locale_word);
6293         mutex_unlock(&instance->aen_mutex);
6294         return error;
6295 }
6296
6297 /**
6298  * megasas_mgmt_ioctl - char node ioctl entry point
6299  */
6300 static long
6301 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6302 {
6303         switch (cmd) {
6304         case MEGASAS_IOC_FIRMWARE:
6305                 return megasas_mgmt_ioctl_fw(file, arg);
6306
6307         case MEGASAS_IOC_GET_AEN:
6308                 return megasas_mgmt_ioctl_aen(file, arg);
6309         }
6310
6311         return -ENOTTY;
6312 }
6313
6314 #ifdef CONFIG_COMPAT
6315 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6316 {
6317         struct compat_megasas_iocpacket __user *cioc =
6318             (struct compat_megasas_iocpacket __user *)arg;
6319         struct megasas_iocpacket __user *ioc =
6320             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6321         int i;
6322         int error = 0;
6323         compat_uptr_t ptr;
6324
6325         if (clear_user(ioc, sizeof(*ioc)))
6326                 return -EFAULT;
6327
6328         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6329             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6330             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6331             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6332             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6333             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6334                 return -EFAULT;
6335
6336         /*
6337          * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6338          * sense_len is not null, so prepare the 64bit value under
6339          * the same condition.
6340          */
6341         if (ioc->sense_len) {
6342                 void __user **sense_ioc_ptr =
6343                         (void __user **)(ioc->frame.raw + ioc->sense_off);
6344                 compat_uptr_t *sense_cioc_ptr =
6345                         (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
6346                 if (get_user(ptr, sense_cioc_ptr) ||
6347                     put_user(compat_ptr(ptr), sense_ioc_ptr))
6348                         return -EFAULT;
6349         }
6350
6351         for (i = 0; i < MAX_IOCTL_SGE; i++) {
6352                 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
6353                     put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
6354                     copy_in_user(&ioc->sgl[i].iov_len,
6355                                  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
6356                         return -EFAULT;
6357         }
6358
6359         error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
6360
6361         if (copy_in_user(&cioc->frame.hdr.cmd_status,
6362                          &ioc->frame.hdr.cmd_status, sizeof(u8))) {
6363                 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
6364                 return -EFAULT;
6365         }
6366         return error;
6367 }
6368
6369 static long
6370 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
6371                           unsigned long arg)
6372 {
6373         switch (cmd) {
6374         case MEGASAS_IOC_FIRMWARE32:
6375                 return megasas_mgmt_compat_ioctl_fw(file, arg);
6376         case MEGASAS_IOC_GET_AEN:
6377                 return megasas_mgmt_ioctl_aen(file, arg);
6378         }
6379
6380         return -ENOTTY;
6381 }
6382 #endif
6383
6384 /*
6385  * File operations structure for management interface
6386  */
6387 static const struct file_operations megasas_mgmt_fops = {
6388         .owner = THIS_MODULE,
6389         .open = megasas_mgmt_open,
6390         .fasync = megasas_mgmt_fasync,
6391         .unlocked_ioctl = megasas_mgmt_ioctl,
6392         .poll = megasas_mgmt_poll,
6393 #ifdef CONFIG_COMPAT
6394         .compat_ioctl = megasas_mgmt_compat_ioctl,
6395 #endif
6396         .llseek = noop_llseek,
6397 };
6398
6399 /*
6400  * PCI hotplug support registration structure
6401  */
6402 static struct pci_driver megasas_pci_driver = {
6403
6404         .name = "megaraid_sas",
6405         .id_table = megasas_pci_table,
6406         .probe = megasas_probe_one,
6407         .remove = megasas_detach_one,
6408         .suspend = megasas_suspend,
6409         .resume = megasas_resume,
6410         .shutdown = megasas_shutdown,
6411 };
6412
6413 /*
6414  * Sysfs driver attributes
6415  */
6416 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
6417 {
6418         return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
6419                         MEGASAS_VERSION);
6420 }
6421
6422 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
6423
6424 static ssize_t
6425 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
6426 {
6427         return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
6428                 MEGASAS_RELDATE);
6429 }
6430
6431 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, NULL);
6432
6433 static ssize_t
6434 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
6435 {
6436         return sprintf(buf, "%u\n", support_poll_for_event);
6437 }
6438
6439 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
6440                         megasas_sysfs_show_support_poll_for_event, NULL);
6441
6442  static ssize_t
6443 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
6444 {
6445         return sprintf(buf, "%u\n", support_device_change);
6446 }
6447
6448 static DRIVER_ATTR(support_device_change, S_IRUGO,
6449                         megasas_sysfs_show_support_device_change, NULL);
6450
6451 static ssize_t
6452 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
6453 {
6454         return sprintf(buf, "%u\n", megasas_dbg_lvl);
6455 }
6456
6457 static ssize_t
6458 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
6459 {
6460         int retval = count;
6461         if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
6462                 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
6463                 retval = -EINVAL;
6464         }
6465         return retval;
6466 }
6467
6468 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
6469                 megasas_sysfs_set_dbg_lvl);
6470
6471 static void
6472 megasas_aen_polling(struct work_struct *work)
6473 {
6474         struct megasas_aen_event *ev =
6475                 container_of(work, struct megasas_aen_event, hotplug_work.work);
6476         struct megasas_instance *instance = ev->instance;
6477         union megasas_evt_class_locale class_locale;
6478         struct  Scsi_Host *host;
6479         struct  scsi_device *sdev1;
6480         u16     pd_index = 0;
6481         u16     ld_index = 0;
6482         int     i, j, doscan = 0;
6483         u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
6484         int error;
6485
6486         if (!instance) {
6487                 printk(KERN_ERR "invalid instance!\n");
6488                 kfree(ev);
6489                 return;
6490         }
6491
6492         /* Adjust event workqueue thread wait time for VF mode */
6493         if (instance->requestorId)
6494                 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6495
6496         /* Don't run the event workqueue thread if OCR is running */
6497         for (i = 0; i < wait_time; i++) {
6498                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL)
6499                         break;
6500                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6501                         dev_notice(&instance->pdev->dev, "%s waiting for "
6502                                "controller reset to finish for scsi%d\n",
6503                                __func__, instance->host->host_no);
6504                 }
6505                 msleep(1000);
6506         }
6507
6508         instance->ev = NULL;
6509         host = instance->host;
6510         if (instance->evt_detail) {
6511
6512                 switch (le32_to_cpu(instance->evt_detail->code)) {
6513                 case MR_EVT_PD_INSERTED:
6514                         if (megasas_get_pd_list(instance) == 0) {
6515                         for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6516                                 for (j = 0;
6517                                 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6518                                 j++) {
6519
6520                                 pd_index =
6521                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6522
6523                                 sdev1 =
6524                                 scsi_device_lookup(host, i, j, 0);
6525
6526                                 if (instance->pd_list[pd_index].driveState
6527                                                 == MR_PD_STATE_SYSTEM) {
6528                                                 if (!sdev1) {
6529                                                 scsi_add_device(host, i, j, 0);
6530                                                 }
6531
6532                                         if (sdev1)
6533                                                 scsi_device_put(sdev1);
6534                                         }
6535                                 }
6536                         }
6537                         }
6538                         doscan = 0;
6539                         break;
6540
6541                 case MR_EVT_PD_REMOVED:
6542                         if (megasas_get_pd_list(instance) == 0) {
6543                         for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6544                                 for (j = 0;
6545                                 j < MEGASAS_MAX_DEV_PER_CHANNEL;
6546                                 j++) {
6547
6548                                 pd_index =
6549                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6550
6551                                 sdev1 =
6552                                 scsi_device_lookup(host, i, j, 0);
6553
6554                                 if (instance->pd_list[pd_index].driveState
6555                                         == MR_PD_STATE_SYSTEM) {
6556                                         if (sdev1) {
6557                                                 scsi_device_put(sdev1);
6558                                         }
6559                                 } else {
6560                                         if (sdev1) {
6561                                                 scsi_remove_device(sdev1);
6562                                                 scsi_device_put(sdev1);
6563                                         }
6564                                 }
6565                                 }
6566                         }
6567                         }
6568                         doscan = 0;
6569                         break;
6570
6571                 case MR_EVT_LD_OFFLINE:
6572                 case MR_EVT_CFG_CLEARED:
6573                 case MR_EVT_LD_DELETED:
6574                         if (!instance->requestorId ||
6575                             (instance->requestorId &&
6576                              megasas_get_ld_vf_affiliation(instance, 0))) {
6577                                 if (megasas_ld_list_query(instance,
6578                                                           MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6579                                         megasas_get_ld_list(instance);
6580                                 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6581                                         for (j = 0;
6582                                              j < MEGASAS_MAX_DEV_PER_CHANNEL;
6583                                              j++) {
6584
6585                                                 ld_index =
6586                                                         (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6587
6588                                                 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6589
6590                                                 if (instance->ld_ids[ld_index]
6591                                                     != 0xff) {
6592                                                         if (sdev1)
6593                                                                 scsi_device_put(sdev1);
6594                                                 } else {
6595                                                         if (sdev1) {
6596                                                                 scsi_remove_device(sdev1);
6597                                                                 scsi_device_put(sdev1);
6598                                                         }
6599                                                 }
6600                                         }
6601                                 }
6602                                 doscan = 0;
6603                         }
6604                         break;
6605                 case MR_EVT_LD_CREATED:
6606                         if (!instance->requestorId ||
6607                             (instance->requestorId &&
6608                              megasas_get_ld_vf_affiliation(instance, 0))) {
6609                                 if (megasas_ld_list_query(instance,
6610                                                           MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6611                                         megasas_get_ld_list(instance);
6612                                 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6613                                         for (j = 0;
6614                                              j < MEGASAS_MAX_DEV_PER_CHANNEL;
6615                                              j++) {
6616                                                 ld_index =
6617                                                         (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6618
6619                                                 sdev1 = scsi_device_lookup(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6620
6621                                                 if (instance->ld_ids[ld_index]
6622                                                     != 0xff) {
6623                                                         if (!sdev1)
6624                                                                 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6625                                                 }
6626                                                 if (sdev1)
6627                                                         scsi_device_put(sdev1);
6628                                         }
6629                                 }
6630                                 doscan = 0;
6631                         }
6632                         break;
6633                 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
6634                 case MR_EVT_FOREIGN_CFG_IMPORTED:
6635                 case MR_EVT_LD_STATE_CHANGE:
6636                         doscan = 1;
6637                         break;
6638                 default:
6639                         doscan = 0;
6640                         break;
6641                 }
6642         } else {
6643                 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
6644                 kfree(ev);
6645                 return;
6646         }
6647
6648         if (doscan) {
6649                 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
6650                        instance->host->host_no);
6651                 if (megasas_get_pd_list(instance) == 0) {
6652                         for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
6653                                 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
6654                                         pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
6655                                         sdev1 = scsi_device_lookup(host, i, j, 0);
6656                                         if (instance->pd_list[pd_index].driveState ==
6657                                             MR_PD_STATE_SYSTEM) {
6658                                                 if (!sdev1) {
6659                                                         scsi_add_device(host, i, j, 0);
6660                                                 }
6661                                                 if (sdev1)
6662                                                         scsi_device_put(sdev1);
6663                                         } else {
6664                                                 if (sdev1) {
6665                                                         scsi_remove_device(sdev1);
6666                                                         scsi_device_put(sdev1);
6667                                                 }
6668                                         }
6669                                 }
6670                         }
6671                 }
6672
6673                 if (!instance->requestorId ||
6674                     (instance->requestorId &&
6675                      megasas_get_ld_vf_affiliation(instance, 0))) {
6676                         if (megasas_ld_list_query(instance,
6677                                                   MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
6678                                 megasas_get_ld_list(instance);
6679                         for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
6680                                 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL;
6681                                      j++) {
6682                                         ld_index =
6683                                                 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
6684
6685                                         sdev1 = scsi_device_lookup(host,
6686                                                                    MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6687                                         if (instance->ld_ids[ld_index]
6688                                             != 0xff) {
6689                                                 if (!sdev1)
6690                                                         scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
6691                                                 else
6692                                                         scsi_device_put(sdev1);
6693                                         } else {
6694                                                 if (sdev1) {
6695                                                         scsi_remove_device(sdev1);
6696                                                         scsi_device_put(sdev1);
6697                                                 }
6698                                         }
6699                                 }
6700                         }
6701                 }
6702         }
6703
6704         if ( instance->aen_cmd != NULL ) {
6705                 kfree(ev);
6706                 return ;
6707         }
6708
6709         seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
6710
6711         /* Register AEN with FW for latest sequence number plus 1 */
6712         class_locale.members.reserved = 0;
6713         class_locale.members.locale = MR_EVT_LOCALE_ALL;
6714         class_locale.members.class = MR_EVT_CLASS_DEBUG;
6715         mutex_lock(&instance->aen_mutex);
6716         error = megasas_register_aen(instance, seq_num,
6717                                         class_locale.word);
6718         mutex_unlock(&instance->aen_mutex);
6719
6720         if (error)
6721                 dev_err(&instance->pdev->dev, "register aen failed error %x\n", error);
6722
6723         kfree(ev);
6724 }
6725
6726 /**
6727  * megasas_init - Driver load entry point
6728  */
6729 static int __init megasas_init(void)
6730 {
6731         int rval;
6732
6733         /*
6734          * Announce driver version and other information
6735          */
6736         pr_info("megasas: %s\n", MEGASAS_VERSION);
6737
6738         spin_lock_init(&poll_aen_lock);
6739
6740         support_poll_for_event = 2;
6741         support_device_change = 1;
6742
6743         memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
6744
6745         /*
6746          * Register character device node
6747          */
6748         rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
6749
6750         if (rval < 0) {
6751                 printk(KERN_DEBUG "megasas: failed to open device node\n");
6752                 return rval;
6753         }
6754
6755         megasas_mgmt_majorno = rval;
6756
6757         /*
6758          * Register ourselves as PCI hotplug module
6759          */
6760         rval = pci_register_driver(&megasas_pci_driver);
6761
6762         if (rval) {
6763                 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
6764                 goto err_pcidrv;
6765         }
6766
6767         rval = driver_create_file(&megasas_pci_driver.driver,
6768                                   &driver_attr_version);
6769         if (rval)
6770                 goto err_dcf_attr_ver;
6771
6772         rval = driver_create_file(&megasas_pci_driver.driver,
6773                                   &driver_attr_release_date);
6774         if (rval)
6775                 goto err_dcf_rel_date;
6776
6777         rval = driver_create_file(&megasas_pci_driver.driver,
6778                                 &driver_attr_support_poll_for_event);
6779         if (rval)
6780                 goto err_dcf_support_poll_for_event;
6781
6782         rval = driver_create_file(&megasas_pci_driver.driver,
6783                                   &driver_attr_dbg_lvl);
6784         if (rval)
6785                 goto err_dcf_dbg_lvl;
6786         rval = driver_create_file(&megasas_pci_driver.driver,
6787                                 &driver_attr_support_device_change);
6788         if (rval)
6789                 goto err_dcf_support_device_change;
6790
6791         return rval;
6792
6793 err_dcf_support_device_change:
6794         driver_remove_file(&megasas_pci_driver.driver,
6795                            &driver_attr_dbg_lvl);
6796 err_dcf_dbg_lvl:
6797         driver_remove_file(&megasas_pci_driver.driver,
6798                         &driver_attr_support_poll_for_event);
6799 err_dcf_support_poll_for_event:
6800         driver_remove_file(&megasas_pci_driver.driver,
6801                            &driver_attr_release_date);
6802 err_dcf_rel_date:
6803         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6804 err_dcf_attr_ver:
6805         pci_unregister_driver(&megasas_pci_driver);
6806 err_pcidrv:
6807         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6808         return rval;
6809 }
6810
6811 /**
6812  * megasas_exit - Driver unload entry point
6813  */
6814 static void __exit megasas_exit(void)
6815 {
6816         driver_remove_file(&megasas_pci_driver.driver,
6817                            &driver_attr_dbg_lvl);
6818         driver_remove_file(&megasas_pci_driver.driver,
6819                         &driver_attr_support_poll_for_event);
6820         driver_remove_file(&megasas_pci_driver.driver,
6821                         &driver_attr_support_device_change);
6822         driver_remove_file(&megasas_pci_driver.driver,
6823                            &driver_attr_release_date);
6824         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
6825
6826         pci_unregister_driver(&megasas_pci_driver);
6827         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
6828 }
6829
6830 module_init(megasas_init);
6831 module_exit(megasas_exit);