lpfc_sli.c 109.0 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-2007 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 33 34 35 36 37 38 39 40

#include "lpfc_hw.h"
#include "lpfc_sli.h"
#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 已提交
41
#include "lpfc_debugfs.h"
已提交
42 43 44 45 46 47

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


/* 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;

69 70 71 72
		/* SLI-2/SLI-3 provide different sized iocbs.  Given a pointer
		 * to the start of the ring, and the slot number of the
		 * desired iocb entry, calc a pointer to that entry.
		 */
73 74 75 76 77 78 79 80 81 82 83 84 85 86
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);
}

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);
}

J
James Smart 已提交
87 88
static struct lpfc_iocbq *
__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
89 90 91 92 93 94 95 96
{
	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;
}

J
James Smart 已提交
97 98 99 100 101 102 103 104 105 106 107 108
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;
}

A
Adrian Bunk 已提交
109
static void
J
James Smart 已提交
110
__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
111
{
J
James Smart 已提交
112
	size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
113 114 115 116 117 118 119 120

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

J
James Smart 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
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);
}

已提交
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
/*
 * Translate the iocb command to an iocb command type used to decide the final
 * disposition of each completed IOCB.
 */
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 已提交
164 165 166 167
	case CMD_FCP_TSEND_CX:
	case CMD_FCP_TRSP_CX:
	case CMD_FCP_TRECEIVE_CX:
	case CMD_FCP_AUTO_TRSP_CX:
已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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 已提交
182 183 184
	case CMD_FCP_TSEND64_CX:
	case CMD_FCP_TRSP64_CX:
	case CMD_FCP_TRECEIVE64_CX:
已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	case CMD_GEN_REQUEST64_CR:
	case CMD_GEN_REQUEST64_CX:
	case CMD_XMIT_ELS_RSP64_CX:
		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:
202
	case CMD_ASYNC_STATUS:
203 204 205
	case CMD_IOCB_RCV_SEQ64_CX:
	case CMD_IOCB_RCV_ELS64_CX:
	case CMD_IOCB_RCV_CONT64_CX:
已提交
206 207 208 209 210 211 212 213 214 215 216
		type = LPFC_UNSOL_IOCB;
		break;
	default:
		type = LPFC_UNKNOWN_IOCB;
		break;
	}

	return type;
}

static int
217
lpfc_sli_ring_map(struct lpfc_hba *phba)
已提交
218 219
{
	struct lpfc_sli *psli = &phba->sli;
220 221 222
	LPFC_MBOXQ_t *pmb;
	MAILBOX_t *pmbox;
	int i, rc, ret = 0;
已提交
223

224 225 226 227 228
	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;
已提交
229 230 231 232
	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) {
233
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
234
					"0446 Adapter failed to init (%d), "
已提交
235 236
					"mbxCmd x%x CFG_RING, mbxStatus x%x, "
					"ring %d\n",
237 238
					rc, pmbox->mbxCommand,
					pmbox->mbxStatus, i);
J
James Smart 已提交
239
			phba->link_state = LPFC_HBA_ERROR;
240 241
			ret = -ENXIO;
			break;
已提交
242 243
		}
	}
244 245
	mempool_free(pmb, phba->mbox_mem_pool);
	return ret;
已提交
246 247 248
}

static int
J
James Smart 已提交
249 250
lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			struct lpfc_iocbq *piocb)
已提交
251 252 253
{
	list_add_tail(&piocb->list, &pring->txcmplq);
	pring->txcmplq_cnt++;
254 255 256 257 258 259 260 261 262 263
	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));
	}

已提交
264

J
James Smart 已提交
265
	return 0;
已提交
266 267 268
}

static struct lpfc_iocbq *
J
James Smart 已提交
269
lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
270 271 272
{
	struct lpfc_iocbq *cmd_iocb;

J
James Smart 已提交
273 274
	list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
	if (cmd_iocb != NULL)
已提交
275
		pring->txq_cnt--;
J
James Smart 已提交
276
	return cmd_iocb;
已提交
277 278 279 280 281
}

static IOCB_t *
lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
282 283 284
	struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
		&phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
		&phba->slim2p->mbx.us.s2.port[pring->ringno];
已提交
285 286 287 288 289 290 291 292 293 294 295 296
	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,
297
					"0315 Ring %d issue: portCmdGet %d "
已提交
298
					"is bigger then cmd ring %d\n",
299
					pring->ringno,
已提交
300 301
					pring->local_getidx, max_cmd_idx);

J
James Smart 已提交
302
			phba->link_state = LPFC_HBA_ERROR;
已提交
303 304 305 306 307 308
			/*
			 * All error attention handlers are posted to
			 * worker thread
			 */
			phba->work_ha |= HA_ERATT;
			phba->work_hs = HS_FFER3;
309 310

			/* hbalock should already be held */
已提交
311
			if (phba->work_wait)
312
				lpfc_worker_wake_up(phba);
已提交
313 314 315 316 317 318 319 320

			return NULL;
		}

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

321
	return lpfc_cmd_iocb(phba, pring);
已提交
322 323
}

324
uint16_t
J
James Smart 已提交
325
lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
已提交
326
{
J
James Smart 已提交
327 328
	struct lpfc_iocbq **new_arr;
	struct lpfc_iocbq **old_arr;
329 330 331
	size_t new_len;
	struct lpfc_sli *psli = &phba->sli;
	uint16_t iotag;
已提交
332

J
James Smart 已提交
333
	spin_lock_irq(&phba->hbalock);
334 335 336 337
	iotag = psli->last_iotag;
	if(++iotag < psli->iocbq_lookup_len) {
		psli->last_iotag = iotag;
		psli->iocbq_lookup[iotag] = iocbq;
J
James Smart 已提交
338
		spin_unlock_irq(&phba->hbalock);
339 340
		iocbq->iotag = iotag;
		return iotag;
J
James Smart 已提交
341
	} else if (psli->iocbq_lookup_len < (0xffff
342 343
					   - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
		new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
J
James Smart 已提交
344 345
		spin_unlock_irq(&phba->hbalock);
		new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
346 347
				  GFP_KERNEL);
		if (new_arr) {
J
James Smart 已提交
348
			spin_lock_irq(&phba->hbalock);
349 350 351 352 353 354 355 356
			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 已提交
357
					spin_unlock_irq(&phba->hbalock);
358 359 360
					iocbq->iotag = iotag;
					return iotag;
				}
J
James Smart 已提交
361
				spin_unlock_irq(&phba->hbalock);
362 363 364 365 366
				return 0;
			}
			if (psli->iocbq_lookup)
				memcpy(new_arr, old_arr,
				       ((psli->last_iotag  + 1) *
J
James Smart 已提交
367
					sizeof (struct lpfc_iocbq *)));
368 369 370 371
			psli->iocbq_lookup = new_arr;
			psli->iocbq_lookup_len = new_len;
			psli->last_iotag = iotag;
			psli->iocbq_lookup[iotag] = iocbq;
J
James Smart 已提交
372
			spin_unlock_irq(&phba->hbalock);
373 374 375 376
			iocbq->iotag = iotag;
			kfree(old_arr);
			return iotag;
		}
377
	} else
J
James Smart 已提交
378
		spin_unlock_irq(&phba->hbalock);
已提交
379

380
	lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
381 382
			"0318 Failed to allocate IOTAG.last IOTAG is %d\n",
			psli->last_iotag);
已提交
383

384
	return 0;
已提交
385 386 387 388 389 390 391
}

static void
lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
		IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
{
	/*
392
	 * Set up an iotag
已提交
393
	 */
394
	nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
已提交
395

396 397 398 399 400 401 402 403
	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));
	}

已提交
404 405 406
	/*
	 * Issue iocb command to adapter
	 */
407
	lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
已提交
408 409 410 411 412 413 414 415 416 417
	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);
418
	else
J
James Smart 已提交
419
		__lpfc_sli_release_iocbq(phba, nextiocb);
已提交
420 421 422 423 424 425

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

static void
J
James Smart 已提交
430
lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
{
	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++;
}

static void
J
James Smart 已提交
449
lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
450 451 452 453 454 455 456 457 458 459 460 461
{
	int ringno = pring->ringno;

	/*
	 * Tell the HBA that there is work to do in this ring.
	 */
	wmb();
	writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
	readl(phba->CAregaddr); /* flush */
}

static void
J
James Smart 已提交
462
lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
已提交
463 464 465 466 467 468 469 470 471 472 473 474
{
	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 已提交
475
	    lpfc_is_link_up(phba) &&
已提交
476
	    (pring->ringno != phba->sli.fcp_ring ||
477
	     phba->sli.sli_flag & LPFC_PROCESS_LA)) {
已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491

		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;
}

A
Adrian Bunk 已提交
492
static struct lpfc_hbq_entry *
493 494 495 496 497 498 499 500 501
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)) {
502
		uint32_t raw_index = phba->hbq_get[hbqno];
503 504 505 506 507 508
		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,
509
					LOG_SLI | LOG_VPORT,
510
					"1802 HBQ %d: local_hbqGetIdx "
511
					"%u is > than hbqp->entry_count %u\n",
512
					hbqno, hbqp->local_hbqGetIdx,
513 514 515 516 517 518 519 520 521 522
					hbqp->entry_count);

			phba->link_state = LPFC_HBA_ERROR;
			return NULL;
		}

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

523 524
	return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
			hbqp->hbqPutIdx;
525 526 527 528 529
}

void
lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
{
530 531
	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
	struct hbq_dmabuf *hbq_buf;
532
	int i, hbq_count;
533

534
	hbq_count = lpfc_sli_hbq_count();
535
	/* Return all memory used by all HBQs */
536 537 538 539 540 541 542
	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);
		}
543
		phba->hbqs[i].buffer_count = 0;
544 545 546
	}
}

547
static struct lpfc_hbq_entry *
548
lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
549
			 struct hbq_dmabuf *hbq_buf)
550 551
{
	struct lpfc_hbq_entry *hbqe;
552
	dma_addr_t physaddr = hbq_buf->dbuf.phys;
553 554 555 556 557 558

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

559 560
		hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
		hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
561
		hbqe->bde.tus.f.bdeSize = hbq_buf->size;
562
		hbqe->bde.tus.f.bdeFlags = 0;
563 564 565
		hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
		hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
				/* Sync SLIM */
566 567
		hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
		writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
568
				/* flush */
569
		readl(phba->hbq_put + hbqno);
570
		list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
571
	}
572
	return hbqe;
573 574
}

575 576 577 578 579
static struct lpfc_hbq_init lpfc_els_hbq = {
	.rn = 1,
	.entry_count = 200,
	.mask_count = 0,
	.profile = 0,
580
	.ring_mask = (1 << LPFC_ELS_RING),
581 582 583 584
	.buffer_count = 0,
	.init_count = 20,
	.add_count = 5,
};
585

586 587 588 589 590 591 592 593 594 595 596
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,
};

597
struct lpfc_hbq_init *lpfc_hbq_defs[] = {
598
	&lpfc_els_hbq,
599
	&lpfc_extra_hbq,
600
};
601

J
James Smart 已提交
602
static int
603
lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
604
{
605 606
	uint32_t i, start, end;
	struct hbq_dmabuf *hbq_buffer;
607

608 609 610 611
	if (!phba->hbqs[hbqno].hbq_alloc_buffer) {
		return 0;
	}

612 613
	start = phba->hbqs[hbqno].buffer_count;
	end = count + start;
614 615 616
	if (end > lpfc_hbq_defs[hbqno]->entry_count) {
		end = lpfc_hbq_defs[hbqno]->entry_count;
	}
617 618

	/* Populate HBQ entries */
619
	for (i = start; i < end; i++) {
620
		hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
621 622 623
		if (!hbq_buffer)
			return 1;
		hbq_buffer->tag = (i | (hbqno << 16));
624
		if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
625
			phba->hbqs[hbqno].buffer_count++;
626 627
		else
			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
628 629 630 631
	}
	return 0;
}

632 633
int
lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
634
{
635 636 637
	return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
					 lpfc_hbq_defs[qno]->add_count));
}
638

A
Adrian Bunk 已提交
639
static int
640 641 642 643
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));
644 645
}

A
Adrian Bunk 已提交
646
static struct hbq_dmabuf *
647
lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
648
{
649 650
	struct lpfc_dmabuf *d_buf;
	struct hbq_dmabuf *hbq_buf;
651 652 653
	uint32_t hbqno;

	hbqno = tag >> 16;
654
	if (hbqno >= LPFC_MAX_HBQS)
655
		return NULL;
656

657
	list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
658
		hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
659
		if (hbq_buf->tag == tag) {
660
			return hbq_buf;
661 662
		}
	}
663
	lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
664
			"1803 Bad hbq tag. Data: x%x x%x\n",
