lpfc_bsg.c 162.7 KB
Newer Older
1 2 3
/*******************************************************************
 * This file is part of the Emulex Linux Device Driver for         *
 * Fibre Channel Host Bus Adapters.                                *
4
 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5
 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
J
James Smart 已提交
6
 * Copyright (C) 2009-2015 Emulex.  All rights reserved.           *
7
 * EMULEX and SLI are trademarks of Emulex.                        *
J
James Smart 已提交
8
 * www.broadcom.com                                                *
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *                                                                 *
 * This program is free software; you can redistribute it and/or   *
 * 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.                                     *
 *******************************************************************/

#include <linux/interrupt.h>
#include <linux/mempool.h>
#include <linux/pci.h>
26
#include <linux/slab.h>
27
#include <linux/delay.h>
28
#include <linux/list.h>
29
#include <linux/bsg-lib.h>
30
#include <linux/vmalloc.h>
31 32 33 34 35

#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_transport_fc.h>
#include <scsi/scsi_bsg_fc.h>
36
#include <scsi/fc/fc_fs.h>
37 38 39 40 41 42

#include "lpfc_hw4.h"
#include "lpfc_hw.h"
#include "lpfc_sli.h"
#include "lpfc_sli4.h"
#include "lpfc_nl.h"
43
#include "lpfc_bsg.h"
44 45 46 47 48
#include "lpfc_disc.h"
#include "lpfc_scsi.h"
#include "lpfc.h"
#include "lpfc_logmsg.h"
#include "lpfc_crtn.h"
49
#include "lpfc_debugfs.h"
50 51 52
#include "lpfc_vport.h"
#include "lpfc_version.h"

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
struct lpfc_bsg_event {
	struct list_head node;
	struct kref kref;
	wait_queue_head_t wq;

	/* Event type and waiter identifiers */
	uint32_t type_mask;
	uint32_t req_id;
	uint32_t reg_id;

	/* next two flags are here for the auto-delete logic */
	unsigned long wait_time_stamp;
	int waiting;

	/* seen and not seen events */
	struct list_head events_to_get;
	struct list_head events_to_see;

71 72
	/* driver data associated with the job */
	void *dd_data;
73 74 75 76
};

struct lpfc_bsg_iocb {
	struct lpfc_iocbq *cmdiocbq;
77
	struct lpfc_dmabuf *rmp;
78 79 80
	struct lpfc_nodelist *ndlp;
};

81 82 83
struct lpfc_bsg_mbox {
	LPFC_MBOXQ_t *pmboxq;
	MAILBOX_t *mb;
84
	struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
85 86 87
	uint8_t *ext; /* extended mailbox data */
	uint32_t mbOffset; /* from app */
	uint32_t inExtWLen; /* from app */
88
	uint32_t outExtWLen; /* from app */
89 90
};

91 92 93 94
#define MENLO_DID 0x0000FC0E

struct lpfc_bsg_menlo {
	struct lpfc_iocbq *cmdiocbq;
95
	struct lpfc_dmabuf *rmp;
96 97
};

98 99
#define TYPE_EVT 	1
#define TYPE_IOCB	2
100
#define TYPE_MBOX	3
101
#define TYPE_MENLO	4
102 103
struct bsg_job_data {
	uint32_t type;
104
	struct bsg_job *set_job; /* job waiting for this iocb to finish */
105 106 107
	union {
		struct lpfc_bsg_event *evt;
		struct lpfc_bsg_iocb iocb;
108
		struct lpfc_bsg_mbox mbox;
109
		struct lpfc_bsg_menlo menlo;
110 111 112 113 114 115 116 117 118 119 120
	} context_un;
};

struct event_data {
	struct list_head node;
	uint32_t type;
	uint32_t immed_dat;
	void *data;
	uint32_t len;
};

121
#define BUF_SZ_4K 4096
122 123 124 125 126 127 128
#define SLI_CT_ELX_LOOPBACK 0x10

enum ELX_LOOPBACK_CMD {
	ELX_LOOPBACK_XRI_SETUP,
	ELX_LOOPBACK_DATA,
};

129 130 131
#define ELX_LOOPBACK_HEADER_SZ \
	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)

132 133 134 135 136 137
struct lpfc_dmabufext {
	struct lpfc_dmabuf dma;
	uint32_t size;
	uint32_t flag;
};

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static void
lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
{
	struct lpfc_dmabuf *mlast, *next_mlast;

	if (mlist) {
		list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
					 list) {
			lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
			list_del(&mlast->list);
			kfree(mlast);
		}
		lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
		kfree(mlist);
	}
	return;
}

static struct lpfc_dmabuf *
lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
		       int outbound_buffers, struct ulp_bde64 *bpl,
		       int *bpl_entries)
{
	struct lpfc_dmabuf *mlist = NULL;
	struct lpfc_dmabuf *mp;
	unsigned int bytes_left = size;

	/* Verify we can support the size specified */
	if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
		return NULL;

	/* Determine the number of dma buffers to allocate */
	*bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
			size/LPFC_BPL_SIZE);

	/* Allocate dma buffer and place in BPL passed */
	while (bytes_left) {
		/* Allocate dma buffer  */
		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
		if (!mp) {
			if (mlist)
				lpfc_free_bsg_buffers(phba, mlist);
			return NULL;
		}

		INIT_LIST_HEAD(&mp->list);
		mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));

		if (!mp->virt) {
			kfree(mp);
			if (mlist)
				lpfc_free_bsg_buffers(phba, mlist);
			return NULL;
		}

		/* Queue it to a linked list */
		if (!mlist)
			mlist = mp;
		else
			list_add_tail(&mp->list, &mlist->list);

		/* Add buffer to buffer pointer list */
		if (outbound_buffers)
			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
		else
			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
		bpl->tus.f.bdeSize = (uint16_t)
			(bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
			 bytes_left);
		bytes_left -= bpl->tus.f.bdeSize;
		bpl->tus.w = le32_to_cpu(bpl->tus.w);
		bpl++;
	}
	return mlist;
}

static unsigned int
lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
218
		   struct bsg_buffer *bsg_buffers,
219 220 221 222 223 224 225 226
		   unsigned int bytes_to_transfer, int to_buffers)
{

	struct lpfc_dmabuf *mp;
	unsigned int transfer_bytes, bytes_copied = 0;
	unsigned int sg_offset, dma_offset;
	unsigned char *dma_address, *sg_address;
	LIST_HEAD(temp_list);
227 228 229 230
	struct sg_mapping_iter miter;
	unsigned long flags;
	unsigned int sg_flags = SG_MITER_ATOMIC;
	bool sg_valid;
231 232 233 234

	list_splice_init(&dma_buffers->list, &temp_list);
	list_add(&dma_buffers->list, &temp_list);
	sg_offset = 0;
235 236 237 238 239 240 241 242
	if (to_buffers)
		sg_flags |= SG_MITER_FROM_SG;
	else
		sg_flags |= SG_MITER_TO_SG;
	sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
		       sg_flags);
	local_irq_save(flags);
	sg_valid = sg_miter_next(&miter);
243 244
	list_for_each_entry(mp, &temp_list, list) {
		dma_offset = 0;
245
		while (bytes_to_transfer && sg_valid &&
246 247 248 249
		       (dma_offset < LPFC_BPL_SIZE)) {
			dma_address = mp->virt + dma_offset;
			if (sg_offset) {
				/* Continue previous partial transfer of sg */
250 251
				sg_address = miter.addr + sg_offset;
				transfer_bytes = miter.length - sg_offset;
252
			} else {
253 254
				sg_address = miter.addr;
				transfer_bytes = miter.length;
255 256 257 258 259 260 261 262 263 264 265 266 267
			}
			if (bytes_to_transfer < transfer_bytes)
				transfer_bytes = bytes_to_transfer;
			if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
				transfer_bytes = LPFC_BPL_SIZE - dma_offset;
			if (to_buffers)
				memcpy(dma_address, sg_address, transfer_bytes);
			else
				memcpy(sg_address, dma_address, transfer_bytes);
			dma_offset += transfer_bytes;
			sg_offset += transfer_bytes;
			bytes_to_transfer -= transfer_bytes;
			bytes_copied += transfer_bytes;
268
			if (sg_offset >= miter.length) {
269
				sg_offset = 0;
270
				sg_valid = sg_miter_next(&miter);
271 272 273
			}
		}
	}
274 275
	sg_miter_stop(&miter);
	local_irq_restore(flags);
276 277 278 279 280
	list_del_init(&dma_buffers->list);
	list_splice(&temp_list, &dma_buffers->list);
	return bytes_copied;
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
/**
 * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
 * @phba: Pointer to HBA context object.
 * @cmdiocbq: Pointer to command iocb.
 * @rspiocbq: Pointer to response iocb.
 *
 * This function is the completion handler for iocbs issued using
 * lpfc_bsg_send_mgmt_cmd function. This function is called by the
 * ring event handler function without any lock held. This function
 * can be called from both worker thread context and interrupt
 * context. This function also can be called from another thread which
 * cleans up the SLI layer objects.
 * This function copies the contents of the response iocb to the
 * response iocb memory object provided by the caller of
 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
 * sleeps for the iocb completion.
 **/
static void
lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
{
	struct bsg_job_data *dd_data;
304
	struct bsg_job *job;
305
	struct fc_bsg_reply *bsg_reply;
306
	IOCB_t *rsp;
307
	struct lpfc_dmabuf *bmp, *cmp, *rmp;
308 309 310
	struct lpfc_nodelist *ndlp;
	struct lpfc_bsg_iocb *iocb;
	unsigned long flags;
311
	unsigned int rsp_size;
312 313
	int rc = 0;

314 315 316
	dd_data = cmdiocbq->context1;

	/* Determine if job has been aborted */
317
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
318 319
	job = dd_data->set_job;
	if (job) {
320
		bsg_reply = job->reply;
321 322
		/* Prevent timeout handling from trying to abort job */
		job->dd_data = NULL;
323
	}
324
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
325

326 327
	/* Close the timeout handler abort window */
	spin_lock_irqsave(&phba->hbalock, flags);
328
	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
329 330
	spin_unlock_irqrestore(&phba->hbalock, flags);

331
	iocb = &dd_data->context_un.iocb;
332 333 334 335
	ndlp = iocb->ndlp;
	rmp = iocb->rmp;
	cmp = cmdiocbq->context2;
	bmp = cmdiocbq->context3;
336 337
	rsp = &rspiocbq->iocb;

338
	/* Copy the completed data or set the error status */
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
	if (job) {
		if (rsp->ulpStatus) {
			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
				case IOERR_SEQUENCE_TIMEOUT:
					rc = -ETIMEDOUT;
					break;
				case IOERR_INVALID_RPI:
					rc = -EFAULT;
					break;
				default:
					rc = -EACCES;
					break;
				}
			} else {
355 356
				rc = -EACCES;
			}
357 358
		} else {
			rsp_size = rsp->un.genreq64.bdl.bdeSize;
359
			bsg_reply->reply_payload_rcv_len =
360 361 362 363
				lpfc_bsg_copy_data(rmp, &job->reply_payload,
						   rsp_size, 0);
		}
	}
364

365 366
	lpfc_free_bsg_buffers(phba, cmp);
	lpfc_free_bsg_buffers(phba, rmp);
367
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
368
	kfree(bmp);
369 370 371
	lpfc_sli_release_iocbq(phba, cmdiocbq);
	lpfc_nlp_put(ndlp);
	kfree(dd_data);
372 373 374 375

	/* Complete the job if the job is still active */

	if (job) {
376
		bsg_reply->result = rc;
J
Johannes Thumshirn 已提交
377
		bsg_job_done(job, bsg_reply->result,
378
			       bsg_reply->reply_payload_rcv_len);
379
	}
380 381 382
	return;
}

383
/**
384
 * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
385
 * @job: fc_bsg_job to handle
386
 **/
387
static int
388
lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
389
{
390
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
391
	struct lpfc_hba *phba = vport->phba;
392
	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
393
	struct lpfc_nodelist *ndlp = rdata->pnode;
394
	struct fc_bsg_reply *bsg_reply = job->reply;
395 396 397 398
	struct ulp_bde64 *bpl = NULL;
	uint32_t timeout;
	struct lpfc_iocbq *cmdiocbq = NULL;
	IOCB_t *cmd;
399
	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
400 401
	int request_nseg;
	int reply_nseg;
402
	struct bsg_job_data *dd_data;
403
	unsigned long flags;
404
	uint32_t creg_val;
405
	int rc = 0;
406
	int iocb_stat;
407 408

	/* in case no data is transferred */
409
	bsg_reply->reply_payload_rcv_len = 0;
410

411 412 413 414 415 416 417 418 419
	/* allocate our bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2733 Failed allocation of dd_data\n");
		rc = -ENOMEM;
		goto no_dd_data;
	}

420
	if (!lpfc_nlp_get(ndlp)) {
421 422 423 424
		rc = -ENODEV;
		goto no_ndlp;
	}

425 426
	if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
		rc = -ENODEV;
427
		goto free_ndlp;
428 429 430 431 432
	}

	cmdiocbq = lpfc_sli_get_iocbq(phba);
	if (!cmdiocbq) {
		rc = -ENOMEM;
433
		goto free_ndlp;
434 435
	}

436
	cmd = &cmdiocbq->iocb;
437 438 439 440 441 442

	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!bmp) {
		rc = -ENOMEM;
		goto free_cmdiocbq;
	}
443 444 445
	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
	if (!bmp->virt) {
		rc = -ENOMEM;
446
		goto free_bmp;
447 448 449
	}

	INIT_LIST_HEAD(&bmp->list);
450

451
	bpl = (struct ulp_bde64 *) bmp->virt;
452 453 454 455 456 457
	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
				     1, bpl, &request_nseg);
	if (!cmp) {
		rc = -ENOMEM;
		goto free_bmp;
458
	}
459 460
	lpfc_bsg_copy_data(cmp, &job->request_payload,
			   job->request_payload.payload_len, 1);
461

462 463 464 465 466 467 468
	bpl += request_nseg;
	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
				     bpl, &reply_nseg);
	if (!rmp) {
		rc = -ENOMEM;
		goto free_cmp;
469 470 471 472 473 474 475 476 477 478 479
	}

	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
	cmd->un.genreq64.bdl.bdeSize =
		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
480 481
	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
482 483 484 485
	cmd->ulpBdeCount = 1;
	cmd->ulpLe = 1;
	cmd->ulpClass = CLASS3;
	cmd->ulpContext = ndlp->nlp_rpi;
486 487
	if (phba->sli_rev == LPFC_SLI_REV4)
		cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
488 489
	cmd->ulpOwner = OWN_CHIP;
	cmdiocbq->vport = phba->pport;
490
	cmdiocbq->context3 = bmp;
491 492
	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
	timeout = phba->fc_ratov * 2;
493 494 495
	cmd->ulpTimeout = timeout;

	cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
496 497 498
	cmdiocbq->context1 = dd_data;
	cmdiocbq->context2 = cmp;
	cmdiocbq->context3 = bmp;
499
	cmdiocbq->context_un.ndlp = ndlp;
500
	dd_data->type = TYPE_IOCB;
501
	dd_data->set_job = job;
502
	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
503 504 505
	dd_data->context_un.iocb.ndlp = ndlp;
	dd_data->context_un.iocb.rmp = rmp;
	job->dd_data = dd_data;
506 507

	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
508 509
		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
			rc = -EIO ;
510
			goto free_rmp;
511
		}
512 513 514
		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
		writel(creg_val, phba->HCregaddr);
		readl(phba->HCregaddr); /* flush */
515 516
	}

517
	iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
518 519 520 521 522 523

	if (iocb_stat == IOCB_SUCCESS) {
		spin_lock_irqsave(&phba->hbalock, flags);
		/* make sure the I/O had not been completed yet */
		if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
			/* open up abort window to timeout handler */
524
			cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
525 526
		}
		spin_unlock_irqrestore(&phba->hbalock, flags);
527
		return 0; /* done for now */
528
	} else if (iocb_stat == IOCB_BUSY) {
529
		rc = -EAGAIN;
530
	} else {
531
		rc = -EIO;
532
	}
533

534
	/* iocb failed so cleanup */
535
	job->dd_data = NULL;
536

537 538 539 540
free_rmp:
	lpfc_free_bsg_buffers(phba, rmp);
free_cmp:
	lpfc_free_bsg_buffers(phba, cmp);
541
free_bmp:
542 543
	if (bmp->virt)
		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
544
	kfree(bmp);
545 546
free_cmdiocbq:
	lpfc_sli_release_iocbq(phba, cmdiocbq);
547
free_ndlp:
548
	lpfc_nlp_put(ndlp);
549 550 551 552
no_ndlp:
	kfree(dd_data);
no_dd_data:
	/* make error code available to userspace */
553
	bsg_reply->result = rc;
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
	job->dd_data = NULL;
	return rc;
}

/**
 * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
 * @phba: Pointer to HBA context object.
 * @cmdiocbq: Pointer to command iocb.
 * @rspiocbq: Pointer to response iocb.
 *
 * This function is the completion handler for iocbs issued using
 * lpfc_bsg_rport_els_cmp function. This function is called by the
 * ring event handler function without any lock held. This function
 * can be called from both worker thread context and interrupt
 * context. This function also can be called from other thread which
 * cleans up the SLI layer objects.
570
 * This function copies the contents of the response iocb to the
571 572 573 574 575 576 577 578 579 580
 * response iocb memory object provided by the caller of
 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
 * sleeps for the iocb completion.
 **/
static void
lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
{
	struct bsg_job_data *dd_data;
581
	struct bsg_job *job;
582
	struct fc_bsg_reply *bsg_reply;
583 584
	IOCB_t *rsp;
	struct lpfc_nodelist *ndlp;
585
	struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
586 587 588
	struct fc_bsg_ctels_reply *els_reply;
	uint8_t *rjt_data;
	unsigned long flags;
589
	unsigned int rsp_size;
590 591 592
	int rc = 0;

	dd_data = cmdiocbq->context1;
593 594
	ndlp = dd_data->context_un.iocb.ndlp;
	cmdiocbq->context1 = ndlp;
595

596 597 598 599
	/* Determine if job has been aborted */
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	job = dd_data->set_job;
	if (job) {
600
		bsg_reply = job->reply;
601 602 603 604
		/* Prevent timeout handling from trying to abort job  */
		job->dd_data = NULL;
	}
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
605

606 607
	/* Close the timeout handler abort window */
	spin_lock_irqsave(&phba->hbalock, flags);
608
	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
609 610
	spin_unlock_irqrestore(&phba->hbalock, flags);

611
	rsp = &rspiocbq->iocb;
612 613
	pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
	prsp = (struct lpfc_dmabuf *)pcmd->list.next;
614

615 616 617
	/* Copy the completed job data or determine the job status if job is
	 * still active
	 */
618

619 620 621
	if (job) {
		if (rsp->ulpStatus == IOSTAT_SUCCESS) {
			rsp_size = rsp->un.elsreq64.bdl.bdeSize;
622
			bsg_reply->reply_payload_rcv_len =
623 624 625 626 627
				sg_copy_from_buffer(job->reply_payload.sg_list,
						    job->reply_payload.sg_cnt,
						    prsp->virt,
						    rsp_size);
		} else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
628
			bsg_reply->reply_payload_rcv_len =
629 630 631
				sizeof(struct fc_bsg_ctels_reply);
			/* LS_RJT data returned in word 4 */
			rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
632
			els_reply = &bsg_reply->reply_data.ctels_reply;
633 634 635 636 637 638 639 640 641
			els_reply->status = FC_CTELS_STATUS_REJECT;
			els_reply->rjt_data.action = rjt_data[3];
			els_reply->rjt_data.reason_code = rjt_data[2];
			els_reply->rjt_data.reason_explanation = rjt_data[1];
			els_reply->rjt_data.vendor_unique = rjt_data[0];
		} else {
			rc = -EIO;
		}
	}
642

643
	lpfc_nlp_put(ndlp);
644
	lpfc_els_free_iocb(phba, cmdiocbq);
645
	kfree(dd_data);
646 647 648 649

	/* Complete the job if the job is still active */

	if (job) {
650
		bsg_reply->result = rc;
J
Johannes Thumshirn 已提交
651
		bsg_job_done(job, bsg_reply->result,
652
			       bsg_reply->reply_payload_rcv_len);
653
	}
654
	return;
655 656 657 658 659
}

/**
 * lpfc_bsg_rport_els - send an ELS command from a bsg request
 * @job: fc_bsg_job to handle
660
 **/
661
static int
662
lpfc_bsg_rport_els(struct bsg_job *job)
663
{
664
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
665
	struct lpfc_hba *phba = vport->phba;
666
	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
667
	struct lpfc_nodelist *ndlp = rdata->pnode;
668 669
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
670 671 672 673
	uint32_t elscmd;
	uint32_t cmdsize;
	struct lpfc_iocbq *cmdiocbq;
	uint16_t rpi = 0;
674
	struct bsg_job_data *dd_data;
675
	unsigned long flags;
676
	uint32_t creg_val;
677 678 679
	int rc = 0;

	/* in case no data is transferred */
680
	bsg_reply->reply_payload_rcv_len = 0;
681

682 683 684 685 686 687 688 689 690
	/* verify the els command is not greater than the
	 * maximum ELS transfer size.
	 */

	if (job->request_payload.payload_len > FCELSSIZE) {
		rc = -EINVAL;
		goto no_dd_data;
	}

691 692 693 694 695 696 697 698 699
	/* allocate our bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2735 Failed allocation of dd_data\n");
		rc = -ENOMEM;
		goto no_dd_data;
	}

700
	elscmd = bsg_request->rqst_data.r_els.els_code;
701
	cmdsize = job->request_payload.payload_len;
702 703 704

	if (!lpfc_nlp_get(ndlp)) {
		rc = -ENODEV;
705
		goto free_dd_data;
706 707
	}

708 709 710 711 712
	/* We will use the allocated dma buffers by prep els iocb for command
	 * and response to ensure if the job times out and the request is freed,
	 * we won't be dma into memory that is no longer allocated to for the
	 * request.
	 */
713

714
	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
715 716
				      ndlp->nlp_DID, elscmd);
	if (!cmdiocbq) {
717
		rc = -EIO;
718
		goto release_ndlp;
719 720
	}

