lpfc_sli.c 210.8 KB
Newer Older
已提交
1 2
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
3
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2004-2008 Emulex.  All rights reserved.           *
5
 * EMULEX and SLI are trademarks of Emulex.                        *
已提交
6
 * www.emulex.com                                                  *
7
 * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
已提交
8 9
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
10 11 12 13 14 15 16 17 18 19
 * modify it under the terms of version 2 of the GNU General       *
 * Public License as published by the Free Software Foundation.    *
 * This program is distributed in the hope that it will be useful. *
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
 * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
 * more details, a copy of which can be found in the file COPYING  *
 * included with this package.                                     *
已提交
20 21 22 23 24 25 26
 *******************************************************************/

#include <linux/blkdev.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/delay.h>

27
#include <scsi/scsi.h>
已提交
28 29 30
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>
31
#include <scsi/scsi_transport_fc.h>
32
#include <scsi/fc/fc_fs.h>
已提交
33

34
#include "lpfc_hw4.h"
已提交
35 36
#include "lpfc_hw.h"
#include "lpfc_sli.h"
37
#include "lpfc_sli4.h"
38
#include "lpfc_nl.h"
已提交
39 40 41 42 43 44
#include "lpfc_disc.h"
#include "lpfc_scsi.h"
#include "lpfc.h"
#include "lpfc_crtn.h"
#include "lpfc_logmsg.h"
#include "lpfc_compat.h"
J
James Smart 已提交
45
#include "lpfc_debugfs.h"
已提交
46 47 48 49 50 51

/*
 * Define macro to log: Mailbox command x%x cannot issue Data
 * This allows multiple uses of lpfc_msgBlk0311
 * w/o perturbing log msg utility.
 */
52
#define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
已提交
53 54 55
			lpfc_printf_log(phba, \
				KERN_INFO, \
				LOG_MBOX | LOG_SLI, \
56
				"(%d):0311 Mailbox command x%x cannot " \
57 58 59
				"issue Data: x%x x%x x%x\n", \
				pmbox->vport ? pmbox->vport->vpi : 0, \
				pmbox->mb.mbxCommand,		\
J
James Smart 已提交
60
				phba->pport->port_state,	\
已提交
61
				psli->sli_flag,	\
J
James Smart 已提交
62
				flag)
已提交
63 64 65 66 67 68 69 70 71 72


/* There are only four IOCB completion types. */
typedef enum _lpfc_iocb_type {
	LPFC_UNKNOWN_IOCB,
	LPFC_UNSOL_IOCB,
	LPFC_SOL_IOCB,
	LPFC_ABORT_IOCB
} lpfc_iocb_type;

73
/**
74
 * lpfc_cmd_iocb - Get next command iocb entry in the ring
75 76 77 78 79 80 81 82
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function returns pointer to next command iocb entry
 * in the command ring. The caller must hold hbalock to prevent
 * other threads consume the next command iocb.
 * SLI-2/SLI-3 provide different sized iocbs.
 **/
83 84 85 86 87 88 89
static inline IOCB_t *
lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
	return (IOCB_t *) (((char *) pring->cmdringaddr) +
			   pring->cmdidx * phba->iocb_cmd_size);
}

90
/**
91
 * lpfc_resp_iocb - Get next response iocb entry in the ring
92 93 94 95 96 97 98 99
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function returns pointer to next response iocb entry
 * in the response ring. The caller must hold hbalock to make sure
 * that no other thread consume the next response iocb.
 * SLI-2/SLI-3 provide different sized iocbs.
 **/
100 101 102 103 104 105 106
static inline IOCB_t *
lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
	return (IOCB_t *) (((char *) pring->rspringaddr) +
			   pring->rspidx * phba->iocb_rsp_size);
}

107
/**
108
 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
109 110 111 112 113 114 115
 * @phba: Pointer to HBA context object.
 *
 * This function is called with hbalock held. This function
 * allocates a new driver iocb object from the iocb pool. If the
 * allocation is successful, it returns pointer to the newly
 * allocated iocb object else it returns NULL.
 **/
J
James Smart 已提交
116 117
static struct lpfc_iocbq *
__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
118 119 120 121 122 123 124 125
{
	struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
	struct lpfc_iocbq * iocbq = NULL;

	list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
	return iocbq;
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
/**
 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
 * @phba: Pointer to HBA context object.
 * @xritag: XRI value.
 *
 * This function clears the sglq pointer from the array of acive
 * sglq's. The xritag that is passed in is used to index into the
 * array. Before the xritag can be used it needs to be adjusted
 * by subtracting the xribase.
 *
 * Returns sglq ponter = success, NULL = Failure.
 **/
static struct lpfc_sglq *
__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
{
	uint16_t adj_xri;
	struct lpfc_sglq *sglq;
	adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
	if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
		return NULL;
	sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
	phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
	return sglq;
}

/**
 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
 * @phba: Pointer to HBA context object.
 * @xritag: XRI value.
 *
 * This function returns the sglq pointer from the array of acive
 * sglq's. The xritag that is passed in is used to index into the
 * array. Before the xritag can be used it needs to be adjusted
 * by subtracting the xribase.
 *
 * Returns sglq ponter = success, NULL = Failure.
 **/
static struct lpfc_sglq *
__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
{
	uint16_t adj_xri;
	struct lpfc_sglq *sglq;
	adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
	if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
		return NULL;
	sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
	return sglq;
}

/**
 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
 * @phba: Pointer to HBA context object.
 *
 * This function is called with hbalock held. This function
 * Gets a new driver sglq object from the sglq list. If the
 * list is not empty then it is successful, it returns pointer to the newly
 * allocated sglq object else it returns NULL.
 **/
static struct lpfc_sglq *
__lpfc_sli_get_sglq(struct lpfc_hba *phba)
{
	struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
	struct lpfc_sglq *sglq = NULL;
	uint16_t adj_xri;
	list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
	adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
	phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
	return sglq;
}

196
/**
197
 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
198 199 200 201 202 203 204
 * @phba: Pointer to HBA context object.
 *
 * This function is called with no lock held. This function
 * allocates a new driver iocb object from the iocb pool. If the
 * allocation is successful, it returns pointer to the newly
 * allocated iocb object else it returns NULL.
 **/
J
James Smart 已提交
205 206 207 208 209 210 211 212 213 214 215 216
struct lpfc_iocbq *
lpfc_sli_get_iocbq(struct lpfc_hba *phba)
{
	struct lpfc_iocbq * iocbq = NULL;
	unsigned long iflags;

	spin_lock_irqsave(&phba->hbalock, iflags);
	iocbq = __lpfc_sli_get_iocbq(phba);
	spin_unlock_irqrestore(&phba->hbalock, iflags);
	return iocbq;
}

217
/**
218
 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
219 220 221 222 223 224 225 226
 * @phba: Pointer to HBA context object.
 * @iocbq: Pointer to driver iocb object.
 *
 * This function is called with hbalock held to release driver
 * iocb object to the iocb pool. The iotag in the iocb object
 * does not change for each use of the iocb object. This function
 * clears all other fields of the iocb object when it is freed.
 **/
A
Adrian Bunk 已提交
227
static void
228
__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
229
{
J
James Smart 已提交
230
	size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
231 232 233 234 235

	/*
	 * Clean all volatile data fields, preserve iotag and node struct.
	 */
	memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
236
	iocbq->sli4_xritag = NO_XRI;
237 238 239
	list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
}

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
/**
 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
 * @phba: Pointer to HBA context object.
 * @iocbq: Pointer to driver iocb object.
 *
 * This function is called with hbalock held to release driver
 * iocb object to the iocb pool. The iotag in the iocb object
 * does not change for each use of the iocb object. This function
 * clears all other fields of the iocb object when it is freed.
 **/
static void
__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
{
	phba->__lpfc_sli_release_iocbq(phba, iocbq);
}

256
/**
257
 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
258 259 260 261 262 263
 * @phba: Pointer to HBA context object.
 * @iocbq: Pointer to driver iocb object.
 *
 * This function is called with no lock held to release the iocb to
 * iocb pool.
 **/
J
James Smart 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276
void
lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
{
	unsigned long iflags;

	/*
	 * Clean all volatile data fields, preserve iotag and node struct.
	 */
	spin_lock_irqsave(&phba->hbalock, iflags);
	__lpfc_sli_release_iocbq(phba, iocbq);
	spin_unlock_irqrestore(&phba->hbalock, iflags);
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/**
 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
 * @phba: Pointer to HBA context object.
 * @iocblist: List of IOCBs.
 * @ulpstatus: ULP status in IOCB command field.
 * @ulpWord4: ULP word-4 in IOCB command field.
 *
 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
 * on the list by invoking the complete callback function associated with the
 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
 * fields.
 **/
void
lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
		      uint32_t ulpstatus, uint32_t ulpWord4)
{
	struct lpfc_iocbq *piocb;

	while (!list_empty(iocblist)) {
		list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);

		if (!piocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, piocb);
		else {
			piocb->iocb.ulpStatus = ulpstatus;
			piocb->iocb.un.ulpWord[4] = ulpWord4;
			(piocb->iocb_cmpl) (phba, piocb, piocb);
		}
	}
	return;
}

309
/**
310 311
 * lpfc_sli_iocb_cmd_type - Get the iocb type
 * @iocb_cmnd: iocb command code.
312 313 314 315 316 317 318 319 320 321 322 323
 *
 * This function is called by ring event handler function to get the iocb type.
 * This function translates the iocb command to an iocb command type used to
 * decide the final disposition of each completed IOCB.
 * The function returns
 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
 * LPFC_SOL_IOCB     if it is a solicited iocb completion
 * LPFC_ABORT_IOCB   if it is an abort iocb
 * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
 *
 * The caller is not required to hold any lock.
 **/
已提交
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static lpfc_iocb_type
lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
{
	lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;

	if (iocb_cmnd > CMD_MAX_IOCB_CMD)
		return 0;

	switch (iocb_cmnd) {
	case CMD_XMIT_SEQUENCE_CR:
	case CMD_XMIT_SEQUENCE_CX:
	case CMD_XMIT_BCAST_CN:
	case CMD_XMIT_BCAST_CX:
	case CMD_ELS_REQUEST_CR:
	case CMD_ELS_REQUEST_CX:
	case CMD_CREATE_XRI_CR:
	case CMD_CREATE_XRI_CX:
	case CMD_GET_RPI_CN:
	case CMD_XMIT_ELS_RSP_CX:
	case CMD_GET_RPI_CR:
	case CMD_FCP_IWRITE_CR:
	case CMD_FCP_IWRITE_CX:
	case CMD_FCP_IREAD_CR:
	case CMD_FCP_IREAD_CX:
	case CMD_FCP_ICMND_CR:
	case CMD_FCP_ICMND_CX:
J
James Smart 已提交
350 351 352 353
	case CMD_FCP_TSEND_CX:
	case CMD_FCP_TRSP_CX:
	case CMD_FCP_TRECEIVE_CX:
	case CMD_FCP_AUTO_TRSP_CX:
已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367
	case CMD_ADAPTER_MSG:
	case CMD_ADAPTER_DUMP:
	case CMD_XMIT_SEQUENCE64_CR:
	case CMD_XMIT_SEQUENCE64_CX:
	case CMD_XMIT_BCAST64_CN:
	case CMD_XMIT_BCAST64_CX:
	case CMD_ELS_REQUEST64_CR:
	case CMD_ELS_REQUEST64_CX:
	case CMD_FCP_IWRITE64_CR:
	case CMD_FCP_IWRITE64_CX:
	case CMD_FCP_IREAD64_CR:
	case CMD_FCP_IREAD64_CX:
	case CMD_FCP_ICMND64_CR:
	case CMD_FCP_ICMND64_CX:
J
James Smart 已提交
368 369 370
	case CMD_FCP_TSEND64_CX:
	case CMD_FCP_TRSP64_CX:
	case CMD_FCP_TRECEIVE64_CX:
已提交
371 372 373
	case CMD_GEN_REQUEST64_CR:
	case CMD_GEN_REQUEST64_CX:
	case CMD_XMIT_ELS_RSP64_CX:
374 375 376 377 378 379 380 381
	case DSSCMD_IWRITE64_CR:
	case DSSCMD_IWRITE64_CX:
	case DSSCMD_IREAD64_CR:
	case DSSCMD_IREAD64_CX:
	case DSSCMD_INVALIDATE_DEK:
	case DSSCMD_SET_KEK:
	case DSSCMD_GET_KEK_ID:
	case DSSCMD_GEN_XFER:
已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395
		type = LPFC_SOL_IOCB;
		break;
	case CMD_ABORT_XRI_CN:
	case CMD_ABORT_XRI_CX:
	case CMD_CLOSE_XRI_CN:
	case CMD_CLOSE_XRI_CX:
	case CMD_XRI_ABORTED_CX:
	case CMD_ABORT_MXRI64_CN:
		type = LPFC_ABORT_IOCB;
		break;
	case CMD_RCV_SEQUENCE_CX:
	case CMD_RCV_ELS_REQ_CX:
	case CMD_RCV_SEQUENCE64_CX:
	case CMD_RCV_ELS_REQ64_CX:
396
	case CMD_ASYNC_STATUS:
397 398 399
	case CMD_IOCB_RCV_SEQ64_CX:
	case CMD_IOCB_RCV_ELS64_CX:
	case CMD_IOCB_RCV_CONT64_CX:
400
	case CMD_IOCB_RET_XRI64_CX:
已提交
401 402
		type = LPFC_UNSOL_IOCB;
		break;
403 404 405 406 407 408 409 410 411 412 413 414 415
	case CMD_IOCB_XMIT_MSEQ64_CR:
	case CMD_IOCB_XMIT_MSEQ64_CX:
	case CMD_IOCB_RCV_SEQ_LIST64_CX:
	case CMD_IOCB_RCV_ELS_LIST64_CX:
	case CMD_IOCB_CLOSE_EXTENDED_CN:
	case CMD_IOCB_ABORT_EXTENDED_CN:
	case CMD_IOCB_RET_HBQE64_CN:
	case CMD_IOCB_FCP_IBIDIR64_CR:
	case CMD_IOCB_FCP_IBIDIR64_CX:
	case CMD_IOCB_FCP_ITASKMGT64_CX:
	case CMD_IOCB_LOGENTRY_CN:
	case CMD_IOCB_LOGENTRY_ASYNC_CN:
		printk("%s - Unhandled SLI-3 Command x%x\n",
416
				__func__, iocb_cmnd);
417 418
		type = LPFC_UNKNOWN_IOCB;
		break;
已提交
419 420 421 422 423 424 425 426
	default:
		type = LPFC_UNKNOWN_IOCB;
		break;
	}

	return type;
}

427
/**
428
 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
429 430 431 432 433 434 435 436 437
 * @phba: Pointer to HBA context object.
 *
 * This function is called from SLI initialization code
 * to configure every ring of the HBA's SLI interface. The
 * caller is not required to hold any lock. This function issues
 * a config_ring mailbox command for each ring.
 * This function returns zero if successful else returns a negative
 * error code.
 **/
已提交
438
static int
439
lpfc_sli_ring_map(struct lpfc_hba *phba)
已提交
440 441
{
	struct lpfc_sli *psli = &phba->sli;
442 443 444
	LPFC_MBOXQ_t *pmb;
	MAILBOX_t *pmbox;
	int i, rc, ret = 0;
已提交
445

446 447 448 449 450
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmb)
		return -ENOMEM;
	pmbox = &pmb->mb;
	phba->link_state = LPFC_INIT_MBX_CMDS;
已提交
451 452 453 454
	for (i = 0; i < psli->num_rings; i++) {
		lpfc_config_ring(phba, i, pmb);
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
		if (rc != MBX_SUCCESS) {
455
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
456
					"0446 Adapter failed to init (%d), "
已提交
457 458
					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
					"ring %d\n",
459 460
					rc, pmbox->mbxCommand,
					pmbox->mbxStatus, i);
J
James Smart 已提交
461
			phba->link_state = LPFC_HBA_ERROR;
462 463
			ret = -ENXIO;
			break;
已提交
464 465
		}
	}
466 467
	mempool_free(pmb, phba->mbox_mem_pool);
	return ret;
已提交
468 469
}

470
/**
471
 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
472 473 474 475 476 477 478 479 480 481
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @piocb: Pointer to the driver iocb object.
 *
 * This function is called with hbalock held. The function adds the
 * new iocb to txcmplq of the given ring. This function always returns
 * 0. If this function is called for ELS ring, this function checks if
 * there is a vport associated with the ELS command. This function also
 * starts els_tmofunc timer if this is an ELS command.
 **/
已提交
482
static int
J
James Smart 已提交
483 484
lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			struct lpfc_iocbq *piocb)
已提交
485 486 487
{
	list_add_tail(&piocb->list, &pring->txcmplq);
	pring->txcmplq_cnt++;
488 489 490 491 492 493 494 495 496 497
	if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
		if (!piocb->vport)
			BUG();
		else
			mod_timer(&piocb->vport->els_tmofunc,
				  jiffies + HZ * (phba->fc_ratov << 1));
	}

已提交
498

J
James Smart 已提交
499
	return 0;
已提交
500 501
}

502
/**
503
 * lpfc_sli_ringtx_get - Get first element of the txq
504 505 506 507 508 509 510 511
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function is called with hbalock held to get next
 * iocb in txq of the given ring. If there is any iocb in
 * the txq, the function returns first iocb in the list after
 * removing the iocb from the list, else it returns NULL.
 **/
已提交
512
static struct lpfc_iocbq *
J
James Smart 已提交
513
lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
514 515 516
{
	struct lpfc_iocbq *cmd_iocb;

J
James Smart 已提交
517 518
	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
	if (cmd_iocb != NULL)
已提交
519
		pring->txq_cnt--;
J
James Smart 已提交
520
	return cmd_iocb;
已提交
521 522
}

523
/**
524
 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
525 526 527 528 529 530 531 532 533 534 535 536
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function is called with hbalock held and the caller must post the
 * iocb without releasing the lock. If the caller releases the lock,
 * iocb slot returned by the function is not guaranteed to be available.
 * The function returns pointer to the next available iocb slot if there
 * is available slot in the ring, else it returns NULL.
 * If the get index of the ring is ahead of the put index, the function
 * will post an error attention event to the worker thread to take the
 * HBA to offline state.
 **/
已提交
537 538 539
static IOCB_t *
lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
540
	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
已提交
541 542 543 544 545 546 547 548 549 550 551
	uint32_t  max_cmd_idx = pring->numCiocb;
	if ((pring->next_cmdidx == pring->cmdidx) &&
	   (++pring->next_cmdidx >= max_cmd_idx))
		pring->next_cmdidx = 0;

	if (unlikely(pring->local_getidx == pring->next_cmdidx)) {

		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);

		if (unlikely(pring->local_getidx >= max_cmd_idx)) {
			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
552
					"0315 Ring %d issue: portCmdGet %d "
553
					"is bigger than cmd ring %d\n",
554
					pring->ringno,
已提交
555 556
					pring->local_getidx, max_cmd_idx);

J
James Smart 已提交
557
			phba->link_state = LPFC_HBA_ERROR;
已提交
558 559 560 561 562 563
			/*
			 * All error attention handlers are posted to
			 * worker thread
			 */
			phba->work_ha |= HA_ERATT;
			phba->work_hs = HS_FFER3;
564

565
			lpfc_worker_wake_up(phba);
已提交
566 567 568 569 570 571 572 573

			return NULL;
		}

		if (pring->local_getidx == pring->next_cmdidx)
			return NULL;
	}

574
	return lpfc_cmd_iocb(phba, pring);
已提交
575 576
}

577
/**
578
 * lpfc_sli_next_iotag - Get an iotag for the iocb
579 580 581 582 583 584 585 586 587 588
 * @phba: Pointer to HBA context object.
 * @iocbq: Pointer to driver iocb object.
 *
 * This function gets an iotag for the iocb. If there is no unused iotag and
 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
 * array and assigns a new iotag.
 * The function returns the allocated iotag if successful, else returns zero.
 * Zero is not a valid iotag.
 * The caller is not required to hold any lock.
 **/
589
uint16_t
J
James Smart 已提交
590
lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
已提交
591
{
J
James Smart 已提交
592 593
	struct lpfc_iocbq **new_arr;
	struct lpfc_iocbq **old_arr;
594 595 596
	size_t new_len;
	struct lpfc_sli *psli = &phba->sli;
	uint16_t iotag;
已提交
597

J
James Smart 已提交
598
	spin_lock_irq(&phba->hbalock);
599 600 601 602
	iotag = psli->last_iotag;
	if(++iotag < psli->iocbq_lookup_len) {
		psli->last_iotag = iotag;
		psli->iocbq_lookup[iotag] = iocbq;
J
James Smart 已提交
603
		spin_unlock_irq(&phba->hbalock);
604 605
		iocbq->iotag = iotag;
		return iotag;
J
James Smart 已提交
606
	} else if (psli->iocbq_lookup_len < (0xffff
607 608
					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
J
James Smart 已提交
609 610
		spin_unlock_irq(&phba->hbalock);
		new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
611 612
				  GFP_KERNEL);
		if (new_arr) {
J
James Smart 已提交
613
			spin_lock_irq(&phba->hbalock);
614 615 616 617 618 619 620 621
			old_arr = psli->iocbq_lookup;
			if (new_len <= psli->iocbq_lookup_len) {
				/* highly unprobable case */
				kfree(new_arr);
				iotag = psli->last_iotag;
				if(++iotag < psli->iocbq_lookup_len) {
					psli->last_iotag = iotag;
					psli->iocbq_lookup[iotag] = iocbq;
J
James Smart 已提交
622
					spin_unlock_irq(&phba->hbalock);
623 624 625
					iocbq->iotag = iotag;
					return iotag;
				}
J
James Smart 已提交
626
				spin_unlock_irq(&phba->hbalock);
627 628 629 630 631
				return 0;
			}
			if (psli->iocbq_lookup)
				memcpy(new_arr, old_arr,
				       ((psli->last_iotag  + 1) *
J
James Smart 已提交
632
					sizeof (struct lpfc_iocbq *)));
633 634 635 636
			psli->iocbq_lookup = new_arr;
			psli->iocbq_lookup_len = new_len;
			psli->last_iotag = iotag;
			psli->iocbq_lookup[iotag] = iocbq;
J
James Smart 已提交
637
			spin_unlock_irq(&phba->hbalock);
638 639 640 641
			iocbq->iotag = iotag;
			kfree(old_arr);
			return iotag;
		}
642
	} else
J
James Smart 已提交
643
		spin_unlock_irq(&phba->hbalock);
已提交
644

645
	lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
646 647
			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
			psli->last_iotag);
已提交
648

649
	return 0;
已提交
650 651
}

652
/**
653
 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
654 655 656 657 658 659 660 661 662 663 664 665
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @iocb: Pointer to iocb slot in the ring.
 * @nextiocb: Pointer to driver iocb object which need to be
 *            posted to firmware.
 *
 * This function is called with hbalock held to post a new iocb to
 * the firmware. This function copies the new iocb to ring iocb slot and
 * updates the ring pointers. It adds the new iocb to txcmplq if there is
 * a completion call back for this iocb else the function will free the
 * iocb object.
 **/
已提交
666 667 668 669 670
static void
lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
{
	/*
671
	 * Set up an iotag
已提交
672
	 */
673
	nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
已提交
674

675

676 677 678 679 680 681 682 683
	if (pring->ringno == LPFC_ELS_RING) {
		lpfc_debugfs_slow_ring_trc(phba,
			"IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
			*(((uint32_t *) &nextiocb->iocb) + 4),
			*(((uint32_t *) &nextiocb->iocb) + 6),
			*(((uint32_t *) &nextiocb->iocb) + 7));
	}

已提交
684 685 686
	/*
	 * Issue iocb command to adapter
	 */
687
	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
已提交
688 689 690 691 692 693 694 695 696 697
	wmb();
	pring->stats.iocb_cmd++;

	/*
	 * If there is no completion routine to call, we can release the
	 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
	 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
	 */
	if (nextiocb->iocb_cmpl)
		lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
698
	else
J
James Smart 已提交
699
		__lpfc_sli_release_iocbq(phba, nextiocb);
已提交
700 701 702 703 704 705

	/*
	 * Let the HBA know what IOCB slot will be the next one the
	 * driver will put a command into.
	 */
	pring->cmdidx = pring->next_cmdidx;
706
	writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
已提交
707 708
}

709
/**
710
 * lpfc_sli_update_full_ring - Update the chip attention register
711 712 713 714 715 716 717 718 719 720
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * The caller is not required to hold any lock for calling this function.
 * This function updates the chip attention bits for the ring to inform firmware
 * that there are pending work to be done for this ring and requests an
 * interrupt when there is space available in the ring. This function is
 * called when the driver is unable to post more iocbs to the ring due
 * to unavailability of space in the ring.
 **/
已提交
721
static void
J
James Smart 已提交
722
lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
{
	int ringno = pring->ringno;

	pring->flag |= LPFC_CALL_RING_AVAILABLE;

	wmb();

	/*
	 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
	 * The HBA will tell us when an IOCB entry is available.
	 */
	writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
	readl(phba->CAregaddr); /* flush */

	pring->stats.iocb_cmd_full++;
}

740
/**
741
 * lpfc_sli_update_ring - Update chip attention register
742 743 744 745 746 747 748
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function updates the chip attention register bit for the
 * given ring to inform HBA that there is more work to be done
 * in this ring. The caller is not required to hold any lock.
 **/
已提交
749
static void
J
James Smart 已提交
750
lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
751 752 753 754 755 756
{
	int ringno = pring->ringno;

	/*
	 * Tell the HBA that there is work to do in this ring.
	 */
757 758 759 760 761
	if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
		wmb();
		writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */
	}
已提交
762 763
}

764
/**
765
 * lpfc_sli_resume_iocb - Process iocbs in the txq
766 767 768 769 770 771 772
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function is called with hbalock held to post pending iocbs
 * in the txq to the firmware. This function is called when driver
 * detects space available in the ring.
 **/
已提交
773
static void
J
James Smart 已提交
774
lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
775 776 777 778 779 780 781 782 783 784 785 786
{
	IOCB_t *iocb;
	struct lpfc_iocbq *nextiocb;

	/*
	 * Check to see if:
	 *  (a) there is anything on the txq to send
	 *  (b) link is up
	 *  (c) link attention events can be processed (fcp ring only)
	 *  (d) IOCB processing is not blocked by the outstanding mbox command.
	 */
	if (pring->txq_cnt &&
J
James Smart 已提交
787
	    lpfc_is_link_up(phba) &&
已提交
788
	    (pring->ringno != phba->sli.fcp_ring ||
789
	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803

		while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
		       (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
			lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);

		if (iocb)
			lpfc_sli_update_ring(phba, pring);
		else
			lpfc_sli_update_full_ring(phba, pring);
	}

	return;
}

804
/**
805
 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
806 807 808 809 810 811 812 813
 * @phba: Pointer to HBA context object.
 * @hbqno: HBQ number.
 *
 * This function is called with hbalock held to get the next
 * available slot for the given HBQ. If there is free slot
 * available for the HBQ it will return pointer to the next available
 * HBQ entry else it will return NULL.
 **/
A
Adrian Bunk 已提交
814
static struct lpfc_hbq_entry *
815 816 817 818 819 820 821 822 823
lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
{
	struct hbq_s *hbqp = &phba->hbqs[hbqno];

	if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
	    ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
		hbqp->next_hbqPutIdx = 0;

	if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
824
		uint32_t raw_index = phba->hbq_get[hbqno];
825 826 827 828 829 830
		uint32_t getidx = le32_to_cpu(raw_index);

		hbqp->local_hbqGetIdx = getidx;

		if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
			lpfc_printf_log(phba, KERN_ERR,
831
					LOG_SLI | LOG_VPORT,
832
					"1802 HBQ %d: local_hbqGetIdx "
833
					"%u is > than hbqp->entry_count %u\n",
834
					hbqno, hbqp->local_hbqGetIdx,
835 836 837 838 839 840 841 842 843 844
					hbqp->entry_count);

			phba->link_state = LPFC_HBA_ERROR;
			return NULL;
		}

		if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
			return NULL;
	}

845 846
	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
			hbqp->hbqPutIdx;
847 848
}

849
/**
850
 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
851 852 853 854 855 856 857
 * @phba: Pointer to HBA context object.
 *
 * This function is called with no lock held to free all the
 * hbq buffers while uninitializing the SLI interface. It also
 * frees the HBQ buffers returned by the firmware but not yet
 * processed by the upper layers.
 **/
858 859 860
void
lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
{
861 862
	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
	struct hbq_dmabuf *hbq_buf;
863
	unsigned long flags;
864
	int i, hbq_count;
865
	uint32_t hbqno;
866

867
	hbq_count = lpfc_sli_hbq_count();
868
	/* Return all memory used by all HBQs */
869
	spin_lock_irqsave(&phba->hbalock, flags);
870 871 872 873 874 875 876
	for (i = 0; i < hbq_count; ++i) {
		list_for_each_entry_safe(dmabuf, next_dmabuf,
				&phba->hbqs[i].hbq_buffer_list, list) {
			hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
			list_del(&hbq_buf->dbuf.list);
			(phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
		}
877
		phba->hbqs[i].buffer_count = 0;
878
	}
879
	/* Return all HBQ buffer that are in-fly */
880 881
	list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
				 list) {
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
		hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
		list_del(&hbq_buf->dbuf.list);
		if (hbq_buf->tag == -1) {
			(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
				(phba, hbq_buf);
		} else {
			hbqno = hbq_buf->tag >> 16;
			if (hbqno >= LPFC_MAX_HBQS)
				(phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
					(phba, hbq_buf);
			else
				(phba->hbqs[hbqno].hbq_free_buffer)(phba,
					hbq_buf);
		}
	}

	/* Mark the HBQs not in use */
	phba->hbq_in_use = 0;
	spin_unlock_irqrestore(&phba->hbalock, flags);
901 902
}

903
/**
904
 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
905 906 907 908 909 910 911 912 913 914
 * @phba: Pointer to HBA context object.
 * @hbqno: HBQ number.
 * @hbq_buf: Pointer to HBQ buffer.
 *
 * This function is called with the hbalock held to post a
 * hbq buffer to the firmware. If the function finds an empty
 * slot in the HBQ, it will post the buffer. The function will return
 * pointer to the hbq entry if it successfully post the buffer
 * else it will return NULL.
 **/
915
static int
916
lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
917
			 struct hbq_dmabuf *hbq_buf)
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
{
	return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
}

/**
 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
 * @phba: Pointer to HBA context object.
 * @hbqno: HBQ number.
 * @hbq_buf: Pointer to HBQ buffer.
 *
 * This function is called with the hbalock held to post a hbq buffer to the
 * firmware. If the function finds an empty slot in the HBQ, it will post the
 * buffer and place it on the hbq_buffer_list. The function will return zero if
 * it successfully post the buffer else it will return an error.
 **/
static int
lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
			    struct hbq_dmabuf *hbq_buf)
