mpt2sas_base.c 131.7 KB
Newer Older
E
Eric Moore 已提交
1 2 3 4 5
/*
 * This is the Fusion MPT base driver providing common API layer interface
 * for access to MPT (Message Passing Technology) firmware.
 *
 * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
6
 * Copyright (C) 2007-2014  LSI Corporation
E
Eric Moore 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 *  (mailto:DL-MPTFusionLinux@lsi.com)
 *
 * 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.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.

 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
#include <linux/sort.h>
#include <linux/io.h>
59
#include <linux/time.h>
60
#include <linux/kthread.h>
61
#include <linux/aer.h>
E
Eric Moore 已提交
62 63 64 65 66 67 68

#include "mpt2sas_base.h"

static MPT_CALLBACK	mpt_callbacks[MPT_MAX_CALLBACKS];

#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */

69 70
#define MAX_HBA_QUEUE_DEPTH	30000
#define MAX_CHAIN_DEPTH		100000
E
Eric Moore 已提交
71 72 73 74 75 76 77 78 79 80 81 82
static int max_queue_depth = -1;
module_param(max_queue_depth, int, 0);
MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");

static int max_sgl_entries = -1;
module_param(max_sgl_entries, int, 0);
MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");

static int msix_disable = -1;
module_param(msix_disable, int, 0);
MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");

83 84 85 86
static int max_msix_vectors = -1;
module_param(max_msix_vectors, int, 0);
MODULE_PARM_DESC(max_msix_vectors, " max msix vectors ");

87 88 89 90 91 92 93 94
static int mpt2sas_fwfault_debug;
MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
	"and halt firmware - (default=0)");

static int disable_discovery = -1;
module_param(disable_discovery, int, 0);
MODULE_PARM_DESC(disable_discovery, " disable discovery ");

95 96 97 98 99 100 101 102 103 104 105 106 107
/**
 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
 *
 */
static int
_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
{
	int ret = param_set_int(val, kp);
	struct MPT2SAS_ADAPTER *ioc;

	if (ret)
		return ret;

108
	printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
109 110 111 112
	list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
		ioc->fwfault_debug = mpt2sas_fwfault_debug;
	return 0;
}
113

114 115 116
module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
    param_get_int, &mpt2sas_fwfault_debug, 0644);

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
/**
 *  mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
 * @arg: input argument, used to derive ioc
 *
 * Return 0 if controller is removed from pci subsystem.
 * Return -1 for other case.
 */
static int mpt2sas_remove_dead_ioc_func(void *arg)
{
		struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
		struct pci_dev *pdev;

		if ((ioc == NULL))
			return -1;

		pdev = ioc->pdev;
		if ((pdev == NULL))
			return -1;
135
		pci_stop_and_remove_bus_device_locked(pdev);
136 137 138 139
		return 0;
}


E
Eric Moore 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
/**
 * _base_fault_reset_work - workq handling ioc fault conditions
 * @work: input argument, used to derive ioc
 * Context: sleep.
 *
 * Return nothing.
 */
static void
_base_fault_reset_work(struct work_struct *work)
{
	struct MPT2SAS_ADAPTER *ioc =
	    container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
	unsigned long	 flags;
	u32 doorbell;
	int rc;
155
	struct task_struct *p;
E
Eric Moore 已提交
156 157

	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
158
	if (ioc->shost_recovery || ioc->pci_error_recovery)
E
Eric Moore 已提交
159 160 161 162
		goto rearm_timer;
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);

	doorbell = mpt2sas_base_get_iocstate(ioc, 0);
163 164 165 166
	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
		printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
			ioc->name, __func__);

167 168 169 170 171 172 173 174 175 176 177 178 179 180
		/* It may be possible that EEH recovery can resolve some of
		 * pci bus failure issues rather removing the dead ioc function
		 * by considering controller is in a non-operational state. So
		 * here priority is given to the EEH recovery. If it doesn't
		 * not resolve this issue, mpt2sas driver will consider this
		 * controller to non-operational state and remove the dead ioc
		 * function.
		 */
		if (ioc->non_operational_loop++ < 5) {
			spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
							 flags);
			goto rearm_timer;
		}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
		/*
		 * Call _scsih_flush_pending_cmds callback so that we flush all
		 * pending commands back to OS. This call is required to aovid
		 * deadlock at block layer. Dead IOC will fail to do diag reset,
		 * and this call is safe since dead ioc will never return any
		 * command back from HW.
		 */
		ioc->schedule_dead_ioc_flush_running_cmds(ioc);
		/*
		 * Set remove_host flag early since kernel thread will
		 * take some time to execute.
		 */
		ioc->remove_host = 1;
		/*Remove the Dead Host */
		p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
		    "mpt2sas_dead_ioc_%d", ioc->id);
		if (IS_ERR(p)) {
			printk(MPT2SAS_ERR_FMT
			"%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
			ioc->name, __func__);
		} else {
		    printk(MPT2SAS_ERR_FMT
			"%s: Running mpt2sas_dead_ioc thread success !!!!\n",
			ioc->name, __func__);
		}

		return; /* don't rearm timer */
	}

210 211
	ioc->non_operational_loop = 0;

E
Eric Moore 已提交
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
		rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
		    FORCE_BIG_HAMMER);
		printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
		    __func__, (rc == 0) ? "success" : "failed");
		doorbell = mpt2sas_base_get_iocstate(ioc, 0);
		if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
			mpt2sas_base_fault_info(ioc, doorbell &
			    MPI2_DOORBELL_DATA_MASK);
	}

	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
 rearm_timer:
	if (ioc->fault_reset_work_q)
		queue_delayed_work(ioc->fault_reset_work_q,
		    &ioc->fault_reset_work,
		    msecs_to_jiffies(FAULT_POLLING_INTERVAL));
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
}

232 233
/**
 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
234
 * @ioc: per adapter object
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
 * Context: sleep.
 *
 * Return nothing.
 */
void
mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
{
	unsigned long	 flags;

	if (ioc->fault_reset_work_q)
		return;

	/* initialize fault polling */
	INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
	snprintf(ioc->fault_reset_work_q_name,
	    sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
	ioc->fault_reset_work_q =
		create_singlethread_workqueue(ioc->fault_reset_work_q_name);
	if (!ioc->fault_reset_work_q) {
		printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
		    ioc->name, __func__, __LINE__);
			return;
	}
	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
	if (ioc->fault_reset_work_q)
		queue_delayed_work(ioc->fault_reset_work_q,
		    &ioc->fault_reset_work,
		    msecs_to_jiffies(FAULT_POLLING_INTERVAL));
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
}

/**
 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
268
 * @ioc: per adapter object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
 * Context: sleep.
 *
 * Return nothing.
 */
void
mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
{
	unsigned long	 flags;
	struct workqueue_struct *wq;

	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
	wq = ioc->fault_reset_work_q;
	ioc->fault_reset_work_q = NULL;
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
	if (wq) {
284
		if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
285 286 287 288 289
			flush_workqueue(wq);
		destroy_workqueue(wq);
	}
}

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
/**
 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
 * @ioc: per adapter object
 * @fault_code: fault code
 *
 * Return nothing.
 */
void
mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
{
	printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
	    ioc->name, fault_code);
}

/**
 * mpt2sas_halt_firmware - halt's mpt controller firmware
 * @ioc: per adapter object
 *
 * For debugging timeout related issues.  Writing 0xCOFFEE00
 * to the doorbell register will halt controller firmware. With
 * the purpose to stop both driver and firmware, the enduser can
 * obtain a ring buffer from controller UART.
 */
void
mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
{
	u32 doorbell;

	if (!ioc->fwfault_debug)
		return;

	dump_stack();

	doorbell = readl(&ioc->chip->Doorbell);
	if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
		mpt2sas_base_fault_info(ioc , doorbell);
	else {
		writel(0xC0FFEE00, &ioc->chip->Doorbell);
		printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
		    "timeout\n", ioc->name);
	}

	panic("panic in %s\n", __func__);
}

E
Eric Moore 已提交
335 336 337
#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
/**
 * _base_sas_ioc_info - verbose translation of the ioc status
338
 * @ioc: per adapter object
E
Eric Moore 已提交
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
 * @mpi_reply: reply mf payload returned from firmware
 * @request_hdr: request mf
 *
 * Return nothing.
 */
static void
_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
     MPI2RequestHeader_t *request_hdr)
{
	u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	char *desc = NULL;
	u16 frame_sz;
	char *func_str = NULL;

	/* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
	if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
	    request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
	    request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
		return;

360 361 362
	if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
		return;

E
Eric Moore 已提交
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 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 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
	switch (ioc_status) {

/****************************************************************************
*  Common IOCStatus values for all replies
****************************************************************************/

	case MPI2_IOCSTATUS_INVALID_FUNCTION:
		desc = "invalid function";
		break;
	case MPI2_IOCSTATUS_BUSY:
		desc = "busy";
		break;
	case MPI2_IOCSTATUS_INVALID_SGL:
		desc = "invalid sgl";
		break;
	case MPI2_IOCSTATUS_INTERNAL_ERROR:
		desc = "internal error";
		break;
	case MPI2_IOCSTATUS_INVALID_VPID:
		desc = "invalid vpid";
		break;
	case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
		desc = "insufficient resources";
		break;
	case MPI2_IOCSTATUS_INVALID_FIELD:
		desc = "invalid field";
		break;
	case MPI2_IOCSTATUS_INVALID_STATE:
		desc = "invalid state";
		break;
	case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
		desc = "op state not supported";
		break;

/****************************************************************************
*  Config IOCStatus values
****************************************************************************/

	case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
		desc = "config invalid action";
		break;
	case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
		desc = "config invalid type";
		break;
	case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
		desc = "config invalid page";
		break;
	case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
		desc = "config invalid data";
		break;
	case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
		desc = "config no defaults";
		break;
	case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
		desc = "config cant commit";
		break;

/****************************************************************************
*  SCSI IO Reply
****************************************************************************/

	case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
	case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
	case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
	case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
	case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
	case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
	case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
	case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
	case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
	case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
	case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
	case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
		break;

/****************************************************************************
*  For use by SCSI Initiator and SCSI Target end-to-end data protection
****************************************************************************/

	case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
		desc = "eedp guard error";
		break;
	case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
		desc = "eedp ref tag error";
		break;
	case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
		desc = "eedp app tag error";
		break;

/****************************************************************************
*  SCSI Target values
****************************************************************************/

	case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
		desc = "target invalid io index";
		break;
	case MPI2_IOCSTATUS_TARGET_ABORTED:
		desc = "target aborted";
		break;
	case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
		desc = "target no conn retryable";
		break;
	case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
		desc = "target no connection";
		break;
	case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
		desc = "target xfer count mismatch";
		break;
	case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
		desc = "target data offset error";
		break;
	case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
		desc = "target too much write data";
		break;
	case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
		desc = "target iu too short";
		break;
	case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
		desc = "target ack nak timeout";
		break;
	case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
		desc = "target nak received";
		break;

/****************************************************************************
*  Serial Attached SCSI values
****************************************************************************/

	case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
		desc = "smp request failed";
		break;
	case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
		desc = "smp data overrun";
		break;

/****************************************************************************
*  Diagnostic Buffer Post / Diagnostic Release values
****************************************************************************/

	case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
		desc = "diagnostic released";
		break;
	default:
		break;
	}

	if (!desc)
		return;

	switch (request_hdr->Function) {
	case MPI2_FUNCTION_CONFIG:
		frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
		func_str = "config_page";
		break;
	case MPI2_FUNCTION_SCSI_TASK_MGMT:
		frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
		func_str = "task_mgmt";
		break;
	case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
		frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
		func_str = "sas_iounit_ctl";
		break;
	case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
		frame_sz = sizeof(Mpi2SepRequest_t);
		func_str = "enclosure";
		break;
	case MPI2_FUNCTION_IOC_INIT:
		frame_sz = sizeof(Mpi2IOCInitRequest_t);
		func_str = "ioc_init";
		break;
	case MPI2_FUNCTION_PORT_ENABLE:
		frame_sz = sizeof(Mpi2PortEnableRequest_t);
		func_str = "port_enable";
		break;
	case MPI2_FUNCTION_SMP_PASSTHROUGH:
		frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
		func_str = "smp_passthru";
		break;
	default:
		frame_sz = 32;
		func_str = "unknown";
		break;
	}

	printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
	    " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);

	_debug_dump_mf(request_hdr, frame_sz/4);
}

/**
 * _base_display_event_data - verbose translation of firmware asyn events
555
 * @ioc: per adapter object
E
Eric Moore 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
 * @mpi_reply: reply mf payload returned from firmware
 *
 * Return nothing.
 */
static void
_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
    Mpi2EventNotificationReply_t *mpi_reply)
{
	char *desc = NULL;
	u16 event;

	if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
		return;

	event = le16_to_cpu(mpi_reply->Event);

	switch (event) {
	case MPI2_EVENT_LOG_DATA:
		desc = "Log Data";
		break;
	case MPI2_EVENT_STATE_CHANGE:
		desc = "Status Change";
		break;
	case MPI2_EVENT_HARD_RESET_RECEIVED:
		desc = "Hard Reset Received";
		break;
	case MPI2_EVENT_EVENT_CHANGE:
		desc = "Event Change";
		break;
	case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
		desc = "Device Status Change";
		break;
	case MPI2_EVENT_IR_OPERATION_STATUS:
589 590
		if (!ioc->hide_ir_msg)
			desc = "IR Operation Status";
E
Eric Moore 已提交
591 592
		break;
	case MPI2_EVENT_SAS_DISCOVERY:
593 594 595 596 597 598 599 600 601
	{
		Mpi2EventDataSasDiscovery_t *event_data =
		    (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
		printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
		    (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
		    "start" : "stop");
		if (event_data->DiscoveryStatus)
			printk("discovery_status(0x%08x)",
			    le32_to_cpu(event_data->DiscoveryStatus));
602
		printk("\n");
603 604
		return;
	}
E
Eric Moore 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
		desc = "SAS Broadcast Primitive";
		break;
	case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
		desc = "SAS Init Device Status Change";
		break;
	case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
		desc = "SAS Init Table Overflow";
		break;
	case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
		desc = "SAS Topology Change List";
		break;
	case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
		desc = "SAS Enclosure Device Status Change";
		break;
	case MPI2_EVENT_IR_VOLUME:
621 622
		if (!ioc->hide_ir_msg)
			desc = "IR Volume";
E
Eric Moore 已提交
623 624
		break;
	case MPI2_EVENT_IR_PHYSICAL_DISK:
625 626
		if (!ioc->hide_ir_msg)
			desc = "IR Physical Disk";
E
Eric Moore 已提交
627 628
		break;
	case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
629 630
		if (!ioc->hide_ir_msg)
			desc = "IR Configuration Change List";
E
Eric Moore 已提交
631 632
		break;
	case MPI2_EVENT_LOG_ENTRY_ADDED:
633 634
		if (!ioc->hide_ir_msg)
			desc = "Log Entry Added";
E
Eric Moore 已提交
635 636 637 638 639 640 641 642 643 644 645 646
		break;
	}

	if (!desc)
		return;

	printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
}
#endif

/**
 * _base_sas_log_info - verbose translation of firmware log info
647
 * @ioc: per adapter object
E
Eric Moore 已提交
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
 * @log_info: log info
 *
 * Return nothing.
 */
static void
_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
{
	union loginfo_type {
		u32	loginfo;
		struct {
			u32	subcode:16;
			u32	code:8;
			u32	originator:4;
			u32	bus_type:4;
		} dw;
	};
	union loginfo_type sas_loginfo;
	char *originator_str = NULL;

	sas_loginfo.loginfo = log_info;
	if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
		return;

671 672 673 674
	/* each nexus loss loginfo */
	if (log_info == 0x31170000)
		return;

E
Eric Moore 已提交
675
	/* eat the loginfos associated with task aborts */
676
	if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
E
Eric Moore 已提交
677 678 679 680 681 682 683 684 685 686 687
	    0x31140000 || log_info == 0x31130000))
		return;

	switch (sas_loginfo.dw.originator) {
	case 0:
		originator_str = "IOP";
		break;
	case 1:
		originator_str = "PL";
		break;
	case 2:
688 689 690 691
		if (!ioc->hide_ir_msg)
			originator_str = "IR";
		else
			originator_str = "WarpDrive";
E
Eric Moore 已提交
692 693 694 695 696 697 698 699 700 701 702
		break;
	}

	printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
	    "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
	     originator_str, sas_loginfo.dw.code,
	     sas_loginfo.dw.subcode);
}