721 722 723 724 725 726 727 728
	rpi = ndlp->nlp_rpi;

	/* Transfer the request payload to allocated command dma buffer */

	sg_copy_to_buffer(job->request_payload.sg_list,
			  job->request_payload.sg_cnt,
			  ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
			  cmdsize);
729

730 731 732 733
	if (phba->sli_rev == LPFC_SLI_REV4)
		cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
	else
		cmdiocbq->iocb.ulpContext = rpi;
734
	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
735
	cmdiocbq->context1 = dd_data;
736
	cmdiocbq->context_un.ndlp = ndlp;
737
	cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
738
	dd_data->type = TYPE_IOCB;
739
	dd_data->set_job = job;
740 741
	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
	dd_data->context_un.iocb.ndlp = ndlp;
742 743
	dd_data->context_un.iocb.rmp = NULL;
	job->dd_data = dd_data;
744 745

	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
746 747 748 749
		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
			rc = -EIO;
			goto linkdown_err;
		}
750 751 752 753
		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
		writel(creg_val, phba->HCregaddr);
		readl(phba->HCregaddr); /* flush */
	}
754

755
	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
756

757 758 759 760 761
	if (rc == IOCB_SUCCESS) {
		spin_lock_irqsave(&phba->hbalock, flags);
		/* make sure the I/O had not been completed/released */
		if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
			/* open up abort window to timeout handler */
762
			cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
763 764
		}
		spin_unlock_irqrestore(&phba->hbalock, flags);
765
		return 0; /* done for now */
766
	} else if (rc == IOCB_BUSY) {
767
		rc = -EAGAIN;
768
	} else {
769
		rc = -EIO;
770
	}
771

772 773
	/* iocb failed so cleanup */
	job->dd_data = NULL;
774

775
linkdown_err:
776 777
	cmdiocbq->context1 = ndlp;
	lpfc_els_free_iocb(phba, cmdiocbq);
778

779 780
release_ndlp:
	lpfc_nlp_put(ndlp);
781

782 783
free_dd_data:
	kfree(dd_data);
784

785 786
no_dd_data:
	/* make error code available to userspace */
787
	bsg_reply->result = rc;
788 789
	job->dd_data = NULL;
	return rc;
790 791
}

792 793 794 795 796 797 798 799
/**
 * lpfc_bsg_event_free - frees an allocated event structure
 * @kref: Pointer to a kref.
 *
 * Called from kref_put. Back cast the kref into an event structure address.
 * Free any events to get, delete associated nodes, free any events to see,
 * free any data then free the event itself.
 **/
800
static void
801
lpfc_bsg_event_free(struct kref *kref)
802
{
803 804
	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
						  kref);
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
	struct event_data *ed;

	list_del(&evt->node);

	while (!list_empty(&evt->events_to_get)) {
		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
		list_del(&ed->node);
		kfree(ed->data);
		kfree(ed);
	}

	while (!list_empty(&evt->events_to_see)) {
		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
		list_del(&ed->node);
		kfree(ed->data);
		kfree(ed);
	}

823
	kfree(evt->dd_data);
824 825 826
	kfree(evt);
}

827 828 829 830
/**
 * lpfc_bsg_event_ref - increments the kref for an event
 * @evt: Pointer to an event structure.
 **/
831
static inline void
832
lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
833
{
834
	kref_get(&evt->kref);
835 836
}

837 838 839 840
/**
 * lpfc_bsg_event_unref - Uses kref_put to free an event structure
 * @evt: Pointer to an event structure.
 **/
841
static inline void
842
lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
843
{
844
	kref_put(&evt->kref, lpfc_bsg_event_free);
845 846
}

847 848 849 850 851 852
/**
 * lpfc_bsg_event_new - allocate and initialize a event structure
 * @ev_mask: Mask of events.
 * @ev_reg_id: Event reg id.
 * @ev_req_id: Event request id.
 **/
853 854 855 856
static struct lpfc_bsg_event *
lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
{
	struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
857

858 859 860 861 862 863 864 865 866
	if (!evt)
		return NULL;

	INIT_LIST_HEAD(&evt->events_to_get);
	INIT_LIST_HEAD(&evt->events_to_see);
	evt->type_mask = ev_mask;
	evt->req_id = ev_req_id;
	evt->reg_id = ev_reg_id;
	evt->wait_time_stamp = jiffies;
867
	evt->dd_data = NULL;
868 869 870 871 872
	init_waitqueue_head(&evt->wq);
	kref_init(&evt->kref);
	return evt;
}

873 874 875 876 877
/**
 * diag_cmd_data_free - Frees an lpfc dma buffer extension
 * @phba: Pointer to HBA context object.
 * @mlist: Pointer to an lpfc dma buffer extension.
 **/
878
static int
879
diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
{
	struct lpfc_dmabufext *mlast;
	struct pci_dev *pcidev;
	struct list_head head, *curr, *next;

	if ((!mlist) || (!lpfc_is_link_up(phba) &&
		(phba->link_flag & LS_LOOPBACK_MODE))) {
		return 0;
	}

	pcidev = phba->pcidev;
	list_add_tail(&head, &mlist->dma.list);

	list_for_each_safe(curr, next, &head) {
		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
		if (mlast->dma.virt)
			dma_free_coherent(&pcidev->dev,
					  mlast->size,
					  mlast->dma.virt,
					  mlast->dma.phys);
		kfree(mlast);
	}
	return 0;
}
904 905 906 907 908 909 910 911

/**
 * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
 * @phba:
 * @pring:
 * @piocbq:
 *
 * This function is called when an unsolicited CT command is received.  It
912
 * forwards the event to any processes registered to receive CT events.
913
 **/
914
int
915 916 917 918 919 920
lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
			struct lpfc_iocbq *piocbq)
{
	uint32_t evt_req_id = 0;
	uint32_t cmd;
	struct lpfc_dmabuf *dmabuf = NULL;
921
	struct lpfc_bsg_event *evt;
922 923 924 925 926 927 928 929 930 931 932
	struct event_data *evt_dat = NULL;
	struct lpfc_iocbq *iocbq;
	size_t offset = 0;
	struct list_head head;
	struct ulp_bde64 *bde;
	dma_addr_t dma_addr;
	int i;
	struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
	struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
	struct lpfc_hbq_entry *hbqe;
	struct lpfc_sli_ct_request *ct_req;
933
	struct bsg_job *job = NULL;
934
	struct fc_bsg_reply *bsg_reply;
935
	struct bsg_job_data *dd_data = NULL;
936
	unsigned long flags;
937
	int size = 0;
938 939 940 941 942 943 944 945

	INIT_LIST_HEAD(&head);
	list_add_tail(&head, &piocbq->list);

	if (piocbq->iocb.ulpBdeCount == 0 ||
	    piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
		goto error_ct_unsol_exit;

946 947 948 949
	if (phba->link_state == LPFC_HBA_ERROR ||
		(!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
		goto error_ct_unsol_exit;

950 951 952 953 954 955 956
	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
		dmabuf = bdeBuf1;
	else {
		dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
				    piocbq->iocb.un.cont64[0].addrLow);
		dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
	}
957 958
	if (dmabuf == NULL)
		goto error_ct_unsol_exit;
959 960 961 962 963 964
	ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
	evt_req_id = ct_req->FsType;
	cmd = ct_req->CommandResponse.bits.CmdRsp;
	if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
		lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);

965
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
966
	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
967 968
		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
			evt->req_id != evt_req_id)
969 970
			continue;

971 972
		lpfc_bsg_event_ref(evt);
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
973
		evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
974 975 976
		if (evt_dat == NULL) {
			spin_lock_irqsave(&phba->ct_ev_lock, flags);
			lpfc_bsg_event_unref(evt);
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
					"2614 Memory allocation failed for "
					"CT event\n");
			break;
		}

		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
			/* take accumulated byte count from the last iocbq */
			iocbq = list_entry(head.prev, typeof(*iocbq), list);
			evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
		} else {
			list_for_each_entry(iocbq, &head, list) {
				for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
					evt_dat->len +=
					iocbq->iocb.un.cont64[i].tus.f.bdeSize;
			}
		}

		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
996
		if (evt_dat->data == NULL) {
997 998 999 1000 1001
			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
					"2615 Memory allocation failed for "
					"CT event data, size %d\n",
					evt_dat->len);
			kfree(evt_dat);
1002
			spin_lock_irqsave(&phba->ct_ev_lock, flags);
1003
			lpfc_bsg_event_unref(evt);
1004
			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1005 1006 1007 1008
			goto error_ct_unsol_exit;
		}

		list_for_each_entry(iocbq, &head, list) {
1009
			size = 0;
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 1045 1046
			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
				bdeBuf1 = iocbq->context2;
				bdeBuf2 = iocbq->context3;
			}
			for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
				if (phba->sli3_options &
				    LPFC_SLI3_HBQ_ENABLED) {
					if (i == 0) {
						hbqe = (struct lpfc_hbq_entry *)
						  &iocbq->iocb.un.ulpWord[0];
						size = hbqe->bde.tus.f.bdeSize;
						dmabuf = bdeBuf1;
					} else if (i == 1) {
						hbqe = (struct lpfc_hbq_entry *)
							&iocbq->iocb.unsli3.
							sli3Words[4];
						size = hbqe->bde.tus.f.bdeSize;
						dmabuf = bdeBuf2;
					}
					if ((offset + size) > evt_dat->len)
						size = evt_dat->len - offset;
				} else {
					size = iocbq->iocb.un.cont64[i].
						tus.f.bdeSize;
					bde = &iocbq->iocb.un.cont64[i];
					dma_addr = getPaddr(bde->addrHigh,
							    bde->addrLow);
					dmabuf = lpfc_sli_ringpostbuf_get(phba,
							pring, dma_addr);
				}
				if (!dmabuf) {
					lpfc_printf_log(phba, KERN_ERR,
						LOG_LIBDFC, "2616 No dmabuf "
						"found for iocbq 0x%p\n",
						iocbq);
					kfree(evt_dat->data);
					kfree(evt_dat);
1047 1048
					spin_lock_irqsave(&phba->ct_ev_lock,
						flags);
1049
					lpfc_bsg_event_unref(evt);
1050 1051
					spin_unlock_irqrestore(
						&phba->ct_ev_lock, flags);
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
					goto error_ct_unsol_exit;
				}
				memcpy((char *)(evt_dat->data) + offset,
				       dmabuf->virt, size);
				offset += size;
				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
				    !(phba->sli3_options &
				      LPFC_SLI3_HBQ_ENABLED)) {
					lpfc_sli_ringpostbuf_put(phba, pring,
								 dmabuf);
				} else {
					switch (cmd) {
1064
					case ELX_LOOPBACK_DATA:
1065 1066 1067 1068 1069
						if (phba->sli_rev <
						    LPFC_SLI_REV4)
							diag_cmd_data_free(phba,
							(struct lpfc_dmabufext
							 *)dmabuf);
1070
						break;
1071
					case ELX_LOOPBACK_XRI_SETUP:
1072 1073 1074 1075 1076 1077 1078 1079
						if ((phba->sli_rev ==
							LPFC_SLI_REV2) ||
							(phba->sli3_options &
							LPFC_SLI3_HBQ_ENABLED
							)) {
							lpfc_in_buf_free(phba,
									dmabuf);
						} else {
1080 1081 1082
							lpfc_post_buffer(phba,
									 pring,
									 1);
1083
						}
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
						break;
					default:
						if (!(phba->sli3_options &
						      LPFC_SLI3_HBQ_ENABLED))
							lpfc_post_buffer(phba,
									 pring,
									 1);
						break;
					}
				}
			}
		}

1097
		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1098 1099
		if (phba->sli_rev == LPFC_SLI_REV4) {
			evt_dat->immed_dat = phba->ctx_idx;
1100
			phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1101
			/* Provide warning for over-run of the ct_ctx array */
1102
			if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1103 1104 1105 1106 1107 1108 1109 1110 1111
			    UNSOL_VALID)
				lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
						"2717 CT context array entry "
						"[%d] over-run: oxid:x%x, "
						"sid:x%x\n", phba->ctx_idx,
						phba->ct_ctx[
						    evt_dat->immed_dat].oxid,
						phba->ct_ctx[
						    evt_dat->immed_dat].SID);
1112 1113
			phba->ct_ctx[evt_dat->immed_dat].rxid =
				piocbq->iocb.ulpContext;
1114
			phba->ct_ctx[evt_dat->immed_dat].oxid =
1115
				piocbq->iocb.unsli3.rcvsli3.ox_id;
1116 1117
			phba->ct_ctx[evt_dat->immed_dat].SID =
				piocbq->iocb.un.rcvels.remoteID;
1118
			phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1119 1120 1121 1122 1123
		} else
			evt_dat->immed_dat = piocbq->iocb.ulpContext;

		evt_dat->type = FC_REG_CT_EVENT;
		list_add(&evt_dat->node, &evt->events_to_see);
1124 1125 1126
		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
			wake_up_interruptible(&evt->wq);
			lpfc_bsg_event_unref(evt);
1127
			break;
1128 1129 1130 1131
		}

		list_move(evt->events_to_see.prev, &evt->events_to_get);

1132 1133 1134 1135
		dd_data = (struct bsg_job_data *)evt->dd_data;
		job = dd_data->set_job;
		dd_data->set_job = NULL;
		lpfc_bsg_event_unref(evt);
1136
		if (job) {
1137 1138
			bsg_reply = job->reply;
			bsg_reply->reply_payload_rcv_len = size;
1139
			/* make error code available to userspace */
1140
			bsg_reply->result = 0;
1141 1142 1143
			job->dd_data = NULL;
			/* complete the job back to userspace */
			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
J
Johannes Thumshirn 已提交
1144
			bsg_job_done(job, bsg_reply->result,
1145
				       bsg_reply->reply_payload_rcv_len);
1146 1147
			spin_lock_irqsave(&phba->ct_ev_lock, flags);
		}
1148
	}
1149
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1150 1151 1152 1153

error_ct_unsol_exit:
	if (!list_empty(&head))
		list_del(&head);
1154 1155
	if ((phba->sli_rev < LPFC_SLI_REV4) &&
	    (evt_req_id == SLI_CT_ELX_LOOPBACK))
1156
		return 0;
1157
	return 1;
1158 1159
}

1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
/**
 * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
 * @phba: Pointer to HBA context object.
 * @dmabuf: pointer to a dmabuf that describes the FC sequence
 *
 * This function handles abort to the CT command toward management plane
 * for SLI4 port.
 *
 * If the pending context of a CT command to management plane present, clears
 * such context and returns 1 for handled; otherwise, it returns 0 indicating
 * no context exists.
 **/
int
lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
{
	struct fc_frame_header fc_hdr;
	struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
	int ctx_idx, handled = 0;
	uint16_t oxid, rxid;
	uint32_t sid;

	memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
	sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
	oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
	rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);

	for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
		if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
			continue;
		if (phba->ct_ctx[ctx_idx].rxid != rxid)
			continue;
		if (phba->ct_ctx[ctx_idx].oxid != oxid)
			continue;
		if (phba->ct_ctx[ctx_idx].SID != sid)
			continue;
		phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
		handled = 1;
	}
	return handled;
}

1201
/**
1202
 * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1203
 * @job: SET_EVENT fc_bsg_job
1204
 **/
1205
static int
1206
lpfc_bsg_hba_set_event(struct bsg_job *job)
1207
{
1208
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1209
	struct lpfc_hba *phba = vport->phba;
1210
	struct fc_bsg_request *bsg_request = job->request;
1211
	struct set_ct_event *event_req;
1212
	struct lpfc_bsg_event *evt;
1213
	int rc = 0;
1214 1215 1216
	struct bsg_job_data *dd_data = NULL;
	uint32_t ev_mask;
	unsigned long flags;
1217 1218 1219 1220 1221 1222

	if (job->request_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2612 Received SET_CT_EVENT below minimum "
				"size\n");
1223 1224 1225 1226
		rc = -EINVAL;
		goto job_error;
	}

1227
	event_req = (struct set_ct_event *)
1228
		bsg_request->rqst_data.h_vendor.vendor_cmd;
1229 1230
	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
				FC_REG_EVENT_MASK);
1231
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1232 1233
	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
		if (evt->reg_id == event_req->ev_reg_id) {
1234
			lpfc_bsg_event_ref(evt);
1235
			evt->wait_time_stamp = jiffies;
1236
			dd_data = (struct bsg_job_data *)evt->dd_data;
1237 1238 1239
			break;
		}
	}
1240
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1241 1242 1243

	if (&evt->node == &phba->ct_ev_waiters) {
		/* no event waiting struct yet - first call */
1244 1245 1246 1247 1248 1249 1250
		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
		if (dd_data == NULL) {
			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
					"2734 Failed allocation of dd_data\n");
			rc = -ENOMEM;
			goto job_error;
		}
1251
		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1252 1253 1254 1255 1256
					event_req->ev_req_id);
		if (!evt) {
			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
					"2617 Failed allocation of event "
					"waiter\n");
1257 1258
			rc = -ENOMEM;
			goto job_error;
1259
		}
1260 1261 1262 1263
		dd_data->type = TYPE_EVT;
		dd_data->set_job = NULL;
		dd_data->context_un.evt = evt;
		evt->dd_data = (void *)dd_data;
1264
		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1265
		list_add(&evt->node, &phba->ct_ev_waiters);
1266 1267
		lpfc_bsg_event_ref(evt);
		evt->wait_time_stamp = jiffies;
1268
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1269 1270
	}

1271
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1272
	evt->waiting = 1;
1273
	dd_data->set_job = job; /* for unsolicited command */
1274
	job->dd_data = dd_data; /* for fc transport timeout callback*/
1275
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1276
	return 0; /* call job done later */
1277

1278 1279 1280
job_error:
	if (dd_data != NULL)
		kfree(dd_data);
1281

1282 1283
	job->dd_data = NULL;
	return rc;
1284 1285 1286
}

/**
1287
 * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1288
 * @job: GET_EVENT fc_bsg_job
1289
 **/
1290
static int
1291
lpfc_bsg_hba_get_event(struct bsg_job *job)
1292
{
1293
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1294
	struct lpfc_hba *phba = vport->phba;
1295 1296
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
1297 1298
	struct get_ct_event *event_req;
	struct get_ct_event_reply *event_reply;
1299
	struct lpfc_bsg_event *evt, *evt_next;
1300
	struct event_data *evt_dat = NULL;
1301
	unsigned long flags;
1302
	uint32_t rc = 0;
1303 1304 1305 1306 1307 1308

	if (job->request_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2613 Received GET_CT_EVENT request below "
				"minimum size\n");
1309 1310
		rc = -EINVAL;
		goto job_error;
1311 1312 1313
	}

	event_req = (struct get_ct_event *)
1314
		bsg_request->rqst_data.h_vendor.vendor_cmd;
1315 1316

	event_reply = (struct get_ct_event_reply *)
1317
		bsg_reply->reply_data.vendor_reply.vendor_rsp;
1318
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1319
	list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1320 1321 1322
		if (evt->reg_id == event_req->ev_reg_id) {
			if (list_empty(&evt->events_to_get))
				break;
1323
			lpfc_bsg_event_ref(evt);
1324 1325 1326 1327 1328 1329 1330
			evt->wait_time_stamp = jiffies;
			evt_dat = list_entry(evt->events_to_get.prev,
					     struct event_data, node);
			list_del(&evt_dat->node);
			break;
		}
	}
1331
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1332

1333 1334 1335 1336
	/* The app may continue to ask for event data until it gets
	 * an error indicating that there isn't anymore
	 */
	if (evt_dat == NULL) {
1337
		bsg_reply->reply_payload_rcv_len = 0;
1338
		rc = -ENOENT;
1339
		goto job_error;
1340 1341
	}

1342 1343 1344 1345 1346 1347
	if (evt_dat->len > job->request_payload.payload_len) {
		evt_dat->len = job->request_payload.payload_len;
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2618 Truncated event data at %d "
				"bytes\n",
				job->request_payload.payload_len);
1348 1349
	}

1350
	event_reply->type = evt_dat->type;
1351 1352
	event_reply->immed_data = evt_dat->immed_dat;
	if (evt_dat->len > 0)
1353
		bsg_reply->reply_payload_rcv_len =
1354 1355
			sg_copy_from_buffer(job->request_payload.sg_list,
					    job->request_payload.sg_cnt,
1356 1357
					    evt_dat->data, evt_dat->len);
	else
1358
		bsg_reply->reply_payload_rcv_len = 0;
1359

1360
	if (evt_dat) {
1361
		kfree(evt_dat->data);
1362 1363 1364
		kfree(evt_dat);
	}

1365
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1366
	lpfc_bsg_event_unref(evt);
1367
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1368
	job->dd_data = NULL;
1369
	bsg_reply->result = 0;
J
Johannes Thumshirn 已提交
1370
	bsg_job_done(job, bsg_reply->result,
1371
		       bsg_reply->reply_payload_rcv_len);
1372
	return 0;
1373

1374 1375
job_error:
	job->dd_data = NULL;
1376
	bsg_reply->result = rc;
1377 1378 1379 1380
	return rc;
}

/**
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
 * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
 * @phba: Pointer to HBA context object.
 * @cmdiocbq: Pointer to command iocb.
 * @rspiocbq: Pointer to response iocb.
 *
 * This function is the completion handler for iocbs issued using
 * lpfc_issue_ct_rsp_cmp function. This function is called by the
 * ring event handler function without any lock held. This function
 * can be called from both worker thread context and interrupt
 * context. This function also can be called from other thread which
 * cleans up the SLI layer objects.
 * This function copy the contents of the response iocb to the
 * response iocb memory object provided by the caller of
 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
 * sleeps for the iocb completion.
 **/
