2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2011 LSI Corporation.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * FILE: megaraid_sas_fusion.c
22 * Authors: LSI Corporation
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
);
77 MR_BuildRaidContext(struct IO_REQUEST_INFO
*io_info
,
78 struct RAID_CONTEXT
*pRAID_Context
,
79 struct MR_FW_RAID_MAP_ALL
*map
);
80 u16
MR_TargetIdToLdGet(u32 ldTgtId
, struct MR_FW_RAID_MAP_ALL
*map
);
81 struct MR_LD_RAID
*MR_LdRaidGet(u32 ld
, struct MR_FW_RAID_MAP_ALL
*map
);
83 u16
MR_GetLDTgtId(u32 ld
, struct MR_FW_RAID_MAP_ALL
*map
);
86 megasas_check_and_restore_queue_depth(struct megasas_instance
*instance
);
88 u8
MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL
*map
,
89 struct LD_LOAD_BALANCE_INFO
*lbInfo
);
90 u16
get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO
*lbInfo
,
91 struct IO_REQUEST_INFO
*in_info
);
92 int megasas_transition_to_ready(struct megasas_instance
*instance
, int ocr
);
93 void megaraid_sas_kill_hba(struct megasas_instance
*instance
);
95 extern u32 megasas_dbg_lvl
;
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
102 megasas_enable_intr_fusion(struct megasas_register_set __iomem
*regs
)
104 /* For Thunderbolt/Invader also clear intr on enable */
105 writel(~0, ®s
->outbound_intr_status
);
106 readl(®s
->outbound_intr_status
);
108 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
110 /* Dummy readl to force pci flush */
111 readl(®s
->outbound_intr_mask
);
115 * megasas_disable_intr_fusion - Disables interrupt
116 * @regs: MFI register set
119 megasas_disable_intr_fusion(struct megasas_register_set __iomem
*regs
)
121 u32 mask
= 0xFFFFFFFF;
124 writel(mask
, ®s
->outbound_intr_mask
);
125 /* Dummy readl to force pci flush */
126 status
= readl(®s
->outbound_intr_mask
);
130 megasas_clear_intr_fusion(struct megasas_register_set __iomem
*regs
)
134 * Check if it is our interrupt
136 status
= readl(®s
->outbound_intr_status
);
139 writel(status
, ®s
->outbound_intr_status
);
140 readl(®s
->outbound_intr_status
);
143 if (!(status
& MFI_FUSION_ENABLE_INTERRUPT_MASK
))
150 * megasas_get_cmd_fusion - Get a command from the free pool
151 * @instance: Adapter soft state
153 * Returns a free command from the pool
155 struct megasas_cmd_fusion
*megasas_get_cmd_fusion(struct megasas_instance
159 struct fusion_context
*fusion
=
160 (struct fusion_context
*)instance
->ctrl_context
;
161 struct megasas_cmd_fusion
*cmd
= NULL
;
163 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
165 if (!list_empty(&fusion
->cmd_pool
)) {
166 cmd
= list_entry((&fusion
->cmd_pool
)->next
,
167 struct megasas_cmd_fusion
, list
);
168 list_del_init(&cmd
->list
);
170 printk(KERN_ERR
"megasas: Command pool (fusion) empty!\n");
173 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
178 * megasas_return_cmd_fusion - Return a cmd to free command pool
179 * @instance: Adapter soft state
180 * @cmd: Command packet to be returned to free command pool
183 megasas_return_cmd_fusion(struct megasas_instance
*instance
,
184 struct megasas_cmd_fusion
*cmd
)
187 struct fusion_context
*fusion
=
188 (struct fusion_context
*)instance
->ctrl_context
;
190 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
193 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
;
194 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
196 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
200 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
201 * @instance: Adapter soft state
203 static void megasas_teardown_frame_pool_fusion(
204 struct megasas_instance
*instance
)
207 struct fusion_context
*fusion
= instance
->ctrl_context
;
209 u16 max_cmd
= instance
->max_fw_cmds
;
211 struct megasas_cmd_fusion
*cmd
;
213 if (!fusion
->sg_dma_pool
|| !fusion
->sense_dma_pool
) {
214 printk(KERN_ERR
"megasas: dma pool is null. SG Pool %p, "
215 "sense pool : %p\n", fusion
->sg_dma_pool
,
216 fusion
->sense_dma_pool
);
221 * Return all frames to pool
223 for (i
= 0; i
< max_cmd
; i
++) {
225 cmd
= fusion
->cmd_list
[i
];
228 pci_pool_free(fusion
->sg_dma_pool
, cmd
->sg_frame
,
229 cmd
->sg_frame_phys_addr
);
232 pci_pool_free(fusion
->sense_dma_pool
, cmd
->sense
,
233 cmd
->sense_phys_addr
);
237 * Now destroy the pool itself
239 pci_pool_destroy(fusion
->sg_dma_pool
);
240 pci_pool_destroy(fusion
->sense_dma_pool
);
242 fusion
->sg_dma_pool
= NULL
;
243 fusion
->sense_dma_pool
= NULL
;
247 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
248 * @instance: Adapter soft state
251 megasas_free_cmds_fusion(struct megasas_instance
*instance
)
254 struct fusion_context
*fusion
= instance
->ctrl_context
;
256 u32 max_cmds
, req_sz
, reply_sz
, io_frames_sz
;
259 req_sz
= fusion
->request_alloc_sz
;
260 reply_sz
= fusion
->reply_alloc_sz
;
261 io_frames_sz
= fusion
->io_frames_alloc_sz
;
263 max_cmds
= instance
->max_fw_cmds
;
265 /* Free descriptors and request Frames memory */
266 if (fusion
->req_frames_desc
)
267 dma_free_coherent(&instance
->pdev
->dev
, req_sz
,
268 fusion
->req_frames_desc
,
269 fusion
->req_frames_desc_phys
);
271 if (fusion
->reply_frames_desc
) {
272 pci_pool_free(fusion
->reply_frames_desc_pool
,
273 fusion
->reply_frames_desc
,
274 fusion
->reply_frames_desc_phys
);
275 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
278 if (fusion
->io_request_frames
) {
279 pci_pool_free(fusion
->io_request_frames_pool
,
280 fusion
->io_request_frames
,
281 fusion
->io_request_frames_phys
);
282 pci_pool_destroy(fusion
->io_request_frames_pool
);
285 /* Free the Fusion frame pool */
286 megasas_teardown_frame_pool_fusion(instance
);
288 /* Free all the commands in the cmd_list */
289 for (i
= 0; i
< max_cmds
; i
++)
290 kfree(fusion
->cmd_list
[i
]);
292 /* Free the cmd_list buffer itself */
293 kfree(fusion
->cmd_list
);
294 fusion
->cmd_list
= NULL
;
296 INIT_LIST_HEAD(&fusion
->cmd_pool
);
300 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
301 * @instance: Adapter soft state
304 static int megasas_create_frame_pool_fusion(struct megasas_instance
*instance
)
308 struct fusion_context
*fusion
;
309 struct megasas_cmd_fusion
*cmd
;
310 u32 total_sz_chain_frame
;
312 fusion
= instance
->ctrl_context
;
313 max_cmd
= instance
->max_fw_cmds
;
315 total_sz_chain_frame
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
318 * Use DMA pool facility provided by PCI layer
321 fusion
->sg_dma_pool
= pci_pool_create("megasas sg pool fusion",
323 total_sz_chain_frame
, 4,
325 if (!fusion
->sg_dma_pool
) {
326 printk(KERN_DEBUG
"megasas: failed to setup request pool "
330 fusion
->sense_dma_pool
= pci_pool_create("megasas sense pool fusion",
332 SCSI_SENSE_BUFFERSIZE
, 64, 0);
334 if (!fusion
->sense_dma_pool
) {
335 printk(KERN_DEBUG
"megasas: failed to setup sense pool "
337 pci_pool_destroy(fusion
->sg_dma_pool
);
338 fusion
->sg_dma_pool
= NULL
;
343 * Allocate and attach a frame to each of the commands in cmd_list
345 for (i
= 0; i
< max_cmd
; i
++) {
347 cmd
= fusion
->cmd_list
[i
];
349 cmd
->sg_frame
= pci_pool_alloc(fusion
->sg_dma_pool
,
351 &cmd
->sg_frame_phys_addr
);
353 cmd
->sense
= pci_pool_alloc(fusion
->sense_dma_pool
,
354 GFP_KERNEL
, &cmd
->sense_phys_addr
);
356 * megasas_teardown_frame_pool_fusion() takes care of freeing
357 * whatever has been allocated
359 if (!cmd
->sg_frame
|| !cmd
->sense
) {
360 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed\n");
361 megasas_teardown_frame_pool_fusion(instance
);
369 * megasas_alloc_cmds_fusion - Allocates the command packets
370 * @instance: Adapter soft state
373 * Each frame has a 32-bit field called context. This context is used to get
374 * back the megasas_cmd_fusion from the frame when a frame gets completed
375 * In this driver, the 32 bit values are the indices into an array cmd_list.
376 * This array is used only to look up the megasas_cmd_fusion given the context.
377 * The free commands themselves are maintained in a linked list called cmd_pool.
379 * cmds are formed in the io_request and sg_frame members of the
380 * megasas_cmd_fusion. The context field is used to get a request descriptor
381 * and is used as SMID of the cmd.
382 * SMID value range is from 1 to max_fw_cmds.
385 megasas_alloc_cmds_fusion(struct megasas_instance
*instance
)
388 u32 max_cmd
, io_frames_sz
;
389 struct fusion_context
*fusion
;
390 struct megasas_cmd_fusion
*cmd
;
391 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
393 dma_addr_t io_req_base_phys
;
396 fusion
= instance
->ctrl_context
;
398 max_cmd
= instance
->max_fw_cmds
;
400 fusion
->req_frames_desc
=
401 dma_alloc_coherent(&instance
->pdev
->dev
,
402 fusion
->request_alloc_sz
,
403 &fusion
->req_frames_desc_phys
, GFP_KERNEL
);
405 if (!fusion
->req_frames_desc
) {
406 printk(KERN_ERR
"megasas; Could not allocate memory for "
411 fusion
->reply_frames_desc_pool
=
412 pci_pool_create("reply_frames pool", instance
->pdev
,
413 fusion
->reply_alloc_sz
, 16, 0);
415 if (!fusion
->reply_frames_desc_pool
) {
416 printk(KERN_ERR
"megasas; Could not allocate memory for "
417 "reply_frame pool\n");
418 goto fail_reply_desc
;
421 fusion
->reply_frames_desc
=
422 pci_pool_alloc(fusion
->reply_frames_desc_pool
, GFP_KERNEL
,
423 &fusion
->reply_frames_desc_phys
);
424 if (!fusion
->reply_frames_desc
) {
425 printk(KERN_ERR
"megasas; Could not allocate memory for "
426 "reply_frame pool\n");
427 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
428 goto fail_reply_desc
;
431 reply_desc
= fusion
->reply_frames_desc
;
432 for (i
= 0; i
< fusion
->reply_q_depth
; i
++, reply_desc
++)
433 reply_desc
->Words
= ULLONG_MAX
;
435 io_frames_sz
= fusion
->io_frames_alloc_sz
;
437 fusion
->io_request_frames_pool
=
438 pci_pool_create("io_request_frames pool", instance
->pdev
,
439 fusion
->io_frames_alloc_sz
, 16, 0);
441 if (!fusion
->io_request_frames_pool
) {
442 printk(KERN_ERR
"megasas: Could not allocate memory for "
443 "io_request_frame pool\n");
447 fusion
->io_request_frames
=
448 pci_pool_alloc(fusion
->io_request_frames_pool
, GFP_KERNEL
,
449 &fusion
->io_request_frames_phys
);
450 if (!fusion
->io_request_frames
) {
451 printk(KERN_ERR
"megasas: Could not allocate memory for "
452 "io_request_frames frames\n");
453 pci_pool_destroy(fusion
->io_request_frames_pool
);
458 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
459 * Allocate the dynamic array first and then allocate individual
462 fusion
->cmd_list
= kmalloc(sizeof(struct megasas_cmd_fusion
*)
463 *max_cmd
, GFP_KERNEL
);
465 if (!fusion
->cmd_list
) {
466 printk(KERN_DEBUG
"megasas: out of memory. Could not alloc "
467 "memory for cmd_list_fusion\n");
471 memset(fusion
->cmd_list
, 0, sizeof(struct megasas_cmd_fusion
*)
474 max_cmd
= instance
->max_fw_cmds
;
475 for (i
= 0; i
< max_cmd
; i
++) {
476 fusion
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd_fusion
),
478 if (!fusion
->cmd_list
[i
]) {
479 printk(KERN_ERR
"Could not alloc cmd list fusion\n");
481 for (j
= 0; j
< i
; j
++)
482 kfree(fusion
->cmd_list
[j
]);
484 kfree(fusion
->cmd_list
);
485 fusion
->cmd_list
= NULL
;
490 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
491 io_req_base
= fusion
->io_request_frames
+
492 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
493 io_req_base_phys
= fusion
->io_request_frames_phys
+
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
497 * Add all the commands to command pool (fusion->cmd_pool)
500 /* SMID 0 is reserved. Set SMID/index from 1 */
501 for (i
= 0; i
< max_cmd
; i
++) {
502 cmd
= fusion
->cmd_list
[i
];
503 offset
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
* i
;
504 memset(cmd
, 0, sizeof(struct megasas_cmd_fusion
));
507 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
; /* Set to Invalid */
508 cmd
->instance
= instance
;
510 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
511 (io_req_base
+ offset
);
512 memset(cmd
->io_request
, 0,
513 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
));
514 cmd
->io_request_phys_addr
= io_req_base_phys
+ offset
;
516 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
520 * Create a frame pool and assign one frame to each cmd
522 if (megasas_create_frame_pool_fusion(instance
)) {
523 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
524 megasas_free_cmds_fusion(instance
);
531 pci_pool_free(fusion
->io_request_frames_pool
, fusion
->io_request_frames
,
532 fusion
->io_request_frames_phys
);
533 pci_pool_destroy(fusion
->io_request_frames_pool
);
535 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
536 fusion
->reply_frames_desc
,
537 fusion
->reply_frames_desc_phys
);
538 pci_pool_free(fusion
->reply_frames_desc_pool
,
539 fusion
->reply_frames_desc
,
540 fusion
->reply_frames_desc_phys
);
541 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
544 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
545 fusion
->req_frames_desc
,
546 fusion
->req_frames_desc_phys
);
552 * wait_and_poll - Issues a polling command
553 * @instance: Adapter soft state
554 * @cmd: Command packet to be issued
556 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
559 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
562 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
564 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
567 * Wait for cmd_status to change
569 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
+= 20) {
574 if (frame_hdr
->cmd_status
== 0xff)
581 * megasas_ioc_init_fusion - Initializes the FW
582 * @instance: Adapter soft state
584 * Issues the IOC Init cmd
587 megasas_ioc_init_fusion(struct megasas_instance
*instance
)
589 struct megasas_init_frame
*init_frame
;
590 struct MPI2_IOC_INIT_REQUEST
*IOCInitMessage
;
591 dma_addr_t ioc_init_handle
;
592 struct megasas_cmd
*cmd
;
594 struct fusion_context
*fusion
;
595 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
597 struct megasas_header
*frame_hdr
;
599 fusion
= instance
->ctrl_context
;
601 cmd
= megasas_get_cmd(instance
);
604 printk(KERN_ERR
"Could not allocate cmd for INIT Frame\n");
610 dma_alloc_coherent(&instance
->pdev
->dev
,
611 sizeof(struct MPI2_IOC_INIT_REQUEST
),
612 &ioc_init_handle
, GFP_KERNEL
);
614 if (!IOCInitMessage
) {
615 printk(KERN_ERR
"Could not allocate memory for "
621 memset(IOCInitMessage
, 0, sizeof(struct MPI2_IOC_INIT_REQUEST
));
623 IOCInitMessage
->Function
= MPI2_FUNCTION_IOC_INIT
;
624 IOCInitMessage
->WhoInit
= MPI2_WHOINIT_HOST_DRIVER
;
625 IOCInitMessage
->MsgVersion
= MPI2_VERSION
;
626 IOCInitMessage
->HeaderVersion
= MPI2_HEADER_VERSION
;
627 IOCInitMessage
->SystemRequestFrameSize
=
628 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
/ 4;
630 IOCInitMessage
->ReplyDescriptorPostQueueDepth
= fusion
->reply_q_depth
;
631 IOCInitMessage
->ReplyDescriptorPostQueueAddress
=
632 fusion
->reply_frames_desc_phys
;
633 IOCInitMessage
->SystemRequestFrameBaseAddress
=
634 fusion
->io_request_frames_phys
;
636 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
637 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
639 frame_hdr
= &cmd
->frame
->hdr
;
640 frame_hdr
->cmd_status
= 0xFF;
641 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
643 init_frame
->cmd
= MFI_CMD_INIT
;
644 init_frame
->cmd_status
= 0xFF;
646 init_frame
->queue_info_new_phys_addr_lo
= ioc_init_handle
;
647 init_frame
->data_xfer_len
= sizeof(struct MPI2_IOC_INIT_REQUEST
);
650 (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)fusion
->req_frames_desc
;
652 req_desc
->Words
= cmd
->frame_phys_addr
;
653 req_desc
->MFAIo
.RequestFlags
=
654 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA
<<
655 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
658 * disable the intr before firing the init frame
660 instance
->instancet
->disable_intr(instance
->reg_set
);
662 for (i
= 0; i
< (10 * 1000); i
+= 20) {
663 if (readl(&instance
->reg_set
->doorbell
) & 1)
669 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
670 req_desc
->u
.high
, instance
->reg_set
);
672 wait_and_poll(instance
, cmd
);
674 frame_hdr
= &cmd
->frame
->hdr
;
675 if (frame_hdr
->cmd_status
!= 0) {
679 printk(KERN_ERR
"megasas:IOC Init cmd success\n");
684 megasas_return_cmd(instance
, cmd
);
686 dma_free_coherent(&instance
->pdev
->dev
,
687 sizeof(struct MPI2_IOC_INIT_REQUEST
),
688 IOCInitMessage
, ioc_init_handle
);
694 * megasas_get_ld_map_info - Returns FW's ld_map structure
695 * @instance: Adapter soft state
696 * @pend: Pend the command or not
697 * Issues an internal command (DCMD) to get the FW's controller PD
698 * list structure. This information is mainly used to find out SYSTEM
699 * supported by the FW.
702 megasas_get_ld_map_info(struct megasas_instance
*instance
)
705 struct megasas_cmd
*cmd
;
706 struct megasas_dcmd_frame
*dcmd
;
707 struct MR_FW_RAID_MAP_ALL
*ci
;
710 struct fusion_context
*fusion
;
712 cmd
= megasas_get_cmd(instance
);
715 printk(KERN_DEBUG
"megasas: Failed to get cmd for map info.\n");
719 fusion
= instance
->ctrl_context
;
722 megasas_return_cmd(instance
, cmd
);
726 dcmd
= &cmd
->frame
->dcmd
;
728 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
729 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
731 ci
= fusion
->ld_map
[(instance
->map_id
& 1)];
732 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
& 1)];
735 printk(KERN_DEBUG
"Failed to alloc mem for ld_map_info\n");
736 megasas_return_cmd(instance
, cmd
);
740 memset(ci
, 0, sizeof(*ci
));
741 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
743 dcmd
->cmd
= MFI_CMD_DCMD
;
744 dcmd
->cmd_status
= 0xFF;
746 dcmd
->flags
= MFI_FRAME_DIR_READ
;
749 dcmd
->data_xfer_len
= size_map_info
;
750 dcmd
->opcode
= MR_DCMD_LD_MAP_GET_INFO
;
751 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
752 dcmd
->sgl
.sge32
[0].length
= size_map_info
;
754 if (!megasas_issue_polled(instance
, cmd
))
757 printk(KERN_ERR
"megasas: Get LD Map Info Failed\n");
761 megasas_return_cmd(instance
, cmd
);
767 megasas_get_map_info(struct megasas_instance
*instance
)
769 struct fusion_context
*fusion
= instance
->ctrl_context
;
771 fusion
->fast_path_io
= 0;
772 if (!megasas_get_ld_map_info(instance
)) {
773 if (MR_ValidateMapInfo(fusion
->ld_map
[(instance
->map_id
& 1)],
774 fusion
->load_balance_info
)) {
775 fusion
->fast_path_io
= 1;
783 * megasas_sync_map_info - Returns FW's ld_map structure
784 * @instance: Adapter soft state
786 * Issues an internal command (DCMD) to get the FW's controller PD
787 * list structure. This information is mainly used to find out SYSTEM
788 * supported by the FW.
791 megasas_sync_map_info(struct megasas_instance
*instance
)
794 struct megasas_cmd
*cmd
;
795 struct megasas_dcmd_frame
*dcmd
;
796 u32 size_sync_info
, num_lds
;
797 struct fusion_context
*fusion
;
798 struct MR_LD_TARGET_SYNC
*ci
= NULL
;
799 struct MR_FW_RAID_MAP_ALL
*map
;
800 struct MR_LD_RAID
*raid
;
801 struct MR_LD_TARGET_SYNC
*ld_sync
;
805 cmd
= megasas_get_cmd(instance
);
808 printk(KERN_DEBUG
"megasas: Failed to get cmd for sync"
813 fusion
= instance
->ctrl_context
;
816 megasas_return_cmd(instance
, cmd
);
820 map
= fusion
->ld_map
[instance
->map_id
& 1];
822 num_lds
= map
->raidMap
.ldCount
;
824 dcmd
= &cmd
->frame
->dcmd
;
826 size_sync_info
= sizeof(struct MR_LD_TARGET_SYNC
) *num_lds
;
828 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
830 ci
= (struct MR_LD_TARGET_SYNC
*)
831 fusion
->ld_map
[(instance
->map_id
- 1) & 1];
832 memset(ci
, 0, sizeof(struct MR_FW_RAID_MAP_ALL
));
834 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
- 1) & 1];
836 ld_sync
= (struct MR_LD_TARGET_SYNC
*)ci
;
838 for (i
= 0; i
< num_lds
; i
++, ld_sync
++) {
839 raid
= MR_LdRaidGet(i
, map
);
840 ld_sync
->targetId
= MR_GetLDTgtId(i
, map
);
841 ld_sync
->seqNum
= raid
->seqNum
;
844 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
845 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
847 dcmd
->cmd
= MFI_CMD_DCMD
;
848 dcmd
->cmd_status
= 0xFF;
850 dcmd
->flags
= MFI_FRAME_DIR_WRITE
;
853 dcmd
->data_xfer_len
= size_map_info
;
854 dcmd
->mbox
.b
[0] = num_lds
;
855 dcmd
->mbox
.b
[1] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
856 dcmd
->opcode
= MR_DCMD_LD_MAP_GET_INFO
;
857 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
858 dcmd
->sgl
.sge32
[0].length
= size_map_info
;
860 instance
->map_update_cmd
= cmd
;
862 instance
->instancet
->issue_dcmd(instance
, cmd
);
868 * megasas_init_adapter_fusion - Initializes the FW
869 * @instance: Adapter soft state
871 * This is the main function for initializing firmware.
874 megasas_init_adapter_fusion(struct megasas_instance
*instance
)
876 struct megasas_register_set __iomem
*reg_set
;
877 struct fusion_context
*fusion
;
881 fusion
= instance
->ctrl_context
;
883 reg_set
= instance
->reg_set
;
886 * Get various operational parameters from status register
888 instance
->max_fw_cmds
=
889 instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
890 instance
->max_fw_cmds
= min(instance
->max_fw_cmds
, (u16
)1008);
893 * Reduce the max supported cmds by 1. This is to ensure that the
894 * reply_q_sz (1 more than the max cmd that driver may send)
895 * does not exceed max cmds that the FW can support
897 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
898 /* Only internal cmds (DCMD) need to have MFI frames */
899 instance
->max_mfi_cmds
= MEGASAS_INT_CMDS
;
901 max_cmd
= instance
->max_fw_cmds
;
903 fusion
->reply_q_depth
= ((max_cmd
+ 1 + 15)/16)*16;
905 fusion
->request_alloc_sz
=
906 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *max_cmd
;
907 fusion
->reply_alloc_sz
= sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)
908 *(fusion
->reply_q_depth
);
909 fusion
->io_frames_alloc_sz
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
+
910 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
*
911 (max_cmd
+ 1)); /* Extra 1 for SMID 0 */
913 fusion
->max_sge_in_main_msg
=
914 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
915 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
))/16;
917 fusion
->max_sge_in_chain
=
918 MEGASAS_MAX_SZ_CHAIN_FRAME
/ sizeof(union MPI2_SGE_IO_UNION
);
920 instance
->max_num_sge
= fusion
->max_sge_in_main_msg
+
921 fusion
->max_sge_in_chain
- 2;
923 /* Used for pass thru MFI frame (DCMD) */
924 fusion
->chain_offset_mfi_pthru
=
925 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
)/16;
927 fusion
->chain_offset_io_request
=
928 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
929 sizeof(union MPI2_SGE_IO_UNION
))/16;
931 fusion
->last_reply_idx
= 0;
934 * Allocate memory for descriptors
935 * Create a pool of commands
937 if (megasas_alloc_cmds(instance
))
938 goto fail_alloc_mfi_cmds
;
939 if (megasas_alloc_cmds_fusion(instance
))
940 goto fail_alloc_cmds
;
942 if (megasas_ioc_init_fusion(instance
))
945 instance
->flag_ieee
= 1;
947 fusion
->map_sz
= sizeof(struct MR_FW_RAID_MAP
) +
948 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
950 fusion
->fast_path_io
= 0;
952 for (i
= 0; i
< 2; i
++) {
953 fusion
->ld_map
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
955 &fusion
->ld_map_phys
[i
],
957 if (!fusion
->ld_map
[i
]) {
958 printk(KERN_ERR
"megasas: Could not allocate memory "
964 if (!megasas_get_map_info(instance
))
965 megasas_sync_map_info(instance
);
971 dma_free_coherent(&instance
->pdev
->dev
, fusion
->map_sz
,
972 fusion
->ld_map
[0], fusion
->ld_map_phys
[0]);
974 megasas_free_cmds_fusion(instance
);
976 megasas_free_cmds(instance
);
982 * megasas_fire_cmd_fusion - Sends command to the FW
983 * @frame_phys_addr : Physical address of cmd
984 * @frame_count : Number of frames for the command
985 * @regs : MFI register set
988 megasas_fire_cmd_fusion(struct megasas_instance
*instance
,
989 dma_addr_t req_desc_lo
,
991 struct megasas_register_set __iomem
*regs
)
995 spin_lock_irqsave(&instance
->hba_lock
, flags
);
998 &(regs
)->inbound_low_queue_port
);
999 writel(req_desc_hi
, &(regs
)->inbound_high_queue_port
);
1000 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1004 * map_cmd_status - Maps FW cmd status to OS cmd status
1005 * @cmd : Pointer to cmd
1006 * @status : status of cmd returned by FW
1007 * @ext_status : ext status of cmd returned by FW
1011 map_cmd_status(struct megasas_cmd_fusion
*cmd
, u8 status
, u8 ext_status
)
1017 cmd
->scmd
->result
= DID_OK
<< 16;
1020 case MFI_STAT_SCSI_IO_FAILED
:
1021 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1022 cmd
->scmd
->result
= (DID_ERROR
<< 16) | ext_status
;
1025 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1027 cmd
->scmd
->result
= (DID_OK
<< 16) | ext_status
;
1028 if (ext_status
== SAM_STAT_CHECK_CONDITION
) {
1029 memset(cmd
->scmd
->sense_buffer
, 0,
1030 SCSI_SENSE_BUFFERSIZE
);
1031 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1032 SCSI_SENSE_BUFFERSIZE
);
1033 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1037 case MFI_STAT_LD_OFFLINE
:
1038 case MFI_STAT_DEVICE_NOT_FOUND
:
1039 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1043 printk(KERN_DEBUG
"megasas: FW status %#x\n", status
);
1044 cmd
->scmd
->result
= DID_ERROR
<< 16;
1050 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1051 * @instance: Adapter soft state
1052 * @scp: SCSI command from the mid-layer
1053 * @sgl_ptr: SGL to be filled in
1054 * @cmd: cmd we are working on
1056 * If successful, this function returns the number of SG elements.
1059 megasas_make_sgl_fusion(struct megasas_instance
*instance
,
1060 struct scsi_cmnd
*scp
,
1061 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
1062 struct megasas_cmd_fusion
*cmd
)
1064 int i
, sg_processed
;
1065 int sge_count
, sge_idx
;
1066 struct scatterlist
*os_sgl
;
1067 struct fusion_context
*fusion
;
1069 fusion
= instance
->ctrl_context
;
1071 cmd
->io_request
->ChainOffset
= 0;
1073 sge_count
= scsi_dma_map(scp
);
1075 BUG_ON(sge_count
< 0);
1077 if (sge_count
> instance
->max_num_sge
|| !sge_count
)
1080 if (sge_count
> fusion
->max_sge_in_main_msg
) {
1081 /* One element to store the chain info */
1082 sge_idx
= fusion
->max_sge_in_main_msg
- 1;
1084 sge_idx
= sge_count
;
1086 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1087 sgl_ptr
->Length
= sg_dma_len(os_sgl
);
1088 sgl_ptr
->Address
= sg_dma_address(os_sgl
);
1092 sg_processed
= i
+ 1;
1094 if ((sg_processed
== (fusion
->max_sge_in_main_msg
- 1)) &&
1095 (sge_count
> fusion
->max_sge_in_main_msg
)) {
1097 struct MPI25_IEEE_SGE_CHAIN64
*sg_chain
;
1098 cmd
->io_request
->ChainOffset
=
1099 fusion
->chain_offset_io_request
;
1101 /* Prepare chain element */
1102 sg_chain
->NextChainOffset
= 0;
1103 sg_chain
->Flags
= (IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1104 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
);
1105 sg_chain
->Length
= (sizeof(union MPI2_SGE_IO_UNION
)
1106 *(sge_count
- sg_processed
));
1107 sg_chain
->Address
= cmd
->sg_frame_phys_addr
;
1110 (struct MPI25_IEEE_SGE_CHAIN64
*)cmd
->sg_frame
;
1118 * megasas_set_pd_lba - Sets PD LBA
1120 * @cdb_len: cdb length
1121 * @start_blk: Start block of IO
1123 * Used to set the PD LBA in CDB for FP IOs
1126 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
, u8 cdb_len
,
1127 struct IO_REQUEST_INFO
*io_info
, struct scsi_cmnd
*scp
,
1128 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
, u32 ref_tag
)
1130 struct MR_LD_RAID
*raid
;
1132 u64 start_blk
= io_info
->pdBlock
;
1133 u8
*cdb
= io_request
->CDB
.CDB32
;
1134 u32 num_blocks
= io_info
->numBlocks
;
1135 u8 opcode
= 0, flagvals
= 0, groupnum
= 0, control
= 0;
1137 /* Check if T10 PI (DIF) is enabled for this LD */
1138 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, local_map_ptr
);
1139 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1140 if (raid
->capability
.ldPiMode
== MR_PROT_INFO_TYPE_CONTROLLER
) {
1141 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1142 cdb
[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD
;
1143 cdb
[7] = MEGASAS_SCSI_ADDL_CDB_LEN
;
1145 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1146 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32
;
1148 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32
;
1149 cdb
[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL
;
1152 cdb
[12] = (u8
)((start_blk
>> 56) & 0xff);
1153 cdb
[13] = (u8
)((start_blk
>> 48) & 0xff);
1154 cdb
[14] = (u8
)((start_blk
>> 40) & 0xff);
1155 cdb
[15] = (u8
)((start_blk
>> 32) & 0xff);
1156 cdb
[16] = (u8
)((start_blk
>> 24) & 0xff);
1157 cdb
[17] = (u8
)((start_blk
>> 16) & 0xff);
1158 cdb
[18] = (u8
)((start_blk
>> 8) & 0xff);
1159 cdb
[19] = (u8
)(start_blk
& 0xff);
1161 /* Logical block reference tag */
1162 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
=
1163 cpu_to_be32(ref_tag
);
1164 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0xffff;
1166 io_request
->DataLength
= num_blocks
* 512;
1167 io_request
->IoFlags
= 32; /* Specify 32-byte cdb */
1169 /* Transfer length */
1170 cdb
[28] = (u8
)((num_blocks
>> 24) & 0xff);
1171 cdb
[29] = (u8
)((num_blocks
>> 16) & 0xff);
1172 cdb
[30] = (u8
)((num_blocks
>> 8) & 0xff);
1173 cdb
[31] = (u8
)(num_blocks
& 0xff);
1175 /* set SCSI IO EEDPFlags */
1176 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
) {
1177 io_request
->EEDPFlags
=
1178 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1179 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG
|
1180 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
|
1181 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG
|
1182 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD
;
1184 io_request
->EEDPFlags
=
1185 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1186 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
;
1188 io_request
->Control
|= (0x4 << 26);
1189 io_request
->EEDPBlockSize
= MEGASAS_EEDPBLOCKSIZE
;
1191 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1192 if (((cdb_len
== 12) || (cdb_len
== 16)) &&
1193 (start_blk
<= 0xffffffff)) {
1194 if (cdb_len
== 16) {
1195 opcode
= cdb
[0] == READ_16
? READ_10
: WRITE_10
;
1200 opcode
= cdb
[0] == READ_12
? READ_10
: WRITE_10
;
1206 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1213 /* Transfer length */
1214 cdb
[8] = (u8
)(num_blocks
& 0xff);
1215 cdb
[7] = (u8
)((num_blocks
>> 8) & 0xff);
1217 io_request
->IoFlags
= 10; /* Specify 10-byte cdb */
1219 } else if ((cdb_len
< 16) && (start_blk
> 0xffffffff)) {
1220 /* Convert to 16 byte CDB for large LBA's */
1223 opcode
= cdb
[0] == READ_6
? READ_16
: WRITE_16
;
1228 cdb
[0] == READ_10
? READ_16
: WRITE_16
;
1235 cdb
[0] == READ_12
? READ_16
: WRITE_16
;
1242 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1249 /* Transfer length */
1250 cdb
[13] = (u8
)(num_blocks
& 0xff);
1251 cdb
[12] = (u8
)((num_blocks
>> 8) & 0xff);
1252 cdb
[11] = (u8
)((num_blocks
>> 16) & 0xff);
1253 cdb
[10] = (u8
)((num_blocks
>> 24) & 0xff);
1255 io_request
->IoFlags
= 16; /* Specify 16-byte cdb */
1259 /* Normal case, just load LBA here */
1263 u8 val
= cdb
[1] & 0xE0;
1264 cdb
[3] = (u8
)(start_blk
& 0xff);
1265 cdb
[2] = (u8
)((start_blk
>> 8) & 0xff);
1266 cdb
[1] = val
| ((u8
)(start_blk
>> 16) & 0x1f);
1270 cdb
[5] = (u8
)(start_blk
& 0xff);
1271 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1272 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1273 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1276 cdb
[5] = (u8
)(start_blk
& 0xff);
1277 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1278 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1279 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1282 cdb
[9] = (u8
)(start_blk
& 0xff);
1283 cdb
[8] = (u8
)((start_blk
>> 8) & 0xff);
1284 cdb
[7] = (u8
)((start_blk
>> 16) & 0xff);
1285 cdb
[6] = (u8
)((start_blk
>> 24) & 0xff);
1286 cdb
[5] = (u8
)((start_blk
>> 32) & 0xff);
1287 cdb
[4] = (u8
)((start_blk
>> 40) & 0xff);
1288 cdb
[3] = (u8
)((start_blk
>> 48) & 0xff);
1289 cdb
[2] = (u8
)((start_blk
>> 56) & 0xff);
1296 * megasas_build_ldio_fusion - Prepares IOs to devices
1297 * @instance: Adapter soft state
1298 * @scp: SCSI command
1299 * @cmd: Command to be prepared
1301 * Prepares the io_request and chain elements (sg_frame) for IO
1302 * The IO can be for PD (Fast Path) or LD
1305 megasas_build_ldio_fusion(struct megasas_instance
*instance
,
1306 struct scsi_cmnd
*scp
,
1307 struct megasas_cmd_fusion
*cmd
)
1310 u32 start_lba_lo
, start_lba_hi
, device_id
;
1311 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1312 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1313 struct IO_REQUEST_INFO io_info
;
1314 struct fusion_context
*fusion
;
1315 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1317 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1319 fusion
= instance
->ctrl_context
;
1321 io_request
= cmd
->io_request
;
1322 io_request
->RaidContext
.VirtualDiskTgtId
= device_id
;
1323 io_request
->RaidContext
.status
= 0;
1324 io_request
->RaidContext
.exStatus
= 0;
1326 req_desc
= (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)cmd
->request_desc
;
1333 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1335 if (scp
->cmd_len
== 6) {
1336 io_request
->DataLength
= (u32
) scp
->cmnd
[4];
1337 start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1338 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1340 start_lba_lo
&= 0x1FFFFF;
1344 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1346 else if (scp
->cmd_len
== 10) {
1347 io_request
->DataLength
= (u32
) scp
->cmnd
[8] |
1348 ((u32
) scp
->cmnd
[7] << 8);
1349 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1350 ((u32
) scp
->cmnd
[3] << 16) |
1351 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1355 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1357 else if (scp
->cmd_len
== 12) {
1358 io_request
->DataLength
= ((u32
) scp
->cmnd
[6] << 24) |
1359 ((u32
) scp
->cmnd
[7] << 16) |
1360 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1361 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1362 ((u32
) scp
->cmnd
[3] << 16) |
1363 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1367 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1369 else if (scp
->cmd_len
== 16) {
1370 io_request
->DataLength
= ((u32
) scp
->cmnd
[10] << 24) |
1371 ((u32
) scp
->cmnd
[11] << 16) |
1372 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1373 start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1374 ((u32
) scp
->cmnd
[7] << 16) |
1375 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1377 start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1378 ((u32
) scp
->cmnd
[3] << 16) |
1379 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1382 memset(&io_info
, 0, sizeof(struct IO_REQUEST_INFO
));
1383 io_info
.ldStartBlock
= ((u64
)start_lba_hi
<< 32) | start_lba_lo
;
1384 io_info
.numBlocks
= io_request
->DataLength
;
1385 io_info
.ldTgtId
= device_id
;
1387 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1390 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1392 if ((MR_TargetIdToLdGet(device_id
, local_map_ptr
) >=
1393 MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
)) {
1394 io_request
->RaidContext
.regLockFlags
= 0;
1397 if (MR_BuildRaidContext(&io_info
, &io_request
->RaidContext
,
1399 fp_possible
= io_info
.fpOkForIo
;
1403 megasas_set_pd_lba(io_request
, scp
->cmd_len
, &io_info
, scp
,
1404 local_map_ptr
, start_lba_lo
);
1405 io_request
->DataLength
= scsi_bufflen(scp
);
1406 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1407 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1408 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1409 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1410 if ((fusion
->load_balance_info
[device_id
].loadBalanceFlag
) &&
1413 get_updated_dev_handle(
1414 &fusion
->load_balance_info
[device_id
],
1416 scp
->SCp
.Status
|= MEGASAS_LOAD_BALANCE_FLAG
;
1418 scp
->SCp
.Status
&= ~MEGASAS_LOAD_BALANCE_FLAG
;
1419 cmd
->request_desc
->SCSIIO
.DevHandle
= io_info
.devHandle
;
1420 io_request
->DevHandle
= io_info
.devHandle
;
1422 io_request
->RaidContext
.timeoutValue
=
1423 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1424 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1425 io_request
->DevHandle
= device_id
;
1426 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1427 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1428 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1433 * megasas_build_dcdb_fusion - Prepares IOs to devices
1434 * @instance: Adapter soft state
1435 * @scp: SCSI command
1436 * @cmd: Command to be prepared
1438 * Prepares the io_request frame for non-io cmds
1441 megasas_build_dcdb_fusion(struct megasas_instance
*instance
,
1442 struct scsi_cmnd
*scmd
,
1443 struct megasas_cmd_fusion
*cmd
)
1446 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1448 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1449 struct fusion_context
*fusion
= instance
->ctrl_context
;
1451 io_request
= cmd
->io_request
;
1452 device_id
= MEGASAS_DEV_INDEX(instance
, scmd
);
1453 pd_index
= (scmd
->device
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
)
1455 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1457 /* Check if this is a system PD I/O */
1458 if (instance
->pd_list
[pd_index
].driveState
== MR_PD_STATE_SYSTEM
) {
1459 io_request
->Function
= 0;
1460 io_request
->DevHandle
=
1461 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1462 io_request
->RaidContext
.timeoutValue
=
1463 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1464 io_request
->RaidContext
.regLockFlags
= 0;
1465 io_request
->RaidContext
.regLockRowLBA
= 0;
1466 io_request
->RaidContext
.regLockLength
= 0;
1467 io_request
->RaidContext
.RAIDFlags
=
1468 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
<<
1469 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
;
1470 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1471 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1472 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1474 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1475 io_request
->DevHandle
= device_id
;
1476 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1477 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1478 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1480 io_request
->RaidContext
.VirtualDiskTgtId
= device_id
;
1481 io_request
->LUN
[1] = scmd
->device
->lun
;
1482 io_request
->DataLength
= scsi_bufflen(scmd
);
1486 * megasas_build_io_fusion - Prepares IOs to devices
1487 * @instance: Adapter soft state
1488 * @scp: SCSI command
1489 * @cmd: Command to be prepared
1491 * Invokes helper functions to prepare request frames
1492 * and sets flags appropriate for IO/Non-IO cmd
1495 megasas_build_io_fusion(struct megasas_instance
*instance
,
1496 struct scsi_cmnd
*scp
,
1497 struct megasas_cmd_fusion
*cmd
)
1499 u32 device_id
, sge_count
;
1500 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
= cmd
->io_request
;
1502 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1504 /* Zero out some fields so they don't get reused */
1505 io_request
->LUN
[1] = 0;
1506 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
= 0;
1507 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0;
1508 io_request
->EEDPFlags
= 0;
1509 io_request
->Control
= 0;
1510 io_request
->EEDPBlockSize
= 0;
1511 io_request
->IoFlags
= 0;
1512 io_request
->RaidContext
.RAIDFlags
= 0;
1514 memcpy(io_request
->CDB
.CDB32
, scp
->cmnd
, scp
->cmd_len
);
1516 * Just the CDB length,rest of the Flags are zero
1517 * This will be modified for FP in build_ldio_fusion
1519 io_request
->IoFlags
= scp
->cmd_len
;
1521 if (megasas_is_ldio(scp
))
1522 megasas_build_ldio_fusion(instance
, scp
, cmd
);
1524 megasas_build_dcdb_fusion(instance
, scp
, cmd
);
1531 megasas_make_sgl_fusion(instance
, scp
,
1532 (struct MPI25_IEEE_SGE_CHAIN64
*)
1533 &io_request
->SGL
, cmd
);
1535 if (sge_count
> instance
->max_num_sge
) {
1536 printk(KERN_ERR
"megasas: Error. sge_count (0x%x) exceeds "
1537 "max (0x%x) allowed\n", sge_count
,
1538 instance
->max_num_sge
);
1542 io_request
->RaidContext
.numSGE
= sge_count
;
1544 io_request
->SGLFlags
= MPI2_SGE_FLAGS_64_BIT_ADDRESSING
;
1546 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1547 io_request
->Control
|= MPI2_SCSIIO_CONTROL_WRITE
;
1548 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1549 io_request
->Control
|= MPI2_SCSIIO_CONTROL_READ
;
1551 io_request
->SGLOffset0
=
1552 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
) / 4;
1554 io_request
->SenseBufferLowAddress
= cmd
->sense_phys_addr
;
1555 io_request
->SenseBufferLength
= SCSI_SENSE_BUFFERSIZE
;
1558 scp
->SCp
.ptr
= (char *)cmd
;
1563 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1564 megasas_get_request_descriptor(struct megasas_instance
*instance
, u16 index
)
1567 struct fusion_context
*fusion
;
1569 if (index
>= instance
->max_fw_cmds
) {
1570 printk(KERN_ERR
"megasas: Invalid SMID (0x%x)request for "
1571 "descriptor\n", index
);
1574 fusion
= instance
->ctrl_context
;
1575 p
= fusion
->req_frames_desc
1576 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *index
;
1578 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)p
;
1582 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1583 * issuing non IOCTL cmd
1584 * @instance: Adapter soft state
1585 * @scmd: pointer to scsi cmd from OS
1588 megasas_build_and_issue_cmd_fusion(struct megasas_instance
*instance
,
1589 struct scsi_cmnd
*scmd
)
1591 struct megasas_cmd_fusion
*cmd
;
1592 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1594 struct fusion_context
*fusion
;
1596 fusion
= instance
->ctrl_context
;
1598 cmd
= megasas_get_cmd_fusion(instance
);
1600 return SCSI_MLQUEUE_HOST_BUSY
;
1604 req_desc
= megasas_get_request_descriptor(instance
, index
-1);
1608 req_desc
->Words
= 0;
1609 cmd
->request_desc
= req_desc
;
1611 if (megasas_build_io_fusion(instance
, scmd
, cmd
)) {
1612 megasas_return_cmd_fusion(instance
, cmd
);
1613 printk(KERN_ERR
"megasas: Error building command.\n");
1614 cmd
->request_desc
= NULL
;
1618 req_desc
= cmd
->request_desc
;
1619 req_desc
->SCSIIO
.SMID
= index
;
1621 if (cmd
->io_request
->ChainOffset
!= 0 &&
1622 cmd
->io_request
->ChainOffset
!= 0xF)
1623 printk(KERN_ERR
"megasas: The chain offset value is not "
1624 "correct : %x\n", cmd
->io_request
->ChainOffset
);
1627 * Issue the command to the FW
1629 atomic_inc(&instance
->fw_outstanding
);
1631 instance
->instancet
->fire_cmd(instance
,
1632 req_desc
->u
.low
, req_desc
->u
.high
,
1639 * complete_cmd_fusion - Completes command
1640 * @instance: Adapter soft state
1641 * Completes all commands that is in reply descriptor queue
1644 complete_cmd_fusion(struct megasas_instance
*instance
)
1646 union MPI2_REPLY_DESCRIPTORS_UNION
*desc
;
1647 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*reply_desc
;
1648 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
1649 struct fusion_context
*fusion
;
1650 struct megasas_cmd
*cmd_mfi
;
1651 struct megasas_cmd_fusion
*cmd_fusion
;
1652 u16 smid
, num_completed
;
1653 u8 reply_descript_type
, arm
;
1654 u32 status
, extStatus
, device_id
;
1655 union desc_value d_val
;
1656 struct LD_LOAD_BALANCE_INFO
*lbinfo
;
1658 fusion
= instance
->ctrl_context
;
1660 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1663 desc
= fusion
->reply_frames_desc
;
1664 desc
+= fusion
->last_reply_idx
;
1666 reply_desc
= (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1668 d_val
.word
= desc
->Words
;
1670 reply_descript_type
= reply_desc
->ReplyFlags
&
1671 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1673 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1676 d_val
.word
= desc
->Words
;
1680 while ((d_val
.u
.low
!= UINT_MAX
) && (d_val
.u
.high
!= UINT_MAX
)) {
1681 smid
= reply_desc
->SMID
;
1683 cmd_fusion
= fusion
->cmd_list
[smid
- 1];
1686 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
1687 cmd_fusion
->io_request
;
1689 if (cmd_fusion
->scmd
)
1690 cmd_fusion
->scmd
->SCp
.ptr
= NULL
;
1692 status
= scsi_io_req
->RaidContext
.status
;
1693 extStatus
= scsi_io_req
->RaidContext
.exStatus
;
1695 switch (scsi_io_req
->Function
) {
1696 case MPI2_FUNCTION_SCSI_IO_REQUEST
: /*Fast Path IO.*/
1697 /* Update load balancing info */
1698 device_id
= MEGASAS_DEV_INDEX(instance
,
1700 lbinfo
= &fusion
->load_balance_info
[device_id
];
1701 if (cmd_fusion
->scmd
->SCp
.Status
&
1702 MEGASAS_LOAD_BALANCE_FLAG
) {
1703 arm
= lbinfo
->raid1DevHandle
[0] ==
1704 cmd_fusion
->io_request
->DevHandle
? 0 :
1706 atomic_dec(&lbinfo
->scsi_pending_cmds
[arm
]);
1707 cmd_fusion
->scmd
->SCp
.Status
&=
1708 ~MEGASAS_LOAD_BALANCE_FLAG
;
1710 if (reply_descript_type
==
1711 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS
) {
1712 if (megasas_dbg_lvl
== 5)
1713 printk(KERN_ERR
"\nmegasas: FAST Path "
1716 /* Fall thru and complete IO */
1717 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
: /* LD-IO Path */
1718 /* Map the FW Cmd Status */
1719 map_cmd_status(cmd_fusion
, status
, extStatus
);
1720 scsi_dma_unmap(cmd_fusion
->scmd
);
1721 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
1722 scsi_io_req
->RaidContext
.status
= 0;
1723 scsi_io_req
->RaidContext
.exStatus
= 0;
1724 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1725 atomic_dec(&instance
->fw_outstanding
);
1728 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
: /*MFI command */
1729 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
1730 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
1731 cmd_fusion
->flags
= 0;
1732 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1737 fusion
->last_reply_idx
++;
1738 if (fusion
->last_reply_idx
>= fusion
->reply_q_depth
)
1739 fusion
->last_reply_idx
= 0;
1741 desc
->Words
= ULLONG_MAX
;
1744 /* Get the next reply descriptor */
1745 if (!fusion
->last_reply_idx
)
1746 desc
= fusion
->reply_frames_desc
;
1751 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1753 d_val
.word
= desc
->Words
;
1755 reply_descript_type
= reply_desc
->ReplyFlags
&
1756 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1758 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1766 writel(fusion
->last_reply_idx
,
1767 &instance
->reg_set
->reply_post_host_index
);
1768 megasas_check_and_restore_queue_depth(instance
);
1773 * megasas_complete_cmd_dpc_fusion - Completes command
1774 * @instance: Adapter soft state
1776 * Tasklet to complete cmds
1779 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr
)
1781 struct megasas_instance
*instance
=
1782 (struct megasas_instance
*)instance_addr
;
1783 unsigned long flags
;
1785 /* If we have already declared adapter dead, donot complete cmds */
1786 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1787 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1788 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1791 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1793 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1794 complete_cmd_fusion(instance
);
1795 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1799 * megasas_isr_fusion - isr entry point
1801 irqreturn_t
megasas_isr_fusion(int irq
, void *devp
)
1803 struct megasas_instance
*instance
= (struct megasas_instance
*)devp
;
1804 u32 mfiStatus
, fw_state
;
1806 if (!instance
->msi_flag
) {
1807 mfiStatus
= instance
->instancet
->clear_intr(instance
->reg_set
);
1812 /* If we are resetting, bail */
1813 if (test_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
)) {
1814 instance
->instancet
->clear_intr(instance
->reg_set
);
1818 if (!complete_cmd_fusion(instance
)) {
1819 instance
->instancet
->clear_intr(instance
->reg_set
);
1820 /* If we didn't complete any commands, check for FW fault */
1821 fw_state
= instance
->instancet
->read_fw_status_reg(
1822 instance
->reg_set
) & MFI_STATE_MASK
;
1823 if (fw_state
== MFI_STATE_FAULT
)
1824 schedule_work(&instance
->work_init
);
1831 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1832 * @instance: Adapter soft state
1833 * mfi_cmd: megasas_cmd pointer
1837 build_mpt_mfi_pass_thru(struct megasas_instance
*instance
,
1838 struct megasas_cmd
*mfi_cmd
)
1840 struct MPI25_IEEE_SGE_CHAIN64
*mpi25_ieee_chain
;
1841 struct MPI2_RAID_SCSI_IO_REQUEST
*io_req
;
1842 struct megasas_cmd_fusion
*cmd
;
1843 struct fusion_context
*fusion
;
1844 struct megasas_header
*frame_hdr
= &mfi_cmd
->frame
->hdr
;
1846 cmd
= megasas_get_cmd_fusion(instance
);
1850 /* Save the smid. To be used for returning the cmd */
1851 mfi_cmd
->context
.smid
= cmd
->index
;
1853 cmd
->sync_cmd_idx
= mfi_cmd
->index
;
1856 * For cmds where the flag is set, store the flag and check
1857 * on completion. For cmds with this flag, don't call
1858 * megasas_complete_cmd
1861 if (frame_hdr
->flags
& MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
)
1862 cmd
->flags
= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
1864 fusion
= instance
->ctrl_context
;
1865 io_req
= cmd
->io_request
;
1867 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
.IeeeChain
;
1869 io_req
->Function
= MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
;
1870 io_req
->SGLOffset0
= offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
,
1872 io_req
->ChainOffset
= fusion
->chain_offset_mfi_pthru
;
1874 mpi25_ieee_chain
->Address
= mfi_cmd
->frame_phys_addr
;
1876 mpi25_ieee_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1877 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
;
1879 mpi25_ieee_chain
->Length
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
1885 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1886 * @instance: Adapter soft state
1887 * @cmd: mfi cmd to build
1890 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1891 build_mpt_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
1893 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1896 if (build_mpt_mfi_pass_thru(instance
, cmd
)) {
1897 printk(KERN_ERR
"Couldn't build MFI pass thru cmd\n");
1901 index
= cmd
->context
.smid
;
1903 req_desc
= megasas_get_request_descriptor(instance
, index
- 1);
1908 req_desc
->Words
= 0;
1909 req_desc
->SCSIIO
.RequestFlags
= (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1910 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1912 req_desc
->SCSIIO
.SMID
= index
;
1918 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1919 * @instance: Adapter soft state
1920 * @cmd: mfi cmd pointer
1924 megasas_issue_dcmd_fusion(struct megasas_instance
*instance
,
1925 struct megasas_cmd
*cmd
)
1927 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1929 req_desc
= build_mpt_cmd(instance
, cmd
);
1931 printk(KERN_ERR
"Couldn't issue MFI pass thru cmd\n");
1934 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
1935 req_desc
->u
.high
, instance
->reg_set
);
1939 * megasas_release_fusion - Reverses the FW initialization
1940 * @intance: Adapter soft state
1943 megasas_release_fusion(struct megasas_instance
*instance
)
1945 megasas_free_cmds(instance
);
1946 megasas_free_cmds_fusion(instance
);
1948 iounmap(instance
->reg_set
);
1950 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
1954 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1955 * @regs: MFI register set
1958 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem
*regs
)
1960 return readl(&(regs
)->outbound_scratch_pad
);
1964 * megasas_adp_reset_fusion - For controller reset
1965 * @regs: MFI register set
1968 megasas_adp_reset_fusion(struct megasas_instance
*instance
,
1969 struct megasas_register_set __iomem
*regs
)
1975 * megasas_check_reset_fusion - For controller reset check
1976 * @regs: MFI register set
1979 megasas_check_reset_fusion(struct megasas_instance
*instance
,
1980 struct megasas_register_set __iomem
*regs
)
1985 /* This function waits for outstanding commands on fusion to complete */
1986 int megasas_wait_for_outstanding_fusion(struct megasas_instance
*instance
)
1988 int i
, outstanding
, retval
= 0;
1989 u32 fw_state
, wait_time
= MEGASAS_RESET_WAIT_TIME
;
1991 for (i
= 0; i
< wait_time
; i
++) {
1992 /* Check if firmware is in fault state */
1993 fw_state
= instance
->instancet
->read_fw_status_reg(
1994 instance
->reg_set
) & MFI_STATE_MASK
;
1995 if (fw_state
== MFI_STATE_FAULT
) {
1996 printk(KERN_WARNING
"megasas: Found FW in FAULT state,"
1997 " will reset adapter.\n");
2002 outstanding
= atomic_read(&instance
->fw_outstanding
);
2006 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
2007 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
2008 "commands to complete\n", i
, outstanding
);
2009 megasas_complete_cmd_dpc_fusion(
2010 (unsigned long)instance
);
2015 if (atomic_read(&instance
->fw_outstanding
)) {
2016 printk("megaraid_sas: pending commands remain after waiting, "
2017 "will reset adapter.\n");
2024 void megasas_reset_reply_desc(struct megasas_instance
*instance
)
2027 struct fusion_context
*fusion
;
2028 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
2030 fusion
= instance
->ctrl_context
;
2031 fusion
->last_reply_idx
= 0;
2032 reply_desc
= fusion
->reply_frames_desc
;
2033 for (i
= 0 ; i
< fusion
->reply_q_depth
; i
++, reply_desc
++)
2034 reply_desc
->Words
= ULLONG_MAX
;
2037 /* Core fusion reset function */
2038 int megasas_reset_fusion(struct Scsi_Host
*shost
)
2040 int retval
= SUCCESS
, i
, j
, retry
= 0;
2041 struct megasas_instance
*instance
;
2042 struct megasas_cmd_fusion
*cmd_fusion
;
2043 struct fusion_context
*fusion
;
2044 struct megasas_cmd
*cmd_mfi
;
2045 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2046 u32 host_diag
, abs_state
, status_reg
, reset_adapter
;
2048 instance
= (struct megasas_instance
*)shost
->hostdata
;
2049 fusion
= instance
->ctrl_context
;
2051 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2052 printk(KERN_WARNING
"megaraid_sas: Hardware critical error, "
2053 "returning FAILED.\n");
2057 mutex_lock(&instance
->reset_mutex
);
2058 set_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2059 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2060 instance
->instancet
->disable_intr(instance
->reg_set
);
2063 /* First try waiting for commands to complete */
2064 if (megasas_wait_for_outstanding_fusion(instance
)) {
2065 printk(KERN_WARNING
"megaraid_sas: resetting fusion "
2067 /* Now return commands back to the OS */
2068 for (i
= 0 ; i
< instance
->max_fw_cmds
; i
++) {
2069 cmd_fusion
= fusion
->cmd_list
[i
];
2070 if (cmd_fusion
->scmd
) {
2071 scsi_dma_unmap(cmd_fusion
->scmd
);
2072 cmd_fusion
->scmd
->result
= (DID_RESET
<< 16);
2073 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
2074 megasas_return_cmd_fusion(instance
, cmd_fusion
);
2075 atomic_dec(&instance
->fw_outstanding
);
2079 status_reg
= instance
->instancet
->read_fw_status_reg(
2081 abs_state
= status_reg
& MFI_STATE_MASK
;
2082 reset_adapter
= status_reg
& MFI_RESET_ADAPTER
;
2083 if (instance
->disableOnlineCtrlReset
||
2084 (abs_state
== MFI_STATE_FAULT
&& !reset_adapter
)) {
2085 /* Reset not supported, kill adapter */
2086 printk(KERN_WARNING
"megaraid_sas: Reset not supported"
2087 ", killing adapter.\n");
2088 megaraid_sas_kill_hba(instance
);
2089 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2094 /* Now try to reset the chip */
2095 for (i
= 0; i
< MEGASAS_FUSION_MAX_RESET_TRIES
; i
++) {
2096 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE
,
2097 &instance
->reg_set
->fusion_seq_offset
);
2098 writel(MPI2_WRSEQ_1ST_KEY_VALUE
,
2099 &instance
->reg_set
->fusion_seq_offset
);
2100 writel(MPI2_WRSEQ_2ND_KEY_VALUE
,
2101 &instance
->reg_set
->fusion_seq_offset
);
2102 writel(MPI2_WRSEQ_3RD_KEY_VALUE
,
2103 &instance
->reg_set
->fusion_seq_offset
);
2104 writel(MPI2_WRSEQ_4TH_KEY_VALUE
,
2105 &instance
->reg_set
->fusion_seq_offset
);
2106 writel(MPI2_WRSEQ_5TH_KEY_VALUE
,
2107 &instance
->reg_set
->fusion_seq_offset
);
2108 writel(MPI2_WRSEQ_6TH_KEY_VALUE
,
2109 &instance
->reg_set
->fusion_seq_offset
);
2111 /* Check that the diag write enable (DRWE) bit is on */
2112 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2114 while (!(host_diag
& HOST_DIAG_WRITE_ENABLE
)) {
2117 readl(&instance
->reg_set
->fusion_host_diag
);
2118 if (retry
++ == 100) {
2119 printk(KERN_WARNING
"megaraid_sas: "
2120 "Host diag unlock failed!\n");
2124 if (!(host_diag
& HOST_DIAG_WRITE_ENABLE
))
2127 /* Send chip reset command */
2128 writel(host_diag
| HOST_DIAG_RESET_ADAPTER
,
2129 &instance
->reg_set
->fusion_host_diag
);
2132 /* Make sure reset adapter bit is cleared */
2133 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2135 while (host_diag
& HOST_DIAG_RESET_ADAPTER
) {
2138 readl(&instance
->reg_set
->fusion_host_diag
);
2139 if (retry
++ == 1000) {
2140 printk(KERN_WARNING
"megaraid_sas: "
2141 "Diag reset adapter never "
2146 if (host_diag
& HOST_DIAG_RESET_ADAPTER
)
2150 instance
->instancet
->read_fw_status_reg(
2151 instance
->reg_set
) & MFI_STATE_MASK
;
2154 while ((abs_state
<= MFI_STATE_FW_INIT
) &&
2158 instance
->instancet
->read_fw_status_reg(
2159 instance
->reg_set
) & MFI_STATE_MASK
;
2161 if (abs_state
<= MFI_STATE_FW_INIT
) {
2162 printk(KERN_WARNING
"megaraid_sas: firmware "
2163 "state < MFI_STATE_FW_INIT, state = "
2164 "0x%x\n", abs_state
);
2168 /* Wait for FW to become ready */
2169 if (megasas_transition_to_ready(instance
, 1)) {
2170 printk(KERN_WARNING
"megaraid_sas: Failed to "
2171 "transition controller to ready.\n");
2175 megasas_reset_reply_desc(instance
);
2176 if (megasas_ioc_init_fusion(instance
)) {
2177 printk(KERN_WARNING
"megaraid_sas: "
2178 "megasas_ioc_init_fusion() failed!\n");
2182 clear_bit(MEGASAS_FUSION_IN_RESET
,
2183 &instance
->reset_flags
);
2184 instance
->instancet
->enable_intr(instance
->reg_set
);
2185 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2187 /* Re-fire management commands */
2188 for (j
= 0 ; j
< instance
->max_fw_cmds
; j
++) {
2189 cmd_fusion
= fusion
->cmd_list
[j
];
2190 if (cmd_fusion
->sync_cmd_idx
!=
2194 cmd_list
[cmd_fusion
->sync_cmd_idx
];
2195 if (cmd_mfi
->frame
->dcmd
.opcode
==
2196 MR_DCMD_LD_MAP_GET_INFO
) {
2197 megasas_return_cmd(instance
,
2199 megasas_return_cmd_fusion(
2200 instance
, cmd_fusion
);
2203 megasas_get_request_descriptor(
2205 cmd_mfi
->context
.smid
2212 instance
->instancet
->
2225 /* Reset load balance info */
2226 memset(fusion
->load_balance_info
, 0,
2227 sizeof(struct LD_LOAD_BALANCE_INFO
)
2228 *MAX_LOGICAL_DRIVES
);
2230 if (!megasas_get_map_info(instance
))
2231 megasas_sync_map_info(instance
);
2233 /* Adapter reset completed successfully */
2234 printk(KERN_WARNING
"megaraid_sas: Reset "
2239 /* Reset failed, kill the adapter */
2240 printk(KERN_WARNING
"megaraid_sas: Reset failed, killing "
2242 megaraid_sas_kill_hba(instance
);
2245 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2246 instance
->instancet
->enable_intr(instance
->reg_set
);
2247 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2250 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2251 mutex_unlock(&instance
->reset_mutex
);
2255 /* Fusion OCR work queue */
2256 void megasas_fusion_ocr_wq(struct work_struct
*work
)
2258 struct megasas_instance
*instance
=
2259 container_of(work
, struct megasas_instance
, work_init
);
2261 megasas_reset_fusion(instance
->host
);
2264 struct megasas_instance_template megasas_instance_template_fusion
= {
2265 .fire_cmd
= megasas_fire_cmd_fusion
,
2266 .enable_intr
= megasas_enable_intr_fusion
,
2267 .disable_intr
= megasas_disable_intr_fusion
,
2268 .clear_intr
= megasas_clear_intr_fusion
,
2269 .read_fw_status_reg
= megasas_read_fw_status_reg_fusion
,
2270 .adp_reset
= megasas_adp_reset_fusion
,
2271 .check_reset
= megasas_check_reset_fusion
,
2272 .service_isr
= megasas_isr_fusion
,
2273 .tasklet
= megasas_complete_cmd_dpc_fusion
,
2274 .init_adapter
= megasas_init_adapter_fusion
,
2275 .build_and_issue_cmd
= megasas_build_and_issue_cmd_fusion
,
2276 .issue_dcmd
= megasas_issue_dcmd_fusion
,