megaraid_sas_fusion.c 123.0 KB
Newer Older
1 2 3
/*
 *  Linux MegaRAID driver for SAS based RAID controllers
 *
4 5
 *  Copyright (c) 2009-2013  LSI Corporation
 *  Copyright (c) 2013-2014  Avago Technologies
6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21
 *
 *  FILE: megaraid_sas_fusion.c
 *
22
 *  Authors: Avago Technologies
23
 *           Sumant Patro
24 25 26
 *           Adam Radford
 *           Kashyap Desai <kashyap.desai@avagotech.com>
 *           Sumit Saxena <sumit.saxena@avagotech.com>
27
 *
28
 *  Send feedback to: megaraidlinux.pdl@avagotech.com
29
 *
30 31
 *  Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
 *  San Jose, California 95131
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 */

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/list.h>
#include <linux/moduleparam.h>
#include <linux/module.h>
#include <linux/spinlock.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/uio.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/compat.h>
#include <linux/blkdev.h>
#include <linux/mutex.h>
#include <linux/poll.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
55
#include <scsi/scsi_dbg.h>
56
#include <linux/dmi.h>
57 58 59 60

#include "megaraid_sas_fusion.h"
#include "megaraid_sas.h"

61

62 63 64 65 66 67 68
extern void megasas_free_cmds(struct megasas_instance *instance);
extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
					   *instance);
extern void
megasas_complete_cmd(struct megasas_instance *instance,
		     struct megasas_cmd *cmd, u8 alt_status);
int
69 70
wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
	      int seconds);
71 72 73 74 75 76 77 78 79

void
megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
int megasas_alloc_cmds(struct megasas_instance *instance);
int
megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
int
megasas_issue_polled(struct megasas_instance *instance,
		     struct megasas_cmd *cmd);
80 81 82
void
megasas_check_and_restore_queue_depth(struct megasas_instance *instance);

83
int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
84 85 86
void megaraid_sas_kill_hba(struct megasas_instance *instance);

extern u32 megasas_dbg_lvl;
87 88 89 90 91 92 93
void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
				  int initial);
void megasas_start_timer(struct megasas_instance *instance,
			struct timer_list *timer,
			 void *fn, unsigned long interval);
extern struct megasas_mgmt_info megasas_mgmt_info;
94
extern unsigned int resetwaittime;
95
extern unsigned int dual_qdepth_disable;
96 97
static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
static void megasas_free_reply_fusion(struct megasas_instance *instance);
98

99 100


101 102 103 104 105
/**
 * megasas_enable_intr_fusion -	Enables interrupts
 * @regs:			MFI register set
 */
void
106
megasas_enable_intr_fusion(struct megasas_instance *instance)
107
{
108 109
	struct megasas_register_set __iomem *regs;
	regs = instance->reg_set;
110 111

	instance->mask_interrupts = 0;
112 113 114 115
	/* For Thunderbolt/Invader also clear intr on enable */
	writel(~0, &regs->outbound_intr_status);
	readl(&regs->outbound_intr_status);

116 117 118 119 120 121 122 123 124 125 126
	writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);

	/* Dummy readl to force pci flush */
	readl(&regs->outbound_intr_mask);
}

/**
 * megasas_disable_intr_fusion - Disables interrupt
 * @regs:			 MFI register set
 */
void
127
megasas_disable_intr_fusion(struct megasas_instance *instance)
128 129 130
{
	u32 mask = 0xFFFFFFFF;
	u32 status;
131 132 133
	struct megasas_register_set __iomem *regs;
	regs = instance->reg_set;
	instance->mask_interrupts = 1;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

	writel(mask, &regs->outbound_intr_mask);
	/* Dummy readl to force pci flush */
	status = readl(&regs->outbound_intr_mask);
}

int
megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
{
	u32 status;
	/*
	 * Check if it is our interrupt
	 */
	status = readl(&regs->outbound_intr_status);

	if (status & 1) {
		writel(status, &regs->outbound_intr_status);
		readl(&regs->outbound_intr_status);
		return 1;
	}
	if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
		return 0;

	return 1;
}

/**
 * megasas_get_cmd_fusion -	Get a command from the free pool
 * @instance:		Adapter soft state
 *
164
 * Returns a blk_tag indexed mpt frame
165
 */
166 167
inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
						  *instance, u32 blk_tag)
168
{
169
	struct fusion_context *fusion;
170

171 172
	fusion = instance->ctrl_context;
	return fusion->cmd_list[blk_tag];
173 174 175 176 177 178 179
}

/**
 * megasas_return_cmd_fusion -	Return a cmd to free command pool
 * @instance:		Adapter soft state
 * @cmd:		Command packet to be returned to free command pool
 */
180 181
inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
	struct megasas_cmd_fusion *cmd)
182 183
{
	cmd->scmd = NULL;
184
	memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
185 186 187
}

/**
188
 * megasas_fire_cmd_fusion -	Sends command to the FW
189
 */
190 191
static void
megasas_fire_cmd_fusion(struct megasas_instance *instance,
192
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, bool is_32bit)
193
{
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	struct megasas_register_set __iomem *regs = instance->reg_set;
	unsigned long flags;

	if (is_32bit)
		writel(le32_to_cpu(req_desc->u.low),
			&(regs)->inbound_single_queue_port);
	else if (instance->is_ventura) {
		spin_lock_irqsave(&instance->hba_lock, flags);
		writel(le32_to_cpu(req_desc->u.low),
			&(regs)->inbound_low_queue_port);
		writel(le32_to_cpu(req_desc->u.high),
			&(regs)->inbound_high_queue_port);
		mmiowb();
		spin_unlock_irqrestore(&instance->hba_lock, flags);
	} else {
209 210 211 212 213 214
#if defined(writeq) && defined(CONFIG_64BIT)
	u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
			le32_to_cpu(req_desc->u.low));

	writeq(req_data, &instance->reg_set->inbound_low_queue_port);
#else
215

216 217 218 219 220
	spin_lock_irqsave(&instance->hba_lock, flags);
	writel(le32_to_cpu(req_desc->u.low),
		&instance->reg_set->inbound_low_queue_port);
	writel(le32_to_cpu(req_desc->u.high),
		&instance->reg_set->inbound_high_queue_port);
T
Tomas Henzl 已提交
221
	mmiowb();
222 223
	spin_unlock_irqrestore(&instance->hba_lock, flags);
#endif
224
	}
225 226
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
/**
 * megasas_fusion_update_can_queue -	Do all Adapter Queue depth related calculations here
 * @instance:							Adapter soft state
 * fw_boot_context:						Whether this function called during probe or after OCR
 *
 * This function is only for fusion controllers.
 * Update host can queue, if firmware downgrade max supported firmware commands.
 * Firmware upgrade case will be skiped because underlying firmware has
 * more resource than exposed to the OS.
 *
 */
static void
megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
{
	u16 cur_max_fw_cmds = 0;
	u16 ldio_threshold = 0;
	struct megasas_register_set __iomem *reg_set;

	reg_set = instance->reg_set;

	cur_max_fw_cmds = readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;

	if (dual_qdepth_disable || !cur_max_fw_cmds)
		cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
	else
		ldio_threshold =
			(instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;

	dev_info(&instance->pdev->dev,
			"Current firmware maximum commands: %d\t LDIO threshold: %d\n",
			cur_max_fw_cmds, ldio_threshold);

	if (fw_boot_context == OCR_CONTEXT) {
		cur_max_fw_cmds = cur_max_fw_cmds - 1;
		if (cur_max_fw_cmds <= instance->max_fw_cmds) {
			instance->cur_can_queue =
				cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
						MEGASAS_FUSION_IOCTL_CMDS);
			instance->host->can_queue = instance->cur_can_queue;
			instance->ldio_threshold = ldio_threshold;
		}
	} else {
		instance->max_fw_cmds = cur_max_fw_cmds;
		instance->ldio_threshold = ldio_threshold;

		if (!instance->is_rdpq)
273 274
			instance->max_fw_cmds =
				min_t(u16, instance->max_fw_cmds, 1024);
275

276 277 278
		if (reset_devices)
			instance->max_fw_cmds = min(instance->max_fw_cmds,
						(u16)MEGASAS_KDUMP_QUEUE_DEPTH);
279 280 281 282 283 284 285 286 287 288 289
		/*
		* Reduce the max supported cmds by 1. This is to ensure that the
		* reply_q_sz (1 more than the max cmd that driver may send)
		* does not exceed max cmds that the FW can support
		*/
		instance->max_fw_cmds = instance->max_fw_cmds-1;

		instance->max_scsi_cmds = instance->max_fw_cmds -
				(MEGASAS_FUSION_INTERNAL_CMDS +
				MEGASAS_FUSION_IOCTL_CMDS);
		instance->cur_can_queue = instance->max_scsi_cmds;
290
		instance->host->can_queue = instance->cur_can_queue;
291
	}
292 293 294 295 296 297

	if (instance->is_ventura)
		instance->max_mpt_cmds =
		instance->max_fw_cmds * RAID_1_10_RMW_CMDS;
	else
		instance->max_mpt_cmds = instance->max_fw_cmds;
298
}
299
/**
300 301
 * megasas_free_cmds_fusion -	Free all the cmds in the free cmd pool
 * @instance:		Adapter soft state
302
 */
303 304
void
megasas_free_cmds_fusion(struct megasas_instance *instance)
305 306 307 308 309
{
	int i;
	struct fusion_context *fusion = instance->ctrl_context;
	struct megasas_cmd_fusion *cmd;

310
	/* SG, Sense */
311
	for (i = 0; i < instance->max_mpt_cmds; i++) {
312
		cmd = fusion->cmd_list[i];
313 314 315
		if (cmd) {
			if (cmd->sg_frame)
				pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
316
				      cmd->sg_frame_phys_addr);
317 318
			if (cmd->sense)
				pci_pool_free(fusion->sense_dma_pool, cmd->sense,
319
				      cmd->sense_phys_addr);
320
		}
321 322
	}

323 324 325 326 327 328 329 330
	if (fusion->sg_dma_pool) {
		pci_pool_destroy(fusion->sg_dma_pool);
		fusion->sg_dma_pool = NULL;
	}
	if (fusion->sense_dma_pool) {
		pci_pool_destroy(fusion->sense_dma_pool);
		fusion->sense_dma_pool = NULL;
	}
331 332


333 334 335 336 337
	/* Reply Frame, Desc*/
	if (instance->is_rdpq)
		megasas_free_rdpq_fusion(instance);
	else
		megasas_free_reply_fusion(instance);
338

339
	/* Request Frame, Desc*/
340
	if (fusion->req_frames_desc)
341 342 343 344
		dma_free_coherent(&instance->pdev->dev,
			fusion->request_alloc_sz, fusion->req_frames_desc,
			fusion->req_frames_desc_phys);
	if (fusion->io_request_frames)
345
		pci_pool_free(fusion->io_request_frames_pool,
346 347 348
			fusion->io_request_frames,
			fusion->io_request_frames_phys);
	if (fusion->io_request_frames_pool) {
349
		pci_pool_destroy(fusion->io_request_frames_pool);
350
		fusion->io_request_frames_pool = NULL;
351 352 353
	}


354
	/* cmd_list */
355
	for (i = 0; i < instance->max_mpt_cmds; i++)
356 357 358 359 360 361
		kfree(fusion->cmd_list[i]);

	kfree(fusion->cmd_list);
}

/**
362
 * megasas_create_sg_sense_fusion -	Creates DMA pool for cmd frames
363 364 365
 * @instance:			Adapter soft state
 *
 */
366
static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
367 368 369 370 371 372 373 374 375 376
{
	int i;
	u32 max_cmd;
	struct fusion_context *fusion;
	struct megasas_cmd_fusion *cmd;

	fusion = instance->ctrl_context;
	max_cmd = instance->max_fw_cmds;


377 378 379 380 381 382 383
	fusion->sg_dma_pool =
			pci_pool_create("mr_sg", instance->pdev,
				instance->max_chain_frame_sz, 4, 0);
	/* SCSI_SENSE_BUFFERSIZE  = 96 bytes */
	fusion->sense_dma_pool =
			pci_pool_create("mr_sense", instance->pdev,
				SCSI_SENSE_BUFFERSIZE, 64, 0);
384

385 386 387
	if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
388 389 390 391 392 393 394 395 396
		return -ENOMEM;
	}

	/*
	 * Allocate and attach a frame to each of the commands in cmd_list
	 */
	for (i = 0; i < max_cmd; i++) {
		cmd = fusion->cmd_list[i];
		cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
397
					GFP_KERNEL, &cmd->sg_frame_phys_addr);
398 399

		cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
400
					GFP_KERNEL, &cmd->sense_phys_addr);
401
		if (!cmd->sg_frame || !cmd->sense) {
402 403
			dev_err(&instance->pdev->dev,
				"Failed from %s %d\n",  __func__, __LINE__);
404 405 406
			return -ENOMEM;
		}
	}
407 408 409 410 411 412 413 414 415 416 417 418 419

	/* create sense buffer for the raid 1/10 fp */
	for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
		cmd = fusion->cmd_list[i];
		cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
			GFP_KERNEL, &cmd->sense_phys_addr);
		if (!cmd->sense) {
			dev_err(&instance->pdev->dev,
				"Failed from %s %d\n",  __func__, __LINE__);
			return -ENOMEM;
		}
	}

420 421 422 423
	return 0;
}

int
424
megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
425
{
426
	u32 max_mpt_cmd, i;
427 428 429 430
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;

431
	max_mpt_cmd = instance->max_mpt_cmds;
432

433 434 435 436 437
	/*
	 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
	 * Allocate the dynamic array first and then allocate individual
	 * commands.
	 */
438 439 440
	fusion->cmd_list =
		kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
			GFP_KERNEL);
441 442 443 444 445 446
	if (!fusion->cmd_list) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
	}

447 448 449


	for (i = 0; i < max_mpt_cmd; i++) {
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
		fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
					      GFP_KERNEL);
		if (!fusion->cmd_list[i]) {
			dev_err(&instance->pdev->dev,
				"Failed from %s %d\n",  __func__, __LINE__);
			return -ENOMEM;
		}
	}
	return 0;
}
int
megasas_alloc_request_fusion(struct megasas_instance *instance)
{
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;

467 468
	fusion->req_frames_desc =
		dma_alloc_coherent(&instance->pdev->dev,
469 470
			fusion->request_alloc_sz,
			&fusion->req_frames_desc_phys, GFP_KERNEL);
471
	if (!fusion->req_frames_desc) {
472 473 474
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
475 476
	}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
	fusion->io_request_frames_pool =
			pci_pool_create("mr_ioreq", instance->pdev,
				fusion->io_frames_alloc_sz, 16, 0);

	if (!fusion->io_request_frames_pool) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
	}

	fusion->io_request_frames =
			pci_pool_alloc(fusion->io_request_frames_pool,
				GFP_KERNEL, &fusion->io_request_frames_phys);
	if (!fusion->io_request_frames) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
	}
	return 0;
}

int
megasas_alloc_reply_fusion(struct megasas_instance *instance)
{
	int i, count;
	struct fusion_context *fusion;
	union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
	fusion = instance->ctrl_context;

506
	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
507
	fusion->reply_frames_desc_pool =
508
			pci_pool_create("mr_reply", instance->pdev,
509
				fusion->reply_alloc_sz * count, 16, 0);
510 511

	if (!fusion->reply_frames_desc_pool) {
512 513 514
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
515 516
	}

517 518 519 520 521 522 523
	fusion->reply_frames_desc[0] =
		pci_pool_alloc(fusion->reply_frames_desc_pool,
			GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
	if (!fusion->reply_frames_desc[0]) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
524
	}
525
	reply_desc = fusion->reply_frames_desc[0];
526
	for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
527
		reply_desc->Words = cpu_to_le64(ULLONG_MAX);
528

529 530 531 532 533 534 535
	/* This is not a rdpq mode, but driver still populate
	 * reply_frame_desc array to use same msix index in ISR path.
	 */
	for (i = 0; i < (count - 1); i++)
		fusion->reply_frames_desc[i + 1] =
			fusion->reply_frames_desc[i] +
			(fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
536

537 538
	return 0;
}
539

540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
int
megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
{
	int i, j, count;
	struct fusion_context *fusion;
	union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;

	fusion = instance->ctrl_context;

	fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
				sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
				&fusion->rdpq_phys);
	if (!fusion->rdpq_virt) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
556 557
	}

558 559 560 561 562 563 564 565 566 567
	memset(fusion->rdpq_virt, 0,
			sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
	fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
							 instance->pdev, fusion->reply_alloc_sz, 16, 0);

	if (!fusion->reply_frames_desc_pool) {
		dev_err(&instance->pdev->dev,
			"Failed from %s %d\n",  __func__, __LINE__);
		return -ENOMEM;
568 569
	}

