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