static void
lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
{
	struct bsg_job_data *dd_data;
1403
	struct bsg_job *job;
1404
	struct fc_bsg_reply *bsg_reply;
1405
	IOCB_t *rsp;
1406
	struct lpfc_dmabuf *bmp, *cmp;
1407 1408 1409 1410
	struct lpfc_nodelist *ndlp;
	unsigned long flags;
	int rc = 0;

1411 1412 1413
	dd_data = cmdiocbq->context1;

	/* Determine if job has been aborted */
1414
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1415 1416 1417 1418
	job = dd_data->set_job;
	if (job) {
		/* Prevent timeout handling from trying to abort job  */
		job->dd_data = NULL;
1419
	}
1420
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1421

1422 1423
	/* Close the timeout handler abort window */
	spin_lock_irqsave(&phba->hbalock, flags);
1424
	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1425 1426
	spin_unlock_irqrestore(&phba->hbalock, flags);

1427
	ndlp = dd_data->context_un.iocb.ndlp;
1428 1429 1430
	cmp = cmdiocbq->context2;
	bmp = cmdiocbq->context3;
	rsp = &rspiocbq->iocb;
1431

1432
	/* Copy the completed job data or set the error status */
1433

1434
	if (job) {
1435
		bsg_reply = job->reply;
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
		if (rsp->ulpStatus) {
			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
				case IOERR_SEQUENCE_TIMEOUT:
					rc = -ETIMEDOUT;
					break;
				case IOERR_INVALID_RPI:
					rc = -EFAULT;
					break;
				default:
					rc = -EACCES;
					break;
				}
			} else {
1450 1451
				rc = -EACCES;
			}
1452
		} else {
1453
			bsg_reply->reply_payload_rcv_len = 0;
1454 1455
		}
	}
1456

1457
	lpfc_free_bsg_buffers(phba, cmp);
1458
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1459
	kfree(bmp);
1460 1461 1462
	lpfc_sli_release_iocbq(phba, cmdiocbq);
	lpfc_nlp_put(ndlp);
	kfree(dd_data);
1463 1464 1465 1466

	/* Complete the job if the job is still active */

	if (job) {
1467
		bsg_reply->result = rc;
J
Johannes Thumshirn 已提交
1468
		bsg_job_done(job, bsg_reply->result,
1469
			       bsg_reply->reply_payload_rcv_len);
1470
	}
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
	return;
}

/**
 * lpfc_issue_ct_rsp - issue a ct response
 * @phba: Pointer to HBA context object.
 * @job: Pointer to the job object.
 * @tag: tag index value into the ports context exchange array.
 * @bmp: Pointer to a dma buffer descriptor.
 * @num_entry: Number of enties in the bde.
 **/
1482
static int
1483
lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1484 1485
		  struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
		  int num_entry)
1486
{
1487 1488 1489 1490 1491
	IOCB_t *icmd;
	struct lpfc_iocbq *ctiocb = NULL;
	int rc = 0;
	struct lpfc_nodelist *ndlp = NULL;
	struct bsg_job_data *dd_data;
1492
	unsigned long flags;
1493
	uint32_t creg_val;
1494

1495 1496 1497 1498 1499 1500 1501 1502
	/* allocate our bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2736 Failed allocation of dd_data\n");
		rc = -ENOMEM;
		goto no_dd_data;
	}
1503

1504 1505 1506
	/* Allocate buffer for  command iocb */
	ctiocb = lpfc_sli_get_iocbq(phba);
	if (!ctiocb) {
1507
		rc = -ENOMEM;
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
		goto no_ctiocb;
	}

	icmd = &ctiocb->iocb;
	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;

	/* Fill in rest of iocb */
	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
	icmd->ulpBdeCount = 1;
	icmd->ulpLe = 1;
	icmd->ulpClass = CLASS3;
	if (phba->sli_rev == LPFC_SLI_REV4) {
		/* Do not issue unsol response if oxid not marked as valid */
1529
		if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
1530 1531 1532
			rc = IOCB_ERROR;
			goto issue_ct_rsp_exit;
		}
1533 1534
		icmd->ulpContext = phba->ct_ctx[tag].rxid;
		icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
1535 1536 1537 1538 1539 1540 1541 1542 1543
		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
		if (!ndlp) {
			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
				 "2721 ndlp null for oxid %x SID %x\n",
					icmd->ulpContext,
					phba->ct_ctx[tag].SID);
			rc = IOCB_ERROR;
			goto issue_ct_rsp_exit;
		}
1544 1545 1546

		/* Check if the ndlp is active */
		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1547
			rc = IOCB_ERROR;
1548 1549 1550 1551 1552 1553 1554
			goto issue_ct_rsp_exit;
		}

		/* get a refernece count so the ndlp doesn't go away while
		 * we respond
		 */
		if (!lpfc_nlp_get(ndlp)) {
1555
			rc = IOCB_ERROR;
1556 1557 1558
			goto issue_ct_rsp_exit;
		}

1559
		icmd->un.ulpWord[3] =
1560 1561
				phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];

1562
		/* The exchange is done, mark the entry as invalid */
1563
		phba->ct_ctx[tag].valid = UNSOL_INVALID;
1564 1565 1566 1567 1568 1569 1570
	} else
		icmd->ulpContext = (ushort) tag;

	icmd->ulpTimeout = phba->fc_ratov * 2;

	/* Xmit CT response on exchange <xid> */
	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1571 1572
		"2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
		icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
1573 1574 1575 1576

	ctiocb->iocb_cmpl = NULL;
	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
	ctiocb->vport = phba->pport;
1577 1578
	ctiocb->context1 = dd_data;
	ctiocb->context2 = cmp;
1579
	ctiocb->context3 = bmp;
1580
	ctiocb->context_un.ndlp = ndlp;
1581
	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1582

1583
	dd_data->type = TYPE_IOCB;
1584
	dd_data->set_job = job;
1585 1586
	dd_data->context_un.iocb.cmdiocbq = ctiocb;
	dd_data->context_un.iocb.ndlp = ndlp;
1587 1588
	dd_data->context_un.iocb.rmp = NULL;
	job->dd_data = dd_data;
1589 1590

	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1591 1592 1593 1594
		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
			rc = -IOCB_ERROR;
			goto issue_ct_rsp_exit;
		}
1595 1596 1597
		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
		writel(creg_val, phba->HCregaddr);
		readl(phba->HCregaddr); /* flush */
1598
	}
1599

1600 1601
	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);

1602 1603 1604 1605 1606
	if (rc == IOCB_SUCCESS) {
		spin_lock_irqsave(&phba->hbalock, flags);
		/* make sure the I/O had not been completed/released */
		if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
			/* open up abort window to timeout handler */
1607
			ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
1608 1609
		}
		spin_unlock_irqrestore(&phba->hbalock, flags);
1610
		return 0; /* done for now */
1611 1612 1613 1614
	}

	/* iocb failed so cleanup */
	job->dd_data = NULL;
1615 1616 1617 1618 1619 1620

issue_ct_rsp_exit:
	lpfc_sli_release_iocbq(phba, ctiocb);
no_ctiocb:
	kfree(dd_data);
no_dd_data:
1621
	return rc;
1622 1623 1624
}

/**
1625 1626 1627 1628
 * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
 * @job: SEND_MGMT_RESP fc_bsg_job
 **/
static int
1629
lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
1630
{
1631
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1632
	struct lpfc_hba *phba = vport->phba;
1633 1634
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
1635
	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1636
		bsg_request->rqst_data.h_vendor.vendor_cmd;
1637
	struct ulp_bde64 *bpl;
1638 1639
	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
	int bpl_entries;
1640 1641 1642 1643
	uint32_t tag = mgmt_resp->tag;
	unsigned long reqbfrcnt =
			(unsigned long)job->request_payload.payload_len;
	int rc = 0;
1644

1645
	/* in case no data is transferred */
1646
	bsg_reply->reply_payload_rcv_len = 0;
1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666

	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
		rc = -ERANGE;
		goto send_mgmt_rsp_exit;
	}

	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!bmp) {
		rc = -ENOMEM;
		goto send_mgmt_rsp_exit;
	}

	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
	if (!bmp->virt) {
		rc = -ENOMEM;
		goto send_mgmt_rsp_free_bmp;
	}

	INIT_LIST_HEAD(&bmp->list);
	bpl = (struct ulp_bde64 *) bmp->virt;
1667 1668 1669 1670 1671 1672
	bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
				     1, bpl, &bpl_entries);
	if (!cmp) {
		rc = -ENOMEM;
		goto send_mgmt_rsp_free_bmp;
1673
	}
1674 1675
	lpfc_bsg_copy_data(cmp, &job->request_payload,
			   job->request_payload.payload_len, 1);
1676

1677
	rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
1678 1679 1680 1681 1682

	if (rc == IOCB_SUCCESS)
		return 0; /* done for now */

	rc = -EACCES;
1683 1684

	lpfc_free_bsg_buffers(phba, cmp);
1685 1686

send_mgmt_rsp_free_bmp:
1687 1688
	if (bmp->virt)
		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1689 1690 1691
	kfree(bmp);
send_mgmt_rsp_exit:
	/* make error code available to userspace */
1692
	bsg_reply->result = rc;
1693
	job->dd_data = NULL;
1694 1695 1696 1697
	return rc;
}

/**
1698 1699 1700 1701 1702 1703 1704
 * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
 * @phba: Pointer to HBA context object.
 *
 * This function is responsible for preparing driver for diag loopback
 * on device.
 */
static int
1705
lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1706 1707 1708 1709
{
	struct lpfc_vport **vports;
	struct Scsi_Host *shost;
	struct lpfc_sli *psli;
1710
	struct lpfc_queue *qp = NULL;
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
	struct lpfc_sli_ring *pring;
	int i = 0;

	psli = &phba->sli;
	if (!psli)
		return -ENODEV;


	if ((phba->link_state == LPFC_HBA_ERROR) ||
	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
		return -EACCES;

	vports = lpfc_create_vport_work_array(phba);
	if (vports) {
		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
			shost = lpfc_shost_from_vport(vports[i]);
			scsi_block_requests(shost);
		}
		lpfc_destroy_vport_work_array(phba, vports);
	} else {
		shost = lpfc_shost_from_vport(phba->pport);
		scsi_block_requests(shost);
	}

1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746
	if (phba->sli_rev != LPFC_SLI_REV4) {
		pring = &psli->sli3_ring[LPFC_FCP_RING];
		lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
		return 0;
	}
	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
		pring = qp->pring;
		if (!pring || (pring->ringno != LPFC_FCP_RING))
			continue;
		if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
				      &pring->ring_lock))
1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
			break;
	}
	return 0;
}

/**
 * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
 * @phba: Pointer to HBA context object.
 *
 * This function is responsible for driver exit processing of setting up
 * diag loopback mode on device.
 */
static void
lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
{
	struct Scsi_Host *shost;
	struct lpfc_vport **vports;
	int i;

	vports = lpfc_create_vport_work_array(phba);
	if (vports) {
		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
			shost = lpfc_shost_from_vport(vports[i]);
			scsi_unblock_requests(shost);
		}
		lpfc_destroy_vport_work_array(phba, vports);
	} else {
		shost = lpfc_shost_from_vport(phba->pport);
		scsi_unblock_requests(shost);
	}
	return;
}

/**
 * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
 * @phba: Pointer to HBA context object.
1783
 * @job: LPFC_BSG_VENDOR_DIAG_MODE
1784
 *
1785 1786
 * This function is responsible for placing an sli3  port into diagnostic
 * loopback mode in order to perform a diagnostic loopback test.
1787 1788 1789 1790 1791
 * All new scsi requests are blocked, a small delay is used to allow the
 * scsi requests to complete then the link is brought down. If the link is
 * is placed in loopback mode then scsi requests are again allowed
 * so the scsi mid-layer doesn't give up on the port.
 * All of this is done in-line.
1792
 */
1793
static int
1794
lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
1795
{
1796 1797
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
1798 1799 1800
	struct diag_mode_set *loopback_mode;
	uint32_t link_flags;
	uint32_t timeout;
1801
	LPFC_MBOXQ_t *pmboxq  = NULL;
1802
	int mbxstatus = MBX_SUCCESS;
1803 1804
	int i = 0;
	int rc = 0;
1805

1806
	/* no data to return just the return code */
1807
	bsg_reply->reply_payload_rcv_len = 0;
1808

1809 1810
	if (job->request_len < sizeof(struct fc_bsg_request) +
	    sizeof(struct diag_mode_set)) {
1811
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1812 1813 1814 1815 1816
				"2738 Received DIAG MODE request size:%d "
				"below the minimum size:%d\n",
				job->request_len,
				(int)(sizeof(struct fc_bsg_request) +
				sizeof(struct diag_mode_set)));
1817 1818 1819 1820
		rc = -EINVAL;
		goto job_error;
	}

1821
	rc = lpfc_bsg_diag_mode_enter(phba);
1822 1823 1824 1825
	if (rc)
		goto job_error;

	/* bring the link to diagnostic mode */
1826
	loopback_mode = (struct diag_mode_set *)
1827
		bsg_request->rqst_data.h_vendor.vendor_cmd;
1828
	link_flags = loopback_mode->type;
J
James Smart 已提交
1829
	timeout = loopback_mode->timeout * 100;
1830 1831 1832 1833

	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmboxq) {
		rc = -ENOMEM;
1834
		goto loopback_mode_exit;
1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
	}
	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
	pmboxq->u.mb.mbxOwner = OWN_HOST;

	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);

	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
		/* wait for link down before proceeding */
		i = 0;
		while (phba->link_state != LPFC_LINK_DOWN) {
			if (i++ > timeout) {
				rc = -ETIMEDOUT;
				goto loopback_mode_exit;
			}
			msleep(10);
		}

		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
		if (link_flags == INTERNAL_LOOP_BACK)
			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
		else
			pmboxq->u.mb.un.varInitLnk.link_flags =
				FLAGS_TOPOLOGY_MODE_LOOP;

		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
		pmboxq->u.mb.mbxOwner = OWN_HOST;

		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
						     LPFC_MBOX_TMO);

		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
			rc = -ENODEV;
		else {
1869
			spin_lock_irq(&phba->hbalock);
1870
			phba->link_flag |= LS_LOOPBACK_MODE;
1871
			spin_unlock_irq(&phba->hbalock);
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
			/* wait for the link attention interrupt */
			msleep(100);

			i = 0;
			while (phba->link_state != LPFC_HBA_READY) {
				if (i++ > timeout) {
					rc = -ETIMEDOUT;
					break;
				}

				msleep(10);
			}
		}

	} else
		rc = -ENODEV;

loopback_mode_exit:
1890
	lpfc_bsg_diag_mode_exit(phba);
1891 1892 1893 1894

	/*
	 * Let SLI layer release mboxq if mbox command completed after timeout.
	 */
1895
	if (pmboxq && mbxstatus != MBX_TIMEOUT)
1896 1897 1898 1899
		mempool_free(pmboxq, phba->mbox_mem_pool);

job_error:
	/* make error code available to userspace */
1900
	bsg_reply->result = rc;
1901 1902
	/* complete the job back to userspace if no error */
	if (rc == 0)
J
Johannes Thumshirn 已提交
1903
		bsg_job_done(job, bsg_reply->result,
1904
			       bsg_reply->reply_payload_rcv_len);
1905 1906 1907 1908
	return rc;
}

/**
1909 1910 1911
 * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
 * @phba: Pointer to HBA context object.
 * @diag: Flag for set link to diag or nomral operation state.
1912
 *
1913 1914 1915 1916 1917
 * This function is responsible for issuing a sli4 mailbox command for setting
 * link to either diag state or normal operation state.
 */
static int
lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1918
{
1919 1920 1921 1922
	LPFC_MBOXQ_t *pmboxq;
	struct lpfc_mbx_set_link_diag_state *link_diag_state;
	uint32_t req_len, alloc_len;
	int mbxstatus = MBX_SUCCESS, rc;
1923

1924 1925
	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmboxq)
1926
		return -ENOMEM;
1927

1928 1929 1930 1931 1932 1933 1934 1935
	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
		   sizeof(struct lpfc_sli4_cfg_mhdr));
	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
				req_len, LPFC_SLI4_MBX_EMBED);
	if (alloc_len != req_len) {
		rc = -ENOMEM;
		goto link_diag_state_set_out;
1936
	}
1937 1938 1939 1940 1941
	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
			"3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
			diag, phba->sli4_hba.lnk_info.lnk_tp,
			phba->sli4_hba.lnk_info.lnk_no);

1942
	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1943 1944
	bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
	       LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1945
	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1946
	       phba->sli4_hba.lnk_info.lnk_no);
1947
	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1948
	       phba->sli4_hba.lnk_info.lnk_tp);
1949 1950 1951 1952 1953 1954
	if (diag)
		bf_set(lpfc_mbx_set_diag_state_diag,
		       &link_diag_state->u.req, 1);
	else
		bf_set(lpfc_mbx_set_diag_state_diag,
		       &link_diag_state->u.req, 0);
1955

1956
	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1957

1958 1959 1960 1961
	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
		rc = 0;
	else
		rc = -ENODEV;
1962

1963 1964 1965
link_diag_state_set_out:
	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
		mempool_free(pmboxq, phba->mbox_mem_pool);
1966

1967
	return rc;
1968 1969
}

1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
/**
 * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
 * @phba: Pointer to HBA context object.
 *
 * This function is responsible for issuing a sli4 mailbox command for setting
 * up internal loopback diagnostic.
 */
static int
lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
{
	LPFC_MBOXQ_t *pmboxq;
	uint32_t req_len, alloc_len;
	struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
	int mbxstatus = MBX_SUCCESS, rc = 0;

	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmboxq)
		return -ENOMEM;
	req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
		   sizeof(struct lpfc_sli4_cfg_mhdr));
	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
				req_len, LPFC_SLI4_MBX_EMBED);
	if (alloc_len != req_len) {
		mempool_free(pmboxq, phba->mbox_mem_pool);
		return -ENOMEM;
	}
	link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
	bf_set(lpfc_mbx_set_diag_state_link_num,
	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
	bf_set(lpfc_mbx_set_diag_state_link_type,
	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
	bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
2003
	       LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042

	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3127 Failed setup loopback mode mailbox "
				"command, rc:x%x, status:x%x\n", mbxstatus,
				pmboxq->u.mb.mbxStatus);
		rc = -ENODEV;
	}
	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
		mempool_free(pmboxq, phba->mbox_mem_pool);
	return rc;
}

/**
 * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
 * @phba: Pointer to HBA context object.
 *
 * This function set up SLI4 FC port registrations for diagnostic run, which
 * includes all the rpis, vfi, and also vpi.
 */
static int
lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
{
	int rc;

	if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3136 Port still had vfi registered: "
				"mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
				phba->pport->fc_myDID, phba->fcf.fcfi,
				phba->sli4_hba.vfi_ids[phba->pport->vfi],
				phba->vpi_ids[phba->pport->vpi]);
		return -EINVAL;
	}
	rc = lpfc_issue_reg_vfi(phba->pport);
	return rc;
}

2043
/**
2044 2045 2046
 * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
 * @phba: Pointer to HBA context object.
 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2047
 *
2048 2049 2050 2051
 * This function is responsible for placing an sli4 port into diagnostic
 * loopback mode in order to perform a diagnostic loopback test.
 */
static int
2052
lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
2053
{
2054 2055
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
2056
	struct diag_mode_set *loopback_mode;
2057 2058
	uint32_t link_flags, timeout;
	int i, rc = 0;
2059

2060
	/* no data to return just the return code */
2061
	bsg_reply->reply_payload_rcv_len = 0;
2062

2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073
	if (job->request_len < sizeof(struct fc_bsg_request) +
	    sizeof(struct diag_mode_set)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3011 Received DIAG MODE request size:%d "
				"below the minimum size:%d\n",
				job->request_len,
				(int)(sizeof(struct fc_bsg_request) +
				sizeof(struct diag_mode_set)));
		rc = -EINVAL;
		goto job_error;
	}
2074

2075
	rc = lpfc_bsg_diag_mode_enter(phba);
2076 2077 2078
	if (rc)
		goto job_error;

2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
	/* indicate we are in loobpack diagnostic mode */
	spin_lock_irq(&phba->hbalock);
	phba->link_flag |= LS_LOOPBACK_MODE;
	spin_unlock_irq(&phba->hbalock);

	/* reset port to start frome scratch */
	rc = lpfc_selective_reset(phba);
	if (rc)
		goto job_error;

2089
	/* bring the link to diagnostic mode */
2090 2091
	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
			"3129 Bring link to diagnostic state.\n");
2092
	loopback_mode = (struct diag_mode_set *)
2093
		bsg_request->rqst_data.h_vendor.vendor_cmd;
2094 2095 2096 2097
	link_flags = loopback_mode->type;
	timeout = loopback_mode->timeout * 100;

	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2098 2099 2100 2101
	if (rc) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3130 Failed to bring link to diagnostic "
				"state, rc:x%x\n", rc);
2102
		goto loopback_mode_exit;
2103
	}
2104 2105 2106 2107 2108 2109

	/* wait for link down before proceeding */
	i = 0;
	while (phba->link_state != LPFC_LINK_DOWN) {
		if (i++ > timeout) {
			rc = -ETIMEDOUT;
2110 2111 2112 2113
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3131 Timeout waiting for link to "
					"diagnostic mode, timeout:%d ms\n",
					timeout * 10);
2114 2115 2116
			goto loopback_mode_exit;
		}
		msleep(10);
2117
	}
2118

2119
	/* set up loopback mode */
2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133
	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
			"3132 Set up loopback mode:x%x\n", link_flags);

	if (link_flags == INTERNAL_LOOP_BACK)
		rc = lpfc_sli4_bsg_set_internal_loopback(phba);
	else if (link_flags == EXTERNAL_LOOP_BACK)
		rc = lpfc_hba_init_link_fc_topology(phba,
						    FLAGS_TOPOLOGY_MODE_PT_PT,
						    MBX_NOWAIT);
	else {
		rc = -EINVAL;
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"3141 Loopback mode:x%x not supported\n",
				link_flags);
