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
24 * Adam Radford <linuxraid@lsi.com>
26 * Send feedback to: <megaraidlinux@lsi.com>
28 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
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>
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
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
);
65 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
);
68 megasas_return_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
);
69 int megasas_alloc_cmds(struct megasas_instance
*instance
);
71 megasas_clear_intr_fusion(struct megasas_register_set __iomem
*regs
);
73 megasas_issue_polled(struct megasas_instance
*instance
,
74 struct megasas_cmd
*cmd
);
76 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
);
78 u16
get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO
*lbInfo
,
79 struct IO_REQUEST_INFO
*in_info
);
80 int megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
);
81 void megaraid_sas_kill_hba(struct megasas_instance
*instance
);
83 extern u32 megasas_dbg_lvl
;
84 extern int resetwaittime
;
87 * megasas_enable_intr_fusion - Enables interrupts
88 * @regs: MFI register set
91 megasas_enable_intr_fusion(struct megasas_instance
*instance
)
93 struct megasas_register_set __iomem
*regs
;
94 regs
= instance
->reg_set
;
95 /* For Thunderbolt/Invader also clear intr on enable */
96 writel(~0, ®s
->outbound_intr_status
);
97 readl(®s
->outbound_intr_status
);
99 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
101 /* Dummy readl to force pci flush */
102 readl(®s
->outbound_intr_mask
);
103 instance
->mask_interrupts
= 0;
107 * megasas_disable_intr_fusion - Disables interrupt
108 * @regs: MFI register set
111 megasas_disable_intr_fusion(struct megasas_instance
*instance
)
113 u32 mask
= 0xFFFFFFFF;
115 struct megasas_register_set __iomem
*regs
;
116 regs
= instance
->reg_set
;
117 instance
->mask_interrupts
= 1;
119 writel(mask
, ®s
->outbound_intr_mask
);
120 /* Dummy readl to force pci flush */
121 status
= readl(®s
->outbound_intr_mask
);
125 megasas_clear_intr_fusion(struct megasas_register_set __iomem
*regs
)
129 * Check if it is our interrupt
131 status
= readl(®s
->outbound_intr_status
);
134 writel(status
, ®s
->outbound_intr_status
);
135 readl(®s
->outbound_intr_status
);
138 if (!(status
& MFI_FUSION_ENABLE_INTERRUPT_MASK
))
145 * megasas_get_cmd_fusion - Get a command from the free pool
146 * @instance: Adapter soft state
148 * Returns a free command from the pool
150 struct megasas_cmd_fusion
*megasas_get_cmd_fusion(struct megasas_instance
154 struct fusion_context
*fusion
=
155 (struct fusion_context
*)instance
->ctrl_context
;
156 struct megasas_cmd_fusion
*cmd
= NULL
;
158 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
160 if (!list_empty(&fusion
->cmd_pool
)) {
161 cmd
= list_entry((&fusion
->cmd_pool
)->next
,
162 struct megasas_cmd_fusion
, list
);
163 list_del_init(&cmd
->list
);
165 printk(KERN_ERR
"megasas: Command pool (fusion) empty!\n");
168 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
173 * megasas_return_cmd_fusion - Return a cmd to free command pool
174 * @instance: Adapter soft state
175 * @cmd: Command packet to be returned to free command pool
178 megasas_return_cmd_fusion(struct megasas_instance
*instance
,
179 struct megasas_cmd_fusion
*cmd
)
182 struct fusion_context
*fusion
=
183 (struct fusion_context
*)instance
->ctrl_context
;
185 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
188 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
;
189 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
191 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
195 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
196 * @instance: Adapter soft state
198 static void megasas_teardown_frame_pool_fusion(
199 struct megasas_instance
*instance
)
202 struct fusion_context
*fusion
= instance
->ctrl_context
;
204 u16 max_cmd
= instance
->max_fw_cmds
;
206 struct megasas_cmd_fusion
*cmd
;
208 if (!fusion
->sg_dma_pool
|| !fusion
->sense_dma_pool
) {
209 printk(KERN_ERR
"megasas: dma pool is null. SG Pool %p, "
210 "sense pool : %p\n", fusion
->sg_dma_pool
,
211 fusion
->sense_dma_pool
);
216 * Return all frames to pool
218 for (i
= 0; i
< max_cmd
; i
++) {
220 cmd
= fusion
->cmd_list
[i
];
223 pci_pool_free(fusion
->sg_dma_pool
, cmd
->sg_frame
,
224 cmd
->sg_frame_phys_addr
);
227 pci_pool_free(fusion
->sense_dma_pool
, cmd
->sense
,
228 cmd
->sense_phys_addr
);
232 * Now destroy the pool itself
234 pci_pool_destroy(fusion
->sg_dma_pool
);
235 pci_pool_destroy(fusion
->sense_dma_pool
);
237 fusion
->sg_dma_pool
= NULL
;
238 fusion
->sense_dma_pool
= NULL
;
242 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
243 * @instance: Adapter soft state
246 megasas_free_cmds_fusion(struct megasas_instance
*instance
)
249 struct fusion_context
*fusion
= instance
->ctrl_context
;
251 u32 max_cmds
, req_sz
, reply_sz
, io_frames_sz
;
254 req_sz
= fusion
->request_alloc_sz
;
255 reply_sz
= fusion
->reply_alloc_sz
;
256 io_frames_sz
= fusion
->io_frames_alloc_sz
;
258 max_cmds
= instance
->max_fw_cmds
;
260 /* Free descriptors and request Frames memory */
261 if (fusion
->req_frames_desc
)
262 dma_free_coherent(&instance
->pdev
->dev
, req_sz
,
263 fusion
->req_frames_desc
,
264 fusion
->req_frames_desc_phys
);
266 if (fusion
->reply_frames_desc
) {
267 pci_pool_free(fusion
->reply_frames_desc_pool
,
268 fusion
->reply_frames_desc
,
269 fusion
->reply_frames_desc_phys
);
270 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
273 if (fusion
->io_request_frames
) {
274 pci_pool_free(fusion
->io_request_frames_pool
,
275 fusion
->io_request_frames
,
276 fusion
->io_request_frames_phys
);
277 pci_pool_destroy(fusion
->io_request_frames_pool
);
280 /* Free the Fusion frame pool */
281 megasas_teardown_frame_pool_fusion(instance
);
283 /* Free all the commands in the cmd_list */
284 for (i
= 0; i
< max_cmds
; i
++)
285 kfree(fusion
->cmd_list
[i
]);
287 /* Free the cmd_list buffer itself */
288 kfree(fusion
->cmd_list
);
289 fusion
->cmd_list
= NULL
;
291 INIT_LIST_HEAD(&fusion
->cmd_pool
);
295 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
296 * @instance: Adapter soft state
299 static int megasas_create_frame_pool_fusion(struct megasas_instance
*instance
)
303 struct fusion_context
*fusion
;
304 struct megasas_cmd_fusion
*cmd
;
305 u32 total_sz_chain_frame
;
307 fusion
= instance
->ctrl_context
;
308 max_cmd
= instance
->max_fw_cmds
;
310 total_sz_chain_frame
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
313 * Use DMA pool facility provided by PCI layer
316 fusion
->sg_dma_pool
= pci_pool_create("megasas sg pool fusion",
318 total_sz_chain_frame
, 4,
320 if (!fusion
->sg_dma_pool
) {
321 printk(KERN_DEBUG
"megasas: failed to setup request pool "
325 fusion
->sense_dma_pool
= pci_pool_create("megasas sense pool fusion",
327 SCSI_SENSE_BUFFERSIZE
, 64, 0);
329 if (!fusion
->sense_dma_pool
) {
330 printk(KERN_DEBUG
"megasas: failed to setup sense pool "
332 pci_pool_destroy(fusion
->sg_dma_pool
);
333 fusion
->sg_dma_pool
= NULL
;
338 * Allocate and attach a frame to each of the commands in cmd_list
340 for (i
= 0; i
< max_cmd
; i
++) {
342 cmd
= fusion
->cmd_list
[i
];
344 cmd
->sg_frame
= pci_pool_alloc(fusion
->sg_dma_pool
,
346 &cmd
->sg_frame_phys_addr
);
348 cmd
->sense
= pci_pool_alloc(fusion
->sense_dma_pool
,
349 GFP_KERNEL
, &cmd
->sense_phys_addr
);
351 * megasas_teardown_frame_pool_fusion() takes care of freeing
352 * whatever has been allocated
354 if (!cmd
->sg_frame
|| !cmd
->sense
) {
355 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed\n");
356 megasas_teardown_frame_pool_fusion(instance
);
364 * megasas_alloc_cmds_fusion - Allocates the command packets
365 * @instance: Adapter soft state
368 * Each frame has a 32-bit field called context. This context is used to get
369 * back the megasas_cmd_fusion from the frame when a frame gets completed
370 * In this driver, the 32 bit values are the indices into an array cmd_list.
371 * This array is used only to look up the megasas_cmd_fusion given the context.
372 * The free commands themselves are maintained in a linked list called cmd_pool.
374 * cmds are formed in the io_request and sg_frame members of the
375 * megasas_cmd_fusion. The context field is used to get a request descriptor
376 * and is used as SMID of the cmd.
377 * SMID value range is from 1 to max_fw_cmds.
380 megasas_alloc_cmds_fusion(struct megasas_instance
*instance
)
383 u32 max_cmd
, io_frames_sz
;
384 struct fusion_context
*fusion
;
385 struct megasas_cmd_fusion
*cmd
;
386 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
388 dma_addr_t io_req_base_phys
;
391 fusion
= instance
->ctrl_context
;
393 max_cmd
= instance
->max_fw_cmds
;
395 fusion
->req_frames_desc
=
396 dma_alloc_coherent(&instance
->pdev
->dev
,
397 fusion
->request_alloc_sz
,
398 &fusion
->req_frames_desc_phys
, GFP_KERNEL
);
400 if (!fusion
->req_frames_desc
) {
401 printk(KERN_ERR
"megasas; Could not allocate memory for "
406 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
407 fusion
->reply_frames_desc_pool
=
408 pci_pool_create("reply_frames pool", instance
->pdev
,
409 fusion
->reply_alloc_sz
* count
, 16, 0);
411 if (!fusion
->reply_frames_desc_pool
) {
412 printk(KERN_ERR
"megasas; Could not allocate memory for "
413 "reply_frame pool\n");
414 goto fail_reply_desc
;
417 fusion
->reply_frames_desc
=
418 pci_pool_alloc(fusion
->reply_frames_desc_pool
, GFP_KERNEL
,
419 &fusion
->reply_frames_desc_phys
);
420 if (!fusion
->reply_frames_desc
) {
421 printk(KERN_ERR
"megasas; Could not allocate memory for "
422 "reply_frame pool\n");
423 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
424 goto fail_reply_desc
;
427 reply_desc
= fusion
->reply_frames_desc
;
428 for (i
= 0; i
< fusion
->reply_q_depth
* count
; i
++, reply_desc
++)
429 reply_desc
->Words
= ULLONG_MAX
;
431 io_frames_sz
= fusion
->io_frames_alloc_sz
;
433 fusion
->io_request_frames_pool
=
434 pci_pool_create("io_request_frames pool", instance
->pdev
,
435 fusion
->io_frames_alloc_sz
, 16, 0);
437 if (!fusion
->io_request_frames_pool
) {
438 printk(KERN_ERR
"megasas: Could not allocate memory for "
439 "io_request_frame pool\n");
443 fusion
->io_request_frames
=
444 pci_pool_alloc(fusion
->io_request_frames_pool
, GFP_KERNEL
,
445 &fusion
->io_request_frames_phys
);
446 if (!fusion
->io_request_frames
) {
447 printk(KERN_ERR
"megasas: Could not allocate memory for "
448 "io_request_frames frames\n");
449 pci_pool_destroy(fusion
->io_request_frames_pool
);
454 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
455 * Allocate the dynamic array first and then allocate individual
458 fusion
->cmd_list
= kzalloc(sizeof(struct megasas_cmd_fusion
*)
459 * max_cmd
, GFP_KERNEL
);
461 if (!fusion
->cmd_list
) {
462 printk(KERN_DEBUG
"megasas: out of memory. Could not alloc "
463 "memory for cmd_list_fusion\n");
467 max_cmd
= instance
->max_fw_cmds
;
468 for (i
= 0; i
< max_cmd
; i
++) {
469 fusion
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd_fusion
),
471 if (!fusion
->cmd_list
[i
]) {
472 printk(KERN_ERR
"Could not alloc cmd list fusion\n");
474 for (j
= 0; j
< i
; j
++)
475 kfree(fusion
->cmd_list
[j
]);
477 kfree(fusion
->cmd_list
);
478 fusion
->cmd_list
= NULL
;
483 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
484 io_req_base
= fusion
->io_request_frames
+
485 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
486 io_req_base_phys
= fusion
->io_request_frames_phys
+
487 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
490 * Add all the commands to command pool (fusion->cmd_pool)
493 /* SMID 0 is reserved. Set SMID/index from 1 */
494 for (i
= 0; i
< max_cmd
; i
++) {
495 cmd
= fusion
->cmd_list
[i
];
496 offset
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
* i
;
497 memset(cmd
, 0, sizeof(struct megasas_cmd_fusion
));
500 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
; /* Set to Invalid */
501 cmd
->instance
= instance
;
503 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
504 (io_req_base
+ offset
);
505 memset(cmd
->io_request
, 0,
506 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
));
507 cmd
->io_request_phys_addr
= io_req_base_phys
+ offset
;
509 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
513 * Create a frame pool and assign one frame to each cmd
515 if (megasas_create_frame_pool_fusion(instance
)) {
516 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
517 megasas_free_cmds_fusion(instance
);
524 pci_pool_free(fusion
->io_request_frames_pool
, fusion
->io_request_frames
,
525 fusion
->io_request_frames_phys
);
526 pci_pool_destroy(fusion
->io_request_frames_pool
);
528 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
529 fusion
->reply_frames_desc
,
530 fusion
->reply_frames_desc_phys
);
531 pci_pool_free(fusion
->reply_frames_desc_pool
,
532 fusion
->reply_frames_desc
,
533 fusion
->reply_frames_desc_phys
);
534 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
537 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
538 fusion
->req_frames_desc
,
539 fusion
->req_frames_desc_phys
);
545 * wait_and_poll - Issues a polling command
546 * @instance: Adapter soft state
547 * @cmd: Command packet to be issued
549 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
552 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
555 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
557 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
560 * Wait for cmd_status to change
562 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
+= 20) {
567 if (frame_hdr
->cmd_status
== 0xff)
574 * megasas_ioc_init_fusion - Initializes the FW
575 * @instance: Adapter soft state
577 * Issues the IOC Init cmd
580 megasas_ioc_init_fusion(struct megasas_instance
*instance
)
582 struct megasas_init_frame
*init_frame
;
583 struct MPI2_IOC_INIT_REQUEST
*IOCInitMessage
;
584 dma_addr_t ioc_init_handle
;
585 struct megasas_cmd
*cmd
;
587 struct fusion_context
*fusion
;
588 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
590 struct megasas_header
*frame_hdr
;
592 fusion
= instance
->ctrl_context
;
594 cmd
= megasas_get_cmd(instance
);
597 printk(KERN_ERR
"Could not allocate cmd for INIT Frame\n");
603 dma_alloc_coherent(&instance
->pdev
->dev
,
604 sizeof(struct MPI2_IOC_INIT_REQUEST
),
605 &ioc_init_handle
, GFP_KERNEL
);
607 if (!IOCInitMessage
) {
608 printk(KERN_ERR
"Could not allocate memory for "
614 memset(IOCInitMessage
, 0, sizeof(struct MPI2_IOC_INIT_REQUEST
));
616 IOCInitMessage
->Function
= MPI2_FUNCTION_IOC_INIT
;
617 IOCInitMessage
->WhoInit
= MPI2_WHOINIT_HOST_DRIVER
;
618 IOCInitMessage
->MsgVersion
= cpu_to_le16(MPI2_VERSION
);
619 IOCInitMessage
->HeaderVersion
= cpu_to_le16(MPI2_HEADER_VERSION
);
620 IOCInitMessage
->SystemRequestFrameSize
= cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
/ 4);
622 IOCInitMessage
->ReplyDescriptorPostQueueDepth
= cpu_to_le16(fusion
->reply_q_depth
);
623 IOCInitMessage
->ReplyDescriptorPostQueueAddress
= cpu_to_le64(fusion
->reply_frames_desc_phys
);
624 IOCInitMessage
->SystemRequestFrameBaseAddress
= cpu_to_le64(fusion
->io_request_frames_phys
);
625 IOCInitMessage
->HostMSIxVectors
= instance
->msix_vectors
;
626 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
627 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
629 frame_hdr
= &cmd
->frame
->hdr
;
630 frame_hdr
->cmd_status
= 0xFF;
631 frame_hdr
->flags
|= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
);
633 init_frame
->cmd
= MFI_CMD_INIT
;
634 init_frame
->cmd_status
= 0xFF;
636 /* driver support Extended MSIX */
637 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
638 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
639 init_frame
->driver_operations
.
640 mfi_capabilities
.support_additional_msix
= 1;
641 /* driver supports HA / Remote LUN over Fast Path interface */
642 init_frame
->driver_operations
.mfi_capabilities
.support_fp_remote_lun
644 /* Convert capability to LE32 */
645 cpu_to_le32s((u32
*)&init_frame
->driver_operations
.mfi_capabilities
);
647 init_frame
->queue_info_new_phys_addr_lo
= cpu_to_le32((u32
)ioc_init_handle
);
648 init_frame
->data_xfer_len
= cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST
));
651 (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)fusion
->req_frames_desc
;
654 req_desc
->MFAIo
.RequestFlags
=
655 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA
<<
656 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
657 cpu_to_le32s((u32
*)&req_desc
->MFAIo
);
658 req_desc
->Words
|= cpu_to_le64(cmd
->frame_phys_addr
);
661 * disable the intr before firing the init frame
663 instance
->instancet
->disable_intr(instance
);
665 for (i
= 0; i
< (10 * 1000); i
+= 20) {
666 if (readl(&instance
->reg_set
->doorbell
) & 1)
672 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
673 req_desc
->u
.high
, instance
->reg_set
);
675 wait_and_poll(instance
, cmd
);
677 frame_hdr
= &cmd
->frame
->hdr
;
678 if (frame_hdr
->cmd_status
!= 0) {
682 printk(KERN_ERR
"megasas:IOC Init cmd success\n");
687 megasas_return_cmd(instance
, cmd
);
689 dma_free_coherent(&instance
->pdev
->dev
,
690 sizeof(struct MPI2_IOC_INIT_REQUEST
),
691 IOCInitMessage
, ioc_init_handle
);
697 * megasas_get_ld_map_info - Returns FW's ld_map structure
698 * @instance: Adapter soft state
699 * @pend: Pend the command or not
700 * Issues an internal command (DCMD) to get the FW's controller PD
701 * list structure. This information is mainly used to find out SYSTEM
702 * supported by the FW.
705 megasas_get_ld_map_info(struct megasas_instance
*instance
)
708 struct megasas_cmd
*cmd
;
709 struct megasas_dcmd_frame
*dcmd
;
710 struct MR_FW_RAID_MAP_ALL
*ci
;
713 struct fusion_context
*fusion
;
715 cmd
= megasas_get_cmd(instance
);
718 printk(KERN_DEBUG
"megasas: Failed to get cmd for map info.\n");
722 fusion
= instance
->ctrl_context
;
725 megasas_return_cmd(instance
, cmd
);
729 dcmd
= &cmd
->frame
->dcmd
;
731 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
732 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
734 ci
= fusion
->ld_map
[(instance
->map_id
& 1)];
735 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
& 1)];
738 printk(KERN_DEBUG
"Failed to alloc mem for ld_map_info\n");
739 megasas_return_cmd(instance
, cmd
);
743 memset(ci
, 0, sizeof(*ci
));
744 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
746 dcmd
->cmd
= MFI_CMD_DCMD
;
747 dcmd
->cmd_status
= 0xFF;
749 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
752 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
753 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
754 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
755 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(size_map_info
);
757 if (!megasas_issue_polled(instance
, cmd
))
760 printk(KERN_ERR
"megasas: Get LD Map Info Failed\n");
764 megasas_return_cmd(instance
, cmd
);
770 megasas_get_map_info(struct megasas_instance
*instance
)
772 struct fusion_context
*fusion
= instance
->ctrl_context
;
774 fusion
->fast_path_io
= 0;
775 if (!megasas_get_ld_map_info(instance
)) {
776 if (MR_ValidateMapInfo(instance
)) {
777 fusion
->fast_path_io
= 1;
785 * megasas_sync_map_info - Returns FW's ld_map structure
786 * @instance: Adapter soft state
788 * Issues an internal command (DCMD) to get the FW's controller PD
789 * list structure. This information is mainly used to find out SYSTEM
790 * supported by the FW.
793 megasas_sync_map_info(struct megasas_instance
*instance
)
796 struct megasas_cmd
*cmd
;
797 struct megasas_dcmd_frame
*dcmd
;
798 u32 size_sync_info
, num_lds
;
799 struct fusion_context
*fusion
;
800 struct MR_LD_TARGET_SYNC
*ci
= NULL
;
801 struct MR_FW_RAID_MAP_ALL
*map
;
802 struct MR_LD_RAID
*raid
;
803 struct MR_LD_TARGET_SYNC
*ld_sync
;
807 cmd
= megasas_get_cmd(instance
);
810 printk(KERN_DEBUG
"megasas: Failed to get cmd for sync"
815 fusion
= instance
->ctrl_context
;
818 megasas_return_cmd(instance
, cmd
);
822 map
= fusion
->ld_map
[instance
->map_id
& 1];
824 num_lds
= le32_to_cpu(map
->raidMap
.ldCount
);
826 dcmd
= &cmd
->frame
->dcmd
;
828 size_sync_info
= sizeof(struct MR_LD_TARGET_SYNC
) *num_lds
;
830 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
832 ci
= (struct MR_LD_TARGET_SYNC
*)
833 fusion
->ld_map
[(instance
->map_id
- 1) & 1];
834 memset(ci
, 0, sizeof(struct MR_FW_RAID_MAP_ALL
));
836 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
- 1) & 1];
838 ld_sync
= (struct MR_LD_TARGET_SYNC
*)ci
;
840 for (i
= 0; i
< num_lds
; i
++, ld_sync
++) {
841 raid
= MR_LdRaidGet(i
, map
);
842 ld_sync
->targetId
= MR_GetLDTgtId(i
, map
);
843 ld_sync
->seqNum
= raid
->seqNum
;
846 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
847 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
849 dcmd
->cmd
= MFI_CMD_DCMD
;
850 dcmd
->cmd_status
= 0xFF;
852 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_WRITE
);
855 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
856 dcmd
->mbox
.b
[0] = num_lds
;
857 dcmd
->mbox
.b
[1] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
858 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
859 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
860 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(size_map_info
);
862 instance
->map_update_cmd
= cmd
;
864 instance
->instancet
->issue_dcmd(instance
, cmd
);
870 * meagasas_display_intel_branding - Display branding string
871 * @instance: per adapter object
876 megasas_display_intel_branding(struct megasas_instance
*instance
)
878 if (instance
->pdev
->subsystem_vendor
!= PCI_VENDOR_ID_INTEL
)
881 switch (instance
->pdev
->device
) {
882 case PCI_DEVICE_ID_LSI_INVADER
:
883 switch (instance
->pdev
->subsystem_device
) {
884 case MEGARAID_INTEL_RS3DC080_SSDID
:
885 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
886 instance
->host
->host_no
,
887 MEGARAID_INTEL_RS3DC080_BRANDING
);
889 case MEGARAID_INTEL_RS3DC040_SSDID
:
890 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
891 instance
->host
->host_no
,
892 MEGARAID_INTEL_RS3DC040_BRANDING
);
894 case MEGARAID_INTEL_RS3SC008_SSDID
:
895 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
896 instance
->host
->host_no
,
897 MEGARAID_INTEL_RS3SC008_BRANDING
);
899 case MEGARAID_INTEL_RS3MC044_SSDID
:
900 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
901 instance
->host
->host_no
,
902 MEGARAID_INTEL_RS3MC044_BRANDING
);
908 case PCI_DEVICE_ID_LSI_FURY
:
909 switch (instance
->pdev
->subsystem_device
) {
910 case MEGARAID_INTEL_RS3WC080_SSDID
:
911 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
912 instance
->host
->host_no
,
913 MEGARAID_INTEL_RS3WC080_BRANDING
);
915 case MEGARAID_INTEL_RS3WC040_SSDID
:
916 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
917 instance
->host
->host_no
,
918 MEGARAID_INTEL_RS3WC040_BRANDING
);
930 * megasas_init_adapter_fusion - Initializes the FW
931 * @instance: Adapter soft state
933 * This is the main function for initializing firmware.
936 megasas_init_adapter_fusion(struct megasas_instance
*instance
)
938 struct megasas_register_set __iomem
*reg_set
;
939 struct fusion_context
*fusion
;
943 fusion
= instance
->ctrl_context
;
945 reg_set
= instance
->reg_set
;
948 * Get various operational parameters from status register
950 instance
->max_fw_cmds
=
951 instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
952 instance
->max_fw_cmds
= min(instance
->max_fw_cmds
, (u16
)1008);
955 * Reduce the max supported cmds by 1. This is to ensure that the
956 * reply_q_sz (1 more than the max cmd that driver may send)
957 * does not exceed max cmds that the FW can support
959 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
960 /* Only internal cmds (DCMD) need to have MFI frames */
961 instance
->max_mfi_cmds
= MEGASAS_INT_CMDS
;
963 max_cmd
= instance
->max_fw_cmds
;
965 fusion
->reply_q_depth
= ((max_cmd
+ 1 + 15)/16)*16;
967 fusion
->request_alloc_sz
=
968 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *max_cmd
;
969 fusion
->reply_alloc_sz
= sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)
970 *(fusion
->reply_q_depth
);
971 fusion
->io_frames_alloc_sz
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
+
972 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
*
973 (max_cmd
+ 1)); /* Extra 1 for SMID 0 */
975 fusion
->max_sge_in_main_msg
=
976 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
977 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
))/16;
979 fusion
->max_sge_in_chain
=
980 MEGASAS_MAX_SZ_CHAIN_FRAME
/ sizeof(union MPI2_SGE_IO_UNION
);
982 instance
->max_num_sge
= fusion
->max_sge_in_main_msg
+
983 fusion
->max_sge_in_chain
- 2;
985 /* Used for pass thru MFI frame (DCMD) */
986 fusion
->chain_offset_mfi_pthru
=
987 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
)/16;
989 fusion
->chain_offset_io_request
=
990 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
991 sizeof(union MPI2_SGE_IO_UNION
))/16;
993 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
994 for (i
= 0 ; i
< count
; i
++)
995 fusion
->last_reply_idx
[i
] = 0;
998 * Allocate memory for descriptors
999 * Create a pool of commands
1001 if (megasas_alloc_cmds(instance
))
1002 goto fail_alloc_mfi_cmds
;
1003 if (megasas_alloc_cmds_fusion(instance
))
1004 goto fail_alloc_cmds
;
1006 if (megasas_ioc_init_fusion(instance
))
1009 megasas_display_intel_branding(instance
);
1011 instance
->flag_ieee
= 1;
1013 fusion
->map_sz
= sizeof(struct MR_FW_RAID_MAP
) +
1014 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
1016 fusion
->fast_path_io
= 0;
1018 for (i
= 0; i
< 2; i
++) {
1019 fusion
->ld_map
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
1021 &fusion
->ld_map_phys
[i
],
1023 if (!fusion
->ld_map
[i
]) {
1024 printk(KERN_ERR
"megasas: Could not allocate memory "
1030 if (!megasas_get_map_info(instance
))
1031 megasas_sync_map_info(instance
);
1037 dma_free_coherent(&instance
->pdev
->dev
, fusion
->map_sz
,
1038 fusion
->ld_map
[0], fusion
->ld_map_phys
[0]);
1040 megasas_free_cmds_fusion(instance
);
1042 megasas_free_cmds(instance
);
1043 fail_alloc_mfi_cmds
:
1048 * megasas_fire_cmd_fusion - Sends command to the FW
1049 * @frame_phys_addr : Physical address of cmd
1050 * @frame_count : Number of frames for the command
1051 * @regs : MFI register set
1054 megasas_fire_cmd_fusion(struct megasas_instance
*instance
,
1055 dma_addr_t req_desc_lo
,
1057 struct megasas_register_set __iomem
*regs
)
1059 unsigned long flags
;
1061 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1063 writel(le32_to_cpu(req_desc_lo
), &(regs
)->inbound_low_queue_port
);
1064 writel(le32_to_cpu(req_desc_hi
), &(regs
)->inbound_high_queue_port
);
1065 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1069 * map_cmd_status - Maps FW cmd status to OS cmd status
1070 * @cmd : Pointer to cmd
1071 * @status : status of cmd returned by FW
1072 * @ext_status : ext status of cmd returned by FW
1076 map_cmd_status(struct megasas_cmd_fusion
*cmd
, u8 status
, u8 ext_status
)
1082 cmd
->scmd
->result
= DID_OK
<< 16;
1085 case MFI_STAT_SCSI_IO_FAILED
:
1086 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1087 cmd
->scmd
->result
= (DID_ERROR
<< 16) | ext_status
;
1090 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1092 cmd
->scmd
->result
= (DID_OK
<< 16) | ext_status
;
1093 if (ext_status
== SAM_STAT_CHECK_CONDITION
) {
1094 memset(cmd
->scmd
->sense_buffer
, 0,
1095 SCSI_SENSE_BUFFERSIZE
);
1096 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1097 SCSI_SENSE_BUFFERSIZE
);
1098 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1102 case MFI_STAT_LD_OFFLINE
:
1103 case MFI_STAT_DEVICE_NOT_FOUND
:
1104 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1106 case MFI_STAT_CONFIG_SEQ_MISMATCH
:
1107 cmd
->scmd
->result
= DID_IMM_RETRY
<< 16;
1110 printk(KERN_DEBUG
"megasas: FW status %#x\n", status
);
1111 cmd
->scmd
->result
= DID_ERROR
<< 16;
1117 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1118 * @instance: Adapter soft state
1119 * @scp: SCSI command from the mid-layer
1120 * @sgl_ptr: SGL to be filled in
1121 * @cmd: cmd we are working on
1123 * If successful, this function returns the number of SG elements.
1126 megasas_make_sgl_fusion(struct megasas_instance
*instance
,
1127 struct scsi_cmnd
*scp
,
1128 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
1129 struct megasas_cmd_fusion
*cmd
)
1131 int i
, sg_processed
, sge_count
;
1132 struct scatterlist
*os_sgl
;
1133 struct fusion_context
*fusion
;
1135 fusion
= instance
->ctrl_context
;
1137 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1138 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1139 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
= sgl_ptr
;
1140 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
1141 sgl_ptr_end
->Flags
= 0;
1144 sge_count
= scsi_dma_map(scp
);
1146 BUG_ON(sge_count
< 0);
1148 if (sge_count
> instance
->max_num_sge
|| !sge_count
)
1151 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1152 sgl_ptr
->Length
= cpu_to_le32(sg_dma_len(os_sgl
));
1153 sgl_ptr
->Address
= cpu_to_le64(sg_dma_address(os_sgl
));
1155 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1156 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1157 if (i
== sge_count
- 1)
1158 sgl_ptr
->Flags
= IEEE_SGE_FLAGS_END_OF_LIST
;
1162 sg_processed
= i
+ 1;
1164 if ((sg_processed
== (fusion
->max_sge_in_main_msg
- 1)) &&
1165 (sge_count
> fusion
->max_sge_in_main_msg
)) {
1167 struct MPI25_IEEE_SGE_CHAIN64
*sg_chain
;
1168 if ((instance
->pdev
->device
==
1169 PCI_DEVICE_ID_LSI_INVADER
) ||
1170 (instance
->pdev
->device
==
1171 PCI_DEVICE_ID_LSI_FURY
)) {
1172 if ((le16_to_cpu(cmd
->io_request
->IoFlags
) &
1173 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
) !=
1174 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
)
1175 cmd
->io_request
->ChainOffset
=
1177 chain_offset_io_request
;
1179 cmd
->io_request
->ChainOffset
= 0;
1181 cmd
->io_request
->ChainOffset
=
1182 fusion
->chain_offset_io_request
;
1185 /* Prepare chain element */
1186 sg_chain
->NextChainOffset
= 0;
1187 if ((instance
->pdev
->device
==
1188 PCI_DEVICE_ID_LSI_INVADER
) ||
1189 (instance
->pdev
->device
==
1190 PCI_DEVICE_ID_LSI_FURY
))
1191 sg_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
;
1194 (IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1195 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
);
1196 sg_chain
->Length
= cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION
) * (sge_count
- sg_processed
)));
1197 sg_chain
->Address
= cpu_to_le64(cmd
->sg_frame_phys_addr
);
1200 (struct MPI25_IEEE_SGE_CHAIN64
*)cmd
->sg_frame
;
1208 * megasas_set_pd_lba - Sets PD LBA
1210 * @cdb_len: cdb length
1211 * @start_blk: Start block of IO
1213 * Used to set the PD LBA in CDB for FP IOs
1216 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
, u8 cdb_len
,
1217 struct IO_REQUEST_INFO
*io_info
, struct scsi_cmnd
*scp
,
1218 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
, u32 ref_tag
)
1220 struct MR_LD_RAID
*raid
;
1222 u64 start_blk
= io_info
->pdBlock
;
1223 u8
*cdb
= io_request
->CDB
.CDB32
;
1224 u32 num_blocks
= io_info
->numBlocks
;
1225 u8 opcode
= 0, flagvals
= 0, groupnum
= 0, control
= 0;
1227 /* Check if T10 PI (DIF) is enabled for this LD */
1228 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, local_map_ptr
);
1229 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1230 if (raid
->capability
.ldPiMode
== MR_PROT_INFO_TYPE_CONTROLLER
) {
1231 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1232 cdb
[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD
;
1233 cdb
[7] = MEGASAS_SCSI_ADDL_CDB_LEN
;
1235 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1236 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32
;
1238 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32
;
1239 cdb
[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL
;
1242 cdb
[12] = (u8
)((start_blk
>> 56) & 0xff);
1243 cdb
[13] = (u8
)((start_blk
>> 48) & 0xff);
1244 cdb
[14] = (u8
)((start_blk
>> 40) & 0xff);
1245 cdb
[15] = (u8
)((start_blk
>> 32) & 0xff);
1246 cdb
[16] = (u8
)((start_blk
>> 24) & 0xff);
1247 cdb
[17] = (u8
)((start_blk
>> 16) & 0xff);
1248 cdb
[18] = (u8
)((start_blk
>> 8) & 0xff);
1249 cdb
[19] = (u8
)(start_blk
& 0xff);
1251 /* Logical block reference tag */
1252 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
=
1253 cpu_to_be32(ref_tag
);
1254 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0xffff;
1255 io_request
->IoFlags
= cpu_to_le16(32); /* Specify 32-byte cdb */
1257 /* Transfer length */
1258 cdb
[28] = (u8
)((num_blocks
>> 24) & 0xff);
1259 cdb
[29] = (u8
)((num_blocks
>> 16) & 0xff);
1260 cdb
[30] = (u8
)((num_blocks
>> 8) & 0xff);
1261 cdb
[31] = (u8
)(num_blocks
& 0xff);
1263 /* set SCSI IO EEDPFlags */
1264 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
) {
1265 io_request
->EEDPFlags
= cpu_to_le16(
1266 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1267 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG
|
1268 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
|
1269 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG
|
1270 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD
);
1272 io_request
->EEDPFlags
= cpu_to_le16(
1273 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1274 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
);
1276 io_request
->Control
|= cpu_to_le32((0x4 << 26));
1277 io_request
->EEDPBlockSize
= cpu_to_le32(scp
->device
->sector_size
);
1279 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1280 if (((cdb_len
== 12) || (cdb_len
== 16)) &&
1281 (start_blk
<= 0xffffffff)) {
1282 if (cdb_len
== 16) {
1283 opcode
= cdb
[0] == READ_16
? READ_10
: WRITE_10
;
1288 opcode
= cdb
[0] == READ_12
? READ_10
: WRITE_10
;
1294 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1301 /* Transfer length */
1302 cdb
[8] = (u8
)(num_blocks
& 0xff);
1303 cdb
[7] = (u8
)((num_blocks
>> 8) & 0xff);
1305 io_request
->IoFlags
= cpu_to_le16(10); /* Specify 10-byte cdb */
1307 } else if ((cdb_len
< 16) && (start_blk
> 0xffffffff)) {
1308 /* Convert to 16 byte CDB for large LBA's */
1311 opcode
= cdb
[0] == READ_6
? READ_16
: WRITE_16
;
1316 cdb
[0] == READ_10
? READ_16
: WRITE_16
;
1323 cdb
[0] == READ_12
? READ_16
: WRITE_16
;
1330 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1337 /* Transfer length */
1338 cdb
[13] = (u8
)(num_blocks
& 0xff);
1339 cdb
[12] = (u8
)((num_blocks
>> 8) & 0xff);
1340 cdb
[11] = (u8
)((num_blocks
>> 16) & 0xff);
1341 cdb
[10] = (u8
)((num_blocks
>> 24) & 0xff);
1343 io_request
->IoFlags
= cpu_to_le16(16); /* Specify 16-byte cdb */
1347 /* Normal case, just load LBA here */
1351 u8 val
= cdb
[1] & 0xE0;
1352 cdb
[3] = (u8
)(start_blk
& 0xff);
1353 cdb
[2] = (u8
)((start_blk
>> 8) & 0xff);
1354 cdb
[1] = val
| ((u8
)(start_blk
>> 16) & 0x1f);
1358 cdb
[5] = (u8
)(start_blk
& 0xff);
1359 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1360 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1361 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1364 cdb
[5] = (u8
)(start_blk
& 0xff);
1365 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1366 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1367 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1370 cdb
[9] = (u8
)(start_blk
& 0xff);
1371 cdb
[8] = (u8
)((start_blk
>> 8) & 0xff);
1372 cdb
[7] = (u8
)((start_blk
>> 16) & 0xff);
1373 cdb
[6] = (u8
)((start_blk
>> 24) & 0xff);
1374 cdb
[5] = (u8
)((start_blk
>> 32) & 0xff);
1375 cdb
[4] = (u8
)((start_blk
>> 40) & 0xff);
1376 cdb
[3] = (u8
)((start_blk
>> 48) & 0xff);
1377 cdb
[2] = (u8
)((start_blk
>> 56) & 0xff);
1384 * megasas_build_ldio_fusion - Prepares IOs to devices
1385 * @instance: Adapter soft state
1386 * @scp: SCSI command
1387 * @cmd: Command to be prepared
1389 * Prepares the io_request and chain elements (sg_frame) for IO
1390 * The IO can be for PD (Fast Path) or LD
1393 megasas_build_ldio_fusion(struct megasas_instance
*instance
,
1394 struct scsi_cmnd
*scp
,
1395 struct megasas_cmd_fusion
*cmd
)
1398 u32 start_lba_lo
, start_lba_hi
, device_id
, datalength
= 0;
1399 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1400 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1401 struct IO_REQUEST_INFO io_info
;
1402 struct fusion_context
*fusion
;
1403 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1406 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1408 fusion
= instance
->ctrl_context
;
1410 io_request
= cmd
->io_request
;
1411 io_request
->RaidContext
.VirtualDiskTgtId
= cpu_to_le16(device_id
);
1412 io_request
->RaidContext
.status
= 0;
1413 io_request
->RaidContext
.exStatus
= 0;
1415 req_desc
= (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)cmd
->request_desc
;
1422 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1424 if (scp
->cmd_len
== 6) {
1425 datalength
= (u32
) scp
->cmnd
[4];
1426 start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1427 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1429 start_lba_lo
&= 0x1FFFFF;
1433 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1435 else if (scp
->cmd_len
== 10) {
1436 datalength
= (u32
) scp
->cmnd
[8] |
1437 ((u32
) scp
->cmnd
[7] << 8);
1438 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1439 ((u32
) scp
->cmnd
[3] << 16) |
1440 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1444 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1446 else if (scp
->cmd_len
== 12) {
1447 datalength
= ((u32
) scp
->cmnd
[6] << 24) |
1448 ((u32
) scp
->cmnd
[7] << 16) |
1449 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1450 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1451 ((u32
) scp
->cmnd
[3] << 16) |
1452 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1456 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1458 else if (scp
->cmd_len
== 16) {
1459 datalength
= ((u32
) scp
->cmnd
[10] << 24) |
1460 ((u32
) scp
->cmnd
[11] << 16) |
1461 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1462 start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1463 ((u32
) scp
->cmnd
[7] << 16) |
1464 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1466 start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1467 ((u32
) scp
->cmnd
[3] << 16) |
1468 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1471 memset(&io_info
, 0, sizeof(struct IO_REQUEST_INFO
));
1472 io_info
.ldStartBlock
= ((u64
)start_lba_hi
<< 32) | start_lba_lo
;
1473 io_info
.numBlocks
= datalength
;
1474 io_info
.ldTgtId
= device_id
;
1475 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scp
));
1477 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1480 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1482 if ((MR_TargetIdToLdGet(device_id
, local_map_ptr
) >=
1483 MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
)) {
1484 io_request
->RaidContext
.regLockFlags
= 0;
1487 if (MR_BuildRaidContext(instance
, &io_info
,
1488 &io_request
->RaidContext
,
1489 local_map_ptr
, &raidLUN
))
1490 fp_possible
= io_info
.fpOkForIo
;
1493 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1494 id by default, not CPU group id, otherwise all MSI-X queues won't
1496 cmd
->request_desc
->SCSIIO
.MSIxIndex
= instance
->msix_vectors
?
1497 smp_processor_id() % instance
->msix_vectors
: 0;
1500 megasas_set_pd_lba(io_request
, scp
->cmd_len
, &io_info
, scp
,
1501 local_map_ptr
, start_lba_lo
);
1502 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1503 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1504 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1505 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1506 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1507 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1508 if (io_request
->RaidContext
.regLockFlags
==
1510 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1511 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK
<<
1512 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1513 io_request
->RaidContext
.Type
= MPI2_TYPE_CUDA
;
1514 io_request
->RaidContext
.nseg
= 0x1;
1515 io_request
->IoFlags
|= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
);
1516 io_request
->RaidContext
.regLockFlags
|=
1517 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA
|
1518 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
1520 if ((fusion
->load_balance_info
[device_id
].loadBalanceFlag
) &&
1523 get_updated_dev_handle(
1524 &fusion
->load_balance_info
[device_id
],
1526 scp
->SCp
.Status
|= MEGASAS_LOAD_BALANCE_FLAG
;
1528 scp
->SCp
.Status
&= ~MEGASAS_LOAD_BALANCE_FLAG
;
1529 cmd
->request_desc
->SCSIIO
.DevHandle
= io_info
.devHandle
;
1530 io_request
->DevHandle
= io_info
.devHandle
;
1531 /* populate the LUN field */
1532 memcpy(io_request
->LUN
, raidLUN
, 8);
1534 io_request
->RaidContext
.timeoutValue
=
1535 cpu_to_le16(local_map_ptr
->raidMap
.fpPdIoTimeoutSec
);
1536 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1537 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1538 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1539 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1540 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1541 if (io_request
->RaidContext
.regLockFlags
==
1543 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1544 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK
<<
1545 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1546 io_request
->RaidContext
.Type
= MPI2_TYPE_CUDA
;
1547 io_request
->RaidContext
.regLockFlags
|=
1548 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0
|
1549 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
1550 io_request
->RaidContext
.nseg
= 0x1;
1552 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1553 io_request
->DevHandle
= cpu_to_le16(device_id
);
1558 * megasas_build_dcdb_fusion - Prepares IOs to devices
1559 * @instance: Adapter soft state
1560 * @scp: SCSI command
1561 * @cmd: Command to be prepared
1563 * Prepares the io_request frame for non-io cmds
1566 megasas_build_dcdb_fusion(struct megasas_instance
*instance
,
1567 struct scsi_cmnd
*scmd
,
1568 struct megasas_cmd_fusion
*cmd
)
1571 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1573 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1574 struct fusion_context
*fusion
= instance
->ctrl_context
;
1578 struct MR_LD_RAID
*raid
;
1579 struct RAID_CONTEXT
*pRAID_Context
;
1581 io_request
= cmd
->io_request
;
1582 device_id
= MEGASAS_DEV_INDEX(instance
, scmd
);
1583 pd_index
= (scmd
->device
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
)
1585 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1587 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scmd
));
1590 /* Check if this is a system PD I/O */
1591 if (scmd
->device
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1592 instance
->pd_list
[pd_index
].driveState
== MR_PD_STATE_SYSTEM
) {
1593 io_request
->Function
= 0;
1594 if (fusion
->fast_path_io
)
1595 io_request
->DevHandle
=
1596 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1597 io_request
->RaidContext
.timeoutValue
=
1598 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1599 io_request
->RaidContext
.regLockFlags
= 0;
1600 io_request
->RaidContext
.regLockRowLBA
= 0;
1601 io_request
->RaidContext
.regLockLength
= 0;
1602 io_request
->RaidContext
.RAIDFlags
=
1603 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
<<
1604 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
;
1605 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1606 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1607 io_request
->IoFlags
|=
1608 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
;
1609 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1610 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1611 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1612 cmd
->request_desc
->SCSIIO
.DevHandle
=
1613 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1615 * If the command is for the tape device, set the
1616 * FP timeout to the os layer timeout value.
1618 if (scmd
->device
->type
== TYPE_TAPE
) {
1619 if ((scmd
->request
->timeout
/ HZ
) > 0xFFFF)
1620 io_request
->RaidContext
.timeoutValue
=
1623 io_request
->RaidContext
.timeoutValue
=
1624 scmd
->request
->timeout
/ HZ
;
1627 if (scmd
->device
->channel
< MEGASAS_MAX_PD_CHANNELS
)
1630 ld
= MR_TargetIdToLdGet(device_id
, local_map_ptr
);
1631 if ((ld
>= MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
))
1634 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1636 /* check if this LD is FP capable */
1637 if (!(raid
->capability
.fpNonRWCapable
))
1638 /* not FP capable, send as non-FP */
1641 /* get RAID_Context pointer */
1642 pRAID_Context
= &io_request
->RaidContext
;
1644 /* set RAID context values */
1645 pRAID_Context
->regLockFlags
= REGION_TYPE_SHARED_READ
;
1646 pRAID_Context
->timeoutValue
= raid
->fpIoTimeoutForLd
;
1647 pRAID_Context
->VirtualDiskTgtId
= cpu_to_le16(device_id
);
1648 pRAID_Context
->regLockRowLBA
= 0;
1649 pRAID_Context
->regLockLength
= 0;
1650 pRAID_Context
->configSeqNum
= raid
->seqNum
;
1652 /* get the DevHandle for the PD (since this is
1653 fpNonRWCapable, this is a single disk RAID0) */
1655 arRef
= MR_LdSpanArrayGet(ld
, span
, local_map_ptr
);
1656 pd
= MR_ArPdGet(arRef
, physArm
, local_map_ptr
);
1657 devHandle
= MR_PdDevHandleGet(pd
, local_map_ptr
);
1659 /* build request descriptor */
1660 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1661 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1662 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1663 cmd
->request_desc
->SCSIIO
.DevHandle
= devHandle
;
1665 /* populate the LUN field */
1666 memcpy(io_request
->LUN
, raid
->LUN
, 8);
1668 /* build the raidScsiIO structure */
1669 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1670 io_request
->DevHandle
= devHandle
;
1675 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1676 io_request
->DevHandle
= cpu_to_le16(device_id
);
1677 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1678 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1679 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1681 io_request
->RaidContext
.VirtualDiskTgtId
= cpu_to_le16(device_id
);
1682 io_request
->LUN
[1] = scmd
->device
->lun
;
1686 * megasas_build_io_fusion - Prepares IOs to devices
1687 * @instance: Adapter soft state
1688 * @scp: SCSI command
1689 * @cmd: Command to be prepared
1691 * Invokes helper functions to prepare request frames
1692 * and sets flags appropriate for IO/Non-IO cmd
1695 megasas_build_io_fusion(struct megasas_instance
*instance
,
1696 struct scsi_cmnd
*scp
,
1697 struct megasas_cmd_fusion
*cmd
)
1699 u32 device_id
, sge_count
;
1700 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
= cmd
->io_request
;
1702 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1704 /* Zero out some fields so they don't get reused */
1705 io_request
->LUN
[1] = 0;
1706 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
= 0;
1707 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0;
1708 io_request
->EEDPFlags
= 0;
1709 io_request
->Control
= 0;
1710 io_request
->EEDPBlockSize
= 0;
1711 io_request
->ChainOffset
= 0;
1712 io_request
->RaidContext
.RAIDFlags
= 0;
1713 io_request
->RaidContext
.Type
= 0;
1714 io_request
->RaidContext
.nseg
= 0;
1716 memcpy(io_request
->CDB
.CDB32
, scp
->cmnd
, scp
->cmd_len
);
1718 * Just the CDB length,rest of the Flags are zero
1719 * This will be modified for FP in build_ldio_fusion
1721 io_request
->IoFlags
= cpu_to_le16(scp
->cmd_len
);
1723 if (megasas_is_ldio(scp
))
1724 megasas_build_ldio_fusion(instance
, scp
, cmd
);
1726 megasas_build_dcdb_fusion(instance
, scp
, cmd
);
1733 megasas_make_sgl_fusion(instance
, scp
,
1734 (struct MPI25_IEEE_SGE_CHAIN64
*)
1735 &io_request
->SGL
, cmd
);
1737 if (sge_count
> instance
->max_num_sge
) {
1738 printk(KERN_ERR
"megasas: Error. sge_count (0x%x) exceeds "
1739 "max (0x%x) allowed\n", sge_count
,
1740 instance
->max_num_sge
);
1744 io_request
->RaidContext
.numSGE
= sge_count
;
1746 io_request
->SGLFlags
= cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING
);
1748 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1749 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE
);
1750 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1751 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ
);
1753 io_request
->SGLOffset0
=
1754 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
) / 4;
1756 io_request
->SenseBufferLowAddress
= cpu_to_le32(cmd
->sense_phys_addr
);
1757 io_request
->SenseBufferLength
= SCSI_SENSE_BUFFERSIZE
;
1760 scp
->SCp
.ptr
= (char *)cmd
;
1765 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1766 megasas_get_request_descriptor(struct megasas_instance
*instance
, u16 index
)
1769 struct fusion_context
*fusion
;
1771 if (index
>= instance
->max_fw_cmds
) {
1772 printk(KERN_ERR
"megasas: Invalid SMID (0x%x)request for "
1773 "descriptor\n", index
);
1776 fusion
= instance
->ctrl_context
;
1777 p
= fusion
->req_frames_desc
1778 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *index
;
1780 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)p
;
1784 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1785 * issuing non IOCTL cmd
1786 * @instance: Adapter soft state
1787 * @scmd: pointer to scsi cmd from OS
1790 megasas_build_and_issue_cmd_fusion(struct megasas_instance
*instance
,
1791 struct scsi_cmnd
*scmd
)
1793 struct megasas_cmd_fusion
*cmd
;
1794 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1796 struct fusion_context
*fusion
;
1798 fusion
= instance
->ctrl_context
;
1800 cmd
= megasas_get_cmd_fusion(instance
);
1802 return SCSI_MLQUEUE_HOST_BUSY
;
1806 req_desc
= megasas_get_request_descriptor(instance
, index
-1);
1810 req_desc
->Words
= 0;
1811 cmd
->request_desc
= req_desc
;
1813 if (megasas_build_io_fusion(instance
, scmd
, cmd
)) {
1814 megasas_return_cmd_fusion(instance
, cmd
);
1815 printk(KERN_ERR
"megasas: Error building command.\n");
1816 cmd
->request_desc
= NULL
;
1820 req_desc
= cmd
->request_desc
;
1821 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
1823 if (cmd
->io_request
->ChainOffset
!= 0 &&
1824 cmd
->io_request
->ChainOffset
!= 0xF)
1825 printk(KERN_ERR
"megasas: The chain offset value is not "
1826 "correct : %x\n", cmd
->io_request
->ChainOffset
);
1829 * Issue the command to the FW
1831 atomic_inc(&instance
->fw_outstanding
);
1833 instance
->instancet
->fire_cmd(instance
,
1834 req_desc
->u
.low
, req_desc
->u
.high
,
1841 * complete_cmd_fusion - Completes command
1842 * @instance: Adapter soft state
1843 * Completes all commands that is in reply descriptor queue
1846 complete_cmd_fusion(struct megasas_instance
*instance
, u32 MSIxIndex
)
1848 union MPI2_REPLY_DESCRIPTORS_UNION
*desc
;
1849 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*reply_desc
;
1850 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
1851 struct fusion_context
*fusion
;
1852 struct megasas_cmd
*cmd_mfi
;
1853 struct megasas_cmd_fusion
*cmd_fusion
;
1854 u16 smid
, num_completed
;
1855 u8 reply_descript_type
, arm
;
1856 u32 status
, extStatus
, device_id
;
1857 union desc_value d_val
;
1858 struct LD_LOAD_BALANCE_INFO
*lbinfo
;
1860 fusion
= instance
->ctrl_context
;
1862 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1865 desc
= fusion
->reply_frames_desc
;
1866 desc
+= ((MSIxIndex
* fusion
->reply_alloc_sz
)/
1867 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)) +
1868 fusion
->last_reply_idx
[MSIxIndex
];
1870 reply_desc
= (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1872 d_val
.word
= desc
->Words
;
1874 reply_descript_type
= reply_desc
->ReplyFlags
&
1875 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1877 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1882 while ((d_val
.u
.low
!= UINT_MAX
) && (d_val
.u
.high
!= UINT_MAX
)) {
1883 smid
= le16_to_cpu(reply_desc
->SMID
);
1885 cmd_fusion
= fusion
->cmd_list
[smid
- 1];
1888 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
1889 cmd_fusion
->io_request
;
1891 if (cmd_fusion
->scmd
)
1892 cmd_fusion
->scmd
->SCp
.ptr
= NULL
;
1894 status
= scsi_io_req
->RaidContext
.status
;
1895 extStatus
= scsi_io_req
->RaidContext
.exStatus
;
1897 switch (scsi_io_req
->Function
) {
1898 case MPI2_FUNCTION_SCSI_IO_REQUEST
: /*Fast Path IO.*/
1899 /* Update load balancing info */
1900 device_id
= MEGASAS_DEV_INDEX(instance
,
1902 lbinfo
= &fusion
->load_balance_info
[device_id
];
1903 if (cmd_fusion
->scmd
->SCp
.Status
&
1904 MEGASAS_LOAD_BALANCE_FLAG
) {
1905 arm
= lbinfo
->raid1DevHandle
[0] ==
1906 cmd_fusion
->io_request
->DevHandle
? 0 :
1908 atomic_dec(&lbinfo
->scsi_pending_cmds
[arm
]);
1909 cmd_fusion
->scmd
->SCp
.Status
&=
1910 ~MEGASAS_LOAD_BALANCE_FLAG
;
1912 if (reply_descript_type
==
1913 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS
) {
1914 if (megasas_dbg_lvl
== 5)
1915 printk(KERN_ERR
"\nmegasas: FAST Path "
1918 /* Fall thru and complete IO */
1919 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
: /* LD-IO Path */
1920 /* Map the FW Cmd Status */
1921 map_cmd_status(cmd_fusion
, status
, extStatus
);
1922 scsi_dma_unmap(cmd_fusion
->scmd
);
1923 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
1924 scsi_io_req
->RaidContext
.status
= 0;
1925 scsi_io_req
->RaidContext
.exStatus
= 0;
1926 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1927 atomic_dec(&instance
->fw_outstanding
);
1930 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
: /*MFI command */
1931 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
1932 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
1933 cmd_fusion
->flags
= 0;
1934 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1939 fusion
->last_reply_idx
[MSIxIndex
]++;
1940 if (fusion
->last_reply_idx
[MSIxIndex
] >=
1941 fusion
->reply_q_depth
)
1942 fusion
->last_reply_idx
[MSIxIndex
] = 0;
1944 desc
->Words
= ULLONG_MAX
;
1947 /* Get the next reply descriptor */
1948 if (!fusion
->last_reply_idx
[MSIxIndex
])
1949 desc
= fusion
->reply_frames_desc
+
1950 ((MSIxIndex
* fusion
->reply_alloc_sz
)/
1951 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
));
1956 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1958 d_val
.word
= desc
->Words
;
1960 reply_descript_type
= reply_desc
->ReplyFlags
&
1961 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1963 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1971 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1972 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1973 writel(((MSIxIndex
& 0x7) << 24) |
1974 fusion
->last_reply_idx
[MSIxIndex
],
1975 instance
->reply_post_host_index_addr
[MSIxIndex
/8]);
1977 writel((MSIxIndex
<< 24) |
1978 fusion
->last_reply_idx
[MSIxIndex
],
1979 instance
->reply_post_host_index_addr
[0]);
1980 megasas_check_and_restore_queue_depth(instance
);
1985 * megasas_complete_cmd_dpc_fusion - Completes command
1986 * @instance: Adapter soft state
1988 * Tasklet to complete cmds
1991 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr
)
1993 struct megasas_instance
*instance
=
1994 (struct megasas_instance
*)instance_addr
;
1995 unsigned long flags
;
1996 u32 count
, MSIxIndex
;
1998 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
2000 /* If we have already declared adapter dead, donot complete cmds */
2001 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2002 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2003 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2006 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2008 for (MSIxIndex
= 0 ; MSIxIndex
< count
; MSIxIndex
++)
2009 complete_cmd_fusion(instance
, MSIxIndex
);
2013 * megasas_isr_fusion - isr entry point
2015 irqreturn_t
megasas_isr_fusion(int irq
, void *devp
)
2017 struct megasas_irq_context
*irq_context
= devp
;
2018 struct megasas_instance
*instance
= irq_context
->instance
;
2019 u32 mfiStatus
, fw_state
;
2021 if (instance
->mask_interrupts
)
2024 if (!instance
->msix_vectors
) {
2025 mfiStatus
= instance
->instancet
->clear_intr(instance
->reg_set
);
2030 /* If we are resetting, bail */
2031 if (test_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
)) {
2032 instance
->instancet
->clear_intr(instance
->reg_set
);
2036 if (!complete_cmd_fusion(instance
, irq_context
->MSIxIndex
)) {
2037 instance
->instancet
->clear_intr(instance
->reg_set
);
2038 /* If we didn't complete any commands, check for FW fault */
2039 fw_state
= instance
->instancet
->read_fw_status_reg(
2040 instance
->reg_set
) & MFI_STATE_MASK
;
2041 if (fw_state
== MFI_STATE_FAULT
)
2042 schedule_work(&instance
->work_init
);
2049 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2050 * @instance: Adapter soft state
2051 * mfi_cmd: megasas_cmd pointer
2055 build_mpt_mfi_pass_thru(struct megasas_instance
*instance
,
2056 struct megasas_cmd
*mfi_cmd
)
2058 struct MPI25_IEEE_SGE_CHAIN64
*mpi25_ieee_chain
;
2059 struct MPI2_RAID_SCSI_IO_REQUEST
*io_req
;
2060 struct megasas_cmd_fusion
*cmd
;
2061 struct fusion_context
*fusion
;
2062 struct megasas_header
*frame_hdr
= &mfi_cmd
->frame
->hdr
;
2064 cmd
= megasas_get_cmd_fusion(instance
);
2068 /* Save the smid. To be used for returning the cmd */
2069 mfi_cmd
->context
.smid
= cmd
->index
;
2071 cmd
->sync_cmd_idx
= mfi_cmd
->index
;
2074 * For cmds where the flag is set, store the flag and check
2075 * on completion. For cmds with this flag, don't call
2076 * megasas_complete_cmd
2079 if (frame_hdr
->flags
& MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
)
2080 cmd
->flags
= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
2082 fusion
= instance
->ctrl_context
;
2083 io_req
= cmd
->io_request
;
2085 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
2086 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
2087 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
=
2088 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
;
2089 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
2090 sgl_ptr_end
->Flags
= 0;
2094 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
.IeeeChain
;
2096 io_req
->Function
= MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
;
2097 io_req
->SGLOffset0
= offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
,
2099 io_req
->ChainOffset
= fusion
->chain_offset_mfi_pthru
;
2101 mpi25_ieee_chain
->Address
= cpu_to_le64(mfi_cmd
->frame_phys_addr
);
2103 mpi25_ieee_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
2104 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
;
2106 mpi25_ieee_chain
->Length
= cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME
);
2112 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2113 * @instance: Adapter soft state
2114 * @cmd: mfi cmd to build
2117 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
2118 build_mpt_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2120 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2123 if (build_mpt_mfi_pass_thru(instance
, cmd
)) {
2124 printk(KERN_ERR
"Couldn't build MFI pass thru cmd\n");
2128 index
= cmd
->context
.smid
;
2130 req_desc
= megasas_get_request_descriptor(instance
, index
- 1);
2135 req_desc
->Words
= 0;
2136 req_desc
->SCSIIO
.RequestFlags
= (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
2137 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
2139 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
2145 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2146 * @instance: Adapter soft state
2147 * @cmd: mfi cmd pointer
2151 megasas_issue_dcmd_fusion(struct megasas_instance
*instance
,
2152 struct megasas_cmd
*cmd
)
2154 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2156 req_desc
= build_mpt_cmd(instance
, cmd
);
2158 printk(KERN_ERR
"Couldn't issue MFI pass thru cmd\n");
2161 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
2162 req_desc
->u
.high
, instance
->reg_set
);
2166 * megasas_release_fusion - Reverses the FW initialization
2167 * @intance: Adapter soft state
2170 megasas_release_fusion(struct megasas_instance
*instance
)
2172 megasas_free_cmds(instance
);
2173 megasas_free_cmds_fusion(instance
);
2175 iounmap(instance
->reg_set
);
2177 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
2181 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2182 * @regs: MFI register set
2185 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem
*regs
)
2187 return readl(&(regs
)->outbound_scratch_pad
);
2191 * megasas_adp_reset_fusion - For controller reset
2192 * @regs: MFI register set
2195 megasas_adp_reset_fusion(struct megasas_instance
*instance
,
2196 struct megasas_register_set __iomem
*regs
)
2202 * megasas_check_reset_fusion - For controller reset check
2203 * @regs: MFI register set
2206 megasas_check_reset_fusion(struct megasas_instance
*instance
,
2207 struct megasas_register_set __iomem
*regs
)
2212 /* This function waits for outstanding commands on fusion to complete */
2213 int megasas_wait_for_outstanding_fusion(struct megasas_instance
*instance
)
2215 int i
, outstanding
, retval
= 0;
2218 for (i
= 0; i
< resetwaittime
; i
++) {
2219 /* Check if firmware is in fault state */
2220 fw_state
= instance
->instancet
->read_fw_status_reg(
2221 instance
->reg_set
) & MFI_STATE_MASK
;
2222 if (fw_state
== MFI_STATE_FAULT
) {
2223 printk(KERN_WARNING
"megasas: Found FW in FAULT state,"
2224 " will reset adapter.\n");
2229 outstanding
= atomic_read(&instance
->fw_outstanding
);
2233 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
2234 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
2235 "commands to complete\n", i
, outstanding
);
2236 megasas_complete_cmd_dpc_fusion(
2237 (unsigned long)instance
);
2242 if (atomic_read(&instance
->fw_outstanding
)) {
2243 printk("megaraid_sas: pending commands remain after waiting, "
2244 "will reset adapter.\n");
2251 void megasas_reset_reply_desc(struct megasas_instance
*instance
)
2254 struct fusion_context
*fusion
;
2255 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
2257 fusion
= instance
->ctrl_context
;
2258 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
2259 for (i
= 0 ; i
< count
; i
++)
2260 fusion
->last_reply_idx
[i
] = 0;
2261 reply_desc
= fusion
->reply_frames_desc
;
2262 for (i
= 0 ; i
< fusion
->reply_q_depth
* count
; i
++, reply_desc
++)
2263 reply_desc
->Words
= ULLONG_MAX
;
2266 /* Core fusion reset function */
2267 int megasas_reset_fusion(struct Scsi_Host
*shost
)
2269 int retval
= SUCCESS
, i
, j
, retry
= 0;
2270 struct megasas_instance
*instance
;
2271 struct megasas_cmd_fusion
*cmd_fusion
;
2272 struct fusion_context
*fusion
;
2273 struct megasas_cmd
*cmd_mfi
;
2274 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2275 u32 host_diag
, abs_state
, status_reg
, reset_adapter
;
2277 instance
= (struct megasas_instance
*)shost
->hostdata
;
2278 fusion
= instance
->ctrl_context
;
2280 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2281 printk(KERN_WARNING
"megaraid_sas: Hardware critical error, "
2282 "returning FAILED.\n");
2286 mutex_lock(&instance
->reset_mutex
);
2287 set_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2288 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2289 instance
->instancet
->disable_intr(instance
);
2292 /* First try waiting for commands to complete */
2293 if (megasas_wait_for_outstanding_fusion(instance
)) {
2294 printk(KERN_WARNING
"megaraid_sas: resetting fusion "
2296 /* Now return commands back to the OS */
2297 for (i
= 0 ; i
< instance
->max_fw_cmds
; i
++) {
2298 cmd_fusion
= fusion
->cmd_list
[i
];
2299 if (cmd_fusion
->scmd
) {
2300 scsi_dma_unmap(cmd_fusion
->scmd
);
2301 cmd_fusion
->scmd
->result
= (DID_RESET
<< 16);
2302 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
2303 megasas_return_cmd_fusion(instance
, cmd_fusion
);
2304 atomic_dec(&instance
->fw_outstanding
);
2308 status_reg
= instance
->instancet
->read_fw_status_reg(
2310 abs_state
= status_reg
& MFI_STATE_MASK
;
2311 reset_adapter
= status_reg
& MFI_RESET_ADAPTER
;
2312 if (instance
->disableOnlineCtrlReset
||
2313 (abs_state
== MFI_STATE_FAULT
&& !reset_adapter
)) {
2314 /* Reset not supported, kill adapter */
2315 printk(KERN_WARNING
"megaraid_sas: Reset not supported"
2316 ", killing adapter.\n");
2317 megaraid_sas_kill_hba(instance
);
2318 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2323 /* Now try to reset the chip */
2324 for (i
= 0; i
< MEGASAS_FUSION_MAX_RESET_TRIES
; i
++) {
2325 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE
,
2326 &instance
->reg_set
->fusion_seq_offset
);
2327 writel(MPI2_WRSEQ_1ST_KEY_VALUE
,
2328 &instance
->reg_set
->fusion_seq_offset
);
2329 writel(MPI2_WRSEQ_2ND_KEY_VALUE
,
2330 &instance
->reg_set
->fusion_seq_offset
);
2331 writel(MPI2_WRSEQ_3RD_KEY_VALUE
,
2332 &instance
->reg_set
->fusion_seq_offset
);
2333 writel(MPI2_WRSEQ_4TH_KEY_VALUE
,
2334 &instance
->reg_set
->fusion_seq_offset
);
2335 writel(MPI2_WRSEQ_5TH_KEY_VALUE
,
2336 &instance
->reg_set
->fusion_seq_offset
);
2337 writel(MPI2_WRSEQ_6TH_KEY_VALUE
,
2338 &instance
->reg_set
->fusion_seq_offset
);
2340 /* Check that the diag write enable (DRWE) bit is on */
2341 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2343 while (!(host_diag
& HOST_DIAG_WRITE_ENABLE
)) {
2346 readl(&instance
->reg_set
->fusion_host_diag
);
2347 if (retry
++ == 100) {
2348 printk(KERN_WARNING
"megaraid_sas: "
2349 "Host diag unlock failed!\n");
2353 if (!(host_diag
& HOST_DIAG_WRITE_ENABLE
))
2356 /* Send chip reset command */
2357 writel(host_diag
| HOST_DIAG_RESET_ADAPTER
,
2358 &instance
->reg_set
->fusion_host_diag
);
2361 /* Make sure reset adapter bit is cleared */
2362 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2364 while (host_diag
& HOST_DIAG_RESET_ADAPTER
) {
2367 readl(&instance
->reg_set
->fusion_host_diag
);
2368 if (retry
++ == 1000) {
2369 printk(KERN_WARNING
"megaraid_sas: "
2370 "Diag reset adapter never "
2375 if (host_diag
& HOST_DIAG_RESET_ADAPTER
)
2379 instance
->instancet
->read_fw_status_reg(
2380 instance
->reg_set
) & MFI_STATE_MASK
;
2383 while ((abs_state
<= MFI_STATE_FW_INIT
) &&
2387 instance
->instancet
->read_fw_status_reg(
2388 instance
->reg_set
) & MFI_STATE_MASK
;
2390 if (abs_state
<= MFI_STATE_FW_INIT
) {
2391 printk(KERN_WARNING
"megaraid_sas: firmware "
2392 "state < MFI_STATE_FW_INIT, state = "
2393 "0x%x\n", abs_state
);
2397 /* Wait for FW to become ready */
2398 if (megasas_transition_to_ready(instance
, 1)) {
2399 printk(KERN_WARNING
"megaraid_sas: Failed to "
2400 "transition controller to ready.\n");
2404 megasas_reset_reply_desc(instance
);
2405 if (megasas_ioc_init_fusion(instance
)) {
2406 printk(KERN_WARNING
"megaraid_sas: "
2407 "megasas_ioc_init_fusion() failed!\n");
2411 clear_bit(MEGASAS_FUSION_IN_RESET
,
2412 &instance
->reset_flags
);
2413 instance
->instancet
->enable_intr(instance
);
2414 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2416 /* Re-fire management commands */
2417 for (j
= 0 ; j
< instance
->max_fw_cmds
; j
++) {
2418 cmd_fusion
= fusion
->cmd_list
[j
];
2419 if (cmd_fusion
->sync_cmd_idx
!=
2423 cmd_list
[cmd_fusion
->sync_cmd_idx
];
2424 if (cmd_mfi
->frame
->dcmd
.opcode
==
2425 MR_DCMD_LD_MAP_GET_INFO
) {
2426 megasas_return_cmd(instance
,
2428 megasas_return_cmd_fusion(
2429 instance
, cmd_fusion
);
2432 megasas_get_request_descriptor(
2434 cmd_mfi
->context
.smid
2441 instance
->instancet
->
2454 /* Reset load balance info */
2455 memset(fusion
->load_balance_info
, 0,
2456 sizeof(struct LD_LOAD_BALANCE_INFO
)
2457 *MAX_LOGICAL_DRIVES
);
2459 if (!megasas_get_map_info(instance
))
2460 megasas_sync_map_info(instance
);
2462 /* Adapter reset completed successfully */
2463 printk(KERN_WARNING
"megaraid_sas: Reset "
2468 /* Reset failed, kill the adapter */
2469 printk(KERN_WARNING
"megaraid_sas: Reset failed, killing "
2471 megaraid_sas_kill_hba(instance
);
2474 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2475 instance
->instancet
->enable_intr(instance
);
2476 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2479 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2480 mutex_unlock(&instance
->reset_mutex
);
2484 /* Fusion OCR work queue */
2485 void megasas_fusion_ocr_wq(struct work_struct
*work
)
2487 struct megasas_instance
*instance
=
2488 container_of(work
, struct megasas_instance
, work_init
);
2490 megasas_reset_fusion(instance
->host
);
2493 struct megasas_instance_template megasas_instance_template_fusion
= {
2494 .fire_cmd
= megasas_fire_cmd_fusion
,
2495 .enable_intr
= megasas_enable_intr_fusion
,
2496 .disable_intr
= megasas_disable_intr_fusion
,
2497 .clear_intr
= megasas_clear_intr_fusion
,
2498 .read_fw_status_reg
= megasas_read_fw_status_reg_fusion
,
2499 .adp_reset
= megasas_adp_reset_fusion
,
2500 .check_reset
= megasas_check_reset_fusion
,
2501 .service_isr
= megasas_isr_fusion
,
2502 .tasklet
= megasas_complete_cmd_dpc_fusion
,
2503 .init_adapter
= megasas_init_adapter_fusion
,
2504 .build_and_issue_cmd
= megasas_build_and_issue_cmd_fusion
,
2505 .issue_dcmd
= megasas_issue_dcmd_fusion
,