570 571 572 573 574 575 576 577 578
	for (i = 0; i < count; i++) {
		fusion->reply_frames_desc[i] =
				pci_pool_alloc(fusion->reply_frames_desc_pool,
					GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
		if (!fusion->reply_frames_desc[i]) {
			dev_err(&instance->pdev->dev,
				"Failed from %s %d\n",  __func__, __LINE__);
			return -ENOMEM;
		}
579

580 581 582 583 584 585
		fusion->rdpq_virt[i].RDPQBaseAddress =
			fusion->reply_frames_desc_phys[i];

		reply_desc = fusion->reply_frames_desc[i];
		for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
			reply_desc->Words = cpu_to_le64(ULLONG_MAX);
586
	}
587 588
	return 0;
}
589

590 591
static void
megasas_free_rdpq_fusion(struct megasas_instance *instance) {
592

593 594
	int i;
	struct fusion_context *fusion;
595

596 597 598 599 600 601 602
	fusion = instance->ctrl_context;

	for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
		if (fusion->reply_frames_desc[i])
			pci_pool_free(fusion->reply_frames_desc_pool,
				fusion->reply_frames_desc[i],
				fusion->reply_frames_desc_phys[i]);
603 604
	}

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
	if (fusion->reply_frames_desc_pool)
		pci_pool_destroy(fusion->reply_frames_desc_pool);

	if (fusion->rdpq_virt)
		pci_free_consistent(instance->pdev,
			sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
			fusion->rdpq_virt, fusion->rdpq_phys);
}

static void
megasas_free_reply_fusion(struct megasas_instance *instance) {

	struct fusion_context *fusion;

	fusion = instance->ctrl_context;

	if (fusion->reply_frames_desc[0])
		pci_pool_free(fusion->reply_frames_desc_pool,
			fusion->reply_frames_desc[0],
			fusion->reply_frames_desc_phys[0]);

	if (fusion->reply_frames_desc_pool)
		pci_pool_destroy(fusion->reply_frames_desc_pool);

}


/**
 * megasas_alloc_cmds_fusion -	Allocates the command packets
 * @instance:		Adapter soft state
 *
 *
 * Each frame has a 32-bit field called context. This context is used to get
 * back the megasas_cmd_fusion from the frame when a frame gets completed
 * In this driver, the 32 bit values are the indices into an array cmd_list.
 * This array is used only to look up the megasas_cmd_fusion given the context.
 * The free commands themselves are maintained in a linked list called cmd_pool.
 *
 * cmds are formed in the io_request and sg_frame members of the
 * megasas_cmd_fusion. The context field is used to get a request descriptor
 * and is used as SMID of the cmd.
 * SMID value range is from 1 to max_fw_cmds.
 */
int
megasas_alloc_cmds_fusion(struct megasas_instance *instance)
{
	int i;
	struct fusion_context *fusion;
	struct megasas_cmd_fusion *cmd;
	u32 offset;
	dma_addr_t io_req_base_phys;
	u8 *io_req_base;


	fusion = instance->ctrl_context;

	if (megasas_alloc_cmdlist_fusion(instance))
		goto fail_exit;

	if (megasas_alloc_request_fusion(instance))
		goto fail_exit;

	if (instance->is_rdpq) {
		if (megasas_alloc_rdpq_fusion(instance))
			goto fail_exit;
	} else
		if (megasas_alloc_reply_fusion(instance))
			goto fail_exit;


	/* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
	io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
	io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
678 679 680 681 682 683

	/*
	 * Add all the commands to command pool (fusion->cmd_pool)
	 */

	/* SMID 0 is reserved. Set SMID/index from 1 */
684
	for (i = 0; i < instance->max_mpt_cmds; i++) {
685 686 687 688 689
		cmd = fusion->cmd_list[i];
		offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
		memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
		cmd->index = i + 1;
		cmd->scmd = NULL;
690 691
		cmd->sync_cmd_idx =
		(i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
692 693
				(i - instance->max_scsi_cmds) :
				(u32)ULONG_MAX; /* Set to Invalid */
694 695 696 697 698 699 700
		cmd->instance = instance;
		cmd->io_request =
			(struct MPI2_RAID_SCSI_IO_REQUEST *)
		  (io_req_base + offset);
		memset(cmd->io_request, 0,
		       sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
		cmd->io_request_phys_addr = io_req_base_phys + offset;
701
		cmd->is_raid_1_fp_write = 0;
702 703
	}

704 705
	if (megasas_create_sg_sense_fusion(instance))
		goto fail_exit;
706 707 708

	return 0;

709 710
fail_exit:
	megasas_free_cmds_fusion(instance);
711 712 713 714 715 716 717 718 719 720 721
	return -ENOMEM;
}

/**
 * wait_and_poll -	Issues a polling command
 * @instance:			Adapter soft state
 * @cmd:			Command packet to be issued
 *
 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
 */
int
722 723
wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
	int seconds)
724 725 726
{
	int i;
	struct megasas_header *frame_hdr = &cmd->frame->hdr;
727
	struct fusion_context *fusion;
728

729
	u32 msecs = seconds * 1000;
730

731
	fusion = instance->ctrl_context;
732 733 734 735 736 737 738 739
	/*
	 * Wait for cmd_status to change
	 */
	for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
		rmb();
		msleep(20);
	}

740 741 742 743 744 745
	if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
		return DCMD_TIMEOUT;
	else if (frame_hdr->cmd_status == MFI_STAT_OK)
		return DCMD_SUCCESS;
	else
		return DCMD_FAILED;
746 747 748 749 750 751 752 753 754 755 756 757
}

/**
 * megasas_ioc_init_fusion -	Initializes the FW
 * @instance:		Adapter soft state
 *
 * Issues the IOC Init cmd
 */
int
megasas_ioc_init_fusion(struct megasas_instance *instance)
{
	struct megasas_init_frame *init_frame;
758
	struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
759 760
	dma_addr_t	ioc_init_handle;
	struct megasas_cmd *cmd;
761
	u8 ret, cur_rdpq_mode;
762
	struct fusion_context *fusion;
763
	union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
764 765
	int i;
	struct megasas_header *frame_hdr;
766
	const char *sys_info;
767
	MFI_CAPABILITIES *drv_ops;
768
	u32 scratch_pad_2;
769 770 771 772 773 774

	fusion = instance->ctrl_context;

	cmd = megasas_get_cmd(instance);

	if (!cmd) {
775
		dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
776 777 778 779
		ret = 1;
		goto fail_get_cmd;
	}

780 781 782 783 784 785 786 787 788 789 790 791
	scratch_pad_2 = readl
		(&instance->reg_set->outbound_scratch_pad_2);

	cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;

	if (instance->is_rdpq && !cur_rdpq_mode) {
		dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
			" from RDPQ mode to non RDPQ mode\n");
		ret = 1;
		goto fail_fw_init;
	}

792 793 794 795 796
	instance->fw_sync_cache_support = (scratch_pad_2 &
		MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
	dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
		 instance->fw_sync_cache_support ? "Yes" : "No");

797 798 799 800 801 802
	IOCInitMessage =
	  dma_alloc_coherent(&instance->pdev->dev,
			     sizeof(struct MPI2_IOC_INIT_REQUEST),
			     &ioc_init_handle, GFP_KERNEL);

	if (!IOCInitMessage) {
803
		dev_err(&instance->pdev->dev, "Could not allocate memory for "
804 805 806 807 808 809 810 811 812
		       "IOCInitMessage\n");
		ret = 1;
		goto fail_fw_init;
	}

	memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));

	IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
	IOCInitMessage->WhoInit	= MPI2_WHOINIT_HOST_DRIVER;
813 814 815 816 817
	IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
	IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
	IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);

	IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
818 819 820 821 822
	IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
			cpu_to_le64(fusion->rdpq_phys) :
			cpu_to_le64(fusion->reply_frames_desc_phys[0]);
	IOCInitMessage->MsgFlags = instance->is_rdpq ?
			MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
823
	IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
824
	IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
825 826 827 828 829
	init_frame = (struct megasas_init_frame *)cmd->frame;
	memset(init_frame, 0, MEGAMFI_FRAME_SIZE);

	frame_hdr = &cmd->frame->hdr;
	frame_hdr->cmd_status = 0xFF;
830 831 832
	frame_hdr->flags = cpu_to_le16(
		le16_to_cpu(frame_hdr->flags) |
		MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
833 834 835 836

	init_frame->cmd	= MFI_CMD_INIT;
	init_frame->cmd_status = 0xFF;

837 838
	drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);

839
	/* driver support Extended MSIX */
840
	if (fusion->adapter_type == INVADER_SERIES)
841
		drv_ops->mfi_capabilities.support_additional_msix = 1;
842
	/* driver supports HA / Remote LUN over Fast Path interface */
843 844 845 846 847 848
	drv_ops->mfi_capabilities.support_fp_remote_lun = 1;

	drv_ops->mfi_capabilities.support_max_255lds = 1;
	drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
	drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;

849 850 851
	if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
		drv_ops->mfi_capabilities.support_ext_io_size = 1;

852
	drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
853 854
	if (!dual_qdepth_disable)
		drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
855

856
	drv_ops->mfi_capabilities.support_qd_throttling = 1;
857 858
	/* Convert capability to LE32 */
	cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
859

860 861 862 863 864 865 866 867 868 869
	sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
	if (instance->system_info_buf && sys_info) {
		memcpy(instance->system_info_buf->systemId, sys_info,
			strlen(sys_info) > 64 ? 64 : strlen(sys_info));
		instance->system_info_buf->systemIdLength =
			strlen(sys_info) > 64 ? 64 : strlen(sys_info);
		init_frame->system_info_lo = instance->system_info_h;
		init_frame->system_info_hi = 0;
	}

870 871 872 873
	init_frame->queue_info_new_phys_addr_hi =
		cpu_to_le32(upper_32_bits(ioc_init_handle));
	init_frame->queue_info_new_phys_addr_lo =
		cpu_to_le32(lower_32_bits(ioc_init_handle));
874
	init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
875

876 877
	req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
	req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
878
	req_desc.MFAIo.RequestFlags =
879
		(MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
880
		MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
881 882 883 884

	/*
	 * disable the intr before firing the init frame
	 */
885
	instance->instancet->disable_intr(instance);
886 887 888 889 890 891 892 893

	for (i = 0; i < (10 * 1000); i += 20) {
		if (readl(&instance->reg_set->doorbell) & 1)
			msleep(20);
		else
			break;
	}

894
	megasas_fire_cmd_fusion(instance, &req_desc, false);
895

896
	wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
897 898 899 900 901 902

	frame_hdr = &cmd->frame->hdr;
	if (frame_hdr->cmd_status != 0) {
		ret = 1;
		goto fail_fw_init;
	}
903
	dev_info(&instance->pdev->dev, "Init cmd success\n");
904 905 906 907 908 909 910 911 912 913 914 915 916

	ret = 0;

fail_fw_init:
	megasas_return_cmd(instance, cmd);
	if (IOCInitMessage)
		dma_free_coherent(&instance->pdev->dev,
				  sizeof(struct MPI2_IOC_INIT_REQUEST),
				  IOCInitMessage, ioc_init_handle);
fail_get_cmd:
	return ret;
}

917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
/**
 * megasas_sync_pd_seq_num -	JBOD SEQ MAP
 * @instance:		Adapter soft state
 * @pend:		set to 1, if it is pended jbod map.
 *
 * Issue Jbod map to the firmware. If it is pended command,
 * issue command and return. If it is first instance of jbod map
 * issue and receive command.
 */
int
megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
	int ret = 0;
	u32 pd_seq_map_sz;
	struct megasas_cmd *cmd;
	struct megasas_dcmd_frame *dcmd;
	struct fusion_context *fusion = instance->ctrl_context;
	struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
	dma_addr_t pd_seq_h;

	pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
	pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
	pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
			(sizeof(struct MR_PD_CFG_SEQ) *
			(MAX_PHYSICAL_DEVICES - 1));

	cmd = megasas_get_cmd(instance);
	if (!cmd) {
		dev_err(&instance->pdev->dev,
			"Could not get mfi cmd. Fail from %s %d\n",
			__func__, __LINE__);
		return -ENOMEM;
	}

	dcmd = &cmd->frame->dcmd;

	memset(pd_sync, 0, pd_seq_map_sz);
	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
	dcmd->cmd = MFI_CMD_DCMD;
	dcmd->cmd_status = 0xFF;
	dcmd->sge_count = 1;
	dcmd->timeout = 0;
	dcmd->pad_0 = 0;
	dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
	dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
	dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);

	if (pend) {
		dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
		dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
		instance->jbod_seq_cmd = cmd;
		instance->instancet->issue_dcmd(instance, cmd);
		return 0;
	}

	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);

	/* Below code is only for non pended DCMD */
	if (instance->ctrl_context && !instance->mask_interrupts)
976 977
		ret = megasas_issue_blocked_cmd(instance, cmd,
			MFI_IO_TIMEOUT_SECS);
978 979 980 981 982 983 984 985 986 987
	else
		ret = megasas_issue_polled(instance, cmd);

	if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
		dev_warn(&instance->pdev->dev,
			"driver supports max %d JBOD, but FW reports %d\n",
			MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
		ret = -EINVAL;
	}

988 989 990 991
	if (ret == DCMD_TIMEOUT && instance->ctrl_context)
		megaraid_sas_kill_hba(instance);

	if (ret == DCMD_SUCCESS)
992 993 994 995 996 997
		instance->pd_seq_map_id++;

	megasas_return_cmd(instance, cmd);
	return ret;
}

998 999 1000 1001 1002 1003 1004
/*
 * megasas_get_ld_map_info -	Returns FW's ld_map structure
 * @instance:				Adapter soft state
 * @pend:				Pend the command or not
 * Issues an internal command (DCMD) to get the FW's controller PD
 * list structure.  This information is mainly used to find out SYSTEM
 * supported by the FW.
1005 1006 1007 1008 1009 1010 1011
 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
 * dcmd.mbox.b[0]	- number of LDs being sync'd
 * dcmd.mbox.b[1]	- 0 - complete command immediately.
 *			- 1 - pend till config change
 * dcmd.mbox.b[2]	- 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
 *			- 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
 *				uses extended struct MR_FW_RAID_MAP_EXT
1012 1013 1014 1015 1016 1017 1018
 */
static int
megasas_get_ld_map_info(struct megasas_instance *instance)
{
	int ret = 0;
	struct megasas_cmd *cmd;
	struct megasas_dcmd_frame *dcmd;
1019
	void *ci;
1020 1021 1022 1023 1024 1025 1026
	dma_addr_t ci_h = 0;
	u32 size_map_info;
	struct fusion_context *fusion;

	cmd = megasas_get_cmd(instance);

	if (!cmd) {
1027
		dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1028 1029 1030 1031 1032 1033 1034
		return -ENOMEM;
	}

	fusion = instance->ctrl_context;

	if (!fusion) {
		megasas_return_cmd(instance, cmd);
1035
		return -ENXIO;
1036 1037 1038 1039
	}

	dcmd = &cmd->frame->dcmd;

1040
	size_map_info = fusion->current_map_sz;
1041

1042
	ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1043 1044 1045
	ci_h = fusion->ld_map_phys[(instance->map_id & 1)];

	if (!ci) {
1046
		dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1047 1048 1049 1050
		megasas_return_cmd(instance, cmd);
		return -ENOMEM;
	}

1051
	memset(ci, 0, fusion->max_map_sz);
1052
	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1053 1054 1055 1056 1057
#if VD_EXT_DEBUG
	dev_dbg(&instance->pdev->dev,
		"%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
		__func__, cpu_to_le32(size_map_info));
#endif
1058 1059 1060
	dcmd->cmd = MFI_CMD_DCMD;
	dcmd->cmd_status = 0xFF;
	dcmd->sge_count = 1;
1061
	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
1062 1063
	dcmd->timeout = 0;
	dcmd->pad_0 = 0;
1064 1065 1066 1067
	dcmd->data_xfer_len = cpu_to_le32(size_map_info);
	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
	dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
1068

1069 1070
	if (instance->ctrl_context && !instance->mask_interrupts)
		ret = megasas_issue_blocked_cmd(instance, cmd,
1071
			MFI_IO_TIMEOUT_SECS);
1072 1073
	else
		ret = megasas_issue_polled(instance, cmd);
1074

1075 1076 1077
	if (ret == DCMD_TIMEOUT && instance->ctrl_context)
		megaraid_sas_kill_hba(instance);

1078
	megasas_return_cmd(instance, cmd);
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089

	return ret;
}

u8
megasas_get_map_info(struct megasas_instance *instance)
{
	struct fusion_context *fusion = instance->ctrl_context;

	fusion->fast_path_io = 0;
	if (!megasas_get_ld_map_info(instance)) {
1090
		if (MR_ValidateMapInfo(instance)) {
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
			fusion->fast_path_io = 1;
			return 0;
		}
	}
	return 1;
}

/*
 * megasas_sync_map_info -	Returns FW's ld_map structure
 * @instance:				Adapter soft state
 *
 * Issues an internal command (DCMD) to get the FW's controller PD
 * list structure.  This information is mainly used to find out SYSTEM
 * supported by the FW.
 */