936 937
{
	struct lpfc_hbq_entry *hbqe;
938
	dma_addr_t physaddr = hbq_buf->dbuf.phys;
939 940 941 942 943 944

	/* Get next HBQ entry slot to use */
	hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
	if (hbqe) {
		struct hbq_s *hbqp = &phba->hbqs[hbqno];

945 946
		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
947
		hbqe->bde.tus.f.bdeSize = hbq_buf->size;
948
		hbqe->bde.tus.f.bdeFlags = 0;
949 950 951
		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
				/* Sync SLIM */
952 953
		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
954
				/* flush */
955
		readl(phba->hbq_put + hbqno);
956
		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
957 958 959
		return 0;
	} else
		return -ENOMEM;
960 961
}

962
/* HBQ for ELS and CT traffic. */
963 964 965 966 967
static struct lpfc_hbq_init lpfc_els_hbq = {
	.rn = 1,
	.entry_count = 200,
	.mask_count = 0,
	.profile = 0,
968
	.ring_mask = (1 << LPFC_ELS_RING),
969
	.buffer_count = 0,
970 971
	.init_count = 40,
	.add_count = 40,
972
};
973

974
/* HBQ for the extra ring if needed */
975 976 977 978 979 980 981 982 983 984 985
static struct lpfc_hbq_init lpfc_extra_hbq = {
	.rn = 1,
	.entry_count = 200,
	.mask_count = 0,
	.profile = 0,
	.ring_mask = (1 << LPFC_EXTRA_RING),
	.buffer_count = 0,
	.init_count = 0,
	.add_count = 5,
};

986
/* Array of HBQs */
987
struct lpfc_hbq_init *lpfc_hbq_defs[] = {
988
	&lpfc_els_hbq,
989
	&lpfc_extra_hbq,
990
};
991

992
/**
993
 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
994 995 996 997
 * @phba: Pointer to HBA context object.
 * @hbqno: HBQ number.
 * @count: Number of HBQ buffers to be posted.
 *
998 999 1000
 * This function is called with no lock held to post more hbq buffers to the
 * given HBQ. The function returns the number of HBQ buffers successfully
 * posted.
1001
 **/
J
James Smart 已提交
1002
static int
1003
lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1004
{
1005
	uint32_t i, posted = 0;
1006
	unsigned long flags;
1007
	struct hbq_dmabuf *hbq_buffer;
1008
	LIST_HEAD(hbq_buf_list);
M
Matthew Wilcox 已提交
1009
	if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1010 1011
		return 0;

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	if ((phba->hbqs[hbqno].buffer_count + count) >
	    lpfc_hbq_defs[hbqno]->entry_count)
		count = lpfc_hbq_defs[hbqno]->entry_count -
					phba->hbqs[hbqno].buffer_count;
	if (!count)
		return 0;
	/* Allocate HBQ entries */
	for (i = 0; i < count; i++) {
		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
		if (!hbq_buffer)
			break;
		list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
	}
1025 1026
	/* Check whether HBQ is still in use */
	spin_lock_irqsave(&phba->hbalock, flags);
M
Matthew Wilcox 已提交
1027
	if (!phba->hbq_in_use)
1028 1029 1030 1031 1032 1033
		goto err;
	while (!list_empty(&hbq_buf_list)) {
		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
				 dbuf.list);
		hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
				      (hbqno << 16));
1034
		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1035
			phba->hbqs[hbqno].buffer_count++;
1036 1037
			posted++;
		} else
1038
			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1039
	}
1040
	spin_unlock_irqrestore(&phba->hbalock, flags);
1041 1042
	return posted;
err:
M
Matthew Wilcox 已提交
1043
	spin_unlock_irqrestore(&phba->hbalock, flags);
1044 1045 1046 1047 1048 1049
	while (!list_empty(&hbq_buf_list)) {
		list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
				 dbuf.list);
		(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
	}
	return 0;
1050 1051
}

1052
/**
1053
 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1054 1055 1056 1057
 * @phba: Pointer to HBA context object.
 * @qno: HBQ number.
 *
 * This function posts more buffers to the HBQ. This function
1058 1059
 * is called with no lock held. The function returns the number of HBQ entries
 * successfully allocated.
1060
 **/
1061 1062
int
lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1063
{
1064 1065 1066
	return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
					 lpfc_hbq_defs[qno]->add_count));
}
1067

1068
/**
1069
 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1070 1071 1072 1073 1074
 * @phba: Pointer to HBA context object.
 * @qno:  HBQ queue number.
 *
 * This function is called from SLI initialization code path with
 * no lock held to post initial HBQ buffers to firmware. The
1075
 * function returns the number of HBQ entries successfully allocated.
1076
 **/
A
Adrian Bunk 已提交
1077
static int
1078 1079 1080 1081
lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
{
	return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
					 lpfc_hbq_defs[qno]->init_count));
1082 1083
}

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
/**
 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
 * @phba: Pointer to HBA context object.
 * @hbqno: HBQ number.
 *
 * This function removes the first hbq buffer on an hbq list and returns a
 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
 **/
static struct hbq_dmabuf *
lpfc_sli_hbqbuf_get(struct list_head *rb_list)
{
	struct lpfc_dmabuf *d_buf;

	list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
	if (!d_buf)
		return NULL;
	return container_of(d_buf, struct hbq_dmabuf, dbuf);
}

1103
/**
1104
 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1105 1106 1107 1108 1109 1110 1111 1112
 * @phba: Pointer to HBA context object.
 * @tag: Tag of the hbq buffer.
 *
 * This function is called with hbalock held. This function searches
 * for the hbq buffer associated with the given tag in the hbq buffer
 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
 * it returns NULL.
 **/
A
Adrian Bunk 已提交
1113
static struct hbq_dmabuf *
1114
lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1115
{
1116 1117
	struct lpfc_dmabuf *d_buf;
	struct hbq_dmabuf *hbq_buf;
1118 1119 1120
	uint32_t hbqno;

	hbqno = tag >> 16;
1121
	if (hbqno >= LPFC_MAX_HBQS)
1122
		return NULL;
1123

1124
	spin_lock_irq(&phba->hbalock);
1125
	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1126
		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1127
		if (hbq_buf->tag == tag) {
1128
			spin_unlock_irq(&phba->hbalock);
1129
			return hbq_buf;
1130 1131
		}
	}
1132
	spin_unlock_irq(&phba->hbalock);
1133
	lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1134
			"1803 Bad hbq tag. Data: x%x x%x\n",
1135
			tag, phba->hbqs[tag >> 16].buffer_count);
1136
	return NULL;
1137 1138
}

1139
/**
1140
 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1141 1142 1143 1144 1145 1146 1147
 * @phba: Pointer to HBA context object.
 * @hbq_buffer: Pointer to HBQ buffer.
 *
 * This function is called with hbalock. This function gives back
 * the hbq buffer to firmware. If the HBQ does not have space to
 * post the buffer, it will free the buffer.
 **/
1148
void
1149
lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1150 1151 1152
{
	uint32_t hbqno;

1153 1154
	if (hbq_buffer) {
		hbqno = hbq_buffer->tag >> 16;
1155
		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1156
			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1157 1158 1159
	}
}

1160
/**
1161
 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1162 1163 1164 1165 1166 1167 1168
 * @mbxCommand: mailbox command code.
 *
 * This function is called by the mailbox event handler function to verify
 * that the completed mailbox command is a legitimate mailbox command. If the
 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
 * and the mailbox event handler will take the HBA offline.
 **/
已提交
1169 1170 1171 1172 1173 1174 1175 1176 1177
static int
lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
{
	uint8_t ret;

	switch (mbxCommand) {
	case MBX_LOAD_SM:
	case MBX_READ_NV:
	case MBX_WRITE_NV:
1178
	case MBX_WRITE_VPARMS:
已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
	case MBX_RUN_BIU_DIAG:
	case MBX_INIT_LINK:
	case MBX_DOWN_LINK:
	case MBX_CONFIG_LINK:
	case MBX_CONFIG_RING:
	case MBX_RESET_RING:
	case MBX_READ_CONFIG:
	case MBX_READ_RCONFIG:
	case MBX_READ_SPARM:
	case MBX_READ_STATUS:
	case MBX_READ_RPI:
	case MBX_READ_XRI:
	case MBX_READ_REV:
	case MBX_READ_LNK_STAT:
	case MBX_REG_LOGIN:
	case MBX_UNREG_LOGIN:
	case MBX_READ_LA:
	case MBX_CLEAR_LA:
	case MBX_DUMP_MEMORY:
	case MBX_DUMP_CONTEXT:
	case MBX_RUN_DIAGS:
	case MBX_RESTART:
	case MBX_UPDATE_CFG:
	case MBX_DOWN_LOAD:
	case MBX_DEL_LD_ENTRY:
	case MBX_RUN_PROGRAM:
	case MBX_SET_MASK:
1206
	case MBX_SET_VARIABLE:
已提交
1207
	case MBX_UNREG_D_ID:
1208
	case MBX_KILL_BOARD:
已提交
1209
	case MBX_CONFIG_FARP:
1210
	case MBX_BEACON:
已提交
1211 1212 1213 1214 1215 1216 1217
	case MBX_LOAD_AREA:
	case MBX_RUN_BIU_DIAG64:
	case MBX_CONFIG_PORT:
	case MBX_READ_SPARM64:
	case MBX_READ_RPI64:
	case MBX_REG_LOGIN64:
	case MBX_READ_LA64:
1218
	case MBX_WRITE_WWN:
已提交
1219 1220
	case MBX_SET_DEBUG:
	case MBX_LOAD_EXP_ROM:
1221
	case MBX_ASYNCEVT_ENABLE:
1222 1223
	case MBX_REG_VPI:
	case MBX_UNREG_VPI:
J
James Smart 已提交
1224
	case MBX_HEARTBEAT:
1225 1226
	case MBX_PORT_CAPABILITIES:
	case MBX_PORT_IOV_CONTROL:
已提交
1227 1228 1229 1230 1231 1232
		ret = mbxCommand;
		break;
	default:
		ret = MBX_SHUTDOWN;
		break;
	}
J
James Smart 已提交
1233
	return ret;
已提交
1234
}
1235 1236

/**
1237
 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1238 1239 1240 1241 1242 1243 1244 1245 1246
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to mailbox command.
 *
 * This is completion handler function for mailbox commands issued from
 * lpfc_sli_issue_mbox_wait function. This function is called by the
 * mailbox event handler function with no lock held. This function
 * will wake up thread waiting on the wait queue pointed by context1
 * of the mailbox.
 **/
已提交
1247
static void
J
James Smart 已提交
1248
lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
已提交
1249 1250
{
	wait_queue_head_t *pdone_q;
J
James Smart 已提交
1251
	unsigned long drvr_flag;
已提交
1252 1253 1254 1255 1256

	/*
	 * If pdone_q is empty, the driver thread gave up waiting and
	 * continued running.
	 */
1257
	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
J
James Smart 已提交
1258
	spin_lock_irqsave(&phba->hbalock, drvr_flag);
已提交
1259 1260 1261
	pdone_q = (wait_queue_head_t *) pmboxq->context1;
	if (pdone_q)
		wake_up_interruptible(pdone_q);
J
James Smart 已提交
1262
	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
1263 1264 1265
	return;
}

1266 1267

/**
1268
 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1269 1270 1271 1272 1273 1274 1275 1276
 * @phba: Pointer to HBA context object.
 * @pmb: Pointer to mailbox object.
 *
 * This function is the default mailbox completion handler. It
 * frees the memory resources associated with the completed mailbox
 * command. If the completed command is a REG_LOGIN mailbox command,
 * this function will issue a UREG_LOGIN to re-claim the RPI.
 **/
已提交
1277
void
J
James Smart 已提交
1278
lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
已提交
1279 1280
{
	struct lpfc_dmabuf *mp;
1281 1282 1283
	uint16_t rpi;
	int rc;

已提交
1284
	mp = (struct lpfc_dmabuf *) (pmb->context1);
1285

已提交
1286 1287 1288 1289
	if (mp) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
	}
1290 1291 1292 1293 1294

	/*
	 * If a REG_LOGIN succeeded  after node is destroyed or node
	 * is in re-discovery driver need to cleanup the RPI.
	 */
J
James Smart 已提交
1295 1296 1297
	if (!(phba->pport->load_flag & FC_UNLOADING) &&
	    pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
	    !pmb->mb.mbxStatus) {
1298 1299

		rpi = pmb->mb.un.varWords[0];
1300 1301
		lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1302 1303 1304 1305 1306
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc != MBX_NOT_FINISHED)
			return;
	}

J
James Smart 已提交
1307
	mempool_free(pmb, phba->mbox_mem_pool);
已提交
1308 1309 1310
	return;
}

1311
/**
1312
 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
 * @phba: Pointer to HBA context object.
 *
 * This function is called with no lock held. This function processes all
 * the completed mailbox commands and gives it to upper layers. The interrupt
 * service routine processes mailbox completion interrupt and adds completed
 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
 * Worker thread call lpfc_sli_handle_mb_event, which will return the
 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
 * function returns the mailbox commands to the upper layer by calling the
 * completion handler function of each mailbox.
 **/
已提交
1324
int
J
James Smart 已提交
1325
lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
已提交
1326
{
1327
	MAILBOX_t *pmbox;
已提交
1328
	LPFC_MBOXQ_t *pmb;
1329 1330
	int rc;
	LIST_HEAD(cmplq);
已提交
1331 1332 1333

	phba->sli.slistat.mbox_event++;

1334 1335 1336 1337
	/* Get all completed mailboxe buffers into the cmplq */
	spin_lock_irq(&phba->hbalock);
	list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
	spin_unlock_irq(&phba->hbalock);
已提交
1338

1339 1340 1341 1342 1343
	/* Get a Mailbox buffer to setup mailbox commands for callback */
	do {
		list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
		if (pmb == NULL)
			break;
J
James Smart 已提交
1344

1345
		pmbox = &pmb->mb;
已提交
1346

J
James Smart 已提交
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		if (pmbox->mbxCommand != MBX_HEARTBEAT) {
			if (pmb->vport) {
				lpfc_debugfs_disc_trc(pmb->vport,
					LPFC_DISC_TRC_MBOX_VPORT,
					"MBOX cmpl vport: cmd:x%x mb:x%x x%x",
					(uint32_t)pmbox->mbxCommand,
					pmbox->un.varWords[0],
					pmbox->un.varWords[1]);
			}
			else {
				lpfc_debugfs_disc_trc(phba->pport,
					LPFC_DISC_TRC_MBOX,
					"MBOX cmpl:       cmd:x%x mb:x%x x%x",
					(uint32_t)pmbox->mbxCommand,
					pmbox->un.varWords[0],
					pmbox->un.varWords[1]);
			}
		}

已提交
1366 1367 1368 1369 1370 1371
		/*
		 * It is a fatal error if unknown mbox command completion.
		 */
		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
		    MBX_SHUTDOWN) {
			/* Unknow mailbox command compl */
1372
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1373
					"(%d):0323 Unknown Mailbox command "
1374 1375 1376
					"%x Cmpl\n",
					pmb->vport ? pmb->vport->vpi : 0,
					pmbox->mbxCommand);
J
James Smart 已提交
1377
			phba->link_state = LPFC_HBA_ERROR;
已提交
1378 1379
			phba->work_hs = HS_FFER3;
			lpfc_handle_eratt(phba);
1380
			continue;
已提交
1381 1382 1383 1384 1385 1386
		}

		if (pmbox->mbxStatus) {
			phba->sli.slistat.mbox_stat_err++;
			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
				/* Mbox cmd cmpl error - RETRYing */
1387 1388
				lpfc_printf_log(phba, KERN_INFO,
						LOG_MBOX | LOG_SLI,
1389
						"(%d):0305 Mbox cmd cmpl "
1390 1391 1392 1393 1394 1395 1396
						"error - RETRYing Data: x%x "
						"x%x x%x x%x\n",
						pmb->vport ? pmb->vport->vpi :0,
						pmbox->mbxCommand,
						pmbox->mbxStatus,
						pmbox->un.varWords[0],
						pmb->vport->port_state);
已提交
1397 1398
				pmbox->mbxStatus = 0;
				pmbox->mbxOwner = OWN_HOST;
J
James Smart 已提交
1399
				spin_lock_irq(&phba->hbalock);
已提交
1400
				phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
1401
				spin_unlock_irq(&phba->hbalock);
已提交
1402 1403
				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
				if (rc == MBX_SUCCESS)
1404
					continue;
已提交
1405 1406 1407 1408
			}
		}

		/* Mailbox cmd <cmd> Cmpl <cmpl> */
1409
		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1410
				"(%d):0307 Mailbox cmd x%x Cmpl x%p "
已提交
1411
				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1412
				pmb->vport ? pmb->vport->vpi : 0,
已提交
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
				pmbox->mbxCommand,
				pmb->mbox_cmpl,
				*((uint32_t *) pmbox),
				pmbox->un.varWords[0],
				pmbox->un.varWords[1],
				pmbox->un.varWords[2],
				pmbox->un.varWords[3],
				pmbox->un.varWords[4],
				pmbox->un.varWords[5],
				pmbox->un.varWords[6],
				pmbox->un.varWords[7]);

1425
		if (pmb->mbox_cmpl)
已提交
1426
			pmb->mbox_cmpl(phba,pmb);
1427 1428 1429
	} while (1);
	return 0;
}
已提交
1430

1431
/**
1432
 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @tag: buffer tag.
 *
 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
 * is set in the tag the buffer is posted for a particular exchange,
 * the function will return the buffer without replacing the buffer.
 * If the buffer is for unsolicited ELS or CT traffic, this function
 * returns the buffer and also posts another buffer to the firmware.
 **/
1443 1444
static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba *phba,
1445 1446
		  struct lpfc_sli_ring *pring,
		  uint32_t tag)
1447
{
1448 1449
	struct hbq_dmabuf *hbq_entry;

1450 1451
	if (tag & QUE_BUFTAG_BIT)
		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1452 1453 1454 1455
	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
	if (!hbq_entry)
		return NULL;
	return &hbq_entry->dbuf;
1456
}
1457

1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
/**
 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
 * @fch_type: the type for the first frame of the sequence.
 *
 * This function is called with no lock held. This function uses the r_ctl and
 * type of the received sequence to find the correct callback function to call
 * to process the sequence.
 **/
static int
lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
			 uint32_t fch_type)
{
	int i;

	/* unSolicited Responses */
	if (pring->prt[0].profile) {
		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
									saveq);
		return 1;
	}
	/* We must search, based on rctl / type
	   for the right routine */
	for (i = 0; i < pring->num_mask; i++) {
		if ((pring->prt[i].rctl == fch_r_ctl) &&
		    (pring->prt[i].type == fch_type)) {
			if (pring->prt[i].lpfc_sli_rcv_unsol_event)
				(pring->prt[i].lpfc_sli_rcv_unsol_event)
						(phba, pring, saveq);
			return 1;
		}
	}
	return 0;
}
1497 1498

/**
1499
 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @saveq: Pointer to the unsolicited iocb.
 *
 * This function is called with no lock held by the ring event handler
 * when there is an unsolicited iocb posted to the response ring by the
 * firmware. This function gets the buffer associated with the iocbs
 * and calls the event handler for the ring. This function handles both
 * qring buffers and hbq buffers.
 * When the function returns 1 the caller can free the iocb object otherwise
 * upper layer functions will free the iocb objects.
 **/
已提交
1512 1513 1514 1515 1516 1517 1518
static int
lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			    struct lpfc_iocbq *saveq)
{
	IOCB_t           * irsp;
	WORD5            * w5p;
	uint32_t           Rctl, Type;
1519
	uint32_t           match;
1520
	struct lpfc_iocbq *iocbq;
1521
	struct lpfc_dmabuf *dmzbuf;
已提交
1522 1523 1524

	match = 0;
	irsp = &(saveq->iocb);
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540

	if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
		if (pring->lpfc_sli_rcv_async_status)
			pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
		else
			lpfc_printf_log(phba,
					KERN_WARNING,
					LOG_SLI,
					"0316 Ring %d handler: unexpected "
					"ASYNC_STATUS iocb received evt_code "
					"0x%x\n",
					pring->ringno,
					irsp->un.asyncstat.evt_code);
		return 1;
	}

1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
	if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
		(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
		if (irsp->ulpBdeCount > 0) {
			dmzbuf = lpfc_sli_get_buff(phba, pring,
					irsp->un.ulpWord[3]);
			lpfc_in_buf_free(phba, dmzbuf);
		}

		if (irsp->ulpBdeCount > 1) {
			dmzbuf = lpfc_sli_get_buff(phba, pring,
					irsp->unsli3.sli3Words[3]);
			lpfc_in_buf_free(phba, dmzbuf);
		}

		if (irsp->ulpBdeCount > 2) {
			dmzbuf = lpfc_sli_get_buff(phba, pring,
				irsp->unsli3.sli3Words[7]);
			lpfc_in_buf_free(phba, dmzbuf);
		}

		return 1;
	}

1564
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
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
		if (irsp->ulpBdeCount != 0) {
			saveq->context2 = lpfc_sli_get_buff(phba, pring,
						irsp->un.ulpWord[3]);
			if (!saveq->context2)
				lpfc_printf_log(phba,
					KERN_ERR,
					LOG_SLI,
					"0341 Ring %d Cannot find buffer for "
					"an unsolicited iocb. tag 0x%x\n",
					pring->ringno,
					irsp->un.ulpWord[3]);
		}
		if (irsp->ulpBdeCount == 2) {
			saveq->context3 = lpfc_sli_get_buff(phba, pring,
						irsp->unsli3.sli3Words[7]);
			if (!saveq->context3)
				lpfc_printf_log(phba,
					KERN_ERR,
					LOG_SLI,
					"0342 Ring %d Cannot find buffer for an"
					" unsolicited iocb. tag 0x%x\n",
					pring->ringno,
					irsp->unsli3.sli3Words[7]);
		}
		list_for_each_entry(iocbq, &saveq->list, list) {
			irsp = &(iocbq->iocb);
			if (irsp->ulpBdeCount != 0) {
				iocbq->context2 = lpfc_sli_get_buff(phba, pring,
							irsp->un.ulpWord[3]);
1594
				if (!iocbq->context2)
1595 1596 1597 1598 1599 1600
					lpfc_printf_log(phba,
						KERN_ERR,
						LOG_SLI,
						"0343 Ring %d Cannot find "
						"buffer for an unsolicited iocb"
						". tag 0x%x\n", pring->ringno,
1601
						irsp->un.ulpWord[3]);
1602 1603 1604
			}
			if (irsp->ulpBdeCount == 2) {
				iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1605
						irsp->unsli3.sli3Words[7]);
1606
				if (!iocbq->context3)
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616
					lpfc_printf_log(phba,
						KERN_ERR,
						LOG_SLI,
						"0344 Ring %d Cannot find "
						"buffer for an unsolicited "
						"iocb. tag 0x%x\n",
						pring->ringno,
						irsp->unsli3.sli3Words[7]);
			}
		}
1617
	}
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
	if (irsp->ulpBdeCount != 0 &&
	    (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
	     irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
		int found = 0;

		/* search continue save q for same XRI */
		list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
			if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
				list_add_tail(&saveq->list, &iocbq->list);
				found = 1;
				break;
			}
		}
		if (!found)
			list_add_tail(&saveq->clist,
				      &pring->iocb_continue_saveq);
		if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
			list_del_init(&iocbq->clist);
			saveq = iocbq;
			irsp = &(saveq->iocb);
		} else
			return 0;
	}
	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
	    (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
	    (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
		Rctl = FC_ELS_REQ;
		Type = FC_ELS_DATA;
	} else {
		w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
		Rctl = w5p->hcsw.Rctl;
		Type = w5p->hcsw.Type;

		/* Firmware Workaround */
		if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
			Rctl = FC_ELS_REQ;
			Type = FC_ELS_DATA;
			w5p->hcsw.Rctl = Rctl;
			w5p->hcsw.Type = Type;
		}
	}
1661

1662
	if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
1663
		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1664
				"0313 Ring %d handler: unexpected Rctl x%x "
1665
				"Type x%x received\n",
1666
				pring->ringno, Rctl, Type);
1667

1668
	return 1;
已提交
1669 1670
}

1671
/**
1672
 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @prspiocb: Pointer to response iocb object.
 *
 * This function looks up the iocb_lookup table to get the command iocb
 * corresponding to the given response iocb using the iotag of the
 * response iocb. This function is called with the hbalock held.
 * This function returns the command iocb object if it finds the command
 * iocb else returns NULL.
 **/
已提交
1683
static struct lpfc_iocbq *
J
James Smart 已提交
1684 1685 1686
lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
		      struct lpfc_sli_ring *pring,
		      struct lpfc_iocbq *prspiocb)
已提交
1687 1688 1689 1690
{
	struct lpfc_iocbq *cmd_iocb = NULL;
	uint16_t iotag;

1691 1692 1693 1694
	iotag = prspiocb->iocb.ulpIoTag;

	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
		cmd_iocb = phba->sli.iocbq_lookup[iotag];
1695
		list_del_init(&cmd_iocb->list);
1696 1697
		pring->txcmplq_cnt--;
		return cmd_iocb;
已提交
1698 1699 1700
	}

	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1701
			"0317 iotag x%x is out off "
1702
			"range: max iotag x%x wd0 x%x\n",
1703
			iotag, phba->sli.last_iotag,
1704
			*(((uint32_t *) &prspiocb->iocb) + 7));
已提交
1705 1706 1707
	return NULL;
}

1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738
/**
 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @iotag: IOCB tag.
 *
 * This function looks up the iocb_lookup table to get the command iocb
 * corresponding to the given iotag. This function is called with the
 * hbalock held.
 * This function returns the command iocb object if it finds the command
 * iocb else returns NULL.
 **/
static struct lpfc_iocbq *
lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
			     struct lpfc_sli_ring *pring, uint16_t iotag)
{
	struct lpfc_iocbq *cmd_iocb;

	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
		cmd_iocb = phba->sli.iocbq_lookup[iotag];
		list_del_init(&cmd_iocb->list);
		pring->txcmplq_cnt--;
		return cmd_iocb;
	}

	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
			"0372 iotag x%x is out off range: max iotag (x%x)\n",
			iotag, phba->sli.last_iotag);
	return NULL;
}

1739
/**
1740
 * lpfc_sli_process_sol_iocb - process solicited iocb completion
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @saveq: Pointer to the response iocb to be processed.
 *
 * This function is called by the ring event handler for non-fcp
 * rings when there is a new response iocb in the response ring.
 * The caller is not required to hold any locks. This function
 * gets the command iocb associated with the response iocb and
 * calls the completion handler for the command iocb. If there
 * is no completion handler, the function will free the resources
 * associated with command iocb. If the response iocb is for
 * an already aborted command iocb, the status of the completion
 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
 * This function always returns 1.
 **/
已提交
1756
static int
J
James Smart 已提交
1757
lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
已提交
1758 1759
			  struct lpfc_iocbq *saveq)
{
J
James Smart 已提交
1760
	struct lpfc_iocbq *cmdiocbp;
已提交
1761 1762 1763 1764
	int rc = 1;
	unsigned long iflag;

	/* Based on the iotag field, get the cmd IOCB from the txcmplq */
J
James Smart 已提交
1765
	spin_lock_irqsave(&phba->hbalock, iflag);
1766
	cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
J
James Smart 已提交
1767 1768
	spin_unlock_irqrestore(&phba->hbalock, iflag);

已提交
1769 1770
	if (cmdiocbp) {
		if (cmdiocbp->iocb_cmpl) {
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			/*
			 * If an ELS command failed send an event to mgmt
			 * application.
			 */
			if (saveq->iocb.ulpStatus &&
			     (pring->ringno == LPFC_ELS_RING) &&
			     (cmdiocbp->iocb.ulpCommand ==
				CMD_ELS_REQUEST64_CR))
				lpfc_send_els_failure_event(phba,
					cmdiocbp, saveq);

已提交
1782 1783 1784 1785 1786
			/*
			 * Post all ELS completions to the worker thread.
			 * All other are passed to the completion callback.
			 */
			if (pring->ringno == LPFC_ELS_RING) {
1787 1788 1789 1790 1791 1792 1793
				if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
					cmdiocbp->iocb_flag &=
						~LPFC_DRIVER_ABORTED;
					saveq->iocb.ulpStatus =
						IOSTAT_LOCAL_REJECT;
					saveq->iocb.un.ulpWord[4] =
						IOERR_SLI_ABORTED;
1794 1795 1796 1797 1798 1799

					/* Firmware could still be in progress
					 * of DMAing payload, so don't free data
					 * buffer till after a hbeat.
					 */
					saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
1800
				}
已提交
1801
			}
J
James Smart 已提交
1802
			(cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1803 1804
		} else
			lpfc_sli_release_iocbq(phba, cmdiocbp);
已提交
1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
	} else {
		/*
		 * Unknown initiating command based on the response iotag.
		 * This could be the case on the ELS ring because of
		 * lpfc_els_abort().
		 */
		if (pring->ringno != LPFC_ELS_RING) {
			/*
			 * Ring <ringno> handler: unexpected completion IoTag
			 * <IoTag>
			 */
1816
			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1817 1818 1819 1820 1821 1822 1823 1824 1825
					 "0322 Ring %d handler: "
					 "unexpected completion IoTag x%x "
					 "Data: x%x x%x x%x x%x\n",
					 pring->ringno,
					 saveq->iocb.ulpIoTag,
					 saveq->iocb.ulpStatus,
					 saveq->iocb.un.ulpWord[4],
					 saveq->iocb.ulpCommand,
					 saveq->iocb.ulpContext);
已提交
1826 1827
		}
	}