2134 2135 2136
		goto loopback_mode_exit;
	}

2137
	if (!rc) {
2138 2139 2140
		/* wait for the link attention interrupt */
		msleep(100);
		i = 0;
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165
		while (phba->link_state < LPFC_LINK_UP) {
			if (i++ > timeout) {
				rc = -ETIMEDOUT;
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3137 Timeout waiting for link up "
					"in loopback mode, timeout:%d ms\n",
					timeout * 10);
				break;
			}
			msleep(10);
		}
	}

	/* port resource registration setup for loopback diagnostic */
	if (!rc) {
		/* set up a none zero myDID for loopback test */
		phba->pport->fc_myDID = 1;
		rc = lpfc_sli4_diag_fcport_reg_setup(phba);
	} else
		goto loopback_mode_exit;

	if (!rc) {
		/* wait for the port ready */
		msleep(100);
		i = 0;
2166 2167 2168
		while (phba->link_state != LPFC_HBA_READY) {
			if (i++ > timeout) {
				rc = -ETIMEDOUT;
2169 2170 2171 2172
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3133 Timeout waiting for port "
					"loopback mode ready, timeout:%d ms\n",
					timeout * 10);
2173 2174 2175 2176 2177 2178 2179
				break;
			}
			msleep(10);
		}
	}

loopback_mode_exit:
2180 2181 2182 2183 2184 2185
	/* clear loopback diagnostic mode */
	if (rc) {
		spin_lock_irq(&phba->hbalock);
		phba->link_flag &= ~LS_LOOPBACK_MODE;
		spin_unlock_irq(&phba->hbalock);
	}
2186 2187 2188 2189
	lpfc_bsg_diag_mode_exit(phba);

job_error:
	/* make error code available to userspace */
2190
	bsg_reply->result = rc;
2191 2192
	/* complete the job back to userspace if no error */
	if (rc == 0)
J
Johannes Thumshirn 已提交
2193
		bsg_job_done(job, bsg_reply->result,
2194
			       bsg_reply->reply_payload_rcv_len);
2195
	return rc;
2196 2197 2198
}

/**
2199 2200
 * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
 * @job: LPFC_BSG_VENDOR_DIAG_MODE
2201
 *
2202 2203 2204 2205
 * This function is responsible for responding to check and dispatch bsg diag
 * command from the user to proper driver action routines.
 */
static int
2206
lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
2207 2208 2209 2210 2211 2212
{
	struct Scsi_Host *shost;
	struct lpfc_vport *vport;
	struct lpfc_hba *phba;
	int rc;

2213
	shost = fc_bsg_to_shost(job);
2214 2215
	if (!shost)
		return -ENODEV;
2216
	vport = shost_priv(shost);
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241
	if (!vport)
		return -ENODEV;
	phba = vport->phba;
	if (!phba)
		return -ENODEV;

	if (phba->sli_rev < LPFC_SLI_REV4)
		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
		 LPFC_SLI_INTF_IF_TYPE_2)
		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
	else
		rc = -ENODEV;

	return rc;
}

/**
 * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
 * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
 *
 * This function is responsible for responding to check and dispatch bsg diag
 * command from the user to proper driver action routines.
 */
static int
2242
lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
2243
{
2244 2245
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
2246 2247 2248
	struct Scsi_Host *shost;
	struct lpfc_vport *vport;
	struct lpfc_hba *phba;
2249 2250 2251
	struct diag_mode_set *loopback_mode_end_cmd;
	uint32_t timeout;
	int rc, i;
2252

2253
	shost = fc_bsg_to_shost(job);
2254 2255
	if (!shost)
		return -ENODEV;
2256
	vport = shost_priv(shost);
2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
	if (!vport)
		return -ENODEV;
	phba = vport->phba;
	if (!phba)
		return -ENODEV;

	if (phba->sli_rev < LPFC_SLI_REV4)
		return -ENODEV;
	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
	    LPFC_SLI_INTF_IF_TYPE_2)
		return -ENODEV;

2269 2270 2271 2272 2273
	/* clear loopback diagnostic mode */
	spin_lock_irq(&phba->hbalock);
	phba->link_flag &= ~LS_LOOPBACK_MODE;
	spin_unlock_irq(&phba->hbalock);
	loopback_mode_end_cmd = (struct diag_mode_set *)
2274
			bsg_request->rqst_data.h_vendor.vendor_cmd;
2275 2276
	timeout = loopback_mode_end_cmd->timeout * 100;

2277
	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2278 2279 2280 2281 2282 2283
	if (rc) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3139 Failed to bring link to diagnostic "
				"state, rc:x%x\n", rc);
		goto loopback_mode_end_exit;
	}
2284

2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
	/* wait for link down before proceeding */
	i = 0;
	while (phba->link_state != LPFC_LINK_DOWN) {
		if (i++ > timeout) {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3140 Timeout waiting for link to "
					"diagnostic mode_end, timeout:%d ms\n",
					timeout * 10);
			/* there is nothing much we can do here */
			break;
		}
		msleep(10);
	}
2298

2299 2300 2301 2302 2303 2304
	/* reset port resource registrations */
	rc = lpfc_selective_reset(phba);
	phba->pport->fc_myDID = 0;

loopback_mode_end_exit:
	/* make return code available to userspace */
2305
	bsg_reply->result = rc;
2306 2307
	/* complete the job back to userspace if no error */
	if (rc == 0)
J
Johannes Thumshirn 已提交
2308
		bsg_job_done(job, bsg_reply->result,
2309
			       bsg_reply->reply_payload_rcv_len);
2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320
	return rc;
}

/**
 * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
 * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
 *
 * This function is to perform SLI4 diag link test request from the user
 * applicaiton.
 */
static int
2321
lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
2322
{
2323 2324
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
	struct Scsi_Host *shost;
	struct lpfc_vport *vport;
	struct lpfc_hba *phba;
	LPFC_MBOXQ_t *pmboxq;
	struct sli4_link_diag *link_diag_test_cmd;
	uint32_t req_len, alloc_len;
	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
	union lpfc_sli4_cfg_shdr *shdr;
	uint32_t shdr_status, shdr_add_status;
	struct diag_status *diag_status_reply;
	int mbxstatus, rc = 0;

2337
	shost = fc_bsg_to_shost(job);
2338 2339 2340 2341
	if (!shost) {
		rc = -ENODEV;
		goto job_error;
	}
2342
	vport = shost_priv(shost);
2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374
	if (!vport) {
		rc = -ENODEV;
		goto job_error;
	}
	phba = vport->phba;
	if (!phba) {
		rc = -ENODEV;
		goto job_error;
	}

	if (phba->sli_rev < LPFC_SLI_REV4) {
		rc = -ENODEV;
		goto job_error;
	}
	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
	    LPFC_SLI_INTF_IF_TYPE_2) {
		rc = -ENODEV;
		goto job_error;
	}

	if (job->request_len < sizeof(struct fc_bsg_request) +
	    sizeof(struct sli4_link_diag)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3013 Received LINK DIAG TEST request "
				" size:%d below the minimum size:%d\n",
				job->request_len,
				(int)(sizeof(struct fc_bsg_request) +
				sizeof(struct sli4_link_diag)));
		rc = -EINVAL;
		goto job_error;
	}

2375
	rc = lpfc_bsg_diag_mode_enter(phba);
2376 2377 2378 2379
	if (rc)
		goto job_error;

	link_diag_test_cmd = (struct sli4_link_diag *)
2380
			 bsg_request->rqst_data.h_vendor.vendor_cmd;
2381 2382 2383 2384 2385 2386 2387

	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);

	if (rc)
		goto job_error;

	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2388
	if (!pmboxq)
2389 2390 2391 2392 2393 2394 2395
		goto link_diag_test_exit;

	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
		   sizeof(struct lpfc_sli4_cfg_mhdr));
	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
				     req_len, LPFC_SLI4_MBX_EMBED);
2396
	if (alloc_len != req_len)
2397
		goto link_diag_test_exit;
2398

2399 2400
	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2401
	       phba->sli4_hba.lnk_info.lnk_no);
2402
	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2403
	       phba->sli4_hba.lnk_info.lnk_tp);
2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
	       link_diag_test_cmd->test_id);
	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
	       link_diag_test_cmd->loops);
	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
	       link_diag_test_cmd->test_version);
	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
	       link_diag_test_cmd->error_action);

	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);

	shdr = (union lpfc_sli4_cfg_shdr *)
		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
	if (shdr_status || shdr_add_status || mbxstatus) {
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"3010 Run link diag test mailbox failed with "
				"mbx_status x%x status x%x, add_status x%x\n",
				mbxstatus, shdr_status, shdr_add_status);
	}

	diag_status_reply = (struct diag_status *)
2427
			    bsg_reply->reply_data.vendor_reply.vendor_rsp;
2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454

	if (job->reply_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"3012 Received Run link diag test reply "
				"below minimum size (%d): reply_len:%d\n",
				(int)(sizeof(struct fc_bsg_request) +
				sizeof(struct diag_status)),
				job->reply_len);
		rc = -EINVAL;
		goto job_error;
	}

	diag_status_reply->mbox_status = mbxstatus;
	diag_status_reply->shdr_status = shdr_status;
	diag_status_reply->shdr_add_status = shdr_add_status;

link_diag_test_exit:
	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);

	if (pmboxq)
		mempool_free(pmboxq, phba->mbox_mem_pool);

	lpfc_bsg_diag_mode_exit(phba);

job_error:
	/* make error code available to userspace */
2455
	bsg_reply->result = rc;
2456 2457
	/* complete the job back to userspace if no error */
	if (rc == 0)
J
Johannes Thumshirn 已提交
2458
		bsg_job_done(job, bsg_reply->result,
2459
			       bsg_reply->reply_payload_rcv_len);
2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480
	return rc;
}

/**
 * lpfcdiag_loop_self_reg - obtains a remote port login id
 * @phba: Pointer to HBA context object
 * @rpi: Pointer to a remote port login id
 *
 * This function obtains a remote port login id so the diag loopback test
 * can send and receive its own unsolicited CT command.
 **/
static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
{
	LPFC_MBOXQ_t *mbox;
	struct lpfc_dmabuf *dmabuff;
	int status;

	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!mbox)
		return -ENOMEM;

2481 2482 2483 2484 2485
	if (phba->sli_rev < LPFC_SLI_REV4)
		status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
				(uint8_t *)&phba->pport->fc_sparam,
				mbox, *rpi);
	else {
2486
		*rpi = lpfc_sli4_alloc_rpi(phba);
J
James Smart 已提交
2487 2488 2489 2490
		if (*rpi == LPFC_RPI_ALLOC_ERROR) {
			mempool_free(mbox, phba->mbox_mem_pool);
			return -EBUSY;
		}
2491 2492 2493 2494 2495 2496
		status = lpfc_reg_rpi(phba, phba->pport->vpi,
				phba->pport->fc_myDID,
				(uint8_t *)&phba->pport->fc_sparam,
				mbox, *rpi);
	}

2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518
	if (status) {
		mempool_free(mbox, phba->mbox_mem_pool);
		if (phba->sli_rev == LPFC_SLI_REV4)
			lpfc_sli4_free_rpi(phba, *rpi);
		return -ENOMEM;
	}

	dmabuff = (struct lpfc_dmabuf *) mbox->context1;
	mbox->context1 = NULL;
	mbox->context2 = NULL;
	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);

	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
		kfree(dmabuff);
		if (status != MBX_TIMEOUT)
			mempool_free(mbox, phba->mbox_mem_pool);
		if (phba->sli_rev == LPFC_SLI_REV4)
			lpfc_sli4_free_rpi(phba, *rpi);
		return -ENODEV;
	}

2519 2520
	if (phba->sli_rev < LPFC_SLI_REV4)
		*rpi = mbox->u.mb.un.varWords[0];
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544

	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
	kfree(dmabuff);
	mempool_free(mbox, phba->mbox_mem_pool);
	return 0;
}

/**
 * lpfcdiag_loop_self_unreg - unregs from the rpi
 * @phba: Pointer to HBA context object
 * @rpi: Remote port login id
 *
 * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
 **/
static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
{
	LPFC_MBOXQ_t *mbox;
	int status;

	/* Allocate mboxq structure */
	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (mbox == NULL)
		return -ENOMEM;

2545 2546 2547 2548 2549 2550
	if (phba->sli_rev < LPFC_SLI_REV4)
		lpfc_unreg_login(phba, 0, rpi, mbox);
	else
		lpfc_unreg_login(phba, phba->pport->vpi,
				 phba->sli4_hba.rpi_ids[rpi], mbox);

2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);

	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
		if (status != MBX_TIMEOUT)
			mempool_free(mbox, phba->mbox_mem_pool);
		return -EIO;
	}
	mempool_free(mbox, phba->mbox_mem_pool);
	if (phba->sli_rev == LPFC_SLI_REV4)
		lpfc_sli4_free_rpi(phba, rpi);
	return 0;
}

/**
 * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
 * @phba: Pointer to HBA context object
 * @rpi: Remote port login id
 * @txxri: Pointer to transmit exchange id
 * @rxxri: Pointer to response exchabge id
 *
 * This function obtains the transmit and receive ids required to send
 * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
 * flags are used to the unsolicted response handler is able to process
 * the ct command sent on the same port.
 **/
static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
			 uint16_t *txxri, uint16_t * rxxri)
2578 2579 2580 2581 2582 2583 2584 2585
{
	struct lpfc_bsg_event *evt;
	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
	IOCB_t *cmd, *rsp;
	struct lpfc_dmabuf *dmabuf;
	struct ulp_bde64 *bpl = NULL;
	struct lpfc_sli_ct_request *ctreq = NULL;
	int ret_val = 0;
2586
	int time_left;
2587
	int iocb_stat = IOCB_SUCCESS;
2588 2589 2590 2591 2592 2593 2594
	unsigned long flags;

	*txxri = 0;
	*rxxri = 0;
	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
				SLI_CT_ELX_LOOPBACK);
	if (!evt)
2595
		return -ENOMEM;
2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607

	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	list_add(&evt->node, &phba->ct_ev_waiters);
	lpfc_bsg_event_ref(evt);
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

	cmdiocbq = lpfc_sli_get_iocbq(phba);
	rspiocbq = lpfc_sli_get_iocbq(phba);

	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (dmabuf) {
		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
		if (dmabuf->virt) {
			INIT_LIST_HEAD(&dmabuf->list);
			bpl = (struct ulp_bde64 *) dmabuf->virt;
			memset(bpl, 0, sizeof(*bpl));
			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
			bpl->addrHigh =
				le32_to_cpu(putPaddrHigh(dmabuf->phys +
					sizeof(*bpl)));
			bpl->addrLow =
				le32_to_cpu(putPaddrLow(dmabuf->phys +
					sizeof(*bpl)));
			bpl->tus.f.bdeFlags = 0;
			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
			bpl->tus.w = le32_to_cpu(bpl->tus.w);
		}
2623 2624 2625
	}

	if (cmdiocbq == NULL || rspiocbq == NULL ||
2626 2627
	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
		dmabuf->virt == NULL) {
2628
		ret_val = -ENOMEM;
2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
		goto err_get_xri_exit;
	}

	cmd = &cmdiocbq->iocb;
	rsp = &rspiocbq->iocb;

	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);

	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
	ctreq->RevisionId.bits.InId = 0;
	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
	ctreq->FsSubType = 0;
	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
	ctreq->CommandResponse.bits.Size = 0;


	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);

	cmd->un.xseq64.w5.hcsw.Fctl = LA;
	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;

	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
	cmd->ulpBdeCount = 1;
	cmd->ulpLe = 1;
	cmd->ulpClass = CLASS3;
	cmd->ulpContext = rpi;

	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
	cmdiocbq->vport = phba->pport;
2663
	cmdiocbq->iocb_cmpl = NULL;
2664

2665
	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2666 2667 2668
				rspiocbq,
				(phba->fc_ratov * 2)
				+ LPFC_DRVR_TIMEOUT);
2669
	if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
2670
		ret_val = -EIO;
2671
		goto err_get_xri_exit;
2672
	}
2673 2674 2675 2676
	*txxri =  rsp->ulpContext;

	evt->waiting = 1;
	evt->wait_time_stamp = jiffies;
2677
	time_left = wait_event_interruptible_timeout(
2678
		evt->wq, !list_empty(&evt->events_to_see),
2679 2680
		msecs_to_jiffies(1000 *
			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
2681
	if (list_empty(&evt->events_to_see))
2682
		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704
	else {
		spin_lock_irqsave(&phba->ct_ev_lock, flags);
		list_move(evt->events_to_see.prev, &evt->events_to_get);
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
		*rxxri = (list_entry(evt->events_to_get.prev,
				     typeof(struct event_data),
				     node))->immed_dat;
	}
	evt->waiting = 0;

err_get_xri_exit:
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	lpfc_bsg_event_unref(evt); /* release ref */
	lpfc_bsg_event_unref(evt); /* delete */
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

	if (dmabuf) {
		if (dmabuf->virt)
			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
		kfree(dmabuf);
	}

2705
	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2706 2707 2708 2709 2710 2711
		lpfc_sli_release_iocbq(phba, cmdiocbq);
	if (rspiocbq)
		lpfc_sli_release_iocbq(phba, rspiocbq);
	return ret_val;
}

2712 2713 2714 2715
/**
 * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
 * @phba: Pointer to HBA context object
 *
J
James Smart 已提交
2716
 * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
2717
 * returns the pointer to the buffer.
2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732
 **/
static struct lpfc_dmabuf *
lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
{
	struct lpfc_dmabuf *dmabuf;
	struct pci_dev *pcidev = phba->pcidev;

	/* allocate dma buffer struct */
	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!dmabuf)
		return NULL;

	INIT_LIST_HEAD(&dmabuf->list);

	/* now, allocate dma buffer */
J
Joe Perches 已提交
2733 2734
	dmabuf->virt = dma_zalloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
					   &(dmabuf->phys), GFP_KERNEL);
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790

	if (!dmabuf->virt) {
		kfree(dmabuf);
		return NULL;
	}

	return dmabuf;
}

/**
 * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
 * @phba: Pointer to HBA context object.
 * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
 *
 * This routine just simply frees a dma buffer and its associated buffer
 * descriptor referred by @dmabuf.
 **/
static void
lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
{
	struct pci_dev *pcidev = phba->pcidev;

	if (!dmabuf)
		return;

	if (dmabuf->virt)
		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
				  dmabuf->virt, dmabuf->phys);
	kfree(dmabuf);
	return;
}

/**
 * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
 * @phba: Pointer to HBA context object.
 * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
 *
 * This routine just simply frees all dma buffers and their associated buffer
 * descriptors referred by @dmabuf_list.
 **/
static void
lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
			    struct list_head *dmabuf_list)
{
	struct lpfc_dmabuf *dmabuf, *next_dmabuf;

	if (list_empty(dmabuf_list))
		return;

	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
		list_del_init(&dmabuf->list);
		lpfc_bsg_dma_page_free(phba, dmabuf);
	}
	return;
}

2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
/**
 * diag_cmd_data_alloc - fills in a bde struct with dma buffers
 * @phba: Pointer to HBA context object
 * @bpl: Pointer to 64 bit bde structure
 * @size: Number of bytes to process
 * @nocopydata: Flag to copy user data into the allocated buffer
 *
 * This function allocates page size buffers and populates an lpfc_dmabufext.
 * If allowed the user data pointed to with indataptr is copied into the kernel
 * memory. The chained list of page size buffers is returned.
 **/
static struct lpfc_dmabufext *
diag_cmd_data_alloc(struct lpfc_hba *phba,
		   struct ulp_bde64 *bpl, uint32_t size,
		   int nocopydata)
{
	struct lpfc_dmabufext *mlist = NULL;
	struct lpfc_dmabufext *dmp;
	int cnt, offset = 0, i = 0;
	struct pci_dev *pcidev;

	pcidev = phba->pcidev;

	while (size) {
		/* We get chunks of 4K */
		if (size > BUF_SZ_4K)
			cnt = BUF_SZ_4K;
		else
			cnt = size;

		/* allocate struct lpfc_dmabufext buffer header */
		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
		if (!dmp)
			goto out;

		INIT_LIST_HEAD(&dmp->dma.list);

		/* Queue it to a linked list */
		if (mlist)
			list_add_tail(&dmp->dma.list, &mlist->dma.list);
		else
			mlist = dmp;

		/* allocate buffer */
		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
						   cnt,
						   &(dmp->dma.phys),
						   GFP_KERNEL);

		if (!dmp->dma.virt)
			goto out;

		dmp->size = cnt;

		if (nocopydata) {
			bpl->tus.f.bdeFlags = 0;
			pci_dma_sync_single_for_device(phba->pcidev,
				dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);

		} else {
			memset((uint8_t *)dmp->dma.virt, 0, cnt);
			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
		}

		/* build buffer ptr list for IOCB */
		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
		bpl->tus.f.bdeSize = (ushort) cnt;
		bpl->tus.w = le32_to_cpu(bpl->tus.w);
		bpl++;

		i++;
		offset += cnt;
		size -= cnt;
	}

2867 2868 2869 2870
	if (mlist) {
		mlist->flag = i;
		return mlist;
	}
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
out:
	diag_cmd_data_free(phba, mlist);
	return NULL;
}

/**
 * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
 * @phba: Pointer to HBA context object
 * @rxxri: Receive exchange id
 * @len: Number of data bytes
 *
L
Lucas De Marchi 已提交
2882
 * This function allocates and posts a data buffer of sufficient size to receive
2883 2884 2885 2886 2887
 * an unsolicted CT command.
 **/
