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