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