665
			tag, phba->hbqs[tag >> 16].buffer_count);
666
	return NULL;
667 668 669
}

void
670
lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
671 672 673
{
	uint32_t hbqno;

674 675 676 677 678
	if (hbq_buffer) {
		hbqno = hbq_buffer->tag >> 16;
		if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
			(phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
		}
679 680 681
	}
}

已提交
682 683 684 685 686 687 688 689 690
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:
691
	case MBX_WRITE_VPARMS:
已提交
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
	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:
719
	case MBX_SET_VARIABLE:
已提交
720
	case MBX_UNREG_D_ID:
721
	case MBX_KILL_BOARD:
已提交
722
	case MBX_CONFIG_FARP:
723
	case MBX_BEACON:
已提交
724 725 726 727 728 729 730
	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:
731
	case MBX_WRITE_WWN:
已提交
732 733
	case MBX_SET_DEBUG:
	case MBX_LOAD_EXP_ROM:
734
	case MBX_ASYNCEVT_ENABLE:
735 736
	case MBX_REG_VPI:
	case MBX_UNREG_VPI:
J
James Smart 已提交
737
	case MBX_HEARTBEAT:
已提交
738 739 740 741 742 743
		ret = mbxCommand;
		break;
	default:
		ret = MBX_SHUTDOWN;
		break;
	}
J
James Smart 已提交
744
	return ret;
已提交
745 746
}
static void
J
James Smart 已提交
747
lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
已提交
748 749
{
	wait_queue_head_t *pdone_q;
J
James Smart 已提交
750
	unsigned long drvr_flag;
已提交
751 752 753 754 755

	/*
	 * If pdone_q is empty, the driver thread gave up waiting and
	 * continued running.
	 */
756
	pmboxq->mbox_flag |= LPFC_MBX_WAKE;
J
James Smart 已提交
757
	spin_lock_irqsave(&phba->hbalock, drvr_flag);
已提交
758 759 760
	pdone_q = (wait_queue_head_t *) pmboxq->context1;
	if (pdone_q)
		wake_up_interruptible(pdone_q);
J
James Smart 已提交
761
	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
762 763 764 765
	return;
}

void
J
James Smart 已提交
766
lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
已提交
767 768
{
	struct lpfc_dmabuf *mp;
769 770 771
	uint16_t rpi;
	int rc;

已提交
772
	mp = (struct lpfc_dmabuf *) (pmb->context1);
773

已提交
774 775 776 777
	if (mp) {
		lpfc_mbuf_free(phba, mp->virt, mp->phys);
		kfree(mp);
	}
778 779 780 781 782

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

		rpi = pmb->mb.un.varWords[0];
788 789
		lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
		pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
790 791 792 793 794
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
		if (rc != MBX_NOT_FINISHED)
			return;
	}

J
James Smart 已提交
795
	mempool_free(pmb, phba->mbox_mem_pool);
已提交
796 797 798 799
	return;
}

int
J
James Smart 已提交
800
lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
已提交
801
{
802
	MAILBOX_t *pmbox;
已提交
803
	LPFC_MBOXQ_t *pmb;
804 805
	int rc;
	LIST_HEAD(cmplq);
已提交
806 807 808

	phba->sli.slistat.mbox_event++;

809 810 811 812
	/* 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);
已提交
813

814 815 816 817 818
	/* 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 已提交
819

820
		pmbox = &pmb->mb;
已提交
821

J
James Smart 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
		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]);
			}
		}

已提交
841 842 843 844 845 846
		/*
		 * It is a fatal error if unknown mbox command completion.
		 */
		if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
		    MBX_SHUTDOWN) {
			/* Unknow mailbox command compl */
847
			lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
848
					"(%d):0323 Unknown Mailbox command "
849 850 851
					"%x Cmpl\n",
					pmb->vport ? pmb->vport->vpi : 0,
					pmbox->mbxCommand);
J
James Smart 已提交
852
			phba->link_state = LPFC_HBA_ERROR;
已提交
853 854
			phba->work_hs = HS_FFER3;
			lpfc_handle_eratt(phba);
855
			continue;
已提交
856 857 858 859 860 861
		}

		if (pmbox->mbxStatus) {
			phba->sli.slistat.mbox_stat_err++;
			if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
				/* Mbox cmd cmpl error - RETRYing */
862 863
				lpfc_printf_log(phba, KERN_INFO,
						LOG_MBOX | LOG_SLI,
864
						"(%d):0305 Mbox cmd cmpl "
865 866 867 868 869 870 871
						"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);
已提交
872 873
				pmbox->mbxStatus = 0;
				pmbox->mbxOwner = OWN_HOST;
J
James Smart 已提交
874
				spin_lock_irq(&phba->hbalock);
已提交
875
				phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
876
				spin_unlock_irq(&phba->hbalock);
已提交
877 878
				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
				if (rc == MBX_SUCCESS)
879
					continue;
已提交
880 881 882 883
			}
		}

		/* Mailbox cmd <cmd> Cmpl <cmpl> */
884
		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
885
				"(%d):0307 Mailbox cmd x%x Cmpl x%p "
已提交
886
				"Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
887
				pmb->vport ? pmb->vport->vpi : 0,
已提交
888 889 890 891 892 893 894 895 896 897 898 899
				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]);

900
		if (pmb->mbox_cmpl)
已提交
901
			pmb->mbox_cmpl(phba,pmb);
902 903 904
	} while (1);
	return 0;
}
已提交
905

906 907 908 909
static struct lpfc_dmabuf *
lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
{
	struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
910 911 912
	uint32_t hbqno;
	void *virt;		/* virtual address ptr */
	dma_addr_t phys;	/* mapped address */
已提交
913

914 915 916 917
	hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
	if (hbq_entry == NULL)
		return NULL;
	list_del(&hbq_entry->dbuf.list);
918 919 920

	hbqno = tag >> 16;
	new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
921 922 923
	if (new_hbq_entry == NULL)
		return &hbq_entry->dbuf;
	new_hbq_entry->tag = -1;
924 925 926 927 928 929
	phys = new_hbq_entry->dbuf.phys;
	virt = new_hbq_entry->dbuf.virt;
	new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
	new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
	hbq_entry->dbuf.phys = phys;
	hbq_entry->dbuf.virt = virt;
930 931
	lpfc_sli_free_hbq(phba, hbq_entry);
	return &new_hbq_entry->dbuf;
已提交
932
}
933

934 935 936 937 938 939 940 941 942 943
static struct lpfc_dmabuf *
lpfc_sli_get_buff(struct lpfc_hba *phba,
			struct lpfc_sli_ring *pring,
			uint32_t tag)
{
	if (tag & QUE_BUFTAG_BIT)
		return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
	else
		return lpfc_sli_replace_hbqbuff(phba, tag);
}
944

已提交
945 946 947 948 949 950 951 952
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;
	uint32_t           match, i;
953
	struct lpfc_iocbq *iocbq;
已提交
954 955 956

	match = 0;
	irsp = &(saveq->iocb);
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972

	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;
	}

已提交
973
	if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
974 975 976
	    || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)
	    || (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)
	    || (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX)) {
已提交
977 978 979 980 981 982 983 984 985 986 987
		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) &&
988 989
			(irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
			 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
已提交
990 991 992 993 994 995
			Rctl = FC_ELS_REQ;
			Type = FC_ELS_DATA;
			w5p->hcsw.Rctl = Rctl;
			w5p->hcsw.Type = Type;
		}
	}
996 997

	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		struct lpfc_hbq_entry *hbqe_1, *hbqe_2;
		hbqe_1 = (struct lpfc_hbq_entry *) &saveq->iocb.un.ulpWord[0];
		hbqe_2 = (struct lpfc_hbq_entry *) &saveq->iocb.
				unsli3.sli3Words[4];

		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) {
			hbqe_1 = (struct lpfc_hbq_entry *) &iocbq->iocb.
				un.ulpWord[0];
			hbqe_2 = (struct lpfc_hbq_entry *) &iocbq->iocb.
				unsli3.sli3Words[4];
			irsp = &(iocbq->iocb);

			if (irsp->ulpBdeCount != 0) {
				iocbq->context2 = lpfc_sli_get_buff(phba, pring,
							irsp->un.ulpWord[3]);
				if (!saveq->context2)
					lpfc_printf_log(phba,
						KERN_ERR,
						LOG_SLI,
						"0343 Ring %d Cannot find "
						"buffer for an unsolicited iocb"
						". tag 0x%x\n", pring->ringno,
1045
						irsp->un.ulpWord[3]);
1046 1047 1048
			}
			if (irsp->ulpBdeCount == 2) {
				iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1049
						irsp->unsli3.sli3Words[7]);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
				if (!saveq->context3)
					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]);
			}
		}
1061 1062
	}

已提交
1063 1064
	/* unSolicited Responses */
	if (pring->prt[0].profile) {
1065 1066 1067
		if (pring->prt[0].lpfc_sli_rcv_unsol_event)
			(pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
									saveq);
已提交
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		match = 1;
	} else {
		/* We must search, based on rctl / type
		   for the right routine */
		for (i = 0; i < pring->num_mask;
		     i++) {
			if ((pring->prt[i].rctl ==
			     Rctl)
			    && (pring->prt[i].
				type == Type)) {
1078 1079 1080
				if (pring->prt[i].lpfc_sli_rcv_unsol_event)
					(pring->prt[i].lpfc_sli_rcv_unsol_event)
							(phba, pring, saveq);
已提交
1081 1082 1083 1084 1085 1086 1087 1088 1089
				match = 1;
				break;
			}
		}
	}
	if (match == 0) {
		/* Unexpected Rctl / Type received */
		/* Ring <ringno> handler: unexpected
		   Rctl <Rctl> Type <Type> received */
1090
		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1091
				"0313 Ring %d handler: unexpected Rctl x%x "
1092
				"Type x%x received\n",
1093
				pring->ringno, Rctl, Type);
已提交
1094
	}
1095
	return 1;
已提交
1096 1097 1098
}

static struct lpfc_iocbq *
J
James Smart 已提交
1099 1100 1101
lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
		      struct lpfc_sli_ring *pring,
		      struct lpfc_iocbq *prspiocb)
已提交
1102 1103 1104 1105
{
	struct lpfc_iocbq *cmd_iocb = NULL;
	uint16_t iotag;

1106 1107 1108 1109
	iotag = prspiocb->iocb.ulpIoTag;

	if (iotag != 0 && iotag <= phba->sli.last_iotag) {
		cmd_iocb = phba->sli.iocbq_lookup[iotag];
1110
		list_del_init(&cmd_iocb->list);
1111 1112
		pring->txcmplq_cnt--;
		return cmd_iocb;
已提交
1113 1114 1115
	}

	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1116
			"0317 iotag x%x is out off "
1117
			"range: max iotag x%x wd0 x%x\n",
1118
			iotag, phba->sli.last_iotag,
1119
			*(((uint32_t *) &prspiocb->iocb) + 7));
已提交
1120 1121 1122 1123
	return NULL;
}

static int
J
James Smart 已提交
1124
lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
已提交
1125 1126
			  struct lpfc_iocbq *saveq)
{
J
James Smart 已提交
1127
	struct lpfc_iocbq *cmdiocbp;
已提交
1128 1129 1130 1131
	int rc = 1;
	unsigned long iflag;

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

已提交
1136 1137 1138 1139 1140 1141 1142
	if (cmdiocbp) {
		if (cmdiocbp->iocb_cmpl) {
			/*
			 * Post all ELS completions to the worker thread.
			 * All other are passed to the completion callback.
			 */
			if (pring->ringno == LPFC_ELS_RING) {
1143 1144 1145 1146 1147 1148 1149
				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;
1150 1151 1152 1153 1154 1155

					/* 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;
1156
				}
已提交
1157
			}
J
James Smart 已提交
1158
			(cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1159 1160
		} else
			lpfc_sli_release_iocbq(phba, cmdiocbp);
已提交
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
	} 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>
			 */
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
			lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
					 "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);
已提交
1182 1183
		}
	}
1184

已提交
1185 1186 1187
	return rc;
}

J
James Smart 已提交
1188 1189
static void
lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1190
{
1191 1192 1193
	struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
		&phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
		&phba->slim2p->mbx.us.s2.port[pring->ringno];
1194 1195 1196 1197 1198
	/*
	 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
	 * rsp ring <portRspMax>
	 */
	lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1199
			"0312 Ring %d handler: portRspPut %d "
1200
			"is bigger then rsp ring %d\n",
1201
			pring->ringno, le32_to_cpu(pgp->rspPutInx),
1202 1203
			pring->numRiocb);

J
James Smart 已提交
1204
	phba->link_state = LPFC_HBA_ERROR;
1205 1206 1207 1208 1209 1210 1211

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