/**
 * _base_display_reply_info -
703
 * @ioc: per adapter object
E
Eric Moore 已提交
704
 * @smid: system request message index
705
 * @msix_index: MSIX table index supplied by the OS
E
Eric Moore 已提交
706 707 708 709 710
 * @reply: reply message frame(lower 32bit addr)
 *
 * Return nothing.
 */
static void
711
_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
E
Eric Moore 已提交
712 713 714 715 716 717
    u32 reply)
{
	MPI2DefaultReply_t *mpi_reply;
	u16 ioc_status;

	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
718 719 720 721 722
	if (unlikely(!mpi_reply)) {
		printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
			ioc->name, __FILE__, __LINE__, __func__);
		return;
	}
E
Eric Moore 已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
	if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
	    (ioc->logging_level & MPT_DEBUG_REPLY)) {
		_base_sas_ioc_info(ioc , mpi_reply,
		   mpt2sas_base_get_msg_frame(ioc, smid));
	}
#endif
	if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
		_base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
}

/**
 * mpt2sas_base_done - base internal command completion routine
737
 * @ioc: per adapter object
E
Eric Moore 已提交
738
 * @smid: system request message index
739
 * @msix_index: MSIX table index supplied by the OS
E
Eric Moore 已提交
740 741
 * @reply: reply message frame(lower 32bit addr)
 *
742 743
 * Return 1 meaning mf should be freed from _base_interrupt
 *        0 means the mf is freed from this function.
E
Eric Moore 已提交
744
 */
745
u8
746 747
mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
    u32 reply)
E
Eric Moore 已提交
748 749 750 751 752
{
	MPI2DefaultReply_t *mpi_reply;

	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
	if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
753
		return 1;
E
Eric Moore 已提交
754 755

	if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
756
		return 1;
E
Eric Moore 已提交
757 758 759 760 761 762 763

	ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
	if (mpi_reply) {
		ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
		memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
	}
	ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
764

E
Eric Moore 已提交
765
	complete(&ioc->base_cmds.done);
766
	return 1;
E
Eric Moore 已提交
767 768 769 770
}

/**
 * _base_async_event - main callback handler for firmware asyn events
771
 * @ioc: per adapter object
772
 * @msix_index: MSIX table index supplied by the OS
E
Eric Moore 已提交
773 774
 * @reply: reply message frame(lower 32bit addr)
 *
775
 * Returns void.
E
Eric Moore 已提交
776
 */
777
static void
778
_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
E
Eric Moore 已提交
779 780 781 782 783 784 785
{
	Mpi2EventNotificationReply_t *mpi_reply;
	Mpi2EventAckRequest_t *ack_request;
	u16 smid;

	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
	if (!mpi_reply)
786
		return;
E
Eric Moore 已提交
787
	if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
788
		return;
E
Eric Moore 已提交
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
	_base_display_event_data(ioc, mpi_reply);
#endif
	if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
		goto out;
	smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		goto out;
	}

	ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
	memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
	ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
	ack_request->Event = mpi_reply->Event;
	ack_request->EventContext = mpi_reply->EventContext;
806 807 808
	ack_request->VF_ID = 0;  /* TODO */
	ack_request->VP_ID = 0;
	mpt2sas_base_put_smid_default(ioc, smid);
E
Eric Moore 已提交
809 810 811 812

 out:

	/* scsih callback handler */
813
	mpt2sas_scsih_event_callback(ioc, msix_index, reply);
E
Eric Moore 已提交
814 815

	/* ctl callback handler */
816
	mpt2sas_ctl_event_callback(ioc, msix_index, reply);
817

818
	return;
E
Eric Moore 已提交
819 820
}

821 822 823 824 825 826 827 828 829 830 831
/**
 * _base_get_cb_idx - obtain the callback index
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return callback index.
 */
static u8
_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
{
	int i;
832 833 834
	u8 cb_idx;

	if (smid < ioc->hi_priority_smid) {
835 836
		i = smid - 1;
		cb_idx = ioc->scsi_lookup[i].cb_idx;
837 838 839 840 841 842 843 844
	} else if (smid < ioc->internal_smid) {
		i = smid - ioc->hi_priority_smid;
		cb_idx = ioc->hpr_lookup[i].cb_idx;
	} else if (smid <= ioc->hba_queue_depth) {
		i = smid - ioc->internal_smid;
		cb_idx = ioc->internal_lookup[i].cb_idx;
	} else
		cb_idx = 0xFF;
845 846 847
	return cb_idx;
}

E
Eric Moore 已提交
848 849
/**
 * _base_mask_interrupts - disable interrupts
850
 * @ioc: per adapter object
E
Eric Moore 已提交
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
 *
 * Disabling ResetIRQ, Reply and Doorbell Interrupts
 *
 * Return nothing.
 */
static void
_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
{
	u32 him_register;

	ioc->mask_interrupts = 1;
	him_register = readl(&ioc->chip->HostInterruptMask);
	him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
	writel(him_register, &ioc->chip->HostInterruptMask);
	readl(&ioc->chip->HostInterruptMask);
}

/**
 * _base_unmask_interrupts - enable interrupts
870
 * @ioc: per adapter object
E
Eric Moore 已提交
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
 *
 * Enabling only Reply Interrupts
 *
 * Return nothing.
 */
static void
_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
{
	u32 him_register;

	him_register = readl(&ioc->chip->HostInterruptMask);
	him_register &= ~MPI2_HIM_RIM;
	writel(him_register, &ioc->chip->HostInterruptMask);
	ioc->mask_interrupts = 0;
}

887 888 889 890 891 892 893 894
union reply_descriptor {
	u64 word;
	struct {
		u32 low;
		u32 high;
	} u;
};

E
Eric Moore 已提交
895 896 897 898 899 900 901 902 903 904 905
/**
 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
 * @irq: irq number (not used)
 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
 * @r: pt_regs pointer (not used)
 *
 * Return IRQ_HANDLE if processed, else IRQ_NONE.
 */
static irqreturn_t
_base_interrupt(int irq, void *bus_id)
{
906
	struct adapter_reply_queue *reply_q = bus_id;
907
	union reply_descriptor rd;
908
	u32 completed_cmds;
E
Eric Moore 已提交
909 910 911 912
	u8 request_desript_type;
	u16 smid;
	u8 cb_idx;
	u32 reply;
913 914
	u8 msix_index = reply_q->msix_index;
	struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
915
	Mpi2ReplyDescriptorsUnion_t *rpf;
916
	u8 rc;
E
Eric Moore 已提交
917 918 919 920

	if (ioc->mask_interrupts)
		return IRQ_NONE;

921 922 923 924
	if (!atomic_add_unless(&reply_q->busy, 1, 1))
		return IRQ_NONE;

	rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
925 926
	request_desript_type = rpf->Default.ReplyFlags
	     & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
927 928
	if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
		atomic_dec(&reply_q->busy);
E
Eric Moore 已提交
929
		return IRQ_NONE;
930
	}
E
Eric Moore 已提交
931 932

	completed_cmds = 0;
933
	cb_idx = 0xFF;
E
Eric Moore 已提交
934
	do {
935
		rd.word = le64_to_cpu(rpf->Words);
936
		if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
E
Eric Moore 已提交
937 938
			goto out;
		reply = 0;
939
		smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
E
Eric Moore 已提交
940 941
		if (request_desript_type ==
		    MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
942 943
			reply = le32_to_cpu
				(rpf->AddressReply.ReplyFrameAddress);
944 945 946
			if (reply > ioc->reply_dma_max_address ||
			    reply < ioc->reply_dma_min_address)
				reply = 0;
E
Eric Moore 已提交
947 948 949 950 951 952
		} else if (request_desript_type ==
		    MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
			goto next;
		else if (request_desript_type ==
		    MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
			goto next;
953
		if (smid) {
954
			cb_idx = _base_get_cb_idx(ioc, smid);
955 956 957 958
		if ((likely(cb_idx < MPT_MAX_CALLBACKS))
			    && (likely(mpt_callbacks[cb_idx] != NULL))) {
				rc = mpt_callbacks[cb_idx](ioc, smid,
				    msix_index, reply);
E
Eric Moore 已提交
959
			if (reply)
960 961
				_base_display_reply_info(ioc, smid,
				    msix_index, reply);
962 963
			if (rc)
				mpt2sas_base_free_smid(ioc, smid);
964
			}
E
Eric Moore 已提交
965 966
		}
		if (!smid)
967
			_base_async_event(ioc, msix_index, reply);
E
Eric Moore 已提交
968 969 970 971 972 973 974 975 976

		/* reply free queue handling */
		if (reply) {
			ioc->reply_free_host_index =
			    (ioc->reply_free_host_index ==
			    (ioc->reply_free_queue_depth - 1)) ?
			    0 : ioc->reply_free_host_index + 1;
			ioc->reply_free[ioc->reply_free_host_index] =
			    cpu_to_le32(reply);
977
			wmb();
E
Eric Moore 已提交
978 979 980 981 982
			writel(ioc->reply_free_host_index,
			    &ioc->chip->ReplyFreeHostIndex);
		}

 next:
983

984
		rpf->Words = cpu_to_le64(ULLONG_MAX);
985 986
		reply_q->reply_post_host_index =
		    (reply_q->reply_post_host_index ==
987
		    (ioc->reply_post_queue_depth - 1)) ? 0 :
988
		    reply_q->reply_post_host_index + 1;
E
Eric Moore 已提交
989
		request_desript_type =
990 991
		    reply_q->reply_post_free[reply_q->reply_post_host_index].
		    Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
E
Eric Moore 已提交
992 993 994
		completed_cmds++;
		if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
			goto out;
995 996
		if (!reply_q->reply_post_host_index)
			rpf = reply_q->reply_post_free;
997 998
		else
			rpf++;
E
Eric Moore 已提交
999 1000 1001 1002
	} while (1);

 out:

1003 1004
	if (!completed_cmds) {
		atomic_dec(&reply_q->busy);
E
Eric Moore 已提交
1005
		return IRQ_NONE;
1006
	}
E
Eric Moore 已提交
1007
	wmb();
1008 1009 1010 1011 1012 1013 1014 1015 1016
	if (ioc->is_warpdrive) {
		writel(reply_q->reply_post_host_index,
		ioc->reply_post_host_index[msix_index]);
		atomic_dec(&reply_q->busy);
		return IRQ_HANDLED;
	}
	writel(reply_q->reply_post_host_index | (msix_index <<
	    MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
	atomic_dec(&reply_q->busy);
E
Eric Moore 已提交
1017 1018 1019
	return IRQ_HANDLED;
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
/**
 * _base_is_controller_msix_enabled - is controller support muli-reply queues
 * @ioc: per adapter object
 *
 */
static inline int
_base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
{
	return (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
}

/**
 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
 * @ioc: per adapter object
 * Context: ISR conext
 *
 * Called when a Task Management request has completed. We want
 * to flush the other reply queues so all the outstanding IO has been
 * completed back to OS before we process the TM completetion.
 *
 * Return nothing.
 */
void
mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
{
	struct adapter_reply_queue *reply_q;

	/* If MSIX capability is turned off
	 * then multi-queues are not enabled
	 */
	if (!_base_is_controller_msix_enabled(ioc))
		return;

	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
		if (ioc->shost_recovery)
			return;
		/* TMs are on msix_index == 0 */
		if (reply_q->msix_index == 0)
			continue;
		_base_interrupt(reply_q->vector, (void *)reply_q);
	}
}

E
Eric Moore 已提交
1064
/**
L
Lucas De Marchi 已提交
1065
 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
E
Eric Moore 已提交
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
 * @cb_idx: callback index
 *
 * Return nothing.
 */
void
mpt2sas_base_release_callback_handler(u8 cb_idx)
{
	mpt_callbacks[cb_idx] = NULL;
}

/**
 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
 * @cb_func: callback function
 *
 * Returns cb_func.
 */
u8
mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
{
	u8 cb_idx;

	for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
		if (mpt_callbacks[cb_idx] == NULL)
			break;

	mpt_callbacks[cb_idx] = cb_func;
	return cb_idx;
}

/**
 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
 *
 * Return nothing.
 */
void
mpt2sas_base_initialize_callback_handler(void)
{
	u8 cb_idx;

	for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
		mpt2sas_base_release_callback_handler(cb_idx);
}

/**
 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
 * @ioc: per adapter object
 * @paddr: virtual address for SGE
 *
 * Create a zero length scatter gather entry to insure the IOCs hardware has
 * something to use if the target device goes brain dead and tries
 * to send data even when none is asked for.
 *
 * Return nothing.
 */
void
mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
{
	u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
	    MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
	    MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
	    MPI2_SGE_FLAGS_SHIFT);
	ioc->base_add_sg_single(paddr, flags_length, -1);
}

/**
 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
 * @paddr: virtual address for SGE
 * @flags_length: SGE flags and data transfer length
 * @dma_addr: Physical address
 *
 * Return nothing.
 */
static void
_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
{
	Mpi2SGESimple32_t *sgel = paddr;

	flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
	sgel->FlagsLength = cpu_to_le32(flags_length);
	sgel->Address = cpu_to_le32(dma_addr);
}


/**
 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
 * @paddr: virtual address for SGE
 * @flags_length: SGE flags and data transfer length
 * @dma_addr: Physical address
 *
 * Return nothing.
 */
static void
_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
{
	Mpi2SGESimple64_t *sgel = paddr;

	flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
	    MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
	sgel->FlagsLength = cpu_to_le32(flags_length);
	sgel->Address = cpu_to_le64(dma_addr);
}

#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))

/**
 * _base_config_dma_addressing - set dma addressing
 * @ioc: per adapter object
 * @pdev: PCI device struct
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
{
	struct sysinfo s;
	char *desc = NULL;

	if (sizeof(dma_addr_t) > 4) {
		const uint64_t required_mask =
		    dma_get_required_mask(&pdev->dev);
1187 1188 1189
		if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
		    DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
		    DMA_BIT_MASK(64))) {
E
Eric Moore 已提交
1190 1191 1192 1193 1194 1195 1196
			ioc->base_add_sg_single = &_base_add_sg_single_64;
			ioc->sge_size = sizeof(Mpi2SGESimple64_t);
			desc = "64";
			goto out;
		}
	}

1197 1198
	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
	    && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
E
Eric Moore 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
		ioc->base_add_sg_single = &_base_add_sg_single_32;
		ioc->sge_size = sizeof(Mpi2SGESimple32_t);
		desc = "32";
	} else
		return -ENODEV;

 out:
	si_meminfo(&s);
	printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
	    "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));

	return 0;
}

/**
1214
 * _base_check_enable_msix - checks MSIX capabable.
E
Eric Moore 已提交
1215 1216
 * @ioc: per adapter object
 *
1217 1218
 * Check to see if card is capable of MSIX, and set number
 * of available msix vectors
E
Eric Moore 已提交
1219
 */
1220 1221
static int
_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
E
Eric Moore 已提交
1222
{
1223 1224
	int base;
	u16 message_control;
E
Eric Moore 已提交
1225 1226


1227 1228 1229 1230 1231 1232 1233
	/* Check whether controller SAS2008 B0 controller,
	   if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
	if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
	    ioc->pdev->revision == 0x01) {
		return -EINVAL;
	}

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
	if (!base) {
		dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
		    "supported\n", ioc->name));
		return -EINVAL;
	}

	/* get msix vector count */
	/* NUMA_IO not supported for older controllers */
	if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
	    ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
		ioc->msix_vector_count = 1;
	else {
		pci_read_config_word(ioc->pdev, base + 2, &message_control);
		ioc->msix_vector_count = (message_control & 0x3FF) + 1;
	}
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
	    "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));

	return 0;
E
Eric Moore 已提交
1259 1260 1261
}

/**
1262
 * _base_free_irq - free irq
E
Eric Moore 已提交
1263 1264
 * @ioc: per adapter object
 *
1265
 * Freeing respective reply_queue from the list.
E
Eric Moore 已提交
1266 1267
 */
static void
1268
_base_free_irq(struct MPT2SAS_ADAPTER *ioc)
E
Eric Moore 已提交
1269
{
1270
	struct adapter_reply_queue *reply_q, *next;
E
Eric Moore 已提交
1271

1272
	if (list_empty(&ioc->reply_queue_list))
E
Eric Moore 已提交
1273 1274
		return;

1275 1276 1277 1278 1279 1280
	list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
		list_del(&reply_q->list);
		synchronize_irq(reply_q->vector);
		free_irq(reply_q->vector, reply_q);
		kfree(reply_q);
	}
E
Eric Moore 已提交
1281 1282 1283
}

/**
1284
 * _base_request_irq - request irq
E
Eric Moore 已提交
1285
 * @ioc: per adapter object
1286 1287
 * @index: msix index into vector table
 * @vector: irq vector
E
Eric Moore 已提交
1288
 *
1289
 * Inserting respective reply_queue into the list.
E
Eric Moore 已提交
1290 1291
 */
static int
1292
_base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
E
Eric Moore 已提交
1293
{
1294 1295
	struct adapter_reply_queue *reply_q;
	int r;
E
Eric Moore 已提交
1296

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
	reply_q =  kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
	if (!reply_q) {
		printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
		    ioc->name, (int)sizeof(struct adapter_reply_queue));
		return -ENOMEM;
	}
	reply_q->ioc = ioc;
	reply_q->msix_index = index;
	reply_q->vector = vector;
	atomic_set(&reply_q->busy, 0);
	if (ioc->msix_enable)
		snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
		    MPT2SAS_DRIVER_NAME, ioc->id, index);
	else
		snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
		    MPT2SAS_DRIVER_NAME, ioc->id);
	r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
	    reply_q);
	if (r) {
		printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
		    reply_q->name, vector);
		kfree(reply_q);
		return -EBUSY;