1828

已提交
1829 1830 1831
	return rc;
}

1832
/**
1833
 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
1834 1835 1836 1837 1838 1839 1840 1841
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function is called from the iocb ring event handlers when
 * put pointer is ahead of the get pointer for a ring. This function signal
 * an error attention condition to the worker thread and the worker
 * thread will transition the HBA to offline state.
 **/
J
James Smart 已提交
1842 1843
static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1844
{
1845
	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1846
	/*
1847
	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
1848 1849 1850
	 * rsp ring <portRspMax>
	 */
	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1851
			"0312 Ring %d handler: portRspPut %d "
1852
			"is bigger than rsp ring %d\n",
1853
			pring->ringno, le32_to_cpu(pgp->rspPutInx),
1854 1855
			pring->numRiocb);

J
James Smart 已提交
1856
	phba->link_state = LPFC_HBA_ERROR;
1857 1858 1859 1860 1861 1862 1863

	/*
	 * All error attention handlers are posted to
	 * worker thread
	 */
	phba->work_ha |= HA_ERATT;
	phba->work_hs = HS_FFER3;
1864

1865
	lpfc_worker_wake_up(phba);
1866 1867 1868 1869

	return;
}

1870
/**
1871
 * lpfc_poll_eratt - Error attention polling timer timeout handler
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
 * @ptr: Pointer to address of HBA context object.
 *
 * This function is invoked by the Error Attention polling timer when the
 * timer times out. It will check the SLI Error Attention register for
 * possible attention events. If so, it will post an Error Attention event
 * and wake up worker thread to process it. Otherwise, it will set up the
 * Error Attention polling timer for the next poll.
 **/
void lpfc_poll_eratt(unsigned long ptr)
{
	struct lpfc_hba *phba;
	uint32_t eratt = 0;

	phba = (struct lpfc_hba *)ptr;

	/* Check chip HA register for error event */
	eratt = lpfc_sli_check_eratt(phba);

	if (eratt)
		/* Tell the worker thread there is work to do */
		lpfc_worker_wake_up(phba);
	else
		/* Restart the timer for next eratt poll */
		mod_timer(&phba->eratt_poll, jiffies +
					HZ * LPFC_ERATT_POLL_INTERVAL);
	return;
}

1900
/**
1901
 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914
 * @phba: Pointer to HBA context object.
 *
 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
 * is enabled.
 *
 * The caller does not hold any lock.
 * The function processes each response iocb in the response ring until it
 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
 * LE bit set. The function will call the completion handler of the command iocb
 * if the response iocb indicates a completion for a command iocb or it is
 * an abort completion.
 **/
J
James Smart 已提交
1915
void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1916
{
J
James Smart 已提交
1917 1918
	struct lpfc_sli      *psli  = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1919 1920 1921 1922
	IOCB_t *irsp = NULL;
	IOCB_t *entry = NULL;
	struct lpfc_iocbq *cmdiocbq = NULL;
	struct lpfc_iocbq rspiocbq;
1923
	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1924 1925 1926 1927 1928
	uint32_t status;
	uint32_t portRspPut, portRspMax;
	int type;
	uint32_t rsp_cmpl = 0;
	uint32_t ha_copy;
J
James Smart 已提交
1929
	unsigned long iflags;
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945

	pring->stats.iocb_event++;

	/*
	 * The next available response entry should never exceed the maximum
	 * entries.  If it does, treat it as an adapter hardware error.
	 */
	portRspMax = pring->numRiocb;
	portRspPut = le32_to_cpu(pgp->rspPutInx);
	if (unlikely(portRspPut >= portRspMax)) {
		lpfc_sli_rsp_pointers_error(phba, pring);
		return;
	}

	rmb();
	while (pring->rspidx != portRspPut) {
1946
		entry = lpfc_resp_iocb(phba, pring);
1947 1948 1949 1950 1951
		if (++pring->rspidx >= portRspMax)
			pring->rspidx = 0;

		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
				      (uint32_t *) &rspiocbq.iocb,
1952
				      phba->iocb_rsp_size);
1953 1954 1955 1956 1957 1958 1959 1960
		irsp = &rspiocbq.iocb;
		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
		pring->stats.iocb_rsp++;
		rsp_cmpl++;

		if (unlikely(irsp->ulpStatus)) {
			/* Rsp ring <ringno> error: IOCB */
			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1961
					"0326 Rsp Ring %d error: IOCB Data: "
1962
					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
1963
					pring->ringno,
1964 1965 1966 1967 1968 1969
					irsp->un.ulpWord[0],
					irsp->un.ulpWord[1],
					irsp->un.ulpWord[2],
					irsp->un.ulpWord[3],
					irsp->un.ulpWord[4],
					irsp->un.ulpWord[5],
1970 1971
					*(uint32_t *)&irsp->un1,
					*((uint32_t *)&irsp->un1 + 1));
1972 1973 1974 1975 1976 1977 1978 1979 1980 1981
		}

		switch (type) {
		case LPFC_ABORT_IOCB:
		case LPFC_SOL_IOCB:
			/*
			 * Idle exchange closed via ABTS from port.  No iocb
			 * resources need to be recovered.
			 */
			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1982
				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1983 1984 1985
						"0314 IOCB cmd 0x%x "
						"processed. Skipping "
						"completion",
1986
						irsp->ulpCommand);
1987 1988 1989
				break;
			}

J
James Smart 已提交
1990
			spin_lock_irqsave(&phba->hbalock, iflags);
1991 1992
			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
							 &rspiocbq);
J
James Smart 已提交
1993
			spin_unlock_irqrestore(&phba->hbalock, iflags);
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
			if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
				(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
						      &rspiocbq);
			}
			break;
		default:
			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
				char adaptermsg[LPFC_MAX_ADPTMSG];
				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
				memcpy(&adaptermsg[0], (uint8_t *) irsp,
				       MAX_MSG_DATA);
2005 2006
				dev_warn(&((phba->pcidev)->dev),
					 "lpfc%d: %s\n",
2007 2008 2009 2010
					 phba->brd_no, adaptermsg);
			} else {
				/* Unknown IOCB command */
				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2011
						"0321 Unknown IOCB command "
2012
						"Data: x%x, x%x x%x x%x x%x\n",
2013
						type, irsp->ulpCommand,
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026
						irsp->ulpStatus,
						irsp->ulpIoTag,
						irsp->ulpContext);
			}
			break;
		}

		/*
		 * The response IOCB has been processed.  Update the ring
		 * pointer in SLIM.  If the port response put pointer has not
		 * been updated, sync the pgp->rspPutInx and fetch the new port
		 * response put pointer.
		 */
2027
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2028 2029 2030 2031 2032 2033 2034 2035 2036

		if (pring->rspidx == portRspPut)
			portRspPut = le32_to_cpu(pgp->rspPutInx);
	}

	ha_copy = readl(phba->HAregaddr);
	ha_copy >>= (LPFC_FCP_RING * 4);

	if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
J
James Smart 已提交
2037
		spin_lock_irqsave(&phba->hbalock, iflags);
2038 2039 2040 2041
		pring->stats.iocb_rsp_full++;
		status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
		writel(status, phba->CAregaddr);
		readl(phba->CAregaddr);
J
James Smart 已提交
2042
		spin_unlock_irqrestore(&phba->hbalock, iflags);
2043 2044 2045
	}
	if ((ha_copy & HA_R0CE_RSP) &&
	    (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
J
James Smart 已提交
2046
		spin_lock_irqsave(&phba->hbalock, iflags);
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
		pring->stats.iocb_cmd_empty++;

		/* Force update of the local copy of cmdGetInx */
		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
		lpfc_sli_resume_iocb(phba, pring);

		if ((pring->lpfc_sli_cmd_available))
			(pring->lpfc_sli_cmd_available) (phba, pring);

J
James Smart 已提交
2057
		spin_unlock_irqrestore(&phba->hbalock, iflags);
2058 2059 2060 2061 2062
	}

	return;
}

2063
/**
2064
 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @mask: Host attention register mask for this ring.
 *
 * This function is called from the interrupt context when there is a ring
 * event for the fcp ring. The caller does not hold any lock.
 * The function processes each response iocb in the response ring until it
 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
 * LE bit set. The function will call the completion handler of the command iocb
 * if the response iocb indicates a completion for a command iocb or it is
 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
 * function if this is an unsolicited iocb.
已提交
2077
 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2078 2079
 * to check it explicitly. This function always returns 1.
 **/
已提交
2080
static int
J
James Smart 已提交
2081 2082
lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
				struct lpfc_sli_ring *pring, uint32_t mask)
已提交
2083
{
2084
	struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
已提交
2085
	IOCB_t *irsp = NULL;
2086
	IOCB_t *entry = NULL;
已提交
2087 2088 2089 2090 2091 2092 2093 2094 2095
	struct lpfc_iocbq *cmdiocbq = NULL;
	struct lpfc_iocbq rspiocbq;
	uint32_t status;
	uint32_t portRspPut, portRspMax;
	int rc = 1;
	lpfc_iocb_type type;
	unsigned long iflag;
	uint32_t rsp_cmpl = 0;

J
James Smart 已提交
2096
	spin_lock_irqsave(&phba->hbalock, iflag);
已提交
2097 2098 2099 2100 2101 2102 2103 2104 2105
	pring->stats.iocb_event++;

	/*
	 * The next available response entry should never exceed the maximum
	 * entries.  If it does, treat it as an adapter hardware error.
	 */
	portRspMax = pring->numRiocb;
	portRspPut = le32_to_cpu(pgp->rspPutInx);
	if (unlikely(portRspPut >= portRspMax)) {
2106
		lpfc_sli_rsp_pointers_error(phba, pring);
J
James Smart 已提交
2107
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
2108 2109 2110 2111 2112
		return 1;
	}

	rmb();
	while (pring->rspidx != portRspPut) {
2113 2114 2115 2116 2117
		/*
		 * Fetch an entry off the ring and copy it into a local data
		 * structure.  The copy involves a byte-swap since the
		 * network byte order and pci byte orders are different.
		 */
2118
		entry = lpfc_resp_iocb(phba, pring);
J
James Smart 已提交
2119
		phba->last_completion_time = jiffies;
2120 2121 2122 2123

		if (++pring->rspidx >= portRspMax)
			pring->rspidx = 0;

2124 2125
		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
				      (uint32_t *) &rspiocbq.iocb,
2126
				      phba->iocb_rsp_size);
2127
		INIT_LIST_HEAD(&(rspiocbq.list));
2128 2129
		irsp = &rspiocbq.iocb;

已提交
2130 2131 2132 2133 2134
		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
		pring->stats.iocb_rsp++;
		rsp_cmpl++;

		if (unlikely(irsp->ulpStatus)) {
2135 2136 2137 2138 2139 2140 2141
			/*
			 * If resource errors reported from HBA, reduce
			 * queuedepths of the SCSI device.
			 */
			if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
				(irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
				spin_unlock_irqrestore(&phba->hbalock, iflag);
2142
				phba->lpfc_rampdown_queue_depth(phba);
2143 2144 2145
				spin_lock_irqsave(&phba->hbalock, iflag);
			}

已提交
2146 2147
			/* Rsp ring <ringno> error: IOCB */
			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2148
					"0336 Rsp Ring %d error: IOCB Data: "
2149
					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
2150
					pring->ringno,
2151 2152 2153 2154 2155 2156
					irsp->un.ulpWord[0],
					irsp->un.ulpWord[1],
					irsp->un.ulpWord[2],
					irsp->un.ulpWord[3],
					irsp->un.ulpWord[4],
					irsp->un.ulpWord[5],
2157 2158
					*(uint32_t *)&irsp->un1,
					*((uint32_t *)&irsp->un1 + 1));
已提交
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
		}

		switch (type) {
		case LPFC_ABORT_IOCB:
		case LPFC_SOL_IOCB:
			/*
			 * Idle exchange closed via ABTS from port.  No iocb
			 * resources need to be recovered.
			 */
			if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2169
				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2170
						"0333 IOCB cmd 0x%x"
2171
						" processed. Skipping"
2172
						" completion\n",
2173
						irsp->ulpCommand);
已提交
2174 2175 2176
				break;
			}

2177 2178
			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
							 &rspiocbq);
已提交
2179
			if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2180 2181 2182 2183
				if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
					(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
							      &rspiocbq);
				} else {
J
James Smart 已提交
2184 2185
					spin_unlock_irqrestore(&phba->hbalock,
							       iflag);
2186 2187
					(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
							      &rspiocbq);
J
James Smart 已提交
2188
					spin_lock_irqsave(&phba->hbalock,
2189 2190
							  iflag);
				}
已提交
2191 2192
			}
			break;
2193
		case LPFC_UNSOL_IOCB:
J
James Smart 已提交
2194
			spin_unlock_irqrestore(&phba->hbalock, iflag);
2195
			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
J
James Smart 已提交
2196
			spin_lock_irqsave(&phba->hbalock, iflag);
2197
			break;
已提交
2198 2199 2200 2201 2202 2203
		default:
			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
				char adaptermsg[LPFC_MAX_ADPTMSG];
				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
				memcpy(&adaptermsg[0], (uint8_t *) irsp,
				       MAX_MSG_DATA);
2204 2205
				dev_warn(&((phba->pcidev)->dev),
					 "lpfc%d: %s\n",
已提交
2206 2207 2208 2209
					 phba->brd_no, adaptermsg);
			} else {
				/* Unknown IOCB command */
				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2210
						"0334 Unknown IOCB command "
2211
						"Data: x%x, x%x x%x x%x x%x\n",
2212
						type, irsp->ulpCommand,
2213 2214 2215
						irsp->ulpStatus,
						irsp->ulpIoTag,
						irsp->ulpContext);
已提交
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225
			}
			break;
		}

		/*
		 * The response IOCB has been processed.  Update the ring
		 * pointer in SLIM.  If the port response put pointer has not
		 * been updated, sync the pgp->rspPutInx and fetch the new port
		 * response put pointer.
		 */
2226
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
已提交
2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250

		if (pring->rspidx == portRspPut)
			portRspPut = le32_to_cpu(pgp->rspPutInx);
	}

	if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
		pring->stats.iocb_rsp_full++;
		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
		writel(status, phba->CAregaddr);
		readl(phba->CAregaddr);
	}
	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
		pring->stats.iocb_cmd_empty++;

		/* Force update of the local copy of cmdGetInx */
		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
		lpfc_sli_resume_iocb(phba, pring);

		if ((pring->lpfc_sli_cmd_available))
			(pring->lpfc_sli_cmd_available) (phba, pring);

	}

J
James Smart 已提交
2251
	spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
2252 2253 2254
	return rc;
}

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 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 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 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428
 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @rspiocbp: Pointer to driver response IOCB object.
 *
 * This function is called from the worker thread when there is a slow-path
 * response IOCB to process. This function chains all the response iocbs until
 * seeing the iocb with the LE bit set. The function will call
 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
 * completion of a command iocb. The function will call the
 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
 * The function frees the resources or calls the completion handler if this
 * iocb is an abort completion. The function returns NULL when the response
 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
 * this function shall chain the iocb on to the iocb_continueq and return the
 * response iocb passed in.
 **/
static struct lpfc_iocbq *
lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			struct lpfc_iocbq *rspiocbp)
{
	struct lpfc_iocbq *saveq;
	struct lpfc_iocbq *cmdiocbp;
	struct lpfc_iocbq *next_iocb;
	IOCB_t *irsp = NULL;
	uint32_t free_saveq;
	uint8_t iocb_cmd_type;
	lpfc_iocb_type type;
	unsigned long iflag;
	int rc;

	spin_lock_irqsave(&phba->hbalock, iflag);
	/* First add the response iocb to the countinueq list */
	list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
	pring->iocb_continueq_cnt++;

	/* Now, determine whetehr the list is completed for processing */
	irsp = &rspiocbp->iocb;
	if (irsp->ulpLe) {
		/*
		 * By default, the driver expects to free all resources
		 * associated with this iocb completion.
		 */
		free_saveq = 1;
		saveq = list_get_first(&pring->iocb_continueq,
				       struct lpfc_iocbq, list);
		irsp = &(saveq->iocb);
		list_del_init(&pring->iocb_continueq);
		pring->iocb_continueq_cnt = 0;

		pring->stats.iocb_rsp++;

		/*
		 * If resource errors reported from HBA, reduce
		 * queuedepths of the SCSI device.
		 */
		if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
		    (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
			spin_unlock_irqrestore(&phba->hbalock, iflag);
			phba->lpfc_rampdown_queue_depth(phba);
			spin_lock_irqsave(&phba->hbalock, iflag);
		}

		if (irsp->ulpStatus) {
			/* Rsp ring <ringno> error: IOCB */
			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
					"0328 Rsp Ring %d error: "
					"IOCB Data: "
					"x%x x%x x%x x%x "
					"x%x x%x x%x x%x "
					"x%x x%x x%x x%x "
					"x%x x%x x%x x%x\n",
					pring->ringno,
					irsp->un.ulpWord[0],
					irsp->un.ulpWord[1],
					irsp->un.ulpWord[2],
					irsp->un.ulpWord[3],
					irsp->un.ulpWord[4],
					irsp->un.ulpWord[5],
					*(((uint32_t *) irsp) + 6),
					*(((uint32_t *) irsp) + 7),
					*(((uint32_t *) irsp) + 8),
					*(((uint32_t *) irsp) + 9),
					*(((uint32_t *) irsp) + 10),
					*(((uint32_t *) irsp) + 11),
					*(((uint32_t *) irsp) + 12),
					*(((uint32_t *) irsp) + 13),
					*(((uint32_t *) irsp) + 14),
					*(((uint32_t *) irsp) + 15));
		}

		/*
		 * Fetch the IOCB command type and call the correct completion
		 * routine. Solicited and Unsolicited IOCBs on the ELS ring
		 * get freed back to the lpfc_iocb_list by the discovery
		 * kernel thread.
		 */
		iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
		type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
		switch (type) {
		case LPFC_SOL_IOCB:
			spin_unlock_irqrestore(&phba->hbalock, iflag);
			rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
			spin_lock_irqsave(&phba->hbalock, iflag);
			break;

		case LPFC_UNSOL_IOCB:
			spin_unlock_irqrestore(&phba->hbalock, iflag);
			rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
			spin_lock_irqsave(&phba->hbalock, iflag);
			if (!rc)
				free_saveq = 0;
			break;

		case LPFC_ABORT_IOCB:
			cmdiocbp = NULL;
			if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
				cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
								 saveq);
			if (cmdiocbp) {
				/* Call the specified completion routine */
				if (cmdiocbp->iocb_cmpl) {
					spin_unlock_irqrestore(&phba->hbalock,
							       iflag);
					(cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
							      saveq);
					spin_lock_irqsave(&phba->hbalock,
							  iflag);
				} else
					__lpfc_sli_release_iocbq(phba,
								 cmdiocbp);
			}
			break;

		case LPFC_UNKNOWN_IOCB:
			if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
				char adaptermsg[LPFC_MAX_ADPTMSG];
				memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
				memcpy(&adaptermsg[0], (uint8_t *)irsp,
				       MAX_MSG_DATA);
				dev_warn(&((phba->pcidev)->dev),
					 "lpfc%d: %s\n",
					 phba->brd_no, adaptermsg);
			} else {
				/* Unknown IOCB command */
				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
						"0335 Unknown IOCB "
						"command Data: x%x "
						"x%x x%x x%x\n",
						irsp->ulpCommand,
						irsp->ulpStatus,
						irsp->ulpIoTag,
						irsp->ulpContext);
			}
			break;
		}

		if (free_saveq) {
			list_for_each_entry_safe(rspiocbp, next_iocb,
						 &saveq->list, list) {
				list_del(&rspiocbp->list);
				__lpfc_sli_release_iocbq(phba, rspiocbp);
			}
			__lpfc_sli_release_iocbq(phba, saveq);
		}
		rspiocbp = NULL;
	}
	spin_unlock_irqrestore(&phba->hbalock, iflag);
	return rspiocbp;
}

/**
 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2429 2430 2431 2432
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @mask: Host attention register mask for this ring.
 *
2433 2434
 * This routine wraps the actual slow_ring event process routine from the
 * API jump table function pointer from the lpfc_hba struct.
2435
 **/
2436
void
J
James Smart 已提交
2437 2438
lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
				struct lpfc_sli_ring *pring, uint32_t mask)
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456
{
	phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
}

/**
 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @mask: Host attention register mask for this ring.
 *
 * This function is called from the worker thread when there is a ring event
 * for non-fcp rings. The caller does not hold any lock. The function will
 * remove each response iocb in the response ring and calls the handle
 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
 **/
static void
lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
				   struct lpfc_sli_ring *pring, uint32_t mask)
已提交
2457
{
2458
	struct lpfc_pgp *pgp;
已提交
2459 2460 2461 2462 2463
	IOCB_t *entry;
	IOCB_t *irsp = NULL;
	struct lpfc_iocbq *rspiocbp = NULL;
	uint32_t portRspPut, portRspMax;
	unsigned long iflag;
2464
	uint32_t status;
已提交
2465

2466
	pgp = &phba->port_gp[pring->ringno];
J
James Smart 已提交
2467
	spin_lock_irqsave(&phba->hbalock, iflag);
已提交
2468 2469 2470 2471 2472 2473 2474 2475 2476 2477
	pring->stats.iocb_event++;

	/*
	 * The next available response entry should never exceed the maximum
	 * entries.  If it does, treat it as an adapter hardware error.
	 */
	portRspMax = pring->numRiocb;
	portRspPut = le32_to_cpu(pgp->rspPutInx);
	if (portRspPut >= portRspMax) {
		/*
2478
		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
已提交
2479 2480
		 * rsp ring <portRspMax>
		 */
2481
		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2482
				"0303 Ring %d handler: portRspPut %d "
2483
				"is bigger than rsp ring %d\n",
2484
				pring->ringno, portRspPut, portRspMax);
已提交
2485

J
James Smart 已提交
2486 2487
		phba->link_state = LPFC_HBA_ERROR;
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
2488 2489 2490 2491

		phba->work_hs = HS_FFER3;
		lpfc_handle_eratt(phba);

2492
		return;
已提交
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
	}

	rmb();
	while (pring->rspidx != portRspPut) {
		/*
		 * Build a completion list and call the appropriate handler.
		 * The process is to get the next available response iocb, get
		 * a free iocb from the list, copy the response data into the
		 * free iocb, insert to the continuation list, and update the
		 * next response index to slim.  This process makes response
		 * iocb's in the ring available to DMA as fast as possible but
		 * pays a penalty for a copy operation.  Since the iocb is
		 * only 32 bytes, this penalty is considered small relative to
		 * the PCI reads for register values and a slim write.  When
		 * the ulpLe field is set, the entire Command has been
		 * received.
		 */
2510 2511
		entry = lpfc_resp_iocb(phba, pring);

J
James Smart 已提交
2512
		phba->last_completion_time = jiffies;
J
James Smart 已提交
2513
		rspiocbp = __lpfc_sli_get_iocbq(phba);
已提交
2514 2515
		if (rspiocbp == NULL) {
			printk(KERN_ERR "%s: out of buffers! Failing "
2516
			       "completion.\n", __func__);
已提交
2517 2518 2519
			break;
		}

2520 2521
		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
				      phba->iocb_rsp_size);
已提交
2522 2523 2524 2525 2526
		irsp = &rspiocbp->iocb;

		if (++pring->rspidx >= portRspMax)
			pring->rspidx = 0;

2527 2528 2529 2530 2531 2532 2533 2534
		if (pring->ringno == LPFC_ELS_RING) {
			lpfc_debugfs_slow_ring_trc(phba,
			"IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
				*(((uint32_t *) irsp) + 4),
				*(((uint32_t *) irsp) + 6),
				*(((uint32_t *) irsp) + 7));
		}

2535
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
已提交
2536

2537 2538 2539 2540
		spin_unlock_irqrestore(&phba->hbalock, iflag);
		/* Handle the response IOCB */
		rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
		spin_lock_irqsave(&phba->hbalock, iflag);
已提交
2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551

		/*
		 * If the port response put pointer has not been updated, sync
		 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
		 * response put pointer.
		 */
		if (pring->rspidx == portRspPut) {
			portRspPut = le32_to_cpu(pgp->rspPutInx);
		}
	} /* while (pring->rspidx != portRspPut) */

2552
	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
已提交
2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572
		/* At least one response entry has been freed */
		pring->stats.iocb_rsp_full++;
		/* SET RxRE_RSP in Chip Att register */
		status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
		writel(status, phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */
	}
	if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
		pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
		pring->stats.iocb_cmd_empty++;

		/* Force update of the local copy of cmdGetInx */
		pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
		lpfc_sli_resume_iocb(phba, pring);

		if ((pring->lpfc_sli_cmd_available))
			(pring->lpfc_sli_cmd_available) (phba, pring);

	}

J
James Smart 已提交
2573
	spin_unlock_irqrestore(&phba->hbalock, iflag);
2574
	return;
已提交
2575 2576
}

2577
/**
2578
 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
2579 2580 2581 2582 2583 2584 2585 2586
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 *
 * This function aborts all iocbs in the given ring and frees all the iocb
 * objects in txq. This function issues an abort iocb for all the iocb commands
 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
 * the return of this function. The caller is not required to hold any locks.
 **/
J
James Smart 已提交
2587
void
已提交
2588 2589
lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
2590
	LIST_HEAD(completions);
已提交
2591 2592
	struct lpfc_iocbq *iocb, *next_iocb;

2593 2594 2595 2596
	if (pring->ringno == LPFC_ELS_RING) {
		lpfc_fabric_abort_hba(phba);
	}

已提交
2597 2598 2599
	/* Error everything on txq and txcmplq
	 * First do the txq.
	 */
J
James Smart 已提交
2600
	spin_lock_irq(&phba->hbalock);
2601
	list_splice_init(&pring->txq, &completions);
已提交
2602 2603 2604
	pring->txq_cnt = 0;

	/* Next issue ABTS for everything on the txcmplq */
2605 2606
	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
		lpfc_sli_issue_abort_iotag(phba, pring, iocb);
已提交
2607

J
James Smart 已提交
2608
	spin_unlock_irq(&phba->hbalock);
已提交
2609

2610 2611 2612
	/* Cancel all the IOCBs from the completions list */
	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
			      IOERR_SLI_ABORTED);
已提交
2613 2614
}

2615
/**
2616
 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646
 * @phba: Pointer to HBA context object.
 *
 * This function flushes all iocbs in the fcp ring and frees all the iocb
 * objects in txq and txcmplq. This function will not issue abort iocbs
 * for all the iocb commands in txcmplq, they will just be returned with
 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
 * slot has been permanently disabled.
 **/
void
lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
{
	LIST_HEAD(txq);
	LIST_HEAD(txcmplq);
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring  *pring;

	/* Currently, only one fcp ring */
	pring = &psli->ring[psli->fcp_ring];

	spin_lock_irq(&phba->hbalock);
	/* Retrieve everything on txq */
	list_splice_init(&pring->txq, &txq);
	pring->txq_cnt = 0;

	/* Retrieve everything on the txcmplq */
	list_splice_init(&pring->txcmplq, &txcmplq);
	pring->txcmplq_cnt = 0;
	spin_unlock_irq(&phba->hbalock);

	/* Flush the txq */
2647 2648
	lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
			      IOERR_SLI_DOWN);
2649 2650

	/* Flush the txcmpq */
2651 2652
	lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
			      IOERR_SLI_DOWN);
2653 2654
}

2655
/**
2656
 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667
 * @phba: Pointer to HBA context object.
 * @mask: Bit mask to be checked.
 *
 * This function reads the host status register and compares
 * with the provided bit mask to check if HBA completed
 * the restart. This function will wait in a loop for the
 * HBA to complete restart. If the HBA does not restart within
 * 15 iterations, the function will reset the HBA again. The
 * function returns 1 when HBA fail to restart otherwise returns
 * zero.
 **/
2668 2669
static int
lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
已提交
2670
{
2671 2672 2673
	uint32_t status;
	int i = 0;
	int retval = 0;
已提交
2674

2675 2676
	/* Read the HBA Host Status Register */
	status = readl(phba->HSregaddr);
已提交
2677

2678 2679 2680 2681 2682 2683 2684 2685 2686
	/*
	 * Check status register every 100ms for 5 retries, then every
	 * 500ms for 5, then every 2.5 sec for 5, then reset board and
	 * every 2.5 sec for 4.
	 * Break our of the loop if errors occurred during init.
	 */
	while (((status & mask) != mask) &&
	       !(status & HS_FFERM) &&
	       i++ < 20) {
已提交
2687

2688 2689 2690 2691 2692 2693
		if (i <= 5)
			msleep(10);
		else if (i <= 10)
			msleep(500);
		else
			msleep(2500);
已提交
2694

2695
		if (i == 15) {
J
James Smart 已提交
2696
				/* Do post */
2697
			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2698 2699 2700 2701 2702
			lpfc_sli_brdrestart(phba);
		}
		/* Read the HBA Host Status Register */
		status = readl(phba->HSregaddr);
	}
已提交
2703

2704 2705
	/* Check to see if any errors occurred during init */
	if ((status & HS_FFERM) || (i >= 20)) {
J
James Smart 已提交
2706
		phba->link_state = LPFC_HBA_ERROR;
2707
		retval = 1;
已提交
2708 2709
	}

2710 2711
	return retval;
}
已提交
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 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762
/**
 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
 * @phba: Pointer to HBA context object.
 * @mask: Bit mask to be checked.
 *
 * This function checks the host status register to check if HBA is
 * ready. This function will wait in a loop for the HBA to be ready
 * If the HBA is not ready , the function will will reset the HBA PCI
 * function again. The function returns 1 when HBA fail to be ready
 * otherwise returns zero.
 **/