static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
			     size_t len)
{
2888
	struct lpfc_sli_ring *pring;
2889 2890 2891 2892 2893 2894 2895 2896 2897 2898
	struct lpfc_iocbq *cmdiocbq;
	IOCB_t *cmd = NULL;
	struct list_head head, *curr, *next;
	struct lpfc_dmabuf *rxbmp;
	struct lpfc_dmabuf *dmp;
	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
	struct ulp_bde64 *rxbpl = NULL;
	uint32_t num_bde;
	struct lpfc_dmabufext *rxbuffer = NULL;
	int ret_val = 0;
2899
	int iocb_stat;
2900 2901
	int i = 0;

2902 2903
	pring = lpfc_phba_elsring(phba);

2904 2905 2906 2907
	cmdiocbq = lpfc_sli_get_iocbq(phba);
	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (rxbmp != NULL) {
		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2908 2909 2910 2911 2912
		if (rxbmp->virt) {
			INIT_LIST_HEAD(&rxbmp->list);
			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
		}
2913 2914
	}

2915
	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2916
		ret_val = -ENOMEM;
2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
		goto err_post_rxbufs_exit;
	}

	/* Queue buffers for the receive exchange */
	num_bde = (uint32_t)rxbuffer->flag;
	dmp = &rxbuffer->dma;

	cmd = &cmdiocbq->iocb;
	i = 0;

	INIT_LIST_HEAD(&head);
	list_add_tail(&head, &dmp->list);
	list_for_each_safe(curr, next, &head) {
		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
		list_del(curr);

		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
			cmd->un.quexri64cx.buff.bde.addrHigh =
				putPaddrHigh(mp[i]->phys);
			cmd->un.quexri64cx.buff.bde.addrLow =
				putPaddrLow(mp[i]->phys);
			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
				((struct lpfc_dmabufext *)mp[i])->size;
			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
			cmd->ulpCommand = CMD_QUE_XRI64_CX;
			cmd->ulpPU = 0;
			cmd->ulpLe = 1;
			cmd->ulpBdeCount = 1;
			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;

		} else {
			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
			cmd->un.cont64[i].tus.f.bdeSize =
				((struct lpfc_dmabufext *)mp[i])->size;
					cmd->ulpBdeCount = ++i;

			if ((--num_bde > 0) && (i < 2))
				continue;

			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
			cmd->ulpLe = 1;
		}

		cmd->ulpClass = CLASS3;
		cmd->ulpContext = rxxri;

2965 2966 2967
		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
						0);
		if (iocb_stat == IOCB_ERROR) {
2968 2969 2970 2971 2972 2973
			diag_cmd_data_free(phba,
				(struct lpfc_dmabufext *)mp[0]);
			if (mp[1])
				diag_cmd_data_free(phba,
					  (struct lpfc_dmabufext *)mp[1]);
			dmp = list_entry(next, struct lpfc_dmabuf, list);
2974
			ret_val = -EIO;
2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987
			goto err_post_rxbufs_exit;
		}

		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
		if (mp[1]) {
			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
			mp[1] = NULL;
		}

		/* The iocb was freed by lpfc_sli_issue_iocb */
		cmdiocbq = lpfc_sli_get_iocbq(phba);
		if (!cmdiocbq) {
			dmp = list_entry(next, struct lpfc_dmabuf, list);
2988
			ret_val = -EIO;
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010
			goto err_post_rxbufs_exit;
		}

		cmd = &cmdiocbq->iocb;
		i = 0;
	}
	list_del(&head);

err_post_rxbufs_exit:

	if (rxbmp) {
		if (rxbmp->virt)
			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
		kfree(rxbmp);
	}

	if (cmdiocbq)
		lpfc_sli_release_iocbq(phba, cmdiocbq);
	return ret_val;
}

/**
3011
 * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029
 * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
 *
 * This function receives a user data buffer to be transmitted and received on
 * the same port, the link must be up and in loopback mode prior
 * to being called.
 * 1. A kernel buffer is allocated to copy the user data into.
 * 2. The port registers with "itself".
 * 3. The transmit and receive exchange ids are obtained.
 * 4. The receive exchange id is posted.
 * 5. A new els loopback event is created.
 * 6. The command and response iocbs are allocated.
 * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
 *
 * This function is meant to be called n times while the port is in loopback
 * so it is the apps responsibility to issue a reset to take the port out
 * of loopback mode.
 **/
static int
3030
lpfc_bsg_diag_loopback_run(struct bsg_job *job)
3031
{
3032
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3033
	struct fc_bsg_reply *bsg_reply = job->reply;
3034 3035 3036 3037 3038 3039 3040
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_bsg_event *evt;
	struct event_data *evdat;
	struct lpfc_sli *psli = &phba->sli;
	uint32_t size;
	uint32_t full_size;
	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
3041
	uint16_t rpi = 0;
3042 3043
	struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
	IOCB_t *cmd, *rsp = NULL;
3044 3045 3046 3047 3048 3049
	struct lpfc_sli_ct_request *ctreq;
	struct lpfc_dmabuf *txbmp;
	struct ulp_bde64 *txbpl = NULL;
	struct lpfc_dmabufext *txbuffer = NULL;
	struct list_head head;
	struct lpfc_dmabuf  *curr;
3050
	uint16_t txxri = 0, rxxri;
3051 3052 3053
	uint32_t num_bde;
	uint8_t *ptr = NULL, *rx_databuf = NULL;
	int rc = 0;
3054
	int time_left;
3055
	int iocb_stat = IOCB_SUCCESS;
3056 3057 3058 3059 3060
	unsigned long flags;
	void *dataout = NULL;
	uint32_t total_mem;

	/* in case no data is returned return just the return code */
3061
	bsg_reply->reply_payload_rcv_len = 0;
3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097

	if (job->request_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2739 Received DIAG TEST request below minimum "
				"size\n");
		rc = -EINVAL;
		goto loopback_test_exit;
	}

	if (job->request_payload.payload_len !=
		job->reply_payload.payload_len) {
		rc = -EINVAL;
		goto loopback_test_exit;
	}

	if ((phba->link_state == LPFC_HBA_ERROR) ||
	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
		rc = -EACCES;
		goto loopback_test_exit;
	}

	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
		rc = -EACCES;
		goto loopback_test_exit;
	}

	size = job->request_payload.payload_len;
	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */

	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
		rc = -ERANGE;
		goto loopback_test_exit;
	}

3098
	if (full_size >= BUF_SZ_4K) {
3099 3100 3101 3102 3103 3104 3105 3106
		/*
		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
		 * then we allocate 64k and re-use that buffer over and over to
		 * xfer the whole block. This is because Linux kernel has a
		 * problem allocating more than 120k of kernel space memory. Saw
		 * problem with GET_FCPTARGETMAPPING...
		 */
		if (size <= (64 * 1024))
3107
			total_mem = full_size;
3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125
		else
			total_mem = 64 * 1024;
	} else
		/* Allocate memory for ioctl data */
		total_mem = BUF_SZ_4K;

	dataout = kmalloc(total_mem, GFP_KERNEL);
	if (dataout == NULL) {
		rc = -ENOMEM;
		goto loopback_test_exit;
	}

	ptr = dataout;
	ptr += ELX_LOOPBACK_HEADER_SZ;
	sg_copy_to_buffer(job->request_payload.sg_list,
				job->request_payload.sg_cnt,
				ptr, size);
	rc = lpfcdiag_loop_self_reg(phba, &rpi);
3126
	if (rc)
3127 3128
		goto loopback_test_exit;

3129 3130 3131 3132 3133 3134
	if (phba->sli_rev < LPFC_SLI_REV4) {
		rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
		if (rc) {
			lpfcdiag_loop_self_unreg(phba, rpi);
			goto loopback_test_exit;
		}
3135

3136 3137 3138 3139 3140
		rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
		if (rc) {
			lpfcdiag_loop_self_unreg(phba, rpi);
			goto loopback_test_exit;
		}
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155
	}
	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
				SLI_CT_ELX_LOOPBACK);
	if (!evt) {
		lpfcdiag_loop_self_unreg(phba, rpi);
		rc = -ENOMEM;
		goto loopback_test_exit;
	}

	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	list_add(&evt->node, &phba->ct_ev_waiters);
	lpfc_bsg_event_ref(evt);
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

	cmdiocbq = lpfc_sli_get_iocbq(phba);
3156 3157
	if (phba->sli_rev < LPFC_SLI_REV4)
		rspiocbq = lpfc_sli_get_iocbq(phba);
3158 3159 3160 3161
	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);

	if (txbmp) {
		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3162 3163 3164
		if (txbmp->virt) {
			INIT_LIST_HEAD(&txbmp->list);
			txbpl = (struct ulp_bde64 *) txbmp->virt;
3165 3166
			txbuffer = diag_cmd_data_alloc(phba,
							txbpl, full_size, 0);
3167
		}
3168 3169
	}

3170 3171 3172 3173 3174
	if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
		rc = -ENOMEM;
		goto err_loopback_test_exit;
	}
	if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
3175 3176 3177 3178 3179
		rc = -ENOMEM;
		goto err_loopback_test_exit;
	}

	cmd = &cmdiocbq->iocb;
3180 3181
	if (phba->sli_rev < LPFC_SLI_REV4)
		rsp = &rspiocbq->iocb;
3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227

	INIT_LIST_HEAD(&head);
	list_add_tail(&head, &txbuffer->dma.list);
	list_for_each_entry(curr, &head, list) {
		segment_len = ((struct lpfc_dmabufext *)curr)->size;
		if (current_offset == 0) {
			ctreq = curr->virt;
			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
			ctreq->RevisionId.bits.InId = 0;
			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
			ctreq->FsSubType = 0;
			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
			ctreq->CommandResponse.bits.Size   = size;
			segment_offset = ELX_LOOPBACK_HEADER_SZ;
		} else
			segment_offset = 0;

		BUG_ON(segment_offset >= segment_len);
		memcpy(curr->virt + segment_offset,
			ptr + current_offset,
			segment_len - segment_offset);

		current_offset += segment_len - segment_offset;
		BUG_ON(current_offset > size);
	}
	list_del(&head);

	/* Build the XMIT_SEQUENCE iocb */
	num_bde = (uint32_t)txbuffer->flag;

	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));

	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;

	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
	cmd->ulpBdeCount = 1;
	cmd->ulpLe = 1;
	cmd->ulpClass = CLASS3;

3228 3229 3230 3231 3232 3233 3234 3235 3236
	if (phba->sli_rev < LPFC_SLI_REV4) {
		cmd->ulpContext = txxri;
	} else {
		cmd->un.xseq64.bdl.ulpIoTag32 = 0;
		cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
		cmdiocbq->context3 = txbmp;
		cmdiocbq->sli4_xritag = NO_XRI;
		cmd->unsli3.rcvsli3.ox_id = 0xffff;
	}
3237
	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
3238
	cmdiocbq->iocb_flag |= LPFC_IO_LOOPBACK;
3239
	cmdiocbq->vport = phba->pport;
3240
	cmdiocbq->iocb_cmpl = NULL;
3241 3242 3243
	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
					     rspiocbq, (phba->fc_ratov * 2) +
					     LPFC_DRVR_TIMEOUT);
3244

3245 3246 3247
	if ((iocb_stat != IOCB_SUCCESS) ||
	    ((phba->sli_rev < LPFC_SLI_REV4) &&
	     (rsp->ulpStatus != IOSTAT_SUCCESS))) {
3248 3249 3250
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"3126 Failed loopback test issue iocb: "
				"iocb_stat:x%x\n", iocb_stat);
3251 3252 3253 3254 3255
		rc = -EIO;
		goto err_loopback_test_exit;
	}

	evt->waiting = 1;
3256
	time_left = wait_event_interruptible_timeout(
3257
		evt->wq, !list_empty(&evt->events_to_see),
3258 3259
		msecs_to_jiffies(1000 *
			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
3260
	evt->waiting = 0;
3261
	if (list_empty(&evt->events_to_see)) {
3262
		rc = (time_left) ? -EINTR : -ETIMEDOUT;
3263 3264 3265 3266
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"3125 Not receiving unsolicited event, "
				"rc:x%x\n", rc);
	} else {
3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285
		spin_lock_irqsave(&phba->ct_ev_lock, flags);
		list_move(evt->events_to_see.prev, &evt->events_to_get);
		evdat = list_entry(evt->events_to_get.prev,
				   typeof(*evdat), node);
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
		rx_databuf = evdat->data;
		if (evdat->len != full_size) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"1603 Loopback test did not receive expected "
				"data length. actual length 0x%x expected "
				"length 0x%x\n",
				evdat->len, full_size);
			rc = -EIO;
		} else if (rx_databuf == NULL)
			rc = -EIO;
		else {
			rc = IOCB_SUCCESS;
			/* skip over elx loopback header */
			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3286
			bsg_reply->reply_payload_rcv_len =
3287 3288 3289
				sg_copy_from_buffer(job->reply_payload.sg_list,
						    job->reply_payload.sg_cnt,
						    rx_databuf, size);
3290
			bsg_reply->reply_payload_rcv_len = size;
3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301
		}
	}

err_loopback_test_exit:
	lpfcdiag_loop_self_unreg(phba, rpi);

	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	lpfc_bsg_event_unref(evt); /* release ref */
	lpfc_bsg_event_unref(evt); /* delete */
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

3302
	if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319
		lpfc_sli_release_iocbq(phba, cmdiocbq);

	if (rspiocbq != NULL)
		lpfc_sli_release_iocbq(phba, rspiocbq);

	if (txbmp != NULL) {
		if (txbpl != NULL) {
			if (txbuffer != NULL)
				diag_cmd_data_free(phba, txbuffer);
			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
		}
		kfree(txbmp);
	}

loopback_test_exit:
	kfree(dataout);
	/* make error code available to userspace */
3320
	bsg_reply->result = rc;
3321 3322
	job->dd_data = NULL;
	/* complete the job back to userspace if no error */
3323
	if (rc == IOCB_SUCCESS)
J
Johannes Thumshirn 已提交
3324
		bsg_job_done(job, bsg_reply->result,
3325
			       bsg_reply->reply_payload_rcv_len);
3326 3327 3328 3329 3330 3331 3332 3333
	return rc;
}

/**
 * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
 * @job: GET_DFC_REV fc_bsg_job
 **/
static int
3334
lpfc_bsg_get_dfc_rev(struct bsg_job *job)
3335
{
3336
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3337
	struct fc_bsg_reply *bsg_reply = job->reply;
3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351
	struct lpfc_hba *phba = vport->phba;
	struct get_mgmt_rev_reply *event_reply;
	int rc = 0;

	if (job->request_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2740 Received GET_DFC_REV request below "
				"minimum size\n");
		rc = -EINVAL;
		goto job_error;
	}

	event_reply = (struct get_mgmt_rev_reply *)
3352
		bsg_reply->reply_data.vendor_reply.vendor_rsp;
3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365

	if (job->reply_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2741 Received GET_DFC_REV reply below "
				"minimum size\n");
		rc = -EINVAL;
		goto job_error;
	}

	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
job_error:
3366
	bsg_reply->result = rc;
3367
	if (rc == 0)
J
Johannes Thumshirn 已提交
3368
		bsg_job_done(job, bsg_reply->result,
3369
			       bsg_reply->reply_payload_rcv_len);
3370 3371 3372 3373
	return rc;
}

/**
3374
 * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3375 3376 3377 3378 3379 3380 3381 3382 3383
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to mailbox command.
 *
 * This is completion handler function for mailbox commands issued from
 * lpfc_bsg_issue_mbox function. This function is called by the
 * mailbox event handler function with no lock held. This function
 * will wake up thread waiting on the wait queue pointed by context1
 * of the mailbox.
 **/
3384
static void
3385
lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3386 3387
{
	struct bsg_job_data *dd_data;
3388
	struct fc_bsg_reply *bsg_reply;
3389
	struct bsg_job *job;
3390 3391
	uint32_t size;
	unsigned long flags;
3392
	uint8_t *pmb, *pmb_buf;
3393 3394 3395

	dd_data = pmboxq->context1;

3396 3397 3398
	/*
	 * The outgoing buffer is readily referred from the dma buffer,
	 * just need to get header part from mailboxq structure.
3399
	 */
3400 3401 3402
	pmb = (uint8_t *)&pmboxq->u.mb;
	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3403

3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
	/* Determine if job has been aborted */

	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	job = dd_data->set_job;
	if (job) {
		/* Prevent timeout handling from trying to abort job  */
		job->dd_data = NULL;
	}
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

	/* Copy the mailbox data to the job if it is still active */

3416
	if (job) {
3417
		bsg_reply = job->reply;
3418
		size = job->reply_payload.payload_len;
3419
		bsg_reply->reply_payload_rcv_len =
3420
			sg_copy_from_buffer(job->reply_payload.sg_list,
3421 3422
					    job->reply_payload.sg_cnt,
					    pmb_buf, size);
3423
	}
3424

3425
	dd_data->set_job = NULL;
3426
	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3427 3428 3429
	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
	kfree(dd_data);

3430 3431
	/* Complete the job if the job is still active */

3432
	if (job) {
3433
		bsg_reply->result = 0;
J
Johannes Thumshirn 已提交
3434
		bsg_job_done(job, bsg_reply->result,
3435
			       bsg_reply->reply_payload_rcv_len);
3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
	}
	return;
}

/**
 * lpfc_bsg_check_cmd_access - test for a supported mailbox command
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a mailbox object.
 * @vport: Pointer to a vport object.
 *
 * Some commands require the port to be offline, some may not be called from
 * the application.
 **/
static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
	MAILBOX_t *mb, struct lpfc_vport *vport)
{
	/* return negative error values for bsg job */
	switch (mb->mbxCommand) {
	/* Offline only */
	case MBX_INIT_LINK:
	case MBX_DOWN_LINK:
	case MBX_CONFIG_LINK:
	case MBX_CONFIG_RING:
	case MBX_RESET_RING:
	case MBX_UNREG_LOGIN:
	case MBX_CLEAR_LA:
	case MBX_DUMP_CONTEXT:
	case MBX_RUN_DIAGS:
	case MBX_RESTART:
	case MBX_SET_MASK:
		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2743 Command 0x%x is illegal in on-line "
				"state\n",
				mb->mbxCommand);
			return -EPERM;
		}
	case MBX_WRITE_NV:
	case MBX_WRITE_VPARMS:
	case MBX_LOAD_SM:
	case MBX_READ_NV:
	case MBX_READ_CONFIG:
	case MBX_READ_RCONFIG:
	case MBX_READ_STATUS:
	case MBX_READ_XRI:
	case MBX_READ_REV:
	case MBX_READ_LNK_STAT:
	case MBX_DUMP_MEMORY:
	case MBX_DOWN_LOAD:
	case MBX_UPDATE_CFG:
	case MBX_KILL_BOARD:
3487
	case MBX_READ_TOPOLOGY:
3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510
	case MBX_LOAD_AREA:
	case MBX_LOAD_EXP_ROM:
	case MBX_BEACON:
	case MBX_DEL_LD_ENTRY:
	case MBX_SET_DEBUG:
	case MBX_WRITE_WWN:
	case MBX_SLI4_CONFIG:
	case MBX_READ_EVENT_LOG:
	case MBX_READ_EVENT_LOG_STATUS:
	case MBX_WRITE_EVENT_LOG:
	case MBX_PORT_CAPABILITIES:
	case MBX_PORT_IOV_CONTROL:
	case MBX_RUN_BIU_DIAG64:
		break;
	case MBX_SET_VARIABLE:
		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
			"1226 mbox: set_variable 0x%x, 0x%x\n",
			mb->un.varWords[0],
			mb->un.varWords[1]);
		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
			&& (mb->un.varWords[1] == 1)) {
			phba->wait_4_mlo_maint_flg = 1;
		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
3511
			spin_lock_irq(&phba->hbalock);
3512
			phba->link_flag &= ~LS_LOOPBACK_MODE;
3513
			spin_unlock_irq(&phba->hbalock);
3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564
			phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
		}
		break;
	case MBX_READ_SPARM64:
	case MBX_REG_LOGIN:
	case MBX_REG_LOGIN64:
	case MBX_CONFIG_PORT:
	case MBX_RUN_BIU_DIAG:
	default:
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
			"2742 Unknown Command 0x%x\n",
			mb->mbxCommand);
		return -EPERM;
	}

	return 0; /* ok */
}

/**
 * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
 * @phba: Pointer to HBA context object.
 *
 * This is routine clean up and reset BSG handling of multi-buffer mbox
 * command session.
 **/
static void
lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
{
	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
		return;

	/* free all memory, including dma buffers */
	lpfc_bsg_dma_page_list_free(phba,
				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
	/* multi-buffer write mailbox command pass-through complete */
	memset((char *)&phba->mbox_ext_buf_ctx, 0,
	       sizeof(struct lpfc_mbox_ext_buf_ctx));
	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);

	return;
}

/**
 * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to mailbox command.
 *
 * This is routine handles BSG job for mailbox commands completions with
 * multiple external buffers.
 **/
3565
static struct bsg_job *
3566 3567 3568
lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
{
	struct bsg_job_data *dd_data;
3569
	struct bsg_job *job;
3570
	struct fc_bsg_reply *bsg_reply;
3571 3572 3573 3574
	uint8_t *pmb, *pmb_buf;
	unsigned long flags;
	uint32_t size;
	int rc = 0;
3575 3576 3577
	struct lpfc_dmabuf *dmabuf;
	struct lpfc_sli_config_mbox *sli_cfg_mbx;
	uint8_t *pmbx;
3578 3579

	dd_data = pmboxq->context1;
3580 3581 3582 3583 3584

	/* Determine if job has been aborted */
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	job = dd_data->set_job;
	if (job) {
3585
		bsg_reply = job->reply;
3586 3587
		/* Prevent timeout handling from trying to abort job  */
		job->dd_data = NULL;
3588
	}
3589
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3590 3591 3592 3593 3594

	/*
	 * The outgoing buffer is readily referred from the dma buffer,
	 * just need to get header part from mailboxq structure.
	 */
3595

3596 3597
	pmb = (uint8_t *)&pmboxq->u.mb;
	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3598
	/* Copy the byte swapped response mailbox back to the user */
3599
	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610
	/* if there is any non-embedded extended data copy that too */
	dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
		pmbx = (uint8_t *)dmabuf->virt;
		/* byte swap the extended data following the mailbox command */
		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
			&pmbx[sizeof(MAILBOX_t)],
			sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
	}