	/* hbalock should already be held */
1214
	if (phba->work_wait)
1215
		lpfc_worker_wake_up(phba);
1216 1217 1218 1219

	return;
}

J
James Smart 已提交
1220
void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1221
{
J
James Smart 已提交
1222 1223
	struct lpfc_sli      *psli  = &phba->sli;
	struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
	IOCB_t *irsp = NULL;
	IOCB_t *entry = NULL;
	struct lpfc_iocbq *cmdiocbq = NULL;
	struct lpfc_iocbq rspiocbq;
	struct lpfc_pgp *pgp;
	uint32_t status;
	uint32_t portRspPut, portRspMax;
	int type;
	uint32_t rsp_cmpl = 0;
	uint32_t ha_copy;
J
James Smart 已提交
1234
	unsigned long iflags;
1235 1236 1237

	pring->stats.iocb_event++;

1238 1239 1240 1241
	pgp = (phba->sli_rev == 3) ?
		&phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
		&phba->slim2p->mbx.us.s2.port[pring->ringno];

1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

	/*
	 * 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) {
1256
		entry = lpfc_resp_iocb(phba, pring);
1257 1258 1259 1260 1261
		if (++pring->rspidx >= portRspMax)
			pring->rspidx = 0;

		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
				      (uint32_t *) &rspiocbq.iocb,
1262
				      phba->iocb_rsp_size);
1263 1264 1265 1266 1267 1268 1269 1270
		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,
1271
					"0326 Rsp Ring %d error: IOCB Data: "
1272
					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
1273
					pring->ringno,
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
					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));
		}

		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)) {
1292
				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1293 1294 1295
						"0314 IOCB cmd 0x%x "
						"processed. Skipping "
						"completion",
1296
						irsp->ulpCommand);
1297 1298 1299
				break;
			}

J
James Smart 已提交
1300
			spin_lock_irqsave(&phba->hbalock, iflags);
1301 1302
			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
							 &rspiocbq);
J
James Smart 已提交
1303
			spin_unlock_irqrestore(&phba->hbalock, iflags);
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
			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);
1315 1316
				dev_warn(&((phba->pcidev)->dev),
					 "lpfc%d: %s\n",
1317 1318 1319 1320
					 phba->brd_no, adaptermsg);
			} else {
				/* Unknown IOCB command */
				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1321
						"0321 Unknown IOCB command "
1322
						"Data: x%x, x%x x%x x%x x%x\n",
1323
						type, irsp->ulpCommand,
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
						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.
		 */
1337
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1338 1339 1340 1341 1342 1343 1344 1345 1346

		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 已提交
1347
		spin_lock_irqsave(&phba->hbalock, iflags);
1348 1349 1350 1351
		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 已提交
1352
		spin_unlock_irqrestore(&phba->hbalock, iflags);
1353 1354 1355
	}
	if ((ha_copy & HA_R0CE_RSP) &&
	    (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
J
James Smart 已提交
1356
		spin_lock_irqsave(&phba->hbalock, iflags);
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
		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 已提交
1367
		spin_unlock_irqrestore(&phba->hbalock, iflags);
1368 1369 1370 1371 1372
	}

	return;
}

已提交
1373 1374 1375 1376 1377
/*
 * This routine presumes LPFC_FCP_RING handling and doesn't bother
 * to check it explicitly.
 */
static int
J
James Smart 已提交
1378 1379
lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
				struct lpfc_sli_ring *pring, uint32_t mask)
已提交
1380
{
1381 1382 1383
	struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
		&phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
		&phba->slim2p->mbx.us.s2.port[pring->ringno];
已提交
1384
	IOCB_t *irsp = NULL;
1385
	IOCB_t *entry = NULL;
已提交
1386 1387 1388 1389 1390 1391 1392 1393 1394
	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 已提交
1395
	spin_lock_irqsave(&phba->hbalock, iflag);
已提交
1396 1397 1398 1399 1400 1401 1402 1403 1404
	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)) {
1405
		lpfc_sli_rsp_pointers_error(phba, pring);
J
James Smart 已提交
1406
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
1407 1408 1409 1410 1411
		return 1;
	}

	rmb();
	while (pring->rspidx != portRspPut) {
1412 1413 1414 1415 1416
		/*
		 * 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.
		 */
1417
		entry = lpfc_resp_iocb(phba, pring);
J
James Smart 已提交
1418
		phba->last_completion_time = jiffies;
1419 1420 1421 1422

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

1423 1424
		lpfc_sli_pcimem_bcopy((uint32_t *) entry,
				      (uint32_t *) &rspiocbq.iocb,
1425
				      phba->iocb_rsp_size);
1426
		INIT_LIST_HEAD(&(rspiocbq.list));
1427 1428
		irsp = &rspiocbq.iocb;

已提交
1429 1430 1431 1432 1433
		type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
		pring->stats.iocb_rsp++;
		rsp_cmpl++;

		if (unlikely(irsp->ulpStatus)) {
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
			/*
			 * 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);
				lpfc_adjust_queue_depth(phba);
				spin_lock_irqsave(&phba->hbalock, iflag);
			}

已提交
1445 1446
			/* Rsp ring <ringno> error: IOCB */
			lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1447
					"0336 Rsp Ring %d error: IOCB Data: "
1448
					"x%x x%x x%x x%x x%x x%x x%x x%x\n",
1449
					pring->ringno,
1450 1451 1452 1453 1454 1455 1456 1457
					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));
已提交
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
		}

		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)) {
1468
				lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1469
						"0333 IOCB cmd 0x%x"
1470
						" processed. Skipping"
1471
						" completion\n",
1472
						irsp->ulpCommand);
已提交
1473 1474 1475
				break;
			}

1476 1477
			cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
							 &rspiocbq);
已提交
1478
			if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1479 1480 1481 1482
				if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
					(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
							      &rspiocbq);
				} else {
J
James Smart 已提交
1483 1484
					spin_unlock_irqrestore(&phba->hbalock,
							       iflag);
1485 1486
					(cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
							      &rspiocbq);
J
James Smart 已提交
1487
					spin_lock_irqsave(&phba->hbalock,
1488 1489
							  iflag);
				}
已提交
1490 1491
			}
			break;
1492
		case LPFC_UNSOL_IOCB:
J
James Smart 已提交
1493
			spin_unlock_irqrestore(&phba->hbalock, iflag);
1494
			lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
J
James Smart 已提交
1495
			spin_lock_irqsave(&phba->hbalock, iflag);
1496
			break;
已提交
1497 1498 1499 1500 1501 1502
		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);
1503 1504
				dev_warn(&((phba->pcidev)->dev),
					 "lpfc%d: %s\n",
已提交
1505 1506 1507 1508
					 phba->brd_no, adaptermsg);
			} else {
				/* Unknown IOCB command */
				lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1509
						"0334 Unknown IOCB command "
1510
						"Data: x%x, x%x x%x x%x x%x\n",
1511
						type, irsp->ulpCommand,
1512 1513 1514
						irsp->ulpStatus,
						irsp->ulpIoTag,
						irsp->ulpContext);
已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
			}
			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.
		 */
1525
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
已提交
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549

		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 已提交
1550
	spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
1551 1552 1553 1554
	return rc;
}

int
J
James Smart 已提交
1555 1556
lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
				struct lpfc_sli_ring *pring, uint32_t mask)
已提交
1557
{
1558 1559 1560
	struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
		&phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
		&phba->slim2p->mbx.us.s2.port[pring->ringno];
已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
	IOCB_t *entry;
	IOCB_t *irsp = NULL;
	struct lpfc_iocbq *rspiocbp = NULL;
	struct lpfc_iocbq *next_iocb;
	struct lpfc_iocbq *cmdiocbp;
	struct lpfc_iocbq *saveq;
	uint8_t iocb_cmd_type;
	lpfc_iocb_type type;
	uint32_t status, free_saveq;
	uint32_t portRspPut, portRspMax;
	int rc = 1;
	unsigned long iflag;

J
James Smart 已提交
1574
	spin_lock_irqsave(&phba->hbalock, iflag);
已提交
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	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) {
		/*
		 * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
		 * rsp ring <portRspMax>
		 */
1588
		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1589
				"0303 Ring %d handler: portRspPut %d "
已提交
1590
				"is bigger then rsp ring %d\n",
1591
				pring->ringno, portRspPut, portRspMax);
已提交
1592

J
James Smart 已提交
1593 1594
		phba->link_state = LPFC_HBA_ERROR;
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616

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

		return 1;
	}

	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.
		 */
1617 1618
		entry = lpfc_resp_iocb(phba, pring);

J
James Smart 已提交
1619
		phba->last_completion_time = jiffies;
J
James Smart 已提交
1620
		rspiocbp = __lpfc_sli_get_iocbq(phba);
已提交
1621 1622 1623 1624 1625 1626
		if (rspiocbp == NULL) {
			printk(KERN_ERR "%s: out of buffers! Failing "
			       "completion.\n", __FUNCTION__);
			break;
		}

1627 1628
		lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
				      phba->iocb_rsp_size);
已提交
1629 1630 1631 1632 1633
		irsp = &rspiocbp->iocb;

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

1634 1635 1636 1637 1638 1639 1640 1641
		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));
		}

1642
		writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665

		if (list_empty(&(pring->iocb_continueq))) {
			list_add(&rspiocbp->list, &(pring->iocb_continueq));
		} else {
			list_add_tail(&rspiocbp->list,
				      &(pring->iocb_continueq));
		}

		pring->iocb_continueq_cnt++;
		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++;

1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676
			/*
			 * 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);
				lpfc_adjust_queue_depth(phba);
				spin_lock_irqsave(&phba->hbalock, iflag);
			}

已提交
1677 1678
			if (irsp->ulpStatus) {
				/* Rsp ring <ringno> error: IOCB */
1679
				lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1680
						"0328 Rsp Ring %d error: "
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702
						"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));
已提交
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713
			}

			/*
			 * 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);
			if (type == LPFC_SOL_IOCB) {
J
James Smart 已提交
1714
				spin_unlock_irqrestore(&phba->hbalock,
已提交
1715 1716
						       iflag);
				rc = lpfc_sli_process_sol_iocb(phba, pring,
J
James Smart 已提交
1717 1718
							       saveq);
				spin_lock_irqsave(&phba->hbalock, iflag);
已提交
1719
			} else if (type == LPFC_UNSOL_IOCB) {
J
James Smart 已提交
1720
				spin_unlock_irqrestore(&phba->hbalock,
已提交
1721 1722
						       iflag);
				rc = lpfc_sli_process_unsol_iocb(phba, pring,
J
James Smart 已提交
1723 1724
								 saveq);
				spin_lock_irqsave(&phba->hbalock, iflag);
已提交
1725 1726 1727
			} else if (type == LPFC_ABORT_IOCB) {
				if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
				    ((cmdiocbp =
1728 1729
				      lpfc_sli_iocbq_lookup(phba, pring,
							    saveq)))) {
已提交
1730 1731 1732 1733
					/* Call the specified completion
					   routine */
					if (cmdiocbp->iocb_cmpl) {
						spin_unlock_irqrestore(
J
James Smart 已提交
1734
						       &phba->hbalock,
已提交
1735 1736 1737 1738
						       iflag);
						(cmdiocbp->iocb_cmpl) (phba,
							     cmdiocbp, saveq);
						spin_lock_irqsave(
J
James Smart 已提交
1739
							  &phba->hbalock,
已提交
1740
							  iflag);
1741
					} else
J
James Smart 已提交
1742
						__lpfc_sli_release_iocbq(phba,
1743
								      cmdiocbp);
已提交
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754
				}
			} else if (type == 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),
1755
						 "lpfc%d: %s\n",
已提交
1756 1757 1758
						 phba->brd_no, adaptermsg);
				} else {
					/* Unknown IOCB command */
1759
					lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1760
							"0335 Unknown IOCB "
1761 1762 1763 1764 1765 1766
							"command Data: x%x "
							"x%x x%x x%x\n",
							irsp->ulpCommand,
							irsp->ulpStatus,
							irsp->ulpIoTag,
							irsp->ulpContext);
已提交
1767 1768 1769 1770
				}
			}

			if (free_saveq) {
J
James Smart 已提交
1771 1772 1773 1774 1775
				list_for_each_entry_safe(rspiocbp, next_iocb,
							 &saveq->list, list) {
					list_del(&rspiocbp->list);
					__lpfc_sli_release_iocbq(phba,
								 rspiocbp);
已提交
1776
				}
J
James Smart 已提交
1777
				__lpfc_sli_release_iocbq(phba, saveq);
已提交
1778
			}
1779
			rspiocbp = NULL;
已提交
1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791
		}

		/*
		 * 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) */

