Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
blobf13e7abd345a9b3956243fc5e047207f66d0dbe8
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);
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 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
106 /* Dummy readl to force pci flush */
107 readl(&regs->outbound_intr_mask);
111 * megasas_disable_intr_fusion - Disables interrupt
112 * @regs: MFI register set
114 void
115 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
117 u32 mask = 0xFFFFFFFF;
118 u32 status;
120 writel(mask, &regs->outbound_intr_mask);
121 /* Dummy readl to force pci flush */
122 status = readl(&regs->outbound_intr_mask);
126 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
128 u32 status;
130 * Check if it is our interrupt
132 status = readl(&regs->outbound_intr_status);
134 if (status & 1) {
135 writel(status, &regs->outbound_intr_status);
136 readl(&regs->outbound_intr_status);
137 return 1;
139 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
140 return 0;
143 * dummy read to flush PCI
145 readl(&regs->outbound_intr_status);
147 return 1;
151 * megasas_get_cmd_fusion - Get a command from the free pool
152 * @instance: Adapter soft state
154 * Returns a free command from the pool
156 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
157 *instance)
159 unsigned long flags;
160 struct fusion_context *fusion =
161 (struct fusion_context *)instance->ctrl_context;
162 struct megasas_cmd_fusion *cmd = NULL;
164 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
166 if (!list_empty(&fusion->cmd_pool)) {
167 cmd = list_entry((&fusion->cmd_pool)->next,
168 struct megasas_cmd_fusion, list);
169 list_del_init(&cmd->list);
170 } else {
171 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
174 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
175 return cmd;
179 * megasas_return_cmd_fusion - Return a cmd to free command pool
180 * @instance: Adapter soft state
181 * @cmd: Command packet to be returned to free command pool
183 static inline void
184 megasas_return_cmd_fusion(struct megasas_instance *instance,
185 struct megasas_cmd_fusion *cmd)
187 unsigned long flags;
188 struct fusion_context *fusion =
189 (struct fusion_context *)instance->ctrl_context;
191 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
193 cmd->scmd = NULL;
194 cmd->sync_cmd_idx = (u32)ULONG_MAX;
195 list_add_tail(&cmd->list, &fusion->cmd_pool);
197 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
201 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
202 * @instance: Adapter soft state
204 static void megasas_teardown_frame_pool_fusion(
205 struct megasas_instance *instance)
207 int i;
208 struct fusion_context *fusion = instance->ctrl_context;
210 u16 max_cmd = instance->max_fw_cmds;
212 struct megasas_cmd_fusion *cmd;
214 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
215 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
216 "sense pool : %p\n", fusion->sg_dma_pool,
217 fusion->sense_dma_pool);
218 return;
222 * Return all frames to pool
224 for (i = 0; i < max_cmd; i++) {
226 cmd = fusion->cmd_list[i];
228 if (cmd->sg_frame)
229 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
230 cmd->sg_frame_phys_addr);
232 if (cmd->sense)
233 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
234 cmd->sense_phys_addr);
238 * Now destroy the pool itself
240 pci_pool_destroy(fusion->sg_dma_pool);
241 pci_pool_destroy(fusion->sense_dma_pool);
243 fusion->sg_dma_pool = NULL;
244 fusion->sense_dma_pool = NULL;
248 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
249 * @instance: Adapter soft state
251 void
252 megasas_free_cmds_fusion(struct megasas_instance *instance)
254 int i;
255 struct fusion_context *fusion = instance->ctrl_context;
257 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
260 req_sz = fusion->request_alloc_sz;
261 reply_sz = fusion->reply_alloc_sz;
262 io_frames_sz = fusion->io_frames_alloc_sz;
264 max_cmds = instance->max_fw_cmds;
266 /* Free descriptors and request Frames memory */
267 if (fusion->req_frames_desc)
268 dma_free_coherent(&instance->pdev->dev, req_sz,
269 fusion->req_frames_desc,
270 fusion->req_frames_desc_phys);
272 if (fusion->reply_frames_desc) {
273 pci_pool_free(fusion->reply_frames_desc_pool,
274 fusion->reply_frames_desc,
275 fusion->reply_frames_desc_phys);
276 pci_pool_destroy(fusion->reply_frames_desc_pool);
279 if (fusion->io_request_frames) {
280 pci_pool_free(fusion->io_request_frames_pool,
281 fusion->io_request_frames,
282 fusion->io_request_frames_phys);
283 pci_pool_destroy(fusion->io_request_frames_pool);
286 /* Free the Fusion frame pool */
287 megasas_teardown_frame_pool_fusion(instance);
289 /* Free all the commands in the cmd_list */
290 for (i = 0; i < max_cmds; i++)
291 kfree(fusion->cmd_list[i]);
293 /* Free the cmd_list buffer itself */
294 kfree(fusion->cmd_list);
295 fusion->cmd_list = NULL;
297 INIT_LIST_HEAD(&fusion->cmd_pool);
301 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
302 * @instance: Adapter soft state
305 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
307 int i;
308 u32 max_cmd;
309 struct fusion_context *fusion;
310 struct megasas_cmd_fusion *cmd;
311 u32 total_sz_chain_frame;
313 fusion = instance->ctrl_context;
314 max_cmd = instance->max_fw_cmds;
316 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
319 * Use DMA pool facility provided by PCI layer
322 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
323 instance->pdev,
324 total_sz_chain_frame, 4,
326 if (!fusion->sg_dma_pool) {
327 printk(KERN_DEBUG "megasas: failed to setup request pool "
328 "fusion\n");
329 return -ENOMEM;
331 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
332 instance->pdev,
333 SCSI_SENSE_BUFFERSIZE, 64, 0);
335 if (!fusion->sense_dma_pool) {
336 printk(KERN_DEBUG "megasas: failed to setup sense pool "
337 "fusion\n");
338 pci_pool_destroy(fusion->sg_dma_pool);
339 fusion->sg_dma_pool = NULL;
340 return -ENOMEM;
344 * Allocate and attach a frame to each of the commands in cmd_list
346 for (i = 0; i < max_cmd; i++) {
348 cmd = fusion->cmd_list[i];
350 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
351 GFP_KERNEL,
352 &cmd->sg_frame_phys_addr);
354 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
355 GFP_KERNEL, &cmd->sense_phys_addr);
357 * megasas_teardown_frame_pool_fusion() takes care of freeing
358 * whatever has been allocated
360 if (!cmd->sg_frame || !cmd->sense) {
361 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
362 megasas_teardown_frame_pool_fusion(instance);
363 return -ENOMEM;
366 return 0;
370 * megasas_alloc_cmds_fusion - Allocates the command packets
371 * @instance: Adapter soft state
374 * Each frame has a 32-bit field called context. This context is used to get
375 * back the megasas_cmd_fusion from the frame when a frame gets completed
376 * In this driver, the 32 bit values are the indices into an array cmd_list.
377 * This array is used only to look up the megasas_cmd_fusion given the context.
378 * The free commands themselves are maintained in a linked list called cmd_pool.
380 * cmds are formed in the io_request and sg_frame members of the
381 * megasas_cmd_fusion. The context field is used to get a request descriptor
382 * and is used as SMID of the cmd.
383 * SMID value range is from 1 to max_fw_cmds.
386 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
388 int i, j;
389 u32 max_cmd, io_frames_sz;
390 struct fusion_context *fusion;
391 struct megasas_cmd_fusion *cmd;
392 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
393 u32 offset;
394 dma_addr_t io_req_base_phys;
395 u8 *io_req_base;
397 fusion = instance->ctrl_context;
399 max_cmd = instance->max_fw_cmds;
401 fusion->req_frames_desc =
402 dma_alloc_coherent(&instance->pdev->dev,
403 fusion->request_alloc_sz,
404 &fusion->req_frames_desc_phys, GFP_KERNEL);
406 if (!fusion->req_frames_desc) {
407 printk(KERN_ERR "megasas; Could not allocate memory for "
408 "request_frames\n");
409 goto fail_req_desc;
412 fusion->reply_frames_desc_pool =
413 pci_pool_create("reply_frames pool", instance->pdev,
414 fusion->reply_alloc_sz, 16, 0);
416 if (!fusion->reply_frames_desc_pool) {
417 printk(KERN_ERR "megasas; Could not allocate memory for "
418 "reply_frame pool\n");
419 goto fail_reply_desc;
422 fusion->reply_frames_desc =
423 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
424 &fusion->reply_frames_desc_phys);
425 if (!fusion->reply_frames_desc) {
426 printk(KERN_ERR "megasas; Could not allocate memory for "
427 "reply_frame pool\n");
428 pci_pool_destroy(fusion->reply_frames_desc_pool);
429 goto fail_reply_desc;
432 reply_desc = fusion->reply_frames_desc;
433 for (i = 0; i < fusion->reply_q_depth; i++, reply_desc++)
434 reply_desc->Words = ULLONG_MAX;
436 io_frames_sz = fusion->io_frames_alloc_sz;
438 fusion->io_request_frames_pool =
439 pci_pool_create("io_request_frames pool", instance->pdev,
440 fusion->io_frames_alloc_sz, 16, 0);
442 if (!fusion->io_request_frames_pool) {
443 printk(KERN_ERR "megasas: Could not allocate memory for "
444 "io_request_frame pool\n");
445 goto fail_io_frames;
448 fusion->io_request_frames =
449 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
450 &fusion->io_request_frames_phys);
451 if (!fusion->io_request_frames) {
452 printk(KERN_ERR "megasas: Could not allocate memory for "
453 "io_request_frames frames\n");
454 pci_pool_destroy(fusion->io_request_frames_pool);
455 goto fail_io_frames;
459 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
460 * Allocate the dynamic array first and then allocate individual
461 * commands.
463 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
464 *max_cmd, GFP_KERNEL);
466 if (!fusion->cmd_list) {
467 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
468 "memory for cmd_list_fusion\n");
469 goto fail_cmd_list;
472 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
473 *max_cmd);
475 max_cmd = instance->max_fw_cmds;
476 for (i = 0; i < max_cmd; i++) {
477 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
478 GFP_KERNEL);
479 if (!fusion->cmd_list[i]) {
480 printk(KERN_ERR "Could not alloc cmd list fusion\n");
482 for (j = 0; j < i; j++)
483 kfree(fusion->cmd_list[j]);
485 kfree(fusion->cmd_list);
486 fusion->cmd_list = NULL;
487 goto fail_cmd_list;
491 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
492 io_req_base = fusion->io_request_frames +
493 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
494 io_req_base_phys = fusion->io_request_frames_phys +
495 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
498 * Add all the commands to command pool (fusion->cmd_pool)
501 /* SMID 0 is reserved. Set SMID/index from 1 */
502 for (i = 0; i < max_cmd; i++) {
503 cmd = fusion->cmd_list[i];
504 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
505 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
506 cmd->index = i + 1;
507 cmd->scmd = NULL;
508 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
509 cmd->instance = instance;
510 cmd->io_request =
511 (struct MPI2_RAID_SCSI_IO_REQUEST *)
512 (io_req_base + offset);
513 memset(cmd->io_request, 0,
514 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
515 cmd->io_request_phys_addr = io_req_base_phys + offset;
517 list_add_tail(&cmd->list, &fusion->cmd_pool);
521 * Create a frame pool and assign one frame to each cmd
523 if (megasas_create_frame_pool_fusion(instance)) {
524 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
525 megasas_free_cmds_fusion(instance);
526 goto fail_req_desc;
529 return 0;
531 fail_cmd_list:
532 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
533 fusion->io_request_frames_phys);
534 pci_pool_destroy(fusion->io_request_frames_pool);
535 fail_io_frames:
536 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
537 fusion->reply_frames_desc,
538 fusion->reply_frames_desc_phys);
539 pci_pool_free(fusion->reply_frames_desc_pool,
540 fusion->reply_frames_desc,
541 fusion->reply_frames_desc_phys);
542 pci_pool_destroy(fusion->reply_frames_desc_pool);
544 fail_reply_desc:
545 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
546 fusion->req_frames_desc,
547 fusion->req_frames_desc_phys);
548 fail_req_desc:
549 return -ENOMEM;
553 * wait_and_poll - Issues a polling command
554 * @instance: Adapter soft state
555 * @cmd: Command packet to be issued
557 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
560 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
562 int i;
563 struct megasas_header *frame_hdr = &cmd->frame->hdr;
565 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
568 * Wait for cmd_status to change
570 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
571 rmb();
572 msleep(20);
575 if (frame_hdr->cmd_status == 0xff)
576 return -ETIME;
578 return 0;
582 * megasas_ioc_init_fusion - Initializes the FW
583 * @instance: Adapter soft state
585 * Issues the IOC Init cmd
588 megasas_ioc_init_fusion(struct megasas_instance *instance)
590 struct megasas_init_frame *init_frame;
591 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
592 dma_addr_t ioc_init_handle;
593 u32 context;
594 struct megasas_cmd *cmd;
595 u8 ret;
596 struct fusion_context *fusion;
597 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
598 int i;
599 struct megasas_header *frame_hdr;
601 fusion = instance->ctrl_context;
603 cmd = megasas_get_cmd(instance);
605 if (!cmd) {
606 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
607 ret = 1;
608 goto fail_get_cmd;
611 IOCInitMessage =
612 dma_alloc_coherent(&instance->pdev->dev,
613 sizeof(struct MPI2_IOC_INIT_REQUEST),
614 &ioc_init_handle, GFP_KERNEL);
616 if (!IOCInitMessage) {
617 printk(KERN_ERR "Could not allocate memory for "
618 "IOCInitMessage\n");
619 ret = 1;
620 goto fail_fw_init;
623 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
625 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
626 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
627 IOCInitMessage->MsgVersion = MPI2_VERSION;
628 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
629 IOCInitMessage->SystemRequestFrameSize =
630 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
632 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
633 IOCInitMessage->ReplyDescriptorPostQueueAddress =
634 fusion->reply_frames_desc_phys;
635 IOCInitMessage->SystemRequestFrameBaseAddress =
636 fusion->io_request_frames_phys;
638 init_frame = (struct megasas_init_frame *)cmd->frame;
639 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
641 frame_hdr = &cmd->frame->hdr;
642 context = init_frame->context;
643 init_frame->context = context;
645 frame_hdr->cmd_status = 0xFF;
646 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
648 init_frame->cmd = MFI_CMD_INIT;
649 init_frame->cmd_status = 0xFF;
651 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
652 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
654 req_desc =
655 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
657 req_desc->Words = cmd->frame_phys_addr;
658 req_desc->MFAIo.RequestFlags =
659 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
660 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
663 * disable the intr before firing the init frame
665 instance->instancet->disable_intr(instance->reg_set);
667 for (i = 0; i < (10 * 1000); i += 20) {
668 if (readl(&instance->reg_set->doorbell) & 1)
669 msleep(20);
670 else
671 break;
674 instance->instancet->fire_cmd(instance, req_desc->u.low,
675 req_desc->u.high, instance->reg_set);
677 wait_and_poll(instance, cmd);
679 frame_hdr = &cmd->frame->hdr;
680 if (frame_hdr->cmd_status != 0) {
681 ret = 1;
682 goto fail_fw_init;
684 printk(KERN_ERR "megasas:IOC Init cmd success\n");
686 ret = 0;
688 fail_fw_init:
689 megasas_return_cmd(instance, cmd);
690 if (IOCInitMessage)
691 dma_free_coherent(&instance->pdev->dev,
692 sizeof(struct MPI2_IOC_INIT_REQUEST),
693 IOCInitMessage, ioc_init_handle);
694 fail_get_cmd:
695 return ret;
699 * megasas_get_ld_map_info - Returns FW's ld_map structure
700 * @instance: Adapter soft state
701 * @pend: Pend the command or not
702 * Issues an internal command (DCMD) to get the FW's controller PD
703 * list structure. This information is mainly used to find out SYSTEM
704 * supported by the FW.
706 static int
707 megasas_get_ld_map_info(struct megasas_instance *instance)
709 int ret = 0;
710 struct megasas_cmd *cmd;
711 struct megasas_dcmd_frame *dcmd;
712 struct MR_FW_RAID_MAP_ALL *ci;
713 dma_addr_t ci_h = 0;
714 u32 size_map_info;
715 struct fusion_context *fusion;
717 cmd = megasas_get_cmd(instance);
719 if (!cmd) {
720 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
721 return -ENOMEM;
724 fusion = instance->ctrl_context;
726 if (!fusion) {
727 megasas_return_cmd(instance, cmd);
728 return 1;
731 dcmd = &cmd->frame->dcmd;
733 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
734 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
736 ci = fusion->ld_map[(instance->map_id & 1)];
737 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
739 if (!ci) {
740 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
741 megasas_return_cmd(instance, cmd);
742 return -ENOMEM;
745 memset(ci, 0, sizeof(*ci));
746 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
748 dcmd->cmd = MFI_CMD_DCMD;
749 dcmd->cmd_status = 0xFF;
750 dcmd->sge_count = 1;
751 dcmd->flags = MFI_FRAME_DIR_READ;
752 dcmd->timeout = 0;
753 dcmd->pad_0 = 0;
754 dcmd->data_xfer_len = size_map_info;
755 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
756 dcmd->sgl.sge32[0].phys_addr = ci_h;
757 dcmd->sgl.sge32[0].length = size_map_info;
759 if (!megasas_issue_polled(instance, cmd))
760 ret = 0;
761 else {
762 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
763 ret = -1;
766 megasas_return_cmd(instance, cmd);
768 return ret;
772 megasas_get_map_info(struct megasas_instance *instance)
774 struct fusion_context *fusion = instance->ctrl_context;
776 fusion->fast_path_io = 0;
777 if (!megasas_get_ld_map_info(instance)) {
778 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
779 fusion->load_balance_info)) {
780 fusion->fast_path_io = 1;
781 return 0;
784 return 1;
788 * megasas_sync_map_info - Returns FW's ld_map structure
789 * @instance: Adapter soft state
791 * Issues an internal command (DCMD) to get the FW's controller PD
792 * list structure. This information is mainly used to find out SYSTEM
793 * supported by the FW.
796 megasas_sync_map_info(struct megasas_instance *instance)
798 int ret = 0, i;
799 struct megasas_cmd *cmd;
800 struct megasas_dcmd_frame *dcmd;
801 u32 size_sync_info, num_lds;
802 struct fusion_context *fusion;
803 struct MR_LD_TARGET_SYNC *ci = NULL;
804 struct MR_FW_RAID_MAP_ALL *map;
805 struct MR_LD_RAID *raid;
806 struct MR_LD_TARGET_SYNC *ld_sync;
807 dma_addr_t ci_h = 0;
808 u32 size_map_info;
810 cmd = megasas_get_cmd(instance);
812 if (!cmd) {
813 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
814 "info.\n");
815 return -ENOMEM;
818 fusion = instance->ctrl_context;
820 if (!fusion) {
821 megasas_return_cmd(instance, cmd);
822 return 1;
825 map = fusion->ld_map[instance->map_id & 1];
827 num_lds = map->raidMap.ldCount;
829 dcmd = &cmd->frame->dcmd;
831 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
833 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
835 ci = (struct MR_LD_TARGET_SYNC *)
836 fusion->ld_map[(instance->map_id - 1) & 1];
837 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
839 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
841 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
843 for (i = 0; i < num_lds; i++, ld_sync++) {
844 raid = MR_LdRaidGet(i, map);
845 ld_sync->targetId = MR_GetLDTgtId(i, map);
846 ld_sync->seqNum = raid->seqNum;
849 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
850 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
852 dcmd->cmd = MFI_CMD_DCMD;
853 dcmd->cmd_status = 0xFF;
854 dcmd->sge_count = 1;
855 dcmd->flags = MFI_FRAME_DIR_WRITE;
856 dcmd->timeout = 0;
857 dcmd->pad_0 = 0;
858 dcmd->data_xfer_len = size_map_info;
859 dcmd->mbox.b[0] = num_lds;
860 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
861 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
862 dcmd->sgl.sge32[0].phys_addr = ci_h;
863 dcmd->sgl.sge32[0].length = size_map_info;
865 instance->map_update_cmd = cmd;
867 instance->instancet->issue_dcmd(instance, cmd);
869 return ret;
873 * megasas_init_adapter_fusion - Initializes the FW
874 * @instance: Adapter soft state
876 * This is the main function for initializing firmware.
879 megasas_init_adapter_fusion(struct megasas_instance *instance)
881 struct megasas_register_set __iomem *reg_set;
882 struct fusion_context *fusion;
883 u32 max_cmd;
884 int i = 0;
886 fusion = instance->ctrl_context;
888 reg_set = instance->reg_set;
891 * Get various operational parameters from status register
893 instance->max_fw_cmds =
894 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
895 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
898 * Reduce the max supported cmds by 1. This is to ensure that the
899 * reply_q_sz (1 more than the max cmd that driver may send)
900 * does not exceed max cmds that the FW can support
902 instance->max_fw_cmds = instance->max_fw_cmds-1;
903 /* Only internal cmds (DCMD) need to have MFI frames */
904 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
906 max_cmd = instance->max_fw_cmds;
908 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
910 fusion->request_alloc_sz =
911 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
912 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
913 *(fusion->reply_q_depth);
914 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
915 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
916 (max_cmd + 1)); /* Extra 1 for SMID 0 */
918 fusion->max_sge_in_main_msg =
919 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
920 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
922 fusion->max_sge_in_chain =
923 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
925 instance->max_num_sge = fusion->max_sge_in_main_msg +
926 fusion->max_sge_in_chain - 2;
928 /* Used for pass thru MFI frame (DCMD) */
929 fusion->chain_offset_mfi_pthru =
930 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
932 fusion->chain_offset_io_request =
933 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
934 sizeof(union MPI2_SGE_IO_UNION))/16;
936 fusion->last_reply_idx = 0;
939 * Allocate memory for descriptors
940 * Create a pool of commands
942 if (megasas_alloc_cmds(instance))
943 goto fail_alloc_mfi_cmds;
944 if (megasas_alloc_cmds_fusion(instance))
945 goto fail_alloc_cmds;
947 if (megasas_ioc_init_fusion(instance))
948 goto fail_ioc_init;
950 instance->flag_ieee = 1;
952 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
953 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
955 fusion->fast_path_io = 0;
957 for (i = 0; i < 2; i++) {
958 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
959 fusion->map_sz,
960 &fusion->ld_map_phys[i],
961 GFP_KERNEL);
962 if (!fusion->ld_map[i]) {
963 printk(KERN_ERR "megasas: Could not allocate memory "
964 "for map info\n");
965 goto fail_map_info;
969 if (!megasas_get_map_info(instance))
970 megasas_sync_map_info(instance);
972 return 0;
974 fail_map_info:
975 if (i == 1)
976 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
977 fusion->ld_map[0], fusion->ld_map_phys[0]);
978 fail_ioc_init:
979 megasas_free_cmds_fusion(instance);
980 fail_alloc_cmds:
981 megasas_free_cmds(instance);
982 fail_alloc_mfi_cmds:
983 return 1;
987 * megasas_fire_cmd_fusion - Sends command to the FW
988 * @frame_phys_addr : Physical address of cmd
989 * @frame_count : Number of frames for the command
990 * @regs : MFI register set
992 void
993 megasas_fire_cmd_fusion(struct megasas_instance *instance,
994 dma_addr_t req_desc_lo,
995 u32 req_desc_hi,
996 struct megasas_register_set __iomem *regs)
998 unsigned long flags;
1000 spin_lock_irqsave(&instance->hba_lock, flags);
1002 writel(req_desc_lo,
1003 &(regs)->inbound_low_queue_port);
1004 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1005 spin_unlock_irqrestore(&instance->hba_lock, flags);
1009 * map_cmd_status - Maps FW cmd status to OS cmd status
1010 * @cmd : Pointer to cmd
1011 * @status : status of cmd returned by FW
1012 * @ext_status : ext status of cmd returned by FW
1015 void
1016 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1019 switch (status) {
1021 case MFI_STAT_OK:
1022 cmd->scmd->result = DID_OK << 16;
1023 break;
1025 case MFI_STAT_SCSI_IO_FAILED:
1026 case MFI_STAT_LD_INIT_IN_PROGRESS:
1027 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1028 break;
1030 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1032 cmd->scmd->result = (DID_OK << 16) | ext_status;
1033 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1034 memset(cmd->scmd->sense_buffer, 0,
1035 SCSI_SENSE_BUFFERSIZE);
1036 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1037 SCSI_SENSE_BUFFERSIZE);
1038 cmd->scmd->result |= DRIVER_SENSE << 24;
1040 break;
1042 case MFI_STAT_LD_OFFLINE:
1043 case MFI_STAT_DEVICE_NOT_FOUND:
1044 cmd->scmd->result = DID_BAD_TARGET << 16;
1045 break;
1047 default:
1048 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1049 cmd->scmd->result = DID_ERROR << 16;
1050 break;
1055 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1056 * @instance: Adapter soft state
1057 * @scp: SCSI command from the mid-layer
1058 * @sgl_ptr: SGL to be filled in
1059 * @cmd: cmd we are working on
1061 * If successful, this function returns the number of SG elements.
1063 static int
1064 megasas_make_sgl_fusion(struct megasas_instance *instance,
1065 struct scsi_cmnd *scp,
1066 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1067 struct megasas_cmd_fusion *cmd)
1069 int i, sg_processed;
1070 int sge_count, sge_idx;
1071 struct scatterlist *os_sgl;
1072 struct fusion_context *fusion;
1074 fusion = instance->ctrl_context;
1076 cmd->io_request->ChainOffset = 0;
1078 sge_count = scsi_dma_map(scp);
1080 BUG_ON(sge_count < 0);
1082 if (sge_count > instance->max_num_sge || !sge_count)
1083 return sge_count;
1085 if (sge_count > fusion->max_sge_in_main_msg) {
1086 /* One element to store the chain info */
1087 sge_idx = fusion->max_sge_in_main_msg - 1;
1088 } else
1089 sge_idx = sge_count;
1091 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1092 sgl_ptr->Length = sg_dma_len(os_sgl);
1093 sgl_ptr->Address = sg_dma_address(os_sgl);
1094 sgl_ptr->Flags = 0;
1095 sgl_ptr++;
1097 sg_processed = i + 1;
1099 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1100 (sge_count > fusion->max_sge_in_main_msg)) {
1102 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1103 cmd->io_request->ChainOffset =
1104 fusion->chain_offset_io_request;
1105 sg_chain = sgl_ptr;
1106 /* Prepare chain element */
1107 sg_chain->NextChainOffset = 0;
1108 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1109 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1110 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1111 *(sge_count - sg_processed));
1112 sg_chain->Address = cmd->sg_frame_phys_addr;
1114 sgl_ptr =
1115 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1119 return sge_count;
1123 * megasas_set_pd_lba - Sets PD LBA
1124 * @cdb: CDB
1125 * @cdb_len: cdb length
1126 * @start_blk: Start block of IO
1128 * Used to set the PD LBA in CDB for FP IOs
1130 void
1131 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1132 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1133 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1135 struct MR_LD_RAID *raid;
1136 u32 ld;
1137 u64 start_blk = io_info->pdBlock;
1138 u8 *cdb = io_request->CDB.CDB32;
1139 u32 num_blocks = io_info->numBlocks;
1140 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1142 /* Check if T10 PI (DIF) is enabled for this LD */
1143 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1144 raid = MR_LdRaidGet(ld, local_map_ptr);
1145 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1146 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1147 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1148 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1150 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1151 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1152 else
1153 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1154 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1156 /* LBA */
1157 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1158 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1159 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1160 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1161 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1162 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1163 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1164 cdb[19] = (u8)(start_blk & 0xff);
1166 /* Logical block reference tag */
1167 io_request->CDB.EEDP32.PrimaryReferenceTag =
1168 cpu_to_be32(ref_tag);
1169 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1171 io_request->DataLength = num_blocks * 512;
1172 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1174 /* Transfer length */
1175 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1176 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1177 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1178 cdb[31] = (u8)(num_blocks & 0xff);
1180 /* set SCSI IO EEDPFlags */
1181 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1182 io_request->EEDPFlags =
1183 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1184 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1185 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1186 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1187 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1188 } else {
1189 io_request->EEDPFlags =
1190 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1191 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1193 io_request->Control |= (0x4 << 26);
1194 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1195 } else {
1196 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1197 if (((cdb_len == 12) || (cdb_len == 16)) &&
1198 (start_blk <= 0xffffffff)) {
1199 if (cdb_len == 16) {
1200 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1201 flagvals = cdb[1];
1202 groupnum = cdb[14];
1203 control = cdb[15];
1204 } else {
1205 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1206 flagvals = cdb[1];
1207 groupnum = cdb[10];
1208 control = cdb[11];
1211 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1213 cdb[0] = opcode;
1214 cdb[1] = flagvals;
1215 cdb[6] = groupnum;
1216 cdb[9] = control;
1218 /* Transfer length */
1219 cdb[8] = (u8)(num_blocks & 0xff);
1220 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1222 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1223 cdb_len = 10;
1224 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1225 /* Convert to 16 byte CDB for large LBA's */
1226 switch (cdb_len) {
1227 case 6:
1228 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1229 control = cdb[5];
1230 break;
1231 case 10:
1232 opcode =
1233 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1234 flagvals = cdb[1];
1235 groupnum = cdb[6];
1236 control = cdb[9];
1237 break;
1238 case 12:
1239 opcode =
1240 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1241 flagvals = cdb[1];
1242 groupnum = cdb[10];
1243 control = cdb[11];
1244 break;
1247 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1249 cdb[0] = opcode;
1250 cdb[1] = flagvals;
1251 cdb[14] = groupnum;
1252 cdb[15] = control;
1254 /* Transfer length */
1255 cdb[13] = (u8)(num_blocks & 0xff);
1256 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1257 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1258 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1260 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1261 cdb_len = 16;
1264 /* Normal case, just load LBA here */
1265 switch (cdb_len) {
1266 case 6:
1268 u8 val = cdb[1] & 0xE0;
1269 cdb[3] = (u8)(start_blk & 0xff);
1270 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1271 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1272 break;
1274 case 10:
1275 cdb[5] = (u8)(start_blk & 0xff);
1276 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1277 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1278 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1279 break;
1280 case 12:
1281 cdb[5] = (u8)(start_blk & 0xff);
1282 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1283 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1284 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1285 break;
1286 case 16:
1287 cdb[9] = (u8)(start_blk & 0xff);
1288 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1289 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1290 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1291 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1292 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1293 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1294 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1295 break;
1301 * megasas_build_ldio_fusion - Prepares IOs to devices
1302 * @instance: Adapter soft state
1303 * @scp: SCSI command
1304 * @cmd: Command to be prepared
1306 * Prepares the io_request and chain elements (sg_frame) for IO
1307 * The IO can be for PD (Fast Path) or LD
1309 void
1310 megasas_build_ldio_fusion(struct megasas_instance *instance,
1311 struct scsi_cmnd *scp,
1312 struct megasas_cmd_fusion *cmd)
1314 u8 fp_possible;
1315 u32 start_lba_lo, start_lba_hi, device_id;
1316 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1317 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1318 struct IO_REQUEST_INFO io_info;
1319 struct fusion_context *fusion;
1320 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1322 device_id = MEGASAS_DEV_INDEX(instance, scp);
1324 fusion = instance->ctrl_context;
1326 io_request = cmd->io_request;
1327 io_request->RaidContext.VirtualDiskTgtId = device_id;
1328 io_request->RaidContext.status = 0;
1329 io_request->RaidContext.exStatus = 0;
1331 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1333 start_lba_lo = 0;
1334 start_lba_hi = 0;
1335 fp_possible = 0;
1338 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1340 if (scp->cmd_len == 6) {
1341 io_request->DataLength = (u32) scp->cmnd[4];
1342 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1343 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1345 start_lba_lo &= 0x1FFFFF;
1349 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1351 else if (scp->cmd_len == 10) {
1352 io_request->DataLength = (u32) scp->cmnd[8] |
1353 ((u32) scp->cmnd[7] << 8);
1354 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1355 ((u32) scp->cmnd[3] << 16) |
1356 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1360 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1362 else if (scp->cmd_len == 12) {
1363 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1364 ((u32) scp->cmnd[7] << 16) |
1365 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1366 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1367 ((u32) scp->cmnd[3] << 16) |
1368 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1372 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1374 else if (scp->cmd_len == 16) {
1375 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1376 ((u32) scp->cmnd[11] << 16) |
1377 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1378 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1379 ((u32) scp->cmnd[7] << 16) |
1380 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1382 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1383 ((u32) scp->cmnd[3] << 16) |
1384 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1387 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1388 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1389 io_info.numBlocks = io_request->DataLength;
1390 io_info.ldTgtId = device_id;
1392 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1393 io_info.isRead = 1;
1395 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1397 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1398 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1399 io_request->RaidContext.regLockFlags = 0;
1400 fp_possible = 0;
1401 } else {
1402 if (MR_BuildRaidContext(&io_info, &io_request->RaidContext,
1403 local_map_ptr))
1404 fp_possible = io_info.fpOkForIo;
1407 if (fp_possible) {
1408 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1409 local_map_ptr, start_lba_lo);
1410 io_request->DataLength = scsi_bufflen(scp);
1411 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1412 cmd->request_desc->SCSIIO.RequestFlags =
1413 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1414 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1415 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1416 (io_info.isRead)) {
1417 io_info.devHandle =
1418 get_updated_dev_handle(
1419 &fusion->load_balance_info[device_id],
1420 &io_info);
1421 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1422 } else
1423 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1424 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1425 io_request->DevHandle = io_info.devHandle;
1426 } else {
1427 io_request->RaidContext.timeoutValue =
1428 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1429 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1430 io_request->DevHandle = device_id;
1431 cmd->request_desc->SCSIIO.RequestFlags =
1432 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1433 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1434 } /* Not FP */
1438 * megasas_build_dcdb_fusion - Prepares IOs to devices
1439 * @instance: Adapter soft state
1440 * @scp: SCSI command
1441 * @cmd: Command to be prepared
1443 * Prepares the io_request frame for non-io cmds
1445 static void
1446 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1447 struct scsi_cmnd *scmd,
1448 struct megasas_cmd_fusion *cmd)
1450 u32 device_id;
1451 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1452 u16 pd_index = 0;
1453 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1454 struct fusion_context *fusion = instance->ctrl_context;
1456 io_request = cmd->io_request;
1457 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1458 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1459 +scmd->device->id;
1460 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1462 /* Check if this is a system PD I/O */
1463 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1464 io_request->Function = 0;
1465 io_request->DevHandle =
1466 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1467 io_request->RaidContext.timeoutValue =
1468 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1469 io_request->RaidContext.regLockFlags = 0;
1470 io_request->RaidContext.regLockRowLBA = 0;
1471 io_request->RaidContext.regLockLength = 0;
1472 io_request->RaidContext.RAIDFlags =
1473 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1474 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1475 cmd->request_desc->SCSIIO.RequestFlags =
1476 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1477 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1478 } else {
1479 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1480 io_request->DevHandle = device_id;
1481 cmd->request_desc->SCSIIO.RequestFlags =
1482 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1483 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1485 io_request->RaidContext.VirtualDiskTgtId = device_id;
1486 io_request->LUN[1] = scmd->device->lun;
1487 io_request->DataLength = scsi_bufflen(scmd);
1491 * megasas_build_io_fusion - Prepares IOs to devices
1492 * @instance: Adapter soft state
1493 * @scp: SCSI command
1494 * @cmd: Command to be prepared
1496 * Invokes helper functions to prepare request frames
1497 * and sets flags appropriate for IO/Non-IO cmd
1500 megasas_build_io_fusion(struct megasas_instance *instance,
1501 struct scsi_cmnd *scp,
1502 struct megasas_cmd_fusion *cmd)
1504 u32 device_id, sge_count;
1505 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1507 device_id = MEGASAS_DEV_INDEX(instance, scp);
1509 /* Zero out some fields so they don't get reused */
1510 io_request->LUN[1] = 0;
1511 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1512 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1513 io_request->EEDPFlags = 0;
1514 io_request->Control = 0;
1515 io_request->EEDPBlockSize = 0;
1516 io_request->IoFlags = 0;
1517 io_request->RaidContext.RAIDFlags = 0;
1519 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1521 * Just the CDB length,rest of the Flags are zero
1522 * This will be modified for FP in build_ldio_fusion
1524 io_request->IoFlags = scp->cmd_len;
1526 if (megasas_is_ldio(scp))
1527 megasas_build_ldio_fusion(instance, scp, cmd);
1528 else
1529 megasas_build_dcdb_fusion(instance, scp, cmd);
1532 * Construct SGL
1535 sge_count =
1536 megasas_make_sgl_fusion(instance, scp,
1537 (struct MPI25_IEEE_SGE_CHAIN64 *)
1538 &io_request->SGL, cmd);
1540 if (sge_count > instance->max_num_sge) {
1541 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1542 "max (0x%x) allowed\n", sge_count,
1543 instance->max_num_sge);
1544 return 1;
1547 io_request->RaidContext.numSGE = sge_count;
1549 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1551 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1552 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1553 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1554 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1556 io_request->SGLOffset0 =
1557 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1559 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1560 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1562 cmd->scmd = scp;
1563 scp->SCp.ptr = (char *)cmd;
1565 return 0;
1568 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1569 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1571 u8 *p;
1572 struct fusion_context *fusion;
1574 if (index >= instance->max_fw_cmds) {
1575 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1576 "descriptor\n", index);
1577 return NULL;
1579 fusion = instance->ctrl_context;
1580 p = fusion->req_frames_desc
1581 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1583 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1587 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1588 * issuing non IOCTL cmd
1589 * @instance: Adapter soft state
1590 * @scmd: pointer to scsi cmd from OS
1592 static u32
1593 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1594 struct scsi_cmnd *scmd)
1596 struct megasas_cmd_fusion *cmd;
1597 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1598 u32 index;
1599 struct fusion_context *fusion;
1601 fusion = instance->ctrl_context;
1603 cmd = megasas_get_cmd_fusion(instance);
1604 if (!cmd)
1605 return SCSI_MLQUEUE_HOST_BUSY;
1607 index = cmd->index;
1609 req_desc = megasas_get_request_descriptor(instance, index-1);
1610 if (!req_desc)
1611 return 1;
1613 req_desc->Words = 0;
1614 cmd->request_desc = req_desc;
1615 cmd->request_desc->Words = 0;
1617 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1618 megasas_return_cmd_fusion(instance, cmd);
1619 printk(KERN_ERR "megasas: Error building command.\n");
1620 cmd->request_desc = NULL;
1621 return 1;
1624 req_desc = cmd->request_desc;
1625 req_desc->SCSIIO.SMID = index;
1627 if (cmd->io_request->ChainOffset != 0 &&
1628 cmd->io_request->ChainOffset != 0xF)
1629 printk(KERN_ERR "megasas: The chain offset value is not "
1630 "correct : %x\n", cmd->io_request->ChainOffset);
1633 * Issue the command to the FW
1635 atomic_inc(&instance->fw_outstanding);
1637 instance->instancet->fire_cmd(instance,
1638 req_desc->u.low, req_desc->u.high,
1639 instance->reg_set);
1641 return 0;
1645 * complete_cmd_fusion - Completes command
1646 * @instance: Adapter soft state
1647 * Completes all commands that is in reply descriptor queue
1650 complete_cmd_fusion(struct megasas_instance *instance)
1652 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1653 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1654 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1655 struct fusion_context *fusion;
1656 struct megasas_cmd *cmd_mfi;
1657 struct megasas_cmd_fusion *cmd_fusion;
1658 u16 smid, num_completed;
1659 u8 reply_descript_type, arm;
1660 u32 status, extStatus, device_id;
1661 union desc_value d_val;
1662 struct LD_LOAD_BALANCE_INFO *lbinfo;
1664 fusion = instance->ctrl_context;
1666 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1667 return IRQ_HANDLED;
1669 desc = fusion->reply_frames_desc;
1670 desc += fusion->last_reply_idx;
1672 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1674 d_val.word = desc->Words;
1676 reply_descript_type = reply_desc->ReplyFlags &
1677 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1679 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1680 return IRQ_NONE;
1682 d_val.word = desc->Words;
1684 num_completed = 0;
1686 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1687 smid = reply_desc->SMID;
1689 cmd_fusion = fusion->cmd_list[smid - 1];
1691 scsi_io_req =
1692 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1693 cmd_fusion->io_request;
1695 if (cmd_fusion->scmd)
1696 cmd_fusion->scmd->SCp.ptr = NULL;
1698 status = scsi_io_req->RaidContext.status;
1699 extStatus = scsi_io_req->RaidContext.exStatus;
1701 switch (scsi_io_req->Function) {
1702 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1703 /* Update load balancing info */
1704 device_id = MEGASAS_DEV_INDEX(instance,
1705 cmd_fusion->scmd);
1706 lbinfo = &fusion->load_balance_info[device_id];
1707 if (cmd_fusion->scmd->SCp.Status &
1708 MEGASAS_LOAD_BALANCE_FLAG) {
1709 arm = lbinfo->raid1DevHandle[0] ==
1710 cmd_fusion->io_request->DevHandle ? 0 :
1712 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1713 cmd_fusion->scmd->SCp.Status &=
1714 ~MEGASAS_LOAD_BALANCE_FLAG;
1716 if (reply_descript_type ==
1717 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1718 if (megasas_dbg_lvl == 5)
1719 printk(KERN_ERR "\nmegasas: FAST Path "
1720 "IO Success\n");
1722 /* Fall thru and complete IO */
1723 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1724 /* Map the FW Cmd Status */
1725 map_cmd_status(cmd_fusion, status, extStatus);
1726 scsi_dma_unmap(cmd_fusion->scmd);
1727 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1728 scsi_io_req->RaidContext.status = 0;
1729 scsi_io_req->RaidContext.exStatus = 0;
1730 megasas_return_cmd_fusion(instance, cmd_fusion);
1731 atomic_dec(&instance->fw_outstanding);
1733 break;
1734 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1735 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1736 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1737 cmd_fusion->flags = 0;
1738 megasas_return_cmd_fusion(instance, cmd_fusion);
1740 break;
1743 fusion->last_reply_idx++;
1744 if (fusion->last_reply_idx >= fusion->reply_q_depth)
1745 fusion->last_reply_idx = 0;
1747 desc->Words = ULLONG_MAX;
1748 num_completed++;
1750 /* Get the next reply descriptor */
1751 if (!fusion->last_reply_idx)
1752 desc = fusion->reply_frames_desc;
1753 else
1754 desc++;
1756 reply_desc =
1757 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1759 d_val.word = desc->Words;
1761 reply_descript_type = reply_desc->ReplyFlags &
1762 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1764 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1765 break;
1768 if (!num_completed)
1769 return IRQ_NONE;
1771 wmb();
1772 writel(fusion->last_reply_idx,
1773 &instance->reg_set->reply_post_host_index);
1774 megasas_check_and_restore_queue_depth(instance);
1775 return IRQ_HANDLED;
1779 * megasas_complete_cmd_dpc_fusion - Completes command
1780 * @instance: Adapter soft state
1782 * Tasklet to complete cmds
1784 void
1785 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1787 struct megasas_instance *instance =
1788 (struct megasas_instance *)instance_addr;
1789 unsigned long flags;
1791 /* If we have already declared adapter dead, donot complete cmds */
1792 spin_lock_irqsave(&instance->hba_lock, flags);
1793 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1794 spin_unlock_irqrestore(&instance->hba_lock, flags);
1795 return;
1797 spin_unlock_irqrestore(&instance->hba_lock, flags);
1799 spin_lock_irqsave(&instance->completion_lock, flags);
1800 complete_cmd_fusion(instance);
1801 spin_unlock_irqrestore(&instance->completion_lock, flags);
1805 * megasas_isr_fusion - isr entry point
1807 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1809 struct megasas_instance *instance = (struct megasas_instance *)devp;
1810 u32 mfiStatus, fw_state;
1812 if (!instance->msi_flag) {
1813 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1814 if (!mfiStatus)
1815 return IRQ_NONE;
1818 /* If we are resetting, bail */
1819 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
1820 return IRQ_HANDLED;
1822 if (!complete_cmd_fusion(instance)) {
1823 /* If we didn't complete any commands, check for FW fault */
1824 fw_state = instance->instancet->read_fw_status_reg(
1825 instance->reg_set) & MFI_STATE_MASK;
1826 if (fw_state == MFI_STATE_FAULT)
1827 schedule_work(&instance->work_init);
1830 return IRQ_HANDLED;
1834 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1835 * @instance: Adapter soft state
1836 * mfi_cmd: megasas_cmd pointer
1840 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1841 struct megasas_cmd *mfi_cmd)
1843 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1844 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1845 struct megasas_cmd_fusion *cmd;
1846 struct fusion_context *fusion;
1847 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1849 cmd = megasas_get_cmd_fusion(instance);
1850 if (!cmd)
1851 return 1;
1853 /* Save the smid. To be used for returning the cmd */
1854 mfi_cmd->context.smid = cmd->index;
1856 cmd->sync_cmd_idx = mfi_cmd->index;
1859 * For cmds where the flag is set, store the flag and check
1860 * on completion. For cmds with this flag, don't call
1861 * megasas_complete_cmd
1864 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1865 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1867 fusion = instance->ctrl_context;
1868 io_req = cmd->io_request;
1869 mpi25_ieee_chain =
1870 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1872 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1873 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1874 SGL) / 4;
1875 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1877 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1879 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1880 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1882 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1884 return 0;
1888 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1889 * @instance: Adapter soft state
1890 * @cmd: mfi cmd to build
1893 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1894 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1896 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1897 u16 index;
1899 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1900 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1901 return NULL;
1904 index = cmd->context.smid;
1906 req_desc = megasas_get_request_descriptor(instance, index - 1);
1908 if (!req_desc)
1909 return NULL;
1911 req_desc->Words = 0;
1912 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1913 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1915 req_desc->SCSIIO.SMID = index;
1917 return req_desc;
1921 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1922 * @instance: Adapter soft state
1923 * @cmd: mfi cmd pointer
1926 void
1927 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
1928 struct megasas_cmd *cmd)
1930 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1931 union desc_value d_val;
1933 req_desc = build_mpt_cmd(instance, cmd);
1934 if (!req_desc) {
1935 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
1936 return;
1938 d_val.word = req_desc->Words;
1940 instance->instancet->fire_cmd(instance, req_desc->u.low,
1941 req_desc->u.high, instance->reg_set);
1945 * megasas_release_fusion - Reverses the FW initialization
1946 * @intance: Adapter soft state
1948 void
1949 megasas_release_fusion(struct megasas_instance *instance)
1951 megasas_free_cmds(instance);
1952 megasas_free_cmds_fusion(instance);
1954 iounmap(instance->reg_set);
1956 pci_release_selected_regions(instance->pdev, instance->bar);
1960 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1961 * @regs: MFI register set
1963 static u32
1964 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
1966 return readl(&(regs)->outbound_scratch_pad);
1970 * megasas_adp_reset_fusion - For controller reset
1971 * @regs: MFI register set
1973 static int
1974 megasas_adp_reset_fusion(struct megasas_instance *instance,
1975 struct megasas_register_set __iomem *regs)
1977 return 0;
1981 * megasas_check_reset_fusion - For controller reset check
1982 * @regs: MFI register set
1984 static int
1985 megasas_check_reset_fusion(struct megasas_instance *instance,
1986 struct megasas_register_set __iomem *regs)
1988 return 0;
1991 /* This function waits for outstanding commands on fusion to complete */
1992 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
1994 int i, outstanding, retval = 0;
1995 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
1997 for (i = 0; i < wait_time; i++) {
1998 /* Check if firmware is in fault state */
1999 fw_state = instance->instancet->read_fw_status_reg(
2000 instance->reg_set) & MFI_STATE_MASK;
2001 if (fw_state == MFI_STATE_FAULT) {
2002 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2003 " will reset adapter.\n");
2004 retval = 1;
2005 goto out;
2008 outstanding = atomic_read(&instance->fw_outstanding);
2009 if (!outstanding)
2010 goto out;
2012 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2013 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2014 "commands to complete\n", i, outstanding);
2015 megasas_complete_cmd_dpc_fusion(
2016 (unsigned long)instance);
2018 msleep(1000);
2021 if (atomic_read(&instance->fw_outstanding)) {
2022 printk("megaraid_sas: pending commands remain after waiting, "
2023 "will reset adapter.\n");
2024 retval = 1;
2026 out:
2027 return retval;
2030 void megasas_reset_reply_desc(struct megasas_instance *instance)
2032 int i;
2033 struct fusion_context *fusion;
2034 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2036 fusion = instance->ctrl_context;
2037 fusion->last_reply_idx = 0;
2038 reply_desc = fusion->reply_frames_desc;
2039 for (i = 0 ; i < fusion->reply_q_depth; i++, reply_desc++)
2040 reply_desc->Words = ULLONG_MAX;
2043 /* Core fusion reset function */
2044 int megasas_reset_fusion(struct Scsi_Host *shost)
2046 int retval = SUCCESS, i, j, retry = 0;
2047 struct megasas_instance *instance;
2048 struct megasas_cmd_fusion *cmd_fusion;
2049 struct fusion_context *fusion;
2050 struct megasas_cmd *cmd_mfi;
2051 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2052 u32 host_diag, abs_state, status_reg, reset_adapter;
2054 instance = (struct megasas_instance *)shost->hostdata;
2055 fusion = instance->ctrl_context;
2057 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2058 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2059 "returning FAILED.\n");
2060 retval = FAILED;
2061 goto out;
2064 mutex_lock(&instance->reset_mutex);
2065 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2066 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2067 instance->instancet->disable_intr(instance->reg_set);
2068 msleep(1000);
2070 /* First try waiting for commands to complete */
2071 if (megasas_wait_for_outstanding_fusion(instance)) {
2072 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2073 "adapter.\n");
2074 /* Now return commands back to the OS */
2075 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2076 cmd_fusion = fusion->cmd_list[i];
2077 if (cmd_fusion->scmd) {
2078 scsi_dma_unmap(cmd_fusion->scmd);
2079 cmd_fusion->scmd->result = (DID_RESET << 16);
2080 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2081 megasas_return_cmd_fusion(instance, cmd_fusion);
2082 atomic_dec(&instance->fw_outstanding);
2086 status_reg = instance->instancet->read_fw_status_reg(
2087 instance->reg_set);
2088 abs_state = status_reg & MFI_STATE_MASK;
2089 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2090 if (instance->disableOnlineCtrlReset ||
2091 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2092 /* Reset not supported, kill adapter */
2093 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2094 ", killing adapter.\n");
2095 megaraid_sas_kill_hba(instance);
2096 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2097 retval = FAILED;
2098 goto out;
2101 /* Now try to reset the chip */
2102 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2103 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2104 &instance->reg_set->fusion_seq_offset);
2105 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2106 &instance->reg_set->fusion_seq_offset);
2107 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2108 &instance->reg_set->fusion_seq_offset);
2109 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2110 &instance->reg_set->fusion_seq_offset);
2111 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2112 &instance->reg_set->fusion_seq_offset);
2113 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2114 &instance->reg_set->fusion_seq_offset);
2115 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2116 &instance->reg_set->fusion_seq_offset);
2118 /* Check that the diag write enable (DRWE) bit is on */
2119 host_diag = readl(&instance->reg_set->fusion_host_diag);
2120 retry = 0;
2121 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2122 msleep(100);
2123 host_diag =
2124 readl(&instance->reg_set->fusion_host_diag);
2125 if (retry++ == 100) {
2126 printk(KERN_WARNING "megaraid_sas: "
2127 "Host diag unlock failed!\n");
2128 break;
2131 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2132 continue;
2134 /* Send chip reset command */
2135 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2136 &instance->reg_set->fusion_host_diag);
2137 msleep(3000);
2139 /* Make sure reset adapter bit is cleared */
2140 host_diag = readl(&instance->reg_set->fusion_host_diag);
2141 retry = 0;
2142 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2143 msleep(100);
2144 host_diag =
2145 readl(&instance->reg_set->fusion_host_diag);
2146 if (retry++ == 1000) {
2147 printk(KERN_WARNING "megaraid_sas: "
2148 "Diag reset adapter never "
2149 "cleared!\n");
2150 break;
2153 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2154 continue;
2156 abs_state =
2157 instance->instancet->read_fw_status_reg(
2158 instance->reg_set) & MFI_STATE_MASK;
2159 retry = 0;
2161 while ((abs_state <= MFI_STATE_FW_INIT) &&
2162 (retry++ < 1000)) {
2163 msleep(100);
2164 abs_state =
2165 instance->instancet->read_fw_status_reg(
2166 instance->reg_set) & MFI_STATE_MASK;
2168 if (abs_state <= MFI_STATE_FW_INIT) {
2169 printk(KERN_WARNING "megaraid_sas: firmware "
2170 "state < MFI_STATE_FW_INIT, state = "
2171 "0x%x\n", abs_state);
2172 continue;
2175 /* Wait for FW to become ready */
2176 if (megasas_transition_to_ready(instance)) {
2177 printk(KERN_WARNING "megaraid_sas: Failed to "
2178 "transition controller to ready.\n");
2179 continue;
2182 megasas_reset_reply_desc(instance);
2183 if (megasas_ioc_init_fusion(instance)) {
2184 printk(KERN_WARNING "megaraid_sas: "
2185 "megasas_ioc_init_fusion() failed!\n");
2186 continue;
2189 instance->instancet->enable_intr(instance->reg_set);
2190 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2192 /* Re-fire management commands */
2193 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2194 cmd_fusion = fusion->cmd_list[j];
2195 if (cmd_fusion->sync_cmd_idx !=
2196 (u32)ULONG_MAX) {
2197 cmd_mfi =
2198 instance->
2199 cmd_list[cmd_fusion->sync_cmd_idx];
2200 if (cmd_mfi->frame->dcmd.opcode ==
2201 MR_DCMD_LD_MAP_GET_INFO) {
2202 megasas_return_cmd(instance,
2203 cmd_mfi);
2204 megasas_return_cmd_fusion(
2205 instance, cmd_fusion);
2206 } else {
2207 req_desc =
2208 megasas_get_request_descriptor(
2209 instance,
2210 cmd_mfi->context.smid
2211 -1);
2212 if (!req_desc)
2213 printk(KERN_WARNING
2214 "req_desc NULL"
2215 "\n");
2216 else {
2217 instance->instancet->
2218 fire_cmd(instance,
2219 req_desc->
2220 u.low,
2221 req_desc->
2222 u.high,
2223 instance->
2224 reg_set);
2230 /* Reset load balance info */
2231 memset(fusion->load_balance_info, 0,
2232 sizeof(struct LD_LOAD_BALANCE_INFO)
2233 *MAX_LOGICAL_DRIVES);
2235 if (!megasas_get_map_info(instance))
2236 megasas_sync_map_info(instance);
2238 /* Adapter reset completed successfully */
2239 printk(KERN_WARNING "megaraid_sas: Reset "
2240 "successful.\n");
2241 retval = SUCCESS;
2242 goto out;
2244 /* Reset failed, kill the adapter */
2245 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2246 "adapter.\n");
2247 megaraid_sas_kill_hba(instance);
2248 retval = FAILED;
2249 } else {
2250 instance->instancet->enable_intr(instance->reg_set);
2251 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2253 out:
2254 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2255 mutex_unlock(&instance->reset_mutex);
2256 return retval;
2259 /* Fusion OCR work queue */
2260 void megasas_fusion_ocr_wq(struct work_struct *work)
2262 struct megasas_instance *instance =
2263 container_of(work, struct megasas_instance, work_init);
2265 megasas_reset_fusion(instance->host);
2268 struct megasas_instance_template megasas_instance_template_fusion = {
2269 .fire_cmd = megasas_fire_cmd_fusion,
2270 .enable_intr = megasas_enable_intr_fusion,
2271 .disable_intr = megasas_disable_intr_fusion,
2272 .clear_intr = megasas_clear_intr_fusion,
2273 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2274 .adp_reset = megasas_adp_reset_fusion,
2275 .check_reset = megasas_check_reset_fusion,
2276 .service_isr = megasas_isr_fusion,
2277 .tasklet = megasas_complete_cmd_dpc_fusion,
2278 .init_adapter = megasas_init_adapter_fusion,
2279 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2280 .issue_dcmd = megasas_issue_dcmd_fusion,