]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - drivers/scsi/megaraid/megaraid_sas_fusion.c
UBUNTU: Ubuntu-5.3.0-29.31
[mirror_ubuntu-eoan-kernel.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
CommitLineData
1ccea77e 1// SPDX-License-Identifier: GPL-2.0-or-later
9c915a8c
AR
2/*
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
e399065b 5 * Copyright (c) 2009-2013 LSI Corporation
365597cf
S
6 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
9c915a8c 8 *
9c915a8c
AR
9 * FILE: megaraid_sas_fusion.c
10 *
365597cf 11 * Authors: Broadcom Inc.
9c915a8c 12 * Sumant Patro
e399065b 13 * Adam Radford
365597cf
S
14 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
9c915a8c 16 *
365597cf 17 * Send feedback to: megaraidlinux.pdl@broadcom.com
9c915a8c
AR
18 */
19
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/pci.h>
23#include <linux/list.h>
24#include <linux/moduleparam.h>
25#include <linux/module.h>
26#include <linux/spinlock.h>
27#include <linux/interrupt.h>
28#include <linux/delay.h>
9c915a8c
AR
29#include <linux/uio.h>
30#include <linux/uaccess.h>
31#include <linux/fs.h>
32#include <linux/compat.h>
33#include <linux/blkdev.h>
34#include <linux/mutex.h>
35#include <linux/poll.h>
5fc499b6 36#include <linux/vmalloc.h>
3f6194af 37#include <linux/workqueue.h>
62a04f81 38#include <linux/irq_poll.h>
9c915a8c
AR
39
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
90dc9d98 44#include <scsi/scsi_dbg.h>
5765c5b8 45#include <linux/dmi.h>
9c915a8c
AR
46
47#include "megaraid_sas_fusion.h"
48#include "megaraid_sas.h"
49
4026e9aa 50
9c915a8c
AR
51extern void megasas_free_cmds(struct megasas_instance *instance);
52extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
53 *instance);
54extern void
55megasas_complete_cmd(struct megasas_instance *instance,
56 struct megasas_cmd *cmd, u8 alt_status);
9c915a8c 57int
229fe47c
AR
58wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
59 int seconds);
9c915a8c
AR
60
61void
62megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
63int megasas_alloc_cmds(struct megasas_instance *instance);
64int
de516379 65megasas_clear_intr_fusion(struct megasas_instance *instance);
9c915a8c
AR
66int
67megasas_issue_polled(struct megasas_instance *instance,
68 struct megasas_cmd *cmd);
53ef2bbd
AR
69void
70megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
71
058a8fac 72int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
9c915a8c
AR
73void megaraid_sas_kill_hba(struct megasas_instance *instance);
74
75extern u32 megasas_dbg_lvl;
229fe47c
AR
76int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
77 int initial);
c251a7be 78void megasas_start_timer(struct megasas_instance *instance);
229fe47c 79extern struct megasas_mgmt_info megasas_mgmt_info;
e3d178ca 80extern unsigned int resetwaittime;
308ec459 81extern unsigned int dual_qdepth_disable;
179ac142
SS
82static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
83static void megasas_free_reply_fusion(struct megasas_instance *instance);
e97e673c
S
84static inline
85void megasas_configure_queue_sizes(struct megasas_instance *instance);
3f6194af 86static void megasas_fusion_crash_dump(struct megasas_instance *instance);
272652fc
S
87extern u32 megasas_readl(struct megasas_instance *instance,
88 const volatile void __iomem *addr);
9c915a8c 89
78409d4b
S
90/**
91 * megasas_adp_reset_wait_for_ready - initiate chip reset and wait for
92 * controller to come to ready state
93 * @instance - adapter's soft state
94 * @do_adp_reset - If true, do a chip reset
95 * @ocr_context - If called from OCR context this will
96 * be set to 1, else 0
97 *
98 * This function initates a chip reset followed by a wait for controller to
99 * transition to ready state.
100 * During this, driver will block all access to PCI config space from userspace
101 */
102int
103megasas_adp_reset_wait_for_ready(struct megasas_instance *instance,
104 bool do_adp_reset,
105 int ocr_context)
106{
107 int ret = FAILED;
108
109 /*
110 * Block access to PCI config space from userspace
111 * when diag reset is initiated from driver
112 */
96c9603c 113 if (megasas_dbg_lvl & OCR_DEBUG)
78409d4b
S
114 dev_info(&instance->pdev->dev,
115 "Block access to PCI config space %s %d\n",
116 __func__, __LINE__);
117
118 pci_cfg_access_lock(instance->pdev);
119
120 if (do_adp_reset) {
121 if (instance->instancet->adp_reset
122 (instance, instance->reg_set))
123 goto out;
124 }
125
126 /* Wait for FW to become ready */
127 if (megasas_transition_to_ready(instance, ocr_context)) {
128 dev_warn(&instance->pdev->dev,
129 "Failed to transition controller to ready for scsi%d.\n",
130 instance->host->host_no);
131 goto out;
132 }
133
134 ret = SUCCESS;
135out:
96c9603c 136 if (megasas_dbg_lvl & OCR_DEBUG)
78409d4b
S
137 dev_info(&instance->pdev->dev,
138 "Unlock access to PCI config space %s %d\n",
139 __func__, __LINE__);
140
141 pci_cfg_access_unlock(instance->pdev);
142
143 return ret;
144}
145
107a60dd
S
146/**
147 * megasas_check_same_4gb_region - check if allocation
148 * crosses same 4GB boundary or not
149 * @instance - adapter's soft instance
150 * start_addr - start address of DMA allocation
151 * size - size of allocation in bytes
152 * return - true : allocation does not cross same
153 * 4GB boundary
154 * false: allocation crosses same
155 * 4GB boundary
156 */
157static inline bool megasas_check_same_4gb_region
158 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
159{
160 dma_addr_t end_addr;
161
162 end_addr = start_addr + size;
fc62b3fc 163
107a60dd
S
164 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
165 dev_err(&instance->pdev->dev,
166 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
167 (unsigned long long)start_addr,
168 (unsigned long long)end_addr);
169 return false;
170 }
171
172 return true;
173}
fc62b3fc 174
9c915a8c
AR
175/**
176 * megasas_enable_intr_fusion - Enables interrupts
177 * @regs: MFI register set
178 */
179void
d46a3ad6 180megasas_enable_intr_fusion(struct megasas_instance *instance)
9c915a8c 181{
d46a3ad6
SS
182 struct megasas_register_set __iomem *regs;
183 regs = instance->reg_set;
c2ced171
SS
184
185 instance->mask_interrupts = 0;
6497b247
AR
186 /* For Thunderbolt/Invader also clear intr on enable */
187 writel(~0, &regs->outbound_intr_status);
188 readl(&regs->outbound_intr_status);
189
9c915a8c
AR
190 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
191
192 /* Dummy readl to force pci flush */
223d5818
S
193 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
194 __func__, readl(&regs->outbound_intr_mask));
9c915a8c
AR
195}
196
197/**
198 * megasas_disable_intr_fusion - Disables interrupt
199 * @regs: MFI register set
200 */
201void
d46a3ad6 202megasas_disable_intr_fusion(struct megasas_instance *instance)
9c915a8c
AR
203{
204 u32 mask = 0xFFFFFFFF;
d46a3ad6
SS
205 struct megasas_register_set __iomem *regs;
206 regs = instance->reg_set;
207 instance->mask_interrupts = 1;
9c915a8c
AR
208
209 writel(mask, &regs->outbound_intr_mask);
210 /* Dummy readl to force pci flush */
223d5818
S
211 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
212 __func__, readl(&regs->outbound_intr_mask));
9c915a8c
AR
213}
214
215int
de516379 216megasas_clear_intr_fusion(struct megasas_instance *instance)
9c915a8c
AR
217{
218 u32 status;
de516379
S
219 struct megasas_register_set __iomem *regs;
220 regs = instance->reg_set;
9c915a8c
AR
221 /*
222 * Check if it is our interrupt
223 */
c65bfc81
S
224 status = megasas_readl(instance,
225 &regs->outbound_intr_status);
9c915a8c
AR
226
227 if (status & 1) {
228 writel(status, &regs->outbound_intr_status);
229 readl(&regs->outbound_intr_status);
230 return 1;
231 }
232 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
233 return 0;
234
9c915a8c
AR
235 return 1;
236}
237
238/**
239 * megasas_get_cmd_fusion - Get a command from the free pool
240 * @instance: Adapter soft state
241 *
4026e9aa 242 * Returns a blk_tag indexed mpt frame
9c915a8c 243 */
4026e9aa
SS
244inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
245 *instance, u32 blk_tag)
9c915a8c 246{
4026e9aa 247 struct fusion_context *fusion;
9c915a8c 248
4026e9aa
SS
249 fusion = instance->ctrl_context;
250 return fusion->cmd_list[blk_tag];
9c915a8c
AR
251}
252
253/**
254 * megasas_return_cmd_fusion - Return a cmd to free command pool
255 * @instance: Adapter soft state
256 * @cmd: Command packet to be returned to free command pool
257 */
90dc9d98
SS
258inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
259 struct megasas_cmd_fusion *cmd)
9c915a8c 260{
9c915a8c 261 cmd->scmd = NULL;
8bf7c65d
S
262 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
263 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
264 cmd->cmd_completed = false;
90dc9d98
SS
265}
266
267/**
5885571d
CP
268 * megasas_write_64bit_req_desc - PCI writes 64bit request descriptor
269 * @instance: Adapter soft state
270 * @req_desc: 64bit Request descriptor
90dc9d98 271 */
01d7f03b 272static void
5885571d 273megasas_write_64bit_req_desc(struct megasas_instance *instance,
45b8a35e 274 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
90dc9d98 275{
45b8a35e 276#if defined(writeq) && defined(CONFIG_64BIT)
9ff97fa8
S
277 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
278 le32_to_cpu(req_desc->u.low));
9ff97fa8 279 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
45b8a35e 280#else
9ff97fa8
S
281 unsigned long flags;
282 spin_lock_irqsave(&instance->hba_lock, flags);
283 writel(le32_to_cpu(req_desc->u.low),
284 &instance->reg_set->inbound_low_queue_port);
285 writel(le32_to_cpu(req_desc->u.high),
286 &instance->reg_set->inbound_high_queue_port);
9ff97fa8 287 spin_unlock_irqrestore(&instance->hba_lock, flags);
01d7f03b 288#endif
9c915a8c
AR
289}
290
5885571d
CP
291/**
292 * megasas_fire_cmd_fusion - Sends command to the FW
293 * @instance: Adapter soft state
294 * @req_desc: 32bit or 64bit Request descriptor
295 *
296 * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
297 * Prior to AERO_SERIES support 64 bit Descriptor.
298 */
299static void
300megasas_fire_cmd_fusion(struct megasas_instance *instance,
301 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
302{
303 if (instance->atomic_desc_support)
304 writel(le32_to_cpu(req_desc->u.low),
305 &instance->reg_set->inbound_single_queue_port);
306 else
307 megasas_write_64bit_req_desc(instance, req_desc);
308}
309
308ec459
SS
310/**
311 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
312 * @instance: Adapter soft state
313 * fw_boot_context: Whether this function called during probe or after OCR
314 *
315 * This function is only for fusion controllers.
316 * Update host can queue, if firmware downgrade max supported firmware commands.
317 * Firmware upgrade case will be skiped because underlying firmware has
318 * more resource than exposed to the OS.
319 *
320 */
321static void
322megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
323{
324 u16 cur_max_fw_cmds = 0;
325 u16 ldio_threshold = 0;
326 struct megasas_register_set __iomem *reg_set;
327
328 reg_set = instance->reg_set;
329
81b76452 330 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
f369a315 331 if (instance->adapter_type < VENTURA_SERIES)
9581ebeb 332 cur_max_fw_cmds =
272652fc
S
333 megasas_readl(instance,
334 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
308ec459
SS
335
336 if (dual_qdepth_disable || !cur_max_fw_cmds)
de516379 337 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
308ec459
SS
338 else
339 ldio_threshold =
de516379 340 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
308ec459
SS
341
342 dev_info(&instance->pdev->dev,
b5c5d0ad 343 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
e97e673c 344 cur_max_fw_cmds, ldio_threshold);
308ec459
SS
345
346 if (fw_boot_context == OCR_CONTEXT) {
347 cur_max_fw_cmds = cur_max_fw_cmds - 1;
013aec66 348 if (cur_max_fw_cmds < instance->max_fw_cmds) {
308ec459
SS
349 instance->cur_can_queue =
350 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
351 MEGASAS_FUSION_IOCTL_CMDS);
352 instance->host->can_queue = instance->cur_can_queue;
353 instance->ldio_threshold = ldio_threshold;
354 }
355 } else {
356 instance->max_fw_cmds = cur_max_fw_cmds;
357 instance->ldio_threshold = ldio_threshold;
358
c3e385a1
SS
359 if (reset_devices)
360 instance->max_fw_cmds = min(instance->max_fw_cmds,
361 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
308ec459
SS
362 /*
363 * Reduce the max supported cmds by 1. This is to ensure that the
364 * reply_q_sz (1 more than the max cmd that driver may send)
365 * does not exceed max cmds that the FW can support
366 */
367 instance->max_fw_cmds = instance->max_fw_cmds-1;
308ec459
SS
368 }
369}
9c915a8c 370/**
179ac142
SS
371 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
372 * @instance: Adapter soft state
9c915a8c 373 */
179ac142
SS
374void
375megasas_free_cmds_fusion(struct megasas_instance *instance)
9c915a8c
AR
376{
377 int i;
378 struct fusion_context *fusion = instance->ctrl_context;
9c915a8c
AR
379 struct megasas_cmd_fusion *cmd;
380
107a60dd
S
381 if (fusion->sense)
382 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
383 fusion->sense_phys_addr);
384
385 /* SG */
386 if (fusion->cmd_list) {
387 for (i = 0; i < instance->max_mpt_cmds; i++) {
388 cmd = fusion->cmd_list[i];
389 if (cmd) {
390 if (cmd->sg_frame)
391 dma_pool_free(fusion->sg_dma_pool,
392 cmd->sg_frame,
393 cmd->sg_frame_phys_addr);
394 }
395 kfree(cmd);
179ac142 396 }
107a60dd 397 kfree(fusion->cmd_list);
9c915a8c
AR
398 }
399
179ac142 400 if (fusion->sg_dma_pool) {
fc69d86d 401 dma_pool_destroy(fusion->sg_dma_pool);
179ac142
SS
402 fusion->sg_dma_pool = NULL;
403 }
404 if (fusion->sense_dma_pool) {
fc69d86d 405 dma_pool_destroy(fusion->sense_dma_pool);
179ac142
SS
406 fusion->sense_dma_pool = NULL;
407 }
9c915a8c 408
9c915a8c 409
179ac142
SS
410 /* Reply Frame, Desc*/
411 if (instance->is_rdpq)
412 megasas_free_rdpq_fusion(instance);
413 else
414 megasas_free_reply_fusion(instance);
9c915a8c 415
179ac142 416 /* Request Frame, Desc*/
9c915a8c 417 if (fusion->req_frames_desc)
179ac142
SS
418 dma_free_coherent(&instance->pdev->dev,
419 fusion->request_alloc_sz, fusion->req_frames_desc,
420 fusion->req_frames_desc_phys);
421 if (fusion->io_request_frames)
fc69d86d 422 dma_pool_free(fusion->io_request_frames_pool,
179ac142
SS
423 fusion->io_request_frames,
424 fusion->io_request_frames_phys);
425 if (fusion->io_request_frames_pool) {
fc69d86d 426 dma_pool_destroy(fusion->io_request_frames_pool);
179ac142 427 fusion->io_request_frames_pool = NULL;
9c915a8c 428 }
9c915a8c
AR
429}
430
431/**
179ac142 432 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
9c915a8c
AR
433 * @instance: Adapter soft state
434 *
435 */
179ac142 436static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
9c915a8c
AR
437{
438 int i;
50b7f5a2 439 u16 max_cmd;
9c915a8c
AR
440 struct fusion_context *fusion;
441 struct megasas_cmd_fusion *cmd;
107a60dd
S
442 int sense_sz;
443 u32 offset;
9c915a8c
AR
444
445 fusion = instance->ctrl_context;
446 max_cmd = instance->max_fw_cmds;
107a60dd 447 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
9c915a8c 448
179ac142 449 fusion->sg_dma_pool =
fc69d86d 450 dma_pool_create("mr_sg", &instance->pdev->dev,
15dd0381
S
451 instance->max_chain_frame_sz,
452 MR_DEFAULT_NVME_PAGE_SIZE, 0);
179ac142
SS
453 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
454 fusion->sense_dma_pool =
fc69d86d 455 dma_pool_create("mr_sense", &instance->pdev->dev,
107a60dd 456 sense_sz, 64, 0);
9c915a8c 457
179ac142
SS
458 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
459 dev_err(&instance->pdev->dev,
460 "Failed from %s %d\n", __func__, __LINE__);
9c915a8c
AR
461 return -ENOMEM;
462 }
463
107a60dd
S
464 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
465 GFP_KERNEL, &fusion->sense_phys_addr);
466 if (!fusion->sense) {
467 dev_err(&instance->pdev->dev,
468 "failed from %s %d\n", __func__, __LINE__);
469 return -ENOMEM;
470 }
471
472 /* sense buffer, request frame and reply desc pool requires to be in
473 * same 4 gb region. Below function will check this.
474 * In case of failure, new pci pool will be created with updated
475 * alignment.
476 * Older allocation and pool will be destroyed.
477 * Alignment will be used such a way that next allocation if success,
478 * will always meet same 4gb region requirement.
479 * Actual requirement is not alignment, but we need start and end of
480 * DMA address must have same upper 32 bit address.
481 */
482
483 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
484 sense_sz)) {
485 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
486 fusion->sense_phys_addr);
487 fusion->sense = NULL;
488 dma_pool_destroy(fusion->sense_dma_pool);
489
490 fusion->sense_dma_pool =
491 dma_pool_create("mr_sense_align", &instance->pdev->dev,
492 sense_sz, roundup_pow_of_two(sense_sz),
493 0);
494 if (!fusion->sense_dma_pool) {
495 dev_err(&instance->pdev->dev,
496 "Failed from %s %d\n", __func__, __LINE__);
497 return -ENOMEM;
498 }
499 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
500 GFP_KERNEL,
501 &fusion->sense_phys_addr);
502 if (!fusion->sense) {
503 dev_err(&instance->pdev->dev,
504 "failed from %s %d\n", __func__, __LINE__);
505 return -ENOMEM;
506 }
507 }
508
9c915a8c
AR
509 /*
510 * Allocate and attach a frame to each of the commands in cmd_list
511 */
512 for (i = 0; i < max_cmd; i++) {
9c915a8c 513 cmd = fusion->cmd_list[i];
fc69d86d 514 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
179ac142 515 GFP_KERNEL, &cmd->sg_frame_phys_addr);
9c915a8c 516
107a60dd
S
517 offset = SCSI_SENSE_BUFFERSIZE * i;
518 cmd->sense = (u8 *)fusion->sense + offset;
519 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
520
521 if (!cmd->sg_frame) {
179ac142
SS
522 dev_err(&instance->pdev->dev,
523 "Failed from %s %d\n", __func__, __LINE__);
9c915a8c
AR
524 return -ENOMEM;
525 }
526 }
69c337c0
SC
527
528 /* create sense buffer for the raid 1/10 fp */
529 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
530 cmd = fusion->cmd_list[i];
107a60dd
S
531 offset = SCSI_SENSE_BUFFERSIZE * i;
532 cmd->sense = (u8 *)fusion->sense + offset;
533 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
534
69c337c0
SC
535 }
536
9c915a8c
AR
537 return 0;
538}
539
e45ab43b 540static int
179ac142 541megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
9c915a8c 542{
70c54e21 543 u32 max_mpt_cmd, i, j;
9c915a8c 544 struct fusion_context *fusion;
9c915a8c
AR
545
546 fusion = instance->ctrl_context;
547
69c337c0 548 max_mpt_cmd = instance->max_mpt_cmds;
9c915a8c 549
179ac142
SS
550 /*
551 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
552 * Allocate the dynamic array first and then allocate individual
553 * commands.
554 */
69c337c0 555 fusion->cmd_list =
6396bb22 556 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
69c337c0 557 GFP_KERNEL);
179ac142
SS
558 if (!fusion->cmd_list) {
559 dev_err(&instance->pdev->dev,
560 "Failed from %s %d\n", __func__, __LINE__);
561 return -ENOMEM;
562 }
563
69c337c0 564 for (i = 0; i < max_mpt_cmd; i++) {
179ac142
SS
565 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
566 GFP_KERNEL);
567 if (!fusion->cmd_list[i]) {
70c54e21
SW
568 for (j = 0; j < i; j++)
569 kfree(fusion->cmd_list[j]);
570 kfree(fusion->cmd_list);
179ac142
SS
571 dev_err(&instance->pdev->dev,
572 "Failed from %s %d\n", __func__, __LINE__);
573 return -ENOMEM;
574 }
575 }
70c54e21 576
179ac142
SS
577 return 0;
578}
e45ab43b
Y
579
580static int
179ac142
SS
581megasas_alloc_request_fusion(struct megasas_instance *instance)
582{
583 struct fusion_context *fusion;
584
585 fusion = instance->ctrl_context;
586
e97e673c 587retry_alloc:
179ac142 588 fusion->io_request_frames_pool =
fc69d86d 589 dma_pool_create("mr_ioreq", &instance->pdev->dev,
179ac142
SS
590 fusion->io_frames_alloc_sz, 16, 0);
591
592 if (!fusion->io_request_frames_pool) {
593 dev_err(&instance->pdev->dev,
594 "Failed from %s %d\n", __func__, __LINE__);
595 return -ENOMEM;
596 }
597
598 fusion->io_request_frames =
fc69d86d 599 dma_pool_alloc(fusion->io_request_frames_pool,
179ac142
SS
600 GFP_KERNEL, &fusion->io_request_frames_phys);
601 if (!fusion->io_request_frames) {
e97e673c
S
602 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
603 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
604 dma_pool_destroy(fusion->io_request_frames_pool);
605 megasas_configure_queue_sizes(instance);
606 goto retry_alloc;
607 } else {
608 dev_err(&instance->pdev->dev,
609 "Failed from %s %d\n", __func__, __LINE__);
610 return -ENOMEM;
611 }
612 }
613
107a60dd
S
614 if (!megasas_check_same_4gb_region(instance,
615 fusion->io_request_frames_phys,
616 fusion->io_frames_alloc_sz)) {
617 dma_pool_free(fusion->io_request_frames_pool,
618 fusion->io_request_frames,
619 fusion->io_request_frames_phys);
620 fusion->io_request_frames = NULL;
621 dma_pool_destroy(fusion->io_request_frames_pool);
622
623 fusion->io_request_frames_pool =
624 dma_pool_create("mr_ioreq_align",
625 &instance->pdev->dev,
626 fusion->io_frames_alloc_sz,
627 roundup_pow_of_two(fusion->io_frames_alloc_sz),
628 0);
629
630 if (!fusion->io_request_frames_pool) {
631 dev_err(&instance->pdev->dev,
632 "Failed from %s %d\n", __func__, __LINE__);
633 return -ENOMEM;
634 }
635
636 fusion->io_request_frames =
637 dma_pool_alloc(fusion->io_request_frames_pool,
638 GFP_KERNEL,
639 &fusion->io_request_frames_phys);
640
641 if (!fusion->io_request_frames) {
642 dev_err(&instance->pdev->dev,
643 "Failed from %s %d\n", __func__, __LINE__);
644 return -ENOMEM;
645 }
646 }
647
e97e673c
S
648 fusion->req_frames_desc =
649 dma_alloc_coherent(&instance->pdev->dev,
650 fusion->request_alloc_sz,
651 &fusion->req_frames_desc_phys, GFP_KERNEL);
652 if (!fusion->req_frames_desc) {
179ac142
SS
653 dev_err(&instance->pdev->dev,
654 "Failed from %s %d\n", __func__, __LINE__);
655 return -ENOMEM;
656 }
e97e673c 657
179ac142
SS
658 return 0;
659}
660
e45ab43b 661static int
179ac142
SS
662megasas_alloc_reply_fusion(struct megasas_instance *instance)
663{
664 int i, count;
665 struct fusion_context *fusion;
666 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
667 fusion = instance->ctrl_context;
668
c8e858fe 669 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
9c915a8c 670 fusion->reply_frames_desc_pool =
fc69d86d 671 dma_pool_create("mr_reply", &instance->pdev->dev,
c8e858fe 672 fusion->reply_alloc_sz * count, 16, 0);
9c915a8c
AR
673
674 if (!fusion->reply_frames_desc_pool) {
179ac142
SS
675 dev_err(&instance->pdev->dev,
676 "Failed from %s %d\n", __func__, __LINE__);
677 return -ENOMEM;
9c915a8c
AR
678 }
679
179ac142 680 fusion->reply_frames_desc[0] =
fc69d86d 681 dma_pool_alloc(fusion->reply_frames_desc_pool,
179ac142
SS
682 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
683 if (!fusion->reply_frames_desc[0]) {
684 dev_err(&instance->pdev->dev,
685 "Failed from %s %d\n", __func__, __LINE__);
686 return -ENOMEM;
9c915a8c 687 }
107a60dd
S
688
689 if (!megasas_check_same_4gb_region(instance,
690 fusion->reply_frames_desc_phys[0],
691 (fusion->reply_alloc_sz * count))) {
692 dma_pool_free(fusion->reply_frames_desc_pool,
693 fusion->reply_frames_desc[0],
694 fusion->reply_frames_desc_phys[0]);
695 fusion->reply_frames_desc[0] = NULL;
696 dma_pool_destroy(fusion->reply_frames_desc_pool);
697
698 fusion->reply_frames_desc_pool =
699 dma_pool_create("mr_reply_align",
700 &instance->pdev->dev,
701 fusion->reply_alloc_sz * count,
702 roundup_pow_of_two(fusion->reply_alloc_sz * count),
703 0);
704
705 if (!fusion->reply_frames_desc_pool) {
706 dev_err(&instance->pdev->dev,
707 "Failed from %s %d\n", __func__, __LINE__);
708 return -ENOMEM;
709 }
710
711 fusion->reply_frames_desc[0] =
712 dma_pool_alloc(fusion->reply_frames_desc_pool,
713 GFP_KERNEL,
714 &fusion->reply_frames_desc_phys[0]);
715
716 if (!fusion->reply_frames_desc[0]) {
717 dev_err(&instance->pdev->dev,
718 "Failed from %s %d\n", __func__, __LINE__);
719 return -ENOMEM;
720 }
721 }
722
179ac142 723 reply_desc = fusion->reply_frames_desc[0];
c8e858fe 724 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
c6f5bf81 725 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
9c915a8c 726
179ac142
SS
727 /* This is not a rdpq mode, but driver still populate
728 * reply_frame_desc array to use same msix index in ISR path.
729 */
730 for (i = 0; i < (count - 1); i++)
731 fusion->reply_frames_desc[i + 1] =
732 fusion->reply_frames_desc[i] +
733 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
9c915a8c 734
179ac142
SS
735 return 0;
736}
9c915a8c 737
e45ab43b 738static int
179ac142
SS
739megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
740{
107a60dd 741 int i, j, k, msix_count;
179ac142
SS
742 struct fusion_context *fusion;
743 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
107a60dd
S
744 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
745 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
746 u8 dma_alloc_count, abs_index;
747 u32 chunk_size, array_size, offset;
179ac142
SS
748
749 fusion = instance->ctrl_context;
107a60dd
S
750 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
751 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
752 MAX_MSIX_QUEUES_FUSION;
179ac142 753
750afb08
LC
754 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
755 array_size, &fusion->rdpq_phys,
756 GFP_KERNEL);
179ac142
SS
757 if (!fusion->rdpq_virt) {
758 dev_err(&instance->pdev->dev,
759 "Failed from %s %d\n", __func__, __LINE__);
760 return -ENOMEM;
9c915a8c
AR
761 }
762
107a60dd
S
763 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
764
fc69d86d
RP
765 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
766 &instance->pdev->dev,
107a60dd
S
767 chunk_size, 16, 0);
768 fusion->reply_frames_desc_pool_align =
769 dma_pool_create("mr_rdpq_align",
770 &instance->pdev->dev,
771 chunk_size,
772 roundup_pow_of_two(chunk_size),
773 0);
774
775 if (!fusion->reply_frames_desc_pool ||
776 !fusion->reply_frames_desc_pool_align) {
179ac142
SS
777 dev_err(&instance->pdev->dev,
778 "Failed from %s %d\n", __func__, __LINE__);
779 return -ENOMEM;
9c915a8c
AR
780 }
781
107a60dd
S
782/*
783 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
784 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
785 * within 4GB boundary and also reply queues in a set must have same
786 * upper 32-bits in their memory address. so here driver is allocating the
787 * DMA'able memory for reply queues according. Driver uses limitation of
788 * VENTURA_SERIES to manage INVADER_SERIES as well.
789 */
790 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
791
792 for (i = 0; i < dma_alloc_count; i++) {
793 rdpq_chunk_virt[i] =
794 dma_pool_alloc(fusion->reply_frames_desc_pool,
795 GFP_KERNEL, &rdpq_chunk_phys[i]);
796 if (!rdpq_chunk_virt[i]) {
179ac142
SS
797 dev_err(&instance->pdev->dev,
798 "Failed from %s %d\n", __func__, __LINE__);
799 return -ENOMEM;
800 }
107a60dd
S
801 /* reply desc pool requires to be in same 4 gb region.
802 * Below function will check this.
803 * In case of failure, new pci pool will be created with updated
804 * alignment.
805 * For RDPQ buffers, driver always allocate two separate pci pool.
806 * Alignment will be used such a way that next allocation if
807 * success, will always meet same 4gb region requirement.
808 * rdpq_tracker keep track of each buffer's physical,
809 * virtual address and pci pool descriptor. It will help driver
810 * while freeing the resources.
811 *
812 */
813 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
814 chunk_size)) {
815 dma_pool_free(fusion->reply_frames_desc_pool,
816 rdpq_chunk_virt[i],
817 rdpq_chunk_phys[i]);
9c915a8c 818
107a60dd
S
819 rdpq_chunk_virt[i] =
820 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
821 GFP_KERNEL, &rdpq_chunk_phys[i]);
822 if (!rdpq_chunk_virt[i]) {
823 dev_err(&instance->pdev->dev,
824 "Failed from %s %d\n",
825 __func__, __LINE__);
826 return -ENOMEM;
827 }
828 fusion->rdpq_tracker[i].dma_pool_ptr =
829 fusion->reply_frames_desc_pool_align;
830 } else {
831 fusion->rdpq_tracker[i].dma_pool_ptr =
832 fusion->reply_frames_desc_pool;
833 }
179ac142 834
107a60dd
S
835 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
836 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
9c915a8c 837 }
107a60dd
S
838
839 for (k = 0; k < dma_alloc_count; k++) {
840 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
841 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
842
843 if (abs_index == msix_count)
844 break;
845 offset = fusion->reply_alloc_sz * i;
846 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
847 cpu_to_le64(rdpq_chunk_phys[k] + offset);
848 fusion->reply_frames_desc_phys[abs_index] =
849 rdpq_chunk_phys[k] + offset;
850 fusion->reply_frames_desc[abs_index] =
851 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
852
853 reply_desc = fusion->reply_frames_desc[abs_index];
854 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
855 reply_desc->Words = ULLONG_MAX;
856 }
857 }
858
179ac142
SS
859 return 0;
860}
9c915a8c 861
179ac142
SS
862static void
863megasas_free_rdpq_fusion(struct megasas_instance *instance) {
9c915a8c 864
179ac142
SS
865 int i;
866 struct fusion_context *fusion;
9c915a8c 867
179ac142
SS
868 fusion = instance->ctrl_context;
869
107a60dd
S
870 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
871 if (fusion->rdpq_tracker[i].pool_entry_virt)
872 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
873 fusion->rdpq_tracker[i].pool_entry_virt,
874 fusion->rdpq_tracker[i].pool_entry_phys);
875
9c915a8c
AR
876 }
877
19c05072
TM
878 dma_pool_destroy(fusion->reply_frames_desc_pool);
879 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
179ac142
SS
880
881 if (fusion->rdpq_virt)
60ee6529 882 dma_free_coherent(&instance->pdev->dev,
179ac142
SS
883 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
884 fusion->rdpq_virt, fusion->rdpq_phys);
885}
886
887static void
888megasas_free_reply_fusion(struct megasas_instance *instance) {
889
890 struct fusion_context *fusion;
891
892 fusion = instance->ctrl_context;
893
894 if (fusion->reply_frames_desc[0])
fc69d86d 895 dma_pool_free(fusion->reply_frames_desc_pool,
179ac142
SS
896 fusion->reply_frames_desc[0],
897 fusion->reply_frames_desc_phys[0]);
898
19c05072 899 dma_pool_destroy(fusion->reply_frames_desc_pool);
179ac142
SS
900
901}
902
903
904/**
905 * megasas_alloc_cmds_fusion - Allocates the command packets
906 * @instance: Adapter soft state
907 *
908 *
909 * Each frame has a 32-bit field called context. This context is used to get
910 * back the megasas_cmd_fusion from the frame when a frame gets completed
911 * In this driver, the 32 bit values are the indices into an array cmd_list.
912 * This array is used only to look up the megasas_cmd_fusion given the context.
913 * The free commands themselves are maintained in a linked list called cmd_pool.
914 *
915 * cmds are formed in the io_request and sg_frame members of the
916 * megasas_cmd_fusion. The context field is used to get a request descriptor
917 * and is used as SMID of the cmd.
918 * SMID value range is from 1 to max_fw_cmds.
919 */
e45ab43b 920static int
179ac142
SS
921megasas_alloc_cmds_fusion(struct megasas_instance *instance)
922{
923 int i;
924 struct fusion_context *fusion;
925 struct megasas_cmd_fusion *cmd;
926 u32 offset;
927 dma_addr_t io_req_base_phys;
928 u8 *io_req_base;
929
930
931 fusion = instance->ctrl_context;
932
179ac142
SS
933 if (megasas_alloc_request_fusion(instance))
934 goto fail_exit;
935
936 if (instance->is_rdpq) {
937 if (megasas_alloc_rdpq_fusion(instance))
938 goto fail_exit;
939 } else
940 if (megasas_alloc_reply_fusion(instance))
941 goto fail_exit;
942
e97e673c
S
943 if (megasas_alloc_cmdlist_fusion(instance))
944 goto fail_exit;
945
946 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
947 instance->max_fw_cmds);
179ac142
SS
948
949 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
950 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
951 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
9c915a8c
AR
952
953 /*
954 * Add all the commands to command pool (fusion->cmd_pool)
955 */
956
957 /* SMID 0 is reserved. Set SMID/index from 1 */
69c337c0 958 for (i = 0; i < instance->max_mpt_cmds; i++) {
9c915a8c
AR
959 cmd = fusion->cmd_list[i];
960 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
961 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
962 cmd->index = i + 1;
963 cmd->scmd = NULL;
69c337c0
SC
964 cmd->sync_cmd_idx =
965 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
4026e9aa
SS
966 (i - instance->max_scsi_cmds) :
967 (u32)ULONG_MAX; /* Set to Invalid */
9c915a8c
AR
968 cmd->instance = instance;
969 cmd->io_request =
970 (struct MPI2_RAID_SCSI_IO_REQUEST *)
971 (io_req_base + offset);
972 memset(cmd->io_request, 0,
973 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
974 cmd->io_request_phys_addr = io_req_base_phys + offset;
8bf7c65d 975 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
9c915a8c
AR
976 }
977
179ac142
SS
978 if (megasas_create_sg_sense_fusion(instance))
979 goto fail_exit;
9c915a8c
AR
980
981 return 0;
982
179ac142
SS
983fail_exit:
984 megasas_free_cmds_fusion(instance);
9c915a8c
AR
985 return -ENOMEM;
986}
987
988/**
989 * wait_and_poll - Issues a polling command
990 * @instance: Adapter soft state
991 * @cmd: Command packet to be issued
992 *
993 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
994 */
995int
229fe47c
AR
996wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
997 int seconds)
9c915a8c
AR
998{
999 int i;
1000 struct megasas_header *frame_hdr = &cmd->frame->hdr;
ccf6c1f2 1001 u32 status_reg;
9c915a8c 1002
229fe47c 1003 u32 msecs = seconds * 1000;
9c915a8c
AR
1004
1005 /*
1006 * Wait for cmd_status to change
1007 */
1008 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
1009 rmb();
1010 msleep(20);
ccf6c1f2
CP
1011 if (!(i % 5000)) {
1012 status_reg = instance->instancet->read_fw_status_reg(instance)
1013 & MFI_STATE_MASK;
1014 if (status_reg == MFI_STATE_FAULT)
1015 break;
1016 }
9c915a8c
AR
1017 }
1018
6d40afbc
SS
1019 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
1020 return DCMD_TIMEOUT;
1021 else if (frame_hdr->cmd_status == MFI_STAT_OK)
1022 return DCMD_SUCCESS;
1023 else
1024 return DCMD_FAILED;
9c915a8c
AR
1025}
1026
1027/**
1028 * megasas_ioc_init_fusion - Initializes the FW
1029 * @instance: Adapter soft state
1030 *
1031 * Issues the IOC Init cmd
1032 */
1033int
1034megasas_ioc_init_fusion(struct megasas_instance *instance)
1035{
1036 struct megasas_init_frame *init_frame;
179ac142 1037 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
9c915a8c 1038 dma_addr_t ioc_init_handle;
9c915a8c 1039 struct megasas_cmd *cmd;
179ac142 1040 u8 ret, cur_rdpq_mode;
9c915a8c 1041 struct fusion_context *fusion;
c77a9bd8 1042 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
9c915a8c
AR
1043 int i;
1044 struct megasas_header *frame_hdr;
5765c5b8 1045 const char *sys_info;
4dbbe3ce 1046 MFI_CAPABILITIES *drv_ops;
81b76452 1047 u32 scratch_pad_1;
b45093dd 1048 ktime_t time;
107a60dd 1049 bool cur_fw_64bit_dma_capable;
132147d7 1050 bool cur_intr_coalescing;
9c915a8c
AR
1051
1052 fusion = instance->ctrl_context;
1053
9b3d028f
S
1054 ioc_init_handle = fusion->ioc_init_request_phys;
1055 IOCInitMessage = fusion->ioc_init_request;
9c915a8c 1056
b9637d14 1057 cmd = fusion->ioc_init_cmd;
9c915a8c 1058
272652fc
S
1059 scratch_pad_1 = megasas_readl
1060 (instance, &instance->reg_set->outbound_scratch_pad_1);
179ac142 1061
81b76452 1062 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
179ac142 1063
107a60dd
S
1064 if (instance->adapter_type == INVADER_SERIES) {
1065 cur_fw_64bit_dma_capable =
81b76452 1066 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
107a60dd
S
1067
1068 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1069 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1070 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1071 megaraid_sas_kill_hba(instance);
1072 ret = 1;
1073 goto fail_fw_init;
1074 }
1075 }
1076
179ac142
SS
1077 if (instance->is_rdpq && !cur_rdpq_mode) {
1078 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1079 " from RDPQ mode to non RDPQ mode\n");
1080 ret = 1;
1081 goto fail_fw_init;
1082 }
1083
132147d7
CP
1084 cur_intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
1085 true : false;
1086
1087 if ((instance->low_latency_index_start ==
1088 MR_HIGH_IOPS_QUEUE_COUNT) && cur_intr_coalescing)
299ee426 1089 instance->perf_mode = MR_BALANCED_PERF_MODE;
132147d7 1090
299ee426
CP
1091 dev_info(&instance->pdev->dev, "Performance mode :%s\n",
1092 MEGASAS_PERF_MODE_2STR(instance->perf_mode));
132147d7 1093
81b76452 1094 instance->fw_sync_cache_support = (scratch_pad_1 &
d0fc91d6
KD
1095 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1096 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1097 instance->fw_sync_cache_support ? "Yes" : "No");
1098
9c915a8c
AR
1099 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1100
1101 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1102 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
94cd65dd
SS
1103 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1104 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1105 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1106
1107 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
179ac142
SS
1108 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1109 cpu_to_le64(fusion->rdpq_phys) :
1110 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1111 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1112 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
94cd65dd 1113 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
107a60dd 1114 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
5738f996 1115 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
15dd0381 1116 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
b99fc202 1117
b45093dd 1118 time = ktime_get_real();
b99fc202 1119 /* Convert to milliseconds as per FW requirement */
b45093dd 1120 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
b99fc202 1121
9c915a8c 1122 init_frame = (struct megasas_init_frame *)cmd->frame;
cb51efeb 1123 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
9c915a8c
AR
1124
1125 frame_hdr = &cmd->frame->hdr;
9c915a8c 1126 frame_hdr->cmd_status = 0xFF;
b52fd077 1127 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
9c915a8c
AR
1128
1129 init_frame->cmd = MFI_CMD_INIT;
1130 init_frame->cmd_status = 0xFF;
1131
4dbbe3ce 1132 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1133
d46a3ad6 1134 /* driver support Extended MSIX */
c365178f 1135 if (instance->adapter_type >= INVADER_SERIES)
4dbbe3ce 1136 drv_ops->mfi_capabilities.support_additional_msix = 1;
21c9e160 1137 /* driver supports HA / Remote LUN over Fast Path interface */
4dbbe3ce 1138 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1139
1140 drv_ops->mfi_capabilities.support_max_255lds = 1;
1141 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1142 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1143
bd5f9484 1144 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1145 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1146
8f05024c 1147 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
308ec459
SS
1148 if (!dual_qdepth_disable)
1149 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
8f05024c 1150
52b62ac7 1151 drv_ops->mfi_capabilities.support_qd_throttling = 1;
ede7c3ce 1152 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
f870bcbe 1153 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
f6fe5731 1154 drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
107a60dd
S
1155
1156 if (instance->consistent_mask_64bit)
1157 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1158
94cd65dd
SS
1159 /* Convert capability to LE32 */
1160 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
21c9e160 1161
5765c5b8
SS
1162 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1163 if (instance->system_info_buf && sys_info) {
1164 memcpy(instance->system_info_buf->systemId, sys_info,
1165 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1166 instance->system_info_buf->systemIdLength =
1167 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
107a60dd
S
1168 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1169 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
5765c5b8
SS
1170 }
1171
fdc5a97c
HR
1172 init_frame->queue_info_new_phys_addr_hi =
1173 cpu_to_le32(upper_32_bits(ioc_init_handle));
1174 init_frame->queue_info_new_phys_addr_lo =
1175 cpu_to_le32(lower_32_bits(ioc_init_handle));
94cd65dd 1176 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
9c915a8c 1177
ea836f40
CP
1178 /*
1179 * Each bit in replyqueue_mask represents one group of MSI-x vectors
1180 * (each group has 8 vectors)
1181 */
299ee426
CP
1182 switch (instance->perf_mode) {
1183 case MR_BALANCED_PERF_MODE:
ea836f40 1184 init_frame->replyqueue_mask =
299ee426
CP
1185 cpu_to_le16(~(~0 << instance->low_latency_index_start/8));
1186 break;
1187 case MR_IOPS_PERF_MODE:
1188 init_frame->replyqueue_mask =
1189 cpu_to_le16(~(~0 << instance->msix_vectors/8));
1190 break;
1191 }
1192
ea836f40 1193
200aed58
SS
1194 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1195 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
c77a9bd8 1196 req_desc.MFAIo.RequestFlags =
9c915a8c 1197 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
200aed58 1198 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
9c915a8c
AR
1199
1200 /*
1201 * disable the intr before firing the init frame
1202 */
d46a3ad6 1203 instance->instancet->disable_intr(instance);
9c915a8c
AR
1204
1205 for (i = 0; i < (10 * 1000); i += 20) {
272652fc 1206 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
9c915a8c
AR
1207 msleep(20);
1208 else
1209 break;
1210 }
1211
5885571d
CP
1212 /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1213 megasas_write_64bit_req_desc(instance, &req_desc);
9c915a8c 1214
1b60d4e5 1215 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
9c915a8c
AR
1216
1217 frame_hdr = &cmd->frame->hdr;
1218 if (frame_hdr->cmd_status != 0) {
1219 ret = 1;
1220 goto fail_fw_init;
1221 }
9c915a8c 1222
5885571d
CP
1223 if (instance->adapter_type >= AERO_SERIES) {
1224 scratch_pad_1 = megasas_readl
1225 (instance, &instance->reg_set->outbound_scratch_pad_1);
1226
1227 instance->atomic_desc_support =
1228 (scratch_pad_1 & MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
1229
1230 dev_info(&instance->pdev->dev, "FW supports atomic descriptor\t: %s\n",
1231 instance->atomic_desc_support ? "Yes" : "No");
1232 }
1233
fb1633d5 1234 return 0;
9c915a8c
AR
1235
1236fail_fw_init:
2d2c2331 1237 dev_err(&instance->pdev->dev,
fb1633d5
VL
1238 "Init cmd return status FAILED for SCSI host %d\n",
1239 instance->host->host_no);
2d2c2331 1240
9c915a8c
AR
1241 return ret;
1242}
1243
3761cb4c 1244/**
1245 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1246 * @instance: Adapter soft state
1247 * @pend: set to 1, if it is pended jbod map.
1248 *
1249 * Issue Jbod map to the firmware. If it is pended command,
1250 * issue command and return. If it is first instance of jbod map
1251 * issue and receive command.
1252 */
1253int
1254megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1255 int ret = 0;
e58ed500 1256 size_t pd_seq_map_sz;
3761cb4c 1257 struct megasas_cmd *cmd;
1258 struct megasas_dcmd_frame *dcmd;
1259 struct fusion_context *fusion = instance->ctrl_context;
1260 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1261 dma_addr_t pd_seq_h;
1262
1263 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1264 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
e58ed500 1265 pd_seq_map_sz = struct_size(pd_sync, seq, MAX_PHYSICAL_DEVICES - 1);
3761cb4c 1266
1267 cmd = megasas_get_cmd(instance);
1268 if (!cmd) {
1269 dev_err(&instance->pdev->dev,
1270 "Could not get mfi cmd. Fail from %s %d\n",
1271 __func__, __LINE__);
1272 return -ENOMEM;
1273 }
1274
1275 dcmd = &cmd->frame->dcmd;
1276
1277 memset(pd_sync, 0, pd_seq_map_sz);
1278 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
107a60dd
S
1279
1280 if (pend) {
1281 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1282 dcmd->flags = MFI_FRAME_DIR_WRITE;
1283 instance->jbod_seq_cmd = cmd;
1284 } else {
1285 dcmd->flags = MFI_FRAME_DIR_READ;
1286 }
1287
3761cb4c 1288 dcmd->cmd = MFI_CMD_DCMD;
1289 dcmd->cmd_status = 0xFF;
1290 dcmd->sge_count = 1;
1291 dcmd->timeout = 0;
1292 dcmd->pad_0 = 0;
1293 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1294 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
107a60dd
S
1295
1296 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
3761cb4c 1297
1298 if (pend) {
3761cb4c 1299 instance->instancet->issue_dcmd(instance, cmd);
1300 return 0;
1301 }
1302
3761cb4c 1303 /* Below code is only for non pended DCMD */
55fecaec 1304 if (!instance->mask_interrupts)
6d40afbc
SS
1305 ret = megasas_issue_blocked_cmd(instance, cmd,
1306 MFI_IO_TIMEOUT_SECS);
3761cb4c 1307 else
1308 ret = megasas_issue_polled(instance, cmd);
1309
1310 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1311 dev_warn(&instance->pdev->dev,
1312 "driver supports max %d JBOD, but FW reports %d\n",
1313 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1314 ret = -EINVAL;
1315 }
1316
55fecaec 1317 if (ret == DCMD_TIMEOUT)
6d40afbc
SS
1318 megaraid_sas_kill_hba(instance);
1319
1320 if (ret == DCMD_SUCCESS)
3761cb4c 1321 instance->pd_seq_map_id++;
1322
1323 megasas_return_cmd(instance, cmd);
1324 return ret;
1325}
1326
9c915a8c
AR
1327/*
1328 * megasas_get_ld_map_info - Returns FW's ld_map structure
1329 * @instance: Adapter soft state
1330 * @pend: Pend the command or not
1331 * Issues an internal command (DCMD) to get the FW's controller PD
1332 * list structure. This information is mainly used to find out SYSTEM
1333 * supported by the FW.
51087a86
SS
1334 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1335 * dcmd.mbox.b[0] - number of LDs being sync'd
1336 * dcmd.mbox.b[1] - 0 - complete command immediately.
1337 * - 1 - pend till config change
1338 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1339 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1340 * uses extended struct MR_FW_RAID_MAP_EXT
9c915a8c
AR
1341 */
1342static int
1343megasas_get_ld_map_info(struct megasas_instance *instance)
1344{
1345 int ret = 0;
1346 struct megasas_cmd *cmd;
1347 struct megasas_dcmd_frame *dcmd;
51087a86 1348 void *ci;
9c915a8c
AR
1349 dma_addr_t ci_h = 0;
1350 u32 size_map_info;
1351 struct fusion_context *fusion;
1352
1353 cmd = megasas_get_cmd(instance);
1354
1355 if (!cmd) {
1be18254 1356 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
9c915a8c
AR
1357 return -ENOMEM;
1358 }
1359
1360 fusion = instance->ctrl_context;
1361
1362 if (!fusion) {
1363 megasas_return_cmd(instance, cmd);
2f8bdfa8 1364 return -ENXIO;
9c915a8c
AR
1365 }
1366
1367 dcmd = &cmd->frame->dcmd;
1368
51087a86 1369 size_map_info = fusion->current_map_sz;
9c915a8c 1370
51087a86 1371 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
9c915a8c
AR
1372 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1373
1374 if (!ci) {
1be18254 1375 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
9c915a8c
AR
1376 megasas_return_cmd(instance, cmd);
1377 return -ENOMEM;
1378 }
1379
51087a86 1380 memset(ci, 0, fusion->max_map_sz);
9c915a8c 1381 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
9c915a8c
AR
1382 dcmd->cmd = MFI_CMD_DCMD;
1383 dcmd->cmd_status = 0xFF;
1384 dcmd->sge_count = 1;
107a60dd 1385 dcmd->flags = MFI_FRAME_DIR_READ;
9c915a8c
AR
1386 dcmd->timeout = 0;
1387 dcmd->pad_0 = 0;
94cd65dd
SS
1388 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1389 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
9c915a8c 1390
107a60dd 1391 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
9c915a8c 1392
55fecaec 1393 if (!instance->mask_interrupts)
90dc9d98 1394 ret = megasas_issue_blocked_cmd(instance, cmd,
6d40afbc 1395 MFI_IO_TIMEOUT_SECS);
90dc9d98
SS
1396 else
1397 ret = megasas_issue_polled(instance, cmd);
9c915a8c 1398
55fecaec 1399 if (ret == DCMD_TIMEOUT)
6d40afbc
SS
1400 megaraid_sas_kill_hba(instance);
1401
4026e9aa 1402 megasas_return_cmd(instance, cmd);
9c915a8c
AR
1403
1404 return ret;
1405}
1406
1407u8
1408megasas_get_map_info(struct megasas_instance *instance)
1409{
1410 struct fusion_context *fusion = instance->ctrl_context;
1411
1412 fusion->fast_path_io = 0;
1413 if (!megasas_get_ld_map_info(instance)) {
5f19f7c8 1414 if (MR_ValidateMapInfo(instance, instance->map_id)) {
9c915a8c
AR
1415 fusion->fast_path_io = 1;
1416 return 0;
1417 }
1418 }
1419 return 1;
1420}
1421
1422/*
1423 * megasas_sync_map_info - Returns FW's ld_map structure
1424 * @instance: Adapter soft state
1425 *
1426 * Issues an internal command (DCMD) to get the FW's controller PD
1427 * list structure. This information is mainly used to find out SYSTEM
1428 * supported by the FW.
1429 */
1430int
1431megasas_sync_map_info(struct megasas_instance *instance)
1432{
f4fc2093 1433 int i;
9c915a8c
AR
1434 struct megasas_cmd *cmd;
1435 struct megasas_dcmd_frame *dcmd;
d2d0358b 1436 u16 num_lds;
9c915a8c
AR
1437 struct fusion_context *fusion;
1438 struct MR_LD_TARGET_SYNC *ci = NULL;
51087a86 1439 struct MR_DRV_RAID_MAP_ALL *map;
9c915a8c
AR
1440 struct MR_LD_RAID *raid;
1441 struct MR_LD_TARGET_SYNC *ld_sync;
1442 dma_addr_t ci_h = 0;
1443 u32 size_map_info;
1444
1445 cmd = megasas_get_cmd(instance);
1446
1447 if (!cmd) {
1be18254 1448 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
9c915a8c
AR
1449 return -ENOMEM;
1450 }
1451
1452 fusion = instance->ctrl_context;
1453
1454 if (!fusion) {
1455 megasas_return_cmd(instance, cmd);
1456 return 1;
1457 }
1458
51087a86 1459 map = fusion->ld_drv_map[instance->map_id & 1];
9c915a8c 1460
6e755ddc 1461 num_lds = le16_to_cpu(map->raidMap.ldCount);
9c915a8c
AR
1462
1463 dcmd = &cmd->frame->dcmd;
1464
9c915a8c
AR
1465 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1466
1467 ci = (struct MR_LD_TARGET_SYNC *)
1468 fusion->ld_map[(instance->map_id - 1) & 1];
51087a86 1469 memset(ci, 0, fusion->max_map_sz);
9c915a8c
AR
1470
1471 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1472
1473 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1474
1475 for (i = 0; i < num_lds; i++, ld_sync++) {
1476 raid = MR_LdRaidGet(i, map);
1477 ld_sync->targetId = MR_GetLDTgtId(i, map);
1478 ld_sync->seqNum = raid->seqNum;
1479 }
1480
51087a86 1481 size_map_info = fusion->current_map_sz;
9c915a8c
AR
1482
1483 dcmd->cmd = MFI_CMD_DCMD;
1484 dcmd->cmd_status = 0xFF;
1485 dcmd->sge_count = 1;
107a60dd 1486 dcmd->flags = MFI_FRAME_DIR_WRITE;
9c915a8c
AR
1487 dcmd->timeout = 0;
1488 dcmd->pad_0 = 0;
94cd65dd 1489 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
9c915a8c
AR
1490 dcmd->mbox.b[0] = num_lds;
1491 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
94cd65dd 1492 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
107a60dd
S
1493
1494 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
9c915a8c
AR
1495
1496 instance->map_update_cmd = cmd;
1497
1498 instance->instancet->issue_dcmd(instance, cmd);
1499
f4fc2093 1500 return 0;
9c915a8c
AR
1501}
1502
39b72c3c
SS
1503/*
1504 * meagasas_display_intel_branding - Display branding string
1505 * @instance: per adapter object
1506 *
1507 * Return nothing.
1508 */
1509static void
1510megasas_display_intel_branding(struct megasas_instance *instance)
1511{
1512 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1513 return;
1514
1515 switch (instance->pdev->device) {
1516 case PCI_DEVICE_ID_LSI_INVADER:
1517 switch (instance->pdev->subsystem_device) {
1518 case MEGARAID_INTEL_RS3DC080_SSDID:
1519 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1520 instance->host->host_no,
1521 MEGARAID_INTEL_RS3DC080_BRANDING);
1522 break;
1523 case MEGARAID_INTEL_RS3DC040_SSDID:
1524 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1525 instance->host->host_no,
1526 MEGARAID_INTEL_RS3DC040_BRANDING);
1527 break;
1528 case MEGARAID_INTEL_RS3SC008_SSDID:
1529 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1530 instance->host->host_no,
1531 MEGARAID_INTEL_RS3SC008_BRANDING);
1532 break;
1533 case MEGARAID_INTEL_RS3MC044_SSDID:
1534 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1535 instance->host->host_no,
1536 MEGARAID_INTEL_RS3MC044_BRANDING);
1537 break;
1538 default:
1539 break;
1540 }
1541 break;
1542 case PCI_DEVICE_ID_LSI_FURY:
1543 switch (instance->pdev->subsystem_device) {
1544 case MEGARAID_INTEL_RS3WC080_SSDID:
1545 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1546 instance->host->host_no,
1547 MEGARAID_INTEL_RS3WC080_BRANDING);
1548 break;
1549 case MEGARAID_INTEL_RS3WC040_SSDID:
1550 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1551 instance->host->host_no,
1552 MEGARAID_INTEL_RS3WC040_BRANDING);
1553 break;
1554 default:
1555 break;
1556 }
1557 break;
7364d34b 1558 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1559 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1560 switch (instance->pdev->subsystem_device) {
1561 case MEGARAID_INTEL_RMS3BC160_SSDID:
1562 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1563 instance->host->host_no,
1564 MEGARAID_INTEL_RMS3BC160_BRANDING);
1565 break;
1566 default:
1567 break;
1568 }
1569 break;
39b72c3c
SS
1570 default:
1571 break;
1572 }
1573}
1574
def3e8df
S
1575/**
1576 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1577 * @instance: Adapter soft state
1578 *
1579 * return: if success: return 0
1580 * failed: return -ENOMEM
1581 */
1582static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1583{
1584 struct fusion_context *fusion;
1585 int i = 0;
1586
1587 fusion = instance->ctrl_context;
1588
1589 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1590
1591 for (i = 0; i < 2; i++) {
1592 fusion->ld_map[i] = NULL;
1593
1594 fusion->ld_drv_map[i] = (void *)
1595 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1596 fusion->drv_map_pages);
1597
1598 if (!fusion->ld_drv_map[i]) {
1599 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1600
1601 if (!fusion->ld_drv_map[i]) {
1602 dev_err(&instance->pdev->dev,
1603 "Could not allocate memory for local map"
1604 " size requested: %d\n",
1605 fusion->drv_map_sz);
1606 goto ld_drv_map_alloc_fail;
1607 }
1608 }
1609 }
1610
1611 for (i = 0; i < 2; i++) {
1612 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1613 fusion->max_map_sz,
1614 &fusion->ld_map_phys[i],
1615 GFP_KERNEL);
1616 if (!fusion->ld_map[i]) {
1617 dev_err(&instance->pdev->dev,
1618 "Could not allocate memory for map info %s:%d\n",
1619 __func__, __LINE__);
1620 goto ld_map_alloc_fail;
1621 }
1622 }
1623
1624 return 0;
1625
1626ld_map_alloc_fail:
1627 for (i = 0; i < 2; i++) {
1628 if (fusion->ld_map[i])
1629 dma_free_coherent(&instance->pdev->dev,
1630 fusion->max_map_sz,
1631 fusion->ld_map[i],
1632 fusion->ld_map_phys[i]);
1633 }
1634
1635ld_drv_map_alloc_fail:
1636 for (i = 0; i < 2; i++) {
1637 if (fusion->ld_drv_map[i]) {
1638 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1639 vfree(fusion->ld_drv_map[i]);
1640 else
1641 free_pages((ulong)fusion->ld_drv_map[i],
1642 fusion->drv_map_pages);
1643 }
1644 }
1645
1646 return -ENOMEM;
1647}
1648
e97e673c
S
1649/**
1650 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1651 * reply desc queue,
1652 * IO request frame queue, set can_queue.
1653 * @instance: Adapter soft state
1654 * @return: void
1655 */
1656static inline
1657void megasas_configure_queue_sizes(struct megasas_instance *instance)
1658{
1659 struct fusion_context *fusion;
1660 u16 max_cmd;
1661
1662 fusion = instance->ctrl_context;
1663 max_cmd = instance->max_fw_cmds;
1664
630d42b7 1665 if (instance->adapter_type >= VENTURA_SERIES)
e97e673c
S
1666 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1667 else
1668 instance->max_mpt_cmds = instance->max_fw_cmds;
1669
9fb98561 1670 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
e97e673c
S
1671 instance->cur_can_queue = instance->max_scsi_cmds;
1672 instance->host->can_queue = instance->cur_can_queue;
1673
1674 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1675
1676 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1677 instance->max_mpt_cmds;
1678 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1679 (fusion->reply_q_depth);
1680 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1681 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1682 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1683}
1684
b9637d14
S
1685static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1686{
1687 struct fusion_context *fusion;
1688 struct megasas_cmd *cmd;
1689
1690 fusion = instance->ctrl_context;
1691
e05ee4e9 1692 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
b9637d14
S
1693
1694 if (!cmd) {
1695 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1696 __func__, __LINE__);
1697 return -ENOMEM;
1698 }
1699
1700 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1701 IOC_INIT_FRAME_SIZE,
1702 &cmd->frame_phys_addr, GFP_KERNEL);
1703
1704 if (!cmd->frame) {
1705 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1706 __func__, __LINE__);
1707 kfree(cmd);
1708 return -ENOMEM;
1709 }
1710
1711 fusion->ioc_init_cmd = cmd;
1712 return 0;
1713}
1714
1715/**
1716 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1717 * @instance: Adapter soft state
1718 */
1719static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1720{
1721 struct fusion_context *fusion;
1722
1723 fusion = instance->ctrl_context;
1724
1725 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1726 dma_free_coherent(&instance->pdev->dev,
1727 IOC_INIT_FRAME_SIZE,
1728 fusion->ioc_init_cmd->frame,
1729 fusion->ioc_init_cmd->frame_phys_addr);
1730
19c05072 1731 kfree(fusion->ioc_init_cmd);
b9637d14
S
1732}
1733
9c915a8c
AR
1734/**
1735 * megasas_init_adapter_fusion - Initializes the FW
1736 * @instance: Adapter soft state
1737 *
1738 * This is the main function for initializing firmware.
1739 */
e45ab43b 1740static u32
9c915a8c
AR
1741megasas_init_adapter_fusion(struct megasas_instance *instance)
1742{
9c915a8c 1743 struct fusion_context *fusion;
81b76452 1744 u32 scratch_pad_1;
c8e858fe 1745 int i = 0, count;
ccf6c1f2 1746 u32 status_reg;
9c915a8c
AR
1747
1748 fusion = instance->ctrl_context;
1749
308ec459 1750 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
9c915a8c 1751
ae09a6c1
SS
1752 /*
1753 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1754 */
1755 instance->max_mfi_cmds =
1756 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
9c915a8c 1757
e97e673c 1758 megasas_configure_queue_sizes(instance);
9c915a8c 1759
272652fc
S
1760 scratch_pad_1 = megasas_readl(instance,
1761 &instance->reg_set->outbound_scratch_pad_1);
81b76452 1762 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
bd5f9484 1763 * Firmware support extended IO chain frame which is 4 times more than
1764 * legacy Firmware.
1765 * Legacy Firmware - Frame size is (8 * 128) = 1K
1766 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1767 */
81b76452 1768 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
bd5f9484 1769 instance->max_chain_frame_sz =
81b76452 1770 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
bd5f9484 1771 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1772 else
1773 instance->max_chain_frame_sz =
81b76452 1774 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
bd5f9484 1775 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1776
1777 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1778 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1779 instance->max_chain_frame_sz,
1780 MEGASAS_CHAIN_FRAME_SZ_MIN);
1781 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1782 }
1783
9c915a8c 1784 fusion->max_sge_in_main_msg =
bd5f9484 1785 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1786 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
9c915a8c
AR
1787
1788 fusion->max_sge_in_chain =
bd5f9484 1789 instance->max_chain_frame_sz
1790 / sizeof(union MPI2_SGE_IO_UNION);
9c915a8c 1791
bd5f9484 1792 instance->max_num_sge =
1793 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1794 + fusion->max_sge_in_chain - 2);
9c915a8c
AR
1795
1796 /* Used for pass thru MFI frame (DCMD) */
1797 fusion->chain_offset_mfi_pthru =
1798 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1799
1800 fusion->chain_offset_io_request =
1801 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1802 sizeof(union MPI2_SGE_IO_UNION))/16;
1803
c8e858fe
AR
1804 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1805 for (i = 0 ; i < count; i++)
1806 fusion->last_reply_idx[i] = 0;
9c915a8c 1807
f26ac3a1 1808 /*
ec779595 1809 * For fusion adapters, 3 commands for IOCTL and 8 commands
f26ac3a1
SS
1810 * for driver's internal DCMDs.
1811 */
1812 instance->max_scsi_cmds = instance->max_fw_cmds -
1813 (MEGASAS_FUSION_INTERNAL_CMDS +
1814 MEGASAS_FUSION_IOCTL_CMDS);
1815 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1816
b9637d14
S
1817 if (megasas_alloc_ioc_init_frame(instance))
1818 return 1;
1819
9c915a8c
AR
1820 /*
1821 * Allocate memory for descriptors
1822 * Create a pool of commands
1823 */
1824 if (megasas_alloc_cmds(instance))
1825 goto fail_alloc_mfi_cmds;
1826 if (megasas_alloc_cmds_fusion(instance))
1827 goto fail_alloc_cmds;
1828
ccf6c1f2
CP
1829 if (megasas_ioc_init_fusion(instance)) {
1830 status_reg = instance->instancet->read_fw_status_reg(instance);
1831 if (((status_reg & MFI_STATE_MASK) == MFI_STATE_FAULT) &&
1832 (status_reg & MFI_RESET_ADAPTER)) {
1833 /* Do a chip reset and then retry IOC INIT once */
1834 if (megasas_adp_reset_wait_for_ready
1835 (instance, true, 0) == FAILED)
1836 goto fail_ioc_init;
1837
1838 if (megasas_ioc_init_fusion(instance))
1839 goto fail_ioc_init;
1840 } else {
1841 goto fail_ioc_init;
1842 }
1843 }
9c915a8c 1844
39b72c3c 1845 megasas_display_intel_branding(instance);
d009b576 1846 if (megasas_get_ctrl_info(instance)) {
51087a86
SS
1847 dev_err(&instance->pdev->dev,
1848 "Could not get controller info. Fail from %s %d\n",
1849 __func__, __LINE__);
1850 goto fail_ioc_init;
1851 }
1852
9c915a8c 1853 instance->flag_ieee = 1;
a48ba0ec 1854 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
62a04f81 1855 instance->threshold_reply_count = instance->max_fw_cmds / 4;
51087a86 1856 fusion->fast_path_io = 0;
9c915a8c 1857
def3e8df
S
1858 if (megasas_allocate_raid_maps(instance))
1859 goto fail_ioc_init;
9c915a8c
AR
1860
1861 if (!megasas_get_map_info(instance))
1862 megasas_sync_map_info(instance);
1863
1864 return 0;
1865
9c915a8c 1866fail_ioc_init:
eb1b1237
AR
1867 megasas_free_cmds_fusion(instance);
1868fail_alloc_cmds:
1869 megasas_free_cmds(instance);
1870fail_alloc_mfi_cmds:
b9637d14 1871 megasas_free_ioc_init_cmd(instance);
9c915a8c
AR
1872 return 1;
1873}
1874
3f6194af
S
1875/**
1876 * megasas_fault_detect_work - Worker function of
1877 * FW fault handling workqueue.
1878 */
1879static void
1880megasas_fault_detect_work(struct work_struct *work)
1881{
1882 struct megasas_instance *instance =
1883 container_of(work, struct megasas_instance,
1884 fw_fault_work.work);
1885 u32 fw_state, dma_state, status;
1886
1887 /* Check the fw state */
de516379 1888 fw_state = instance->instancet->read_fw_status_reg(instance) &
3f6194af
S
1889 MFI_STATE_MASK;
1890
1891 if (fw_state == MFI_STATE_FAULT) {
de516379
S
1892 dma_state = instance->instancet->read_fw_status_reg(instance) &
1893 MFI_STATE_DMADONE;
3f6194af
S
1894 /* Start collecting crash, if DMA bit is done */
1895 if (instance->crash_dump_drv_support &&
1896 instance->crash_dump_app_support && dma_state) {
1897 megasas_fusion_crash_dump(instance);
1898 } else {
1899 if (instance->unload == 0) {
1900 status = megasas_reset_fusion(instance->host, 0);
1901 if (status != SUCCESS) {
1902 dev_err(&instance->pdev->dev,
1903 "Failed from %s %d, do not re-arm timer\n",
1904 __func__, __LINE__);
1905 return;
1906 }
1907 }
1908 }
1909 }
1910
1911 if (instance->fw_fault_work_q)
1912 queue_delayed_work(instance->fw_fault_work_q,
1913 &instance->fw_fault_work,
1914 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1915}
1916
1917int
1918megasas_fusion_start_watchdog(struct megasas_instance *instance)
1919{
1920 /* Check if the Fault WQ is already started */
1921 if (instance->fw_fault_work_q)
1922 return SUCCESS;
1923
1924 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1925
1926 snprintf(instance->fault_handler_work_q_name,
1927 sizeof(instance->fault_handler_work_q_name),
1928 "poll_megasas%d_status", instance->host->host_no);
1929
1930 instance->fw_fault_work_q =
1931 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1932 if (!instance->fw_fault_work_q) {
1933 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1934 __func__, __LINE__);
1935 return FAILED;
1936 }
1937
1938 queue_delayed_work(instance->fw_fault_work_q,
1939 &instance->fw_fault_work,
1940 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1941
1942 return SUCCESS;
1943}
1944
1945void
1946megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1947{
1948 struct workqueue_struct *wq;
1949
1950 if (instance->fw_fault_work_q) {
1951 wq = instance->fw_fault_work_q;
1952 instance->fw_fault_work_q = NULL;
1953 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1954 flush_workqueue(wq);
1955 destroy_workqueue(wq);
1956 }
1957}
1958
9c915a8c
AR
1959/**
1960 * map_cmd_status - Maps FW cmd status to OS cmd status
1961 * @cmd : Pointer to cmd
1962 * @status : status of cmd returned by FW
1963 * @ext_status : ext status of cmd returned by FW
1964 */
1965
e45ab43b 1966static void
69c337c0 1967map_cmd_status(struct fusion_context *fusion,
41064f1b
S
1968 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1969 u32 data_length, u8 *sense)
9c915a8c 1970{
def0eab3 1971 u8 cmd_type;
31d9a57b 1972 int resid;
9c915a8c 1973
def0eab3 1974 cmd_type = megasas_cmd_type(scmd);
9c915a8c
AR
1975 switch (status) {
1976
1977 case MFI_STAT_OK:
69c337c0 1978 scmd->result = DID_OK << 16;
9c915a8c
AR
1979 break;
1980
1981 case MFI_STAT_SCSI_IO_FAILED:
1982 case MFI_STAT_LD_INIT_IN_PROGRESS:
69c337c0 1983 scmd->result = (DID_ERROR << 16) | ext_status;
9c915a8c
AR
1984 break;
1985
1986 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1987
69c337c0 1988 scmd->result = (DID_OK << 16) | ext_status;
9c915a8c 1989 if (ext_status == SAM_STAT_CHECK_CONDITION) {
69c337c0 1990 memset(scmd->sense_buffer, 0,
9c915a8c 1991 SCSI_SENSE_BUFFERSIZE);
69c337c0 1992 memcpy(scmd->sense_buffer, sense,
9c915a8c 1993 SCSI_SENSE_BUFFERSIZE);
69c337c0 1994 scmd->result |= DRIVER_SENSE << 24;
9c915a8c 1995 }
31d9a57b
S
1996
1997 /*
1998 * If the IO request is partially completed, then MR FW will
1999 * update "io_request->DataLength" field with actual number of
2000 * bytes transferred.Driver will set residual bytes count in
2001 * SCSI command structure.
2002 */
2003 resid = (scsi_bufflen(scmd) - data_length);
2004 scsi_set_resid(scmd, resid);
def0eab3
S
2005
2006 if (resid &&
2007 ((cmd_type == READ_WRITE_LDIO) ||
2008 (cmd_type == READ_WRITE_SYSPDIO)))
2009 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
2010 " requested/completed 0x%x/0x%x\n",
2011 status, scsi_bufflen(scmd), data_length);
9c915a8c
AR
2012 break;
2013
2014 case MFI_STAT_LD_OFFLINE:
2015 case MFI_STAT_DEVICE_NOT_FOUND:
69c337c0 2016 scmd->result = DID_BAD_TARGET << 16;
9c915a8c 2017 break;
36807e67 2018 case MFI_STAT_CONFIG_SEQ_MISMATCH:
69c337c0 2019 scmd->result = DID_IMM_RETRY << 16;
36807e67 2020 break;
9c915a8c 2021 default:
69c337c0 2022 scmd->result = DID_ERROR << 16;
9c915a8c
AR
2023 break;
2024 }
2025}
2026
33203bc4
S
2027/**
2028 * megasas_is_prp_possible -
2029 * Checks if native NVMe PRPs can be built for the IO
2030 *
2031 * @instance: Adapter soft state
2032 * @scmd: SCSI command from the mid-layer
2033 * @sge_count: scatter gather element count.
2034 *
2035 * Returns: true: PRPs can be built
2036 * false: IEEE SGLs needs to be built
2037 */
2038static bool
2039megasas_is_prp_possible(struct megasas_instance *instance,
2040 struct scsi_cmnd *scmd, int sge_count)
2041{
33203bc4
S
2042 int i;
2043 u32 data_length = 0;
2044 struct scatterlist *sg_scmd;
2045 bool build_prp = false;
2046 u32 mr_nvme_pg_size;
2047
2048 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2049 MR_DEFAULT_NVME_PAGE_SIZE);
33203bc4
S
2050 data_length = scsi_bufflen(scmd);
2051 sg_scmd = scsi_sglist(scmd);
2052
2053 /*
2054 * NVMe uses one PRP for each page (or part of a page)
2055 * look at the data length - if 4 pages or less then IEEE is OK
2056 * if > 5 pages then we need to build a native SGL
2057 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2058 * if this first size in the page is >= the residual beyond 4 pages
2059 * then use IEEE, otherwise use native SGL
2060 */
2061
2062 if (data_length > (mr_nvme_pg_size * 5)) {
2063 build_prp = true;
2064 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
2065 (data_length <= (mr_nvme_pg_size * 5))) {
2066 /* check if 1st SG entry size is < residual beyond 4 pages */
2067 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
2068 build_prp = true;
2069 }
2070
2071/*
2072 * Below code detects gaps/holes in IO data buffers.
2073 * What does holes/gaps mean?
2074 * Any SGE except first one in a SGL starts at non NVME page size
2075 * aligned address OR Any SGE except last one in a SGL ends at
2076 * non NVME page size boundary.
2077 *
2078 * Driver has already informed block layer by setting boundary rules for
2079 * bio merging done at NVME page size boundary calling kernel API
2080 * blk_queue_virt_boundary inside slave_config.
2081 * Still there is possibility of IO coming with holes to driver because of
2082 * IO merging done by IO scheduler.
2083 *
2084 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
2085 * IO scheduling so no IO merging.
2086 *
2087 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
2088 * then sending IOs with holes.
2089 *
2090 * Though driver can request block layer to disable IO merging by calling-
8b904b5b 2091 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
33203bc4
S
2092 * user may tune sysfs parameter- nomerges again to 0 or 1.
2093 *
2094 * If in future IO scheduling is enabled with SCSI BLK MQ,
2095 * this algorithm to detect holes will be required in driver
2096 * for SCSI BLK MQ enabled case as well.
2097 *
2098 *
2099 */
2100 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
2101 if ((i != 0) && (i != (sge_count - 1))) {
2102 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
2103 mega_mod64(sg_dma_address(sg_scmd),
2104 mr_nvme_pg_size)) {
2105 build_prp = false;
33203bc4
S
2106 break;
2107 }
2108 }
2109
2110 if ((sge_count > 1) && (i == 0)) {
2111 if ((mega_mod64((sg_dma_address(sg_scmd) +
2112 sg_dma_len(sg_scmd)),
2113 mr_nvme_pg_size))) {
2114 build_prp = false;
33203bc4
S
2115 break;
2116 }
2117 }
2118
2119 if ((sge_count > 1) && (i == (sge_count - 1))) {
2120 if (mega_mod64(sg_dma_address(sg_scmd),
2121 mr_nvme_pg_size)) {
2122 build_prp = false;
33203bc4
S
2123 break;
2124 }
2125 }
2126 }
2127
2128 return build_prp;
2129}
2130
2131/**
2132 * megasas_make_prp_nvme -
2133 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2134 *
2135 * @instance: Adapter soft state
2136 * @scmd: SCSI command from the mid-layer
2137 * @sgl_ptr: SGL to be filled in
2138 * @cmd: Fusion command frame
2139 * @sge_count: scatter gather element count.
2140 *
2141 * Returns: true: PRPs are built
2142 * false: IEEE SGLs needs to be built
2143 */
2144static bool
2145megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2146 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2147 struct megasas_cmd_fusion *cmd, int sge_count)
2148{
2149 int sge_len, offset, num_prp_in_chain = 0;
2150 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
d1da522f
AB
2151 u64 *ptr_sgl;
2152 dma_addr_t ptr_sgl_phys;
33203bc4
S
2153 u64 sge_addr;
2154 u32 page_mask, page_mask_result;
2155 struct scatterlist *sg_scmd;
2156 u32 first_prp_len;
2157 bool build_prp = false;
2158 int data_len = scsi_bufflen(scmd);
33203bc4
S
2159 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2160 MR_DEFAULT_NVME_PAGE_SIZE);
2161
33203bc4
S
2162 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2163
2164 if (!build_prp)
2165 return false;
2166
2167 /*
2168 * Nvme has a very convoluted prp format. One prp is required
2169 * for each page or partial page. Driver need to split up OS sg_list
2170 * entries if it is longer than one page or cross a page
2171 * boundary. Driver also have to insert a PRP list pointer entry as
2172 * the last entry in each physical page of the PRP list.
2173 *
2174 * NOTE: The first PRP "entry" is actually placed in the first
2175 * SGL entry in the main message as IEEE 64 format. The 2nd
2176 * entry in the main message is the chain element, and the rest
2177 * of the PRP entries are built in the contiguous pcie buffer.
2178 */
2179 page_mask = mr_nvme_pg_size - 1;
2180 ptr_sgl = (u64 *)cmd->sg_frame;
d1da522f 2181 ptr_sgl_phys = cmd->sg_frame_phys_addr;
33203bc4
S
2182 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2183
2184 /* Build chain frame element which holds all prps except first*/
2185 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2186 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2187
d1da522f 2188 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
33203bc4
S
2189 main_chain_element->NextChainOffset = 0;
2190 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2191 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2192 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2193
2194 /* Build first prp, sge need not to be page aligned*/
2195 ptr_first_sgl = sgl_ptr;
2196 sg_scmd = scsi_sglist(scmd);
2197 sge_addr = sg_dma_address(sg_scmd);
2198 sge_len = sg_dma_len(sg_scmd);
2199
2200 offset = (u32)(sge_addr & page_mask);
2201 first_prp_len = mr_nvme_pg_size - offset;
2202
2203 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2204 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2205
2206 data_len -= first_prp_len;
2207
2208 if (sge_len > first_prp_len) {
2209 sge_addr += first_prp_len;
2210 sge_len -= first_prp_len;
2211 } else if (sge_len == first_prp_len) {
2212 sg_scmd = sg_next(sg_scmd);
2213 sge_addr = sg_dma_address(sg_scmd);
2214 sge_len = sg_dma_len(sg_scmd);
2215 }
2216
2217 for (;;) {
2218 offset = (u32)(sge_addr & page_mask);
2219
2220 /* Put PRP pointer due to page boundary*/
2221 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2222 if (unlikely(!page_mask_result)) {
2223 scmd_printk(KERN_NOTICE,
2224 scmd, "page boundary ptr_sgl: 0x%p\n",
2225 ptr_sgl);
d1da522f
AB
2226 ptr_sgl_phys += 8;
2227 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
33203bc4
S
2228 ptr_sgl++;
2229 num_prp_in_chain++;
2230 }
2231
2232 *ptr_sgl = cpu_to_le64(sge_addr);
2233 ptr_sgl++;
d1da522f 2234 ptr_sgl_phys += 8;
33203bc4
S
2235 num_prp_in_chain++;
2236
2237 sge_addr += mr_nvme_pg_size;
2238 sge_len -= mr_nvme_pg_size;
2239 data_len -= mr_nvme_pg_size;
2240
2241 if (data_len <= 0)
2242 break;
2243
2244 if (sge_len > 0)
2245 continue;
2246
2247 sg_scmd = sg_next(sg_scmd);
2248 sge_addr = sg_dma_address(sg_scmd);
2249 sge_len = sg_dma_len(sg_scmd);
2250 }
2251
2252 main_chain_element->Length =
2253 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2254
33203bc4
S
2255 return build_prp;
2256}
2257
9c915a8c
AR
2258/**
2259 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2260 * @instance: Adapter soft state
2261 * @scp: SCSI command from the mid-layer
2262 * @sgl_ptr: SGL to be filled in
2263 * @cmd: cmd we are working on
33203bc4 2264 * @sge_count sge count
9c915a8c 2265 *
9c915a8c 2266 */
33203bc4 2267static void
9c915a8c
AR
2268megasas_make_sgl_fusion(struct megasas_instance *instance,
2269 struct scsi_cmnd *scp,
2270 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
33203bc4 2271 struct megasas_cmd_fusion *cmd, int sge_count)
9c915a8c 2272{
33203bc4 2273 int i, sg_processed;
9c915a8c
AR
2274 struct scatterlist *os_sgl;
2275 struct fusion_context *fusion;
2276
2277 fusion = instance->ctrl_context;
2278
c365178f 2279 if (instance->adapter_type >= INVADER_SERIES) {
36807e67
AR
2280 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2281 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2282 sgl_ptr_end->Flags = 0;
2283 }
9c915a8c 2284
9c915a8c 2285 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
94cd65dd
SS
2286 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2287 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
9c915a8c 2288 sgl_ptr->Flags = 0;
c365178f 2289 if (instance->adapter_type >= INVADER_SERIES)
36807e67
AR
2290 if (i == sge_count - 1)
2291 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
9c915a8c 2292 sgl_ptr++;
9c915a8c
AR
2293 sg_processed = i + 1;
2294
2295 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2296 (sge_count > fusion->max_sge_in_main_msg)) {
2297
2298 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
c365178f 2299 if (instance->adapter_type >= INVADER_SERIES) {
94cd65dd
SS
2300 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2301 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2302 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
36807e67
AR
2303 cmd->io_request->ChainOffset =
2304 fusion->
2305 chain_offset_io_request;
2306 else
2307 cmd->io_request->ChainOffset = 0;
2308 } else
2309 cmd->io_request->ChainOffset =
2310 fusion->chain_offset_io_request;
2311
9c915a8c
AR
2312 sg_chain = sgl_ptr;
2313 /* Prepare chain element */
2314 sg_chain->NextChainOffset = 0;
c365178f 2315 if (instance->adapter_type >= INVADER_SERIES)
36807e67
AR
2316 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2317 else
2318 sg_chain->Flags =
2319 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2320 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
94cd65dd
SS
2321 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2322 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
9c915a8c
AR
2323
2324 sgl_ptr =
2325 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
bd5f9484 2326 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
9c915a8c
AR
2327 }
2328 }
33203bc4
S
2329}
2330
2331/**
2332 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2333 * @scp: SCSI command pointer
2334 * @instance: Soft instance of controller
2335 * @cmd: Fusion command pointer
2336 *
2337 * This function will build sgls based on device type.
2338 * For nvme drives, there is different way of building sgls in nvme native
2339 * format- PRPs(Physical Region Page).
2340 *
2341 * Returns the number of sg lists actually used, zero if the sg lists
2342 * is NULL, or -ENOMEM if the mapping failed
2343 */
2344static
2345int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2346 struct megasas_cmd_fusion *cmd)
2347{
2348 int sge_count;
2349 bool build_prp = false;
2350 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2351
2352 sge_count = scsi_dma_map(scp);
2353
2354 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2355 return sge_count;
2356
2357 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2358 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2359 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2360 (cmd->pd_interface == NVME_PD))
2361 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2362 cmd, sge_count);
2363
2364 if (!build_prp)
2365 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2366 cmd, sge_count);
9c915a8c
AR
2367
2368 return sge_count;
2369}
2370
2371/**
2372 * megasas_set_pd_lba - Sets PD LBA
2373 * @cdb: CDB
2374 * @cdb_len: cdb length
2375 * @start_blk: Start block of IO
2376 *
2377 * Used to set the PD LBA in CDB for FP IOs
2378 */
e45ab43b 2379static void
9c915a8c
AR
2380megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2381 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
51087a86 2382 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
9c915a8c
AR
2383{
2384 struct MR_LD_RAID *raid;
d2d0358b 2385 u16 ld;
9c915a8c
AR
2386 u64 start_blk = io_info->pdBlock;
2387 u8 *cdb = io_request->CDB.CDB32;
2388 u32 num_blocks = io_info->numBlocks;
495c5609 2389 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
9c915a8c
AR
2390
2391 /* Check if T10 PI (DIF) is enabled for this LD */
2392 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2393 raid = MR_LdRaidGet(ld, local_map_ptr);
2394 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2395 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2396 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2397 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2398
60ee6529 2399 if (scp->sc_data_direction == DMA_FROM_DEVICE)
9c915a8c
AR
2400 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2401 else
2402 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2403 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2404
2405 /* LBA */
2406 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2407 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2408 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2409 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2410 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2411 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2412 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2413 cdb[19] = (u8)(start_blk & 0xff);
2414
2415 /* Logical block reference tag */
2416 io_request->CDB.EEDP32.PrimaryReferenceTag =
2417 cpu_to_be32(ref_tag);
6e755ddc 2418 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
94cd65dd 2419 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
9c915a8c
AR
2420
2421 /* Transfer length */
2422 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2423 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2424 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2425 cdb[31] = (u8)(num_blocks & 0xff);
2426
2427 /* set SCSI IO EEDPFlags */
60ee6529 2428 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
94cd65dd 2429 io_request->EEDPFlags = cpu_to_le16(
9c915a8c
AR
2430 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2431 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2432 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2433 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
45d44603 2434 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
94cd65dd 2435 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
9c915a8c 2436 } else {
94cd65dd 2437 io_request->EEDPFlags = cpu_to_le16(
9c915a8c 2438 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
94cd65dd 2439 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
9c915a8c 2440 }
94cd65dd
SS
2441 io_request->Control |= cpu_to_le32((0x4 << 26));
2442 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
9c915a8c
AR
2443 } else {
2444 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2445 if (((cdb_len == 12) || (cdb_len == 16)) &&
2446 (start_blk <= 0xffffffff)) {
2447 if (cdb_len == 16) {
2448 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2449 flagvals = cdb[1];
2450 groupnum = cdb[14];
2451 control = cdb[15];
2452 } else {
2453 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2454 flagvals = cdb[1];
2455 groupnum = cdb[10];
2456 control = cdb[11];
2457 }
2458
2459 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2460
2461 cdb[0] = opcode;
2462 cdb[1] = flagvals;
2463 cdb[6] = groupnum;
2464 cdb[9] = control;
2465
2466 /* Transfer length */
2467 cdb[8] = (u8)(num_blocks & 0xff);
2468 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2469
94cd65dd 2470 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
9c915a8c 2471 cdb_len = 10;
495c5609
AR
2472 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2473 /* Convert to 16 byte CDB for large LBA's */
2474 switch (cdb_len) {
2475 case 6:
2476 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2477 control = cdb[5];
2478 break;
2479 case 10:
2480 opcode =
2481 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2482 flagvals = cdb[1];
2483 groupnum = cdb[6];
2484 control = cdb[9];
2485 break;
2486 case 12:
2487 opcode =
2488 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2489 flagvals = cdb[1];
2490 groupnum = cdb[10];
2491 control = cdb[11];
2492 break;
2493 }
2494
2495 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2496
2497 cdb[0] = opcode;
2498 cdb[1] = flagvals;
2499 cdb[14] = groupnum;
2500 cdb[15] = control;
2501
2502 /* Transfer length */
2503 cdb[13] = (u8)(num_blocks & 0xff);
2504 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2505 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2506 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2507
94cd65dd 2508 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
495c5609 2509 cdb_len = 16;
9c915a8c
AR
2510 }
2511
2512 /* Normal case, just load LBA here */
2513 switch (cdb_len) {
2514 case 6:
2515 {
2516 u8 val = cdb[1] & 0xE0;
2517 cdb[3] = (u8)(start_blk & 0xff);
2518 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2519 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2520 break;
2521 }
2522 case 10:
2523 cdb[5] = (u8)(start_blk & 0xff);
2524 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2525 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2526 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2527 break;
2528 case 12:
2529 cdb[5] = (u8)(start_blk & 0xff);
2530 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2531 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2532 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2533 break;
2534 case 16:
2535 cdb[9] = (u8)(start_blk & 0xff);
2536 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2537 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2538 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2539 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2540 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2541 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2542 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2543 break;
2544 }
2545 }
2546}
2547
fdd84e25
SC
2548/**
2549 * megasas_stream_detect - stream detection on read and and write IOs
2550 * @instance: Adapter soft state
2551 * @cmd: Command to be prepared
2552 * @io_info: IO Request info
2553 *
2554 */
2555
2556/** stream detection on read and and write IOs */
2557static void megasas_stream_detect(struct megasas_instance *instance,
41064f1b
S
2558 struct megasas_cmd_fusion *cmd,
2559 struct IO_REQUEST_INFO *io_info)
fdd84e25
SC
2560{
2561 struct fusion_context *fusion = instance->ctrl_context;
2562 u32 device_id = io_info->ldTgtId;
2563 struct LD_STREAM_DETECT *current_ld_sd
2564 = fusion->stream_detect_by_ld[device_id];
2565 u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2566 u32 shifted_values, unshifted_values;
2567 u32 index_value_mask, shifted_values_mask;
2568 int i;
2569 bool is_read_ahead = false;
2570 struct STREAM_DETECT *current_sd;
2571 /* find possible stream */
2572 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
41064f1b
S
2573 stream_num = (*track_stream >>
2574 (i * BITS_PER_INDEX_STREAM)) &
fdd84e25
SC
2575 STREAM_MASK;
2576 current_sd = &current_ld_sd->stream_track[stream_num];
41064f1b
S
2577 /* if we found a stream, update the raid
2578 * context and also update the mruBitMap
2579 */
2580 /* boundary condition */
2581 if ((current_sd->next_seq_lba) &&
2582 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2583 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2584 (current_sd->is_read == io_info->isRead)) {
2585
2586 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2587 ((!io_info->isRead) || (!is_read_ahead)))
2588 /*
2589 * Once the API availible we need to change this.
2590 * At this point we are not allowing any gap
2591 */
2592 continue;
2593
2594 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2595 current_sd->next_seq_lba =
2596 io_info->ldStartBlock + io_info->numBlocks;
fdd84e25 2597 /*
41064f1b 2598 * update the mruBitMap LRU
fdd84e25 2599 */
41064f1b
S
2600 shifted_values_mask =
2601 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2602 shifted_values = ((*track_stream & shifted_values_mask)
2603 << BITS_PER_INDEX_STREAM);
2604 index_value_mask =
2605 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2606 unshifted_values =
2607 *track_stream & ~(shifted_values_mask |
2608 index_value_mask);
2609 *track_stream =
2610 unshifted_values | shifted_values | stream_num;
2611 return;
fdd84e25 2612 }
fdd84e25
SC
2613 }
2614 /*
2615 * if we did not find any stream, create a new one
2616 * from the least recently used
2617 */
41064f1b
S
2618 stream_num = (*track_stream >>
2619 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2620 STREAM_MASK;
fdd84e25
SC
2621 current_sd = &current_ld_sd->stream_track[stream_num];
2622 current_sd->is_read = io_info->isRead;
2623 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
41064f1b 2624 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
fdd84e25 2625 return;
fdd84e25
SC
2626}
2627
f6c0d55c
S
2628/**
2629 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2630 * affinity (cpu of the controller) and raid_flags in the raid context
2631 * based on IO type.
2632 *
2633 * @praid_context: IO RAID context
2634 * @raid: LD raid map
2635 * @fp_possible: Is fast path possible?
2636 * @is_read: Is read IO?
2637 *
2638 */
2639static void
49f2bf10
CP
2640megasas_set_raidflag_cpu_affinity(struct fusion_context *fusion,
2641 union RAID_CONTEXT_UNION *praid_context,
2642 struct MR_LD_RAID *raid, bool fp_possible,
2643 u8 is_read, u32 scsi_buff_len)
f6c0d55c
S
2644{
2645 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2646 struct RAID_CONTEXT_G35 *rctx_g35;
2647
2648 rctx_g35 = &praid_context->raid_context_g35;
2649 if (fp_possible) {
2650 if (is_read) {
2651 if ((raid->cpuAffinity.pdRead.cpu0) &&
2652 (raid->cpuAffinity.pdRead.cpu1))
2653 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2654 else if (raid->cpuAffinity.pdRead.cpu1)
2655 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2656 } else {
2657 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2658 (raid->cpuAffinity.pdWrite.cpu1))
2659 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2660 else if (raid->cpuAffinity.pdWrite.cpu1)
2661 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2662 /* Fast path cache by pass capable R0/R1 VD */
2663 if ((raid->level <= 1) &&
2664 (raid->capability.fp_cache_bypass_capable)) {
a174118b
S
2665 rctx_g35->routing_flags |=
2666 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
f6c0d55c
S
2667 rctx_g35->raid_flags =
2668 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2669 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2670 }
2671 }
2672 } else {
2673 if (is_read) {
2674 if ((raid->cpuAffinity.ldRead.cpu0) &&
2675 (raid->cpuAffinity.ldRead.cpu1))
2676 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2677 else if (raid->cpuAffinity.ldRead.cpu1)
2678 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2679 } else {
2680 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2681 (raid->cpuAffinity.ldWrite.cpu1))
2682 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2683 else if (raid->cpuAffinity.ldWrite.cpu1)
2684 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2685
a174118b 2686 if (is_stream_detected(rctx_g35) &&
874d025d 2687 ((raid->level == 5) || (raid->level == 6)) &&
f6c0d55c
S
2688 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2689 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2690 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2691 }
2692 }
2693
a174118b
S
2694 rctx_g35->routing_flags |=
2695 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
a48ba0ec
S
2696
2697 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2698 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2699 * IO Subtype is not bitmap.
2700 */
49f2bf10
CP
2701 if ((fusion->pcie_bw_limitation) && (raid->level == 1) && (!is_read) &&
2702 (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)) {
2703 praid_context->raid_context_g35.raid_flags =
2704 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2705 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
a48ba0ec 2706 }
f6c0d55c
S
2707}
2708
9c915a8c
AR
2709/**
2710 * megasas_build_ldio_fusion - Prepares IOs to devices
2711 * @instance: Adapter soft state
2712 * @scp: SCSI command
2713 * @cmd: Command to be prepared
2714 *
2715 * Prepares the io_request and chain elements (sg_frame) for IO
2716 * The IO can be for PD (Fast Path) or LD
2717 */
e45ab43b 2718static void
9c915a8c
AR
2719megasas_build_ldio_fusion(struct megasas_instance *instance,
2720 struct scsi_cmnd *scp,
2721 struct megasas_cmd_fusion *cmd)
2722{
f6c0d55c
S
2723 bool fp_possible;
2724 u16 ld;
2725 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2726 u32 scsi_buff_len;
9c915a8c 2727 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
9c915a8c
AR
2728 struct IO_REQUEST_INFO io_info;
2729 struct fusion_context *fusion;
51087a86 2730 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
21c9e160 2731 u8 *raidLUN;
fdd84e25 2732 unsigned long spinlock_flags;
f6c0d55c
S
2733 struct MR_LD_RAID *raid = NULL;
2734 struct MR_PRIV_DEVICE *mrdev_priv;
9e77018e
S
2735 struct RAID_CONTEXT *rctx;
2736 struct RAID_CONTEXT_G35 *rctx_g35;
9c915a8c 2737
4a5c814d 2738 device_id = MEGASAS_DEV_INDEX(scp);
9c915a8c
AR
2739
2740 fusion = instance->ctrl_context;
2741
2742 io_request = cmd->io_request;
9e77018e
S
2743 rctx = &io_request->RaidContext.raid_context;
2744 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2745
2746 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2747 rctx->status = 0;
2748 rctx->ex_status = 0;
9c915a8c 2749
9c915a8c
AR
2750 start_lba_lo = 0;
2751 start_lba_hi = 0;
f6c0d55c 2752 fp_possible = false;
9c915a8c
AR
2753
2754 /*
2755 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2756 */
2757 if (scp->cmd_len == 6) {
f9eff815 2758 datalength = (u32) scp->cmnd[4];
9c915a8c
AR
2759 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2760 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2761
2762 start_lba_lo &= 0x1FFFFF;
2763 }
2764
2765 /*
2766 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2767 */
2768 else if (scp->cmd_len == 10) {
f9eff815 2769 datalength = (u32) scp->cmnd[8] |
9c915a8c
AR
2770 ((u32) scp->cmnd[7] << 8);
2771 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2772 ((u32) scp->cmnd[3] << 16) |
2773 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2774 }
2775
2776 /*
2777 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2778 */
2779 else if (scp->cmd_len == 12) {
f9eff815 2780 datalength = ((u32) scp->cmnd[6] << 24) |
9c915a8c
AR
2781 ((u32) scp->cmnd[7] << 16) |
2782 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2783 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2784 ((u32) scp->cmnd[3] << 16) |
2785 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2786 }
2787
2788 /*
2789 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2790 */
2791 else if (scp->cmd_len == 16) {
f9eff815 2792 datalength = ((u32) scp->cmnd[10] << 24) |
9c915a8c
AR
2793 ((u32) scp->cmnd[11] << 16) |
2794 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2795 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2796 ((u32) scp->cmnd[7] << 16) |
2797 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2798
2799 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2800 ((u32) scp->cmnd[3] << 16) |
2801 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2802 }
2803
2804 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2805 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
f9eff815 2806 io_info.numBlocks = datalength;
9c915a8c 2807 io_info.ldTgtId = device_id;
8bf7c65d 2808 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
f6c0d55c
S
2809 scsi_buff_len = scsi_bufflen(scp);
2810 io_request->DataLength = cpu_to_le32(scsi_buff_len);
f39e5e52 2811 io_info.data_arms = 1;
9c915a8c 2812
60ee6529 2813 if (scp->sc_data_direction == DMA_FROM_DEVICE)
9c915a8c
AR
2814 io_info.isRead = 1;
2815
51087a86 2816 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
d889344e 2817 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
9c915a8c 2818
d2d0358b
S
2819 if (ld < instance->fw_supported_vd_count)
2820 raid = MR_LdRaidGet(ld, local_map_ptr);
2821
2822 if (!raid || (!fusion->fast_path_io)) {
9e77018e 2823 rctx->reg_lock_flags = 0;
f6c0d55c 2824 fp_possible = false;
9c915a8c 2825 } else {
9e77018e 2826 if (MR_BuildRaidContext(instance, &io_info, rctx,
21c9e160 2827 local_map_ptr, &raidLUN))
f6c0d55c 2828 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
9c915a8c
AR
2829 }
2830
299ee426 2831 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
f39e5e52
CP
2832 atomic_read(&scp->device->device_busy) >
2833 (io_info.data_arms * MR_DEVICE_HIGH_IOPS_DEPTH))
2834 cmd->request_desc->SCSIIO.MSIxIndex =
2835 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
2836 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
2837 else if (instance->msix_load_balance)
1d15d909
S
2838 cmd->request_desc->SCSIIO.MSIxIndex =
2839 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
2840 instance->msix_vectors));
2841 else
2842 cmd->request_desc->SCSIIO.MSIxIndex =
2843 instance->reply_map[raw_smp_processor_id()];
c8e858fe 2844
630d42b7 2845 if (instance->adapter_type >= VENTURA_SERIES) {
a48ba0ec
S
2846 /* FP for Optimal raid level 1.
2847 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2848 * are built by the driver as LD I/Os.
2849 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2850 * (there is never a reason to process these as buffered writes)
2851 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2852 * with the SLD bit asserted.
2853 */
8bf7c65d 2854 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
f6c0d55c
S
2855 mrdev_priv = scp->device->hostdata;
2856
2857 if (atomic_inc_return(&instance->fw_outstanding) >
2858 (instance->host->can_queue)) {
2859 fp_possible = false;
2860 atomic_dec(&instance->fw_outstanding);
49f2bf10
CP
2861 } else if (fusion->pcie_bw_limitation &&
2862 ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2863 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0))) {
a48ba0ec
S
2864 fp_possible = false;
2865 atomic_dec(&instance->fw_outstanding);
2866 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2867 atomic_set(&mrdev_priv->r1_ldio_hint,
2868 instance->r1_ldio_hint_default);
f6c0d55c
S
2869 }
2870 }
2871
4959e61b
S
2872 if (!fp_possible ||
2873 (io_info.isRead && io_info.ra_capable)) {
2874 spin_lock_irqsave(&instance->stream_lock,
2875 spinlock_flags);
2876 megasas_stream_detect(instance, cmd, &io_info);
2877 spin_unlock_irqrestore(&instance->stream_lock,
2878 spinlock_flags);
2879 /* In ventura if stream detected for a read and it is
2880 * read ahead capable make this IO as LDIO
2881 */
9e77018e 2882 if (is_stream_detected(rctx_g35))
4959e61b
S
2883 fp_possible = false;
2884 }
2885
f6c0d55c
S
2886 /* If raid is NULL, set CPU affinity to default CPU0 */
2887 if (raid)
49f2bf10 2888 megasas_set_raidflag_cpu_affinity(fusion, &io_request->RaidContext,
a48ba0ec
S
2889 raid, fp_possible, io_info.isRead,
2890 scsi_buff_len);
f6c0d55c 2891 else
9e77018e 2892 rctx_g35->routing_flags |=
a174118b 2893 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
f6c0d55c 2894 }
d889344e 2895
9c915a8c
AR
2896 if (fp_possible) {
2897 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2898 local_map_ptr, start_lba_lo);
9c915a8c
AR
2899 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2900 cmd->request_desc->SCSIIO.RequestFlags =
2c048351 2901 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
9c915a8c 2902 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
c365178f 2903 if (instance->adapter_type == INVADER_SERIES) {
9e77018e
S
2904 rctx->type = MPI2_TYPE_CUDA;
2905 rctx->nseg = 0x1;
94cd65dd 2906 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
9e77018e 2907 rctx->reg_lock_flags |=
36807e67
AR
2908 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2909 MR_RL_FLAGS_SEQ_NUM_ENABLE);
630d42b7 2910 } else if (instance->adapter_type >= VENTURA_SERIES) {
9e77018e
S
2911 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2912 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2913 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
d889344e 2914 io_request->IoFlags |=
a174118b 2915 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
36807e67 2916 }
5fc499b6
S
2917 if (fusion->load_balance_info &&
2918 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2919 (io_info.isRead)) {
9c915a8c 2920 io_info.devHandle =
d2552ebe 2921 get_updated_dev_handle(instance,
9c915a8c 2922 &fusion->load_balance_info[device_id],
33203bc4 2923 &io_info, local_map_ptr);
9c915a8c 2924 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
d2552ebe 2925 cmd->pd_r1_lb = io_info.pd_after_lb;
630d42b7 2926 if (instance->adapter_type >= VENTURA_SERIES)
9e77018e 2927 rctx_g35->span_arm = io_info.span_arm;
d889344e 2928 else
9e77018e 2929 rctx->span_arm = io_info.span_arm;
d889344e 2930
9c915a8c
AR
2931 } else
2932 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
5765c5b8 2933
630d42b7 2934 if (instance->adapter_type >= VENTURA_SERIES)
69c337c0 2935 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
8bf7c65d
S
2936 else
2937 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
69c337c0 2938
5765c5b8 2939 if ((raidLUN[0] == 1) &&
75b96061 2940 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
5765c5b8
SS
2941 instance->dev_handle = !(instance->dev_handle);
2942 io_info.devHandle =
2943 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2944 }
2945
9c915a8c
AR
2946 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2947 io_request->DevHandle = io_info.devHandle;
33203bc4 2948 cmd->pd_interface = io_info.pd_interface;
21c9e160
AR
2949 /* populate the LUN field */
2950 memcpy(io_request->LUN, raidLUN, 8);
9c915a8c 2951 } else {
9e77018e 2952 rctx->timeout_value =
94cd65dd 2953 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
9c915a8c
AR
2954 cmd->request_desc->SCSIIO.RequestFlags =
2955 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2956 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
c365178f 2957 if (instance->adapter_type == INVADER_SERIES) {
8f05024c 2958 if (io_info.do_fp_rlbypass ||
9e77018e 2959 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
36807e67
AR
2960 cmd->request_desc->SCSIIO.RequestFlags =
2961 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2962 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
9e77018e
S
2963 rctx->type = MPI2_TYPE_CUDA;
2964 rctx->reg_lock_flags |=
36807e67 2965 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
9e77018e
S
2966 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2967 rctx->nseg = 0x1;
630d42b7 2968 } else if (instance->adapter_type >= VENTURA_SERIES) {
9e77018e
S
2969 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2970 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2971 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
36807e67
AR
2972 }
2973 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
94cd65dd 2974 io_request->DevHandle = cpu_to_le16(device_id);
d889344e 2975
9c915a8c
AR
2976 } /* Not FP */
2977}
2978
2979/**
4a5c814d 2980 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
9c915a8c
AR
2981 * @instance: Adapter soft state
2982 * @scp: SCSI command
2983 * @cmd: Command to be prepared
2984 *
4a5c814d 2985 * Prepares the io_request frame for non-rw io cmds for vd.
9c915a8c 2986 */
4a5c814d
SS
2987static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2988 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
9c915a8c
AR
2989{
2990 u32 device_id;
2991 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
25fb13dd 2992 u16 ld;
51087a86 2993 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
9c915a8c 2994 struct fusion_context *fusion = instance->ctrl_context;
21c9e160 2995 u8 span, physArm;
9ab9ed38 2996 __le16 devHandle;
d2d0358b 2997 u32 arRef, pd;
21c9e160
AR
2998 struct MR_LD_RAID *raid;
2999 struct RAID_CONTEXT *pRAID_Context;
4a5c814d 3000 u8 fp_possible = 1;
9c915a8c
AR
3001
3002 io_request = cmd->io_request;
4a5c814d 3003 device_id = MEGASAS_DEV_INDEX(scmd);
51087a86 3004 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
94cd65dd 3005 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4a5c814d 3006 /* get RAID_Context pointer */
fdd84e25 3007 pRAID_Context = &io_request->RaidContext.raid_context;
4a5c814d 3008 /* Check with FW team */
d889344e
SC
3009 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3010 pRAID_Context->reg_lock_row_lba = 0;
3011 pRAID_Context->reg_lock_length = 0;
21c9e160 3012
4a5c814d
SS
3013 if (fusion->fast_path_io && (
3014 device_id < instance->fw_supported_vd_count)) {
ab2f0608 3015
21c9e160 3016 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
c7a082e4 3017 if (ld >= instance->fw_supported_vd_count - 1)
4a5c814d 3018 fp_possible = 0;
d2d0358b
S
3019 else {
3020 raid = MR_LdRaidGet(ld, local_map_ptr);
3021 if (!(raid->capability.fpNonRWCapable))
3022 fp_possible = 0;
3023 }
4a5c814d
SS
3024 } else
3025 fp_possible = 0;
21c9e160 3026
4a5c814d
SS
3027 if (!fp_possible) {
3028 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3029 io_request->DevHandle = cpu_to_le16(device_id);
3030 io_request->LUN[1] = scmd->device->lun;
d889344e 3031 pRAID_Context->timeout_value =
4a5c814d
SS
3032 cpu_to_le16 (scmd->request->timeout / HZ);
3033 cmd->request_desc->SCSIIO.RequestFlags =
3034 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3035 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3036 } else {
21c9e160
AR
3037
3038 /* set RAID context values */
d889344e 3039 pRAID_Context->config_seq_num = raid->seqNum;
630d42b7 3040 if (instance->adapter_type < VENTURA_SERIES)
d889344e
SC
3041 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
3042 pRAID_Context->timeout_value =
3043 cpu_to_le16(raid->fpIoTimeoutForLd);
21c9e160
AR
3044
3045 /* get the DevHandle for the PD (since this is
3046 fpNonRWCapable, this is a single disk RAID0) */
3047 span = physArm = 0;
3048 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
3049 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
3050 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
3051
3052 /* build request descriptor */
3053 cmd->request_desc->SCSIIO.RequestFlags =
2c048351 3054 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
4a5c814d 3055 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
21c9e160
AR
3056 cmd->request_desc->SCSIIO.DevHandle = devHandle;
3057
3058 /* populate the LUN field */
3059 memcpy(io_request->LUN, raid->LUN, 8);
3060
3061 /* build the raidScsiIO structure */
3062 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3063 io_request->DevHandle = devHandle;
4a5c814d
SS
3064 }
3065}
21c9e160 3066
4a5c814d
SS
3067/**
3068 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3069 * @instance: Adapter soft state
3070 * @scp: SCSI command
3071 * @cmd: Command to be prepared
3072 * @fp_possible: parameter to detect fast path or firmware path io.
3073 *
3074 * Prepares the io_request frame for rw/non-rw io cmds for syspds
3075 */
3076static void
3077megasas_build_syspd_fusion(struct megasas_instance *instance,
1d6dbd17
S
3078 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
3079 bool fp_possible)
4a5c814d
SS
3080{
3081 u32 device_id;
3082 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
3083 u16 pd_index = 0;
3084 u16 os_timeout_value;
3085 u16 timeout_limit;
3086 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
3087 struct RAID_CONTEXT *pRAID_Context;
3761cb4c 3088 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
33203bc4 3089 struct MR_PRIV_DEVICE *mr_device_priv_data;
4a5c814d 3090 struct fusion_context *fusion = instance->ctrl_context;
3761cb4c 3091 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
4a5c814d
SS
3092
3093 device_id = MEGASAS_DEV_INDEX(scmd);
3094 pd_index = MEGASAS_PD_INDEX(scmd);
3095 os_timeout_value = scmd->request->timeout / HZ;
33203bc4
S
3096 mr_device_priv_data = scmd->device->hostdata;
3097 cmd->pd_interface = mr_device_priv_data->interface_type;
21c9e160 3098
4a5c814d
SS
3099 io_request = cmd->io_request;
3100 /* get RAID_Context pointer */
fdd84e25 3101 pRAID_Context = &io_request->RaidContext.raid_context;
d889344e
SC
3102 pRAID_Context->reg_lock_flags = 0;
3103 pRAID_Context->reg_lock_row_lba = 0;
3104 pRAID_Context->reg_lock_length = 0;
4a5c814d
SS
3105 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3106 io_request->LUN[1] = scmd->device->lun;
d889344e 3107 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
4a5c814d
SS
3108 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
3109
3761cb4c 3110 /* If FW supports PD sequence number */
59db5a93
CP
3111 if (instance->support_seqnum_jbod_fp) {
3112 if (instance->use_seqnum_jbod_fp &&
3113 instance->pd_list[pd_index].driveType == TYPE_DISK) {
3114
3115 /* More than 256 PD/JBOD support for Ventura */
3116 if (instance->support_morethan256jbod)
3117 pRAID_Context->virtual_disk_tgt_id =
3118 pd_sync->seq[pd_index].pd_target_id;
3119 else
3120 pRAID_Context->virtual_disk_tgt_id =
3121 cpu_to_le16(device_id +
3122 (MAX_PHYSICAL_DEVICES - 1));
3123 pRAID_Context->config_seq_num =
3124 pd_sync->seq[pd_index].seqNum;
3125 io_request->DevHandle =
3126 pd_sync->seq[pd_index].devHandle;
3127 if (instance->adapter_type >= VENTURA_SERIES) {
3128 io_request->RaidContext.raid_context_g35.routing_flags |=
3129 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3130 io_request->RaidContext.raid_context_g35.nseg_type |=
3131 (1 << RAID_CONTEXT_NSEG_SHIFT);
3132 io_request->RaidContext.raid_context_g35.nseg_type |=
3133 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3134 } else {
3135 pRAID_Context->type = MPI2_TYPE_CUDA;
3136 pRAID_Context->nseg = 0x1;
3137 pRAID_Context->reg_lock_flags |=
3138 (MR_RL_FLAGS_SEQ_NUM_ENABLE |
3139 MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3140 }
a174118b 3141 } else {
59db5a93
CP
3142 pRAID_Context->virtual_disk_tgt_id =
3143 cpu_to_le16(device_id +
3144 (MAX_PHYSICAL_DEVICES - 1));
3145 pRAID_Context->config_seq_num = 0;
3146 io_request->DevHandle = cpu_to_le16(0xFFFF);
a174118b 3147 }
3761cb4c 3148 } else {
d889344e
SC
3149 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3150 pRAID_Context->config_seq_num = 0;
59db5a93
CP
3151
3152 if (fusion->fast_path_io) {
3153 local_map_ptr =
3154 fusion->ld_drv_map[(instance->map_id & 1)];
3155 io_request->DevHandle =
3156 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3157 } else {
3158 io_request->DevHandle = cpu_to_le16(0xFFFF);
3159 }
3761cb4c 3160 }
4a5c814d
SS
3161
3162 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
4a5c814d 3163
299ee426 3164 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
f39e5e52
CP
3165 atomic_read(&scmd->device->device_busy) > MR_DEVICE_HIGH_IOPS_DEPTH)
3166 cmd->request_desc->SCSIIO.MSIxIndex =
3167 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
3168 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
3169 else if (instance->msix_load_balance)
1d15d909
S
3170 cmd->request_desc->SCSIIO.MSIxIndex =
3171 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
3172 instance->msix_vectors));
3173 else
3174 cmd->request_desc->SCSIIO.MSIxIndex =
3175 instance->reply_map[raw_smp_processor_id()];
4a5c814d
SS
3176
3177 if (!fp_possible) {
3178 /* system pd firmware path */
9c915a8c 3179 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
9c915a8c
AR
3180 cmd->request_desc->SCSIIO.RequestFlags =
3181 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
4a5c814d 3182 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
d889344e
SC
3183 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3184 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
4a5c814d 3185 } else {
3239b8cd
S
3186 if (os_timeout_value)
3187 os_timeout_value++;
3188
4a5c814d
SS
3189 /* system pd Fast Path */
3190 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4a5c814d
SS
3191 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3192 255 : 0xFFFF;
d889344e 3193 pRAID_Context->timeout_value =
4a5c814d
SS
3194 cpu_to_le16((os_timeout_value > timeout_limit) ?
3195 timeout_limit : os_timeout_value);
c365178f 3196 if (instance->adapter_type >= INVADER_SERIES)
4a5c814d
SS
3197 io_request->IoFlags |=
3198 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
d5573584 3199
4a5c814d 3200 cmd->request_desc->SCSIIO.RequestFlags =
2c048351 3201 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
4a5c814d 3202 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
9c915a8c 3203 }
9c915a8c
AR
3204}
3205
3206/**
3207 * megasas_build_io_fusion - Prepares IOs to devices
3208 * @instance: Adapter soft state
3209 * @scp: SCSI command
3210 * @cmd: Command to be prepared
3211 *
3212 * Invokes helper functions to prepare request frames
3213 * and sets flags appropriate for IO/Non-IO cmd
3214 */
e45ab43b 3215static int
9c915a8c
AR
3216megasas_build_io_fusion(struct megasas_instance *instance,
3217 struct scsi_cmnd *scp,
3218 struct megasas_cmd_fusion *cmd)
3219{
33203bc4 3220 int sge_count;
4a5c814d 3221 u8 cmd_type;
9c915a8c 3222 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1d6dbd17
S
3223 struct MR_PRIV_DEVICE *mr_device_priv_data;
3224 mr_device_priv_data = scp->device->hostdata;
9c915a8c 3225
9c915a8c 3226 /* Zero out some fields so they don't get reused */
9cb78c16 3227 memset(io_request->LUN, 0x0, 8);
9c915a8c
AR
3228 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3229 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3230 io_request->EEDPFlags = 0;
3231 io_request->Control = 0;
3232 io_request->EEDPBlockSize = 0;
36807e67 3233 io_request->ChainOffset = 0;
d889344e
SC
3234 io_request->RaidContext.raid_context.raid_flags = 0;
3235 io_request->RaidContext.raid_context.type = 0;
fdd84e25 3236 io_request->RaidContext.raid_context.nseg = 0;
9c915a8c
AR
3237
3238 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3239 /*
3240 * Just the CDB length,rest of the Flags are zero
3241 * This will be modified for FP in build_ldio_fusion
3242 */
94cd65dd 3243 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
9c915a8c 3244
4a5c814d
SS
3245 switch (cmd_type = megasas_cmd_type(scp)) {
3246 case READ_WRITE_LDIO:
9c915a8c 3247 megasas_build_ldio_fusion(instance, scp, cmd);
4a5c814d
SS
3248 break;
3249 case NON_READ_WRITE_LDIO:
3250 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3251 break;
3252 case READ_WRITE_SYSPDIO:
1d6dbd17
S
3253 megasas_build_syspd_fusion(instance, scp, cmd, true);
3254 break;
4a5c814d 3255 case NON_READ_WRITE_SYSPDIO:
1d6dbd17
S
3256 if (instance->secure_jbod_support ||
3257 mr_device_priv_data->is_tm_capable)
3258 megasas_build_syspd_fusion(instance, scp, cmd, false);
4a5c814d 3259 else
1d6dbd17 3260 megasas_build_syspd_fusion(instance, scp, cmd, true);
4a5c814d
SS
3261 break;
3262 default:
3263 break;
3264 }
9c915a8c
AR
3265
3266 /*
3267 * Construct SGL
3268 */
3269
33203bc4 3270 sge_count = megasas_make_sgl(instance, scp, cmd);
9c915a8c 3271
33203bc4
S
3272 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3273 dev_err(&instance->pdev->dev,
3274 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3275 __func__, __LINE__, sge_count, instance->max_num_sge);
9c915a8c
AR
3276 return 1;
3277 }
3278
630d42b7 3279 if (instance->adapter_type >= VENTURA_SERIES) {
a174118b
S
3280 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3281 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3282 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3283 } else {
d889344e
SC
3284 /* numSGE store lower 8 bit of sge_count.
3285 * numSGEExt store higher 8 bit of sge_count
3286 */
3287 io_request->RaidContext.raid_context.num_sge = sge_count;
3288 io_request->RaidContext.raid_context.num_sge_ext =
3289 (u8)(sge_count >> 8);
3290 }
9c915a8c 3291
94cd65dd 3292 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
9c915a8c 3293
60ee6529 3294 if (scp->sc_data_direction == DMA_TO_DEVICE)
94cd65dd 3295 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
60ee6529 3296 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
94cd65dd 3297 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
9c915a8c
AR
3298
3299 io_request->SGLOffset0 =
3300 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3301
107a60dd
S
3302 io_request->SenseBufferLowAddress =
3303 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
9c915a8c
AR
3304 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3305
3306 cmd->scmd = scp;
3307 scp->SCp.ptr = (char *)cmd;
3308
3309 return 0;
3310}
3311
52205ac8 3312static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
9c915a8c
AR
3313megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3314{
3315 u8 *p;
3316 struct fusion_context *fusion;
3317
9c915a8c 3318 fusion = instance->ctrl_context;
69c337c0
SC
3319 p = fusion->req_frames_desc +
3320 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
9c915a8c
AR
3321
3322 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3323}
3324
69c337c0
SC
3325
3326/* megasas_prepate_secondRaid1_IO
3327 * It prepares the raid 1 second IO
3328 */
e45ab43b
Y
3329static void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3330 struct megasas_cmd_fusion *cmd,
3331 struct megasas_cmd_fusion *r1_cmd)
69c337c0
SC
3332{
3333 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3334 struct fusion_context *fusion;
69c337c0
SC
3335 fusion = instance->ctrl_context;
3336 req_desc = cmd->request_desc;
8bf7c65d
S
3337 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3338 memcpy(r1_cmd->io_request, cmd->io_request,
3339 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3340 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3341 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3342 /*sense buffer is different for r1 command*/
3343 r1_cmd->io_request->SenseBufferLowAddress =
107a60dd 3344 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
8bf7c65d
S
3345 r1_cmd->scmd = cmd->scmd;
3346 req_desc2 = megasas_get_request_descriptor(instance,
3347 (r1_cmd->index - 1));
3348 req_desc2->Words = 0;
3349 r1_cmd->request_desc = req_desc2;
3350 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3351 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3352 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3353 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3354 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
7fc55700 3355 cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
8bf7c65d 3356 cpu_to_le16(r1_cmd->index);
7fc55700 3357 r1_cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
8bf7c65d
S
3358 cpu_to_le16(cmd->index);
3359 /*MSIxIndex of both commands request descriptors should be same*/
3360 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3361 cmd->request_desc->SCSIIO.MSIxIndex;
3362 /*span arm is different for r1 cmd*/
3363 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
69c337c0 3364 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
69c337c0
SC
3365}
3366
9c915a8c
AR
3367/**
3368 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3369 * issuing non IOCTL cmd
3370 * @instance: Adapter soft state
3371 * @scmd: pointer to scsi cmd from OS
3372 */
3373static u32
3374megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3375 struct scsi_cmnd *scmd)
3376{
69c337c0 3377 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
9c915a8c
AR
3378 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3379 u32 index;
9c915a8c 3380
308ec459
SS
3381 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3382 instance->ldio_threshold &&
3383 (atomic_inc_return(&instance->ldio_outstanding) >
3384 instance->ldio_threshold)) {
3385 atomic_dec(&instance->ldio_outstanding);
3386 return SCSI_MLQUEUE_DEVICE_BUSY;
3387 }
3388
69c337c0
SC
3389 if (atomic_inc_return(&instance->fw_outstanding) >
3390 instance->host->can_queue) {
69c337c0
SC
3391 atomic_dec(&instance->fw_outstanding);
3392 return SCSI_MLQUEUE_HOST_BUSY;
3393 }
3394
4026e9aa 3395 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
9c915a8c 3396
69c337c0
SC
3397 if (!cmd) {
3398 atomic_dec(&instance->fw_outstanding);
3399 return SCSI_MLQUEUE_HOST_BUSY;
3400 }
3401
9c915a8c
AR
3402 index = cmd->index;
3403
3404 req_desc = megasas_get_request_descriptor(instance, index-1);
9c915a8c
AR
3405
3406 req_desc->Words = 0;
3407 cmd->request_desc = req_desc;
9c915a8c
AR
3408
3409 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3410 megasas_return_cmd_fusion(instance, cmd);
1be18254 3411 dev_err(&instance->pdev->dev, "Error building command\n");
9c915a8c 3412 cmd->request_desc = NULL;
69c337c0 3413 atomic_dec(&instance->fw_outstanding);
f9a9dee6 3414 return SCSI_MLQUEUE_HOST_BUSY;
9c915a8c
AR
3415 }
3416
3417 req_desc = cmd->request_desc;
94cd65dd 3418 req_desc->SCSIIO.SMID = cpu_to_le16(index);
9c915a8c
AR
3419
3420 if (cmd->io_request->ChainOffset != 0 &&
3421 cmd->io_request->ChainOffset != 0xF)
1be18254 3422 dev_err(&instance->pdev->dev, "The chain offset value is not "
9c915a8c 3423 "correct : %x\n", cmd->io_request->ChainOffset);
69c337c0
SC
3424 /*
3425 * if it is raid 1/10 fp write capable.
3426 * try to get second command from pool and construct it.
3427 * From FW, it has confirmed that lba values of two PDs
3428 * corresponds to single R1/10 LD are always same
3429 *
3430 */
3431 /* driver side count always should be less than max_fw_cmds
3432 * to get new command
3433 */
8bf7c65d 3434 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
69c337c0
SC
3435 r1_cmd = megasas_get_cmd_fusion(instance,
3436 (scmd->request->tag + instance->max_fw_cmds));
3437 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3438 }
3439
18bbcabd 3440
9c915a8c
AR
3441 /*
3442 * Issue the command to the FW
3443 */
9c915a8c 3444
45b8a35e 3445 megasas_fire_cmd_fusion(instance, req_desc);
9c915a8c 3446
18bbcabd 3447 if (r1_cmd)
45b8a35e 3448 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
18bbcabd 3449
69c337c0 3450
9c915a8c
AR
3451 return 0;
3452}
3453
8bf7c65d
S
3454/**
3455 * megasas_complete_r1_command -
3456 * completes R1 FP write commands which has valid peer smid
3457 * @instance: Adapter soft state
3458 * @cmd_fusion: MPT command frame
3459 *
3460 */
3461static inline void
3462megasas_complete_r1_command(struct megasas_instance *instance,
3463 struct megasas_cmd_fusion *cmd)
3464{
3465 u8 *sense, status, ex_status;
3466 u32 data_length;
3467 u16 peer_smid;
3468 struct fusion_context *fusion;
3469 struct megasas_cmd_fusion *r1_cmd = NULL;
3470 struct scsi_cmnd *scmd_local = NULL;
3471 struct RAID_CONTEXT_G35 *rctx_g35;
3472
3473 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3474 fusion = instance->ctrl_context;
7fc55700 3475 peer_smid = le16_to_cpu(rctx_g35->flow_specific.peer_smid);
8bf7c65d
S
3476
3477 r1_cmd = fusion->cmd_list[peer_smid - 1];
3478 scmd_local = cmd->scmd;
3479 status = rctx_g35->status;
3480 ex_status = rctx_g35->ex_status;
3481 data_length = cmd->io_request->DataLength;
3482 sense = cmd->sense;
3483
3484 cmd->cmd_completed = true;
3485
3486 /* Check if peer command is completed or not*/
3487 if (r1_cmd->cmd_completed) {
3488 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3489 if (rctx_g35->status != MFI_STAT_OK) {
3490 status = rctx_g35->status;
3491 ex_status = rctx_g35->ex_status;
3492 data_length = r1_cmd->io_request->DataLength;
3493 sense = r1_cmd->sense;
3494 }
3495
3496 megasas_return_cmd_fusion(instance, r1_cmd);
3497 map_cmd_status(fusion, scmd_local, status, ex_status,
3498 le32_to_cpu(data_length), sense);
3499 if (instance->ldio_threshold &&
3500 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3501 atomic_dec(&instance->ldio_outstanding);
3502 scmd_local->SCp.ptr = NULL;
3503 megasas_return_cmd_fusion(instance, cmd);
3504 scsi_dma_unmap(scmd_local);
3505 scmd_local->scsi_done(scmd_local);
3506 }
3507}
3508
9c915a8c
AR
3509/**
3510 * complete_cmd_fusion - Completes command
3511 * @instance: Adapter soft state
3512 * Completes all commands that is in reply descriptor queue
3513 */
3514int
62a04f81
S
3515complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex,
3516 struct megasas_irq_context *irq_context)
9c915a8c
AR
3517{
3518 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3519 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3520 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3521 struct fusion_context *fusion;
3522 struct megasas_cmd *cmd_mfi;
8bf7c65d 3523 struct megasas_cmd_fusion *cmd_fusion;
9c915a8c 3524 u16 smid, num_completed;
8bf7c65d
S
3525 u8 reply_descript_type, *sense, status, extStatus;
3526 u32 device_id, data_length;
9c915a8c
AR
3527 union desc_value d_val;
3528 struct LD_LOAD_BALANCE_INFO *lbinfo;
db4fc864 3529 int threshold_reply_count = 0;
4026e9aa 3530 struct scsi_cmnd *scmd_local = NULL;
18365b13
SS
3531 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3532 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
9c915a8c
AR
3533
3534 fusion = instance->ctrl_context;
3535
8a01a41d 3536 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
9c915a8c
AR
3537 return IRQ_HANDLED;
3538
179ac142
SS
3539 desc = fusion->reply_frames_desc[MSIxIndex] +
3540 fusion->last_reply_idx[MSIxIndex];
9c915a8c
AR
3541
3542 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3543
3544 d_val.word = desc->Words;
3545
3546 reply_descript_type = reply_desc->ReplyFlags &
3547 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3548
3549 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3550 return IRQ_NONE;
3551
9c915a8c
AR
3552 num_completed = 0;
3553
c6f5bf81
CH
3554 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3555 d_val.u.high != cpu_to_le32(UINT_MAX)) {
69c337c0 3556
94cd65dd 3557 smid = le16_to_cpu(reply_desc->SMID);
9c915a8c 3558 cmd_fusion = fusion->cmd_list[smid - 1];
8bf7c65d
S
3559 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3560 cmd_fusion->io_request;
9c915a8c 3561
4026e9aa 3562 scmd_local = cmd_fusion->scmd;
fdd84e25 3563 status = scsi_io_req->RaidContext.raid_context.status;
d889344e 3564 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
69c337c0
SC
3565 sense = cmd_fusion->sense;
3566 data_length = scsi_io_req->DataLength;
9c915a8c
AR
3567
3568 switch (scsi_io_req->Function) {
18365b13
SS
3569 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3570 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3571 cmd_fusion->io_request;
3572 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3573 &mr_tm_req->TmRequest;
3574 dev_dbg(&instance->pdev->dev, "TM completion:"
3575 "type: 0x%x TaskMID: 0x%x\n",
3576 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3577 complete(&cmd_fusion->done);
3578 break;
9c915a8c
AR
3579 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3580 /* Update load balancing info */
8bf7c65d
S
3581 if (fusion->load_balance_info &&
3582 (cmd_fusion->scmd->SCp.Status &
3583 MEGASAS_LOAD_BALANCE_FLAG)) {
3584 device_id = MEGASAS_DEV_INDEX(scmd_local);
3585 lbinfo = &fusion->load_balance_info[device_id];
d2552ebe 3586 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
8bf7c65d 3587 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
9c915a8c 3588 }
74e716f2 3589 /* Fall through - and complete IO */
9c915a8c 3590 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
8bf7c65d
S
3591 atomic_dec(&instance->fw_outstanding);
3592 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
69c337c0 3593 map_cmd_status(fusion, scmd_local, status,
8bf7c65d
S
3594 extStatus, le32_to_cpu(data_length),
3595 sense);
3596 if (instance->ldio_threshold &&
3597 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
b71b49c2 3598 atomic_dec(&instance->ldio_outstanding);
8bf7c65d 3599 scmd_local->SCp.ptr = NULL;
69c337c0
SC
3600 megasas_return_cmd_fusion(instance, cmd_fusion);
3601 scsi_dma_unmap(scmd_local);
3602 scmd_local->scsi_done(scmd_local);
8bf7c65d
S
3603 } else /* Optimal VD - R1 FP command completion. */
3604 megasas_complete_r1_command(instance, cmd_fusion);
9c915a8c
AR
3605 break;
3606 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3607 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4026e9aa
SS
3608 /* Poll mode. Dummy free.
3609 * In case of Interrupt mode, caller has reverse check.
3610 */
3611 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3612 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3613 megasas_return_cmd(instance, cmd_mfi);
3614 } else
3615 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
9c915a8c
AR
3616 break;
3617 }
3618
c8e858fe
AR
3619 fusion->last_reply_idx[MSIxIndex]++;
3620 if (fusion->last_reply_idx[MSIxIndex] >=
3621 fusion->reply_q_depth)
3622 fusion->last_reply_idx[MSIxIndex] = 0;
9c915a8c 3623
c6f5bf81 3624 desc->Words = cpu_to_le64(ULLONG_MAX);
9c915a8c 3625 num_completed++;
db4fc864 3626 threshold_reply_count++;
9c915a8c
AR
3627
3628 /* Get the next reply descriptor */
c8e858fe 3629 if (!fusion->last_reply_idx[MSIxIndex])
179ac142 3630 desc = fusion->reply_frames_desc[MSIxIndex];
9c915a8c
AR
3631 else
3632 desc++;
3633
3634 reply_desc =
3635 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3636
3637 d_val.word = desc->Words;
3638
3639 reply_descript_type = reply_desc->ReplyFlags &
3640 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3641
3642 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3643 break;
db4fc864
SS
3644 /*
3645 * Write to reply post host index register after completing threshold
3646 * number of reply counts and still there are more replies in reply queue
3647 * pending to be completed
3648 */
62a04f81 3649 if (threshold_reply_count >= instance->threshold_reply_count) {
2493c67e 3650 if (instance->msix_combined)
db4fc864
SS
3651 writel(((MSIxIndex & 0x7) << 24) |
3652 fusion->last_reply_idx[MSIxIndex],
3653 instance->reply_post_host_index_addr[MSIxIndex/8]);
3654 else
3655 writel((MSIxIndex << 24) |
3656 fusion->last_reply_idx[MSIxIndex],
3657 instance->reply_post_host_index_addr[0]);
3658 threshold_reply_count = 0;
62a04f81
S
3659 if (irq_context) {
3660 if (!irq_context->irq_poll_scheduled) {
3661 irq_context->irq_poll_scheduled = true;
a6ffd5bf 3662 irq_context->irq_line_enable = true;
62a04f81
S
3663 irq_poll_sched(&irq_context->irqpoll);
3664 }
3665 return num_completed;
3666 }
db4fc864 3667 }
9c915a8c
AR
3668 }
3669
62a04f81
S
3670 if (num_completed) {
3671 wmb();
3672 if (instance->msix_combined)
3673 writel(((MSIxIndex & 0x7) << 24) |
3674 fusion->last_reply_idx[MSIxIndex],
3675 instance->reply_post_host_index_addr[MSIxIndex/8]);
3676 else
3677 writel((MSIxIndex << 24) |
3678 fusion->last_reply_idx[MSIxIndex],
3679 instance->reply_post_host_index_addr[0]);
3680 megasas_check_and_restore_queue_depth(instance);
3681 }
3682 return num_completed;
3683}
9c915a8c 3684
62a04f81
S
3685/**
3686 * megasas_enable_irq_poll() - enable irqpoll
3687 */
3688static void megasas_enable_irq_poll(struct megasas_instance *instance)
3689{
3690 u32 count, i;
3691 struct megasas_irq_context *irq_ctx;
3692
3693 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3694
3695 for (i = 0; i < count; i++) {
3696 irq_ctx = &instance->irq_context[i];
3697 irq_poll_enable(&irq_ctx->irqpoll);
3698 }
9c915a8c
AR
3699}
3700
29206da1
S
3701/**
3702 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3703 * @instance: Adapter soft state
3704 */
3705void megasas_sync_irqs(unsigned long instance_addr)
3706{
3707 u32 count, i;
3708 struct megasas_instance *instance =
3709 (struct megasas_instance *)instance_addr;
62a04f81 3710 struct megasas_irq_context *irq_ctx;
29206da1
S
3711
3712 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3713
62a04f81 3714 for (i = 0; i < count; i++) {
29206da1 3715 synchronize_irq(pci_irq_vector(instance->pdev, i));
62a04f81
S
3716 irq_ctx = &instance->irq_context[i];
3717 irq_poll_disable(&irq_ctx->irqpoll);
3718 if (irq_ctx->irq_poll_scheduled) {
3719 irq_ctx->irq_poll_scheduled = false;
3720 enable_irq(irq_ctx->os_irq);
3721 }
3722 }
3723}
3724
3725/**
3726 * megasas_irqpoll() - process a queue for completed reply descriptors
3727 * @irqpoll: IRQ poll structure associated with queue to poll.
3728 * @budget: Threshold of reply descriptors to process per poll.
3729 *
3730 * Return: The number of entries processed.
3731 */
3732
3733int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3734{
3735 struct megasas_irq_context *irq_ctx;
3736 struct megasas_instance *instance;
3737 int num_entries;
3738
3739 irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3740 instance = irq_ctx->instance;
3741
a6ffd5bf
CP
3742 if (irq_ctx->irq_line_enable) {
3743 disable_irq(irq_ctx->os_irq);
3744 irq_ctx->irq_line_enable = false;
3745 }
3746
62a04f81
S
3747 num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3748 if (num_entries < budget) {
3749 irq_poll_complete(irqpoll);
3750 irq_ctx->irq_poll_scheduled = false;
3751 enable_irq(irq_ctx->os_irq);
3752 }
3753
3754 return num_entries;
29206da1
S
3755}
3756
9c915a8c
AR
3757/**
3758 * megasas_complete_cmd_dpc_fusion - Completes command
3759 * @instance: Adapter soft state
3760 *
3761 * Tasklet to complete cmds
3762 */
3763void
3764megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3765{
3766 struct megasas_instance *instance =
3767 (struct megasas_instance *)instance_addr;
c8e858fe
AR
3768 u32 count, MSIxIndex;
3769
3770 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
9c915a8c
AR
3771
3772 /* If we have already declared adapter dead, donot complete cmds */
34bd9f27 3773 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
9c915a8c 3774 return;
9c915a8c 3775
c8e858fe 3776 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
62a04f81 3777 complete_cmd_fusion(instance, MSIxIndex, NULL);
9c915a8c
AR
3778}
3779
3780/**
3781 * megasas_isr_fusion - isr entry point
3782 */
3783irqreturn_t megasas_isr_fusion(int irq, void *devp)
3784{
c8e858fe
AR
3785 struct megasas_irq_context *irq_context = devp;
3786 struct megasas_instance *instance = irq_context->instance;
3f6194af 3787 u32 mfiStatus;
9c915a8c 3788
d46a3ad6
SS
3789 if (instance->mask_interrupts)
3790 return IRQ_NONE;
3791
a6ffd5bf
CP
3792#if defined(ENABLE_IRQ_POLL)
3793 if (irq_context->irq_poll_scheduled)
3794 return IRQ_HANDLED;
3795#endif
3796
c8e858fe 3797 if (!instance->msix_vectors) {
de516379 3798 mfiStatus = instance->instancet->clear_intr(instance);
9c915a8c
AR
3799 if (!mfiStatus)
3800 return IRQ_NONE;
3801 }
3802
3803 /* If we are resetting, bail */
6497b247 3804 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
de516379 3805 instance->instancet->clear_intr(instance);
9c915a8c 3806 return IRQ_HANDLED;
6497b247 3807 }
9c915a8c 3808
62a04f81
S
3809 return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3810 ? IRQ_HANDLED : IRQ_NONE;
9c915a8c
AR
3811}
3812
3813/**
3814 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3815 * @instance: Adapter soft state
3816 * mfi_cmd: megasas_cmd pointer
3817 *
3818 */
7a7ae4f1 3819void
9c915a8c
AR
3820build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3821 struct megasas_cmd *mfi_cmd)
3822{
3823 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3824 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3825 struct megasas_cmd_fusion *cmd;
3826 struct fusion_context *fusion;
3827 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3828
4026e9aa
SS
3829 fusion = instance->ctrl_context;
3830
3831 cmd = megasas_get_cmd_fusion(instance,
3832 instance->max_scsi_cmds + mfi_cmd->index);
9c915a8c
AR
3833
3834 /* Save the smid. To be used for returning the cmd */
3835 mfi_cmd->context.smid = cmd->index;
90dc9d98 3836
9c915a8c
AR
3837 /*
3838 * For cmds where the flag is set, store the flag and check
3839 * on completion. For cmds with this flag, don't call
3840 * megasas_complete_cmd
3841 */
3842
6e755ddc 3843 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
4026e9aa 3844 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
9c915a8c 3845
9c915a8c 3846 io_req = cmd->io_request;
36807e67 3847
c365178f 3848 if (instance->adapter_type >= INVADER_SERIES) {
36807e67
AR
3849 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3850 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3851 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3852 sgl_ptr_end->Flags = 0;
3853 }
3854
9c915a8c
AR
3855 mpi25_ieee_chain =
3856 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3857
3858 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3859 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3860 SGL) / 4;
3861 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3862
94cd65dd 3863 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
9c915a8c
AR
3864
3865 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3866 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3867
ed2983f4 3868 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
9c915a8c
AR
3869}
3870
3871/**
3872 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3873 * @instance: Adapter soft state
3874 * @cmd: mfi cmd to build
3875 *
3876 */
3877union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3878build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3879{
52205ac8 3880 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
9c915a8c
AR
3881 u16 index;
3882
7a7ae4f1 3883 build_mpt_mfi_pass_thru(instance, cmd);
9c915a8c
AR
3884 index = cmd->context.smid;
3885
3886 req_desc = megasas_get_request_descriptor(instance, index - 1);
3887
9c915a8c
AR
3888 req_desc->Words = 0;
3889 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3890 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3891
94cd65dd 3892 req_desc->SCSIIO.SMID = cpu_to_le16(index);
9c915a8c
AR
3893
3894 return req_desc;
3895}
3896
3897/**
3898 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3899 * @instance: Adapter soft state
3900 * @cmd: mfi cmd pointer
3901 *
3902 */
f4fc2093 3903void
9c915a8c
AR
3904megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3905 struct megasas_cmd *cmd)
3906{
3907 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
9c915a8c
AR
3908
3909 req_desc = build_mpt_cmd(instance, cmd);
6d40afbc 3910
45b8a35e 3911 megasas_fire_cmd_fusion(instance, req_desc);
f4fc2093 3912 return;
9c915a8c
AR
3913}
3914
3915/**
3916 * megasas_release_fusion - Reverses the FW initialization
4b63b286 3917 * @instance: Adapter soft state
9c915a8c
AR
3918 */
3919void
3920megasas_release_fusion(struct megasas_instance *instance)
3921{
b9637d14 3922 megasas_free_ioc_init_cmd(instance);
9c915a8c
AR
3923 megasas_free_cmds(instance);
3924 megasas_free_cmds_fusion(instance);
3925
3926 iounmap(instance->reg_set);
3927
e7f85168 3928 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
9c915a8c
AR
3929}
3930
3931/**
3932 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3933 * @regs: MFI register set
3934 */
3935static u32
de516379 3936megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
9c915a8c 3937{
272652fc 3938 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
9c915a8c
AR
3939}
3940
fc62b3fc
SS
3941/**
3942 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3943 * @instance: Controller's soft instance
3944 * return: Number of allocated host crash buffers
3945 */
3946static void
3947megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3948{
3949 unsigned int i;
3950
fc62b3fc 3951 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
def3e8df 3952 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
fc62b3fc
SS
3953 if (!instance->crash_buf[i]) {
3954 dev_info(&instance->pdev->dev, "Firmware crash dump "
3955 "memory allocation failed at index %d\n", i);
3956 break;
3957 }
3958 }
3959 instance->drv_buf_alloc = i;
3960}
3961
3962/**
3963 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3964 * @instance: Controller's soft instance
3965 */
3966void
3967megasas_free_host_crash_buffer(struct megasas_instance *instance)
3968{
def3e8df 3969 unsigned int i;
fc62b3fc
SS
3970 for (i = 0; i < instance->drv_buf_alloc; i++) {
3971 if (instance->crash_buf[i])
def3e8df 3972 vfree(instance->crash_buf[i]);
fc62b3fc
SS
3973 }
3974 instance->drv_buf_index = 0;
3975 instance->drv_buf_alloc = 0;
3976 instance->fw_crash_state = UNAVAILABLE;
3977 instance->fw_crash_buffer_size = 0;
3978}
3979
9c915a8c
AR
3980/**
3981 * megasas_adp_reset_fusion - For controller reset
3982 * @regs: MFI register set
3983 */
3984static int
3985megasas_adp_reset_fusion(struct megasas_instance *instance,
3986 struct megasas_register_set __iomem *regs)
3987{
79b82c2c 3988 u32 host_diag, abs_state, retry;
3989
3990 /* Now try to reset the chip */
3991 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3992 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3993 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3994 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3995 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3996 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3997 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3998
3999 /* Check that the diag write enable (DRWE) bit is on */
272652fc 4000 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
79b82c2c 4001 retry = 0;
4002 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
4003 msleep(100);
272652fc
S
4004 host_diag = megasas_readl(instance,
4005 &instance->reg_set->fusion_host_diag);
79b82c2c 4006 if (retry++ == 100) {
4007 dev_warn(&instance->pdev->dev,
4008 "Host diag unlock failed from %s %d\n",
4009 __func__, __LINE__);
4010 break;
4011 }
4012 }
4013 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
4014 return -1;
4015
4016 /* Send chip reset command */
4017 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
4018 &instance->reg_set->fusion_host_diag);
4019 msleep(3000);
4020
4021 /* Make sure reset adapter bit is cleared */
272652fc 4022 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
79b82c2c 4023 retry = 0;
4024 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
4025 msleep(100);
272652fc
S
4026 host_diag = megasas_readl(instance,
4027 &instance->reg_set->fusion_host_diag);
79b82c2c 4028 if (retry++ == 1000) {
4029 dev_warn(&instance->pdev->dev,
4030 "Diag reset adapter never cleared %s %d\n",
4031 __func__, __LINE__);
4032 break;
4033 }
4034 }
4035 if (host_diag & HOST_DIAG_RESET_ADAPTER)
4036 return -1;
4037
de516379 4038 abs_state = instance->instancet->read_fw_status_reg(instance)
79b82c2c 4039 & MFI_STATE_MASK;
4040 retry = 0;
4041
4042 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
4043 msleep(100);
4044 abs_state = instance->instancet->
de516379 4045 read_fw_status_reg(instance) & MFI_STATE_MASK;
79b82c2c 4046 }
4047 if (abs_state <= MFI_STATE_FW_INIT) {
4048 dev_warn(&instance->pdev->dev,
4049 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4050 abs_state, __func__, __LINE__);
4051 return -1;
4052 }
4053
9c915a8c
AR
4054 return 0;
4055}
4056
4057/**
4058 * megasas_check_reset_fusion - For controller reset check
4059 * @regs: MFI register set
4060 */
4061static int
4062megasas_check_reset_fusion(struct megasas_instance *instance,
4063 struct megasas_register_set __iomem *regs)
4064{
4065 return 0;
4066}
4067
f0c21df6
S
4068/**
4069 * megasas_trigger_snap_dump - Trigger snap dump in FW
4070 * @instance: Soft instance of adapter
4071 */
4072static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4073{
4074 int j;
b6661342 4075 u32 fw_state, abs_state;
f0c21df6
S
4076
4077 if (!instance->disableOnlineCtrlReset) {
4078 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4079 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4080 &instance->reg_set->doorbell);
4081 readl(&instance->reg_set->doorbell);
4082 }
4083
4084 for (j = 0; j < instance->snapdump_wait_time; j++) {
b6661342
S
4085 abs_state = instance->instancet->read_fw_status_reg(instance);
4086 fw_state = abs_state & MFI_STATE_MASK;
f0c21df6 4087 if (fw_state == MFI_STATE_FAULT) {
b6661342
S
4088 dev_printk(KERN_ERR, &instance->pdev->dev,
4089 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4090 abs_state & MFI_STATE_FAULT_CODE,
4091 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
f0c21df6
S
4092 return;
4093 }
4094 msleep(1000);
4095 }
4096}
4097
9c915a8c 4098/* This function waits for outstanding commands on fusion to complete */
229fe47c 4099int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
6d40afbc 4100 int reason, int *convert)
9c915a8c 4101{
229fe47c 4102 int i, outstanding, retval = 0, hb_seconds_missed = 0;
b6661342 4103 u32 fw_state, abs_state;
f0c21df6 4104 u32 waittime_for_io_completion;
9c915a8c 4105
f0c21df6
S
4106 waittime_for_io_completion =
4107 min_t(u32, resetwaittime,
4108 (resetwaittime - instance->snapdump_wait_time));
4109
4110 if (reason == MFI_IO_TIMEOUT_OCR) {
4111 dev_info(&instance->pdev->dev,
4112 "MFI command is timed out\n");
4113 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4114 if (instance->snapdump_wait_time)
4115 megasas_trigger_snap_dump(instance);
4116 retval = 1;
4117 goto out;
4118 }
4119
4120 for (i = 0; i < waittime_for_io_completion; i++) {
9c915a8c 4121 /* Check if firmware is in fault state */
b6661342
S
4122 abs_state = instance->instancet->read_fw_status_reg(instance);
4123 fw_state = abs_state & MFI_STATE_MASK;
9c915a8c 4124 if (fw_state == MFI_STATE_FAULT) {
b6661342
S
4125 dev_printk(KERN_ERR, &instance->pdev->dev,
4126 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4127 abs_state & MFI_STATE_FAULT_CODE,
4128 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
64d0b8e4 4129 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
def0eab3
S
4130 if (instance->requestorId && reason) {
4131 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4132 " state while polling during"
4133 " I/O timeout handling for %d\n",
4134 instance->host->host_no);
4135 *convert = 1;
4136 }
4137
229fe47c
AR
4138 retval = 1;
4139 goto out;
4140 }
6d40afbc 4141
6d40afbc 4142
229fe47c 4143 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
6d40afbc 4144 if (instance->requestorId && !reason) {
9c915a8c
AR
4145 retval = 1;
4146 goto out;
4147 }
4148
229fe47c 4149 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
def0eab3 4150 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
229fe47c
AR
4151 if (instance->hb_host_mem->HB.fwCounter !=
4152 instance->hb_host_mem->HB.driverCounter) {
4153 instance->hb_host_mem->HB.driverCounter =
4154 instance->hb_host_mem->HB.fwCounter;
4155 hb_seconds_missed = 0;
4156 } else {
4157 hb_seconds_missed++;
4158 if (hb_seconds_missed ==
4159 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
1be18254 4160 dev_warn(&instance->pdev->dev, "SR-IOV:"
229fe47c
AR
4161 " Heartbeat never completed "
4162 " while polling during I/O "
4163 " timeout handling for "
4164 "scsi%d.\n",
4165 instance->host->host_no);
4166 *convert = 1;
4167 retval = 1;
4168 goto out;
4169 }
4170 }
4171 }
4172
14298cf3 4173 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
9c915a8c
AR
4174 outstanding = atomic_read(&instance->fw_outstanding);
4175 if (!outstanding)
4176 goto out;
4177
4178 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1be18254 4179 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
229fe47c
AR
4180 "commands to complete for scsi%d\n", i,
4181 outstanding, instance->host->host_no);
9c915a8c
AR
4182 }
4183 msleep(1000);
4184 }
4185
f0c21df6
S
4186 if (instance->snapdump_wait_time) {
4187 megasas_trigger_snap_dump(instance);
4188 retval = 1;
4189 goto out;
4190 }
4191
9c915a8c 4192 if (atomic_read(&instance->fw_outstanding)) {
1be18254 4193 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
229fe47c
AR
4194 "will reset adapter scsi%d.\n",
4195 instance->host->host_no);
18e1c7f6 4196 *convert = 1;
9c915a8c
AR
4197 retval = 1;
4198 }
f0c21df6 4199
9c915a8c
AR
4200out:
4201 return retval;
4202}
4203
4204void megasas_reset_reply_desc(struct megasas_instance *instance)
4205{
179ac142 4206 int i, j, count;
9c915a8c
AR
4207 struct fusion_context *fusion;
4208 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4209
4210 fusion = instance->ctrl_context;
c8e858fe 4211 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
179ac142 4212 for (i = 0 ; i < count ; i++) {
c8e858fe 4213 fusion->last_reply_idx[i] = 0;
179ac142
SS
4214 reply_desc = fusion->reply_frames_desc[i];
4215 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4216 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4217 }
9c915a8c
AR
4218}
4219
9b828182
SS
4220/*
4221 * megasas_refire_mgmt_cmd : Re-fire management commands
4222 * @instance: Controller's soft instance
4223*/
4224void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
4225{
4226 int j;
4227 struct megasas_cmd_fusion *cmd_fusion;
4228 struct fusion_context *fusion;
4229 struct megasas_cmd *cmd_mfi;
4230 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4231 u16 smid;
6d40afbc 4232 bool refire_cmd = 0;
54b28049
S
4233 u8 result;
4234 u32 opcode = 0;
9b828182
SS
4235
4236 fusion = instance->ctrl_context;
4237
4238 /* Re-fire management commands.
4239 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4240 */
4241 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4242 cmd_fusion = fusion->cmd_list[j];
4243 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4244 smid = le16_to_cpu(cmd_mfi->context.smid);
54b28049 4245 result = REFIRE_CMD;
9b828182
SS
4246
4247 if (!smid)
4248 continue;
690e9c38 4249
54b28049
S
4250 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4251
4252 switch (cmd_mfi->frame->hdr.cmd) {
4253 case MFI_CMD_DCMD:
4254 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4255 /* Do not refire shutdown command */
4256 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4257 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4258 result = COMPLETE_CMD;
4259 break;
4260 }
4261
4262 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4263 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4264 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4265
4266 if (!refire_cmd)
4267 result = RETURN_CMD;
4268
4269 break;
f870bcbe
S
4270 case MFI_CMD_NVME:
4271 if (!instance->support_nvme_passthru) {
4272 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4273 result = COMPLETE_CMD;
4274 }
54b28049 4275
58136856
CP
4276 break;
4277 case MFI_CMD_TOOLBOX:
4278 if (!instance->support_pci_lane_margining) {
4279 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4280 result = COMPLETE_CMD;
4281 }
4282
f870bcbe 4283 break;
54b28049
S
4284 default:
4285 break;
690e9c38
S
4286 }
4287
54b28049
S
4288 switch (result) {
4289 case REFIRE_CMD:
45b8a35e 4290 megasas_fire_cmd_fusion(instance, req_desc);
54b28049
S
4291 break;
4292 case RETURN_CMD:
9b828182 4293 megasas_return_cmd(instance, cmd_mfi);
54b28049
S
4294 break;
4295 case COMPLETE_CMD:
4296 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4297 break;
4298 }
9b828182 4299 }
9c915a8c
AR
4300}
4301
18365b13
SS
4302/*
4303 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4304 * @instance: per adapter struct
4305 * @channel: the channel assigned by the OS
4306 * @id: the id assigned by the OS
4307 *
4308 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4309 */
4310
4311static int megasas_track_scsiio(struct megasas_instance *instance,
4312 int id, int channel)
4313{
4314 int i, found = 0;
4315 struct megasas_cmd_fusion *cmd_fusion;
4316 struct fusion_context *fusion;
4317 fusion = instance->ctrl_context;
4318
4319 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4320 cmd_fusion = fusion->cmd_list[i];
4321 if (cmd_fusion->scmd &&
4322 (cmd_fusion->scmd->device->id == id &&
4323 cmd_fusion->scmd->device->channel == channel)) {
4324 dev_info(&instance->pdev->dev,
4325 "SCSI commands pending to target"
4326 "channel %d id %d \tSMID: 0x%x\n",
4327 channel, id, cmd_fusion->index);
4328 scsi_print_command(cmd_fusion->scmd);
4329 found = 1;
4330 break;
4331 }
4332 }
4333
4334 return found ? FAILED : SUCCESS;
4335}
4336
4337/**
4338 * megasas_tm_response_code - translation of device response code
4339 * @ioc: per adapter object
4340 * @mpi_reply: MPI reply returned by firmware
4341 *
4342 * Return nothing.
4343 */
4344static void
4345megasas_tm_response_code(struct megasas_instance *instance,
4346 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4347{
4348 char *desc;
4349
4350 switch (mpi_reply->ResponseCode) {
4351 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4352 desc = "task management request completed";
4353 break;
4354 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4355 desc = "invalid frame";
4356 break;
4357 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4358 desc = "task management request not supported";
4359 break;
4360 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4361 desc = "task management request failed";
4362 break;
4363 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4364 desc = "task management request succeeded";
4365 break;
4366 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4367 desc = "invalid lun";
4368 break;
4369 case 0xA:
4370 desc = "overlapped tag attempted";
4371 break;
4372 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4373 desc = "task queued, however not sent to target";
4374 break;
4375 default:
4376 desc = "unknown";
4377 break;
4378 }
4379 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4380 mpi_reply->ResponseCode, desc);
4381 dev_dbg(&instance->pdev->dev,
4382 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4383 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4384 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4385 mpi_reply->Function, mpi_reply->TaskType,
4386 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4387}
4388
4389/**
4390 * megasas_issue_tm - main routine for sending tm requests
4391 * @instance: per adapter struct
4392 * @device_handle: device handle
4393 * @channel: the channel assigned by the OS
4394 * @id: the id assigned by the OS
4395 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4396 * @smid_task: smid assigned to the task
4397 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4398 * Context: user
4399 *
4400 * MegaRaid use MPT interface for Task Magement request.
4401 * A generic API for sending task management requests to firmware.
4402 *
4403 * Return SUCCESS or FAILED.
4404 */
4405static int
4406megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
e9495e2d
S
4407 uint channel, uint id, u16 smid_task, u8 type,
4408 struct MR_PRIV_DEVICE *mr_device_priv_data)
18365b13
SS
4409{
4410 struct MR_TASK_MANAGE_REQUEST *mr_request;
4411 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4412 unsigned long timeleft;
4413 struct megasas_cmd_fusion *cmd_fusion;
4414 struct megasas_cmd *cmd_mfi;
4415 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
28661c88 4416 struct fusion_context *fusion = NULL;
18365b13
SS
4417 struct megasas_cmd_fusion *scsi_lookup;
4418 int rc;
e9495e2d 4419 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
18365b13
SS
4420 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4421
4422 fusion = instance->ctrl_context;
4423
4424 cmd_mfi = megasas_get_cmd(instance);
4425
4426 if (!cmd_mfi) {
4427 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4428 __func__, __LINE__);
4429 return -ENOMEM;
4430 }
4431
4432 cmd_fusion = megasas_get_cmd_fusion(instance,
4433 instance->max_scsi_cmds + cmd_mfi->index);
4434
4435 /* Save the smid. To be used for returning the cmd */
4436 cmd_mfi->context.smid = cmd_fusion->index;
4437
4438 req_desc = megasas_get_request_descriptor(instance,
4439 (cmd_fusion->index - 1));
18365b13
SS
4440
4441 cmd_fusion->request_desc = req_desc;
4442 req_desc->Words = 0;
4443
18365b13
SS
4444 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4445 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4446 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4447 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4448 mpi_request->DevHandle = cpu_to_le16(device_handle);
4449 mpi_request->TaskType = type;
4450 mpi_request->TaskMID = cpu_to_le16(smid_task);
4451 mpi_request->LUN[1] = 0;
4452
4453
4454 req_desc = cmd_fusion->request_desc;
4455 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4456 req_desc->HighPriority.RequestFlags =
4457 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4458 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4459 req_desc->HighPriority.MSIxIndex = 0;
4460 req_desc->HighPriority.LMID = 0;
4461 req_desc->HighPriority.Reserved1 = 0;
4462
4463 if (channel < MEGASAS_MAX_PD_CHANNELS)
4464 mr_request->tmReqFlags.isTMForPD = 1;
4465 else
4466 mr_request->tmReqFlags.isTMForLD = 1;
4467
4468 init_completion(&cmd_fusion->done);
45b8a35e 4469 megasas_fire_cmd_fusion(instance, req_desc);
18365b13 4470
e9495e2d
S
4471 switch (type) {
4472 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4473 timeout = mr_device_priv_data->task_abort_tmo;
4474 break;
4475 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4476 timeout = mr_device_priv_data->target_reset_tmo;
4477 break;
4478 }
4479
4480 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
18365b13
SS
4481
4482 if (!timeleft) {
4483 dev_err(&instance->pdev->dev,
4484 "task mgmt type 0x%x timed out\n", type);
4485 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
4486 mutex_unlock(&instance->reset_mutex);
4487 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4488 mutex_lock(&instance->reset_mutex);
4489 return rc;
4490 }
4491
4492 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4493 megasas_tm_response_code(instance, mpi_reply);
4494
4495 megasas_return_cmd(instance, cmd_mfi);
4496 rc = SUCCESS;
4497 switch (type) {
4498 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
28661c88
S
4499 scsi_lookup = fusion->cmd_list[smid_task - 1];
4500
18365b13
SS
4501 if (scsi_lookup->scmd == NULL)
4502 break;
4503 else {
4504 instance->instancet->disable_intr(instance);
29206da1 4505 megasas_sync_irqs((unsigned long)instance);
18365b13 4506 instance->instancet->enable_intr(instance);
62a04f81 4507 megasas_enable_irq_poll(instance);
18365b13
SS
4508 if (scsi_lookup->scmd == NULL)
4509 break;
4510 }
4511 rc = FAILED;
4512 break;
4513
4514 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4515 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4516 break;
4517 instance->instancet->disable_intr(instance);
bdb5c55f 4518 megasas_sync_irqs((unsigned long)instance);
18365b13
SS
4519 rc = megasas_track_scsiio(instance, id, channel);
4520 instance->instancet->enable_intr(instance);
62a04f81 4521 megasas_enable_irq_poll(instance);
18365b13
SS
4522
4523 break;
4524 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4525 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4526 break;
4527 default:
4528 rc = FAILED;
4529 break;
4530 }
4531
4532 return rc;
4533
4534}
4535
4536/*
4537 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4538 * @instance: per adapter struct
4539 *
4540 * Return Non Zero index, if SMID found in outstanding commands
4541 */
4542static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4543{
4544 int i, ret = 0;
4545 struct megasas_instance *instance;
4546 struct megasas_cmd_fusion *cmd_fusion;
4547 struct fusion_context *fusion;
4548
4549 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4550
4551 fusion = instance->ctrl_context;
4552
4553 for (i = 0; i < instance->max_scsi_cmds; i++) {
4554 cmd_fusion = fusion->cmd_list[i];
4555 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4556 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4557 " SMID: %d\n", cmd_fusion->index);
4558 ret = cmd_fusion->index;
4559 break;
4560 }
4561 }
4562
4563 return ret;
4564}
4565
4566/*
4567* megasas_get_tm_devhandle - Get devhandle for TM request
4568* @sdev- OS provided scsi device
4569*
4570* Returns- devhandle/targetID of SCSI device
4571*/
4572static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4573{
4574 u16 pd_index = 0;
4575 u32 device_id;
4576 struct megasas_instance *instance;
4577 struct fusion_context *fusion;
4578 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4579 u16 devhandle = (u16)ULONG_MAX;
4580
4581 instance = (struct megasas_instance *)sdev->host->hostdata;
4582 fusion = instance->ctrl_context;
4583
3cabd162 4584 if (!MEGASAS_IS_LOGICAL(sdev)) {
18365b13 4585 if (instance->use_seqnum_jbod_fp) {
3cabd162
S
4586 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4587 + sdev->id;
4588 pd_sync = (void *)fusion->pd_seq_sync
4589 [(instance->pd_seq_map_id - 1) & 1];
4590 devhandle = pd_sync->seq[pd_index].devHandle;
18365b13
SS
4591 } else
4592 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4593 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4594 } else {
4595 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4596 + sdev->id;
4597 devhandle = device_id;
4598 }
4599
4600 return devhandle;
4601}
4602
4603/*
4604 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4605 * @scmd : pointer to scsi command object
4606 *
4607 * Return SUCCESS, if command aborted else FAILED
4608 */
4609
4610int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4611{
4612 struct megasas_instance *instance;
4613 u16 smid, devhandle;
18365b13
SS
4614 int ret;
4615 struct MR_PRIV_DEVICE *mr_device_priv_data;
4616 mr_device_priv_data = scmd->device->hostdata;
4617
18365b13 4618 instance = (struct megasas_instance *)scmd->device->host->hostdata;
18365b13 4619
8a01a41d 4620 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
18365b13
SS
4621 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4622 "SCSI host:%d\n", instance->host->host_no);
4623 ret = FAILED;
4624 return ret;
4625 }
4626
4627 if (!mr_device_priv_data) {
4628 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4629 "scmd(%p)\n", scmd);
4630 scmd->result = DID_NO_CONNECT << 16;
4631 ret = SUCCESS;
4632 goto out;
4633 }
4634
18365b13
SS
4635 if (!mr_device_priv_data->is_tm_capable) {
4636 ret = FAILED;
4637 goto out;
4638 }
4639
4640 mutex_lock(&instance->reset_mutex);
4641
4642 smid = megasas_fusion_smid_lookup(scmd);
4643
4644 if (!smid) {
4645 ret = SUCCESS;
4646 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
efc372c1 4647 " issued is not found in outstanding commands\n");
18365b13
SS
4648 mutex_unlock(&instance->reset_mutex);
4649 goto out;
4650 }
4651
4652 devhandle = megasas_get_tm_devhandle(scmd->device);
4653
4654 if (devhandle == (u16)ULONG_MAX) {
4655 ret = SUCCESS;
4656 sdev_printk(KERN_INFO, scmd->device,
4657 "task abort issued for invalid devhandle\n");
4658 mutex_unlock(&instance->reset_mutex);
4659 goto out;
4660 }
4661 sdev_printk(KERN_INFO, scmd->device,
96c9603c 4662 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
18365b13
SS
4663 scmd, devhandle);
4664
4665 mr_device_priv_data->tm_busy = 1;
4666 ret = megasas_issue_tm(instance, devhandle,
4667 scmd->device->channel, scmd->device->id, smid,
e9495e2d
S
4668 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4669 mr_device_priv_data);
18365b13
SS
4670 mr_device_priv_data->tm_busy = 0;
4671
4672 mutex_unlock(&instance->reset_mutex);
96c9603c 4673 scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
18365b13 4674 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
96c9603c
S
4675out:
4676 scsi_print_command(scmd);
4677 if (megasas_dbg_lvl & TM_DEBUG)
4678 megasas_dump_fusion_io(scmd);
18365b13
SS
4679
4680 return ret;
4681}
4682
4683/*
4684 * megasas_reset_target_fusion : target reset function for fusion adapters
4685 * scmd: SCSI command pointer
4686 *
4687 * Returns SUCCESS if all commands associated with target aborted else FAILED
4688 */
4689
4690int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4691{
4692
4693 struct megasas_instance *instance;
4694 int ret = FAILED;
4695 u16 devhandle;
18365b13
SS
4696 struct MR_PRIV_DEVICE *mr_device_priv_data;
4697 mr_device_priv_data = scmd->device->hostdata;
4698
4699 instance = (struct megasas_instance *)scmd->device->host->hostdata;
18365b13 4700
8a01a41d 4701 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
18365b13
SS
4702 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4703 "SCSI host:%d\n", instance->host->host_no);
4704 ret = FAILED;
4705 return ret;
4706 }
4707
4708 if (!mr_device_priv_data) {
96c9603c
S
4709 sdev_printk(KERN_INFO, scmd->device,
4710 "device been deleted! scmd: (0x%p)\n", scmd);
18365b13
SS
4711 scmd->result = DID_NO_CONNECT << 16;
4712 ret = SUCCESS;
4713 goto out;
4714 }
4715
18365b13
SS
4716 if (!mr_device_priv_data->is_tm_capable) {
4717 ret = FAILED;
4718 goto out;
4719 }
4720
4721 mutex_lock(&instance->reset_mutex);
4722 devhandle = megasas_get_tm_devhandle(scmd->device);
4723
4724 if (devhandle == (u16)ULONG_MAX) {
4725 ret = SUCCESS;
4726 sdev_printk(KERN_INFO, scmd->device,
4727 "target reset issued for invalid devhandle\n");
4728 mutex_unlock(&instance->reset_mutex);
4729 goto out;
4730 }
4731
4732 sdev_printk(KERN_INFO, scmd->device,
96c9603c 4733 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
18365b13
SS
4734 scmd, devhandle);
4735 mr_device_priv_data->tm_busy = 1;
4736 ret = megasas_issue_tm(instance, devhandle,
4737 scmd->device->channel, scmd->device->id, 0,
e9495e2d
S
4738 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4739 mr_device_priv_data);
18365b13
SS
4740 mr_device_priv_data->tm_busy = 0;
4741 mutex_unlock(&instance->reset_mutex);
96c9603c 4742 scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
18365b13
SS
4743 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4744
96c9603c 4745out:
18365b13
SS
4746 return ret;
4747}
4748
8f67c8c5
SS
4749/*SRIOV get other instance in cluster if any*/
4750struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4751{
4752 int i;
4753
4754 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4755 if (megasas_mgmt_info.instance[i] &&
4756 (megasas_mgmt_info.instance[i] != instance) &&
4757 megasas_mgmt_info.instance[i]->requestorId &&
4758 megasas_mgmt_info.instance[i]->peerIsPresent &&
4759 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4760 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4761 return megasas_mgmt_info.instance[i];
4762 }
4763 return NULL;
4764}
4765
229fe47c
AR
4766/* Check for a second path that is currently UP */
4767int megasas_check_mpio_paths(struct megasas_instance *instance,
4768 struct scsi_cmnd *scmd)
4769{
8f67c8c5 4770 struct megasas_instance *peer_instance = NULL;
f55cf47d 4771 int retval = (DID_REQUEUE << 16);
8f67c8c5
SS
4772
4773 if (instance->peerIsPresent) {
4774 peer_instance = megasas_get_peer_instance(instance);
4775 if ((peer_instance) &&
4776 (atomic_read(&peer_instance->adprecovery) ==
4777 MEGASAS_HBA_OPERATIONAL))
4778 retval = (DID_NO_CONNECT << 16);
229fe47c 4779 }
229fe47c
AR
4780 return retval;
4781}
4782
9c915a8c 4783/* Core fusion reset function */
18365b13 4784int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
9c915a8c 4785{
fdd84e25 4786 int retval = SUCCESS, i, j, convert = 0;
9c915a8c 4787 struct megasas_instance *instance;
8bf7c65d 4788 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
9c915a8c 4789 struct fusion_context *fusion;
96c9603c 4790 u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
fc62b3fc 4791 u32 io_timeout_in_crash_mode = 0;
4026e9aa 4792 struct scsi_cmnd *scmd_local = NULL;
18365b13 4793 struct scsi_device *sdev;
e9495e2d
S
4794 int ret_target_prop = DCMD_FAILED;
4795 bool is_target_prop = false;
44e8d693
S
4796 bool do_adp_reset = true;
4797 int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
9c915a8c
AR
4798
4799 instance = (struct megasas_instance *)shost->hostdata;
4800 fusion = instance->ctrl_context;
4801
229fe47c
AR
4802 mutex_lock(&instance->reset_mutex);
4803
8a01a41d 4804 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1be18254 4805 dev_warn(&instance->pdev->dev, "Hardware critical error, "
229fe47c
AR
4806 "returning FAILED for scsi%d.\n",
4807 instance->host->host_no);
a2fbcbc3 4808 mutex_unlock(&instance->reset_mutex);
d4a759a9 4809 return FAILED;
9c915a8c 4810 }
de516379 4811 status_reg = instance->instancet->read_fw_status_reg(instance);
fc62b3fc
SS
4812 abs_state = status_reg & MFI_STATE_MASK;
4813
4814 /* IO timeout detected, forcibly put FW in FAULT state */
4815 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
18365b13
SS
4816 instance->crash_dump_app_support && reason) {
4817 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
fc62b3fc 4818 "forcibly FAULT Firmware\n");
8a01a41d 4819 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
272652fc 4820 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
fc62b3fc
SS
4821 writel(status_reg | MFI_STATE_FORCE_OCR,
4822 &instance->reg_set->doorbell);
4823 readl(&instance->reg_set->doorbell);
4824 mutex_unlock(&instance->reset_mutex);
4825 do {
4826 ssleep(3);
4827 io_timeout_in_crash_mode++;
4828 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4829 "seconds for crash dump collection and OCR "
4830 "to be done\n", (io_timeout_in_crash_mode * 3));
8a01a41d 4831 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
fc62b3fc
SS
4832 (io_timeout_in_crash_mode < 80));
4833
8a01a41d 4834 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
fc62b3fc
SS
4835 dev_info(&instance->pdev->dev, "OCR done for IO "
4836 "timeout case\n");
4837 retval = SUCCESS;
4838 } else {
4839 dev_info(&instance->pdev->dev, "Controller is not "
4840 "operational after 240 seconds wait for IO "
4841 "timeout case in FW crash dump mode\n do "
4842 "OCR/kill adapter\n");
4843 retval = megasas_reset_fusion(shost, 0);
4844 }
4845 return retval;
4846 }
9c915a8c 4847
229fe47c
AR
4848 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4849 del_timer_sync(&instance->sriov_heartbeat_timer);
7e70e733 4850 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
8a01a41d 4851 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
d46a3ad6 4852 instance->instancet->disable_intr(instance);
29206da1 4853 megasas_sync_irqs((unsigned long)instance);
7e70e733 4854
9c915a8c 4855 /* First try waiting for commands to complete */
18365b13 4856 if (megasas_wait_for_outstanding_fusion(instance, reason,
229fe47c 4857 &convert)) {
8a01a41d 4858 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
1be18254 4859 dev_warn(&instance->pdev->dev, "resetting fusion "
229fe47c
AR
4860 "adapter scsi%d.\n", instance->host->host_no);
4861 if (convert)
18365b13 4862 reason = 0;
229fe47c 4863
96c9603c 4864 if (megasas_dbg_lvl & OCR_DEBUG)
def0eab3
S
4865 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4866
9c915a8c 4867 /* Now return commands back to the OS */
4026e9aa 4868 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
9c915a8c 4869 cmd_fusion = fusion->cmd_list[i];
69c337c0 4870 /*check for extra commands issued by driver*/
630d42b7 4871 if (instance->adapter_type >= VENTURA_SERIES) {
8bf7c65d
S
4872 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4873 megasas_return_cmd_fusion(instance, r1_cmd);
69c337c0 4874 }
4026e9aa 4875 scmd_local = cmd_fusion->scmd;
9c915a8c 4876 if (cmd_fusion->scmd) {
96c9603c 4877 if (megasas_dbg_lvl & OCR_DEBUG) {
def0eab3
S
4878 sdev_printk(KERN_INFO,
4879 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4880 cmd_fusion->index);
96c9603c 4881 megasas_dump_fusion_io(cmd_fusion->scmd);
def0eab3
S
4882 }
4883
96c9603c
S
4884 if (cmd_fusion->io_request->Function ==
4885 MPI2_FUNCTION_SCSI_IO_REQUEST)
4886 fpio_count++;
4887
4026e9aa 4888 scmd_local->result =
229fe47c 4889 megasas_check_mpio_paths(instance,
4026e9aa 4890 scmd_local);
b71b49c2
SC
4891 if (instance->ldio_threshold &&
4892 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
308ec459 4893 atomic_dec(&instance->ldio_outstanding);
9c915a8c 4894 megasas_return_cmd_fusion(instance, cmd_fusion);
4026e9aa
SS
4895 scsi_dma_unmap(scmd_local);
4896 scmd_local->scsi_done(scmd_local);
9c915a8c
AR
4897 }
4898 }
4899
96c9603c
S
4900 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
4901 fpio_count);
4902
69c337c0
SC
4903 atomic_set(&instance->fw_outstanding, 0);
4904
de516379 4905 status_reg = instance->instancet->read_fw_status_reg(instance);
7e70e733
AR
4906 abs_state = status_reg & MFI_STATE_MASK;
4907 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4908 if (instance->disableOnlineCtrlReset ||
4909 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
9c915a8c 4910 /* Reset not supported, kill adapter */
1be18254 4911 dev_warn(&instance->pdev->dev, "Reset not supported"
229fe47c
AR
4912 ", killing adapter scsi%d.\n",
4913 instance->host->host_no);
62a04f81 4914 goto kill_hba;
9c915a8c
AR
4915 }
4916
229fe47c 4917 /* Let SR-IOV VF & PF sync up if there was a HB failure */
18365b13 4918 if (instance->requestorId && !reason) {
229fe47c 4919 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
44e8d693
S
4920 do_adp_reset = false;
4921 max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
229fe47c
AR
4922 }
4923
9c915a8c 4924 /* Now try to reset the chip */
44e8d693 4925 for (i = 0; i < max_reset_tries; i++) {
78409d4b
S
4926 /*
4927 * Do adp reset and wait for
4928 * controller to transition to ready
4929 */
4930 if (megasas_adp_reset_wait_for_ready(instance,
4931 do_adp_reset, 1) == FAILED)
9c915a8c 4932 continue;
44e8d693 4933
9c915a8c 4934 /* Wait for FW to become ready */
058a8fac 4935 if (megasas_transition_to_ready(instance, 1)) {
3885c26b
SS
4936 dev_warn(&instance->pdev->dev,
4937 "Failed to transition controller to ready for "
4938 "scsi%d.\n", instance->host->host_no);
44e8d693 4939 continue;
9c915a8c 4940 }
9c915a8c 4941 megasas_reset_reply_desc(instance);
308ec459
SS
4942 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4943
9c915a8c 4944 if (megasas_ioc_init_fusion(instance)) {
44e8d693 4945 continue;
9c915a8c
AR
4946 }
4947
d009b576
SS
4948 if (megasas_get_ctrl_info(instance)) {
4949 dev_info(&instance->pdev->dev,
4950 "Failed from %s %d\n",
4951 __func__, __LINE__);
62a04f81 4952 goto kill_hba;
d009b576 4953 }
54b28049
S
4954
4955 megasas_refire_mgmt_cmd(instance);
4956
9c915a8c 4957 /* Reset load balance info */
5fc499b6
S
4958 if (fusion->load_balance_info)
4959 memset(fusion->load_balance_info, 0,
4960 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4961 MAX_LOGICAL_DRIVES_EXT));
9c915a8c
AR
4962
4963 if (!megasas_get_map_info(instance))
4964 megasas_sync_map_info(instance);
4965
3761cb4c 4966 megasas_setup_jbod_map(instance);
4967
fdd84e25 4968 /* reset stream detection array */
630d42b7 4969 if (instance->adapter_type >= VENTURA_SERIES) {
fdd84e25
SC
4970 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4971 memset(fusion->stream_detect_by_ld[j],
4972 0, sizeof(struct LD_STREAM_DETECT));
4973 fusion->stream_detect_by_ld[j]->mru_bit_map
4974 = MR_STREAM_BITMAP;
4975 }
4976 }
4977
ac340c5f
SS
4978 clear_bit(MEGASAS_FUSION_IN_RESET,
4979 &instance->reset_flags);
4980 instance->instancet->enable_intr(instance);
62a04f81 4981 megasas_enable_irq_poll(instance);
e9495e2d
S
4982 shost_for_each_device(sdev, shost) {
4983 if ((instance->tgt_prop) &&
4984 (instance->nvme_page_size))
4985 ret_target_prop = megasas_get_target_prop(instance, sdev);
4986
4987 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4988 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4989 }
4990
8a01a41d 4991 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
ac340c5f 4992
223d5818
S
4993 dev_info(&instance->pdev->dev,
4994 "Adapter is OPERATIONAL for scsi:%d\n",
4995 instance->host->host_no);
2d2c2331 4996
229fe47c
AR
4997 /* Restart SR-IOV heartbeat */
4998 if (instance->requestorId) {
4999 if (!megasas_sriov_start_heartbeat(instance, 0))
c251a7be 5000 megasas_start_timer(instance);
229fe47c
AR
5001 else
5002 instance->skip_heartbeat_timer_del = 1;
5003 }
5004
ac340c5f
SS
5005 if (instance->crash_dump_drv_support &&
5006 instance->crash_dump_app_support)
5007 megasas_set_crash_dump_params(instance,
5008 MR_CRASH_BUF_TURN_ON);
5009 else
5010 megasas_set_crash_dump_params(instance,
5011 MR_CRASH_BUF_TURN_OFF);
5012
f0c21df6
S
5013 if (instance->snapdump_wait_time) {
5014 megasas_get_snapdump_properties(instance);
5015 dev_info(&instance->pdev->dev,
5016 "Snap dump wait time\t: %d\n",
5017 instance->snapdump_wait_time);
5018 }
5019
9c915a8c 5020 retval = SUCCESS;
2d2c2331
S
5021
5022 /* Adapter reset completed successfully */
5023 dev_warn(&instance->pdev->dev,
5024 "Reset successful for scsi%d.\n",
5025 instance->host->host_no);
5026
9c915a8c
AR
5027 goto out;
5028 }
5029 /* Reset failed, kill the adapter */
1be18254 5030 dev_warn(&instance->pdev->dev, "Reset failed, killing "
229fe47c 5031 "adapter scsi%d.\n", instance->host->host_no);
62a04f81 5032 goto kill_hba;
9c915a8c 5033 } else {
229fe47c
AR
5034 /* For VF: Restart HB timer if we didn't OCR */
5035 if (instance->requestorId) {
c251a7be 5036 megasas_start_timer(instance);
229fe47c 5037 }
3f0e58bc 5038 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
d46a3ad6 5039 instance->instancet->enable_intr(instance);
62a04f81 5040 megasas_enable_irq_poll(instance);
8a01a41d 5041 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
62a04f81 5042 goto out;
9c915a8c 5043 }
62a04f81
S
5044kill_hba:
5045 megaraid_sas_kill_hba(instance);
5046 megasas_enable_irq_poll(instance);
5047 instance->skip_heartbeat_timer_del = 1;
5048 retval = FAILED;
9c915a8c
AR
5049out:
5050 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5051 mutex_unlock(&instance->reset_mutex);
5052 return retval;
5053}
5054
3f6194af
S
5055/* Fusion Crash dump collection */
5056void megasas_fusion_crash_dump(struct megasas_instance *instance)
fc62b3fc 5057{
fc62b3fc
SS
5058 u32 status_reg;
5059 u8 partial_copy = 0;
3f6194af 5060 int wait = 0;
fc62b3fc
SS
5061
5062
de516379 5063 status_reg = instance->instancet->read_fw_status_reg(instance);
fc62b3fc
SS
5064
5065 /*
5066 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5067 * to host crash buffers
5068 */
5069 if (instance->drv_buf_index == 0) {
5070 /* Buffer is already allocated for old Crash dump.
5071 * Do OCR and do not wait for crash dump collection
5072 */
5073 if (instance->drv_buf_alloc) {
5074 dev_info(&instance->pdev->dev, "earlier crash dump is "
5075 "not yet copied by application, ignoring this "
5076 "crash dump and initiating OCR\n");
5077 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5078 writel(status_reg,
81b76452
S
5079 &instance->reg_set->outbound_scratch_pad_0);
5080 readl(&instance->reg_set->outbound_scratch_pad_0);
fc62b3fc
SS
5081 return;
5082 }
5083 megasas_alloc_host_crash_buffer(instance);
5084 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5085 "allocated: %d\n", instance->drv_buf_alloc);
5086 }
5087
3f6194af
S
5088 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5089 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5090 if (!(status_reg & MFI_STATE_DMADONE)) {
5091 /*
5092 * Next crash dump buffer is not yet DMA'd by FW
5093 * Check after 10ms. Wait for 1 second for FW to
5094 * post the next buffer. If not bail out.
5095 */
5096 wait++;
5097 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5098 status_reg = instance->instancet->read_fw_status_reg(
de516379 5099 instance);
3f6194af
S
5100 continue;
5101 }
5102
5103 wait = 0;
5104 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5105 dev_info(&instance->pdev->dev,
5106 "Driver is done copying the buffer: %d\n",
5107 instance->drv_buf_alloc);
5108 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5109 partial_copy = 1;
5110 break;
5111 } else {
5112 memcpy(instance->crash_buf[instance->drv_buf_index],
5113 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5114 instance->drv_buf_index++;
5115 status_reg &= ~MFI_STATE_DMADONE;
5116 }
5117
81b76452
S
5118 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5119 readl(&instance->reg_set->outbound_scratch_pad_0);
3f6194af
S
5120
5121 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
de516379 5122 status_reg = instance->instancet->read_fw_status_reg(instance);
fc62b3fc
SS
5123 }
5124
5125 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5126 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5127 "of copied buffers: %d\n", instance->drv_buf_index);
5128 instance->fw_crash_buffer_size = instance->drv_buf_index;
5129 instance->fw_crash_state = AVAILABLE;
5130 instance->drv_buf_index = 0;
81b76452
S
5131 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5132 readl(&instance->reg_set->outbound_scratch_pad_0);
fc62b3fc
SS
5133 if (!partial_copy)
5134 megasas_reset_fusion(instance->host, 0);
fc62b3fc
SS
5135 }
5136}
5137
5138
9c915a8c
AR
5139/* Fusion OCR work queue */
5140void megasas_fusion_ocr_wq(struct work_struct *work)
5141{
5142 struct megasas_instance *instance =
5143 container_of(work, struct megasas_instance, work_init);
5144
229fe47c 5145 megasas_reset_fusion(instance->host, 0);
9c915a8c
AR
5146}
5147
5fc499b6
S
5148/* Allocate fusion context */
5149int
5150megasas_alloc_fusion_context(struct megasas_instance *instance)
5151{
5152 struct fusion_context *fusion;
5153
2dd689c8
S
5154 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
5155 GFP_KERNEL);
5fc499b6 5156 if (!instance->ctrl_context) {
2dd689c8
S
5157 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5158 __func__, __LINE__);
5159 return -ENOMEM;
5fc499b6
S
5160 }
5161
5162 fusion = instance->ctrl_context;
5163
2dd689c8
S
5164 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5165 sizeof(LD_SPAN_INFO));
5166 fusion->log_to_span =
5167 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5168 fusion->log_to_span_pages);
5169 if (!fusion->log_to_span) {
fad953ce
KC
5170 fusion->log_to_span =
5171 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5172 sizeof(LD_SPAN_INFO)));
2dd689c8
S
5173 if (!fusion->log_to_span) {
5174 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5175 __func__, __LINE__);
5176 return -ENOMEM;
5177 }
5178 }
5179
5fc499b6
S
5180 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5181 sizeof(struct LD_LOAD_BALANCE_INFO));
5182 fusion->load_balance_info =
5183 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5184 fusion->load_balance_info_pages);
5185 if (!fusion->load_balance_info) {
fad953ce
KC
5186 fusion->load_balance_info =
5187 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5188 sizeof(struct LD_LOAD_BALANCE_INFO)));
5fc499b6
S
5189 if (!fusion->load_balance_info)
5190 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5191 "continuing without Load Balance support\n");
5192 }
5193
5194 return 0;
5195}
5196
5197void
5198megasas_free_fusion_context(struct megasas_instance *instance)
5199{
5200 struct fusion_context *fusion = instance->ctrl_context;
5201
5202 if (fusion) {
5203 if (fusion->load_balance_info) {
5204 if (is_vmalloc_addr(fusion->load_balance_info))
5205 vfree(fusion->load_balance_info);
5206 else
5207 free_pages((ulong)fusion->load_balance_info,
5208 fusion->load_balance_info_pages);
5209 }
5210
2dd689c8
S
5211 if (fusion->log_to_span) {
5212 if (is_vmalloc_addr(fusion->log_to_span))
5213 vfree(fusion->log_to_span);
5214 else
5215 free_pages((ulong)fusion->log_to_span,
5216 fusion->log_to_span_pages);
5217 }
5218
5219 kfree(fusion);
5fc499b6
S
5220 }
5221}
5222
9c915a8c 5223struct megasas_instance_template megasas_instance_template_fusion = {
9c915a8c
AR
5224 .enable_intr = megasas_enable_intr_fusion,
5225 .disable_intr = megasas_disable_intr_fusion,
5226 .clear_intr = megasas_clear_intr_fusion,
5227 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5228 .adp_reset = megasas_adp_reset_fusion,
5229 .check_reset = megasas_check_reset_fusion,
5230 .service_isr = megasas_isr_fusion,
5231 .tasklet = megasas_complete_cmd_dpc_fusion,
5232 .init_adapter = megasas_init_adapter_fusion,
5233 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5234 .issue_dcmd = megasas_issue_dcmd_fusion,
5235};