1792
	if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
已提交
1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
		/* 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 已提交
1813
	spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
1814 1815 1816
	return rc;
}

J
James Smart 已提交
1817
void
已提交
1818 1819
lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
{
1820
	LIST_HEAD(completions);
已提交
1821
	struct lpfc_iocbq *iocb, *next_iocb;
1822
	IOCB_t *cmd = NULL;
已提交
1823

1824 1825 1826 1827
	if (pring->ringno == LPFC_ELS_RING) {
		lpfc_fabric_abort_hba(phba);
	}

已提交
1828 1829 1830
	/* Error everything on txq and txcmplq
	 * First do the txq.
	 */
J
James Smart 已提交
1831
	spin_lock_irq(&phba->hbalock);
1832
	list_splice_init(&pring->txq, &completions);
已提交
1833 1834 1835
	pring->txq_cnt = 0;

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

J
James Smart 已提交
1839
	spin_unlock_irq(&phba->hbalock);
已提交
1840

1841 1842 1843
	while (!list_empty(&completions)) {
		iocb = list_get_first(&completions, struct lpfc_iocbq, list);
		cmd = &iocb->iocb;
1844
		list_del_init(&iocb->list);
已提交
1845

J
James Smart 已提交
1846 1847 1848
		if (!iocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, iocb);
		else {
已提交
1849 1850 1851
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
			(iocb->iocb_cmpl) (phba, iocb, iocb);
J
James Smart 已提交
1852
		}
已提交
1853 1854 1855
	}
}

1856
int
J
James Smart 已提交
1857
lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
已提交
1858
{
1859 1860 1861
	uint32_t status;
	int i = 0;
	int retval = 0;
已提交
1862

1863 1864
	/* Read the HBA Host Status Register */
	status = readl(phba->HSregaddr);
已提交
1865

1866 1867 1868 1869 1870 1871 1872 1873 1874
	/*
	 * 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) {
已提交
1875

1876 1877 1878 1879 1880 1881
		if (i <= 5)
			msleep(10);
		else if (i <= 10)
			msleep(500);
		else
			msleep(2500);
已提交
1882

1883
		if (i == 15) {
J
James Smart 已提交
1884
				/* Do post */
1885
			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1886 1887 1888 1889 1890
			lpfc_sli_brdrestart(phba);
		}
		/* Read the HBA Host Status Register */
		status = readl(phba->HSregaddr);
	}
已提交
1891

1892 1893
	/* Check to see if any errors occurred during init */
	if ((status & HS_FFERM) || (i >= 20)) {
J
James Smart 已提交
1894
		phba->link_state = LPFC_HBA_ERROR;
1895
		retval = 1;
已提交
1896 1897
	}

1898 1899
	return retval;
}
已提交
1900

1901 1902
#define BARRIER_TEST_PATTERN (0xdeadbeef)

J
James Smart 已提交
1903
void lpfc_reset_barrier(struct lpfc_hba *phba)
1904
{
J
James Smart 已提交
1905 1906
	uint32_t __iomem *resp_buf;
	uint32_t __iomem *mbox_buf;
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
	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 已提交
1922
	resp_buf = phba->MBslimaddr;
1923 1924 1925 1926 1927

	/* Disable the error attention */
	hc_copy = readl(phba->HCregaddr);
	writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
J
James Smart 已提交
1928
	phba->link_flag |= LS_IGNORE_ERATT;
1929 1930 1931 1932

	if (readl(phba->HAregaddr) & HA_ERATT) {
		/* Clear Chip error bit */
		writel(HA_ERATT, phba->HAregaddr);
J
James Smart 已提交
1933
		phba->pport->stopped = 1;
1934 1935 1936 1937 1938 1939 1940
	}

	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 已提交
1941
	mbox_buf = phba->MBslimaddr;
1942 1943 1944 1945 1946 1947 1948 1949
	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 已提交
1950
		    phba->pport->stopped)
1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
			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 已提交
1967
		phba->pport->stopped = 1;
1968 1969 1970
	}

restore_hc:
J
James Smart 已提交
1971
	phba->link_flag &= ~LS_IGNORE_ERATT;
1972 1973 1974 1975
	writel(hc_copy, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
}

1976
int
J
James Smart 已提交
1977
lpfc_sli_brdkill(struct lpfc_hba *phba)
1978 1979 1980 1981 1982 1983 1984
{
	struct lpfc_sli *psli;
	LPFC_MBOXQ_t *pmb;
	uint32_t status;
	uint32_t ha_copy;
	int retval;
	int i = 0;
已提交
1985

1986
	psli = &phba->sli;
已提交
1987

1988
	/* Kill HBA */
1989
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1990 1991
			"0329 Kill HBA Data: x%x x%x\n",
			phba->pport->port_state, psli->sli_flag);
1992

1993 1994
	pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmb)
1995 1996 1997
		return 1;

	/* Disable the error attention */
J
James Smart 已提交
1998
	spin_lock_irq(&phba->hbalock);
1999 2000 2001 2002
	status = readl(phba->HCregaddr);
	status &= ~HC_ERINT_ENA;
	writel(status, phba->HCregaddr);
	readl(phba->HCregaddr); /* flush */
J
James Smart 已提交
2003 2004
	phba->link_flag |= LS_IGNORE_ERATT;
	spin_unlock_irq(&phba->hbalock);
2005 2006 2007 2008 2009 2010 2011 2012

	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 已提交
2013 2014 2015
		spin_lock_irq(&phba->hbalock);
		phba->link_flag &= ~LS_IGNORE_ERATT;
		spin_unlock_irq(&phba->hbalock);
2016 2017 2018
		return 1;
	}

2019 2020
	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035
	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);
2036 2037
	if (ha_copy & HA_ERATT) {
		writel(HA_ERATT, phba->HAregaddr);
J
James Smart 已提交
2038
		phba->pport->stopped = 1;
2039
	}
J
James Smart 已提交
2040
	spin_lock_irq(&phba->hbalock);
2041
	psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2042 2043
	phba->link_flag &= ~LS_IGNORE_ERATT;
	spin_unlock_irq(&phba->hbalock);
2044 2045 2046

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

J
James Smart 已提交
2049
	return ha_copy & HA_ERATT ? 0 : 1;
已提交
2050 2051
}

2052
int
J
James Smart 已提交
2053
lpfc_sli_brdreset(struct lpfc_hba *phba)
已提交
2054
{
2055
	struct lpfc_sli *psli;
已提交
2056
	struct lpfc_sli_ring *pring;
2057
	uint16_t cfg_value;
已提交
2058 2059
	int i;

2060
	psli = &phba->sli;
已提交
2061

2062 2063
	/* Reset HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2064
			"0325 Reset HBA Data: x%x x%x\n",
J
James Smart 已提交
2065
			phba->pport->port_state, psli->sli_flag);
已提交
2066 2067 2068

	/* perform board reset */
	phba->fc_eventTag = 0;
J
James Smart 已提交
2069 2070
	phba->pport->fc_myDID = 0;
	phba->pport->fc_prevDID = 0;
已提交
2071

2072 2073 2074 2075 2076 2077
	/* 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)));

2078
	psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
2079 2080 2081 2082 2083 2084 2085 2086 2087
	/* 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);
已提交
2088 2089

	/* Initialize relevant SLI info */
2090 2091
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
已提交
2092 2093 2094 2095 2096 2097 2098 2099
		pring->flag = 0;
		pring->rspidx = 0;
		pring->next_cmdidx  = 0;
		pring->local_getidx = 0;
		pring->cmdidx = 0;
		pring->missbufcnt = 0;
	}

J
James Smart 已提交
2100
	phba->link_state = LPFC_WARM_START;
2101 2102 2103 2104
	return 0;
}

int
J
James Smart 已提交
2105
lpfc_sli_brdrestart(struct lpfc_hba *phba)
2106 2107 2108 2109 2110 2111 2112
{
	MAILBOX_t *mb;
	struct lpfc_sli *psli;
	uint16_t skip_post;
	volatile uint32_t word0;
	void __iomem *to_slim;

J
James Smart 已提交
2113
	spin_lock_irq(&phba->hbalock);
2114 2115 2116 2117 2118

	psli = &phba->sli;

	/* Restart HBA */
	lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2119
			"0337 Restart HBA Data: x%x x%x\n",
J
James Smart 已提交
2120
			phba->pport->port_state, psli->sli_flag);
2121 2122 2123 2124 2125 2126

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

2127 2128
	lpfc_reset_barrier(phba);

2129 2130 2131 2132 2133
	to_slim = phba->MBslimaddr;
	writel(*(uint32_t *) mb, to_slim);
	readl(to_slim); /* flush */

	/* Only skip post after fc_ffinit is completed */
J
James Smart 已提交
2134
	if (phba->pport->port_state) {
2135 2136
		skip_post = 1;
		word0 = 1;	/* This is really setting up word1 */
已提交
2137
	} else {
2138 2139
		skip_post = 0;
		word0 = 0;	/* This is really setting up word1 */
已提交
2140
	}
J
James Smart 已提交
2141
	to_slim = phba->MBslimaddr + sizeof (uint32_t);
2142 2143
	writel(*(uint32_t *) mb, to_slim);
	readl(to_slim); /* flush */
已提交
2144

2145
	lpfc_sli_brdreset(phba);
J
James Smart 已提交
2146 2147
	phba->pport->stopped = 0;
	phba->link_state = LPFC_INIT_START;
2148

J
James Smart 已提交
2149
	spin_unlock_irq(&phba->hbalock);
2150

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

2154 2155 2156 2157 2158 2159
	if (skip_post)
		mdelay(100);
	else
		mdelay(2000);

	lpfc_hba_down_post(phba);
已提交
2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182

	return 0;
}

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> */
2183
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2184
					"0436 Adapter failed to init, "
2185 2186 2187 2188
					"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 已提交
2189
			phba->link_state = LPFC_HBA_ERROR;
已提交
2190 2191 2192 2193 2194 2195 2196 2197
			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> */
2198
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2199
					"0437 Adapter failed to init, "
2200 2201 2202 2203
					"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 已提交
2204
			phba->link_state = LPFC_HBA_ERROR;
已提交
2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216
			return -EIO;
		}

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

		if (i == 15) {
J
James Smart 已提交
2217
				/* Do post */
2218
			phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2219
			lpfc_sli_brdrestart(phba);
已提交
2220 2221 2222 2223 2224 2225 2226 2227 2228
		}
		/* 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> */
2229
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2230
				"0438 Adapter failed to init, chipset, "
2231 2232 2233 2234
				"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 已提交
2235
		phba->link_state = LPFC_HBA_ERROR;
已提交
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
		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;
}

2249
int
2250 2251
lpfc_sli_hbq_count(void)
{
2252
	return ARRAY_SIZE(lpfc_hbq_defs);
2253 2254 2255 2256 2257 2258 2259 2260 2261 2262
}

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)
2263
		count += lpfc_hbq_defs[i]->entry_count;
2264 2265 2266
	return count;
}

已提交
2267
int
2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281
lpfc_sli_hbq_size(void)
{
	return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
}

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;

2282 2283 2284
				/* Get a Mailbox buffer to setup mailbox
				 * commands for HBA initialization
				 */
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
	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;

	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 =
2301
			lpfc_hbq_defs[hbqno]->entry_count;
2302 2303
		lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
			hbq_entry_index, pmb);
2304 2305 2306 2307 2308 2309 2310
		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,
2311
					LOG_SLI | LOG_VPORT,
2312
					"1805 Adapter failed to init. "
2313
					"Data: x%x x%x x%x\n",
2314
					pmbox->mbxCommand,
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325
					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);

2326 2327 2328 2329 2330
	/* Initially populate or replenish the HBQs */
	for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
		if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
			return -ENOMEM;
	}
2331 2332 2333 2334 2335
	return 0;
}

static int
lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
已提交
2336 2337 2338 2339 2340 2341
{
	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 已提交
2342
		phba->link_state = LPFC_HBA_ERROR;
已提交
2343 2344 2345
		return -ENOMEM;
	}

2346
	phba->sli_rev = sli_mode;
