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
;
96 instance
->mask_interrupts
= 0;
97 /* For Thunderbolt/Invader also clear intr on enable */
98 writel(~0, ®s
->outbound_intr_status
);
99 readl(®s
->outbound_intr_status
);
101 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
103 /* Dummy readl to force pci flush */
104 readl(®s
->outbound_intr_mask
);
108 * megasas_disable_intr_fusion - Disables interrupt
109 * @regs: MFI register set
112 megasas_disable_intr_fusion(struct megasas_instance
*instance
)
114 u32 mask
= 0xFFFFFFFF;
116 struct megasas_register_set __iomem
*regs
;
117 regs
= instance
->reg_set
;
118 instance
->mask_interrupts
= 1;
120 writel(mask
, ®s
->outbound_intr_mask
);
121 /* Dummy readl to force pci flush */
122 status
= readl(®s
->outbound_intr_mask
);
126 megasas_clear_intr_fusion(struct megasas_register_set __iomem
*regs
)
130 * Check if it is our interrupt
132 status
= readl(®s
->outbound_intr_status
);
135 writel(status
, ®s
->outbound_intr_status
);
136 readl(®s
->outbound_intr_status
);
139 if (!(status
& MFI_FUSION_ENABLE_INTERRUPT_MASK
))
146 * megasas_get_cmd_fusion - Get a command from the free pool
147 * @instance: Adapter soft state
149 * Returns a free command from the pool
151 struct megasas_cmd_fusion
*megasas_get_cmd_fusion(struct megasas_instance
155 struct fusion_context
*fusion
=
156 (struct fusion_context
*)instance
->ctrl_context
;
157 struct megasas_cmd_fusion
*cmd
= NULL
;
159 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
161 if (!list_empty(&fusion
->cmd_pool
)) {
162 cmd
= list_entry((&fusion
->cmd_pool
)->next
,
163 struct megasas_cmd_fusion
, list
);
164 list_del_init(&cmd
->list
);
166 printk(KERN_ERR
"megasas: Command pool (fusion) empty!\n");
169 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
174 * megasas_return_cmd_fusion - Return a cmd to free command pool
175 * @instance: Adapter soft state
176 * @cmd: Command packet to be returned to free command pool
179 megasas_return_cmd_fusion(struct megasas_instance
*instance
,
180 struct megasas_cmd_fusion
*cmd
)
183 struct fusion_context
*fusion
=
184 (struct fusion_context
*)instance
->ctrl_context
;
186 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
189 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
;
190 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
192 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
196 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
197 * @instance: Adapter soft state
199 static void megasas_teardown_frame_pool_fusion(
200 struct megasas_instance
*instance
)
203 struct fusion_context
*fusion
= instance
->ctrl_context
;
205 u16 max_cmd
= instance
->max_fw_cmds
;
207 struct megasas_cmd_fusion
*cmd
;
209 if (!fusion
->sg_dma_pool
|| !fusion
->sense_dma_pool
) {
210 printk(KERN_ERR
"megasas: dma pool is null. SG Pool %p, "
211 "sense pool : %p\n", fusion
->sg_dma_pool
,
212 fusion
->sense_dma_pool
);
217 * Return all frames to pool
219 for (i
= 0; i
< max_cmd
; i
++) {
221 cmd
= fusion
->cmd_list
[i
];
224 pci_pool_free(fusion
->sg_dma_pool
, cmd
->sg_frame
,
225 cmd
->sg_frame_phys_addr
);
228 pci_pool_free(fusion
->sense_dma_pool
, cmd
->sense
,
229 cmd
->sense_phys_addr
);
233 * Now destroy the pool itself
235 pci_pool_destroy(fusion
->sg_dma_pool
);
236 pci_pool_destroy(fusion
->sense_dma_pool
);
238 fusion
->sg_dma_pool
= NULL
;
239 fusion
->sense_dma_pool
= NULL
;
243 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
244 * @instance: Adapter soft state
247 megasas_free_cmds_fusion(struct megasas_instance
*instance
)
250 struct fusion_context
*fusion
= instance
->ctrl_context
;
252 u32 max_cmds
, req_sz
, reply_sz
, io_frames_sz
;
255 req_sz
= fusion
->request_alloc_sz
;
256 reply_sz
= fusion
->reply_alloc_sz
;
257 io_frames_sz
= fusion
->io_frames_alloc_sz
;
259 max_cmds
= instance
->max_fw_cmds
;
261 /* Free descriptors and request Frames memory */
262 if (fusion
->req_frames_desc
)
263 dma_free_coherent(&instance
->pdev
->dev
, req_sz
,
264 fusion
->req_frames_desc
,
265 fusion
->req_frames_desc_phys
);
267 if (fusion
->reply_frames_desc
) {
268 pci_pool_free(fusion
->reply_frames_desc_pool
,
269 fusion
->reply_frames_desc
,
270 fusion
->reply_frames_desc_phys
);
271 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
274 if (fusion
->io_request_frames
) {
275 pci_pool_free(fusion
->io_request_frames_pool
,
276 fusion
->io_request_frames
,
277 fusion
->io_request_frames_phys
);
278 pci_pool_destroy(fusion
->io_request_frames_pool
);
281 /* Free the Fusion frame pool */
282 megasas_teardown_frame_pool_fusion(instance
);
284 /* Free all the commands in the cmd_list */
285 for (i
= 0; i
< max_cmds
; i
++)
286 kfree(fusion
->cmd_list
[i
]);
288 /* Free the cmd_list buffer itself */
289 kfree(fusion
->cmd_list
);
290 fusion
->cmd_list
= NULL
;
292 INIT_LIST_HEAD(&fusion
->cmd_pool
);
296 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
297 * @instance: Adapter soft state
300 static int megasas_create_frame_pool_fusion(struct megasas_instance
*instance
)
304 struct fusion_context
*fusion
;
305 struct megasas_cmd_fusion
*cmd
;
306 u32 total_sz_chain_frame
;
308 fusion
= instance
->ctrl_context
;
309 max_cmd
= instance
->max_fw_cmds
;
311 total_sz_chain_frame
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
314 * Use DMA pool facility provided by PCI layer
317 fusion
->sg_dma_pool
= pci_pool_create("megasas sg pool fusion",
319 total_sz_chain_frame
, 4,
321 if (!fusion
->sg_dma_pool
) {
322 printk(KERN_DEBUG
"megasas: failed to setup request pool "
326 fusion
->sense_dma_pool
= pci_pool_create("megasas sense pool fusion",
328 SCSI_SENSE_BUFFERSIZE
, 64, 0);
330 if (!fusion
->sense_dma_pool
) {
331 printk(KERN_DEBUG
"megasas: failed to setup sense pool "
333 pci_pool_destroy(fusion
->sg_dma_pool
);
334 fusion
->sg_dma_pool
= NULL
;
339 * Allocate and attach a frame to each of the commands in cmd_list
341 for (i
= 0; i
< max_cmd
; i
++) {
343 cmd
= fusion
->cmd_list
[i
];
345 cmd
->sg_frame
= pci_pool_alloc(fusion
->sg_dma_pool
,
347 &cmd
->sg_frame_phys_addr
);
349 cmd
->sense
= pci_pool_alloc(fusion
->sense_dma_pool
,
350 GFP_KERNEL
, &cmd
->sense_phys_addr
);
352 * megasas_teardown_frame_pool_fusion() takes care of freeing
353 * whatever has been allocated
355 if (!cmd
->sg_frame
|| !cmd
->sense
) {
356 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed\n");
357 megasas_teardown_frame_pool_fusion(instance
);
365 * megasas_alloc_cmds_fusion - Allocates the command packets
366 * @instance: Adapter soft state
369 * Each frame has a 32-bit field called context. This context is used to get
370 * back the megasas_cmd_fusion from the frame when a frame gets completed
371 * In this driver, the 32 bit values are the indices into an array cmd_list.
372 * This array is used only to look up the megasas_cmd_fusion given the context.
373 * The free commands themselves are maintained in a linked list called cmd_pool.
375 * cmds are formed in the io_request and sg_frame members of the
376 * megasas_cmd_fusion. The context field is used to get a request descriptor
377 * and is used as SMID of the cmd.
378 * SMID value range is from 1 to max_fw_cmds.
381 megasas_alloc_cmds_fusion(struct megasas_instance
*instance
)
384 u32 max_cmd
, io_frames_sz
;
385 struct fusion_context
*fusion
;
386 struct megasas_cmd_fusion
*cmd
;
387 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
389 dma_addr_t io_req_base_phys
;
392 fusion
= instance
->ctrl_context
;
394 max_cmd
= instance
->max_fw_cmds
;
396 fusion
->req_frames_desc
=
397 dma_alloc_coherent(&instance
->pdev
->dev
,
398 fusion
->request_alloc_sz
,
399 &fusion
->req_frames_desc_phys
, GFP_KERNEL
);
401 if (!fusion
->req_frames_desc
) {
402 printk(KERN_ERR
"megasas; Could not allocate memory for "
407 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
408 fusion
->reply_frames_desc_pool
=
409 pci_pool_create("reply_frames pool", instance
->pdev
,
410 fusion
->reply_alloc_sz
* count
, 16, 0);
412 if (!fusion
->reply_frames_desc_pool
) {
413 printk(KERN_ERR
"megasas; Could not allocate memory for "
414 "reply_frame pool\n");
415 goto fail_reply_desc
;
418 fusion
->reply_frames_desc
=
419 pci_pool_alloc(fusion
->reply_frames_desc_pool
, GFP_KERNEL
,
420 &fusion
->reply_frames_desc_phys
);
421 if (!fusion
->reply_frames_desc
) {
422 printk(KERN_ERR
"megasas; Could not allocate memory for "
423 "reply_frame pool\n");
424 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
425 goto fail_reply_desc
;
428 reply_desc
= fusion
->reply_frames_desc
;
429 for (i
= 0; i
< fusion
->reply_q_depth
* count
; i
++, reply_desc
++)
430 reply_desc
->Words
= ULLONG_MAX
;
432 io_frames_sz
= fusion
->io_frames_alloc_sz
;
434 fusion
->io_request_frames_pool
=
435 pci_pool_create("io_request_frames pool", instance
->pdev
,
436 fusion
->io_frames_alloc_sz
, 16, 0);
438 if (!fusion
->io_request_frames_pool
) {
439 printk(KERN_ERR
"megasas: Could not allocate memory for "
440 "io_request_frame pool\n");
444 fusion
->io_request_frames
=
445 pci_pool_alloc(fusion
->io_request_frames_pool
, GFP_KERNEL
,
446 &fusion
->io_request_frames_phys
);
447 if (!fusion
->io_request_frames
) {
448 printk(KERN_ERR
"megasas: Could not allocate memory for "
449 "io_request_frames frames\n");
450 pci_pool_destroy(fusion
->io_request_frames_pool
);
455 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
456 * Allocate the dynamic array first and then allocate individual
459 fusion
->cmd_list
= kzalloc(sizeof(struct megasas_cmd_fusion
*)
460 * max_cmd
, GFP_KERNEL
);
462 if (!fusion
->cmd_list
) {
463 printk(KERN_DEBUG
"megasas: out of memory. Could not alloc "
464 "memory for cmd_list_fusion\n");
468 max_cmd
= instance
->max_fw_cmds
;
469 for (i
= 0; i
< max_cmd
; i
++) {
470 fusion
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd_fusion
),
472 if (!fusion
->cmd_list
[i
]) {
473 printk(KERN_ERR
"Could not alloc cmd list fusion\n");
475 for (j
= 0; j
< i
; j
++)
476 kfree(fusion
->cmd_list
[j
]);
478 kfree(fusion
->cmd_list
);
479 fusion
->cmd_list
= NULL
;
484 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
485 io_req_base
= fusion
->io_request_frames
+
486 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
487 io_req_base_phys
= fusion
->io_request_frames_phys
+
488 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
491 * Add all the commands to command pool (fusion->cmd_pool)
494 /* SMID 0 is reserved. Set SMID/index from 1 */
495 for (i
= 0; i
< max_cmd
; i
++) {
496 cmd
= fusion
->cmd_list
[i
];
497 offset
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
* i
;
498 memset(cmd
, 0, sizeof(struct megasas_cmd_fusion
));
501 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
; /* Set to Invalid */
502 cmd
->instance
= instance
;
504 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
505 (io_req_base
+ offset
);
506 memset(cmd
->io_request
, 0,
507 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
));
508 cmd
->io_request_phys_addr
= io_req_base_phys
+ offset
;
510 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
514 * Create a frame pool and assign one frame to each cmd
516 if (megasas_create_frame_pool_fusion(instance
)) {
517 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
518 megasas_free_cmds_fusion(instance
);
525 pci_pool_free(fusion
->io_request_frames_pool
, fusion
->io_request_frames
,
526 fusion
->io_request_frames_phys
);
527 pci_pool_destroy(fusion
->io_request_frames_pool
);
529 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
530 fusion
->reply_frames_desc
,
531 fusion
->reply_frames_desc_phys
);
532 pci_pool_free(fusion
->reply_frames_desc_pool
,
533 fusion
->reply_frames_desc
,
534 fusion
->reply_frames_desc_phys
);
535 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
538 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
539 fusion
->req_frames_desc
,
540 fusion
->req_frames_desc_phys
);
546 * wait_and_poll - Issues a polling command
547 * @instance: Adapter soft state
548 * @cmd: Command packet to be issued
550 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
553 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
556 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
558 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
561 * Wait for cmd_status to change
563 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
+= 20) {
568 if (frame_hdr
->cmd_status
== 0xff)
575 * megasas_ioc_init_fusion - Initializes the FW
576 * @instance: Adapter soft state
578 * Issues the IOC Init cmd
581 megasas_ioc_init_fusion(struct megasas_instance
*instance
)
583 struct megasas_init_frame
*init_frame
;
584 struct MPI2_IOC_INIT_REQUEST
*IOCInitMessage
;
585 dma_addr_t ioc_init_handle
;
586 struct megasas_cmd
*cmd
;
588 struct fusion_context
*fusion
;
589 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
591 struct megasas_header
*frame_hdr
;
593 fusion
= instance
->ctrl_context
;
595 cmd
= megasas_get_cmd(instance
);
598 printk(KERN_ERR
"Could not allocate cmd for INIT Frame\n");
604 dma_alloc_coherent(&instance
->pdev
->dev
,
605 sizeof(struct MPI2_IOC_INIT_REQUEST
),
606 &ioc_init_handle
, GFP_KERNEL
);
608 if (!IOCInitMessage
) {
609 printk(KERN_ERR
"Could not allocate memory for "
615 memset(IOCInitMessage
, 0, sizeof(struct MPI2_IOC_INIT_REQUEST
));
617 IOCInitMessage
->Function
= MPI2_FUNCTION_IOC_INIT
;
618 IOCInitMessage
->WhoInit
= MPI2_WHOINIT_HOST_DRIVER
;
619 IOCInitMessage
->MsgVersion
= cpu_to_le16(MPI2_VERSION
);
620 IOCInitMessage
->HeaderVersion
= cpu_to_le16(MPI2_HEADER_VERSION
);
621 IOCInitMessage
->SystemRequestFrameSize
= cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
/ 4);
623 IOCInitMessage
->ReplyDescriptorPostQueueDepth
= cpu_to_le16(fusion
->reply_q_depth
);
624 IOCInitMessage
->ReplyDescriptorPostQueueAddress
= cpu_to_le64(fusion
->reply_frames_desc_phys
);
625 IOCInitMessage
->SystemRequestFrameBaseAddress
= cpu_to_le64(fusion
->io_request_frames_phys
);
626 IOCInitMessage
->HostMSIxVectors
= instance
->msix_vectors
;
627 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
628 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
630 frame_hdr
= &cmd
->frame
->hdr
;
631 frame_hdr
->cmd_status
= 0xFF;
632 frame_hdr
->flags
|= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
);
634 init_frame
->cmd
= MFI_CMD_INIT
;
635 init_frame
->cmd_status
= 0xFF;
637 /* driver support Extended MSIX */
638 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
639 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
640 init_frame
->driver_operations
.
641 mfi_capabilities
.support_additional_msix
= 1;
642 /* driver supports HA / Remote LUN over Fast Path interface */
643 init_frame
->driver_operations
.mfi_capabilities
.support_fp_remote_lun
645 /* Convert capability to LE32 */
646 cpu_to_le32s((u32
*)&init_frame
->driver_operations
.mfi_capabilities
);
648 init_frame
->queue_info_new_phys_addr_lo
= cpu_to_le32((u32
)ioc_init_handle
);
649 init_frame
->data_xfer_len
= cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST
));
652 (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)fusion
->req_frames_desc
;
655 req_desc
->MFAIo
.RequestFlags
=
656 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA
<<
657 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
658 cpu_to_le32s((u32
*)&req_desc
->MFAIo
);
659 req_desc
->Words
|= cpu_to_le64(cmd
->frame_phys_addr
);
662 * disable the intr before firing the init frame
664 instance
->instancet
->disable_intr(instance
);
666 for (i
= 0; i
< (10 * 1000); i
+= 20) {
667 if (readl(&instance
->reg_set
->doorbell
) & 1)
673 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
674 req_desc
->u
.high
, instance
->reg_set
);
676 wait_and_poll(instance
, cmd
);
678 frame_hdr
= &cmd
->frame
->hdr
;
679 if (frame_hdr
->cmd_status
!= 0) {
683 printk(KERN_ERR
"megasas:IOC Init cmd success\n");
688 megasas_return_cmd(instance
, cmd
);
690 dma_free_coherent(&instance
->pdev
->dev
,
691 sizeof(struct MPI2_IOC_INIT_REQUEST
),
692 IOCInitMessage
, ioc_init_handle
);
698 * megasas_get_ld_map_info - Returns FW's ld_map structure
699 * @instance: Adapter soft state
700 * @pend: Pend the command or not
701 * Issues an internal command (DCMD) to get the FW's controller PD
702 * list structure. This information is mainly used to find out SYSTEM
703 * supported by the FW.
706 megasas_get_ld_map_info(struct megasas_instance
*instance
)
709 struct megasas_cmd
*cmd
;
710 struct megasas_dcmd_frame
*dcmd
;
711 struct MR_FW_RAID_MAP_ALL
*ci
;
714 struct fusion_context
*fusion
;
716 cmd
= megasas_get_cmd(instance
);
719 printk(KERN_DEBUG
"megasas: Failed to get cmd for map info.\n");
723 fusion
= instance
->ctrl_context
;
726 megasas_return_cmd(instance
, cmd
);
730 dcmd
= &cmd
->frame
->dcmd
;
732 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
733 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
735 ci
= fusion
->ld_map
[(instance
->map_id
& 1)];
736 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
& 1)];
739 printk(KERN_DEBUG
"Failed to alloc mem for ld_map_info\n");
740 megasas_return_cmd(instance
, cmd
);
744 memset(ci
, 0, sizeof(*ci
));
745 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
747 dcmd
->cmd
= MFI_CMD_DCMD
;
748 dcmd
->cmd_status
= 0xFF;
750 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_READ
);
753 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
754 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
755 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
756 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(size_map_info
);
758 if (!megasas_issue_polled(instance
, cmd
))
761 printk(KERN_ERR
"megasas: Get LD Map Info Failed\n");
765 megasas_return_cmd(instance
, cmd
);
771 megasas_get_map_info(struct megasas_instance
*instance
)
773 struct fusion_context
*fusion
= instance
->ctrl_context
;
775 fusion
->fast_path_io
= 0;
776 if (!megasas_get_ld_map_info(instance
)) {
777 if (MR_ValidateMapInfo(instance
)) {
778 fusion
->fast_path_io
= 1;
786 * megasas_sync_map_info - Returns FW's ld_map structure
787 * @instance: Adapter soft state
789 * Issues an internal command (DCMD) to get the FW's controller PD
790 * list structure. This information is mainly used to find out SYSTEM
791 * supported by the FW.
794 megasas_sync_map_info(struct megasas_instance
*instance
)
797 struct megasas_cmd
*cmd
;
798 struct megasas_dcmd_frame
*dcmd
;
799 u32 size_sync_info
, num_lds
;
800 struct fusion_context
*fusion
;
801 struct MR_LD_TARGET_SYNC
*ci
= NULL
;
802 struct MR_FW_RAID_MAP_ALL
*map
;
803 struct MR_LD_RAID
*raid
;
804 struct MR_LD_TARGET_SYNC
*ld_sync
;
808 cmd
= megasas_get_cmd(instance
);
811 printk(KERN_DEBUG
"megasas: Failed to get cmd for sync"
816 fusion
= instance
->ctrl_context
;
819 megasas_return_cmd(instance
, cmd
);
823 map
= fusion
->ld_map
[instance
->map_id
& 1];
825 num_lds
= le32_to_cpu(map
->raidMap
.ldCount
);
827 dcmd
= &cmd
->frame
->dcmd
;
829 size_sync_info
= sizeof(struct MR_LD_TARGET_SYNC
) *num_lds
;
831 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
833 ci
= (struct MR_LD_TARGET_SYNC
*)
834 fusion
->ld_map
[(instance
->map_id
- 1) & 1];
835 memset(ci
, 0, sizeof(struct MR_FW_RAID_MAP_ALL
));
837 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
- 1) & 1];
839 ld_sync
= (struct MR_LD_TARGET_SYNC
*)ci
;
841 for (i
= 0; i
< num_lds
; i
++, ld_sync
++) {
842 raid
= MR_LdRaidGet(i
, map
);
843 ld_sync
->targetId
= MR_GetLDTgtId(i
, map
);
844 ld_sync
->seqNum
= raid
->seqNum
;
847 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
848 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
850 dcmd
->cmd
= MFI_CMD_DCMD
;
851 dcmd
->cmd_status
= 0xFF;
853 dcmd
->flags
= cpu_to_le16(MFI_FRAME_DIR_WRITE
);
856 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
857 dcmd
->mbox
.b
[0] = num_lds
;
858 dcmd
->mbox
.b
[1] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
859 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
860 dcmd
->sgl
.sge32
[0].phys_addr
= cpu_to_le32(ci_h
);
861 dcmd
->sgl
.sge32
[0].length
= cpu_to_le32(size_map_info
);
863 instance
->map_update_cmd
= cmd
;
865 instance
->instancet
->issue_dcmd(instance
, cmd
);
871 * meagasas_display_intel_branding - Display branding string
872 * @instance: per adapter object
877 megasas_display_intel_branding(struct megasas_instance
*instance
)
879 if (instance
->pdev
->subsystem_vendor
!= PCI_VENDOR_ID_INTEL
)
882 switch (instance
->pdev
->device
) {
883 case PCI_DEVICE_ID_LSI_INVADER
:
884 switch (instance
->pdev
->subsystem_device
) {
885 case MEGARAID_INTEL_RS3DC080_SSDID
:
886 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
887 instance
->host
->host_no
,
888 MEGARAID_INTEL_RS3DC080_BRANDING
);
890 case MEGARAID_INTEL_RS3DC040_SSDID
:
891 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
892 instance
->host
->host_no
,
893 MEGARAID_INTEL_RS3DC040_BRANDING
);
895 case MEGARAID_INTEL_RS3SC008_SSDID
:
896 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
897 instance
->host
->host_no
,
898 MEGARAID_INTEL_RS3SC008_BRANDING
);
900 case MEGARAID_INTEL_RS3MC044_SSDID
:
901 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
902 instance
->host
->host_no
,
903 MEGARAID_INTEL_RS3MC044_BRANDING
);
909 case PCI_DEVICE_ID_LSI_FURY
:
910 switch (instance
->pdev
->subsystem_device
) {
911 case MEGARAID_INTEL_RS3WC080_SSDID
:
912 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
913 instance
->host
->host_no
,
914 MEGARAID_INTEL_RS3WC080_BRANDING
);
916 case MEGARAID_INTEL_RS3WC040_SSDID
:
917 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
918 instance
->host
->host_no
,
919 MEGARAID_INTEL_RS3WC040_BRANDING
);
931 * megasas_init_adapter_fusion - Initializes the FW
932 * @instance: Adapter soft state
934 * This is the main function for initializing firmware.
937 megasas_init_adapter_fusion(struct megasas_instance
*instance
)
939 struct megasas_register_set __iomem
*reg_set
;
940 struct fusion_context
*fusion
;
944 fusion
= instance
->ctrl_context
;
946 reg_set
= instance
->reg_set
;
949 * Get various operational parameters from status register
951 instance
->max_fw_cmds
=
952 instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
953 instance
->max_fw_cmds
= min(instance
->max_fw_cmds
, (u16
)1008);
956 * Reduce the max supported cmds by 1. This is to ensure that the
957 * reply_q_sz (1 more than the max cmd that driver may send)
958 * does not exceed max cmds that the FW can support
960 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
961 /* Only internal cmds (DCMD) need to have MFI frames */
962 instance
->max_mfi_cmds
= MEGASAS_INT_CMDS
;
964 max_cmd
= instance
->max_fw_cmds
;
966 fusion
->reply_q_depth
= ((max_cmd
+ 1 + 15)/16)*16;
968 fusion
->request_alloc_sz
=
969 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *max_cmd
;
970 fusion
->reply_alloc_sz
= sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)
971 *(fusion
->reply_q_depth
);
972 fusion
->io_frames_alloc_sz
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
+
973 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
*
974 (max_cmd
+ 1)); /* Extra 1 for SMID 0 */
976 fusion
->max_sge_in_main_msg
=
977 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
978 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
))/16;
980 fusion
->max_sge_in_chain
=
981 MEGASAS_MAX_SZ_CHAIN_FRAME
/ sizeof(union MPI2_SGE_IO_UNION
);
983 instance
->max_num_sge
= fusion
->max_sge_in_main_msg
+
984 fusion
->max_sge_in_chain
- 2;
986 /* Used for pass thru MFI frame (DCMD) */
987 fusion
->chain_offset_mfi_pthru
=
988 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
)/16;
990 fusion
->chain_offset_io_request
=
991 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
992 sizeof(union MPI2_SGE_IO_UNION
))/16;
994 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
995 for (i
= 0 ; i
< count
; i
++)
996 fusion
->last_reply_idx
[i
] = 0;
999 * Allocate memory for descriptors
1000 * Create a pool of commands
1002 if (megasas_alloc_cmds(instance
))
1003 goto fail_alloc_mfi_cmds
;
1004 if (megasas_alloc_cmds_fusion(instance
))
1005 goto fail_alloc_cmds
;
1007 if (megasas_ioc_init_fusion(instance
))
1010 megasas_display_intel_branding(instance
);
1012 instance
->flag_ieee
= 1;
1014 fusion
->map_sz
= sizeof(struct MR_FW_RAID_MAP
) +
1015 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
1017 fusion
->fast_path_io
= 0;
1019 for (i
= 0; i
< 2; i
++) {
1020 fusion
->ld_map
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
1022 &fusion
->ld_map_phys
[i
],
1024 if (!fusion
->ld_map
[i
]) {
1025 printk(KERN_ERR
"megasas: Could not allocate memory "
1031 if (!megasas_get_map_info(instance
))
1032 megasas_sync_map_info(instance
);
1038 dma_free_coherent(&instance
->pdev
->dev
, fusion
->map_sz
,
1039 fusion
->ld_map
[0], fusion
->ld_map_phys
[0]);
1041 megasas_free_cmds_fusion(instance
);
1043 megasas_free_cmds(instance
);
1044 fail_alloc_mfi_cmds
:
1049 * megasas_fire_cmd_fusion - Sends command to the FW
1050 * @frame_phys_addr : Physical address of cmd
1051 * @frame_count : Number of frames for the command
1052 * @regs : MFI register set
1055 megasas_fire_cmd_fusion(struct megasas_instance
*instance
,
1056 dma_addr_t req_desc_lo
,
1058 struct megasas_register_set __iomem
*regs
)
1060 unsigned long flags
;
1062 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1064 writel(le32_to_cpu(req_desc_lo
), &(regs
)->inbound_low_queue_port
);
1065 writel(le32_to_cpu(req_desc_hi
), &(regs
)->inbound_high_queue_port
);
1066 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1070 * map_cmd_status - Maps FW cmd status to OS cmd status
1071 * @cmd : Pointer to cmd
1072 * @status : status of cmd returned by FW
1073 * @ext_status : ext status of cmd returned by FW
1077 map_cmd_status(struct megasas_cmd_fusion
*cmd
, u8 status
, u8 ext_status
)
1083 cmd
->scmd
->result
= DID_OK
<< 16;
1086 case MFI_STAT_SCSI_IO_FAILED
:
1087 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1088 cmd
->scmd
->result
= (DID_ERROR
<< 16) | ext_status
;
1091 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1093 cmd
->scmd
->result
= (DID_OK
<< 16) | ext_status
;
1094 if (ext_status
== SAM_STAT_CHECK_CONDITION
) {
1095 memset(cmd
->scmd
->sense_buffer
, 0,
1096 SCSI_SENSE_BUFFERSIZE
);
1097 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1098 SCSI_SENSE_BUFFERSIZE
);
1099 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1103 case MFI_STAT_LD_OFFLINE
:
1104 case MFI_STAT_DEVICE_NOT_FOUND
:
1105 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1107 case MFI_STAT_CONFIG_SEQ_MISMATCH
:
1108 cmd
->scmd
->result
= DID_IMM_RETRY
<< 16;
1111 printk(KERN_DEBUG
"megasas: FW status %#x\n", status
);
1112 cmd
->scmd
->result
= DID_ERROR
<< 16;
1118 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1119 * @instance: Adapter soft state
1120 * @scp: SCSI command from the mid-layer
1121 * @sgl_ptr: SGL to be filled in
1122 * @cmd: cmd we are working on
1124 * If successful, this function returns the number of SG elements.
1127 megasas_make_sgl_fusion(struct megasas_instance
*instance
,
1128 struct scsi_cmnd
*scp
,
1129 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
1130 struct megasas_cmd_fusion
*cmd
)
1132 int i
, sg_processed
, sge_count
;
1133 struct scatterlist
*os_sgl
;
1134 struct fusion_context
*fusion
;
1136 fusion
= instance
->ctrl_context
;
1138 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1139 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1140 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
= sgl_ptr
;
1141 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
1142 sgl_ptr_end
->Flags
= 0;
1145 sge_count
= scsi_dma_map(scp
);
1147 BUG_ON(sge_count
< 0);
1149 if (sge_count
> instance
->max_num_sge
|| !sge_count
)
1152 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1153 sgl_ptr
->Length
= cpu_to_le32(sg_dma_len(os_sgl
));
1154 sgl_ptr
->Address
= cpu_to_le64(sg_dma_address(os_sgl
));
1156 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1157 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1158 if (i
== sge_count
- 1)
1159 sgl_ptr
->Flags
= IEEE_SGE_FLAGS_END_OF_LIST
;
1163 sg_processed
= i
+ 1;
1165 if ((sg_processed
== (fusion
->max_sge_in_main_msg
- 1)) &&
1166 (sge_count
> fusion
->max_sge_in_main_msg
)) {
1168 struct MPI25_IEEE_SGE_CHAIN64
*sg_chain
;
1169 if ((instance
->pdev
->device
==
1170 PCI_DEVICE_ID_LSI_INVADER
) ||
1171 (instance
->pdev
->device
==
1172 PCI_DEVICE_ID_LSI_FURY
)) {
1173 if ((le16_to_cpu(cmd
->io_request
->IoFlags
) &
1174 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
) !=
1175 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
)
1176 cmd
->io_request
->ChainOffset
=
1178 chain_offset_io_request
;
1180 cmd
->io_request
->ChainOffset
= 0;
1182 cmd
->io_request
->ChainOffset
=
1183 fusion
->chain_offset_io_request
;
1186 /* Prepare chain element */
1187 sg_chain
->NextChainOffset
= 0;
1188 if ((instance
->pdev
->device
==
1189 PCI_DEVICE_ID_LSI_INVADER
) ||
1190 (instance
->pdev
->device
==
1191 PCI_DEVICE_ID_LSI_FURY
))
1192 sg_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
;
1195 (IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1196 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
);
1197 sg_chain
->Length
= cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION
) * (sge_count
- sg_processed
)));
1198 sg_chain
->Address
= cpu_to_le64(cmd
->sg_frame_phys_addr
);
1201 (struct MPI25_IEEE_SGE_CHAIN64
*)cmd
->sg_frame
;
1209 * megasas_set_pd_lba - Sets PD LBA
1211 * @cdb_len: cdb length
1212 * @start_blk: Start block of IO
1214 * Used to set the PD LBA in CDB for FP IOs
1217 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
, u8 cdb_len
,
1218 struct IO_REQUEST_INFO
*io_info
, struct scsi_cmnd
*scp
,
1219 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
, u32 ref_tag
)
1221 struct MR_LD_RAID
*raid
;
1223 u64 start_blk
= io_info
->pdBlock
;
1224 u8
*cdb
= io_request
->CDB
.CDB32
;
1225 u32 num_blocks
= io_info
->numBlocks
;
1226 u8 opcode
= 0, flagvals
= 0, groupnum
= 0, control
= 0;
1228 /* Check if T10 PI (DIF) is enabled for this LD */
1229 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, local_map_ptr
);
1230 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1231 if (raid
->capability
.ldPiMode
== MR_PROT_INFO_TYPE_CONTROLLER
) {
1232 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1233 cdb
[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD
;
1234 cdb
[7] = MEGASAS_SCSI_ADDL_CDB_LEN
;
1236 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1237 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32
;
1239 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32
;
1240 cdb
[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL
;
1243 cdb
[12] = (u8
)((start_blk
>> 56) & 0xff);
1244 cdb
[13] = (u8
)((start_blk
>> 48) & 0xff);
1245 cdb
[14] = (u8
)((start_blk
>> 40) & 0xff);
1246 cdb
[15] = (u8
)((start_blk
>> 32) & 0xff);
1247 cdb
[16] = (u8
)((start_blk
>> 24) & 0xff);
1248 cdb
[17] = (u8
)((start_blk
>> 16) & 0xff);
1249 cdb
[18] = (u8
)((start_blk
>> 8) & 0xff);
1250 cdb
[19] = (u8
)(start_blk
& 0xff);
1252 /* Logical block reference tag */
1253 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
=
1254 cpu_to_be32(ref_tag
);
1255 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0xffff;
1256 io_request
->IoFlags
= cpu_to_le16(32); /* Specify 32-byte cdb */
1258 /* Transfer length */
1259 cdb
[28] = (u8
)((num_blocks
>> 24) & 0xff);
1260 cdb
[29] = (u8
)((num_blocks
>> 16) & 0xff);
1261 cdb
[30] = (u8
)((num_blocks
>> 8) & 0xff);
1262 cdb
[31] = (u8
)(num_blocks
& 0xff);
1264 /* set SCSI IO EEDPFlags */
1265 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
) {
1266 io_request
->EEDPFlags
= cpu_to_le16(
1267 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1268 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG
|
1269 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
|
1270 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG
|
1271 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD
);
1273 io_request
->EEDPFlags
= cpu_to_le16(
1274 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1275 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
);
1277 io_request
->Control
|= cpu_to_le32((0x4 << 26));
1278 io_request
->EEDPBlockSize
= cpu_to_le32(scp
->device
->sector_size
);
1280 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1281 if (((cdb_len
== 12) || (cdb_len
== 16)) &&
1282 (start_blk
<= 0xffffffff)) {
1283 if (cdb_len
== 16) {
1284 opcode
= cdb
[0] == READ_16
? READ_10
: WRITE_10
;
1289 opcode
= cdb
[0] == READ_12
? READ_10
: WRITE_10
;
1295 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1302 /* Transfer length */
1303 cdb
[8] = (u8
)(num_blocks
& 0xff);
1304 cdb
[7] = (u8
)((num_blocks
>> 8) & 0xff);
1306 io_request
->IoFlags
= cpu_to_le16(10); /* Specify 10-byte cdb */
1308 } else if ((cdb_len
< 16) && (start_blk
> 0xffffffff)) {
1309 /* Convert to 16 byte CDB for large LBA's */
1312 opcode
= cdb
[0] == READ_6
? READ_16
: WRITE_16
;
1317 cdb
[0] == READ_10
? READ_16
: WRITE_16
;
1324 cdb
[0] == READ_12
? READ_16
: WRITE_16
;
1331 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1338 /* Transfer length */
1339 cdb
[13] = (u8
)(num_blocks
& 0xff);
1340 cdb
[12] = (u8
)((num_blocks
>> 8) & 0xff);
1341 cdb
[11] = (u8
)((num_blocks
>> 16) & 0xff);
1342 cdb
[10] = (u8
)((num_blocks
>> 24) & 0xff);
1344 io_request
->IoFlags
= cpu_to_le16(16); /* Specify 16-byte cdb */
1348 /* Normal case, just load LBA here */
1352 u8 val
= cdb
[1] & 0xE0;
1353 cdb
[3] = (u8
)(start_blk
& 0xff);
1354 cdb
[2] = (u8
)((start_blk
>> 8) & 0xff);
1355 cdb
[1] = val
| ((u8
)(start_blk
>> 16) & 0x1f);
1359 cdb
[5] = (u8
)(start_blk
& 0xff);
1360 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1361 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1362 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1365 cdb
[5] = (u8
)(start_blk
& 0xff);
1366 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1367 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1368 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1371 cdb
[9] = (u8
)(start_blk
& 0xff);
1372 cdb
[8] = (u8
)((start_blk
>> 8) & 0xff);
1373 cdb
[7] = (u8
)((start_blk
>> 16) & 0xff);
1374 cdb
[6] = (u8
)((start_blk
>> 24) & 0xff);
1375 cdb
[5] = (u8
)((start_blk
>> 32) & 0xff);
1376 cdb
[4] = (u8
)((start_blk
>> 40) & 0xff);
1377 cdb
[3] = (u8
)((start_blk
>> 48) & 0xff);
1378 cdb
[2] = (u8
)((start_blk
>> 56) & 0xff);
1385 * megasas_build_ldio_fusion - Prepares IOs to devices
1386 * @instance: Adapter soft state
1387 * @scp: SCSI command
1388 * @cmd: Command to be prepared
1390 * Prepares the io_request and chain elements (sg_frame) for IO
1391 * The IO can be for PD (Fast Path) or LD
1394 megasas_build_ldio_fusion(struct megasas_instance
*instance
,
1395 struct scsi_cmnd
*scp
,
1396 struct megasas_cmd_fusion
*cmd
)
1399 u32 start_lba_lo
, start_lba_hi
, device_id
, datalength
= 0;
1400 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1401 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1402 struct IO_REQUEST_INFO io_info
;
1403 struct fusion_context
*fusion
;
1404 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1407 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1409 fusion
= instance
->ctrl_context
;
1411 io_request
= cmd
->io_request
;
1412 io_request
->RaidContext
.VirtualDiskTgtId
= cpu_to_le16(device_id
);
1413 io_request
->RaidContext
.status
= 0;
1414 io_request
->RaidContext
.exStatus
= 0;
1416 req_desc
= (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)cmd
->request_desc
;
1423 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1425 if (scp
->cmd_len
== 6) {
1426 datalength
= (u32
) scp
->cmnd
[4];
1427 start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1428 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1430 start_lba_lo
&= 0x1FFFFF;
1434 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1436 else if (scp
->cmd_len
== 10) {
1437 datalength
= (u32
) scp
->cmnd
[8] |
1438 ((u32
) scp
->cmnd
[7] << 8);
1439 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1440 ((u32
) scp
->cmnd
[3] << 16) |
1441 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1445 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1447 else if (scp
->cmd_len
== 12) {
1448 datalength
= ((u32
) scp
->cmnd
[6] << 24) |
1449 ((u32
) scp
->cmnd
[7] << 16) |
1450 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1451 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1452 ((u32
) scp
->cmnd
[3] << 16) |
1453 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1457 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1459 else if (scp
->cmd_len
== 16) {
1460 datalength
= ((u32
) scp
->cmnd
[10] << 24) |
1461 ((u32
) scp
->cmnd
[11] << 16) |
1462 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1463 start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1464 ((u32
) scp
->cmnd
[7] << 16) |
1465 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1467 start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1468 ((u32
) scp
->cmnd
[3] << 16) |
1469 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1472 memset(&io_info
, 0, sizeof(struct IO_REQUEST_INFO
));
1473 io_info
.ldStartBlock
= ((u64
)start_lba_hi
<< 32) | start_lba_lo
;
1474 io_info
.numBlocks
= datalength
;
1475 io_info
.ldTgtId
= device_id
;
1476 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scp
));
1478 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1481 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1483 if ((MR_TargetIdToLdGet(device_id
, local_map_ptr
) >=
1484 MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
)) {
1485 io_request
->RaidContext
.regLockFlags
= 0;
1488 if (MR_BuildRaidContext(instance
, &io_info
,
1489 &io_request
->RaidContext
,
1490 local_map_ptr
, &raidLUN
))
1491 fp_possible
= io_info
.fpOkForIo
;
1494 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1495 id by default, not CPU group id, otherwise all MSI-X queues won't
1497 cmd
->request_desc
->SCSIIO
.MSIxIndex
= instance
->msix_vectors
?
1498 smp_processor_id() % instance
->msix_vectors
: 0;
1501 megasas_set_pd_lba(io_request
, scp
->cmd_len
, &io_info
, scp
,
1502 local_map_ptr
, start_lba_lo
);
1503 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1504 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1505 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1506 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1507 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1508 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1509 if (io_request
->RaidContext
.regLockFlags
==
1511 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1512 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK
<<
1513 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1514 io_request
->RaidContext
.Type
= MPI2_TYPE_CUDA
;
1515 io_request
->RaidContext
.nseg
= 0x1;
1516 io_request
->IoFlags
|= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
);
1517 io_request
->RaidContext
.regLockFlags
|=
1518 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA
|
1519 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
1521 if ((fusion
->load_balance_info
[device_id
].loadBalanceFlag
) &&
1524 get_updated_dev_handle(
1525 &fusion
->load_balance_info
[device_id
],
1527 scp
->SCp
.Status
|= MEGASAS_LOAD_BALANCE_FLAG
;
1529 scp
->SCp
.Status
&= ~MEGASAS_LOAD_BALANCE_FLAG
;
1530 cmd
->request_desc
->SCSIIO
.DevHandle
= io_info
.devHandle
;
1531 io_request
->DevHandle
= io_info
.devHandle
;
1532 /* populate the LUN field */
1533 memcpy(io_request
->LUN
, raidLUN
, 8);
1535 io_request
->RaidContext
.timeoutValue
=
1536 cpu_to_le16(local_map_ptr
->raidMap
.fpPdIoTimeoutSec
);
1537 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1538 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1539 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1540 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1541 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
1542 if (io_request
->RaidContext
.regLockFlags
==
1544 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1545 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK
<<
1546 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1547 io_request
->RaidContext
.Type
= MPI2_TYPE_CUDA
;
1548 io_request
->RaidContext
.regLockFlags
|=
1549 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0
|
1550 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
1551 io_request
->RaidContext
.nseg
= 0x1;
1553 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1554 io_request
->DevHandle
= cpu_to_le16(device_id
);
1559 * megasas_build_dcdb_fusion - Prepares IOs to devices
1560 * @instance: Adapter soft state
1561 * @scp: SCSI command
1562 * @cmd: Command to be prepared
1564 * Prepares the io_request frame for non-io cmds
1567 megasas_build_dcdb_fusion(struct megasas_instance
*instance
,
1568 struct scsi_cmnd
*scmd
,
1569 struct megasas_cmd_fusion
*cmd
)
1572 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1574 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1575 struct fusion_context
*fusion
= instance
->ctrl_context
;
1579 struct MR_LD_RAID
*raid
;
1580 struct RAID_CONTEXT
*pRAID_Context
;
1582 io_request
= cmd
->io_request
;
1583 device_id
= MEGASAS_DEV_INDEX(instance
, scmd
);
1584 pd_index
= (scmd
->device
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
)
1586 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1588 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scmd
));
1591 /* Check if this is a system PD I/O */
1592 if (scmd
->device
->channel
< MEGASAS_MAX_PD_CHANNELS
&&
1593 instance
->pd_list
[pd_index
].driveState
== MR_PD_STATE_SYSTEM
) {
1594 io_request
->Function
= 0;
1595 if (fusion
->fast_path_io
)
1596 io_request
->DevHandle
=
1597 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1598 io_request
->RaidContext
.timeoutValue
=
1599 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1600 io_request
->RaidContext
.regLockFlags
= 0;
1601 io_request
->RaidContext
.regLockRowLBA
= 0;
1602 io_request
->RaidContext
.regLockLength
= 0;
1603 io_request
->RaidContext
.RAIDFlags
=
1604 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
<<
1605 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
;
1606 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1607 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1608 io_request
->IoFlags
|=
1609 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
;
1610 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1611 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1612 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1613 cmd
->request_desc
->SCSIIO
.DevHandle
=
1614 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1616 * If the command is for the tape device, set the
1617 * FP timeout to the os layer timeout value.
1619 if (scmd
->device
->type
== TYPE_TAPE
) {
1620 if ((scmd
->request
->timeout
/ HZ
) > 0xFFFF)
1621 io_request
->RaidContext
.timeoutValue
=
1624 io_request
->RaidContext
.timeoutValue
=
1625 scmd
->request
->timeout
/ HZ
;
1628 if (scmd
->device
->channel
< MEGASAS_MAX_PD_CHANNELS
)
1631 ld
= MR_TargetIdToLdGet(device_id
, local_map_ptr
);
1632 if ((ld
>= MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
))
1635 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1637 /* check if this LD is FP capable */
1638 if (!(raid
->capability
.fpNonRWCapable
))
1639 /* not FP capable, send as non-FP */
1642 /* get RAID_Context pointer */
1643 pRAID_Context
= &io_request
->RaidContext
;
1645 /* set RAID context values */
1646 pRAID_Context
->regLockFlags
= REGION_TYPE_SHARED_READ
;
1647 pRAID_Context
->timeoutValue
= raid
->fpIoTimeoutForLd
;
1648 pRAID_Context
->VirtualDiskTgtId
= cpu_to_le16(device_id
);
1649 pRAID_Context
->regLockRowLBA
= 0;
1650 pRAID_Context
->regLockLength
= 0;
1651 pRAID_Context
->configSeqNum
= raid
->seqNum
;
1653 /* get the DevHandle for the PD (since this is
1654 fpNonRWCapable, this is a single disk RAID0) */
1656 arRef
= MR_LdSpanArrayGet(ld
, span
, local_map_ptr
);
1657 pd
= MR_ArPdGet(arRef
, physArm
, local_map_ptr
);
1658 devHandle
= MR_PdDevHandleGet(pd
, local_map_ptr
);
1660 /* build request descriptor */
1661 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1662 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1663 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1664 cmd
->request_desc
->SCSIIO
.DevHandle
= devHandle
;
1666 /* populate the LUN field */
1667 memcpy(io_request
->LUN
, raid
->LUN
, 8);
1669 /* build the raidScsiIO structure */
1670 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1671 io_request
->DevHandle
= devHandle
;
1676 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1677 io_request
->DevHandle
= cpu_to_le16(device_id
);
1678 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1679 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1680 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1682 io_request
->RaidContext
.VirtualDiskTgtId
= cpu_to_le16(device_id
);
1683 io_request
->LUN
[1] = scmd
->device
->lun
;
1687 * megasas_build_io_fusion - Prepares IOs to devices
1688 * @instance: Adapter soft state
1689 * @scp: SCSI command
1690 * @cmd: Command to be prepared
1692 * Invokes helper functions to prepare request frames
1693 * and sets flags appropriate for IO/Non-IO cmd
1696 megasas_build_io_fusion(struct megasas_instance
*instance
,
1697 struct scsi_cmnd
*scp
,
1698 struct megasas_cmd_fusion
*cmd
)
1700 u32 device_id
, sge_count
;
1701 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
= cmd
->io_request
;
1703 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1705 /* Zero out some fields so they don't get reused */
1706 io_request
->LUN
[1] = 0;
1707 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
= 0;
1708 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0;
1709 io_request
->EEDPFlags
= 0;
1710 io_request
->Control
= 0;
1711 io_request
->EEDPBlockSize
= 0;
1712 io_request
->ChainOffset
= 0;
1713 io_request
->RaidContext
.RAIDFlags
= 0;
1714 io_request
->RaidContext
.Type
= 0;
1715 io_request
->RaidContext
.nseg
= 0;
1717 memcpy(io_request
->CDB
.CDB32
, scp
->cmnd
, scp
->cmd_len
);
1719 * Just the CDB length,rest of the Flags are zero
1720 * This will be modified for FP in build_ldio_fusion
1722 io_request
->IoFlags
= cpu_to_le16(scp
->cmd_len
);
1724 if (megasas_is_ldio(scp
))
1725 megasas_build_ldio_fusion(instance
, scp
, cmd
);
1727 megasas_build_dcdb_fusion(instance
, scp
, cmd
);
1734 megasas_make_sgl_fusion(instance
, scp
,
1735 (struct MPI25_IEEE_SGE_CHAIN64
*)
1736 &io_request
->SGL
, cmd
);
1738 if (sge_count
> instance
->max_num_sge
) {
1739 printk(KERN_ERR
"megasas: Error. sge_count (0x%x) exceeds "
1740 "max (0x%x) allowed\n", sge_count
,
1741 instance
->max_num_sge
);
1745 io_request
->RaidContext
.numSGE
= sge_count
;
1747 io_request
->SGLFlags
= cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING
);
1749 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1750 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE
);
1751 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1752 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ
);
1754 io_request
->SGLOffset0
=
1755 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
) / 4;
1757 io_request
->SenseBufferLowAddress
= cpu_to_le32(cmd
->sense_phys_addr
);
1758 io_request
->SenseBufferLength
= SCSI_SENSE_BUFFERSIZE
;
1761 scp
->SCp
.ptr
= (char *)cmd
;
1766 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1767 megasas_get_request_descriptor(struct megasas_instance
*instance
, u16 index
)
1770 struct fusion_context
*fusion
;
1772 if (index
>= instance
->max_fw_cmds
) {
1773 printk(KERN_ERR
"megasas: Invalid SMID (0x%x)request for "
1774 "descriptor\n", index
);
1777 fusion
= instance
->ctrl_context
;
1778 p
= fusion
->req_frames_desc
1779 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *index
;
1781 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)p
;
1785 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1786 * issuing non IOCTL cmd
1787 * @instance: Adapter soft state
1788 * @scmd: pointer to scsi cmd from OS
1791 megasas_build_and_issue_cmd_fusion(struct megasas_instance
*instance
,
1792 struct scsi_cmnd
*scmd
)
1794 struct megasas_cmd_fusion
*cmd
;
1795 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1797 struct fusion_context
*fusion
;
1799 fusion
= instance
->ctrl_context
;
1801 cmd
= megasas_get_cmd_fusion(instance
);
1803 return SCSI_MLQUEUE_HOST_BUSY
;
1807 req_desc
= megasas_get_request_descriptor(instance
, index
-1);
1811 req_desc
->Words
= 0;
1812 cmd
->request_desc
= req_desc
;
1814 if (megasas_build_io_fusion(instance
, scmd
, cmd
)) {
1815 megasas_return_cmd_fusion(instance
, cmd
);
1816 printk(KERN_ERR
"megasas: Error building command.\n");
1817 cmd
->request_desc
= NULL
;
1821 req_desc
= cmd
->request_desc
;
1822 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
1824 if (cmd
->io_request
->ChainOffset
!= 0 &&
1825 cmd
->io_request
->ChainOffset
!= 0xF)
1826 printk(KERN_ERR
"megasas: The chain offset value is not "
1827 "correct : %x\n", cmd
->io_request
->ChainOffset
);
1830 * Issue the command to the FW
1832 atomic_inc(&instance
->fw_outstanding
);
1834 instance
->instancet
->fire_cmd(instance
,
1835 req_desc
->u
.low
, req_desc
->u
.high
,
1842 * complete_cmd_fusion - Completes command
1843 * @instance: Adapter soft state
1844 * Completes all commands that is in reply descriptor queue
1847 complete_cmd_fusion(struct megasas_instance
*instance
, u32 MSIxIndex
)
1849 union MPI2_REPLY_DESCRIPTORS_UNION
*desc
;
1850 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*reply_desc
;
1851 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
1852 struct fusion_context
*fusion
;
1853 struct megasas_cmd
*cmd_mfi
;
1854 struct megasas_cmd_fusion
*cmd_fusion
;
1855 u16 smid
, num_completed
;
1856 u8 reply_descript_type
, arm
;
1857 u32 status
, extStatus
, device_id
;
1858 union desc_value d_val
;
1859 struct LD_LOAD_BALANCE_INFO
*lbinfo
;
1861 fusion
= instance
->ctrl_context
;
1863 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1866 desc
= fusion
->reply_frames_desc
;
1867 desc
+= ((MSIxIndex
* fusion
->reply_alloc_sz
)/
1868 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)) +
1869 fusion
->last_reply_idx
[MSIxIndex
];
1871 reply_desc
= (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1873 d_val
.word
= desc
->Words
;
1875 reply_descript_type
= reply_desc
->ReplyFlags
&
1876 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1878 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1883 while ((d_val
.u
.low
!= UINT_MAX
) && (d_val
.u
.high
!= UINT_MAX
)) {
1884 smid
= le16_to_cpu(reply_desc
->SMID
);
1886 cmd_fusion
= fusion
->cmd_list
[smid
- 1];
1889 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
1890 cmd_fusion
->io_request
;
1892 if (cmd_fusion
->scmd
)
1893 cmd_fusion
->scmd
->SCp
.ptr
= NULL
;
1895 status
= scsi_io_req
->RaidContext
.status
;
1896 extStatus
= scsi_io_req
->RaidContext
.exStatus
;
1898 switch (scsi_io_req
->Function
) {
1899 case MPI2_FUNCTION_SCSI_IO_REQUEST
: /*Fast Path IO.*/
1900 /* Update load balancing info */
1901 device_id
= MEGASAS_DEV_INDEX(instance
,
1903 lbinfo
= &fusion
->load_balance_info
[device_id
];
1904 if (cmd_fusion
->scmd
->SCp
.Status
&
1905 MEGASAS_LOAD_BALANCE_FLAG
) {
1906 arm
= lbinfo
->raid1DevHandle
[0] ==
1907 cmd_fusion
->io_request
->DevHandle
? 0 :
1909 atomic_dec(&lbinfo
->scsi_pending_cmds
[arm
]);
1910 cmd_fusion
->scmd
->SCp
.Status
&=
1911 ~MEGASAS_LOAD_BALANCE_FLAG
;
1913 if (reply_descript_type
==
1914 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS
) {
1915 if (megasas_dbg_lvl
== 5)
1916 printk(KERN_ERR
"\nmegasas: FAST Path "
1919 /* Fall thru and complete IO */
1920 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
: /* LD-IO Path */
1921 /* Map the FW Cmd Status */
1922 map_cmd_status(cmd_fusion
, status
, extStatus
);
1923 scsi_dma_unmap(cmd_fusion
->scmd
);
1924 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
1925 scsi_io_req
->RaidContext
.status
= 0;
1926 scsi_io_req
->RaidContext
.exStatus
= 0;
1927 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1928 atomic_dec(&instance
->fw_outstanding
);
1931 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
: /*MFI command */
1932 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
1933 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
1934 cmd_fusion
->flags
= 0;
1935 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1940 fusion
->last_reply_idx
[MSIxIndex
]++;
1941 if (fusion
->last_reply_idx
[MSIxIndex
] >=
1942 fusion
->reply_q_depth
)
1943 fusion
->last_reply_idx
[MSIxIndex
] = 0;
1945 desc
->Words
= ULLONG_MAX
;
1948 /* Get the next reply descriptor */
1949 if (!fusion
->last_reply_idx
[MSIxIndex
])
1950 desc
= fusion
->reply_frames_desc
+
1951 ((MSIxIndex
* fusion
->reply_alloc_sz
)/
1952 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
));
1957 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1959 d_val
.word
= desc
->Words
;
1961 reply_descript_type
= reply_desc
->ReplyFlags
&
1962 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1964 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1972 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
1973 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
))
1974 writel(((MSIxIndex
& 0x7) << 24) |
1975 fusion
->last_reply_idx
[MSIxIndex
],
1976 instance
->reply_post_host_index_addr
[MSIxIndex
/8]);
1978 writel((MSIxIndex
<< 24) |
1979 fusion
->last_reply_idx
[MSIxIndex
],
1980 instance
->reply_post_host_index_addr
[0]);
1981 megasas_check_and_restore_queue_depth(instance
);
1986 * megasas_complete_cmd_dpc_fusion - Completes command
1987 * @instance: Adapter soft state
1989 * Tasklet to complete cmds
1992 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr
)
1994 struct megasas_instance
*instance
=
1995 (struct megasas_instance
*)instance_addr
;
1996 unsigned long flags
;
1997 u32 count
, MSIxIndex
;
1999 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
2001 /* If we have already declared adapter dead, donot complete cmds */
2002 spin_lock_irqsave(&instance
->hba_lock
, flags
);
2003 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2004 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2007 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
2009 for (MSIxIndex
= 0 ; MSIxIndex
< count
; MSIxIndex
++)
2010 complete_cmd_fusion(instance
, MSIxIndex
);
2014 * megasas_isr_fusion - isr entry point
2016 irqreturn_t
megasas_isr_fusion(int irq
, void *devp
)
2018 struct megasas_irq_context
*irq_context
= devp
;
2019 struct megasas_instance
*instance
= irq_context
->instance
;
2020 u32 mfiStatus
, fw_state
;
2022 if (instance
->mask_interrupts
)
2025 if (!instance
->msix_vectors
) {
2026 mfiStatus
= instance
->instancet
->clear_intr(instance
->reg_set
);
2031 /* If we are resetting, bail */
2032 if (test_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
)) {
2033 instance
->instancet
->clear_intr(instance
->reg_set
);
2037 if (!complete_cmd_fusion(instance
, irq_context
->MSIxIndex
)) {
2038 instance
->instancet
->clear_intr(instance
->reg_set
);
2039 /* If we didn't complete any commands, check for FW fault */
2040 fw_state
= instance
->instancet
->read_fw_status_reg(
2041 instance
->reg_set
) & MFI_STATE_MASK
;
2042 if (fw_state
== MFI_STATE_FAULT
)
2043 schedule_work(&instance
->work_init
);
2050 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2051 * @instance: Adapter soft state
2052 * mfi_cmd: megasas_cmd pointer
2056 build_mpt_mfi_pass_thru(struct megasas_instance
*instance
,
2057 struct megasas_cmd
*mfi_cmd
)
2059 struct MPI25_IEEE_SGE_CHAIN64
*mpi25_ieee_chain
;
2060 struct MPI2_RAID_SCSI_IO_REQUEST
*io_req
;
2061 struct megasas_cmd_fusion
*cmd
;
2062 struct fusion_context
*fusion
;
2063 struct megasas_header
*frame_hdr
= &mfi_cmd
->frame
->hdr
;
2065 cmd
= megasas_get_cmd_fusion(instance
);
2069 /* Save the smid. To be used for returning the cmd */
2070 mfi_cmd
->context
.smid
= cmd
->index
;
2072 cmd
->sync_cmd_idx
= mfi_cmd
->index
;
2075 * For cmds where the flag is set, store the flag and check
2076 * on completion. For cmds with this flag, don't call
2077 * megasas_complete_cmd
2080 if (frame_hdr
->flags
& MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
)
2081 cmd
->flags
= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
2083 fusion
= instance
->ctrl_context
;
2084 io_req
= cmd
->io_request
;
2086 if ((instance
->pdev
->device
== PCI_DEVICE_ID_LSI_INVADER
) ||
2087 (instance
->pdev
->device
== PCI_DEVICE_ID_LSI_FURY
)) {
2088 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
=
2089 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
;
2090 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
2091 sgl_ptr_end
->Flags
= 0;
2095 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
.IeeeChain
;
2097 io_req
->Function
= MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
;
2098 io_req
->SGLOffset0
= offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
,
2100 io_req
->ChainOffset
= fusion
->chain_offset_mfi_pthru
;
2102 mpi25_ieee_chain
->Address
= cpu_to_le64(mfi_cmd
->frame_phys_addr
);
2104 mpi25_ieee_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
2105 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
;
2107 mpi25_ieee_chain
->Length
= cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME
);
2113 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2114 * @instance: Adapter soft state
2115 * @cmd: mfi cmd to build
2118 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
2119 build_mpt_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
2121 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2124 if (build_mpt_mfi_pass_thru(instance
, cmd
)) {
2125 printk(KERN_ERR
"Couldn't build MFI pass thru cmd\n");
2129 index
= cmd
->context
.smid
;
2131 req_desc
= megasas_get_request_descriptor(instance
, index
- 1);
2136 req_desc
->Words
= 0;
2137 req_desc
->SCSIIO
.RequestFlags
= (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
2138 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
2140 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
2146 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2147 * @instance: Adapter soft state
2148 * @cmd: mfi cmd pointer
2152 megasas_issue_dcmd_fusion(struct megasas_instance
*instance
,
2153 struct megasas_cmd
*cmd
)
2155 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2157 req_desc
= build_mpt_cmd(instance
, cmd
);
2159 printk(KERN_ERR
"Couldn't issue MFI pass thru cmd\n");
2162 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
2163 req_desc
->u
.high
, instance
->reg_set
);
2167 * megasas_release_fusion - Reverses the FW initialization
2168 * @intance: Adapter soft state
2171 megasas_release_fusion(struct megasas_instance
*instance
)
2173 megasas_free_cmds(instance
);
2174 megasas_free_cmds_fusion(instance
);
2176 iounmap(instance
->reg_set
);
2178 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
2182 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2183 * @regs: MFI register set
2186 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem
*regs
)
2188 return readl(&(regs
)->outbound_scratch_pad
);
2192 * megasas_adp_reset_fusion - For controller reset
2193 * @regs: MFI register set
2196 megasas_adp_reset_fusion(struct megasas_instance
*instance
,
2197 struct megasas_register_set __iomem
*regs
)
2203 * megasas_check_reset_fusion - For controller reset check
2204 * @regs: MFI register set
2207 megasas_check_reset_fusion(struct megasas_instance
*instance
,
2208 struct megasas_register_set __iomem
*regs
)
2213 /* This function waits for outstanding commands on fusion to complete */
2214 int megasas_wait_for_outstanding_fusion(struct megasas_instance
*instance
)
2216 int i
, outstanding
, retval
= 0;
2219 for (i
= 0; i
< resetwaittime
; i
++) {
2220 /* Check if firmware is in fault state */
2221 fw_state
= instance
->instancet
->read_fw_status_reg(
2222 instance
->reg_set
) & MFI_STATE_MASK
;
2223 if (fw_state
== MFI_STATE_FAULT
) {
2224 printk(KERN_WARNING
"megasas: Found FW in FAULT state,"
2225 " will reset adapter.\n");
2230 outstanding
= atomic_read(&instance
->fw_outstanding
);
2234 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
2235 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
2236 "commands to complete\n", i
, outstanding
);
2237 megasas_complete_cmd_dpc_fusion(
2238 (unsigned long)instance
);
2243 if (atomic_read(&instance
->fw_outstanding
)) {
2244 printk("megaraid_sas: pending commands remain after waiting, "
2245 "will reset adapter.\n");
2252 void megasas_reset_reply_desc(struct megasas_instance
*instance
)
2255 struct fusion_context
*fusion
;
2256 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
2258 fusion
= instance
->ctrl_context
;
2259 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
2260 for (i
= 0 ; i
< count
; i
++)
2261 fusion
->last_reply_idx
[i
] = 0;
2262 reply_desc
= fusion
->reply_frames_desc
;
2263 for (i
= 0 ; i
< fusion
->reply_q_depth
* count
; i
++, reply_desc
++)
2264 reply_desc
->Words
= ULLONG_MAX
;
2267 /* Core fusion reset function */
2268 int megasas_reset_fusion(struct Scsi_Host
*shost
)
2270 int retval
= SUCCESS
, i
, j
, retry
= 0;
2271 struct megasas_instance
*instance
;
2272 struct megasas_cmd_fusion
*cmd_fusion
;
2273 struct fusion_context
*fusion
;
2274 struct megasas_cmd
*cmd_mfi
;
2275 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2276 u32 host_diag
, abs_state
, status_reg
, reset_adapter
;
2278 instance
= (struct megasas_instance
*)shost
->hostdata
;
2279 fusion
= instance
->ctrl_context
;
2281 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2282 printk(KERN_WARNING
"megaraid_sas: Hardware critical error, "
2283 "returning FAILED.\n");
2287 mutex_lock(&instance
->reset_mutex
);
2288 set_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2289 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2290 instance
->instancet
->disable_intr(instance
);
2293 /* First try waiting for commands to complete */
2294 if (megasas_wait_for_outstanding_fusion(instance
)) {
2295 printk(KERN_WARNING
"megaraid_sas: resetting fusion "
2297 /* Now return commands back to the OS */
2298 for (i
= 0 ; i
< instance
->max_fw_cmds
; i
++) {
2299 cmd_fusion
= fusion
->cmd_list
[i
];
2300 if (cmd_fusion
->scmd
) {
2301 scsi_dma_unmap(cmd_fusion
->scmd
);
2302 cmd_fusion
->scmd
->result
= (DID_RESET
<< 16);
2303 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
2304 megasas_return_cmd_fusion(instance
, cmd_fusion
);
2305 atomic_dec(&instance
->fw_outstanding
);
2309 status_reg
= instance
->instancet
->read_fw_status_reg(
2311 abs_state
= status_reg
& MFI_STATE_MASK
;
2312 reset_adapter
= status_reg
& MFI_RESET_ADAPTER
;
2313 if (instance
->disableOnlineCtrlReset
||
2314 (abs_state
== MFI_STATE_FAULT
&& !reset_adapter
)) {
2315 /* Reset not supported, kill adapter */
2316 printk(KERN_WARNING
"megaraid_sas: Reset not supported"
2317 ", killing adapter.\n");
2318 megaraid_sas_kill_hba(instance
);
2319 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2324 /* Now try to reset the chip */
2325 for (i
= 0; i
< MEGASAS_FUSION_MAX_RESET_TRIES
; i
++) {
2326 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE
,
2327 &instance
->reg_set
->fusion_seq_offset
);
2328 writel(MPI2_WRSEQ_1ST_KEY_VALUE
,
2329 &instance
->reg_set
->fusion_seq_offset
);
2330 writel(MPI2_WRSEQ_2ND_KEY_VALUE
,
2331 &instance
->reg_set
->fusion_seq_offset
);
2332 writel(MPI2_WRSEQ_3RD_KEY_VALUE
,
2333 &instance
->reg_set
->fusion_seq_offset
);
2334 writel(MPI2_WRSEQ_4TH_KEY_VALUE
,
2335 &instance
->reg_set
->fusion_seq_offset
);
2336 writel(MPI2_WRSEQ_5TH_KEY_VALUE
,
2337 &instance
->reg_set
->fusion_seq_offset
);
2338 writel(MPI2_WRSEQ_6TH_KEY_VALUE
,
2339 &instance
->reg_set
->fusion_seq_offset
);
2341 /* Check that the diag write enable (DRWE) bit is on */
2342 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2344 while (!(host_diag
& HOST_DIAG_WRITE_ENABLE
)) {
2347 readl(&instance
->reg_set
->fusion_host_diag
);
2348 if (retry
++ == 100) {
2349 printk(KERN_WARNING
"megaraid_sas: "
2350 "Host diag unlock failed!\n");
2354 if (!(host_diag
& HOST_DIAG_WRITE_ENABLE
))
2357 /* Send chip reset command */
2358 writel(host_diag
| HOST_DIAG_RESET_ADAPTER
,
2359 &instance
->reg_set
->fusion_host_diag
);
2362 /* Make sure reset adapter bit is cleared */
2363 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2365 while (host_diag
& HOST_DIAG_RESET_ADAPTER
) {
2368 readl(&instance
->reg_set
->fusion_host_diag
);
2369 if (retry
++ == 1000) {
2370 printk(KERN_WARNING
"megaraid_sas: "
2371 "Diag reset adapter never "
2376 if (host_diag
& HOST_DIAG_RESET_ADAPTER
)
2380 instance
->instancet
->read_fw_status_reg(
2381 instance
->reg_set
) & MFI_STATE_MASK
;
2384 while ((abs_state
<= MFI_STATE_FW_INIT
) &&
2388 instance
->instancet
->read_fw_status_reg(
2389 instance
->reg_set
) & MFI_STATE_MASK
;
2391 if (abs_state
<= MFI_STATE_FW_INIT
) {
2392 printk(KERN_WARNING
"megaraid_sas: firmware "
2393 "state < MFI_STATE_FW_INIT, state = "
2394 "0x%x\n", abs_state
);
2398 /* Wait for FW to become ready */
2399 if (megasas_transition_to_ready(instance
, 1)) {
2400 printk(KERN_WARNING
"megaraid_sas: Failed to "
2401 "transition controller to ready.\n");
2405 megasas_reset_reply_desc(instance
);
2406 if (megasas_ioc_init_fusion(instance
)) {
2407 printk(KERN_WARNING
"megaraid_sas: "
2408 "megasas_ioc_init_fusion() failed!\n");
2412 clear_bit(MEGASAS_FUSION_IN_RESET
,
2413 &instance
->reset_flags
);
2414 instance
->instancet
->enable_intr(instance
);
2415 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2417 /* Re-fire management commands */
2418 for (j
= 0 ; j
< instance
->max_fw_cmds
; j
++) {
2419 cmd_fusion
= fusion
->cmd_list
[j
];
2420 if (cmd_fusion
->sync_cmd_idx
!=
2424 cmd_list
[cmd_fusion
->sync_cmd_idx
];
2425 if (cmd_mfi
->frame
->dcmd
.opcode
==
2426 MR_DCMD_LD_MAP_GET_INFO
) {
2427 megasas_return_cmd(instance
,
2429 megasas_return_cmd_fusion(
2430 instance
, cmd_fusion
);
2433 megasas_get_request_descriptor(
2435 cmd_mfi
->context
.smid
2442 instance
->instancet
->
2455 /* Reset load balance info */
2456 memset(fusion
->load_balance_info
, 0,
2457 sizeof(struct LD_LOAD_BALANCE_INFO
)
2458 *MAX_LOGICAL_DRIVES
);
2460 if (!megasas_get_map_info(instance
))
2461 megasas_sync_map_info(instance
);
2463 /* Adapter reset completed successfully */
2464 printk(KERN_WARNING
"megaraid_sas: Reset "
2469 /* Reset failed, kill the adapter */
2470 printk(KERN_WARNING
"megaraid_sas: Reset failed, killing "
2472 megaraid_sas_kill_hba(instance
);
2475 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2476 instance
->instancet
->enable_intr(instance
);
2477 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2480 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2481 mutex_unlock(&instance
->reset_mutex
);
2485 /* Fusion OCR work queue */
2486 void megasas_fusion_ocr_wq(struct work_struct
*work
)
2488 struct megasas_instance
*instance
=
2489 container_of(work
, struct megasas_instance
, work_init
);
2491 megasas_reset_fusion(instance
->host
);
2494 struct megasas_instance_template megasas_instance_template_fusion
= {
2495 .fire_cmd
= megasas_fire_cmd_fusion
,
2496 .enable_intr
= megasas_enable_intr_fusion
,
2497 .disable_intr
= megasas_disable_intr_fusion
,
2498 .clear_intr
= megasas_clear_intr_fusion
,
2499 .read_fw_status_reg
= megasas_read_fw_status_reg_fusion
,
2500 .adp_reset
= megasas_adp_reset_fusion
,
2501 .check_reset
= megasas_check_reset_fusion
,
2502 .service_isr
= megasas_isr_fusion
,
2503 .tasklet
= megasas_complete_cmd_dpc_fusion
,
2504 .init_adapter
= megasas_init_adapter_fusion
,
2505 .build_and_issue_cmd
= megasas_build_and_issue_cmd_fusion
,
2506 .issue_dcmd
= megasas_issue_dcmd_fusion
,