static int
lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
{
	uint32_t status;
	int retval = 0;

	/* Read the HBA Host Status Register */
	status = lpfc_sli4_post_status_check(phba);

	if (status) {
		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
		lpfc_sli_brdrestart(phba);
		status = lpfc_sli4_post_status_check(phba);
	}

	/* Check to see if any errors occurred during init */
	if (status) {
		phba->link_state = LPFC_HBA_ERROR;
		retval = 1;
	} else
		phba->sli4_hba.intr_enable = 0;

	return retval;
}

/**
 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
 * @phba: Pointer to HBA context object.
 * @mask: Bit mask to be checked.
 *
 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
 * from the API jump table function pointer from the lpfc_hba struct.
 **/
int
lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
{
	return phba->lpfc_sli_brdready(phba, mask);
}

2763 2764
#define BARRIER_TEST_PATTERN (0xdeadbeef)

2765
/**
2766
 * lpfc_reset_barrier - Make HBA ready for HBA reset
2767 2768 2769 2770 2771
 * @phba: Pointer to HBA context object.
 *
 * This function is called before resetting an HBA. This
 * function requests HBA to quiesce DMAs before a reset.
 **/
J
James Smart 已提交
2772
void lpfc_reset_barrier(struct lpfc_hba *phba)
2773
{
J
James Smart 已提交
2774 2775
	uint32_t __iomem *resp_buf;
	uint32_t __iomem *mbox_buf;
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790
	volatile uint32_t mbox;
	uint32_t hc_copy;
	int  i;
	uint8_t hdrtype;

	pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
	if (hdrtype != 0x80 ||
	    (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
	     FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
		return;

	/*
	 * Tell the other part of the chip to suspend temporarily all
	 * its DMA activity.
	 */
J
James Smart 已提交
2791
	resp_buf = phba->MBslimaddr;
2792 2793 2794 2795 2796

	/* Disable the error attention */
	hc_copy = readl(phba->HCregaddr);
	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
J
James Smart 已提交
2797
	phba->link_flag |= LS_IGNORE_ERATT;
2798 2799 2800 2801

	if (readl(phba->HAregaddr) & HA_ERATT) {
		/* Clear Chip error bit */
		writel(HA_ERATT, phba->HAregaddr);
J
James Smart 已提交
2802
		phba->pport->stopped = 1;
2803 2804 2805 2806 2807 2808 2809
	}

	mbox = 0;
	((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
	((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;

	writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
J
James Smart 已提交
2810
	mbox_buf = phba->MBslimaddr;
2811 2812 2813 2814 2815 2816 2817 2818
	writel(mbox, mbox_buf);

	for (i = 0;
	     readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
		mdelay(1);

	if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
		if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
J
James Smart 已提交
2819
		    phba->pport->stopped)
2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835
			goto restore_hc;
		else
			goto clear_errat;
	}

	((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
	for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
		mdelay(1);

clear_errat:

	while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
		mdelay(1);

	if (readl(phba->HAregaddr) & HA_ERATT) {
		writel(HA_ERATT, phba->HAregaddr);
J
James Smart 已提交
2836
		phba->pport->stopped = 1;
2837 2838 2839
	}

restore_hc:
J
James Smart 已提交
2840
	phba->link_flag &= ~LS_IGNORE_ERATT;
2841 2842 2843 2844
	writel(hc_copy, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
}

2845
/**
2846
 * lpfc_sli_brdkill - Issue a kill_board mailbox command
2847 2848 2849 2850 2851 2852 2853 2854 2855
 * @phba: Pointer to HBA context object.
 *
 * This function issues a kill_board mailbox command and waits for
 * the error attention interrupt. This function is called for stopping
 * the firmware processing. The caller is not required to hold any
 * locks. This function calls lpfc_hba_down_post function to free
 * any pending commands after the kill. The function will return 1 when it
 * fails to kill the board else will return 0.
 **/
2856
int
J
James Smart 已提交
2857
lpfc_sli_brdkill(struct lpfc_hba *phba)
2858 2859 2860 2861 2862 2863 2864
{
	struct lpfc_sli *psli;
	LPFC_MBOXQ_t *pmb;
	uint32_t status;
	uint32_t ha_copy;
	int retval;
	int i = 0;
已提交
2865

2866
	psli = &phba->sli;
已提交
2867

2868
	/* Kill HBA */
2869
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2870 2871
			"0329 Kill HBA Data: x%x x%x\n",
			phba->pport->port_state, psli->sli_flag);
2872

2873 2874
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmb)
2875 2876 2877
		return 1;

	/* Disable the error attention */
J
James Smart 已提交
2878
	spin_lock_irq(&phba->hbalock);
2879 2880 2881 2882
	status = readl(phba->HCregaddr);
	status &= ~HC_ERINT_ENA;
	writel(status, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
J
James Smart 已提交
2883 2884
	phba->link_flag |= LS_IGNORE_ERATT;
	spin_unlock_irq(&phba->hbalock);
2885 2886 2887 2888 2889 2890 2891 2892

	lpfc_kill_board(phba, pmb);
	pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
	retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);

	if (retval != MBX_SUCCESS) {
		if (retval != MBX_BUSY)
			mempool_free(pmb, phba->mbox_mem_pool);
J
James Smart 已提交
2893 2894 2895
		spin_lock_irq(&phba->hbalock);
		phba->link_flag &= ~LS_IGNORE_ERATT;
		spin_unlock_irq(&phba->hbalock);
2896 2897 2898
		return 1;
	}

2899 2900
	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;

2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915
	mempool_free(pmb, phba->mbox_mem_pool);

	/* There is no completion for a KILL_BOARD mbox cmd. Check for an error
	 * attention every 100ms for 3 seconds. If we don't get ERATT after
	 * 3 seconds we still set HBA_ERROR state because the status of the
	 * board is now undefined.
	 */
	ha_copy = readl(phba->HAregaddr);

	while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
		mdelay(100);
		ha_copy = readl(phba->HAregaddr);
	}

	del_timer_sync(&psli->mbox_tmo);
2916 2917
	if (ha_copy & HA_ERATT) {
		writel(HA_ERATT, phba->HAregaddr);
J
James Smart 已提交
2918
		phba->pport->stopped = 1;
2919
	}
J
James Smart 已提交
2920
	spin_lock_irq(&phba->hbalock);
2921
	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2922 2923
	phba->link_flag &= ~LS_IGNORE_ERATT;
	spin_unlock_irq(&phba->hbalock);
2924 2925 2926

	psli->mbox_active = NULL;
	lpfc_hba_down_post(phba);
J
James Smart 已提交
2927
	phba->link_state = LPFC_HBA_ERROR;
2928

J
James Smart 已提交
2929
	return ha_copy & HA_ERATT ? 0 : 1;
已提交
2930 2931
}

2932
/**
2933
 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
2934 2935 2936 2937 2938 2939 2940 2941 2942
 * @phba: Pointer to HBA context object.
 *
 * This function resets the HBA by writing HC_INITFF to the control
 * register. After the HBA resets, this function resets all the iocb ring
 * indices. This function disables PCI layer parity checking during
 * the reset.
 * This function returns 0 always.
 * The caller is not required to hold any locks.
 **/
2943
int
J
James Smart 已提交
2944
lpfc_sli_brdreset(struct lpfc_hba *phba)
已提交
2945
{
2946
	struct lpfc_sli *psli;
已提交
2947
	struct lpfc_sli_ring *pring;
2948
	uint16_t cfg_value;
已提交
2949 2950
	int i;

2951
	psli = &phba->sli;
已提交
2952

2953 2954
	/* Reset HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2955
			"0325 Reset HBA Data: x%x x%x\n",
J
James Smart 已提交
2956
			phba->pport->port_state, psli->sli_flag);
已提交
2957 2958 2959

	/* perform board reset */
	phba->fc_eventTag = 0;
J
James Smart 已提交
2960 2961
	phba->pport->fc_myDID = 0;
	phba->pport->fc_prevDID = 0;
已提交
2962

2963 2964 2965 2966 2967 2968
	/* Turn off parity checking and serr during the physical reset */
	pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
	pci_write_config_word(phba->pcidev, PCI_COMMAND,
			      (cfg_value &
			       ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));

2969 2970
	psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);

2971 2972 2973 2974 2975 2976 2977 2978 2979
	/* Now toggle INITFF bit in the Host Control Register */
	writel(HC_INITFF, phba->HCregaddr);
	mdelay(1);
	readl(phba->HCregaddr); /* flush */
	writel(0, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */

	/* Restore PCI cmd register */
	pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
已提交
2980 2981

	/* Initialize relevant SLI info */
2982 2983
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
已提交
2984 2985 2986 2987 2988 2989 2990 2991
		pring->flag = 0;
		pring->rspidx = 0;
		pring->next_cmdidx  = 0;
		pring->local_getidx = 0;
		pring->cmdidx = 0;
		pring->missbufcnt = 0;
	}

J
James Smart 已提交
2992
	phba->link_state = LPFC_WARM_START;
2993 2994 2995
	return 0;
}

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 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056
 * lpfc_sli4_brdreset - Reset a sli-4 HBA
 * @phba: Pointer to HBA context object.
 *
 * This function resets a SLI4 HBA. This function disables PCI layer parity
 * checking during resets the device. The caller is not required to hold
 * any locks.
 *
 * This function returns 0 always.
 **/
int
lpfc_sli4_brdreset(struct lpfc_hba *phba)
{
	struct lpfc_sli *psli = &phba->sli;
	uint16_t cfg_value;
	uint8_t qindx;

	/* Reset HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
			"0295 Reset HBA Data: x%x x%x\n",
			phba->pport->port_state, psli->sli_flag);

	/* perform board reset */
	phba->fc_eventTag = 0;
	phba->pport->fc_myDID = 0;
	phba->pport->fc_prevDID = 0;

	/* Turn off parity checking and serr during the physical reset */
	pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
	pci_write_config_word(phba->pcidev, PCI_COMMAND,
			      (cfg_value &
			      ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));

	spin_lock_irq(&phba->hbalock);
	psli->sli_flag &= ~(LPFC_PROCESS_LA);
	phba->fcf.fcf_flag = 0;
	/* Clean up the child queue list for the CQs */
	list_del_init(&phba->sli4_hba.mbx_wq->list);
	list_del_init(&phba->sli4_hba.els_wq->list);
	list_del_init(&phba->sli4_hba.hdr_rq->list);
	list_del_init(&phba->sli4_hba.dat_rq->list);
	list_del_init(&phba->sli4_hba.mbx_cq->list);
	list_del_init(&phba->sli4_hba.els_cq->list);
	list_del_init(&phba->sli4_hba.rxq_cq->list);
	for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
		list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
	for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
		list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
	spin_unlock_irq(&phba->hbalock);

	/* Now physically reset the device */
	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
			"0389 Performing PCI function reset!\n");
	/* Perform FCoE PCI function reset */
	lpfc_pci_function_reset(phba);

	return 0;
}

/**
 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067
 * @phba: Pointer to HBA context object.
 *
 * This function is called in the SLI initialization code path to
 * restart the HBA. The caller is not required to hold any lock.
 * This function writes MBX_RESTART mailbox command to the SLIM and
 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
 * function to free any pending commands. The function enables
 * POST only during the first initialization. The function returns zero.
 * The function does not guarantee completion of MBX_RESTART mailbox
 * command before the return of this function.
 **/
3068 3069
static int
lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3070 3071 3072 3073 3074 3075
{
	MAILBOX_t *mb;
	struct lpfc_sli *psli;
	volatile uint32_t word0;
	void __iomem *to_slim;

J
James Smart 已提交
3076
	spin_lock_irq(&phba->hbalock);
3077 3078 3079 3080 3081

	psli = &phba->sli;

	/* Restart HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3082
			"0337 Restart HBA Data: x%x x%x\n",
J
James Smart 已提交
3083
			phba->pport->port_state, psli->sli_flag);
3084 3085 3086 3087 3088 3089

	word0 = 0;
	mb = (MAILBOX_t *) &word0;
	mb->mbxCommand = MBX_RESTART;
	mb->mbxHc = 1;

3090 3091
	lpfc_reset_barrier(phba);

3092 3093 3094 3095 3096
	to_slim = phba->MBslimaddr;
	writel(*(uint32_t *) mb, to_slim);
	readl(to_slim); /* flush */

	/* Only skip post after fc_ffinit is completed */
3097
	if (phba->pport->port_state)
3098
		word0 = 1;	/* This is really setting up word1 */
3099
	else
3100
		word0 = 0;	/* This is really setting up word1 */
J
James Smart 已提交
3101
	to_slim = phba->MBslimaddr + sizeof (uint32_t);
3102 3103
	writel(*(uint32_t *) mb, to_slim);
	readl(to_slim); /* flush */
已提交
3104

3105
	lpfc_sli_brdreset(phba);
J
James Smart 已提交
3106 3107
	phba->pport->stopped = 0;
	phba->link_state = LPFC_INIT_START;
3108
	phba->hba_flag = 0;
J
James Smart 已提交
3109
	spin_unlock_irq(&phba->hbalock);
3110

3111 3112 3113
	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
	psli->stats_start = get_seconds();

3114 3115
	/* Give the INITFF and Post time to settle. */
	mdelay(100);
3116 3117

	lpfc_hba_down_post(phba);
已提交
3118 3119 3120 3121

	return 0;
}

3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 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 3168 3169 3170
/**
 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
 * @phba: Pointer to HBA context object.
 *
 * This function is called in the SLI initialization code path to restart
 * a SLI4 HBA. The caller is not required to hold any lock.
 * At the end of the function, it calls lpfc_hba_down_post function to
 * free any pending commands.
 **/
static int
lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
{
	struct lpfc_sli *psli = &phba->sli;


	/* Restart HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
			"0296 Restart HBA Data: x%x x%x\n",
			phba->pport->port_state, psli->sli_flag);

	lpfc_sli4_brdreset(phba);

	spin_lock_irq(&phba->hbalock);
	phba->pport->stopped = 0;
	phba->link_state = LPFC_INIT_START;
	phba->hba_flag = 0;
	spin_unlock_irq(&phba->hbalock);

	memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
	psli->stats_start = get_seconds();

	lpfc_hba_down_post(phba);

	return 0;
}

/**
 * lpfc_sli_brdrestart - Wrapper func for restarting hba
 * @phba: Pointer to HBA context object.
 *
 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
 * API jump table function pointer from the lpfc_hba struct.
**/
int
lpfc_sli_brdrestart(struct lpfc_hba *phba)
{
	return phba->lpfc_sli_brdrestart(phba);
}

3171
/**
3172
 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3173 3174 3175 3176 3177 3178 3179 3180
 * @phba: Pointer to HBA context object.
 *
 * This function is called after a HBA restart to wait for successful
 * restart of the HBA. Successful restart of the HBA is indicated by
 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
 * iteration, the function will restart the HBA again. The function returns
 * zero if HBA successfully restarted else returns negative error code.
 **/
已提交
3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199
static int
lpfc_sli_chipset_init(struct lpfc_hba *phba)
{
	uint32_t status, i = 0;

	/* Read the HBA Host Status Register */
	status = readl(phba->HSregaddr);

	/* Check status register to see what current state is */
	i = 0;
	while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {

		/* Check every 100ms for 5 retries, then every 500ms for 5, then
		 * every 2.5 sec for 5, then reset board and every 2.5 sec for
		 * 4.
		 */
		if (i++ >= 20) {
			/* Adapter failed to init, timeout, status reg
			   <status> */
3200
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3201
					"0436 Adapter failed to init, "
3202 3203 3204 3205
					"timeout, status reg x%x, "
					"FW Data: A8 x%x AC x%x\n", status,
					readl(phba->MBslimaddr + 0xa8),
					readl(phba->MBslimaddr + 0xac));
J
James Smart 已提交
3206
			phba->link_state = LPFC_HBA_ERROR;
已提交
3207 3208 3209 3210 3211 3212 3213 3214
			return -ETIMEDOUT;
		}

		/* Check to see if any errors occurred during init */
		if (status & HS_FFERM) {
			/* ERROR: During chipset initialization */
			/* Adapter failed to init, chipset, status reg
			   <status> */
3215
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3216
					"0437 Adapter failed to init, "
3217 3218 3219 3220
					"chipset, status reg x%x, "
					"FW Data: A8 x%x AC x%x\n", status,
					readl(phba->MBslimaddr + 0xa8),
					readl(phba->MBslimaddr + 0xac));
J
James Smart 已提交
3221
			phba->link_state = LPFC_HBA_ERROR;
已提交
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
			return -EIO;
		}

		if (i <= 5) {
			msleep(10);
		} else if (i <= 10) {
			msleep(500);
		} else {
			msleep(2500);
		}

		if (i == 15) {
J
James Smart 已提交
3234
				/* Do post */
3235
			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3236
			lpfc_sli_brdrestart(phba);
已提交
3237 3238 3239 3240 3241 3242 3243 3244 3245
		}
		/* Read the HBA Host Status Register */
		status = readl(phba->HSregaddr);
	}

	/* Check to see if any errors occurred during init */
	if (status & HS_FFERM) {
		/* ERROR: During chipset initialization */
		/* Adapter failed to init, chipset, status reg <status> */
3246
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3247
				"0438 Adapter failed to init, chipset, "
3248 3249 3250 3251
				"status reg x%x, "
				"FW Data: A8 x%x AC x%x\n", status,
				readl(phba->MBslimaddr + 0xa8),
				readl(phba->MBslimaddr + 0xac));
J
James Smart 已提交
3252
		phba->link_state = LPFC_HBA_ERROR;
已提交
3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265
		return -EIO;
	}

	/* Clear all interrupt enable conditions */
	writel(0, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */

	/* setup host attn register */
	writel(0xffffffff, phba->HAregaddr);
	readl(phba->HAregaddr); /* flush */
	return 0;
}

3266
/**
3267
 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3268 3269 3270 3271
 *
 * This function calculates and returns the number of HBQs required to be
 * configured.
 **/
3272
int
3273 3274
lpfc_sli_hbq_count(void)
{
3275
	return ARRAY_SIZE(lpfc_hbq_defs);
3276 3277
}

3278
/**
3279
 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3280 3281 3282 3283 3284
 *
 * This function adds the number of hbq entries in every HBQ to get
 * the total number of hbq entries required for the HBA and returns
 * the total count.
 **/
3285 3286 3287 3288 3289 3290 3291 3292
static int
lpfc_sli_hbq_entry_count(void)
{
	int  hbq_count = lpfc_sli_hbq_count();
	int  count = 0;
	int  i;

	for (i = 0; i < hbq_count; ++i)
3293
		count += lpfc_hbq_defs[i]->entry_count;
3294 3295 3296
	return count;
}

3297
/**
3298
 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3299 3300 3301 3302
 *
 * This function calculates amount of memory required for all hbq entries
 * to be configured and returns the total memory required.
 **/
已提交
3303
int
3304 3305 3306 3307 3308
lpfc_sli_hbq_size(void)
{
	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
}

3309
/**
3310
 * lpfc_sli_hbq_setup - configure and initialize HBQs
3311 3312 3313 3314 3315 3316 3317
 * @phba: Pointer to HBA context object.
 *
 * This function is called during the SLI initialization to configure
 * all the HBQs and post buffers to the HBQ. The caller is not
 * required to hold any locks. This function will return zero if successful
 * else it will return negative error code.
 **/
3318 3319 3320 3321 3322 3323 3324 3325 3326
static int
lpfc_sli_hbq_setup(struct lpfc_hba *phba)
{
	int  hbq_count = lpfc_sli_hbq_count();
	LPFC_MBOXQ_t *pmb;
	MAILBOX_t *pmbox;
	uint32_t hbqno;
	uint32_t hbq_entry_index;

3327 3328 3329
				/* Get a Mailbox buffer to setup mailbox
				 * commands for HBA initialization
				 */
3330 3331 3332 3333 3334 3335 3336 3337 3338
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);

	if (!pmb)
		return -ENOMEM;

	pmbox = &pmb->mb;

	/* Initialize the struct lpfc_sli_hbq structure for each hbq */
	phba->link_state = LPFC_INIT_MBX_CMDS;
3339
	phba->hbq_in_use = 1;
3340 3341 3342 3343 3344 3345 3346

	hbq_entry_index = 0;
	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
		phba->hbqs[hbqno].next_hbqPutIdx = 0;
		phba->hbqs[hbqno].hbqPutIdx      = 0;
		phba->hbqs[hbqno].local_hbqGetIdx   = 0;
		phba->hbqs[hbqno].entry_count =
3347
			lpfc_hbq_defs[hbqno]->entry_count;
3348 3349
		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
			hbq_entry_index, pmb);
3350 3351 3352 3353 3354 3355 3356
		hbq_entry_index += phba->hbqs[hbqno].entry_count;

		if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
			/* Adapter failed to init, mbxCmd <cmd> CFG_RING,
			   mbxStatus <status>, ring <num> */

			lpfc_printf_log(phba, KERN_ERR,
3357
					LOG_SLI | LOG_VPORT,
3358
					"1805 Adapter failed to init. "
3359
					"Data: x%x x%x x%x\n",
3360
					pmbox->mbxCommand,
3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371
					pmbox->mbxStatus, hbqno);

			phba->link_state = LPFC_HBA_ERROR;
			mempool_free(pmb, phba->mbox_mem_pool);
			return ENXIO;
		}
	}
	phba->hbq_count = hbq_count;

	mempool_free(pmb, phba->mbox_mem_pool);

3372
	/* Initially populate or replenish the HBQs */
3373 3374
	for (hbqno = 0; hbqno < hbq_count; ++hbqno)
		lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3375 3376 3377
	return 0;
}

3378
/**
3379
 * lpfc_sli_config_port - Issue config port mailbox command
3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390
 * @phba: Pointer to HBA context object.
 * @sli_mode: sli mode - 2/3
 *
 * This function is called by the sli intialization code path
 * to issue config_port mailbox command. This function restarts the
 * HBA firmware and issues a config_port mailbox command to configure
 * the SLI interface in the sli mode specified by sli_mode
 * variable. The caller is not required to hold any locks.
 * The function returns 0 if successful, else returns negative error
 * code.
 **/
3391 3392
int
lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
已提交
3393 3394 3395 3396 3397 3398
{
	LPFC_MBOXQ_t *pmb;
	uint32_t resetcount = 0, rc = 0, done = 0;

	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmb) {
J
James Smart 已提交
3399
		phba->link_state = LPFC_HBA_ERROR;
已提交
3400 3401 3402
		return -ENOMEM;
	}

3403
	phba->sli_rev = sli_mode;
已提交
3404
	while (resetcount < 2 && !done) {
J
James Smart 已提交
3405
		spin_lock_irq(&phba->hbalock);
3406
		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
3407
		spin_unlock_irq(&phba->hbalock);
3408
		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3409
		lpfc_sli_brdrestart(phba);
已提交
3410 3411 3412 3413
		rc = lpfc_sli_chipset_init(phba);
		if (rc)
			break;

J
James Smart 已提交
3414
		spin_lock_irq(&phba->hbalock);
3415
		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
3416
		spin_unlock_irq(&phba->hbalock);
已提交
3417 3418
		resetcount++;

3419 3420 3421 3422 3423
		/* Call pre CONFIG_PORT mailbox command initialization.  A
		 * value of 0 means the call was successful.  Any other
		 * nonzero value is a failure, but if ERESTART is returned,
		 * the driver may reset the HBA and try again.
		 */
已提交
3424 3425
		rc = lpfc_config_port_prep(phba);
		if (rc == -ERESTART) {
3426
			phba->link_state = LPFC_LINK_UNKNOWN;
已提交
3427
			continue;
3428
		} else if (rc)
已提交
3429
			break;
J
James Smart 已提交
3430
		phba->link_state = LPFC_INIT_MBX_CMDS;
已提交
3431 3432
		lpfc_config_port(phba, pmb);
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3433 3434 3435
		phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
					LPFC_SLI3_HBQ_ENABLED |
					LPFC_SLI3_CRP_ENABLED |
3436 3437
					LPFC_SLI3_INB_ENABLED |
					LPFC_SLI3_BG_ENABLED);
3438
		if (rc != MBX_SUCCESS) {
已提交
3439
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3440
				"0442 Adapter failed to init, mbxCmd x%x "
3441
				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3442
				pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
J
James Smart 已提交
3443
			spin_lock_irq(&phba->hbalock);
已提交
3444
			phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
J
James Smart 已提交
3445 3446
			spin_unlock_irq(&phba->hbalock);
			rc = -ENXIO;
3447
		} else
3448
			done = 1;
已提交
3449
	}
3450 3451 3452 3453
	if (!done) {
		rc = -EINVAL;
		goto do_prep_failed;
	}
3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469
	if (pmb->mb.un.varCfgPort.sli_mode == 3) {
		if (!pmb->mb.un.varCfgPort.cMA) {
			rc = -ENXIO;
			goto do_prep_failed;
		}
		if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
			phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
			phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
		} else
			phba->max_vpi = 0;
		if (pmb->mb.un.varCfgPort.gerbm)
			phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
		if (pmb->mb.un.varCfgPort.gcrp)
			phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
		if (pmb->mb.un.varCfgPort.ginb) {
			phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3470
			phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3471 3472 3473 3474 3475 3476
			phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
			phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
			phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
			phba->inb_last_counter =
					phba->mbox->us.s3_inb_pgp.counter;
		} else {
3477
			phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3478 3479 3480 3481
			phba->port_gp = phba->mbox->us.s3_pgp.port;
			phba->inb_ha_copy = NULL;
			phba->inb_counter = NULL;
		}
3482 3483 3484 3485 3486 3487 3488 3489 3490

		if (phba->cfg_enable_bg) {
			if (pmb->mb.un.varCfgPort.gbg)
				phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
			else
				lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
						"0443 Adapter did not grant "
						"BlockGuard\n");
		}
3491
	} else {
3492
		phba->hbq_get = NULL;
3493 3494 3495
		phba->port_gp = phba->mbox->us.s2.port;
		phba->inb_ha_copy = NULL;
		phba->inb_counter = NULL;
3496
		phba->max_vpi = 0;
3497
	}
3498
do_prep_failed:
3499 3500 3501 3502
	mempool_free(pmb, phba->mbox_mem_pool);
	return rc;
}

3503 3504

/**
3505
 * lpfc_sli_hba_setup - SLI intialization function
3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516
 * @phba: Pointer to HBA context object.
 *
 * This function is the main SLI intialization function. This function
 * is called by the HBA intialization code, HBA reset code and HBA
 * error attention handler code. Caller is not required to hold any
 * locks. This function issues config_port mailbox command to configure
 * the SLI, setup iocb rings and HBQ rings. In the end the function
 * calls the config_port_post function to issue init_link mailbox
 * command and to start the discovery. The function will return zero
 * if successful, else it will return negative error code.
 **/
3517 3518 3519 3520
int
lpfc_sli_hba_setup(struct lpfc_hba *phba)
{
	uint32_t rc;
3521
	int  mode = 3;
3522 3523 3524

	switch (lpfc_sli_mode) {
	case 2:
3525
		if (phba->cfg_enable_npiv) {
3526
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3527
				"1824 NPIV enabled: Override lpfc_sli_mode "
3528
				"parameter (%d) to auto (0).\n",
3529
				lpfc_sli_mode);
3530 3531
			break;
		}
3532 3533 3534 3535 3536 3537
		mode = 2;
		break;
	case 0:
	case 3:
		break;
	default:
3538
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3539 3540
				"1819 Unrecognized lpfc_sli_mode "
				"parameter: %d.\n", lpfc_sli_mode);
3541 3542 3543 3544

		break;
	}

3545 3546
	rc = lpfc_sli_config_port(phba, mode);

3547
	if (rc && lpfc_sli_mode == 3)
3548
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3549 3550
				"1820 Unable to select SLI-3.  "
				"Not supported by adapter.\n");
3551
	if (rc && mode != 2)
3552
		rc = lpfc_sli_config_port(phba, 2);
3553
	if (rc)
已提交
3554 3555
		goto lpfc_sli_hba_setup_error;

3556 3557 3558 3559 3560 3561
	if (phba->sli_rev == 3) {
		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
	} else {
		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
3562
		phba->sli3_options = 0;
3563 3564 3565
	}

	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3566 3567
			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
			phba->sli_rev, phba->max_vpi);
3568
	rc = lpfc_sli_ring_map(phba);
已提交
3569 3570 3571 3572

	if (rc)
		goto lpfc_sli_hba_setup_error;

3573
	/* Init HBQs */
3574 3575 3576 3577 3578 3579
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
		rc = lpfc_sli_hbq_setup(phba);
		if (rc)
			goto lpfc_sli_hba_setup_error;
	}

已提交
3580 3581 3582 3583 3584 3585
	phba->sli.sli_flag |= LPFC_PROCESS_LA;

	rc = lpfc_config_port_post(phba);
	if (rc)
		goto lpfc_sli_hba_setup_error;

3586 3587
	return rc;

3588
lpfc_sli_hba_setup_error:
J
James Smart 已提交
3589
	phba->link_state = LPFC_HBA_ERROR;
3590
	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3591
			"0445 Firmware initialization failed\n");
已提交
3592 3593 3594
	return rc;
}

3595
/**
3596 3597 3598 3599 3600 3601
 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
 * @phba: Pointer to HBA context object.
 * @mboxq: mailbox pointer.
 * This function issue a dump mailbox command to read config region
 * 23 and parse the records in the region and populate driver
 * data structure.
3602
 **/
3603 3604 3605
static int
lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
		LPFC_MBOXQ_t *mboxq)
