power: reset: corrections for simple syscon reboot driver
[linux-2.6/btrfs-unstable.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
blob3ed03dfab76c4997eadb43aed51be2dbad3837a3
1 /*
2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2012 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,
66 int seconds);
68 void
69 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
70 int megasas_alloc_cmds(struct megasas_instance *instance);
71 int
72 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
73 int
74 megasas_issue_polled(struct megasas_instance *instance,
75 struct megasas_cmd *cmd);
76 void
77 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
79 u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
80 struct IO_REQUEST_INFO *in_info);
81 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
82 void megaraid_sas_kill_hba(struct megasas_instance *instance);
84 extern u32 megasas_dbg_lvl;
85 void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
86 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
87 int initial);
88 void megasas_start_timer(struct megasas_instance *instance,
89 struct timer_list *timer,
90 void *fn, unsigned long interval);
91 extern struct megasas_mgmt_info megasas_mgmt_info;
92 extern int resetwaittime;
94 /**
95 * megasas_enable_intr_fusion - Enables interrupts
96 * @regs: MFI register set
98 void
99 megasas_enable_intr_fusion(struct megasas_instance *instance)
101 struct megasas_register_set __iomem *regs;
102 regs = instance->reg_set;
103 /* For Thunderbolt/Invader also clear intr on enable */
104 writel(~0, &regs->outbound_intr_status);
105 readl(&regs->outbound_intr_status);
107 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
109 /* Dummy readl to force pci flush */
110 readl(&regs->outbound_intr_mask);
111 instance->mask_interrupts = 0;
115 * megasas_disable_intr_fusion - Disables interrupt
116 * @regs: MFI register set
118 void
119 megasas_disable_intr_fusion(struct megasas_instance *instance)
121 u32 mask = 0xFFFFFFFF;
122 u32 status;
123 struct megasas_register_set __iomem *regs;
124 regs = instance->reg_set;
125 instance->mask_interrupts = 1;
127 writel(mask, &regs->outbound_intr_mask);
128 /* Dummy readl to force pci flush */
129 status = readl(&regs->outbound_intr_mask);
133 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
135 u32 status;
137 * Check if it is our interrupt
139 status = readl(&regs->outbound_intr_status);
141 if (status & 1) {
142 writel(status, &regs->outbound_intr_status);
143 readl(&regs->outbound_intr_status);
144 return 1;
146 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
147 return 0;
149 return 1;
153 * megasas_get_cmd_fusion - Get a command from the free pool
154 * @instance: Adapter soft state
156 * Returns a free command from the pool
158 struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
159 *instance)
161 unsigned long flags;
162 struct fusion_context *fusion =
163 (struct fusion_context *)instance->ctrl_context;
164 struct megasas_cmd_fusion *cmd = NULL;
166 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
168 if (!list_empty(&fusion->cmd_pool)) {
169 cmd = list_entry((&fusion->cmd_pool)->next,
170 struct megasas_cmd_fusion, list);
171 list_del_init(&cmd->list);
172 } else {
173 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
176 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
177 return cmd;
181 * megasas_return_cmd_fusion - Return a cmd to free command pool
182 * @instance: Adapter soft state
183 * @cmd: Command packet to be returned to free command pool
185 static inline void
186 megasas_return_cmd_fusion(struct megasas_instance *instance,
187 struct megasas_cmd_fusion *cmd)
189 unsigned long flags;
190 struct fusion_context *fusion =
191 (struct fusion_context *)instance->ctrl_context;
193 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
195 cmd->scmd = NULL;
196 cmd->sync_cmd_idx = (u32)ULONG_MAX;
197 list_add_tail(&cmd->list, &fusion->cmd_pool);
199 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
203 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
204 * @instance: Adapter soft state
206 static void megasas_teardown_frame_pool_fusion(
207 struct megasas_instance *instance)
209 int i;
210 struct fusion_context *fusion = instance->ctrl_context;
212 u16 max_cmd = instance->max_fw_cmds;
214 struct megasas_cmd_fusion *cmd;
216 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
217 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
218 "sense pool : %p\n", fusion->sg_dma_pool,
219 fusion->sense_dma_pool);
220 return;
224 * Return all frames to pool
226 for (i = 0; i < max_cmd; i++) {
228 cmd = fusion->cmd_list[i];
230 if (cmd->sg_frame)
231 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
232 cmd->sg_frame_phys_addr);
234 if (cmd->sense)
235 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
236 cmd->sense_phys_addr);
240 * Now destroy the pool itself
242 pci_pool_destroy(fusion->sg_dma_pool);
243 pci_pool_destroy(fusion->sense_dma_pool);
245 fusion->sg_dma_pool = NULL;
246 fusion->sense_dma_pool = NULL;
250 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
251 * @instance: Adapter soft state
253 void
254 megasas_free_cmds_fusion(struct megasas_instance *instance)
256 int i;
257 struct fusion_context *fusion = instance->ctrl_context;
259 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
262 req_sz = fusion->request_alloc_sz;
263 reply_sz = fusion->reply_alloc_sz;
264 io_frames_sz = fusion->io_frames_alloc_sz;
266 max_cmds = instance->max_fw_cmds;
268 /* Free descriptors and request Frames memory */
269 if (fusion->req_frames_desc)
270 dma_free_coherent(&instance->pdev->dev, req_sz,
271 fusion->req_frames_desc,
272 fusion->req_frames_desc_phys);
274 if (fusion->reply_frames_desc) {
275 pci_pool_free(fusion->reply_frames_desc_pool,
276 fusion->reply_frames_desc,
277 fusion->reply_frames_desc_phys);
278 pci_pool_destroy(fusion->reply_frames_desc_pool);
281 if (fusion->io_request_frames) {
282 pci_pool_free(fusion->io_request_frames_pool,
283 fusion->io_request_frames,
284 fusion->io_request_frames_phys);
285 pci_pool_destroy(fusion->io_request_frames_pool);
288 /* Free the Fusion frame pool */
289 megasas_teardown_frame_pool_fusion(instance);
291 /* Free all the commands in the cmd_list */
292 for (i = 0; i < max_cmds; i++)
293 kfree(fusion->cmd_list[i]);
295 /* Free the cmd_list buffer itself */
296 kfree(fusion->cmd_list);
297 fusion->cmd_list = NULL;
299 INIT_LIST_HEAD(&fusion->cmd_pool);
303 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
304 * @instance: Adapter soft state
307 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
309 int i;
310 u32 max_cmd;
311 struct fusion_context *fusion;
312 struct megasas_cmd_fusion *cmd;
313 u32 total_sz_chain_frame;
315 fusion = instance->ctrl_context;
316 max_cmd = instance->max_fw_cmds;
318 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
321 * Use DMA pool facility provided by PCI layer
324 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
325 instance->pdev,
326 total_sz_chain_frame, 4,
328 if (!fusion->sg_dma_pool) {
329 printk(KERN_DEBUG "megasas: failed to setup request pool "
330 "fusion\n");
331 return -ENOMEM;
333 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
334 instance->pdev,
335 SCSI_SENSE_BUFFERSIZE, 64, 0);
337 if (!fusion->sense_dma_pool) {
338 printk(KERN_DEBUG "megasas: failed to setup sense pool "
339 "fusion\n");
340 pci_pool_destroy(fusion->sg_dma_pool);
341 fusion->sg_dma_pool = NULL;
342 return -ENOMEM;
346 * Allocate and attach a frame to each of the commands in cmd_list
348 for (i = 0; i < max_cmd; i++) {
350 cmd = fusion->cmd_list[i];
352 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
353 GFP_KERNEL,
354 &cmd->sg_frame_phys_addr);
356 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
357 GFP_KERNEL, &cmd->sense_phys_addr);
359 * megasas_teardown_frame_pool_fusion() takes care of freeing
360 * whatever has been allocated
362 if (!cmd->sg_frame || !cmd->sense) {
363 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
364 megasas_teardown_frame_pool_fusion(instance);
365 return -ENOMEM;
368 return 0;
372 * megasas_alloc_cmds_fusion - Allocates the command packets
373 * @instance: Adapter soft state
376 * Each frame has a 32-bit field called context. This context is used to get
377 * back the megasas_cmd_fusion from the frame when a frame gets completed
378 * In this driver, the 32 bit values are the indices into an array cmd_list.
379 * This array is used only to look up the megasas_cmd_fusion given the context.
380 * The free commands themselves are maintained in a linked list called cmd_pool.
382 * cmds are formed in the io_request and sg_frame members of the
383 * megasas_cmd_fusion. The context field is used to get a request descriptor
384 * and is used as SMID of the cmd.
385 * SMID value range is from 1 to max_fw_cmds.
388 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
390 int i, j, count;
391 u32 max_cmd, io_frames_sz;
392 struct fusion_context *fusion;
393 struct megasas_cmd_fusion *cmd;
394 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
395 u32 offset;
396 dma_addr_t io_req_base_phys;
397 u8 *io_req_base;
399 fusion = instance->ctrl_context;
401 max_cmd = instance->max_fw_cmds;
403 fusion->req_frames_desc =
404 dma_alloc_coherent(&instance->pdev->dev,
405 fusion->request_alloc_sz,
406 &fusion->req_frames_desc_phys, GFP_KERNEL);
408 if (!fusion->req_frames_desc) {
409 printk(KERN_ERR "megasas; Could not allocate memory for "
410 "request_frames\n");
411 goto fail_req_desc;
414 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
415 fusion->reply_frames_desc_pool =
416 pci_pool_create("reply_frames pool", instance->pdev,
417 fusion->reply_alloc_sz * count, 16, 0);
419 if (!fusion->reply_frames_desc_pool) {
420 printk(KERN_ERR "megasas; Could not allocate memory for "
421 "reply_frame pool\n");
422 goto fail_reply_desc;
425 fusion->reply_frames_desc =
426 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
427 &fusion->reply_frames_desc_phys);
428 if (!fusion->reply_frames_desc) {
429 printk(KERN_ERR "megasas; Could not allocate memory for "
430 "reply_frame pool\n");
431 pci_pool_destroy(fusion->reply_frames_desc_pool);
432 goto fail_reply_desc;
435 reply_desc = fusion->reply_frames_desc;
436 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
437 reply_desc->Words = ULLONG_MAX;
439 io_frames_sz = fusion->io_frames_alloc_sz;
441 fusion->io_request_frames_pool =
442 pci_pool_create("io_request_frames pool", instance->pdev,
443 fusion->io_frames_alloc_sz, 16, 0);
445 if (!fusion->io_request_frames_pool) {
446 printk(KERN_ERR "megasas: Could not allocate memory for "
447 "io_request_frame pool\n");
448 goto fail_io_frames;
451 fusion->io_request_frames =
452 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
453 &fusion->io_request_frames_phys);
454 if (!fusion->io_request_frames) {
455 printk(KERN_ERR "megasas: Could not allocate memory for "
456 "io_request_frames frames\n");
457 pci_pool_destroy(fusion->io_request_frames_pool);
458 goto fail_io_frames;
462 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
463 * Allocate the dynamic array first and then allocate individual
464 * commands.
466 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
467 * max_cmd, GFP_KERNEL);
469 if (!fusion->cmd_list) {
470 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
471 "memory for cmd_list_fusion\n");
472 goto fail_cmd_list;
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,
561 int seconds)
563 int i;
564 struct megasas_header *frame_hdr = &cmd->frame->hdr;
566 u32 msecs = seconds * 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 = cpu_to_le16(MPI2_VERSION);
628 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
629 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
631 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
632 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
633 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
634 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
635 init_frame = (struct megasas_init_frame *)cmd->frame;
636 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
638 frame_hdr = &cmd->frame->hdr;
639 frame_hdr->cmd_status = 0xFF;
640 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
642 init_frame->cmd = MFI_CMD_INIT;
643 init_frame->cmd_status = 0xFF;
645 /* driver support Extended MSIX */
646 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
647 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
648 init_frame->driver_operations.
649 mfi_capabilities.support_additional_msix = 1;
650 /* driver supports HA / Remote LUN over Fast Path interface */
651 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
652 = 1;
653 /* Convert capability to LE32 */
654 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
656 init_frame->queue_info_new_phys_addr_hi =
657 cpu_to_le32(upper_32_bits(ioc_init_handle));
658 init_frame->queue_info_new_phys_addr_lo =
659 cpu_to_le32(lower_32_bits(ioc_init_handle));
660 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
662 req_desc.Words = 0;
663 req_desc.MFAIo.RequestFlags =
664 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
665 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
666 cpu_to_le32s((u32 *)&req_desc.MFAIo);
667 req_desc.Words |= cpu_to_le64(cmd->frame_phys_addr);
670 * disable the intr before firing the init frame
672 instance->instancet->disable_intr(instance);
674 for (i = 0; i < (10 * 1000); i += 20) {
675 if (readl(&instance->reg_set->doorbell) & 1)
676 msleep(20);
677 else
678 break;
681 instance->instancet->fire_cmd(instance, req_desc.u.low,
682 req_desc.u.high, instance->reg_set);
684 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
686 frame_hdr = &cmd->frame->hdr;
687 if (frame_hdr->cmd_status != 0) {
688 ret = 1;
689 goto fail_fw_init;
691 printk(KERN_ERR "megasas:IOC Init cmd success\n");
693 ret = 0;
695 fail_fw_init:
696 megasas_return_cmd(instance, cmd);
697 if (IOCInitMessage)
698 dma_free_coherent(&instance->pdev->dev,
699 sizeof(struct MPI2_IOC_INIT_REQUEST),
700 IOCInitMessage, ioc_init_handle);
701 fail_get_cmd:
702 return ret;
706 * megasas_get_ld_map_info - Returns FW's ld_map structure
707 * @instance: Adapter soft state
708 * @pend: Pend the command or not
709 * Issues an internal command (DCMD) to get the FW's controller PD
710 * list structure. This information is mainly used to find out SYSTEM
711 * supported by the FW.
713 static int
714 megasas_get_ld_map_info(struct megasas_instance *instance)
716 int ret = 0;
717 struct megasas_cmd *cmd;
718 struct megasas_dcmd_frame *dcmd;
719 struct MR_FW_RAID_MAP_ALL *ci;
720 dma_addr_t ci_h = 0;
721 u32 size_map_info;
722 struct fusion_context *fusion;
724 cmd = megasas_get_cmd(instance);
726 if (!cmd) {
727 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
728 return -ENOMEM;
731 fusion = instance->ctrl_context;
733 if (!fusion) {
734 megasas_return_cmd(instance, cmd);
735 return -ENXIO;
738 dcmd = &cmd->frame->dcmd;
740 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
741 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
743 ci = fusion->ld_map[(instance->map_id & 1)];
744 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
746 if (!ci) {
747 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
748 megasas_return_cmd(instance, cmd);
749 return -ENOMEM;
752 memset(ci, 0, sizeof(*ci));
753 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
755 dcmd->cmd = MFI_CMD_DCMD;
756 dcmd->cmd_status = 0xFF;
757 dcmd->sge_count = 1;
758 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
759 dcmd->timeout = 0;
760 dcmd->pad_0 = 0;
761 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
762 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
763 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
764 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
766 if (!megasas_issue_polled(instance, cmd))
767 ret = 0;
768 else {
769 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
770 ret = -1;
773 megasas_return_cmd(instance, cmd);
775 return ret;
779 megasas_get_map_info(struct megasas_instance *instance)
781 struct fusion_context *fusion = instance->ctrl_context;
783 fusion->fast_path_io = 0;
784 if (!megasas_get_ld_map_info(instance)) {
785 if (MR_ValidateMapInfo(instance)) {
786 fusion->fast_path_io = 1;
787 return 0;
790 return 1;
794 * megasas_sync_map_info - Returns FW's ld_map structure
795 * @instance: Adapter soft state
797 * Issues an internal command (DCMD) to get the FW's controller PD
798 * list structure. This information is mainly used to find out SYSTEM
799 * supported by the FW.
802 megasas_sync_map_info(struct megasas_instance *instance)
804 int ret = 0, i;
805 struct megasas_cmd *cmd;
806 struct megasas_dcmd_frame *dcmd;
807 u32 size_sync_info, num_lds;
808 struct fusion_context *fusion;
809 struct MR_LD_TARGET_SYNC *ci = NULL;
810 struct MR_FW_RAID_MAP_ALL *map;
811 struct MR_LD_RAID *raid;
812 struct MR_LD_TARGET_SYNC *ld_sync;
813 dma_addr_t ci_h = 0;
814 u32 size_map_info;
816 cmd = megasas_get_cmd(instance);
818 if (!cmd) {
819 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
820 "info.\n");
821 return -ENOMEM;
824 fusion = instance->ctrl_context;
826 if (!fusion) {
827 megasas_return_cmd(instance, cmd);
828 return 1;
831 map = fusion->ld_map[instance->map_id & 1];
833 num_lds = le32_to_cpu(map->raidMap.ldCount);
835 dcmd = &cmd->frame->dcmd;
837 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
839 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
841 ci = (struct MR_LD_TARGET_SYNC *)
842 fusion->ld_map[(instance->map_id - 1) & 1];
843 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
845 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
847 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
849 for (i = 0; i < num_lds; i++, ld_sync++) {
850 raid = MR_LdRaidGet(i, map);
851 ld_sync->targetId = MR_GetLDTgtId(i, map);
852 ld_sync->seqNum = raid->seqNum;
855 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
856 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
858 dcmd->cmd = MFI_CMD_DCMD;
859 dcmd->cmd_status = 0xFF;
860 dcmd->sge_count = 1;
861 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
862 dcmd->timeout = 0;
863 dcmd->pad_0 = 0;
864 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
865 dcmd->mbox.b[0] = num_lds;
866 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
867 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
868 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
869 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
871 instance->map_update_cmd = cmd;
873 instance->instancet->issue_dcmd(instance, cmd);
875 return ret;
879 * meagasas_display_intel_branding - Display branding string
880 * @instance: per adapter object
882 * Return nothing.
884 static void
885 megasas_display_intel_branding(struct megasas_instance *instance)
887 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
888 return;
890 switch (instance->pdev->device) {
891 case PCI_DEVICE_ID_LSI_INVADER:
892 switch (instance->pdev->subsystem_device) {
893 case MEGARAID_INTEL_RS3DC080_SSDID:
894 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
895 instance->host->host_no,
896 MEGARAID_INTEL_RS3DC080_BRANDING);
897 break;
898 case MEGARAID_INTEL_RS3DC040_SSDID:
899 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
900 instance->host->host_no,
901 MEGARAID_INTEL_RS3DC040_BRANDING);
902 break;
903 case MEGARAID_INTEL_RS3SC008_SSDID:
904 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
905 instance->host->host_no,
906 MEGARAID_INTEL_RS3SC008_BRANDING);
907 break;
908 case MEGARAID_INTEL_RS3MC044_SSDID:
909 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
910 instance->host->host_no,
911 MEGARAID_INTEL_RS3MC044_BRANDING);
912 break;
913 default:
914 break;
916 break;
917 case PCI_DEVICE_ID_LSI_FURY:
918 switch (instance->pdev->subsystem_device) {
919 case MEGARAID_INTEL_RS3WC080_SSDID:
920 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
921 instance->host->host_no,
922 MEGARAID_INTEL_RS3WC080_BRANDING);
923 break;
924 case MEGARAID_INTEL_RS3WC040_SSDID:
925 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
926 instance->host->host_no,
927 MEGARAID_INTEL_RS3WC040_BRANDING);
928 break;
929 default:
930 break;
932 break;
933 default:
934 break;
939 * megasas_init_adapter_fusion - Initializes the FW
940 * @instance: Adapter soft state
942 * This is the main function for initializing firmware.
945 megasas_init_adapter_fusion(struct megasas_instance *instance)
947 struct megasas_register_set __iomem *reg_set;
948 struct fusion_context *fusion;
949 u32 max_cmd;
950 int i = 0, count;
952 fusion = instance->ctrl_context;
954 reg_set = instance->reg_set;
957 * Get various operational parameters from status register
959 instance->max_fw_cmds =
960 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
961 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
964 * Reduce the max supported cmds by 1. This is to ensure that the
965 * reply_q_sz (1 more than the max cmd that driver may send)
966 * does not exceed max cmds that the FW can support
968 instance->max_fw_cmds = instance->max_fw_cmds-1;
969 /* Only internal cmds (DCMD) need to have MFI frames */
970 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
972 max_cmd = instance->max_fw_cmds;
974 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
976 fusion->request_alloc_sz =
977 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
978 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
979 *(fusion->reply_q_depth);
980 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
981 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
982 (max_cmd + 1)); /* Extra 1 for SMID 0 */
984 fusion->max_sge_in_main_msg =
985 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
986 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
988 fusion->max_sge_in_chain =
989 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
991 instance->max_num_sge = fusion->max_sge_in_main_msg +
992 fusion->max_sge_in_chain - 2;
994 /* Used for pass thru MFI frame (DCMD) */
995 fusion->chain_offset_mfi_pthru =
996 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
998 fusion->chain_offset_io_request =
999 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1000 sizeof(union MPI2_SGE_IO_UNION))/16;
1002 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1003 for (i = 0 ; i < count; i++)
1004 fusion->last_reply_idx[i] = 0;
1007 * Allocate memory for descriptors
1008 * Create a pool of commands
1010 if (megasas_alloc_cmds(instance))
1011 goto fail_alloc_mfi_cmds;
1012 if (megasas_alloc_cmds_fusion(instance))
1013 goto fail_alloc_cmds;
1015 if (megasas_ioc_init_fusion(instance))
1016 goto fail_ioc_init;
1018 megasas_display_intel_branding(instance);
1020 instance->flag_ieee = 1;
1022 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
1023 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
1025 fusion->fast_path_io = 0;
1027 for (i = 0; i < 2; i++) {
1028 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1029 fusion->map_sz,
1030 &fusion->ld_map_phys[i],
1031 GFP_KERNEL);
1032 if (!fusion->ld_map[i]) {
1033 printk(KERN_ERR "megasas: Could not allocate memory "
1034 "for map info\n");
1035 goto fail_map_info;
1039 if (!megasas_get_map_info(instance))
1040 megasas_sync_map_info(instance);
1042 return 0;
1044 fail_map_info:
1045 if (i == 1)
1046 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
1047 fusion->ld_map[0], fusion->ld_map_phys[0]);
1048 fail_ioc_init:
1049 megasas_free_cmds_fusion(instance);
1050 fail_alloc_cmds:
1051 megasas_free_cmds(instance);
1052 fail_alloc_mfi_cmds:
1053 return 1;
1057 * megasas_fire_cmd_fusion - Sends command to the FW
1058 * @frame_phys_addr : Physical address of cmd
1059 * @frame_count : Number of frames for the command
1060 * @regs : MFI register set
1062 void
1063 megasas_fire_cmd_fusion(struct megasas_instance *instance,
1064 dma_addr_t req_desc_lo,
1065 u32 req_desc_hi,
1066 struct megasas_register_set __iomem *regs)
1068 unsigned long flags;
1070 spin_lock_irqsave(&instance->hba_lock, flags);
1072 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1073 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
1074 spin_unlock_irqrestore(&instance->hba_lock, flags);
1078 * map_cmd_status - Maps FW cmd status to OS cmd status
1079 * @cmd : Pointer to cmd
1080 * @status : status of cmd returned by FW
1081 * @ext_status : ext status of cmd returned by FW
1084 void
1085 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1088 switch (status) {
1090 case MFI_STAT_OK:
1091 cmd->scmd->result = DID_OK << 16;
1092 break;
1094 case MFI_STAT_SCSI_IO_FAILED:
1095 case MFI_STAT_LD_INIT_IN_PROGRESS:
1096 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1097 break;
1099 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1101 cmd->scmd->result = (DID_OK << 16) | ext_status;
1102 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1103 memset(cmd->scmd->sense_buffer, 0,
1104 SCSI_SENSE_BUFFERSIZE);
1105 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1106 SCSI_SENSE_BUFFERSIZE);
1107 cmd->scmd->result |= DRIVER_SENSE << 24;
1109 break;
1111 case MFI_STAT_LD_OFFLINE:
1112 case MFI_STAT_DEVICE_NOT_FOUND:
1113 cmd->scmd->result = DID_BAD_TARGET << 16;
1114 break;
1115 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1116 cmd->scmd->result = DID_IMM_RETRY << 16;
1117 break;
1118 default:
1119 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1120 cmd->scmd->result = DID_ERROR << 16;
1121 break;
1126 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1127 * @instance: Adapter soft state
1128 * @scp: SCSI command from the mid-layer
1129 * @sgl_ptr: SGL to be filled in
1130 * @cmd: cmd we are working on
1132 * If successful, this function returns the number of SG elements.
1134 static int
1135 megasas_make_sgl_fusion(struct megasas_instance *instance,
1136 struct scsi_cmnd *scp,
1137 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1138 struct megasas_cmd_fusion *cmd)
1140 int i, sg_processed, sge_count;
1141 struct scatterlist *os_sgl;
1142 struct fusion_context *fusion;
1144 fusion = instance->ctrl_context;
1146 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1147 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1148 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1149 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1150 sgl_ptr_end->Flags = 0;
1153 sge_count = scsi_dma_map(scp);
1155 BUG_ON(sge_count < 0);
1157 if (sge_count > instance->max_num_sge || !sge_count)
1158 return sge_count;
1160 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1161 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1162 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1163 sgl_ptr->Flags = 0;
1164 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1165 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1166 if (i == sge_count - 1)
1167 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1169 sgl_ptr++;
1171 sg_processed = i + 1;
1173 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1174 (sge_count > fusion->max_sge_in_main_msg)) {
1176 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1177 if ((instance->pdev->device ==
1178 PCI_DEVICE_ID_LSI_INVADER) ||
1179 (instance->pdev->device ==
1180 PCI_DEVICE_ID_LSI_FURY)) {
1181 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1182 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1183 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1184 cmd->io_request->ChainOffset =
1185 fusion->
1186 chain_offset_io_request;
1187 else
1188 cmd->io_request->ChainOffset = 0;
1189 } else
1190 cmd->io_request->ChainOffset =
1191 fusion->chain_offset_io_request;
1193 sg_chain = sgl_ptr;
1194 /* Prepare chain element */
1195 sg_chain->NextChainOffset = 0;
1196 if ((instance->pdev->device ==
1197 PCI_DEVICE_ID_LSI_INVADER) ||
1198 (instance->pdev->device ==
1199 PCI_DEVICE_ID_LSI_FURY))
1200 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1201 else
1202 sg_chain->Flags =
1203 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1204 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1205 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1206 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1208 sgl_ptr =
1209 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1213 return sge_count;
1217 * megasas_set_pd_lba - Sets PD LBA
1218 * @cdb: CDB
1219 * @cdb_len: cdb length
1220 * @start_blk: Start block of IO
1222 * Used to set the PD LBA in CDB for FP IOs
1224 void
1225 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1226 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1227 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1229 struct MR_LD_RAID *raid;
1230 u32 ld;
1231 u64 start_blk = io_info->pdBlock;
1232 u8 *cdb = io_request->CDB.CDB32;
1233 u32 num_blocks = io_info->numBlocks;
1234 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1236 /* Check if T10 PI (DIF) is enabled for this LD */
1237 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1238 raid = MR_LdRaidGet(ld, local_map_ptr);
1239 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1240 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1241 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1242 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1244 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1245 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1246 else
1247 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1248 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1250 /* LBA */
1251 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1252 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1253 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1254 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1255 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1256 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1257 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1258 cdb[19] = (u8)(start_blk & 0xff);
1260 /* Logical block reference tag */
1261 io_request->CDB.EEDP32.PrimaryReferenceTag =
1262 cpu_to_be32(ref_tag);
1263 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1264 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1266 /* Transfer length */
1267 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1268 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1269 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1270 cdb[31] = (u8)(num_blocks & 0xff);
1272 /* set SCSI IO EEDPFlags */
1273 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1274 io_request->EEDPFlags = cpu_to_le16(
1275 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1276 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1277 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1278 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1279 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1280 } else {
1281 io_request->EEDPFlags = cpu_to_le16(
1282 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1283 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1285 io_request->Control |= cpu_to_le32((0x4 << 26));
1286 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1287 } else {
1288 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1289 if (((cdb_len == 12) || (cdb_len == 16)) &&
1290 (start_blk <= 0xffffffff)) {
1291 if (cdb_len == 16) {
1292 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1293 flagvals = cdb[1];
1294 groupnum = cdb[14];
1295 control = cdb[15];
1296 } else {
1297 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1298 flagvals = cdb[1];
1299 groupnum = cdb[10];
1300 control = cdb[11];
1303 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1305 cdb[0] = opcode;
1306 cdb[1] = flagvals;
1307 cdb[6] = groupnum;
1308 cdb[9] = control;
1310 /* Transfer length */
1311 cdb[8] = (u8)(num_blocks & 0xff);
1312 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1314 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1315 cdb_len = 10;
1316 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1317 /* Convert to 16 byte CDB for large LBA's */
1318 switch (cdb_len) {
1319 case 6:
1320 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1321 control = cdb[5];
1322 break;
1323 case 10:
1324 opcode =
1325 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1326 flagvals = cdb[1];
1327 groupnum = cdb[6];
1328 control = cdb[9];
1329 break;
1330 case 12:
1331 opcode =
1332 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1333 flagvals = cdb[1];
1334 groupnum = cdb[10];
1335 control = cdb[11];
1336 break;
1339 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1341 cdb[0] = opcode;
1342 cdb[1] = flagvals;
1343 cdb[14] = groupnum;
1344 cdb[15] = control;
1346 /* Transfer length */
1347 cdb[13] = (u8)(num_blocks & 0xff);
1348 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1349 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1350 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1352 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1353 cdb_len = 16;
1356 /* Normal case, just load LBA here */
1357 switch (cdb_len) {
1358 case 6:
1360 u8 val = cdb[1] & 0xE0;
1361 cdb[3] = (u8)(start_blk & 0xff);
1362 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1363 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1364 break;
1366 case 10:
1367 cdb[5] = (u8)(start_blk & 0xff);
1368 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1369 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1370 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1371 break;
1372 case 12:
1373 cdb[5] = (u8)(start_blk & 0xff);
1374 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1375 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1376 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1377 break;
1378 case 16:
1379 cdb[9] = (u8)(start_blk & 0xff);
1380 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1381 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1382 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1383 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1384 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1385 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1386 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1387 break;
1393 * megasas_build_ldio_fusion - Prepares IOs to devices
1394 * @instance: Adapter soft state
1395 * @scp: SCSI command
1396 * @cmd: Command to be prepared
1398 * Prepares the io_request and chain elements (sg_frame) for IO
1399 * The IO can be for PD (Fast Path) or LD
1401 void
1402 megasas_build_ldio_fusion(struct megasas_instance *instance,
1403 struct scsi_cmnd *scp,
1404 struct megasas_cmd_fusion *cmd)
1406 u8 fp_possible;
1407 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1408 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1409 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1410 struct IO_REQUEST_INFO io_info;
1411 struct fusion_context *fusion;
1412 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1413 u8 *raidLUN;
1415 device_id = MEGASAS_DEV_INDEX(instance, scp);
1417 fusion = instance->ctrl_context;
1419 io_request = cmd->io_request;
1420 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1421 io_request->RaidContext.status = 0;
1422 io_request->RaidContext.exStatus = 0;
1424 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1426 start_lba_lo = 0;
1427 start_lba_hi = 0;
1428 fp_possible = 0;
1431 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1433 if (scp->cmd_len == 6) {
1434 datalength = (u32) scp->cmnd[4];
1435 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1436 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1438 start_lba_lo &= 0x1FFFFF;
1442 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1444 else if (scp->cmd_len == 10) {
1445 datalength = (u32) scp->cmnd[8] |
1446 ((u32) scp->cmnd[7] << 8);
1447 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1448 ((u32) scp->cmnd[3] << 16) |
1449 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1453 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1455 else if (scp->cmd_len == 12) {
1456 datalength = ((u32) scp->cmnd[6] << 24) |
1457 ((u32) scp->cmnd[7] << 16) |
1458 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1459 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1460 ((u32) scp->cmnd[3] << 16) |
1461 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1465 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1467 else if (scp->cmd_len == 16) {
1468 datalength = ((u32) scp->cmnd[10] << 24) |
1469 ((u32) scp->cmnd[11] << 16) |
1470 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1471 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1472 ((u32) scp->cmnd[7] << 16) |
1473 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1475 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1476 ((u32) scp->cmnd[3] << 16) |
1477 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1480 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1481 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1482 io_info.numBlocks = datalength;
1483 io_info.ldTgtId = device_id;
1484 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1486 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1487 io_info.isRead = 1;
1489 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1491 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1492 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1493 io_request->RaidContext.regLockFlags = 0;
1494 fp_possible = 0;
1495 } else {
1496 if (MR_BuildRaidContext(instance, &io_info,
1497 &io_request->RaidContext,
1498 local_map_ptr, &raidLUN))
1499 fp_possible = io_info.fpOkForIo;
1502 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1503 id by default, not CPU group id, otherwise all MSI-X queues won't
1504 be utilized */
1505 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1506 smp_processor_id() % instance->msix_vectors : 0;
1508 if (fp_possible) {
1509 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1510 local_map_ptr, start_lba_lo);
1511 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1512 cmd->request_desc->SCSIIO.RequestFlags =
1513 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1514 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1515 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1516 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1517 if (io_request->RaidContext.regLockFlags ==
1518 REGION_TYPE_UNUSED)
1519 cmd->request_desc->SCSIIO.RequestFlags =
1520 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1521 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1522 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1523 io_request->RaidContext.nseg = 0x1;
1524 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1525 io_request->RaidContext.regLockFlags |=
1526 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1527 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1529 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1530 (io_info.isRead)) {
1531 io_info.devHandle =
1532 get_updated_dev_handle(
1533 &fusion->load_balance_info[device_id],
1534 &io_info);
1535 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1536 } else
1537 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1538 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1539 io_request->DevHandle = io_info.devHandle;
1540 /* populate the LUN field */
1541 memcpy(io_request->LUN, raidLUN, 8);
1542 } else {
1543 io_request->RaidContext.timeoutValue =
1544 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
1545 cmd->request_desc->SCSIIO.RequestFlags =
1546 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1547 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1548 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1549 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1550 if (io_request->RaidContext.regLockFlags ==
1551 REGION_TYPE_UNUSED)
1552 cmd->request_desc->SCSIIO.RequestFlags =
1553 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1554 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1555 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1556 io_request->RaidContext.regLockFlags |=
1557 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1558 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1559 io_request->RaidContext.nseg = 0x1;
1561 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1562 io_request->DevHandle = cpu_to_le16(device_id);
1563 } /* Not FP */
1567 * megasas_build_dcdb_fusion - Prepares IOs to devices
1568 * @instance: Adapter soft state
1569 * @scp: SCSI command
1570 * @cmd: Command to be prepared
1572 * Prepares the io_request frame for non-io cmds
1574 static void
1575 megasas_build_dcdb_fusion(struct megasas_instance *instance,
1576 struct scsi_cmnd *scmd,
1577 struct megasas_cmd_fusion *cmd)
1579 u32 device_id;
1580 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1581 u16 pd_index = 0;
1582 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1583 struct fusion_context *fusion = instance->ctrl_context;
1584 u8 span, physArm;
1585 u16 devHandle;
1586 u32 ld, arRef, pd;
1587 struct MR_LD_RAID *raid;
1588 struct RAID_CONTEXT *pRAID_Context;
1590 io_request = cmd->io_request;
1591 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1592 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1593 +scmd->device->id;
1594 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1596 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1599 /* Check if this is a system PD I/O */
1600 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1601 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
1602 io_request->Function = 0;
1603 if (fusion->fast_path_io)
1604 io_request->DevHandle =
1605 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1606 io_request->RaidContext.timeoutValue =
1607 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1608 io_request->RaidContext.regLockFlags = 0;
1609 io_request->RaidContext.regLockRowLBA = 0;
1610 io_request->RaidContext.regLockLength = 0;
1611 io_request->RaidContext.RAIDFlags =
1612 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1613 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1614 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1615 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1616 io_request->IoFlags |= cpu_to_le16(
1617 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1618 cmd->request_desc->SCSIIO.RequestFlags =
1619 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1620 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1621 cmd->request_desc->SCSIIO.DevHandle =
1622 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1623 cmd->request_desc->SCSIIO.MSIxIndex =
1624 instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
1626 * If the command is for the tape device, set the
1627 * FP timeout to the os layer timeout value.
1629 if (scmd->device->type == TYPE_TAPE) {
1630 if ((scmd->request->timeout / HZ) > 0xFFFF)
1631 io_request->RaidContext.timeoutValue =
1632 0xFFFF;
1633 else
1634 io_request->RaidContext.timeoutValue =
1635 scmd->request->timeout / HZ;
1637 } else {
1638 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1639 goto NonFastPath;
1641 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1642 if ((ld >= MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io))
1643 goto NonFastPath;
1645 raid = MR_LdRaidGet(ld, local_map_ptr);
1647 /* check if this LD is FP capable */
1648 if (!(raid->capability.fpNonRWCapable))
1649 /* not FP capable, send as non-FP */
1650 goto NonFastPath;
1652 /* get RAID_Context pointer */
1653 pRAID_Context = &io_request->RaidContext;
1655 /* set RAID context values */
1656 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1657 pRAID_Context->timeoutValue = raid->fpIoTimeoutForLd;
1658 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1659 pRAID_Context->regLockRowLBA = 0;
1660 pRAID_Context->regLockLength = 0;
1661 pRAID_Context->configSeqNum = raid->seqNum;
1663 /* get the DevHandle for the PD (since this is
1664 fpNonRWCapable, this is a single disk RAID0) */
1665 span = physArm = 0;
1666 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1667 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1668 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1670 /* build request descriptor */
1671 cmd->request_desc->SCSIIO.RequestFlags =
1672 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1673 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1674 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1676 /* populate the LUN field */
1677 memcpy(io_request->LUN, raid->LUN, 8);
1679 /* build the raidScsiIO structure */
1680 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1681 io_request->DevHandle = devHandle;
1683 return;
1685 NonFastPath:
1686 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1687 io_request->DevHandle = cpu_to_le16(device_id);
1688 cmd->request_desc->SCSIIO.RequestFlags =
1689 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1690 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1692 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1693 int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
1697 * megasas_build_io_fusion - Prepares IOs to devices
1698 * @instance: Adapter soft state
1699 * @scp: SCSI command
1700 * @cmd: Command to be prepared
1702 * Invokes helper functions to prepare request frames
1703 * and sets flags appropriate for IO/Non-IO cmd
1706 megasas_build_io_fusion(struct megasas_instance *instance,
1707 struct scsi_cmnd *scp,
1708 struct megasas_cmd_fusion *cmd)
1710 u32 device_id, sge_count;
1711 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1713 device_id = MEGASAS_DEV_INDEX(instance, scp);
1715 /* Zero out some fields so they don't get reused */
1716 memset(io_request->LUN, 0x0, 8);
1717 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1718 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1719 io_request->EEDPFlags = 0;
1720 io_request->Control = 0;
1721 io_request->EEDPBlockSize = 0;
1722 io_request->ChainOffset = 0;
1723 io_request->RaidContext.RAIDFlags = 0;
1724 io_request->RaidContext.Type = 0;
1725 io_request->RaidContext.nseg = 0;
1727 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1729 * Just the CDB length,rest of the Flags are zero
1730 * This will be modified for FP in build_ldio_fusion
1732 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
1734 if (megasas_is_ldio(scp))
1735 megasas_build_ldio_fusion(instance, scp, cmd);
1736 else
1737 megasas_build_dcdb_fusion(instance, scp, cmd);
1740 * Construct SGL
1743 sge_count =
1744 megasas_make_sgl_fusion(instance, scp,
1745 (struct MPI25_IEEE_SGE_CHAIN64 *)
1746 &io_request->SGL, cmd);
1748 if (sge_count > instance->max_num_sge) {
1749 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1750 "max (0x%x) allowed\n", sge_count,
1751 instance->max_num_sge);
1752 return 1;
1755 io_request->RaidContext.numSGE = sge_count;
1757 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
1759 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1760 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
1761 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1762 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
1764 io_request->SGLOffset0 =
1765 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1767 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
1768 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1770 cmd->scmd = scp;
1771 scp->SCp.ptr = (char *)cmd;
1773 return 0;
1776 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1777 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1779 u8 *p;
1780 struct fusion_context *fusion;
1782 if (index >= instance->max_fw_cmds) {
1783 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1784 "descriptor for scsi%d\n", index,
1785 instance->host->host_no);
1786 return NULL;
1788 fusion = instance->ctrl_context;
1789 p = fusion->req_frames_desc
1790 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1792 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1796 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1797 * issuing non IOCTL cmd
1798 * @instance: Adapter soft state
1799 * @scmd: pointer to scsi cmd from OS
1801 static u32
1802 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1803 struct scsi_cmnd *scmd)
1805 struct megasas_cmd_fusion *cmd;
1806 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1807 u32 index;
1808 struct fusion_context *fusion;
1810 fusion = instance->ctrl_context;
1812 cmd = megasas_get_cmd_fusion(instance);
1813 if (!cmd)
1814 return SCSI_MLQUEUE_HOST_BUSY;
1816 index = cmd->index;
1818 req_desc = megasas_get_request_descriptor(instance, index-1);
1819 if (!req_desc)
1820 return 1;
1822 req_desc->Words = 0;
1823 cmd->request_desc = req_desc;
1825 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1826 megasas_return_cmd_fusion(instance, cmd);
1827 printk(KERN_ERR "megasas: Error building command.\n");
1828 cmd->request_desc = NULL;
1829 return 1;
1832 req_desc = cmd->request_desc;
1833 req_desc->SCSIIO.SMID = cpu_to_le16(index);
1835 if (cmd->io_request->ChainOffset != 0 &&
1836 cmd->io_request->ChainOffset != 0xF)
1837 printk(KERN_ERR "megasas: The chain offset value is not "
1838 "correct : %x\n", cmd->io_request->ChainOffset);
1841 * Issue the command to the FW
1843 atomic_inc(&instance->fw_outstanding);
1845 instance->instancet->fire_cmd(instance,
1846 req_desc->u.low, req_desc->u.high,
1847 instance->reg_set);
1849 return 0;
1853 * complete_cmd_fusion - Completes command
1854 * @instance: Adapter soft state
1855 * Completes all commands that is in reply descriptor queue
1858 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1860 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1861 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1862 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1863 struct fusion_context *fusion;
1864 struct megasas_cmd *cmd_mfi;
1865 struct megasas_cmd_fusion *cmd_fusion;
1866 u16 smid, num_completed;
1867 u8 reply_descript_type, arm;
1868 u32 status, extStatus, device_id;
1869 union desc_value d_val;
1870 struct LD_LOAD_BALANCE_INFO *lbinfo;
1872 fusion = instance->ctrl_context;
1874 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1875 return IRQ_HANDLED;
1877 desc = fusion->reply_frames_desc;
1878 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1879 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1880 fusion->last_reply_idx[MSIxIndex];
1882 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1884 d_val.word = desc->Words;
1886 reply_descript_type = reply_desc->ReplyFlags &
1887 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1889 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1890 return IRQ_NONE;
1892 num_completed = 0;
1894 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1895 smid = le16_to_cpu(reply_desc->SMID);
1897 cmd_fusion = fusion->cmd_list[smid - 1];
1899 scsi_io_req =
1900 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1901 cmd_fusion->io_request;
1903 if (cmd_fusion->scmd)
1904 cmd_fusion->scmd->SCp.ptr = NULL;
1906 status = scsi_io_req->RaidContext.status;
1907 extStatus = scsi_io_req->RaidContext.exStatus;
1909 switch (scsi_io_req->Function) {
1910 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1911 /* Update load balancing info */
1912 device_id = MEGASAS_DEV_INDEX(instance,
1913 cmd_fusion->scmd);
1914 lbinfo = &fusion->load_balance_info[device_id];
1915 if (cmd_fusion->scmd->SCp.Status &
1916 MEGASAS_LOAD_BALANCE_FLAG) {
1917 arm = lbinfo->raid1DevHandle[0] ==
1918 cmd_fusion->io_request->DevHandle ? 0 :
1920 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1921 cmd_fusion->scmd->SCp.Status &=
1922 ~MEGASAS_LOAD_BALANCE_FLAG;
1924 if (reply_descript_type ==
1925 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1926 if (megasas_dbg_lvl == 5)
1927 printk(KERN_ERR "\nmegasas: FAST Path "
1928 "IO Success\n");
1930 /* Fall thru and complete IO */
1931 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1932 /* Map the FW Cmd Status */
1933 map_cmd_status(cmd_fusion, status, extStatus);
1934 scsi_dma_unmap(cmd_fusion->scmd);
1935 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1936 scsi_io_req->RaidContext.status = 0;
1937 scsi_io_req->RaidContext.exStatus = 0;
1938 megasas_return_cmd_fusion(instance, cmd_fusion);
1939 atomic_dec(&instance->fw_outstanding);
1941 break;
1942 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1943 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1944 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1945 cmd_fusion->flags = 0;
1946 megasas_return_cmd_fusion(instance, cmd_fusion);
1948 break;
1951 fusion->last_reply_idx[MSIxIndex]++;
1952 if (fusion->last_reply_idx[MSIxIndex] >=
1953 fusion->reply_q_depth)
1954 fusion->last_reply_idx[MSIxIndex] = 0;
1956 desc->Words = ULLONG_MAX;
1957 num_completed++;
1959 /* Get the next reply descriptor */
1960 if (!fusion->last_reply_idx[MSIxIndex])
1961 desc = fusion->reply_frames_desc +
1962 ((MSIxIndex * fusion->reply_alloc_sz)/
1963 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
1964 else
1965 desc++;
1967 reply_desc =
1968 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1970 d_val.word = desc->Words;
1972 reply_descript_type = reply_desc->ReplyFlags &
1973 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1975 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1976 break;
1979 if (!num_completed)
1980 return IRQ_NONE;
1982 wmb();
1983 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1984 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1985 writel(((MSIxIndex & 0x7) << 24) |
1986 fusion->last_reply_idx[MSIxIndex],
1987 instance->reply_post_host_index_addr[MSIxIndex/8]);
1988 else
1989 writel((MSIxIndex << 24) |
1990 fusion->last_reply_idx[MSIxIndex],
1991 instance->reply_post_host_index_addr[0]);
1992 megasas_check_and_restore_queue_depth(instance);
1993 return IRQ_HANDLED;
1997 * megasas_complete_cmd_dpc_fusion - Completes command
1998 * @instance: Adapter soft state
2000 * Tasklet to complete cmds
2002 void
2003 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2005 struct megasas_instance *instance =
2006 (struct megasas_instance *)instance_addr;
2007 unsigned long flags;
2008 u32 count, MSIxIndex;
2010 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2012 /* If we have already declared adapter dead, donot complete cmds */
2013 spin_lock_irqsave(&instance->hba_lock, flags);
2014 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2015 spin_unlock_irqrestore(&instance->hba_lock, flags);
2016 return;
2018 spin_unlock_irqrestore(&instance->hba_lock, flags);
2020 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2021 complete_cmd_fusion(instance, MSIxIndex);
2025 * megasas_isr_fusion - isr entry point
2027 irqreturn_t megasas_isr_fusion(int irq, void *devp)
2029 struct megasas_irq_context *irq_context = devp;
2030 struct megasas_instance *instance = irq_context->instance;
2031 u32 mfiStatus, fw_state;
2033 if (instance->mask_interrupts)
2034 return IRQ_NONE;
2036 if (!instance->msix_vectors) {
2037 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2038 if (!mfiStatus)
2039 return IRQ_NONE;
2042 /* If we are resetting, bail */
2043 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2044 instance->instancet->clear_intr(instance->reg_set);
2045 return IRQ_HANDLED;
2048 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2049 instance->instancet->clear_intr(instance->reg_set);
2050 /* If we didn't complete any commands, check for FW fault */
2051 fw_state = instance->instancet->read_fw_status_reg(
2052 instance->reg_set) & MFI_STATE_MASK;
2053 if (fw_state == MFI_STATE_FAULT) {
2054 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2055 "for scsi%d\n", instance->host->host_no);
2056 schedule_work(&instance->work_init);
2060 return IRQ_HANDLED;
2064 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2065 * @instance: Adapter soft state
2066 * mfi_cmd: megasas_cmd pointer
2070 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2071 struct megasas_cmd *mfi_cmd)
2073 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2074 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2075 struct megasas_cmd_fusion *cmd;
2076 struct fusion_context *fusion;
2077 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2079 cmd = megasas_get_cmd_fusion(instance);
2080 if (!cmd)
2081 return 1;
2083 /* Save the smid. To be used for returning the cmd */
2084 mfi_cmd->context.smid = cmd->index;
2086 cmd->sync_cmd_idx = mfi_cmd->index;
2089 * For cmds where the flag is set, store the flag and check
2090 * on completion. For cmds with this flag, don't call
2091 * megasas_complete_cmd
2094 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2095 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2097 fusion = instance->ctrl_context;
2098 io_req = cmd->io_request;
2100 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2101 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2102 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2103 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2104 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2105 sgl_ptr_end->Flags = 0;
2108 mpi25_ieee_chain =
2109 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2111 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2112 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2113 SGL) / 4;
2114 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2116 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
2118 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2119 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2121 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
2123 return 0;
2127 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2128 * @instance: Adapter soft state
2129 * @cmd: mfi cmd to build
2132 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2133 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2135 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2136 u16 index;
2138 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2139 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2140 return NULL;
2143 index = cmd->context.smid;
2145 req_desc = megasas_get_request_descriptor(instance, index - 1);
2147 if (!req_desc)
2148 return NULL;
2150 req_desc->Words = 0;
2151 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2152 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2154 req_desc->SCSIIO.SMID = cpu_to_le16(index);
2156 return req_desc;
2160 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2161 * @instance: Adapter soft state
2162 * @cmd: mfi cmd pointer
2165 void
2166 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2167 struct megasas_cmd *cmd)
2169 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2171 req_desc = build_mpt_cmd(instance, cmd);
2172 if (!req_desc) {
2173 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2174 return;
2176 instance->instancet->fire_cmd(instance, req_desc->u.low,
2177 req_desc->u.high, instance->reg_set);
2181 * megasas_release_fusion - Reverses the FW initialization
2182 * @intance: Adapter soft state
2184 void
2185 megasas_release_fusion(struct megasas_instance *instance)
2187 megasas_free_cmds(instance);
2188 megasas_free_cmds_fusion(instance);
2190 iounmap(instance->reg_set);
2192 pci_release_selected_regions(instance->pdev, instance->bar);
2196 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2197 * @regs: MFI register set
2199 static u32
2200 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2202 return readl(&(regs)->outbound_scratch_pad);
2206 * megasas_adp_reset_fusion - For controller reset
2207 * @regs: MFI register set
2209 static int
2210 megasas_adp_reset_fusion(struct megasas_instance *instance,
2211 struct megasas_register_set __iomem *regs)
2213 return 0;
2217 * megasas_check_reset_fusion - For controller reset check
2218 * @regs: MFI register set
2220 static int
2221 megasas_check_reset_fusion(struct megasas_instance *instance,
2222 struct megasas_register_set __iomem *regs)
2224 return 0;
2227 /* This function waits for outstanding commands on fusion to complete */
2228 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2229 int iotimeout, int *convert)
2231 int i, outstanding, retval = 0, hb_seconds_missed = 0;
2232 u32 fw_state;
2234 for (i = 0; i < resetwaittime; i++) {
2235 /* Check if firmware is in fault state */
2236 fw_state = instance->instancet->read_fw_status_reg(
2237 instance->reg_set) & MFI_STATE_MASK;
2238 if (fw_state == MFI_STATE_FAULT) {
2239 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2240 " will reset adapter scsi%d.\n",
2241 instance->host->host_no);
2242 retval = 1;
2243 goto out;
2245 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2246 if (instance->requestorId && !iotimeout) {
2247 retval = 1;
2248 goto out;
2251 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2252 if (instance->requestorId && iotimeout) {
2253 if (instance->hb_host_mem->HB.fwCounter !=
2254 instance->hb_host_mem->HB.driverCounter) {
2255 instance->hb_host_mem->HB.driverCounter =
2256 instance->hb_host_mem->HB.fwCounter;
2257 hb_seconds_missed = 0;
2258 } else {
2259 hb_seconds_missed++;
2260 if (hb_seconds_missed ==
2261 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2262 printk(KERN_WARNING "megasas: SR-IOV:"
2263 " Heartbeat never completed "
2264 " while polling during I/O "
2265 " timeout handling for "
2266 "scsi%d.\n",
2267 instance->host->host_no);
2268 *convert = 1;
2269 retval = 1;
2270 goto out;
2275 outstanding = atomic_read(&instance->fw_outstanding);
2276 if (!outstanding)
2277 goto out;
2279 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2280 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2281 "commands to complete for scsi%d\n", i,
2282 outstanding, instance->host->host_no);
2283 megasas_complete_cmd_dpc_fusion(
2284 (unsigned long)instance);
2286 msleep(1000);
2289 if (atomic_read(&instance->fw_outstanding)) {
2290 printk("megaraid_sas: pending commands remain after waiting, "
2291 "will reset adapter scsi%d.\n",
2292 instance->host->host_no);
2293 retval = 1;
2295 out:
2296 return retval;
2299 void megasas_reset_reply_desc(struct megasas_instance *instance)
2301 int i, count;
2302 struct fusion_context *fusion;
2303 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2305 fusion = instance->ctrl_context;
2306 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2307 for (i = 0 ; i < count ; i++)
2308 fusion->last_reply_idx[i] = 0;
2309 reply_desc = fusion->reply_frames_desc;
2310 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2311 reply_desc->Words = ULLONG_MAX;
2314 /* Check for a second path that is currently UP */
2315 int megasas_check_mpio_paths(struct megasas_instance *instance,
2316 struct scsi_cmnd *scmd)
2318 int i, j, retval = (DID_RESET << 16);
2320 if (instance->mpio && instance->requestorId) {
2321 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2322 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2323 if (megasas_mgmt_info.instance[i] &&
2324 (megasas_mgmt_info.instance[i] != instance) &&
2325 megasas_mgmt_info.instance[i]->mpio &&
2326 megasas_mgmt_info.instance[i]->requestorId
2328 (megasas_mgmt_info.instance[i]->ld_ids[j]
2329 == scmd->device->id)) {
2330 retval = (DID_NO_CONNECT << 16);
2331 goto out;
2334 out:
2335 return retval;
2338 /* Core fusion reset function */
2339 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2341 int retval = SUCCESS, i, j, retry = 0, convert = 0;
2342 struct megasas_instance *instance;
2343 struct megasas_cmd_fusion *cmd_fusion;
2344 struct fusion_context *fusion;
2345 struct megasas_cmd *cmd_mfi;
2346 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2347 u32 host_diag, abs_state, status_reg, reset_adapter;
2349 instance = (struct megasas_instance *)shost->hostdata;
2350 fusion = instance->ctrl_context;
2352 mutex_lock(&instance->reset_mutex);
2354 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2355 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2356 "returning FAILED for scsi%d.\n",
2357 instance->host->host_no);
2358 return FAILED;
2361 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2362 del_timer_sync(&instance->sriov_heartbeat_timer);
2363 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2364 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
2365 instance->instancet->disable_intr(instance);
2366 msleep(1000);
2368 /* First try waiting for commands to complete */
2369 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2370 &convert)) {
2371 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2372 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2373 "adapter scsi%d.\n", instance->host->host_no);
2374 if (convert)
2375 iotimeout = 0;
2377 /* Now return commands back to the OS */
2378 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2379 cmd_fusion = fusion->cmd_list[i];
2380 if (cmd_fusion->scmd) {
2381 scsi_dma_unmap(cmd_fusion->scmd);
2382 cmd_fusion->scmd->result =
2383 megasas_check_mpio_paths(instance,
2384 cmd_fusion->scmd);
2385 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2386 megasas_return_cmd_fusion(instance, cmd_fusion);
2387 atomic_dec(&instance->fw_outstanding);
2391 status_reg = instance->instancet->read_fw_status_reg(
2392 instance->reg_set);
2393 abs_state = status_reg & MFI_STATE_MASK;
2394 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2395 if (instance->disableOnlineCtrlReset ||
2396 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2397 /* Reset not supported, kill adapter */
2398 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2399 ", killing adapter scsi%d.\n",
2400 instance->host->host_no);
2401 megaraid_sas_kill_hba(instance);
2402 instance->skip_heartbeat_timer_del = 1;
2403 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2404 retval = FAILED;
2405 goto out;
2408 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2409 if (instance->requestorId && !iotimeout) {
2410 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2411 /* Look for a late HB update after VF settle time */
2412 if (abs_state == MFI_STATE_OPERATIONAL &&
2413 (instance->hb_host_mem->HB.fwCounter !=
2414 instance->hb_host_mem->HB.driverCounter)) {
2415 instance->hb_host_mem->HB.driverCounter =
2416 instance->hb_host_mem->HB.fwCounter;
2417 printk(KERN_WARNING "megasas: SR-IOV:"
2418 "Late FW heartbeat update for "
2419 "scsi%d.\n",
2420 instance->host->host_no);
2421 } else {
2422 /* In VF mode, first poll for FW ready */
2423 for (i = 0;
2424 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2425 i += 20) {
2426 status_reg =
2427 instance->instancet->
2428 read_fw_status_reg(
2429 instance->reg_set);
2430 abs_state = status_reg &
2431 MFI_STATE_MASK;
2432 if (abs_state == MFI_STATE_READY) {
2433 printk(KERN_WARNING "megasas"
2434 ": SR-IOV: FW was found"
2435 "to be in ready state "
2436 "for scsi%d.\n",
2437 instance->host->host_no);
2438 break;
2440 msleep(20);
2442 if (abs_state != MFI_STATE_READY) {
2443 printk(KERN_WARNING "megasas: SR-IOV: "
2444 "FW not in ready state after %d"
2445 " seconds for scsi%d, status_reg = "
2446 "0x%x.\n",
2447 MEGASAS_RESET_WAIT_TIME,
2448 instance->host->host_no,
2449 status_reg);
2450 megaraid_sas_kill_hba(instance);
2451 instance->skip_heartbeat_timer_del = 1;
2452 instance->adprecovery =
2453 MEGASAS_HW_CRITICAL_ERROR;
2454 retval = FAILED;
2455 goto out;
2460 /* Now try to reset the chip */
2461 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2462 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2463 &instance->reg_set->fusion_seq_offset);
2464 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2465 &instance->reg_set->fusion_seq_offset);
2466 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2467 &instance->reg_set->fusion_seq_offset);
2468 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2469 &instance->reg_set->fusion_seq_offset);
2470 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2471 &instance->reg_set->fusion_seq_offset);
2472 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2473 &instance->reg_set->fusion_seq_offset);
2474 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2475 &instance->reg_set->fusion_seq_offset);
2477 /* Check that the diag write enable (DRWE) bit is on */
2478 host_diag = readl(&instance->reg_set->fusion_host_diag);
2479 retry = 0;
2480 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2481 msleep(100);
2482 host_diag =
2483 readl(&instance->reg_set->fusion_host_diag);
2484 if (retry++ == 100) {
2485 printk(KERN_WARNING "megaraid_sas: "
2486 "Host diag unlock failed! "
2487 "for scsi%d\n",
2488 instance->host->host_no);
2489 break;
2492 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2493 continue;
2495 /* Send chip reset command */
2496 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2497 &instance->reg_set->fusion_host_diag);
2498 msleep(3000);
2500 /* Make sure reset adapter bit is cleared */
2501 host_diag = readl(&instance->reg_set->fusion_host_diag);
2502 retry = 0;
2503 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2504 msleep(100);
2505 host_diag =
2506 readl(&instance->reg_set->fusion_host_diag);
2507 if (retry++ == 1000) {
2508 printk(KERN_WARNING "megaraid_sas: "
2509 "Diag reset adapter never "
2510 "cleared for scsi%d!\n",
2511 instance->host->host_no);
2512 break;
2515 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2516 continue;
2518 abs_state =
2519 instance->instancet->read_fw_status_reg(
2520 instance->reg_set) & MFI_STATE_MASK;
2521 retry = 0;
2523 while ((abs_state <= MFI_STATE_FW_INIT) &&
2524 (retry++ < 1000)) {
2525 msleep(100);
2526 abs_state =
2527 instance->instancet->read_fw_status_reg(
2528 instance->reg_set) & MFI_STATE_MASK;
2530 if (abs_state <= MFI_STATE_FW_INIT) {
2531 printk(KERN_WARNING "megaraid_sas: firmware "
2532 "state < MFI_STATE_FW_INIT, state = "
2533 "0x%x for scsi%d\n", abs_state,
2534 instance->host->host_no);
2535 continue;
2538 /* Wait for FW to become ready */
2539 if (megasas_transition_to_ready(instance, 1)) {
2540 printk(KERN_WARNING "megaraid_sas: Failed to "
2541 "transition controller to ready "
2542 "for scsi%d.\n",
2543 instance->host->host_no);
2544 continue;
2547 megasas_reset_reply_desc(instance);
2548 if (megasas_ioc_init_fusion(instance)) {
2549 printk(KERN_WARNING "megaraid_sas: "
2550 "megasas_ioc_init_fusion() failed!"
2551 " for scsi%d\n",
2552 instance->host->host_no);
2553 continue;
2556 /* Re-fire management commands */
2557 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2558 cmd_fusion = fusion->cmd_list[j];
2559 if (cmd_fusion->sync_cmd_idx !=
2560 (u32)ULONG_MAX) {
2561 cmd_mfi =
2562 instance->
2563 cmd_list[cmd_fusion->sync_cmd_idx];
2564 if (cmd_mfi->frame->dcmd.opcode ==
2565 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
2566 megasas_return_cmd(instance,
2567 cmd_mfi);
2568 megasas_return_cmd_fusion(
2569 instance, cmd_fusion);
2570 } else {
2571 req_desc =
2572 megasas_get_request_descriptor(
2573 instance,
2574 cmd_mfi->context.smid
2575 -1);
2576 if (!req_desc) {
2577 printk(KERN_WARNING
2578 "req_desc NULL"
2579 " for scsi%d\n",
2580 instance->host->host_no);
2581 /* Return leaked MPT
2582 frame */
2583 megasas_return_cmd_fusion(instance, cmd_fusion);
2584 } else {
2585 instance->instancet->
2586 fire_cmd(instance,
2587 req_desc->
2588 u.low,
2589 req_desc->
2590 u.high,
2591 instance->
2592 reg_set);
2598 clear_bit(MEGASAS_FUSION_IN_RESET,
2599 &instance->reset_flags);
2600 instance->instancet->enable_intr(instance);
2601 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2603 /* Reset load balance info */
2604 memset(fusion->load_balance_info, 0,
2605 sizeof(struct LD_LOAD_BALANCE_INFO)
2606 *MAX_LOGICAL_DRIVES);
2608 if (!megasas_get_map_info(instance))
2609 megasas_sync_map_info(instance);
2611 /* Restart SR-IOV heartbeat */
2612 if (instance->requestorId) {
2613 if (!megasas_sriov_start_heartbeat(instance, 0))
2614 megasas_start_timer(instance,
2615 &instance->sriov_heartbeat_timer,
2616 megasas_sriov_heartbeat_handler,
2617 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2618 else
2619 instance->skip_heartbeat_timer_del = 1;
2622 /* Adapter reset completed successfully */
2623 printk(KERN_WARNING "megaraid_sas: Reset "
2624 "successful for scsi%d.\n",
2625 instance->host->host_no);
2626 retval = SUCCESS;
2627 goto out;
2629 /* Reset failed, kill the adapter */
2630 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2631 "adapter scsi%d.\n", instance->host->host_no);
2632 megaraid_sas_kill_hba(instance);
2633 instance->skip_heartbeat_timer_del = 1;
2634 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2635 retval = FAILED;
2636 } else {
2637 /* For VF: Restart HB timer if we didn't OCR */
2638 if (instance->requestorId) {
2639 megasas_start_timer(instance,
2640 &instance->sriov_heartbeat_timer,
2641 megasas_sriov_heartbeat_handler,
2642 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2644 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2645 instance->instancet->enable_intr(instance);
2646 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2648 out:
2649 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2650 mutex_unlock(&instance->reset_mutex);
2651 return retval;
2654 /* Fusion OCR work queue */
2655 void megasas_fusion_ocr_wq(struct work_struct *work)
2657 struct megasas_instance *instance =
2658 container_of(work, struct megasas_instance, work_init);
2660 megasas_reset_fusion(instance->host, 0);
2663 struct megasas_instance_template megasas_instance_template_fusion = {
2664 .fire_cmd = megasas_fire_cmd_fusion,
2665 .enable_intr = megasas_enable_intr_fusion,
2666 .disable_intr = megasas_disable_intr_fusion,
2667 .clear_intr = megasas_clear_intr_fusion,
2668 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2669 .adp_reset = megasas_adp_reset_fusion,
2670 .check_reset = megasas_check_reset_fusion,
2671 .service_isr = megasas_isr_fusion,
2672 .tasklet = megasas_complete_cmd_dpc_fusion,
2673 .init_adapter = megasas_init_adapter_fusion,
2674 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2675 .issue_dcmd = megasas_issue_dcmd_fusion,