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