Merge tag 'pinctrl-v3.19-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[cascardo/linux.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2009-2013  LSI Corporation
5  *  Copyright (c) 2013-2014  Avago Technologies
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  *  FILE: megaraid_sas_fusion.c
21  *
22  *  Authors: Avago Technologies
23  *           Sumant Patro
24  *           Adam Radford
25  *           Kashyap Desai <kashyap.desai@avagotech.com>
26  *           Sumit Saxena <sumit.saxena@avagotech.com>
27  *
28  *  Send feedback to: megaraidlinux.pdl@avagotech.com
29  *
30  *  Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31  *  San Jose, California 95131
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/uaccess.h>
45 #include <linux/fs.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
50
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_dbg.h>
56
57 #include "megaraid_sas_fusion.h"
58 #include "megaraid_sas.h"
59
60 extern void megasas_free_cmds(struct megasas_instance *instance);
61 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
62                                            *instance);
63 extern void
64 megasas_complete_cmd(struct megasas_instance *instance,
65                      struct megasas_cmd *cmd, u8 alt_status);
66 int megasas_is_ldio(struct scsi_cmnd *cmd);
67 int
68 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
69               int seconds);
70
71 void
72 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
73 int megasas_alloc_cmds(struct megasas_instance *instance);
74 int
75 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
76 int
77 megasas_issue_polled(struct megasas_instance *instance,
78                      struct megasas_cmd *cmd);
79 void
80 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
81
82 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
83 void megaraid_sas_kill_hba(struct megasas_instance *instance);
84
85 extern u32 megasas_dbg_lvl;
86 void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
87 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
88                                   int initial);
89 void megasas_start_timer(struct megasas_instance *instance,
90                         struct timer_list *timer,
91                          void *fn, unsigned long interval);
92 extern struct megasas_mgmt_info megasas_mgmt_info;
93 extern int resetwaittime;
94
95
96
97 /**
98  * megasas_enable_intr_fusion - Enables interrupts
99  * @regs:                       MFI register set
100  */
101 void
102 megasas_enable_intr_fusion(struct megasas_instance *instance)
103 {
104         struct megasas_register_set __iomem *regs;
105         regs = instance->reg_set;
106         /* For Thunderbolt/Invader also clear intr on enable */
107         writel(~0, &regs->outbound_intr_status);
108         readl(&regs->outbound_intr_status);
109
110         writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
111
112         /* Dummy readl to force pci flush */
113         readl(&regs->outbound_intr_mask);
114         instance->mask_interrupts = 0;
115 }
116
117 /**
118  * megasas_disable_intr_fusion - Disables interrupt
119  * @regs:                        MFI register set
120  */
121 void
122 megasas_disable_intr_fusion(struct megasas_instance *instance)
123 {
124         u32 mask = 0xFFFFFFFF;
125         u32 status;
126         struct megasas_register_set __iomem *regs;
127         regs = instance->reg_set;
128         instance->mask_interrupts = 1;
129
130         writel(mask, &regs->outbound_intr_mask);
131         /* Dummy readl to force pci flush */
132         status = readl(&regs->outbound_intr_mask);
133 }
134
135 int
136 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
137 {
138         u32 status;
139         /*
140          * Check if it is our interrupt
141          */
142         status = readl(&regs->outbound_intr_status);
143
144         if (status & 1) {
145                 writel(status, &regs->outbound_intr_status);
146                 readl(&regs->outbound_intr_status);
147                 return 1;
148         }
149         if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
150                 return 0;
151
152         return 1;
153 }
154
155 /**
156  * megasas_get_cmd_fusion -     Get a command from the free pool
157  * @instance:           Adapter soft state
158  *
159  * Returns a free command from the pool
160  */
161 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
162                                                   *instance)
163 {
164         unsigned long flags;
165         struct fusion_context *fusion =
166                 (struct fusion_context *)instance->ctrl_context;
167         struct megasas_cmd_fusion *cmd = NULL;
168
169         spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
170
171         if (!list_empty(&fusion->cmd_pool)) {
172                 cmd = list_entry((&fusion->cmd_pool)->next,
173                                  struct megasas_cmd_fusion, list);
174                 list_del_init(&cmd->list);
175         } else {
176                 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
177         }
178
179         spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
180         return cmd;
181 }
182
183 /**
184  * megasas_return_cmd_fusion -  Return a cmd to free command pool
185  * @instance:           Adapter soft state
186  * @cmd:                Command packet to be returned to free command pool
187  */
188 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
189         struct megasas_cmd_fusion *cmd)
190 {
191         unsigned long flags;
192         struct fusion_context *fusion =
193                 (struct fusion_context *)instance->ctrl_context;
194
195         spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
196
197         cmd->scmd = NULL;
198         cmd->sync_cmd_idx = (u32)ULONG_MAX;
199         list_add(&cmd->list, (&fusion->cmd_pool)->next);
200
201         spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
202 }
203
204 /**
205  * megasas_return_mfi_mpt_pthr - Return a mfi and mpt to free command pool
206  * @instance:           Adapter soft state
207  * @cmd_mfi:            MFI Command packet to be returned to free command pool
208  * @cmd_mpt:            MPT Command packet to be returned to free command pool
209  */
210 inline void megasas_return_mfi_mpt_pthr(struct megasas_instance *instance,
211                 struct megasas_cmd *cmd_mfi,
212                 struct megasas_cmd_fusion *cmd_fusion)
213 {
214         unsigned long flags;
215
216         /*
217          * TO DO: optimize this code and use only one lock instead of two
218          * locks being used currently- mpt_pool_lock is acquired
219          * inside mfi_pool_lock
220          */
221         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
222         megasas_return_cmd_fusion(instance, cmd_fusion);
223         if (atomic_read(&cmd_mfi->mfi_mpt_pthr) != MFI_MPT_ATTACHED)
224                 dev_err(&instance->pdev->dev, "Possible bug from %s %d\n",
225                         __func__, __LINE__);
226         atomic_set(&cmd_mfi->mfi_mpt_pthr, MFI_MPT_DETACHED);
227         __megasas_return_cmd(instance, cmd_mfi);
228         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
229 }
230
231 /**
232  * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
233  * @instance:                           Adapter soft state
234  */
235 static void megasas_teardown_frame_pool_fusion(
236         struct megasas_instance *instance)
237 {
238         int i;
239         struct fusion_context *fusion = instance->ctrl_context;
240
241         u16 max_cmd = instance->max_fw_cmds;
242
243         struct megasas_cmd_fusion *cmd;
244
245         if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
246                 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
247                        "sense pool : %p\n", fusion->sg_dma_pool,
248                        fusion->sense_dma_pool);
249                 return;
250         }
251
252         /*
253          * Return all frames to pool
254          */
255         for (i = 0; i < max_cmd; i++) {
256
257                 cmd = fusion->cmd_list[i];
258
259                 if (cmd->sg_frame)
260                         pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
261                                       cmd->sg_frame_phys_addr);
262
263                 if (cmd->sense)
264                         pci_pool_free(fusion->sense_dma_pool, cmd->sense,
265                                       cmd->sense_phys_addr);
266         }
267
268         /*
269          * Now destroy the pool itself
270          */
271         pci_pool_destroy(fusion->sg_dma_pool);
272         pci_pool_destroy(fusion->sense_dma_pool);
273
274         fusion->sg_dma_pool = NULL;
275         fusion->sense_dma_pool = NULL;
276 }
277
278 /**
279  * megasas_free_cmds_fusion -   Free all the cmds in the free cmd pool
280  * @instance:           Adapter soft state
281  */
282 void
283 megasas_free_cmds_fusion(struct megasas_instance *instance)
284 {
285         int i;
286         struct fusion_context *fusion = instance->ctrl_context;
287
288         u32 max_cmds, req_sz, reply_sz, io_frames_sz;
289
290
291         req_sz = fusion->request_alloc_sz;
292         reply_sz = fusion->reply_alloc_sz;
293         io_frames_sz = fusion->io_frames_alloc_sz;
294
295         max_cmds = instance->max_fw_cmds;
296
297         /* Free descriptors and request Frames memory */
298         if (fusion->req_frames_desc)
299                 dma_free_coherent(&instance->pdev->dev, req_sz,
300                                   fusion->req_frames_desc,
301                                   fusion->req_frames_desc_phys);
302
303         if (fusion->reply_frames_desc) {
304                 pci_pool_free(fusion->reply_frames_desc_pool,
305                               fusion->reply_frames_desc,
306                               fusion->reply_frames_desc_phys);
307                 pci_pool_destroy(fusion->reply_frames_desc_pool);
308         }
309
310         if (fusion->io_request_frames) {
311                 pci_pool_free(fusion->io_request_frames_pool,
312                               fusion->io_request_frames,
313                               fusion->io_request_frames_phys);
314                 pci_pool_destroy(fusion->io_request_frames_pool);
315         }
316
317         /* Free the Fusion frame pool */
318         megasas_teardown_frame_pool_fusion(instance);
319
320         /* Free all the commands in the cmd_list */
321         for (i = 0; i < max_cmds; i++)
322                 kfree(fusion->cmd_list[i]);
323
324         /* Free the cmd_list buffer itself */
325         kfree(fusion->cmd_list);
326         fusion->cmd_list = NULL;
327
328         INIT_LIST_HEAD(&fusion->cmd_pool);
329 }
330
331 /**
332  * megasas_create_frame_pool_fusion -   Creates DMA pool for cmd frames
333  * @instance:                   Adapter soft state
334  *
335  */
336 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
337 {
338         int i;
339         u32 max_cmd;
340         struct fusion_context *fusion;
341         struct megasas_cmd_fusion *cmd;
342         u32 total_sz_chain_frame;
343
344         fusion = instance->ctrl_context;
345         max_cmd = instance->max_fw_cmds;
346
347         total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
348
349         /*
350          * Use DMA pool facility provided by PCI layer
351          */
352
353         fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
354                                               instance->pdev,
355                                               total_sz_chain_frame, 4,
356                                               0);
357         if (!fusion->sg_dma_pool) {
358                 printk(KERN_DEBUG "megasas: failed to setup request pool "
359                        "fusion\n");
360                 return -ENOMEM;
361         }
362         fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
363                                                  instance->pdev,
364                                                  SCSI_SENSE_BUFFERSIZE, 64, 0);
365
366         if (!fusion->sense_dma_pool) {
367                 printk(KERN_DEBUG "megasas: failed to setup sense pool "
368                        "fusion\n");
369                 pci_pool_destroy(fusion->sg_dma_pool);
370                 fusion->sg_dma_pool = NULL;
371                 return -ENOMEM;
372         }
373
374         /*
375          * Allocate and attach a frame to each of the commands in cmd_list
376          */
377         for (i = 0; i < max_cmd; i++) {
378
379                 cmd = fusion->cmd_list[i];
380
381                 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
382                                                GFP_KERNEL,
383                                                &cmd->sg_frame_phys_addr);
384
385                 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
386                                             GFP_KERNEL, &cmd->sense_phys_addr);
387                 /*
388                  * megasas_teardown_frame_pool_fusion() takes care of freeing
389                  * whatever has been allocated
390                  */
391                 if (!cmd->sg_frame || !cmd->sense) {
392                         printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
393                         megasas_teardown_frame_pool_fusion(instance);
394                         return -ENOMEM;
395                 }
396         }
397         return 0;
398 }
399
400 /**
401  * megasas_alloc_cmds_fusion -  Allocates the command packets
402  * @instance:           Adapter soft state
403  *
404  *
405  * Each frame has a 32-bit field called context. This context is used to get
406  * back the megasas_cmd_fusion from the frame when a frame gets completed
407  * In this driver, the 32 bit values are the indices into an array cmd_list.
408  * This array is used only to look up the megasas_cmd_fusion given the context.
409  * The free commands themselves are maintained in a linked list called cmd_pool.
410  *
411  * cmds are formed in the io_request and sg_frame members of the
412  * megasas_cmd_fusion. The context field is used to get a request descriptor
413  * and is used as SMID of the cmd.
414  * SMID value range is from 1 to max_fw_cmds.
415  */
416 int
417 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
418 {
419         int i, j, count;
420         u32 max_cmd, io_frames_sz;
421         struct fusion_context *fusion;
422         struct megasas_cmd_fusion *cmd;
423         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
424         u32 offset;
425         dma_addr_t io_req_base_phys;
426         u8 *io_req_base;
427
428         fusion = instance->ctrl_context;
429
430         max_cmd = instance->max_fw_cmds;
431
432         fusion->req_frames_desc =
433                 dma_alloc_coherent(&instance->pdev->dev,
434                                    fusion->request_alloc_sz,
435                                    &fusion->req_frames_desc_phys, GFP_KERNEL);
436
437         if (!fusion->req_frames_desc) {
438                 printk(KERN_ERR "megasas; Could not allocate memory for "
439                        "request_frames\n");
440                 goto fail_req_desc;
441         }
442
443         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
444         fusion->reply_frames_desc_pool =
445                 pci_pool_create("reply_frames pool", instance->pdev,
446                                 fusion->reply_alloc_sz * count, 16, 0);
447
448         if (!fusion->reply_frames_desc_pool) {
449                 printk(KERN_ERR "megasas; Could not allocate memory for "
450                        "reply_frame pool\n");
451                 goto fail_reply_desc;
452         }
453
454         fusion->reply_frames_desc =
455                 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
456                                &fusion->reply_frames_desc_phys);
457         if (!fusion->reply_frames_desc) {
458                 printk(KERN_ERR "megasas; Could not allocate memory for "
459                        "reply_frame pool\n");
460                 pci_pool_destroy(fusion->reply_frames_desc_pool);
461                 goto fail_reply_desc;
462         }
463
464         reply_desc = fusion->reply_frames_desc;
465         for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
466                 reply_desc->Words = ULLONG_MAX;
467
468         io_frames_sz = fusion->io_frames_alloc_sz;
469
470         fusion->io_request_frames_pool =
471                 pci_pool_create("io_request_frames pool", instance->pdev,
472                                 fusion->io_frames_alloc_sz, 16, 0);
473
474         if (!fusion->io_request_frames_pool) {
475                 printk(KERN_ERR "megasas: Could not allocate memory for "
476                        "io_request_frame pool\n");
477                 goto fail_io_frames;
478         }
479
480         fusion->io_request_frames =
481                 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
482                                &fusion->io_request_frames_phys);
483         if (!fusion->io_request_frames) {
484                 printk(KERN_ERR "megasas: Could not allocate memory for "
485                        "io_request_frames frames\n");
486                 pci_pool_destroy(fusion->io_request_frames_pool);
487                 goto fail_io_frames;
488         }
489
490         /*
491          * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
492          * Allocate the dynamic array first and then allocate individual
493          * commands.
494          */
495         fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
496                                    * max_cmd, GFP_KERNEL);
497
498         if (!fusion->cmd_list) {
499                 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
500                        "memory for cmd_list_fusion\n");
501                 goto fail_cmd_list;
502         }
503
504         max_cmd = instance->max_fw_cmds;
505         for (i = 0; i < max_cmd; i++) {
506                 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
507                                               GFP_KERNEL);
508                 if (!fusion->cmd_list[i]) {
509                         printk(KERN_ERR "Could not alloc cmd list fusion\n");
510
511                         for (j = 0; j < i; j++)
512                                 kfree(fusion->cmd_list[j]);
513
514                         kfree(fusion->cmd_list);
515                         fusion->cmd_list = NULL;
516                         goto fail_cmd_list;
517                 }
518         }
519
520         /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
521         io_req_base = fusion->io_request_frames +
522                 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
523         io_req_base_phys = fusion->io_request_frames_phys +
524                 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
525
526         /*
527          * Add all the commands to command pool (fusion->cmd_pool)
528          */
529
530         /* SMID 0 is reserved. Set SMID/index from 1 */
531         for (i = 0; i < max_cmd; i++) {
532                 cmd = fusion->cmd_list[i];
533                 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
534                 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
535                 cmd->index = i + 1;
536                 cmd->scmd = NULL;
537                 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
538                 cmd->instance = instance;
539                 cmd->io_request =
540                         (struct MPI2_RAID_SCSI_IO_REQUEST *)
541                   (io_req_base + offset);
542                 memset(cmd->io_request, 0,
543                        sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
544                 cmd->io_request_phys_addr = io_req_base_phys + offset;
545
546                 list_add_tail(&cmd->list, &fusion->cmd_pool);
547         }
548
549         /*
550          * Create a frame pool and assign one frame to each cmd
551          */
552         if (megasas_create_frame_pool_fusion(instance)) {
553                 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
554                 megasas_free_cmds_fusion(instance);
555                 goto fail_req_desc;
556         }
557
558         return 0;
559
560 fail_cmd_list:
561         pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
562                       fusion->io_request_frames_phys);
563         pci_pool_destroy(fusion->io_request_frames_pool);
564 fail_io_frames:
565         dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
566                           fusion->reply_frames_desc,
567                           fusion->reply_frames_desc_phys);
568         pci_pool_free(fusion->reply_frames_desc_pool,
569                       fusion->reply_frames_desc,
570                       fusion->reply_frames_desc_phys);
571         pci_pool_destroy(fusion->reply_frames_desc_pool);
572
573 fail_reply_desc:
574         dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
575                           fusion->req_frames_desc,
576                           fusion->req_frames_desc_phys);
577 fail_req_desc:
578         return -ENOMEM;
579 }
580
581 /**
582  * wait_and_poll -      Issues a polling command
583  * @instance:                   Adapter soft state
584  * @cmd:                        Command packet to be issued
585  *
586  * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
587  */
588 int
589 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
590         int seconds)
591 {
592         int i;
593         struct megasas_header *frame_hdr = &cmd->frame->hdr;
594         struct fusion_context *fusion;
595
596         u32 msecs = seconds * 1000;
597
598         fusion = instance->ctrl_context;
599         /*
600          * Wait for cmd_status to change
601          */
602         for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
603                 rmb();
604                 msleep(20);
605         }
606
607         if (frame_hdr->cmd_status == 0xff) {
608                 if (fusion)
609                         megasas_return_mfi_mpt_pthr(instance, cmd,
610                                 cmd->mpt_pthr_cmd_blocked);
611                 return -ETIME;
612         }
613
614         return 0;
615 }
616
617 /**
618  * megasas_ioc_init_fusion -    Initializes the FW
619  * @instance:           Adapter soft state
620  *
621  * Issues the IOC Init cmd
622  */
623 int
624 megasas_ioc_init_fusion(struct megasas_instance *instance)
625 {
626         struct megasas_init_frame *init_frame;
627         struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
628         dma_addr_t      ioc_init_handle;
629         struct megasas_cmd *cmd;
630         u8 ret;
631         struct fusion_context *fusion;
632         union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
633         int i;
634         struct megasas_header *frame_hdr;
635
636         fusion = instance->ctrl_context;
637
638         cmd = megasas_get_cmd(instance);
639
640         if (!cmd) {
641                 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
642                 ret = 1;
643                 goto fail_get_cmd;
644         }
645
646         IOCInitMessage =
647           dma_alloc_coherent(&instance->pdev->dev,
648                              sizeof(struct MPI2_IOC_INIT_REQUEST),
649                              &ioc_init_handle, GFP_KERNEL);
650
651         if (!IOCInitMessage) {
652                 printk(KERN_ERR "Could not allocate memory for "
653                        "IOCInitMessage\n");
654                 ret = 1;
655                 goto fail_fw_init;
656         }
657
658         memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
659
660         IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
661         IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
662         IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
663         IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
664         IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
665
666         IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
667         IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
668         IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
669         IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
670         init_frame = (struct megasas_init_frame *)cmd->frame;
671         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
672
673         frame_hdr = &cmd->frame->hdr;
674         frame_hdr->cmd_status = 0xFF;
675         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
676
677         init_frame->cmd = MFI_CMD_INIT;
678         init_frame->cmd_status = 0xFF;
679
680         /* driver support Extended MSIX */
681         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
682                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
683                 init_frame->driver_operations.
684                         mfi_capabilities.support_additional_msix = 1;
685         /* driver supports HA / Remote LUN over Fast Path interface */
686         init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
687                 = 1;
688         init_frame->driver_operations.mfi_capabilities.support_max_255lds
689                 = 1;
690         init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
691                 = 1;
692         /* Convert capability to LE32 */
693         cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
694
695         init_frame->queue_info_new_phys_addr_hi =
696                 cpu_to_le32(upper_32_bits(ioc_init_handle));
697         init_frame->queue_info_new_phys_addr_lo =
698                 cpu_to_le32(lower_32_bits(ioc_init_handle));
699         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
700
701         req_desc.Words = 0;
702         req_desc.MFAIo.RequestFlags =
703                 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
704                  MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
705         cpu_to_le32s((u32 *)&req_desc.MFAIo);
706         req_desc.Words |= cpu_to_le64(cmd->frame_phys_addr);
707
708         /*
709          * disable the intr before firing the init frame
710          */
711         instance->instancet->disable_intr(instance);
712
713         for (i = 0; i < (10 * 1000); i += 20) {
714                 if (readl(&instance->reg_set->doorbell) & 1)
715                         msleep(20);
716                 else
717                         break;
718         }
719
720         instance->instancet->fire_cmd(instance, req_desc.u.low,
721                                       req_desc.u.high, instance->reg_set);
722
723         wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
724
725         frame_hdr = &cmd->frame->hdr;
726         if (frame_hdr->cmd_status != 0) {
727                 ret = 1;
728                 goto fail_fw_init;
729         }
730         printk(KERN_ERR "megasas:IOC Init cmd success\n");
731
732         ret = 0;
733
734 fail_fw_init:
735         megasas_return_cmd(instance, cmd);
736         if (IOCInitMessage)
737                 dma_free_coherent(&instance->pdev->dev,
738                                   sizeof(struct MPI2_IOC_INIT_REQUEST),
739                                   IOCInitMessage, ioc_init_handle);
740 fail_get_cmd:
741         return ret;
742 }
743
744 /*
745  * megasas_get_ld_map_info -    Returns FW's ld_map structure
746  * @instance:                           Adapter soft state
747  * @pend:                               Pend the command or not
748  * Issues an internal command (DCMD) to get the FW's controller PD
749  * list structure.  This information is mainly used to find out SYSTEM
750  * supported by the FW.
751  * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
752  * dcmd.mbox.b[0]       - number of LDs being sync'd
753  * dcmd.mbox.b[1]       - 0 - complete command immediately.
754  *                      - 1 - pend till config change
755  * dcmd.mbox.b[2]       - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
756  *                      - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
757  *                              uses extended struct MR_FW_RAID_MAP_EXT
758  */
759 static int
760 megasas_get_ld_map_info(struct megasas_instance *instance)
761 {
762         int ret = 0;
763         struct megasas_cmd *cmd;
764         struct megasas_dcmd_frame *dcmd;
765         void *ci;
766         dma_addr_t ci_h = 0;
767         u32 size_map_info;
768         struct fusion_context *fusion;
769
770         cmd = megasas_get_cmd(instance);
771
772         if (!cmd) {
773                 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
774                 return -ENOMEM;
775         }
776
777         fusion = instance->ctrl_context;
778
779         if (!fusion) {
780                 megasas_return_cmd(instance, cmd);
781                 return -ENXIO;
782         }
783
784         dcmd = &cmd->frame->dcmd;
785
786         size_map_info = fusion->current_map_sz;
787
788         ci = (void *) fusion->ld_map[(instance->map_id & 1)];
789         ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
790
791         if (!ci) {
792                 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
793                 megasas_return_cmd(instance, cmd);
794                 return -ENOMEM;
795         }
796
797         memset(ci, 0, fusion->max_map_sz);
798         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
799 #if VD_EXT_DEBUG
800         dev_dbg(&instance->pdev->dev,
801                 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
802                 __func__, cpu_to_le32(size_map_info));
803 #endif
804         dcmd->cmd = MFI_CMD_DCMD;
805         dcmd->cmd_status = 0xFF;
806         dcmd->sge_count = 1;
807         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
808         dcmd->timeout = 0;
809         dcmd->pad_0 = 0;
810         dcmd->data_xfer_len = cpu_to_le32(size_map_info);
811         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
812         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
813         dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
814
815         if (instance->ctrl_context && !instance->mask_interrupts)
816                 ret = megasas_issue_blocked_cmd(instance, cmd,
817                         MEGASAS_BLOCKED_CMD_TIMEOUT);
818         else
819                 ret = megasas_issue_polled(instance, cmd);
820
821         if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
822                 megasas_return_mfi_mpt_pthr(instance, cmd,
823                         cmd->mpt_pthr_cmd_blocked);
824         else
825                 megasas_return_cmd(instance, cmd);
826
827         return ret;
828 }
829
830 u8
831 megasas_get_map_info(struct megasas_instance *instance)
832 {
833         struct fusion_context *fusion = instance->ctrl_context;
834
835         fusion->fast_path_io = 0;
836         if (!megasas_get_ld_map_info(instance)) {
837                 if (MR_ValidateMapInfo(instance)) {
838                         fusion->fast_path_io = 1;
839                         return 0;
840                 }
841         }
842         return 1;
843 }
844
845 /*
846  * megasas_sync_map_info -      Returns FW's ld_map structure
847  * @instance:                           Adapter soft state
848  *
849  * Issues an internal command (DCMD) to get the FW's controller PD
850  * list structure.  This information is mainly used to find out SYSTEM
851  * supported by the FW.
852  */
853 int
854 megasas_sync_map_info(struct megasas_instance *instance)
855 {
856         int ret = 0, i;
857         struct megasas_cmd *cmd;
858         struct megasas_dcmd_frame *dcmd;
859         u32 size_sync_info, num_lds;
860         struct fusion_context *fusion;
861         struct MR_LD_TARGET_SYNC *ci = NULL;
862         struct MR_DRV_RAID_MAP_ALL *map;
863         struct MR_LD_RAID  *raid;
864         struct MR_LD_TARGET_SYNC *ld_sync;
865         dma_addr_t ci_h = 0;
866         u32 size_map_info;
867
868         cmd = megasas_get_cmd(instance);
869
870         if (!cmd) {
871                 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
872                        "info.\n");
873                 return -ENOMEM;
874         }
875
876         fusion = instance->ctrl_context;
877
878         if (!fusion) {
879                 megasas_return_cmd(instance, cmd);
880                 return 1;
881         }
882
883         map = fusion->ld_drv_map[instance->map_id & 1];
884
885         num_lds = le16_to_cpu(map->raidMap.ldCount);
886
887         dcmd = &cmd->frame->dcmd;
888
889         size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
890
891         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
892
893         ci = (struct MR_LD_TARGET_SYNC *)
894           fusion->ld_map[(instance->map_id - 1) & 1];
895         memset(ci, 0, fusion->max_map_sz);
896
897         ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
898
899         ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
900
901         for (i = 0; i < num_lds; i++, ld_sync++) {
902                 raid = MR_LdRaidGet(i, map);
903                 ld_sync->targetId = MR_GetLDTgtId(i, map);
904                 ld_sync->seqNum = raid->seqNum;
905         }
906
907         size_map_info = fusion->current_map_sz;
908
909         dcmd->cmd = MFI_CMD_DCMD;
910         dcmd->cmd_status = 0xFF;
911         dcmd->sge_count = 1;
912         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
913         dcmd->timeout = 0;
914         dcmd->pad_0 = 0;
915         dcmd->data_xfer_len = cpu_to_le32(size_map_info);
916         dcmd->mbox.b[0] = num_lds;
917         dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
918         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
919         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
920         dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
921
922         instance->map_update_cmd = cmd;
923
924         instance->instancet->issue_dcmd(instance, cmd);
925
926         return ret;
927 }
928
929 /*
930  * meagasas_display_intel_branding - Display branding string
931  * @instance: per adapter object
932  *
933  * Return nothing.
934  */
935 static void
936 megasas_display_intel_branding(struct megasas_instance *instance)
937 {
938         if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
939                 return;
940
941         switch (instance->pdev->device) {
942         case PCI_DEVICE_ID_LSI_INVADER:
943                 switch (instance->pdev->subsystem_device) {
944                 case MEGARAID_INTEL_RS3DC080_SSDID:
945                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
946                                 instance->host->host_no,
947                                 MEGARAID_INTEL_RS3DC080_BRANDING);
948                         break;
949                 case MEGARAID_INTEL_RS3DC040_SSDID:
950                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
951                                 instance->host->host_no,
952                                 MEGARAID_INTEL_RS3DC040_BRANDING);
953                         break;
954                 case MEGARAID_INTEL_RS3SC008_SSDID:
955                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
956                                 instance->host->host_no,
957                                 MEGARAID_INTEL_RS3SC008_BRANDING);
958                         break;
959                 case MEGARAID_INTEL_RS3MC044_SSDID:
960                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
961                                 instance->host->host_no,
962                                 MEGARAID_INTEL_RS3MC044_BRANDING);
963                         break;
964                 default:
965                         break;
966                 }
967                 break;
968         case PCI_DEVICE_ID_LSI_FURY:
969                 switch (instance->pdev->subsystem_device) {
970                 case MEGARAID_INTEL_RS3WC080_SSDID:
971                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
972                                 instance->host->host_no,
973                                 MEGARAID_INTEL_RS3WC080_BRANDING);
974                         break;
975                 case MEGARAID_INTEL_RS3WC040_SSDID:
976                         dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
977                                 instance->host->host_no,
978                                 MEGARAID_INTEL_RS3WC040_BRANDING);
979                         break;
980                 default:
981                         break;
982                 }
983                 break;
984         default:
985                 break;
986         }
987 }
988
989 /**
990  * megasas_init_adapter_fusion -        Initializes the FW
991  * @instance:           Adapter soft state
992  *
993  * This is the main function for initializing firmware.
994  */
995 u32
996 megasas_init_adapter_fusion(struct megasas_instance *instance)
997 {
998         struct megasas_register_set __iomem *reg_set;
999         struct fusion_context *fusion;
1000         u32 max_cmd;
1001         int i = 0, count;
1002
1003         fusion = instance->ctrl_context;
1004
1005         reg_set = instance->reg_set;
1006
1007         /*
1008          * Get various operational parameters from status register
1009          */
1010         instance->max_fw_cmds =
1011                 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1012         instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
1013
1014         /*
1015          * Reduce the max supported cmds by 1. This is to ensure that the
1016          * reply_q_sz (1 more than the max cmd that driver may send)
1017          * does not exceed max cmds that the FW can support
1018          */
1019         instance->max_fw_cmds = instance->max_fw_cmds-1;
1020         /* Only internal cmds (DCMD) need to have MFI frames */
1021         instance->max_mfi_cmds = MEGASAS_INT_CMDS;
1022
1023         max_cmd = instance->max_fw_cmds;
1024
1025         fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
1026
1027         fusion->request_alloc_sz =
1028                 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1029         fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1030                 *(fusion->reply_q_depth);
1031         fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1032                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1033                  (max_cmd + 1)); /* Extra 1 for SMID 0 */
1034
1035         fusion->max_sge_in_main_msg =
1036           (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1037            offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1038
1039         fusion->max_sge_in_chain =
1040                 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
1041
1042         instance->max_num_sge = rounddown_pow_of_two(
1043                 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
1044
1045         /* Used for pass thru MFI frame (DCMD) */
1046         fusion->chain_offset_mfi_pthru =
1047                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1048
1049         fusion->chain_offset_io_request =
1050                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1051                  sizeof(union MPI2_SGE_IO_UNION))/16;
1052
1053         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1054         for (i = 0 ; i < count; i++)
1055                 fusion->last_reply_idx[i] = 0;
1056
1057         /*
1058          * Allocate memory for descriptors
1059          * Create a pool of commands
1060          */
1061         if (megasas_alloc_cmds(instance))
1062                 goto fail_alloc_mfi_cmds;
1063         if (megasas_alloc_cmds_fusion(instance))
1064                 goto fail_alloc_cmds;
1065
1066         if (megasas_ioc_init_fusion(instance))
1067                 goto fail_ioc_init;
1068
1069         megasas_display_intel_branding(instance);
1070         if (megasas_get_ctrl_info(instance)) {
1071                 dev_err(&instance->pdev->dev,
1072                         "Could not get controller info. Fail from %s %d\n",
1073                         __func__, __LINE__);
1074                 goto fail_ioc_init;
1075         }
1076
1077         instance->flag_ieee = 1;
1078         fusion->fast_path_io = 0;
1079
1080         fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1081         for (i = 0; i < 2; i++) {
1082                 fusion->ld_map[i] = NULL;
1083                 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1084                         fusion->drv_map_pages);
1085                 if (!fusion->ld_drv_map[i]) {
1086                         dev_err(&instance->pdev->dev, "Could not allocate "
1087                                 "memory for local map info for %d pages\n",
1088                                 fusion->drv_map_pages);
1089                         if (i == 1)
1090                                 free_pages((ulong)fusion->ld_drv_map[0],
1091                                         fusion->drv_map_pages);
1092                         goto fail_ioc_init;
1093                 }
1094                 memset(fusion->ld_drv_map[i], 0,
1095                         ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
1096         }
1097
1098         for (i = 0; i < 2; i++) {
1099                 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1100                                                        fusion->max_map_sz,
1101                                                        &fusion->ld_map_phys[i],
1102                                                        GFP_KERNEL);
1103                 if (!fusion->ld_map[i]) {
1104                         printk(KERN_ERR "megasas: Could not allocate memory "
1105                                "for map info\n");
1106                         goto fail_map_info;
1107                 }
1108         }
1109
1110         if (!megasas_get_map_info(instance))
1111                 megasas_sync_map_info(instance);
1112
1113         return 0;
1114
1115 fail_map_info:
1116         if (i == 1)
1117                 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
1118                                   fusion->ld_map[0], fusion->ld_map_phys[0]);
1119 fail_ioc_init:
1120         megasas_free_cmds_fusion(instance);
1121 fail_alloc_cmds:
1122         megasas_free_cmds(instance);
1123 fail_alloc_mfi_cmds:
1124         return 1;
1125 }
1126
1127 /**
1128  * megasas_fire_cmd_fusion -    Sends command to the FW
1129  * @frame_phys_addr :           Physical address of cmd
1130  * @frame_count :               Number of frames for the command
1131  * @regs :                      MFI register set
1132  */
1133 void
1134 megasas_fire_cmd_fusion(struct megasas_instance *instance,
1135                         dma_addr_t req_desc_lo,
1136                         u32 req_desc_hi,
1137                         struct megasas_register_set __iomem *regs)
1138 {
1139 #if defined(writeq) && defined(CONFIG_64BIT)
1140         u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
1141                         le32_to_cpu(req_desc_lo));
1142
1143         writeq(req_data, &(regs)->inbound_low_queue_port);
1144 #else
1145         unsigned long flags;
1146
1147         spin_lock_irqsave(&instance->hba_lock, flags);
1148
1149         writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1150         writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
1151         spin_unlock_irqrestore(&instance->hba_lock, flags);
1152 #endif
1153 }
1154
1155 /**
1156  * map_cmd_status -     Maps FW cmd status to OS cmd status
1157  * @cmd :               Pointer to cmd
1158  * @status :            status of cmd returned by FW
1159  * @ext_status :        ext status of cmd returned by FW
1160  */
1161
1162 void
1163 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1164 {
1165
1166         switch (status) {
1167
1168         case MFI_STAT_OK:
1169                 cmd->scmd->result = DID_OK << 16;
1170                 break;
1171
1172         case MFI_STAT_SCSI_IO_FAILED:
1173         case MFI_STAT_LD_INIT_IN_PROGRESS:
1174                 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1175                 break;
1176
1177         case MFI_STAT_SCSI_DONE_WITH_ERROR:
1178
1179                 cmd->scmd->result = (DID_OK << 16) | ext_status;
1180                 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1181                         memset(cmd->scmd->sense_buffer, 0,
1182                                SCSI_SENSE_BUFFERSIZE);
1183                         memcpy(cmd->scmd->sense_buffer, cmd->sense,
1184                                SCSI_SENSE_BUFFERSIZE);
1185                         cmd->scmd->result |= DRIVER_SENSE << 24;
1186                 }
1187                 break;
1188
1189         case MFI_STAT_LD_OFFLINE:
1190         case MFI_STAT_DEVICE_NOT_FOUND:
1191                 cmd->scmd->result = DID_BAD_TARGET << 16;
1192                 break;
1193         case MFI_STAT_CONFIG_SEQ_MISMATCH:
1194                 cmd->scmd->result = DID_IMM_RETRY << 16;
1195                 break;
1196         default:
1197                 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1198                 cmd->scmd->result = DID_ERROR << 16;
1199                 break;
1200         }
1201 }
1202
1203 /**
1204  * megasas_make_sgl_fusion -    Prepares 32-bit SGL
1205  * @instance:           Adapter soft state
1206  * @scp:                SCSI command from the mid-layer
1207  * @sgl_ptr:            SGL to be filled in
1208  * @cmd:                cmd we are working on
1209  *
1210  * If successful, this function returns the number of SG elements.
1211  */
1212 static int
1213 megasas_make_sgl_fusion(struct megasas_instance *instance,
1214                         struct scsi_cmnd *scp,
1215                         struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1216                         struct megasas_cmd_fusion *cmd)
1217 {
1218         int i, sg_processed, sge_count;
1219         struct scatterlist *os_sgl;
1220         struct fusion_context *fusion;
1221
1222         fusion = instance->ctrl_context;
1223
1224         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1225                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1226                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1227                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1228                 sgl_ptr_end->Flags = 0;
1229         }
1230
1231         sge_count = scsi_dma_map(scp);
1232
1233         BUG_ON(sge_count < 0);
1234
1235         if (sge_count > instance->max_num_sge || !sge_count)
1236                 return sge_count;
1237
1238         scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1239                 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1240                 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1241                 sgl_ptr->Flags = 0;
1242                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1243                         (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1244                         if (i == sge_count - 1)
1245                                 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1246                 }
1247                 sgl_ptr++;
1248
1249                 sg_processed = i + 1;
1250
1251                 if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
1252                     (sge_count > fusion->max_sge_in_main_msg)) {
1253
1254                         struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1255                         if ((instance->pdev->device ==
1256                                 PCI_DEVICE_ID_LSI_INVADER) ||
1257                                 (instance->pdev->device ==
1258                                 PCI_DEVICE_ID_LSI_FURY)) {
1259                                 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1260                                         MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1261                                         MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1262                                         cmd->io_request->ChainOffset =
1263                                                 fusion->
1264                                                 chain_offset_io_request;
1265                                 else
1266                                         cmd->io_request->ChainOffset = 0;
1267                         } else
1268                                 cmd->io_request->ChainOffset =
1269                                         fusion->chain_offset_io_request;
1270
1271                         sg_chain = sgl_ptr;
1272                         /* Prepare chain element */
1273                         sg_chain->NextChainOffset = 0;
1274                         if ((instance->pdev->device ==
1275                                 PCI_DEVICE_ID_LSI_INVADER) ||
1276                                 (instance->pdev->device ==
1277                                 PCI_DEVICE_ID_LSI_FURY))
1278                                 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1279                         else
1280                                 sg_chain->Flags =
1281                                         (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1282                                          MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1283                         sg_chain->Length =  cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1284                         sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1285
1286                         sgl_ptr =
1287                           (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1288                 }
1289         }
1290
1291         return sge_count;
1292 }
1293
1294 /**
1295  * megasas_set_pd_lba - Sets PD LBA
1296  * @cdb:                CDB
1297  * @cdb_len:            cdb length
1298  * @start_blk:          Start block of IO
1299  *
1300  * Used to set the PD LBA in CDB for FP IOs
1301  */
1302 void
1303 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1304                    struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1305                    struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1306 {
1307         struct MR_LD_RAID *raid;
1308         u32 ld;
1309         u64 start_blk = io_info->pdBlock;
1310         u8 *cdb = io_request->CDB.CDB32;
1311         u32 num_blocks = io_info->numBlocks;
1312         u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1313
1314         /* Check if T10 PI (DIF) is enabled for this LD */
1315         ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1316         raid = MR_LdRaidGet(ld, local_map_ptr);
1317         if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1318                 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1319                 cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1320                 cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;
1321
1322                 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1323                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1324                 else
1325                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1326                 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1327
1328                 /* LBA */
1329                 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1330                 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1331                 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1332                 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1333                 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1334                 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1335                 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1336                 cdb[19] = (u8)(start_blk & 0xff);
1337
1338                 /* Logical block reference tag */
1339                 io_request->CDB.EEDP32.PrimaryReferenceTag =
1340                         cpu_to_be32(ref_tag);
1341                 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
1342                 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1343
1344                 /* Transfer length */
1345                 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1346                 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1347                 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1348                 cdb[31] = (u8)(num_blocks & 0xff);
1349
1350                 /* set SCSI IO EEDPFlags */
1351                 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1352                         io_request->EEDPFlags = cpu_to_le16(
1353                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
1354                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1355                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1356                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1357                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1358                 } else {
1359                         io_request->EEDPFlags = cpu_to_le16(
1360                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1361                                 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1362                 }
1363                 io_request->Control |= cpu_to_le32((0x4 << 26));
1364                 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1365         } else {
1366                 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1367                 if (((cdb_len == 12) || (cdb_len == 16)) &&
1368                     (start_blk <= 0xffffffff)) {
1369                         if (cdb_len == 16) {
1370                                 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1371                                 flagvals = cdb[1];
1372                                 groupnum = cdb[14];
1373                                 control = cdb[15];
1374                         } else {
1375                                 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1376                                 flagvals = cdb[1];
1377                                 groupnum = cdb[10];
1378                                 control = cdb[11];
1379                         }
1380
1381                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1382
1383                         cdb[0] = opcode;
1384                         cdb[1] = flagvals;
1385                         cdb[6] = groupnum;
1386                         cdb[9] = control;
1387
1388                         /* Transfer length */
1389                         cdb[8] = (u8)(num_blocks & 0xff);
1390                         cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1391
1392                         io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1393                         cdb_len = 10;
1394                 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1395                         /* Convert to 16 byte CDB for large LBA's */
1396                         switch (cdb_len) {
1397                         case 6:
1398                                 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1399                                 control = cdb[5];
1400                                 break;
1401                         case 10:
1402                                 opcode =
1403                                         cdb[0] == READ_10 ? READ_16 : WRITE_16;
1404                                 flagvals = cdb[1];
1405                                 groupnum = cdb[6];
1406                                 control = cdb[9];
1407                                 break;
1408                         case 12:
1409                                 opcode =
1410                                         cdb[0] == READ_12 ? READ_16 : WRITE_16;
1411                                 flagvals = cdb[1];
1412                                 groupnum = cdb[10];
1413                                 control = cdb[11];
1414                                 break;
1415                         }
1416
1417                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1418
1419                         cdb[0] = opcode;
1420                         cdb[1] = flagvals;
1421                         cdb[14] = groupnum;
1422                         cdb[15] = control;
1423
1424                         /* Transfer length */
1425                         cdb[13] = (u8)(num_blocks & 0xff);
1426                         cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1427                         cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1428                         cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1429
1430                         io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1431                         cdb_len = 16;
1432                 }
1433
1434                 /* Normal case, just load LBA here */
1435                 switch (cdb_len) {
1436                 case 6:
1437                 {
1438                         u8 val = cdb[1] & 0xE0;
1439                         cdb[3] = (u8)(start_blk & 0xff);
1440                         cdb[2] = (u8)((start_blk >> 8) & 0xff);
1441                         cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1442                         break;
1443                 }
1444                 case 10:
1445                         cdb[5] = (u8)(start_blk & 0xff);
1446                         cdb[4] = (u8)((start_blk >> 8) & 0xff);
1447                         cdb[3] = (u8)((start_blk >> 16) & 0xff);
1448                         cdb[2] = (u8)((start_blk >> 24) & 0xff);
1449                         break;
1450                 case 12:
1451                         cdb[5]    = (u8)(start_blk & 0xff);
1452                         cdb[4]    = (u8)((start_blk >> 8) & 0xff);
1453                         cdb[3]    = (u8)((start_blk >> 16) & 0xff);
1454                         cdb[2]    = (u8)((start_blk >> 24) & 0xff);
1455                         break;
1456                 case 16:
1457                         cdb[9]    = (u8)(start_blk & 0xff);
1458                         cdb[8]    = (u8)((start_blk >> 8) & 0xff);
1459                         cdb[7]    = (u8)((start_blk >> 16) & 0xff);
1460                         cdb[6]    = (u8)((start_blk >> 24) & 0xff);
1461                         cdb[5]    = (u8)((start_blk >> 32) & 0xff);
1462                         cdb[4]    = (u8)((start_blk >> 40) & 0xff);
1463                         cdb[3]    = (u8)((start_blk >> 48) & 0xff);
1464                         cdb[2]    = (u8)((start_blk >> 56) & 0xff);
1465                         break;
1466                 }
1467         }
1468 }
1469
1470 /**
1471  * megasas_build_ldio_fusion -  Prepares IOs to devices
1472  * @instance:           Adapter soft state
1473  * @scp:                SCSI command
1474  * @cmd:                Command to be prepared
1475  *
1476  * Prepares the io_request and chain elements (sg_frame) for IO
1477  * The IO can be for PD (Fast Path) or LD
1478  */
1479 void
1480 megasas_build_ldio_fusion(struct megasas_instance *instance,
1481                           struct scsi_cmnd *scp,
1482                           struct megasas_cmd_fusion *cmd)
1483 {
1484         u8 fp_possible;
1485         u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1486         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1487         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1488         struct IO_REQUEST_INFO io_info;
1489         struct fusion_context *fusion;
1490         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1491         u8 *raidLUN;
1492
1493         device_id = MEGASAS_DEV_INDEX(instance, scp);
1494
1495         fusion = instance->ctrl_context;
1496
1497         io_request = cmd->io_request;
1498         io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1499         io_request->RaidContext.status = 0;
1500         io_request->RaidContext.exStatus = 0;
1501
1502         req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1503
1504         start_lba_lo = 0;
1505         start_lba_hi = 0;
1506         fp_possible = 0;
1507
1508         /*
1509          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1510          */
1511         if (scp->cmd_len == 6) {
1512                 datalength = (u32) scp->cmnd[4];
1513                 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1514                         ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1515
1516                 start_lba_lo &= 0x1FFFFF;
1517         }
1518
1519         /*
1520          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1521          */
1522         else if (scp->cmd_len == 10) {
1523                 datalength = (u32) scp->cmnd[8] |
1524                         ((u32) scp->cmnd[7] << 8);
1525                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1526                         ((u32) scp->cmnd[3] << 16) |
1527                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1528         }
1529
1530         /*
1531          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1532          */
1533         else if (scp->cmd_len == 12) {
1534                 datalength = ((u32) scp->cmnd[6] << 24) |
1535                         ((u32) scp->cmnd[7] << 16) |
1536                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1537                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1538                         ((u32) scp->cmnd[3] << 16) |
1539                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1540         }
1541
1542         /*
1543          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1544          */
1545         else if (scp->cmd_len == 16) {
1546                 datalength = ((u32) scp->cmnd[10] << 24) |
1547                         ((u32) scp->cmnd[11] << 16) |
1548                         ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1549                 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1550                         ((u32) scp->cmnd[7] << 16) |
1551                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1552
1553                 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1554                         ((u32) scp->cmnd[3] << 16) |
1555                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1556         }
1557
1558         memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1559         io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1560         io_info.numBlocks = datalength;
1561         io_info.ldTgtId = device_id;
1562         io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1563
1564         if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1565                 io_info.isRead = 1;
1566
1567         local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1568
1569         if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1570                 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
1571                 io_request->RaidContext.regLockFlags  = 0;
1572                 fp_possible = 0;
1573         } else {
1574                 if (MR_BuildRaidContext(instance, &io_info,
1575                                         &io_request->RaidContext,
1576                                         local_map_ptr, &raidLUN))
1577                         fp_possible = io_info.fpOkForIo;
1578         }
1579
1580         /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1581            id by default, not CPU group id, otherwise all MSI-X queues won't
1582            be utilized */
1583         cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1584                 smp_processor_id() % instance->msix_vectors : 0;
1585
1586         if (fp_possible) {
1587                 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1588                                    local_map_ptr, start_lba_lo);
1589                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1590                 cmd->request_desc->SCSIIO.RequestFlags =
1591                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1592                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1593                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1594                         (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1595                         if (io_request->RaidContext.regLockFlags ==
1596                             REGION_TYPE_UNUSED)
1597                                 cmd->request_desc->SCSIIO.RequestFlags =
1598                                         (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1599                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1600                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1601                         io_request->RaidContext.nseg = 0x1;
1602                         io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1603                         io_request->RaidContext.regLockFlags |=
1604                           (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1605                            MR_RL_FLAGS_SEQ_NUM_ENABLE);
1606                 }
1607                 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1608                     (io_info.isRead)) {
1609                         io_info.devHandle =
1610                                 get_updated_dev_handle(instance,
1611                                         &fusion->load_balance_info[device_id],
1612                                         &io_info);
1613                         scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1614                         cmd->pd_r1_lb = io_info.pd_after_lb;
1615                 } else
1616                         scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1617                 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1618                 io_request->DevHandle = io_info.devHandle;
1619                 /* populate the LUN field */
1620                 memcpy(io_request->LUN, raidLUN, 8);
1621         } else {
1622                 io_request->RaidContext.timeoutValue =
1623                         cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
1624                 cmd->request_desc->SCSIIO.RequestFlags =
1625                         (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1626                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1627                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1628                         (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1629                         if (io_request->RaidContext.regLockFlags ==
1630                             REGION_TYPE_UNUSED)
1631                                 cmd->request_desc->SCSIIO.RequestFlags =
1632                                         (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1633                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1634                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1635                         io_request->RaidContext.regLockFlags |=
1636                                 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1637                                  MR_RL_FLAGS_SEQ_NUM_ENABLE);
1638                         io_request->RaidContext.nseg = 0x1;
1639                 }
1640                 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1641                 io_request->DevHandle = cpu_to_le16(device_id);
1642         } /* Not FP */
1643 }
1644
1645 /**
1646  * megasas_build_dcdb_fusion -  Prepares IOs to devices
1647  * @instance:           Adapter soft state
1648  * @scp:                SCSI command
1649  * @cmd:                Command to be prepared
1650  *
1651  * Prepares the io_request frame for non-io cmds
1652  */
1653 static void
1654 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1655                           struct scsi_cmnd *scmd,
1656                           struct megasas_cmd_fusion *cmd)
1657 {
1658         u32 device_id;
1659         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1660         u16 pd_index = 0;
1661         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1662         struct fusion_context *fusion = instance->ctrl_context;
1663         u8                          span, physArm;
1664         u16                         devHandle;
1665         u32                         ld, arRef, pd;
1666         struct MR_LD_RAID                  *raid;
1667         struct RAID_CONTEXT                *pRAID_Context;
1668
1669         io_request = cmd->io_request;
1670         device_id = MEGASAS_DEV_INDEX(instance, scmd);
1671         pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1672                 +scmd->device->id;
1673         local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1674
1675         io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1676
1677
1678         /* Check if this is a system PD I/O */
1679         if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1680             instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1681                 io_request->Function = 0;
1682                 if (fusion->fast_path_io)
1683                         io_request->DevHandle =
1684                         local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1685                 io_request->RaidContext.timeoutValue =
1686                         local_map_ptr->raidMap.fpPdIoTimeoutSec;
1687                 io_request->RaidContext.regLockFlags = 0;
1688                 io_request->RaidContext.regLockRowLBA = 0;
1689                 io_request->RaidContext.regLockLength = 0;
1690                 io_request->RaidContext.RAIDFlags =
1691                         MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1692                         MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1693                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1694                         (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1695                         io_request->IoFlags |= cpu_to_le16(
1696                                 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1697                 cmd->request_desc->SCSIIO.RequestFlags =
1698                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1699                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1700                 cmd->request_desc->SCSIIO.DevHandle =
1701                         local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1702                 cmd->request_desc->SCSIIO.MSIxIndex =
1703                         instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
1704                 /*
1705                  * If the command is for the tape device, set the
1706                  * FP timeout to the os layer timeout value.
1707                  */
1708                 if (scmd->device->type == TYPE_TAPE) {
1709                         if ((scmd->request->timeout / HZ) > 0xFFFF)
1710                                 io_request->RaidContext.timeoutValue =
1711                                         0xFFFF;
1712                         else
1713                                 io_request->RaidContext.timeoutValue =
1714                                         scmd->request->timeout / HZ;
1715                 }
1716         } else {
1717                 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1718                         goto NonFastPath;
1719
1720                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1721                 if ((ld >= instance->fw_supported_vd_count) ||
1722                         (!fusion->fast_path_io))
1723                         goto NonFastPath;
1724
1725                 raid = MR_LdRaidGet(ld, local_map_ptr);
1726
1727                 /* check if this LD is FP capable */
1728                 if (!(raid->capability.fpNonRWCapable))
1729                         /* not FP capable, send as non-FP */
1730                         goto NonFastPath;
1731
1732                 /* get RAID_Context pointer */
1733                 pRAID_Context = &io_request->RaidContext;
1734
1735                 /* set RAID context values */
1736                 pRAID_Context->regLockFlags     = REGION_TYPE_SHARED_READ;
1737                 pRAID_Context->timeoutValue     = cpu_to_le16(raid->fpIoTimeoutForLd);
1738                 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1739                 pRAID_Context->regLockRowLBA    = 0;
1740                 pRAID_Context->regLockLength    = 0;
1741                 pRAID_Context->configSeqNum     = raid->seqNum;
1742
1743                 /* get the DevHandle for the PD (since this is
1744                    fpNonRWCapable, this is a single disk RAID0) */
1745                 span = physArm = 0;
1746                 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1747                 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1748                 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1749
1750                 /* build request descriptor */
1751                 cmd->request_desc->SCSIIO.RequestFlags =
1752                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1753                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1754                 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1755
1756                 /* populate the LUN field */
1757                 memcpy(io_request->LUN, raid->LUN, 8);
1758
1759                 /* build the raidScsiIO structure */
1760                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1761                 io_request->DevHandle = devHandle;
1762
1763                 return;
1764
1765 NonFastPath:
1766                 io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1767                 io_request->DevHandle = cpu_to_le16(device_id);
1768                 cmd->request_desc->SCSIIO.RequestFlags =
1769                         (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1770                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1771         }
1772         io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1773         int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
1774 }
1775
1776 /**
1777  * megasas_build_io_fusion -    Prepares IOs to devices
1778  * @instance:           Adapter soft state
1779  * @scp:                SCSI command
1780  * @cmd:                Command to be prepared
1781  *
1782  * Invokes helper functions to prepare request frames
1783  * and sets flags appropriate for IO/Non-IO cmd
1784  */
1785 int
1786 megasas_build_io_fusion(struct megasas_instance *instance,
1787                         struct scsi_cmnd *scp,
1788                         struct megasas_cmd_fusion *cmd)
1789 {
1790         u32 device_id, sge_count;
1791         struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1792
1793         device_id = MEGASAS_DEV_INDEX(instance, scp);
1794
1795         /* Zero out some fields so they don't get reused */
1796         memset(io_request->LUN, 0x0, 8);
1797         io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1798         io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1799         io_request->EEDPFlags = 0;
1800         io_request->Control = 0;
1801         io_request->EEDPBlockSize = 0;
1802         io_request->ChainOffset = 0;
1803         io_request->RaidContext.RAIDFlags = 0;
1804         io_request->RaidContext.Type = 0;
1805         io_request->RaidContext.nseg = 0;
1806
1807         memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1808         /*
1809          * Just the CDB length,rest of the Flags are zero
1810          * This will be modified for FP in build_ldio_fusion
1811          */
1812         io_request->IoFlags = cpu_to_le16(scp->cmd_len);
1813
1814         if (megasas_is_ldio(scp))
1815                 megasas_build_ldio_fusion(instance, scp, cmd);
1816         else
1817                 megasas_build_dcdb_fusion(instance, scp, cmd);
1818
1819         /*
1820          * Construct SGL
1821          */
1822
1823         sge_count =
1824                 megasas_make_sgl_fusion(instance, scp,
1825                                         (struct MPI25_IEEE_SGE_CHAIN64 *)
1826                                         &io_request->SGL, cmd);
1827
1828         if (sge_count > instance->max_num_sge) {
1829                 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1830                        "max (0x%x) allowed\n", sge_count,
1831                        instance->max_num_sge);
1832                 return 1;
1833         }
1834
1835         io_request->RaidContext.numSGE = sge_count;
1836
1837         io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
1838
1839         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1840                 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
1841         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1842                 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
1843
1844         io_request->SGLOffset0 =
1845                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1846
1847         io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
1848         io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1849
1850         cmd->scmd = scp;
1851         scp->SCp.ptr = (char *)cmd;
1852
1853         return 0;
1854 }
1855
1856 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1857 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1858 {
1859         u8 *p;
1860         struct fusion_context *fusion;
1861
1862         if (index >= instance->max_fw_cmds) {
1863                 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1864                        "descriptor for scsi%d\n", index,
1865                         instance->host->host_no);
1866                 return NULL;
1867         }
1868         fusion = instance->ctrl_context;
1869         p = fusion->req_frames_desc
1870                 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1871
1872         return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1873 }
1874
1875 /**
1876  * megasas_build_and_issue_cmd_fusion -Main routine for building and
1877  *                                     issuing non IOCTL cmd
1878  * @instance:                   Adapter soft state
1879  * @scmd:                       pointer to scsi cmd from OS
1880  */
1881 static u32
1882 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1883                                    struct scsi_cmnd *scmd)
1884 {
1885         struct megasas_cmd_fusion *cmd;
1886         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1887         u32 index;
1888         struct fusion_context *fusion;
1889
1890         fusion = instance->ctrl_context;
1891
1892         cmd = megasas_get_cmd_fusion(instance);
1893         if (!cmd)
1894                 return SCSI_MLQUEUE_HOST_BUSY;
1895
1896         index = cmd->index;
1897
1898         req_desc = megasas_get_request_descriptor(instance, index-1);
1899         if (!req_desc)
1900                 return 1;
1901
1902         req_desc->Words = 0;
1903         cmd->request_desc = req_desc;
1904
1905         if (megasas_build_io_fusion(instance, scmd, cmd)) {
1906                 megasas_return_cmd_fusion(instance, cmd);
1907                 printk(KERN_ERR "megasas: Error building command.\n");
1908                 cmd->request_desc = NULL;
1909                 return 1;
1910         }
1911
1912         req_desc = cmd->request_desc;
1913         req_desc->SCSIIO.SMID = cpu_to_le16(index);
1914
1915         if (cmd->io_request->ChainOffset != 0 &&
1916             cmd->io_request->ChainOffset != 0xF)
1917                 printk(KERN_ERR "megasas: The chain offset value is not "
1918                        "correct : %x\n", cmd->io_request->ChainOffset);
1919
1920         /*
1921          * Issue the command to the FW
1922          */
1923         atomic_inc(&instance->fw_outstanding);
1924
1925         instance->instancet->fire_cmd(instance,
1926                                       req_desc->u.low, req_desc->u.high,
1927                                       instance->reg_set);
1928
1929         return 0;
1930 }
1931
1932 /**
1933  * complete_cmd_fusion -        Completes command
1934  * @instance:                   Adapter soft state
1935  * Completes all commands that is in reply descriptor queue
1936  */
1937 int
1938 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1939 {
1940         union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1941         struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1942         struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1943         struct fusion_context *fusion;
1944         struct megasas_cmd *cmd_mfi;
1945         struct megasas_cmd_fusion *cmd_fusion;
1946         u16 smid, num_completed;
1947         u8 reply_descript_type;
1948         u32 status, extStatus, device_id;
1949         union desc_value d_val;
1950         struct LD_LOAD_BALANCE_INFO *lbinfo;
1951         int threshold_reply_count = 0;
1952
1953         fusion = instance->ctrl_context;
1954
1955         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1956                 return IRQ_HANDLED;
1957
1958         desc = fusion->reply_frames_desc;
1959         desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1960                  sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1961                 fusion->last_reply_idx[MSIxIndex];
1962
1963         reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1964
1965         d_val.word = desc->Words;
1966
1967         reply_descript_type = reply_desc->ReplyFlags &
1968                 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1969
1970         if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1971                 return IRQ_NONE;
1972
1973         num_completed = 0;
1974
1975         while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1976                 smid = le16_to_cpu(reply_desc->SMID);
1977
1978                 cmd_fusion = fusion->cmd_list[smid - 1];
1979
1980                 scsi_io_req =
1981                         (struct MPI2_RAID_SCSI_IO_REQUEST *)
1982                   cmd_fusion->io_request;
1983
1984                 if (cmd_fusion->scmd)
1985                         cmd_fusion->scmd->SCp.ptr = NULL;
1986
1987                 status = scsi_io_req->RaidContext.status;
1988                 extStatus = scsi_io_req->RaidContext.exStatus;
1989
1990                 switch (scsi_io_req->Function) {
1991                 case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
1992                         /* Update load balancing info */
1993                         device_id = MEGASAS_DEV_INDEX(instance,
1994                                                       cmd_fusion->scmd);
1995                         lbinfo = &fusion->load_balance_info[device_id];
1996                         if (cmd_fusion->scmd->SCp.Status &
1997                             MEGASAS_LOAD_BALANCE_FLAG) {
1998                                 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
1999                                 cmd_fusion->scmd->SCp.Status &=
2000                                         ~MEGASAS_LOAD_BALANCE_FLAG;
2001                         }
2002                         if (reply_descript_type ==
2003                             MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2004                                 if (megasas_dbg_lvl == 5)
2005                                         printk(KERN_ERR "\nmegasas: FAST Path "
2006                                                "IO Success\n");
2007                         }
2008                         /* Fall thru and complete IO */
2009                 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2010                         /* Map the FW Cmd Status */
2011                         map_cmd_status(cmd_fusion, status, extStatus);
2012                         scsi_dma_unmap(cmd_fusion->scmd);
2013                         cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2014                         scsi_io_req->RaidContext.status = 0;
2015                         scsi_io_req->RaidContext.exStatus = 0;
2016                         megasas_return_cmd_fusion(instance, cmd_fusion);
2017                         atomic_dec(&instance->fw_outstanding);
2018
2019                         break;
2020                 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2021                         cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2022
2023                         if (!cmd_mfi->mpt_pthr_cmd_blocked) {
2024                                 if (megasas_dbg_lvl == 5)
2025                                         dev_info(&instance->pdev->dev,
2026                                                 "freeing mfi/mpt pass-through "
2027                                                 "from %s %d\n",
2028                                                  __func__, __LINE__);
2029                                 megasas_return_mfi_mpt_pthr(instance, cmd_mfi,
2030                                         cmd_fusion);
2031                         }
2032
2033                         megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2034                         cmd_fusion->flags = 0;
2035                         break;
2036                 }
2037
2038                 fusion->last_reply_idx[MSIxIndex]++;
2039                 if (fusion->last_reply_idx[MSIxIndex] >=
2040                     fusion->reply_q_depth)
2041                         fusion->last_reply_idx[MSIxIndex] = 0;
2042
2043                 desc->Words = ULLONG_MAX;
2044                 num_completed++;
2045                 threshold_reply_count++;
2046
2047                 /* Get the next reply descriptor */
2048                 if (!fusion->last_reply_idx[MSIxIndex])
2049                         desc = fusion->reply_frames_desc +
2050                                 ((MSIxIndex * fusion->reply_alloc_sz)/
2051                                  sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
2052                 else
2053                         desc++;
2054
2055                 reply_desc =
2056                   (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2057
2058                 d_val.word = desc->Words;
2059
2060                 reply_descript_type = reply_desc->ReplyFlags &
2061                         MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2062
2063                 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2064                         break;
2065                 /*
2066                  * Write to reply post host index register after completing threshold
2067                  * number of reply counts and still there are more replies in reply queue
2068                  * pending to be completed
2069                  */
2070                 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2071                         if ((instance->pdev->device ==
2072                                 PCI_DEVICE_ID_LSI_INVADER) ||
2073                                 (instance->pdev->device ==
2074                                 PCI_DEVICE_ID_LSI_FURY))
2075                                 writel(((MSIxIndex & 0x7) << 24) |
2076                                         fusion->last_reply_idx[MSIxIndex],
2077                                         instance->reply_post_host_index_addr[MSIxIndex/8]);
2078                         else
2079                                 writel((MSIxIndex << 24) |
2080                                         fusion->last_reply_idx[MSIxIndex],
2081                                         instance->reply_post_host_index_addr[0]);
2082                         threshold_reply_count = 0;
2083                 }
2084         }
2085
2086         if (!num_completed)
2087                 return IRQ_NONE;
2088
2089         wmb();
2090         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2091                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2092                 writel(((MSIxIndex & 0x7) << 24) |
2093                         fusion->last_reply_idx[MSIxIndex],
2094                         instance->reply_post_host_index_addr[MSIxIndex/8]);
2095         else
2096                 writel((MSIxIndex << 24) |
2097                         fusion->last_reply_idx[MSIxIndex],
2098                         instance->reply_post_host_index_addr[0]);
2099         megasas_check_and_restore_queue_depth(instance);
2100         return IRQ_HANDLED;
2101 }
2102
2103 /**
2104  * megasas_complete_cmd_dpc_fusion -    Completes command
2105  * @instance:                   Adapter soft state
2106  *
2107  * Tasklet to complete cmds
2108  */
2109 void
2110 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2111 {
2112         struct megasas_instance *instance =
2113                 (struct megasas_instance *)instance_addr;
2114         unsigned long flags;
2115         u32 count, MSIxIndex;
2116
2117         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2118
2119         /* If we have already declared adapter dead, donot complete cmds */
2120         spin_lock_irqsave(&instance->hba_lock, flags);
2121         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2122                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2123                 return;
2124         }
2125         spin_unlock_irqrestore(&instance->hba_lock, flags);
2126
2127         for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2128                 complete_cmd_fusion(instance, MSIxIndex);
2129 }
2130
2131 /**
2132  * megasas_isr_fusion - isr entry point
2133  */
2134 irqreturn_t megasas_isr_fusion(int irq, void *devp)
2135 {
2136         struct megasas_irq_context *irq_context = devp;
2137         struct megasas_instance *instance = irq_context->instance;
2138         u32 mfiStatus, fw_state, dma_state;
2139
2140         if (instance->mask_interrupts)
2141                 return IRQ_NONE;
2142
2143         if (!instance->msix_vectors) {
2144                 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2145                 if (!mfiStatus)
2146                         return IRQ_NONE;
2147         }
2148
2149         /* If we are resetting, bail */
2150         if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2151                 instance->instancet->clear_intr(instance->reg_set);
2152                 return IRQ_HANDLED;
2153         }
2154
2155         if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2156                 instance->instancet->clear_intr(instance->reg_set);
2157                 /* If we didn't complete any commands, check for FW fault */
2158                 fw_state = instance->instancet->read_fw_status_reg(
2159                         instance->reg_set) & MFI_STATE_MASK;
2160                 dma_state = instance->instancet->read_fw_status_reg
2161                         (instance->reg_set) & MFI_STATE_DMADONE;
2162                 if (instance->crash_dump_drv_support &&
2163                         instance->crash_dump_app_support) {
2164                         /* Start collecting crash, if DMA bit is done */
2165                         if ((fw_state == MFI_STATE_FAULT) && dma_state)
2166                                 schedule_work(&instance->crash_init);
2167                         else if (fw_state == MFI_STATE_FAULT)
2168                                 schedule_work(&instance->work_init);
2169                 } else if (fw_state == MFI_STATE_FAULT) {
2170                         printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2171                                "for scsi%d\n", instance->host->host_no);
2172                         schedule_work(&instance->work_init);
2173                 }
2174         }
2175
2176         return IRQ_HANDLED;
2177 }
2178
2179 /**
2180  * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2181  * @instance:                   Adapter soft state
2182  * mfi_cmd:                     megasas_cmd pointer
2183  *
2184  */
2185 u8
2186 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2187                         struct megasas_cmd *mfi_cmd)
2188 {
2189         struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2190         struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2191         struct megasas_cmd_fusion *cmd;
2192         struct fusion_context *fusion;
2193         struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2194         u32 opcode;
2195
2196         cmd = megasas_get_cmd_fusion(instance);
2197         if (!cmd)
2198                 return 1;
2199
2200         /*  Save the smid. To be used for returning the cmd */
2201         mfi_cmd->context.smid = cmd->index;
2202         cmd->sync_cmd_idx = mfi_cmd->index;
2203
2204         /* Set this only for Blocked commands */
2205         opcode = le32_to_cpu(mfi_cmd->frame->dcmd.opcode);
2206         if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2207                 && (mfi_cmd->frame->dcmd.mbox.b[1] == 1))
2208                 mfi_cmd->is_wait_event = 1;
2209
2210         if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
2211                 mfi_cmd->is_wait_event = 1;
2212
2213         if (mfi_cmd->is_wait_event)
2214                 mfi_cmd->mpt_pthr_cmd_blocked = cmd;
2215
2216         /*
2217          * For cmds where the flag is set, store the flag and check
2218          * on completion. For cmds with this flag, don't call
2219          * megasas_complete_cmd
2220          */
2221
2222         if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
2223                 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2224
2225         fusion = instance->ctrl_context;
2226         io_req = cmd->io_request;
2227
2228         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2229                 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2230                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2231                         (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2232                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2233                 sgl_ptr_end->Flags = 0;
2234         }
2235
2236         mpi25_ieee_chain =
2237           (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2238
2239         io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2240         io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2241                                        SGL) / 4;
2242         io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2243
2244         mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
2245
2246         mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2247                 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2248
2249         mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
2250
2251         return 0;
2252 }
2253
2254 /**
2255  * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2256  * @instance:                   Adapter soft state
2257  * @cmd:                        mfi cmd to build
2258  *
2259  */
2260 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2261 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2262 {
2263         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2264         u16 index;
2265
2266         if (build_mpt_mfi_pass_thru(instance, cmd)) {
2267                 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2268                 return NULL;
2269         }
2270
2271         index = cmd->context.smid;
2272
2273         req_desc = megasas_get_request_descriptor(instance, index - 1);
2274
2275         if (!req_desc)
2276                 return NULL;
2277
2278         req_desc->Words = 0;
2279         req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2280                                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2281
2282         req_desc->SCSIIO.SMID = cpu_to_le16(index);
2283
2284         return req_desc;
2285 }
2286
2287 /**
2288  * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2289  * @instance:                   Adapter soft state
2290  * @cmd:                        mfi cmd pointer
2291  *
2292  */
2293 void
2294 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2295                           struct megasas_cmd *cmd)
2296 {
2297         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2298
2299         req_desc = build_mpt_cmd(instance, cmd);
2300         if (!req_desc) {
2301                 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2302                 return;
2303         }
2304         atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_ATTACHED);
2305         instance->instancet->fire_cmd(instance, req_desc->u.low,
2306                                       req_desc->u.high, instance->reg_set);
2307 }
2308
2309 /**
2310  * megasas_release_fusion -     Reverses the FW initialization
2311  * @intance:                    Adapter soft state
2312  */
2313 void
2314 megasas_release_fusion(struct megasas_instance *instance)
2315 {
2316         megasas_free_cmds(instance);
2317         megasas_free_cmds_fusion(instance);
2318
2319         iounmap(instance->reg_set);
2320
2321         pci_release_selected_regions(instance->pdev, instance->bar);
2322 }
2323
2324 /**
2325  * megasas_read_fw_status_reg_fusion - returns the current FW status value
2326  * @regs:                       MFI register set
2327  */
2328 static u32
2329 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2330 {
2331         return readl(&(regs)->outbound_scratch_pad);
2332 }
2333
2334 /**
2335  * megasas_alloc_host_crash_buffer -    Host buffers for Crash dump collection from Firmware
2336  * @instance:                           Controller's soft instance
2337  * return:                              Number of allocated host crash buffers
2338  */
2339 static void
2340 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2341 {
2342         unsigned int i;
2343
2344         instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2345         for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2346                 instance->crash_buf[i] = (void  *)__get_free_pages(GFP_KERNEL,
2347                                 instance->crash_buf_pages);
2348                 if (!instance->crash_buf[i]) {
2349                         dev_info(&instance->pdev->dev, "Firmware crash dump "
2350                                 "memory allocation failed at index %d\n", i);
2351                         break;
2352                 }
2353                 memset(instance->crash_buf[i], 0,
2354                         ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
2355         }
2356         instance->drv_buf_alloc = i;
2357 }
2358
2359 /**
2360  * megasas_free_host_crash_buffer -     Host buffers for Crash dump collection from Firmware
2361  * @instance:                           Controller's soft instance
2362  */
2363 void
2364 megasas_free_host_crash_buffer(struct megasas_instance *instance)
2365 {
2366         unsigned int i
2367 ;
2368         for (i = 0; i < instance->drv_buf_alloc; i++) {
2369                 if (instance->crash_buf[i])
2370                         free_pages((ulong)instance->crash_buf[i],
2371                                         instance->crash_buf_pages);
2372         }
2373         instance->drv_buf_index = 0;
2374         instance->drv_buf_alloc = 0;
2375         instance->fw_crash_state = UNAVAILABLE;
2376         instance->fw_crash_buffer_size = 0;
2377 }
2378
2379 /**
2380  * megasas_adp_reset_fusion -   For controller reset
2381  * @regs:                               MFI register set
2382  */
2383 static int
2384 megasas_adp_reset_fusion(struct megasas_instance *instance,
2385                          struct megasas_register_set __iomem *regs)
2386 {
2387         return 0;
2388 }
2389
2390 /**
2391  * megasas_check_reset_fusion - For controller reset check
2392  * @regs:                               MFI register set
2393  */
2394 static int
2395 megasas_check_reset_fusion(struct megasas_instance *instance,
2396                            struct megasas_register_set __iomem *regs)
2397 {
2398         return 0;
2399 }
2400
2401 /* This function waits for outstanding commands on fusion to complete */
2402 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2403                                         int iotimeout, int *convert)
2404 {
2405         int i, outstanding, retval = 0, hb_seconds_missed = 0;
2406         u32 fw_state;
2407
2408         for (i = 0; i < resetwaittime; i++) {
2409                 /* Check if firmware is in fault state */
2410                 fw_state = instance->instancet->read_fw_status_reg(
2411                         instance->reg_set) & MFI_STATE_MASK;
2412                 if (fw_state == MFI_STATE_FAULT) {
2413                         printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2414                                " will reset adapter scsi%d.\n",
2415                                 instance->host->host_no);
2416                         retval = 1;
2417                         goto out;
2418                 }
2419                 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2420                 if (instance->requestorId && !iotimeout) {
2421                         retval = 1;
2422                         goto out;
2423                 }
2424
2425                 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2426                 if (instance->requestorId && iotimeout) {
2427                         if (instance->hb_host_mem->HB.fwCounter !=
2428                             instance->hb_host_mem->HB.driverCounter) {
2429                                 instance->hb_host_mem->HB.driverCounter =
2430                                         instance->hb_host_mem->HB.fwCounter;
2431                                 hb_seconds_missed = 0;
2432                         } else {
2433                                 hb_seconds_missed++;
2434                                 if (hb_seconds_missed ==
2435                                     (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2436                                         printk(KERN_WARNING "megasas: SR-IOV:"
2437                                                " Heartbeat never completed "
2438                                                " while polling during I/O "
2439                                                " timeout handling for "
2440                                                "scsi%d.\n",
2441                                                instance->host->host_no);
2442                                                *convert = 1;
2443                                                retval = 1;
2444                                                goto out;
2445                                 }
2446                         }
2447                 }
2448
2449                 outstanding = atomic_read(&instance->fw_outstanding);
2450                 if (!outstanding)
2451                         goto out;
2452
2453                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2454                         printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2455                                "commands to complete for scsi%d\n", i,
2456                                outstanding, instance->host->host_no);
2457                         megasas_complete_cmd_dpc_fusion(
2458                                 (unsigned long)instance);
2459                 }
2460                 msleep(1000);
2461         }
2462
2463         if (atomic_read(&instance->fw_outstanding)) {
2464                 printk("megaraid_sas: pending commands remain after waiting, "
2465                        "will reset adapter scsi%d.\n",
2466                        instance->host->host_no);
2467                 retval = 1;
2468         }
2469 out:
2470         return retval;
2471 }
2472
2473 void  megasas_reset_reply_desc(struct megasas_instance *instance)
2474 {
2475         int i, count;
2476         struct fusion_context *fusion;
2477         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2478
2479         fusion = instance->ctrl_context;
2480         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2481         for (i = 0 ; i < count ; i++)
2482                 fusion->last_reply_idx[i] = 0;
2483         reply_desc = fusion->reply_frames_desc;
2484         for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2485                 reply_desc->Words = ULLONG_MAX;
2486 }
2487
2488 /* Check for a second path that is currently UP */
2489 int megasas_check_mpio_paths(struct megasas_instance *instance,
2490         struct scsi_cmnd *scmd)
2491 {
2492         int i, j, retval = (DID_RESET << 16);
2493
2494         if (instance->mpio && instance->requestorId) {
2495                 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2496                         for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2497                                 if (megasas_mgmt_info.instance[i] &&
2498                                     (megasas_mgmt_info.instance[i] != instance) &&
2499                                     megasas_mgmt_info.instance[i]->mpio &&
2500                                     megasas_mgmt_info.instance[i]->requestorId
2501                                     &&
2502                                     (megasas_mgmt_info.instance[i]->ld_ids[j]
2503                                      == scmd->device->id)) {
2504                                             retval = (DID_NO_CONNECT << 16);
2505                                             goto out;
2506                                 }
2507         }
2508 out:
2509         return retval;
2510 }
2511
2512 /* Core fusion reset function */
2513 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2514 {
2515         int retval = SUCCESS, i, j, retry = 0, convert = 0;
2516         struct megasas_instance *instance;
2517         struct megasas_cmd_fusion *cmd_fusion;
2518         struct fusion_context *fusion;
2519         struct megasas_cmd *cmd_mfi;
2520         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2521         u32 host_diag, abs_state, status_reg, reset_adapter;
2522         u32 io_timeout_in_crash_mode = 0;
2523
2524         instance = (struct megasas_instance *)shost->hostdata;
2525         fusion = instance->ctrl_context;
2526
2527         mutex_lock(&instance->reset_mutex);
2528
2529         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2530                 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2531                        "returning FAILED for scsi%d.\n",
2532                         instance->host->host_no);
2533                 mutex_unlock(&instance->reset_mutex);
2534                 return FAILED;
2535         }
2536         status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2537         abs_state = status_reg & MFI_STATE_MASK;
2538
2539         /* IO timeout detected, forcibly put FW in FAULT state */
2540         if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2541                 instance->crash_dump_app_support && iotimeout) {
2542                 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2543                         "forcibly FAULT Firmware\n");
2544                 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2545                 status_reg = readl(&instance->reg_set->doorbell);
2546                 writel(status_reg | MFI_STATE_FORCE_OCR,
2547                         &instance->reg_set->doorbell);
2548                 readl(&instance->reg_set->doorbell);
2549                 mutex_unlock(&instance->reset_mutex);
2550                 do {
2551                         ssleep(3);
2552                         io_timeout_in_crash_mode++;
2553                         dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2554                                 "seconds for crash dump collection and OCR "
2555                                 "to be done\n", (io_timeout_in_crash_mode * 3));
2556                 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2557                         (io_timeout_in_crash_mode < 80));
2558
2559                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2560                         dev_info(&instance->pdev->dev, "OCR done for IO "
2561                                 "timeout case\n");
2562                         retval = SUCCESS;
2563                 } else {
2564                         dev_info(&instance->pdev->dev, "Controller is not "
2565                                 "operational after 240 seconds wait for IO "
2566                                 "timeout case in FW crash dump mode\n do "
2567                                 "OCR/kill adapter\n");
2568                         retval = megasas_reset_fusion(shost, 0);
2569                 }
2570                 return retval;
2571         }
2572
2573         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2574                 del_timer_sync(&instance->sriov_heartbeat_timer);
2575         set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2576         instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
2577         instance->instancet->disable_intr(instance);
2578         msleep(1000);
2579
2580         /* First try waiting for commands to complete */
2581         if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2582                                                 &convert)) {
2583                 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2584                 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2585                        "adapter scsi%d.\n", instance->host->host_no);
2586                 if (convert)
2587                         iotimeout = 0;
2588
2589                 /* Now return commands back to the OS */
2590                 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2591                         cmd_fusion = fusion->cmd_list[i];
2592                         if (cmd_fusion->scmd) {
2593                                 scsi_dma_unmap(cmd_fusion->scmd);
2594                                 cmd_fusion->scmd->result =
2595                                         megasas_check_mpio_paths(instance,
2596                                                                  cmd_fusion->scmd);
2597                                 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2598                                 megasas_return_cmd_fusion(instance, cmd_fusion);
2599                                 atomic_dec(&instance->fw_outstanding);
2600                         }
2601                 }
2602
2603                 status_reg = instance->instancet->read_fw_status_reg(
2604                         instance->reg_set);
2605                 abs_state = status_reg & MFI_STATE_MASK;
2606                 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2607                 if (instance->disableOnlineCtrlReset ||
2608                     (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2609                         /* Reset not supported, kill adapter */
2610                         printk(KERN_WARNING "megaraid_sas: Reset not supported"
2611                                ", killing adapter scsi%d.\n",
2612                                 instance->host->host_no);
2613                         megaraid_sas_kill_hba(instance);
2614                         instance->skip_heartbeat_timer_del = 1;
2615                         instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2616                         retval = FAILED;
2617                         goto out;
2618                 }
2619
2620                 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2621                 if (instance->requestorId && !iotimeout) {
2622                         msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2623                         /* Look for a late HB update after VF settle time */
2624                         if (abs_state == MFI_STATE_OPERATIONAL &&
2625                             (instance->hb_host_mem->HB.fwCounter !=
2626                              instance->hb_host_mem->HB.driverCounter)) {
2627                                         instance->hb_host_mem->HB.driverCounter =
2628                                                 instance->hb_host_mem->HB.fwCounter;
2629                                         printk(KERN_WARNING "megasas: SR-IOV:"
2630                                                "Late FW heartbeat update for "
2631                                                "scsi%d.\n",
2632                                                instance->host->host_no);
2633                         } else {
2634                                 /* In VF mode, first poll for FW ready */
2635                                 for (i = 0;
2636                                      i < (MEGASAS_RESET_WAIT_TIME * 1000);
2637                                      i += 20) {
2638                                         status_reg =
2639                                                 instance->instancet->
2640                                                 read_fw_status_reg(
2641                                                         instance->reg_set);
2642                                         abs_state = status_reg &
2643                                                 MFI_STATE_MASK;
2644                                         if (abs_state == MFI_STATE_READY) {
2645                                                 printk(KERN_WARNING "megasas"
2646                                                        ": SR-IOV: FW was found"
2647                                                        "to be in ready state "
2648                                                        "for scsi%d.\n",
2649                                                        instance->host->host_no);
2650                                                 break;
2651                                         }
2652                                         msleep(20);
2653                                 }
2654                                 if (abs_state != MFI_STATE_READY) {
2655                                         printk(KERN_WARNING "megasas: SR-IOV: "
2656                                                "FW not in ready state after %d"
2657                                                " seconds for scsi%d, status_reg = "
2658                                                "0x%x.\n",
2659                                                MEGASAS_RESET_WAIT_TIME,
2660                                                instance->host->host_no,
2661                                                status_reg);
2662                                         megaraid_sas_kill_hba(instance);
2663                                         instance->skip_heartbeat_timer_del = 1;
2664                                         instance->adprecovery =
2665                                                 MEGASAS_HW_CRITICAL_ERROR;
2666                                         retval = FAILED;
2667                                         goto out;
2668                                 }
2669                         }
2670                 }
2671
2672                 /* Now try to reset the chip */
2673                 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2674                         writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2675                                &instance->reg_set->fusion_seq_offset);
2676                         writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2677                                &instance->reg_set->fusion_seq_offset);
2678                         writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2679                                &instance->reg_set->fusion_seq_offset);
2680                         writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2681                                &instance->reg_set->fusion_seq_offset);
2682                         writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2683                                &instance->reg_set->fusion_seq_offset);
2684                         writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2685                                &instance->reg_set->fusion_seq_offset);
2686                         writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2687                                &instance->reg_set->fusion_seq_offset);
2688
2689                         /* Check that the diag write enable (DRWE) bit is on */
2690                         host_diag = readl(&instance->reg_set->fusion_host_diag);
2691                         retry = 0;
2692                         while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2693                                 msleep(100);
2694                                 host_diag =
2695                                 readl(&instance->reg_set->fusion_host_diag);
2696                                 if (retry++ == 100) {
2697                                         printk(KERN_WARNING "megaraid_sas: "
2698                                                "Host diag unlock failed! "
2699                                                "for scsi%d\n",
2700                                                 instance->host->host_no);
2701                                         break;
2702                                 }
2703                         }
2704                         if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2705                                 continue;
2706
2707                         /* Send chip reset command */
2708                         writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2709                                &instance->reg_set->fusion_host_diag);
2710                         msleep(3000);
2711
2712                         /* Make sure reset adapter bit is cleared */
2713                         host_diag = readl(&instance->reg_set->fusion_host_diag);
2714                         retry = 0;
2715                         while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2716                                 msleep(100);
2717                                 host_diag =
2718                                 readl(&instance->reg_set->fusion_host_diag);
2719                                 if (retry++ == 1000) {
2720                                         printk(KERN_WARNING "megaraid_sas: "
2721                                                "Diag reset adapter never "
2722                                                "cleared for scsi%d!\n",
2723                                                 instance->host->host_no);
2724                                         break;
2725                                 }
2726                         }
2727                         if (host_diag & HOST_DIAG_RESET_ADAPTER)
2728                                 continue;
2729
2730                         abs_state =
2731                                 instance->instancet->read_fw_status_reg(
2732                                         instance->reg_set) & MFI_STATE_MASK;
2733                         retry = 0;
2734
2735                         while ((abs_state <= MFI_STATE_FW_INIT) &&
2736                                (retry++ < 1000)) {
2737                                 msleep(100);
2738                                 abs_state =
2739                                 instance->instancet->read_fw_status_reg(
2740                                         instance->reg_set) & MFI_STATE_MASK;
2741                         }
2742                         if (abs_state <= MFI_STATE_FW_INIT) {
2743                                 printk(KERN_WARNING "megaraid_sas: firmware "
2744                                        "state < MFI_STATE_FW_INIT, state = "
2745                                        "0x%x for scsi%d\n", abs_state,
2746                                         instance->host->host_no);
2747                                 continue;
2748                         }
2749
2750                         /* Wait for FW to become ready */
2751                         if (megasas_transition_to_ready(instance, 1)) {
2752                                 printk(KERN_WARNING "megaraid_sas: Failed to "
2753                                        "transition controller to ready "
2754                                        "for scsi%d.\n",
2755                                        instance->host->host_no);
2756                                 continue;
2757                         }
2758
2759                         megasas_reset_reply_desc(instance);
2760                         if (megasas_ioc_init_fusion(instance)) {
2761                                 printk(KERN_WARNING "megaraid_sas: "
2762                                        "megasas_ioc_init_fusion() failed!"
2763                                        " for scsi%d\n",
2764                                        instance->host->host_no);
2765                                 continue;
2766                         }
2767
2768                         /* Re-fire management commands */
2769                         for (j = 0 ; j < instance->max_fw_cmds; j++) {
2770                                 cmd_fusion = fusion->cmd_list[j];
2771                                 if (cmd_fusion->sync_cmd_idx !=
2772                                     (u32)ULONG_MAX) {
2773                                         cmd_mfi =
2774                                         instance->
2775                                         cmd_list[cmd_fusion->sync_cmd_idx];
2776                                         if (cmd_mfi->frame->dcmd.opcode ==
2777                                             cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
2778                                                 megasas_return_mfi_mpt_pthr(instance, cmd_mfi, cmd_fusion);
2779                                         } else  {
2780                                                 req_desc =
2781                                                 megasas_get_request_descriptor(
2782                                                         instance,
2783                                                         cmd_mfi->context.smid
2784                                                         -1);
2785                                                 if (!req_desc) {
2786                                                         printk(KERN_WARNING
2787                                                                "req_desc NULL"
2788                                                                " for scsi%d\n",
2789                                                                 instance->host->host_no);
2790                                                         /* Return leaked MPT
2791                                                            frame */
2792                                                         megasas_return_cmd_fusion(instance, cmd_fusion);
2793                                                 } else {
2794                                                         instance->instancet->
2795                                                         fire_cmd(instance,
2796                                                                  req_desc->
2797                                                                  u.low,
2798                                                                  req_desc->
2799                                                                  u.high,
2800                                                                  instance->
2801                                                                  reg_set);
2802                                                 }
2803                                         }
2804                                 }
2805                         }
2806
2807                         if (megasas_get_ctrl_info(instance)) {
2808                                 dev_info(&instance->pdev->dev,
2809                                         "Failed from %s %d\n",
2810                                         __func__, __LINE__);
2811                                 instance->adprecovery =
2812                                         MEGASAS_HW_CRITICAL_ERROR;
2813                                 megaraid_sas_kill_hba(instance);
2814                                 retval = FAILED;
2815                         }
2816                         /* Reset load balance info */
2817                         memset(fusion->load_balance_info, 0,
2818                                sizeof(struct LD_LOAD_BALANCE_INFO)
2819                                *MAX_LOGICAL_DRIVES_EXT);
2820
2821                         if (!megasas_get_map_info(instance))
2822                                 megasas_sync_map_info(instance);
2823
2824                         clear_bit(MEGASAS_FUSION_IN_RESET,
2825                                   &instance->reset_flags);
2826                         instance->instancet->enable_intr(instance);
2827                         instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2828
2829                         /* Restart SR-IOV heartbeat */
2830                         if (instance->requestorId) {
2831                                 if (!megasas_sriov_start_heartbeat(instance, 0))
2832                                         megasas_start_timer(instance,
2833                                                             &instance->sriov_heartbeat_timer,
2834                                                             megasas_sriov_heartbeat_handler,
2835                                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2836                                 else
2837                                         instance->skip_heartbeat_timer_del = 1;
2838                         }
2839
2840                         /* Adapter reset completed successfully */
2841                         printk(KERN_WARNING "megaraid_sas: Reset "
2842                                "successful for scsi%d.\n",
2843                                 instance->host->host_no);
2844
2845                         if (instance->crash_dump_drv_support &&
2846                                 instance->crash_dump_app_support)
2847                                 megasas_set_crash_dump_params(instance,
2848                                         MR_CRASH_BUF_TURN_ON);
2849                         else
2850                                 megasas_set_crash_dump_params(instance,
2851                                         MR_CRASH_BUF_TURN_OFF);
2852
2853                         retval = SUCCESS;
2854                         goto out;
2855                 }
2856                 /* Reset failed, kill the adapter */
2857                 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2858                        "adapter scsi%d.\n", instance->host->host_no);
2859                 megaraid_sas_kill_hba(instance);
2860                 instance->skip_heartbeat_timer_del = 1;
2861                 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2862                 retval = FAILED;
2863         } else {
2864                 /* For VF: Restart HB timer if we didn't OCR */
2865                 if (instance->requestorId) {
2866                         megasas_start_timer(instance,
2867                                             &instance->sriov_heartbeat_timer,
2868                                             megasas_sriov_heartbeat_handler,
2869                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2870                 }
2871                 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2872                 instance->instancet->enable_intr(instance);
2873                 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2874         }
2875 out:
2876         clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2877         mutex_unlock(&instance->reset_mutex);
2878         return retval;
2879 }
2880
2881 /* Fusion Crash dump collection work queue */
2882 void  megasas_fusion_crash_dump_wq(struct work_struct *work)
2883 {
2884         struct megasas_instance *instance =
2885                 container_of(work, struct megasas_instance, crash_init);
2886         u32 status_reg;
2887         u8 partial_copy = 0;
2888
2889
2890         status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2891
2892         /*
2893          * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2894          * to host crash buffers
2895          */
2896         if (instance->drv_buf_index == 0) {
2897                 /* Buffer is already allocated for old Crash dump.
2898                  * Do OCR and do not wait for crash dump collection
2899                  */
2900                 if (instance->drv_buf_alloc) {
2901                         dev_info(&instance->pdev->dev, "earlier crash dump is "
2902                                 "not yet copied by application, ignoring this "
2903                                 "crash dump and initiating OCR\n");
2904                         status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2905                         writel(status_reg,
2906                                 &instance->reg_set->outbound_scratch_pad);
2907                         readl(&instance->reg_set->outbound_scratch_pad);
2908                         return;
2909                 }
2910                 megasas_alloc_host_crash_buffer(instance);
2911                 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2912                         "allocated: %d\n", instance->drv_buf_alloc);
2913         }
2914
2915         /*
2916          * Driver has allocated max buffers, which can be allocated
2917          * and FW has more crash dump data, then driver will
2918          * ignore the data.
2919          */
2920         if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2921                 dev_info(&instance->pdev->dev, "Driver is done copying "
2922                         "the buffer: %d\n", instance->drv_buf_alloc);
2923                 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2924                 partial_copy = 1;
2925         } else {
2926                 memcpy(instance->crash_buf[instance->drv_buf_index],
2927                         instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2928                 instance->drv_buf_index++;
2929                 status_reg &= ~MFI_STATE_DMADONE;
2930         }
2931
2932         if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2933                 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2934                         "of copied buffers: %d\n", instance->drv_buf_index);
2935                 instance->fw_crash_buffer_size =  instance->drv_buf_index;
2936                 instance->fw_crash_state = AVAILABLE;
2937                 instance->drv_buf_index = 0;
2938                 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2939                 readl(&instance->reg_set->outbound_scratch_pad);
2940                 if (!partial_copy)
2941                         megasas_reset_fusion(instance->host, 0);
2942         } else {
2943                 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2944                 readl(&instance->reg_set->outbound_scratch_pad);
2945         }
2946 }
2947
2948
2949 /* Fusion OCR work queue */
2950 void megasas_fusion_ocr_wq(struct work_struct *work)
2951 {
2952         struct megasas_instance *instance =
2953                 container_of(work, struct megasas_instance, work_init);
2954
2955         megasas_reset_fusion(instance->host, 0);
2956 }
2957
2958 struct megasas_instance_template megasas_instance_template_fusion = {
2959         .fire_cmd = megasas_fire_cmd_fusion,
2960         .enable_intr = megasas_enable_intr_fusion,
2961         .disable_intr = megasas_disable_intr_fusion,
2962         .clear_intr = megasas_clear_intr_fusion,
2963         .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2964         .adp_reset = megasas_adp_reset_fusion,
2965         .check_reset = megasas_check_reset_fusion,
2966         .service_isr = megasas_isr_fusion,
2967         .tasklet = megasas_complete_cmd_dpc_fusion,
2968         .init_adapter = megasas_init_adapter_fusion,
2969         .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2970         .issue_dcmd = megasas_issue_dcmd_fusion,
2971 };