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