3611

3612 3613
	/* Complete the job if the job is still active */

3614 3615
	if (job) {
		size = job->reply_payload.payload_len;
3616
		bsg_reply->reply_payload_rcv_len =
3617 3618 3619
			sg_copy_from_buffer(job->reply_payload.sg_list,
					    job->reply_payload.sg_cnt,
					    pmb_buf, size);
3620

3621
		/* result for successful */
3622
		bsg_reply->result = 0;
3623

3624
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3625
				"2937 SLI_CONFIG ext-buffer mailbox command "
3626 3627 3628
				"(x%x/x%x) complete bsg job done, bsize:%d\n",
				phba->mbox_ext_buf_ctx.nembType,
				phba->mbox_ext_buf_ctx.mboxType, size);
3629 3630 3631 3632 3633
		lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
					phba->mbox_ext_buf_ctx.nembType,
					phba->mbox_ext_buf_ctx.mboxType,
					dma_ebuf, sta_pos_addr,
					phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3634
	} else {
3635
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3636
				"2938 SLI_CONFIG ext-buffer mailbox "
3637 3638 3639
				"command (x%x/x%x) failure, rc:x%x\n",
				phba->mbox_ext_buf_ctx.nembType,
				phba->mbox_ext_buf_ctx.mboxType, rc);
3640 3641 3642
	}


3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659
	/* state change */
	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
	kfree(dd_data);
	return job;
}

/**
 * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to mailbox command.
 *
 * This is completion handler function for mailbox read commands with multiple
 * external buffers.
 **/
static void
lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
{
3660
	struct bsg_job *job;
3661
	struct fc_bsg_reply *bsg_reply;
3662

3663 3664
	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);

3665
	/* handle the BSG job with mailbox command */
3666
	if (!job)
3667 3668 3669
		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;

	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3670
			"2939 SLI_CONFIG ext-buffer rd mailbox command "
3671 3672 3673 3674 3675 3676 3677 3678 3679
			"complete, ctxState:x%x, mbxStatus:x%x\n",
			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);

	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
		lpfc_bsg_mbox_ext_session_reset(phba);

	/* free base driver mailbox structure memory */
	mempool_free(pmboxq, phba->mbox_mem_pool);

3680
	/* if the job is still active, call job done */
3681 3682
	if (job) {
		bsg_reply = job->reply;
J
Johannes Thumshirn 已提交
3683
		bsg_job_done(job, bsg_reply->result,
3684 3685
			       bsg_reply->reply_payload_rcv_len);
	}
3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699
	return;
}

/**
 * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
 * @phba: Pointer to HBA context object.
 * @pmboxq: Pointer to mailbox command.
 *
 * This is completion handler function for mailbox write commands with multiple
 * external buffers.
 **/
static void
lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
{
3700
	struct bsg_job *job;
3701
	struct fc_bsg_reply *bsg_reply;
3702

3703 3704
	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);

3705
	/* handle the BSG job with the mailbox command */
3706
	if (!job)
3707 3708 3709
		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;

	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3710
			"2940 SLI_CONFIG ext-buffer wr mailbox command "
3711 3712 3713 3714 3715 3716 3717
			"complete, ctxState:x%x, mbxStatus:x%x\n",
			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);

	/* free all memory, including dma buffers */
	mempool_free(pmboxq, phba->mbox_mem_pool);
	lpfc_bsg_mbox_ext_session_reset(phba);

3718
	/* if the job is still active, call job done */
3719 3720
	if (job) {
		bsg_reply = job->reply;
J
Johannes Thumshirn 已提交
3721
		bsg_job_done(job, bsg_reply->result,
3722 3723
			       bsg_reply->reply_payload_rcv_len);
	}
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831

	return;
}

static void
lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
				struct lpfc_dmabuf *ext_dmabuf)
{
	struct lpfc_sli_config_mbox *sli_cfg_mbx;

	/* pointer to the start of mailbox command */
	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;

	if (nemb_tp == nemb_mse) {
		if (index == 0) {
			sli_cfg_mbx->un.sli_config_emb0_subsys.
				mse[index].pa_hi =
				putPaddrHigh(mbx_dmabuf->phys +
					     sizeof(MAILBOX_t));
			sli_cfg_mbx->un.sli_config_emb0_subsys.
				mse[index].pa_lo =
				putPaddrLow(mbx_dmabuf->phys +
					    sizeof(MAILBOX_t));
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"2943 SLI_CONFIG(mse)[%d], "
					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
					index,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].buf_len,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].pa_hi,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].pa_lo);
		} else {
			sli_cfg_mbx->un.sli_config_emb0_subsys.
				mse[index].pa_hi =
				putPaddrHigh(ext_dmabuf->phys);
			sli_cfg_mbx->un.sli_config_emb0_subsys.
				mse[index].pa_lo =
				putPaddrLow(ext_dmabuf->phys);
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"2944 SLI_CONFIG(mse)[%d], "
					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
					index,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].buf_len,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].pa_hi,
					sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[index].pa_lo);
		}
	} else {
		if (index == 0) {
			sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_hi =
				putPaddrHigh(mbx_dmabuf->phys +
					     sizeof(MAILBOX_t));
			sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_lo =
				putPaddrLow(mbx_dmabuf->phys +
					    sizeof(MAILBOX_t));
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3007 SLI_CONFIG(hbd)[%d], "
					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
				index,
				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
				&sli_cfg_mbx->un.
				sli_config_emb1_subsys.hbd[index]),
				sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_hi,
				sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_lo);

		} else {
			sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_hi =
				putPaddrHigh(ext_dmabuf->phys);
			sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_lo =
				putPaddrLow(ext_dmabuf->phys);
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"3008 SLI_CONFIG(hbd)[%d], "
					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
				index,
				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
				&sli_cfg_mbx->un.
				sli_config_emb1_subsys.hbd[index]),
				sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_hi,
				sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[index].pa_lo);
		}
	}
	return;
}

/**
 * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a BSG mailbox object.
 * @nemb_tp: Enumerate of non-embedded mailbox command type.
 * @dmabuff: Pointer to a DMA buffer descriptor.
 *
 * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
 * non-embedded external bufffers.
 **/
static int
3832
lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3833 3834 3835
			      enum nemb_type nemb_tp,
			      struct lpfc_dmabuf *dmabuf)
{
3836
	struct fc_bsg_request *bsg_request = job->request;
3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848
	struct lpfc_sli_config_mbox *sli_cfg_mbx;
	struct dfc_mbox_req *mbox_req;
	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
	uint32_t ext_buf_cnt, ext_buf_index;
	struct lpfc_dmabuf *ext_dmabuf = NULL;
	struct bsg_job_data *dd_data = NULL;
	LPFC_MBOXQ_t *pmboxq = NULL;
	MAILBOX_t *pmb;
	uint8_t *pmbx;
	int rc, i;

	mbox_req =
3849
	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870

	/* pointer to the start of mailbox command */
	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;

	if (nemb_tp == nemb_mse) {
		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
					"2945 Handled SLI_CONFIG(mse) rd, "
					"ext_buf_cnt(%d) out of range(%d)\n",
					ext_buf_cnt,
					LPFC_MBX_SLI_CONFIG_MAX_MSE);
			rc = -ERANGE;
			goto job_error;
		}
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2941 Handled SLI_CONFIG(mse) rd, "
				"ext_buf_cnt:%d\n", ext_buf_cnt);
	} else {
		/* sanity check on interface type for support */
3871
		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891
		    LPFC_SLI_INTF_IF_TYPE_2) {
			rc = -ENODEV;
			goto job_error;
		}
		/* nemb_tp == nemb_hbd */
		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
					"2946 Handled SLI_CONFIG(hbd) rd, "
					"ext_buf_cnt(%d) out of range(%d)\n",
					ext_buf_cnt,
					LPFC_MBX_SLI_CONFIG_MAX_HBD);
			rc = -ERANGE;
			goto job_error;
		}
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2942 Handled SLI_CONFIG(hbd) rd, "
				"ext_buf_cnt:%d\n", ext_buf_cnt);
	}

3892 3893 3894 3895
	/* before dma descriptor setup */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
					sta_pre_addr, dmabuf, ext_buf_cnt);

3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942
	/* reject non-embedded mailbox command with none external buffer */
	if (ext_buf_cnt == 0) {
		rc = -EPERM;
		goto job_error;
	} else if (ext_buf_cnt > 1) {
		/* additional external read buffers */
		for (i = 1; i < ext_buf_cnt; i++) {
			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
			if (!ext_dmabuf) {
				rc = -ENOMEM;
				goto job_error;
			}
			list_add_tail(&ext_dmabuf->list,
				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
		}
	}

	/* bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		rc = -ENOMEM;
		goto job_error;
	}

	/* mailbox command structure for base driver */
	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmboxq) {
		rc = -ENOMEM;
		goto job_error;
	}
	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));

	/* for the first external buffer */
	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);

	/* for the rest of external buffer descriptors if any */
	if (ext_buf_cnt > 1) {
		ext_buf_index = 1;
		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
						ext_buf_index, dmabuf,
						curr_dmabuf);
			ext_buf_index++;
		}
	}

3943 3944 3945 3946
	/* after dma descriptor setup */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
					sta_pos_addr, dmabuf, ext_buf_cnt);

3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967
	/* construct base driver mbox command */
	pmb = &pmboxq->u.mb;
	pmbx = (uint8_t *)dmabuf->virt;
	memcpy(pmb, pmbx, sizeof(*pmb));
	pmb->mbxOwner = OWN_HOST;
	pmboxq->vport = phba->pport;

	/* multi-buffer handling context */
	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;

	/* callback for multi-buffer read mailbox command */
	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;

	/* context fields to callback function */
	pmboxq->context1 = dd_data;
	dd_data->type = TYPE_MBOX;
3968
	dd_data->set_job = job;
3969 3970 3971 3972 3973 3974 3975
	dd_data->context_un.mbox.pmboxq = pmboxq;
	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
	job->dd_data = dd_data;

	/* state change */
	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;

3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987
	/*
	 * Non-embedded mailbox subcommand data gets byte swapped here because
	 * the lower level driver code only does the first 64 mailbox words.
	 */
	if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
		(nemb_tp == nemb_mse))
		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
			&pmbx[sizeof(MAILBOX_t)],
				sli_cfg_mbx->un.sli_config_emb0_subsys.
					mse[0].buf_len);

3988 3989 3990 3991
	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2947 Issued SLI_CONFIG ext-buffer "
3992
				"mailbox command, rc:x%x\n", rc);
3993
		return SLI_CONFIG_HANDLED;
3994 3995 3996
	}
	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
			"2948 Failed to issue SLI_CONFIG ext-buffer "
3997
			"mailbox command, rc:x%x\n", rc);
3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019
	rc = -EPIPE;

job_error:
	if (pmboxq)
		mempool_free(pmboxq, phba->mbox_mem_pool);
	lpfc_bsg_dma_page_list_free(phba,
				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
	kfree(dd_data);
	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
	return rc;
}

/**
 * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a BSG mailbox object.
 * @dmabuff: Pointer to a DMA buffer descriptor.
 *
 * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
 * non-embedded external bufffers.
 **/
static int
4020
lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
4021 4022 4023
			       enum nemb_type nemb_tp,
			       struct lpfc_dmabuf *dmabuf)
{
4024 4025
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
4026 4027 4028 4029 4030 4031 4032
	struct dfc_mbox_req *mbox_req;
	struct lpfc_sli_config_mbox *sli_cfg_mbx;
	uint32_t ext_buf_cnt;
	struct bsg_job_data *dd_data = NULL;
	LPFC_MBOXQ_t *pmboxq = NULL;
	MAILBOX_t *pmb;
	uint8_t *mbx;
4033
	int rc = SLI_CONFIG_NOT_HANDLED, i;
4034 4035

	mbox_req =
4036
	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4037 4038 4039 4040 4041 4042 4043 4044 4045

	/* pointer to the start of mailbox command */
	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;

	if (nemb_tp == nemb_mse) {
		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4046
					"2953 Failed SLI_CONFIG(mse) wr, "
4047 4048 4049 4050 4051 4052 4053 4054 4055 4056
					"ext_buf_cnt(%d) out of range(%d)\n",
					ext_buf_cnt,
					LPFC_MBX_SLI_CONFIG_MAX_MSE);
			return -ERANGE;
		}
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2949 Handled SLI_CONFIG(mse) wr, "
				"ext_buf_cnt:%d\n", ext_buf_cnt);
	} else {
		/* sanity check on interface type for support */
4057
		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4058 4059 4060 4061 4062 4063
		    LPFC_SLI_INTF_IF_TYPE_2)
			return -ENODEV;
		/* nemb_tp == nemb_hbd */
		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4064
					"2954 Failed SLI_CONFIG(hbd) wr, "
4065 4066 4067 4068 4069 4070 4071 4072 4073 4074
					"ext_buf_cnt(%d) out of range(%d)\n",
					ext_buf_cnt,
					LPFC_MBX_SLI_CONFIG_MAX_HBD);
			return -ERANGE;
		}
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2950 Handled SLI_CONFIG(hbd) wr, "
				"ext_buf_cnt:%d\n", ext_buf_cnt);
	}

4075 4076 4077 4078
	/* before dma buffer descriptor setup */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
					sta_pre_addr, dmabuf, ext_buf_cnt);

4079 4080 4081 4082 4083 4084
	if (ext_buf_cnt == 0)
		return -EPERM;

	/* for the first external buffer */
	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);

4085 4086 4087 4088
	/* after dma descriptor setup */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
					sta_pos_addr, dmabuf, ext_buf_cnt);

4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138
	/* log for looking forward */
	for (i = 1; i < ext_buf_cnt; i++) {
		if (nemb_tp == nemb_mse)
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
				mse[i].buf_len);
		else
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
				&sli_cfg_mbx->un.sli_config_emb1_subsys.
				hbd[i]));
	}

	/* multi-buffer handling context */
	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;

	if (ext_buf_cnt == 1) {
		/* bsg tracking structure */
		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
		if (!dd_data) {
			rc = -ENOMEM;
			goto job_error;
		}

		/* mailbox command structure for base driver */
		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
		if (!pmboxq) {
			rc = -ENOMEM;
			goto job_error;
		}
		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
		pmb = &pmboxq->u.mb;
		mbx = (uint8_t *)dmabuf->virt;
		memcpy(pmb, mbx, sizeof(*pmb));
		pmb->mbxOwner = OWN_HOST;
		pmboxq->vport = phba->pport;

		/* callback for multi-buffer read mailbox command */
		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;

		/* context fields to callback function */
		pmboxq->context1 = dd_data;
		dd_data->type = TYPE_MBOX;
4139
		dd_data->set_job = job;
4140 4141 4142 4143 4144 4145
		dd_data->context_un.mbox.pmboxq = pmboxq;
		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
		job->dd_data = dd_data;

		/* state change */

4146
		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4147 4148 4149 4150
		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"2955 Issued SLI_CONFIG ext-buffer "
4151
					"mailbox command, rc:x%x\n", rc);
4152
			return SLI_CONFIG_HANDLED;
4153 4154 4155
		}
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"2956 Failed to issue SLI_CONFIG ext-buffer "
4156
				"mailbox command, rc:x%x\n", rc);
4157
		rc = -EPIPE;
4158
		goto job_error;
4159 4160
	}

4161
	/* wait for additoinal external buffers */
4162

4163
	bsg_reply->result = 0;
J
Johannes Thumshirn 已提交
4164
	bsg_job_done(job, bsg_reply->result,
4165
		       bsg_reply->reply_payload_rcv_len);
4166 4167
	return SLI_CONFIG_HANDLED;

4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186
job_error:
	if (pmboxq)
		mempool_free(pmboxq, phba->mbox_mem_pool);
	kfree(dd_data);

	return rc;
}

/**
 * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a BSG mailbox object.
 * @dmabuff: Pointer to a DMA buffer descriptor.
 *
 * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
 * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
 * with embedded sussystem 0x1 and opcodes with external HBDs.
 **/
static int
4187
lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4188 4189 4190 4191 4192 4193 4194
			     struct lpfc_dmabuf *dmabuf)
{
	struct lpfc_sli_config_mbox *sli_cfg_mbx;
	uint32_t subsys;
	uint32_t opcode;
	int rc = SLI_CONFIG_NOT_HANDLED;

4195
	/* state change on new multi-buffer pass-through mailbox command */
4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208
	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;

	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;

	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
			switch (opcode) {
			case FCOE_OPCODE_READ_FCF:
4209
			case FCOE_OPCODE_GET_DPORT_RESULTS:
4210 4211 4212 4213 4214 4215 4216 4217
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"2957 Handled SLI_CONFIG "
						"subsys_fcoe, opcode:x%x\n",
						opcode);
				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
							nemb_mse, dmabuf);
				break;
			case FCOE_OPCODE_ADD_FCF:
4218 4219
			case FCOE_OPCODE_SET_DPORT_MODE:
			case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
4220 4221 4222 4223 4224 4225 4226 4227 4228
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"2958 Handled SLI_CONFIG "
						"subsys_fcoe, opcode:x%x\n",
						opcode);
				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
							nemb_mse, dmabuf);
				break;
			default:
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4229
						"2959 Reject SLI_CONFIG "
4230 4231
						"subsys_fcoe, opcode:x%x\n",
						opcode);
4232 4233 4234 4235 4236 4237
				rc = -EPERM;
				break;
			}
		} else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
			switch (opcode) {
			case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4238
			case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
J
James Smart 已提交
4239
			case COMN_OPCODE_GET_PROFILE_CONFIG:
4240 4241
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"3106 Handled SLI_CONFIG "
4242
						"subsys_comn, opcode:x%x\n",
4243 4244 4245 4246 4247 4248 4249
						opcode);
				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
							nemb_mse, dmabuf);
				break;
			default:
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"3107 Reject SLI_CONFIG "
4250
						"subsys_comn, opcode:x%x\n",
4251 4252
						opcode);
				rc = -EPERM;
4253 4254 4255 4256
				break;
			}
		} else {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4257
					"2977 Reject SLI_CONFIG "
4258 4259
					"subsys:x%d, opcode:x%x\n",
					subsys, opcode);
4260
			rc = -EPERM;
4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295
		}
	} else {
		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
			switch (opcode) {
			case COMN_OPCODE_READ_OBJECT:
			case COMN_OPCODE_READ_OBJECT_LIST:
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"2960 Handled SLI_CONFIG "
						"subsys_comn, opcode:x%x\n",
						opcode);
				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
							nemb_hbd, dmabuf);
				break;
			case COMN_OPCODE_WRITE_OBJECT:
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"2961 Handled SLI_CONFIG "
						"subsys_comn, opcode:x%x\n",
						opcode);
				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
							nemb_hbd, dmabuf);
				break;
			default:
				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
						"2962 Not handled SLI_CONFIG "
						"subsys_comn, opcode:x%x\n",
						opcode);
				rc = SLI_CONFIG_NOT_HANDLED;
				break;
			}
		} else {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4296
					"2978 Not handled SLI_CONFIG "
4297 4298 4299 4300 4301
					"subsys:x%d, opcode:x%x\n",
					subsys, opcode);
			rc = SLI_CONFIG_NOT_HANDLED;
		}
	}
4302 4303 4304 4305 4306

	/* state reset on not handled new multi-buffer mailbox command */
	if (rc != SLI_CONFIG_HANDLED)
		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;

4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335
	return rc;
}

/**
 * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
 * @phba: Pointer to HBA context object.
 *
 * This routine is for requesting to abort a pass-through mailbox command with
 * multiple external buffers due to error condition.
 **/
static void
lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
{
	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
	else
		lpfc_bsg_mbox_ext_session_reset(phba);
	return;
}

/**
 * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
 * @phba: Pointer to HBA context object.
 * @dmabuf: Pointer to a DMA buffer descriptor.
 *
 * This routine extracts the next mailbox read external buffer back to
 * user space through BSG.
 **/
static int
4336
lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
4337
{
4338
	struct fc_bsg_reply *bsg_reply = job->reply;
4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368
	struct lpfc_sli_config_mbox *sli_cfg_mbx;
	struct lpfc_dmabuf *dmabuf;
	uint8_t *pbuf;
	uint32_t size;
	uint32_t index;

	index = phba->mbox_ext_buf_ctx.seqNum;
	phba->mbox_ext_buf_ctx.seqNum++;

	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;

	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2963 SLI_CONFIG (mse) ext-buffer rd get "
				"buffer[%d], size:%d\n", index, size);
	} else {
		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
				"buffer[%d], size:%d\n", index, size);
	}
	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
		return -EPIPE;
	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
				  struct lpfc_dmabuf, list);
	list_del_init(&dmabuf->list);
4369 4370 4371 4372 4373 4374

	/* after dma buffer descriptor setup */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
					mbox_rd, dma_ebuf, sta_pos_addr,
					dmabuf, index);

4375
	pbuf = (uint8_t *)dmabuf->virt;
4376
	bsg_reply->reply_payload_rcv_len =
4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389
		sg_copy_from_buffer(job->reply_payload.sg_list,
				    job->reply_payload.sg_cnt,
				    pbuf, size);

	lpfc_bsg_dma_page_free(phba, dmabuf);

	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
				"command session done\n");
		lpfc_bsg_mbox_ext_session_reset(phba);
	}

4390
	bsg_reply->result = 0;
J
Johannes Thumshirn 已提交
4391
	bsg_job_done(job, bsg_reply->result,
4392
		       bsg_reply->reply_payload_rcv_len);
4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405

	return SLI_CONFIG_HANDLED;
}

/**
 * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
 * @phba: Pointer to HBA context object.
 * @dmabuf: Pointer to a DMA buffer descriptor.
 *
 * This routine sets up the next mailbox read external buffer obtained
 * from user space through BSG.
 **/