已提交
3606
{
3607 3608 3609 3610
	struct lpfc_dmabuf *mp;
	struct lpfc_mqe *mqe;
	uint32_t data_length;
	int rc;
已提交
3611

3612 3613 3614 3615 3616
	/* Program the default value of vlan_id and fc_map */
	phba->valid_vlan = 0;
	phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
	phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
	phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
J
James Smart 已提交
3617

3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653
	mqe = &mboxq->u.mqe;
	if (lpfc_dump_fcoe_param(phba, mboxq))
		return -ENOMEM;

	mp = (struct lpfc_dmabuf *) mboxq->context1;
	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);

	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
			"(%d):2571 Mailbox cmd x%x Status x%x "
			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
			"CQ: x%x x%x x%x x%x\n",
			mboxq->vport ? mboxq->vport->vpi : 0,
			bf_get(lpfc_mqe_command, mqe),
			bf_get(lpfc_mqe_status, mqe),
			mqe->un.mb_words[0], mqe->un.mb_words[1],
			mqe->un.mb_words[2], mqe->un.mb_words[3],
			mqe->un.mb_words[4], mqe->un.mb_words[5],
			mqe->un.mb_words[6], mqe->un.mb_words[7],
			mqe->un.mb_words[8], mqe->un.mb_words[9],
			mqe->un.mb_words[10], mqe->un.mb_words[11],
			mqe->un.mb_words[12], mqe->un.mb_words[13],
			mqe->un.mb_words[14], mqe->un.mb_words[15],
			mqe->un.mb_words[16], mqe->un.mb_words[50],
			mboxq->mcqe.word0,
			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
			mboxq->mcqe.trailer);

	if (rc) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
		return -EIO;
	}
	data_length = mqe->un.mb_words[5];
	if (data_length > DMP_FCOEPARAM_RGN_SIZE)
		return -EIO;
已提交
3654

3655 3656 3657 3658 3659
	lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
	kfree(mp);
	return 0;
}
3660 3661

/**
3662 3663 3664 3665 3666 3667
 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
 * @phba: pointer to lpfc hba data structure.
 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
 * @vpd: pointer to the memory to hold resulting port vpd data.
 * @vpd_size: On input, the number of bytes allocated to @vpd.
 *	      On output, the number of data bytes in @vpd.
3668
 *
3669 3670 3671 3672 3673 3674
 * This routine executes a READ_REV SLI4 mailbox command.  In
 * addition, this routine gets the port vpd data.
 *
 * Return codes
 * 	0 - sucessful
 * 	ENOMEM - could not allocated memory.
3675
 **/
3676 3677 3678
static int
lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
		    uint8_t *vpd, uint32_t *vpd_size)
已提交
3679
{
3680 3681 3682 3683
	int rc = 0;
	uint32_t dma_size;
	struct lpfc_dmabuf *dmabuf;
	struct lpfc_mqe *mqe;
已提交
3684

3685 3686 3687 3688 3689 3690 3691
	dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!dmabuf)
		return -ENOMEM;

	/*
	 * Get a DMA buffer for the vpd data resulting from the READ_REV
	 * mailbox command.
3692
	 */
3693 3694 3695 3696 3697 3698 3699 3700
	dma_size = *vpd_size;
	dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
					  dma_size,
					  &dmabuf->phys,
					  GFP_KERNEL);
	if (!dmabuf->virt) {
		kfree(dmabuf);
		return -ENOMEM;
3701
	}
3702
	memset(dmabuf->virt, 0, dma_size);
3703

3704 3705 3706 3707
	/*
	 * The SLI4 implementation of READ_REV conflicts at word1,
	 * bits 31:16 and SLI4 adds vpd functionality not present
	 * in SLI3.  This code corrects the conflicts.
3708
	 */
3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722
	lpfc_read_rev(phba, mboxq);
	mqe = &mboxq->u.mqe;
	mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
	mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
	mqe->un.read_rev.word1 &= 0x0000FFFF;
	bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
	bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);

	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
	if (rc) {
		dma_free_coherent(&phba->pcidev->dev, dma_size,
				  dmabuf->virt, dmabuf->phys);
		return -EIO;
	}
3723

3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743
	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
			"(%d):0380 Mailbox cmd x%x Status x%x "
			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
			"x%x x%x x%x x%x x%x x%x x%x x%x x%x "
			"CQ: x%x x%x x%x x%x\n",
			mboxq->vport ? mboxq->vport->vpi : 0,
			bf_get(lpfc_mqe_command, mqe),
			bf_get(lpfc_mqe_status, mqe),
			mqe->un.mb_words[0], mqe->un.mb_words[1],
			mqe->un.mb_words[2], mqe->un.mb_words[3],
			mqe->un.mb_words[4], mqe->un.mb_words[5],
			mqe->un.mb_words[6], mqe->un.mb_words[7],
			mqe->un.mb_words[8], mqe->un.mb_words[9],
			mqe->un.mb_words[10], mqe->un.mb_words[11],
			mqe->un.mb_words[12], mqe->un.mb_words[13],
			mqe->un.mb_words[14], mqe->un.mb_words[15],
			mqe->un.mb_words[16], mqe->un.mb_words[50],
			mboxq->mcqe.word0,
			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
			mboxq->mcqe.trailer);
3744

3745 3746 3747 3748 3749 3750 3751
	/*
	 * The available vpd length cannot be bigger than the
	 * DMA buffer passed to the port.  Catch the less than
	 * case and update the caller's size.
	 */
	if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
		*vpd_size = mqe->un.read_rev.avail_vpd_len;
3752

3753 3754 3755 3756 3757
	lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
	dma_free_coherent(&phba->pcidev->dev, dma_size,
			  dmabuf->virt, dmabuf->phys);
	kfree(dmabuf);
	return 0;
已提交
3758 3759
}

3760
/**
3761 3762
 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
 * @phba: pointer to lpfc hba data structure.
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 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 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 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 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 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
 * This routine is called to explicitly arm the SLI4 device's completion and
 * event queues
 **/
static void
lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
{
	uint8_t fcp_eqidx;

	lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
	lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
	lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
		lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
				     LPFC_QUEUE_REARM);
	lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
	for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
		lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
				     LPFC_QUEUE_REARM);
}

/**
 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
 * @phba: Pointer to HBA context object.
 *
 * This function is the main SLI4 device intialization PCI function. This
 * function is called by the HBA intialization code, HBA reset code and
 * HBA error attention handler code. Caller is not required to hold any
 * locks.
 **/
int
lpfc_sli4_hba_setup(struct lpfc_hba *phba)
{
	int rc;
	LPFC_MBOXQ_t *mboxq;
	struct lpfc_mqe *mqe;
	uint8_t *vpd;
	uint32_t vpd_size;
	uint32_t ftr_rsp = 0;
	struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
	struct lpfc_vport *vport = phba->pport;
	struct lpfc_dmabuf *mp;

	/* Perform a PCI function reset to start from clean */
	rc = lpfc_pci_function_reset(phba);
	if (unlikely(rc))
		return -ENODEV;

	/* Check the HBA Host Status Register for readyness */
	rc = lpfc_sli4_post_status_check(phba);
	if (unlikely(rc))
		return -ENODEV;
	else {
		spin_lock_irq(&phba->hbalock);
		phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
		spin_unlock_irq(&phba->hbalock);
	}

	/*
	 * Allocate a single mailbox container for initializing the
	 * port.
	 */
	mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mboxq)
		return -ENOMEM;

	/*
	 * Continue initialization with default values even if driver failed
	 * to read FCoE param config regions
	 */
	if (lpfc_sli4_read_fcoe_params(phba, mboxq))
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
			"2570 Failed to read FCoE parameters \n");

	/* Issue READ_REV to collect vpd and FW information. */
	vpd_size = PAGE_SIZE;
	vpd = kzalloc(vpd_size, GFP_KERNEL);
	if (!vpd) {
		rc = -ENOMEM;
		goto out_free_mbox;
	}

	rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
	if (unlikely(rc))
		goto out_free_vpd;

	mqe = &mboxq->u.mqe;
	if ((bf_get(lpfc_mbx_rd_rev_sli_lvl,
		    &mqe->un.read_rev) != LPFC_SLI_REV4) ||
	    (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev) == 0)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
			"0376 READ_REV Error. SLI Level %d "
			"FCoE enabled %d\n",
			bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev),
			bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev));
		rc = -EIO;
		goto out_free_vpd;
	}
	/* Single threaded at this point, no need for lock */
	spin_lock_irq(&phba->hbalock);
	phba->hba_flag |= HBA_FCOE_SUPPORT;
	spin_unlock_irq(&phba->hbalock);
	/*
	 * Evaluate the read rev and vpd data. Populate the driver
	 * state with the results. If this routine fails, the failure
	 * is not fatal as the driver will use generic values.
	 */
	rc = lpfc_parse_vpd(phba, vpd, vpd_size);
	if (unlikely(!rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0377 Error %d parsing vpd. "
				"Using defaults.\n", rc);
		rc = 0;
	}

	/* By now, we should determine the SLI revision, hard code for now */
	phba->sli_rev = LPFC_SLI_REV4;

	/*
	 * Discover the port's supported feature set and match it against the
	 * hosts requests.
	 */
	lpfc_request_features(phba, mboxq);
	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
	if (unlikely(rc)) {
		rc = -EIO;
		goto out_free_vpd;
	}

	/*
	 * The port must support FCP initiator mode as this is the
	 * only mode running in the host.
	 */
	if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
				"0378 No support for fcpi mode.\n");
		ftr_rsp++;
	}

	/*
	 * If the port cannot support the host's requested features
	 * then turn off the global config parameters to disable the
	 * feature in the driver.  This is not a fatal error.
	 */
	if ((phba->cfg_enable_bg) &&
	    !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
		ftr_rsp++;

	if (phba->max_vpi && phba->cfg_enable_npiv &&
	    !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
		ftr_rsp++;

	if (ftr_rsp) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
				"0379 Feature Mismatch Data: x%08x %08x "
				"x%x x%x x%x\n", mqe->un.req_ftrs.word2,
				mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
				phba->cfg_enable_npiv, phba->max_vpi);
		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
			phba->cfg_enable_bg = 0;
		if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
			phba->cfg_enable_npiv = 0;
	}

	/* These SLI3 features are assumed in SLI4 */
	spin_lock_irq(&phba->hbalock);
	phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
	spin_unlock_irq(&phba->hbalock);

	/* Read the port's service parameters. */
	lpfc_read_sparam(phba, mboxq, vport->vpi);
	mboxq->vport = vport;
	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
	mp = (struct lpfc_dmabuf *) mboxq->context1;
	if (rc == MBX_SUCCESS) {
		memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
		rc = 0;
	}

	/*
	 * This memory was allocated by the lpfc_read_sparam routine. Release
	 * it to the mbuf pool.
	 */
	lpfc_mbuf_free(phba, mp->virt, mp->phys);
	kfree(mp);
	mboxq->context1 = NULL;
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0382 READ_SPARAM command failed "
				"status %d, mbxStatus x%x\n",
				rc, bf_get(lpfc_mqe_status, mqe));
		phba->link_state = LPFC_HBA_ERROR;
		rc = -EIO;
		goto out_free_vpd;
	}

	if (phba->cfg_soft_wwnn)
		u64_to_wwn(phba->cfg_soft_wwnn,
			   vport->fc_sparam.nodeName.u.wwn);
	if (phba->cfg_soft_wwpn)
		u64_to_wwn(phba->cfg_soft_wwpn,
			   vport->fc_sparam.portName.u.wwn);
	memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
	       sizeof(struct lpfc_name));
	memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
	       sizeof(struct lpfc_name));

	/* Update the fc_host data structures with new wwn. */
	fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);

	/* Register SGL pool to the device using non-embedded mailbox command */
	rc = lpfc_sli4_post_sgl_list(phba);
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0582 Error %d during sgl post operation", rc);
		rc = -ENODEV;
		goto out_free_vpd;
	}

	/* Register SCSI SGL pool to the device */
	rc = lpfc_sli4_repost_scsi_sgl_list(phba);
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
				"0383 Error %d during scsi sgl post opeation",
				rc);
		/* Some Scsi buffers were moved to the abort scsi list */
		/* A pci function reset will repost them */
		rc = -ENODEV;
		goto out_free_vpd;
	}

	/* Post the rpi header region to the device. */
	rc = lpfc_sli4_post_all_rpi_hdrs(phba);
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0393 Error %d during rpi post operation\n",
				rc);
		rc = -ENODEV;
		goto out_free_vpd;
	}
	/* Temporary initialization of lpfc_fip_flag to non-fip */
	bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);

	/* Set up all the queues to the device */
	rc = lpfc_sli4_queue_setup(phba);
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0381 Error %d during queue setup.\n ", rc);
		goto out_stop_timers;
	}

	/* Arm the CQs and then EQs on device */
	lpfc_sli4_arm_cqeq_intr(phba);

	/* Indicate device interrupt mode */
	phba->sli4_hba.intr_enable = 1;

	/* Allow asynchronous mailbox command to go through */
	spin_lock_irq(&phba->hbalock);
	phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
	spin_unlock_irq(&phba->hbalock);

	/* Post receive buffers to the device */
	lpfc_sli4_rb_setup(phba);

	/* Start the ELS watchdog timer */
	/*
	 * The driver for SLI4 is not yet ready to process timeouts
	 * or interrupts.  Once it is, the comment bars can be removed.
	 */
	/* mod_timer(&vport->els_tmofunc,
	 *           jiffies + HZ * (phba->fc_ratov*2)); */

	/* Start heart beat timer */
	mod_timer(&phba->hb_tmofunc,
		  jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
	phba->hb_outstanding = 0;
	phba->last_completion_time = jiffies;

	/* Start error attention (ERATT) polling timer */
	mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);

	/*
	 * The port is ready, set the host's link state to LINK_DOWN
	 * in preparation for link interrupts.
	 */
	lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
	mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
	lpfc_set_loopback_flag(phba);
	/* Change driver state to LPFC_LINK_DOWN right before init link */
	spin_lock_irq(&phba->hbalock);
	phba->link_state = LPFC_LINK_DOWN;
	spin_unlock_irq(&phba->hbalock);
	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
	if (unlikely(rc != MBX_NOT_FINISHED)) {
		kfree(vpd);
		return 0;
	} else
		rc = -EIO;

	/* Unset all the queues set up in this routine when error out */
	if (rc)
		lpfc_sli4_queue_unset(phba);

out_stop_timers:
	if (rc)
		lpfc_stop_hba_timers(phba);
out_free_vpd:
	kfree(vpd);
out_free_mbox:
	mempool_free(mboxq, phba->mbox_mem_pool);
	return rc;
}

/**
 * lpfc_mbox_timeout - Timeout call back function for mbox timer
 * @ptr: context object - pointer to hba structure.
 *
 * This is the callback function for mailbox timer. The mailbox
 * timer is armed when a new mailbox command is issued and the timer
 * is deleted when the mailbox complete. The function is called by
 * the kernel timer code when a mailbox does not complete within
 * expected time. This function wakes up the worker thread to
 * process the mailbox timeout and returns. All the processing is
 * done by the worker thread function lpfc_mbox_timeout_handler.
 **/
void
lpfc_mbox_timeout(unsigned long ptr)
{
	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
	unsigned long iflag;
	uint32_t tmo_posted;

	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
	if (!tmo_posted)
		phba->pport->work_port_events |= WORKER_MBOX_TMO;
	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);

	if (!tmo_posted)
		lpfc_worker_wake_up(phba);
	return;
}


/**
 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
 * @phba: Pointer to HBA context object.
 *
 * This function is called from worker thread when a mailbox command times out.
 * The caller is not required to hold any locks. This function will reset the
 * HBA and recover all the pending commands.
 **/
void
lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
{
	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
	MAILBOX_t *mb = &pmbox->mb;
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring;

	/* Check the pmbox pointer first.  There is a race condition
	 * between the mbox timeout handler getting executed in the
	 * worklist and the mailbox actually completing. When this
	 * race condition occurs, the mbox_active will be NULL.
	 */
	spin_lock_irq(&phba->hbalock);
	if (pmbox == NULL) {
		lpfc_printf_log(phba, KERN_WARNING,
				LOG_MBOX | LOG_SLI,
				"0353 Active Mailbox cleared - mailbox timeout "
				"exiting\n");
		spin_unlock_irq(&phba->hbalock);
		return;
	}

	/* Mbox cmd <mbxCommand> timeout */
	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
			"0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
			mb->mbxCommand,
			phba->pport->port_state,
			phba->sli.sli_flag,
			phba->sli.mbox_active);
	spin_unlock_irq(&phba->hbalock);

	/* Setting state unknown so lpfc_sli_abort_iocb_ring
	 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
	 * it to fail all oustanding SCSI IO.
	 */
	spin_lock_irq(&phba->pport->work_port_lock);
	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
	spin_unlock_irq(&phba->pport->work_port_lock);
	spin_lock_irq(&phba->hbalock);
	phba->link_state = LPFC_LINK_UNKNOWN;
	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
	spin_unlock_irq(&phba->hbalock);

	pring = &psli->ring[psli->fcp_ring];
	lpfc_sli_abort_iocb_ring(phba, pring);

	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
			"0345 Resetting board due to mailbox timeout\n");

	/* Reset the HBA device */
	lpfc_reset_hba(phba);
}

/**
 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
 * @phba: Pointer to HBA context object.
 * @pmbox: Pointer to mailbox object.
 * @flag: Flag indicating how the mailbox need to be processed.
 *
 * This function is called by discovery code and HBA management code
 * to submit a mailbox command to firmware with SLI-3 interface spec. This
 * function gets the hbalock to protect the data structures.
 * The mailbox command can be submitted in polling mode, in which case
 * this function will wait in a polling loop for the completion of the
 * mailbox.
 * If the mailbox is submitted in no_wait mode (not polling) the
 * function will submit the command and returns immediately without waiting
 * for the mailbox completion. The no_wait is supported only when HBA
 * is in SLI2/SLI3 mode - interrupts are enabled.
 * The SLI interface allows only one mailbox pending at a time. If the
 * mailbox is issued in polling mode and there is already a mailbox
 * pending, then the function will return an error. If the mailbox is issued
 * in NO_WAIT mode and there is a mailbox pending already, the function
 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
 * The sli layer owns the mailbox object until the completion of mailbox
 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
 * return codes the caller owns the mailbox command after the return of
 * the function.
4196
 **/
4197 4198 4199
static int
lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
		       uint32_t flag)
已提交
4200 4201
{
	MAILBOX_t *mb;
J
James Smart 已提交
4202
	struct lpfc_sli *psli = &phba->sli;
已提交
4203 4204 4205
	uint32_t status, evtctr;
	uint32_t ha_copy;
	int i;
4206
	unsigned long timeout;
已提交
4207
	unsigned long drvr_flag = 0;
4208
	uint32_t word0, ldata;
已提交
4209
	void __iomem *to_slim;
4210 4211 4212 4213 4214
	int processing_queue = 0;

	spin_lock_irqsave(&phba->hbalock, drvr_flag);
	if (!pmbox) {
		/* processing mbox queue from intr_handler */
4215 4216 4217 4218
		if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
			return MBX_SUCCESS;
		}
4219 4220 4221 4222 4223 4224 4225 4226
		processing_queue = 1;
		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
		pmbox = lpfc_mbox_get(phba);
		if (!pmbox) {
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
			return MBX_SUCCESS;
		}
	}
已提交
4227

4228
	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4229
		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4230
		if(!pmbox->vport) {
4231
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4232
			lpfc_printf_log(phba, KERN_ERR,
4233
					LOG_MBOX | LOG_VPORT,
4234
					"1806 Mbox x%x failed. No vport\n",
4235
					pmbox->u.mb.mbxCommand);
4236
			dump_stack();
4237
			goto out_not_finished;
4238 4239 4240
		}
	}

4241
	/* If the PCI channel is in offline state, do not post mbox. */
4242 4243 4244 4245
	if (unlikely(pci_channel_offline(phba->pcidev))) {
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
		goto out_not_finished;
	}
4246

4247 4248 4249 4250 4251 4252
	/* If HBA has a deferred error attention, fail the iocb. */
	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
		goto out_not_finished;
	}

已提交
4253
	psli = &phba->sli;
4254

4255
	mb = &pmbox->u.mb;
已提交
4256 4257
	status = MBX_SUCCESS;

J
James Smart 已提交
4258 4259
	if (phba->link_state == LPFC_HBA_ERROR) {
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4260 4261

		/* Mbox command <mbxCommand> cannot issue */
4262 4263 4264 4265 4266
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):0311 Mailbox command x%x cannot "
				"issue Data: x%x x%x\n",
				pmbox->vport ? pmbox->vport->vpi : 0,
				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4267
		goto out_not_finished;
4268 4269
	}

4270 4271
	if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
	    !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
J
James Smart 已提交
4272
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4273 4274 4275 4276 4277
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2528 Mailbox command x%x cannot "
				"issue Data: x%x x%x\n",
				pmbox->vport ? pmbox->vport->vpi : 0,
				pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4278
		goto out_not_finished;
4279 4280
	}

已提交
4281 4282 4283 4284 4285 4286 4287
	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
		/* Polling for a mbox command when another one is already active
		 * is not allowed in SLI. Also, the driver must have established
		 * SLI2 mode to queue and process multiple mbox commands.
		 */

		if (flag & MBX_POLL) {
J
James Smart 已提交
4288
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
4289 4290

			/* Mbox command <mbxCommand> cannot issue */
4291 4292 4293 4294 4295 4296
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
					"(%d):2529 Mailbox command x%x "
					"cannot issue Data: x%x x%x\n",
					pmbox->vport ? pmbox->vport->vpi : 0,
					pmbox->u.mb.mbxCommand,
					psli->sli_flag, flag);
4297
			goto out_not_finished;
已提交
4298 4299
		}

4300
		if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
J
James Smart 已提交
4301
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
4302
			/* Mbox command <mbxCommand> cannot issue */
4303 4304 4305 4306 4307 4308
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
					"(%d):2530 Mailbox command x%x "
					"cannot issue Data: x%x x%x\n",
					pmbox->vport ? pmbox->vport->vpi : 0,
					pmbox->u.mb.mbxCommand,
					psli->sli_flag, flag);
4309
			goto out_not_finished;
已提交
4310 4311 4312 4313 4314 4315 4316 4317
		}

		/* Another mailbox command is still being processed, queue this
		 * command to be processed later.
		 */
		lpfc_mbox_put(phba, pmbox);

		/* Mbox cmd issue - BUSY */
4318
		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4319
				"(%d):0308 Mbox cmd issue - BUSY Data: "
4320 4321 4322 4323
				"x%x x%x x%x x%x\n",
				pmbox->vport ? pmbox->vport->vpi : 0xffffff,
				mb->mbxCommand, phba->pport->port_state,
				psli->sli_flag, flag);
已提交
4324 4325

		psli->slistat.mbox_busy++;
J
James Smart 已提交
4326
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
4327

J
James Smart 已提交
4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342
		if (pmbox->vport) {
			lpfc_debugfs_disc_trc(pmbox->vport,
				LPFC_DISC_TRC_MBOX_VPORT,
				"MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
				(uint32_t)mb->mbxCommand,
				mb->un.varWords[0], mb->un.varWords[1]);
		}
		else {
			lpfc_debugfs_disc_trc(phba->pport,
				LPFC_DISC_TRC_MBOX,
				"MBOX Bsy:        cmd:x%x mb:x%x x%x",
				(uint32_t)mb->mbxCommand,
				mb->un.varWords[0], mb->un.varWords[1]);
		}

J
James Smart 已提交
4343
		return MBX_BUSY;
已提交
4344 4345 4346 4347 4348 4349
	}

	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;

	/* If we are not polling, we MUST be in SLI2 mode */
	if (flag != MBX_POLL) {
4350
		if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4351
		    (mb->mbxCommand != MBX_KILL_BOARD)) {
已提交
4352
			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
4353
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
4354
			/* Mbox command <mbxCommand> cannot issue */
4355 4356 4357 4358 4359 4360
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
					"(%d):2531 Mailbox command x%x "
					"cannot issue Data: x%x x%x\n",
					pmbox->vport ? pmbox->vport->vpi : 0,
					pmbox->u.mb.mbxCommand,
					psli->sli_flag, flag);
4361
			goto out_not_finished;
已提交
4362 4363
		}
		/* timeout active mbox command */
4364 4365
		mod_timer(&psli->mbox_tmo, (jiffies +
			       (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
已提交
4366 4367 4368
	}

	/* Mailbox cmd <cmd> issue */
4369
	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4370
			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4371
			"x%x\n",
4372
			pmbox->vport ? pmbox->vport->vpi : 0,
4373 4374
			mb->mbxCommand, phba->pport->port_state,
			psli->sli_flag, flag);
已提交
4375

J
James Smart 已提交
4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392
	if (mb->mbxCommand != MBX_HEARTBEAT) {
		if (pmbox->vport) {
			lpfc_debugfs_disc_trc(pmbox->vport,
				LPFC_DISC_TRC_MBOX_VPORT,
				"MBOX Send vport: cmd:x%x mb:x%x x%x",
				(uint32_t)mb->mbxCommand,
				mb->un.varWords[0], mb->un.varWords[1]);
		}
		else {
			lpfc_debugfs_disc_trc(phba->pport,
				LPFC_DISC_TRC_MBOX,
				"MBOX Send:       cmd:x%x mb:x%x x%x",
				(uint32_t)mb->mbxCommand,
				mb->un.varWords[0], mb->un.varWords[1]);
		}
	}

已提交
4393 4394 4395 4396 4397 4398
	psli->slistat.mbox_cmd++;
	evtctr = psli->slistat.mbox_event;

	/* next set own bit for the adapter and copy over command word */
	mb->mbxOwner = OWN_CHIP;

4399
	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
已提交
4400
		/* First copy command data to host SLIM area */
4401
		lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
已提交
4402
	} else {
4403
		if (mb->mbxCommand == MBX_CONFIG_PORT) {
已提交
4404
			/* copy command data into host mbox for cmpl */
4405
			lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
已提交
4406 4407 4408 4409 4410 4411 4412 4413 4414
		}

		/* First copy mbox command data to HBA SLIM, skip past first
		   word */
		to_slim = phba->MBslimaddr + sizeof (uint32_t);
		lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
			    MAILBOX_CMD_SIZE - sizeof (uint32_t));

		/* Next copy over first word, with mbxOwner set */
4415
		ldata = *((uint32_t *)mb);
已提交
4416 4417 4418 4419 4420 4421
		to_slim = phba->MBslimaddr;
		writel(ldata, to_slim);
		readl(to_slim); /* flush */

		if (mb->mbxCommand == MBX_CONFIG_PORT) {
			/* switch over to host mailbox */
4422
			psli->sli_flag |= LPFC_SLI_ACTIVE;
已提交
4423 4424 4425 4426 4427 4428 4429
		}
	}

	wmb();

	switch (flag) {
	case MBX_NOWAIT:
4430
		/* Set up reference to mailbox command */
已提交
4431
		psli->mbox_active = pmbox;
4432 4433 4434 4435
		/* Interrupt board to do it */
		writel(CA_MBATT, phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */
		/* Don't wait for it to finish, just return */
已提交
4436 4437 4438
		break;

	case MBX_POLL:
4439
		/* Set up null reference to mailbox command */
已提交
4440
		psli->mbox_active = NULL;
4441 4442 4443 4444
		/* Interrupt board to do it */
		writel(CA_MBATT, phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */

4445
		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
已提交
4446
			/* First read mbox status word */
4447
			word0 = *((uint32_t *)phba->mbox);
已提交
4448 4449 4450 4451 4452 4453 4454 4455
			word0 = le32_to_cpu(word0);
		} else {
			/* First read mbox status word */
			word0 = readl(phba->MBslimaddr);
		}

		/* Read the HBA Host Attention Register */
		ha_copy = readl(phba->HAregaddr);
4456 4457 4458 4459
		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
							     mb->mbxCommand) *
					   1000) + jiffies;
		i = 0;
已提交
4460
		/* Wait for command to complete */
4461 4462
		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
		       (!(ha_copy & HA_MBATT) &&
J
James Smart 已提交
4463
			(phba->link_state > LPFC_WARM_START))) {
4464
			if (time_after(jiffies, timeout)) {
已提交
4465
				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
4466
				spin_unlock_irqrestore(&phba->hbalock,
已提交
4467
						       drvr_flag);
4468
				goto out_not_finished;
已提交
4469 4470 4471 4472 4473 4474 4475 4476
			}

			/* Check if we took a mbox interrupt while we were
			   polling */
			if (((word0 & OWN_CHIP) != OWN_CHIP)
			    && (evtctr != psli->slistat.mbox_event))
				break;

4477 4478 4479 4480 4481 4482
			if (i++ > 10) {
				spin_unlock_irqrestore(&phba->hbalock,
						       drvr_flag);
				msleep(1);
				spin_lock_irqsave(&phba->hbalock, drvr_flag);
			}
已提交
4483

4484
			if (psli->sli_flag & LPFC_SLI_ACTIVE) {
已提交
4485
				/* First copy command data */
4486
				word0 = *((uint32_t *)phba->mbox);
已提交
4487 4488 4489
				word0 = le32_to_cpu(word0);
				if (mb->mbxCommand == MBX_CONFIG_PORT) {
					MAILBOX_t *slimmb;
4490
					uint32_t slimword0;
已提交
4491 4492 4493 4494 4495 4496
					/* Check real SLIM for any errors */
					slimword0 = readl(phba->MBslimaddr);
					slimmb = (MAILBOX_t *) & slimword0;
					if (((slimword0 & OWN_CHIP) != OWN_CHIP)
					    && slimmb->mbxStatus) {
						psli->sli_flag &=
4497
						    ~LPFC_SLI_ACTIVE;
已提交
4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508
						word0 = slimword0;
					}
				}
			} else {
				/* First copy command data */
				word0 = readl(phba->MBslimaddr);
			}
			/* Read the HBA Host Attention Register */
			ha_copy = readl(phba->HAregaddr);
		}

4509
		if (psli->sli_flag & LPFC_SLI_ACTIVE) {
已提交
4510
			/* copy results back to user */
4511
			lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
已提交
4512 4513 4514 4515 4516 4517
		} else {
			/* First copy command data */
			lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
							MAILBOX_CMD_SIZE);
			if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
				pmbox->context2) {
4518
				lpfc_memcpy_from_slim((void *)pmbox->context2,
已提交
4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530
				      phba->MBslimaddr + DMP_RSP_OFFSET,
						      mb->un.varDmp.word_cnt);
			}
		}

		writel(HA_MBATT, phba->HAregaddr);
		readl(phba->HAregaddr); /* flush */

		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
		status = mb->mbxStatus;
	}

