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
);
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 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK
, &(regs
)->outbound_intr_mask
);
106 /* Dummy readl to force pci flush */
107 readl(®s
->outbound_intr_mask
);
111 * megasas_disable_intr_fusion - Disables interrupt
112 * @regs: MFI register set
115 megasas_disable_intr_fusion(struct megasas_register_set __iomem
*regs
)
117 u32 mask
= 0xFFFFFFFF;
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
))
143 * dummy read to flush PCI
145 readl(®s
->outbound_intr_status
);
151 * megasas_get_cmd_fusion - Get a command from the free pool
152 * @instance: Adapter soft state
154 * Returns a free command from the pool
156 struct megasas_cmd_fusion
*megasas_get_cmd_fusion(struct megasas_instance
160 struct fusion_context
*fusion
=
161 (struct fusion_context
*)instance
->ctrl_context
;
162 struct megasas_cmd_fusion
*cmd
= NULL
;
164 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
166 if (!list_empty(&fusion
->cmd_pool
)) {
167 cmd
= list_entry((&fusion
->cmd_pool
)->next
,
168 struct megasas_cmd_fusion
, list
);
169 list_del_init(&cmd
->list
);
171 printk(KERN_ERR
"megasas: Command pool (fusion) empty!\n");
174 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
179 * megasas_return_cmd_fusion - Return a cmd to free command pool
180 * @instance: Adapter soft state
181 * @cmd: Command packet to be returned to free command pool
184 megasas_return_cmd_fusion(struct megasas_instance
*instance
,
185 struct megasas_cmd_fusion
*cmd
)
188 struct fusion_context
*fusion
=
189 (struct fusion_context
*)instance
->ctrl_context
;
191 spin_lock_irqsave(&fusion
->cmd_pool_lock
, flags
);
194 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
;
195 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
197 spin_unlock_irqrestore(&fusion
->cmd_pool_lock
, flags
);
201 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
202 * @instance: Adapter soft state
204 static void megasas_teardown_frame_pool_fusion(
205 struct megasas_instance
*instance
)
208 struct fusion_context
*fusion
= instance
->ctrl_context
;
210 u16 max_cmd
= instance
->max_fw_cmds
;
212 struct megasas_cmd_fusion
*cmd
;
214 if (!fusion
->sg_dma_pool
|| !fusion
->sense_dma_pool
) {
215 printk(KERN_ERR
"megasas: dma pool is null. SG Pool %p, "
216 "sense pool : %p\n", fusion
->sg_dma_pool
,
217 fusion
->sense_dma_pool
);
222 * Return all frames to pool
224 for (i
= 0; i
< max_cmd
; i
++) {
226 cmd
= fusion
->cmd_list
[i
];
229 pci_pool_free(fusion
->sg_dma_pool
, cmd
->sg_frame
,
230 cmd
->sg_frame_phys_addr
);
233 pci_pool_free(fusion
->sense_dma_pool
, cmd
->sense
,
234 cmd
->sense_phys_addr
);
238 * Now destroy the pool itself
240 pci_pool_destroy(fusion
->sg_dma_pool
);
241 pci_pool_destroy(fusion
->sense_dma_pool
);
243 fusion
->sg_dma_pool
= NULL
;
244 fusion
->sense_dma_pool
= NULL
;
248 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
249 * @instance: Adapter soft state
252 megasas_free_cmds_fusion(struct megasas_instance
*instance
)
255 struct fusion_context
*fusion
= instance
->ctrl_context
;
257 u32 max_cmds
, req_sz
, reply_sz
, io_frames_sz
;
260 req_sz
= fusion
->request_alloc_sz
;
261 reply_sz
= fusion
->reply_alloc_sz
;
262 io_frames_sz
= fusion
->io_frames_alloc_sz
;
264 max_cmds
= instance
->max_fw_cmds
;
266 /* Free descriptors and request Frames memory */
267 if (fusion
->req_frames_desc
)
268 dma_free_coherent(&instance
->pdev
->dev
, req_sz
,
269 fusion
->req_frames_desc
,
270 fusion
->req_frames_desc_phys
);
272 if (fusion
->reply_frames_desc
) {
273 pci_pool_free(fusion
->reply_frames_desc_pool
,
274 fusion
->reply_frames_desc
,
275 fusion
->reply_frames_desc_phys
);
276 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
279 if (fusion
->io_request_frames
) {
280 pci_pool_free(fusion
->io_request_frames_pool
,
281 fusion
->io_request_frames
,
282 fusion
->io_request_frames_phys
);
283 pci_pool_destroy(fusion
->io_request_frames_pool
);
286 /* Free the Fusion frame pool */
287 megasas_teardown_frame_pool_fusion(instance
);
289 /* Free all the commands in the cmd_list */
290 for (i
= 0; i
< max_cmds
; i
++)
291 kfree(fusion
->cmd_list
[i
]);
293 /* Free the cmd_list buffer itself */
294 kfree(fusion
->cmd_list
);
295 fusion
->cmd_list
= NULL
;
297 INIT_LIST_HEAD(&fusion
->cmd_pool
);
301 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
302 * @instance: Adapter soft state
305 static int megasas_create_frame_pool_fusion(struct megasas_instance
*instance
)
309 struct fusion_context
*fusion
;
310 struct megasas_cmd_fusion
*cmd
;
311 u32 total_sz_chain_frame
;
313 fusion
= instance
->ctrl_context
;
314 max_cmd
= instance
->max_fw_cmds
;
316 total_sz_chain_frame
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
319 * Use DMA pool facility provided by PCI layer
322 fusion
->sg_dma_pool
= pci_pool_create("megasas sg pool fusion",
324 total_sz_chain_frame
, 4,
326 if (!fusion
->sg_dma_pool
) {
327 printk(KERN_DEBUG
"megasas: failed to setup request pool "
331 fusion
->sense_dma_pool
= pci_pool_create("megasas sense pool fusion",
333 SCSI_SENSE_BUFFERSIZE
, 64, 0);
335 if (!fusion
->sense_dma_pool
) {
336 printk(KERN_DEBUG
"megasas: failed to setup sense pool "
338 pci_pool_destroy(fusion
->sg_dma_pool
);
339 fusion
->sg_dma_pool
= NULL
;
344 * Allocate and attach a frame to each of the commands in cmd_list
346 for (i
= 0; i
< max_cmd
; i
++) {
348 cmd
= fusion
->cmd_list
[i
];
350 cmd
->sg_frame
= pci_pool_alloc(fusion
->sg_dma_pool
,
352 &cmd
->sg_frame_phys_addr
);
354 cmd
->sense
= pci_pool_alloc(fusion
->sense_dma_pool
,
355 GFP_KERNEL
, &cmd
->sense_phys_addr
);
357 * megasas_teardown_frame_pool_fusion() takes care of freeing
358 * whatever has been allocated
360 if (!cmd
->sg_frame
|| !cmd
->sense
) {
361 printk(KERN_DEBUG
"megasas: pci_pool_alloc failed\n");
362 megasas_teardown_frame_pool_fusion(instance
);
370 * megasas_alloc_cmds_fusion - Allocates the command packets
371 * @instance: Adapter soft state
374 * Each frame has a 32-bit field called context. This context is used to get
375 * back the megasas_cmd_fusion from the frame when a frame gets completed
376 * In this driver, the 32 bit values are the indices into an array cmd_list.
377 * This array is used only to look up the megasas_cmd_fusion given the context.
378 * The free commands themselves are maintained in a linked list called cmd_pool.
380 * cmds are formed in the io_request and sg_frame members of the
381 * megasas_cmd_fusion. The context field is used to get a request descriptor
382 * and is used as SMID of the cmd.
383 * SMID value range is from 1 to max_fw_cmds.
386 megasas_alloc_cmds_fusion(struct megasas_instance
*instance
)
389 u32 max_cmd
, io_frames_sz
;
390 struct fusion_context
*fusion
;
391 struct megasas_cmd_fusion
*cmd
;
392 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
394 dma_addr_t io_req_base_phys
;
397 fusion
= instance
->ctrl_context
;
399 max_cmd
= instance
->max_fw_cmds
;
401 fusion
->req_frames_desc
=
402 dma_alloc_coherent(&instance
->pdev
->dev
,
403 fusion
->request_alloc_sz
,
404 &fusion
->req_frames_desc_phys
, GFP_KERNEL
);
406 if (!fusion
->req_frames_desc
) {
407 printk(KERN_ERR
"megasas; Could not allocate memory for "
412 fusion
->reply_frames_desc_pool
=
413 pci_pool_create("reply_frames pool", instance
->pdev
,
414 fusion
->reply_alloc_sz
, 16, 0);
416 if (!fusion
->reply_frames_desc_pool
) {
417 printk(KERN_ERR
"megasas; Could not allocate memory for "
418 "reply_frame pool\n");
419 goto fail_reply_desc
;
422 fusion
->reply_frames_desc
=
423 pci_pool_alloc(fusion
->reply_frames_desc_pool
, GFP_KERNEL
,
424 &fusion
->reply_frames_desc_phys
);
425 if (!fusion
->reply_frames_desc
) {
426 printk(KERN_ERR
"megasas; Could not allocate memory for "
427 "reply_frame pool\n");
428 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
429 goto fail_reply_desc
;
432 reply_desc
= fusion
->reply_frames_desc
;
433 for (i
= 0; i
< fusion
->reply_q_depth
; i
++, reply_desc
++)
434 reply_desc
->Words
= ULLONG_MAX
;
436 io_frames_sz
= fusion
->io_frames_alloc_sz
;
438 fusion
->io_request_frames_pool
=
439 pci_pool_create("io_request_frames pool", instance
->pdev
,
440 fusion
->io_frames_alloc_sz
, 16, 0);
442 if (!fusion
->io_request_frames_pool
) {
443 printk(KERN_ERR
"megasas: Could not allocate memory for "
444 "io_request_frame pool\n");
448 fusion
->io_request_frames
=
449 pci_pool_alloc(fusion
->io_request_frames_pool
, GFP_KERNEL
,
450 &fusion
->io_request_frames_phys
);
451 if (!fusion
->io_request_frames
) {
452 printk(KERN_ERR
"megasas: Could not allocate memory for "
453 "io_request_frames frames\n");
454 pci_pool_destroy(fusion
->io_request_frames_pool
);
459 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
460 * Allocate the dynamic array first and then allocate individual
463 fusion
->cmd_list
= kmalloc(sizeof(struct megasas_cmd_fusion
*)
464 *max_cmd
, GFP_KERNEL
);
466 if (!fusion
->cmd_list
) {
467 printk(KERN_DEBUG
"megasas: out of memory. Could not alloc "
468 "memory for cmd_list_fusion\n");
472 memset(fusion
->cmd_list
, 0, sizeof(struct megasas_cmd_fusion
*)
475 max_cmd
= instance
->max_fw_cmds
;
476 for (i
= 0; i
< max_cmd
; i
++) {
477 fusion
->cmd_list
[i
] = kmalloc(sizeof(struct megasas_cmd_fusion
),
479 if (!fusion
->cmd_list
[i
]) {
480 printk(KERN_ERR
"Could not alloc cmd list fusion\n");
482 for (j
= 0; j
< i
; j
++)
483 kfree(fusion
->cmd_list
[j
]);
485 kfree(fusion
->cmd_list
);
486 fusion
->cmd_list
= NULL
;
491 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
492 io_req_base
= fusion
->io_request_frames
+
493 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
494 io_req_base_phys
= fusion
->io_request_frames_phys
+
495 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
498 * Add all the commands to command pool (fusion->cmd_pool)
501 /* SMID 0 is reserved. Set SMID/index from 1 */
502 for (i
= 0; i
< max_cmd
; i
++) {
503 cmd
= fusion
->cmd_list
[i
];
504 offset
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
* i
;
505 memset(cmd
, 0, sizeof(struct megasas_cmd_fusion
));
508 cmd
->sync_cmd_idx
= (u32
)ULONG_MAX
; /* Set to Invalid */
509 cmd
->instance
= instance
;
511 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
512 (io_req_base
+ offset
);
513 memset(cmd
->io_request
, 0,
514 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
));
515 cmd
->io_request_phys_addr
= io_req_base_phys
+ offset
;
517 list_add_tail(&cmd
->list
, &fusion
->cmd_pool
);
521 * Create a frame pool and assign one frame to each cmd
523 if (megasas_create_frame_pool_fusion(instance
)) {
524 printk(KERN_DEBUG
"megasas: Error creating frame DMA pool\n");
525 megasas_free_cmds_fusion(instance
);
532 pci_pool_free(fusion
->io_request_frames_pool
, fusion
->io_request_frames
,
533 fusion
->io_request_frames_phys
);
534 pci_pool_destroy(fusion
->io_request_frames_pool
);
536 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
537 fusion
->reply_frames_desc
,
538 fusion
->reply_frames_desc_phys
);
539 pci_pool_free(fusion
->reply_frames_desc_pool
,
540 fusion
->reply_frames_desc
,
541 fusion
->reply_frames_desc_phys
);
542 pci_pool_destroy(fusion
->reply_frames_desc_pool
);
545 dma_free_coherent(&instance
->pdev
->dev
, fusion
->request_alloc_sz
,
546 fusion
->req_frames_desc
,
547 fusion
->req_frames_desc_phys
);
553 * wait_and_poll - Issues a polling command
554 * @instance: Adapter soft state
555 * @cmd: Command packet to be issued
557 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
560 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
563 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
565 u32 msecs
= MFI_POLL_TIMEOUT_SECS
* 1000;
568 * Wait for cmd_status to change
570 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
+= 20) {
575 if (frame_hdr
->cmd_status
== 0xff)
582 * megasas_ioc_init_fusion - Initializes the FW
583 * @instance: Adapter soft state
585 * Issues the IOC Init cmd
588 megasas_ioc_init_fusion(struct megasas_instance
*instance
)
590 struct megasas_init_frame
*init_frame
;
591 struct MPI2_IOC_INIT_REQUEST
*IOCInitMessage
;
592 dma_addr_t ioc_init_handle
;
594 struct megasas_cmd
*cmd
;
596 struct fusion_context
*fusion
;
597 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
599 struct megasas_header
*frame_hdr
;
601 fusion
= instance
->ctrl_context
;
603 cmd
= megasas_get_cmd(instance
);
606 printk(KERN_ERR
"Could not allocate cmd for INIT Frame\n");
612 dma_alloc_coherent(&instance
->pdev
->dev
,
613 sizeof(struct MPI2_IOC_INIT_REQUEST
),
614 &ioc_init_handle
, GFP_KERNEL
);
616 if (!IOCInitMessage
) {
617 printk(KERN_ERR
"Could not allocate memory for "
623 memset(IOCInitMessage
, 0, sizeof(struct MPI2_IOC_INIT_REQUEST
));
625 IOCInitMessage
->Function
= MPI2_FUNCTION_IOC_INIT
;
626 IOCInitMessage
->WhoInit
= MPI2_WHOINIT_HOST_DRIVER
;
627 IOCInitMessage
->MsgVersion
= MPI2_VERSION
;
628 IOCInitMessage
->HeaderVersion
= MPI2_HEADER_VERSION
;
629 IOCInitMessage
->SystemRequestFrameSize
=
630 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
/ 4;
632 IOCInitMessage
->ReplyDescriptorPostQueueDepth
= fusion
->reply_q_depth
;
633 IOCInitMessage
->ReplyDescriptorPostQueueAddress
=
634 fusion
->reply_frames_desc_phys
;
635 IOCInitMessage
->SystemRequestFrameBaseAddress
=
636 fusion
->io_request_frames_phys
;
638 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
639 memset(init_frame
, 0, MEGAMFI_FRAME_SIZE
);
641 frame_hdr
= &cmd
->frame
->hdr
;
642 context
= init_frame
->context
;
643 init_frame
->context
= context
;
645 frame_hdr
->cmd_status
= 0xFF;
646 frame_hdr
->flags
|= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
648 init_frame
->cmd
= MFI_CMD_INIT
;
649 init_frame
->cmd_status
= 0xFF;
651 init_frame
->queue_info_new_phys_addr_lo
= ioc_init_handle
;
652 init_frame
->data_xfer_len
= sizeof(struct MPI2_IOC_INIT_REQUEST
);
655 (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)fusion
->req_frames_desc
;
657 req_desc
->Words
= cmd
->frame_phys_addr
;
658 req_desc
->MFAIo
.RequestFlags
=
659 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA
<<
660 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
663 * disable the intr before firing the init frame
665 instance
->instancet
->disable_intr(instance
->reg_set
);
667 for (i
= 0; i
< (10 * 1000); i
+= 20) {
668 if (readl(&instance
->reg_set
->doorbell
) & 1)
674 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
675 req_desc
->u
.high
, instance
->reg_set
);
677 wait_and_poll(instance
, cmd
);
679 frame_hdr
= &cmd
->frame
->hdr
;
680 if (frame_hdr
->cmd_status
!= 0) {
684 printk(KERN_ERR
"megasas:IOC Init cmd success\n");
689 megasas_return_cmd(instance
, cmd
);
691 dma_free_coherent(&instance
->pdev
->dev
,
692 sizeof(struct MPI2_IOC_INIT_REQUEST
),
693 IOCInitMessage
, ioc_init_handle
);
699 * megasas_return_cmd_for_smid - Returns a cmd_fusion for a SMID
700 * @instance: Adapter soft state
704 megasas_return_cmd_for_smid(struct megasas_instance
*instance
, u16 smid
)
706 struct fusion_context
*fusion
;
707 struct megasas_cmd_fusion
*cmd
;
709 fusion
= instance
->ctrl_context
;
710 cmd
= fusion
->cmd_list
[smid
- 1];
711 megasas_return_cmd_fusion(instance
, cmd
);
715 * megasas_get_ld_map_info - Returns FW's ld_map structure
716 * @instance: Adapter soft state
717 * @pend: Pend the command or not
718 * Issues an internal command (DCMD) to get the FW's controller PD
719 * list structure. This information is mainly used to find out SYSTEM
720 * supported by the FW.
723 megasas_get_ld_map_info(struct megasas_instance
*instance
)
726 struct megasas_cmd
*cmd
;
727 struct megasas_dcmd_frame
*dcmd
;
728 struct MR_FW_RAID_MAP_ALL
*ci
;
731 struct fusion_context
*fusion
;
733 cmd
= megasas_get_cmd(instance
);
736 printk(KERN_DEBUG
"megasas: Failed to get cmd for map info.\n");
740 fusion
= instance
->ctrl_context
;
743 megasas_return_cmd(instance
, cmd
);
747 dcmd
= &cmd
->frame
->dcmd
;
749 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
750 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
752 ci
= fusion
->ld_map
[(instance
->map_id
& 1)];
753 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
& 1)];
756 printk(KERN_DEBUG
"Failed to alloc mem for ld_map_info\n");
757 megasas_return_cmd(instance
, cmd
);
761 memset(ci
, 0, sizeof(*ci
));
762 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
764 dcmd
->cmd
= MFI_CMD_DCMD
;
765 dcmd
->cmd_status
= 0xFF;
767 dcmd
->flags
= MFI_FRAME_DIR_READ
;
770 dcmd
->data_xfer_len
= size_map_info
;
771 dcmd
->opcode
= MR_DCMD_LD_MAP_GET_INFO
;
772 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
773 dcmd
->sgl
.sge32
[0].length
= size_map_info
;
775 if (!megasas_issue_polled(instance
, cmd
))
778 printk(KERN_ERR
"megasas: Get LD Map Info Failed\n");
782 megasas_return_cmd(instance
, cmd
);
788 megasas_get_map_info(struct megasas_instance
*instance
)
790 struct fusion_context
*fusion
= instance
->ctrl_context
;
792 fusion
->fast_path_io
= 0;
793 if (!megasas_get_ld_map_info(instance
)) {
794 if (MR_ValidateMapInfo(fusion
->ld_map
[(instance
->map_id
& 1)],
795 fusion
->load_balance_info
)) {
796 fusion
->fast_path_io
= 1;
804 * megasas_sync_map_info - Returns FW's ld_map structure
805 * @instance: Adapter soft state
807 * Issues an internal command (DCMD) to get the FW's controller PD
808 * list structure. This information is mainly used to find out SYSTEM
809 * supported by the FW.
812 megasas_sync_map_info(struct megasas_instance
*instance
)
815 struct megasas_cmd
*cmd
;
816 struct megasas_dcmd_frame
*dcmd
;
817 u32 size_sync_info
, num_lds
;
818 struct fusion_context
*fusion
;
819 struct MR_LD_TARGET_SYNC
*ci
= NULL
;
820 struct MR_FW_RAID_MAP_ALL
*map
;
821 struct MR_LD_RAID
*raid
;
822 struct MR_LD_TARGET_SYNC
*ld_sync
;
826 cmd
= megasas_get_cmd(instance
);
829 printk(KERN_DEBUG
"megasas: Failed to get cmd for sync"
834 fusion
= instance
->ctrl_context
;
837 megasas_return_cmd(instance
, cmd
);
841 map
= fusion
->ld_map
[instance
->map_id
& 1];
843 num_lds
= map
->raidMap
.ldCount
;
845 dcmd
= &cmd
->frame
->dcmd
;
847 size_sync_info
= sizeof(struct MR_LD_TARGET_SYNC
) *num_lds
;
849 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
851 ci
= (struct MR_LD_TARGET_SYNC
*)
852 fusion
->ld_map
[(instance
->map_id
- 1) & 1];
853 memset(ci
, 0, sizeof(struct MR_FW_RAID_MAP_ALL
));
855 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
- 1) & 1];
857 ld_sync
= (struct MR_LD_TARGET_SYNC
*)ci
;
859 for (i
= 0; i
< num_lds
; i
++, ld_sync
++) {
860 raid
= MR_LdRaidGet(i
, map
);
861 ld_sync
->targetId
= MR_GetLDTgtId(i
, map
);
862 ld_sync
->seqNum
= raid
->seqNum
;
865 size_map_info
= sizeof(struct MR_FW_RAID_MAP
) +
866 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
868 dcmd
->cmd
= MFI_CMD_DCMD
;
869 dcmd
->cmd_status
= 0xFF;
871 dcmd
->flags
= MFI_FRAME_DIR_WRITE
;
874 dcmd
->data_xfer_len
= size_map_info
;
875 dcmd
->mbox
.b
[0] = num_lds
;
876 dcmd
->mbox
.b
[1] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
877 dcmd
->opcode
= MR_DCMD_LD_MAP_GET_INFO
;
878 dcmd
->sgl
.sge32
[0].phys_addr
= ci_h
;
879 dcmd
->sgl
.sge32
[0].length
= size_map_info
;
881 instance
->map_update_cmd
= cmd
;
883 instance
->instancet
->issue_dcmd(instance
, cmd
);
889 * megasas_init_adapter_fusion - Initializes the FW
890 * @instance: Adapter soft state
892 * This is the main function for initializing firmware.
895 megasas_init_adapter_fusion(struct megasas_instance
*instance
)
897 struct megasas_register_set __iomem
*reg_set
;
898 struct fusion_context
*fusion
;
902 fusion
= instance
->ctrl_context
;
904 reg_set
= instance
->reg_set
;
907 * Get various operational parameters from status register
909 instance
->max_fw_cmds
=
910 instance
->instancet
->read_fw_status_reg(reg_set
) & 0x00FFFF;
911 instance
->max_fw_cmds
= min(instance
->max_fw_cmds
, (u16
)1008);
914 * Reduce the max supported cmds by 1. This is to ensure that the
915 * reply_q_sz (1 more than the max cmd that driver may send)
916 * does not exceed max cmds that the FW can support
918 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
919 /* Only internal cmds (DCMD) need to have MFI frames */
920 instance
->max_mfi_cmds
= MEGASAS_INT_CMDS
;
922 max_cmd
= instance
->max_fw_cmds
;
924 fusion
->reply_q_depth
= ((max_cmd
+ 1 + 15)/16)*16;
926 fusion
->request_alloc_sz
=
927 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *max_cmd
;
928 fusion
->reply_alloc_sz
= sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
)
929 *(fusion
->reply_q_depth
);
930 fusion
->io_frames_alloc_sz
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
+
931 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
*
932 (max_cmd
+ 1)); /* Extra 1 for SMID 0 */
934 fusion
->max_sge_in_main_msg
=
935 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
936 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
))/16;
938 fusion
->max_sge_in_chain
=
939 MEGASAS_MAX_SZ_CHAIN_FRAME
/ sizeof(union MPI2_SGE_IO_UNION
);
941 instance
->max_num_sge
= fusion
->max_sge_in_main_msg
+
942 fusion
->max_sge_in_chain
- 2;
944 /* Used for pass thru MFI frame (DCMD) */
945 fusion
->chain_offset_mfi_pthru
=
946 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
)/16;
948 fusion
->chain_offset_io_request
=
949 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
950 sizeof(union MPI2_SGE_IO_UNION
))/16;
952 fusion
->last_reply_idx
= 0;
955 * Allocate memory for descriptors
956 * Create a pool of commands
958 if (megasas_alloc_cmds(instance
))
959 goto fail_alloc_mfi_cmds
;
960 if (megasas_alloc_cmds_fusion(instance
))
961 goto fail_alloc_cmds
;
963 if (megasas_ioc_init_fusion(instance
))
966 instance
->flag_ieee
= 1;
968 fusion
->map_sz
= sizeof(struct MR_FW_RAID_MAP
) +
969 (sizeof(struct MR_LD_SPAN_MAP
) *(MAX_LOGICAL_DRIVES
- 1));
971 fusion
->fast_path_io
= 0;
973 for (i
= 0; i
< 2; i
++) {
974 fusion
->ld_map
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
976 &fusion
->ld_map_phys
[i
],
978 if (!fusion
->ld_map
[i
]) {
979 printk(KERN_ERR
"megasas: Could not allocate memory "
985 if (!megasas_get_map_info(instance
))
986 megasas_sync_map_info(instance
);
992 dma_free_coherent(&instance
->pdev
->dev
, fusion
->map_sz
,
993 fusion
->ld_map
[0], fusion
->ld_map_phys
[0]);
995 megasas_free_cmds_fusion(instance
);
997 megasas_free_cmds(instance
);
1003 * megasas_fire_cmd_fusion - Sends command to the FW
1004 * @frame_phys_addr : Physical address of cmd
1005 * @frame_count : Number of frames for the command
1006 * @regs : MFI register set
1009 megasas_fire_cmd_fusion(struct megasas_instance
*instance
,
1010 dma_addr_t req_desc_lo
,
1012 struct megasas_register_set __iomem
*regs
)
1014 unsigned long flags
;
1016 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1019 &(regs
)->inbound_low_queue_port
);
1020 writel(req_desc_hi
, &(regs
)->inbound_high_queue_port
);
1021 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1025 * map_cmd_status - Maps FW cmd status to OS cmd status
1026 * @cmd : Pointer to cmd
1027 * @status : status of cmd returned by FW
1028 * @ext_status : ext status of cmd returned by FW
1032 map_cmd_status(struct megasas_cmd_fusion
*cmd
, u8 status
, u8 ext_status
)
1038 cmd
->scmd
->result
= DID_OK
<< 16;
1041 case MFI_STAT_SCSI_IO_FAILED
:
1042 case MFI_STAT_LD_INIT_IN_PROGRESS
:
1043 cmd
->scmd
->result
= (DID_ERROR
<< 16) | ext_status
;
1046 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
1048 cmd
->scmd
->result
= (DID_OK
<< 16) | ext_status
;
1049 if (ext_status
== SAM_STAT_CHECK_CONDITION
) {
1050 memset(cmd
->scmd
->sense_buffer
, 0,
1051 SCSI_SENSE_BUFFERSIZE
);
1052 memcpy(cmd
->scmd
->sense_buffer
, cmd
->sense
,
1053 SCSI_SENSE_BUFFERSIZE
);
1054 cmd
->scmd
->result
|= DRIVER_SENSE
<< 24;
1058 case MFI_STAT_LD_OFFLINE
:
1059 case MFI_STAT_DEVICE_NOT_FOUND
:
1060 cmd
->scmd
->result
= DID_BAD_TARGET
<< 16;
1064 printk(KERN_DEBUG
"megasas: FW status %#x\n", status
);
1065 cmd
->scmd
->result
= DID_ERROR
<< 16;
1071 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1072 * @instance: Adapter soft state
1073 * @scp: SCSI command from the mid-layer
1074 * @sgl_ptr: SGL to be filled in
1075 * @cmd: cmd we are working on
1077 * If successful, this function returns the number of SG elements.
1080 megasas_make_sgl_fusion(struct megasas_instance
*instance
,
1081 struct scsi_cmnd
*scp
,
1082 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
1083 struct megasas_cmd_fusion
*cmd
)
1085 int i
, sg_processed
;
1086 int sge_count
, sge_idx
;
1087 struct scatterlist
*os_sgl
;
1088 struct fusion_context
*fusion
;
1090 fusion
= instance
->ctrl_context
;
1092 cmd
->io_request
->ChainOffset
= 0;
1094 sge_count
= scsi_dma_map(scp
);
1096 BUG_ON(sge_count
< 0);
1098 if (sge_count
> instance
->max_num_sge
|| !sge_count
)
1101 if (sge_count
> fusion
->max_sge_in_main_msg
) {
1102 /* One element to store the chain info */
1103 sge_idx
= fusion
->max_sge_in_main_msg
- 1;
1105 sge_idx
= sge_count
;
1107 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
1108 sgl_ptr
->Length
= sg_dma_len(os_sgl
);
1109 sgl_ptr
->Address
= sg_dma_address(os_sgl
);
1113 sg_processed
= i
+ 1;
1115 if ((sg_processed
== (fusion
->max_sge_in_main_msg
- 1)) &&
1116 (sge_count
> fusion
->max_sge_in_main_msg
)) {
1118 struct MPI25_IEEE_SGE_CHAIN64
*sg_chain
;
1119 cmd
->io_request
->ChainOffset
=
1120 fusion
->chain_offset_io_request
;
1122 /* Prepare chain element */
1123 sg_chain
->NextChainOffset
= 0;
1124 sg_chain
->Flags
= (IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1125 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
);
1126 sg_chain
->Length
= (sizeof(union MPI2_SGE_IO_UNION
)
1127 *(sge_count
- sg_processed
));
1128 sg_chain
->Address
= cmd
->sg_frame_phys_addr
;
1131 (struct MPI25_IEEE_SGE_CHAIN64
*)cmd
->sg_frame
;
1139 * megasas_set_pd_lba - Sets PD LBA
1141 * @cdb_len: cdb length
1142 * @start_blk: Start block of IO
1144 * Used to set the PD LBA in CDB for FP IOs
1147 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
, u8 cdb_len
,
1148 struct IO_REQUEST_INFO
*io_info
, struct scsi_cmnd
*scp
,
1149 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
, u32 ref_tag
)
1151 struct MR_LD_RAID
*raid
;
1153 u64 start_blk
= io_info
->pdBlock
;
1154 u8
*cdb
= io_request
->CDB
.CDB32
;
1155 u32 num_blocks
= io_info
->numBlocks
;
1156 u8 opcode
, flagvals
, groupnum
, control
;
1158 /* Check if T10 PI (DIF) is enabled for this LD */
1159 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, local_map_ptr
);
1160 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
1161 if (raid
->capability
.ldPiMode
== MR_PROT_INFO_TYPE_CONTROLLER
) {
1162 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1163 cdb
[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD
;
1164 cdb
[7] = MEGASAS_SCSI_ADDL_CDB_LEN
;
1166 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1167 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32
;
1169 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32
;
1170 cdb
[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL
;
1173 cdb
[12] = (u8
)((start_blk
>> 56) & 0xff);
1174 cdb
[13] = (u8
)((start_blk
>> 48) & 0xff);
1175 cdb
[14] = (u8
)((start_blk
>> 40) & 0xff);
1176 cdb
[15] = (u8
)((start_blk
>> 32) & 0xff);
1177 cdb
[16] = (u8
)((start_blk
>> 24) & 0xff);
1178 cdb
[17] = (u8
)((start_blk
>> 16) & 0xff);
1179 cdb
[18] = (u8
)((start_blk
>> 8) & 0xff);
1180 cdb
[19] = (u8
)(start_blk
& 0xff);
1182 /* Logical block reference tag */
1183 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
=
1184 cpu_to_be32(ref_tag
);
1185 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0xffff;
1187 io_request
->DataLength
= num_blocks
* 512;
1188 io_request
->IoFlags
= 32; /* Specify 32-byte cdb */
1190 /* Transfer length */
1191 cdb
[28] = (u8
)((num_blocks
>> 24) & 0xff);
1192 cdb
[29] = (u8
)((num_blocks
>> 16) & 0xff);
1193 cdb
[30] = (u8
)((num_blocks
>> 8) & 0xff);
1194 cdb
[31] = (u8
)(num_blocks
& 0xff);
1196 /* set SCSI IO EEDPFlags */
1197 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
) {
1198 io_request
->EEDPFlags
=
1199 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1200 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG
|
1201 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
|
1202 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG
|
1203 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD
;
1205 io_request
->EEDPFlags
=
1206 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
1207 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
;
1209 io_request
->Control
|= (0x4 << 26);
1210 io_request
->EEDPBlockSize
= MEGASAS_EEDPBLOCKSIZE
;
1212 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1213 if (((cdb_len
== 12) || (cdb_len
== 16)) &&
1214 (start_blk
<= 0xffffffff)) {
1215 if (cdb_len
== 16) {
1216 opcode
= cdb
[0] == READ_16
? READ_10
: WRITE_10
;
1221 opcode
= cdb
[0] == READ_12
? READ_10
: WRITE_10
;
1227 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
1234 /* Transfer length */
1235 cdb
[8] = (u8
)(num_blocks
& 0xff);
1236 cdb
[7] = (u8
)((num_blocks
>> 8) & 0xff);
1241 /* Normal case, just load LBA here */
1245 u8 val
= cdb
[1] & 0xE0;
1246 cdb
[3] = (u8
)(start_blk
& 0xff);
1247 cdb
[2] = (u8
)((start_blk
>> 8) & 0xff);
1248 cdb
[1] = val
| ((u8
)(start_blk
>> 16) & 0x1f);
1252 cdb
[5] = (u8
)(start_blk
& 0xff);
1253 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1254 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1255 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1258 cdb
[5] = (u8
)(start_blk
& 0xff);
1259 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
1260 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
1261 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
1264 cdb
[9] = (u8
)(start_blk
& 0xff);
1265 cdb
[8] = (u8
)((start_blk
>> 8) & 0xff);
1266 cdb
[7] = (u8
)((start_blk
>> 16) & 0xff);
1267 cdb
[6] = (u8
)((start_blk
>> 24) & 0xff);
1268 cdb
[5] = (u8
)((start_blk
>> 32) & 0xff);
1269 cdb
[4] = (u8
)((start_blk
>> 40) & 0xff);
1270 cdb
[3] = (u8
)((start_blk
>> 48) & 0xff);
1271 cdb
[2] = (u8
)((start_blk
>> 56) & 0xff);
1278 * megasas_build_ldio_fusion - Prepares IOs to devices
1279 * @instance: Adapter soft state
1280 * @scp: SCSI command
1281 * @cmd: Command to be prepared
1283 * Prepares the io_request and chain elements (sg_frame) for IO
1284 * The IO can be for PD (Fast Path) or LD
1287 megasas_build_ldio_fusion(struct megasas_instance
*instance
,
1288 struct scsi_cmnd
*scp
,
1289 struct megasas_cmd_fusion
*cmd
)
1292 u32 start_lba_lo
, start_lba_hi
, device_id
;
1293 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1294 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1295 struct IO_REQUEST_INFO io_info
;
1296 struct fusion_context
*fusion
;
1297 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1299 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1301 fusion
= instance
->ctrl_context
;
1303 io_request
= cmd
->io_request
;
1304 io_request
->RaidContext
.VirtualDiskTgtId
= device_id
;
1305 io_request
->RaidContext
.status
= 0;
1306 io_request
->RaidContext
.exStatus
= 0;
1308 req_desc
= (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)cmd
->request_desc
;
1315 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1317 if (scp
->cmd_len
== 6) {
1318 io_request
->DataLength
= (u32
) scp
->cmnd
[4];
1319 start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
1320 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
1322 start_lba_lo
&= 0x1FFFFF;
1326 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1328 else if (scp
->cmd_len
== 10) {
1329 io_request
->DataLength
= (u32
) scp
->cmnd
[8] |
1330 ((u32
) scp
->cmnd
[7] << 8);
1331 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1332 ((u32
) scp
->cmnd
[3] << 16) |
1333 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1337 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1339 else if (scp
->cmd_len
== 12) {
1340 io_request
->DataLength
= ((u32
) scp
->cmnd
[6] << 24) |
1341 ((u32
) scp
->cmnd
[7] << 16) |
1342 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1343 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
1344 ((u32
) scp
->cmnd
[3] << 16) |
1345 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1349 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1351 else if (scp
->cmd_len
== 16) {
1352 io_request
->DataLength
= ((u32
) scp
->cmnd
[10] << 24) |
1353 ((u32
) scp
->cmnd
[11] << 16) |
1354 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
1355 start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
1356 ((u32
) scp
->cmnd
[7] << 16) |
1357 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
1359 start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
1360 ((u32
) scp
->cmnd
[3] << 16) |
1361 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
1364 memset(&io_info
, 0, sizeof(struct IO_REQUEST_INFO
));
1365 io_info
.ldStartBlock
= ((u64
)start_lba_hi
<< 32) | start_lba_lo
;
1366 io_info
.numBlocks
= io_request
->DataLength
;
1367 io_info
.ldTgtId
= device_id
;
1369 if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1372 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1374 if ((MR_TargetIdToLdGet(device_id
, local_map_ptr
) >=
1375 MAX_LOGICAL_DRIVES
) || (!fusion
->fast_path_io
)) {
1376 io_request
->RaidContext
.regLockFlags
= 0;
1379 if (MR_BuildRaidContext(&io_info
, &io_request
->RaidContext
,
1381 fp_possible
= io_info
.fpOkForIo
;
1385 megasas_set_pd_lba(io_request
, scp
->cmd_len
, &io_info
, scp
,
1386 local_map_ptr
, start_lba_lo
);
1387 io_request
->DataLength
= scsi_bufflen(scp
);
1388 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
1389 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1390 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1391 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1392 if ((fusion
->load_balance_info
[device_id
].loadBalanceFlag
) &&
1395 get_updated_dev_handle(
1396 &fusion
->load_balance_info
[device_id
],
1398 scp
->SCp
.Status
|= MEGASAS_LOAD_BALANCE_FLAG
;
1400 scp
->SCp
.Status
&= ~MEGASAS_LOAD_BALANCE_FLAG
;
1401 cmd
->request_desc
->SCSIIO
.DevHandle
= io_info
.devHandle
;
1402 io_request
->DevHandle
= io_info
.devHandle
;
1404 io_request
->RaidContext
.timeoutValue
=
1405 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1406 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1407 io_request
->DevHandle
= device_id
;
1408 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1409 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1410 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1415 * megasas_build_dcdb_fusion - Prepares IOs to devices
1416 * @instance: Adapter soft state
1417 * @scp: SCSI command
1418 * @cmd: Command to be prepared
1420 * Prepares the io_request frame for non-io cmds
1423 megasas_build_dcdb_fusion(struct megasas_instance
*instance
,
1424 struct scsi_cmnd
*scmd
,
1425 struct megasas_cmd_fusion
*cmd
)
1428 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
1430 struct MR_FW_RAID_MAP_ALL
*local_map_ptr
;
1431 struct fusion_context
*fusion
= instance
->ctrl_context
;
1433 io_request
= cmd
->io_request
;
1434 device_id
= MEGASAS_DEV_INDEX(instance
, scmd
);
1435 pd_index
= (scmd
->device
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
)
1437 local_map_ptr
= fusion
->ld_map
[(instance
->map_id
& 1)];
1439 /* Check if this is a system PD I/O */
1440 if (instance
->pd_list
[pd_index
].driveState
== MR_PD_STATE_SYSTEM
) {
1441 io_request
->Function
= 0;
1442 io_request
->DevHandle
=
1443 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
1444 io_request
->RaidContext
.timeoutValue
=
1445 local_map_ptr
->raidMap
.fpPdIoTimeoutSec
;
1446 io_request
->RaidContext
.regLockFlags
= 0;
1447 io_request
->RaidContext
.regLockRowLBA
= 0;
1448 io_request
->RaidContext
.regLockLength
= 0;
1449 io_request
->RaidContext
.RAIDFlags
=
1450 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
<<
1451 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
;
1452 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1453 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
1454 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1456 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
1457 io_request
->DevHandle
= device_id
;
1458 cmd
->request_desc
->SCSIIO
.RequestFlags
=
1459 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1460 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1462 io_request
->RaidContext
.VirtualDiskTgtId
= device_id
;
1463 io_request
->LUN
[1] = scmd
->device
->lun
;
1464 io_request
->DataLength
= scsi_bufflen(scmd
);
1468 * megasas_build_io_fusion - Prepares IOs to devices
1469 * @instance: Adapter soft state
1470 * @scp: SCSI command
1471 * @cmd: Command to be prepared
1473 * Invokes helper functions to prepare request frames
1474 * and sets flags appropriate for IO/Non-IO cmd
1477 megasas_build_io_fusion(struct megasas_instance
*instance
,
1478 struct scsi_cmnd
*scp
,
1479 struct megasas_cmd_fusion
*cmd
)
1481 u32 device_id
, sge_count
;
1482 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
= cmd
->io_request
;
1484 device_id
= MEGASAS_DEV_INDEX(instance
, scp
);
1486 /* Zero out some fields so they don't get reused */
1487 io_request
->LUN
[1] = 0;
1488 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
= 0;
1489 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0;
1490 io_request
->EEDPFlags
= 0;
1491 io_request
->Control
= 0;
1492 io_request
->EEDPBlockSize
= 0;
1493 io_request
->IoFlags
= 0;
1494 io_request
->RaidContext
.RAIDFlags
= 0;
1496 memcpy(io_request
->CDB
.CDB32
, scp
->cmnd
, scp
->cmd_len
);
1498 * Just the CDB length,rest of the Flags are zero
1499 * This will be modified for FP in build_ldio_fusion
1501 io_request
->IoFlags
= scp
->cmd_len
;
1503 if (megasas_is_ldio(scp
))
1504 megasas_build_ldio_fusion(instance
, scp
, cmd
);
1506 megasas_build_dcdb_fusion(instance
, scp
, cmd
);
1513 megasas_make_sgl_fusion(instance
, scp
,
1514 (struct MPI25_IEEE_SGE_CHAIN64
*)
1515 &io_request
->SGL
, cmd
);
1517 if (sge_count
> instance
->max_num_sge
) {
1518 printk(KERN_ERR
"megasas: Error. sge_count (0x%x) exceeds "
1519 "max (0x%x) allowed\n", sge_count
,
1520 instance
->max_num_sge
);
1524 io_request
->RaidContext
.numSGE
= sge_count
;
1526 io_request
->SGLFlags
= MPI2_SGE_FLAGS_64_BIT_ADDRESSING
;
1528 if (scp
->sc_data_direction
== PCI_DMA_TODEVICE
)
1529 io_request
->Control
|= MPI2_SCSIIO_CONTROL_WRITE
;
1530 else if (scp
->sc_data_direction
== PCI_DMA_FROMDEVICE
)
1531 io_request
->Control
|= MPI2_SCSIIO_CONTROL_READ
;
1533 io_request
->SGLOffset0
=
1534 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
) / 4;
1536 io_request
->SenseBufferLowAddress
= cmd
->sense_phys_addr
;
1537 io_request
->SenseBufferLength
= SCSI_SENSE_BUFFERSIZE
;
1540 scp
->SCp
.ptr
= (char *)cmd
;
1545 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1546 megasas_get_request_descriptor(struct megasas_instance
*instance
, u16 index
)
1549 struct fusion_context
*fusion
;
1551 if (index
>= instance
->max_fw_cmds
) {
1552 printk(KERN_ERR
"megasas: Invalid SMID (0x%x)request for "
1553 "descriptor\n", index
);
1556 fusion
= instance
->ctrl_context
;
1557 p
= fusion
->req_frames_desc
1558 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *index
;
1560 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)p
;
1564 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1565 * issuing non IOCTL cmd
1566 * @instance: Adapter soft state
1567 * @scmd: pointer to scsi cmd from OS
1570 megasas_build_and_issue_cmd_fusion(struct megasas_instance
*instance
,
1571 struct scsi_cmnd
*scmd
)
1573 struct megasas_cmd_fusion
*cmd
;
1574 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1576 struct fusion_context
*fusion
;
1578 fusion
= instance
->ctrl_context
;
1580 cmd
= megasas_get_cmd_fusion(instance
);
1582 return SCSI_MLQUEUE_HOST_BUSY
;
1586 req_desc
= megasas_get_request_descriptor(instance
, index
-1);
1590 req_desc
->Words
= 0;
1591 cmd
->request_desc
= req_desc
;
1592 cmd
->request_desc
->Words
= 0;
1594 if (megasas_build_io_fusion(instance
, scmd
, cmd
)) {
1595 megasas_return_cmd_fusion(instance
, cmd
);
1596 printk(KERN_ERR
"megasas: Error building command.\n");
1597 cmd
->request_desc
= NULL
;
1601 req_desc
= cmd
->request_desc
;
1602 req_desc
->SCSIIO
.SMID
= index
;
1604 if (cmd
->io_request
->ChainOffset
!= 0 &&
1605 cmd
->io_request
->ChainOffset
!= 0xF)
1606 printk(KERN_ERR
"megasas: The chain offset value is not "
1607 "correct : %x\n", cmd
->io_request
->ChainOffset
);
1610 * Issue the command to the FW
1612 atomic_inc(&instance
->fw_outstanding
);
1614 instance
->instancet
->fire_cmd(instance
,
1615 req_desc
->u
.low
, req_desc
->u
.high
,
1622 * complete_cmd_fusion - Completes command
1623 * @instance: Adapter soft state
1624 * Completes all commands that is in reply descriptor queue
1627 complete_cmd_fusion(struct megasas_instance
*instance
)
1629 union MPI2_REPLY_DESCRIPTORS_UNION
*desc
;
1630 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*reply_desc
;
1631 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
1632 struct fusion_context
*fusion
;
1633 struct megasas_cmd
*cmd_mfi
;
1634 struct megasas_cmd_fusion
*cmd_fusion
;
1635 u16 smid
, num_completed
;
1636 u8 reply_descript_type
, arm
;
1637 u32 status
, extStatus
, device_id
;
1638 union desc_value d_val
;
1639 struct LD_LOAD_BALANCE_INFO
*lbinfo
;
1641 fusion
= instance
->ctrl_context
;
1643 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
)
1646 desc
= fusion
->reply_frames_desc
;
1647 desc
+= fusion
->last_reply_idx
;
1649 reply_desc
= (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1651 d_val
.word
= desc
->Words
;
1653 reply_descript_type
= reply_desc
->ReplyFlags
&
1654 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1656 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1659 d_val
.word
= desc
->Words
;
1663 while ((d_val
.u
.low
!= UINT_MAX
) && (d_val
.u
.high
!= UINT_MAX
)) {
1664 smid
= reply_desc
->SMID
;
1666 cmd_fusion
= fusion
->cmd_list
[smid
- 1];
1669 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
1670 cmd_fusion
->io_request
;
1672 if (cmd_fusion
->scmd
)
1673 cmd_fusion
->scmd
->SCp
.ptr
= NULL
;
1675 status
= scsi_io_req
->RaidContext
.status
;
1676 extStatus
= scsi_io_req
->RaidContext
.exStatus
;
1678 switch (scsi_io_req
->Function
) {
1679 case MPI2_FUNCTION_SCSI_IO_REQUEST
: /*Fast Path IO.*/
1680 /* Update load balancing info */
1681 device_id
= MEGASAS_DEV_INDEX(instance
,
1683 lbinfo
= &fusion
->load_balance_info
[device_id
];
1684 if (cmd_fusion
->scmd
->SCp
.Status
&
1685 MEGASAS_LOAD_BALANCE_FLAG
) {
1686 arm
= lbinfo
->raid1DevHandle
[0] ==
1687 cmd_fusion
->io_request
->DevHandle
? 0 :
1689 atomic_dec(&lbinfo
->scsi_pending_cmds
[arm
]);
1690 cmd_fusion
->scmd
->SCp
.Status
&=
1691 ~MEGASAS_LOAD_BALANCE_FLAG
;
1693 if (reply_descript_type
==
1694 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS
) {
1695 if (megasas_dbg_lvl
== 5)
1696 printk(KERN_ERR
"\nmegasas: FAST Path "
1699 /* Fall thru and complete IO */
1700 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
: /* LD-IO Path */
1701 /* Map the FW Cmd Status */
1702 map_cmd_status(cmd_fusion
, status
, extStatus
);
1703 scsi_dma_unmap(cmd_fusion
->scmd
);
1704 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
1705 scsi_io_req
->RaidContext
.status
= 0;
1706 scsi_io_req
->RaidContext
.exStatus
= 0;
1707 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1708 atomic_dec(&instance
->fw_outstanding
);
1711 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
: /*MFI command */
1712 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
1713 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
1714 cmd_fusion
->flags
= 0;
1715 megasas_return_cmd_fusion(instance
, cmd_fusion
);
1720 fusion
->last_reply_idx
++;
1721 if (fusion
->last_reply_idx
>= fusion
->reply_q_depth
)
1722 fusion
->last_reply_idx
= 0;
1724 desc
->Words
= ULLONG_MAX
;
1727 /* Get the next reply descriptor */
1728 if (!fusion
->last_reply_idx
)
1729 desc
= fusion
->reply_frames_desc
;
1734 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
1736 d_val
.word
= desc
->Words
;
1738 reply_descript_type
= reply_desc
->ReplyFlags
&
1739 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
1741 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
1749 writel(fusion
->last_reply_idx
,
1750 &instance
->reg_set
->reply_post_host_index
);
1751 megasas_check_and_restore_queue_depth(instance
);
1756 * megasas_complete_cmd_dpc_fusion - Completes command
1757 * @instance: Adapter soft state
1759 * Tasklet to complete cmds
1762 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr
)
1764 struct megasas_instance
*instance
=
1765 (struct megasas_instance
*)instance_addr
;
1766 unsigned long flags
;
1768 /* If we have already declared adapter dead, donot complete cmds */
1769 spin_lock_irqsave(&instance
->hba_lock
, flags
);
1770 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
1771 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1774 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
1776 spin_lock_irqsave(&instance
->completion_lock
, flags
);
1777 complete_cmd_fusion(instance
);
1778 spin_unlock_irqrestore(&instance
->completion_lock
, flags
);
1782 * megasas_isr_fusion - isr entry point
1784 irqreturn_t
megasas_isr_fusion(int irq
, void *devp
)
1786 struct megasas_instance
*instance
= (struct megasas_instance
*)devp
;
1787 u32 mfiStatus
, fw_state
;
1789 if (!instance
->msi_flag
) {
1790 mfiStatus
= instance
->instancet
->clear_intr(instance
->reg_set
);
1795 /* If we are resetting, bail */
1796 if (test_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
))
1799 if (!complete_cmd_fusion(instance
)) {
1800 /* If we didn't complete any commands, check for FW fault */
1801 fw_state
= instance
->instancet
->read_fw_status_reg(
1802 instance
->reg_set
) & MFI_STATE_MASK
;
1803 if (fw_state
== MFI_STATE_FAULT
)
1804 schedule_work(&instance
->work_init
);
1811 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1812 * @instance: Adapter soft state
1813 * mfi_cmd: megasas_cmd pointer
1817 build_mpt_mfi_pass_thru(struct megasas_instance
*instance
,
1818 struct megasas_cmd
*mfi_cmd
)
1820 struct MPI25_IEEE_SGE_CHAIN64
*mpi25_ieee_chain
;
1821 struct MPI2_RAID_SCSI_IO_REQUEST
*io_req
;
1822 struct megasas_cmd_fusion
*cmd
;
1823 struct fusion_context
*fusion
;
1824 struct megasas_header
*frame_hdr
= &mfi_cmd
->frame
->hdr
;
1826 cmd
= megasas_get_cmd_fusion(instance
);
1830 /* Save the smid. To be used for returning the cmd */
1831 mfi_cmd
->context
.smid
= cmd
->index
;
1833 cmd
->sync_cmd_idx
= mfi_cmd
->index
;
1836 * For cmds where the flag is set, store the flag and check
1837 * on completion. For cmds with this flag, don't call
1838 * megasas_complete_cmd
1841 if (frame_hdr
->flags
& MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
)
1842 cmd
->flags
= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
;
1844 fusion
= instance
->ctrl_context
;
1845 io_req
= cmd
->io_request
;
1847 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
.IeeeChain
;
1849 io_req
->Function
= MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
;
1850 io_req
->SGLOffset0
= offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
,
1852 io_req
->ChainOffset
= fusion
->chain_offset_mfi_pthru
;
1854 mpi25_ieee_chain
->Address
= mfi_cmd
->frame_phys_addr
;
1856 mpi25_ieee_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
1857 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
;
1859 mpi25_ieee_chain
->Length
= MEGASAS_MAX_SZ_CHAIN_FRAME
;
1865 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1866 * @instance: Adapter soft state
1867 * @cmd: mfi cmd to build
1870 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
1871 build_mpt_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
1873 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1876 if (build_mpt_mfi_pass_thru(instance
, cmd
)) {
1877 printk(KERN_ERR
"Couldn't build MFI pass thru cmd\n");
1881 index
= cmd
->context
.smid
;
1883 req_desc
= megasas_get_request_descriptor(instance
, index
- 1);
1888 req_desc
->Words
= 0;
1889 req_desc
->SCSIIO
.RequestFlags
= (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
1890 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1892 req_desc
->SCSIIO
.SMID
= index
;
1898 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1899 * @instance: Adapter soft state
1900 * @cmd: mfi cmd pointer
1904 megasas_issue_dcmd_fusion(struct megasas_instance
*instance
,
1905 struct megasas_cmd
*cmd
)
1907 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
1908 union desc_value d_val
;
1910 req_desc
= build_mpt_cmd(instance
, cmd
);
1912 printk(KERN_ERR
"Couldn't issue MFI pass thru cmd\n");
1915 d_val
.word
= req_desc
->Words
;
1917 instance
->instancet
->fire_cmd(instance
, req_desc
->u
.low
,
1918 req_desc
->u
.high
, instance
->reg_set
);
1922 * megasas_release_fusion - Reverses the FW initialization
1923 * @intance: Adapter soft state
1926 megasas_release_fusion(struct megasas_instance
*instance
)
1928 megasas_free_cmds(instance
);
1929 megasas_free_cmds_fusion(instance
);
1931 iounmap(instance
->reg_set
);
1933 pci_release_selected_regions(instance
->pdev
, instance
->bar
);
1937 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1938 * @regs: MFI register set
1941 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem
*regs
)
1943 return readl(&(regs
)->outbound_scratch_pad
);
1947 * megasas_adp_reset_fusion - For controller reset
1948 * @regs: MFI register set
1951 megasas_adp_reset_fusion(struct megasas_instance
*instance
,
1952 struct megasas_register_set __iomem
*regs
)
1958 * megasas_check_reset_fusion - For controller reset check
1959 * @regs: MFI register set
1962 megasas_check_reset_fusion(struct megasas_instance
*instance
,
1963 struct megasas_register_set __iomem
*regs
)
1968 /* This function waits for outstanding commands on fusion to complete */
1969 int megasas_wait_for_outstanding_fusion(struct megasas_instance
*instance
)
1971 int i
, outstanding
, retval
= 0;
1972 u32 fw_state
, wait_time
= MEGASAS_RESET_WAIT_TIME
;
1974 for (i
= 0; i
< wait_time
; i
++) {
1975 /* Check if firmware is in fault state */
1976 fw_state
= instance
->instancet
->read_fw_status_reg(
1977 instance
->reg_set
) & MFI_STATE_MASK
;
1978 if (fw_state
== MFI_STATE_FAULT
) {
1979 printk(KERN_WARNING
"megasas: Found FW in FAULT state,"
1980 " will reset adapter.\n");
1985 outstanding
= atomic_read(&instance
->fw_outstanding
);
1989 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
1990 printk(KERN_NOTICE
"megasas: [%2d]waiting for %d "
1991 "commands to complete\n", i
, outstanding
);
1992 megasas_complete_cmd_dpc_fusion(
1993 (unsigned long)instance
);
1998 if (atomic_read(&instance
->fw_outstanding
)) {
1999 printk("megaraid_sas: pending commands remain after waiting, "
2000 "will reset adapter.\n");
2007 void megasas_reset_reply_desc(struct megasas_instance
*instance
)
2010 struct fusion_context
*fusion
;
2011 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
2013 fusion
= instance
->ctrl_context
;
2014 fusion
->last_reply_idx
= 0;
2015 reply_desc
= fusion
->reply_frames_desc
;
2016 for (i
= 0 ; i
< fusion
->reply_q_depth
; i
++, reply_desc
++)
2017 reply_desc
->Words
= ULLONG_MAX
;
2020 /* Core fusion reset function */
2021 int megasas_reset_fusion(struct Scsi_Host
*shost
)
2023 int retval
= SUCCESS
, i
, j
, retry
= 0;
2024 struct megasas_instance
*instance
;
2025 struct megasas_cmd_fusion
*cmd_fusion
;
2026 struct fusion_context
*fusion
;
2027 struct megasas_cmd
*cmd_mfi
;
2028 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
2029 u32 host_diag
, abs_state
;
2031 instance
= (struct megasas_instance
*)shost
->hostdata
;
2032 fusion
= instance
->ctrl_context
;
2034 mutex_lock(&instance
->reset_mutex
);
2035 set_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2036 instance
->adprecovery
= MEGASAS_ADPRESET_SM_INFAULT
;
2037 instance
->instancet
->disable_intr(instance
->reg_set
);
2040 if (instance
->adprecovery
== MEGASAS_HW_CRITICAL_ERROR
) {
2041 printk(KERN_WARNING
"megaraid_sas: Hardware critical error, "
2042 "returning FAILED.\n");
2047 /* First try waiting for commands to complete */
2048 if (megasas_wait_for_outstanding_fusion(instance
)) {
2049 printk(KERN_WARNING
"megaraid_sas: resetting fusion "
2051 /* Now return commands back to the OS */
2052 for (i
= 0 ; i
< instance
->max_fw_cmds
; i
++) {
2053 cmd_fusion
= fusion
->cmd_list
[i
];
2054 if (cmd_fusion
->scmd
) {
2055 scsi_dma_unmap(cmd_fusion
->scmd
);
2056 cmd_fusion
->scmd
->result
= (DID_RESET
<< 16);
2057 cmd_fusion
->scmd
->scsi_done(cmd_fusion
->scmd
);
2058 megasas_return_cmd_fusion(instance
, cmd_fusion
);
2059 atomic_dec(&instance
->fw_outstanding
);
2063 if (instance
->disableOnlineCtrlReset
== 1) {
2064 /* Reset not supported, kill adapter */
2065 printk(KERN_WARNING
"megaraid_sas: Reset not supported"
2066 ", killing adapter.\n");
2067 megaraid_sas_kill_hba(instance
);
2068 instance
->adprecovery
= MEGASAS_HW_CRITICAL_ERROR
;
2073 /* Now try to reset the chip */
2074 for (i
= 0; i
< MEGASAS_FUSION_MAX_RESET_TRIES
; i
++) {
2075 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE
,
2076 &instance
->reg_set
->fusion_seq_offset
);
2077 writel(MPI2_WRSEQ_1ST_KEY_VALUE
,
2078 &instance
->reg_set
->fusion_seq_offset
);
2079 writel(MPI2_WRSEQ_2ND_KEY_VALUE
,
2080 &instance
->reg_set
->fusion_seq_offset
);
2081 writel(MPI2_WRSEQ_3RD_KEY_VALUE
,
2082 &instance
->reg_set
->fusion_seq_offset
);
2083 writel(MPI2_WRSEQ_4TH_KEY_VALUE
,
2084 &instance
->reg_set
->fusion_seq_offset
);
2085 writel(MPI2_WRSEQ_5TH_KEY_VALUE
,
2086 &instance
->reg_set
->fusion_seq_offset
);
2087 writel(MPI2_WRSEQ_6TH_KEY_VALUE
,
2088 &instance
->reg_set
->fusion_seq_offset
);
2090 /* Check that the diag write enable (DRWE) bit is on */
2091 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2092 while (!(host_diag
& HOST_DIAG_WRITE_ENABLE
)) {
2095 readl(&instance
->reg_set
->fusion_host_diag
);
2096 if (retry
++ == 100) {
2097 printk(KERN_WARNING
"megaraid_sas: "
2098 "Host diag unlock failed!\n");
2102 if (!(host_diag
& HOST_DIAG_WRITE_ENABLE
))
2105 /* Send chip reset command */
2106 writel(host_diag
| HOST_DIAG_RESET_ADAPTER
,
2107 &instance
->reg_set
->fusion_host_diag
);
2110 /* Make sure reset adapter bit is cleared */
2111 host_diag
= readl(&instance
->reg_set
->fusion_host_diag
);
2113 while (host_diag
& HOST_DIAG_RESET_ADAPTER
) {
2116 readl(&instance
->reg_set
->fusion_host_diag
);
2117 if (retry
++ == 1000) {
2118 printk(KERN_WARNING
"megaraid_sas: "
2119 "Diag reset adapter never "
2124 if (host_diag
& HOST_DIAG_RESET_ADAPTER
)
2128 instance
->instancet
->read_fw_status_reg(
2132 while ((abs_state
<= MFI_STATE_FW_INIT
) &&
2136 instance
->instancet
->read_fw_status_reg(
2139 if (abs_state
<= MFI_STATE_FW_INIT
) {
2140 printk(KERN_WARNING
"megaraid_sas: firmware "
2141 "state < MFI_STATE_FW_INIT, state = "
2142 "0x%x\n", abs_state
);
2146 /* Wait for FW to become ready */
2147 if (megasas_transition_to_ready(instance
)) {
2148 printk(KERN_WARNING
"megaraid_sas: Failed to "
2149 "transition controller to ready.\n");
2153 megasas_reset_reply_desc(instance
);
2154 if (megasas_ioc_init_fusion(instance
)) {
2155 printk(KERN_WARNING
"megaraid_sas: "
2156 "megasas_ioc_init_fusion() failed!\n");
2160 instance
->instancet
->enable_intr(instance
->reg_set
);
2161 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2163 /* Re-fire management commands */
2164 for (j
= 0 ; j
< instance
->max_fw_cmds
; j
++) {
2165 cmd_fusion
= fusion
->cmd_list
[j
];
2166 if (cmd_fusion
->sync_cmd_idx
!=
2170 cmd_list
[cmd_fusion
->sync_cmd_idx
];
2171 if (cmd_mfi
->frame
->dcmd
.opcode
==
2172 MR_DCMD_LD_MAP_GET_INFO
) {
2173 megasas_return_cmd(instance
,
2175 megasas_return_cmd_fusion(
2176 instance
, cmd_fusion
);
2179 megasas_get_request_descriptor(
2181 cmd_mfi
->context
.smid
2188 instance
->instancet
->
2201 /* Reset load balance info */
2202 memset(fusion
->load_balance_info
, 0,
2203 sizeof(struct LD_LOAD_BALANCE_INFO
)
2204 *MAX_LOGICAL_DRIVES
);
2206 if (!megasas_get_map_info(instance
))
2207 megasas_sync_map_info(instance
);
2209 /* Adapter reset completed successfully */
2210 printk(KERN_WARNING
"megaraid_sas: Reset "
2215 /* Reset failed, kill the adapter */
2216 printk(KERN_WARNING
"megaraid_sas: Reset failed, killing "
2218 megaraid_sas_kill_hba(instance
);
2221 instance
->instancet
->enable_intr(instance
->reg_set
);
2222 instance
->adprecovery
= MEGASAS_HBA_OPERATIONAL
;
2225 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
2226 mutex_unlock(&instance
->reset_mutex
);
2230 /* Fusion OCR work queue */
2231 void megasas_fusion_ocr_wq(struct work_struct
*work
)
2233 struct megasas_instance
*instance
=
2234 container_of(work
, struct megasas_instance
, work_init
);
2236 megasas_reset_fusion(instance
->host
);
2239 struct megasas_instance_template megasas_instance_template_fusion
= {
2240 .fire_cmd
= megasas_fire_cmd_fusion
,
2241 .enable_intr
= megasas_enable_intr_fusion
,
2242 .disable_intr
= megasas_disable_intr_fusion
,
2243 .clear_intr
= megasas_clear_intr_fusion
,
2244 .read_fw_status_reg
= megasas_read_fw_status_reg_fusion
,
2245 .adp_reset
= megasas_adp_reset_fusion
,
2246 .check_reset
= megasas_check_reset_fusion
,
2247 .service_isr
= megasas_isr_fusion
,
2248 .tasklet
= megasas_complete_cmd_dpc_fusion
,
2249 .init_adapter
= megasas_init_adapter_fusion
,
2250 .build_and_issue_cmd
= megasas_build_and_issue_cmd_fusion
,
2251 .issue_dcmd
= megasas_issue_dcmd_fusion
,