E
Eric Moore 已提交
1320 1321
	}

1322 1323 1324 1325
	INIT_LIST_HEAD(&reply_q->list);
	list_add_tail(&reply_q->list, &ioc->reply_queue_list);
	return 0;
}
E
Eric Moore 已提交
1326

1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
/**
 * _base_assign_reply_queues - assigning msix index for each cpu
 * @ioc: per adapter object
 *
 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
 *
 * It would nice if we could call irq_set_affinity, however it is not
 * an exported symbol
 */
static void
_base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
{
1339
	unsigned int cpu, nr_cpus, nr_msix, index = 0;
E
Eric Moore 已提交
1340

1341 1342 1343 1344
	if (!_base_is_controller_msix_enabled(ioc))
		return;

	memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362

	nr_cpus = num_online_cpus();
	nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
					       ioc->facts.MaxMSIxVectors);
	if (!nr_msix)
		return;

	cpu = cpumask_first(cpu_online_mask);

	do {
		unsigned int i, group = nr_cpus / nr_msix;

		if (index < nr_cpus % nr_msix)
			group++;

		for (i = 0 ; i < group ; i++) {
			ioc->cpu_msix_table[cpu] = index;
			cpu = cpumask_next(cpu, cpu_online_mask);
1363
		}
1364 1365 1366 1367

		index++;

	} while (cpu < nr_cpus);
E
Eric Moore 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
}

/**
 * _base_disable_msix - disables msix
 * @ioc: per adapter object
 *
 */
static void
_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
{
	if (ioc->msix_enable) {
		pci_disable_msix(ioc->pdev);
		ioc->msix_enable = 0;
	}
}

/**
 * _base_enable_msix - enables msix, failback to io_apic
 * @ioc: per adapter object
 *
 */
static int
_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
{
1392
	struct msix_entry *entries, *a;
E
Eric Moore 已提交
1393
	int r;
1394
	int i;
E
Eric Moore 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
	u8 try_msix = 0;

	if (msix_disable == -1 || msix_disable == 0)
		try_msix = 1;

	if (!try_msix)
		goto try_ioapic;

	if (_base_check_enable_msix(ioc) != 0)
		goto try_ioapic;

1406
	ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1407 1408
	    ioc->msix_vector_count);

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	if (max_msix_vectors > 0) {
		ioc->reply_queue_count = min_t(int, max_msix_vectors,
		    ioc->reply_queue_count);
		ioc->msix_vector_count = ioc->reply_queue_count;
	}

	printk(MPT2SAS_INFO_FMT
	"MSI-X vectors supported: %d, no of cores: %d, max_msix_vectors: %d\n",
	 ioc->name, ioc->msix_vector_count, ioc->cpu_count, max_msix_vectors);

1419 1420 1421 1422 1423 1424
	entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
	    GFP_KERNEL);
	if (!entries) {
		dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
		    "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
		    __LINE__, __func__));
E
Eric Moore 已提交
1425 1426 1427
		goto try_ioapic;
	}

1428 1429 1430
	for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
		a->entry = i;

1431
	r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
E
Eric Moore 已提交
1432
	if (r) {
1433 1434
		dfailprintk(ioc, printk(MPT2SAS_INFO_FMT
		    "pci_enable_msix_exact failed (r=%d) !!!\n", ioc->name, r));
1435
		kfree(entries);
E
Eric Moore 已提交
1436 1437 1438
		goto try_ioapic;
	}

1439 1440 1441 1442 1443 1444 1445 1446 1447
	ioc->msix_enable = 1;
	for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
		r = _base_request_irq(ioc, i, a->vector);
		if (r) {
			_base_free_irq(ioc);
			_base_disable_msix(ioc);
			kfree(entries);
			goto try_ioapic;
		}
E
Eric Moore 已提交
1448 1449
	}

1450
	kfree(entries);
E
Eric Moore 已提交
1451 1452 1453 1454 1455
	return 0;

/* failback to io_apic interrupt routing */
 try_ioapic:

1456
	r = _base_request_irq(ioc, 0, ioc->pdev->irq);
E
Eric Moore 已提交
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473

	return r;
}

/**
 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
 * @ioc: per adapter object
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
{
	struct pci_dev *pdev = ioc->pdev;
	u32 memap_sz;
	u32 pio_sz;
	int i, r = 0;
1474 1475
	u64 pio_chip = 0;
	u64 chip_phys = 0;
1476
	struct adapter_reply_queue *reply_q;
E
Eric Moore 已提交
1477

1478
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
E
Eric Moore 已提交
1479 1480 1481 1482 1483 1484
	    ioc->name, __func__));

	ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
	if (pci_enable_device_mem(pdev)) {
		printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
		    "failed\n", ioc->name);
1485
		ioc->bars = 0;
E
Eric Moore 已提交
1486 1487 1488 1489 1490 1491 1492 1493
		return -ENODEV;
	}


	if (pci_request_selected_regions(pdev, ioc->bars,
	    MPT2SAS_DRIVER_NAME)) {
		printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
		    "failed\n", ioc->name);
1494
		ioc->bars = 0;
E
Eric Moore 已提交
1495 1496 1497 1498
		r = -ENODEV;
		goto out_fail;
	}

1499 1500 1501
	/* AER (Advanced Error Reporting) hooks */
	pci_enable_pcie_error_reporting(pdev);

E
Eric Moore 已提交
1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
	pci_set_master(pdev);

	if (_base_config_dma_addressing(ioc, pdev) != 0) {
		printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
		    ioc->name, pci_name(pdev));
		r = -ENODEV;
		goto out_fail;
	}

	for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1512
		if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
E
Eric Moore 已提交
1513 1514
			if (pio_sz)
				continue;
1515
			pio_chip = (u64)pci_resource_start(pdev, i);
E
Eric Moore 已提交
1516 1517 1518 1519
			pio_sz = pci_resource_len(pdev, i);
		} else {
			if (memap_sz)
				continue;
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
			/* verify memory resource is valid before using */
			if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
				ioc->chip_phys = pci_resource_start(pdev, i);
				chip_phys = (u64)ioc->chip_phys;
				memap_sz = pci_resource_len(pdev, i);
				ioc->chip = ioremap(ioc->chip_phys, memap_sz);
				if (ioc->chip == NULL) {
					printk(MPT2SAS_ERR_FMT "unable to map "
					    "adapter memory!\n", ioc->name);
					r = -EINVAL;
					goto out_fail;
				}
E
Eric Moore 已提交
1532 1533 1534 1535 1536 1537 1538 1539 1540
			}
		}
	}

	_base_mask_interrupts(ioc);
	r = _base_enable_msix(ioc);
	if (r)
		goto out_fail;

1541 1542 1543 1544 1545
	list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
		printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
		    reply_q->name,  ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
		    "IO-APIC enabled"), reply_q->vector);

1546 1547 1548 1549
	printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
	    ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
	printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
	    ioc->name, (unsigned long long)pio_chip, pio_sz);
E
Eric Moore 已提交
1550

1551 1552 1553
	/* Save PCI configuration state for recovery from PCI AER/EEH errors */
	pci_save_state(pdev);

E
Eric Moore 已提交
1554 1555 1556 1557 1558 1559 1560
	return 0;

 out_fail:
	if (ioc->chip_phys)
		iounmap(ioc->chip);
	ioc->chip_phys = 0;
	pci_release_selected_regions(ioc->pdev, ioc->bars);
1561
	pci_disable_pcie_error_reporting(pdev);
E
Eric Moore 已提交
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	pci_disable_device(pdev);
	return r;
}

/**
 * mpt2sas_base_get_msg_frame - obtain request mf pointer
 * @ioc: per adapter object
 * @smid: system request message index(smid zero is invalid)
 *
 * Returns virt pointer to message frame.
 */
void *
mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
{
	return (void *)(ioc->request + (smid * ioc->request_sz));
}

/**
 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Returns virt pointer to sense buffer.
 */
void *
mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
{
	return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
}

/**
 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
 * @ioc: per adapter object
 * @smid: system request message index
 *
1597
 * Returns phys pointer to the low 32bit address of the sense buffer.
E
Eric Moore 已提交
1598
 */
1599
__le32
E
Eric Moore 已提交
1600 1601
mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
{
1602 1603
	return cpu_to_le32(ioc->sense_dma +
			((smid - 1) * SCSI_SENSE_BUFFERSIZE));
E
Eric Moore 已提交
1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621
}

/**
 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
 * @ioc: per adapter object
 * @phys_addr: lower 32 physical addr of the reply
 *
 * Converts 32bit lower physical addr into a virt address.
 */
void *
mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
{
	if (!phys_addr)
		return NULL;
	return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
}

/**
1622
 * mpt2sas_base_get_smid - obtain a free smid from internal queue
E
Eric Moore 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
 * @ioc: per adapter object
 * @cb_idx: callback index
 *
 * Returns smid (zero is invalid)
 */
u16
mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
{
	unsigned long flags;
	struct request_tracker *request;
	u16 smid;

1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	if (list_empty(&ioc->internal_free_list)) {
		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
		printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
		    ioc->name, __func__);
		return 0;
	}

	request = list_entry(ioc->internal_free_list.next,
	    struct request_tracker, tracker_list);
	request->cb_idx = cb_idx;
	smid = request->smid;
	list_del(&request->tracker_list);
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
	return smid;
}

/**
 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
 * @ioc: per adapter object
 * @cb_idx: callback index
 * @scmd: pointer to scsi command object
 *
 * Returns smid (zero is invalid)
 */
u16
mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
    struct scsi_cmnd *scmd)
{
	unsigned long flags;
1665
	struct scsiio_tracker *request;
1666 1667
	u16 smid;

E
Eric Moore 已提交
1668 1669 1670 1671 1672 1673 1674 1675 1676
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	if (list_empty(&ioc->free_list)) {
		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
		printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
		    ioc->name, __func__);
		return 0;
	}

	request = list_entry(ioc->free_list.next,
1677
	    struct scsiio_tracker, tracker_list);
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
	request->scmd = scmd;
	request->cb_idx = cb_idx;
	smid = request->smid;
	list_del(&request->tracker_list);
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
	return smid;
}

/**
 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
 * @ioc: per adapter object
 * @cb_idx: callback index
 *
 * Returns smid (zero is invalid)
 */
u16
mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
{
	unsigned long flags;
	struct request_tracker *request;
	u16 smid;

	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	if (list_empty(&ioc->hpr_free_list)) {
		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
		return 0;
	}

	request = list_entry(ioc->hpr_free_list.next,
	    struct request_tracker, tracker_list);
E
Eric Moore 已提交
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
	request->cb_idx = cb_idx;
	smid = request->smid;
	list_del(&request->tracker_list);
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
	return smid;
}


/**
 * mpt2sas_base_free_smid - put smid back on free_list
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return nothing.
 */
void
mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
{
	unsigned long flags;
1727
	int i;
1728
	struct chain_tracker *chain_req, *next;
E
Eric Moore 已提交
1729 1730

	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1731 1732 1733 1734 1735 1736 1737
	if (smid < ioc->hi_priority_smid) {
		/* scsiio queue */
		i = smid - 1;
		if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
			list_for_each_entry_safe(chain_req, next,
			    &ioc->scsi_lookup[i].chain_list, tracker_list) {
				list_del_init(&chain_req->tracker_list);
1738
				list_add(&chain_req->tracker_list,
1739 1740
				    &ioc->free_chain_list);
			}
1741
		}
1742 1743
		ioc->scsi_lookup[i].cb_idx = 0xFF;
		ioc->scsi_lookup[i].scmd = NULL;
1744
		ioc->scsi_lookup[i].direct_io = 0;
1745
		list_add(&ioc->scsi_lookup[i].tracker_list,
1746
		    &ioc->free_list);
1747 1748
		spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

1749 1750 1751 1752 1753 1754 1755 1756
		/*
		 * See _wait_for_commands_to_complete() call with regards
		 * to this code.
		 */
		if (ioc->shost_recovery && ioc->pending_io_count) {
			if (ioc->pending_io_count == 1)
				wake_up(&ioc->reset_wq);
			ioc->pending_io_count--;
1757
		}
1758 1759 1760 1761 1762
		return;
	} else if (smid < ioc->internal_smid) {
		/* hi-priority */
		i = smid - ioc->hi_priority_smid;
		ioc->hpr_lookup[i].cb_idx = 0xFF;
1763
		list_add(&ioc->hpr_lookup[i].tracker_list,
1764 1765 1766 1767 1768
		    &ioc->hpr_free_list);
	} else if (smid <= ioc->hba_queue_depth) {
		/* internal queue */
		i = smid - ioc->internal_smid;
		ioc->internal_lookup[i].cb_idx = 0xFF;
1769
		list_add(&ioc->internal_lookup[i].tracker_list,
1770
		    &ioc->internal_free_list);
1771
	}
E
Eric Moore 已提交
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
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
}

/**
 * _base_writeq - 64 bit write to MMIO
 * @ioc: per adapter object
 * @b: data payload
 * @addr: address in MMIO space
 * @writeq_lock: spin lock
 *
 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
 * care of 32 bit environment where its not quarenteed to send the entire word
 * in one transfer.
 */