已提交
2347
	while (resetcount < 2 && !done) {
J
James Smart 已提交
2348
		spin_lock_irq(&phba->hbalock);
2349
		phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2350
		spin_unlock_irq(&phba->hbalock);
2351
		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2352
		lpfc_sli_brdrestart(phba);
已提交
2353 2354 2355 2356 2357
		msleep(2500);
		rc = lpfc_sli_chipset_init(phba);
		if (rc)
			break;

J
James Smart 已提交
2358
		spin_lock_irq(&phba->hbalock);
2359
		phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2360
		spin_unlock_irq(&phba->hbalock);
已提交
2361 2362
		resetcount++;

2363 2364 2365 2366 2367
		/* 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.
		 */
已提交
2368 2369
		rc = lpfc_config_port_prep(phba);
		if (rc == -ERESTART) {
2370
			phba->link_state = LPFC_LINK_UNKNOWN;
已提交
2371 2372 2373 2374 2375
			continue;
		} else if (rc) {
			break;
		}

J
James Smart 已提交
2376
		phba->link_state = LPFC_INIT_MBX_CMDS;
已提交
2377 2378
		lpfc_config_port(phba, pmb);
		rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2379
		if (rc != MBX_SUCCESS) {
已提交
2380
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2381
				"0442 Adapter failed to init, mbxCmd x%x "
2382
				"CONFIG_PORT, mbxStatus x%x Data: x%x\n",
2383
				pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
J
James Smart 已提交
2384
			spin_lock_irq(&phba->hbalock);
已提交
2385
			phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
J
James Smart 已提交
2386 2387
			spin_unlock_irq(&phba->hbalock);
			rc = -ENXIO;
2388 2389
		} else {
			done = 1;
2390 2391 2392 2393
			phba->max_vpi = (phba->max_vpi &&
					 pmb->mb.un.varCfgPort.gmv) != 0
				? pmb->mb.un.varCfgPort.max_vpi
				: 0;
已提交
2394 2395
		}
	}
2396 2397 2398 2399 2400 2401 2402

	if (!done) {
		rc = -EINVAL;
		goto do_prep_failed;
	}

	if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
2403
		(!pmb->mb.un.varCfgPort.cMA)) {
2404 2405 2406 2407 2408
		rc = -ENXIO;
		goto do_prep_failed;
	}
	return rc;

2409
do_prep_failed:
2410 2411 2412 2413 2414 2415 2416 2417
	mempool_free(pmb, phba->mbox_mem_pool);
	return rc;
}

int
lpfc_sli_hba_setup(struct lpfc_hba *phba)
{
	uint32_t rc;
2418
	int  mode = 3;
2419 2420 2421

	switch (lpfc_sli_mode) {
	case 2:
2422
		if (phba->cfg_enable_npiv) {
2423
			lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2424
				"1824 NPIV enabled: Override lpfc_sli_mode "
2425
				"parameter (%d) to auto (0).\n",
2426
				lpfc_sli_mode);
2427 2428
			break;
		}
2429 2430 2431 2432 2433 2434
		mode = 2;
		break;
	case 0:
	case 3:
		break;
	default:
2435
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2436 2437
				"1819 Unrecognized lpfc_sli_mode "
				"parameter: %d.\n", lpfc_sli_mode);
2438 2439 2440 2441 2442 2443

		break;
	}

	rc = lpfc_do_config_port(phba, mode);
	if (rc && lpfc_sli_mode == 3)
2444
		lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2445 2446
				"1820 Unable to select SLI-3.  "
				"Not supported by adapter.\n");
2447 2448 2449
	if (rc && mode != 2)
		rc = lpfc_do_config_port(phba, 2);
	if (rc)
已提交
2450 2451
		goto lpfc_sli_hba_setup_error;

2452 2453 2454 2455 2456 2457 2458 2459 2460
	if (phba->sli_rev == 3) {
		phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
		phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
		phba->sli3_options |= LPFC_SLI3_ENABLED;
		phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;

	} else {
		phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
		phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
2461
		phba->sli3_options = 0;
2462 2463 2464
	}

	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2465 2466
			"0444 Firmware in SLI %x mode. Max_vpi %d\n",
			phba->sli_rev, phba->max_vpi);
2467
	rc = lpfc_sli_ring_map(phba);
已提交
2468 2469 2470 2471

	if (rc)
		goto lpfc_sli_hba_setup_error;

2472
				/* Init HBQs */
2473 2474 2475 2476 2477 2478 2479

	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
		rc = lpfc_sli_hbq_setup(phba);
		if (rc)
			goto lpfc_sli_hba_setup_error;
	}

已提交
2480 2481 2482 2483 2484 2485
	phba->sli.sli_flag |= LPFC_PROCESS_LA;

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

2486 2487
	return rc;

2488
lpfc_sli_hba_setup_error:
J
James Smart 已提交
2489
	phba->link_state = LPFC_HBA_ERROR;
2490
	lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2491
			"0445 Firmware initialization failed\n");
已提交
2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510
	return rc;
}

/*! lpfc_mbox_timeout
 *
 * \pre
 * \post
 * \param hba Pointer to per struct lpfc_hba structure
 * \param l1  Pointer to the driver's mailbox queue.
 * \return
 *   void
 *
 * \b Description:
 *
 * This routine handles mailbox timeout events at timer interrupt context.
 */
void
lpfc_mbox_timeout(unsigned long ptr)
{
2511
	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
已提交
2512
	unsigned long iflag;
J
James Smart 已提交
2513
	uint32_t tmo_posted;
已提交
2514

J
James Smart 已提交
2515
	spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
2516
	tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
J
James Smart 已提交
2517 2518 2519 2520 2521
	if (!tmo_posted)
		phba->pport->work_port_events |= WORKER_MBOX_TMO;
	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);

	if (!tmo_posted) {
2522
		spin_lock_irqsave(&phba->hbalock, iflag);
已提交
2523
		if (phba->work_wait)
2524 2525
			lpfc_worker_wake_up(phba);
		spin_unlock_irqrestore(&phba->hbalock, iflag);
已提交
2526 2527 2528 2529 2530 2531
	}
}

void
lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
{
J
James Smart 已提交
2532 2533
	LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
	MAILBOX_t *mb = &pmbox->mb;
2534 2535
	struct lpfc_sli *psli = &phba->sli;
	struct lpfc_sli_ring *pring;
已提交
2536

J
James Smart 已提交
2537
	if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
已提交
2538 2539 2540 2541
		return;
	}

	/* Mbox cmd <mbxCommand> timeout */
2542
	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2543
			"0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
2544 2545 2546 2547
			mb->mbxCommand,
			phba->pport->port_state,
			phba->sli.sli_flag,
			phba->sli.mbox_active);
已提交
2548

2549 2550 2551 2552
	/* 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.
	 */
J
James Smart 已提交
2553 2554 2555 2556 2557 2558
	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;
	phba->pport->fc_flag |= FC_ESTABLISH_LINK;
2559
	psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
J
James Smart 已提交
2560
	spin_unlock_irq(&phba->hbalock);
2561 2562 2563 2564 2565

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

	lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2566
			"0345 Resetting board due to mailbox timeout\n");
2567 2568 2569 2570
	/*
	 * lpfc_offline calls lpfc_sli_hba_down which will clean up
	 * on oustanding mailbox commands.
	 */
2571 2572 2573 2574 2575
	/* If resets are disabled then set error state and return. */
	if (!phba->cfg_enable_hba_reset) {
		phba->link_state = LPFC_HBA_ERROR;
		return;
	}
2576 2577 2578 2579 2580 2581
	lpfc_offline_prep(phba);
	lpfc_offline(phba);
	lpfc_sli_brdrestart(phba);
	if (lpfc_online(phba) == 0)		/* Initialize the HBA */
		mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
	lpfc_unblock_mgmt_io(phba);
已提交
2582 2583 2584 2585
	return;
}

int
J
James Smart 已提交
2586
lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
已提交
2587 2588
{
	MAILBOX_t *mb;
J
James Smart 已提交
2589
	struct lpfc_sli *psli = &phba->sli;
已提交
2590 2591 2592
	uint32_t status, evtctr;
	uint32_t ha_copy;
	int i;
2593
	unsigned long timeout;
已提交
2594 2595 2596 2597
	unsigned long drvr_flag = 0;
	volatile uint32_t word0, ldata;
	void __iomem *to_slim;

2598
	if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
2599
		pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
2600 2601
		if(!pmbox->vport) {
			lpfc_printf_log(phba, KERN_ERR,
2602
					LOG_MBOX | LOG_VPORT,
2603
					"1806 Mbox x%x failed. No vport\n",
2604 2605 2606 2607 2608 2609
					pmbox->mb.mbxCommand);
			dump_stack();
			return MBXERR_ERROR;
		}
	}

2610

2611 2612 2613 2614
	/* If the PCI channel is in offline state, do not post mbox. */
	if (unlikely(pci_channel_offline(phba->pcidev)))
		return MBX_NOT_FINISHED;

J
James Smart 已提交
2615
	spin_lock_irqsave(&phba->hbalock, drvr_flag);
已提交
2616
	psli = &phba->sli;
2617 2618


已提交
2619 2620 2621
	mb = &pmbox->mb;
	status = MBX_SUCCESS;

J
James Smart 已提交
2622 2623
	if (phba->link_state == LPFC_HBA_ERROR) {
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2624 2625

		/* Mbox command <mbxCommand> cannot issue */
2626
		LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
J
James Smart 已提交
2627
		return MBX_NOT_FINISHED;
2628 2629
	}

2630 2631
	if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
	    !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
J
James Smart 已提交
2632
		spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2633
		LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
J
James Smart 已提交
2634
		return MBX_NOT_FINISHED;
2635 2636
	}

已提交
2637 2638 2639 2640 2641 2642 2643
	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 已提交
2644
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
2645 2646

			/* Mbox command <mbxCommand> cannot issue */
2647
			LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
J
James Smart 已提交
2648
			return MBX_NOT_FINISHED;
已提交
2649 2650 2651
		}

		if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
J
James Smart 已提交
2652
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
2653
			/* Mbox command <mbxCommand> cannot issue */
2654
			LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
J
James Smart 已提交
2655
			return MBX_NOT_FINISHED;
已提交
2656 2657 2658 2659 2660 2661 2662 2663
		}

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

		/* Mbox cmd issue - BUSY */
2664
		lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2665
				"(%d):0308 Mbox cmd issue - BUSY Data: "
2666 2667 2668 2669
				"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);
已提交
2670 2671

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

J
James Smart 已提交
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688
		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 已提交
2689
		return MBX_BUSY;
已提交
2690 2691 2692 2693 2694 2695
	}

	psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;

	/* If we are not polling, we MUST be in SLI2 mode */
	if (flag != MBX_POLL) {
2696 2697
		if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
		    (mb->mbxCommand != MBX_KILL_BOARD)) {
已提交
2698
			psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2699
			spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
已提交
2700
			/* Mbox command <mbxCommand> cannot issue */
2701
			LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
J
James Smart 已提交
2702
			return MBX_NOT_FINISHED;
已提交
2703 2704
		}
		/* timeout active mbox command */
2705 2706
		mod_timer(&psli->mbox_tmo, (jiffies +
			       (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
已提交
2707 2708 2709
	}

	/* Mailbox cmd <cmd> issue */
2710
	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2711
			"(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
2712
			"x%x\n",
2713
			pmbox->vport ? pmbox->vport->vpi : 0,
2714 2715
			mb->mbxCommand, phba->pport->port_state,
			psli->sli_flag, flag);
已提交
2716

J
James Smart 已提交
2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733
	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]);
		}
	}

已提交
2734 2735 2736 2737 2738 2739 2740 2741
	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;

	if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
		/* First copy command data to host SLIM area */
2742
		lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
已提交
2743
	} else {
2744
		if (mb->mbxCommand == MBX_CONFIG_PORT) {
已提交
2745
			/* copy command data into host mbox for cmpl */
2746
			lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2747
					      MAILBOX_CMD_SIZE);
已提交
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
		}

		/* 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 */
		ldata = *((volatile uint32_t *)mb);
		to_slim = phba->MBslimaddr;
		writel(ldata, to_slim);
		readl(to_slim); /* flush */

		if (mb->mbxCommand == MBX_CONFIG_PORT) {
			/* switch over to host mailbox */
			psli->sli_flag |= LPFC_SLI2_ACTIVE;
		}
	}

	wmb();

	switch (flag) {
	case MBX_NOWAIT:
2772
		/* Set up reference to mailbox command */
已提交
2773
		psli->mbox_active = pmbox;
2774 2775 2776 2777
		/* Interrupt board to do it */
		writel(CA_MBATT, phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */
		/* Don't wait for it to finish, just return */
已提交
2778 2779 2780
		break;

	case MBX_POLL:
2781
		/* Set up null reference to mailbox command */
已提交
2782
		psli->mbox_active = NULL;
2783 2784 2785 2786
		/* Interrupt board to do it */
		writel(CA_MBATT, phba->CAregaddr);
		readl(phba->CAregaddr); /* flush */

已提交
2787 2788
		if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
			/* First read mbox status word */
2789
			word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
已提交
2790 2791 2792 2793 2794 2795 2796 2797
			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);
2798 2799 2800 2801
		timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
							     mb->mbxCommand) *
					   1000) + jiffies;
		i = 0;