J
James Smart 已提交
4531 4532
	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
	return status;
4533 4534 4535

out_not_finished:
	if (processing_queue) {
4536
		pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
4537 4538 4539
		lpfc_mbox_cmpl_put(phba, pmbox);
	}
	return MBX_NOT_FINISHED;
已提交
4540 4541
}

4542 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 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948
/**
 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
 * @phba: Pointer to HBA context object.
 * @mboxq: Pointer to mailbox object.
 *
 * The function posts a mailbox to the port.  The mailbox is expected
 * to be comletely filled in and ready for the port to operate on it.
 * This routine executes a synchronous completion operation on the
 * mailbox by polling for its completion.
 *
 * The caller must not be holding any locks when calling this routine.
 *
 * Returns:
 *	MBX_SUCCESS - mailbox posted successfully
 *	Any of the MBX error values.
 **/
static int
lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
{
	int rc = MBX_SUCCESS;
	unsigned long iflag;
	uint32_t db_ready;
	uint32_t mcqe_status;
	uint32_t mbx_cmnd;
	unsigned long timeout;
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_mqe *mb = &mboxq->u.mqe;
	struct lpfc_bmbx_create *mbox_rgn;
	struct dma_address *dma_address;
	struct lpfc_register bmbx_reg;

	/*
	 * Only one mailbox can be active to the bootstrap mailbox region
	 * at a time and there is no queueing provided.
	 */
	spin_lock_irqsave(&phba->hbalock, iflag);
	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
		spin_unlock_irqrestore(&phba->hbalock, iflag);
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2532 Mailbox command x%x (x%x) "
				"cannot issue Data: x%x x%x\n",
				mboxq->vport ? mboxq->vport->vpi : 0,
				mboxq->u.mb.mbxCommand,
				lpfc_sli4_mbox_opcode_get(phba, mboxq),
				psli->sli_flag, MBX_POLL);
		return MBXERR_ERROR;
	}
	/* The server grabs the token and owns it until release */
	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
	phba->sli.mbox_active = mboxq;
	spin_unlock_irqrestore(&phba->hbalock, iflag);

	/*
	 * Initialize the bootstrap memory region to avoid stale data areas
	 * in the mailbox post.  Then copy the caller's mailbox contents to
	 * the bmbx mailbox region.
	 */
	mbx_cmnd = bf_get(lpfc_mqe_command, mb);
	memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
	lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
			      sizeof(struct lpfc_mqe));

	/* Post the high mailbox dma address to the port and wait for ready. */
	dma_address = &phba->sli4_hba.bmbx.dma_address;
	writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);

	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
				   * 1000) + jiffies;
	do {
		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
		if (!db_ready)
			msleep(2);

		if (time_after(jiffies, timeout)) {
			rc = MBXERR_ERROR;
			goto exit;
		}
	} while (!db_ready);

	/* Post the low mailbox dma address to the port. */
	writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
	timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
				   * 1000) + jiffies;
	do {
		bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
		db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
		if (!db_ready)
			msleep(2);

		if (time_after(jiffies, timeout)) {
			rc = MBXERR_ERROR;
			goto exit;
		}
	} while (!db_ready);

	/*
	 * Read the CQ to ensure the mailbox has completed.
	 * If so, update the mailbox status so that the upper layers
	 * can complete the request normally.
	 */
	lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
			      sizeof(struct lpfc_mqe));
	mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
	lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
			      sizeof(struct lpfc_mcqe));
	mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);

	/* Prefix the mailbox status with range x4000 to note SLI4 status. */
	if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
		bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
		rc = MBXERR_ERROR;
	}

	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
			"(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
			"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
			" x%x x%x CQ: x%x x%x x%x x%x\n",
			mboxq->vport ? mboxq->vport->vpi : 0,
			mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
			bf_get(lpfc_mqe_status, mb),
			mb->un.mb_words[0], mb->un.mb_words[1],
			mb->un.mb_words[2], mb->un.mb_words[3],
			mb->un.mb_words[4], mb->un.mb_words[5],
			mb->un.mb_words[6], mb->un.mb_words[7],
			mb->un.mb_words[8], mb->un.mb_words[9],
			mb->un.mb_words[10], mb->un.mb_words[11],
			mb->un.mb_words[12], mboxq->mcqe.word0,
			mboxq->mcqe.mcqe_tag0, 	mboxq->mcqe.mcqe_tag1,
			mboxq->mcqe.trailer);
exit:
	/* We are holding the token, no needed for lock when release */
	spin_lock_irqsave(&phba->hbalock, iflag);
	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
	phba->sli.mbox_active = NULL;
	spin_unlock_irqrestore(&phba->hbalock, iflag);
	return rc;
}

/**
 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
 * @phba: Pointer to HBA context object.
 * @pmbox: Pointer to mailbox object.
 * @flag: Flag indicating how the mailbox need to be processed.
 *
 * This function is called by discovery code and HBA management code to submit
 * a mailbox command to firmware with SLI-4 interface spec.
 *
 * Return codes the caller owns the mailbox command after the return of the
 * function.
 **/
static int
lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
		       uint32_t flag)
{
	struct lpfc_sli *psli = &phba->sli;
	unsigned long iflags;
	int rc;

	/* Detect polling mode and jump to a handler */
	if (!phba->sli4_hba.intr_enable) {
		if (flag == MBX_POLL)
			rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
		else
			rc = -EIO;
		if (rc != MBX_SUCCESS)
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
					"(%d):2541 Mailbox command x%x "
					"(x%x) cannot issue Data: x%x x%x\n",
					mboxq->vport ? mboxq->vport->vpi : 0,
					mboxq->u.mb.mbxCommand,
					lpfc_sli4_mbox_opcode_get(phba, mboxq),
					psli->sli_flag, flag);
		return rc;
	} else if (flag == MBX_POLL) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2542 Mailbox command x%x (x%x) "
				"cannot issue Data: x%x x%x\n",
				mboxq->vport ? mboxq->vport->vpi : 0,
				mboxq->u.mb.mbxCommand,
				lpfc_sli4_mbox_opcode_get(phba, mboxq),
				psli->sli_flag, flag);
		return -EIO;
	}

	/* Now, interrupt mode asynchrous mailbox command */
	rc = lpfc_mbox_cmd_check(phba, mboxq);
	if (rc) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2543 Mailbox command x%x (x%x) "
				"cannot issue Data: x%x x%x\n",
				mboxq->vport ? mboxq->vport->vpi : 0,
				mboxq->u.mb.mbxCommand,
				lpfc_sli4_mbox_opcode_get(phba, mboxq),
				psli->sli_flag, flag);
		goto out_not_finished;
	}
	rc = lpfc_mbox_dev_check(phba);
	if (unlikely(rc)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2544 Mailbox command x%x (x%x) "
				"cannot issue Data: x%x x%x\n",
				mboxq->vport ? mboxq->vport->vpi : 0,
				mboxq->u.mb.mbxCommand,
				lpfc_sli4_mbox_opcode_get(phba, mboxq),
				psli->sli_flag, flag);
		goto out_not_finished;
	}

	/* Put the mailbox command to the driver internal FIFO */
	psli->slistat.mbox_busy++;
	spin_lock_irqsave(&phba->hbalock, iflags);
	lpfc_mbox_put(phba, mboxq);
	spin_unlock_irqrestore(&phba->hbalock, iflags);
	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
			"(%d):0354 Mbox cmd issue - Enqueue Data: "
			"x%x (x%x) x%x x%x x%x\n",
			mboxq->vport ? mboxq->vport->vpi : 0xffffff,
			bf_get(lpfc_mqe_command, &mboxq->u.mqe),
			lpfc_sli4_mbox_opcode_get(phba, mboxq),
			phba->pport->port_state,
			psli->sli_flag, MBX_NOWAIT);
	/* Wake up worker thread to transport mailbox command from head */
	lpfc_worker_wake_up(phba);

	return MBX_BUSY;

out_not_finished:
	return MBX_NOT_FINISHED;
}

/**
 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
 * @phba: Pointer to HBA context object.
 *
 * This function is called by worker thread to send a mailbox command to
 * SLI4 HBA firmware.
 *
 **/
int
lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
{
	struct lpfc_sli *psli = &phba->sli;
	LPFC_MBOXQ_t *mboxq;
	int rc = MBX_SUCCESS;
	unsigned long iflags;
	struct lpfc_mqe *mqe;
	uint32_t mbx_cmnd;

	/* Check interrupt mode before post async mailbox command */
	if (unlikely(!phba->sli4_hba.intr_enable))
		return MBX_NOT_FINISHED;

	/* Check for mailbox command service token */
	spin_lock_irqsave(&phba->hbalock, iflags);
	if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
		spin_unlock_irqrestore(&phba->hbalock, iflags);
		return MBX_NOT_FINISHED;
	}
	if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
		spin_unlock_irqrestore(&phba->hbalock, iflags);
		return MBX_NOT_FINISHED;
	}
	if (unlikely(phba->sli.mbox_active)) {
		spin_unlock_irqrestore(&phba->hbalock, iflags);
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"0384 There is pending active mailbox cmd\n");
		return MBX_NOT_FINISHED;
	}
	/* Take the mailbox command service token */
	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;

	/* Get the next mailbox command from head of queue */
	mboxq = lpfc_mbox_get(phba);

	/* If no more mailbox command waiting for post, we're done */
	if (!mboxq) {
		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
		spin_unlock_irqrestore(&phba->hbalock, iflags);
		return MBX_SUCCESS;
	}
	phba->sli.mbox_active = mboxq;
	spin_unlock_irqrestore(&phba->hbalock, iflags);

	/* Check device readiness for posting mailbox command */
	rc = lpfc_mbox_dev_check(phba);
	if (unlikely(rc))
		/* Driver clean routine will clean up pending mailbox */
		goto out_not_finished;

	/* Prepare the mbox command to be posted */
	mqe = &mboxq->u.mqe;
	mbx_cmnd = bf_get(lpfc_mqe_command, mqe);

	/* Start timer for the mbox_tmo and log some mailbox post messages */
	mod_timer(&psli->mbox_tmo, (jiffies +
		  (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));

	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
			"(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
			"x%x x%x\n",
			mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
			lpfc_sli4_mbox_opcode_get(phba, mboxq),
			phba->pport->port_state, psli->sli_flag);

	if (mbx_cmnd != MBX_HEARTBEAT) {
		if (mboxq->vport) {
			lpfc_debugfs_disc_trc(mboxq->vport,
				LPFC_DISC_TRC_MBOX_VPORT,
				"MBOX Send vport: cmd:x%x mb:x%x x%x",
				mbx_cmnd, mqe->un.mb_words[0],
				mqe->un.mb_words[1]);
		} else {
			lpfc_debugfs_disc_trc(phba->pport,
				LPFC_DISC_TRC_MBOX,
				"MBOX Send: cmd:x%x mb:x%x x%x",
				mbx_cmnd, mqe->un.mb_words[0],
				mqe->un.mb_words[1]);
		}
	}
	psli->slistat.mbox_cmd++;

	/* Post the mailbox command to the port */
	rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
	if (rc != MBX_SUCCESS) {
		lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
				"(%d):2533 Mailbox command x%x (x%x) "
				"cannot issue Data: x%x x%x\n",
				mboxq->vport ? mboxq->vport->vpi : 0,
				mboxq->u.mb.mbxCommand,
				lpfc_sli4_mbox_opcode_get(phba, mboxq),
				psli->sli_flag, MBX_NOWAIT);
		goto out_not_finished;
	}

	return rc;

out_not_finished:
	spin_lock_irqsave(&phba->hbalock, iflags);
	mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
	__lpfc_mbox_cmpl_put(phba, mboxq);
	/* Release the token */
	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
	phba->sli.mbox_active = NULL;
	spin_unlock_irqrestore(&phba->hbalock, iflags);

	return MBX_NOT_FINISHED;
}

/**
 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
 * @phba: Pointer to HBA context object.
 * @pmbox: Pointer to mailbox object.
 * @flag: Flag indicating how the mailbox need to be processed.
 *
 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
 * the API jump table function pointer from the lpfc_hba struct.
 *
 * Return codes the caller owns the mailbox command after the return of the
 * function.
 **/
int
lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
{
	return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
}

/**
 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
 * @phba: The hba struct for which this call is being executed.
 * @dev_grp: The HBA PCI-Device group number.
 *
 * This routine sets up the mbox interface API function jump table in @phba
 * struct.
 * Returns: 0 - success, -ENODEV - failure.
 **/
int
lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
{

	switch (dev_grp) {
	case LPFC_PCI_DEV_LP:
		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
		phba->lpfc_sli_handle_slow_ring_event =
				lpfc_sli_handle_slow_ring_event_s3;
		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
		phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
		break;
	case LPFC_PCI_DEV_OC:
		phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
		phba->lpfc_sli_handle_slow_ring_event =
				lpfc_sli_handle_slow_ring_event_s4;
		phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
		phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
		phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
		break;
	default:
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"1420 Invalid HBA PCI-device group: 0x%x\n",
				dev_grp);
		return -ENODEV;
		break;
	}
	return 0;
}

4949
/**
4950
 * __lpfc_sli_ringtx_put - Add an iocb to the txq
4951 4952 4953 4954 4955 4956 4957 4958
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @piocb: Pointer to address of newly added command iocb.
 *
 * This function is called with hbalock held to add a command
 * iocb to the txq when SLI layer cannot submit the command iocb
 * to the ring.
 **/
J
James Smart 已提交
4959
static void
4960
__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
J
James Smart 已提交
4961
		    struct lpfc_iocbq *piocb)
已提交
4962 4963 4964 4965 4966 4967
{
	/* Insert the caller's iocb in the txq tail for later processing. */
	list_add_tail(&piocb->list, &pring->txq);
	pring->txq_cnt++;
}

4968
/**
4969
 * lpfc_sli_next_iocb - Get the next iocb in the txq
4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @piocb: Pointer to address of newly added command iocb.
 *
 * This function is called with hbalock held before a new
 * iocb is submitted to the firmware. This function checks
 * txq to flush the iocbs in txq to Firmware before
 * submitting new iocbs to the Firmware.
 * If there are iocbs in the txq which need to be submitted
 * to firmware, lpfc_sli_next_iocb returns the first element
 * of the txq after dequeuing it from txq.
 * If there is no iocb in the txq then the function will return
 * *piocb and *piocb is set to NULL. Caller needs to check
 * *piocb to find if there are more commands in the txq.
 **/
已提交
4985 4986
static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
J
James Smart 已提交
4987
		   struct lpfc_iocbq **piocb)
已提交
4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999
{
	struct lpfc_iocbq * nextiocb;

	nextiocb = lpfc_sli_ringtx_get(phba, pring);
	if (!nextiocb) {
		nextiocb = *piocb;
		*piocb = NULL;
	}

	return nextiocb;
}

5000
/**
5001
 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5002
 * @phba: Pointer to HBA context object.
5003
 * @ring_number: SLI ring number to issue iocb on.
5004 5005 5006
 * @piocb: Pointer to command iocb.
 * @flag: Flag indicating if this command can be put into txq.
 *
5007 5008 5009 5010 5011 5012 5013 5014 5015 5016
 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
 * this function allows only iocbs for posting buffers. This function finds
 * next available slot in the command ring and posts the command to the
 * available slot and writes the port attention register to request HBA start
 * processing new iocb. If there is no slot available in the ring and
 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
 * the function returns IOCB_BUSY.
5017
 *
5018 5019 5020
 * This function is called with hbalock held. The function will return success
 * after it successfully submit the iocb to firmware or after adding to the
 * txq.
5021
 **/
5022
static int
5023
__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
已提交
5024 5025 5026 5027
		    struct lpfc_iocbq *piocb, uint32_t flag)
{
	struct lpfc_iocbq *nextiocb;
	IOCB_t *iocb;
5028
	struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
已提交
5029

5030 5031 5032 5033 5034
	if (piocb->iocb_cmpl && (!piocb->vport) &&
	   (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
	   (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
		lpfc_printf_log(phba, KERN_ERR,
				LOG_SLI | LOG_VPORT,
5035
				"1807 IOCB x%x failed. No vport\n",
5036 5037 5038 5039 5040 5041
				piocb->iocb.ulpCommand);
		dump_stack();
		return IOCB_ERROR;
	}


5042 5043 5044 5045
	/* If the PCI channel is in offline state, do not post iocbs. */
	if (unlikely(pci_channel_offline(phba->pcidev)))
		return IOCB_ERROR;

5046 5047 5048 5049
	/* If HBA has a deferred error attention, fail the iocb. */
	if (unlikely(phba->hba_flag & DEFER_ERATT))
		return IOCB_ERROR;

已提交
5050 5051 5052
	/*
	 * We should never get an IOCB if we are in a < LINK_DOWN state
	 */
J
James Smart 已提交
5053
	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
已提交
5054 5055 5056 5057
		return IOCB_ERROR;

	/*
	 * Check to see if we are blocking IOCB processing because of a
5058
	 * outstanding event.
已提交
5059
	 */
5060
	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
已提交
5061 5062
		goto iocb_busy;

J
James Smart 已提交
5063
	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
已提交
5064
		/*
5065
		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
已提交
5066 5067 5068
		 * can be issued if the link is not up.
		 */
		switch (piocb->iocb.ulpCommand) {
5069 5070 5071 5072 5073 5074 5075 5076 5077 5078
		case CMD_GEN_REQUEST64_CR:
		case CMD_GEN_REQUEST64_CX:
			if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
				(piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
					FC_FCP_CMND) ||
				(piocb->iocb.un.genreq64.w5.hcsw.Type !=
					MENLO_TRANSPORT_TYPE))

				goto iocb_busy;
			break;
已提交
5079 5080 5081 5082 5083 5084 5085 5086 5087 5088
		case CMD_QUE_RING_BUF_CN:
		case CMD_QUE_RING_BUF64_CN:
			/*
			 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
			 * completion, iocb_cmpl MUST be 0.
			 */
			if (piocb->iocb_cmpl)
				piocb->iocb_cmpl = NULL;
			/*FALLTHROUGH*/
		case CMD_CREATE_XRI_CR:
5089 5090
		case CMD_CLOSE_XRI_CN:
		case CMD_CLOSE_XRI_CX:
已提交
5091 5092 5093 5094 5095 5096 5097 5098 5099 5100
			break;
		default:
			goto iocb_busy;
		}

	/*
	 * For FCP commands, we must be in a state where we can process link
	 * attention events.
	 */
	} else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5101
			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
已提交
5102
		goto iocb_busy;
5103
	}
已提交
5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124

	while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
	       (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
		lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);

	if (iocb)
		lpfc_sli_update_ring(phba, pring);
	else
		lpfc_sli_update_full_ring(phba, pring);

	if (!piocb)
		return IOCB_SUCCESS;

	goto out_busy;

 iocb_busy:
	pring->stats.iocb_cmd_delay++;

 out_busy:

	if (!(flag & SLI_IOCB_RET_IOCB)) {
5125
		__lpfc_sli_ringtx_put(phba, pring, piocb);
已提交
5126 5127 5128 5129 5130 5131
		return IOCB_SUCCESS;
	}

	return IOCB_BUSY;
}

5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177
/**
 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
 *
 * This routine wraps the actual lockless version for issusing IOCB function
 * pointer from the lpfc_hba struct.
 *
 * Return codes:
 * 	IOCB_ERROR - Error
 * 	IOCB_SUCCESS - Success
 * 	IOCB_BUSY - Busy
 **/
static inline int
__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
		struct lpfc_iocbq *piocb, uint32_t flag)
{
	return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
}

/**
 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
 * @phba: The hba struct for which this call is being executed.
 * @dev_grp: The HBA PCI-Device group number.
 *
 * This routine sets up the SLI interface API function jump table in @phba
 * struct.
 * Returns: 0 - success, -ENODEV - failure.
 **/
int
lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
{

	switch (dev_grp) {
	case LPFC_PCI_DEV_LP:
		phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
		phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
		break;
	default:
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"1419 Invalid HBA PCI-device group: 0x%x\n",
				dev_grp);
		return -ENODEV;
		break;
	}
	phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
	return 0;
}
5178

5179
/**
5180
 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @piocb: Pointer to command iocb.
 * @flag: Flag indicating if this command can be put into txq.
 *
 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
 * function. This function gets the hbalock and calls
 * __lpfc_sli_issue_iocb function and will return the error returned
 * by __lpfc_sli_issue_iocb function. This wrapper is used by
 * functions which do not hold hbalock.
 **/
5192
int
5193
lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
5194 5195 5196 5197 5198 5199
		    struct lpfc_iocbq *piocb, uint32_t flag)
{
	unsigned long iflags;
	int rc;

	spin_lock_irqsave(&phba->hbalock, iflags);
5200
	rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
5201 5202 5203 5204 5205
	spin_unlock_irqrestore(&phba->hbalock, iflags);

	return rc;
}

5206
/**
5207
 * lpfc_extra_ring_setup - Extra ring setup function
5208 5209 5210 5211 5212 5213 5214 5215 5216
 * @phba: Pointer to HBA context object.
 *
 * This function is called while driver attaches with the
 * HBA to setup the extra ring. The extra ring is used
 * only when driver needs to support target mode functionality
 * or IP over FC functionalities.
 *
 * This function is called with no lock held.
 **/
5217 5218 5219 5220 5221 5222 5223 5224 5225
static int
lpfc_extra_ring_setup( struct lpfc_hba *phba)
{
	struct lpfc_sli *psli;
	struct lpfc_sli_ring *pring;

	psli = &phba->sli;

	/* Adjust cmd/rsp ring iocb entries more evenly */
5226 5227

	/* Take some away from the FCP ring */
5228 5229 5230 5231 5232 5233
	pring = &psli->ring[psli->fcp_ring];
	pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
	pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
	pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
	pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;

5234 5235 5236
	/* and give them to the extra ring */
	pring = &psli->ring[psli->extra_ring];

5237 5238 5239 5240 5241 5242 5243 5244 5245
	pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
	pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
	pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
	pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;

	/* Setup default profile for this ring */
	pring->iotag_max = 4096;
	pring->num_mask = 1;
	pring->prt[0].profile = 0;      /* Mask 0 */
5246 5247
	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
	pring->prt[0].type = phba->cfg_multi_ring_type;
5248 5249 5250 5251
	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
	return 0;
}

5252
/**
5253
 * lpfc_sli_async_event_handler - ASYNC iocb handler function
5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @iocbq: Pointer to iocb object.
 *
 * This function is called by the slow ring event handler
 * function when there is an ASYNC event iocb in the ring.
 * This function is called with no lock held.
 * Currently this function handles only temperature related
 * ASYNC events. The function decodes the temperature sensor
 * event message and posts events for the management applications.
 **/
5265
static void
5266 5267 5268 5269 5270 5271 5272 5273
lpfc_sli_async_event_handler(struct lpfc_hba * phba,
	struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
{
	IOCB_t *icmd;
	uint16_t evt_code;
	uint16_t temp;
	struct temp_event temp_event_data;
	struct Scsi_Host *shost;
5274
	uint32_t *iocb_w;
5275 5276 5277 5278 5279 5280 5281

	icmd = &iocbq->iocb;
	evt_code = icmd->un.asyncstat.evt_code;
	temp = icmd->ulpContext;

	if ((evt_code != ASYNC_TEMP_WARN) &&
		(evt_code != ASYNC_TEMP_SAFE)) {
5282
		iocb_w = (uint32_t *) icmd;
5283 5284 5285
		lpfc_printf_log(phba,
			KERN_ERR,
			LOG_SLI,
5286
			"0346 Ring %d handler: unexpected ASYNC_STATUS"
5287 5288 5289 5290 5291
			" evt_code 0x%x \n"
			"W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
			"W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
			"W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
			"W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
5292
			pring->ringno,
5293 5294 5295 5296 5297 5298
			icmd->un.asyncstat.evt_code,
			iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
			iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
			iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
			iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);

5299 5300 5301 5302 5303 5304 5305
		return;
	}
	temp_event_data.data = (uint32_t)temp;
	temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
	if (evt_code == ASYNC_TEMP_WARN) {
		temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
		lpfc_printf_log(phba,
5306
				KERN_ERR,
5307
				LOG_TEMP,
5308
				"0347 Adapter is very hot, please take "
5309 5310 5311 5312 5313 5314
				"corrective action. temperature : %d Celsius\n",
				temp);
	}
	if (evt_code == ASYNC_TEMP_SAFE) {
		temp_event_data.event_code = LPFC_NORMAL_TEMP;
		lpfc_printf_log(phba,
5315
				KERN_ERR,
5316 5317 5318 5319 5320 5321 5322 5323 5324 5325
				LOG_TEMP,
				"0340 Adapter temperature is OK now. "
				"temperature : %d Celsius\n",
				temp);
	}

	/* Send temperature change event to applications */
	shost = lpfc_shost_from_vport(phba->pport);
	fc_host_post_vendor_event(shost, fc_get_event_number(),
		sizeof(temp_event_data), (char *) &temp_event_data,
5326
		LPFC_NL_VENDOR_ID);
5327 5328 5329 5330

}


5331
/**
5332
 * lpfc_sli_setup - SLI ring setup function
5333 5334 5335 5336 5337 5338 5339 5340 5341
 * @phba: Pointer to HBA context object.
 *
 * lpfc_sli_setup sets up rings of the SLI interface with
 * number of iocbs per ring and iotags. This function is
 * called while driver attach to the HBA and before the
 * interrupts are enabled. So there is no need for locking.
 *
 * This function always returns 0.
 **/
已提交
5342 5343 5344
int
lpfc_sli_setup(struct lpfc_hba *phba)
{
5345
	int i, totiocbsize = 0;
已提交
5346 5347 5348 5349 5350 5351 5352
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring;

	psli->num_rings = MAX_CONFIGURED_RINGS;
	psli->sli_flag = 0;
	psli->fcp_ring = LPFC_FCP_RING;
	psli->next_ring = LPFC_FCP_NEXT_RING;
5353
	psli->extra_ring = LPFC_EXTRA_RING;
已提交
5354

5355 5356 5357 5358
	psli->iocbq_lookup = NULL;
	psli->iocbq_lookup_len = 0;
	psli->last_iotag = 0;

已提交
5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
		switch (i) {
		case LPFC_FCP_RING:	/* ring 0 - FCP */
			/* numCiocb and numRiocb are used in config_port */
			pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
			pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
			pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
			pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
			pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
			pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
5370
			pring->sizeCiocb = (phba->sli_rev == 3) ?
5371 5372
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
5373
			pring->sizeRiocb = (phba->sli_rev == 3) ?
5374 5375
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
已提交
5376 5377
			pring->iotag_ctr = 0;
			pring->iotag_max =
5378
			    (phba->cfg_hba_queue_depth * 2);
已提交
5379 5380 5381
			pring->fast_iotag = pring->iotag_max;
			pring->num_mask = 0;
			break;
5382
		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
已提交
5383 5384 5385
			/* numCiocb and numRiocb are used in config_port */
			pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
			pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
5386
			pring->sizeCiocb = (phba->sli_rev == 3) ?
5387 5388
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
5389
			pring->sizeRiocb = (phba->sli_rev == 3) ?
5390 5391
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
J
James Smart 已提交
5392
			pring->iotag_max = phba->cfg_hba_queue_depth;
已提交
5393 5394 5395 5396 5397 5398
			pring->num_mask = 0;
			break;
		case LPFC_ELS_RING:	/* ring 2 - ELS / CT */
			/* numCiocb and numRiocb are used in config_port */
			pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
			pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
5399
			pring->sizeCiocb = (phba->sli_rev == 3) ?
5400 5401
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
5402
			pring->sizeRiocb = (phba->sli_rev == 3) ?
5403 5404
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
已提交
5405 5406 5407
			pring->fast_iotag = 0;
			pring->iotag_ctr = 0;
			pring->iotag_max = 4096;
5408 5409
			pring->lpfc_sli_rcv_async_status =
				lpfc_sli_async_event_handler;
已提交
5410 5411 5412 5413 5414
			pring->num_mask = 4;
			pring->prt[0].profile = 0;	/* Mask 0 */
			pring->prt[0].rctl = FC_ELS_REQ;
			pring->prt[0].type = FC_ELS_DATA;
			pring->prt[0].lpfc_sli_rcv_unsol_event =
5415
			    lpfc_els_unsol_event;
已提交
5416 5417 5418 5419
			pring->prt[1].profile = 0;	/* Mask 1 */
			pring->prt[1].rctl = FC_ELS_RSP;
			pring->prt[1].type = FC_ELS_DATA;
			pring->prt[1].lpfc_sli_rcv_unsol_event =
5420
			    lpfc_els_unsol_event;
已提交
5421 5422 5423 5424 5425 5426
			pring->prt[2].profile = 0;	/* Mask 2 */
			/* NameServer Inquiry */
			pring->prt[2].rctl = FC_UNSOL_CTL;
			/* NameServer */
			pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
			pring->prt[2].lpfc_sli_rcv_unsol_event =
5427
			    lpfc_ct_unsol_event;
已提交
5428 5429 5430 5431 5432 5433
			pring->prt[3].profile = 0;	/* Mask 3 */
			/* NameServer response */
			pring->prt[3].rctl = FC_SOL_CTL;
			/* NameServer */
			pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
			pring->prt[3].lpfc_sli_rcv_unsol_event =
5434
			    lpfc_ct_unsol_event;
已提交
5435 5436
			break;
		}
5437
		totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
5438
				(pring->numRiocb * pring->sizeRiocb);
已提交
5439
	}
5440
	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
已提交
5441
		/* Too many cmd / rsp ring entries in SLI2 SLIM */
5442 5443 5444 5445
		printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
		       "SLI2 SLIM Data: x%x x%lx\n",
		       phba->brd_no, totiocbsize,
		       (unsigned long) MAX_SLIM_IOCB_SIZE);
已提交
5446
	}
5447 5448
	if (phba->cfg_multi_ring_support == 2)
		lpfc_extra_ring_setup(phba);
已提交
5449 5450 5451 5452

	return 0;
}