int
megasas_sync_map_info(struct megasas_instance *instance)
{
	int ret = 0, i;
	struct megasas_cmd *cmd;
	struct megasas_dcmd_frame *dcmd;
	u32 size_sync_info, num_lds;
	struct fusion_context *fusion;
	struct MR_LD_TARGET_SYNC *ci = NULL;
1115
	struct MR_DRV_RAID_MAP_ALL *map;
1116 1117 1118 1119 1120 1121 1122 1123
	struct MR_LD_RAID  *raid;
	struct MR_LD_TARGET_SYNC *ld_sync;
	dma_addr_t ci_h = 0;
	u32 size_map_info;

	cmd = megasas_get_cmd(instance);

	if (!cmd) {
1124
		dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
		return -ENOMEM;
	}

	fusion = instance->ctrl_context;

	if (!fusion) {
		megasas_return_cmd(instance, cmd);
		return 1;
	}

1135
	map = fusion->ld_drv_map[instance->map_id & 1];
1136

1137
	num_lds = le16_to_cpu(map->raidMap.ldCount);
1138 1139 1140 1141 1142 1143 1144 1145 1146

	dcmd = &cmd->frame->dcmd;

	size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;

	memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);

	ci = (struct MR_LD_TARGET_SYNC *)
	  fusion->ld_map[(instance->map_id - 1) & 1];
1147
	memset(ci, 0, fusion->max_map_sz);
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158

	ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];

	ld_sync = (struct MR_LD_TARGET_SYNC *)ci;

	for (i = 0; i < num_lds; i++, ld_sync++) {
		raid = MR_LdRaidGet(i, map);
		ld_sync->targetId = MR_GetLDTgtId(i, map);
		ld_sync->seqNum = raid->seqNum;
	}

1159
	size_map_info = fusion->current_map_sz;
1160 1161 1162 1163

	dcmd->cmd = MFI_CMD_DCMD;
	dcmd->cmd_status = 0xFF;
	dcmd->sge_count = 1;
1164
	dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
1165 1166
	dcmd->timeout = 0;
	dcmd->pad_0 = 0;
1167
	dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1168 1169
	dcmd->mbox.b[0] = num_lds;
	dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1170 1171 1172
	dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
	dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
	dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
1173 1174 1175 1176 1177 1178 1179 1180

	instance->map_update_cmd = cmd;

	instance->instancet->issue_dcmd(instance, cmd);

	return ret;
}

1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235
/*
 * meagasas_display_intel_branding - Display branding string
 * @instance: per adapter object
 *
 * Return nothing.
 */
static void
megasas_display_intel_branding(struct megasas_instance *instance)
{
	if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
		return;

	switch (instance->pdev->device) {
	case PCI_DEVICE_ID_LSI_INVADER:
		switch (instance->pdev->subsystem_device) {
		case MEGARAID_INTEL_RS3DC080_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3DC080_BRANDING);
			break;
		case MEGARAID_INTEL_RS3DC040_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3DC040_BRANDING);
			break;
		case MEGARAID_INTEL_RS3SC008_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3SC008_BRANDING);
			break;
		case MEGARAID_INTEL_RS3MC044_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3MC044_BRANDING);
			break;
		default:
			break;
		}
		break;
	case PCI_DEVICE_ID_LSI_FURY:
		switch (instance->pdev->subsystem_device) {
		case MEGARAID_INTEL_RS3WC080_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3WC080_BRANDING);
			break;
		case MEGARAID_INTEL_RS3WC040_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RS3WC040_BRANDING);
			break;
		default:
			break;
		}
		break;
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
	case PCI_DEVICE_ID_LSI_CUTLASS_52:
	case PCI_DEVICE_ID_LSI_CUTLASS_53:
		switch (instance->pdev->subsystem_device) {
		case MEGARAID_INTEL_RMS3BC160_SSDID:
			dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
				instance->host->host_no,
				MEGARAID_INTEL_RMS3BC160_BRANDING);
			break;
		default:
			break;
		}
		break;
1248 1249 1250 1251 1252
	default:
		break;
	}
}

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
/**
 * megasas_init_adapter_fusion -	Initializes the FW
 * @instance:		Adapter soft state
 *
 * This is the main function for initializing firmware.
 */