#ifndef writeq
static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
    spinlock_t *writeq_lock)
{
	unsigned long flags;
	__u64 data_out = cpu_to_le64(b);

	spin_lock_irqsave(writeq_lock, flags);
	writel((u32)(data_out), addr);
	writel((u32)(data_out >> 32), (addr + 4));
	spin_unlock_irqrestore(writeq_lock, flags);
}
#else
static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
    spinlock_t *writeq_lock)
{
	writeq(cpu_to_le64(b), addr);
}
#endif

1806 1807 1808
static inline u8
_base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
{
1809
	return ioc->cpu_msix_table[raw_smp_processor_id()];
1810 1811
}

E
Eric Moore 已提交
1812 1813 1814 1815 1816 1817 1818 1819 1820
/**
 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
 * @ioc: per adapter object
 * @smid: system request message index
 * @handle: device handle
 *
 * Return nothing.
 */
void
1821
mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
E
Eric Moore 已提交
1822 1823 1824 1825 1826 1827
{
	Mpi2RequestDescriptorUnion_t descriptor;
	u64 *request = (u64 *)&descriptor;


	descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1828
	descriptor.SCSIIO.MSIxIndex =  _base_get_msix_index(ioc);
E
Eric Moore 已提交
1829 1830 1831 1832 1833 1834 1835 1836 1837
	descriptor.SCSIIO.SMID = cpu_to_le16(smid);
	descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
	descriptor.SCSIIO.LMID = 0;
	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
	    &ioc->scsi_lookup_lock);
}


/**
L
Lucas De Marchi 已提交
1838
 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
E
Eric Moore 已提交
1839 1840 1841 1842 1843 1844
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return nothing.
 */
void
1845
mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
E
Eric Moore 已提交
1846 1847 1848 1849 1850 1851
{
	Mpi2RequestDescriptorUnion_t descriptor;
	u64 *request = (u64 *)&descriptor;

	descriptor.HighPriority.RequestFlags =
	    MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1852
	descriptor.HighPriority.MSIxIndex =  0;
E
Eric Moore 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
	descriptor.HighPriority.SMID = cpu_to_le16(smid);
	descriptor.HighPriority.LMID = 0;
	descriptor.HighPriority.Reserved1 = 0;
	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
	    &ioc->scsi_lookup_lock);
}

/**
 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return nothing.
 */
void
1868
mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
E
Eric Moore 已提交
1869 1870 1871 1872 1873
{
	Mpi2RequestDescriptorUnion_t descriptor;
	u64 *request = (u64 *)&descriptor;

	descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
1874
	descriptor.Default.MSIxIndex =  _base_get_msix_index(ioc);
E
Eric Moore 已提交
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	descriptor.Default.SMID = cpu_to_le16(smid);
	descriptor.Default.LMID = 0;
	descriptor.Default.DescriptorTypeDependent = 0;
	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
	    &ioc->scsi_lookup_lock);
}

/**
 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
 * @ioc: per adapter object
 * @smid: system request message index
 * @io_index: value used to track the IO
 *
 * Return nothing.
 */
void
mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
1892
    u16 io_index)
E
Eric Moore 已提交
1893 1894 1895 1896 1897 1898
{
	Mpi2RequestDescriptorUnion_t descriptor;
	u64 *request = (u64 *)&descriptor;

	descriptor.SCSITarget.RequestFlags =
	    MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
1899
	descriptor.SCSITarget.MSIxIndex =  _base_get_msix_index(ioc);
E
Eric Moore 已提交
1900 1901 1902 1903 1904 1905 1906
	descriptor.SCSITarget.SMID = cpu_to_le16(smid);
	descriptor.SCSITarget.LMID = 0;
	descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
	_base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
	    &ioc->scsi_lookup_lock);
}

1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
/**
 * _base_display_dell_branding - Disply branding string
 * @ioc: per adapter object
 *
 * Return nothing.
 */
static void
_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
{
	char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];

	if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
		return;

	memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
	switch (ioc->pdev->subsystem_device) {
	case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
		strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
		strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
		strncpy(dell_branding,
		    MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
		strncpy(dell_branding,
		    MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
		strncpy(dell_branding,
		    MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_PERC_H200_SSDID:
		strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	case MPT2SAS_DELL_6GBPS_SAS_SSDID:
		strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
		    MPT2SAS_DELL_BRANDING_SIZE - 1);
		break;
	default:
		sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
		break;
	}

	printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
	    " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
	    ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
	    ioc->pdev->subsystem_device);
}

1965 1966 1967 1968 1969 1970 1971 1972 1973
/**
 * _base_display_intel_branding - Display branding string
 * @ioc: per adapter object
 *
 * Return nothing.
 */
static void
_base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
{
1974 1975
	if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
		return;
1976

1977 1978
	switch (ioc->pdev->device) {
	case MPI2_MFGPAGE_DEVID_SAS2008:
1979 1980 1981 1982 1983 1984 1985 1986 1987
		switch (ioc->pdev->subsystem_device) {
		case MPT2SAS_INTEL_RMS2LL080_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS2LL080_BRANDING);
			break;
		case MPT2SAS_INTEL_RMS2LL040_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS2LL040_BRANDING);
			break;
1988
		case MPT2SAS_INTEL_SSD910_SSDID:
1989
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1990
			    MPT2SAS_INTEL_SSD910_BRANDING);
1991
			break;
1992 1993 1994 1995 1996 1997 1998 1999 2000
		default:
			break;
		}
	case MPI2_MFGPAGE_DEVID_SAS2308_2:
		switch (ioc->pdev->subsystem_device) {
		case MPT2SAS_INTEL_RS25GB008_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RS25GB008_BRANDING);
			break;
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
		case MPT2SAS_INTEL_RMS25JB080_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25JB080_BRANDING);
			break;
		case MPT2SAS_INTEL_RMS25JB040_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25JB040_BRANDING);
			break;
		case MPT2SAS_INTEL_RMS25KB080_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25KB080_BRANDING);
			break;
		case MPT2SAS_INTEL_RMS25KB040_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25KB040_BRANDING);
			break;
2017 2018 2019 2020 2021 2022 2023 2024
		case MPT2SAS_INTEL_RMS25LB040_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25LB040_BRANDING);
			break;
		case MPT2SAS_INTEL_RMS25LB080_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_INTEL_RMS25LB080_BRANDING);
			break;
2025 2026
		default:
			break;
2027
		}
2028 2029
	default:
		break;
2030 2031 2032
	}
}

2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080
/**
 * _base_display_hp_branding - Display branding string
 * @ioc: per adapter object
 *
 * Return nothing.
 */
static void
_base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
{
	if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
		return;

	switch (ioc->pdev->device) {
	case MPI2_MFGPAGE_DEVID_SAS2004:
		switch (ioc->pdev->subsystem_device) {
		case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
			break;
		default:
			break;
		}
	case MPI2_MFGPAGE_DEVID_SAS2308_2:
		switch (ioc->pdev->subsystem_device) {
		case MPT2SAS_HP_2_4_INTERNAL_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_HP_2_4_INTERNAL_BRANDING);
			break;
		case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
			break;
		case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
			break;
		case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
			printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
			    MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
			break;
		default:
			break;
		}
	default:
		break;
	}
}

E
Eric Moore 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
/**
 * _base_display_ioc_capabilities - Disply IOC's capabilities.
 * @ioc: per adapter object
 *
 * Return nothing.
 */
static void
_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
{
	int i = 0;
	char desc[16];
	u32 iounit_pg1_flags;
2093
	u32 bios_version;
E
Eric Moore 已提交
2094

2095
	bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
E
Eric Moore 已提交
2096 2097 2098 2099 2100 2101 2102 2103
	strncpy(desc, ioc->manu_pg0.ChipName, 16);
	printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
	   "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
	    ioc->name, desc,
	   (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
	   (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
	   (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
	   ioc->facts.FWVersion.Word & 0x000000FF,
2104
	   ioc->pdev->revision,
2105 2106 2107 2108
	   (bios_version & 0xFF000000) >> 24,
	   (bios_version & 0x00FF0000) >> 16,
	   (bios_version & 0x0000FF00) >> 8,
	    bios_version & 0x000000FF);
E
Eric Moore 已提交
2109

2110
	_base_display_dell_branding(ioc);
2111
	_base_display_intel_branding(ioc);
2112
	_base_display_hp_branding(ioc);
2113

E
Eric Moore 已提交
2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129
	printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);

	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
		printk("Initiator");
		i++;
	}

	if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
		printk("%sTarget", i ? "," : "");
		i++;
	}

	i = 0;
	printk("), ");
	printk("Capabilities=(");

2130 2131 2132 2133 2134 2135
	if (!ioc->hide_ir_msg) {
		if (ioc->facts.IOCCapabilities &
		    MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
			printk("Raid");
			i++;
		}
E
Eric Moore 已提交
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
	}

	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
		printk("%sTLR", i ? "," : "");
		i++;
	}

	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
		printk("%sMulticast", i ? "," : "");
		i++;
	}

	if (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
		printk("%sBIDI Target", i ? "," : "");
		i++;
	}

	if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
		printk("%sEEDP", i ? "," : "");
		i++;
	}

	if (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
		printk("%sSnapshot Buffer", i ? "," : "");
		i++;
	}

	if (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
		printk("%sDiag Trace Buffer", i ? "," : "");
		i++;
	}

2171 2172 2173 2174 2175 2176
	if (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
		printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
		i++;
	}

E
Eric Moore 已提交
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191
	if (ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
		printk("%sTask Set Full", i ? "," : "");
		i++;
	}

	iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
	if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
		printk("%sNCQ", i ? "," : "");
		i++;
	}

	printk(")\n");
}

2192
/**
2193
 * mpt2sas_base_update_missing_delay - change the missing delay timers
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203
 * @ioc: per adapter object
 * @device_missing_delay: amount of time till device is reported missing
 * @io_missing_delay: interval IO is returned when there is a missing device
 *
 * Return nothing.
 *
 * Passed on the command line, this function will modify the device missing
 * delay, as well as the io missing delay. This should be called at driver
 * load time.
 */
2204 2205
void
mpt2sas_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282
	u16 device_missing_delay, u8 io_missing_delay)
{
	u16 dmd, dmd_new, dmd_orignal;
	u8 io_missing_delay_original;
	u16 sz;
	Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
	Mpi2ConfigReply_t mpi_reply;
	u8 num_phys = 0;
	u16 ioc_status;

	mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
	if (!num_phys)
		return;

	sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
	    sizeof(Mpi2SasIOUnit1PhyData_t));
	sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
	if (!sas_iounit_pg1) {
		printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
		    ioc->name, __FILE__, __LINE__, __func__);
		goto out;
	}
	if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
	    sas_iounit_pg1, sz))) {
		printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
		    ioc->name, __FILE__, __LINE__, __func__);
		goto out;
	}
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
	    MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
		printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
		    ioc->name, __FILE__, __LINE__, __func__);
		goto out;
	}

	/* device missing delay */
	dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
	if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
		dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
	else
		dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
	dmd_orignal = dmd;
	if (device_missing_delay > 0x7F) {
		dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
		    device_missing_delay;
		dmd = dmd / 16;
		dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
	} else
		dmd = device_missing_delay;
	sas_iounit_pg1->ReportDeviceMissingDelay = dmd;

	/* io missing delay */
	io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
	sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;

	if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
	    sz)) {
		if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
			dmd_new = (dmd &
			    MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
		else
			dmd_new =
		    dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
		printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
		    "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
		printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
		    "new(%d)\n", ioc->name, io_missing_delay_original,
		    io_missing_delay);
		ioc->device_missing_delay = dmd_new;
		ioc->io_missing_delay = io_missing_delay;
	}

out:
	kfree(sas_iounit_pg1);
}

E
Eric Moore 已提交
2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
/**
 * _base_static_config_pages - static start of day config pages
 * @ioc: per adapter object
 *
 * Return nothing.
 */
static void
_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
{
	Mpi2ConfigReply_t mpi_reply;
	u32 iounit_pg1_flags;

	mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2296 2297 2298
	if (ioc->ir_firmware)
		mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
		    &ioc->manu_pg10);
E
Eric Moore 已提交
2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318
	mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
	mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
	mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
	mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
	mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
	_base_display_ioc_capabilities(ioc);

	/*
	 * Enable task_set_full handling in iounit_pg1 when the
	 * facts capabilities indicate that its supported.
	 */
	iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
	if ((ioc->facts.IOCCapabilities &
	    MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
		iounit_pg1_flags &=
		    ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
	else
		iounit_pg1_flags |=
		    MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
	ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2319
	mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2320

E
Eric Moore 已提交
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
}

/**
 * _base_release_memory_pools - release memory
 * @ioc: per adapter object
 *
 * Free memory allocated from _base_allocate_memory_pools.
 *
 * Return nothing.
 */
static void
_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
{
2334 2335
	int i;

2336
	dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393
	    __func__));

	if (ioc->request) {
		pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
		    ioc->request,  ioc->request_dma);
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
		    ": free\n", ioc->name, ioc->request));
		ioc->request = NULL;
	}

	if (ioc->sense) {
		pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
		if (ioc->sense_dma_pool)
			pci_pool_destroy(ioc->sense_dma_pool);
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
		    ": free\n", ioc->name, ioc->sense));
		ioc->sense = NULL;
	}

	if (ioc->reply) {
		pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
		if (ioc->reply_dma_pool)
			pci_pool_destroy(ioc->reply_dma_pool);
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
		     ": free\n", ioc->name, ioc->reply));
		ioc->reply = NULL;
	}

	if (ioc->reply_free) {
		pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
		    ioc->reply_free_dma);
		if (ioc->reply_free_dma_pool)
			pci_pool_destroy(ioc->reply_free_dma_pool);
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
		    "(0x%p): free\n", ioc->name, ioc->reply_free));
		ioc->reply_free = NULL;
	}

	if (ioc->reply_post_free) {
		pci_pool_free(ioc->reply_post_free_dma_pool,
		    ioc->reply_post_free, ioc->reply_post_free_dma);
		if (ioc->reply_post_free_dma_pool)
			pci_pool_destroy(ioc->reply_post_free_dma_pool);
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
		    "reply_post_free_pool(0x%p): free\n", ioc->name,
		    ioc->reply_post_free));
		ioc->reply_post_free = NULL;
	}

	if (ioc->config_page) {
		dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
		    "config_page(0x%p): free\n", ioc->name,
		    ioc->config_page));
		pci_free_consistent(ioc->pdev, ioc->config_page_sz,
		    ioc->config_page, ioc->config_page_dma);
	}

2394 2395 2396 2397
	if (ioc->scsi_lookup) {
		free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
		ioc->scsi_lookup = NULL;
	}
2398 2399
	kfree(ioc->hpr_lookup);
	kfree(ioc->internal_lookup);
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411
	if (ioc->chain_lookup) {
		for (i = 0; i < ioc->chain_depth; i++) {
			if (ioc->chain_lookup[i].chain_buffer)
				pci_pool_free(ioc->chain_dma_pool,
				    ioc->chain_lookup[i].chain_buffer,
				    ioc->chain_lookup[i].chain_buffer_dma);
		}
		if (ioc->chain_dma_pool)
			pci_pool_destroy(ioc->chain_dma_pool);
		free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
		ioc->chain_lookup = NULL;
	}
E
Eric Moore 已提交
2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
}


/**
 * _base_allocate_memory_pools - allocate start of day memory pools
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 success, anything else error
 */
static int
_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc,  int sleep_flag)
{
2425
	struct mpt2sas_facts *facts;
E
Eric Moore 已提交
2426 2427
	u16 max_sge_elements;
	u16 chains_needed_per_io;
2428
	u32 sz, total_sz, reply_post_free_sz;
E
Eric Moore 已提交
2429 2430
	u32 retry_sz;
	u16 max_request_credit;
2431
	int i;
E
Eric Moore 已提交
2432

2433
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448
	    __func__));

	retry_sz = 0;
	facts = &ioc->facts;

	/* command line tunables  for max sgl entries */
	if (max_sgl_entries != -1) {
		ioc->shost->sg_tablesize = (max_sgl_entries <
		    MPT2SAS_SG_DEPTH) ? max_sgl_entries :
		    MPT2SAS_SG_DEPTH;
	} else {
		ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
	}

	/* command line tunables  for max controller queue depth */
