[SCSI] megaraid_sas: Clear FUSION_IN_RESET before enabling interrupts
[pohmelfs.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
blob120499ade9a598f407bd09cdf4e76541907d9912
1 /*
2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2011 LSI Corporation.
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.
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.
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
20 * FILE: megaraid_sas_fusion.c
22 * Authors: LSI Corporation
23 * Sumant Patro
24 * Adam Radford <linuxraid@lsi.com>
26 * Send feedback to: <megaraidlinux@lsi.com>
28 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
29 * ATTN: Linuxraid
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/uaccess.h>
43 #include <linux/fs.h>
44 #include <linux/compat.h>
45 #include <linux/blkdev.h>
46 #include <linux/mutex.h>
47 #include <linux/poll.h>
49 #include <scsi/scsi.h>
50 #include <scsi/scsi_cmnd.h>
51 #include <scsi/scsi_device.h>
52 #include <scsi/scsi_host.h>
54 #include "megaraid_sas_fusion.h"
55 #include "megaraid_sas.h"
57 extern void megasas_free_cmds(struct megasas_instance *instance);
58 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
59 *instance);
60 extern void
61 megasas_complete_cmd(struct megasas_instance *instance,
62 struct megasas_cmd *cmd, u8 alt_status);
63 int megasas_is_ldio(struct scsi_cmnd *cmd);
64 int
65 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
67 void
68 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
69 int megasas_alloc_cmds(struct megasas_instance *instance);
70 int
71 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
72 int
73 megasas_issue_polled(struct megasas_instance *instance,
74 struct megasas_cmd *cmd);
77 MR_BuildRaidContext(struct IO_REQUEST_INFO *io_info,
78 struct RAID_CONTEXT *pRAID_Context,
79 struct MR_FW_RAID_MAP_ALL *map);
80 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
81 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
83 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
85 void
86 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
88 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
89 struct LD_LOAD_BALANCE_INFO *lbInfo);
90 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
91 struct IO_REQUEST_INFO *in_info);
92 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
93 void megaraid_sas_kill_hba(struct megasas_instance *instance);
95 extern u32 megasas_dbg_lvl;
97 /**
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
101 void
102 megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
104 /* For Thunderbolt/Invader also clear intr on enable */
105 writel(~0, &regs->outbound_intr_status);
106 readl(&regs->outbound_intr_status);
108 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
110 /* Dummy readl to force pci flush */
111 readl(&regs->outbound_intr_mask);
115 * megasas_disable_intr_fusion - Disables interrupt
116 * @regs: MFI register set
118 void
119 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
121 u32 mask = 0xFFFFFFFF;
122 u32 status;
124 writel(mask, &regs->outbound_intr_mask);
125 /* Dummy readl to force pci flush */
126 status = readl(&regs->outbound_intr_mask);
130 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
132 u32 status;
134 * Check if it is our interrupt
136 status = readl(&regs->outbound_intr_status);
138 if (status & 1) {
139 writel(status, &regs->outbound_intr_status);
140 readl(&regs->outbound_intr_status);
141 return 1;
143 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
144 return 0;
146 return 1;
150 * megasas_get_cmd_fusion - Get a command from the free pool
151 * @instance: Adapter soft state
153 * Returns a free command from the pool
155 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
156 *instance)
158 unsigned long flags;
159 struct fusion_context *fusion =
160 (struct fusion_context *)instance->ctrl_context;
161 struct megasas_cmd_fusion *cmd = NULL;
163 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
165 if (!list_empty(&fusion->cmd_pool)) {
166 cmd = list_entry((&fusion->cmd_pool)->next,
167 struct megasas_cmd_fusion, list);
168 list_del_init(&cmd->list);
169 } else {
170 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
173 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
174 return cmd;
178 * megasas_return_cmd_fusion - Return a cmd to free command pool
179 * @instance: Adapter soft state
180 * @cmd: Command packet to be returned to free command pool
182 static inline void
183 megasas_return_cmd_fusion(struct megasas_instance *instance,
184 struct megasas_cmd_fusion *cmd)
186 unsigned long flags;
187 struct fusion_context *fusion =
188 (struct fusion_context *)instance->ctrl_context;
190 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
192 cmd->scmd = NULL;
193 cmd->sync_cmd_idx = (u32)ULONG_MAX;
194 list_add_tail(&cmd->list, &fusion->cmd_pool);
196 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
200 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
201 * @instance: Adapter soft state
203 static void megasas_teardown_frame_pool_fusion(
204 struct megasas_instance *instance)
206 int i;
207 struct fusion_context *fusion = instance->ctrl_context;
209 u16 max_cmd = instance->max_fw_cmds;
211 struct megasas_cmd_fusion *cmd;
213 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
214 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
215 "sense pool : %p\n", fusion->sg_dma_pool,
216 fusion->sense_dma_pool);
217 return;
221 * Return all frames to pool
223 for (i = 0; i < max_cmd; i++) {
225 cmd = fusion->cmd_list[i];
227 if (cmd->sg_frame)
228 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
229 cmd->sg_frame_phys_addr);
231 if (cmd->sense)
232 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
233 cmd->sense_phys_addr);
237 * Now destroy the pool itself
239 pci_pool_destroy(fusion->sg_dma_pool);
240 pci_pool_destroy(fusion->sense_dma_pool);
242 fusion->sg_dma_pool = NULL;
243 fusion->sense_dma_pool = NULL;
247 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
248 * @instance: Adapter soft state
250 void
251 megasas_free_cmds_fusion(struct megasas_instance *instance)
253 int i;
254 struct fusion_context *fusion = instance->ctrl_context;
256 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
259 req_sz = fusion->request_alloc_sz;
260 reply_sz = fusion->reply_alloc_sz;
261 io_frames_sz = fusion->io_frames_alloc_sz;
263 max_cmds = instance->max_fw_cmds;
265 /* Free descriptors and request Frames memory */
266 if (fusion->req_frames_desc)
267 dma_free_coherent(&instance->pdev->dev, req_sz,
268 fusion->req_frames_desc,
269 fusion->req_frames_desc_phys);
271 if (fusion->reply_frames_desc) {
272 pci_pool_free(fusion->reply_frames_desc_pool,
273 fusion->reply_frames_desc,
274 fusion->reply_frames_desc_phys);
275 pci_pool_destroy(fusion->reply_frames_desc_pool);
278 if (fusion->io_request_frames) {
279 pci_pool_free(fusion->io_request_frames_pool,
280 fusion->io_request_frames,
281 fusion->io_request_frames_phys);
282 pci_pool_destroy(fusion->io_request_frames_pool);
285 /* Free the Fusion frame pool */
286 megasas_teardown_frame_pool_fusion(instance);
288 /* Free all the commands in the cmd_list */
289 for (i = 0; i < max_cmds; i++)
290 kfree(fusion->cmd_list[i]);
292 /* Free the cmd_list buffer itself */
293 kfree(fusion->cmd_list);
294 fusion->cmd_list = NULL;
296 INIT_LIST_HEAD(&fusion->cmd_pool);
300 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
301 * @instance: Adapter soft state
304 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
306 int i;
307 u32 max_cmd;
308 struct fusion_context *fusion;
309 struct megasas_cmd_fusion *cmd;
310 u32 total_sz_chain_frame;
312 fusion = instance->ctrl_context;
313 max_cmd = instance->max_fw_cmds;
315 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
318 * Use DMA pool facility provided by PCI layer
321 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
322 instance->pdev,
323 total_sz_chain_frame, 4,
325 if (!fusion->sg_dma_pool) {
326 printk(KERN_DEBUG "megasas: failed to setup request pool "
327 "fusion\n");
328 return -ENOMEM;
330 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
331 instance->pdev,
332 SCSI_SENSE_BUFFERSIZE, 64, 0);
334 if (!fusion->sense_dma_pool) {
335 printk(KERN_DEBUG "megasas: failed to setup sense pool "
336 "fusion\n");
337 pci_pool_destroy(fusion->sg_dma_pool);
338 fusion->sg_dma_pool = NULL;
339 return -ENOMEM;
343 * Allocate and attach a frame to each of the commands in cmd_list
345 for (i = 0; i < max_cmd; i++) {
347 cmd = fusion->cmd_list[i];
349 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
350 GFP_KERNEL,
351 &cmd->sg_frame_phys_addr);
353 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
354 GFP_KERNEL, &cmd->sense_phys_addr);
356 * megasas_teardown_frame_pool_fusion() takes care of freeing
357 * whatever has been allocated
359 if (!cmd->sg_frame || !cmd->sense) {
360 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
361 megasas_teardown_frame_pool_fusion(instance);
362 return -ENOMEM;
365 return 0;
369 * megasas_alloc_cmds_fusion - Allocates the command packets
370 * @instance: Adapter soft state
373 * Each frame has a 32-bit field called context. This context is used to get
374 * back the megasas_cmd_fusion from the frame when a frame gets completed
375 * In this driver, the 32 bit values are the indices into an array cmd_list.
376 * This array is used only to look up the megasas_cmd_fusion given the context.
377 * The free commands themselves are maintained in a linked list called cmd_pool.
379 * cmds are formed in the io_request and sg_frame members of the
380 * megasas_cmd_fusion. The context field is used to get a request descriptor
381 * and is used as SMID of the cmd.
382 * SMID value range is from 1 to max_fw_cmds.
385 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
387 int i, j;
388 u32 max_cmd, io_frames_sz;
389 struct fusion_context *fusion;
390 struct megasas_cmd_fusion *cmd;
391 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
392 u32 offset;
393 dma_addr_t io_req_base_phys;
394 u8 *io_req_base;
396 fusion = instance->ctrl_context;
398 max_cmd = instance->max_fw_cmds;
400 fusion->req_frames_desc =
401 dma_alloc_coherent(&instance->pdev->dev,
402 fusion->request_alloc_sz,
403 &fusion->req_frames_desc_phys, GFP_KERNEL);
405 if (!fusion->req_frames_desc) {
406 printk(KERN_ERR "megasas; Could not allocate memory for "
407 "request_frames\n");
408 goto fail_req_desc;
411 fusion->reply_frames_desc_pool =
412 pci_pool_create("reply_frames pool", instance->pdev,
413 fusion->reply_alloc_sz, 16, 0);
415 if (!fusion->reply_frames_desc_pool) {
416 printk(KERN_ERR "megasas; Could not allocate memory for "
417 "reply_frame pool\n");
418 goto fail_reply_desc;
421 fusion->reply_frames_desc =
422 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
423 &fusion->reply_frames_desc_phys);
424 if (!fusion->reply_frames_desc) {
425 printk(KERN_ERR "megasas; Could not allocate memory for "
426 "reply_frame pool\n");
427 pci_pool_destroy(fusion->reply_frames_desc_pool);
428 goto fail_reply_desc;
431 reply_desc = fusion->reply_frames_desc;
432 for (i = 0; i < fusion->reply_q_depth; i++, reply_desc++)
433 reply_desc->Words = ULLONG_MAX;
435 io_frames_sz = fusion->io_frames_alloc_sz;
437 fusion->io_request_frames_pool =
438 pci_pool_create("io_request_frames pool", instance->pdev,
439 fusion->io_frames_alloc_sz, 16, 0);
441 if (!fusion->io_request_frames_pool) {
442 printk(KERN_ERR "megasas: Could not allocate memory for "
443 "io_request_frame pool\n");
444 goto fail_io_frames;
447 fusion->io_request_frames =
448 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
449 &fusion->io_request_frames_phys);
450 if (!fusion->io_request_frames) {
451 printk(KERN_ERR "megasas: Could not allocate memory for "
452 "io_request_frames frames\n");
453 pci_pool_destroy(fusion->io_request_frames_pool);
454 goto fail_io_frames;
458 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
459 * Allocate the dynamic array first and then allocate individual
460 * commands.
462 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
463 *max_cmd, GFP_KERNEL);
465 if (!fusion->cmd_list) {
466 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
467 "memory for cmd_list_fusion\n");
468 goto fail_cmd_list;
471 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
472 *max_cmd);
474 max_cmd = instance->max_fw_cmds;
475 for (i = 0; i < max_cmd; i++) {
476 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
477 GFP_KERNEL);
478 if (!fusion->cmd_list[i]) {
479 printk(KERN_ERR "Could not alloc cmd list fusion\n");
481 for (j = 0; j < i; j++)
482 kfree(fusion->cmd_list[j]);
484 kfree(fusion->cmd_list);
485 fusion->cmd_list = NULL;
486 goto fail_cmd_list;
490 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
491 io_req_base = fusion->io_request_frames +
492 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
493 io_req_base_phys = fusion->io_request_frames_phys +
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
497 * Add all the commands to command pool (fusion->cmd_pool)
500 /* SMID 0 is reserved. Set SMID/index from 1 */
501 for (i = 0; i < max_cmd; i++) {
502 cmd = fusion->cmd_list[i];
503 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
504 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
505 cmd->index = i + 1;
506 cmd->scmd = NULL;
507 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
508 cmd->instance = instance;
509 cmd->io_request =
510 (struct MPI2_RAID_SCSI_IO_REQUEST *)
511 (io_req_base + offset);
512 memset(cmd->io_request, 0,
513 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
514 cmd->io_request_phys_addr = io_req_base_phys + offset;
516 list_add_tail(&cmd->list, &fusion->cmd_pool);
520 * Create a frame pool and assign one frame to each cmd
522 if (megasas_create_frame_pool_fusion(instance)) {
523 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
524 megasas_free_cmds_fusion(instance);
525 goto fail_req_desc;
528 return 0;
530 fail_cmd_list:
531 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
532 fusion->io_request_frames_phys);
533 pci_pool_destroy(fusion->io_request_frames_pool);
534 fail_io_frames:
535 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
536 fusion->reply_frames_desc,
537 fusion->reply_frames_desc_phys);
538 pci_pool_free(fusion->reply_frames_desc_pool,
539 fusion->reply_frames_desc,
540 fusion->reply_frames_desc_phys);
541 pci_pool_destroy(fusion->reply_frames_desc_pool);
543 fail_reply_desc:
544 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
545 fusion->req_frames_desc,
546 fusion->req_frames_desc_phys);
547 fail_req_desc:
548 return -ENOMEM;
552 * wait_and_poll - Issues a polling command
553 * @instance: Adapter soft state
554 * @cmd: Command packet to be issued
556 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
559 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
561 int i;
562 struct megasas_header *frame_hdr = &cmd->frame->hdr;
564 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
567 * Wait for cmd_status to change
569 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
570 rmb();
571 msleep(20);
574 if (frame_hdr->cmd_status == 0xff)
575 return -ETIME;
577 return 0;
581 * megasas_ioc_init_fusion - Initializes the FW
582 * @instance: Adapter soft state
584 * Issues the IOC Init cmd
587 megasas_ioc_init_fusion(struct megasas_instance *instance)
589 struct megasas_init_frame *init_frame;
590 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
591 dma_addr_t ioc_init_handle;
592 struct megasas_cmd *cmd;
593 u8 ret;
594 struct fusion_context *fusion;
595 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
596 int i;
597 struct megasas_header *frame_hdr;
599 fusion = instance->ctrl_context;
601 cmd = megasas_get_cmd(instance);
603 if (!cmd) {
604 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
605 ret = 1;
606 goto fail_get_cmd;
609 IOCInitMessage =
610 dma_alloc_coherent(&instance->pdev->dev,
611 sizeof(struct MPI2_IOC_INIT_REQUEST),
612 &ioc_init_handle, GFP_KERNEL);
614 if (!IOCInitMessage) {
615 printk(KERN_ERR "Could not allocate memory for "
616 "IOCInitMessage\n");
617 ret = 1;
618 goto fail_fw_init;
621 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
623 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
624 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
625 IOCInitMessage->MsgVersion = MPI2_VERSION;
626 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
627 IOCInitMessage->SystemRequestFrameSize =
628 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
630 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
631 IOCInitMessage->ReplyDescriptorPostQueueAddress =
632 fusion->reply_frames_desc_phys;
633 IOCInitMessage->SystemRequestFrameBaseAddress =
634 fusion->io_request_frames_phys;
636 init_frame = (struct megasas_init_frame *)cmd->frame;
637 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
639 frame_hdr = &cmd->frame->hdr;
640 frame_hdr->cmd_status = 0xFF;
641 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
643 init_frame->cmd = MFI_CMD_INIT;
644 init_frame->cmd_status = 0xFF;
646 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
647 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
649 req_desc =
650 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
652 req_desc->Words = cmd->frame_phys_addr;
653 req_desc->MFAIo.RequestFlags =
654 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
655 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
658 * disable the intr before firing the init frame
660 instance->instancet->disable_intr(instance->reg_set);
662 for (i = 0; i < (10 * 1000); i += 20) {
663 if (readl(&instance->reg_set->doorbell) & 1)
664 msleep(20);
665 else
666 break;
669 instance->instancet->fire_cmd(instance, req_desc->u.low,
670 req_desc->u.high, instance->reg_set);
672 wait_and_poll(instance, cmd);
674 frame_hdr = &cmd->frame->hdr;
675 if (frame_hdr->cmd_status != 0) {
676 ret = 1;
677 goto fail_fw_init;
679 printk(KERN_ERR "megasas:IOC Init cmd success\n");
681 ret = 0;
683 fail_fw_init:
684 megasas_return_cmd(instance, cmd);
685 if (IOCInitMessage)
686 dma_free_coherent(&instance->pdev->dev,
687 sizeof(struct MPI2_IOC_INIT_REQUEST),
688 IOCInitMessage, ioc_init_handle);
689 fail_get_cmd:
690 return ret;
694 * megasas_get_ld_map_info - Returns FW's ld_map structure
695 * @instance: Adapter soft state
696 * @pend: Pend the command or not
697 * Issues an internal command (DCMD) to get the FW's controller PD
698 * list structure. This information is mainly used to find out SYSTEM
699 * supported by the FW.
701 static int
702 megasas_get_ld_map_info(struct megasas_instance *instance)
704 int ret = 0;
705 struct megasas_cmd *cmd;
706 struct megasas_dcmd_frame *dcmd;
707 struct MR_FW_RAID_MAP_ALL *ci;
708 dma_addr_t ci_h = 0;
709 u32 size_map_info;
710 struct fusion_context *fusion;
712 cmd = megasas_get_cmd(instance);
714 if (!cmd) {
715 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
716 return -ENOMEM;
719 fusion = instance->ctrl_context;
721 if (!fusion) {
722 megasas_return_cmd(instance, cmd);
723 return 1;
726 dcmd = &cmd->frame->dcmd;
728 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
729 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
731 ci = fusion->ld_map[(instance->map_id & 1)];
732 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
734 if (!ci) {
735 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
736 megasas_return_cmd(instance, cmd);
737 return -ENOMEM;
740 memset(ci, 0, sizeof(*ci));
741 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
743 dcmd->cmd = MFI_CMD_DCMD;
744 dcmd->cmd_status = 0xFF;
745 dcmd->sge_count = 1;
746 dcmd->flags = MFI_FRAME_DIR_READ;
747 dcmd->timeout = 0;
748 dcmd->pad_0 = 0;
749 dcmd->data_xfer_len = size_map_info;
750 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
751 dcmd->sgl.sge32[0].phys_addr = ci_h;
752 dcmd->sgl.sge32[0].length = size_map_info;
754 if (!megasas_issue_polled(instance, cmd))
755 ret = 0;
756 else {
757 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
758 ret = -1;
761 megasas_return_cmd(instance, cmd);
763 return ret;
767 megasas_get_map_info(struct megasas_instance *instance)
769 struct fusion_context *fusion = instance->ctrl_context;
771 fusion->fast_path_io = 0;
772 if (!megasas_get_ld_map_info(instance)) {
773 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
774 fusion->load_balance_info)) {
775 fusion->fast_path_io = 1;
776 return 0;
779 return 1;
783 * megasas_sync_map_info - Returns FW's ld_map structure
784 * @instance: Adapter soft state
786 * Issues an internal command (DCMD) to get the FW's controller PD
787 * list structure. This information is mainly used to find out SYSTEM
788 * supported by the FW.
791 megasas_sync_map_info(struct megasas_instance *instance)
793 int ret = 0, i;
794 struct megasas_cmd *cmd;
795 struct megasas_dcmd_frame *dcmd;
796 u32 size_sync_info, num_lds;
797 struct fusion_context *fusion;
798 struct MR_LD_TARGET_SYNC *ci = NULL;
799 struct MR_FW_RAID_MAP_ALL *map;
800 struct MR_LD_RAID *raid;
801 struct MR_LD_TARGET_SYNC *ld_sync;
802 dma_addr_t ci_h = 0;
803 u32 size_map_info;
805 cmd = megasas_get_cmd(instance);
807 if (!cmd) {
808 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
809 "info.\n");
810 return -ENOMEM;
813 fusion = instance->ctrl_context;
815 if (!fusion) {
816 megasas_return_cmd(instance, cmd);
817 return 1;
820 map = fusion->ld_map[instance->map_id & 1];
822 num_lds = map->raidMap.ldCount;
824 dcmd = &cmd->frame->dcmd;
826 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
828 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
830 ci = (struct MR_LD_TARGET_SYNC *)
831 fusion->ld_map[(instance->map_id - 1) & 1];
832 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
834 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
836 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
838 for (i = 0; i < num_lds; i++, ld_sync++) {
839 raid = MR_LdRaidGet(i, map);
840 ld_sync->targetId = MR_GetLDTgtId(i, map);
841 ld_sync->seqNum = raid->seqNum;
844 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
845 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
847 dcmd->cmd = MFI_CMD_DCMD;
848 dcmd->cmd_status = 0xFF;
849 dcmd->sge_count = 1;
850 dcmd->flags = MFI_FRAME_DIR_WRITE;
851 dcmd->timeout = 0;
852 dcmd->pad_0 = 0;
853 dcmd->data_xfer_len = size_map_info;
854 dcmd->mbox.b[0] = num_lds;
855 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
856 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
857 dcmd->sgl.sge32[0].phys_addr = ci_h;
858 dcmd->sgl.sge32[0].length = size_map_info;
860 instance->map_update_cmd = cmd;
862 instance->instancet->issue_dcmd(instance, cmd);
864 return ret;
868 * megasas_init_adapter_fusion - Initializes the FW
869 * @instance: Adapter soft state
871 * This is the main function for initializing firmware.
874 megasas_init_adapter_fusion(struct megasas_instance *instance)
876 struct megasas_register_set __iomem *reg_set;
877 struct fusion_context *fusion;
878 u32 max_cmd;
879 int i = 0;
881 fusion = instance->ctrl_context;
883 reg_set = instance->reg_set;
886 * Get various operational parameters from status register
888 instance->max_fw_cmds =
889 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
890 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
893 * Reduce the max supported cmds by 1. This is to ensure that the
894 * reply_q_sz (1 more than the max cmd that driver may send)
895 * does not exceed max cmds that the FW can support
897 instance->max_fw_cmds = instance->max_fw_cmds-1;
898 /* Only internal cmds (DCMD) need to have MFI frames */
899 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
901 max_cmd = instance->max_fw_cmds;
903 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
905 fusion->request_alloc_sz =
906 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
907 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
908 *(fusion->reply_q_depth);
909 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
910 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
911 (max_cmd + 1)); /* Extra 1 for SMID 0 */
913 fusion->max_sge_in_main_msg =
914 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
915 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
917 fusion->max_sge_in_chain =
918 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
920 instance->max_num_sge = fusion->max_sge_in_main_msg +
921 fusion->max_sge_in_chain - 2;
923 /* Used for pass thru MFI frame (DCMD) */
924 fusion->chain_offset_mfi_pthru =
925 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
927 fusion->chain_offset_io_request =
928 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
929 sizeof(union MPI2_SGE_IO_UNION))/16;
931 fusion->last_reply_idx = 0;
934 * Allocate memory for descriptors
935 * Create a pool of commands
937 if (megasas_alloc_cmds(instance))
938 goto fail_alloc_mfi_cmds;
939 if (megasas_alloc_cmds_fusion(instance))
940 goto fail_alloc_cmds;
942 if (megasas_ioc_init_fusion(instance))
943 goto fail_ioc_init;
945 instance->flag_ieee = 1;
947 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
948 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
950 fusion->fast_path_io = 0;
952 for (i = 0; i < 2; i++) {
953 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
954 fusion->map_sz,
955 &fusion->ld_map_phys[i],
956 GFP_KERNEL);
957 if (!fusion->ld_map[i]) {
958 printk(KERN_ERR "megasas: Could not allocate memory "
959 "for map info\n");
960 goto fail_map_info;
964 if (!megasas_get_map_info(instance))
965 megasas_sync_map_info(instance);
967 return 0;
969 fail_map_info:
970 if (i == 1)
971 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
972 fusion->ld_map[0], fusion->ld_map_phys[0]);
973 fail_ioc_init:
974 megasas_free_cmds_fusion(instance);
975 fail_alloc_cmds:
976 megasas_free_cmds(instance);
977 fail_alloc_mfi_cmds:
978 return 1;
982 * megasas_fire_cmd_fusion - Sends command to the FW
983 * @frame_phys_addr : Physical address of cmd
984 * @frame_count : Number of frames for the command
985 * @regs : MFI register set
987 void
988 megasas_fire_cmd_fusion(struct megasas_instance *instance,
989 dma_addr_t req_desc_lo,
990 u32 req_desc_hi,
991 struct megasas_register_set __iomem *regs)
993 unsigned long flags;
995 spin_lock_irqsave(&instance->hba_lock, flags);
997 writel(req_desc_lo,
998 &(regs)->inbound_low_queue_port);
999 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1000 spin_unlock_irqrestore(&instance->hba_lock, flags);
1004 * map_cmd_status - Maps FW cmd status to OS cmd status
1005 * @cmd : Pointer to cmd
1006 * @status : status of cmd returned by FW
1007 * @ext_status : ext status of cmd returned by FW
1010 void
1011 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1014 switch (status) {
1016 case MFI_STAT_OK:
1017 cmd->scmd->result = DID_OK << 16;
1018 break;
1020 case MFI_STAT_SCSI_IO_FAILED:
1021 case MFI_STAT_LD_INIT_IN_PROGRESS:
1022 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1023 break;
1025 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1027 cmd->scmd->result = (DID_OK << 16) | ext_status;
1028 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1029 memset(cmd->scmd->sense_buffer, 0,
1030 SCSI_SENSE_BUFFERSIZE);
1031 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1032 SCSI_SENSE_BUFFERSIZE);
1033 cmd->scmd->result |= DRIVER_SENSE << 24;
1035 break;
1037 case MFI_STAT_LD_OFFLINE:
1038 case MFI_STAT_DEVICE_NOT_FOUND:
1039 cmd->scmd->result = DID_BAD_TARGET << 16;
1040 break;
1042 default:
1043 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1044 cmd->scmd->result = DID_ERROR << 16;
1045 break;
1050 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1051 * @instance: Adapter soft state
1052 * @scp: SCSI command from the mid-layer
1053 * @sgl_ptr: SGL to be filled in
1054 * @cmd: cmd we are working on
1056 * If successful, this function returns the number of SG elements.
1058 static int
1059 megasas_make_sgl_fusion(struct megasas_instance *instance,
1060 struct scsi_cmnd *scp,
1061 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1062 struct megasas_cmd_fusion *cmd)
1064 int i, sg_processed;
1065 int sge_count, sge_idx;
1066 struct scatterlist *os_sgl;
1067 struct fusion_context *fusion;
1069 fusion = instance->ctrl_context;
1071 cmd->io_request->ChainOffset = 0;
1073 sge_count = scsi_dma_map(scp);
1075 BUG_ON(sge_count < 0);
1077 if (sge_count > instance->max_num_sge || !sge_count)
1078 return sge_count;
1080 if (sge_count > fusion->max_sge_in_main_msg) {
1081 /* One element to store the chain info */
1082 sge_idx = fusion->max_sge_in_main_msg - 1;
1083 } else
1084 sge_idx = sge_count;
1086 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1087 sgl_ptr->Length = sg_dma_len(os_sgl);
1088 sgl_ptr->Address = sg_dma_address(os_sgl);
1089 sgl_ptr->Flags = 0;
1090 sgl_ptr++;
1092 sg_processed = i + 1;
1094 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1095 (sge_count > fusion->max_sge_in_main_msg)) {
1097 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1098 cmd->io_request->ChainOffset =
1099 fusion->chain_offset_io_request;
1100 sg_chain = sgl_ptr;
1101 /* Prepare chain element */
1102 sg_chain->NextChainOffset = 0;
1103 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1104 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1105 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1106 *(sge_count - sg_processed));
1107 sg_chain->Address = cmd->sg_frame_phys_addr;
1109 sgl_ptr =
1110 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1114 return sge_count;
1118 * megasas_set_pd_lba - Sets PD LBA
1119 * @cdb: CDB
1120 * @cdb_len: cdb length
1121 * @start_blk: Start block of IO
1123 * Used to set the PD LBA in CDB for FP IOs
1125 void
1126 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1127 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1128 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1130 struct MR_LD_RAID *raid;
1131 u32 ld;
1132 u64 start_blk = io_info->pdBlock;
1133 u8 *cdb = io_request->CDB.CDB32;
1134 u32 num_blocks = io_info->numBlocks;
1135 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1137 /* Check if T10 PI (DIF) is enabled for this LD */
1138 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1139 raid = MR_LdRaidGet(ld, local_map_ptr);
1140 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1141 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1142 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1143 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1145 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1146 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1147 else
1148 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1149 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1151 /* LBA */
1152 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1153 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1154 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1155 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1156 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1157 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1158 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1159 cdb[19] = (u8)(start_blk & 0xff);
1161 /* Logical block reference tag */
1162 io_request->CDB.EEDP32.PrimaryReferenceTag =
1163 cpu_to_be32(ref_tag);
1164 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1166 io_request->DataLength = num_blocks * 512;
1167 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1169 /* Transfer length */
1170 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1171 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1172 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1173 cdb[31] = (u8)(num_blocks & 0xff);
1175 /* set SCSI IO EEDPFlags */
1176 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1177 io_request->EEDPFlags =
1178 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1179 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1180 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1181 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1182 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1183 } else {
1184 io_request->EEDPFlags =
1185 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1186 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1188 io_request->Control |= (0x4 << 26);
1189 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1190 } else {
1191 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1192 if (((cdb_len == 12) || (cdb_len == 16)) &&
1193 (start_blk <= 0xffffffff)) {
1194 if (cdb_len == 16) {
1195 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1196 flagvals = cdb[1];
1197 groupnum = cdb[14];
1198 control = cdb[15];
1199 } else {
1200 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1201 flagvals = cdb[1];
1202 groupnum = cdb[10];
1203 control = cdb[11];
1206 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1208 cdb[0] = opcode;
1209 cdb[1] = flagvals;
1210 cdb[6] = groupnum;
1211 cdb[9] = control;
1213 /* Transfer length */
1214 cdb[8] = (u8)(num_blocks & 0xff);
1215 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1217 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1218 cdb_len = 10;
1219 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1220 /* Convert to 16 byte CDB for large LBA's */
1221 switch (cdb_len) {
1222 case 6:
1223 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1224 control = cdb[5];
1225 break;
1226 case 10:
1227 opcode =
1228 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1229 flagvals = cdb[1];
1230 groupnum = cdb[6];
1231 control = cdb[9];
1232 break;
1233 case 12:
1234 opcode =
1235 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1236 flagvals = cdb[1];
1237 groupnum = cdb[10];
1238 control = cdb[11];
1239 break;
1242 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1244 cdb[0] = opcode;
1245 cdb[1] = flagvals;
1246 cdb[14] = groupnum;
1247 cdb[15] = control;
1249 /* Transfer length */
1250 cdb[13] = (u8)(num_blocks & 0xff);
1251 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1252 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1253 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1255 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1256 cdb_len = 16;
1259 /* Normal case, just load LBA here */
1260 switch (cdb_len) {
1261 case 6:
1263 u8 val = cdb[1] & 0xE0;
1264 cdb[3] = (u8)(start_blk & 0xff);
1265 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1266 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1267 break;
1269 case 10:
1270 cdb[5] = (u8)(start_blk & 0xff);
1271 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1272 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1273 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1274 break;
1275 case 12:
1276 cdb[5] = (u8)(start_blk & 0xff);
1277 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1278 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1279 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1280 break;
1281 case 16:
1282 cdb[9] = (u8)(start_blk & 0xff);
1283 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1284 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1285 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1286 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1287 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1288 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1289 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1290 break;
1296 * megasas_build_ldio_fusion - Prepares IOs to devices
1297 * @instance: Adapter soft state
1298 * @scp: SCSI command
1299 * @cmd: Command to be prepared
1301 * Prepares the io_request and chain elements (sg_frame) for IO
1302 * The IO can be for PD (Fast Path) or LD
1304 void
1305 megasas_build_ldio_fusion(struct megasas_instance *instance,
1306 struct scsi_cmnd *scp,
1307 struct megasas_cmd_fusion *cmd)
1309 u8 fp_possible;
1310 u32 start_lba_lo, start_lba_hi, device_id;
1311 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1312 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1313 struct IO_REQUEST_INFO io_info;
1314 struct fusion_context *fusion;
1315 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1317 device_id = MEGASAS_DEV_INDEX(instance, scp);
1319 fusion = instance->ctrl_context;
1321 io_request = cmd->io_request;
1322 io_request->RaidContext.VirtualDiskTgtId = device_id;
1323 io_request->RaidContext.status = 0;
1324 io_request->RaidContext.exStatus = 0;
1326 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1328 start_lba_lo = 0;
1329 start_lba_hi = 0;
1330 fp_possible = 0;
1333 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1335 if (scp->cmd_len == 6) {
1336 io_request->DataLength = (u32) scp->cmnd[4];
1337 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1338 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1340 start_lba_lo &= 0x1FFFFF;
1344 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1346 else if (scp->cmd_len == 10) {
1347 io_request->DataLength = (u32) scp->cmnd[8] |
1348 ((u32) scp->cmnd[7] << 8);
1349 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1350 ((u32) scp->cmnd[3] << 16) |
1351 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1355 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1357 else if (scp->cmd_len == 12) {
1358 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1359 ((u32) scp->cmnd[7] << 16) |
1360 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1361 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1362 ((u32) scp->cmnd[3] << 16) |
1363 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1367 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1369 else if (scp->cmd_len == 16) {
1370 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1371 ((u32) scp->cmnd[11] << 16) |
1372 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1373 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1374 ((u32) scp->cmnd[7] << 16) |
1375 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1377 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1378 ((u32) scp->cmnd[3] << 16) |
1379 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1382 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1383 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1384 io_info.numBlocks = io_request->DataLength;
1385 io_info.ldTgtId = device_id;
1387 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1388 io_info.isRead = 1;
1390 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1392 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1393 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1394 io_request->RaidContext.regLockFlags = 0;
1395 fp_possible = 0;
1396 } else {
1397 if (MR_BuildRaidContext(&io_info, &io_request->RaidContext,
1398 local_map_ptr))
1399 fp_possible = io_info.fpOkForIo;
1402 if (fp_possible) {
1403 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1404 local_map_ptr, start_lba_lo);
1405 io_request->DataLength = scsi_bufflen(scp);
1406 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1407 cmd->request_desc->SCSIIO.RequestFlags =
1408 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1409 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1410 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1411 (io_info.isRead)) {
1412 io_info.devHandle =
1413 get_updated_dev_handle(
1414 &fusion->load_balance_info[device_id],
1415 &io_info);
1416 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1417 } else
1418 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1419 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1420 io_request->DevHandle = io_info.devHandle;
1421 } else {
1422 io_request->RaidContext.timeoutValue =
1423 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1424 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1425 io_request->DevHandle = device_id;
1426 cmd->request_desc->SCSIIO.RequestFlags =
1427 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1428 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1429 } /* Not FP */
1433 * megasas_build_dcdb_fusion - Prepares IOs to devices
1434 * @instance: Adapter soft state
1435 * @scp: SCSI command
1436 * @cmd: Command to be prepared
1438 * Prepares the io_request frame for non-io cmds
1440 static void
1441 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1442 struct scsi_cmnd *scmd,
1443 struct megasas_cmd_fusion *cmd)
1445 u32 device_id;
1446 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1447 u16 pd_index = 0;
1448 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1449 struct fusion_context *fusion = instance->ctrl_context;
1451 io_request = cmd->io_request;
1452 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1453 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1454 +scmd->device->id;
1455 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1457 /* Check if this is a system PD I/O */
1458 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1459 io_request->Function = 0;
1460 io_request->DevHandle =
1461 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1462 io_request->RaidContext.timeoutValue =
1463 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1464 io_request->RaidContext.regLockFlags = 0;
1465 io_request->RaidContext.regLockRowLBA = 0;
1466 io_request->RaidContext.regLockLength = 0;
1467 io_request->RaidContext.RAIDFlags =
1468 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1469 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1470 cmd->request_desc->SCSIIO.RequestFlags =
1471 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1472 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1473 } else {
1474 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1475 io_request->DevHandle = device_id;
1476 cmd->request_desc->SCSIIO.RequestFlags =
1477 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1478 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1480 io_request->RaidContext.VirtualDiskTgtId = device_id;
1481 io_request->LUN[1] = scmd->device->lun;
1482 io_request->DataLength = scsi_bufflen(scmd);
1486 * megasas_build_io_fusion - Prepares IOs to devices
1487 * @instance: Adapter soft state
1488 * @scp: SCSI command
1489 * @cmd: Command to be prepared
1491 * Invokes helper functions to prepare request frames
1492 * and sets flags appropriate for IO/Non-IO cmd
1495 megasas_build_io_fusion(struct megasas_instance *instance,
1496 struct scsi_cmnd *scp,
1497 struct megasas_cmd_fusion *cmd)
1499 u32 device_id, sge_count;
1500 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1502 device_id = MEGASAS_DEV_INDEX(instance, scp);
1504 /* Zero out some fields so they don't get reused */
1505 io_request->LUN[1] = 0;
1506 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1507 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1508 io_request->EEDPFlags = 0;
1509 io_request->Control = 0;
1510 io_request->EEDPBlockSize = 0;
1511 io_request->IoFlags = 0;
1512 io_request->RaidContext.RAIDFlags = 0;
1514 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1516 * Just the CDB length,rest of the Flags are zero
1517 * This will be modified for FP in build_ldio_fusion
1519 io_request->IoFlags = scp->cmd_len;
1521 if (megasas_is_ldio(scp))
1522 megasas_build_ldio_fusion(instance, scp, cmd);
1523 else
1524 megasas_build_dcdb_fusion(instance, scp, cmd);
1527 * Construct SGL
1530 sge_count =
1531 megasas_make_sgl_fusion(instance, scp,
1532 (struct MPI25_IEEE_SGE_CHAIN64 *)
1533 &io_request->SGL, cmd);
1535 if (sge_count > instance->max_num_sge) {
1536 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1537 "max (0x%x) allowed\n", sge_count,
1538 instance->max_num_sge);
1539 return 1;
1542 io_request->RaidContext.numSGE = sge_count;
1544 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1546 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1547 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1548 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1549 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1551 io_request->SGLOffset0 =
1552 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1554 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1555 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1557 cmd->scmd = scp;
1558 scp->SCp.ptr = (char *)cmd;
1560 return 0;
1563 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1564 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1566 u8 *p;
1567 struct fusion_context *fusion;
1569 if (index >= instance->max_fw_cmds) {
1570 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1571 "descriptor\n", index);
1572 return NULL;
1574 fusion = instance->ctrl_context;
1575 p = fusion->req_frames_desc
1576 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1578 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1582 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1583 * issuing non IOCTL cmd
1584 * @instance: Adapter soft state
1585 * @scmd: pointer to scsi cmd from OS
1587 static u32
1588 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1589 struct scsi_cmnd *scmd)
1591 struct megasas_cmd_fusion *cmd;
1592 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1593 u32 index;
1594 struct fusion_context *fusion;
1596 fusion = instance->ctrl_context;
1598 cmd = megasas_get_cmd_fusion(instance);
1599 if (!cmd)
1600 return SCSI_MLQUEUE_HOST_BUSY;
1602 index = cmd->index;
1604 req_desc = megasas_get_request_descriptor(instance, index-1);
1605 if (!req_desc)
1606 return 1;
1608 req_desc->Words = 0;
1609 cmd->request_desc = req_desc;
1611 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1612 megasas_return_cmd_fusion(instance, cmd);
1613 printk(KERN_ERR "megasas: Error building command.\n");
1614 cmd->request_desc = NULL;
1615 return 1;
1618 req_desc = cmd->request_desc;
1619 req_desc->SCSIIO.SMID = index;
1621 if (cmd->io_request->ChainOffset != 0 &&
1622 cmd->io_request->ChainOffset != 0xF)
1623 printk(KERN_ERR "megasas: The chain offset value is not "
1624 "correct : %x\n", cmd->io_request->ChainOffset);
1627 * Issue the command to the FW
1629 atomic_inc(&instance->fw_outstanding);
1631 instance->instancet->fire_cmd(instance,
1632 req_desc->u.low, req_desc->u.high,
1633 instance->reg_set);
1635 return 0;
1639 * complete_cmd_fusion - Completes command
1640 * @instance: Adapter soft state
1641 * Completes all commands that is in reply descriptor queue
1644 complete_cmd_fusion(struct megasas_instance *instance)
1646 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1647 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1648 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1649 struct fusion_context *fusion;
1650 struct megasas_cmd *cmd_mfi;
1651 struct megasas_cmd_fusion *cmd_fusion;
1652 u16 smid, num_completed;
1653 u8 reply_descript_type, arm;
1654 u32 status, extStatus, device_id;
1655 union desc_value d_val;
1656 struct LD_LOAD_BALANCE_INFO *lbinfo;
1658 fusion = instance->ctrl_context;
1660 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1661 return IRQ_HANDLED;
1663 desc = fusion->reply_frames_desc;
1664 desc += fusion->last_reply_idx;
1666 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1668 d_val.word = desc->Words;
1670 reply_descript_type = reply_desc->ReplyFlags &
1671 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1673 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1674 return IRQ_NONE;
1676 d_val.word = desc->Words;
1678 num_completed = 0;
1680 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1681 smid = reply_desc->SMID;
1683 cmd_fusion = fusion->cmd_list[smid - 1];
1685 scsi_io_req =
1686 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1687 cmd_fusion->io_request;
1689 if (cmd_fusion->scmd)
1690 cmd_fusion->scmd->SCp.ptr = NULL;
1692 status = scsi_io_req->RaidContext.status;
1693 extStatus = scsi_io_req->RaidContext.exStatus;
1695 switch (scsi_io_req->Function) {
1696 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1697 /* Update load balancing info */
1698 device_id = MEGASAS_DEV_INDEX(instance,
1699 cmd_fusion->scmd);
1700 lbinfo = &fusion->load_balance_info[device_id];
1701 if (cmd_fusion->scmd->SCp.Status &
1702 MEGASAS_LOAD_BALANCE_FLAG) {
1703 arm = lbinfo->raid1DevHandle[0] ==
1704 cmd_fusion->io_request->DevHandle ? 0 :
1706 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1707 cmd_fusion->scmd->SCp.Status &=
1708 ~MEGASAS_LOAD_BALANCE_FLAG;
1710 if (reply_descript_type ==
1711 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1712 if (megasas_dbg_lvl == 5)
1713 printk(KERN_ERR "\nmegasas: FAST Path "
1714 "IO Success\n");
1716 /* Fall thru and complete IO */
1717 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1718 /* Map the FW Cmd Status */
1719 map_cmd_status(cmd_fusion, status, extStatus);
1720 scsi_dma_unmap(cmd_fusion->scmd);
1721 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1722 scsi_io_req->RaidContext.status = 0;
1723 scsi_io_req->RaidContext.exStatus = 0;
1724 megasas_return_cmd_fusion(instance, cmd_fusion);
1725 atomic_dec(&instance->fw_outstanding);
1727 break;
1728 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1729 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1730 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1731 cmd_fusion->flags = 0;
1732 megasas_return_cmd_fusion(instance, cmd_fusion);
1734 break;
1737 fusion->last_reply_idx++;
1738 if (fusion->last_reply_idx >= fusion->reply_q_depth)
1739 fusion->last_reply_idx = 0;
1741 desc->Words = ULLONG_MAX;
1742 num_completed++;
1744 /* Get the next reply descriptor */
1745 if (!fusion->last_reply_idx)
1746 desc = fusion->reply_frames_desc;
1747 else
1748 desc++;
1750 reply_desc =
1751 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1753 d_val.word = desc->Words;
1755 reply_descript_type = reply_desc->ReplyFlags &
1756 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1758 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1759 break;
1762 if (!num_completed)
1763 return IRQ_NONE;
1765 wmb();
1766 writel(fusion->last_reply_idx,
1767 &instance->reg_set->reply_post_host_index);
1768 megasas_check_and_restore_queue_depth(instance);
1769 return IRQ_HANDLED;
1773 * megasas_complete_cmd_dpc_fusion - Completes command
1774 * @instance: Adapter soft state
1776 * Tasklet to complete cmds
1778 void
1779 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1781 struct megasas_instance *instance =
1782 (struct megasas_instance *)instance_addr;
1783 unsigned long flags;
1785 /* If we have already declared adapter dead, donot complete cmds */
1786 spin_lock_irqsave(&instance->hba_lock, flags);
1787 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1788 spin_unlock_irqrestore(&instance->hba_lock, flags);
1789 return;
1791 spin_unlock_irqrestore(&instance->hba_lock, flags);
1793 spin_lock_irqsave(&instance->completion_lock, flags);
1794 complete_cmd_fusion(instance);
1795 spin_unlock_irqrestore(&instance->completion_lock, flags);
1799 * megasas_isr_fusion - isr entry point
1801 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1803 struct megasas_instance *instance = (struct megasas_instance *)devp;
1804 u32 mfiStatus, fw_state;
1806 if (!instance->msi_flag) {
1807 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1808 if (!mfiStatus)
1809 return IRQ_NONE;
1812 /* If we are resetting, bail */
1813 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1814 instance->instancet->clear_intr(instance->reg_set);
1815 return IRQ_HANDLED;
1818 if (!complete_cmd_fusion(instance)) {
1819 instance->instancet->clear_intr(instance->reg_set);
1820 /* If we didn't complete any commands, check for FW fault */
1821 fw_state = instance->instancet->read_fw_status_reg(
1822 instance->reg_set) & MFI_STATE_MASK;
1823 if (fw_state == MFI_STATE_FAULT)
1824 schedule_work(&instance->work_init);
1827 return IRQ_HANDLED;
1831 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1832 * @instance: Adapter soft state
1833 * mfi_cmd: megasas_cmd pointer
1837 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1838 struct megasas_cmd *mfi_cmd)
1840 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1841 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1842 struct megasas_cmd_fusion *cmd;
1843 struct fusion_context *fusion;
1844 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1846 cmd = megasas_get_cmd_fusion(instance);
1847 if (!cmd)
1848 return 1;
1850 /* Save the smid. To be used for returning the cmd */
1851 mfi_cmd->context.smid = cmd->index;
1853 cmd->sync_cmd_idx = mfi_cmd->index;
1856 * For cmds where the flag is set, store the flag and check
1857 * on completion. For cmds with this flag, don't call
1858 * megasas_complete_cmd
1861 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1862 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1864 fusion = instance->ctrl_context;
1865 io_req = cmd->io_request;
1866 mpi25_ieee_chain =
1867 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1869 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1870 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1871 SGL) / 4;
1872 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1874 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1876 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1877 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1879 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1881 return 0;
1885 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1886 * @instance: Adapter soft state
1887 * @cmd: mfi cmd to build
1890 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1891 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1893 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1894 u16 index;
1896 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1897 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1898 return NULL;
1901 index = cmd->context.smid;
1903 req_desc = megasas_get_request_descriptor(instance, index - 1);
1905 if (!req_desc)
1906 return NULL;
1908 req_desc->Words = 0;
1909 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1910 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1912 req_desc->SCSIIO.SMID = index;
1914 return req_desc;
1918 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1919 * @instance: Adapter soft state
1920 * @cmd: mfi cmd pointer
1923 void
1924 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
1925 struct megasas_cmd *cmd)
1927 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1929 req_desc = build_mpt_cmd(instance, cmd);
1930 if (!req_desc) {
1931 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
1932 return;
1934 instance->instancet->fire_cmd(instance, req_desc->u.low,
1935 req_desc->u.high, instance->reg_set);
1939 * megasas_release_fusion - Reverses the FW initialization
1940 * @intance: Adapter soft state
1942 void
1943 megasas_release_fusion(struct megasas_instance *instance)
1945 megasas_free_cmds(instance);
1946 megasas_free_cmds_fusion(instance);
1948 iounmap(instance->reg_set);
1950 pci_release_selected_regions(instance->pdev, instance->bar);
1954 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1955 * @regs: MFI register set
1957 static u32
1958 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
1960 return readl(&(regs)->outbound_scratch_pad);
1964 * megasas_adp_reset_fusion - For controller reset
1965 * @regs: MFI register set
1967 static int
1968 megasas_adp_reset_fusion(struct megasas_instance *instance,
1969 struct megasas_register_set __iomem *regs)
1971 return 0;
1975 * megasas_check_reset_fusion - For controller reset check
1976 * @regs: MFI register set
1978 static int
1979 megasas_check_reset_fusion(struct megasas_instance *instance,
1980 struct megasas_register_set __iomem *regs)
1982 return 0;
1985 /* This function waits for outstanding commands on fusion to complete */
1986 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
1988 int i, outstanding, retval = 0;
1989 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
1991 for (i = 0; i < wait_time; i++) {
1992 /* Check if firmware is in fault state */
1993 fw_state = instance->instancet->read_fw_status_reg(
1994 instance->reg_set) & MFI_STATE_MASK;
1995 if (fw_state == MFI_STATE_FAULT) {
1996 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
1997 " will reset adapter.\n");
1998 retval = 1;
1999 goto out;
2002 outstanding = atomic_read(&instance->fw_outstanding);
2003 if (!outstanding)
2004 goto out;
2006 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2007 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2008 "commands to complete\n", i, outstanding);
2009 megasas_complete_cmd_dpc_fusion(
2010 (unsigned long)instance);
2012 msleep(1000);
2015 if (atomic_read(&instance->fw_outstanding)) {
2016 printk("megaraid_sas: pending commands remain after waiting, "
2017 "will reset adapter.\n");
2018 retval = 1;
2020 out:
2021 return retval;
2024 void megasas_reset_reply_desc(struct megasas_instance *instance)
2026 int i;
2027 struct fusion_context *fusion;
2028 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2030 fusion = instance->ctrl_context;
2031 fusion->last_reply_idx = 0;
2032 reply_desc = fusion->reply_frames_desc;
2033 for (i = 0 ; i < fusion->reply_q_depth; i++, reply_desc++)
2034 reply_desc->Words = ULLONG_MAX;
2037 /* Core fusion reset function */
2038 int megasas_reset_fusion(struct Scsi_Host *shost)
2040 int retval = SUCCESS, i, j, retry = 0;
2041 struct megasas_instance *instance;
2042 struct megasas_cmd_fusion *cmd_fusion;
2043 struct fusion_context *fusion;
2044 struct megasas_cmd *cmd_mfi;
2045 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2046 u32 host_diag, abs_state, status_reg, reset_adapter;
2048 instance = (struct megasas_instance *)shost->hostdata;
2049 fusion = instance->ctrl_context;
2051 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2052 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2053 "returning FAILED.\n");
2054 return FAILED;
2057 mutex_lock(&instance->reset_mutex);
2058 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2059 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2060 instance->instancet->disable_intr(instance->reg_set);
2061 msleep(1000);
2063 /* First try waiting for commands to complete */
2064 if (megasas_wait_for_outstanding_fusion(instance)) {
2065 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2066 "adapter.\n");
2067 /* Now return commands back to the OS */
2068 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2069 cmd_fusion = fusion->cmd_list[i];
2070 if (cmd_fusion->scmd) {
2071 scsi_dma_unmap(cmd_fusion->scmd);
2072 cmd_fusion->scmd->result = (DID_RESET << 16);
2073 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2074 megasas_return_cmd_fusion(instance, cmd_fusion);
2075 atomic_dec(&instance->fw_outstanding);
2079 status_reg = instance->instancet->read_fw_status_reg(
2080 instance->reg_set);
2081 abs_state = status_reg & MFI_STATE_MASK;
2082 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2083 if (instance->disableOnlineCtrlReset ||
2084 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2085 /* Reset not supported, kill adapter */
2086 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2087 ", killing adapter.\n");
2088 megaraid_sas_kill_hba(instance);
2089 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2090 retval = FAILED;
2091 goto out;
2094 /* Now try to reset the chip */
2095 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2096 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2097 &instance->reg_set->fusion_seq_offset);
2098 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2099 &instance->reg_set->fusion_seq_offset);
2100 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2101 &instance->reg_set->fusion_seq_offset);
2102 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2103 &instance->reg_set->fusion_seq_offset);
2104 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2105 &instance->reg_set->fusion_seq_offset);
2106 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2107 &instance->reg_set->fusion_seq_offset);
2108 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2109 &instance->reg_set->fusion_seq_offset);
2111 /* Check that the diag write enable (DRWE) bit is on */
2112 host_diag = readl(&instance->reg_set->fusion_host_diag);
2113 retry = 0;
2114 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2115 msleep(100);
2116 host_diag =
2117 readl(&instance->reg_set->fusion_host_diag);
2118 if (retry++ == 100) {
2119 printk(KERN_WARNING "megaraid_sas: "
2120 "Host diag unlock failed!\n");
2121 break;
2124 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2125 continue;
2127 /* Send chip reset command */
2128 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2129 &instance->reg_set->fusion_host_diag);
2130 msleep(3000);
2132 /* Make sure reset adapter bit is cleared */
2133 host_diag = readl(&instance->reg_set->fusion_host_diag);
2134 retry = 0;
2135 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2136 msleep(100);
2137 host_diag =
2138 readl(&instance->reg_set->fusion_host_diag);
2139 if (retry++ == 1000) {
2140 printk(KERN_WARNING "megaraid_sas: "
2141 "Diag reset adapter never "
2142 "cleared!\n");
2143 break;
2146 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2147 continue;
2149 abs_state =
2150 instance->instancet->read_fw_status_reg(
2151 instance->reg_set) & MFI_STATE_MASK;
2152 retry = 0;
2154 while ((abs_state <= MFI_STATE_FW_INIT) &&
2155 (retry++ < 1000)) {
2156 msleep(100);
2157 abs_state =
2158 instance->instancet->read_fw_status_reg(
2159 instance->reg_set) & MFI_STATE_MASK;
2161 if (abs_state <= MFI_STATE_FW_INIT) {
2162 printk(KERN_WARNING "megaraid_sas: firmware "
2163 "state < MFI_STATE_FW_INIT, state = "
2164 "0x%x\n", abs_state);
2165 continue;
2168 /* Wait for FW to become ready */
2169 if (megasas_transition_to_ready(instance, 1)) {
2170 printk(KERN_WARNING "megaraid_sas: Failed to "
2171 "transition controller to ready.\n");
2172 continue;
2175 megasas_reset_reply_desc(instance);
2176 if (megasas_ioc_init_fusion(instance)) {
2177 printk(KERN_WARNING "megaraid_sas: "
2178 "megasas_ioc_init_fusion() failed!\n");
2179 continue;
2182 clear_bit(MEGASAS_FUSION_IN_RESET,
2183 &instance->reset_flags);
2184 instance->instancet->enable_intr(instance->reg_set);
2185 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2187 /* Re-fire management commands */
2188 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2189 cmd_fusion = fusion->cmd_list[j];
2190 if (cmd_fusion->sync_cmd_idx !=
2191 (u32)ULONG_MAX) {
2192 cmd_mfi =
2193 instance->
2194 cmd_list[cmd_fusion->sync_cmd_idx];
2195 if (cmd_mfi->frame->dcmd.opcode ==
2196 MR_DCMD_LD_MAP_GET_INFO) {
2197 megasas_return_cmd(instance,
2198 cmd_mfi);
2199 megasas_return_cmd_fusion(
2200 instance, cmd_fusion);
2201 } else {
2202 req_desc =
2203 megasas_get_request_descriptor(
2204 instance,
2205 cmd_mfi->context.smid
2206 -1);
2207 if (!req_desc)
2208 printk(KERN_WARNING
2209 "req_desc NULL"
2210 "\n");
2211 else {
2212 instance->instancet->
2213 fire_cmd(instance,
2214 req_desc->
2215 u.low,
2216 req_desc->
2217 u.high,
2218 instance->
2219 reg_set);
2225 /* Reset load balance info */
2226 memset(fusion->load_balance_info, 0,
2227 sizeof(struct LD_LOAD_BALANCE_INFO)
2228 *MAX_LOGICAL_DRIVES);
2230 if (!megasas_get_map_info(instance))
2231 megasas_sync_map_info(instance);
2233 /* Adapter reset completed successfully */
2234 printk(KERN_WARNING "megaraid_sas: Reset "
2235 "successful.\n");
2236 retval = SUCCESS;
2237 goto out;
2239 /* Reset failed, kill the adapter */
2240 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2241 "adapter.\n");
2242 megaraid_sas_kill_hba(instance);
2243 retval = FAILED;
2244 } else {
2245 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2246 instance->instancet->enable_intr(instance->reg_set);
2247 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2249 out:
2250 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2251 mutex_unlock(&instance->reset_mutex);
2252 return retval;
2255 /* Fusion OCR work queue */
2256 void megasas_fusion_ocr_wq(struct work_struct *work)
2258 struct megasas_instance *instance =
2259 container_of(work, struct megasas_instance, work_init);
2261 megasas_reset_fusion(instance->host);
2264 struct megasas_instance_template megasas_instance_template_fusion = {
2265 .fire_cmd = megasas_fire_cmd_fusion,
2266 .enable_intr = megasas_enable_intr_fusion,
2267 .disable_intr = megasas_disable_intr_fusion,
2268 .clear_intr = megasas_clear_intr_fusion,
2269 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2270 .adp_reset = megasas_adp_reset_fusion,
2271 .check_reset = megasas_check_reset_fusion,
2272 .service_isr = megasas_isr_fusion,
2273 .tasklet = megasas_complete_cmd_dpc_fusion,
2274 .init_adapter = megasas_init_adapter_fusion,
2275 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2276 .issue_dcmd = megasas_issue_dcmd_fusion,