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