2449 2450 2451 2452 2453 2454 2455
	if (max_queue_depth != -1 && max_queue_depth != 0) {
		max_request_credit = min_t(u16, max_queue_depth +
			ioc->hi_priority_depth + ioc->internal_depth,
			facts->RequestCredit);
		if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
			max_request_credit =  MAX_HBA_QUEUE_DEPTH;
	} else
2456 2457
		max_request_credit = min_t(u16, facts->RequestCredit,
		    MAX_HBA_QUEUE_DEPTH);
2458 2459 2460 2461

	ioc->hba_queue_depth = max_request_credit;
	ioc->hi_priority_depth = facts->HighPriorityCredit;
	ioc->internal_depth = ioc->hi_priority_depth + 5;
E
Eric Moore 已提交
2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497

	/* request frame size */
	ioc->request_sz = facts->IOCRequestFrameSize * 4;

	/* reply frame size */
	ioc->reply_sz = facts->ReplyFrameSize * 4;

 retry_allocation:
	total_sz = 0;
	/* calculate number of sg elements left over in the 1st frame */
	max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
	    sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
	ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;

	/* now do the same for a chain buffer */
	max_sge_elements = ioc->request_sz - ioc->sge_size;
	ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;

	ioc->chain_offset_value_for_main_message =
	    ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
	     (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;

	/*
	 *  MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
	 */
	chains_needed_per_io = ((ioc->shost->sg_tablesize -
	   ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
	    + 1;
	if (chains_needed_per_io > facts->MaxChainDepth) {
		chains_needed_per_io = facts->MaxChainDepth;
		ioc->shost->sg_tablesize = min_t(u16,
		ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
		* chains_needed_per_io), ioc->shost->sg_tablesize);
	}
	ioc->chains_needed_per_io = chains_needed_per_io;

2498 2499
	/* reply free queue sizing - taking into account for 64 FW events */
	ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
E
Eric Moore 已提交
2500

2501 2502 2503
	/* calculate reply descriptor post queue depth */
	ioc->reply_post_queue_depth = ioc->hba_queue_depth +
					ioc->reply_free_queue_depth +  1;
2504
	/* align the reply post queue on the next 16 count boundary */
2505 2506 2507 2508 2509
	if (ioc->reply_post_queue_depth % 16)
		ioc->reply_post_queue_depth += 16 -
			(ioc->reply_post_queue_depth % 16);


2510 2511
	if (ioc->reply_post_queue_depth >
	    facts->MaxReplyDescriptorPostQueueDepth) {
2512 2513 2514 2515 2516 2517
		ioc->reply_post_queue_depth =
			facts->MaxReplyDescriptorPostQueueDepth -
		    (facts->MaxReplyDescriptorPostQueueDepth % 16);
		ioc->hba_queue_depth =
			((ioc->reply_post_queue_depth - 64) / 2) - 1;
		ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
E
Eric Moore 已提交
2518
	}
2519

E
Eric Moore 已提交
2520 2521 2522 2523 2524 2525
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
	    "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
	    "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
	    ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
	    ioc->chains_needed_per_io));

2526 2527 2528 2529 2530 2531
	ioc->scsiio_depth = ioc->hba_queue_depth -
	    ioc->hi_priority_depth - ioc->internal_depth;

	/* set the scsi host can_queue depth
	 * with some internal commands that could be outstanding
	 */
2532
	ioc->shost->can_queue = ioc->scsiio_depth;
2533 2534 2535
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
	    "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));

E
Eric Moore 已提交
2536 2537 2538
	/* contiguous pool for request and chains, 16 byte align, one extra "
	 * "frame for smid=0
	 */
2539
	ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2540
	sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2541 2542 2543 2544 2545 2546

	/* hi-priority queue */
	sz += (ioc->hi_priority_depth * ioc->request_sz);

	/* internal queue */
	sz += (ioc->internal_depth * ioc->request_sz);
E
Eric Moore 已提交
2547 2548 2549 2550 2551

	ioc->request_dma_sz = sz;
	ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
	if (!ioc->request) {
		printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2552 2553
		    "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
		    "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
E
Eric Moore 已提交
2554
		    ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2555
		if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
E
Eric Moore 已提交
2556 2557
			goto out;
		retry_sz += 64;
2558
		ioc->hba_queue_depth = max_request_credit - retry_sz;
E
Eric Moore 已提交
2559 2560 2561 2562 2563
		goto retry_allocation;
	}

	if (retry_sz)
		printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
2564 2565
		    "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
		    "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
E
Eric Moore 已提交
2566 2567
		    ioc->chains_needed_per_io, ioc->request_sz, sz/1024);

2568 2569 2570

	/* hi-priority queue */
	ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
E
Eric Moore 已提交
2571
	    ioc->request_sz);
2572 2573 2574 2575 2576
	ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
	    ioc->request_sz);

	/* internal queue */
	ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
E
Eric Moore 已提交
2577
	    ioc->request_sz);
2578 2579 2580 2581
	ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
	    ioc->request_sz);


E
Eric Moore 已提交
2582 2583
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
	    "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2584 2585
	    ioc->request, ioc->hba_queue_depth, ioc->request_sz,
	    (ioc->hba_queue_depth * ioc->request_sz)/1024));
E
Eric Moore 已提交
2586 2587 2588 2589
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
	    ioc->name, (unsigned long long) ioc->request_dma));
	total_sz += sz;

2590
	sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2591
	ioc->scsi_lookup_pages = get_order(sz);
2592
	ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2593
	    GFP_KERNEL, ioc->scsi_lookup_pages);
E
Eric Moore 已提交
2594
	if (!ioc->scsi_lookup) {
2595 2596
		printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
		    "sz(%d)\n", ioc->name, (int)sz);
E
Eric Moore 已提交
2597 2598 2599
		goto out;
	}

2600 2601 2602 2603
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
	    "depth(%d)\n", ioc->name, ioc->request,
	    ioc->scsiio_depth));

2604 2605 2606 2607 2608 2609
	ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
	sz = ioc->chain_depth * sizeof(struct chain_tracker);
	ioc->chain_pages = get_order(sz);

	ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
	    GFP_KERNEL, ioc->chain_pages);
2610 2611 2612 2613 2614
	if (!ioc->chain_lookup) {
		printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
		    "sz(%d)\n", ioc->name, (int)sz);
		goto out;
	}
2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
	ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
	    ioc->request_sz, 16, 0);
	if (!ioc->chain_dma_pool) {
		printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
		    "failed\n", ioc->name);
		goto out;
	}
	for (i = 0; i < ioc->chain_depth; i++) {
		ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
		    ioc->chain_dma_pool , GFP_KERNEL,
		    &ioc->chain_lookup[i].chain_buffer_dma);
		if (!ioc->chain_lookup[i].chain_buffer) {
			ioc->chain_depth = i;
			goto chain_done;
		}
		total_sz += ioc->request_sz;
	}
chain_done:
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
	    "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
	    ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
	    ioc->request_sz))/1024));

2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
	/* initialize hi-priority queue smid's */
	ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
	    sizeof(struct request_tracker), GFP_KERNEL);
	if (!ioc->hpr_lookup) {
		printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
		    ioc->name);
		goto out;
	}
	ioc->hi_priority_smid = ioc->scsiio_depth + 1;
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
	    "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
	    ioc->hi_priority_depth, ioc->hi_priority_smid));

	/* initialize internal queue smid's */
	ioc->internal_lookup = kcalloc(ioc->internal_depth,
	    sizeof(struct request_tracker), GFP_KERNEL);
	if (!ioc->internal_lookup) {
		printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
		    ioc->name);
		goto out;
	}
	ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
	    "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
	     ioc->internal_depth, ioc->internal_smid));
E
Eric Moore 已提交
2663 2664

	/* sense buffers, 4 byte align */
2665
	sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
E
Eric Moore 已提交
2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681
	ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
	    0);
	if (!ioc->sense_dma_pool) {
		printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
		    ioc->name);
		goto out;
	}
	ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
	    &ioc->sense_dma);
	if (!ioc->sense) {
		printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
		    ioc->name);
		goto out;
	}
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
	    "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2682
	    "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
E
Eric Moore 已提交
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703
	    SCSI_SENSE_BUFFERSIZE, sz/1024));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
	    ioc->name, (unsigned long long)ioc->sense_dma));
	total_sz += sz;

	/* reply pool, 4 byte align */
	sz = ioc->reply_free_queue_depth * ioc->reply_sz;
	ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
	    0);
	if (!ioc->reply_dma_pool) {
		printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
		    ioc->name);
		goto out;
	}
	ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
	    &ioc->reply_dma);
	if (!ioc->reply) {
		printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
		    ioc->name);
		goto out;
	}
2704 2705
	ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
	ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
E
Eric Moore 已提交
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
	    "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
	    ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
	    ioc->name, (unsigned long long)ioc->reply_dma));
	total_sz += sz;

	/* reply free queue, 16 byte align */
	sz = ioc->reply_free_queue_depth * 4;
	ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
	    ioc->pdev, sz, 16, 0);
	if (!ioc->reply_free_dma_pool) {
		printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
		    "failed\n", ioc->name);
		goto out;
	}
	ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
	    &ioc->reply_free_dma);
	if (!ioc->reply_free) {
		printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
		    "failed\n", ioc->name);
		goto out;
	}
	memset(ioc->reply_free, 0, sz);
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
	    "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
	    ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
	    "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
	total_sz += sz;

	/* reply post queue, 16 byte align */
2738 2739 2740 2741 2742 2743
	reply_post_free_sz = ioc->reply_post_queue_depth *
	    sizeof(Mpi2DefaultReplyDescriptor_t);
	if (_base_is_controller_msix_enabled(ioc))
		sz = reply_post_free_sz * ioc->reply_queue_count;
	else
		sz = reply_post_free_sz;
E
Eric Moore 已提交
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869
	ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
	    ioc->pdev, sz, 16, 0);
	if (!ioc->reply_post_free_dma_pool) {
		printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
		    "failed\n", ioc->name);
		goto out;
	}
	ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
	    GFP_KERNEL, &ioc->reply_post_free_dma);
	if (!ioc->reply_post_free) {
		printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
		    "failed\n", ioc->name);
		goto out;
	}
	memset(ioc->reply_post_free, 0, sz);
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
	    "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
	    ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
	    sz/1024));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
	    "(0x%llx)\n", ioc->name, (unsigned long long)
	    ioc->reply_post_free_dma));
	total_sz += sz;

	ioc->config_page_sz = 512;
	ioc->config_page = pci_alloc_consistent(ioc->pdev,
	    ioc->config_page_sz, &ioc->config_page_dma);
	if (!ioc->config_page) {
		printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
		    "failed\n", ioc->name);
		goto out;
	}
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
	    "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
	    "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
	total_sz += ioc->config_page_sz;

	printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
	    ioc->name, total_sz/1024);
	printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
	    "Max Controller Queue Depth(%d)\n",
	    ioc->name, ioc->shost->can_queue, facts->RequestCredit);
	printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
	    ioc->name, ioc->shost->sg_tablesize);
	return 0;

 out:
	return -ENOMEM;
}


/**
 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
 * @ioc: Pointer to MPT_ADAPTER structure
 * @cooked: Request raw or cooked IOC state
 *
 * Returns all IOC Doorbell register bits if cooked==0, else just the
 * Doorbell bits in MPI_IOC_STATE_MASK.
 */
u32
mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
{
	u32 s, sc;

	s = readl(&ioc->chip->Doorbell);
	sc = s & MPI2_IOC_STATE_MASK;
	return cooked ? sc : s;
}

/**
 * _base_wait_on_iocstate - waiting on a particular ioc state
 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
    int sleep_flag)
{
	u32 count, cntdn;
	u32 current_state;

	count = 0;
	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
	do {
		current_state = mpt2sas_base_get_iocstate(ioc, 1);
		if (current_state == ioc_state)
			return 0;
		if (count && current_state == MPI2_IOC_STATE_FAULT)
			break;
		if (sleep_flag == CAN_SLEEP)
			msleep(1);
		else
			udelay(500);
		count++;
	} while (--cntdn);

	return current_state;
}

/**
 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
 * a write to the doorbell)
 * @ioc: per adapter object
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 *
 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
 */
static int
_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
    int sleep_flag)
{
	u32 cntdn, count;
	u32 int_status;

	count = 0;
	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
	do {
		int_status = readl(&ioc->chip->HostInterruptStatus);
		if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2870
			dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
L
Lucas De Marchi 已提交
2871
			    "successful count(%d), timeout(%d)\n", ioc->name,
E
Eric Moore 已提交
2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910
			    __func__, count, timeout));
			return 0;
		}
		if (sleep_flag == CAN_SLEEP)
			msleep(1);
		else
			udelay(500);
		count++;
	} while (--cntdn);

	printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
	    "int_status(%x)!\n", ioc->name, __func__, count, int_status);
	return -EFAULT;
}

/**
 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
 * @ioc: per adapter object
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 *
 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
 * doorbell.
 */
static int
_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
    int sleep_flag)
{
	u32 cntdn, count;
	u32 int_status;
	u32 doorbell;

	count = 0;
	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
	do {
		int_status = readl(&ioc->chip->HostInterruptStatus);
		if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2911
			dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
L
Lucas De Marchi 已提交
2912
			    "successful count(%d), timeout(%d)\n", ioc->name,
E
Eric Moore 已提交
2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
			    __func__, count, timeout));
			return 0;
		} else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
			doorbell = readl(&ioc->chip->Doorbell);
			if ((doorbell & MPI2_IOC_STATE_MASK) ==
			    MPI2_IOC_STATE_FAULT) {
				mpt2sas_base_fault_info(ioc , doorbell);
				return -EFAULT;
			}
		} else if (int_status == 0xFFFFFFFF)
			goto out;

		if (sleep_flag == CAN_SLEEP)
			msleep(1);
		else
			udelay(500);
		count++;
	} while (--cntdn);

 out:
	printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
	    "int_status(%x)!\n", ioc->name, __func__, count, int_status);
	return -EFAULT;
}

/**
 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
 * @ioc: per adapter object
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 *
 */
static int
_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
    int sleep_flag)
{
	u32 cntdn, count;
	u32 doorbell_reg;

	count = 0;
	cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
	do {
		doorbell_reg = readl(&ioc->chip->Doorbell);
		if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2959
			dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
L
Lucas De Marchi 已提交
2960
			    "successful count(%d), timeout(%d)\n", ioc->name,
E
Eric Moore 已提交
2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043
			    __func__, count, timeout));
			return 0;
		}
		if (sleep_flag == CAN_SLEEP)
			msleep(1);
		else
			udelay(500);
		count++;
	} while (--cntdn);

	printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
	    "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
	return -EFAULT;
}

/**
 * _base_send_ioc_reset - send doorbell reset
 * @ioc: per adapter object
 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
    int sleep_flag)
{
	u32 ioc_state;
	int r = 0;

	if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
		printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
		    ioc->name, __func__);
		return -EFAULT;
	}

	if (!(ioc->facts.IOCCapabilities &
	   MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
		return -EFAULT;

	printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);

	writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
	    &ioc->chip->Doorbell);
	if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
		r = -EFAULT;
		goto out;
	}
	ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
	    timeout, sleep_flag);
	if (ioc_state) {
		printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
		    " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
		r = -EFAULT;
		goto out;
	}
 out:
	printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
	    ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
	return r;
}

/**
 * _base_handshake_req_reply_wait - send request thru doorbell interface
 * @ioc: per adapter object
 * @request_bytes: request length
 * @request: pointer having request payload
 * @reply_bytes: reply length
 * @reply: pointer to reply payload
 * @timeout: timeout in second
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
    u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
{
	MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
	int i;
	u8 failed;
	u16 dummy;
3044
	__le32 *mfp;
E
Eric Moore 已提交
3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062

	/* make sure doorbell is not in use */
	if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
		printk(MPT2SAS_ERR_FMT "doorbell is in use "
		    " (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}

	/* clear pending doorbell interrupts from previous state changes */
	if (readl(&ioc->chip->HostInterruptStatus) &
	    MPI2_HIS_IOC2SYS_DB_STATUS)
		writel(0, &ioc->chip->HostInterruptStatus);

	/* send message to ioc */
	writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
	    ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
	    &ioc->chip->Doorbell);