5453
/**
5454
 * lpfc_sli_queue_setup - Queue initialization function
5455 5456 5457 5458 5459 5460 5461 5462 5463
 * @phba: Pointer to HBA context object.
 *
 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
 * ring. This function also initializes ring indices of each ring.
 * This function is called during the initialization of the SLI
 * interface of an HBA.
 * This function is called with no lock held and always returns
 * 1.
 **/
已提交
5464
int
J
James Smart 已提交
5465
lpfc_sli_queue_setup(struct lpfc_hba *phba)
已提交
5466 5467 5468
{
	struct lpfc_sli *psli;
	struct lpfc_sli_ring *pring;
5469
	int i;
已提交
5470 5471

	psli = &phba->sli;
J
James Smart 已提交
5472
	spin_lock_irq(&phba->hbalock);
已提交
5473
	INIT_LIST_HEAD(&psli->mboxq);
5474
	INIT_LIST_HEAD(&psli->mboxq_cmpl);
已提交
5475 5476 5477 5478 5479 5480 5481 5482 5483 5484
	/* Initialize list headers for txq and txcmplq as double linked lists */
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
		pring->ringno = i;
		pring->next_cmdidx  = 0;
		pring->local_getidx = 0;
		pring->cmdidx = 0;
		INIT_LIST_HEAD(&pring->txq);
		INIT_LIST_HEAD(&pring->txcmplq);
		INIT_LIST_HEAD(&pring->iocb_continueq);
5485
		INIT_LIST_HEAD(&pring->iocb_continue_saveq);
已提交
5486 5487
		INIT_LIST_HEAD(&pring->postbufq);
	}
J
James Smart 已提交
5488 5489
	spin_unlock_irq(&phba->hbalock);
	return 1;
已提交
5490 5491
}

5492
/**
5493
 * lpfc_sli_host_down - Vport cleanup function
5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508
 * @vport: Pointer to virtual port object.
 *
 * lpfc_sli_host_down is called to clean up the resources
 * associated with a vport before destroying virtual
 * port data structures.
 * This function does following operations:
 * - Free discovery resources associated with this virtual
 *   port.
 * - Free iocbs associated with this virtual port in
 *   the txq.
 * - Send abort for all iocb commands associated with this
 *   vport in txcmplq.
 *
 * This function is called with no lock held and always returns 1.
 **/
5509 5510 5511
int
lpfc_sli_host_down(struct lpfc_vport *vport)
{
J
James Smart 已提交
5512
	LIST_HEAD(completions);
5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring;
	struct lpfc_iocbq *iocb, *next_iocb;
	int i;
	unsigned long flags = 0;
	uint16_t prev_pring_flag;

	lpfc_cleanup_discovery_resources(vport);

	spin_lock_irqsave(&phba->hbalock, flags);
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
		prev_pring_flag = pring->flag;
5527 5528
		/* Only slow rings */
		if (pring->ringno == LPFC_ELS_RING) {
J
James Smart 已提交
5529
			pring->flag |= LPFC_DEFERRED_RING_EVENT;
5530 5531 5532
			/* Set the lpfc data pending flag */
			set_bit(LPFC_DATA_READY, &phba->data_flags);
		}
5533 5534 5535 5536 5537 5538 5539
		/*
		 * Error everything on the txq since these iocbs have not been
		 * given to the FW yet.
		 */
		list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
			if (iocb->vport != vport)
				continue;
J
James Smart 已提交
5540
			list_move_tail(&iocb->list, &completions);
5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556
			pring->txq_cnt--;
		}

		/* Next issue ABTS for everything on the txcmplq */
		list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
									list) {
			if (iocb->vport != vport)
				continue;
			lpfc_sli_issue_abort_iotag(phba, pring, iocb);
		}

		pring->flag = prev_pring_flag;
	}

	spin_unlock_irqrestore(&phba->hbalock, flags);

5557 5558 5559
	/* Cancel all the IOCBs from the completions list */
	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
			      IOERR_SLI_DOWN);
5560 5561 5562
	return 1;
}

5563
/**
5564
 * lpfc_sli_hba_down - Resource cleanup function for the HBA
5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577
 * @phba: Pointer to HBA context object.
 *
 * This function cleans up all iocb, buffers, mailbox commands
 * while shutting down the HBA. This function is called with no
 * lock held and always returns 1.
 * This function does the following to cleanup driver resources:
 * - Free discovery resources for each virtual port
 * - Cleanup any pending fabric iocbs
 * - Iterate through the iocb txq and free each entry
 *   in the list.
 * - Free up any buffer posted to the HBA
 * - Free mailbox commands in the mailbox queue.
 **/
已提交
5578
int
J
James Smart 已提交
5579
lpfc_sli_hba_down(struct lpfc_hba *phba)
已提交
5580
{
5581
	LIST_HEAD(completions);
J
James Smart 已提交
5582
	struct lpfc_sli *psli = &phba->sli;
已提交
5583
	struct lpfc_sli_ring *pring;
5584
	struct lpfc_dmabuf *buf_ptr;
已提交
5585 5586 5587 5588 5589 5590
	LPFC_MBOXQ_t *pmb;
	int i;
	unsigned long flags = 0;

	lpfc_hba_down_prep(phba);

5591 5592
	lpfc_fabric_abort_hba(phba);

J
James Smart 已提交
5593
	spin_lock_irqsave(&phba->hbalock, flags);
已提交
5594 5595
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
5596 5597
		/* Only slow rings */
		if (pring->ringno == LPFC_ELS_RING) {
J
James Smart 已提交
5598
			pring->flag |= LPFC_DEFERRED_RING_EVENT;
5599 5600 5601
			/* Set the lpfc data pending flag */
			set_bit(LPFC_DATA_READY, &phba->data_flags);
		}
已提交
5602 5603 5604 5605 5606

		/*
		 * Error everything on the txq since these iocbs have not been
		 * given to the FW yet.
		 */
5607
		list_splice_init(&pring->txq, &completions);
已提交
5608 5609
		pring->txq_cnt = 0;

5610
	}
J
James Smart 已提交
5611
	spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
5612

5613 5614 5615
	/* Cancel all the IOCBs from the completions list */
	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
			      IOERR_SLI_DOWN);
已提交
5616

5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629
	spin_lock_irqsave(&phba->hbalock, flags);
	list_splice_init(&phba->elsbuf, &completions);
	phba->elsbuf_cnt = 0;
	phba->elsbuf_prev_cnt = 0;
	spin_unlock_irqrestore(&phba->hbalock, flags);

	while (!list_empty(&completions)) {
		list_remove_head(&completions, buf_ptr,
			struct lpfc_dmabuf, list);
		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
		kfree(buf_ptr);
	}

已提交
5630 5631
	/* Return any active mbox cmds */
	del_timer_sync(&psli->mbox_tmo);
J
James Smart 已提交
5632

5633
	spin_lock_irqsave(&phba->pport->work_port_lock, flags);
J
James Smart 已提交
5634
	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
5635
	spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
J
James Smart 已提交
5636

5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666
	return 1;
}

/**
 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
 * @phba: Pointer to HBA context object.
 *
 * This function cleans up all queues, iocb, buffers, mailbox commands while
 * shutting down the SLI4 HBA FCoE function. This function is called with no
 * lock held and always returns 1.
 *
 * This function does the following to cleanup driver FCoE function resources:
 * - Free discovery resources for each virtual port
 * - Cleanup any pending fabric iocbs
 * - Iterate through the iocb txq and free each entry in the list.
 * - Free up any buffer posted to the HBA.
 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
 * - Free mailbox commands in the mailbox queue.
 **/
int
lpfc_sli4_hba_down(struct lpfc_hba *phba)
{
	/* Stop the SLI4 device port */
	lpfc_stop_port(phba);

	/* Tear down the queues in the HBA */
	lpfc_sli4_queue_unset(phba);

	/* unregister default FCFI from the HBA */
	lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
5667

已提交
5668 5669 5670
	return 1;
}

5671
/**
5672
 * lpfc_sli_pcimem_bcopy - SLI memory copy function
5673 5674 5675 5676 5677 5678 5679 5680 5681 5682
 * @srcp: Source memory pointer.
 * @destp: Destination memory pointer.
 * @cnt: Number of words required to be copied.
 *
 * This function is used for copying data between driver memory
 * and the SLI memory. This function also changes the endianness
 * of each word if native endianness is different from SLI
 * endianness. This function can be called with or without
 * lock.
 **/
已提交
5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699
void
lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
{
	uint32_t *src = srcp;
	uint32_t *dest = destp;
	uint32_t ldata;
	int i;

	for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
		ldata = *src;
		ldata = le32_to_cpu(ldata);
		*dest = ldata;
		src++;
		dest++;
	}
}

5700 5701

/**
5702
 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
5703 5704 5705 5706 5707 5708 5709 5710
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @mp: Pointer to driver buffer object.
 *
 * This function is called with no lock held.
 * It always return zero after adding the buffer to the postbufq
 * buffer list.
 **/
已提交
5711
int
J
James Smart 已提交
5712 5713
lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			 struct lpfc_dmabuf *mp)
已提交
5714 5715 5716
{
	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
	   later */
J
James Smart 已提交
5717
	spin_lock_irq(&phba->hbalock);
已提交
5718 5719
	list_add_tail(&mp->list, &pring->postbufq);
	pring->postbufq_cnt++;
J
James Smart 已提交
5720
	spin_unlock_irq(&phba->hbalock);
已提交
5721 5722 5723
	return 0;
}

5724
/**
5725
 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
5726 5727 5728 5729 5730 5731 5732 5733 5734
 * @phba: Pointer to HBA context object.
 *
 * When HBQ is enabled, buffers are searched based on tags. This function
 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
 * does not conflict with tags of buffer posted for unsolicited events.
 * The function returns the allocated tag. The function is called with
 * no locks held.
 **/
5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748
uint32_t
lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
{
	spin_lock_irq(&phba->hbalock);
	phba->buffer_tag_count++;
	/*
	 * Always set the QUE_BUFTAG_BIT to distiguish between
	 * a tag assigned by HBQ.
	 */
	phba->buffer_tag_count |= QUE_BUFTAG_BIT;
	spin_unlock_irq(&phba->hbalock);
	return phba->buffer_tag_count;
}

5749
/**
5750
 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @tag: Buffer tag.
 *
 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
 * iocb is posted to the response ring with the tag of the buffer.
 * This function searches the pring->postbufq list using the tag
 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
 * iocb. If the buffer is found then lpfc_dmabuf object of the
 * buffer is returned to the caller else NULL is returned.
 * This function is called with no lock held.
 **/
5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783
struct lpfc_dmabuf *
lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			uint32_t tag)
{
	struct lpfc_dmabuf *mp, *next_mp;
	struct list_head *slp = &pring->postbufq;

	/* Search postbufq, from the begining, looking for a match on tag */
	spin_lock_irq(&phba->hbalock);
	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
		if (mp->buffer_tag == tag) {
			list_del_init(&mp->list);
			pring->postbufq_cnt--;
			spin_unlock_irq(&phba->hbalock);
			return mp;
		}
	}

	spin_unlock_irq(&phba->hbalock);
	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5784
			"0402 Cannot find virtual addr for buffer tag on "
5785 5786 5787 5788 5789 5790
			"ring %d Data x%lx x%p x%p x%x\n",
			pring->ringno, (unsigned long) tag,
			slp->next, slp->prev, pring->postbufq_cnt);

	return NULL;
}
已提交
5791

5792
/**
5793
 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @phys: DMA address of the buffer.
 *
 * This function searches the buffer list using the dma_address
 * of unsolicited event to find the driver's lpfc_dmabuf object
 * corresponding to the dma_address. The function returns the
 * lpfc_dmabuf object if a buffer is found else it returns NULL.
 * This function is called by the ct and els unsolicited event
 * handlers to get the buffer associated with the unsolicited
 * event.
 *
 * This function is called with no lock held.
 **/
已提交
5808 5809 5810 5811 5812 5813 5814 5815
struct lpfc_dmabuf *
lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			 dma_addr_t phys)
{
	struct lpfc_dmabuf *mp, *next_mp;
	struct list_head *slp = &pring->postbufq;

	/* Search postbufq, from the begining, looking for a match on phys */
J
James Smart 已提交
5816
	spin_lock_irq(&phba->hbalock);
已提交
5817 5818 5819 5820
	list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
		if (mp->phys == phys) {
			list_del_init(&mp->list);
			pring->postbufq_cnt--;
J
James Smart 已提交
5821
			spin_unlock_irq(&phba->hbalock);
已提交
5822 5823 5824 5825
			return mp;
		}
	}

J
James Smart 已提交
5826
	spin_unlock_irq(&phba->hbalock);
已提交
5827
	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5828
			"0410 Cannot find virtual addr for mapped buf on "
已提交
5829
			"ring %d Data x%llx x%p x%p x%x\n",
5830
			pring->ringno, (unsigned long long)phys,
已提交
5831 5832 5833 5834
			slp->next, slp->prev, pring->postbufq_cnt);
	return NULL;
}

5835
/**
5836
 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
5837 5838 5839 5840 5841 5842 5843 5844 5845
 * @phba: Pointer to HBA context object.
 * @cmdiocb: Pointer to driver command iocb object.
 * @rspiocb: Pointer to driver response iocb object.
 *
 * This function is the completion handler for the abort iocbs for
 * ELS commands. This function is called from the ELS ring event
 * handler with no lock held. This function frees memory resources
 * associated with the abort iocb.
 **/
已提交
5846
static void
J
James Smart 已提交
5847 5848
lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
已提交
5849
{
J
James Smart 已提交
5850
	IOCB_t *irsp = &rspiocb->iocb;
5851
	uint16_t abort_iotag, abort_context;
5852
	struct lpfc_iocbq *abort_iocb;
5853 5854 5855 5856 5857 5858 5859 5860
	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];

	abort_iocb = NULL;

	if (irsp->ulpStatus) {
		abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
		abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;

J
James Smart 已提交
5861
		spin_lock_irq(&phba->hbalock);
5862 5863 5864
		if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
			abort_iocb = phba->sli.iocbq_lookup[abort_iotag];

5865
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
5866
				"0327 Cannot abort els iocb %p "
5867 5868
				"with tag %x context %x, abort status %x, "
				"abort code %x\n",
5869 5870
				abort_iocb, abort_iotag, abort_context,
				irsp->ulpStatus, irsp->un.ulpWord[4]);
5871

5872 5873 5874 5875
		/*
		 *  If the iocb is not found in Firmware queue the iocb
		 *  might have completed already. Do not free it again.
		 */
5876
		if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
5877 5878 5879 5880
			spin_unlock_irq(&phba->hbalock);
			lpfc_sli_release_iocbq(phba, cmdiocb);
			return;
		}
5881 5882 5883 5884
		/*
		 * make sure we have the right iocbq before taking it
		 * off the txcmplq and try to call completion routine.
		 */
J
James Smart 已提交
5885 5886 5887 5888 5889
		if (!abort_iocb ||
		    abort_iocb->iocb.ulpContext != abort_context ||
		    (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
			spin_unlock_irq(&phba->hbalock);
		else {
5890
			list_del_init(&abort_iocb->list);
5891
			pring->txcmplq_cnt--;
J
James Smart 已提交
5892
			spin_unlock_irq(&phba->hbalock);
5893

5894 5895 5896 5897 5898 5899
			/* Firmware could still be in progress of DMAing
			 * payload, so don't free data buffer till after
			 * a hbeat.
			 */
			abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;

5900 5901 5902 5903
			abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
			abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
			abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
			(abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
5904 5905 5906
		}
	}

5907
	lpfc_sli_release_iocbq(phba, cmdiocb);
已提交
5908 5909 5910
	return;
}

5911
/**
5912
 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
5913 5914 5915 5916 5917 5918 5919 5920 5921
 * @phba: Pointer to HBA context object.
 * @cmdiocb: Pointer to driver command iocb object.
 * @rspiocb: Pointer to driver response iocb object.
 *
 * The function is called from SLI ring event handler with no
 * lock held. This function is the completion handler for ELS commands
 * which are aborted. The function frees memory resources used for
 * the aborted ELS commands.
 **/
5922 5923 5924 5925 5926 5927 5928 5929
static void
lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
		     struct lpfc_iocbq *rspiocb)
{
	IOCB_t *irsp = &rspiocb->iocb;

	/* ELS cmd tag <ulpIoTag> completes */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
5930
			"0139 Ignoring ELS cmd tag x%x completion Data: "
5931
			"x%x x%x x%x\n",
5932
			irsp->ulpIoTag, irsp->ulpStatus,
5933
			irsp->un.ulpWord[4], irsp->ulpTimeout);
J
James Smart 已提交
5934 5935 5936 5937
	if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
		lpfc_ct_free_iocb(phba, cmdiocb);
	else
		lpfc_els_free_iocb(phba, cmdiocb);
5938 5939 5940
	return;
}

5941
/**
5942
 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
5943 5944 5945 5946 5947 5948 5949 5950 5951
 * @phba: Pointer to HBA context object.
 * @pring: Pointer to driver SLI ring object.
 * @cmdiocb: Pointer to driver command iocb object.
 *
 * This function issues an abort iocb for the provided command
 * iocb. This function is called with hbalock held.
 * The function returns 0 when it fails due to memory allocation
 * failure or when the command iocb is an abort request.
 **/
已提交
5952
int
J
James Smart 已提交
5953 5954
lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			   struct lpfc_iocbq *cmdiocb)
已提交
5955
{
J
James Smart 已提交
5956
	struct lpfc_vport *vport = cmdiocb->vport;
5957
	struct lpfc_iocbq *abtsiocbp;
已提交
5958 5959
	IOCB_t *icmd = NULL;
	IOCB_t *iabt = NULL;
5960 5961
	int retval = IOCB_ERROR;

5962 5963 5964 5965
	/*
	 * There are certain command types we don't want to abort.  And we
	 * don't want to abort commands that are already in the process of
	 * being aborted.
5966 5967
	 */
	icmd = &cmdiocb->iocb;
J
James Smart 已提交
5968
	if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
5969 5970
	    icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
	    (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
5971 5972
		return 0;

J
James Smart 已提交
5973 5974
	/* If we're unloading, don't abort iocb on the ELS ring, but change the
	 * callback so that nothing happens when it finishes.
5975
	 */
J
James Smart 已提交
5976 5977
	if ((vport->load_flag & FC_UNLOADING) &&
	    (pring->ringno == LPFC_ELS_RING)) {
5978 5979 5980 5981
		if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
			cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
		else
			cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
5982
		goto abort_iotag_exit;
5983
	}
已提交
5984 5985

	/* issue ABTS for this IOCB based on iotag */
5986
	abtsiocbp = __lpfc_sli_get_iocbq(phba);
已提交
5987 5988 5989
	if (abtsiocbp == NULL)
		return 0;

5990 5991 5992 5993 5994
	/* This signals the response to set the correct status
	 * before calling the completion handler.
	 */
	cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;

已提交
5995
	iabt = &abtsiocbp->iocb;
5996 5997
	iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
	iabt->un.acxri.abortContextTag = icmd->ulpContext;
5998 5999 6000 6001
	if (phba->sli_rev == LPFC_SLI_REV4)
		iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
	else
		iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
6002 6003
	iabt->ulpLe = 1;
	iabt->ulpClass = icmd->ulpClass;
已提交
6004

J
James Smart 已提交
6005
	if (phba->link_state >= LPFC_LINK_UP)
6006 6007 6008
		iabt->ulpCommand = CMD_ABORT_XRI_CN;
	else
		iabt->ulpCommand = CMD_CLOSE_XRI_CN;
已提交
6009

6010
	abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
6011

6012 6013 6014 6015 6016
	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
			 "0339 Abort xri x%x, original iotag x%x, "
			 "abort cmd iotag x%x\n",
			 iabt->un.acxri.abortContextTag,
			 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
6017
	retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
已提交
6018

6019 6020
	if (retval)
		__lpfc_sli_release_iocbq(phba, abtsiocbp);
6021
abort_iotag_exit:
J
James Smart 已提交
6022 6023 6024 6025
	/*
	 * Caller to this routine should check for IOCB_ERROR
	 * and handle it properly.  This routine no longer removes
	 * iocb off txcmplq and call compl in case of IOCB_ERROR.
6026
	 */
J
James Smart 已提交
6027
	return retval;
已提交
6028 6029
}

6030
/**
6031
 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
6032 6033 6034 6035 6036 6037
 * @iocbq: Pointer to driver iocb object.
 * @vport: Pointer to driver virtual port object.
 * @tgt_id: SCSI ID of the target.
 * @lun_id: LUN ID of the scsi device.
 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
 *
6038
 * This function acts as an iocb filter for functions which abort or count
6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051
 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
 * 0 if the filtering criteria is met for the given iocb and will return
 * 1 if the filtering criteria is not met.
 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
 * given iocb is for the SCSI device specified by vport, tgt_id and
 * lun_id parameter.
 * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
 * given iocb is for the SCSI target specified by vport and tgt_id
 * parameters.
 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
 * given iocb is for the SCSI host associated with the given vport.
 * This function is called with no locks held.
 **/
已提交
6052
static int
6053 6054
lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
			   uint16_t tgt_id, uint64_t lun_id,
6055
			   lpfc_ctx_cmd ctx_cmd)
已提交
6056
{
6057
	struct lpfc_scsi_buf *lpfc_cmd;
已提交
6058 6059
	int rc = 1;

6060 6061 6062
	if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
		return rc;

6063 6064 6065
	if (iocbq->vport != vport)
		return rc;

6066 6067
	lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);

6068
	if (lpfc_cmd->pCmd == NULL)
已提交
6069 6070 6071 6072
		return rc;

	switch (ctx_cmd) {
	case LPFC_CTX_LUN:
6073 6074 6075
		if ((lpfc_cmd->rdata->pnode) &&
		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
		    (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
已提交
6076 6077 6078
			rc = 0;
		break;
	case LPFC_CTX_TGT:
6079 6080
		if ((lpfc_cmd->rdata->pnode) &&
		    (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
已提交
6081 6082 6083 6084 6085 6086 6087
			rc = 0;
		break;
	case LPFC_CTX_HOST:
		rc = 0;
		break;
	default:
		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
6088
			__func__, ctx_cmd);
已提交
6089 6090 6091 6092 6093 6094
		break;
	}

	return rc;
}

6095
/**
6096
 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113
 * @vport: Pointer to virtual port.
 * @tgt_id: SCSI ID of the target.
 * @lun_id: LUN ID of the scsi device.
 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
 *
 * This function returns number of FCP commands pending for the vport.
 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
 * commands pending on the vport associated with SCSI device specified
 * by tgt_id and lun_id parameters.
 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
 * commands pending on the vport associated with SCSI target specified
 * by tgt_id parameter.
 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
 * commands pending on the vport.
 * This function returns the number of iocbs which satisfy the filter.
 * This function is called without any lock held.
 **/
已提交
6114
int
6115 6116
lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
		  lpfc_ctx_cmd ctx_cmd)
已提交
6117
{
6118
	struct lpfc_hba *phba = vport->phba;
6119 6120
	struct lpfc_iocbq *iocbq;
	int sum, i;
已提交
6121

6122 6123
	for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
		iocbq = phba->sli.iocbq_lookup[i];
已提交
6124

6125 6126
		if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
						ctx_cmd) == 0)
6127
			sum++;
已提交
6128
	}
6129

已提交
6130 6131 6132
	return sum;
}

6133
/**
6134
 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
6135 6136 6137 6138 6139 6140 6141 6142
 * @phba: Pointer to HBA context object
 * @cmdiocb: Pointer to command iocb object.
 * @rspiocb: Pointer to response iocb object.
 *
 * This function is called when an aborted FCP iocb completes. This
 * function is called by the ring event handler with no lock held.
 * This function frees the iocb.
 **/
6143
void
J
James Smart 已提交
6144 6145
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
6146
{
6147
	lpfc_sli_release_iocbq(phba, cmdiocb);
6148 6149 6150
	return;
}

6151
/**
6152
 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171
 * @vport: Pointer to virtual port.
 * @pring: Pointer to driver SLI ring object.
 * @tgt_id: SCSI ID of the target.
 * @lun_id: LUN ID of the scsi device.
 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
 *
 * This function sends an abort command for every SCSI command
 * associated with the given virtual port pending on the ring
 * filtered by lpfc_sli_validate_fcp_iocb function.
 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
 * FCP iocbs associated with lun specified by tgt_id and lun_id
 * parameters
 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
 * FCP iocbs associated with virtual port.
 * This function returns number of iocbs it failed to abort.
 * This function is called with no locks held.
 **/
已提交
6172
int
6173 6174
lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
		    uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
已提交
6175
{
6176
	struct lpfc_hba *phba = vport->phba;
6177 6178
	struct lpfc_iocbq *iocbq;
	struct lpfc_iocbq *abtsiocb;
已提交
6179 6180
	IOCB_t *cmd = NULL;
	int errcnt = 0, ret_val = 0;
6181
	int i;
已提交
6182

6183 6184
	for (i = 1; i <= phba->sli.last_iotag; i++) {
		iocbq = phba->sli.iocbq_lookup[i];
已提交
6185

6186
		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
J
James Smart 已提交
6187
					       abort_cmd) != 0)
已提交
6188 6189 6190
			continue;

		/* issue ABTS for this IOCB based on iotag */
6191
		abtsiocb = lpfc_sli_get_iocbq(phba);
已提交
6192 6193 6194 6195 6196
		if (abtsiocb == NULL) {
			errcnt++;
			continue;
		}

6197
		cmd = &iocbq->iocb;
已提交
6198 6199
		abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
		abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
6200 6201 6202 6203
		if (phba->sli_rev == LPFC_SLI_REV4)
			abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
		else
			abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
已提交
6204 6205
		abtsiocb->iocb.ulpLe = 1;
		abtsiocb->iocb.ulpClass = cmd->ulpClass;
J
James Smart 已提交
6206
		abtsiocb->vport = phba->pport;
已提交
6207

J
James Smart 已提交
6208
		if (lpfc_is_link_up(phba))
已提交
6209 6210 6211 6212
			abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
		else
			abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;

6213 6214
		/* Setup callback routine and issue the command. */
		abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
6215 6216
		ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
					      abtsiocb, 0);
已提交
6217
		if (ret_val == IOCB_ERROR) {
6218
			lpfc_sli_release_iocbq(phba, abtsiocb);
已提交
6219 6220 6221 6222 6223 6224 6225 6226
			errcnt++;
			continue;
		}
	}

	return errcnt;
}

6227
/**
6228
 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243
 * @phba: Pointer to HBA context object.
 * @cmdiocbq: Pointer to command iocb.
 * @rspiocbq: Pointer to response iocb.
 *
 * This function is the completion handler for iocbs issued using
 * lpfc_sli_issue_iocb_wait function. This function is called by the
 * ring event handler function without any lock held. This function
 * can be called from both worker thread context and interrupt
 * context. This function also can be called from other thread which
 * cleans up the SLI layer objects.
 * This function copy the contents of the response iocb to the
 * response iocb memory object provided by the caller of
 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
 * sleeps for the iocb completion.
 **/
6244 6245 6246 6247
static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
已提交
6248
{
6249 6250
	wait_queue_head_t *pdone_q;
	unsigned long iflags;
已提交
6251

J
James Smart 已提交
6252
	spin_lock_irqsave(&phba->hbalock, iflags);
6253 6254 6255 6256 6257 6258 6259 6260
	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
	if (cmdiocbq->context2 && rspiocbq)
		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
		       &rspiocbq->iocb, sizeof(IOCB_t));

	pdone_q = cmdiocbq->context_un.wait_queue;
	if (pdone_q)
		wake_up(pdone_q);
J
James Smart 已提交
6261
	spin_unlock_irqrestore(&phba->hbalock, iflags);
已提交
6262 6263 6264
	return;
}

6265
/**
6266
 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294
 * @phba: Pointer to HBA context object..
 * @pring: Pointer to sli ring.
 * @piocb: Pointer to command iocb.
 * @prspiocbq: Pointer to response iocb.
 * @timeout: Timeout in number of seconds.
 *
 * This function issues the iocb to firmware and waits for the
 * iocb to complete. If the iocb command is not
 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
 * Caller should not free the iocb resources if this function
 * returns IOCB_TIMEDOUT.
 * The function waits for the iocb completion using an
 * non-interruptible wait.
 * This function will sleep while waiting for iocb completion.
 * So, this function should not be called from any context which
 * does not allow sleeping. Due to the same reason, this function
 * cannot be called with interrupt disabled.
 * This function assumes that the iocb completions occur while
 * this function sleep. So, this function cannot be called from
 * the thread which process iocb completion for this ring.
 * This function clears the iocb_flag of the iocb object before
 * issuing the iocb and the iocb completion handler sets this
 * flag and wakes this thread when the iocb completes.
 * The contents of the response iocb will be copied to prspiocbq
 * by the completion handler when the command completes.
 * This function returns IOCB_SUCCESS when success.
 * This function is called with no lock held.
 **/
已提交
6295
int
J
James Smart 已提交
6296
lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
6297
			 uint32_t ring_number,
J
James Smart 已提交
6298 6299
			 struct lpfc_iocbq *piocb,
			 struct lpfc_iocbq *prspiocbq,
6300
			 uint32_t timeout)
已提交
6301
{
6302
	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
6303 6304
	long timeleft, timeout_req = 0;
	int retval = IOCB_SUCCESS;
6305
	uint32_t creg_val;
已提交
6306 6307

	/*
6308 6309
	 * If the caller has provided a response iocbq buffer, then context2
	 * is NULL or its an error.
已提交
6310
	 */
6311 6312 6313 6314
	if (prspiocbq) {
		if (piocb->context2)
			return IOCB_ERROR;
		piocb->context2 = prspiocbq;
已提交
6315 6316
	}

6317 6318 6319
	piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
	piocb->context_un.wait_queue = &done_q;
	piocb->iocb_flag &= ~LPFC_IO_WAKE;
已提交
6320

6321 6322 6323 6324 6325 6326 6327
	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
		creg_val = readl(phba->HCregaddr);
		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
		writel(creg_val, phba->HCregaddr);
		readl(phba->HCregaddr); /* flush */
	}