static int
4406
lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
4407 4408
			struct lpfc_dmabuf *dmabuf)
{
4409
	struct fc_bsg_reply *bsg_reply = job->reply;
4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454
	struct bsg_job_data *dd_data = NULL;
	LPFC_MBOXQ_t *pmboxq = NULL;
	MAILBOX_t *pmb;
	enum nemb_type nemb_tp;
	uint8_t *pbuf;
	uint32_t size;
	uint32_t index;
	int rc;

	index = phba->mbox_ext_buf_ctx.seqNum;
	phba->mbox_ext_buf_ctx.seqNum++;
	nemb_tp = phba->mbox_ext_buf_ctx.nembType;

	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		rc = -ENOMEM;
		goto job_error;
	}

	pbuf = (uint8_t *)dmabuf->virt;
	size = job->request_payload.payload_len;
	sg_copy_to_buffer(job->request_payload.sg_list,
			  job->request_payload.sg_cnt,
			  pbuf, size);

	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2966 SLI_CONFIG (mse) ext-buffer wr set "
				"buffer[%d], size:%d\n",
				phba->mbox_ext_buf_ctx.seqNum, size);

	} else {
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
				"buffer[%d], size:%d\n",
				phba->mbox_ext_buf_ctx.seqNum, size);

	}

	/* set up external buffer descriptor and add to external buffer list */
	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
					phba->mbox_ext_buf_ctx.mbx_dmabuf,
					dmabuf);
	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);

4455 4456 4457 4458 4459
	/* after write dma buffer */
	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
					mbox_wr, dma_ebuf, sta_pos_addr,
					dmabuf, index);

4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483
	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
				"2968 SLI_CONFIG ext-buffer wr all %d "
				"ebuffers received\n",
				phba->mbox_ext_buf_ctx.numBuf);
		/* mailbox command structure for base driver */
		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
		if (!pmboxq) {
			rc = -ENOMEM;
			goto job_error;
		}
		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
		pmb = &pmboxq->u.mb;
		memcpy(pmb, pbuf, sizeof(*pmb));
		pmb->mbxOwner = OWN_HOST;
		pmboxq->vport = phba->pport;

		/* callback for multi-buffer write mailbox command */
		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;

		/* context fields to callback function */
		pmboxq->context1 = dd_data;
		dd_data->type = TYPE_MBOX;
4484
		dd_data->set_job = job;
4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495
		dd_data->context_un.mbox.pmboxq = pmboxq;
		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
		job->dd_data = dd_data;

		/* state change */
		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;

		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"2969 Issued SLI_CONFIG ext-buffer "
4496
					"mailbox command, rc:x%x\n", rc);
4497
			return SLI_CONFIG_HANDLED;
4498 4499 4500
		}
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"2970 Failed to issue SLI_CONFIG ext-buffer "
4501
				"mailbox command, rc:x%x\n", rc);
4502 4503 4504 4505 4506
		rc = -EPIPE;
		goto job_error;
	}

	/* wait for additoinal external buffers */
4507
	bsg_reply->result = 0;
J
Johannes Thumshirn 已提交
4508
	bsg_job_done(job, bsg_reply->result,
4509
		       bsg_reply->reply_payload_rcv_len);
4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528
	return SLI_CONFIG_HANDLED;

job_error:
	lpfc_bsg_dma_page_free(phba, dmabuf);
	kfree(dd_data);

	return rc;
}

/**
 * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a BSG mailbox object.
 * @dmabuff: Pointer to a DMA buffer descriptor.
 *
 * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
 * command with multiple non-embedded external buffers.
 **/
static int
4529
lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559
			     struct lpfc_dmabuf *dmabuf)
{
	int rc;

	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
			"2971 SLI_CONFIG buffer (type:x%x)\n",
			phba->mbox_ext_buf_ctx.mboxType);

	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
					"2972 SLI_CONFIG rd buffer state "
					"mismatch:x%x\n",
					phba->mbox_ext_buf_ctx.state);
			lpfc_bsg_mbox_ext_abort(phba);
			return -EPIPE;
		}
		rc = lpfc_bsg_read_ebuf_get(phba, job);
		if (rc == SLI_CONFIG_HANDLED)
			lpfc_bsg_dma_page_free(phba, dmabuf);
	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
					"2973 SLI_CONFIG wr buffer state "
					"mismatch:x%x\n",
					phba->mbox_ext_buf_ctx.state);
			lpfc_bsg_mbox_ext_abort(phba);
			return -EPIPE;
		}
		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4560
	}
4561
	return rc;
4562 4563 4564
}

/**
4565
 * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4566
 * @phba: Pointer to HBA context object.
4567 4568
 * @mb: Pointer to a BSG mailbox object.
 * @dmabuff: Pointer to a DMA buffer descriptor.
4569
 *
4570 4571
 * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
 * (0x9B) mailbox commands and external buffers.
4572
 **/
4573
static int
4574
lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
4575
			    struct lpfc_dmabuf *dmabuf)
4576
{
4577
	struct fc_bsg_request *bsg_request = job->request;
4578
	struct dfc_mbox_req *mbox_req;
4579
	int rc = SLI_CONFIG_NOT_HANDLED;
4580 4581

	mbox_req =
4582
	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4583 4584 4585

	/* mbox command with/without single external buffer */
	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4586
		return rc;
4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598

	/* mbox command and first external buffer */
	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
		if (mbox_req->extSeqNum == 1) {
			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
					"2974 SLI_CONFIG mailbox: tag:%d, "
					"seq:%d\n", mbox_req->extMboxTag,
					mbox_req->extSeqNum);
			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
			return rc;
		} else
			goto sli_cfg_ext_error;
4599 4600
	}

4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635
	/*
	 * handle additional external buffers
	 */

	/* check broken pipe conditions */
	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
		goto sli_cfg_ext_error;
	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
		goto sli_cfg_ext_error;
	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
		goto sli_cfg_ext_error;

	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
			"2975 SLI_CONFIG mailbox external buffer: "
			"extSta:x%x, tag:%d, seq:%d\n",
			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
			mbox_req->extSeqNum);
	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
	return rc;

sli_cfg_ext_error:
	/* all other cases, broken pipe */
	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
			"2976 SLI_CONFIG mailbox broken pipe: "
			"ctxSta:x%x, ctxNumBuf:%d "
			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
			phba->mbox_ext_buf_ctx.state,
			phba->mbox_ext_buf_ctx.numBuf,
			phba->mbox_ext_buf_ctx.mbxTag,
			phba->mbox_ext_buf_ctx.seqNum,
			mbox_req->extMboxTag, mbox_req->extSeqNum);

	lpfc_bsg_mbox_ext_session_reset(phba);

	return -EPIPE;
4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650
}

/**
 * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
 * @phba: Pointer to HBA context object.
 * @mb: Pointer to a mailbox object.
 * @vport: Pointer to a vport object.
 *
 * Allocate a tracking object, mailbox command memory, get a mailbox
 * from the mailbox pool, copy the caller mailbox command.
 *
 * If offline and the sli is active we need to poll for the command (port is
 * being reset) and com-plete the job, otherwise issue the mailbox command and
 * let our completion handler finish the command.
 **/
4651
static int
4652
lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4653 4654
	struct lpfc_vport *vport)
{
4655 4656
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
4657 4658 4659
	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
	/* a 4k buffer to hold the mb and extended data from/to the bsg */
4660
	uint8_t *pmbx = NULL;
4661
	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4662 4663
	struct lpfc_dmabuf *dmabuf = NULL;
	struct dfc_mbox_req *mbox_req;
4664 4665
	struct READ_EVENT_LOG_VAR *rdEventLog;
	uint32_t transmit_length, receive_length, mode;
4666
	struct lpfc_mbx_sli4_config *sli4_config;
4667 4668
	struct lpfc_mbx_nembed_cmd *nembed_sge;
	struct ulp_bde64 *bde;
4669
	uint8_t *ext = NULL;
4670
	int rc = 0;
4671
	uint8_t *from;
4672 4673
	uint32_t size;

4674
	/* in case no data is transferred */
4675
	bsg_reply->reply_payload_rcv_len = 0;
4676

4677 4678 4679 4680 4681 4682 4683
	/* sanity check to protect driver */
	if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
	    job->request_payload.payload_len > BSG_MBOX_SIZE) {
		rc = -ERANGE;
		goto job_done;
	}

4684 4685 4686 4687 4688 4689 4690 4691 4692 4693
	/*
	 * Don't allow mailbox commands to be sent when blocked or when in
	 * the middle of discovery
	 */
	 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
		rc = -EAGAIN;
		goto job_done;
	}

	mbox_req =
4694
	    (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4695

4696
	/* check if requested extended data lengths are valid */
4697 4698
	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4699 4700 4701
		rc = -ERANGE;
		goto job_done;
	}
4702

4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
	dmabuf = lpfc_bsg_dma_page_alloc(phba);
	if (!dmabuf || !dmabuf->virt) {
		rc = -ENOMEM;
		goto job_done;
	}

	/* Get the mailbox command or external buffer from BSG */
	pmbx = (uint8_t *)dmabuf->virt;
	size = job->request_payload.payload_len;
	sg_copy_to_buffer(job->request_payload.sg_list,
			  job->request_payload.sg_cnt, pmbx, size);

	/* Handle possible SLI_CONFIG with non-embedded payloads */
	if (phba->sli_rev == LPFC_SLI_REV4) {
		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
		if (rc == SLI_CONFIG_HANDLED)
			goto job_cont;
		if (rc)
			goto job_done;
		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
	}

	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
	if (rc != 0)
		goto job_done; /* must be negative */

4729 4730 4731 4732 4733
	/* allocate our bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2727 Failed allocation of dd_data\n");
4734 4735
		rc = -ENOMEM;
		goto job_done;
4736 4737 4738 4739
	}

	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
	if (!pmboxq) {
4740 4741
		rc = -ENOMEM;
		goto job_done;
4742
	}
4743
	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4744 4745

	pmb = &pmboxq->u.mb;
4746
	memcpy(pmb, pmbx, sizeof(*pmb));
4747 4748 4749
	pmb->mbxOwner = OWN_HOST;
	pmboxq->vport = vport;

4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762
	/* If HBA encountered an error attention, allow only DUMP
	 * or RESTART mailbox commands until the HBA is restarted.
	 */
	if (phba->pport->stopped &&
	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
	    pmb->mbxCommand != MBX_RESTART &&
	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
	    pmb->mbxCommand != MBX_WRITE_WWN)
		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
				"2797 mbox: Issued mailbox cmd "
				"0x%x while in stopped state.\n",
				pmb->mbxCommand);

4763
	/* extended mailbox commands will need an extended buffer */
4764
	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4765 4766
		from = pmbx;
		ext = from + sizeof(MAILBOX_t);
4767 4768 4769 4770
		pmboxq->context2 = ext;
		pmboxq->in_ext_byte_len =
			mbox_req->inExtWLen * sizeof(uint32_t);
		pmboxq->out_ext_byte_len =
4771
			mbox_req->outExtWLen * sizeof(uint32_t);
4772 4773 4774 4775 4776 4777 4778 4779
		pmboxq->mbox_offset_word = mbox_req->mbOffset;
	}

	/* biu diag will need a kernel buffer to transfer the data
	 * allocate our own buffer and setup the mailbox command to
	 * use ours
	 */
	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4780 4781
		transmit_length = pmb->un.varWords[1];
		receive_length = pmb->un.varWords[4];
4782 4783 4784 4785
		/* transmit length cannot be greater than receive length or
		 * mailbox extension size
		 */
		if ((transmit_length > receive_length) ||
4786
			(transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4787 4788 4789
			rc = -ERANGE;
			goto job_done;
		}
4790
		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4791
			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4792
		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4793
			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4794 4795

		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4796 4797
			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4798
		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4799 4800
			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4801
	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4802 4803 4804
		rdEventLog = &pmb->un.varRdEventLog;
		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
		mode = bf_get(lpfc_event_log, rdEventLog);
4805 4806 4807 4808

		/* receive length cannot be greater than mailbox
		 * extension size
		 */
4809
		if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4810 4811 4812 4813 4814 4815
			rc = -ERANGE;
			goto job_done;
		}

		/* mode zero uses a bde like biu diags command */
		if (mode == 0) {
4816 4817 4818 4819
			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
							+ sizeof(MAILBOX_t));
			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
							+ sizeof(MAILBOX_t));
4820 4821
		}
	} else if (phba->sli_rev == LPFC_SLI_REV4) {
4822 4823 4824 4825 4826 4827
		/* Let type 4 (well known data) through because the data is
		 * returned in varwords[4-8]
		 * otherwise check the recieve length and fetch the buffer addr
		 */
		if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
			(pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4828 4829 4830
			/* rebuild the command for sli4 using our own buffers
			* like we do for biu diags
			*/
4831
			receive_length = pmb->un.varWords[2];
4832 4833 4834
			/* receive length cannot be greater than mailbox
			 * extension size
			 */
4835
			if (receive_length == 0) {
4836 4837 4838
				rc = -ERANGE;
				goto job_done;
			}
4839 4840 4841 4842
			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
						+ sizeof(MAILBOX_t));
			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
						+ sizeof(MAILBOX_t));
4843 4844
		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
			pmb->un.varUpdateCfg.co) {
4845
			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4846 4847

			/* bde size cannot be greater than mailbox ext size */
4848 4849
			if (bde->tus.f.bdeSize >
			    BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4850 4851 4852
				rc = -ERANGE;
				goto job_done;
			}
4853 4854 4855 4856
			bde->addrHigh = putPaddrHigh(dmabuf->phys
						+ sizeof(MAILBOX_t));
			bde->addrLow = putPaddrLow(dmabuf->phys
						+ sizeof(MAILBOX_t));
J
James Smart 已提交
4857
		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872
			/* Handling non-embedded SLI_CONFIG mailbox command */
			sli4_config = &pmboxq->u.mqe.un.sli4_config;
			if (!bf_get(lpfc_mbox_hdr_emb,
			    &sli4_config->header.cfg_mhdr)) {
				/* rebuild the command for sli4 using our
				 * own buffers like we do for biu diags
				 */
				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
						&pmb->un.varWords[0];
				receive_length = nembed_sge->sge[0].length;

				/* receive length cannot be greater than
				 * mailbox extension size
				 */
				if ((receive_length == 0) ||
4873 4874
				    (receive_length >
				     BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4875 4876 4877
					rc = -ERANGE;
					goto job_done;
				}
J
James Smart 已提交
4878

4879 4880 4881 4882 4883 4884
				nembed_sge->sge[0].pa_hi =
						putPaddrHigh(dmabuf->phys
						   + sizeof(MAILBOX_t));
				nembed_sge->sge[0].pa_lo =
						putPaddrLow(dmabuf->phys
						   + sizeof(MAILBOX_t));
J
James Smart 已提交
4885
			}
4886
		}
4887 4888
	}

4889
	dd_data->context_un.mbox.dmabuffers = dmabuf;
4890

4891
	/* setup wake call as IOCB callback */
4892
	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4893 4894 4895 4896

	/* setup context field to pass wait_queue pointer to wake function */
	pmboxq->context1 = dd_data;
	dd_data->type = TYPE_MBOX;
4897
	dd_data->set_job = job;
4898
	dd_data->context_un.mbox.pmboxq = pmboxq;
4899
	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4900 4901 4902
	dd_data->context_un.mbox.ext = ext;
	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4903
	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4904 4905
	job->dd_data = dd_data;

4906 4907 4908 4909
	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
		if (rc != MBX_SUCCESS) {
4910 4911
			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
			goto job_done;
4912 4913
		}

4914
		/* job finished, copy the data */
4915
		memcpy(pmbx, pmb, sizeof(*pmb));
4916
		bsg_reply->reply_payload_rcv_len =
4917
			sg_copy_from_buffer(job->reply_payload.sg_list,
4918 4919
					    job->reply_payload.sg_cnt,
					    pmbx, size);
4920
		/* not waiting mbox already done */
4921 4922
		rc = 0;
		goto job_done;
4923 4924 4925
	}

	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4926 4927 4928 4929 4930 4931
	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
		return 1; /* job started */

job_done:
	/* common exit for error or job completed inline */
	if (pmboxq)
4932
		mempool_free(pmboxq, phba->mbox_mem_pool);
4933
	lpfc_bsg_dma_page_free(phba, dmabuf);
4934
	kfree(dd_data);
4935

4936
job_cont:
4937
	return rc;
4938 4939 4940 4941 4942 4943 4944
}

/**
 * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
 * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
 **/
static int
4945
lpfc_bsg_mbox_cmd(struct bsg_job *job)
4946
{
4947
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
4948 4949
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
4950
	struct lpfc_hba *phba = vport->phba;
4951
	struct dfc_mbox_req *mbox_req;
4952 4953
	int rc = 0;

4954
	/* mix-and-match backward compatibility */
4955
	bsg_reply->reply_payload_rcv_len = 0;
4956 4957
	if (job->request_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4958
		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4959
				"2737 Mix-and-match backward compatibility "
4960 4961 4962 4963 4964 4965
				"between MBOX_REQ old size:%d and "
				"new request size:%d\n",
				(int)(job->request_len -
				      sizeof(struct fc_bsg_request)),
				(int)sizeof(struct dfc_mbox_req));
		mbox_req = (struct dfc_mbox_req *)
4966
				bsg_request->rqst_data.h_vendor.vendor_cmd;
4967 4968
		mbox_req->extMboxTag = 0;
		mbox_req->extSeqNum = 0;
4969 4970 4971 4972 4973 4974
	}

	rc = lpfc_bsg_issue_mbox(phba, job, vport);

	if (rc == 0) {
		/* job done */
4975
		bsg_reply->result = 0;
4976
		job->dd_data = NULL;
J
Johannes Thumshirn 已提交
4977
		bsg_job_done(job, bsg_reply->result,
4978
			       bsg_reply->reply_payload_rcv_len);
4979 4980 4981 4982 4983
	} else if (rc == 1)
		/* job submitted, will complete later*/
		rc = 0; /* return zero, no error */
	else {
		/* some error occurred */
4984
		bsg_reply->result = rc;
4985 4986 4987 4988 4989 4990
		job->dd_data = NULL;
	}

	return rc;
}

4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
/**
 * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
 * @phba: Pointer to HBA context object.
 * @cmdiocbq: Pointer to command iocb.
 * @rspiocbq: Pointer to response iocb.
 *
 * This function is the completion handler for iocbs issued using
 * lpfc_menlo_cmd function. This function is called by the
 * ring event handler function without any lock held. This function
 * can be called from both worker thread context and interrupt
 * context. This function also can be called from another thread which
 * cleans up the SLI layer objects.
 * This function copies the contents of the response iocb to the
 * response iocb memory object provided by the caller of
 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
 * sleeps for the iocb completion.
 **/
static void
lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
			struct lpfc_iocbq *cmdiocbq,
			struct lpfc_iocbq *rspiocbq)
{
	struct bsg_job_data *dd_data;
5014
	struct bsg_job *job;
5015
	struct fc_bsg_reply *bsg_reply;
5016
	IOCB_t *rsp;
5017
	struct lpfc_dmabuf *bmp, *cmp, *rmp;
5018 5019 5020
	struct lpfc_bsg_menlo *menlo;
	unsigned long flags;
	struct menlo_response *menlo_resp;
5021
	unsigned int rsp_size;
5022 5023 5024
	int rc = 0;

	dd_data = cmdiocbq->context1;
5025 5026
	cmp = cmdiocbq->context2;
	bmp = cmdiocbq->context3;
5027
	menlo = &dd_data->context_un.menlo;
5028
	rmp = menlo->rmp;
5029 5030
	rsp = &rspiocbq->iocb;

5031 5032 5033 5034
	/* Determine if job has been aborted */
	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	job = dd_data->set_job;
	if (job) {
5035
		bsg_reply = job->reply;
5036 5037 5038 5039 5040 5041
		/* Prevent timeout handling from trying to abort job  */
		job->dd_data = NULL;
	}
	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

	/* Copy the job data or set the failing status for the job */
5042

5043 5044 5045 5046 5047 5048 5049
	if (job) {
		/* always return the xri, this would be used in the case
		 * of a menlo download to allow the data to be sent as a
		 * continuation of the exchange.
		 */

		menlo_resp = (struct menlo_response *)
5050
			bsg_reply->reply_data.vendor_reply.vendor_rsp;
5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065
		menlo_resp->xri = rsp->ulpContext;
		if (rsp->ulpStatus) {
			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
				case IOERR_SEQUENCE_TIMEOUT:
					rc = -ETIMEDOUT;
					break;
				case IOERR_INVALID_RPI:
					rc = -EFAULT;
					break;
				default:
					rc = -EACCES;
					break;
				}
			} else {
5066 5067
				rc = -EACCES;
			}
5068 5069
		} else {
			rsp_size = rsp->un.genreq64.bdl.bdeSize;
5070
			bsg_reply->reply_payload_rcv_len =
5071 5072 5073 5074 5075
				lpfc_bsg_copy_data(rmp, &job->reply_payload,
						   rsp_size, 0);
		}

	}
5076 5077

	lpfc_sli_release_iocbq(phba, cmdiocbq);
5078 5079 5080
	lpfc_free_bsg_buffers(phba, cmp);
	lpfc_free_bsg_buffers(phba, rmp);
	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5081 5082
	kfree(bmp);
	kfree(dd_data);
5083 5084 5085 5086

	/* Complete the job if active */

	if (job) {
5087
		bsg_reply->result = rc;
J
Johannes Thumshirn 已提交
5088
		bsg_job_done(job, bsg_reply->result,
5089
			       bsg_reply->reply_payload_rcv_len);
5090 5091
	}

5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104
	return;
}

/**
 * lpfc_menlo_cmd - send an ioctl for menlo hardware
 * @job: fc_bsg_job to handle
 *
 * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
 * all the command completions will return the xri for the command.
 * For menlo data requests a gen request 64 CX is used to continue the exchange
 * supplied in the menlo request header xri field.
 **/