3063
	if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
E
Eric Moore 已提交
3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131
		printk(MPT2SAS_ERR_FMT "doorbell handshake "
		   "int failed (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}
	writel(0, &ioc->chip->HostInterruptStatus);

	if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
		printk(MPT2SAS_ERR_FMT "doorbell handshake "
		    "ack failed (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}

	/* send message 32-bits at a time */
	for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
		writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
		if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
			failed = 1;
	}

	if (failed) {
		printk(MPT2SAS_ERR_FMT "doorbell handshake "
		    "sending request failed (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}

	/* now wait for the reply */
	if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
		printk(MPT2SAS_ERR_FMT "doorbell handshake "
		   "int failed (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}

	/* read the first two 16-bits, it gives the total length of the reply */
	reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
	    & MPI2_DOORBELL_DATA_MASK);
	writel(0, &ioc->chip->HostInterruptStatus);
	if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
		printk(MPT2SAS_ERR_FMT "doorbell handshake "
		   "int failed (line=%d)\n", ioc->name, __LINE__);
		return -EFAULT;
	}
	reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
	    & MPI2_DOORBELL_DATA_MASK);
	writel(0, &ioc->chip->HostInterruptStatus);

	for (i = 2; i < default_reply->MsgLength * 2; i++)  {
		if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
			printk(MPT2SAS_ERR_FMT "doorbell "
			    "handshake int failed (line=%d)\n", ioc->name,
			    __LINE__);
			return -EFAULT;
		}
		if (i >=  reply_bytes/2) /* overflow case */
			dummy = readl(&ioc->chip->Doorbell);
		else
			reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
			    & MPI2_DOORBELL_DATA_MASK);
		writel(0, &ioc->chip->HostInterruptStatus);
	}

	_base_wait_for_doorbell_int(ioc, 5, sleep_flag);
	if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
		dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
		    " (line=%d)\n", ioc->name, __LINE__));
	}
	writel(0, &ioc->chip->HostInterruptStatus);

	if (ioc->logging_level & MPT_DEBUG_INIT) {
3132
		mfp = (__le32 *)reply;
3133
		printk(KERN_INFO "\toffset:data\n");
E
Eric Moore 已提交
3134
		for (i = 0; i < reply_bytes/4; i++)
3135
			printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
E
Eric Moore 已提交
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167
			    le32_to_cpu(mfp[i]));
	}
	return 0;
}

/**
 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
 * @ioc: per adapter object
 * @mpi_reply: the reply payload from FW
 * @mpi_request: the request payload sent to FW
 *
 * The SAS IO Unit Control Request message allows the host to perform low-level
 * operations, such as resets on the PHYs of the IO Unit, also allows the host
 * to obtain the IOC assigned device handles for a device if it has other
 * identifying information about the device, in addition allows the host to
 * remove IOC resources associated with the device.
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
    Mpi2SasIoUnitControlReply_t *mpi_reply,
    Mpi2SasIoUnitControlRequest_t *mpi_request)
{
	u16 smid;
	u32 ioc_state;
	unsigned long timeleft;
	u8 issue_reset;
	int rc;
	void *request;
	u16 wait_state_count;

3168
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
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
	    __func__));

	mutex_lock(&ioc->base_cmds.mutex);

	if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
		printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
		    ioc->name, __func__);
		rc = -EAGAIN;
		goto out;
	}

	wait_state_count = 0;
	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
		if (wait_state_count++ == 10) {
			printk(MPT2SAS_ERR_FMT
			    "%s: failed due to ioc not operational\n",
			    ioc->name, __func__);
			rc = -EFAULT;
			goto out;
		}
		ssleep(1);
		ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
		printk(MPT2SAS_INFO_FMT "%s: waiting for "
		    "operational state(count=%d)\n", ioc->name,
		    __func__, wait_state_count);
	}

	smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		rc = -EAGAIN;
		goto out;
	}

	rc = 0;
	ioc->base_cmds.status = MPT2_CMD_PENDING;
	request = mpt2sas_base_get_msg_frame(ioc, smid);
	ioc->base_cmds.smid = smid;
	memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
	if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
	    mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
		ioc->ioc_link_reset_in_progress = 1;
3213
	init_completion(&ioc->base_cmds.done);
3214
	mpt2sas_base_put_smid_default(ioc, smid);
E
Eric Moore 已提交
3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272
	timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
	    msecs_to_jiffies(10000));
	if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
	    mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
	    ioc->ioc_link_reset_in_progress)
		ioc->ioc_link_reset_in_progress = 0;
	if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
		printk(MPT2SAS_ERR_FMT "%s: timeout\n",
		    ioc->name, __func__);
		_debug_dump_mf(mpi_request,
		    sizeof(Mpi2SasIoUnitControlRequest_t)/4);
		if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
			issue_reset = 1;
		goto issue_host_reset;
	}
	if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
		memcpy(mpi_reply, ioc->base_cmds.reply,
		    sizeof(Mpi2SasIoUnitControlReply_t));
	else
		memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;
	goto out;

 issue_host_reset:
	if (issue_reset)
		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
		    FORCE_BIG_HAMMER);
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;
	rc = -EFAULT;
 out:
	mutex_unlock(&ioc->base_cmds.mutex);
	return rc;
}


/**
 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
 * @ioc: per adapter object
 * @mpi_reply: the reply payload from FW
 * @mpi_request: the request payload sent to FW
 *
 * The SCSI Enclosure Processor request message causes the IOC to
 * communicate with SES devices to control LED status signals.
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
    Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
{
	u16 smid;
	u32 ioc_state;
	unsigned long timeleft;
	u8 issue_reset;
	int rc;
	void *request;
	u16 wait_state_count;

3273
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
	    __func__));

	mutex_lock(&ioc->base_cmds.mutex);

	if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
		printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
		    ioc->name, __func__);
		rc = -EAGAIN;
		goto out;
	}

	wait_state_count = 0;
	ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
	while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
		if (wait_state_count++ == 10) {
			printk(MPT2SAS_ERR_FMT
			    "%s: failed due to ioc not operational\n",
			    ioc->name, __func__);
			rc = -EFAULT;
			goto out;
		}
		ssleep(1);
		ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
		printk(MPT2SAS_INFO_FMT "%s: waiting for "
		    "operational state(count=%d)\n", ioc->name,
		    __func__, wait_state_count);
	}

	smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		rc = -EAGAIN;
		goto out;
	}

	rc = 0;
	ioc->base_cmds.status = MPT2_CMD_PENDING;
	request = mpt2sas_base_get_msg_frame(ioc, smid);
	ioc->base_cmds.smid = smid;
	memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3315
	init_completion(&ioc->base_cmds.done);
3316
	mpt2sas_base_put_smid_default(ioc, smid);
E
Eric Moore 已提交
3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
	timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
	    msecs_to_jiffies(10000));
	if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
		printk(MPT2SAS_ERR_FMT "%s: timeout\n",
		    ioc->name, __func__);
		_debug_dump_mf(mpi_request,
		    sizeof(Mpi2SepRequest_t)/4);
		if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
			issue_reset = 1;
		goto issue_host_reset;
	}
	if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
		memcpy(mpi_reply, ioc->base_cmds.reply,
		    sizeof(Mpi2SepReply_t));
	else
		memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;
	goto out;

 issue_host_reset:
	if (issue_reset)
		mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
		    FORCE_BIG_HAMMER);
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;
	rc = -EFAULT;
 out:
	mutex_unlock(&ioc->base_cmds.mutex);
	return rc;
}

/**
 * _base_get_port_facts - obtain port facts reply and save in ioc
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
{
	Mpi2PortFactsRequest_t mpi_request;
3358 3359
	Mpi2PortFactsReply_t mpi_reply;
	struct mpt2sas_port_facts *pfacts;
E
Eric Moore 已提交
3360 3361
	int mpi_reply_sz, mpi_request_sz, r;

3362
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379
	    __func__));

	mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
	mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
	memset(&mpi_request, 0, mpi_request_sz);
	mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
	mpi_request.PortNumber = port;
	r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
	    (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);

	if (r != 0) {
		printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
		    ioc->name, __func__, r);
		return r;
	}

	pfacts = &ioc->pfacts[port];
3380
	memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
E
Eric Moore 已提交
3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400
	pfacts->PortNumber = mpi_reply.PortNumber;
	pfacts->VP_ID = mpi_reply.VP_ID;
	pfacts->VF_ID = mpi_reply.VF_ID;
	pfacts->MaxPostedCmdBuffers =
	    le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);

	return 0;
}

/**
 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
{
	Mpi2IOCFactsRequest_t mpi_request;
3401 3402
	Mpi2IOCFactsReply_t mpi_reply;
	struct mpt2sas_facts *facts;
E
Eric Moore 已提交
3403 3404
	int mpi_reply_sz, mpi_request_sz, r;

3405
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421
	    __func__));

	mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
	mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
	memset(&mpi_request, 0, mpi_request_sz);
	mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
	r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
	    (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);

	if (r != 0) {
		printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
		    ioc->name, __func__, r);
		return r;
	}

	facts = &ioc->facts;
3422
	memset(facts, 0, sizeof(struct mpt2sas_facts));
E
Eric Moore 已提交
3423 3424 3425 3426 3427 3428 3429 3430
	facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
	facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
	facts->VP_ID = mpi_reply.VP_ID;
	facts->VF_ID = mpi_reply.VF_ID;
	facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
	facts->MaxChainDepth = mpi_reply.MaxChainDepth;
	facts->WhoInit = mpi_reply.WhoInit;
	facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3431
	facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
E
Eric Moore 已提交
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
	facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
	facts->MaxReplyDescriptorPostQueueDepth =
	    le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
	facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
	facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
	if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
		ioc->ir_firmware = 1;
	facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
	facts->IOCRequestFrameSize =
	    le16_to_cpu(mpi_reply.IOCRequestFrameSize);
	facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
	facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
	ioc->shost->max_id = -1;
	facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
	facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
	facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
	facts->HighPriorityCredit =
	    le16_to_cpu(mpi_reply.HighPriorityCredit);
	facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
	facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);

	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
	    "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
	    facts->MaxChainDepth));
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
	    "reply frame size(%d)\n", ioc->name,
	    facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
	return 0;
}

/**
 * _base_send_ioc_init - send ioc_init to firmware
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
3470
_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
E
Eric Moore 已提交
3471 3472 3473 3474
{
	Mpi2IOCInitRequest_t mpi_request;
	Mpi2IOCInitReply_t mpi_reply;
	int r;
3475
	struct timeval current_time;
3476
	u16 ioc_status;
E
Eric Moore 已提交
3477

3478
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
3479 3480 3481 3482 3483
	    __func__));

	memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
	mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
	mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3484 3485
	mpi_request.VF_ID = 0; /* TODO */
	mpi_request.VP_ID = 0;
E
Eric Moore 已提交
3486 3487 3488
	mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
	mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);

3489 3490
	if (_base_is_controller_msix_enabled(ioc))
		mpi_request.HostMSIxVectors = ioc->reply_queue_count;
E
Eric Moore 已提交
3491 3492 3493 3494 3495 3496 3497
	mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
	mpi_request.ReplyDescriptorPostQueueDepth =
	    cpu_to_le16(ioc->reply_post_queue_depth);
	mpi_request.ReplyFreeQueueDepth =
	    cpu_to_le16(ioc->reply_free_queue_depth);

	mpi_request.SenseBufferAddressHigh =
3498
	    cpu_to_le32((u64)ioc->sense_dma >> 32);
E
Eric Moore 已提交
3499
	mpi_request.SystemReplyAddressHigh =
3500
	    cpu_to_le32((u64)ioc->reply_dma >> 32);
E
Eric Moore 已提交
3501
	mpi_request.SystemRequestFrameBaseAddress =
3502
	    cpu_to_le64((u64)ioc->request_dma);
E
Eric Moore 已提交
3503
	mpi_request.ReplyFreeQueueAddress =
3504
	    cpu_to_le64((u64)ioc->reply_free_dma);
E
Eric Moore 已提交
3505
	mpi_request.ReplyDescriptorPostQueueAddress =
3506 3507
	    cpu_to_le64((u64)ioc->reply_post_free_dma);

E
Eric Moore 已提交
3508

3509 3510 3511 3512
	/* This time stamp specifies number of milliseconds
	 * since epoch ~ midnight January 1, 1970.
	 */
	do_gettimeofday(&current_time);
3513 3514
	mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
	    (current_time.tv_usec / 1000));
3515

E
Eric Moore 已提交
3516
	if (ioc->logging_level & MPT_DEBUG_INIT) {
3517
		__le32 *mfp;
E
Eric Moore 已提交
3518 3519
		int i;

3520
		mfp = (__le32 *)&mpi_request;
3521
		printk(KERN_INFO "\toffset:data\n");
E
Eric Moore 已提交
3522
		for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3523
			printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
E
Eric Moore 已提交
3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537
			    le32_to_cpu(mfp[i]));
	}

	r = _base_handshake_req_reply_wait(ioc,
	    sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
	    sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
	    sleep_flag);

	if (r != 0) {
		printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
		    ioc->name, __func__, r);
		return r;
	}

3538 3539
	ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
E
Eric Moore 已提交
3540 3541 3542 3543 3544 3545 3546 3547
	    mpi_reply.IOCLogInfo) {
		printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
		r = -EIO;
	}

	return 0;
}

3548 3549 3550 3551 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
/**
 * mpt2sas_port_enable_done - command completion routine for port enable
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 *
 * Return 1 meaning mf should be freed from _base_interrupt
 *        0 means the mf is freed from this function.
 */
u8
mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
	u32 reply)
{
	MPI2DefaultReply_t *mpi_reply;
	u16 ioc_status;

	mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
	if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
		return 1;

	if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
		return 1;

	ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
	if (mpi_reply) {
		ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
		memcpy(ioc->port_enable_cmds.reply, mpi_reply,
		    mpi_reply->MsgLength*4);
	}
	ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;

	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;

	if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
		ioc->port_enable_failed = 1;

	if (ioc->is_driver_loading) {
		if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
			mpt2sas_port_enable_complete(ioc);
			return 1;
		} else {
			ioc->start_scan_failed = ioc_status;
			ioc->start_scan = 0;
			return 1;
		}
	}
	complete(&ioc->port_enable_cmds.done);
	return 1;
}


