nilfs2: unbreak compat ioctl
[zen-stable.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
blobbfd87fab39aa7ff9949d2fe423c24f91fddc273e
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 megasas_instance *instance,
78 struct IO_REQUEST_INFO *io_info,
79 struct RAID_CONTEXT *pRAID_Context,
80 struct MR_FW_RAID_MAP_ALL *map);
81 u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
82 struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
84 u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
86 void
87 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
89 u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
90 struct LD_LOAD_BALANCE_INFO *lbInfo);
91 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
92 struct IO_REQUEST_INFO *in_info);
93 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94 void megaraid_sas_kill_hba(struct megasas_instance *instance);
96 extern u32 megasas_dbg_lvl;
98 /**
99 * megasas_enable_intr_fusion - Enables interrupts
100 * @regs: MFI register set
102 void
103 megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
105 /* For Thunderbolt/Invader also clear intr on enable */
106 writel(~0, &regs->outbound_intr_status);
107 readl(&regs->outbound_intr_status);
109 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
111 /* Dummy readl to force pci flush */
112 readl(&regs->outbound_intr_mask);
116 * megasas_disable_intr_fusion - Disables interrupt
117 * @regs: MFI register set
119 void
120 megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
122 u32 mask = 0xFFFFFFFF;
123 u32 status;
125 writel(mask, &regs->outbound_intr_mask);
126 /* Dummy readl to force pci flush */
127 status = readl(&regs->outbound_intr_mask);
131 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
133 u32 status;
135 * Check if it is our interrupt
137 status = readl(&regs->outbound_intr_status);
139 if (status & 1) {
140 writel(status, &regs->outbound_intr_status);
141 readl(&regs->outbound_intr_status);
142 return 1;
144 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
145 return 0;
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, count;
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 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
413 fusion->reply_frames_desc_pool =
414 pci_pool_create("reply_frames pool", instance->pdev,
415 fusion->reply_alloc_sz * count, 16, 0);
417 if (!fusion->reply_frames_desc_pool) {
418 printk(KERN_ERR "megasas; Could not allocate memory for "
419 "reply_frame pool\n");
420 goto fail_reply_desc;
423 fusion->reply_frames_desc =
424 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
425 &fusion->reply_frames_desc_phys);
426 if (!fusion->reply_frames_desc) {
427 printk(KERN_ERR "megasas; Could not allocate memory for "
428 "reply_frame pool\n");
429 pci_pool_destroy(fusion->reply_frames_desc_pool);
430 goto fail_reply_desc;
433 reply_desc = fusion->reply_frames_desc;
434 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
435 reply_desc->Words = ULLONG_MAX;
437 io_frames_sz = fusion->io_frames_alloc_sz;
439 fusion->io_request_frames_pool =
440 pci_pool_create("io_request_frames pool", instance->pdev,
441 fusion->io_frames_alloc_sz, 16, 0);
443 if (!fusion->io_request_frames_pool) {
444 printk(KERN_ERR "megasas: Could not allocate memory for "
445 "io_request_frame pool\n");
446 goto fail_io_frames;
449 fusion->io_request_frames =
450 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
451 &fusion->io_request_frames_phys);
452 if (!fusion->io_request_frames) {
453 printk(KERN_ERR "megasas: Could not allocate memory for "
454 "io_request_frames frames\n");
455 pci_pool_destroy(fusion->io_request_frames_pool);
456 goto fail_io_frames;
460 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
461 * Allocate the dynamic array first and then allocate individual
462 * commands.
464 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
465 *max_cmd, GFP_KERNEL);
467 if (!fusion->cmd_list) {
468 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
469 "memory for cmd_list_fusion\n");
470 goto fail_cmd_list;
473 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
474 *max_cmd);
476 max_cmd = instance->max_fw_cmds;
477 for (i = 0; i < max_cmd; i++) {
478 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
479 GFP_KERNEL);
480 if (!fusion->cmd_list[i]) {
481 printk(KERN_ERR "Could not alloc cmd list fusion\n");
483 for (j = 0; j < i; j++)
484 kfree(fusion->cmd_list[j]);
486 kfree(fusion->cmd_list);
487 fusion->cmd_list = NULL;
488 goto fail_cmd_list;
492 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
493 io_req_base = fusion->io_request_frames +
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
495 io_req_base_phys = fusion->io_request_frames_phys +
496 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
499 * Add all the commands to command pool (fusion->cmd_pool)
502 /* SMID 0 is reserved. Set SMID/index from 1 */
503 for (i = 0; i < max_cmd; i++) {
504 cmd = fusion->cmd_list[i];
505 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
506 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
507 cmd->index = i + 1;
508 cmd->scmd = NULL;
509 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
510 cmd->instance = instance;
511 cmd->io_request =
512 (struct MPI2_RAID_SCSI_IO_REQUEST *)
513 (io_req_base + offset);
514 memset(cmd->io_request, 0,
515 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
516 cmd->io_request_phys_addr = io_req_base_phys + offset;
518 list_add_tail(&cmd->list, &fusion->cmd_pool);
522 * Create a frame pool and assign one frame to each cmd
524 if (megasas_create_frame_pool_fusion(instance)) {
525 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
526 megasas_free_cmds_fusion(instance);
527 goto fail_req_desc;
530 return 0;
532 fail_cmd_list:
533 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
534 fusion->io_request_frames_phys);
535 pci_pool_destroy(fusion->io_request_frames_pool);
536 fail_io_frames:
537 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
538 fusion->reply_frames_desc,
539 fusion->reply_frames_desc_phys);
540 pci_pool_free(fusion->reply_frames_desc_pool,
541 fusion->reply_frames_desc,
542 fusion->reply_frames_desc_phys);
543 pci_pool_destroy(fusion->reply_frames_desc_pool);
545 fail_reply_desc:
546 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
547 fusion->req_frames_desc,
548 fusion->req_frames_desc_phys);
549 fail_req_desc:
550 return -ENOMEM;
554 * wait_and_poll - Issues a polling command
555 * @instance: Adapter soft state
556 * @cmd: Command packet to be issued
558 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
561 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
563 int i;
564 struct megasas_header *frame_hdr = &cmd->frame->hdr;
566 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
569 * Wait for cmd_status to change
571 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
572 rmb();
573 msleep(20);
576 if (frame_hdr->cmd_status == 0xff)
577 return -ETIME;
579 return 0;
583 * megasas_ioc_init_fusion - Initializes the FW
584 * @instance: Adapter soft state
586 * Issues the IOC Init cmd
589 megasas_ioc_init_fusion(struct megasas_instance *instance)
591 struct megasas_init_frame *init_frame;
592 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
593 dma_addr_t ioc_init_handle;
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;
637 /* Set to 0 for none or 1 MSI-X vectors */
638 IOCInitMessage->HostMSIxVectors = (instance->msix_vectors > 0 ?
639 instance->msix_vectors : 0);
640 init_frame = (struct megasas_init_frame *)cmd->frame;
641 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
643 frame_hdr = &cmd->frame->hdr;
644 frame_hdr->cmd_status = 0xFF;
645 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
647 init_frame->cmd = MFI_CMD_INIT;
648 init_frame->cmd_status = 0xFF;
650 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
651 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
653 req_desc =
654 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
656 req_desc->Words = cmd->frame_phys_addr;
657 req_desc->MFAIo.RequestFlags =
658 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
659 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
662 * disable the intr before firing the init frame
664 instance->instancet->disable_intr(instance->reg_set);
666 for (i = 0; i < (10 * 1000); i += 20) {
667 if (readl(&instance->reg_set->doorbell) & 1)
668 msleep(20);
669 else
670 break;
673 instance->instancet->fire_cmd(instance, req_desc->u.low,
674 req_desc->u.high, instance->reg_set);
676 wait_and_poll(instance, cmd);
678 frame_hdr = &cmd->frame->hdr;
679 if (frame_hdr->cmd_status != 0) {
680 ret = 1;
681 goto fail_fw_init;
683 printk(KERN_ERR "megasas:IOC Init cmd success\n");
685 ret = 0;
687 fail_fw_init:
688 megasas_return_cmd(instance, cmd);
689 if (IOCInitMessage)
690 dma_free_coherent(&instance->pdev->dev,
691 sizeof(struct MPI2_IOC_INIT_REQUEST),
692 IOCInitMessage, ioc_init_handle);
693 fail_get_cmd:
694 return ret;
698 * megasas_get_ld_map_info - Returns FW's ld_map structure
699 * @instance: Adapter soft state
700 * @pend: Pend the command or not
701 * Issues an internal command (DCMD) to get the FW's controller PD
702 * list structure. This information is mainly used to find out SYSTEM
703 * supported by the FW.
705 static int
706 megasas_get_ld_map_info(struct megasas_instance *instance)
708 int ret = 0;
709 struct megasas_cmd *cmd;
710 struct megasas_dcmd_frame *dcmd;
711 struct MR_FW_RAID_MAP_ALL *ci;
712 dma_addr_t ci_h = 0;
713 u32 size_map_info;
714 struct fusion_context *fusion;
716 cmd = megasas_get_cmd(instance);
718 if (!cmd) {
719 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
720 return -ENOMEM;
723 fusion = instance->ctrl_context;
725 if (!fusion) {
726 megasas_return_cmd(instance, cmd);
727 return 1;
730 dcmd = &cmd->frame->dcmd;
732 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
733 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
735 ci = fusion->ld_map[(instance->map_id & 1)];
736 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
738 if (!ci) {
739 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
740 megasas_return_cmd(instance, cmd);
741 return -ENOMEM;
744 memset(ci, 0, sizeof(*ci));
745 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
747 dcmd->cmd = MFI_CMD_DCMD;
748 dcmd->cmd_status = 0xFF;
749 dcmd->sge_count = 1;
750 dcmd->flags = MFI_FRAME_DIR_READ;
751 dcmd->timeout = 0;
752 dcmd->pad_0 = 0;
753 dcmd->data_xfer_len = size_map_info;
754 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
755 dcmd->sgl.sge32[0].phys_addr = ci_h;
756 dcmd->sgl.sge32[0].length = size_map_info;
758 if (!megasas_issue_polled(instance, cmd))
759 ret = 0;
760 else {
761 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
762 ret = -1;
765 megasas_return_cmd(instance, cmd);
767 return ret;
771 megasas_get_map_info(struct megasas_instance *instance)
773 struct fusion_context *fusion = instance->ctrl_context;
775 fusion->fast_path_io = 0;
776 if (!megasas_get_ld_map_info(instance)) {
777 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
778 fusion->load_balance_info)) {
779 fusion->fast_path_io = 1;
780 return 0;
783 return 1;
787 * megasas_sync_map_info - Returns FW's ld_map structure
788 * @instance: Adapter soft state
790 * Issues an internal command (DCMD) to get the FW's controller PD
791 * list structure. This information is mainly used to find out SYSTEM
792 * supported by the FW.
795 megasas_sync_map_info(struct megasas_instance *instance)
797 int ret = 0, i;
798 struct megasas_cmd *cmd;
799 struct megasas_dcmd_frame *dcmd;
800 u32 size_sync_info, num_lds;
801 struct fusion_context *fusion;
802 struct MR_LD_TARGET_SYNC *ci = NULL;
803 struct MR_FW_RAID_MAP_ALL *map;
804 struct MR_LD_RAID *raid;
805 struct MR_LD_TARGET_SYNC *ld_sync;
806 dma_addr_t ci_h = 0;
807 u32 size_map_info;
809 cmd = megasas_get_cmd(instance);
811 if (!cmd) {
812 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
813 "info.\n");
814 return -ENOMEM;
817 fusion = instance->ctrl_context;
819 if (!fusion) {
820 megasas_return_cmd(instance, cmd);
821 return 1;
824 map = fusion->ld_map[instance->map_id & 1];
826 num_lds = map->raidMap.ldCount;
828 dcmd = &cmd->frame->dcmd;
830 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
832 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
834 ci = (struct MR_LD_TARGET_SYNC *)
835 fusion->ld_map[(instance->map_id - 1) & 1];
836 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
838 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
840 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
842 for (i = 0; i < num_lds; i++, ld_sync++) {
843 raid = MR_LdRaidGet(i, map);
844 ld_sync->targetId = MR_GetLDTgtId(i, map);
845 ld_sync->seqNum = raid->seqNum;
848 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
849 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
851 dcmd->cmd = MFI_CMD_DCMD;
852 dcmd->cmd_status = 0xFF;
853 dcmd->sge_count = 1;
854 dcmd->flags = MFI_FRAME_DIR_WRITE;
855 dcmd->timeout = 0;
856 dcmd->pad_0 = 0;
857 dcmd->data_xfer_len = size_map_info;
858 dcmd->mbox.b[0] = num_lds;
859 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
860 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
861 dcmd->sgl.sge32[0].phys_addr = ci_h;
862 dcmd->sgl.sge32[0].length = size_map_info;
864 instance->map_update_cmd = cmd;
866 instance->instancet->issue_dcmd(instance, cmd);
868 return ret;
872 * megasas_init_adapter_fusion - Initializes the FW
873 * @instance: Adapter soft state
875 * This is the main function for initializing firmware.
878 megasas_init_adapter_fusion(struct megasas_instance *instance)
880 struct megasas_register_set __iomem *reg_set;
881 struct fusion_context *fusion;
882 u32 max_cmd;
883 int i = 0, count;
885 fusion = instance->ctrl_context;
887 reg_set = instance->reg_set;
890 * Get various operational parameters from status register
892 instance->max_fw_cmds =
893 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
894 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
897 * Reduce the max supported cmds by 1. This is to ensure that the
898 * reply_q_sz (1 more than the max cmd that driver may send)
899 * does not exceed max cmds that the FW can support
901 instance->max_fw_cmds = instance->max_fw_cmds-1;
902 /* Only internal cmds (DCMD) need to have MFI frames */
903 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
905 max_cmd = instance->max_fw_cmds;
907 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
909 fusion->request_alloc_sz =
910 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
911 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
912 *(fusion->reply_q_depth);
913 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
914 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
915 (max_cmd + 1)); /* Extra 1 for SMID 0 */
917 fusion->max_sge_in_main_msg =
918 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
919 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
921 fusion->max_sge_in_chain =
922 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
924 instance->max_num_sge = fusion->max_sge_in_main_msg +
925 fusion->max_sge_in_chain - 2;
927 /* Used for pass thru MFI frame (DCMD) */
928 fusion->chain_offset_mfi_pthru =
929 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
931 fusion->chain_offset_io_request =
932 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
933 sizeof(union MPI2_SGE_IO_UNION))/16;
935 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
936 for (i = 0 ; i < count; i++)
937 fusion->last_reply_idx[i] = 0;
940 * Allocate memory for descriptors
941 * Create a pool of commands
943 if (megasas_alloc_cmds(instance))
944 goto fail_alloc_mfi_cmds;
945 if (megasas_alloc_cmds_fusion(instance))
946 goto fail_alloc_cmds;
948 if (megasas_ioc_init_fusion(instance))
949 goto fail_ioc_init;
951 instance->flag_ieee = 1;
953 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
954 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
956 fusion->fast_path_io = 0;
958 for (i = 0; i < 2; i++) {
959 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
960 fusion->map_sz,
961 &fusion->ld_map_phys[i],
962 GFP_KERNEL);
963 if (!fusion->ld_map[i]) {
964 printk(KERN_ERR "megasas: Could not allocate memory "
965 "for map info\n");
966 goto fail_map_info;
970 if (!megasas_get_map_info(instance))
971 megasas_sync_map_info(instance);
973 return 0;
975 fail_map_info:
976 if (i == 1)
977 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
978 fusion->ld_map[0], fusion->ld_map_phys[0]);
979 fail_ioc_init:
980 megasas_free_cmds_fusion(instance);
981 fail_alloc_cmds:
982 megasas_free_cmds(instance);
983 fail_alloc_mfi_cmds:
984 return 1;
988 * megasas_fire_cmd_fusion - Sends command to the FW
989 * @frame_phys_addr : Physical address of cmd
990 * @frame_count : Number of frames for the command
991 * @regs : MFI register set
993 void
994 megasas_fire_cmd_fusion(struct megasas_instance *instance,
995 dma_addr_t req_desc_lo,
996 u32 req_desc_hi,
997 struct megasas_register_set __iomem *regs)
999 unsigned long flags;
1001 spin_lock_irqsave(&instance->hba_lock, flags);
1003 writel(req_desc_lo,
1004 &(regs)->inbound_low_queue_port);
1005 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1006 spin_unlock_irqrestore(&instance->hba_lock, flags);
1010 * map_cmd_status - Maps FW cmd status to OS cmd status
1011 * @cmd : Pointer to cmd
1012 * @status : status of cmd returned by FW
1013 * @ext_status : ext status of cmd returned by FW
1016 void
1017 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1020 switch (status) {
1022 case MFI_STAT_OK:
1023 cmd->scmd->result = DID_OK << 16;
1024 break;
1026 case MFI_STAT_SCSI_IO_FAILED:
1027 case MFI_STAT_LD_INIT_IN_PROGRESS:
1028 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1029 break;
1031 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1033 cmd->scmd->result = (DID_OK << 16) | ext_status;
1034 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1035 memset(cmd->scmd->sense_buffer, 0,
1036 SCSI_SENSE_BUFFERSIZE);
1037 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1038 SCSI_SENSE_BUFFERSIZE);
1039 cmd->scmd->result |= DRIVER_SENSE << 24;
1041 break;
1043 case MFI_STAT_LD_OFFLINE:
1044 case MFI_STAT_DEVICE_NOT_FOUND:
1045 cmd->scmd->result = DID_BAD_TARGET << 16;
1046 break;
1047 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1048 cmd->scmd->result = DID_IMM_RETRY << 16;
1049 break;
1050 default:
1051 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1052 cmd->scmd->result = DID_ERROR << 16;
1053 break;
1058 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1059 * @instance: Adapter soft state
1060 * @scp: SCSI command from the mid-layer
1061 * @sgl_ptr: SGL to be filled in
1062 * @cmd: cmd we are working on
1064 * If successful, this function returns the number of SG elements.
1066 static int
1067 megasas_make_sgl_fusion(struct megasas_instance *instance,
1068 struct scsi_cmnd *scp,
1069 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1070 struct megasas_cmd_fusion *cmd)
1072 int i, sg_processed, sge_count;
1073 struct scatterlist *os_sgl;
1074 struct fusion_context *fusion;
1076 fusion = instance->ctrl_context;
1078 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1079 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1080 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1081 sgl_ptr_end->Flags = 0;
1084 sge_count = scsi_dma_map(scp);
1086 BUG_ON(sge_count < 0);
1088 if (sge_count > instance->max_num_sge || !sge_count)
1089 return 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 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1096 if (i == sge_count - 1)
1097 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1099 sgl_ptr++;
1101 sg_processed = i + 1;
1103 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1104 (sge_count > fusion->max_sge_in_main_msg)) {
1106 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1107 if (instance->pdev->device ==
1108 PCI_DEVICE_ID_LSI_INVADER) {
1109 if ((cmd->io_request->IoFlags &
1110 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1111 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1112 cmd->io_request->ChainOffset =
1113 fusion->
1114 chain_offset_io_request;
1115 else
1116 cmd->io_request->ChainOffset = 0;
1117 } else
1118 cmd->io_request->ChainOffset =
1119 fusion->chain_offset_io_request;
1121 sg_chain = sgl_ptr;
1122 /* Prepare chain element */
1123 sg_chain->NextChainOffset = 0;
1124 if (instance->pdev->device ==
1125 PCI_DEVICE_ID_LSI_INVADER)
1126 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1127 else
1128 sg_chain->Flags =
1129 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1130 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1131 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1132 *(sge_count - sg_processed));
1133 sg_chain->Address = cmd->sg_frame_phys_addr;
1135 sgl_ptr =
1136 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1140 return sge_count;
1144 * megasas_set_pd_lba - Sets PD LBA
1145 * @cdb: CDB
1146 * @cdb_len: cdb length
1147 * @start_blk: Start block of IO
1149 * Used to set the PD LBA in CDB for FP IOs
1151 void
1152 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1153 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1154 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1156 struct MR_LD_RAID *raid;
1157 u32 ld;
1158 u64 start_blk = io_info->pdBlock;
1159 u8 *cdb = io_request->CDB.CDB32;
1160 u32 num_blocks = io_info->numBlocks;
1161 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1163 /* Check if T10 PI (DIF) is enabled for this LD */
1164 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1165 raid = MR_LdRaidGet(ld, local_map_ptr);
1166 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1167 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1168 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1169 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1171 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1172 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1173 else
1174 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1175 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1177 /* LBA */
1178 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1179 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1180 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1181 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1182 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1183 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1184 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1185 cdb[19] = (u8)(start_blk & 0xff);
1187 /* Logical block reference tag */
1188 io_request->CDB.EEDP32.PrimaryReferenceTag =
1189 cpu_to_be32(ref_tag);
1190 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1192 io_request->DataLength = num_blocks * 512;
1193 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1195 /* Transfer length */
1196 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1197 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1198 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1199 cdb[31] = (u8)(num_blocks & 0xff);
1201 /* set SCSI IO EEDPFlags */
1202 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1203 io_request->EEDPFlags =
1204 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1205 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1206 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1207 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1208 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1209 } else {
1210 io_request->EEDPFlags =
1211 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1212 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1214 io_request->Control |= (0x4 << 26);
1215 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1216 } else {
1217 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1218 if (((cdb_len == 12) || (cdb_len == 16)) &&
1219 (start_blk <= 0xffffffff)) {
1220 if (cdb_len == 16) {
1221 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1222 flagvals = cdb[1];
1223 groupnum = cdb[14];
1224 control = cdb[15];
1225 } else {
1226 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1227 flagvals = cdb[1];
1228 groupnum = cdb[10];
1229 control = cdb[11];
1232 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1234 cdb[0] = opcode;
1235 cdb[1] = flagvals;
1236 cdb[6] = groupnum;
1237 cdb[9] = control;
1239 /* Transfer length */
1240 cdb[8] = (u8)(num_blocks & 0xff);
1241 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1243 io_request->IoFlags = 10; /* Specify 10-byte cdb */
1244 cdb_len = 10;
1245 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1246 /* Convert to 16 byte CDB for large LBA's */
1247 switch (cdb_len) {
1248 case 6:
1249 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1250 control = cdb[5];
1251 break;
1252 case 10:
1253 opcode =
1254 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1255 flagvals = cdb[1];
1256 groupnum = cdb[6];
1257 control = cdb[9];
1258 break;
1259 case 12:
1260 opcode =
1261 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1262 flagvals = cdb[1];
1263 groupnum = cdb[10];
1264 control = cdb[11];
1265 break;
1268 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1270 cdb[0] = opcode;
1271 cdb[1] = flagvals;
1272 cdb[14] = groupnum;
1273 cdb[15] = control;
1275 /* Transfer length */
1276 cdb[13] = (u8)(num_blocks & 0xff);
1277 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1278 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1279 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1281 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1282 cdb_len = 16;
1285 /* Normal case, just load LBA here */
1286 switch (cdb_len) {
1287 case 6:
1289 u8 val = cdb[1] & 0xE0;
1290 cdb[3] = (u8)(start_blk & 0xff);
1291 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1292 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1293 break;
1295 case 10:
1296 cdb[5] = (u8)(start_blk & 0xff);
1297 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1298 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1299 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1300 break;
1301 case 12:
1302 cdb[5] = (u8)(start_blk & 0xff);
1303 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1304 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1305 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1306 break;
1307 case 16:
1308 cdb[9] = (u8)(start_blk & 0xff);
1309 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1310 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1311 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1312 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1313 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1314 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1315 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1316 break;
1322 * megasas_build_ldio_fusion - Prepares IOs to devices
1323 * @instance: Adapter soft state
1324 * @scp: SCSI command
1325 * @cmd: Command to be prepared
1327 * Prepares the io_request and chain elements (sg_frame) for IO
1328 * The IO can be for PD (Fast Path) or LD
1330 void
1331 megasas_build_ldio_fusion(struct megasas_instance *instance,
1332 struct scsi_cmnd *scp,
1333 struct megasas_cmd_fusion *cmd)
1335 u8 fp_possible;
1336 u32 start_lba_lo, start_lba_hi, device_id;
1337 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1338 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1339 struct IO_REQUEST_INFO io_info;
1340 struct fusion_context *fusion;
1341 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1343 device_id = MEGASAS_DEV_INDEX(instance, scp);
1345 fusion = instance->ctrl_context;
1347 io_request = cmd->io_request;
1348 io_request->RaidContext.VirtualDiskTgtId = device_id;
1349 io_request->RaidContext.status = 0;
1350 io_request->RaidContext.exStatus = 0;
1352 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1354 start_lba_lo = 0;
1355 start_lba_hi = 0;
1356 fp_possible = 0;
1359 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1361 if (scp->cmd_len == 6) {
1362 io_request->DataLength = (u32) scp->cmnd[4];
1363 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1364 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1366 start_lba_lo &= 0x1FFFFF;
1370 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1372 else if (scp->cmd_len == 10) {
1373 io_request->DataLength = (u32) scp->cmnd[8] |
1374 ((u32) scp->cmnd[7] << 8);
1375 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1376 ((u32) scp->cmnd[3] << 16) |
1377 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1381 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1383 else if (scp->cmd_len == 12) {
1384 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1385 ((u32) scp->cmnd[7] << 16) |
1386 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1387 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1388 ((u32) scp->cmnd[3] << 16) |
1389 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1393 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1395 else if (scp->cmd_len == 16) {
1396 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1397 ((u32) scp->cmnd[11] << 16) |
1398 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1399 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1400 ((u32) scp->cmnd[7] << 16) |
1401 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1403 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1404 ((u32) scp->cmnd[3] << 16) |
1405 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1408 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1409 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1410 io_info.numBlocks = io_request->DataLength;
1411 io_info.ldTgtId = device_id;
1413 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1414 io_info.isRead = 1;
1416 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1418 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1419 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1420 io_request->RaidContext.regLockFlags = 0;
1421 fp_possible = 0;
1422 } else {
1423 if (MR_BuildRaidContext(instance, &io_info,
1424 &io_request->RaidContext,
1425 local_map_ptr))
1426 fp_possible = io_info.fpOkForIo;
1429 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1430 id by default, not CPU group id, otherwise all MSI-X queues won't
1431 be utilized */
1432 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1433 smp_processor_id() % instance->msix_vectors : 0;
1435 if (fp_possible) {
1436 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1437 local_map_ptr, start_lba_lo);
1438 io_request->DataLength = scsi_bufflen(scp);
1439 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1440 cmd->request_desc->SCSIIO.RequestFlags =
1441 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1442 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1443 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1444 if (io_request->RaidContext.regLockFlags ==
1445 REGION_TYPE_UNUSED)
1446 cmd->request_desc->SCSIIO.RequestFlags =
1447 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1448 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1449 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1450 io_request->RaidContext.nseg = 0x1;
1451 io_request->IoFlags |=
1452 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1453 io_request->RaidContext.regLockFlags |=
1454 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1455 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1457 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1458 (io_info.isRead)) {
1459 io_info.devHandle =
1460 get_updated_dev_handle(
1461 &fusion->load_balance_info[device_id],
1462 &io_info);
1463 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1464 } else
1465 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1466 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1467 io_request->DevHandle = io_info.devHandle;
1468 } else {
1469 io_request->RaidContext.timeoutValue =
1470 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1471 cmd->request_desc->SCSIIO.RequestFlags =
1472 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1473 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1474 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1475 if (io_request->RaidContext.regLockFlags ==
1476 REGION_TYPE_UNUSED)
1477 cmd->request_desc->SCSIIO.RequestFlags =
1478 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1479 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1480 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1481 io_request->RaidContext.regLockFlags |=
1482 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1483 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1484 io_request->RaidContext.nseg = 0x1;
1486 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1487 io_request->DevHandle = device_id;
1488 } /* Not FP */
1492 * megasas_build_dcdb_fusion - Prepares IOs to devices
1493 * @instance: Adapter soft state
1494 * @scp: SCSI command
1495 * @cmd: Command to be prepared
1497 * Prepares the io_request frame for non-io cmds
1499 static void
1500 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1501 struct scsi_cmnd *scmd,
1502 struct megasas_cmd_fusion *cmd)
1504 u32 device_id;
1505 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1506 u16 pd_index = 0;
1507 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1508 struct fusion_context *fusion = instance->ctrl_context;
1510 io_request = cmd->io_request;
1511 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1512 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1513 +scmd->device->id;
1514 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1516 /* Check if this is a system PD I/O */
1517 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1518 io_request->Function = 0;
1519 io_request->DevHandle =
1520 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1521 io_request->RaidContext.timeoutValue =
1522 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1523 io_request->RaidContext.regLockFlags = 0;
1524 io_request->RaidContext.regLockRowLBA = 0;
1525 io_request->RaidContext.regLockLength = 0;
1526 io_request->RaidContext.RAIDFlags =
1527 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1528 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1529 cmd->request_desc->SCSIIO.RequestFlags =
1530 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1531 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1532 } else {
1533 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1534 io_request->DevHandle = device_id;
1535 cmd->request_desc->SCSIIO.RequestFlags =
1536 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1537 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1539 io_request->RaidContext.VirtualDiskTgtId = device_id;
1540 io_request->LUN[1] = scmd->device->lun;
1541 io_request->DataLength = scsi_bufflen(scmd);
1545 * megasas_build_io_fusion - Prepares IOs to devices
1546 * @instance: Adapter soft state
1547 * @scp: SCSI command
1548 * @cmd: Command to be prepared
1550 * Invokes helper functions to prepare request frames
1551 * and sets flags appropriate for IO/Non-IO cmd
1554 megasas_build_io_fusion(struct megasas_instance *instance,
1555 struct scsi_cmnd *scp,
1556 struct megasas_cmd_fusion *cmd)
1558 u32 device_id, sge_count;
1559 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1561 device_id = MEGASAS_DEV_INDEX(instance, scp);
1563 /* Zero out some fields so they don't get reused */
1564 io_request->LUN[1] = 0;
1565 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1566 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1567 io_request->EEDPFlags = 0;
1568 io_request->Control = 0;
1569 io_request->EEDPBlockSize = 0;
1570 io_request->ChainOffset = 0;
1571 io_request->RaidContext.RAIDFlags = 0;
1572 io_request->RaidContext.Type = 0;
1573 io_request->RaidContext.nseg = 0;
1575 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1577 * Just the CDB length,rest of the Flags are zero
1578 * This will be modified for FP in build_ldio_fusion
1580 io_request->IoFlags = scp->cmd_len;
1582 if (megasas_is_ldio(scp))
1583 megasas_build_ldio_fusion(instance, scp, cmd);
1584 else
1585 megasas_build_dcdb_fusion(instance, scp, cmd);
1588 * Construct SGL
1591 sge_count =
1592 megasas_make_sgl_fusion(instance, scp,
1593 (struct MPI25_IEEE_SGE_CHAIN64 *)
1594 &io_request->SGL, cmd);
1596 if (sge_count > instance->max_num_sge) {
1597 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1598 "max (0x%x) allowed\n", sge_count,
1599 instance->max_num_sge);
1600 return 1;
1603 io_request->RaidContext.numSGE = sge_count;
1605 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1607 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1608 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1609 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1610 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1612 io_request->SGLOffset0 =
1613 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1615 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1616 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1618 cmd->scmd = scp;
1619 scp->SCp.ptr = (char *)cmd;
1621 return 0;
1624 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1625 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1627 u8 *p;
1628 struct fusion_context *fusion;
1630 if (index >= instance->max_fw_cmds) {
1631 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1632 "descriptor\n", index);
1633 return NULL;
1635 fusion = instance->ctrl_context;
1636 p = fusion->req_frames_desc
1637 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1639 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1643 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1644 * issuing non IOCTL cmd
1645 * @instance: Adapter soft state
1646 * @scmd: pointer to scsi cmd from OS
1648 static u32
1649 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1650 struct scsi_cmnd *scmd)
1652 struct megasas_cmd_fusion *cmd;
1653 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1654 u32 index;
1655 struct fusion_context *fusion;
1657 fusion = instance->ctrl_context;
1659 cmd = megasas_get_cmd_fusion(instance);
1660 if (!cmd)
1661 return SCSI_MLQUEUE_HOST_BUSY;
1663 index = cmd->index;
1665 req_desc = megasas_get_request_descriptor(instance, index-1);
1666 if (!req_desc)
1667 return 1;
1669 req_desc->Words = 0;
1670 cmd->request_desc = req_desc;
1672 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1673 megasas_return_cmd_fusion(instance, cmd);
1674 printk(KERN_ERR "megasas: Error building command.\n");
1675 cmd->request_desc = NULL;
1676 return 1;
1679 req_desc = cmd->request_desc;
1680 req_desc->SCSIIO.SMID = index;
1682 if (cmd->io_request->ChainOffset != 0 &&
1683 cmd->io_request->ChainOffset != 0xF)
1684 printk(KERN_ERR "megasas: The chain offset value is not "
1685 "correct : %x\n", cmd->io_request->ChainOffset);
1688 * Issue the command to the FW
1690 atomic_inc(&instance->fw_outstanding);
1692 instance->instancet->fire_cmd(instance,
1693 req_desc->u.low, req_desc->u.high,
1694 instance->reg_set);
1696 return 0;
1700 * complete_cmd_fusion - Completes command
1701 * @instance: Adapter soft state
1702 * Completes all commands that is in reply descriptor queue
1705 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1707 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1708 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1709 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1710 struct fusion_context *fusion;
1711 struct megasas_cmd *cmd_mfi;
1712 struct megasas_cmd_fusion *cmd_fusion;
1713 u16 smid, num_completed;
1714 u8 reply_descript_type, arm;
1715 u32 status, extStatus, device_id;
1716 union desc_value d_val;
1717 struct LD_LOAD_BALANCE_INFO *lbinfo;
1719 fusion = instance->ctrl_context;
1721 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1722 return IRQ_HANDLED;
1724 desc = fusion->reply_frames_desc;
1725 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1726 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1727 fusion->last_reply_idx[MSIxIndex];
1729 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1731 d_val.word = desc->Words;
1733 reply_descript_type = reply_desc->ReplyFlags &
1734 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1736 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1737 return IRQ_NONE;
1739 d_val.word = desc->Words;
1741 num_completed = 0;
1743 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1744 smid = reply_desc->SMID;
1746 cmd_fusion = fusion->cmd_list[smid - 1];
1748 scsi_io_req =
1749 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1750 cmd_fusion->io_request;
1752 if (cmd_fusion->scmd)
1753 cmd_fusion->scmd->SCp.ptr = NULL;
1755 status = scsi_io_req->RaidContext.status;
1756 extStatus = scsi_io_req->RaidContext.exStatus;
1758 switch (scsi_io_req->Function) {
1759 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1760 /* Update load balancing info */
1761 device_id = MEGASAS_DEV_INDEX(instance,
1762 cmd_fusion->scmd);
1763 lbinfo = &fusion->load_balance_info[device_id];
1764 if (cmd_fusion->scmd->SCp.Status &
1765 MEGASAS_LOAD_BALANCE_FLAG) {
1766 arm = lbinfo->raid1DevHandle[0] ==
1767 cmd_fusion->io_request->DevHandle ? 0 :
1769 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1770 cmd_fusion->scmd->SCp.Status &=
1771 ~MEGASAS_LOAD_BALANCE_FLAG;
1773 if (reply_descript_type ==
1774 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1775 if (megasas_dbg_lvl == 5)
1776 printk(KERN_ERR "\nmegasas: FAST Path "
1777 "IO Success\n");
1779 /* Fall thru and complete IO */
1780 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1781 /* Map the FW Cmd Status */
1782 map_cmd_status(cmd_fusion, status, extStatus);
1783 scsi_dma_unmap(cmd_fusion->scmd);
1784 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1785 scsi_io_req->RaidContext.status = 0;
1786 scsi_io_req->RaidContext.exStatus = 0;
1787 megasas_return_cmd_fusion(instance, cmd_fusion);
1788 atomic_dec(&instance->fw_outstanding);
1790 break;
1791 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1792 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1793 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1794 cmd_fusion->flags = 0;
1795 megasas_return_cmd_fusion(instance, cmd_fusion);
1797 break;
1800 fusion->last_reply_idx[MSIxIndex]++;
1801 if (fusion->last_reply_idx[MSIxIndex] >=
1802 fusion->reply_q_depth)
1803 fusion->last_reply_idx[MSIxIndex] = 0;
1805 desc->Words = ULLONG_MAX;
1806 num_completed++;
1808 /* Get the next reply descriptor */
1809 if (!fusion->last_reply_idx[MSIxIndex])
1810 desc = fusion->reply_frames_desc +
1811 ((MSIxIndex * fusion->reply_alloc_sz)/
1812 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1813 else
1814 desc++;
1816 reply_desc =
1817 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1819 d_val.word = desc->Words;
1821 reply_descript_type = reply_desc->ReplyFlags &
1822 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1824 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1825 break;
1828 if (!num_completed)
1829 return IRQ_NONE;
1831 wmb();
1832 writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex],
1833 &instance->reg_set->reply_post_host_index);
1834 megasas_check_and_restore_queue_depth(instance);
1835 return IRQ_HANDLED;
1839 * megasas_complete_cmd_dpc_fusion - Completes command
1840 * @instance: Adapter soft state
1842 * Tasklet to complete cmds
1844 void
1845 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1847 struct megasas_instance *instance =
1848 (struct megasas_instance *)instance_addr;
1849 unsigned long flags;
1850 u32 count, MSIxIndex;
1852 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1854 /* If we have already declared adapter dead, donot complete cmds */
1855 spin_lock_irqsave(&instance->hba_lock, flags);
1856 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1857 spin_unlock_irqrestore(&instance->hba_lock, flags);
1858 return;
1860 spin_unlock_irqrestore(&instance->hba_lock, flags);
1862 spin_lock_irqsave(&instance->completion_lock, flags);
1863 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
1864 complete_cmd_fusion(instance, MSIxIndex);
1865 spin_unlock_irqrestore(&instance->completion_lock, flags);
1869 * megasas_isr_fusion - isr entry point
1871 irqreturn_t megasas_isr_fusion(int irq, void *devp)
1873 struct megasas_irq_context *irq_context = devp;
1874 struct megasas_instance *instance = irq_context->instance;
1875 u32 mfiStatus, fw_state;
1877 if (!instance->msix_vectors) {
1878 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1879 if (!mfiStatus)
1880 return IRQ_NONE;
1883 /* If we are resetting, bail */
1884 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1885 instance->instancet->clear_intr(instance->reg_set);
1886 return IRQ_HANDLED;
1889 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
1890 instance->instancet->clear_intr(instance->reg_set);
1891 /* If we didn't complete any commands, check for FW fault */
1892 fw_state = instance->instancet->read_fw_status_reg(
1893 instance->reg_set) & MFI_STATE_MASK;
1894 if (fw_state == MFI_STATE_FAULT)
1895 schedule_work(&instance->work_init);
1898 return IRQ_HANDLED;
1902 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1903 * @instance: Adapter soft state
1904 * mfi_cmd: megasas_cmd pointer
1908 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1909 struct megasas_cmd *mfi_cmd)
1911 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1912 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1913 struct megasas_cmd_fusion *cmd;
1914 struct fusion_context *fusion;
1915 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1917 cmd = megasas_get_cmd_fusion(instance);
1918 if (!cmd)
1919 return 1;
1921 /* Save the smid. To be used for returning the cmd */
1922 mfi_cmd->context.smid = cmd->index;
1924 cmd->sync_cmd_idx = mfi_cmd->index;
1927 * For cmds where the flag is set, store the flag and check
1928 * on completion. For cmds with this flag, don't call
1929 * megasas_complete_cmd
1932 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1933 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1935 fusion = instance->ctrl_context;
1936 io_req = cmd->io_request;
1938 if (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) {
1939 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
1940 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
1941 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1942 sgl_ptr_end->Flags = 0;
1945 mpi25_ieee_chain =
1946 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1948 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1949 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1950 SGL) / 4;
1951 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1953 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1955 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1956 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1958 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1960 return 0;
1964 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1965 * @instance: Adapter soft state
1966 * @cmd: mfi cmd to build
1969 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1970 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1972 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1973 u16 index;
1975 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1976 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1977 return NULL;
1980 index = cmd->context.smid;
1982 req_desc = megasas_get_request_descriptor(instance, index - 1);
1984 if (!req_desc)
1985 return NULL;
1987 req_desc->Words = 0;
1988 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1989 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1991 req_desc->SCSIIO.SMID = index;
1993 return req_desc;
1997 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1998 * @instance: Adapter soft state
1999 * @cmd: mfi cmd pointer
2002 void
2003 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2004 struct megasas_cmd *cmd)
2006 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2008 req_desc = build_mpt_cmd(instance, cmd);
2009 if (!req_desc) {
2010 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2011 return;
2013 instance->instancet->fire_cmd(instance, req_desc->u.low,
2014 req_desc->u.high, instance->reg_set);
2018 * megasas_release_fusion - Reverses the FW initialization
2019 * @intance: Adapter soft state
2021 void
2022 megasas_release_fusion(struct megasas_instance *instance)
2024 megasas_free_cmds(instance);
2025 megasas_free_cmds_fusion(instance);
2027 iounmap(instance->reg_set);
2029 pci_release_selected_regions(instance->pdev, instance->bar);
2033 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2034 * @regs: MFI register set
2036 static u32
2037 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2039 return readl(&(regs)->outbound_scratch_pad);
2043 * megasas_adp_reset_fusion - For controller reset
2044 * @regs: MFI register set
2046 static int
2047 megasas_adp_reset_fusion(struct megasas_instance *instance,
2048 struct megasas_register_set __iomem *regs)
2050 return 0;
2054 * megasas_check_reset_fusion - For controller reset check
2055 * @regs: MFI register set
2057 static int
2058 megasas_check_reset_fusion(struct megasas_instance *instance,
2059 struct megasas_register_set __iomem *regs)
2061 return 0;
2064 /* This function waits for outstanding commands on fusion to complete */
2065 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2067 int i, outstanding, retval = 0;
2068 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
2070 for (i = 0; i < wait_time; i++) {
2071 /* Check if firmware is in fault state */
2072 fw_state = instance->instancet->read_fw_status_reg(
2073 instance->reg_set) & MFI_STATE_MASK;
2074 if (fw_state == MFI_STATE_FAULT) {
2075 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2076 " will reset adapter.\n");
2077 retval = 1;
2078 goto out;
2081 outstanding = atomic_read(&instance->fw_outstanding);
2082 if (!outstanding)
2083 goto out;
2085 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2086 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2087 "commands to complete\n", i, outstanding);
2088 megasas_complete_cmd_dpc_fusion(
2089 (unsigned long)instance);
2091 msleep(1000);
2094 if (atomic_read(&instance->fw_outstanding)) {
2095 printk("megaraid_sas: pending commands remain after waiting, "
2096 "will reset adapter.\n");
2097 retval = 1;
2099 out:
2100 return retval;
2103 void megasas_reset_reply_desc(struct megasas_instance *instance)
2105 int i, count;
2106 struct fusion_context *fusion;
2107 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2109 fusion = instance->ctrl_context;
2110 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2111 for (i = 0 ; i < count ; i++)
2112 fusion->last_reply_idx[i] = 0;
2113 reply_desc = fusion->reply_frames_desc;
2114 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2115 reply_desc->Words = ULLONG_MAX;
2118 /* Core fusion reset function */
2119 int megasas_reset_fusion(struct Scsi_Host *shost)
2121 int retval = SUCCESS, i, j, retry = 0;
2122 struct megasas_instance *instance;
2123 struct megasas_cmd_fusion *cmd_fusion;
2124 struct fusion_context *fusion;
2125 struct megasas_cmd *cmd_mfi;
2126 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2127 u32 host_diag, abs_state, status_reg, reset_adapter;
2129 instance = (struct megasas_instance *)shost->hostdata;
2130 fusion = instance->ctrl_context;
2132 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2133 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2134 "returning FAILED.\n");
2135 return FAILED;
2138 mutex_lock(&instance->reset_mutex);
2139 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2140 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2141 instance->instancet->disable_intr(instance->reg_set);
2142 msleep(1000);
2144 /* First try waiting for commands to complete */
2145 if (megasas_wait_for_outstanding_fusion(instance)) {
2146 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2147 "adapter.\n");
2148 /* Now return commands back to the OS */
2149 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2150 cmd_fusion = fusion->cmd_list[i];
2151 if (cmd_fusion->scmd) {
2152 scsi_dma_unmap(cmd_fusion->scmd);
2153 cmd_fusion->scmd->result = (DID_RESET << 16);
2154 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2155 megasas_return_cmd_fusion(instance, cmd_fusion);
2156 atomic_dec(&instance->fw_outstanding);
2160 status_reg = instance->instancet->read_fw_status_reg(
2161 instance->reg_set);
2162 abs_state = status_reg & MFI_STATE_MASK;
2163 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2164 if (instance->disableOnlineCtrlReset ||
2165 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2166 /* Reset not supported, kill adapter */
2167 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2168 ", killing adapter.\n");
2169 megaraid_sas_kill_hba(instance);
2170 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2171 retval = FAILED;
2172 goto out;
2175 /* Now try to reset the chip */
2176 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2177 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2178 &instance->reg_set->fusion_seq_offset);
2179 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2180 &instance->reg_set->fusion_seq_offset);
2181 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2182 &instance->reg_set->fusion_seq_offset);
2183 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2184 &instance->reg_set->fusion_seq_offset);
2185 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2186 &instance->reg_set->fusion_seq_offset);
2187 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2188 &instance->reg_set->fusion_seq_offset);
2189 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2190 &instance->reg_set->fusion_seq_offset);
2192 /* Check that the diag write enable (DRWE) bit is on */
2193 host_diag = readl(&instance->reg_set->fusion_host_diag);
2194 retry = 0;
2195 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2196 msleep(100);
2197 host_diag =
2198 readl(&instance->reg_set->fusion_host_diag);
2199 if (retry++ == 100) {
2200 printk(KERN_WARNING "megaraid_sas: "
2201 "Host diag unlock failed!\n");
2202 break;
2205 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2206 continue;
2208 /* Send chip reset command */
2209 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2210 &instance->reg_set->fusion_host_diag);
2211 msleep(3000);
2213 /* Make sure reset adapter bit is cleared */
2214 host_diag = readl(&instance->reg_set->fusion_host_diag);
2215 retry = 0;
2216 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2217 msleep(100);
2218 host_diag =
2219 readl(&instance->reg_set->fusion_host_diag);
2220 if (retry++ == 1000) {
2221 printk(KERN_WARNING "megaraid_sas: "
2222 "Diag reset adapter never "
2223 "cleared!\n");
2224 break;
2227 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2228 continue;
2230 abs_state =
2231 instance->instancet->read_fw_status_reg(
2232 instance->reg_set) & MFI_STATE_MASK;
2233 retry = 0;
2235 while ((abs_state <= MFI_STATE_FW_INIT) &&
2236 (retry++ < 1000)) {
2237 msleep(100);
2238 abs_state =
2239 instance->instancet->read_fw_status_reg(
2240 instance->reg_set) & MFI_STATE_MASK;
2242 if (abs_state <= MFI_STATE_FW_INIT) {
2243 printk(KERN_WARNING "megaraid_sas: firmware "
2244 "state < MFI_STATE_FW_INIT, state = "
2245 "0x%x\n", abs_state);
2246 continue;
2249 /* Wait for FW to become ready */
2250 if (megasas_transition_to_ready(instance, 1)) {
2251 printk(KERN_WARNING "megaraid_sas: Failed to "
2252 "transition controller to ready.\n");
2253 continue;
2256 megasas_reset_reply_desc(instance);
2257 if (megasas_ioc_init_fusion(instance)) {
2258 printk(KERN_WARNING "megaraid_sas: "
2259 "megasas_ioc_init_fusion() failed!\n");
2260 continue;
2263 clear_bit(MEGASAS_FUSION_IN_RESET,
2264 &instance->reset_flags);
2265 instance->instancet->enable_intr(instance->reg_set);
2266 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2268 /* Re-fire management commands */
2269 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2270 cmd_fusion = fusion->cmd_list[j];
2271 if (cmd_fusion->sync_cmd_idx !=
2272 (u32)ULONG_MAX) {
2273 cmd_mfi =
2274 instance->
2275 cmd_list[cmd_fusion->sync_cmd_idx];
2276 if (cmd_mfi->frame->dcmd.opcode ==
2277 MR_DCMD_LD_MAP_GET_INFO) {
2278 megasas_return_cmd(instance,
2279 cmd_mfi);
2280 megasas_return_cmd_fusion(
2281 instance, cmd_fusion);
2282 } else {
2283 req_desc =
2284 megasas_get_request_descriptor(
2285 instance,
2286 cmd_mfi->context.smid
2287 -1);
2288 if (!req_desc)
2289 printk(KERN_WARNING
2290 "req_desc NULL"
2291 "\n");
2292 else {
2293 instance->instancet->
2294 fire_cmd(instance,
2295 req_desc->
2296 u.low,
2297 req_desc->
2298 u.high,
2299 instance->
2300 reg_set);
2306 /* Reset load balance info */
2307 memset(fusion->load_balance_info, 0,
2308 sizeof(struct LD_LOAD_BALANCE_INFO)
2309 *MAX_LOGICAL_DRIVES);
2311 if (!megasas_get_map_info(instance))
2312 megasas_sync_map_info(instance);
2314 /* Adapter reset completed successfully */
2315 printk(KERN_WARNING "megaraid_sas: Reset "
2316 "successful.\n");
2317 retval = SUCCESS;
2318 goto out;
2320 /* Reset failed, kill the adapter */
2321 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2322 "adapter.\n");
2323 megaraid_sas_kill_hba(instance);
2324 retval = FAILED;
2325 } else {
2326 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2327 instance->instancet->enable_intr(instance->reg_set);
2328 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2330 out:
2331 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2332 mutex_unlock(&instance->reset_mutex);
2333 return retval;
2336 /* Fusion OCR work queue */
2337 void megasas_fusion_ocr_wq(struct work_struct *work)
2339 struct megasas_instance *instance =
2340 container_of(work, struct megasas_instance, work_init);
2342 megasas_reset_fusion(instance->host);
2345 struct megasas_instance_template megasas_instance_template_fusion = {
2346 .fire_cmd = megasas_fire_cmd_fusion,
2347 .enable_intr = megasas_enable_intr_fusion,
2348 .disable_intr = megasas_disable_intr_fusion,
2349 .clear_intr = megasas_clear_intr_fusion,
2350 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2351 .adp_reset = megasas_adp_reset_fusion,
2352 .check_reset = megasas_check_reset_fusion,
2353 .service_isr = megasas_isr_fusion,
2354 .tasklet = megasas_complete_cmd_dpc_fusion,
2355 .init_adapter = megasas_init_adapter_fusion,
2356 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2357 .issue_dcmd = megasas_issue_dcmd_fusion,