已提交
2802
		/* Wait for command to complete */
2803 2804
		while (((word0 & OWN_CHIP) == OWN_CHIP) ||
		       (!(ha_copy & HA_MBATT) &&
J
James Smart 已提交
2805
			(phba->link_state > LPFC_WARM_START))) {
2806
			if (time_after(jiffies, timeout)) {
已提交
2807
				psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
J
James Smart 已提交
2808
				spin_unlock_irqrestore(&phba->hbalock,
已提交
2809
						       drvr_flag);
J
James Smart 已提交
2810
				return MBX_NOT_FINISHED;
已提交
2811 2812 2813 2814 2815 2816 2817 2818
			}

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

2819 2820 2821 2822 2823 2824
			if (i++ > 10) {
				spin_unlock_irqrestore(&phba->hbalock,
						       drvr_flag);
				msleep(1);
				spin_lock_irqsave(&phba->hbalock, drvr_flag);
			}
已提交
2825 2826 2827

			if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
				/* First copy command data */
2828 2829
				word0 = *((volatile uint32_t *)
						&phba->slim2p->mbx);
已提交
2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853
				word0 = le32_to_cpu(word0);
				if (mb->mbxCommand == MBX_CONFIG_PORT) {
					MAILBOX_t *slimmb;
					volatile uint32_t slimword0;
					/* 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 &=
						    ~LPFC_SLI2_ACTIVE;
						word0 = slimword0;
					}
				}
			} else {
				/* First copy command data */
				word0 = readl(phba->MBslimaddr);
			}
			/* Read the HBA Host Attention Register */
			ha_copy = readl(phba->HAregaddr);
		}

		if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
			/* copy results back to user */
2854
			lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2855
					      MAILBOX_CMD_SIZE);
已提交
2856 2857 2858 2859 2860 2861
		} else {
			/* First copy command data */
			lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
							MAILBOX_CMD_SIZE);
			if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
				pmbox->context2) {
2862
				lpfc_memcpy_from_slim((void *)pmbox->context2,
已提交
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874
				      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 已提交
2875 2876
	spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
	return status;
已提交
2877 2878
}

2879 2880 2881
/*
 * Caller needs to hold lock.
 */
J
James Smart 已提交
2882
static void
2883
__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
J
James Smart 已提交
2884
		    struct lpfc_iocbq *piocb)
已提交
2885 2886 2887 2888 2889 2890 2891 2892
{
	/* Insert the caller's iocb in the txq tail for later processing. */
	list_add_tail(&piocb->list, &pring->txq);
	pring->txq_cnt++;
}

static struct lpfc_iocbq *
lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
J
James Smart 已提交
2893
		   struct lpfc_iocbq **piocb)
已提交
2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905
{
	struct lpfc_iocbq * nextiocb;

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

	return nextiocb;
}

2906 2907 2908
/*
 * Lockless version of lpfc_sli_issue_iocb.
 */
2909
static int
2910
__lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
已提交
2911 2912 2913 2914 2915
		    struct lpfc_iocbq *piocb, uint32_t flag)
{
	struct lpfc_iocbq *nextiocb;
	IOCB_t *iocb;

2916 2917 2918 2919 2920
	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,
2921
				"1807 IOCB x%x failed. No vport\n",
2922 2923 2924 2925 2926 2927
				piocb->iocb.ulpCommand);
		dump_stack();
		return IOCB_ERROR;
	}


2928 2929 2930 2931
	/* If the PCI channel is in offline state, do not post iocbs. */
	if (unlikely(pci_channel_offline(phba->pcidev)))
		return IOCB_ERROR;

已提交
2932 2933 2934
	/*
	 * We should never get an IOCB if we are in a < LINK_DOWN state
	 */
J
James Smart 已提交
2935
	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
已提交
2936 2937 2938 2939
		return IOCB_ERROR;

	/*
	 * Check to see if we are blocking IOCB processing because of a
2940
	 * outstanding event.
已提交
2941
	 */
2942
	if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
已提交
2943 2944
		goto iocb_busy;

J
James Smart 已提交
2945
	if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
已提交
2946
		/*
2947
		 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
已提交
2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960
		 * can be issued if the link is not up.
		 */
		switch (piocb->iocb.ulpCommand) {
		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:
2961 2962
		case CMD_CLOSE_XRI_CN:
		case CMD_CLOSE_XRI_CX:
已提交
2963 2964 2965 2966 2967 2968 2969 2970 2971 2972
			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 &&
2973
			    !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
已提交
2974
		goto iocb_busy;
2975
	}
已提交
2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996

	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)) {
2997
		__lpfc_sli_ringtx_put(phba, pring, piocb);
已提交
2998 2999 3000 3001 3002 3003
		return IOCB_SUCCESS;
	}

	return IOCB_BUSY;
}

3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018

int
lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
		    struct lpfc_iocbq *piocb, uint32_t flag)
{
	unsigned long iflags;
	int rc;

	spin_lock_irqsave(&phba->hbalock, iflags);
	rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
	spin_unlock_irqrestore(&phba->hbalock, iflags);

	return rc;
}

3019 3020 3021 3022 3023 3024 3025 3026 3027
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 */
3028 3029

	/* Take some away from the FCP ring */
3030 3031 3032 3033 3034 3035
	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;

3036 3037 3038
	/* and give them to the extra ring */
	pring = &psli->ring[psli->extra_ring];

3039 3040 3041 3042 3043 3044 3045 3046 3047
	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 */
3048 3049
	pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
	pring->prt[0].type = phba->cfg_multi_ring_type;
3050 3051 3052 3053
	pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
	return 0;
}

3054
static void
3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
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;

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

	if ((evt_code != ASYNC_TEMP_WARN) &&
		(evt_code != ASYNC_TEMP_SAFE)) {
		lpfc_printf_log(phba,
			KERN_ERR,
			LOG_SLI,
3073
			"0346 Ring %d handler: unexpected ASYNC_STATUS"
3074 3075 3076 3077 3078 3079 3080 3081 3082 3083
			" evt_code 0x%x\n",
			pring->ringno,
			icmd->un.asyncstat.evt_code);
		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,
3084
				KERN_ERR,
3085
				LOG_TEMP,
3086
				"0347 Adapter is very hot, please take "
3087 3088 3089 3090 3091 3092
				"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,
3093
				KERN_ERR,
3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108
				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,
		SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);

}


已提交
3109 3110 3111
int
lpfc_sli_setup(struct lpfc_hba *phba)
{
3112
	int i, totiocbsize = 0;
已提交
3113 3114 3115 3116 3117 3118 3119
	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;
3120
	psli->extra_ring = LPFC_EXTRA_RING;
已提交
3121

3122 3123 3124 3125
	psli->iocbq_lookup = NULL;
	psli->iocbq_lookup_len = 0;
	psli->last_iotag = 0;

已提交
3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136
	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;
3137
			pring->sizeCiocb = (phba->sli_rev == 3) ?
3138 3139
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
3140
			pring->sizeRiocb = (phba->sli_rev == 3) ?
3141 3142
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
已提交
3143 3144
			pring->iotag_ctr = 0;
			pring->iotag_max =
3145
			    (phba->cfg_hba_queue_depth * 2);
已提交
3146 3147 3148
			pring->fast_iotag = pring->iotag_max;
			pring->num_mask = 0;
			break;
3149
		case LPFC_EXTRA_RING:	/* ring 1 - EXTRA */
已提交
3150 3151 3152
			/* numCiocb and numRiocb are used in config_port */
			pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
			pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
3153
			pring->sizeCiocb = (phba->sli_rev == 3) ?
3154 3155
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
3156
			pring->sizeRiocb = (phba->sli_rev == 3) ?
3157 3158
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
J
James Smart 已提交
3159
			pring->iotag_max = phba->cfg_hba_queue_depth;
已提交
3160 3161 3162 3163 3164 3165
			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;
3166
			pring->sizeCiocb = (phba->sli_rev == 3) ?
3167 3168
							SLI3_IOCB_CMD_SIZE :
							SLI2_IOCB_CMD_SIZE;
3169
			pring->sizeRiocb = (phba->sli_rev == 3) ?
3170 3171
							SLI3_IOCB_RSP_SIZE :
							SLI2_IOCB_RSP_SIZE;
已提交
3172 3173 3174
			pring->fast_iotag = 0;
			pring->iotag_ctr = 0;
			pring->iotag_max = 4096;
3175 3176
			pring->lpfc_sli_rcv_async_status =
				lpfc_sli_async_event_handler;
已提交
3177 3178 3179 3180 3181
			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 =
3182
			    lpfc_els_unsol_event;
已提交
3183 3184 3185 3186
			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 =
3187
			    lpfc_els_unsol_event;
已提交
3188 3189 3190 3191 3192 3193
			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 =
3194
			    lpfc_ct_unsol_event;
已提交
3195 3196 3197 3198 3199 3200
			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 =
3201
			    lpfc_ct_unsol_event;
已提交
3202 3203
			break;
		}
3204
		totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
3205
				(pring->numRiocb * pring->sizeRiocb);
已提交
3206
	}
3207
	if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
已提交
3208
		/* Too many cmd / rsp ring entries in SLI2 SLIM */
3209 3210 3211 3212
		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);
已提交
3213
	}
3214 3215
	if (phba->cfg_multi_ring_support == 2)
		lpfc_extra_ring_setup(phba);
已提交
3216 3217 3218 3219 3220

	return 0;
}

int
J
James Smart 已提交
3221
lpfc_sli_queue_setup(struct lpfc_hba *phba)
已提交
3222 3223 3224
{
	struct lpfc_sli *psli;
	struct lpfc_sli_ring *pring;
3225
	int i;
已提交
3226 3227

	psli = &phba->sli;
J
James Smart 已提交
3228
	spin_lock_irq(&phba->hbalock);
已提交
3229
	INIT_LIST_HEAD(&psli->mboxq);
3230
	INIT_LIST_HEAD(&psli->mboxq_cmpl);
已提交
3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242
	/* 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);
		INIT_LIST_HEAD(&pring->postbufq);
	}
J
James Smart 已提交
3243 3244
	spin_unlock_irq(&phba->hbalock);
	return 1;
已提交
3245 3246
}

3247 3248 3249
int
lpfc_sli_host_down(struct lpfc_vport *vport)
{
J
James Smart 已提交
3250
	LIST_HEAD(completions);
3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264
	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;
J
James Smart 已提交
3265 3266
		if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
			pring->flag |= LPFC_DEFERRED_RING_EVENT;
3267 3268 3269 3270 3271 3272 3273
		/*
		 * 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 已提交
3274
			list_move_tail(&iocb->list, &completions);
3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290
			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);

J
James Smart 已提交
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
	while (!list_empty(&completions)) {
		list_remove_head(&completions, iocb, struct lpfc_iocbq, list);

		if (!iocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, iocb);
		else {
			iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
			iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
			(iocb->iocb_cmpl) (phba, iocb, iocb);
		}
	}
3302 3303 3304
	return 1;
}

已提交
3305
int
J
James Smart 已提交
3306
lpfc_sli_hba_down(struct lpfc_hba *phba)
已提交
3307
{
3308
	LIST_HEAD(completions);
J
James Smart 已提交
3309
	struct lpfc_sli *psli = &phba->sli;
已提交
3310
	struct lpfc_sli_ring *pring;
3311
	struct lpfc_dmabuf *buf_ptr;
已提交
3312
	LPFC_MBOXQ_t *pmb;
3313 3314
	struct lpfc_iocbq *iocb;
	IOCB_t *cmd = NULL;
已提交
3315 3316 3317 3318 3319
	int i;
	unsigned long flags = 0;

	lpfc_hba_down_prep(phba);

3320 3321
	lpfc_fabric_abort_hba(phba);

J
James Smart 已提交
3322
	spin_lock_irqsave(&phba->hbalock, flags);
已提交
3323 3324
	for (i = 0; i < psli->num_rings; i++) {
		pring = &psli->ring[i];
J
James Smart 已提交
3325 3326
		if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
			pring->flag |= LPFC_DEFERRED_RING_EVENT;
已提交
3327 3328 3329 3330 3331

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

3335
	}
J
James Smart 已提交
3336
	spin_unlock_irqrestore(&phba->hbalock, flags);
已提交
3337

3338
	while (!list_empty(&completions)) {
3339
		list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3340
		cmd = &iocb->iocb;
已提交
3341

J
James Smart 已提交
3342 3343 3344
		if (!iocb->iocb_cmpl)
			lpfc_sli_release_iocbq(phba, iocb);
		else {
3345 3346 3347
			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
			cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
			(iocb->iocb_cmpl) (phba, iocb, iocb);
J
James Smart 已提交
3348
		}
已提交
3349 3350
	}

3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363
	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);
	}

已提交
3364 3365
	/* Return any active mbox cmds */
	del_timer_sync(&psli->mbox_tmo);
3366
	spin_lock_irqsave(&phba->hbalock, flags);
J
James Smart 已提交
3367

3368
	spin_lock(&phba->pport->work_port_lock);
J
James Smart 已提交
3369
	phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3370
	spin_unlock(&phba->pport->work_port_lock);
J
James Smart 已提交
3371

3372 3373
	if (psli->mbox_active) {
		list_add_tail(&psli->mbox_active->list, &completions);
J
James Smart 已提交
3374 3375
		psli->mbox_active = NULL;
		psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
已提交
3376 3377
	}

3378 3379 3380 3381 3382 3383 3384 3385 3386 3387
	/* Return any pending or completed mbox cmds */
	list_splice_init(&phba->sli.mboxq, &completions);
	list_splice_init(&phba->sli.mboxq_cmpl, &completions);
	INIT_LIST_HEAD(&psli->mboxq);
	INIT_LIST_HEAD(&psli->mboxq_cmpl);

	spin_unlock_irqrestore(&phba->hbalock, flags);

	while (!list_empty(&completions)) {
		list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
已提交
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413
		pmb->mb.mbxStatus = MBX_NOT_FINISHED;
		if (pmb->mbox_cmpl) {
			pmb->mbox_cmpl(phba,pmb);
		}
	}
	return 1;
}

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++;
	}
}