E
Eric Moore 已提交
3600 3601 3602 3603 3604 3605 3606 3607
/**
 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
3608
_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
E
Eric Moore 已提交
3609 3610
{
	Mpi2PortEnableRequest_t *mpi_request;
3611
	Mpi2PortEnableReply_t *mpi_reply;
E
Eric Moore 已提交
3612 3613 3614
	unsigned long timeleft;
	int r = 0;
	u16 smid;
3615
	u16 ioc_status;
E
Eric Moore 已提交
3616 3617 3618

	printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);

3619
	if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
E
Eric Moore 已提交
3620 3621 3622 3623 3624
		printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}

3625
	smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
E
Eric Moore 已提交
3626 3627 3628 3629 3630 3631
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}

3632
	ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
E
Eric Moore 已提交
3633
	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3634
	ioc->port_enable_cmds.smid = smid;
E
Eric Moore 已提交
3635 3636 3637
	memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
	mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;

3638
	init_completion(&ioc->port_enable_cmds.done);
3639
	mpt2sas_base_put_smid_default(ioc, smid);
3640
	timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
E
Eric Moore 已提交
3641
	    300*HZ);
3642
	if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
E
Eric Moore 已提交
3643 3644 3645 3646
		printk(MPT2SAS_ERR_FMT "%s: timeout\n",
		    ioc->name, __func__);
		_debug_dump_mf(mpi_request,
		    sizeof(Mpi2PortEnableRequest_t)/4);
3647
		if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
E
Eric Moore 已提交
3648 3649 3650 3651
			r = -EFAULT;
		else
			r = -ETIME;
		goto out;
3652 3653
	}
	mpi_reply = ioc->port_enable_cmds.reply;
E
Eric Moore 已提交
3654

3655 3656 3657 3658
	ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
	if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
		printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
		    ioc->name, __func__, ioc_status);
E
Eric Moore 已提交
3659
		r = -EFAULT;
3660
		goto out;
E
Eric Moore 已提交
3661 3662
	}
 out:
3663 3664 3665
	ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
	printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
	    "SUCCESS" : "FAILED"));
E
Eric Moore 已提交
3666 3667 3668
	return r;
}

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 3696 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
/**
 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
 * @ioc: per adapter object
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
{
	Mpi2PortEnableRequest_t *mpi_request;
	u16 smid;

	printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);

	if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
		printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}

	smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}

	ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
	ioc->port_enable_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
	mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;

	mpt2sas_base_put_smid_default(ioc, smid);
	return 0;
}

/**
 * _base_determine_wait_on_discovery - desposition
 * @ioc: per adapter object
 *
 * Decide whether to wait on discovery to complete. Used to either
 * locate boot device, or report volumes ahead of physical devices.
 *
 * Returns 1 for wait, 0 for don't wait
 */
static int
_base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
{
	/* We wait for discovery to complete if IR firmware is loaded.
	 * The sas topology events arrive before PD events, so we need time to
	 * turn on the bit in ioc->pd_handles to indicate PD
	 * Also, it maybe required to report Volumes ahead of physical
	 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
	 */
	if (ioc->ir_firmware)
		return 1;

	/* if no Bios, then we don't need to wait */
	if (!ioc->bios_pg3.BiosVersion)
		return 0;

	/* Bios is present, then we drop down here.
	 *
	 * If there any entries in the Bios Page 2, then we wait
	 * for discovery to complete.
	 */

	/* Current Boot Device */
	if ((ioc->bios_pg2.CurrentBootDeviceForm &
	    MPI2_BIOSPAGE2_FORM_MASK) ==
	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
	/* Request Boot Device */
	   (ioc->bios_pg2.ReqBootDeviceForm &
	    MPI2_BIOSPAGE2_FORM_MASK) ==
	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
	/* Alternate Request Boot Device */
	   (ioc->bios_pg2.ReqAltBootDeviceForm &
	    MPI2_BIOSPAGE2_FORM_MASK) ==
	    MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
		return 0;

	return 1;
}


E
Eric Moore 已提交
3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789
/**
 * _base_unmask_events - turn on notification for this event
 * @ioc: per adapter object
 * @event: firmware event
 *
 * The mask is stored in ioc->event_masks.
 */
static void
_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
{
	u32 desired_event;

	if (event >= 128)
		return;

	desired_event = (1 << (event % 32));

	if (event < 32)
		ioc->event_masks[0] &= ~desired_event;
	else if (event < 64)
		ioc->event_masks[1] &= ~desired_event;
	else if (event < 96)
		ioc->event_masks[2] &= ~desired_event;
	else if (event < 128)
		ioc->event_masks[3] &= ~desired_event;
}

/**
 * _base_event_notification - send event notification
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
3790
_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
E
Eric Moore 已提交
3791 3792 3793 3794 3795 3796 3797
{
	Mpi2EventNotificationRequest_t *mpi_request;
	unsigned long timeleft;
	u16 smid;
	int r = 0;
	int i;

3798
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
	    __func__));

	if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
		printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}

	smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
	if (!smid) {
		printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
		    ioc->name, __func__);
		return -EAGAIN;
	}
	ioc->base_cmds.status = MPT2_CMD_PENDING;
	mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
	ioc->base_cmds.smid = smid;
	memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
	mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
3818 3819
	mpi_request->VF_ID = 0; /* TODO */
	mpi_request->VP_ID = 0;
E
Eric Moore 已提交
3820 3821
	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
		mpi_request->EventMasks[i] =
3822
		    cpu_to_le32(ioc->event_masks[i]);
3823
	init_completion(&ioc->base_cmds.done);
3824
	mpt2sas_base_put_smid_default(ioc, smid);
E
Eric Moore 已提交
3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835
	timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
	if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
		printk(MPT2SAS_ERR_FMT "%s: timeout\n",
		    ioc->name, __func__);
		_debug_dump_mf(mpi_request,
		    sizeof(Mpi2EventNotificationRequest_t)/4);
		if (ioc->base_cmds.status & MPT2_CMD_RESET)
			r = -EFAULT;
		else
			r = -ETIME;
	} else
3836
		dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
E
Eric Moore 已提交
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874
		    ioc->name, __func__));
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;
	return r;
}

/**
 * mpt2sas_base_validate_event_type - validating event types
 * @ioc: per adapter object
 * @event: firmware event
 *
 * This will turn on firmware event notification when application
 * ask for that event. We don't mask events that are already enabled.
 */
void
mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
{
	int i, j;
	u32 event_mask, desired_event;
	u8 send_update_to_fw;

	for (i = 0, send_update_to_fw = 0; i <
	    MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
		event_mask = ~event_type[i];
		desired_event = 1;
		for (j = 0; j < 32; j++) {
			if (!(event_mask & desired_event) &&
			    (ioc->event_masks[i] & desired_event)) {
				ioc->event_masks[i] &= ~desired_event;
				send_update_to_fw = 1;
			}
			desired_event = (desired_event << 1);
		}
	}

	if (!send_update_to_fw)
		return;

	mutex_lock(&ioc->base_cmds.mutex);
3875
	_base_event_notification(ioc, CAN_SLEEP);
E
Eric Moore 已提交
3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894
	mutex_unlock(&ioc->base_cmds.mutex);
}

/**
 * _base_diag_reset - the "big hammer" start of day reset
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
{
	u32 host_diagnostic;
	u32 ioc_state;
	u32 count;
	u32 hcb_size;

	printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3895
	drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
E
Eric Moore 已提交
3896 3897 3898 3899 3900 3901 3902
	    ioc->name));

	count = 0;
	do {
		/* Write magic sequence to WriteSequence register
		 * Loop until in diagnostic mode
		 */
3903
		drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
E
Eric Moore 已提交
3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922
		    "sequence\n", ioc->name));
		writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
		writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);

		/* wait 100 msec */
		if (sleep_flag == CAN_SLEEP)
			msleep(100);
		else
			mdelay(100);

		if (count++ > 20)
			goto out;

		host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3923
		drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
E
Eric Moore 已提交
3924 3925 3926 3927 3928 3929 3930
		    "sequence: count(%d), host_diagnostic(0x%08x)\n",
		    ioc->name, count, host_diagnostic));

	} while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);

	hcb_size = readl(&ioc->chip->HCBSize);

3931
	drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
E
Eric Moore 已提交
3932 3933 3934 3935
	    ioc->name));
	writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
	     &ioc->chip->HostDiagnostic);

3936 3937 3938 3939 3940
	/* This delay allows the chip PCIe hardware time to finish reset tasks*/
	if (sleep_flag == CAN_SLEEP)
		msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
	else
		mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
E
Eric Moore 已提交
3941

3942 3943 3944
	/* Approximately 300 second max wait */
	for (count = 0; count < (300000000 /
	    MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
E
Eric Moore 已提交
3945 3946 3947 3948 3949 3950 3951 3952

		host_diagnostic = readl(&ioc->chip->HostDiagnostic);

		if (host_diagnostic == 0xFFFFFFFF)
			goto out;
		if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
			break;

3953
		/* Wait to pass the second read delay window */
E
Eric Moore 已提交
3954
		if (sleep_flag == CAN_SLEEP)
3955 3956
			msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
			       /1000);
E
Eric Moore 已提交
3957
		else
3958 3959
			mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
			       /1000);
E
Eric Moore 已提交
3960 3961 3962 3963
	}

	if (host_diagnostic & MPI2_DIAG_HCB_MODE) {

3964
		drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
E
Eric Moore 已提交
3965 3966 3967 3968 3969 3970
		    "assuming the HCB Address points to good F/W\n",
		    ioc->name));
		host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
		host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
		writel(host_diagnostic, &ioc->chip->HostDiagnostic);

3971
		drsprintk(ioc, printk(MPT2SAS_INFO_FMT
E
Eric Moore 已提交
3972 3973 3974 3975 3976
		    "re-enable the HCDW\n", ioc->name));
		writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
		    &ioc->chip->HCBSize);
	}

3977
	drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
E
Eric Moore 已提交
3978 3979 3980 3981
	    ioc->name));
	writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
	    &ioc->chip->HostDiagnostic);

3982
	drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
E
Eric Moore 已提交
3983 3984 3985
	    "diagnostic register\n", ioc->name));
	writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);

3986
	drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
E
Eric Moore 已提交
3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016
	    "READY state\n", ioc->name));
	ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
	    sleep_flag);
	if (ioc_state) {
		printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
		    " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
		goto out;
	}

	printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
	return 0;

 out:
	printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
	return -EFAULT;
}

/**
 * _base_make_ioc_ready - put controller in READY state
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 * @type: FORCE_BIG_HAMMER or SOFT_RESET
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
    enum reset_type type)
{
	u32 ioc_state;
4017
	int rc;
E
Eric Moore 已提交
4018

4019
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
4020 4021
	    __func__));

4022 4023 4024
	if (ioc->pci_error_recovery)
		return 0;

E
Eric Moore 已提交
4025
	ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4026
	dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
E
Eric Moore 已提交
4027 4028 4029 4030 4031 4032
	    ioc->name, __func__, ioc_state));

	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
		return 0;

	if (ioc_state & MPI2_DOORBELL_USED) {
4033
		dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
E
Eric Moore 已提交
4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048
		    "active!\n", ioc->name));
		goto issue_diag_reset;
	}

	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
		mpt2sas_base_fault_info(ioc, ioc_state &
		    MPI2_DOORBELL_DATA_MASK);
		goto issue_diag_reset;
	}

	if (type == FORCE_BIG_HAMMER)
		goto issue_diag_reset;

	if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
		if (!(_base_send_ioc_reset(ioc,
4049 4050
		    MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
			ioc->ioc_reset_count++;
E
Eric Moore 已提交
4051
			return 0;
4052
	}
E
Eric Moore 已提交
4053 4054

 issue_diag_reset:
4055 4056 4057
	rc = _base_diag_reset(ioc, CAN_SLEEP);
	ioc->ioc_reset_count++;
	return rc;
E
Eric Moore 已提交
4058 4059 4060 4061 4062 4063 4064 4065 4066 4067
}

/**
 * _base_make_ioc_operational - put controller in OPERATIONAL state
 * @ioc: per adapter object
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * Returns 0 for success, non-zero for failure.
 */
static int
4068
_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
E
Eric Moore 已提交
4069 4070 4071 4072
{
	int r, i;
	unsigned long	flags;
	u32 reply_address;
4073
	u16 smid;
4074
	struct _tr_list *delayed_tr, *delayed_tr_next;
4075
	u8 hide_flag;
4076 4077 4078
	struct adapter_reply_queue *reply_q;
	long reply_post_free;
	u32 reply_post_free_sz;
E
Eric Moore 已提交
4079

4080
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
4081 4082
	    __func__));

4083 4084 4085 4086 4087 4088 4089
	/* clean the delayed target reset list */
	list_for_each_entry_safe(delayed_tr, delayed_tr_next,
	    &ioc->delayed_tr_list, list) {
		list_del(&delayed_tr->list);
		kfree(delayed_tr);
	}

4090 4091 4092 4093 4094 4095
	list_for_each_entry_safe(delayed_tr, delayed_tr_next,
	    &ioc->delayed_tr_volume_list, list) {
		list_del(&delayed_tr->list);
		kfree(delayed_tr);
	}

E
Eric Moore 已提交
4096 4097 4098
	/* initialize the scsi lookup free list */
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
	INIT_LIST_HEAD(&ioc->free_list);
4099 4100
	smid = 1;
	for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4101
		INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
E
Eric Moore 已提交
4102
		ioc->scsi_lookup[i].cb_idx = 0xFF;
4103 4104
		ioc->scsi_lookup[i].smid = smid;
		ioc->scsi_lookup[i].scmd = NULL;
4105
		ioc->scsi_lookup[i].direct_io = 0;
E
Eric Moore 已提交
4106 4107 4108
		list_add_tail(&ioc->scsi_lookup[i].tracker_list,
		    &ioc->free_list);
	}
4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128

	/* hi-priority queue */
	INIT_LIST_HEAD(&ioc->hpr_free_list);
	smid = ioc->hi_priority_smid;
	for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
		ioc->hpr_lookup[i].cb_idx = 0xFF;
		ioc->hpr_lookup[i].smid = smid;
		list_add_tail(&ioc->hpr_lookup[i].tracker_list,
		    &ioc->hpr_free_list);
	}

	/* internal queue */
	INIT_LIST_HEAD(&ioc->internal_free_list);
	smid = ioc->internal_smid;
	for (i = 0; i < ioc->internal_depth; i++, smid++) {
		ioc->internal_lookup[i].cb_idx = 0xFF;
		ioc->internal_lookup[i].smid = smid;
		list_add_tail(&ioc->internal_lookup[i].tracker_list,
		    &ioc->internal_free_list);
	}
4129 4130 4131 4132 4133 4134 4135

	/* chain pool */
	INIT_LIST_HEAD(&ioc->free_chain_list);
	for (i = 0; i < ioc->chain_depth; i++)
		list_add_tail(&ioc->chain_lookup[i].tracker_list,
		    &ioc->free_chain_list);

E
Eric Moore 已提交
4136 4137 4138 4139 4140 4141 4142 4143
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

	/* initialize Reply Free Queue */
	for (i = 0, reply_address = (u32)ioc->reply_dma ;
	    i < ioc->reply_free_queue_depth ; i++, reply_address +=
	    ioc->reply_sz)
		ioc->reply_free[i] = cpu_to_le32(reply_address);

4144
	/* initialize reply queues */
4145 4146
	if (ioc->is_driver_loading)
		_base_assign_reply_queues(ioc);
4147

E
Eric Moore 已提交
4148
	/* initialize Reply Post Free Queue */
4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163
	reply_post_free = (long)ioc->reply_post_free;
	reply_post_free_sz = ioc->reply_post_queue_depth *
	    sizeof(Mpi2DefaultReplyDescriptor_t);
	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
		reply_q->reply_post_host_index = 0;
		reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
		    reply_post_free;
		for (i = 0; i < ioc->reply_post_queue_depth; i++)
			reply_q->reply_post_free[i].Words =
							cpu_to_le64(ULLONG_MAX);
		if (!_base_is_controller_msix_enabled(ioc))
			goto skip_init_reply_post_free_queue;
		reply_post_free += reply_post_free_sz;
	}
 skip_init_reply_post_free_queue:
E
Eric Moore 已提交
4164

4165
	r = _base_send_ioc_init(ioc, sleep_flag);
E
Eric Moore 已提交
4166 4167 4168
	if (r)
		return r;

4169
	/* initialize reply free host index */
E
Eric Moore 已提交
4170 4171
	ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
	writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4172 4173 4174 4175 4176 4177 4178 4179 4180 4181

	/* initialize reply post host index */
	list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
		writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
		    &ioc->chip->ReplyPostHostIndex);
		if (!_base_is_controller_msix_enabled(ioc))
			goto skip_init_reply_post_host_index;
	}

 skip_init_reply_post_host_index:
E
Eric Moore 已提交
4182 4183

	_base_unmask_interrupts(ioc);
4184

4185
	r = _base_event_notification(ioc, sleep_flag);
E
Eric Moore 已提交
4186 4187 4188 4189 4190 4191
	if (r)
		return r;

	if (sleep_flag == CAN_SLEEP)
		_base_static_config_pages(ioc);

4192 4193

	if (ioc->is_driver_loading) {
4194 4195
		if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
		    == 0x80) {
4196 4197
			hide_flag = (u8) (
			    le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
4198 4199 4200 4201
			    MFG_PAGE10_HIDE_SSDS_MASK);
			if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
				ioc->mfg_pg10_hide_flag = hide_flag;
		}
4202 4203 4204
		ioc->wait_for_discovery_to_complete =
		    _base_determine_wait_on_discovery(ioc);
		return r; /* scan_start and scan_finished support */
4205
	}
4206
	r = _base_send_port_enable(ioc, sleep_flag);
E
Eric Moore 已提交
4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223
	if (r)
		return r;

	return r;
}

/**
 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
 * @ioc: per adapter object
 *
 * Return nothing.
 */
void
mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
{
	struct pci_dev *pdev = ioc->pdev;

4224
	dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
4225 4226
	    __func__));

4227 4228 4229 4230 4231 4232 4233
	if (ioc->chip_phys && ioc->chip) {
		_base_mask_interrupts(ioc);
		ioc->shost_recovery = 1;
		_base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
		ioc->shost_recovery = 0;
	}

4234
	_base_free_irq(ioc);
E
Eric Moore 已提交
4235
	_base_disable_msix(ioc);
4236 4237

	if (ioc->chip_phys && ioc->chip)
E
Eric Moore 已提交
4238 4239
		iounmap(ioc->chip);
	ioc->chip_phys = 0;
4240 4241 4242 4243 4244 4245

	if (pci_is_enabled(pdev)) {
		pci_release_selected_regions(ioc->pdev, ioc->bars);
		pci_disable_pcie_error_reporting(pdev);
		pci_disable_device(pdev);
	}
E
Eric Moore 已提交
4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258
	return;
}

/**
 * mpt2sas_base_attach - attach controller instance
 * @ioc: per adapter object
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
{
	int r, i;
4259
	int cpu_id, last_cpu_id = 0;
E
Eric Moore 已提交
4260

4261
	dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
4262 4263
	    __func__));

4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288
	/* setup cpu_msix_table */
	ioc->cpu_count = num_online_cpus();
	for_each_online_cpu(cpu_id)
		last_cpu_id = cpu_id;
	ioc->cpu_msix_table_sz = last_cpu_id + 1;
	ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
	ioc->reply_queue_count = 1;
	if (!ioc->cpu_msix_table) {
		dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
		    "cpu_msix_table failed!!!\n", ioc->name));
		r = -ENOMEM;
		goto out_free_resources;
	}

	if (ioc->is_warpdrive) {
		ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
		    sizeof(resource_size_t *), GFP_KERNEL);
		if (!ioc->reply_post_host_index) {
			dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
				"for cpu_msix_table failed!!!\n", ioc->name));
			r = -ENOMEM;
			goto out_free_resources;
		}
	}

E
Eric Moore 已提交
4289 4290
	r = mpt2sas_base_map_resources(ioc);
	if (r)
4291
		goto out_free_resources;
E
Eric Moore 已提交
4292

4293
	if (ioc->is_warpdrive) {
4294 4295
		ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
		    &ioc->chip->ReplyPostHostIndex;
4296 4297

		for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4298 4299 4300
			ioc->reply_post_host_index[i] =
			(resource_size_t __iomem *)
			((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4301 4302 4303
			* 4)));
	}

4304
	pci_set_drvdata(ioc->pdev, ioc->shost);
4305
	r = _base_get_ioc_facts(ioc, CAN_SLEEP);
E
Eric Moore 已提交
4306 4307 4308
	if (r)
		goto out_free_resources;

4309
	r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
E
Eric Moore 已提交
4310 4311 4312
	if (r)
		goto out_free_resources;

4313
	ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4314
	    sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
4315 4316
	if (!ioc->pfacts) {
		r = -ENOMEM;
4317
		goto out_free_resources;
4318
	}
4319 4320 4321 4322 4323 4324 4325

	for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
		r = _base_get_port_facts(ioc, i, CAN_SLEEP);
		if (r)
			goto out_free_resources;
	}

E
Eric Moore 已提交
4326 4327 4328 4329 4330
	r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
	if (r)
		goto out_free_resources;

	init_waitqueue_head(&ioc->reset_wq);
4331 4332 4333 4334 4335 4336
	/* allocate memory pd handle bitmask list */
	ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
	if (ioc->facts.MaxDevHandle % 8)
		ioc->pd_handles_sz++;
	ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
	    GFP_KERNEL);
4337 4338
	if (!ioc->pd_handles) {
		r = -ENOMEM;
4339
		goto out_free_resources;
4340
	}
4341 4342 4343 4344 4345 4346
	ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
	    GFP_KERNEL);
	if (!ioc->blocking_handles) {
		r = -ENOMEM;
		goto out_free_resources;
	}
4347 4348
	ioc->fwfault_debug = mpt2sas_fwfault_debug;

E
Eric Moore 已提交
4349 4350 4351 4352 4353
	/* base internal command bits */
	mutex_init(&ioc->base_cmds.mutex);
	ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->base_cmds.status = MPT2_CMD_NOT_USED;

4354 4355 4356 4357
	/* port_enable command bits */
	ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;

E
Eric Moore 已提交
4358 4359 4360 4361 4362
	/* transport internal command bits */
	ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
	mutex_init(&ioc->transport_cmds.mutex);

4363 4364 4365 4366 4367
	/* scsih internal command bits */
	ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
	mutex_init(&ioc->scsih_cmds.mutex);

E
Eric Moore 已提交
4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379
	/* task management internal command bits */
	ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
	mutex_init(&ioc->tm_cmds.mutex);

	/* config page internal command bits */
	ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
	ioc->config_cmds.status = MPT2_CMD_NOT_USED;
	mutex_init(&ioc->config_cmds.mutex);

	/* ctl module internal command bits */
	ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4380
	ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
E
Eric Moore 已提交
4381 4382 4383
	ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
	mutex_init(&ioc->ctl_cmds.mutex);

4384 4385
	if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
	    !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4386 4387
	    !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
	    !ioc->ctl_cmds.sense) {
4388 4389 4390 4391
		r = -ENOMEM;
		goto out_free_resources;
	}

4392 4393 4394 4395 4396 4397 4398
	if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
	    !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
	    !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
		r = -ENOMEM;
		goto out_free_resources;
	}

E
Eric Moore 已提交
4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412
	for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
		ioc->event_masks[i] = -1;

	/* here we enable the events we care about */
	_base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
	_base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
	_base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
	_base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
	_base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
	_base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
	_base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
	_base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
	_base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
	_base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4413
	r = _base_make_ioc_operational(ioc, CAN_SLEEP);
E
Eric Moore 已提交
4414 4415 4416
	if (r)
		goto out_free_resources;

4417
	ioc->non_operational_loop = 0;
4418

E
Eric Moore 已提交
4419 4420 4421 4422 4423 4424 4425
	return 0;

 out_free_resources:

	ioc->remove_host = 1;
	mpt2sas_base_free_resources(ioc);
	_base_release_memory_pools(ioc);
4426
	pci_set_drvdata(ioc->pdev, NULL);
4427 4428 4429
	kfree(ioc->cpu_msix_table);
	if (ioc->is_warpdrive)
		kfree(ioc->reply_post_host_index);
4430
	kfree(ioc->pd_handles);
4431
	kfree(ioc->blocking_handles);
E
Eric Moore 已提交
4432 4433
	kfree(ioc->tm_cmds.reply);
	kfree(ioc->transport_cmds.reply);
4434
	kfree(ioc->scsih_cmds.reply);
E
Eric Moore 已提交
4435 4436
	kfree(ioc->config_cmds.reply);
	kfree(ioc->base_cmds.reply);
4437
	kfree(ioc->port_enable_cmds.reply);
E
Eric Moore 已提交
4438
	kfree(ioc->ctl_cmds.reply);
4439
	kfree(ioc->ctl_cmds.sense);
E
Eric Moore 已提交
4440 4441 4442 4443
	kfree(ioc->pfacts);
	ioc->ctl_cmds.reply = NULL;
	ioc->base_cmds.reply = NULL;
	ioc->tm_cmds.reply = NULL;
4444
	ioc->scsih_cmds.reply = NULL;
E
Eric Moore 已提交
4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461
	ioc->transport_cmds.reply = NULL;
	ioc->config_cmds.reply = NULL;
	ioc->pfacts = NULL;
	return r;
}


/**
 * mpt2sas_base_detach - remove controller instance
 * @ioc: per adapter object
 *
 * Return nothing.
 */
void
mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
{

4462
	dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
E
Eric Moore 已提交
4463 4464
	    __func__));

4465
	mpt2sas_base_stop_watchdog(ioc);
E
Eric Moore 已提交
4466 4467
	mpt2sas_base_free_resources(ioc);
	_base_release_memory_pools(ioc);
4468
	pci_set_drvdata(ioc->pdev, NULL);
4469 4470 4471
	kfree(ioc->cpu_msix_table);
	if (ioc->is_warpdrive)
		kfree(ioc->reply_post_host_index);
4472
	kfree(ioc->pd_handles);
4473
	kfree(ioc->blocking_handles);
E
Eric Moore 已提交
4474 4475
	kfree(ioc->pfacts);
	kfree(ioc->ctl_cmds.reply);
4476
	kfree(ioc->ctl_cmds.sense);
E
Eric Moore 已提交
4477
	kfree(ioc->base_cmds.reply);
4478
	kfree(ioc->port_enable_cmds.reply);
E
Eric Moore 已提交
4479 4480
	kfree(ioc->tm_cmds.reply);
	kfree(ioc->transport_cmds.reply);
4481
	kfree(ioc->scsih_cmds.reply);
E
Eric Moore 已提交
4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499
	kfree(ioc->config_cmds.reply);
}

/**
 * _base_reset_handler - reset callback handler (for base)
 * @ioc: per adapter object
 * @reset_phase: phase
 *
 * The handler for doing any required cleanup or initialization.
 *
 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
 * MPT2_IOC_DONE_RESET
 *
 * Return nothing.
 */
static void
_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
{
4500 4501
	mpt2sas_scsih_reset_handler(ioc, reset_phase);
	mpt2sas_ctl_reset_handler(ioc, reset_phase);
E
Eric Moore 已提交
4502 4503
	switch (reset_phase) {
	case MPT2_IOC_PRE_RESET:
4504
		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
E
Eric Moore 已提交
4505 4506 4507
		    "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
		break;
	case MPT2_IOC_AFTER_RESET:
4508
		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
E
Eric Moore 已提交
4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519
		    "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
		if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
			ioc->transport_cmds.status |= MPT2_CMD_RESET;
			mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
			complete(&ioc->transport_cmds.done);
		}
		if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
			ioc->base_cmds.status |= MPT2_CMD_RESET;
			mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
			complete(&ioc->base_cmds.done);
		}
4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533
		if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
			ioc->port_enable_failed = 1;
			ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
			mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
			if (ioc->is_driver_loading) {
				ioc->start_scan_failed =
				    MPI2_IOCSTATUS_INTERNAL_ERROR;
				ioc->start_scan = 0;
				ioc->port_enable_cmds.status =
						MPT2_CMD_NOT_USED;
			} else
				complete(&ioc->port_enable_cmds.done);

		}
E
Eric Moore 已提交
4534 4535 4536
		if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
			ioc->config_cmds.status |= MPT2_CMD_RESET;
			mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
4537
			ioc->config_cmds.smid = USHRT_MAX;
E
Eric Moore 已提交
4538 4539 4540 4541
			complete(&ioc->config_cmds.done);
		}
		break;
	case MPT2_IOC_DONE_RESET:
4542
		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
E
Eric Moore 已提交
4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572
		    "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
		break;
	}
}

/**
 * _wait_for_commands_to_complete - reset controller
 * @ioc: Pointer to MPT_ADAPTER structure
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 *
 * This function waiting(3s) for all pending commands to complete
 * prior to putting controller in reset.
 */
static void
_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
{
	u32 ioc_state;
	unsigned long flags;
	u16 i;

	ioc->pending_io_count = 0;
	if (sleep_flag != CAN_SLEEP)
		return;

	ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
	if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
		return;

	/* pending command count */
	spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4573
	for (i = 0; i < ioc->scsiio_depth; i++)
E
Eric Moore 已提交
4574 4575 4576 4577 4578 4579 4580 4581
		if (ioc->scsi_lookup[i].cb_idx != 0xFF)
			ioc->pending_io_count++;
	spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);

	if (!ioc->pending_io_count)
		return;

	/* wait for pending commands to complete */
4582
	wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
E
Eric Moore 已提交
4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596
}

/**
 * mpt2sas_base_hard_reset_handler - reset controller
 * @ioc: Pointer to MPT_ADAPTER structure
 * @sleep_flag: CAN_SLEEP or NO_SLEEP
 * @type: FORCE_BIG_HAMMER or SOFT_RESET
 *
 * Returns 0 for success, non-zero for failure.
 */
int
mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
    enum reset_type type)
{
4597
	int r;
E
Eric Moore 已提交
4598 4599
	unsigned long flags;

4600
	dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
E
Eric Moore 已提交
4601 4602
	    __func__));

4603 4604 4605 4606
	if (ioc->pci_error_recovery) {
		printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
		    ioc->name, __func__);
		r = 0;
4607
		goto out_unlocked;
4608 4609
	}

4610 4611 4612
	if (mpt2sas_fwfault_debug)
		mpt2sas_halt_firmware(ioc);

4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624
	/* TODO - What we really should be doing is pulling
	 * out all the code associated with NO_SLEEP; its never used.
	 * That is legacy code from mpt fusion driver, ported over.
	 * I will leave this BUG_ON here for now till its been resolved.
	 */
	BUG_ON(sleep_flag == NO_SLEEP);

	/* wait for an active reset in progress to complete */
	if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
		do {
			ssleep(1);
		} while (ioc->shost_recovery == 1);
4625
		dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4626 4627
		    __func__));
		return ioc->ioc_reset_in_progress_status;
E
Eric Moore 已提交
4628
	}
4629 4630

	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
E
Eric Moore 已提交
4631 4632 4633 4634 4635 4636 4637 4638 4639 4640
	ioc->shost_recovery = 1;
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);

	_base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
	_wait_for_commands_to_complete(ioc, sleep_flag);
	_base_mask_interrupts(ioc);
	r = _base_make_ioc_ready(ioc, sleep_flag, type);
	if (r)
		goto out;
	_base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
4641 4642 4643 4644

	/* If this hard reset is called while port enable is active, then
	 * there is no reason to call make_ioc_operational
	 */
4645 4646
	if (ioc->is_driver_loading && ioc->port_enable_failed) {
		ioc->remove_host = 1;
4647 4648 4649
		r = -EFAULT;
		goto out;
	}
4650
	r = _base_make_ioc_operational(ioc, sleep_flag);
E
Eric Moore 已提交
4651 4652 4653
	if (!r)
		_base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
 out:
4654
	dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
E
Eric Moore 已提交
4655 4656 4657
	    ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));

	spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4658
	ioc->ioc_reset_in_progress_status = r;
4659
	ioc->shost_recovery = 0;
E
Eric Moore 已提交
4660
	spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4661
	mutex_unlock(&ioc->reset_in_progress_mutex);
4662

4663
 out_unlocked:
4664
	dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
4665
	    __func__));
E
Eric Moore 已提交
4666 4667
	return r;
}