static int
5105
lpfc_menlo_cmd(struct bsg_job *job)
5106
{
5107
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5108 5109
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
5110
	struct lpfc_hba *phba = vport->phba;
5111 5112
	struct lpfc_iocbq *cmdiocbq;
	IOCB_t *cmd;
5113 5114
	int rc = 0;
	struct menlo_command *menlo_cmd;
5115
	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
5116 5117 5118 5119 5120 5121
	int request_nseg;
	int reply_nseg;
	struct bsg_job_data *dd_data;
	struct ulp_bde64 *bpl = NULL;

	/* in case no data is returned return just the return code */
5122
	bsg_reply->reply_payload_rcv_len = 0;
5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
		sizeof(struct menlo_command)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2784 Received MENLO_CMD request below "
				"minimum size\n");
		rc = -ERANGE;
		goto no_dd_data;
	}

	if (job->reply_len <
	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2785 Received MENLO_CMD reply below "
				"minimum size\n");
		rc = -ERANGE;
		goto no_dd_data;
	}

	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2786 Adapter does not support menlo "
				"commands\n");
		rc = -EPERM;
		goto no_dd_data;
	}

	menlo_cmd = (struct menlo_command *)
5152
		bsg_request->rqst_data.h_vendor.vendor_cmd;
5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168

	/* allocate our bsg tracking structure */
	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
	if (!dd_data) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"2787 Failed allocation of dd_data\n");
		rc = -ENOMEM;
		goto no_dd_data;
	}

	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
	if (!bmp) {
		rc = -ENOMEM;
		goto free_dd;
	}

5169 5170
	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
	if (!bmp->virt) {
5171 5172 5173 5174
		rc = -ENOMEM;
		goto free_bmp;
	}

5175
	INIT_LIST_HEAD(&bmp->list);
5176

5177 5178 5179 5180 5181
	bpl = (struct ulp_bde64 *)bmp->virt;
	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
				     1, bpl, &request_nseg);
	if (!cmp) {
5182
		rc = -ENOMEM;
5183
		goto free_bmp;
5184
	}
5185 5186
	lpfc_bsg_copy_data(cmp, &job->request_payload,
			   job->request_payload.payload_len, 1);
5187

5188 5189 5190 5191 5192 5193 5194
	bpl += request_nseg;
	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
				     bpl, &reply_nseg);
	if (!rmp) {
		rc = -ENOMEM;
		goto free_cmp;
5195 5196
	}

5197 5198 5199 5200
	cmdiocbq = lpfc_sli_get_iocbq(phba);
	if (!cmdiocbq) {
		rc = -ENOMEM;
		goto free_rmp;
5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223
	}

	cmd = &cmdiocbq->iocb;
	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
	cmd->un.genreq64.bdl.bdeSize =
	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
	cmd->ulpBdeCount = 1;
	cmd->ulpClass = CLASS3;
	cmd->ulpOwner = OWN_CHIP;
	cmd->ulpLe = 1; /* Limited Edition */
	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
	cmdiocbq->vport = phba->pport;
	/* We want the firmware to timeout before we do */
	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
	cmdiocbq->context1 = dd_data;
5224 5225
	cmdiocbq->context2 = cmp;
	cmdiocbq->context3 = bmp;
5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238
	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
		cmd->ulpPU = MENLO_PU; /* 3 */
		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
	} else {
		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
		cmd->ulpPU = 1;
		cmd->un.ulpWord[4] = 0;
		cmd->ulpContext = menlo_cmd->xri;
	}

	dd_data->type = TYPE_MENLO;
5239
	dd_data->set_job = job;
5240
	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5241 5242
	dd_data->context_un.menlo.rmp = rmp;
	job->dd_data = dd_data;
5243 5244 5245 5246 5247 5248 5249

	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
		MENLO_TIMEOUT - 5);
	if (rc == IOCB_SUCCESS)
		return 0; /* done for now */

	lpfc_sli_release_iocbq(phba, cmdiocbq);
5250 5251 5252 5253 5254

free_rmp:
	lpfc_free_bsg_buffers(phba, rmp);
free_cmp:
	lpfc_free_bsg_buffers(phba, cmp);
5255
free_bmp:
5256 5257
	if (bmp->virt)
		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5258 5259 5260 5261 5262
	kfree(bmp);
free_dd:
	kfree(dd_data);
no_dd_data:
	/* make error code available to userspace */
5263
	bsg_reply->result = rc;
5264 5265 5266
	job->dd_data = NULL;
	return rc;
}
5267

5268
static int
5269
lpfc_forced_link_speed(struct bsg_job *job)
5270
{
5271
	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5272 5273
	struct lpfc_vport *vport = shost_priv(shost);
	struct lpfc_hba *phba = vport->phba;
5274
	struct fc_bsg_reply *bsg_reply = job->reply;
5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288
	struct forced_link_speed_support_reply *forced_reply;
	int rc = 0;

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct get_forced_link_speed_support)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"0048 Received FORCED_LINK_SPEED request "
				"below minimum size\n");
		rc = -EINVAL;
		goto job_error;
	}

	forced_reply = (struct forced_link_speed_support_reply *)
5289
		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304

	if (job->reply_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct forced_link_speed_support_reply)) {
		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
				"0049 Received FORCED_LINK_SPEED reply below "
				"minimum size\n");
		rc = -EINVAL;
		goto job_error;
	}

	forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
				   ? LPFC_FORCED_LINK_SPEED_SUPPORTED
				   : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
job_error:
5305
	bsg_reply->result = rc;
5306
	if (rc == 0)
J
Johannes Thumshirn 已提交
5307
		bsg_job_done(job, bsg_reply->result,
5308
			       bsg_reply->reply_payload_rcv_len);
5309 5310 5311
	return rc;
}

5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635
/**
 * lpfc_check_fwlog_support: Check FW log support on the adapter
 * @phba: Pointer to HBA context object.
 *
 * Check if FW Logging support by the adapter
 **/
int
lpfc_check_fwlog_support(struct lpfc_hba *phba)
{
	struct lpfc_ras_fwlog *ras_fwlog = NULL;

	ras_fwlog = &phba->ras_fwlog;

	if (ras_fwlog->ras_hwsupport == false)
		return -EACCES;
	else if (ras_fwlog->ras_enabled == false)
		return -EPERM;
	else
		return 0;
}

/**
 * lpfc_bsg_get_ras_config: Get RAS configuration settings
 * @job: fc_bsg_job to handle
 *
 * Get RAS configuration values set.
 **/
static int
lpfc_bsg_get_ras_config(struct bsg_job *job)
{
	struct Scsi_Host *shost = fc_bsg_to_shost(job);
	struct lpfc_vport *vport = shost_priv(shost);
	struct fc_bsg_reply *bsg_reply = job->reply;
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_bsg_get_ras_config_reply *ras_reply;
	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
	int rc = 0;

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct lpfc_bsg_ras_req)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"6181 Received RAS_LOG request "
				"below minimum size\n");
		rc = -EINVAL;
		goto ras_job_error;
	}

	/* Check FW log status */
	rc = lpfc_check_fwlog_support(phba);
	if (rc == -EACCES || rc == -EPERM)
		goto ras_job_error;

	ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
		bsg_reply->reply_data.vendor_reply.vendor_rsp;

	/* Current logging state */
	if (ras_fwlog->ras_active == true)
		ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
	else
		ras_reply->state = LPFC_RASLOG_STATE_STOPPED;

	ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
	ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;

ras_job_error:
	/* make error code available to userspace */
	bsg_reply->result = rc;

	/* complete the job back to userspace */
	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
	return rc;
}

/**
 * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
 * @phba: Pointer to HBA context object.
 *
 * Disable FW logging into host memory on the adapter. To
 * be done before reading logs from the host memory.
 **/
static void
lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
{
	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;

	ras_fwlog->ras_active = false;

	/* Disable FW logging to host memory */
	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
}

/**
 * lpfc_bsg_set_ras_config: Set FW logging parameters
 * @job: fc_bsg_job to handle
 *
 * Set log-level parameters for FW-logging in host memory
 **/
static int
lpfc_bsg_set_ras_config(struct bsg_job *job)
{
	struct Scsi_Host *shost = fc_bsg_to_shost(job);
	struct lpfc_vport *vport = shost_priv(shost);
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_bsg_set_ras_config_req *ras_req;
	struct fc_bsg_request *bsg_request = job->request;
	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
	struct fc_bsg_reply *bsg_reply = job->reply;
	uint8_t action = 0, log_level = 0;
	int rc = 0;

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct lpfc_bsg_set_ras_config_req)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"6182 Received RAS_LOG request "
				"below minimum size\n");
		rc = -EINVAL;
		goto ras_job_error;
	}

	/* Check FW log status */
	rc = lpfc_check_fwlog_support(phba);
	if (rc == -EACCES || rc == -EPERM)
		goto ras_job_error;

	ras_req = (struct lpfc_bsg_set_ras_config_req *)
		bsg_request->rqst_data.h_vendor.vendor_cmd;
	action = ras_req->action;
	log_level = ras_req->log_level;

	if (action == LPFC_RASACTION_STOP_LOGGING) {
		/* Check if already disabled */
		if (ras_fwlog->ras_active == false) {
			rc = -ESRCH;
			goto ras_job_error;
		}

		/* Disable logging */
		lpfc_ras_stop_fwlog(phba);
	} else {
		/*action = LPFC_RASACTION_START_LOGGING*/
		if (ras_fwlog->ras_active == true) {
			rc = -EINPROGRESS;
			goto ras_job_error;
		}

		/* Enable logging */
		rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
					      LPFC_RAS_ENABLE_LOGGING);
		if (rc)
			rc = -EINVAL;
	}
ras_job_error:
	/* make error code available to userspace */
	bsg_reply->result = rc;

	/* complete the job back to userspace */
	bsg_job_done(job, bsg_reply->result,
		       bsg_reply->reply_payload_rcv_len);

	return rc;
}

/**
 * lpfc_bsg_get_ras_lwpd: Get log write position data
 * @job: fc_bsg_job to handle
 *
 * Get Offset/Wrap count of the log message written
 * in host memory
 **/
static int
lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
{
	struct Scsi_Host *shost = fc_bsg_to_shost(job);
	struct lpfc_vport *vport = shost_priv(shost);
	struct lpfc_bsg_get_ras_lwpd *ras_reply;
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
	struct fc_bsg_reply *bsg_reply = job->reply;
	uint32_t lwpd_offset = 0;
	uint64_t wrap_value = 0;
	int rc = 0;

	rc = lpfc_check_fwlog_support(phba);
	if (rc == -EACCES || rc == -EPERM)
		goto ras_job_error;

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct lpfc_bsg_ras_req)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"6183 Received RAS_LOG request "
				"below minimum size\n");
		rc = -EINVAL;
		goto ras_job_error;
	}

	ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
		bsg_reply->reply_data.vendor_reply.vendor_rsp;

	lwpd_offset = *((uint32_t *)ras_fwlog->lwpd.virt) & 0xffffffff;
	ras_reply->offset = be32_to_cpu(lwpd_offset);

	wrap_value = *((uint64_t *)ras_fwlog->lwpd.virt);
	ras_reply->wrap_count = be32_to_cpu((wrap_value >> 32) & 0xffffffff);

ras_job_error:
	/* make error code available to userspace */
	bsg_reply->result = rc;

	/* complete the job back to userspace */
	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);

	return rc;
}

/**
 * lpfc_bsg_get_ras_fwlog: Read FW log
 * @job: fc_bsg_job to handle
 *
 * Copy the FW log into the passed buffer.
 **/
static int
lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
{
	struct Scsi_Host *shost = fc_bsg_to_shost(job);
	struct lpfc_vport *vport = shost_priv(shost);
	struct lpfc_hba *phba = vport->phba;
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
	struct lpfc_bsg_get_fwlog_req *ras_req;
	uint32_t rd_offset, rd_index, offset, pending_wlen;
	uint32_t boundary = 0, align_len = 0, write_len = 0;
	void *dest, *src, *fwlog_buff;
	struct lpfc_ras_fwlog *ras_fwlog = NULL;
	struct lpfc_dmabuf *dmabuf, *next;
	int rc = 0;

	ras_fwlog = &phba->ras_fwlog;

	rc = lpfc_check_fwlog_support(phba);
	if (rc == -EACCES || rc == -EPERM)
		goto ras_job_error;

	/* Logging to be stopped before reading */
	if (ras_fwlog->ras_active == true) {
		rc = -EINPROGRESS;
		goto ras_job_error;
	}

	if (job->request_len <
	    sizeof(struct fc_bsg_request) +
	    sizeof(struct lpfc_bsg_get_fwlog_req)) {
		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
				"6184 Received RAS_LOG request "
				"below minimum size\n");
		rc = -EINVAL;
		goto ras_job_error;
	}

	ras_req = (struct lpfc_bsg_get_fwlog_req *)
		bsg_request->rqst_data.h_vendor.vendor_cmd;
	rd_offset = ras_req->read_offset;

	/* Allocate memory to read fw log*/
	fwlog_buff = vmalloc(ras_req->read_size);
	if (!fwlog_buff) {
		rc = -ENOMEM;
		goto ras_job_error;
	}

	rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
	offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
	pending_wlen = ras_req->read_size;
	dest = fwlog_buff;

	list_for_each_entry_safe(dmabuf, next,
			      &ras_fwlog->fwlog_buff_list, list) {

		if (dmabuf->buffer_tag < rd_index)
			continue;

		/* Align read to buffer size */
		if (offset) {
			boundary = ((dmabuf->buffer_tag + 1) *
				    LPFC_RAS_MAX_ENTRY_SIZE);

			align_len = (boundary - offset);
			write_len = min_t(u32, align_len,
					  LPFC_RAS_MAX_ENTRY_SIZE);
		} else {
			write_len = min_t(u32, pending_wlen,
					  LPFC_RAS_MAX_ENTRY_SIZE);
			align_len = 0;
			boundary = 0;
		}
		src = dmabuf->virt + offset;
		memcpy(dest, src, write_len);

		pending_wlen -= write_len;
		if (!pending_wlen)
			break;

		dest += write_len;
		offset = (offset + write_len) % LPFC_RAS_MAX_ENTRY_SIZE;
	}

	bsg_reply->reply_payload_rcv_len =
		sg_copy_from_buffer(job->reply_payload.sg_list,
				    job->reply_payload.sg_cnt,
				    fwlog_buff, ras_req->read_size);

	vfree(fwlog_buff);

ras_job_error:
	bsg_reply->result = rc;
	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);

	return rc;
}


5636 5637 5638 5639 5640
/**
 * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
 * @job: fc_bsg_job to handle
 **/
static int
5641
lpfc_bsg_hst_vendor(struct bsg_job *job)
5642
{
5643 5644 5645
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
	int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658
	int rc;

	switch (command) {
	case LPFC_BSG_VENDOR_SET_CT_EVENT:
		rc = lpfc_bsg_hba_set_event(job);
		break;
	case LPFC_BSG_VENDOR_GET_CT_EVENT:
		rc = lpfc_bsg_hba_get_event(job);
		break;
	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
		rc = lpfc_bsg_send_mgmt_rsp(job);
		break;
	case LPFC_BSG_VENDOR_DIAG_MODE:
5659 5660 5661 5662 5663 5664 5665
		rc = lpfc_bsg_diag_loopback_mode(job);
		break;
	case LPFC_BSG_VENDOR_DIAG_MODE_END:
		rc = lpfc_sli4_bsg_diag_mode_end(job);
		break;
	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
		rc = lpfc_bsg_diag_loopback_run(job);
5666
		break;
5667 5668
	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
		rc = lpfc_sli4_bsg_link_diag_test(job);
5669 5670 5671 5672 5673 5674 5675
		break;
	case LPFC_BSG_VENDOR_GET_MGMT_REV:
		rc = lpfc_bsg_get_dfc_rev(job);
		break;
	case LPFC_BSG_VENDOR_MBOX:
		rc = lpfc_bsg_mbox_cmd(job);
		break;
5676 5677 5678 5679
	case LPFC_BSG_VENDOR_MENLO_CMD:
	case LPFC_BSG_VENDOR_MENLO_DATA:
		rc = lpfc_menlo_cmd(job);
		break;
5680 5681 5682
	case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
		rc = lpfc_forced_link_speed(job);
		break;
5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694
	case LPFC_BSG_VENDOR_RAS_GET_LWPD:
		rc = lpfc_bsg_get_ras_lwpd(job);
		break;
	case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
		rc = lpfc_bsg_get_ras_fwlog(job);
		break;
	case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
		rc = lpfc_bsg_get_ras_config(job);
		break;
	case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
		rc = lpfc_bsg_set_ras_config(job);
		break;
5695 5696
	default:
		rc = -EINVAL;
5697
		bsg_reply->reply_payload_rcv_len = 0;
5698
		/* make error code available to userspace */
5699
		bsg_reply->result = rc;
5700 5701 5702 5703 5704 5705 5706 5707
		break;
	}

	return rc;
}

/**
 * lpfc_bsg_request - handle a bsg request from the FC transport
5708
 * @job: bsg_job to handle
5709 5710
 **/
int
5711
lpfc_bsg_request(struct bsg_job *job)
5712
{
5713 5714
	struct fc_bsg_request *bsg_request = job->request;
	struct fc_bsg_reply *bsg_reply = job->reply;
5715 5716 5717
	uint32_t msgcode;
	int rc;

5718
	msgcode = bsg_request->msgcode;
5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730
	switch (msgcode) {
	case FC_BSG_HST_VENDOR:
		rc = lpfc_bsg_hst_vendor(job);
		break;
	case FC_BSG_RPT_ELS:
		rc = lpfc_bsg_rport_els(job);
		break;
	case FC_BSG_RPT_CT:
		rc = lpfc_bsg_send_mgmt_cmd(job);
		break;
	default:
		rc = -EINVAL;
5731
		bsg_reply->reply_payload_rcv_len = 0;
5732
		/* make error code available to userspace */
5733
		bsg_reply->result = rc;
5734 5735 5736 5737 5738 5739 5740 5741
		break;
	}

	return rc;
}

/**
 * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5742
 * @job: bsg_job that has timed out
5743 5744 5745 5746 5747
 *
 * This function just aborts the job's IOCB.  The aborted IOCB will return to
 * the waiting function which will handle passing the error back to userspace
 **/
int
5748
lpfc_bsg_timeout(struct bsg_job *job)
5749
{
5750
	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5751 5752
	struct lpfc_hba *phba = vport->phba;
	struct lpfc_iocbq *cmdiocb;
5753
	struct lpfc_sli_ring *pring;
5754 5755
	struct bsg_job_data *dd_data;
	unsigned long flags;
5756 5757 5758 5759
	int rc = 0;
	LIST_HEAD(completions);
	struct lpfc_iocbq *check_iocb, *next_iocb;

5760
	pring = lpfc_phba_elsring(phba);
5761 5762
	if (unlikely(!pring))
		return -EIO;
5763

5764 5765 5766 5767 5768 5769 5770 5771
	/* if job's driver data is NULL, the command completed or is in the
	 * the process of completing.  In this case, return status to request
	 * so the timeout is retried.  This avoids double completion issues
	 * and the request will be pulled off the timer queue when the
	 * command's completion handler executes.  Otherwise, prevent the
	 * command's completion handler from executing the job done callback
	 * and continue processing to abort the outstanding the command.
	 */
5772 5773 5774

	spin_lock_irqsave(&phba->ct_ev_lock, flags);
	dd_data = (struct bsg_job_data *)job->dd_data;
5775 5776 5777 5778
	if (dd_data) {
		dd_data->set_job = NULL;
		job->dd_data = NULL;
	} else {
5779
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5780
		return -EAGAIN;
5781 5782 5783 5784
	}

	switch (dd_data->type) {
	case TYPE_IOCB:
5785 5786 5787 5788 5789
		/* Check to see if IOCB was issued to the port or not. If not,
		 * remove it from the txq queue and call cancel iocbs.
		 * Otherwise, call abort iotag
		 */
		cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5790 5791 5792 5793
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

		spin_lock_irqsave(&phba->hbalock, flags);
		/* make sure the I/O abort window is still open */
5794
		if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
5795 5796 5797
			spin_unlock_irqrestore(&phba->hbalock, flags);
			return -EAGAIN;
		}
5798 5799 5800 5801 5802 5803 5804 5805 5806
		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
					 list) {
			if (check_iocb == cmdiocb) {
				list_move_tail(&check_iocb->list, &completions);
				break;
			}
		}
		if (list_empty(&completions))
			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5807
		spin_unlock_irqrestore(&phba->hbalock, flags);
5808 5809 5810 5811 5812
		if (!list_empty(&completions)) {
			lpfc_sli_cancel_iocbs(phba, &completions,
					      IOSTAT_LOCAL_REJECT,
					      IOERR_SLI_ABORTED);
		}
5813
		break;
5814

5815 5816 5817
	case TYPE_EVT:
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
		break;
5818

5819
	case TYPE_MBOX:
5820 5821
		/* Update the ext buf ctx state if needed */

5822 5823
		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5824
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5825
		break;
5826
	case TYPE_MENLO:
5827 5828 5829 5830 5831
		/* Check to see if IOCB was issued to the port or not. If not,
		 * remove it from the txq queue and call cancel iocbs.
		 * Otherwise, call abort iotag.
		 */
		cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5832 5833 5834
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);

		spin_lock_irqsave(&phba->hbalock, flags);
5835 5836 5837 5838 5839 5840 5841 5842 5843
		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
					 list) {
			if (check_iocb == cmdiocb) {
				list_move_tail(&check_iocb->list, &completions);
				break;
			}
		}
		if (list_empty(&completions))
			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5844
		spin_unlock_irqrestore(&phba->hbalock, flags);
5845 5846 5847 5848 5849
		if (!list_empty(&completions)) {
			lpfc_sli_cancel_iocbs(phba, &completions,
					      IOSTAT_LOCAL_REJECT,
					      IOERR_SLI_ABORTED);
		}
5850
		break;
5851 5852 5853 5854
	default:
		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
		break;
	}
5855

5856 5857 5858 5859
	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
	 * otherwise an error message will be displayed on the console
	 * so always return success (zero)
	 */
5860
	return rc;
5861
}