int
J
James Smart 已提交
3414 3415
lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			 struct lpfc_dmabuf *mp)
已提交
3416 3417 3418
{
	/* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
	   later */
J
James Smart 已提交
3419
	spin_lock_irq(&phba->hbalock);
已提交
3420 3421
	list_add_tail(&mp->list, &pring->postbufq);
	pring->postbufq_cnt++;
J
James Smart 已提交
3422
	spin_unlock_irq(&phba->hbalock);
已提交
3423 3424 3425
	return 0;
}

3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466
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;
}

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,
			"0410 Cannot find virtual addr for buffer tag on "
			"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;
}
已提交
3467 3468 3469 3470 3471 3472 3473 3474 3475

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 已提交
3476
	spin_lock_irq(&phba->hbalock);
已提交
3477 3478 3479 3480
	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 已提交
3481
			spin_unlock_irq(&phba->hbalock);
已提交
3482 3483 3484 3485
			return mp;
		}
	}

J
James Smart 已提交
3486
	spin_unlock_irq(&phba->hbalock);
已提交
3487
	lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3488
			"0410 Cannot find virtual addr for mapped buf on "
已提交
3489
			"ring %d Data x%llx x%p x%p x%x\n",
3490
			pring->ringno, (unsigned long long)phys,
已提交
3491 3492 3493 3494 3495
			slp->next, slp->prev, pring->postbufq_cnt);
	return NULL;
}

static void
J
James Smart 已提交
3496 3497
lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
已提交
3498
{
J
James Smart 已提交
3499
	IOCB_t *irsp = &rspiocb->iocb;
3500
	uint16_t abort_iotag, abort_context;
3501
	struct lpfc_iocbq *abort_iocb;
3502 3503 3504 3505 3506 3507 3508 3509
	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 已提交
3510
		spin_lock_irq(&phba->hbalock);
3511 3512 3513
		if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
			abort_iocb = phba->sli.iocbq_lookup[abort_iotag];

3514
		lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
3515
				"0327 Cannot abort els iocb %p "
3516 3517
				"with tag %x context %x, abort status %x, "
				"abort code %x\n",
3518 3519
				abort_iocb, abort_iotag, abort_context,
				irsp->ulpStatus, irsp->un.ulpWord[4]);
3520 3521 3522 3523 3524

		/*
		 * make sure we have the right iocbq before taking it
		 * off the txcmplq and try to call completion routine.
		 */
J
James Smart 已提交
3525 3526 3527 3528 3529
		if (!abort_iocb ||
		    abort_iocb->iocb.ulpContext != abort_context ||
		    (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
			spin_unlock_irq(&phba->hbalock);
		else {
3530
			list_del_init(&abort_iocb->list);
3531
			pring->txcmplq_cnt--;
J
James Smart 已提交
3532
			spin_unlock_irq(&phba->hbalock);
3533

3534 3535 3536 3537 3538 3539
			/* 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;

3540 3541 3542 3543
			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);
3544 3545 3546
		}
	}

3547
	lpfc_sli_release_iocbq(phba, cmdiocb);
已提交
3548 3549 3550
	return;
}

3551 3552 3553 3554 3555 3556 3557 3558
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,
3559
			"0133 Ignoring ELS cmd tag x%x completion Data: "
3560
			"x%x x%x x%x\n",
3561
			irsp->ulpIoTag, irsp->ulpStatus,
3562
			irsp->un.ulpWord[4], irsp->ulpTimeout);
J
James Smart 已提交
3563 3564 3565 3566
	if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
		lpfc_ct_free_iocb(phba, cmdiocb);
	else
		lpfc_els_free_iocb(phba, cmdiocb);
3567 3568 3569
	return;
}

已提交
3570
int
J
James Smart 已提交
3571 3572
lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			   struct lpfc_iocbq *cmdiocb)
已提交
3573
{
J
James Smart 已提交
3574
	struct lpfc_vport *vport = cmdiocb->vport;
3575
	struct lpfc_iocbq *abtsiocbp;
已提交
3576 3577
	IOCB_t *icmd = NULL;
	IOCB_t *iabt = NULL;
3578 3579
	int retval = IOCB_ERROR;

3580 3581 3582 3583
	/*
	 * 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.
3584 3585
	 */
	icmd = &cmdiocb->iocb;
J
James Smart 已提交
3586
	if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
3587 3588
	    icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
	    (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
3589 3590
		return 0;

J
James Smart 已提交
3591 3592
	/* If we're unloading, don't abort iocb on the ELS ring, but change the
	 * callback so that nothing happens when it finishes.
3593
	 */
J
James Smart 已提交
3594 3595
	if ((vport->load_flag & FC_UNLOADING) &&
	    (pring->ringno == LPFC_ELS_RING)) {
3596 3597 3598 3599
		if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
			cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
		else
			cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
3600
		goto abort_iotag_exit;
3601
	}
已提交
3602 3603

	/* issue ABTS for this IOCB based on iotag */
3604
	abtsiocbp = __lpfc_sli_get_iocbq(phba);
已提交
3605 3606 3607
	if (abtsiocbp == NULL)
		return 0;

3608 3609 3610 3611 3612
	/* This signals the response to set the correct status
	 * before calling the completion handler.
	 */
	cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;

已提交
3613
	iabt = &abtsiocbp->iocb;
3614 3615 3616 3617 3618
	iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
	iabt->un.acxri.abortContextTag = icmd->ulpContext;
	iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
	iabt->ulpLe = 1;
	iabt->ulpClass = icmd->ulpClass;
已提交
3619

J
James Smart 已提交
3620
	if (phba->link_state >= LPFC_LINK_UP)
3621 3622 3623
		iabt->ulpCommand = CMD_ABORT_XRI_CN;
	else
		iabt->ulpCommand = CMD_CLOSE_XRI_CN;
已提交
3624

3625
	abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
3626

3627 3628 3629 3630 3631
	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);
3632
	retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
已提交
3633

3634
abort_iotag_exit:
J
James Smart 已提交
3635 3636 3637 3638
	/*
	 * 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.
3639
	 */
J
James Smart 已提交
3640
	return retval;
已提交
3641 3642 3643
}

static int
3644 3645
lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
			   uint16_t tgt_id, uint64_t lun_id,
3646
			   lpfc_ctx_cmd ctx_cmd)
已提交
3647
{
3648 3649
	struct lpfc_scsi_buf *lpfc_cmd;
	struct scsi_cmnd *cmnd;
已提交
3650 3651
	int rc = 1;

3652 3653 3654
	if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
		return rc;

3655 3656 3657
	if (iocbq->vport != vport)
		return rc;

3658 3659 3660 3661
	lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
	cmnd = lpfc_cmd->pCmd;

	if (cmnd == NULL)
已提交
3662 3663 3664 3665
		return rc;

	switch (ctx_cmd) {
	case LPFC_CTX_LUN:
3666 3667
		if ((cmnd->device->id == tgt_id) &&
		    (cmnd->device->lun == lun_id))
已提交
3668 3669 3670
			rc = 0;
		break;
	case LPFC_CTX_TGT:
3671
		if (cmnd->device->id == tgt_id)
已提交
3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686
			rc = 0;
		break;
	case LPFC_CTX_HOST:
		rc = 0;
		break;
	default:
		printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
			__FUNCTION__, ctx_cmd);
		break;
	}

	return rc;
}

int
3687 3688
lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
		  lpfc_ctx_cmd ctx_cmd)
已提交
3689
{
3690
	struct lpfc_hba *phba = vport->phba;
3691 3692
	struct lpfc_iocbq *iocbq;
	int sum, i;
已提交
3693

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

3697 3698
		if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
						ctx_cmd) == 0)
3699
			sum++;
已提交
3700
	}
3701

已提交
3702 3703 3704
	return sum;
}

3705
void
J
James Smart 已提交
3706 3707
lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
			struct lpfc_iocbq *rspiocb)
3708
{
3709
	lpfc_sli_release_iocbq(phba, cmdiocb);
3710 3711 3712
	return;
}

已提交
3713
int
3714 3715
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)
已提交
3716
{
3717
	struct lpfc_hba *phba = vport->phba;
3718 3719
	struct lpfc_iocbq *iocbq;
	struct lpfc_iocbq *abtsiocb;
已提交
3720 3721
	IOCB_t *cmd = NULL;
	int errcnt = 0, ret_val = 0;
3722
	int i;
已提交
3723

3724 3725
	for (i = 1; i <= phba->sli.last_iotag; i++) {
		iocbq = phba->sli.iocbq_lookup[i];
已提交
3726

3727
		if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
J
James Smart 已提交
3728
					       abort_cmd) != 0)
已提交
3729 3730 3731
			continue;

		/* issue ABTS for this IOCB based on iotag */
3732
		abtsiocb = lpfc_sli_get_iocbq(phba);
已提交
3733 3734 3735 3736 3737
		if (abtsiocb == NULL) {
			errcnt++;
			continue;
		}

3738
		cmd = &iocbq->iocb;
已提交
3739 3740 3741 3742 3743
		abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
		abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
		abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
		abtsiocb->iocb.ulpLe = 1;
		abtsiocb->iocb.ulpClass = cmd->ulpClass;
J
James Smart 已提交
3744
		abtsiocb->vport = phba->pport;
已提交
3745

J
James Smart 已提交
3746
		if (lpfc_is_link_up(phba))
已提交
3747 3748 3749 3750
			abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
		else
			abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;

3751 3752
		/* Setup callback routine and issue the command. */
		abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
已提交
3753 3754
		ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
		if (ret_val == IOCB_ERROR) {
3755
			lpfc_sli_release_iocbq(phba, abtsiocb);
已提交
3756 3757 3758 3759 3760 3761 3762 3763
			errcnt++;
			continue;
		}
	}

	return errcnt;
}

3764 3765 3766 3767
static void
lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
已提交
3768
{
3769 3770
	wait_queue_head_t *pdone_q;
	unsigned long iflags;
已提交
3771

J
James Smart 已提交
3772
	spin_lock_irqsave(&phba->hbalock, iflags);
3773 3774 3775 3776 3777 3778 3779 3780
	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 已提交
3781
	spin_unlock_irqrestore(&phba->hbalock, iflags);
已提交
3782 3783 3784
	return;
}

3785 3786 3787 3788 3789 3790
/*
 * Issue the caller's iocb and wait for its completion, but no longer than the
 * caller's timeout.  Note that iocb_flags is cleared before the
 * lpfc_sli_issue_call since the wake routine sets a unique value and by
 * definition this is a wait function.
 */
3791

已提交
3792
int
J
James Smart 已提交
3793 3794 3795 3796
lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
			 struct lpfc_sli_ring *pring,
			 struct lpfc_iocbq *piocb,
			 struct lpfc_iocbq *prspiocbq,
3797
			 uint32_t timeout)
已提交
3798
{
3799
	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
3800 3801
	long timeleft, timeout_req = 0;
	int retval = IOCB_SUCCESS;
3802
	uint32_t creg_val;
已提交
3803 3804

	/*
3805 3806
	 * If the caller has provided a response iocbq buffer, then context2
	 * is NULL or its an error.
已提交
3807
	 */
3808 3809 3810 3811
	if (prspiocbq) {
		if (piocb->context2)
			return IOCB_ERROR;
		piocb->context2 = prspiocbq;
已提交
3812 3813
	}

3814 3815 3816
	piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
	piocb->context_un.wait_queue = &done_q;
	piocb->iocb_flag &= ~LPFC_IO_WAKE;
已提交
3817

3818 3819 3820 3821 3822 3823 3824
	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 */
	}

