788115b72b2292ee6868d3fa528108d3a3d9e279
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
1 /*
2  *  Linux MegaRAID driver for SAS based RAID controllers
3  *
4  *  Copyright (c) 2009-2011  LSI Corporation.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License
8  *  as published by the Free Software Foundation; either version 2
9  *  of the License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *  FILE: megaraid_sas_fusion.c
21  *
22  *  Authors: LSI Corporation
23  *           Sumant Patro
24  *           Adam Radford <linuxraid@lsi.com>
25  *
26  *  Send feedback to: <megaraidlinux@lsi.com>
27  *
28  *  Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
29  *     ATTN: Linuxraid
30  */
31
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/uaccess.h>
43 #include <linux/fs.h>
44 #include <linux/compat.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <linux/poll.h>
48
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_host.h>
53
54 #include "megaraid_sas_fusion.h"
55 #include "megaraid_sas.h"
56
57 extern void megasas_free_cmds(struct megasas_instance *instance);
58 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
59                                            *instance);
60 extern void
61 megasas_complete_cmd(struct megasas_instance *instance,
62                      struct megasas_cmd *cmd, u8 alt_status);
63 int megasas_is_ldio(struct scsi_cmnd *cmd);
64 int
65 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
66
67 void
68 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
69 int megasas_alloc_cmds(struct megasas_instance *instance);
70 int
71 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
72 int
73 megasas_issue_polled(struct megasas_instance *instance,
74                      struct megasas_cmd *cmd);
75
76 u8
77 MR_BuildRaidContext(struct megasas_instance *instance,
78                     struct IO_REQUEST_INFO *io_info,
79                     struct RAID_CONTEXT *pRAID_Context,
80                     struct MR_FW_RAID_MAP_ALL *map);
81 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
82 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
83
84 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
85
86 void
87 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
88
89 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
90                       struct LD_LOAD_BALANCE_INFO *lbInfo);
91 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
92                            struct IO_REQUEST_INFO *in_info);
93 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94 void megaraid_sas_kill_hba(struct megasas_instance *instance);
95
96 extern u32 megasas_dbg_lvl;
97 extern int resetwaittime;
98
99 /**
100  * megasas_enable_intr_fusion - Enables interrupts
101  * @regs:                       MFI register set
102  */
103 void
104 megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
105 {
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 }
115
116 /**
117  * megasas_disable_intr_fusion - Disables interrupt
118  * @regs:                        MFI register set
119  */
120 void
121 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
122 {
123         u32 mask = 0xFFFFFFFF;
124         u32 status;
125
126         writel(mask, &regs->outbound_intr_mask);
127         /* Dummy readl to force pci flush */
128         status = readl(&regs->outbound_intr_mask);
129 }
130
131 int
132 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
133 {
134         u32 status;
135         /*
136          * Check if it is our interrupt
137          */
138         status = readl(&regs->outbound_intr_status);
139
140         if (status & 1) {
141                 writel(status, &regs->outbound_intr_status);
142                 readl(&regs->outbound_intr_status);
143                 return 1;
144         }
145         if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
146                 return 0;
147
148         return 1;
149 }
150
151 /**
152  * megasas_get_cmd_fusion -     Get a command from the free pool
153  * @instance:           Adapter soft state
154  *
155  * Returns a free command from the pool
156  */
157 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
158                                                   *instance)
159 {
160         unsigned long flags;
161         struct fusion_context *fusion =
162                 (struct fusion_context *)instance->ctrl_context;
163         struct megasas_cmd_fusion *cmd = NULL;
164
165         spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
166
167         if (!list_empty(&fusion->cmd_pool)) {
168                 cmd = list_entry((&fusion->cmd_pool)->next,
169                                  struct megasas_cmd_fusion, list);
170                 list_del_init(&cmd->list);
171         } else {
172                 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
173         }
174
175         spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
176         return cmd;
177 }
178
179 /**
180  * megasas_return_cmd_fusion -  Return a cmd to free command pool
181  * @instance:           Adapter soft state
182  * @cmd:                Command packet to be returned to free command pool
183  */
184 static inline void
185 megasas_return_cmd_fusion(struct megasas_instance *instance,
186                           struct megasas_cmd_fusion *cmd)
187 {
188         unsigned long flags;
189         struct fusion_context *fusion =
190                 (struct fusion_context *)instance->ctrl_context;
191
192         spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
193
194         cmd->scmd = NULL;
195         cmd->sync_cmd_idx = (u32)ULONG_MAX;
196         list_add_tail(&cmd->list, &fusion->cmd_pool);
197
198         spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
199 }
200
201 /**
202  * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
203  * @instance:                           Adapter soft state
204  */
205 static void megasas_teardown_frame_pool_fusion(
206         struct megasas_instance *instance)
207 {
208         int i;
209         struct fusion_context *fusion = instance->ctrl_context;
210
211         u16 max_cmd = instance->max_fw_cmds;
212
213         struct megasas_cmd_fusion *cmd;
214
215         if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
216                 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
217                        "sense pool : %p\n", fusion->sg_dma_pool,
218                        fusion->sense_dma_pool);
219                 return;
220         }
221
222         /*
223          * Return all frames to pool
224          */
225         for (i = 0; i < max_cmd; i++) {
226
227                 cmd = fusion->cmd_list[i];
228
229                 if (cmd->sg_frame)
230                         pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
231                                       cmd->sg_frame_phys_addr);
232
233                 if (cmd->sense)
234                         pci_pool_free(fusion->sense_dma_pool, cmd->sense,
235                                       cmd->sense_phys_addr);
236         }
237
238         /*
239          * Now destroy the pool itself
240          */
241         pci_pool_destroy(fusion->sg_dma_pool);
242         pci_pool_destroy(fusion->sense_dma_pool);
243
244         fusion->sg_dma_pool = NULL;
245         fusion->sense_dma_pool = NULL;
246 }
247
248 /**
249  * megasas_free_cmds_fusion -   Free all the cmds in the free cmd pool
250  * @instance:           Adapter soft state
251  */
252 void
253 megasas_free_cmds_fusion(struct megasas_instance *instance)
254 {
255         int i;
256         struct fusion_context *fusion = instance->ctrl_context;
257
258         u32 max_cmds, req_sz, reply_sz, io_frames_sz;
259
260
261         req_sz = fusion->request_alloc_sz;
262         reply_sz = fusion->reply_alloc_sz;
263         io_frames_sz = fusion->io_frames_alloc_sz;
264
265         max_cmds = instance->max_fw_cmds;
266
267         /* Free descriptors and request Frames memory */
268         if (fusion->req_frames_desc)
269                 dma_free_coherent(&instance->pdev->dev, req_sz,
270                                   fusion->req_frames_desc,
271                                   fusion->req_frames_desc_phys);
272
273         if (fusion->reply_frames_desc) {
274                 pci_pool_free(fusion->reply_frames_desc_pool,
275                               fusion->reply_frames_desc,
276                               fusion->reply_frames_desc_phys);
277                 pci_pool_destroy(fusion->reply_frames_desc_pool);
278         }
279
280         if (fusion->io_request_frames) {
281                 pci_pool_free(fusion->io_request_frames_pool,
282                               fusion->io_request_frames,
283                               fusion->io_request_frames_phys);
284                 pci_pool_destroy(fusion->io_request_frames_pool);
285         }
286
287         /* Free the Fusion frame pool */
288         megasas_teardown_frame_pool_fusion(instance);
289
290         /* Free all the commands in the cmd_list */
291         for (i = 0; i < max_cmds; i++)
292                 kfree(fusion->cmd_list[i]);
293
294         /* Free the cmd_list buffer itself */
295         kfree(fusion->cmd_list);
296         fusion->cmd_list = NULL;
297
298         INIT_LIST_HEAD(&fusion->cmd_pool);
299 }
300
301 /**
302  * megasas_create_frame_pool_fusion -   Creates DMA pool for cmd frames
303  * @instance:                   Adapter soft state
304  *
305  */
306 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
307 {
308         int i;
309         u32 max_cmd;
310         struct fusion_context *fusion;
311         struct megasas_cmd_fusion *cmd;
312         u32 total_sz_chain_frame;
313
314         fusion = instance->ctrl_context;
315         max_cmd = instance->max_fw_cmds;
316
317         total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
318
319         /*
320          * Use DMA pool facility provided by PCI layer
321          */
322
323         fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
324                                               instance->pdev,
325                                               total_sz_chain_frame, 4,
326                                               0);
327         if (!fusion->sg_dma_pool) {
328                 printk(KERN_DEBUG "megasas: failed to setup request pool "
329                        "fusion\n");
330                 return -ENOMEM;
331         }
332         fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
333                                                  instance->pdev,
334                                                  SCSI_SENSE_BUFFERSIZE, 64, 0);
335
336         if (!fusion->sense_dma_pool) {
337                 printk(KERN_DEBUG "megasas: failed to setup sense pool "
338                        "fusion\n");
339                 pci_pool_destroy(fusion->sg_dma_pool);
340                 fusion->sg_dma_pool = NULL;
341                 return -ENOMEM;
342         }
343
344         /*
345          * Allocate and attach a frame to each of the commands in cmd_list
346          */
347         for (i = 0; i < max_cmd; i++) {
348
349                 cmd = fusion->cmd_list[i];
350
351                 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
352                                                GFP_KERNEL,
353                                                &cmd->sg_frame_phys_addr);
354
355                 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
356                                             GFP_KERNEL, &cmd->sense_phys_addr);
357                 /*
358                  * megasas_teardown_frame_pool_fusion() takes care of freeing
359                  * whatever has been allocated
360                  */
361                 if (!cmd->sg_frame || !cmd->sense) {
362                         printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
363                         megasas_teardown_frame_pool_fusion(instance);
364                         return -ENOMEM;
365                 }
366         }
367         return 0;
368 }
369
370 /**
371  * megasas_alloc_cmds_fusion -  Allocates the command packets
372  * @instance:           Adapter soft state
373  *
374  *
375  * Each frame has a 32-bit field called context. This context is used to get
376  * back the megasas_cmd_fusion from the frame when a frame gets completed
377  * In this driver, the 32 bit values are the indices into an array cmd_list.
378  * This array is used only to look up the megasas_cmd_fusion given the context.
379  * The free commands themselves are maintained in a linked list called cmd_pool.
380  *
381  * cmds are formed in the io_request and sg_frame members of the
382  * megasas_cmd_fusion. The context field is used to get a request descriptor
383  * and is used as SMID of the cmd.
384  * SMID value range is from 1 to max_fw_cmds.
385  */
386 int
387 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
388 {
389         int i, j, count;
390         u32 max_cmd, io_frames_sz;
391         struct fusion_context *fusion;
392         struct megasas_cmd_fusion *cmd;
393         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
394         u32 offset;
395         dma_addr_t io_req_base_phys;
396         u8 *io_req_base;
397
398         fusion = instance->ctrl_context;
399
400         max_cmd = instance->max_fw_cmds;
401
402         fusion->req_frames_desc =
403                 dma_alloc_coherent(&instance->pdev->dev,
404                                    fusion->request_alloc_sz,
405                                    &fusion->req_frames_desc_phys, GFP_KERNEL);
406
407         if (!fusion->req_frames_desc) {
408                 printk(KERN_ERR "megasas; Could not allocate memory for "
409                        "request_frames\n");
410                 goto fail_req_desc;
411         }
412
413         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
414         fusion->reply_frames_desc_pool =
415                 pci_pool_create("reply_frames pool", instance->pdev,
416                                 fusion->reply_alloc_sz * count, 16, 0);
417
418         if (!fusion->reply_frames_desc_pool) {
419                 printk(KERN_ERR "megasas; Could not allocate memory for "
420                        "reply_frame pool\n");
421                 goto fail_reply_desc;
422         }
423
424         fusion->reply_frames_desc =
425                 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
426                                &fusion->reply_frames_desc_phys);
427         if (!fusion->reply_frames_desc) {
428                 printk(KERN_ERR "megasas; Could not allocate memory for "
429                        "reply_frame pool\n");
430                 pci_pool_destroy(fusion->reply_frames_desc_pool);
431                 goto fail_reply_desc;
432         }
433
434         reply_desc = fusion->reply_frames_desc;
435         for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
436                 reply_desc->Words = ULLONG_MAX;
437
438         io_frames_sz = fusion->io_frames_alloc_sz;
439
440         fusion->io_request_frames_pool =
441                 pci_pool_create("io_request_frames pool", instance->pdev,
442                                 fusion->io_frames_alloc_sz, 16, 0);
443
444         if (!fusion->io_request_frames_pool) {
445                 printk(KERN_ERR "megasas: Could not allocate memory for "
446                        "io_request_frame pool\n");
447                 goto fail_io_frames;
448         }
449
450         fusion->io_request_frames =
451                 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
452                                &fusion->io_request_frames_phys);
453         if (!fusion->io_request_frames) {
454                 printk(KERN_ERR "megasas: Could not allocate memory for "
455                        "io_request_frames frames\n");
456                 pci_pool_destroy(fusion->io_request_frames_pool);
457                 goto fail_io_frames;
458         }
459
460         /*
461          * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
462          * Allocate the dynamic array first and then allocate individual
463          * commands.
464          */
465         fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
466                                    *max_cmd, GFP_KERNEL);
467
468         if (!fusion->cmd_list) {
469                 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
470                        "memory for cmd_list_fusion\n");
471                 goto fail_cmd_list;
472         }
473
474         memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
475                *max_cmd);
476
477         max_cmd = instance->max_fw_cmds;
478         for (i = 0; i < max_cmd; i++) {
479                 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
480                                               GFP_KERNEL);
481                 if (!fusion->cmd_list[i]) {
482                         printk(KERN_ERR "Could not alloc cmd list fusion\n");
483
484                         for (j = 0; j < i; j++)
485                                 kfree(fusion->cmd_list[j]);
486
487                         kfree(fusion->cmd_list);
488                         fusion->cmd_list = NULL;
489                         goto fail_cmd_list;
490                 }
491         }
492
493         /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
494         io_req_base = fusion->io_request_frames +
495                 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
496         io_req_base_phys = fusion->io_request_frames_phys +
497                 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
498
499         /*
500          * Add all the commands to command pool (fusion->cmd_pool)
501          */
502
503         /* SMID 0 is reserved. Set SMID/index from 1 */
504         for (i = 0; i < max_cmd; i++) {
505                 cmd = fusion->cmd_list[i];
506                 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
507                 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
508                 cmd->index = i + 1;
509                 cmd->scmd = NULL;
510                 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
511                 cmd->instance = instance;
512                 cmd->io_request =
513                         (struct MPI2_RAID_SCSI_IO_REQUEST *)
514                   (io_req_base + offset);
515                 memset(cmd->io_request, 0,
516                        sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
517                 cmd->io_request_phys_addr = io_req_base_phys + offset;
518
519                 list_add_tail(&cmd->list, &fusion->cmd_pool);
520         }
521
522         /*
523          * Create a frame pool and assign one frame to each cmd
524          */
525         if (megasas_create_frame_pool_fusion(instance)) {
526                 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
527                 megasas_free_cmds_fusion(instance);
528                 goto fail_req_desc;
529         }
530
531         return 0;
532
533 fail_cmd_list:
534         pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
535                       fusion->io_request_frames_phys);
536         pci_pool_destroy(fusion->io_request_frames_pool);
537 fail_io_frames:
538         dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
539                           fusion->reply_frames_desc,
540                           fusion->reply_frames_desc_phys);
541         pci_pool_free(fusion->reply_frames_desc_pool,
542                       fusion->reply_frames_desc,
543                       fusion->reply_frames_desc_phys);
544         pci_pool_destroy(fusion->reply_frames_desc_pool);
545
546 fail_reply_desc:
547         dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
548                           fusion->req_frames_desc,
549                           fusion->req_frames_desc_phys);
550 fail_req_desc:
551         return -ENOMEM;
552 }
553
554 /**
555  * wait_and_poll -      Issues a polling command
556  * @instance:                   Adapter soft state
557  * @cmd:                        Command packet to be issued
558  *
559  * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
560  */
561 int
562 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
563 {
564         int i;
565         struct megasas_header *frame_hdr = &cmd->frame->hdr;
566
567         u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
568
569         /*
570          * Wait for cmd_status to change
571          */
572         for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
573                 rmb();
574                 msleep(20);
575         }
576
577         if (frame_hdr->cmd_status == 0xff)
578                 return -ETIME;
579
580         return 0;
581 }
582
583 /**
584  * megasas_ioc_init_fusion -    Initializes the FW
585  * @instance:           Adapter soft state
586  *
587  * Issues the IOC Init cmd
588  */
589 int
590 megasas_ioc_init_fusion(struct megasas_instance *instance)
591 {
592         struct megasas_init_frame *init_frame;
593         struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
594         dma_addr_t      ioc_init_handle;
595         struct megasas_cmd *cmd;
596         u8 ret;
597         struct fusion_context *fusion;
598         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
599         int i;
600         struct megasas_header *frame_hdr;
601
602         fusion = instance->ctrl_context;
603
604         cmd = megasas_get_cmd(instance);
605
606         if (!cmd) {
607                 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
608                 ret = 1;
609                 goto fail_get_cmd;
610         }
611
612         IOCInitMessage =
613           dma_alloc_coherent(&instance->pdev->dev,
614                              sizeof(struct MPI2_IOC_INIT_REQUEST),
615                              &ioc_init_handle, GFP_KERNEL);
616
617         if (!IOCInitMessage) {
618                 printk(KERN_ERR "Could not allocate memory for "
619                        "IOCInitMessage\n");
620                 ret = 1;
621                 goto fail_fw_init;
622         }
623
624         memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
625
626         IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
627         IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
628         IOCInitMessage->MsgVersion = MPI2_VERSION;
629         IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
630         IOCInitMessage->SystemRequestFrameSize =
631                 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
632
633         IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
634         IOCInitMessage->ReplyDescriptorPostQueueAddress =
635                 fusion->reply_frames_desc_phys;
636         IOCInitMessage->SystemRequestFrameBaseAddress =
637                 fusion->io_request_frames_phys;
638         IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
639         init_frame = (struct megasas_init_frame *)cmd->frame;
640         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
641
642         frame_hdr = &cmd->frame->hdr;
643         frame_hdr->cmd_status = 0xFF;
644         frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
645
646         init_frame->cmd = MFI_CMD_INIT;
647         init_frame->cmd_status = 0xFF;
648
649         init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
650         init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
651
652         req_desc =
653           (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
654
655         req_desc->Words = cmd->frame_phys_addr;
656         req_desc->MFAIo.RequestFlags =
657                 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
658                  MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
659
660         /*
661          * disable the intr before firing the init frame
662          */
663         instance->instancet->disable_intr(instance->reg_set);
664
665         for (i = 0; i < (10 * 1000); i += 20) {
666                 if (readl(&instance->reg_set->doorbell) & 1)
667                         msleep(20);
668                 else
669                         break;
670         }
671
672         instance->instancet->fire_cmd(instance, req_desc->u.low,
673                                       req_desc->u.high, instance->reg_set);
674
675         wait_and_poll(instance, cmd);
676
677         frame_hdr = &cmd->frame->hdr;
678         if (frame_hdr->cmd_status != 0) {
679                 ret = 1;
680                 goto fail_fw_init;
681         }
682         printk(KERN_ERR "megasas:IOC Init cmd success\n");
683
684         ret = 0;
685
686 fail_fw_init:
687         megasas_return_cmd(instance, cmd);
688         if (IOCInitMessage)
689                 dma_free_coherent(&instance->pdev->dev,
690                                   sizeof(struct MPI2_IOC_INIT_REQUEST),
691                                   IOCInitMessage, ioc_init_handle);
692 fail_get_cmd:
693         return ret;
694 }
695
696 /*
697  * megasas_get_ld_map_info -    Returns FW's ld_map structure
698  * @instance:                           Adapter soft state
699  * @pend:                               Pend the command or not
700  * Issues an internal command (DCMD) to get the FW's controller PD
701  * list structure.  This information is mainly used to find out SYSTEM
702  * supported by the FW.
703  */
704 static int
705 megasas_get_ld_map_info(struct megasas_instance *instance)
706 {
707         int ret = 0;
708         struct megasas_cmd *cmd;
709         struct megasas_dcmd_frame *dcmd;
710         struct MR_FW_RAID_MAP_ALL *ci;
711         dma_addr_t ci_h = 0;
712         u32 size_map_info;
713         struct fusion_context *fusion;
714
715         cmd = megasas_get_cmd(instance);
716
717         if (!cmd) {
718                 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
719                 return -ENOMEM;
720         }
721
722         fusion = instance->ctrl_context;
723
724         if (!fusion) {
725                 megasas_return_cmd(instance, cmd);
726                 return 1;
727         }
728
729         dcmd = &cmd->frame->dcmd;
730
731         size_map_info = sizeof(struct MR_FW_RAID_MAP) +
732                 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
733
734         ci = fusion->ld_map[(instance->map_id & 1)];
735         ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
736
737         if (!ci) {
738                 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
739                 megasas_return_cmd(instance, cmd);
740                 return -ENOMEM;
741         }
742
743         memset(ci, 0, sizeof(*ci));
744         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
745
746         dcmd->cmd = MFI_CMD_DCMD;
747         dcmd->cmd_status = 0xFF;
748         dcmd->sge_count = 1;
749         dcmd->flags = MFI_FRAME_DIR_READ;
750         dcmd->timeout = 0;
751         dcmd->pad_0 = 0;
752         dcmd->data_xfer_len = size_map_info;
753         dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
754         dcmd->sgl.sge32[0].phys_addr = ci_h;
755         dcmd->sgl.sge32[0].length = size_map_info;
756
757         if (!megasas_issue_polled(instance, cmd))
758                 ret = 0;
759         else {
760                 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
761                 ret = -1;
762         }
763
764         megasas_return_cmd(instance, cmd);
765
766         return ret;
767 }
768
769 u8
770 megasas_get_map_info(struct megasas_instance *instance)
771 {
772         struct fusion_context *fusion = instance->ctrl_context;
773
774         fusion->fast_path_io = 0;
775         if (!megasas_get_ld_map_info(instance)) {
776                 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
777                                        fusion->load_balance_info)) {
778                         fusion->fast_path_io = 1;
779                         return 0;
780                 }
781         }
782         return 1;
783 }
784
785 /*
786  * megasas_sync_map_info -      Returns FW's ld_map structure
787  * @instance:                           Adapter soft state
788  *
789  * Issues an internal command (DCMD) to get the FW's controller PD
790  * list structure.  This information is mainly used to find out SYSTEM
791  * supported by the FW.
792  */
793 int
794 megasas_sync_map_info(struct megasas_instance *instance)
795 {
796         int ret = 0, i;
797         struct megasas_cmd *cmd;
798         struct megasas_dcmd_frame *dcmd;
799         u32 size_sync_info, num_lds;
800         struct fusion_context *fusion;
801         struct MR_LD_TARGET_SYNC *ci = NULL;
802         struct MR_FW_RAID_MAP_ALL *map;
803         struct MR_LD_RAID  *raid;
804         struct MR_LD_TARGET_SYNC *ld_sync;
805         dma_addr_t ci_h = 0;
806         u32 size_map_info;
807
808         cmd = megasas_get_cmd(instance);
809
810         if (!cmd) {
811                 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
812                        "info.\n");
813                 return -ENOMEM;
814         }
815
816         fusion = instance->ctrl_context;
817
818         if (!fusion) {
819                 megasas_return_cmd(instance, cmd);
820                 return 1;
821         }
822
823         map = fusion->ld_map[instance->map_id & 1];
824
825         num_lds = map->raidMap.ldCount;
826
827         dcmd = &cmd->frame->dcmd;
828
829         size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
830
831         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
832
833         ci = (struct MR_LD_TARGET_SYNC *)
834           fusion->ld_map[(instance->map_id - 1) & 1];
835         memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
836
837         ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
838
839         ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
840
841         for (i = 0; i < num_lds; i++, ld_sync++) {
842                 raid = MR_LdRaidGet(i, map);
843                 ld_sync->targetId = MR_GetLDTgtId(i, map);
844                 ld_sync->seqNum = raid->seqNum;
845         }
846
847         size_map_info = sizeof(struct MR_FW_RAID_MAP) +
848                 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
849
850         dcmd->cmd = MFI_CMD_DCMD;
851         dcmd->cmd_status = 0xFF;
852         dcmd->sge_count = 1;
853         dcmd->flags = MFI_FRAME_DIR_WRITE;
854         dcmd->timeout = 0;
855         dcmd->pad_0 = 0;
856         dcmd->data_xfer_len = size_map_info;
857         dcmd->mbox.b[0] = num_lds;
858         dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
859         dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
860         dcmd->sgl.sge32[0].phys_addr = ci_h;
861         dcmd->sgl.sge32[0].length = size_map_info;
862
863         instance->map_update_cmd = cmd;
864
865         instance->instancet->issue_dcmd(instance, cmd);
866
867         return ret;
868 }
869
870 /**
871  * megasas_init_adapter_fusion -        Initializes the FW
872  * @instance:           Adapter soft state
873  *
874  * This is the main function for initializing firmware.
875  */
876 u32
877 megasas_init_adapter_fusion(struct megasas_instance *instance)
878 {
879         struct megasas_register_set __iomem *reg_set;
880         struct fusion_context *fusion;
881         u32 max_cmd;
882         int i = 0, count;
883
884         fusion = instance->ctrl_context;
885
886         reg_set = instance->reg_set;
887
888         /*
889          * Get various operational parameters from status register
890          */
891         instance->max_fw_cmds =
892                 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
893         instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
894
895         /*
896          * Reduce the max supported cmds by 1. This is to ensure that the
897          * reply_q_sz (1 more than the max cmd that driver may send)
898          * does not exceed max cmds that the FW can support
899          */
900         instance->max_fw_cmds = instance->max_fw_cmds-1;
901         /* Only internal cmds (DCMD) need to have MFI frames */
902         instance->max_mfi_cmds = MEGASAS_INT_CMDS;
903
904         max_cmd = instance->max_fw_cmds;
905
906         fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
907
908         fusion->request_alloc_sz =
909                 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
910         fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
911                 *(fusion->reply_q_depth);
912         fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
913                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
914                  (max_cmd + 1)); /* Extra 1 for SMID 0 */
915
916         fusion->max_sge_in_main_msg =
917           (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
918            offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
919
920         fusion->max_sge_in_chain =
921                 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
922
923         instance->max_num_sge = fusion->max_sge_in_main_msg +
924                 fusion->max_sge_in_chain - 2;
925
926         /* Used for pass thru MFI frame (DCMD) */
927         fusion->chain_offset_mfi_pthru =
928                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
929
930         fusion->chain_offset_io_request =
931                 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
932                  sizeof(union MPI2_SGE_IO_UNION))/16;
933
934         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
935         for (i = 0 ; i < count; i++)
936                 fusion->last_reply_idx[i] = 0;
937
938         /*
939          * Allocate memory for descriptors
940          * Create a pool of commands
941          */
942         if (megasas_alloc_cmds(instance))
943                 goto fail_alloc_mfi_cmds;
944         if (megasas_alloc_cmds_fusion(instance))
945                 goto fail_alloc_cmds;
946
947         if (megasas_ioc_init_fusion(instance))
948                 goto fail_ioc_init;
949
950         instance->flag_ieee = 1;
951
952         fusion->map_sz =  sizeof(struct MR_FW_RAID_MAP) +
953           (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
954
955         fusion->fast_path_io = 0;
956
957         for (i = 0; i < 2; i++) {
958                 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
959                                                        fusion->map_sz,
960                                                        &fusion->ld_map_phys[i],
961                                                        GFP_KERNEL);
962                 if (!fusion->ld_map[i]) {
963                         printk(KERN_ERR "megasas: Could not allocate memory "
964                                "for map info\n");
965                         goto fail_map_info;
966                 }
967         }
968
969         if (!megasas_get_map_info(instance))
970                 megasas_sync_map_info(instance);
971
972         return 0;
973
974 fail_map_info:
975         if (i == 1)
976                 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
977                                   fusion->ld_map[0], fusion->ld_map_phys[0]);
978 fail_ioc_init:
979         megasas_free_cmds_fusion(instance);
980 fail_alloc_cmds:
981         megasas_free_cmds(instance);
982 fail_alloc_mfi_cmds:
983         return 1;
984 }
985
986 /**
987  * megasas_fire_cmd_fusion -    Sends command to the FW
988  * @frame_phys_addr :           Physical address of cmd
989  * @frame_count :               Number of frames for the command
990  * @regs :                      MFI register set
991  */
992 void
993 megasas_fire_cmd_fusion(struct megasas_instance *instance,
994                         dma_addr_t req_desc_lo,
995                         u32 req_desc_hi,
996                         struct megasas_register_set __iomem *regs)
997 {
998         unsigned long flags;
999
1000         spin_lock_irqsave(&instance->hba_lock, flags);
1001
1002         writel(req_desc_lo,
1003                &(regs)->inbound_low_queue_port);
1004         writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1005         spin_unlock_irqrestore(&instance->hba_lock, flags);
1006 }
1007
1008 /**
1009  * map_cmd_status -     Maps FW cmd status to OS cmd status
1010  * @cmd :               Pointer to cmd
1011  * @status :            status of cmd returned by FW
1012  * @ext_status :        ext status of cmd returned by FW
1013  */
1014
1015 void
1016 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1017 {
1018
1019         switch (status) {
1020
1021         case MFI_STAT_OK:
1022                 cmd->scmd->result = DID_OK << 16;
1023                 break;
1024
1025         case MFI_STAT_SCSI_IO_FAILED:
1026         case MFI_STAT_LD_INIT_IN_PROGRESS:
1027                 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1028                 break;
1029
1030         case MFI_STAT_SCSI_DONE_WITH_ERROR:
1031
1032                 cmd->scmd->result = (DID_OK << 16) | ext_status;
1033                 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1034                         memset(cmd->scmd->sense_buffer, 0,
1035                                SCSI_SENSE_BUFFERSIZE);
1036                         memcpy(cmd->scmd->sense_buffer, cmd->sense,
1037                                SCSI_SENSE_BUFFERSIZE);
1038                         cmd->scmd->result |= DRIVER_SENSE << 24;
1039                 }
1040                 break;
1041
1042         case MFI_STAT_LD_OFFLINE:
1043         case MFI_STAT_DEVICE_NOT_FOUND:
1044                 cmd->scmd->result = DID_BAD_TARGET << 16;
1045                 break;
1046         case MFI_STAT_CONFIG_SEQ_MISMATCH:
1047                 cmd->scmd->result = DID_IMM_RETRY << 16;
1048                 break;
1049         default:
1050                 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1051                 cmd->scmd->result = DID_ERROR << 16;
1052                 break;
1053         }
1054 }
1055
1056 /**
1057  * megasas_make_sgl_fusion -    Prepares 32-bit SGL
1058  * @instance:           Adapter soft state
1059  * @scp:                SCSI command from the mid-layer
1060  * @sgl_ptr:            SGL to be filled in
1061  * @cmd:                cmd we are working on
1062  *
1063  * If successful, this function returns the number of SG elements.
1064  */
1065 static int
1066 megasas_make_sgl_fusion(struct megasas_instance *instance,
1067                         struct scsi_cmnd *scp,
1068                         struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1069                         struct megasas_cmd_fusion *cmd)
1070 {
1071         int i, sg_processed, sge_count;
1072         struct scatterlist *os_sgl;
1073         struct fusion_context *fusion;
1074
1075         fusion = instance->ctrl_context;
1076
1077         if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1078                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1079                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1080                 sgl_ptr_end->Flags = 0;
1081         }
1082
1083         sge_count = scsi_dma_map(scp);
1084
1085         BUG_ON(sge_count < 0);
1086
1087         if (sge_count > instance->max_num_sge || !sge_count)
1088                 return sge_count;
1089
1090         scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1091                 sgl_ptr->Length = sg_dma_len(os_sgl);
1092                 sgl_ptr->Address = sg_dma_address(os_sgl);
1093                 sgl_ptr->Flags = 0;
1094                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1095                         if (i == sge_count - 1)
1096                                 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1097                 }
1098                 sgl_ptr++;
1099
1100                 sg_processed = i + 1;
1101
1102                 if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
1103                     (sge_count > fusion->max_sge_in_main_msg)) {
1104
1105                         struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1106                         if (instance->pdev->device ==
1107                             PCI_DEVICE_ID_LSI_INVADER) {
1108                                 if ((cmd->io_request->IoFlags &
1109                                 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1110                                 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1111                                         cmd->io_request->ChainOffset =
1112                                                 fusion->
1113                                                 chain_offset_io_request;
1114                                 else
1115                                         cmd->io_request->ChainOffset = 0;
1116                         } else
1117                                 cmd->io_request->ChainOffset =
1118                                         fusion->chain_offset_io_request;
1119
1120                         sg_chain = sgl_ptr;
1121                         /* Prepare chain element */
1122                         sg_chain->NextChainOffset = 0;
1123                         if (instance->pdev->device ==
1124                             PCI_DEVICE_ID_LSI_INVADER)
1125                                 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1126                         else
1127                                 sg_chain->Flags =
1128                                         (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1129                                          MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1130                         sg_chain->Length =  (sizeof(union MPI2_SGE_IO_UNION)
1131                                              *(sge_count - sg_processed));
1132                         sg_chain->Address = cmd->sg_frame_phys_addr;
1133
1134                         sgl_ptr =
1135                           (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1136                 }
1137         }
1138
1139         return sge_count;
1140 }
1141
1142 /**
1143  * megasas_set_pd_lba - Sets PD LBA
1144  * @cdb:                CDB
1145  * @cdb_len:            cdb length
1146  * @start_blk:          Start block of IO
1147  *
1148  * Used to set the PD LBA in CDB for FP IOs
1149  */
1150 void
1151 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1152                    struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1153                    struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1154 {
1155         struct MR_LD_RAID *raid;
1156         u32 ld;
1157         u64 start_blk = io_info->pdBlock;
1158         u8 *cdb = io_request->CDB.CDB32;
1159         u32 num_blocks = io_info->numBlocks;
1160         u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1161
1162         /* Check if T10 PI (DIF) is enabled for this LD */
1163         ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1164         raid = MR_LdRaidGet(ld, local_map_ptr);
1165         if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1166                 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1167                 cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1168                 cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;
1169
1170                 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1171                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1172                 else
1173                         cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1174                 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1175
1176                 /* LBA */
1177                 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1178                 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1179                 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1180                 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1181                 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1182                 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1183                 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1184                 cdb[19] = (u8)(start_blk & 0xff);
1185
1186                 /* Logical block reference tag */
1187                 io_request->CDB.EEDP32.PrimaryReferenceTag =
1188                         cpu_to_be32(ref_tag);
1189                 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1190
1191                 io_request->DataLength = num_blocks * 512;
1192                 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1193
1194                 /* Transfer length */
1195                 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1196                 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1197                 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1198                 cdb[31] = (u8)(num_blocks & 0xff);
1199
1200                 /* set SCSI IO EEDPFlags */
1201                 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1202                         io_request->EEDPFlags =
1203                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
1204                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1205                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1206                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1207                                 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1208                 } else {
1209                         io_request->EEDPFlags =
1210                                 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1211                                 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1212                 }
1213                 io_request->Control |= (0x4 << 26);
1214                 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1215         } else {
1216                 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1217                 if (((cdb_len == 12) || (cdb_len == 16)) &&
1218                     (start_blk <= 0xffffffff)) {
1219                         if (cdb_len == 16) {
1220                                 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1221                                 flagvals = cdb[1];
1222                                 groupnum = cdb[14];
1223                                 control = cdb[15];
1224                         } else {
1225                                 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1226                                 flagvals = cdb[1];
1227                                 groupnum = cdb[10];
1228                                 control = cdb[11];
1229                         }
1230
1231                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1232
1233                         cdb[0] = opcode;
1234                         cdb[1] = flagvals;
1235                         cdb[6] = groupnum;
1236                         cdb[9] = control;
1237
1238                         /* Transfer length */
1239                         cdb[8] = (u8)(num_blocks & 0xff);
1240                         cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1241
1242                         io_request->IoFlags = 10; /* Specify 10-byte cdb */
1243                         cdb_len = 10;
1244                 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1245                         /* Convert to 16 byte CDB for large LBA's */
1246                         switch (cdb_len) {
1247                         case 6:
1248                                 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1249                                 control = cdb[5];
1250                                 break;
1251                         case 10:
1252                                 opcode =
1253                                         cdb[0] == READ_10 ? READ_16 : WRITE_16;
1254                                 flagvals = cdb[1];
1255                                 groupnum = cdb[6];
1256                                 control = cdb[9];
1257                                 break;
1258                         case 12:
1259                                 opcode =
1260                                         cdb[0] == READ_12 ? READ_16 : WRITE_16;
1261                                 flagvals = cdb[1];
1262                                 groupnum = cdb[10];
1263                                 control = cdb[11];
1264                                 break;
1265                         }
1266
1267                         memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1268
1269                         cdb[0] = opcode;
1270                         cdb[1] = flagvals;
1271                         cdb[14] = groupnum;
1272                         cdb[15] = control;
1273
1274                         /* Transfer length */
1275                         cdb[13] = (u8)(num_blocks & 0xff);
1276                         cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1277                         cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1278                         cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1279
1280                         io_request->IoFlags = 16; /* Specify 16-byte cdb */
1281                         cdb_len = 16;
1282                 }
1283
1284                 /* Normal case, just load LBA here */
1285                 switch (cdb_len) {
1286                 case 6:
1287                 {
1288                         u8 val = cdb[1] & 0xE0;
1289                         cdb[3] = (u8)(start_blk & 0xff);
1290                         cdb[2] = (u8)((start_blk >> 8) & 0xff);
1291                         cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1292                         break;
1293                 }
1294                 case 10:
1295                         cdb[5] = (u8)(start_blk & 0xff);
1296                         cdb[4] = (u8)((start_blk >> 8) & 0xff);
1297                         cdb[3] = (u8)((start_blk >> 16) & 0xff);
1298                         cdb[2] = (u8)((start_blk >> 24) & 0xff);
1299                         break;
1300                 case 12:
1301                         cdb[5]    = (u8)(start_blk & 0xff);
1302                         cdb[4]    = (u8)((start_blk >> 8) & 0xff);
1303                         cdb[3]    = (u8)((start_blk >> 16) & 0xff);
1304                         cdb[2]    = (u8)((start_blk >> 24) & 0xff);
1305                         break;
1306                 case 16:
1307                         cdb[9]    = (u8)(start_blk & 0xff);
1308                         cdb[8]    = (u8)((start_blk >> 8) & 0xff);
1309                         cdb[7]    = (u8)((start_blk >> 16) & 0xff);
1310                         cdb[6]    = (u8)((start_blk >> 24) & 0xff);
1311                         cdb[5]    = (u8)((start_blk >> 32) & 0xff);
1312                         cdb[4]    = (u8)((start_blk >> 40) & 0xff);
1313                         cdb[3]    = (u8)((start_blk >> 48) & 0xff);
1314                         cdb[2]    = (u8)((start_blk >> 56) & 0xff);
1315                         break;
1316                 }
1317         }
1318 }
1319
1320 /**
1321  * megasas_build_ldio_fusion -  Prepares IOs to devices
1322  * @instance:           Adapter soft state
1323  * @scp:                SCSI command
1324  * @cmd:                Command to be prepared
1325  *
1326  * Prepares the io_request and chain elements (sg_frame) for IO
1327  * The IO can be for PD (Fast Path) or LD
1328  */
1329 void
1330 megasas_build_ldio_fusion(struct megasas_instance *instance,
1331                           struct scsi_cmnd *scp,
1332                           struct megasas_cmd_fusion *cmd)
1333 {
1334         u8 fp_possible;
1335         u32 start_lba_lo, start_lba_hi, device_id;
1336         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1337         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1338         struct IO_REQUEST_INFO io_info;
1339         struct fusion_context *fusion;
1340         struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1341
1342         device_id = MEGASAS_DEV_INDEX(instance, scp);
1343
1344         fusion = instance->ctrl_context;
1345
1346         io_request = cmd->io_request;
1347         io_request->RaidContext.VirtualDiskTgtId = device_id;
1348         io_request->RaidContext.status = 0;
1349         io_request->RaidContext.exStatus = 0;
1350
1351         req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1352
1353         start_lba_lo = 0;
1354         start_lba_hi = 0;
1355         fp_possible = 0;
1356
1357         /*
1358          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1359          */
1360         if (scp->cmd_len == 6) {
1361                 io_request->DataLength = (u32) scp->cmnd[4];
1362                 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1363                         ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1364
1365                 start_lba_lo &= 0x1FFFFF;
1366         }
1367
1368         /*
1369          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1370          */
1371         else if (scp->cmd_len == 10) {
1372                 io_request->DataLength = (u32) scp->cmnd[8] |
1373                         ((u32) scp->cmnd[7] << 8);
1374                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1375                         ((u32) scp->cmnd[3] << 16) |
1376                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1377         }
1378
1379         /*
1380          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1381          */
1382         else if (scp->cmd_len == 12) {
1383                 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1384                         ((u32) scp->cmnd[7] << 16) |
1385                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1386                 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1387                         ((u32) scp->cmnd[3] << 16) |
1388                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1389         }
1390
1391         /*
1392          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1393          */
1394         else if (scp->cmd_len == 16) {
1395                 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1396                         ((u32) scp->cmnd[11] << 16) |
1397                         ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1398                 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1399                         ((u32) scp->cmnd[7] << 16) |
1400                         ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1401
1402                 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1403                         ((u32) scp->cmnd[3] << 16) |
1404                         ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1405         }
1406
1407         memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1408         io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1409         io_info.numBlocks = io_request->DataLength;
1410         io_info.ldTgtId = device_id;
1411
1412         if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1413                 io_info.isRead = 1;
1414
1415         local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1416
1417         if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1418              MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1419                 io_request->RaidContext.regLockFlags  = 0;
1420                 fp_possible = 0;
1421         } else {
1422                 if (MR_BuildRaidContext(instance, &io_info,
1423                                         &io_request->RaidContext,
1424                                         local_map_ptr))
1425                         fp_possible = io_info.fpOkForIo;
1426         }
1427
1428         /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1429            id by default, not CPU group id, otherwise all MSI-X queues won't
1430            be utilized */
1431         cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1432                 smp_processor_id() % instance->msix_vectors : 0;
1433
1434         if (fp_possible) {
1435                 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1436                                    local_map_ptr, start_lba_lo);
1437                 io_request->DataLength = scsi_bufflen(scp);
1438                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1439                 cmd->request_desc->SCSIIO.RequestFlags =
1440                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1441                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1442                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1443                         if (io_request->RaidContext.regLockFlags ==
1444                             REGION_TYPE_UNUSED)
1445                                 cmd->request_desc->SCSIIO.RequestFlags =
1446                                         (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1447                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1448                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1449                         io_request->RaidContext.nseg = 0x1;
1450                         io_request->IoFlags |=
1451                           MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1452                         io_request->RaidContext.regLockFlags |=
1453                           (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1454                            MR_RL_FLAGS_SEQ_NUM_ENABLE);
1455                 }
1456                 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1457                     (io_info.isRead)) {
1458                         io_info.devHandle =
1459                                 get_updated_dev_handle(
1460                                         &fusion->load_balance_info[device_id],
1461                                         &io_info);
1462                         scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1463                 } else
1464                         scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1465                 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1466                 io_request->DevHandle = io_info.devHandle;
1467         } else {
1468                 io_request->RaidContext.timeoutValue =
1469                         local_map_ptr->raidMap.fpPdIoTimeoutSec;
1470                 cmd->request_desc->SCSIIO.RequestFlags =
1471                         (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1472                          << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1473                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1474                         if (io_request->RaidContext.regLockFlags ==
1475                             REGION_TYPE_UNUSED)
1476                                 cmd->request_desc->SCSIIO.RequestFlags =
1477                                         (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1478                                         MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1479                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1480                         io_request->RaidContext.regLockFlags |=
1481                                 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1482                                  MR_RL_FLAGS_SEQ_NUM_ENABLE);
1483                         io_request->RaidContext.nseg = 0x1;
1484                 }
1485                 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1486                 io_request->DevHandle = device_id;
1487         } /* Not FP */
1488 }
1489
1490 /**
1491  * megasas_build_dcdb_fusion -  Prepares IOs to devices
1492  * @instance:           Adapter soft state
1493  * @scp:                SCSI command
1494  * @cmd:                Command to be prepared
1495  *
1496  * Prepares the io_request frame for non-io cmds
1497  */
1498 static void
1499 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1500                           struct scsi_cmnd *scmd,
1501                           struct megasas_cmd_fusion *cmd)
1502 {
1503         u32 device_id;
1504         struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1505         u16 pd_index = 0;
1506         struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1507         struct fusion_context *fusion = instance->ctrl_context;
1508
1509         io_request = cmd->io_request;
1510         device_id = MEGASAS_DEV_INDEX(instance, scmd);
1511         pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1512                 +scmd->device->id;
1513         local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1514
1515         /* Check if this is a system PD I/O */
1516         if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1517                 io_request->Function = 0;
1518                 io_request->DevHandle =
1519                         local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1520                 io_request->RaidContext.timeoutValue =
1521                         local_map_ptr->raidMap.fpPdIoTimeoutSec;
1522                 io_request->RaidContext.regLockFlags = 0;
1523                 io_request->RaidContext.regLockRowLBA = 0;
1524                 io_request->RaidContext.regLockLength = 0;
1525                 io_request->RaidContext.RAIDFlags =
1526                         MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1527                         MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1528                 cmd->request_desc->SCSIIO.RequestFlags =
1529                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1530                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1531         } else {
1532                 io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1533                 io_request->DevHandle = device_id;
1534                 cmd->request_desc->SCSIIO.RequestFlags =
1535                         (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1536                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1537         }
1538         io_request->RaidContext.VirtualDiskTgtId = device_id;
1539         io_request->LUN[1] = scmd->device->lun;
1540         io_request->DataLength = scsi_bufflen(scmd);
1541 }
1542
1543 /**
1544  * megasas_build_io_fusion -    Prepares IOs to devices
1545  * @instance:           Adapter soft state
1546  * @scp:                SCSI command
1547  * @cmd:                Command to be prepared
1548  *
1549  * Invokes helper functions to prepare request frames
1550  * and sets flags appropriate for IO/Non-IO cmd
1551  */
1552 int
1553 megasas_build_io_fusion(struct megasas_instance *instance,
1554                         struct scsi_cmnd *scp,
1555                         struct megasas_cmd_fusion *cmd)
1556 {
1557         u32 device_id, sge_count;
1558         struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1559
1560         device_id = MEGASAS_DEV_INDEX(instance, scp);
1561
1562         /* Zero out some fields so they don't get reused */
1563         io_request->LUN[1] = 0;
1564         io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1565         io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1566         io_request->EEDPFlags = 0;
1567         io_request->Control = 0;
1568         io_request->EEDPBlockSize = 0;
1569         io_request->ChainOffset = 0;
1570         io_request->RaidContext.RAIDFlags = 0;
1571         io_request->RaidContext.Type = 0;
1572         io_request->RaidContext.nseg = 0;
1573
1574         memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1575         /*
1576          * Just the CDB length,rest of the Flags are zero
1577          * This will be modified for FP in build_ldio_fusion
1578          */
1579         io_request->IoFlags = scp->cmd_len;
1580
1581         if (megasas_is_ldio(scp))
1582                 megasas_build_ldio_fusion(instance, scp, cmd);
1583         else
1584                 megasas_build_dcdb_fusion(instance, scp, cmd);
1585
1586         /*
1587          * Construct SGL
1588          */
1589
1590         sge_count =
1591                 megasas_make_sgl_fusion(instance, scp,
1592                                         (struct MPI25_IEEE_SGE_CHAIN64 *)
1593                                         &io_request->SGL, cmd);
1594
1595         if (sge_count > instance->max_num_sge) {
1596                 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1597                        "max (0x%x) allowed\n", sge_count,
1598                        instance->max_num_sge);
1599                 return 1;
1600         }
1601
1602         io_request->RaidContext.numSGE = sge_count;
1603
1604         io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1605
1606         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1607                 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1608         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1609                 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1610
1611         io_request->SGLOffset0 =
1612                 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1613
1614         io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1615         io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1616
1617         cmd->scmd = scp;
1618         scp->SCp.ptr = (char *)cmd;
1619
1620         return 0;
1621 }
1622
1623 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1624 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1625 {
1626         u8 *p;
1627         struct fusion_context *fusion;
1628
1629         if (index >= instance->max_fw_cmds) {
1630                 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1631                        "descriptor\n", index);
1632                 return NULL;
1633         }
1634         fusion = instance->ctrl_context;
1635         p = fusion->req_frames_desc
1636                 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1637
1638         return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1639 }
1640
1641 /**
1642  * megasas_build_and_issue_cmd_fusion -Main routine for building and
1643  *                                     issuing non IOCTL cmd
1644  * @instance:                   Adapter soft state
1645  * @scmd:                       pointer to scsi cmd from OS
1646  */
1647 static u32
1648 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1649                                    struct scsi_cmnd *scmd)
1650 {
1651         struct megasas_cmd_fusion *cmd;
1652         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1653         u32 index;
1654         struct fusion_context *fusion;
1655
1656         fusion = instance->ctrl_context;
1657
1658         cmd = megasas_get_cmd_fusion(instance);
1659         if (!cmd)
1660                 return SCSI_MLQUEUE_HOST_BUSY;
1661
1662         index = cmd->index;
1663
1664         req_desc = megasas_get_request_descriptor(instance, index-1);
1665         if (!req_desc)
1666                 return 1;
1667
1668         req_desc->Words = 0;
1669         cmd->request_desc = req_desc;
1670
1671         if (megasas_build_io_fusion(instance, scmd, cmd)) {
1672                 megasas_return_cmd_fusion(instance, cmd);
1673                 printk(KERN_ERR "megasas: Error building command.\n");
1674                 cmd->request_desc = NULL;
1675                 return 1;
1676         }
1677
1678         req_desc = cmd->request_desc;
1679         req_desc->SCSIIO.SMID = index;
1680
1681         if (cmd->io_request->ChainOffset != 0 &&
1682             cmd->io_request->ChainOffset != 0xF)
1683                 printk(KERN_ERR "megasas: The chain offset value is not "
1684                        "correct : %x\n", cmd->io_request->ChainOffset);
1685
1686         /*
1687          * Issue the command to the FW
1688          */
1689         atomic_inc(&instance->fw_outstanding);
1690
1691         instance->instancet->fire_cmd(instance,
1692                                       req_desc->u.low, req_desc->u.high,
1693                                       instance->reg_set);
1694
1695         return 0;
1696 }
1697
1698 /**
1699  * complete_cmd_fusion -        Completes command
1700  * @instance:                   Adapter soft state
1701  * Completes all commands that is in reply descriptor queue
1702  */
1703 int
1704 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1705 {
1706         union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1707         struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1708         struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1709         struct fusion_context *fusion;
1710         struct megasas_cmd *cmd_mfi;
1711         struct megasas_cmd_fusion *cmd_fusion;
1712         u16 smid, num_completed;
1713         u8 reply_descript_type, arm;
1714         u32 status, extStatus, device_id;
1715         union desc_value d_val;
1716         struct LD_LOAD_BALANCE_INFO *lbinfo;
1717
1718         fusion = instance->ctrl_context;
1719
1720         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1721                 return IRQ_HANDLED;
1722
1723         desc = fusion->reply_frames_desc;
1724         desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1725                  sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1726                 fusion->last_reply_idx[MSIxIndex];
1727
1728         reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1729
1730         d_val.word = desc->Words;
1731
1732         reply_descript_type = reply_desc->ReplyFlags &
1733                 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1734
1735         if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1736                 return IRQ_NONE;
1737
1738         d_val.word = desc->Words;
1739
1740         num_completed = 0;
1741
1742         while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1743                 smid = reply_desc->SMID;
1744
1745                 cmd_fusion = fusion->cmd_list[smid - 1];
1746
1747                 scsi_io_req =
1748                         (struct MPI2_RAID_SCSI_IO_REQUEST *)
1749                   cmd_fusion->io_request;
1750
1751                 if (cmd_fusion->scmd)
1752                         cmd_fusion->scmd->SCp.ptr = NULL;
1753
1754                 status = scsi_io_req->RaidContext.status;
1755                 extStatus = scsi_io_req->RaidContext.exStatus;
1756
1757                 switch (scsi_io_req->Function) {
1758                 case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
1759                         /* Update load balancing info */
1760                         device_id = MEGASAS_DEV_INDEX(instance,
1761                                                       cmd_fusion->scmd);
1762                         lbinfo = &fusion->load_balance_info[device_id];
1763                         if (cmd_fusion->scmd->SCp.Status &
1764                             MEGASAS_LOAD_BALANCE_FLAG) {
1765                                 arm = lbinfo->raid1DevHandle[0] ==
1766                                         cmd_fusion->io_request->DevHandle ? 0 :
1767                                         1;
1768                                 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1769                                 cmd_fusion->scmd->SCp.Status &=
1770                                         ~MEGASAS_LOAD_BALANCE_FLAG;
1771                         }
1772                         if (reply_descript_type ==
1773                             MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1774                                 if (megasas_dbg_lvl == 5)
1775                                         printk(KERN_ERR "\nmegasas: FAST Path "
1776                                                "IO Success\n");
1777                         }
1778                         /* Fall thru and complete IO */
1779                 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1780                         /* Map the FW Cmd Status */
1781                         map_cmd_status(cmd_fusion, status, extStatus);
1782                         scsi_dma_unmap(cmd_fusion->scmd);
1783                         cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1784                         scsi_io_req->RaidContext.status = 0;
1785                         scsi_io_req->RaidContext.exStatus = 0;
1786                         megasas_return_cmd_fusion(instance, cmd_fusion);
1787                         atomic_dec(&instance->fw_outstanding);
1788
1789                         break;
1790                 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1791                         cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1792                         megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1793                         cmd_fusion->flags = 0;
1794                         megasas_return_cmd_fusion(instance, cmd_fusion);
1795
1796                         break;
1797                 }
1798
1799                 fusion->last_reply_idx[MSIxIndex]++;
1800                 if (fusion->last_reply_idx[MSIxIndex] >=
1801                     fusion->reply_q_depth)
1802                         fusion->last_reply_idx[MSIxIndex] = 0;
1803
1804                 desc->Words = ULLONG_MAX;
1805                 num_completed++;
1806
1807                 /* Get the next reply descriptor */
1808                 if (!fusion->last_reply_idx[MSIxIndex])
1809                         desc = fusion->reply_frames_desc +
1810                                 ((MSIxIndex * fusion->reply_alloc_sz)/
1811                                  sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1812                 else
1813                         desc++;
1814
1815                 reply_desc =
1816                   (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1817
1818                 d_val.word = desc->Words;
1819
1820                 reply_descript_type = reply_desc->ReplyFlags &
1821                         MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1822
1823                 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1824                         break;
1825         }
1826
1827         if (!num_completed)
1828                 return IRQ_NONE;
1829
1830         wmb();
1831         writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex],
1832                &instance->reg_set->reply_post_host_index);
1833         megasas_check_and_restore_queue_depth(instance);
1834         return IRQ_HANDLED;
1835 }
1836
1837 /**
1838  * megasas_complete_cmd_dpc_fusion -    Completes command
1839  * @instance:                   Adapter soft state
1840  *
1841  * Tasklet to complete cmds
1842  */
1843 void
1844 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1845 {
1846         struct megasas_instance *instance =
1847                 (struct megasas_instance *)instance_addr;
1848         unsigned long flags;
1849         u32 count, MSIxIndex;
1850
1851         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1852
1853         /* If we have already declared adapter dead, donot complete cmds */
1854         spin_lock_irqsave(&instance->hba_lock, flags);
1855         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1856                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1857                 return;
1858         }
1859         spin_unlock_irqrestore(&instance->hba_lock, flags);
1860
1861         spin_lock_irqsave(&instance->completion_lock, flags);
1862         for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
1863                 complete_cmd_fusion(instance, MSIxIndex);
1864         spin_unlock_irqrestore(&instance->completion_lock, flags);
1865 }
1866
1867 /**
1868  * megasas_isr_fusion - isr entry point
1869  */
1870 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1871 {
1872         struct megasas_irq_context *irq_context = devp;
1873         struct megasas_instance *instance = irq_context->instance;
1874         u32 mfiStatus, fw_state;
1875
1876         if (!instance->msix_vectors) {
1877                 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1878                 if (!mfiStatus)
1879                         return IRQ_NONE;
1880         }
1881
1882         /* If we are resetting, bail */
1883         if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1884                 instance->instancet->clear_intr(instance->reg_set);
1885                 return IRQ_HANDLED;
1886         }
1887
1888         if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
1889                 instance->instancet->clear_intr(instance->reg_set);
1890                 /* If we didn't complete any commands, check for FW fault */
1891                 fw_state = instance->instancet->read_fw_status_reg(
1892                         instance->reg_set) & MFI_STATE_MASK;
1893                 if (fw_state == MFI_STATE_FAULT)
1894                         schedule_work(&instance->work_init);
1895         }
1896
1897         return IRQ_HANDLED;
1898 }
1899
1900 /**
1901  * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1902  * @instance:                   Adapter soft state
1903  * mfi_cmd:                     megasas_cmd pointer
1904  *
1905  */
1906 u8
1907 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1908                         struct megasas_cmd *mfi_cmd)
1909 {
1910         struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1911         struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1912         struct megasas_cmd_fusion *cmd;
1913         struct fusion_context *fusion;
1914         struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1915
1916         cmd = megasas_get_cmd_fusion(instance);
1917         if (!cmd)
1918                 return 1;
1919
1920         /*  Save the smid. To be used for returning the cmd */
1921         mfi_cmd->context.smid = cmd->index;
1922
1923         cmd->sync_cmd_idx = mfi_cmd->index;
1924
1925         /*
1926          * For cmds where the flag is set, store the flag and check
1927          * on completion. For cmds with this flag, don't call
1928          * megasas_complete_cmd
1929          */
1930
1931         if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1932                 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1933
1934         fusion = instance->ctrl_context;
1935         io_req = cmd->io_request;
1936
1937         if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1938                 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
1939                         (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
1940                 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1941                 sgl_ptr_end->Flags = 0;
1942         }
1943
1944         mpi25_ieee_chain =
1945           (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1946
1947         io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1948         io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1949                                        SGL) / 4;
1950         io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1951
1952         mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1953
1954         mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1955                 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1956
1957         mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1958
1959         return 0;
1960 }
1961
1962 /**
1963  * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1964  * @instance:                   Adapter soft state
1965  * @cmd:                        mfi cmd to build
1966  *
1967  */
1968 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1969 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1970 {
1971         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1972         u16 index;
1973
1974         if (build_mpt_mfi_pass_thru(instance, cmd)) {
1975                 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1976                 return NULL;
1977         }
1978
1979         index = cmd->context.smid;
1980
1981         req_desc = megasas_get_request_descriptor(instance, index - 1);
1982
1983         if (!req_desc)
1984                 return NULL;
1985
1986         req_desc->Words = 0;
1987         req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1988                                          MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1989
1990         req_desc->SCSIIO.SMID = index;
1991
1992         return req_desc;
1993 }
1994
1995 /**
1996  * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1997  * @instance:                   Adapter soft state
1998  * @cmd:                        mfi cmd pointer
1999  *
2000  */
2001 void
2002 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2003                           struct megasas_cmd *cmd)
2004 {
2005         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2006
2007         req_desc = build_mpt_cmd(instance, cmd);
2008         if (!req_desc) {
2009                 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2010                 return;
2011         }
2012         instance->instancet->fire_cmd(instance, req_desc->u.low,
2013                                       req_desc->u.high, instance->reg_set);
2014 }
2015
2016 /**
2017  * megasas_release_fusion -     Reverses the FW initialization
2018  * @intance:                    Adapter soft state
2019  */
2020 void
2021 megasas_release_fusion(struct megasas_instance *instance)
2022 {
2023         megasas_free_cmds(instance);
2024         megasas_free_cmds_fusion(instance);
2025
2026         iounmap(instance->reg_set);
2027
2028         pci_release_selected_regions(instance->pdev, instance->bar);
2029 }
2030
2031 /**
2032  * megasas_read_fw_status_reg_fusion - returns the current FW status value
2033  * @regs:                       MFI register set
2034  */
2035 static u32
2036 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2037 {
2038         return readl(&(regs)->outbound_scratch_pad);
2039 }
2040
2041 /**
2042  * megasas_adp_reset_fusion -   For controller reset
2043  * @regs:                               MFI register set
2044  */
2045 static int
2046 megasas_adp_reset_fusion(struct megasas_instance *instance,
2047                          struct megasas_register_set __iomem *regs)
2048 {
2049         return 0;
2050 }
2051
2052 /**
2053  * megasas_check_reset_fusion - For controller reset check
2054  * @regs:                               MFI register set
2055  */
2056 static int
2057 megasas_check_reset_fusion(struct megasas_instance *instance,
2058                            struct megasas_register_set __iomem *regs)
2059 {
2060         return 0;
2061 }
2062
2063 /* This function waits for outstanding commands on fusion to complete */
2064 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2065 {
2066         int i, outstanding, retval = 0;
2067         u32 fw_state;
2068
2069         for (i = 0; i < resetwaittime; i++) {
2070                 /* Check if firmware is in fault state */
2071                 fw_state = instance->instancet->read_fw_status_reg(
2072                         instance->reg_set) & MFI_STATE_MASK;
2073                 if (fw_state == MFI_STATE_FAULT) {
2074                         printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2075                                " will reset adapter.\n");
2076                         retval = 1;
2077                         goto out;
2078                 }
2079
2080                 outstanding = atomic_read(&instance->fw_outstanding);
2081                 if (!outstanding)
2082                         goto out;
2083
2084                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2085                         printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2086                                "commands to complete\n", i, outstanding);
2087                         megasas_complete_cmd_dpc_fusion(
2088                                 (unsigned long)instance);
2089                 }
2090                 msleep(1000);
2091         }
2092
2093         if (atomic_read(&instance->fw_outstanding)) {
2094                 printk("megaraid_sas: pending commands remain after waiting, "
2095                        "will reset adapter.\n");
2096                 retval = 1;
2097         }
2098 out:
2099         return retval;
2100 }
2101
2102 void  megasas_reset_reply_desc(struct megasas_instance *instance)
2103 {
2104         int i, count;
2105         struct fusion_context *fusion;
2106         union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2107
2108         fusion = instance->ctrl_context;
2109         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2110         for (i = 0 ; i < count ; i++)
2111                 fusion->last_reply_idx[i] = 0;
2112         reply_desc = fusion->reply_frames_desc;
2113         for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2114                 reply_desc->Words = ULLONG_MAX;
2115 }
2116
2117 /* Core fusion reset function */
2118 int megasas_reset_fusion(struct Scsi_Host *shost)
2119 {
2120         int retval = SUCCESS, i, j, retry = 0;
2121         struct megasas_instance *instance;
2122         struct megasas_cmd_fusion *cmd_fusion;
2123         struct fusion_context *fusion;
2124         struct megasas_cmd *cmd_mfi;
2125         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2126         u32 host_diag, abs_state, status_reg, reset_adapter;
2127
2128         instance = (struct megasas_instance *)shost->hostdata;
2129         fusion = instance->ctrl_context;
2130
2131         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2132                 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2133                        "returning FAILED.\n");
2134                 return FAILED;
2135         }
2136
2137         mutex_lock(&instance->reset_mutex);
2138         set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2139         instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2140         instance->instancet->disable_intr(instance->reg_set);
2141         msleep(1000);
2142
2143         /* First try waiting for commands to complete */
2144         if (megasas_wait_for_outstanding_fusion(instance)) {
2145                 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2146                        "adapter.\n");
2147                 /* Now return commands back to the OS */
2148                 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2149                         cmd_fusion = fusion->cmd_list[i];
2150                         if (cmd_fusion->scmd) {
2151                                 scsi_dma_unmap(cmd_fusion->scmd);
2152                                 cmd_fusion->scmd->result = (DID_RESET << 16);
2153                                 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2154                                 megasas_return_cmd_fusion(instance, cmd_fusion);
2155                                 atomic_dec(&instance->fw_outstanding);
2156                         }
2157                 }
2158
2159                 status_reg = instance->instancet->read_fw_status_reg(
2160                         instance->reg_set);
2161                 abs_state = status_reg & MFI_STATE_MASK;
2162                 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2163                 if (instance->disableOnlineCtrlReset ||
2164                     (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2165                         /* Reset not supported, kill adapter */
2166                         printk(KERN_WARNING "megaraid_sas: Reset not supported"
2167                                ", killing adapter.\n");
2168                         megaraid_sas_kill_hba(instance);
2169                         instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2170                         retval = FAILED;
2171                         goto out;
2172                 }
2173
2174                 /* Now try to reset the chip */
2175                 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2176                         writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2177                                &instance->reg_set->fusion_seq_offset);
2178                         writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2179                                &instance->reg_set->fusion_seq_offset);
2180                         writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2181                                &instance->reg_set->fusion_seq_offset);
2182                         writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2183                                &instance->reg_set->fusion_seq_offset);
2184                         writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2185                                &instance->reg_set->fusion_seq_offset);
2186                         writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2187                                &instance->reg_set->fusion_seq_offset);
2188                         writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2189                                &instance->reg_set->fusion_seq_offset);
2190
2191                         /* Check that the diag write enable (DRWE) bit is on */
2192                         host_diag = readl(&instance->reg_set->fusion_host_diag);
2193                         retry = 0;
2194                         while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2195                                 msleep(100);
2196                                 host_diag =
2197                                 readl(&instance->reg_set->fusion_host_diag);
2198                                 if (retry++ == 100) {
2199                                         printk(KERN_WARNING "megaraid_sas: "
2200                                                "Host diag unlock failed!\n");
2201                                         break;
2202                                 }
2203                         }
2204                         if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2205                                 continue;
2206
2207                         /* Send chip reset command */
2208                         writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2209                                &instance->reg_set->fusion_host_diag);
2210                         msleep(3000);
2211
2212                         /* Make sure reset adapter bit is cleared */
2213                         host_diag = readl(&instance->reg_set->fusion_host_diag);
2214                         retry = 0;
2215                         while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2216                                 msleep(100);
2217                                 host_diag =
2218                                 readl(&instance->reg_set->fusion_host_diag);
2219                                 if (retry++ == 1000) {
2220                                         printk(KERN_WARNING "megaraid_sas: "
2221                                                "Diag reset adapter never "
2222                                                "cleared!\n");
2223                                         break;
2224                                 }
2225                         }
2226                         if (host_diag & HOST_DIAG_RESET_ADAPTER)
2227                                 continue;
2228
2229                         abs_state =
2230                                 instance->instancet->read_fw_status_reg(
2231                                         instance->reg_set) & MFI_STATE_MASK;
2232                         retry = 0;
2233
2234                         while ((abs_state <= MFI_STATE_FW_INIT) &&
2235                                (retry++ < 1000)) {
2236                                 msleep(100);
2237                                 abs_state =
2238                                 instance->instancet->read_fw_status_reg(
2239                                         instance->reg_set) & MFI_STATE_MASK;
2240                         }
2241                         if (abs_state <= MFI_STATE_FW_INIT) {
2242                                 printk(KERN_WARNING "megaraid_sas: firmware "
2243                                        "state < MFI_STATE_FW_INIT, state = "
2244                                        "0x%x\n", abs_state);
2245                                 continue;
2246                         }
2247
2248                         /* Wait for FW to become ready */
2249                         if (megasas_transition_to_ready(instance, 1)) {
2250                                 printk(KERN_WARNING "megaraid_sas: Failed to "
2251                                        "transition controller to ready.\n");
2252                                 continue;
2253                         }
2254
2255                         megasas_reset_reply_desc(instance);
2256                         if (megasas_ioc_init_fusion(instance)) {
2257                                 printk(KERN_WARNING "megaraid_sas: "
2258                                        "megasas_ioc_init_fusion() failed!\n");
2259                                 continue;
2260                         }
2261
2262                         clear_bit(MEGASAS_FUSION_IN_RESET,
2263                                   &instance->reset_flags);
2264                         instance->instancet->enable_intr(instance->reg_set);
2265                         instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2266
2267                         /* Re-fire management commands */
2268                         for (j = 0 ; j < instance->max_fw_cmds; j++) {
2269                                 cmd_fusion = fusion->cmd_list[j];
2270                                 if (cmd_fusion->sync_cmd_idx !=
2271                                     (u32)ULONG_MAX) {
2272                                         cmd_mfi =
2273                                         instance->
2274                                         cmd_list[cmd_fusion->sync_cmd_idx];
2275                                         if (cmd_mfi->frame->dcmd.opcode ==
2276                                             MR_DCMD_LD_MAP_GET_INFO) {
2277                                                 megasas_return_cmd(instance,
2278                                                                    cmd_mfi);
2279                                                 megasas_return_cmd_fusion(
2280                                                         instance, cmd_fusion);
2281                                         } else  {
2282                                                 req_desc =
2283                                                 megasas_get_request_descriptor(
2284                                                         instance,
2285                                                         cmd_mfi->context.smid
2286                                                         -1);
2287                                                 if (!req_desc)
2288                                                         printk(KERN_WARNING
2289                                                                "req_desc NULL"
2290                                                                "\n");
2291                                                 else {
2292                                                         instance->instancet->
2293                                                         fire_cmd(instance,
2294                                                                  req_desc->
2295                                                                  u.low,
2296                                                                  req_desc->
2297                                                                  u.high,
2298                                                                  instance->
2299                                                                  reg_set);
2300                                                 }
2301                                         }
2302                                 }
2303                         }
2304
2305                         /* Reset load balance info */
2306                         memset(fusion->load_balance_info, 0,
2307                                sizeof(struct LD_LOAD_BALANCE_INFO)
2308                                *MAX_LOGICAL_DRIVES);
2309
2310                         if (!megasas_get_map_info(instance))
2311                                 megasas_sync_map_info(instance);
2312
2313                         /* Adapter reset completed successfully */
2314                         printk(KERN_WARNING "megaraid_sas: Reset "
2315                                "successful.\n");
2316                         retval = SUCCESS;
2317                         goto out;
2318                 }
2319                 /* Reset failed, kill the adapter */
2320                 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2321                        "adapter.\n");
2322                 megaraid_sas_kill_hba(instance);
2323                 retval = FAILED;
2324         } else {
2325                 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2326                 instance->instancet->enable_intr(instance->reg_set);
2327                 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2328         }
2329 out:
2330         clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2331         mutex_unlock(&instance->reset_mutex);
2332         return retval;
2333 }
2334
2335 /* Fusion OCR work queue */
2336 void megasas_fusion_ocr_wq(struct work_struct *work)
2337 {
2338         struct megasas_instance *instance =
2339                 container_of(work, struct megasas_instance, work_init);
2340
2341         megasas_reset_fusion(instance->host);
2342 }
2343
2344 struct megasas_instance_template megasas_instance_template_fusion = {
2345         .fire_cmd = megasas_fire_cmd_fusion,
2346         .enable_intr = megasas_enable_intr_fusion,
2347         .disable_intr = megasas_disable_intr_fusion,
2348         .clear_intr = megasas_clear_intr_fusion,
2349         .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2350         .adp_reset = megasas_adp_reset_fusion,
2351         .check_reset = megasas_check_reset_fusion,
2352         .service_isr = megasas_isr_fusion,
2353         .tasklet = megasas_complete_cmd_dpc_fusion,
2354         .init_adapter = megasas_init_adapter_fusion,
2355         .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2356         .issue_dcmd = megasas_issue_dcmd_fusion,
2357 };