u32
megasas_init_adapter_fusion(struct megasas_instance *instance)
{
	struct megasas_register_set __iomem *reg_set;
	struct fusion_context *fusion;
1264
	u32 max_cmd, scratch_pad_2;
1265
	int i = 0, count;
1266 1267 1268 1269 1270

	fusion = instance->ctrl_context;

	reg_set = instance->reg_set;

1271
	megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1272 1273 1274 1275 1276 1277 1278

	/*
	 * Reduce the max supported cmds by 1. This is to ensure that the
	 * reply_q_sz (1 more than the max cmd that driver may send)
	 * does not exceed max cmds that the FW can support
	 */
	instance->max_fw_cmds = instance->max_fw_cmds-1;
1279 1280 1281 1282 1283 1284

	/*
	 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
	 */
	instance->max_mfi_cmds =
		MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1285 1286 1287

	max_cmd = instance->max_fw_cmds;

1288
	fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
1289 1290

	fusion->request_alloc_sz =
1291
	sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * instance->max_mpt_cmds;
1292 1293 1294
	fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
		*(fusion->reply_q_depth);
	fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1295 1296
		(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
		* (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1297

1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
	/* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
	 * Firmware support extended IO chain frame which is 4 times more than
	 * legacy Firmware.
	 * Legacy Firmware - Frame size is (8 * 128) = 1K
	 * 1M IO Firmware  - Frame size is (8 * 128 * 4)  = 4K
	 */
	if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
		instance->max_chain_frame_sz =
			((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
			MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
	else
		instance->max_chain_frame_sz =
			((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
			MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;

	if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
		dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
			instance->max_chain_frame_sz,
			MEGASAS_CHAIN_FRAME_SZ_MIN);
		instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
	}

1321
	fusion->max_sge_in_main_msg =
1322 1323
		(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
			- offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1324 1325

	fusion->max_sge_in_chain =
1326 1327
		instance->max_chain_frame_sz
			/ sizeof(union MPI2_SGE_IO_UNION);
1328

1329 1330 1331
	instance->max_num_sge =
		rounddown_pow_of_two(fusion->max_sge_in_main_msg
			+ fusion->max_sge_in_chain - 2);
1332 1333 1334 1335 1336 1337 1338 1339 1340

	/* Used for pass thru MFI frame (DCMD) */
	fusion->chain_offset_mfi_pthru =
		offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;

	fusion->chain_offset_io_request =
		(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
		 sizeof(union MPI2_SGE_IO_UNION))/16;

1341 1342 1343
	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
	for (i = 0 ; i < count; i++)
		fusion->last_reply_idx[i] = 0;
1344

1345 1346 1347 1348 1349 1350 1351 1352 1353
	/*
	 * For fusion adapters, 3 commands for IOCTL and 5 commands
	 * for driver's internal DCMDs.
	 */
	instance->max_scsi_cmds = instance->max_fw_cmds -
				(MEGASAS_FUSION_INTERNAL_CMDS +
				MEGASAS_FUSION_IOCTL_CMDS);
	sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
	/*
	 * Allocate memory for descriptors
	 * Create a pool of commands
	 */
	if (megasas_alloc_cmds(instance))
		goto fail_alloc_mfi_cmds;
	if (megasas_alloc_cmds_fusion(instance))
		goto fail_alloc_cmds;

	if (megasas_ioc_init_fusion(instance))
		goto fail_ioc_init;

1366
	megasas_display_intel_branding(instance);
1367
	if (megasas_get_ctrl_info(instance)) {
1368 1369 1370 1371 1372 1373
		dev_err(&instance->pdev->dev,
			"Could not get controller info. Fail from %s %d\n",
			__func__, __LINE__);
		goto fail_ioc_init;
	}

1374
	instance->flag_ieee = 1;
1375
	fusion->fast_path_io = 0;
1376

1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
	fusion->drv_map_pages = get_order(fusion->drv_map_sz);
	for (i = 0; i < 2; i++) {
		fusion->ld_map[i] = NULL;
		fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
			fusion->drv_map_pages);
		if (!fusion->ld_drv_map[i]) {
			dev_err(&instance->pdev->dev, "Could not allocate "
				"memory for local map info for %d pages\n",
				fusion->drv_map_pages);
			if (i == 1)
				free_pages((ulong)fusion->ld_drv_map[0],
					fusion->drv_map_pages);
			goto fail_ioc_init;
		}
1391 1392
		memset(fusion->ld_drv_map[i], 0,
			((1 << PAGE_SHIFT) << fusion->drv_map_pages));
1393 1394
	}

1395 1396
	for (i = 0; i < 2; i++) {
		fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1397
						       fusion->max_map_sz,
1398 1399 1400
						       &fusion->ld_map_phys[i],
						       GFP_KERNEL);
		if (!fusion->ld_map[i]) {
1401
			dev_err(&instance->pdev->dev, "Could not allocate memory "
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
			       "for map info\n");
			goto fail_map_info;
		}
	}

	if (!megasas_get_map_info(instance))
		megasas_sync_map_info(instance);

	return 0;

fail_map_info:
	if (i == 1)
1414
		dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
1415 1416
				  fusion->ld_map[0], fusion->ld_map_phys[0]);
fail_ioc_init:
1417 1418 1419 1420
	megasas_free_cmds_fusion(instance);
fail_alloc_cmds:
	megasas_free_cmds(instance);
fail_alloc_mfi_cmds:
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	return 1;
}

/**
 * map_cmd_status -	Maps FW cmd status to OS cmd status
 * @cmd :		Pointer to cmd
 * @status :		status of cmd returned by FW
 * @ext_status :	ext status of cmd returned by FW
 */

void
1432 1433 1434
map_cmd_status(struct fusion_context *fusion,
	struct scsi_cmnd *scmd, u8 status, u8 ext_status,
			u32 data_length, u8 *sense)
1435 1436 1437 1438 1439
{

	switch (status) {

	case MFI_STAT_OK:
1440
		scmd->result = DID_OK << 16;
1441 1442 1443 1444
		break;

	case MFI_STAT_SCSI_IO_FAILED:
	case MFI_STAT_LD_INIT_IN_PROGRESS:
1445
		scmd->result = (DID_ERROR << 16) | ext_status;
1446 1447 1448 1449
		break;

	case MFI_STAT_SCSI_DONE_WITH_ERROR:

1450
		scmd->result = (DID_OK << 16) | ext_status;
1451
		if (ext_status == SAM_STAT_CHECK_CONDITION) {
1452
			memset(scmd->sense_buffer, 0,
1453
			       SCSI_SENSE_BUFFERSIZE);
1454
			memcpy(scmd->sense_buffer, sense,
1455
			       SCSI_SENSE_BUFFERSIZE);
1456
			scmd->result |= DRIVER_SENSE << 24;
1457 1458 1459 1460 1461
		}
		break;

	case MFI_STAT_LD_OFFLINE:
	case MFI_STAT_DEVICE_NOT_FOUND:
1462
		scmd->result = DID_BAD_TARGET << 16;
1463
		break;
1464
	case MFI_STAT_CONFIG_SEQ_MISMATCH:
1465
		scmd->result = DID_IMM_RETRY << 16;
1466
		break;
1467
	default:
1468
		scmd->result = DID_ERROR << 16;
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
		break;
	}
}

/**
 * megasas_make_sgl_fusion -	Prepares 32-bit SGL
 * @instance:		Adapter soft state
 * @scp:		SCSI command from the mid-layer
 * @sgl_ptr:		SGL to be filled in
 * @cmd:		cmd we are working on
 *
 * If successful, this function returns the number of SG elements.
 */
static int
megasas_make_sgl_fusion(struct megasas_instance *instance,
			struct scsi_cmnd *scp,
			struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
			struct megasas_cmd_fusion *cmd)
{
1488
	int i, sg_processed, sge_count;
1489 1490 1491 1492 1493
	struct scatterlist *os_sgl;
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;

1494
	if (fusion->adapter_type == INVADER_SERIES) {
1495 1496 1497 1498
		struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
		sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
		sgl_ptr_end->Flags = 0;
	}
1499 1500 1501 1502 1503 1504 1505 1506 1507

	sge_count = scsi_dma_map(scp);

	BUG_ON(sge_count < 0);

	if (sge_count > instance->max_num_sge || !sge_count)
		return sge_count;

	scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1508 1509
		sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
		sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1510
		sgl_ptr->Flags = 0;
1511
		if (fusion->adapter_type == INVADER_SERIES)
1512 1513
			if (i == sge_count - 1)
				sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1514 1515 1516 1517 1518 1519 1520 1521
		sgl_ptr++;

		sg_processed = i + 1;

		if ((sg_processed ==  (fusion->max_sge_in_main_msg - 1)) &&
		    (sge_count > fusion->max_sge_in_main_msg)) {

			struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1522
			if (fusion->adapter_type == INVADER_SERIES) {
1523 1524 1525
				if ((le16_to_cpu(cmd->io_request->IoFlags) &
					MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
					MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1526 1527 1528 1529 1530 1531 1532 1533 1534
					cmd->io_request->ChainOffset =
						fusion->
						chain_offset_io_request;
				else
					cmd->io_request->ChainOffset = 0;
			} else
				cmd->io_request->ChainOffset =
					fusion->chain_offset_io_request;

1535 1536 1537
			sg_chain = sgl_ptr;
			/* Prepare chain element */
			sg_chain->NextChainOffset = 0;
1538
			if (fusion->adapter_type == INVADER_SERIES)
1539 1540 1541 1542 1543
				sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
			else
				sg_chain->Flags =
					(IEEE_SGE_FLAGS_CHAIN_ELEMENT |
					 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1544 1545
			sg_chain->Length =  cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
			sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1546 1547 1548

			sgl_ptr =
			  (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1549
			memset(sgl_ptr, 0, instance->max_chain_frame_sz);
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
		}
	}

	return sge_count;
}

/**
 * megasas_set_pd_lba -	Sets PD LBA
 * @cdb:		CDB
 * @cdb_len:		cdb length
 * @start_blk:		Start block of IO
 *
 * Used to set the PD LBA in CDB for FP IOs
 */
void
megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
		   struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1567
		   struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1568 1569 1570 1571 1572 1573
{
	struct MR_LD_RAID *raid;
	u32 ld;
	u64 start_blk = io_info->pdBlock;
	u8 *cdb = io_request->CDB.CDB32;
	u32 num_blocks = io_info->numBlocks;
1574
	u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

	/* Check if T10 PI (DIF) is enabled for this LD */
	ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
	raid = MR_LdRaidGet(ld, local_map_ptr);
	if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
		memset(cdb, 0, sizeof(io_request->CDB.CDB32));
		cdb[0] =  MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
		cdb[7] =  MEGASAS_SCSI_ADDL_CDB_LEN;

		if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
			cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
		else
			cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
		cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;

		/* LBA */
		cdb[12] = (u8)((start_blk >> 56) & 0xff);
		cdb[13] = (u8)((start_blk >> 48) & 0xff);
		cdb[14] = (u8)((start_blk >> 40) & 0xff);
		cdb[15] = (u8)((start_blk >> 32) & 0xff);
		cdb[16] = (u8)((start_blk >> 24) & 0xff);
		cdb[17] = (u8)((start_blk >> 16) & 0xff);
		cdb[18] = (u8)((start_blk >> 8) & 0xff);
		cdb[19] = (u8)(start_blk & 0xff);

		/* Logical block reference tag */
		io_request->CDB.EEDP32.PrimaryReferenceTag =
			cpu_to_be32(ref_tag);
1603
		io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
1604
		io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1605 1606 1607 1608 1609 1610 1611 1612 1613

		/* Transfer length */
		cdb[28] = (u8)((num_blocks >> 24) & 0xff);
		cdb[29] = (u8)((num_blocks >> 16) & 0xff);
		cdb[30] = (u8)((num_blocks >> 8) & 0xff);
		cdb[31] = (u8)(num_blocks & 0xff);

		/* set SCSI IO EEDPFlags */
		if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1614
			io_request->EEDPFlags = cpu_to_le16(
1615 1616 1617 1618
				MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG  |
				MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
				MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
				MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1619
				MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
1620
				MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1621
		} else {
1622
			io_request->EEDPFlags = cpu_to_le16(
1623
				MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1624
				MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1625
		}
1626 1627
		io_request->Control |= cpu_to_le32((0x4 << 26));
		io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
	} else {
		/* Some drives don't support 16/12 byte CDB's, convert to 10 */
		if (((cdb_len == 12) || (cdb_len == 16)) &&
		    (start_blk <= 0xffffffff)) {
			if (cdb_len == 16) {
				opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
				flagvals = cdb[1];
				groupnum = cdb[14];
				control = cdb[15];
			} else {
				opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
				flagvals = cdb[1];
				groupnum = cdb[10];
				control = cdb[11];
			}

			memset(cdb, 0, sizeof(io_request->CDB.CDB32));

			cdb[0] = opcode;
			cdb[1] = flagvals;
			cdb[6] = groupnum;
			cdb[9] = control;

			/* Transfer length */
			cdb[8] = (u8)(num_blocks & 0xff);
			cdb[7] = (u8)((num_blocks >> 8) & 0xff);

1655
			io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1656
			cdb_len = 10;
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
		} else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
			/* Convert to 16 byte CDB for large LBA's */
			switch (cdb_len) {
			case 6:
				opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
				control = cdb[5];
				break;
			case 10:
				opcode =
					cdb[0] == READ_10 ? READ_16 : WRITE_16;
				flagvals = cdb[1];
				groupnum = cdb[6];
				control = cdb[9];
				break;
			case 12:
				opcode =
					cdb[0] == READ_12 ? READ_16 : WRITE_16;
				flagvals = cdb[1];
				groupnum = cdb[10];
				control = cdb[11];
				break;
			}

			memset(cdb, 0, sizeof(io_request->CDB.CDB32));

			cdb[0] = opcode;
			cdb[1] = flagvals;
			cdb[14] = groupnum;
			cdb[15] = control;

			/* Transfer length */
			cdb[13] = (u8)(num_blocks & 0xff);
			cdb[12] = (u8)((num_blocks >> 8) & 0xff);
			cdb[11] = (u8)((num_blocks >> 16) & 0xff);
			cdb[10] = (u8)((num_blocks >> 24) & 0xff);

1693
			io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1694
			cdb_len = 16;
1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732
		}

		/* Normal case, just load LBA here */
		switch (cdb_len) {
		case 6:
		{
			u8 val = cdb[1] & 0xE0;
			cdb[3] = (u8)(start_blk & 0xff);
			cdb[2] = (u8)((start_blk >> 8) & 0xff);
			cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
			break;
		}
		case 10:
			cdb[5] = (u8)(start_blk & 0xff);
			cdb[4] = (u8)((start_blk >> 8) & 0xff);
			cdb[3] = (u8)((start_blk >> 16) & 0xff);
			cdb[2] = (u8)((start_blk >> 24) & 0xff);
			break;
		case 12:
			cdb[5]    = (u8)(start_blk & 0xff);
			cdb[4]    = (u8)((start_blk >> 8) & 0xff);
			cdb[3]    = (u8)((start_blk >> 16) & 0xff);
			cdb[2]    = (u8)((start_blk >> 24) & 0xff);
			break;
		case 16:
			cdb[9]    = (u8)(start_blk & 0xff);
			cdb[8]    = (u8)((start_blk >> 8) & 0xff);
			cdb[7]    = (u8)((start_blk >> 16) & 0xff);
			cdb[6]    = (u8)((start_blk >> 24) & 0xff);
			cdb[5]    = (u8)((start_blk >> 32) & 0xff);
			cdb[4]    = (u8)((start_blk >> 40) & 0xff);
			cdb[3]    = (u8)((start_blk >> 48) & 0xff);
			cdb[2]    = (u8)((start_blk >> 56) & 0xff);
			break;
		}
	}
}

1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
/**
 * megasas_stream_detect -	stream detection on read and and write IOs
 * @instance:		Adapter soft state
 * @cmd:		    Command to be prepared
 * @io_info:		IO Request info
 *
 */

/** stream detection on read and and write IOs */
static void megasas_stream_detect(struct megasas_instance *instance,
				struct megasas_cmd_fusion *cmd,
				struct IO_REQUEST_INFO *io_info)
{
	struct fusion_context *fusion = instance->ctrl_context;
	u32 device_id = io_info->ldTgtId;
	struct LD_STREAM_DETECT *current_ld_sd
		= fusion->stream_detect_by_ld[device_id];
	u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
	u32 shifted_values, unshifted_values;
	u32 index_value_mask, shifted_values_mask;
	int i;
	bool is_read_ahead = false;
	struct STREAM_DETECT *current_sd;
	/* find possible stream */
	for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
		stream_num =
		(*track_stream >> (i * BITS_PER_INDEX_STREAM)) &
			STREAM_MASK;
		current_sd = &current_ld_sd->stream_track[stream_num];
	/* if we found a stream, update the raid
	 *  context and also update the mruBitMap
	 */
	/*	boundary condition */
	if ((current_sd->next_seq_lba) &&
		(io_info->ldStartBlock >= current_sd->next_seq_lba) &&
		(io_info->ldStartBlock <= (current_sd->next_seq_lba+32)) &&
		(current_sd->is_read == io_info->isRead)) {

		if ((io_info->ldStartBlock != current_sd->next_seq_lba)
			&& ((!io_info->isRead) || (!is_read_ahead)))
			/*
			 * Once the API availible we need to change this.
			 * At this point we are not allowing any gap
			 */
			continue;

		cmd->io_request->RaidContext.raid_context_g35.stream_detected = true;
		current_sd->next_seq_lba =
		io_info->ldStartBlock + io_info->numBlocks;
		/*
		 *	update the mruBitMap LRU
		 */
		shifted_values_mask =
			(1 <<  i * BITS_PER_INDEX_STREAM) - 1;
		shifted_values = ((*track_stream & shifted_values_mask)
					<< BITS_PER_INDEX_STREAM);
		index_value_mask =
			STREAM_MASK << i * BITS_PER_INDEX_STREAM;
		unshifted_values =
			*track_stream & ~(shifted_values_mask |
			index_value_mask);
		*track_stream =
			unshifted_values | shifted_values | stream_num;
		return;

		}

	}
	/*
	 * if we did not find any stream, create a new one
	 * from the least recently used
	 */
	stream_num =
	(*track_stream >> ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
			STREAM_MASK;
	current_sd = &current_ld_sd->stream_track[stream_num];
	current_sd->is_read = io_info->isRead;
	current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
	*track_stream =
	(((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
	return;

}

1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
/**
 * megasas_build_ldio_fusion -	Prepares IOs to devices
 * @instance:		Adapter soft state
 * @scp:		SCSI command
 * @cmd:		Command to be prepared
 *
 * Prepares the io_request and chain elements (sg_frame) for IO
 * The IO can be for PD (Fast Path) or LD
 */
void
megasas_build_ldio_fusion(struct megasas_instance *instance,
			  struct scsi_cmnd *scp,
			  struct megasas_cmd_fusion *cmd)
{
	u8 fp_possible;
1832
	u32 start_lba_lo, start_lba_hi, device_id, datalength = 0, ld;
1833 1834 1835 1836
	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
	struct IO_REQUEST_INFO io_info;
	struct fusion_context *fusion;
1837
	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1838
	u8 *raidLUN;
1839
	unsigned long spinlock_flags;
1840 1841
	union RAID_CONTEXT_UNION *praid_context;
	struct MR_LD_RAID *raid;
1842

1843
	device_id = MEGASAS_DEV_INDEX(scp);
1844 1845 1846 1847

	fusion = instance->ctrl_context;

	io_request = cmd->io_request;
1848
	io_request->RaidContext.raid_context.virtual_disk_tgt_id =
1849 1850
		cpu_to_le16(device_id);
	io_request->RaidContext.raid_context.status = 0;
1851
	io_request->RaidContext.raid_context.ex_status = 0;
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862

	req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;

	start_lba_lo = 0;
	start_lba_hi = 0;
	fp_possible = 0;

	/*
	 * 6-byte READ(0x08) or WRITE(0x0A) cdb
	 */
	if (scp->cmd_len == 6) {
1863
		datalength = (u32) scp->cmnd[4];
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873
		start_lba_lo = ((u32) scp->cmnd[1] << 16) |
			((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];

		start_lba_lo &= 0x1FFFFF;
	}

	/*
	 * 10-byte READ(0x28) or WRITE(0x2A) cdb
	 */
	else if (scp->cmd_len == 10) {
1874
		datalength = (u32) scp->cmnd[8] |
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884
			((u32) scp->cmnd[7] << 8);
		start_lba_lo = ((u32) scp->cmnd[2] << 24) |
			((u32) scp->cmnd[3] << 16) |
			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
	}

	/*
	 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
	 */
	else if (scp->cmd_len == 12) {
1885
		datalength = ((u32) scp->cmnd[6] << 24) |
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
			((u32) scp->cmnd[7] << 16) |
			((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
		start_lba_lo = ((u32) scp->cmnd[2] << 24) |
			((u32) scp->cmnd[3] << 16) |
			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
	}

	/*
	 * 16-byte READ(0x88) or WRITE(0x8A) cdb
	 */
	else if (scp->cmd_len == 16) {
1897
		datalength = ((u32) scp->cmnd[10] << 24) |
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
			((u32) scp->cmnd[11] << 16) |
			((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
		start_lba_lo = ((u32) scp->cmnd[6] << 24) |
			((u32) scp->cmnd[7] << 16) |
			((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];

		start_lba_hi = ((u32) scp->cmnd[2] << 24) |
			((u32) scp->cmnd[3] << 16) |
			((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
	}

	memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
	io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1911
	io_info.numBlocks = datalength;
1912
	io_info.ldTgtId = device_id;
1913
	io_info.r1_alt_dev_handle = MR_PD_INVALID;
1914
	io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1915 1916 1917 1918

	if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
		io_info.isRead = 1;

1919
	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1920 1921
	ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
	raid = MR_LdRaidGet(ld, local_map_ptr);
1922 1923

	if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1924
		instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
1925
		io_request->RaidContext.raid_context.reg_lock_flags  = 0;
1926 1927
		fp_possible = 0;
	} else {
1928
		if (MR_BuildRaidContext(instance, &io_info,
1929
					&io_request->RaidContext.raid_context,
1930
					local_map_ptr, &raidLUN))
1931 1932 1933
			fp_possible = io_info.fpOkForIo;
	}

1934
	/* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
1935 1936 1937
	   id by default, not CPU group id, otherwise all MSI-X queues won't
	   be utilized */
	cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1938
		raw_smp_processor_id() % instance->msix_vectors : 0;
1939

1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
	if (instance->is_ventura) {
		spin_lock_irqsave(&instance->stream_lock, spinlock_flags);
		megasas_stream_detect(instance, cmd, &io_info);
		spin_unlock_irqrestore(&instance->stream_lock, spinlock_flags);
		/* In ventura if stream detected for a read and it is read ahead
		 *  capable make this IO as LDIO
		 */
		if (io_request->RaidContext.raid_context_g35.stream_detected &&
				io_info.isRead && io_info.ra_capable)
			fp_possible = false;
	}

1952 1953
	praid_context = &io_request->RaidContext;

1954 1955 1956 1957 1958
	if (fp_possible) {
		megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
				   local_map_ptr, start_lba_lo);
		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
		cmd->request_desc->SCSIIO.RequestFlags =
1959
			(MPI2_REQ_DESCRIPT_FLAGS_FP_IO
1960
			 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1961
		if (fusion->adapter_type == INVADER_SERIES) {
1962
			if (io_request->RaidContext.raid_context.reg_lock_flags ==
1963 1964 1965 1966
			    REGION_TYPE_UNUSED)
				cmd->request_desc->SCSIIO.RequestFlags =
					(MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
					MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1967
			io_request->RaidContext.raid_context.type
1968 1969
				= MPI2_TYPE_CUDA;
			io_request->RaidContext.raid_context.nseg = 0x1;
1970
			io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1971
			io_request->RaidContext.raid_context.reg_lock_flags |=
1972 1973
			  (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
			   MR_RL_FLAGS_SEQ_NUM_ENABLE);
1974 1975 1976 1977 1978 1979 1980
		} else if (instance->is_ventura) {
			io_request->RaidContext.raid_context_g35.type
				= MPI2_TYPE_CUDA;
			io_request->RaidContext.raid_context_g35.nseg = 0x1;
			io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
			io_request->IoFlags |=
			cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1981
		}
1982 1983 1984
		if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
		    (io_info.isRead)) {
			io_info.devHandle =
1985
				get_updated_dev_handle(instance,
1986 1987 1988
					&fusion->load_balance_info[device_id],
					&io_info);
			scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1989
			cmd->pd_r1_lb = io_info.pd_after_lb;
1990 1991 1992 1993 1994 1995 1996
			if (instance->is_ventura)
				io_request->RaidContext.raid_context_g35.span_arm
					= io_info.span_arm;
			else
				io_request->RaidContext.raid_context.span_arm
					= io_info.span_arm;

1997 1998
		} else
			scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1999

2000 2001 2002 2003
		cmd->is_raid_1_fp_write = io_info.is_raid_1_fp_write;
		if (io_info.is_raid_1_fp_write)
			cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;

2004
		if ((raidLUN[0] == 1) &&
2005
			(local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2006 2007 2008 2009 2010
			instance->dev_handle = !(instance->dev_handle);
			io_info.devHandle =
				local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
		}

2011 2012
		cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
		io_request->DevHandle = io_info.devHandle;
2013 2014
		/* populate the LUN field */
		memcpy(io_request->LUN, raidLUN, 8);
2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
		if (instance->is_ventura) {
			if (io_info.isRead) {
				if ((raid->cpuAffinity.pdRead.cpu0) &&
					(raid->cpuAffinity.pdRead.cpu1))
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_FCFS;
				else if (raid->cpuAffinity.pdRead.cpu1)
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_1;
				else
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_0;
			} else {
			if ((raid->cpuAffinity.pdWrite.cpu0)
			&& (raid->cpuAffinity.pdWrite.cpu1))
				praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_FCFS;
				else if (raid->cpuAffinity.pdWrite.cpu1)
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_1;
				else
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_0;
				if (praid_context->raid_context_g35.routing_flags.bits.sld) {
					praid_context->raid_context_g35.raid_flags
					= (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
					<< MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
				}
			}
		}
2045
	} else {
2046
		io_request->RaidContext.raid_context.timeout_value =
2047
			cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2048 2049 2050
		cmd->request_desc->SCSIIO.RequestFlags =
			(MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
			 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2051
		if (fusion->adapter_type == INVADER_SERIES) {
2052
			if (io_info.do_fp_rlbypass ||
2053
			(io_request->RaidContext.raid_context.reg_lock_flags
2054
					== REGION_TYPE_UNUSED))
2055 2056 2057
				cmd->request_desc->SCSIIO.RequestFlags =
					(MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
					MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2058
			io_request->RaidContext.raid_context.type
2059
				= MPI2_TYPE_CUDA;
2060
			io_request->RaidContext.raid_context.reg_lock_flags |=
2061 2062
				(MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
				 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2063
			io_request->RaidContext.raid_context.nseg = 0x1;
2064 2065 2066 2067 2068
		} else if (instance->is_ventura) {
			io_request->RaidContext.raid_context_g35.type
				= MPI2_TYPE_CUDA;
			io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
			io_request->RaidContext.raid_context_g35.nseg = 0x1;
2069 2070
		}
		io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2071
		io_request->DevHandle = cpu_to_le16(device_id);
2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106

		if (instance->is_ventura) {
			if (io_info.isRead) {
				if ((raid->cpuAffinity.ldRead.cpu0)
				&& (raid->cpuAffinity.ldRead.cpu1))
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_FCFS;
				else if (raid->cpuAffinity.ldRead.cpu1)
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
						= MR_RAID_CTX_CPUSEL_1;
				else
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
						= MR_RAID_CTX_CPUSEL_0;
			} else {
				if ((raid->cpuAffinity.ldWrite.cpu0) &&
					(raid->cpuAffinity.ldWrite.cpu1))
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
						= MR_RAID_CTX_CPUSEL_FCFS;
				else if (raid->cpuAffinity.ldWrite.cpu1)
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
						= MR_RAID_CTX_CPUSEL_1;
				else
					praid_context->raid_context_g35.routing_flags.bits.cpu_sel
					= MR_RAID_CTX_CPUSEL_0;

				if (io_request->RaidContext.raid_context_g35.stream_detected
					&& (raid->level == 5) &&
					(raid->writeMode == MR_RL_WRITE_THROUGH_MODE)) {
					if (praid_context->raid_context_g35.routing_flags.bits.cpu_sel
						== MR_RAID_CTX_CPUSEL_FCFS)
						praid_context->raid_context_g35.routing_flags.bits.cpu_sel
							= MR_RAID_CTX_CPUSEL_0;
				}
			}
		}
2107 2108 2109 2110
	} /* Not FP */
}

/**
2111
 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2112 2113 2114 2115
 * @instance:		Adapter soft state
 * @scp:		SCSI command
 * @cmd:		Command to be prepared
 *
2116
 * Prepares the io_request frame for non-rw io cmds for vd.
2117
 */
2118 2119
static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
			  struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2120 2121 2122 2123
{
	u32 device_id;
	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
	u16 pd_index = 0;
2124
	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2125
	struct fusion_context *fusion = instance->ctrl_context;
2126
	u8                          span, physArm;
2127
	__le16                      devHandle;
2128 2129 2130
	u32                         ld, arRef, pd;
	struct MR_LD_RAID                  *raid;
	struct RAID_CONTEXT                *pRAID_Context;
2131
	u8 fp_possible = 1;
2132 2133

	io_request = cmd->io_request;
2134 2135
	device_id = MEGASAS_DEV_INDEX(scmd);
	pd_index = MEGASAS_PD_INDEX(scmd);
2136
	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2137
	io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2138
	/* get RAID_Context pointer */
2139
	pRAID_Context = &io_request->RaidContext.raid_context;
2140
	/* Check with FW team */
2141 2142 2143
	pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
	pRAID_Context->reg_lock_row_lba    = 0;
	pRAID_Context->reg_lock_length    = 0;
2144

2145 2146
	if (fusion->fast_path_io && (
		device_id < instance->fw_supported_vd_count)) {
2147

2148
		ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2149
		if (ld >= instance->fw_supported_vd_count)
2150
			fp_possible = 0;
2151 2152 2153

		raid = MR_LdRaidGet(ld, local_map_ptr);
		if (!(raid->capability.fpNonRWCapable))
2154 2155 2156
			fp_possible = 0;
	} else
		fp_possible = 0;
2157

2158 2159 2160 2161
	if (!fp_possible) {
		io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
		io_request->DevHandle = cpu_to_le16(device_id);
		io_request->LUN[1] = scmd->device->lun;
2162
		pRAID_Context->timeout_value =
2163 2164 2165 2166 2167
			cpu_to_le16 (scmd->request->timeout / HZ);
		cmd->request_desc->SCSIIO.RequestFlags =
			(MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
			MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
	} else {
2168 2169

		/* set RAID context values */
2170 2171 2172 2173 2174
		pRAID_Context->config_seq_num = raid->seqNum;
		if (!instance->is_ventura)
			pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
		pRAID_Context->timeout_value =
			cpu_to_le16(raid->fpIoTimeoutForLd);
2175 2176 2177 2178 2179 2180 2181 2182 2183 2184

		/* get the DevHandle for the PD (since this is
		   fpNonRWCapable, this is a single disk RAID0) */
		span = physArm = 0;
		arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
		pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
		devHandle = MR_PdDevHandleGet(pd, local_map_ptr);

		/* build request descriptor */
		cmd->request_desc->SCSIIO.RequestFlags =
2185
			(MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2186
			MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2187 2188 2189 2190 2191 2192 2193 2194
		cmd->request_desc->SCSIIO.DevHandle = devHandle;

		/* populate the LUN field */
		memcpy(io_request->LUN, raid->LUN, 8);

		/* build the raidScsiIO structure */
		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
		io_request->DevHandle = devHandle;
2195 2196
	}
}
2197

2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
/**
 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
 * @instance:		Adapter soft state
 * @scp:		SCSI command
 * @cmd:		Command to be prepared
 * @fp_possible:	parameter to detect fast path or firmware path io.
 *
 * Prepares the io_request frame for rw/non-rw io cmds for syspds
 */
static void
megasas_build_syspd_fusion(struct megasas_instance *instance,
	struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
{
	u32 device_id;
	struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
	u16 pd_index = 0;
	u16 os_timeout_value;
	u16 timeout_limit;
	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
	struct RAID_CONTEXT	*pRAID_Context;
2218
	struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
2219
	struct fusion_context *fusion = instance->ctrl_context;
2220
	pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
2221 2222 2223 2224

	device_id = MEGASAS_DEV_INDEX(scmd);
	pd_index = MEGASAS_PD_INDEX(scmd);
	os_timeout_value = scmd->request->timeout / HZ;
2225

2226 2227
	io_request = cmd->io_request;
	/* get RAID_Context pointer */
2228
	pRAID_Context = &io_request->RaidContext.raid_context;
2229 2230 2231
	pRAID_Context->reg_lock_flags = 0;
	pRAID_Context->reg_lock_row_lba = 0;
	pRAID_Context->reg_lock_length = 0;
2232 2233
	io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
	io_request->LUN[1] = scmd->device->lun;
2234
	pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
2235 2236
		<< MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;

2237 2238 2239 2240 2241 2242
	/* If FW supports PD sequence number */
	if (instance->use_seqnum_jbod_fp &&
		instance->pd_list[pd_index].driveType == TYPE_DISK) {
		/* TgtId must be incremented by 255 as jbod seq number is index
		 * below raid map
		 */
2243
		pRAID_Context->virtual_disk_tgt_id =
2244
			cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
2245
		pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
2246
		io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
2247 2248 2249 2250
		if (instance->is_ventura)
			io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
		else
		pRAID_Context->reg_lock_flags |=
2251
			(MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
2252
		pRAID_Context->type = MPI2_TYPE_CUDA;
2253
		pRAID_Context->nseg = 0x1;
2254
	} else if (fusion->fast_path_io) {
2255 2256
		pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
		pRAID_Context->config_seq_num = 0;
2257 2258 2259 2260 2261
		local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
		io_request->DevHandle =
			local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
	} else {
		/* Want to send all IO via FW path */
2262 2263
		pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
		pRAID_Context->config_seq_num = 0;
S
Sumit Saxena 已提交
2264
		io_request->DevHandle = cpu_to_le16(0xFFFF);
2265
	}
2266 2267 2268 2269 2270 2271 2272 2273 2274

	cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
	cmd->request_desc->SCSIIO.MSIxIndex =
		instance->msix_vectors ?
		(raw_smp_processor_id() % instance->msix_vectors) : 0;


	if (!fp_possible) {
		/* system pd firmware path */
2275 2276 2277
		io_request->Function  = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
		cmd->request_desc->SCSIIO.RequestFlags =
			(MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2278
				MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2279 2280
		pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
		pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2281 2282 2283 2284 2285
	} else {
		/* system pd Fast Path */
		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
		timeout_limit = (scmd->device->type == TYPE_DISK) ?
				255 : 0xFFFF;
2286
		pRAID_Context->timeout_value =
2287 2288
			cpu_to_le16((os_timeout_value > timeout_limit) ?
			timeout_limit : os_timeout_value);
2289
		if (fusion->adapter_type == INVADER_SERIES)
2290 2291
			io_request->IoFlags |=
				cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2292

2293
		cmd->request_desc->SCSIIO.RequestFlags =
2294
			(MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2295
				MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
	}
}

/**
 * megasas_build_io_fusion -	Prepares IOs to devices
 * @instance:		Adapter soft state
 * @scp:		SCSI command
 * @cmd:		Command to be prepared
 *
 * Invokes helper functions to prepare request frames
 * and sets flags appropriate for IO/Non-IO cmd
 */
int
megasas_build_io_fusion(struct megasas_instance *instance,
			struct scsi_cmnd *scp,
			struct megasas_cmd_fusion *cmd)
{
2313
	u16 sge_count;
2314
	u8  cmd_type;
2315 2316 2317
	struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;

	/* Zero out some fields so they don't get reused */
H
Hannes Reinecke 已提交
2318
	memset(io_request->LUN, 0x0, 8);
2319 2320 2321 2322 2323
	io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
	io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
	io_request->EEDPFlags = 0;
	io_request->Control = 0;
	io_request->EEDPBlockSize = 0;
2324
	io_request->ChainOffset = 0;
2325 2326
	io_request->RaidContext.raid_context.raid_flags = 0;
	io_request->RaidContext.raid_context.type = 0;
2327
	io_request->RaidContext.raid_context.nseg = 0;
2328 2329 2330 2331 2332 2333

	memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
	/*
	 * Just the CDB length,rest of the Flags are zero
	 * This will be modified for FP in build_ldio_fusion
	 */
2334
	io_request->IoFlags = cpu_to_le16(scp->cmd_len);
2335

2336 2337
	switch (cmd_type = megasas_cmd_type(scp)) {
	case READ_WRITE_LDIO:
2338
		megasas_build_ldio_fusion(instance, scp, cmd);
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353
		break;
	case NON_READ_WRITE_LDIO:
		megasas_build_ld_nonrw_fusion(instance, scp, cmd);
		break;
	case READ_WRITE_SYSPDIO:
	case NON_READ_WRITE_SYSPDIO:
		if (instance->secure_jbod_support &&
			(cmd_type == NON_READ_WRITE_SYSPDIO))
			megasas_build_syspd_fusion(instance, scp, cmd, 0);
		else
			megasas_build_syspd_fusion(instance, scp, cmd, 1);
		break;
	default:
		break;
	}
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

	/*
	 * Construct SGL
	 */

	sge_count =
		megasas_make_sgl_fusion(instance, scp,
					(struct MPI25_IEEE_SGE_CHAIN64 *)
					&io_request->SGL, cmd);

	if (sge_count > instance->max_num_sge) {
2365
		dev_err(&instance->pdev->dev, "Error. sge_count (0x%x) exceeds "
2366 2367 2368 2369 2370
		       "max (0x%x) allowed\n", sge_count,
		       instance->max_num_sge);
		return 1;
	}

2371 2372 2373 2374 2375 2376 2377 2378 2379 2380
	if (instance->is_ventura)
		io_request->RaidContext.raid_context_g35.num_sge = sge_count;
	else {
		/* numSGE store lower 8 bit of sge_count.
		 * numSGEExt store higher 8 bit of sge_count
		 */
		io_request->RaidContext.raid_context.num_sge = sge_count;
		io_request->RaidContext.raid_context.num_sge_ext =
			(u8)(sge_count >> 8);
	}
2381

2382
	io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
2383 2384

	if (scp->sc_data_direction == PCI_DMA_TODEVICE)
2385
		io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
2386
	else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2387
		io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
2388 2389 2390 2391

	io_request->SGLOffset0 =
		offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;

2392
	io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
	io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;

	cmd->scmd = scp;
	scp->SCp.ptr = (char *)cmd;

	return 0;
}

union MEGASAS_REQUEST_DESCRIPTOR_UNION *
megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
{
	u8 *p;
	struct fusion_context *fusion;

2407
	if (index >= instance->max_mpt_cmds) {
2408
		dev_err(&instance->pdev->dev, "Invalid SMID (0x%x)request for "
2409 2410
		       "descriptor for scsi%d\n", index,
			instance->host->host_no);
2411 2412 2413
		return NULL;
	}
	fusion = instance->ctrl_context;
2414 2415
	p = fusion->req_frames_desc +
		sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
2416 2417 2418 2419

	return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
}

2420 2421 2422 2423 2424 2425 2426 2427 2428
/*
 * megasas_fpio_to_ldio-
 * This function converts an fp io to ldio
 */

void megasas_fpio_to_ldio(struct megasas_instance *instance,
	struct megasas_cmd_fusion *cmd, struct scsi_cmnd *scmd)
{
	struct fusion_context *fusion;
2429 2430 2431 2432
	union RAID_CONTEXT_UNION *praid_context;
	struct MR_LD_RAID *raid;
	struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
	u32 device_id, ld;
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455

	fusion = instance->ctrl_context;
	cmd->request_desc->SCSIIO.RequestFlags =
		(MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
		<< MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
	cmd->io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
	cmd->io_request->DevHandle = cpu_to_le16(MEGASAS_DEV_INDEX(scmd));

	/*remove FAST PATH ENABLE bit in IoFlags */
	cmd->io_request->IoFlags &=
	cpu_to_le16(~MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);

	/* if the numSGE > max_sge_in_main_sge set the chain offset*/
	if (cmd->io_request->RaidContext.raid_context_g35.num_sge >
		fusion->max_sge_in_main_msg)
		cmd->io_request->ChainOffset = fusion->chain_offset_io_request;
	memcpy(cmd->io_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
	cmd->io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
	cmd->io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
	cmd->io_request->EEDPFlags = 0;
	cmd->io_request->Control = 0;
	cmd->io_request->EEDPBlockSize = 0;
	cmd->is_raid_1_fp_write = 0;
2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484

	device_id = MEGASAS_DEV_INDEX(cmd->scmd);
	local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
	ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
	raid = MR_LdRaidGet(ld, local_map_ptr);
	praid_context = &cmd->io_request->RaidContext;
	if (cmd->scmd->sc_data_direction == PCI_DMA_FROMDEVICE) {
		if ((raid->cpuAffinity.ldRead.cpu0)
		&& (raid->cpuAffinity.ldRead.cpu1))
			praid_context->raid_context_g35.routing_flags.bits.cpu_sel
			= MR_RAID_CTX_CPUSEL_FCFS;
		else if (raid->cpuAffinity.ldRead.cpu1)
			praid_context->raid_context_g35.routing_flags.bits.cpu_sel
			= MR_RAID_CTX_CPUSEL_1;
		else
			praid_context->raid_context_g35.routing_flags.bits.cpu_sel
			= MR_RAID_CTX_CPUSEL_0;
	} else {
	if ((raid->cpuAffinity.ldWrite.cpu0)
		&& (raid->cpuAffinity.ldWrite.cpu1))
		praid_context->raid_context_g35.routing_flags.bits.cpu_sel
			= MR_RAID_CTX_CPUSEL_FCFS;
	else if (raid->cpuAffinity.ldWrite.cpu1)
		praid_context->raid_context_g35.routing_flags.bits.cpu_sel
			= MR_RAID_CTX_CPUSEL_1;
	else
		praid_context->raid_context_g35.routing_flags.bits.cpu_sel
		= MR_RAID_CTX_CPUSEL_0;
	}
2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551
}

/* megasas_prepate_secondRaid1_IO
 *  It prepares the raid 1 second IO
 */
void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
			    struct megasas_cmd_fusion *cmd,
			    struct megasas_cmd_fusion *r1_cmd)
{
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;
	req_desc = cmd->request_desc;
	if (r1_cmd) {
		/* copy the io request frame as well
		 *  as 8 SGEs data for r1 command
		 */
		memcpy(r1_cmd->io_request, cmd->io_request,
			sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
		memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
				(fusion->max_sge_in_main_msg *
				sizeof(union MPI2_SGE_IO_UNION)));
		/*sense buffer is different for r1 command*/
		r1_cmd->io_request->SenseBufferLowAddress =
				cpu_to_le32(r1_cmd->sense_phys_addr);
		r1_cmd->scmd = cmd->scmd;
		req_desc2 =
		megasas_get_request_descriptor(instance, r1_cmd->index-1);
		if (req_desc2) {
			req_desc2->Words = 0;
			r1_cmd->request_desc = req_desc2;
			req_desc2->SCSIIO.SMID =
				cpu_to_le16(r1_cmd->index);
			req_desc2->SCSIIO.RequestFlags =
				req_desc->SCSIIO.RequestFlags;
			r1_cmd->is_raid_1_fp_write = 1;
			r1_cmd->request_desc->SCSIIO.DevHandle =
				cmd->r1_alt_dev_handle;
			r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
			cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
				 cpu_to_le16(r1_cmd->index);
			r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
				cpu_to_le16(cmd->index);
			/* MSIxIndex of both commands request
			 * descriptors should be same
			 */
			r1_cmd->request_desc->SCSIIO.MSIxIndex =
				cmd->request_desc->SCSIIO.MSIxIndex;
			/*span arm is different for r1 cmd*/
			r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
			cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
		} else {
			megasas_return_cmd_fusion(instance, r1_cmd);
			dev_info(&instance->pdev->dev,
				"unable to get request descriptor, firing as normal IO\n");
			atomic_dec(&instance->fw_outstanding);
			megasas_fpio_to_ldio(instance, cmd, cmd->scmd);
		}
	} else {
		dev_info(&instance->pdev->dev,
			"unable to get command, firing as normal IO\n");
		atomic_dec(&instance->fw_outstanding);
		megasas_fpio_to_ldio(instance, cmd, cmd->scmd);
	}
}

2552 2553 2554 2555 2556 2557 2558 2559 2560 2561
/**
 * megasas_build_and_issue_cmd_fusion -Main routine for building and
 *                                     issuing non IOCTL cmd
 * @instance:			Adapter soft state
 * @scmd:			pointer to scsi cmd from OS
 */
static u32
megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
				   struct scsi_cmnd *scmd)
{
2562
	struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
2563 2564 2565 2566 2567 2568
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
	u32 index;
	struct fusion_context *fusion;

	fusion = instance->ctrl_context;

2569 2570 2571 2572 2573 2574 2575 2576
	if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
		instance->ldio_threshold &&
		(atomic_inc_return(&instance->ldio_outstanding) >
		instance->ldio_threshold)) {
		atomic_dec(&instance->ldio_outstanding);
		return SCSI_MLQUEUE_DEVICE_BUSY;
	}

2577 2578 2579 2580 2581 2582 2583
	if (atomic_inc_return(&instance->fw_outstanding) >
			instance->host->can_queue) {
		dev_err(&instance->pdev->dev, "Throttle IOs beyond Controller queue depth\n");
		atomic_dec(&instance->fw_outstanding);
		return SCSI_MLQUEUE_HOST_BUSY;
	}

2584
	cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
2585

2586 2587 2588 2589 2590
	if (!cmd) {
		atomic_dec(&instance->fw_outstanding);
		return SCSI_MLQUEUE_HOST_BUSY;
	}

2591 2592 2593
	index = cmd->index;

	req_desc = megasas_get_request_descriptor(instance, index-1);
2594 2595
	if (!req_desc) {
		atomic_dec(&instance->fw_outstanding);
2596
		return SCSI_MLQUEUE_HOST_BUSY;
2597
	}
2598 2599 2600 2601 2602 2603

	req_desc->Words = 0;
	cmd->request_desc = req_desc;

	if (megasas_build_io_fusion(instance, scmd, cmd)) {
		megasas_return_cmd_fusion(instance, cmd);
2604
		dev_err(&instance->pdev->dev, "Error building command\n");
2605
		cmd->request_desc = NULL;
2606
		atomic_dec(&instance->fw_outstanding);
2607
		return SCSI_MLQUEUE_HOST_BUSY;
2608 2609 2610
	}

	req_desc = cmd->request_desc;
2611
	req_desc->SCSIIO.SMID = cpu_to_le16(index);
2612 2613 2614

	if (cmd->io_request->ChainOffset != 0 &&
	    cmd->io_request->ChainOffset != 0xF)
2615
		dev_err(&instance->pdev->dev, "The chain offset value is not "
2616
		       "correct : %x\n", cmd->io_request->ChainOffset);
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
	/*
	 *	if it is raid 1/10 fp write capable.
	 *	try to get second command from pool and construct it.
	 *	From FW, it has confirmed that lba values of two PDs
	 *	corresponds to single R1/10 LD are always same
	 *
	 */
	/*	driver side count always should be less than max_fw_cmds
	 *	to get new command
	 */
	if (cmd->is_raid_1_fp_write &&
		atomic_inc_return(&instance->fw_outstanding) >
			(instance->host->can_queue)) {
		megasas_fpio_to_ldio(instance, cmd, cmd->scmd);
		atomic_dec(&instance->fw_outstanding);
	} else if (cmd->is_raid_1_fp_write) {
		r1_cmd = megasas_get_cmd_fusion(instance,
				(scmd->request->tag + instance->max_fw_cmds));
		megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
	}

2638 2639 2640 2641 2642

	/*
	 * Issue the command to the FW
	 */

2643
	megasas_fire_cmd_fusion(instance, req_desc, instance->is_ventura);
2644

2645 2646 2647 2648 2649
	if (r1_cmd)
		megasas_fire_cmd_fusion(instance, r1_cmd->request_desc,
				instance->is_ventura);


2650 2651 2652 2653 2654 2655 2656 2657 2658
	return 0;
}

/**
 * complete_cmd_fusion -	Completes command
 * @instance:			Adapter soft state
 * Completes all commands that is in reply descriptor queue
 */
int
2659
complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
2660 2661 2662 2663 2664 2665
{
	union MPI2_REPLY_DESCRIPTORS_UNION *desc;
	struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
	struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
	struct fusion_context *fusion;
	struct megasas_cmd *cmd_mfi;
2666
	struct megasas_cmd_fusion *cmd_fusion, *r1_cmd = NULL;
2667
	u16 smid, num_completed;
2668 2669
	u8 reply_descript_type, *sense;
	u32 status, extStatus, device_id, data_length;
2670 2671
	union desc_value d_val;
	struct LD_LOAD_BALANCE_INFO *lbinfo;
2672
	int threshold_reply_count = 0;
2673
	struct scsi_cmnd *scmd_local = NULL;
2674 2675
	struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
	struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
2676 2677 2678

	fusion = instance->ctrl_context;

2679
	if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2680 2681
		return IRQ_HANDLED;

2682 2683
	desc = fusion->reply_frames_desc[MSIxIndex] +
				fusion->last_reply_idx[MSIxIndex];
2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696

	reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;

	d_val.word = desc->Words;

	reply_descript_type = reply_desc->ReplyFlags &
		MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;

	if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
		return IRQ_NONE;

	num_completed = 0;

2697 2698
	while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
	       d_val.u.high != cpu_to_le32(UINT_MAX)) {
2699 2700 2701 2702 2703 2704 2705 2706 2707
		   /*
		    * Ensure that the peer command is NULL here in case a
		    * command has completed but the R1 FP Write peer has
		    * not completed yet.If not null, it's possible that
		    * another thread will complete the peer
		    * command and should not.
		    */
		r1_cmd = NULL;

2708
		smid = le16_to_cpu(reply_desc->SMID);
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718

		cmd_fusion = fusion->cmd_list[smid - 1];

		scsi_io_req =
			(struct MPI2_RAID_SCSI_IO_REQUEST *)
		  cmd_fusion->io_request;

		if (cmd_fusion->scmd)
			cmd_fusion->scmd->SCp.ptr = NULL;

2719
		scmd_local = cmd_fusion->scmd;
2720
		status = scsi_io_req->RaidContext.raid_context.status;
2721
		extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
2722 2723
		sense = cmd_fusion->sense;
		data_length = scsi_io_req->DataLength;
2724 2725

		switch (scsi_io_req->Function) {
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735
		case MPI2_FUNCTION_SCSI_TASK_MGMT:
			mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
						cmd_fusion->io_request;
			mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
						&mr_tm_req->TmRequest;
			dev_dbg(&instance->pdev->dev, "TM completion:"
				"type: 0x%x TaskMID: 0x%x\n",
				mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
			complete(&cmd_fusion->done);
			break;
2736 2737
		case MPI2_FUNCTION_SCSI_IO_REQUEST:  /*Fast Path IO.*/
			/* Update load balancing info */
2738
			device_id = MEGASAS_DEV_INDEX(scmd_local);
2739
			lbinfo = &fusion->load_balance_info[device_id];
2740 2741 2742 2743 2744 2745
			/*
			 * check for the raid 1/10 fast path writes
			 */
			if (!cmd_fusion->is_raid_1_fp_write &&
				(cmd_fusion->scmd->SCp.Status &
					MEGASAS_LOAD_BALANCE_FLAG)) {
2746
				atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
2747 2748
				cmd_fusion->scmd->SCp.Status &=
					~MEGASAS_LOAD_BALANCE_FLAG;
2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
			} else if (cmd_fusion->is_raid_1_fp_write) {
				/* get peer command */
				if (cmd_fusion->index < instance->max_fw_cmds)
					r1_cmd = fusion->cmd_list[(cmd_fusion->index +
					instance->max_fw_cmds)-1];
				else {
					r1_cmd =
					fusion->cmd_list[(cmd_fusion->index -
						 instance->max_fw_cmds)-1];
				}
				cmd_fusion->cmd_completed = true;
2760
			}
2761

2762 2763 2764
			if (reply_descript_type ==
			    MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
				if (megasas_dbg_lvl == 5)
2765
					dev_err(&instance->pdev->dev, "\nFAST Path "
2766 2767 2768 2769 2770
					       "IO Success\n");
			}
			/* Fall thru and complete IO */
		case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
			/* Map the FW Cmd Status */
2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788
			/*
			 * check for the raid 1/10 fast path writes
			 */
			if (r1_cmd &&  r1_cmd->is_raid_1_fp_write
				&& r1_cmd->cmd_completed) {
				/*
				 * if the peer  Raid  1/10 fast path failed,
				 * mark IO as failed to the scsi layer.
				 * over write the current status by the failed
				 * status makes sure that if any one of
				 * command fails,return fail status to
				 * scsi layer
				 */
				if (r1_cmd->io_request->RaidContext.raid_context.status !=
								MFI_STAT_OK) {
					status =
					r1_cmd->io_request->RaidContext.raid_context.status;
					extStatus =
2789
					r1_cmd->io_request->RaidContext.raid_context.ex_status;
2790 2791 2792 2793 2794
					data_length =
						r1_cmd->io_request->DataLength;
					sense = r1_cmd->sense;
				}
				r1_cmd->io_request->RaidContext.raid_context.status = 0;
2795
				r1_cmd->io_request->RaidContext.raid_context.ex_status = 0;
2796 2797 2798 2799 2800 2801 2802 2803 2804
				cmd_fusion->is_raid_1_fp_write = 0;
				r1_cmd->is_raid_1_fp_write = 0;
				r1_cmd->cmd_completed = false;
				cmd_fusion->cmd_completed = false;
				megasas_return_cmd_fusion(instance, r1_cmd);
			}
			if (!cmd_fusion->is_raid_1_fp_write) {
				map_cmd_status(fusion, scmd_local, status,
					extStatus, data_length, sense);
2805 2806
				scsi_io_req->RaidContext.raid_context.status = 0;
				scsi_io_req->RaidContext.raid_context.ex_status = 0;
2807 2808 2809 2810
				megasas_return_cmd_fusion(instance, cmd_fusion);
				scsi_dma_unmap(scmd_local);
				scmd_local->scsi_done(scmd_local);
			}
2811 2812 2813 2814 2815
			atomic_dec(&instance->fw_outstanding);

			break;
		case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
			cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2816

2817 2818 2819 2820 2821 2822 2823 2824
			/* Poll mode. Dummy free.
			 * In case of Interrupt mode, caller has reverse check.
			 */
			if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
				cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
				megasas_return_cmd(instance, cmd_mfi);
			} else
				megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2825 2826 2827
			break;
		}

2828 2829 2830 2831
		fusion->last_reply_idx[MSIxIndex]++;
		if (fusion->last_reply_idx[MSIxIndex] >=
		    fusion->reply_q_depth)
			fusion->last_reply_idx[MSIxIndex] = 0;
2832

2833
		desc->Words = cpu_to_le64(ULLONG_MAX);
2834
		num_completed++;
2835
		threshold_reply_count++;
2836 2837

		/* Get the next reply descriptor */
2838
		if (!fusion->last_reply_idx[MSIxIndex])
2839
			desc = fusion->reply_frames_desc[MSIxIndex];
2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852
		else
			desc++;

		reply_desc =
		  (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;

		d_val.word = desc->Words;

		reply_descript_type = reply_desc->ReplyFlags &
			MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;

		if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
			break;
2853 2854 2855 2856 2857 2858
		/*
		 * Write to reply post host index register after completing threshold
		 * number of reply counts and still there are more replies in reply queue
		 * pending to be completed
		 */
		if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2859
			if (instance->msix_combined)
2860 2861 2862 2863 2864 2865 2866 2867 2868
				writel(((MSIxIndex & 0x7) << 24) |
					fusion->last_reply_idx[MSIxIndex],
					instance->reply_post_host_index_addr[MSIxIndex/8]);
			else
				writel((MSIxIndex << 24) |
					fusion->last_reply_idx[MSIxIndex],
					instance->reply_post_host_index_addr[0]);
			threshold_reply_count = 0;
		}
2869 2870 2871 2872 2873 2874
	}

	if (!num_completed)
		return IRQ_NONE;

	wmb();
2875
	if (instance->msix_combined)
2876 2877 2878 2879 2880 2881 2882
		writel(((MSIxIndex & 0x7) << 24) |
			fusion->last_reply_idx[MSIxIndex],
			instance->reply_post_host_index_addr[MSIxIndex/8]);
	else
		writel((MSIxIndex << 24) |
			fusion->last_reply_idx[MSIxIndex],
			instance->reply_post_host_index_addr[0]);
2883
	megasas_check_and_restore_queue_depth(instance);
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
	return IRQ_HANDLED;
}

/**
 * megasas_complete_cmd_dpc_fusion -	Completes command
 * @instance:			Adapter soft state
 *
 * Tasklet to complete cmds
 */
void
megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
{
	struct megasas_instance *instance =
		(struct megasas_instance *)instance_addr;
	unsigned long flags;
2899 2900 2901
	u32 count, MSIxIndex;

	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2902 2903 2904

	/* If we have already declared adapter dead, donot complete cmds */
	spin_lock_irqsave(&instance->hba_lock, flags);
2905
	if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2906 2907 2908 2909 2910
		spin_unlock_irqrestore(&instance->hba_lock, flags);
		return;
	}
	spin_unlock_irqrestore(&instance->hba_lock, flags);

2911 2912
	for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
		complete_cmd_fusion(instance, MSIxIndex);
2913 2914 2915 2916 2917 2918 2919
}

/**
 * megasas_isr_fusion - isr entry point
 */
irqreturn_t megasas_isr_fusion(int irq, void *devp)
{
2920 2921
	struct megasas_irq_context *irq_context = devp;
	struct megasas_instance *instance = irq_context->instance;
2922
	u32 mfiStatus, fw_state, dma_state;
2923

2924 2925 2926
	if (instance->mask_interrupts)
		return IRQ_NONE;

2927
	if (!instance->msix_vectors) {
2928 2929 2930 2931 2932 2933
		mfiStatus = instance->instancet->clear_intr(instance->reg_set);
		if (!mfiStatus)
			return IRQ_NONE;
	}

	/* If we are resetting, bail */
2934 2935
	if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
		instance->instancet->clear_intr(instance->reg_set);
2936
		return IRQ_HANDLED;
2937
	}
2938

2939
	if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2940
		instance->instancet->clear_intr(instance->reg_set);
2941 2942 2943
		/* If we didn't complete any commands, check for FW fault */
		fw_state = instance->instancet->read_fw_status_reg(
			instance->reg_set) & MFI_STATE_MASK;
2944 2945 2946 2947 2948 2949 2950
		dma_state = instance->instancet->read_fw_status_reg
			(instance->reg_set) & MFI_STATE_DMADONE;
		if (instance->crash_dump_drv_support &&
			instance->crash_dump_app_support) {
			/* Start collecting crash, if DMA bit is done */
			if ((fw_state == MFI_STATE_FAULT) && dma_state)
				schedule_work(&instance->crash_init);
2951 2952 2953 2954
			else if (fw_state == MFI_STATE_FAULT) {
				if (instance->unload == 0)
					schedule_work(&instance->work_init);
			}
2955
		} else if (fw_state == MFI_STATE_FAULT) {
2956
			dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
2957
			       "for scsi%d\n", instance->host->host_no);
2958 2959
			if (instance->unload == 0)
				schedule_work(&instance->work_init);
2960
		}
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981
	}

	return IRQ_HANDLED;
}

/**
 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
 * @instance:			Adapter soft state
 * mfi_cmd:			megasas_cmd pointer
 *
 */
u8
build_mpt_mfi_pass_thru(struct megasas_instance *instance,
			struct megasas_cmd *mfi_cmd)
{
	struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
	struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
	struct megasas_cmd_fusion *cmd;
	struct fusion_context *fusion;
	struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;

2982 2983 2984 2985
	fusion = instance->ctrl_context;

	cmd = megasas_get_cmd_fusion(instance,
			instance->max_scsi_cmds + mfi_cmd->index);
2986 2987 2988

	/*  Save the smid. To be used for returning the cmd */
	mfi_cmd->context.smid = cmd->index;
2989

2990 2991 2992 2993 2994 2995
	/*
	 * For cmds where the flag is set, store the flag and check
	 * on completion. For cmds with this flag, don't call
	 * megasas_complete_cmd
	 */

2996
	if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
2997
		mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
2998 2999

	io_req = cmd->io_request;
3000

3001
	if (fusion->adapter_type == INVADER_SERIES) {
3002 3003 3004 3005 3006 3007
		struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
			(struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
		sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
		sgl_ptr_end->Flags = 0;
	}

3008 3009 3010 3011 3012 3013 3014 3015
	mpi25_ieee_chain =
	  (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;

	io_req->Function    = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
	io_req->SGLOffset0  = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
				       SGL) / 4;
	io_req->ChainOffset = fusion->chain_offset_mfi_pthru;

3016
	mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3017 3018 3019 3020

	mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
		MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;

3021
	mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038

	return 0;
}

/**
 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
 * @instance:			Adapter soft state
 * @cmd:			mfi cmd to build
 *
 */
union MEGASAS_REQUEST_DESCRIPTOR_UNION *
build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
{
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
	u16 index;

	if (build_mpt_mfi_pass_thru(instance, cmd)) {
3039
		dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053
		return NULL;
	}

	index = cmd->context.smid;

	req_desc = megasas_get_request_descriptor(instance, index - 1);

	if (!req_desc)
		return NULL;

	req_desc->Words = 0;
	req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
					 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);

3054
	req_desc->SCSIIO.SMID = cpu_to_le16(index);
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064

	return req_desc;
}

/**
 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
 * @instance:			Adapter soft state
 * @cmd:			mfi cmd pointer
 *
 */
3065
int
3066 3067 3068 3069 3070 3071 3072
megasas_issue_dcmd_fusion(struct megasas_instance *instance,
			  struct megasas_cmd *cmd)
{
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;

	req_desc = build_mpt_cmd(instance, cmd);
	if (!req_desc) {
3073 3074 3075
		dev_info(&instance->pdev->dev, "Failed from %s %d\n",
					__func__, __LINE__);
		return DCMD_NOT_FIRED;
3076
	}
3077

3078
	megasas_fire_cmd_fusion(instance, req_desc, instance->is_ventura);
3079
	return DCMD_SUCCESS;
3080 3081 3082 3083
}

/**
 * megasas_release_fusion -	Reverses the FW initialization
G
Geert Uytterhoeven 已提交
3084
 * @instance:			Adapter soft state
3085 3086 3087 3088 3089 3090 3091 3092 3093
 */
void
megasas_release_fusion(struct megasas_instance *instance)
{
	megasas_free_cmds(instance);
	megasas_free_cmds_fusion(instance);

	iounmap(instance->reg_set);

3094
	pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106
}

/**
 * megasas_read_fw_status_reg_fusion - returns the current FW status value
 * @regs:			MFI register set
 */
static u32
megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
{
	return readl(&(regs)->outbound_scratch_pad);
}

3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
/**
 * megasas_alloc_host_crash_buffer -	Host buffers for Crash dump collection from Firmware
 * @instance:				Controller's soft instance
 * return:			        Number of allocated host crash buffers
 */
static void
megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
{
	unsigned int i;

	instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
	for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
		instance->crash_buf[i] = (void	*)__get_free_pages(GFP_KERNEL,
				instance->crash_buf_pages);
		if (!instance->crash_buf[i]) {
			dev_info(&instance->pdev->dev, "Firmware crash dump "
				"memory allocation failed at index %d\n", i);
			break;
		}
3126 3127
		memset(instance->crash_buf[i], 0,
			((1 << PAGE_SHIFT) << instance->crash_buf_pages));
3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
	}
	instance->drv_buf_alloc = i;
}

/**
 * megasas_free_host_crash_buffer -	Host buffers for Crash dump collection from Firmware
 * @instance:				Controller's soft instance
 */
void
megasas_free_host_crash_buffer(struct megasas_instance *instance)
{
	unsigned int i
;
	for (i = 0; i < instance->drv_buf_alloc; i++) {
		if (instance->crash_buf[i])
			free_pages((ulong)instance->crash_buf[i],
					instance->crash_buf_pages);
	}
	instance->drv_buf_index = 0;
	instance->drv_buf_alloc = 0;
	instance->fw_crash_state = UNAVAILABLE;
	instance->fw_crash_buffer_size = 0;
}

3152 3153 3154 3155 3156 3157 3158 3159
/**
 * megasas_adp_reset_fusion -	For controller reset
 * @regs:				MFI register set
 */
static int
megasas_adp_reset_fusion(struct megasas_instance *instance,
			 struct megasas_register_set __iomem *regs)
{
3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223
	u32 host_diag, abs_state, retry;

	/* Now try to reset the chip */
	writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
	writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);

	/* Check that the diag write enable (DRWE) bit is on */
	host_diag = readl(&instance->reg_set->fusion_host_diag);
	retry = 0;
	while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
		msleep(100);
		host_diag = readl(&instance->reg_set->fusion_host_diag);
		if (retry++ == 100) {
			dev_warn(&instance->pdev->dev,
				"Host diag unlock failed from %s %d\n",
				__func__, __LINE__);
			break;
		}
	}
	if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
		return -1;

	/* Send chip reset command */
	writel(host_diag | HOST_DIAG_RESET_ADAPTER,
		&instance->reg_set->fusion_host_diag);
	msleep(3000);

	/* Make sure reset adapter bit is cleared */
	host_diag = readl(&instance->reg_set->fusion_host_diag);
	retry = 0;
	while (host_diag & HOST_DIAG_RESET_ADAPTER) {
		msleep(100);
		host_diag = readl(&instance->reg_set->fusion_host_diag);
		if (retry++ == 1000) {
			dev_warn(&instance->pdev->dev,
				"Diag reset adapter never cleared %s %d\n",
				__func__, __LINE__);
			break;
		}
	}
	if (host_diag & HOST_DIAG_RESET_ADAPTER)
		return -1;

	abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
			& MFI_STATE_MASK;
	retry = 0;

	while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
		msleep(100);
		abs_state = instance->instancet->
			read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
	}
	if (abs_state <= MFI_STATE_FW_INIT) {
		dev_warn(&instance->pdev->dev,
			"fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
			abs_state, __func__, __LINE__);
		return -1;
	}

3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238
	return 0;
}

/**
 * megasas_check_reset_fusion -	For controller reset check
 * @regs:				MFI register set
 */
static int
megasas_check_reset_fusion(struct megasas_instance *instance,
			   struct megasas_register_set __iomem *regs)
{
	return 0;
}

/* This function waits for outstanding commands on fusion to complete */
3239
int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
3240
					int reason, int *convert)
3241
{
3242
	int i, outstanding, retval = 0, hb_seconds_missed = 0;
3243
	u32 fw_state;
3244

3245
	for (i = 0; i < resetwaittime; i++) {
3246 3247 3248 3249
		/* Check if firmware is in fault state */
		fw_state = instance->instancet->read_fw_status_reg(
			instance->reg_set) & MFI_STATE_MASK;
		if (fw_state == MFI_STATE_FAULT) {
3250
			dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
3251 3252
			       " will reset adapter scsi%d.\n",
				instance->host->host_no);
3253
			megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3254 3255 3256
			retval = 1;
			goto out;
		}
3257 3258 3259 3260

		if (reason == MFI_IO_TIMEOUT_OCR) {
			dev_info(&instance->pdev->dev,
				"MFI IO is timed out, initiating OCR\n");
3261
			megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3262 3263 3264 3265
			retval = 1;
			goto out;
		}

3266
		/* If SR-IOV VF mode & heartbeat timeout, don't wait */
3267
		if (instance->requestorId && !reason) {
3268 3269 3270 3271
			retval = 1;
			goto out;
		}

3272
		/* If SR-IOV VF mode & I/O timeout, check for HB timeout */
3273
		if (instance->requestorId && reason) {
3274 3275 3276 3277 3278 3279 3280 3281 3282
			if (instance->hb_host_mem->HB.fwCounter !=
			    instance->hb_host_mem->HB.driverCounter) {
				instance->hb_host_mem->HB.driverCounter =
					instance->hb_host_mem->HB.fwCounter;
				hb_seconds_missed = 0;
			} else {
				hb_seconds_missed++;
				if (hb_seconds_missed ==
				    (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
3283
					dev_warn(&instance->pdev->dev, "SR-IOV:"
3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295
					       " Heartbeat never completed "
					       " while polling during I/O "
					       " timeout handling for "
					       "scsi%d.\n",
					       instance->host->host_no);
					       *convert = 1;
					       retval = 1;
					       goto out;
				}
			}
		}

3296 3297 3298 3299 3300
		outstanding = atomic_read(&instance->fw_outstanding);
		if (!outstanding)
			goto out;

		if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
3301
			dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
3302 3303
			       "commands to complete for scsi%d\n", i,
			       outstanding, instance->host->host_no);
3304 3305 3306 3307 3308 3309 3310
			megasas_complete_cmd_dpc_fusion(
				(unsigned long)instance);
		}
		msleep(1000);
	}

	if (atomic_read(&instance->fw_outstanding)) {
3311
		dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
3312 3313
		       "will reset adapter scsi%d.\n",
		       instance->host->host_no);
3314
		*convert = 1;
3315 3316 3317 3318 3319 3320 3321 3322
		retval = 1;
	}
out:
	return retval;
}

void  megasas_reset_reply_desc(struct megasas_instance *instance)
{
3323
	int i, j, count;
3324 3325 3326 3327
	struct fusion_context *fusion;
	union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;

	fusion = instance->ctrl_context;
3328
	count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3329
	for (i = 0 ; i < count ; i++) {
3330
		fusion->last_reply_idx[i] = 0;
3331 3332 3333 3334
		reply_desc = fusion->reply_frames_desc[i];
		for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
			reply_desc->Words = cpu_to_le64(ULLONG_MAX);
	}
3335 3336
}

3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348
/*
 * megasas_refire_mgmt_cmd :	Re-fire management commands
 * @instance:				Controller's soft instance
*/
void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
{
	int j;
	struct megasas_cmd_fusion *cmd_fusion;
	struct fusion_context *fusion;
	struct megasas_cmd *cmd_mfi;
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
	u16 smid;
3349
	bool refire_cmd = 0;
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364

	fusion = instance->ctrl_context;

	/* Re-fire management commands.
	 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
	 */
	for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
		cmd_fusion = fusion->cmd_list[j];
		cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
		smid = le16_to_cpu(cmd_mfi->context.smid);

		if (!smid)
			continue;
		req_desc = megasas_get_request_descriptor
					(instance, smid - 1);
3365
		refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
3366 3367
				cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
				 (cmd_mfi->frame->dcmd.opcode !=
3368 3369 3370
				cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
				&& !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
		if (refire_cmd)
3371
			megasas_fire_cmd_fusion(instance, req_desc,
3372
				instance->is_ventura);
3373
		else
3374 3375
			megasas_return_cmd(instance, cmd_mfi);
	}
3376 3377
}

3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550
/*
 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
 * @instance: per adapter struct
 * @channel: the channel assigned by the OS
 * @id: the id assigned by the OS
 *
 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
 */

static int megasas_track_scsiio(struct megasas_instance *instance,
		int id, int channel)
{
	int i, found = 0;
	struct megasas_cmd_fusion *cmd_fusion;
	struct fusion_context *fusion;
	fusion = instance->ctrl_context;

	for (i = 0 ; i < instance->max_scsi_cmds; i++) {
		cmd_fusion = fusion->cmd_list[i];
		if (cmd_fusion->scmd &&
			(cmd_fusion->scmd->device->id == id &&
			cmd_fusion->scmd->device->channel == channel)) {
			dev_info(&instance->pdev->dev,
				"SCSI commands pending to target"
				"channel %d id %d \tSMID: 0x%x\n",
				channel, id, cmd_fusion->index);
			scsi_print_command(cmd_fusion->scmd);
			found = 1;
			break;
		}
	}

	return found ? FAILED : SUCCESS;
}

/**
 * megasas_tm_response_code - translation of device response code
 * @ioc: per adapter object
 * @mpi_reply: MPI reply returned by firmware
 *
 * Return nothing.
 */
static void
megasas_tm_response_code(struct megasas_instance *instance,
		struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
{
	char *desc;

	switch (mpi_reply->ResponseCode) {
	case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
		desc = "task management request completed";
		break;
	case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
		desc = "invalid frame";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
		desc = "task management request not supported";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
		desc = "task management request failed";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
		desc = "task management request succeeded";
		break;
	case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
		desc = "invalid lun";
		break;
	case 0xA:
		desc = "overlapped tag attempted";
		break;
	case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
		desc = "task queued, however not sent to target";
		break;
	default:
		desc = "unknown";
		break;
	}
	dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
		mpi_reply->ResponseCode, desc);
	dev_dbg(&instance->pdev->dev,
		"TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
		" 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
		mpi_reply->TerminationCount, mpi_reply->DevHandle,
		mpi_reply->Function, mpi_reply->TaskType,
		mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
}

/**
 * megasas_issue_tm - main routine for sending tm requests
 * @instance: per adapter struct
 * @device_handle: device handle
 * @channel: the channel assigned by the OS
 * @id: the id assigned by the OS
 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
 * @smid_task: smid assigned to the task
 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
 * Context: user
 *
 * MegaRaid use MPT interface for Task Magement request.
 * A generic API for sending task management requests to firmware.
 *
 * Return SUCCESS or FAILED.
 */
static int
megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
	uint channel, uint id, u16 smid_task, u8 type)
{
	struct MR_TASK_MANAGE_REQUEST *mr_request;
	struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
	unsigned long timeleft;
	struct megasas_cmd_fusion *cmd_fusion;
	struct megasas_cmd *cmd_mfi;
	union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
	struct fusion_context *fusion;
	struct megasas_cmd_fusion *scsi_lookup;
	int rc;
	struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;

	fusion = instance->ctrl_context;

	cmd_mfi = megasas_get_cmd(instance);

	if (!cmd_mfi) {
		dev_err(&instance->pdev->dev, "Failed from %s %d\n",
			__func__, __LINE__);
		return -ENOMEM;
	}

	cmd_fusion = megasas_get_cmd_fusion(instance,
			instance->max_scsi_cmds + cmd_mfi->index);

	/*  Save the smid. To be used for returning the cmd */
	cmd_mfi->context.smid = cmd_fusion->index;

	req_desc = megasas_get_request_descriptor(instance,
			(cmd_fusion->index - 1));
	if (!req_desc) {
		dev_err(&instance->pdev->dev, "Failed from %s %d\n",
			__func__, __LINE__);
		megasas_return_cmd(instance, cmd_mfi);
		return -ENOMEM;
	}

	cmd_fusion->request_desc = req_desc;
	req_desc->Words = 0;

	scsi_lookup = fusion->cmd_list[smid_task - 1];

	mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
	memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
	mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
	mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
	mpi_request->DevHandle = cpu_to_le16(device_handle);
	mpi_request->TaskType = type;
	mpi_request->TaskMID = cpu_to_le16(smid_task);
	mpi_request->LUN[1] = 0;


	req_desc = cmd_fusion->request_desc;
	req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
	req_desc->HighPriority.RequestFlags =
		(MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
		MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
	req_desc->HighPriority.MSIxIndex =  0;
	req_desc->HighPriority.LMID = 0;
	req_desc->HighPriority.Reserved1 = 0;

	if (channel < MEGASAS_MAX_PD_CHANNELS)
		mr_request->tmReqFlags.isTMForPD = 1;
	else
		mr_request->tmReqFlags.isTMForLD = 1;

	init_completion(&cmd_fusion->done);
3551
	megasas_fire_cmd_fusion(instance, req_desc, instance->is_ventura);
3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695

	timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);

	if (!timeleft) {
		dev_err(&instance->pdev->dev,
			"task mgmt type 0x%x timed out\n", type);
		cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
		mutex_unlock(&instance->reset_mutex);
		rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
		mutex_lock(&instance->reset_mutex);
		return rc;
	}

	mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
	megasas_tm_response_code(instance, mpi_reply);

	megasas_return_cmd(instance, cmd_mfi);
	rc = SUCCESS;
	switch (type) {
	case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
		if (scsi_lookup->scmd == NULL)
			break;
		else {
			instance->instancet->disable_intr(instance);
			msleep(1000);
			megasas_complete_cmd_dpc_fusion
					((unsigned long)instance);
			instance->instancet->enable_intr(instance);
			if (scsi_lookup->scmd == NULL)
				break;
		}
		rc = FAILED;
		break;

	case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
		if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
			break;
		instance->instancet->disable_intr(instance);
		msleep(1000);
		megasas_complete_cmd_dpc_fusion
				((unsigned long)instance);
		rc = megasas_track_scsiio(instance, id, channel);
		instance->instancet->enable_intr(instance);

		break;
	case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
	case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
		break;
	default:
		rc = FAILED;
		break;
	}

	return rc;

}

/*
 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
 * @instance: per adapter struct
 *
 * Return Non Zero index, if SMID found in outstanding commands
 */
static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
{
	int i, ret = 0;
	struct megasas_instance *instance;
	struct megasas_cmd_fusion *cmd_fusion;
	struct fusion_context *fusion;

	instance = (struct megasas_instance *)scmd->device->host->hostdata;

	fusion = instance->ctrl_context;

	for (i = 0; i < instance->max_scsi_cmds; i++) {
		cmd_fusion = fusion->cmd_list[i];
		if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
			scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
				" SMID: %d\n", cmd_fusion->index);
			ret = cmd_fusion->index;
			break;
		}
	}

	return ret;
}

/*
* megasas_get_tm_devhandle - Get devhandle for TM request
* @sdev-		     OS provided scsi device
*
* Returns-		     devhandle/targetID of SCSI device
*/
static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
{
	u16 pd_index = 0;
	u32 device_id;
	struct megasas_instance *instance;
	struct fusion_context *fusion;
	struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
	u16 devhandle = (u16)ULONG_MAX;

	instance = (struct megasas_instance *)sdev->host->hostdata;
	fusion = instance->ctrl_context;

	if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
		if (instance->use_seqnum_jbod_fp) {
				pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
						sdev->id;
				pd_sync = (void *)fusion->pd_seq_sync
						[(instance->pd_seq_map_id - 1) & 1];
				devhandle = pd_sync->seq[pd_index].devHandle;
		} else
			sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
				" without JBOD MAP support from %s %d\n", __func__, __LINE__);
	} else {
		device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
				+ sdev->id;
		devhandle = device_id;
	}

	return devhandle;
}

/*
 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
 * @scmd : pointer to scsi command object
 *
 * Return SUCCESS, if command aborted else FAILED
 */

int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
{
	struct megasas_instance *instance;
	u16 smid, devhandle;
	struct fusion_context *fusion;
	int ret;
	struct MR_PRIV_DEVICE *mr_device_priv_data;
	mr_device_priv_data = scmd->device->hostdata;


	instance = (struct megasas_instance *)scmd->device->host->hostdata;
	fusion = instance->ctrl_context;

3696
	if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775
		dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
		"SCSI host:%d\n", instance->host->host_no);
		ret = FAILED;
		return ret;
	}

	if (!mr_device_priv_data) {
		sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
			"scmd(%p)\n", scmd);
		scmd->result = DID_NO_CONNECT << 16;
		ret = SUCCESS;
		goto out;
	}


	if (!mr_device_priv_data->is_tm_capable) {
		ret = FAILED;
		goto out;
	}

	mutex_lock(&instance->reset_mutex);

	smid = megasas_fusion_smid_lookup(scmd);

	if (!smid) {
		ret = SUCCESS;
		scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
			" issued is not found in oustanding commands\n");
		mutex_unlock(&instance->reset_mutex);
		goto out;
	}

	devhandle = megasas_get_tm_devhandle(scmd->device);

	if (devhandle == (u16)ULONG_MAX) {
		ret = SUCCESS;
		sdev_printk(KERN_INFO, scmd->device,
			"task abort issued for invalid devhandle\n");
		mutex_unlock(&instance->reset_mutex);
		goto out;
	}
	sdev_printk(KERN_INFO, scmd->device,
		"attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
		scmd, devhandle);

	mr_device_priv_data->tm_busy = 1;
	ret = megasas_issue_tm(instance, devhandle,
			scmd->device->channel, scmd->device->id, smid,
			MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
	mr_device_priv_data->tm_busy = 0;

	mutex_unlock(&instance->reset_mutex);
out:
	sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
			((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);

	return ret;
}

/*
 * megasas_reset_target_fusion : target reset function for fusion adapters
 * scmd: SCSI command pointer
 *
 * Returns SUCCESS if all commands associated with target aborted else FAILED
 */

int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
{

	struct megasas_instance *instance;
	int ret = FAILED;
	u16 devhandle;
	struct fusion_context *fusion;
	struct MR_PRIV_DEVICE *mr_device_priv_data;
	mr_device_priv_data = scmd->device->hostdata;

	instance = (struct megasas_instance *)scmd->device->host->hostdata;
	fusion = instance->ctrl_context;

3776
	if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823
		dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
		"SCSI host:%d\n", instance->host->host_no);
		ret = FAILED;
		return ret;
	}

	if (!mr_device_priv_data) {
		sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
			"scmd(%p)\n", scmd);
		scmd->result = DID_NO_CONNECT << 16;
		ret = SUCCESS;
		goto out;
	}


	if (!mr_device_priv_data->is_tm_capable) {
		ret = FAILED;
		goto out;
	}

	mutex_lock(&instance->reset_mutex);
	devhandle = megasas_get_tm_devhandle(scmd->device);

	if (devhandle == (u16)ULONG_MAX) {
		ret = SUCCESS;
		sdev_printk(KERN_INFO, scmd->device,
			"target reset issued for invalid devhandle\n");
		mutex_unlock(&instance->reset_mutex);
		goto out;
	}

	sdev_printk(KERN_INFO, scmd->device,
		"attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
		scmd, devhandle);
	mr_device_priv_data->tm_busy = 1;
	ret = megasas_issue_tm(instance, devhandle,
			scmd->device->channel, scmd->device->id, 0,
			MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
	mr_device_priv_data->tm_busy = 0;
	mutex_unlock(&instance->reset_mutex);
out:
	scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
		(ret == SUCCESS) ? "SUCCESS" : "FAILED");

	return ret;
}

3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840
/*SRIOV get other instance in cluster if any*/
struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
{
	int i;

	for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
		if (megasas_mgmt_info.instance[i] &&
			(megasas_mgmt_info.instance[i] != instance) &&
			 megasas_mgmt_info.instance[i]->requestorId &&
			 megasas_mgmt_info.instance[i]->peerIsPresent &&
			(memcmp((megasas_mgmt_info.instance[i]->clusterId),
			instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
			return megasas_mgmt_info.instance[i];
	}
	return NULL;
}

3841 3842 3843 3844
/* Check for a second path that is currently UP */
int megasas_check_mpio_paths(struct megasas_instance *instance,
	struct scsi_cmnd *scmd)
{
3845 3846 3847 3848 3849 3850 3851 3852 3853
	struct megasas_instance *peer_instance = NULL;
	int retval = (DID_RESET << 16);

	if (instance->peerIsPresent) {
		peer_instance = megasas_get_peer_instance(instance);
		if ((peer_instance) &&
			(atomic_read(&peer_instance->adprecovery) ==
			MEGASAS_HBA_OPERATIONAL))
			retval = (DID_NO_CONNECT << 16);
3854 3855 3856 3857
	}
	return retval;
}

3858
/* Core fusion reset function */
3859
int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
3860
{
3861
	int retval = SUCCESS, i, j, convert = 0;
3862
	struct megasas_instance *instance;
3863
	struct megasas_cmd_fusion *cmd_fusion, *mpt_cmd_fusion;
3864
	struct fusion_context *fusion;
3865
	u32 abs_state, status_reg, reset_adapter;
3866
	u32 io_timeout_in_crash_mode = 0;
3867
	struct scsi_cmnd *scmd_local = NULL;
3868
	struct scsi_device *sdev;
3869 3870 3871 3872

	instance = (struct megasas_instance *)shost->hostdata;
	fusion = instance->ctrl_context;

3873 3874
	mutex_lock(&instance->reset_mutex);

3875
	if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
3876
		dev_warn(&instance->pdev->dev, "Hardware critical error, "
3877 3878
		       "returning FAILED for scsi%d.\n",
			instance->host->host_no);
3879
		mutex_unlock(&instance->reset_mutex);
3880
		return FAILED;
3881
	}
3882 3883 3884 3885 3886
	status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
	abs_state = status_reg & MFI_STATE_MASK;

	/* IO timeout detected, forcibly put FW in FAULT state */
	if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
3887 3888
		instance->crash_dump_app_support && reason) {
		dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
3889
			"forcibly FAULT Firmware\n");
3890
		atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901
		status_reg = readl(&instance->reg_set->doorbell);
		writel(status_reg | MFI_STATE_FORCE_OCR,
			&instance->reg_set->doorbell);
		readl(&instance->reg_set->doorbell);
		mutex_unlock(&instance->reset_mutex);
		do {
			ssleep(3);
			io_timeout_in_crash_mode++;
			dev_dbg(&instance->pdev->dev, "waiting for [%d] "
				"seconds for crash dump collection and OCR "
				"to be done\n", (io_timeout_in_crash_mode * 3));
3902
		} while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
3903 3904
			(io_timeout_in_crash_mode < 80));

3905
		if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917
			dev_info(&instance->pdev->dev, "OCR done for IO "
				"timeout case\n");
			retval = SUCCESS;
		} else {
			dev_info(&instance->pdev->dev, "Controller is not "
				"operational after 240 seconds wait for IO "
				"timeout case in FW crash dump mode\n do "
				"OCR/kill adapter\n");
			retval = megasas_reset_fusion(shost, 0);
		}
		return retval;
	}