3825 3826 3827 3828 3829 3830
	retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
	if (retval == IOCB_SUCCESS) {
		timeout_req = timeout * HZ;
		timeleft = wait_event_timeout(done_q,
				piocb->iocb_flag & LPFC_IO_WAKE,
				timeout_req);
已提交
3831

3832 3833
		if (piocb->iocb_flag & LPFC_IO_WAKE) {
			lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3834
					"0331 IOCB wake signaled\n");
3835
		} else if (timeleft == 0) {
3836
			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3837 3838
					"0338 IOCB wait timeout error - no "
					"wake response Data x%x\n", timeout);
3839
			retval = IOCB_TIMEDOUT;
3840
		} else {
3841
			lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3842 3843
					"0330 IOCB wake NOT set, "
					"Data x%x x%lx\n",
3844 3845
					timeout, (timeleft / jiffies));
			retval = IOCB_TIMEDOUT;
已提交
3846
		}
3847 3848
	} else {
		lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3849 3850
				":0332 IOCB wait issue failed, Data x%x\n",
				retval);
3851
		retval = IOCB_ERROR;
已提交
3852 3853
	}

3854 3855 3856 3857 3858 3859 3860
	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 */
	}

3861 3862 3863 3864 3865
	if (prspiocbq)
		piocb->context2 = NULL;

	piocb->context_un.wait_queue = NULL;
	piocb->iocb_cmpl = NULL;
已提交
3866 3867
	return retval;
}
3868

已提交
3869
int
J
James Smart 已提交
3870
lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
已提交
3871 3872
			 uint32_t timeout)
{
3873
	DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
已提交
3874
	int retval;
J
James Smart 已提交
3875
	unsigned long flag;
已提交
3876 3877

	/* The caller must leave context1 empty. */
3878
	if (pmboxq->context1)
J
James Smart 已提交
3879
		return MBX_NOT_FINISHED;
已提交
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889

	/* 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) {
3890 3891 3892 3893
		wait_event_interruptible_timeout(done_q,
				pmboxq->mbox_flag & LPFC_MBX_WAKE,
				timeout * HZ);

J
James Smart 已提交
3894
		spin_lock_irqsave(&phba->hbalock, flag);
已提交
3895
		pmboxq->context1 = NULL;
3896 3897 3898 3899 3900
		/*
		 * if LPFC_MBX_WAKE flag is set the mailbox is completed
		 * else do not free the resources.
		 */
		if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
已提交
3901
			retval = MBX_SUCCESS;
J
James Smart 已提交
3902
		else {
3903
			retval = MBX_TIMEOUT;
J
James Smart 已提交
3904 3905 3906
			pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
		}
		spin_unlock_irqrestore(&phba->hbalock, flag);
已提交
3907 3908 3909 3910 3911
	}

	return retval;
}

3912 3913 3914
int
lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
{
J
James Smart 已提交
3915
	struct lpfc_vport *vport = phba->pport;
3916
	int i = 0;
3917
	uint32_t ha_copy;
3918

J
James Smart 已提交
3919
	while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
3920 3921 3922
		if (i++ > LPFC_MBOX_TMO * 1000)
			return 1;

3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935
		/*
		 * Call lpfc_sli_handle_mb_event only if a mailbox cmd
		 * did finish. This way we won't get the misleading
		 * "Stray Mailbox Interrupt" message.
		 */
		spin_lock_irq(&phba->hbalock);
		ha_copy = phba->work_ha;
		phba->work_ha &= ~HA_MBATT;
		spin_unlock_irq(&phba->hbalock);

		if (ha_copy & HA_MBATT)
			if (lpfc_sli_handle_mb_event(phba) == 0)
				i = 0;
3936 3937 3938 3939 3940 3941 3942

		msleep(1);
	}

	return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
}

已提交
3943
irqreturn_t
3944
lpfc_intr_handler(int irq, void *dev_id)
已提交
3945
{
J
James Smart 已提交
3946
	struct lpfc_hba  *phba;
已提交
3947 3948 3949 3950 3951
	uint32_t ha_copy;
	uint32_t work_ha_copy;
	unsigned long status;
	uint32_t control;

3952
	MAILBOX_t *mbox, *pmbox;
J
James Smart 已提交
3953 3954 3955
	struct lpfc_vport *vport;
	struct lpfc_nodelist *ndlp;
	struct lpfc_dmabuf *mp;
3956 3957 3958
	LPFC_MBOXQ_t *pmb;
	int rc;

已提交
3959 3960 3961 3962 3963 3964 3965 3966 3967
	/*
	 * 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;

3968 3969 3970 3971
	/* If the pci channel is offline, ignore all the interrupts. */
	if (unlikely(pci_channel_offline(phba->pcidev)))
		return IRQ_NONE;

已提交
3972 3973 3974 3975 3976 3977 3978 3979
	phba->sli.slistat.sli_intr++;

	/*
	 * Call the HBA to see if it is interrupting.  If not, don't claim
	 * the interrupt
	 */

	/* Ignore all interrupts during initialization. */
J
James Smart 已提交
3980
	if (unlikely(phba->link_state < LPFC_LINK_DOWN))
已提交
3981 3982 3983 3984 3985 3986 3987
		return IRQ_NONE;

	/*
	 * Read host attention register to determine interrupt source
	 * Clear Attention Sources, except Error Attention (to
	 * preserve status) and Link Attention
	 */
J
James Smart 已提交
3988
	spin_lock(&phba->hbalock);
已提交
3989
	ha_copy = readl(phba->HAregaddr);
3990 3991 3992
	/* If somebody is waiting to handle an eratt don't process it
	 * here.  The brdkill function will do this.
	 */
J
James Smart 已提交
3993
	if (phba->link_flag & LS_IGNORE_ERATT)
3994
		ha_copy &= ~HA_ERATT;
已提交
3995 3996
	writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
	readl(phba->HAregaddr); /* flush */
J
James Smart 已提交
3997
	spin_unlock(&phba->hbalock);
已提交
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010

	if (unlikely(!ha_copy))
		return IRQ_NONE;

	work_ha_copy = ha_copy & phba->work_ha_mask;

	if (unlikely(work_ha_copy)) {
		if (work_ha_copy & HA_LATT) {
			if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
				/*
				 * Turn off Link Attention interrupts
				 * until CLEAR_LA done
				 */
J
James Smart 已提交
4011
				spin_lock(&phba->hbalock);
已提交
4012 4013 4014 4015 4016
				phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
				control = readl(phba->HCregaddr);
				control &= ~HC_LAINT_ENA;
				writel(control, phba->HCregaddr);
				readl(phba->HCregaddr); /* flush */
J
James Smart 已提交
4017
				spin_unlock(&phba->hbalock);
已提交
4018 4019 4020 4021 4022 4023
			}
			else
				work_ha_copy &= ~HA_LATT;
		}

		if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
J
James Smart 已提交
4024 4025 4026 4027 4028 4029 4030 4031 4032 4033
			/*
			 * 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) {
				spin_lock(&phba->hbalock);
				control = readl(phba->HCregaddr);
4034 4035 4036 4037 4038 4039

				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 已提交
4040
				if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
4041 4042 4043 4044 4045 4046 4047
					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)
						phba->work_wait));

J
James Smart 已提交
4048 4049
					control &=
					    ~(HC_R0INT_ENA << LPFC_ELS_RING);
已提交
4050 4051 4052
					writel(control, phba->HCregaddr);
					readl(phba->HCregaddr); /* flush */
				}
4053 4054 4055 4056 4057 4058 4059 4060
				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)
						phba->work_wait));
				}
J
James Smart 已提交
4061
				spin_unlock(&phba->hbalock);
已提交
4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079
			}
		}

		if (work_ha_copy & HA_ERATT) {
			/*
			 * There was a link/board error.  Read the
			 * status register to retrieve the error event
			 * and process it.
			 */
			phba->sli.slistat.err_attn_event++;
			/* Save status info */
			phba->work_hs = readl(phba->HSregaddr);
			phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
			phba->work_status[1] = readl(phba->MBslimaddr + 0xac);

			/* Clear Chip error bit */
			writel(HA_ERATT, phba->HAregaddr);
			readl(phba->HAregaddr); /* flush */
J
James Smart 已提交
4080
			phba->pport->stopped = 1;
已提交
4081 4082
		}

4083 4084 4085 4086 4087
		if ((work_ha_copy & HA_MBATT) &&
		    (phba->sli.mbox_active)) {
			pmb = phba->sli.mbox_active;
			pmbox = &pmb->mb;
			mbox = &phba->slim2p->mbx;
J
James Smart 已提交
4088
			vport = pmb->vport;
4089 4090 4091 4092 4093 4094 4095 4096

			/* First check out the status word */
			lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
			if (pmbox->mbxOwner != OWN_HOST) {
				/*
				 * Stray Mailbox Interrupt, mbxCommand <cmd>
				 * mbxStatus <status>
				 */
4097
				lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
4098
						LOG_SLI,
4099
						"(%d):0304 Stray Mailbox "
4100 4101
						"Interrupt mbxCommand x%x "
						"mbxStatus x%x\n",
4102
						(vport ? vport->vpi : 0),
4103 4104
						pmbox->mbxCommand,
						pmbox->mbxStatus);
4105 4106 4107 4108 4109
				/* clear mailbox attention bit */
				work_ha_copy &= ~HA_MBATT;
			} else {
				phba->last_completion_time = jiffies;
				del_timer(&phba->sli.mbox_tmo);
J
James Smart 已提交
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
				phba->sli.mbox_active = NULL;
				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;
						spin_lock(&phba->hbalock);
						phba->sli.sli_flag &=
							~LPFC_SLI_MBOX_ACTIVE;
						spin_unlock(&phba->hbalock);
						goto send_current_mbox;
					}
J
James Smart 已提交
4152
				}
4153 4154 4155 4156 4157
				spin_lock(&phba->pport->work_port_lock);
				phba->pport->work_port_events &=
					~WORKER_MBOX_TMO;
				spin_unlock(&phba->pport->work_port_lock);
				lpfc_mbox_cmpl_put(phba, pmb);
J
James Smart 已提交
4158
			}
4159 4160 4161 4162 4163 4164 4165 4166
		}
		if ((work_ha_copy & HA_MBATT) &&
		    (phba->sli.mbox_active == NULL)) {
send_next_mbox:
			spin_lock(&phba->hbalock);
			phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
			pmb = lpfc_mbox_get(phba);
			spin_unlock(&phba->hbalock);
J
James Smart 已提交
4167
send_current_mbox:
4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179
			/* Process next mailbox command if there is one */
			if (pmb != NULL) {
				rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
				if (rc == MBX_NOT_FINISHED) {
					pmb->mb.mbxStatus = MBX_NOT_FINISHED;
					lpfc_mbox_cmpl_put(phba, pmb);
					goto send_next_mbox;
				}
			}

		}

J
James Smart 已提交
4180
		spin_lock(&phba->hbalock);
已提交
4181 4182
		phba->work_ha |= work_ha_copy;
		if (phba->work_wait)
4183
			lpfc_worker_wake_up(phba);
J
James Smart 已提交
4184
		spin_unlock(&phba->hbalock);
已提交
4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195
	}

	ha_copy &= ~(phba->work_ha_mask);

	/*
	 * Process all events on FCP ring.  Take the optimized path for
	 * FCP IO.  Any other IO is slow path and is handled by
	 * the worker thread.
	 */
	status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
	status >>= (4*LPFC_FCP_RING);
J
James Smart 已提交
4196
	if (status & HA_RXMASK)
已提交
4197 4198 4199
		lpfc_sli_handle_fast_ring_event(phba,
						&phba->sli.ring[LPFC_FCP_RING],
						status);
4200 4201 4202 4203 4204 4205 4206 4207 4208

	if (phba->cfg_multi_ring_support == 2) {
		/*
		 * Process all events on extra ring.  Take the optimized path
		 * for extra ring IO.  Any other IO is slow path and is handled
		 * by the worker thread.
		 */
		status = (ha_copy & (HA_RXMASK  << (4*LPFC_EXTRA_RING)));
		status >>= (4*LPFC_EXTRA_RING);
J
James Smart 已提交
4209
		if (status & HA_RXMASK) {
4210 4211 4212 4213 4214
			lpfc_sli_handle_fast_ring_event(phba,
					&phba->sli.ring[LPFC_EXTRA_RING],
					status);
		}
	}
已提交
4215 4216 4217
	return IRQ_HANDLED;

} /* lpfc_intr_handler */