6328
	retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
6329 6330 6331 6332 6333
	if (retval == IOCB_SUCCESS) {
		timeout_req = timeout * HZ;
		timeleft = wait_event_timeout(done_q,
				piocb->iocb_flag & LPFC_IO_WAKE,
				timeout_req);
已提交
6334

6335 6336
		if (piocb->iocb_flag & LPFC_IO_WAKE) {
			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6337
					"0331 IOCB wake signaled\n");
6338
		} else if (timeleft == 0) {
6339
			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6340 6341
					"0338 IOCB wait timeout error - no "
					"wake response Data x%x\n", timeout);
6342
			retval = IOCB_TIMEDOUT;
6343
		} else {
6344
			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6345 6346
					"0330 IOCB wake NOT set, "
					"Data x%x x%lx\n",
6347 6348
					timeout, (timeleft / jiffies));
			retval = IOCB_TIMEDOUT;
已提交
6349
		}
6350 6351
	} else {
		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6352
				"0332 IOCB wait issue failed, Data x%x\n",
6353
				retval);
6354
		retval = IOCB_ERROR;
已提交
6355 6356
	}

6357 6358 6359 6360 6361 6362 6363
	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
		creg_val = readl(phba->HCregaddr);
		creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
		writel(creg_val, phba->HCregaddr);
		readl(phba->HCregaddr); /* flush */
	}

6364 6365 6366 6367 6368
	if (prspiocbq)
		piocb->context2 = NULL;

	piocb->context_un.wait_queue = NULL;
	piocb->iocb_cmpl = NULL;
已提交
6369 6370
	return retval;
}
6371

6372
/**
6373
 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to driver mailbox object.
 * @timeout: Timeout in number of seconds.
 *
 * This function issues the mailbox to firmware and waits for the
 * mailbox command to complete. If the mailbox command is not
 * completed within timeout seconds, it returns MBX_TIMEOUT.
 * The function waits for the mailbox completion using an
 * interruptible wait. If the thread is woken up due to a
 * signal, MBX_TIMEOUT error is returned to the caller. Caller
 * should not free the mailbox resources, if this function returns
 * MBX_TIMEOUT.
 * This function will sleep while waiting for mailbox completion.
 * So, this function should not be called from any context which
 * does not allow sleeping. Due to the same reason, this function
 * cannot be called with interrupt disabled.
 * This function assumes that the mailbox completion occurs while
 * this function sleep. So, this function cannot be called from
 * the worker thread which processes mailbox completion.
 * This function is called in the context of HBA management
 * applications.
 * This function returns MBX_SUCCESS when successful.
 * This function is called with no lock held.
 **/
已提交
6398
int
J
James Smart 已提交
6399
lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
已提交
6400 6401
			 uint32_t timeout)
{
6402
	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
已提交
6403
	int retval;
J
James Smart 已提交
6404
	unsigned long flag;
已提交
6405 6406

	/* The caller must leave context1 empty. */
6407
	if (pmboxq->context1)
J
James Smart 已提交
6408
		return MBX_NOT_FINISHED;
已提交
6409

6410
	pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
已提交
6411 6412 6413 6414 6415 6416 6417 6418 6419
	/* setup wake call as IOCB callback */
	pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
	/* setup context field to pass wait_queue pointer to wake function  */
	pmboxq->context1 = &done_q;

	/* now issue the command */
	retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);

	if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
6420 6421 6422 6423
		wait_event_interruptible_timeout(done_q,
				pmboxq->mbox_flag & LPFC_MBX_WAKE,
				timeout * HZ);

J
James Smart 已提交
6424
		spin_lock_irqsave(&phba->hbalock, flag);
已提交
6425
		pmboxq->context1 = NULL;
6426 6427 6428 6429 6430
		/*
		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
		 * else do not free the resources.
		 */
		if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
已提交
6431
			retval = MBX_SUCCESS;
J
James Smart 已提交
6432
		else {
6433
			retval = MBX_TIMEOUT;
J
James Smart 已提交
6434 6435 6436
			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
		spin_unlock_irqrestore(&phba->hbalock, flag);
已提交
6437 6438 6439 6440 6441
	}

	return retval;
}

6442
/**
6443
 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
6444 6445
 * @phba: Pointer to HBA context.
 *
6446 6447 6448 6449 6450 6451 6452 6453 6454 6455
 * This function is called to shutdown the driver's mailbox sub-system.
 * It first marks the mailbox sub-system is in a block state to prevent
 * the asynchronous mailbox command from issued off the pending mailbox
 * command queue. If the mailbox command sub-system shutdown is due to
 * HBA error conditions such as EEH or ERATT, this routine shall invoke
 * the mailbox sub-system flush routine to forcefully bring down the
 * mailbox sub-system. Otherwise, if it is due to normal condition (such
 * as with offline or HBA function reset), this routine will wait for the
 * outstanding mailbox command to complete before invoking the mailbox
 * sub-system flush routine to gracefully bring down mailbox sub-system.
6456
 **/
6457 6458
void
lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
6459
{
6460 6461 6462
	struct lpfc_sli *psli = &phba->sli;
	uint8_t actcmd = MBX_HEARTBEAT;
	unsigned long timeout;
6463

6464 6465 6466
	spin_lock_irq(&phba->hbalock);
	psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
	spin_unlock_irq(&phba->hbalock);
6467

6468
	if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6469
		spin_lock_irq(&phba->hbalock);
6470 6471
		if (phba->sli.mbox_active)
			actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
6472
		spin_unlock_irq(&phba->hbalock);
6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489
		/* Determine how long we might wait for the active mailbox
		 * command to be gracefully completed by firmware.
		 */
		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
					   1000) + jiffies;
		while (phba->sli.mbox_active) {
			/* Check active mailbox complete status every 2ms */
			msleep(2);
			if (time_after(jiffies, timeout))
				/* Timeout, let the mailbox flush routine to
				 * forcefully release active mailbox command
				 */
				break;
		}
	}
	lpfc_sli_mbox_sys_flush(phba);
}
6490

6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505
/**
 * lpfc_sli_eratt_read - read sli-3 error attention events
 * @phba: Pointer to HBA context.
 *
 * This function is called to read the SLI3 device error attention registers
 * for possible error attention events. The caller must hold the hostlock
 * with spin_lock_irq().
 *
 * This fucntion returns 1 when there is Error Attention in the Host Attention
 * Register and returns 0 otherwise.
 **/
static int
lpfc_sli_eratt_read(struct lpfc_hba *phba)
{
	uint32_t ha_copy;
6506

6507 6508 6509 6510 6511
	/* Read chip Host Attention (HA) register */
	ha_copy = readl(phba->HAregaddr);
	if (ha_copy & HA_ERATT) {
		/* Read host status register to retrieve error event */
		lpfc_sli_read_hs(phba);
6512

6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533
		/* Check if there is a deferred error condition is active */
		if ((HS_FFER1 & phba->work_hs) &&
		    ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
		     HS_FFER6 | HS_FFER7) & phba->work_hs)) {
			spin_lock_irq(&phba->hbalock);
			phba->hba_flag |= DEFER_ERATT;
			spin_unlock_irq(&phba->hbalock);
			/* Clear all interrupt enable conditions */
			writel(0, phba->HCregaddr);
			readl(phba->HCregaddr);
		}

		/* Set the driver HA work bitmap */
		spin_lock_irq(&phba->hbalock);
		phba->work_ha |= HA_ERATT;
		/* Indicate polling handles this ERATT */
		phba->hba_flag |= HBA_ERATT_HANDLED;
		spin_unlock_irq(&phba->hbalock);
		return 1;
	}
	return 0;
6534 6535
}

6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587
/**
 * lpfc_sli4_eratt_read - read sli-4 error attention events
 * @phba: Pointer to HBA context.
 *
 * This function is called to read the SLI4 device error attention registers
 * for possible error attention events. The caller must hold the hostlock
 * with spin_lock_irq().
 *
 * This fucntion returns 1 when there is Error Attention in the Host Attention
 * Register and returns 0 otherwise.
 **/
static int
lpfc_sli4_eratt_read(struct lpfc_hba *phba)
{
	uint32_t uerr_sta_hi, uerr_sta_lo;
	uint32_t onlnreg0, onlnreg1;

	/* For now, use the SLI4 device internal unrecoverable error
	 * registers for error attention. This can be changed later.
	 */
	onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
	onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
	if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
		uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
		uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
		if (uerr_sta_lo || uerr_sta_hi) {
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
					"1423 HBA Unrecoverable error: "
					"uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
					"online0_reg=0x%x, online1_reg=0x%x\n",
					uerr_sta_lo, uerr_sta_hi,
					onlnreg0, onlnreg1);
			/* TEMP: as the driver error recover logic is not
			 * fully developed, we just log the error message
			 * and the device error attention action is now
			 * temporarily disabled.
			 */
			return 0;
			phba->work_status[0] = uerr_sta_lo;
			phba->work_status[1] = uerr_sta_hi;
			spin_lock_irq(&phba->hbalock);
			/* Set the driver HA work bitmap */
			phba->work_ha |= HA_ERATT;
			/* Indicate polling handles this ERATT */
			phba->hba_flag |= HBA_ERATT_HANDLED;
			spin_unlock_irq(&phba->hbalock);
			return 1;
		}
	}
	return 0;
}

6588
/**
6589
 * lpfc_sli_check_eratt - check error attention events
6590 6591
 * @phba: Pointer to HBA context.
 *
6592
 * This function is called from timer soft interrupt context to check HBA's
6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616
 * error attention register bit for error attention events.
 *
 * This fucntion returns 1 when there is Error Attention in the Host Attention
 * Register and returns 0 otherwise.
 **/
int
lpfc_sli_check_eratt(struct lpfc_hba *phba)
{
	uint32_t ha_copy;

	/* If somebody is waiting to handle an eratt, don't process it
	 * here. The brdkill function will do this.
	 */
	if (phba->link_flag & LS_IGNORE_ERATT)
		return 0;

	/* Check if interrupt handler handles this ERATT */
	spin_lock_irq(&phba->hbalock);
	if (phba->hba_flag & HBA_ERATT_HANDLED) {
		/* Interrupt handler has handled ERATT */
		spin_unlock_irq(&phba->hbalock);
		return 0;
	}

6617 6618 6619 6620 6621 6622 6623 6624 6625
	/*
	 * If there is deferred error attention, do not check for error
	 * attention
	 */
	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
		spin_unlock_irq(&phba->hbalock);
		return 0;
	}

6626 6627
	/* If PCI channel is offline, don't process it */
	if (unlikely(pci_channel_offline(phba->pcidev))) {
6628
		spin_unlock_irq(&phba->hbalock);
6629 6630 6631 6632 6633 6634 6635 6636 6637
		return 0;
	}

	switch (phba->sli_rev) {
	case LPFC_SLI_REV2:
	case LPFC_SLI_REV3:
		/* Read chip Host Attention (HA) register */
		ha_copy = lpfc_sli_eratt_read(phba);
		break;
6638 6639 6640 6641
	case LPFC_SLI_REV4:
		/* Read devcie Uncoverable Error (UERR) registers */
		ha_copy = lpfc_sli4_eratt_read(phba);
		break;
6642 6643 6644 6645 6646 6647
	default:
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
				"0299 Invalid SLI revision (%d)\n",
				phba->sli_rev);
		ha_copy = 0;
		break;
6648 6649
	}
	spin_unlock_irq(&phba->hbalock);
6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677

	return ha_copy;
}

/**
 * lpfc_intr_state_check - Check device state for interrupt handling
 * @phba: Pointer to HBA context.
 *
 * This inline routine checks whether a device or its PCI slot is in a state
 * that the interrupt should be handled.
 *
 * This function returns 0 if the device or the PCI slot is in a state that
 * interrupt should be handled, otherwise -EIO.
 */
static inline int
lpfc_intr_state_check(struct lpfc_hba *phba)
{
	/* If the pci channel is offline, ignore all the interrupts */
	if (unlikely(pci_channel_offline(phba->pcidev)))
		return -EIO;

	/* Update device level interrupt statistics */
	phba->sli.slistat.sli_intr++;

	/* Ignore all interrupts during initialization. */
	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
		return -EIO;

6678 6679 6680 6681
	return 0;
}

/**
6682
 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
6683 6684 6685
 * @irq: Interrupt number.
 * @dev_id: The device context pointer.
 *
6686
 * This function is directly called from the PCI layer as an interrupt
6687 6688 6689 6690 6691 6692 6693 6694 6695 6696
 * service routine when device with SLI-3 interface spec is enabled with
 * MSI-X multi-message interrupt mode and there are slow-path events in
 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
 * interrupt mode, this function is called as part of the device-level
 * interrupt handler. When the PCI slot is in error recovery or the HBA
 * is undergoing initialization, the interrupt handler will not process
 * the interrupt. The link attention and ELS ring attention events are
 * handled by the worker thread. The interrupt handler signals the worker
 * thread and returns for these events. This function is called without
 * any lock held. It gets the hbalock to access and update SLI data
6697 6698 6699 6700
 * structures.
 *
 * This function returns IRQ_HANDLED when interrupt is handled else it
 * returns IRQ_NONE.
6701
 **/
已提交
6702
irqreturn_t
6703
lpfc_sli_sp_intr_handler(int irq, void *dev_id)
已提交
6704
{
J
James Smart 已提交
6705
	struct lpfc_hba  *phba;
已提交
6706 6707 6708
	uint32_t ha_copy;
	uint32_t work_ha_copy;
	unsigned long status;
6709
	unsigned long iflag;
已提交
6710 6711
	uint32_t control;

6712
	MAILBOX_t *mbox, *pmbox;
J
James Smart 已提交
6713 6714 6715
	struct lpfc_vport *vport;
	struct lpfc_nodelist *ndlp;
	struct lpfc_dmabuf *mp;
6716 6717 6718
	LPFC_MBOXQ_t *pmb;
	int rc;

已提交
6719 6720 6721 6722
	/*
	 * Get the driver's phba structure from the dev_id and
	 * assume the HBA is not interrupting.
	 */
6723
	phba = (struct lpfc_hba *)dev_id;
已提交
6724 6725 6726 6727 6728

	if (unlikely(!phba))
		return IRQ_NONE;

	/*
6729 6730
	 * Stuff needs to be attented to when this function is invoked as an
	 * individual interrupt handler in MSI-X multi-message interrupt mode
已提交
6731
	 */
6732
	if (phba->intr_type == MSIX) {
6733 6734
		/* Check device state for handling interrupt */
		if (lpfc_intr_state_check(phba))
6735 6736
			return IRQ_NONE;
		/* Need to read HA REG for slow-path events */
6737
		spin_lock_irqsave(&phba->hbalock, iflag);
6738
		ha_copy = readl(phba->HAregaddr);
6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752
		/* If somebody is waiting to handle an eratt don't process it
		 * here. The brdkill function will do this.
		 */
		if (phba->link_flag & LS_IGNORE_ERATT)
			ha_copy &= ~HA_ERATT;
		/* Check the need for handling ERATT in interrupt handler */
		if (ha_copy & HA_ERATT) {
			if (phba->hba_flag & HBA_ERATT_HANDLED)
				/* ERATT polling has handled ERATT */
				ha_copy &= ~HA_ERATT;
			else
				/* Indicate interrupt handler handles ERATT */
				phba->hba_flag |= HBA_ERATT_HANDLED;
		}
6753 6754 6755 6756 6757 6758

		/*
		 * If there is deferred error attention, do not check for any
		 * interrupt.
		 */
		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6759
			spin_unlock_irqrestore(&phba->hbalock, iflag);
6760 6761 6762
			return IRQ_NONE;
		}

6763 6764 6765 6766
		/* Clear up only attention source related to slow-path */
		writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
			phba->HAregaddr);
		readl(phba->HAregaddr); /* flush */
6767
		spin_unlock_irqrestore(&phba->hbalock, iflag);
6768 6769
	} else
		ha_copy = phba->ha_copy;
已提交
6770 6771 6772

	work_ha_copy = ha_copy & phba->work_ha_mask;

6773
	if (work_ha_copy) {
已提交
6774 6775 6776 6777 6778 6779
		if (work_ha_copy & HA_LATT) {
			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
				/*
				 * Turn off Link Attention interrupts
				 * until CLEAR_LA done
				 */
6780
				spin_lock_irqsave(&phba->hbalock, iflag);
已提交
6781 6782 6783 6784 6785
				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
				control = readl(phba->HCregaddr);
				control &= ~HC_LAINT_ENA;
				writel(control, phba->HCregaddr);
				readl(phba->HCregaddr); /* flush */
6786
				spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
6787 6788 6789 6790 6791
			}
			else
				work_ha_copy &= ~HA_LATT;
		}

6792
		if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
J
James Smart 已提交
6793 6794 6795 6796 6797 6798 6799 6800
			/*
			 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
			 * the only slow ring.
			 */
			status = (work_ha_copy &
				(HA_RXMASK  << (4*LPFC_ELS_RING)));
			status >>= (4*LPFC_ELS_RING);
			if (status & HA_RXMASK) {
6801
				spin_lock_irqsave(&phba->hbalock, iflag);
J
James Smart 已提交
6802
				control = readl(phba->HCregaddr);
6803 6804 6805 6806 6807 6808

				lpfc_debugfs_slow_ring_trc(phba,
				"ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
				control, status,
				(uint32_t)phba->sli.slistat.sli_intr);

J
James Smart 已提交
6809
				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
6810 6811 6812 6813 6814
					lpfc_debugfs_slow_ring_trc(phba,
						"ISR Disable ring:"
						"pwork:x%x hawork:x%x wait:x%x",
						phba->work_ha, work_ha_copy,
						(uint32_t)((unsigned long)
6815
						&phba->work_waitq));
6816

J
James Smart 已提交
6817 6818
					control &=
					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
已提交
6819 6820 6821
					writel(control, phba->HCregaddr);
					readl(phba->HCregaddr); /* flush */
				}
6822 6823 6824 6825 6826 6827
				else {
					lpfc_debugfs_slow_ring_trc(phba,
						"ISR slow ring:   pwork:"
						"x%x hawork:x%x wait:x%x",
						phba->work_ha, work_ha_copy,
						(uint32_t)((unsigned long)
6828
						&phba->work_waitq));
6829
				}
6830
				spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
6831 6832
			}
		}
6833
		spin_lock_irqsave(&phba->hbalock, iflag);
6834
		if (work_ha_copy & HA_ERATT) {
6835
			lpfc_sli_read_hs(phba);
6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849
			/*
			 * Check if there is a deferred error condition
			 * is active
			 */
			if ((HS_FFER1 & phba->work_hs) &&
				((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
				HS_FFER6 | HS_FFER7) & phba->work_hs)) {
				phba->hba_flag |= DEFER_ERATT;
				/* Clear all interrupt enable conditions */
				writel(0, phba->HCregaddr);
				readl(phba->HCregaddr);
			}
		}

6850
		if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
6851 6852
			pmb = phba->sli.mbox_active;
			pmbox = &pmb->mb;
6853
			mbox = phba->mbox;
J
James Smart 已提交
6854
			vport = pmb->vport;
6855 6856 6857 6858

			/* First check out the status word */
			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
			if (pmbox->mbxOwner != OWN_HOST) {
6859
				spin_unlock_irqrestore(&phba->hbalock, iflag);
6860 6861 6862 6863
				/*
				 * Stray Mailbox Interrupt, mbxCommand <cmd>
				 * mbxStatus <status>
				 */
6864
				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
6865
						LOG_SLI,
6866
						"(%d):0304 Stray Mailbox "
6867 6868
						"Interrupt mbxCommand x%x "
						"mbxStatus x%x\n",
6869
						(vport ? vport->vpi : 0),
6870 6871
						pmbox->mbxCommand,
						pmbox->mbxStatus);
6872 6873 6874
				/* clear mailbox attention bit */
				work_ha_copy &= ~HA_MBATT;
			} else {
6875
				phba->sli.mbox_active = NULL;
6876
				spin_unlock_irqrestore(&phba->hbalock, iflag);
6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912
				phba->last_completion_time = jiffies;
				del_timer(&phba->sli.mbox_tmo);
				if (pmb->mbox_cmpl) {
					lpfc_sli_pcimem_bcopy(mbox, pmbox,
							MAILBOX_CMD_SIZE);
				}
				if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
					pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;

					lpfc_debugfs_disc_trc(vport,
						LPFC_DISC_TRC_MBOX_VPORT,
						"MBOX dflt rpi: : "
						"status:x%x rpi:x%x",
						(uint32_t)pmbox->mbxStatus,
						pmbox->un.varWords[0], 0);

					if (!pmbox->mbxStatus) {
						mp = (struct lpfc_dmabuf *)
							(pmb->context1);
						ndlp = (struct lpfc_nodelist *)
							pmb->context2;

						/* Reg_LOGIN of dflt RPI was
						 * successful. new lets get
						 * rid of the RPI using the
						 * same mbox buffer.
						 */
						lpfc_unreg_login(phba,
							vport->vpi,
							pmbox->un.varWords[0],
							pmb);
						pmb->mbox_cmpl =
							lpfc_mbx_cmpl_dflt_rpi;
						pmb->context1 = mp;
						pmb->context2 = ndlp;
						pmb->vport = vport;
6913 6914 6915 6916 6917 6918 6919
						rc = lpfc_sli_issue_mbox(phba,
								pmb,
								MBX_NOWAIT);
						if (rc != MBX_BUSY)
							lpfc_printf_log(phba,
							KERN_ERR,
							LOG_MBOX | LOG_SLI,
6920
							"0350 rc should have"
6921
							"been MBX_BUSY");
6922 6923
						if (rc != MBX_NOT_FINISHED)
							goto send_current_mbox;
6924
					}
J
James Smart 已提交
6925
				}
6926 6927 6928
				spin_lock_irqsave(
						&phba->pport->work_port_lock,
						iflag);
6929 6930
				phba->pport->work_port_events &=
					~WORKER_MBOX_TMO;
6931 6932 6933
				spin_unlock_irqrestore(
						&phba->pport->work_port_lock,
						iflag);
6934
				lpfc_mbox_cmpl_put(phba, pmb);
J
James Smart 已提交
6935
			}
6936
		} else
6937
			spin_unlock_irqrestore(&phba->hbalock, iflag);
6938

6939 6940
		if ((work_ha_copy & HA_MBATT) &&
		    (phba->sli.mbox_active == NULL)) {
J
James Smart 已提交
6941
send_current_mbox:
6942
			/* Process next mailbox command if there is one */
6943 6944 6945 6946 6947 6948 6949 6950
			do {
				rc = lpfc_sli_issue_mbox(phba, NULL,
							 MBX_NOWAIT);
			} while (rc == MBX_NOT_FINISHED);
			if (rc != MBX_SUCCESS)
				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
						LOG_SLI, "0349 rc should be "
						"MBX_SUCCESS");
6951 6952
		}

6953
		spin_lock_irqsave(&phba->hbalock, iflag);
已提交
6954
		phba->work_ha |= work_ha_copy;
6955
		spin_unlock_irqrestore(&phba->hbalock, iflag);
6956
		lpfc_worker_wake_up(phba);
已提交
6957
	}
6958
	return IRQ_HANDLED;
已提交
6959

6960
} /* lpfc_sli_sp_intr_handler */
6961 6962

/**
6963
 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
6964 6965 6966 6967
 * @irq: Interrupt number.
 * @dev_id: The device context pointer.
 *
 * This function is directly called from the PCI layer as an interrupt
6968 6969 6970 6971 6972 6973 6974 6975 6976
 * service routine when device with SLI-3 interface spec is enabled with
 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
 * ring event in the HBA. However, when the device is enabled with either
 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
 * device-level interrupt handler. When the PCI slot is in error recovery
 * or the HBA is undergoing initialization, the interrupt handler will not
 * process the interrupt. The SCSI FCP fast-path ring event are handled in
 * the intrrupt context. This function is called without any lock held.
 * It gets the hbalock to access and update SLI data structures.
6977 6978 6979 6980 6981
 *
 * This function returns IRQ_HANDLED when interrupt is handled else it
 * returns IRQ_NONE.
 **/
irqreturn_t
6982
lpfc_sli_fp_intr_handler(int irq, void *dev_id)
6983 6984 6985 6986
{
	struct lpfc_hba  *phba;
	uint32_t ha_copy;
	unsigned long status;
6987
	unsigned long iflag;
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001

	/* Get the driver's phba structure from the dev_id and
	 * assume the HBA is not interrupting.
	 */
	phba = (struct lpfc_hba *) dev_id;

	if (unlikely(!phba))
		return IRQ_NONE;

	/*
	 * Stuff needs to be attented to when this function is invoked as an
	 * individual interrupt handler in MSI-X multi-message interrupt mode
	 */
	if (phba->intr_type == MSIX) {
7002 7003
		/* Check device state for handling interrupt */
		if (lpfc_intr_state_check(phba))
7004 7005 7006 7007
			return IRQ_NONE;
		/* Need to read HA REG for FCP ring and other ring events */
		ha_copy = readl(phba->HAregaddr);
		/* Clear up only attention source related to fast-path */
7008
		spin_lock_irqsave(&phba->hbalock, iflag);
7009 7010 7011 7012 7013
		/*
		 * If there is deferred error attention, do not check for
		 * any interrupt.
		 */
		if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7014
			spin_unlock_irqrestore(&phba->hbalock, iflag);
7015 7016
			return IRQ_NONE;
		}
7017 7018 7019
		writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
			phba->HAregaddr);
		readl(phba->HAregaddr); /* flush */
7020
		spin_unlock_irqrestore(&phba->hbalock, iflag);
7021 7022
	} else
		ha_copy = phba->ha_copy;
已提交
7023 7024

	/*
7025
	 * Process all events on FCP ring. Take the optimized path for FCP IO.
已提交
7026
	 */
7027 7028 7029
	ha_copy &= ~(phba->work_ha_mask);

	status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
已提交
7030
	status >>= (4*LPFC_FCP_RING);
J
James Smart 已提交
7031
	if (status & HA_RXMASK)
已提交
7032 7033 7034
		lpfc_sli_handle_fast_ring_event(phba,
						&phba->sli.ring[LPFC_FCP_RING],
						status);
7035 7036 7037

	if (phba->cfg_multi_ring_support == 2) {
		/*
7038 7039
		 * Process all events on extra ring. Take the optimized path
		 * for extra ring IO.
7040
		 */
7041
		status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
7042
		status >>= (4*LPFC_EXTRA_RING);
J
James Smart 已提交
7043
		if (status & HA_RXMASK) {
7044 7045 7046 7047 7048
			lpfc_sli_handle_fast_ring_event(phba,
					&phba->sli.ring[LPFC_EXTRA_RING],
					status);
		}
	}
已提交
7049
	return IRQ_HANDLED;
7050
}  /* lpfc_sli_fp_intr_handler */
7051 7052

/**
7053
 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
7054 7055 7056
 * @irq: Interrupt number.
 * @dev_id: The device context pointer.
 *
7057 7058 7059 7060 7061 7062 7063 7064
 * This function is the HBA device-level interrupt handler to device with
 * SLI-3 interface spec, called from the PCI layer when either MSI or
 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
 * requires driver attention. This function invokes the slow-path interrupt
 * attention handling function and fast-path interrupt attention handling
 * function in turn to process the relevant HBA attention events. This
 * function is called without any lock held. It gets the hbalock to access
 * and update SLI data structures.
7065 7066 7067 7068 7069
 *
 * This function returns IRQ_HANDLED when interrupt is handled, else it
 * returns IRQ_NONE.
 **/
irqreturn_t
7070
lpfc_sli_intr_handler(int irq, void *dev_id)
7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084
{
	struct lpfc_hba  *phba;
	irqreturn_t sp_irq_rc, fp_irq_rc;
	unsigned long status1, status2;

	/*
	 * Get the driver's phba structure from the dev_id and
	 * assume the HBA is not interrupting.
	 */
	phba = (struct lpfc_hba *) dev_id;

	if (unlikely(!phba))
		return IRQ_NONE;

7085 7086
	/* Check device state for handling interrupt */
	if (lpfc_intr_state_check(phba))
7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102
		return IRQ_NONE;

	spin_lock(&phba->hbalock);
	phba->ha_copy = readl(phba->HAregaddr);
	if (unlikely(!phba->ha_copy)) {
		spin_unlock(&phba->hbalock);
		return IRQ_NONE;
	} else if (phba->ha_copy & HA_ERATT) {
		if (phba->hba_flag & HBA_ERATT_HANDLED)
			/* ERATT polling has handled ERATT */
			phba->ha_copy &= ~HA_ERATT;
		else
			/* Indicate interrupt handler handles ERATT */
			phba->hba_flag |= HBA_ERATT_HANDLED;
	}

7103 7104 7105 7106 7107 7108 7109 7110
	/*
	 * If there is deferred error attention, do not check for any interrupt.
	 */
	if (unlikely(phba->hba_flag & DEFER_ERATT)) {
		spin_unlock_irq(&phba->hbalock);
		return IRQ_NONE;
	}

7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127
	/* Clear attention sources except link and error attentions */
	writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
	readl(phba->HAregaddr); /* flush */
	spin_unlock(&phba->hbalock);

	/*
	 * Invokes slow-path host attention interrupt handling as appropriate.
	 */

	/* status of events with mailbox and link attention */
	status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);

	/* status of events with ELS ring */
	status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
	status2 >>= (4*LPFC_ELS_RING);

	if (status1 || (status2 & HA_RXMASK))
7128
		sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147
	else
		sp_irq_rc = IRQ_NONE;

	/*
	 * Invoke fast-path host attention interrupt handling as appropriate.
	 */

	/* status of events with FCP ring */
	status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
	status1 >>= (4*LPFC_FCP_RING);

	/* status of events with extra ring */
	if (phba->cfg_multi_ring_support == 2) {
		status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
		status2 >>= (4*LPFC_EXTRA_RING);
	} else
		status2 = 0;

	if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
7148
		fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
7149 7150
	else
		fp_irq_rc = IRQ_NONE;
已提交
7151

7152 7153
	/* Return device-level interrupt handling status */
	return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
7154
}  /* lpfc_sli_intr_handler */