3918

3919 3920
	if (instance->requestorId && !instance->skip_heartbeat_timer_del)
		del_timer_sync(&instance->sriov_heartbeat_timer);
3921
	set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
3922
	atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
3923
	instance->instancet->disable_intr(instance);
3924 3925
	msleep(1000);

3926
	/* First try waiting for commands to complete */
3927
	if (megasas_wait_for_outstanding_fusion(instance, reason,
3928
						&convert)) {
3929
		atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3930
		dev_warn(&instance->pdev->dev, "resetting fusion "
3931 3932
		       "adapter scsi%d.\n", instance->host->host_no);
		if (convert)
3933
			reason = 0;
3934

3935
		/* Now return commands back to the OS */
3936
		for (i = 0 ; i < instance->max_scsi_cmds; i++) {
3937
			cmd_fusion = fusion->cmd_list[i];
3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949
			/*check for extra commands issued by driver*/
			if (instance->is_ventura) {
				cmd_fusion->is_raid_1_fp_write = 0;
				cmd_fusion->cmd_completed = false;
				mpt_cmd_fusion =
				fusion->cmd_list[i + instance->max_fw_cmds];
				mpt_cmd_fusion->is_raid_1_fp_write = 0;
				mpt_cmd_fusion->cmd_completed = false;
				if (mpt_cmd_fusion->scmd)
					megasas_return_cmd_fusion(instance,
						mpt_cmd_fusion);
			}
3950
			scmd_local = cmd_fusion->scmd;
3951
			if (cmd_fusion->scmd) {
3952
				scmd_local->result =
3953
					megasas_check_mpio_paths(instance,
3954
							scmd_local);
3955 3956
				if (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
					atomic_dec(&instance->ldio_outstanding);
3957
				megasas_return_cmd_fusion(instance, cmd_fusion);
3958 3959
				scsi_dma_unmap(scmd_local);
				scmd_local->scsi_done(scmd_local);
3960 3961 3962
			}
		}

3963 3964
		atomic_set(&instance->fw_outstanding, 0);

3965 3966 3967 3968 3969 3970
		status_reg = instance->instancet->read_fw_status_reg(
			instance->reg_set);
		abs_state = status_reg & MFI_STATE_MASK;
		reset_adapter = status_reg & MFI_RESET_ADAPTER;
		if (instance->disableOnlineCtrlReset ||
		    (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
3971
			/* Reset not supported, kill adapter */
3972
			dev_warn(&instance->pdev->dev, "Reset not supported"
3973 3974
			       ", killing adapter scsi%d.\n",
				instance->host->host_no);
3975
			megaraid_sas_kill_hba(instance);
3976
			instance->skip_heartbeat_timer_del = 1;
3977 3978 3979 3980
			retval = FAILED;
			goto out;
		}

3981
		/* Let SR-IOV VF & PF sync up if there was a HB failure */
3982
		if (instance->requestorId && !reason) {
3983
			msleep(MEGASAS_OCR_SETTLE_TIME_VF);
S
Sumit Saxena 已提交
3984
			goto transition_to_ready;
3985 3986
		}

3987 3988 3989
		/* Now try to reset the chip */
		for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {

3990 3991
			if (instance->instancet->adp_reset
				(instance, instance->reg_set))
3992
				continue;
S
Sumit Saxena 已提交
3993
transition_to_ready:
3994
			/* Wait for FW to become ready */
3995
			if (megasas_transition_to_ready(instance, 1)) {
S
Sumit Saxena 已提交
3996 3997 3998 3999 4000 4001 4002
				dev_warn(&instance->pdev->dev,
					"Failed to transition controller to ready for "
					"scsi%d.\n", instance->host->host_no);
				if (instance->requestorId && !reason)
					goto fail_kill_adapter;
				else
					continue;
4003 4004
			}
			megasas_reset_reply_desc(instance);
4005 4006
			megasas_fusion_update_can_queue(instance, OCR_CONTEXT);

4007
			if (megasas_ioc_init_fusion(instance)) {
4008
				dev_warn(&instance->pdev->dev,
S
Sumit Saxena 已提交
4009 4010 4011 4012 4013 4014
				       "megasas_ioc_init_fusion() failed! for "
				       "scsi%d\n", instance->host->host_no);
				if (instance->requestorId && !reason)
					goto fail_kill_adapter;
				else
					continue;
4015 4016
			}

4017
			megasas_refire_mgmt_cmd(instance);
4018

4019 4020 4021 4022 4023 4024 4025
			if (megasas_get_ctrl_info(instance)) {
				dev_info(&instance->pdev->dev,
					"Failed from %s %d\n",
					__func__, __LINE__);
				megaraid_sas_kill_hba(instance);
				retval = FAILED;
			}
4026 4027 4028
			/* Reset load balance info */
			memset(fusion->load_balance_info, 0,
			       sizeof(struct LD_LOAD_BALANCE_INFO)
4029
			       *MAX_LOGICAL_DRIVES_EXT);
4030 4031 4032 4033

			if (!megasas_get_map_info(instance))
				megasas_sync_map_info(instance);

4034 4035
			megasas_setup_jbod_map(instance);

4036 4037 4038
			shost_for_each_device(sdev, shost)
				megasas_update_sdev_properties(sdev);

4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
			/* reset stream detection array */
			if (instance->is_ventura) {
				for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
					memset(fusion->stream_detect_by_ld[j],
					0, sizeof(struct LD_STREAM_DETECT));
				 fusion->stream_detect_by_ld[j]->mru_bit_map
						= MR_STREAM_BITMAP;
				}
			}

4049 4050 4051
			clear_bit(MEGASAS_FUSION_IN_RESET,
				  &instance->reset_flags);
			instance->instancet->enable_intr(instance);
4052
			atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4053

4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064
			/* Restart SR-IOV heartbeat */
			if (instance->requestorId) {
				if (!megasas_sriov_start_heartbeat(instance, 0))
					megasas_start_timer(instance,
							    &instance->sriov_heartbeat_timer,
							    megasas_sriov_heartbeat_handler,
							    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
				else
					instance->skip_heartbeat_timer_del = 1;
			}

4065
			/* Adapter reset completed successfully */
4066
			dev_warn(&instance->pdev->dev, "Reset "
4067 4068
			       "successful for scsi%d.\n",
				instance->host->host_no);
4069

4070 4071 4072 4073 4074 4075 4076 4077
			if (instance->crash_dump_drv_support &&
				instance->crash_dump_app_support)
				megasas_set_crash_dump_params(instance,
					MR_CRASH_BUF_TURN_ON);
			else
				megasas_set_crash_dump_params(instance,
					MR_CRASH_BUF_TURN_OFF);

4078 4079 4080
			retval = SUCCESS;
			goto out;
		}
S
Sumit Saxena 已提交
4081
fail_kill_adapter:
4082
		/* Reset failed, kill the adapter */
4083
		dev_warn(&instance->pdev->dev, "Reset failed, killing "
4084
		       "adapter scsi%d.\n", instance->host->host_no);
4085
		megaraid_sas_kill_hba(instance);
4086
		instance->skip_heartbeat_timer_del = 1;
4087 4088
		retval = FAILED;
	} else {
4089 4090 4091 4092 4093 4094 4095
		/* For VF: Restart HB timer if we didn't OCR */
		if (instance->requestorId) {
			megasas_start_timer(instance,
					    &instance->sriov_heartbeat_timer,
					    megasas_sriov_heartbeat_handler,
					    MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
		}
4096
		clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4097
		instance->instancet->enable_intr(instance);
4098
		atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4099 4100 4101 4102 4103 4104 4105
	}
out:
	clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
	mutex_unlock(&instance->reset_mutex);
	return retval;
}

4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173
/* Fusion Crash dump collection work queue */
void  megasas_fusion_crash_dump_wq(struct work_struct *work)
{
	struct megasas_instance *instance =
		container_of(work, struct megasas_instance, crash_init);
	u32 status_reg;
	u8 partial_copy = 0;


	status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);

	/*
	 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
	 * to host crash buffers
	 */
	if (instance->drv_buf_index == 0) {
		/* Buffer is already allocated for old Crash dump.
		 * Do OCR and do not wait for crash dump collection
		 */
		if (instance->drv_buf_alloc) {
			dev_info(&instance->pdev->dev, "earlier crash dump is "
				"not yet copied by application, ignoring this "
				"crash dump and initiating OCR\n");
			status_reg |= MFI_STATE_CRASH_DUMP_DONE;
			writel(status_reg,
				&instance->reg_set->outbound_scratch_pad);
			readl(&instance->reg_set->outbound_scratch_pad);
			return;
		}
		megasas_alloc_host_crash_buffer(instance);
		dev_info(&instance->pdev->dev, "Number of host crash buffers "
			"allocated: %d\n", instance->drv_buf_alloc);
	}

	/*
	 * Driver has allocated max buffers, which can be allocated
	 * and FW has more crash dump data, then driver will
	 * ignore the data.
	 */
	if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
		dev_info(&instance->pdev->dev, "Driver is done copying "
			"the buffer: %d\n", instance->drv_buf_alloc);
		status_reg |= MFI_STATE_CRASH_DUMP_DONE;
		partial_copy = 1;
	} else {
		memcpy(instance->crash_buf[instance->drv_buf_index],
			instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
		instance->drv_buf_index++;
		status_reg &= ~MFI_STATE_DMADONE;
	}

	if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
		dev_info(&instance->pdev->dev, "Crash Dump is available,number "
			"of copied buffers: %d\n", instance->drv_buf_index);
		instance->fw_crash_buffer_size =  instance->drv_buf_index;
		instance->fw_crash_state = AVAILABLE;
		instance->drv_buf_index = 0;
		writel(status_reg, &instance->reg_set->outbound_scratch_pad);
		readl(&instance->reg_set->outbound_scratch_pad);
		if (!partial_copy)
			megasas_reset_fusion(instance->host, 0);
	} else {
		writel(status_reg, &instance->reg_set->outbound_scratch_pad);
		readl(&instance->reg_set->outbound_scratch_pad);
	}
}


4174 4175 4176 4177 4178 4179
/* Fusion OCR work queue */
void megasas_fusion_ocr_wq(struct work_struct *work)
{
	struct megasas_instance *instance =
		container_of(work, struct megasas_instance, work_init);

4180
	megasas_reset_fusion(instance->host, 0);
4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
}

struct megasas_instance_template megasas_instance_template_fusion = {
	.enable_intr = megasas_enable_intr_fusion,
	.disable_intr = megasas_disable_intr_fusion,
	.clear_intr = megasas_clear_intr_fusion,
	.read_fw_status_reg = megasas_read_fw_status_reg_fusion,
	.adp_reset = megasas_adp_reset_fusion,
	.check_reset = megasas_check_reset_fusion,
	.service_isr = megasas_isr_fusion,
	.tasklet = megasas_complete_cmd_dpc_fusion,
	.init_adapter = megasas_init_adapter_fusion,
	.build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
	.issue_dcmd = megasas_issue_dcmd_fusion,
};