xref: /linux/drivers/scsi/lpfc/lpfc_bsg.c (revision 5a0e3ad6af8660be21ca98a971cd00f331318c05)
1f1c3b0fcSJames Smart /*******************************************************************
2f1c3b0fcSJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3f1c3b0fcSJames Smart  * Fibre Channel Host Bus Adapters.                                *
44fede78fSJames Smart  * Copyright (C) 2009-2010 Emulex.  All rights reserved.                *
5f1c3b0fcSJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
6f1c3b0fcSJames Smart  * www.emulex.com                                                  *
7f1c3b0fcSJames Smart  *                                                                 *
8f1c3b0fcSJames Smart  * This program is free software; you can redistribute it and/or   *
9f1c3b0fcSJames Smart  * modify it under the terms of version 2 of the GNU General       *
10f1c3b0fcSJames Smart  * Public License as published by the Free Software Foundation.    *
11f1c3b0fcSJames Smart  * This program is distributed in the hope that it will be useful. *
12f1c3b0fcSJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13f1c3b0fcSJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14f1c3b0fcSJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15f1c3b0fcSJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16f1c3b0fcSJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17f1c3b0fcSJames Smart  * more details, a copy of which can be found in the file COPYING  *
18f1c3b0fcSJames Smart  * included with this package.                                     *
19f1c3b0fcSJames Smart  *******************************************************************/
20f1c3b0fcSJames Smart 
21f1c3b0fcSJames Smart #include <linux/interrupt.h>
22f1c3b0fcSJames Smart #include <linux/mempool.h>
23f1c3b0fcSJames Smart #include <linux/pci.h>
24*5a0e3ad6STejun Heo #include <linux/slab.h>
25277e76f1SJames Smart #include <linux/delay.h>
26f1c3b0fcSJames Smart 
27f1c3b0fcSJames Smart #include <scsi/scsi.h>
28f1c3b0fcSJames Smart #include <scsi/scsi_host.h>
29f1c3b0fcSJames Smart #include <scsi/scsi_transport_fc.h>
30f1c3b0fcSJames Smart #include <scsi/scsi_bsg_fc.h>
316a9c52cfSJames Smart #include <scsi/fc/fc_fs.h>
32f1c3b0fcSJames Smart 
33f1c3b0fcSJames Smart #include "lpfc_hw4.h"
34f1c3b0fcSJames Smart #include "lpfc_hw.h"
35f1c3b0fcSJames Smart #include "lpfc_sli.h"
36f1c3b0fcSJames Smart #include "lpfc_sli4.h"
37f1c3b0fcSJames Smart #include "lpfc_nl.h"
384fede78fSJames Smart #include "lpfc_bsg.h"
39f1c3b0fcSJames Smart #include "lpfc_disc.h"
40f1c3b0fcSJames Smart #include "lpfc_scsi.h"
41f1c3b0fcSJames Smart #include "lpfc.h"
42f1c3b0fcSJames Smart #include "lpfc_logmsg.h"
43f1c3b0fcSJames Smart #include "lpfc_crtn.h"
44f1c3b0fcSJames Smart #include "lpfc_vport.h"
45f1c3b0fcSJames Smart #include "lpfc_version.h"
46f1c3b0fcSJames Smart 
474cc0e56eSJames Smart struct lpfc_bsg_event {
484cc0e56eSJames Smart 	struct list_head node;
494cc0e56eSJames Smart 	struct kref kref;
504cc0e56eSJames Smart 	wait_queue_head_t wq;
514cc0e56eSJames Smart 
524cc0e56eSJames Smart 	/* Event type and waiter identifiers */
534cc0e56eSJames Smart 	uint32_t type_mask;
544cc0e56eSJames Smart 	uint32_t req_id;
554cc0e56eSJames Smart 	uint32_t reg_id;
564cc0e56eSJames Smart 
574cc0e56eSJames Smart 	/* next two flags are here for the auto-delete logic */
584cc0e56eSJames Smart 	unsigned long wait_time_stamp;
594cc0e56eSJames Smart 	int waiting;
604cc0e56eSJames Smart 
614cc0e56eSJames Smart 	/* seen and not seen events */
624cc0e56eSJames Smart 	struct list_head events_to_get;
634cc0e56eSJames Smart 	struct list_head events_to_see;
644cc0e56eSJames Smart 
654cc0e56eSJames Smart 	/* job waiting for this event to finish */
664cc0e56eSJames Smart 	struct fc_bsg_job *set_job;
674cc0e56eSJames Smart };
684cc0e56eSJames Smart 
694cc0e56eSJames Smart struct lpfc_bsg_iocb {
704cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocbq;
714cc0e56eSJames Smart 	struct lpfc_iocbq *rspiocbq;
724cc0e56eSJames Smart 	struct lpfc_dmabuf *bmp;
734cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
744cc0e56eSJames Smart 
754cc0e56eSJames Smart 	/* job waiting for this iocb to finish */
764cc0e56eSJames Smart 	struct fc_bsg_job *set_job;
774cc0e56eSJames Smart };
784cc0e56eSJames Smart 
793b5dd52aSJames Smart struct lpfc_bsg_mbox {
803b5dd52aSJames Smart 	LPFC_MBOXQ_t *pmboxq;
813b5dd52aSJames Smart 	MAILBOX_t *mb;
823b5dd52aSJames Smart 
833b5dd52aSJames Smart 	/* job waiting for this mbox command to finish */
843b5dd52aSJames Smart 	struct fc_bsg_job *set_job;
853b5dd52aSJames Smart };
863b5dd52aSJames Smart 
87e2aed29fSJames Smart #define MENLO_DID 0x0000FC0E
88e2aed29fSJames Smart 
89e2aed29fSJames Smart struct lpfc_bsg_menlo {
90e2aed29fSJames Smart 	struct lpfc_iocbq *cmdiocbq;
91e2aed29fSJames Smart 	struct lpfc_iocbq *rspiocbq;
92e2aed29fSJames Smart 	struct lpfc_dmabuf *bmp;
93e2aed29fSJames Smart 
94e2aed29fSJames Smart 	/* job waiting for this iocb to finish */
95e2aed29fSJames Smart 	struct fc_bsg_job *set_job;
96e2aed29fSJames Smart };
97e2aed29fSJames Smart 
984cc0e56eSJames Smart #define TYPE_EVT 	1
994cc0e56eSJames Smart #define TYPE_IOCB	2
1003b5dd52aSJames Smart #define TYPE_MBOX	3
101e2aed29fSJames Smart #define TYPE_MENLO	4
1024cc0e56eSJames Smart struct bsg_job_data {
1034cc0e56eSJames Smart 	uint32_t type;
1044cc0e56eSJames Smart 	union {
1054cc0e56eSJames Smart 		struct lpfc_bsg_event *evt;
1064cc0e56eSJames Smart 		struct lpfc_bsg_iocb iocb;
1073b5dd52aSJames Smart 		struct lpfc_bsg_mbox mbox;
108e2aed29fSJames Smart 		struct lpfc_bsg_menlo menlo;
1094cc0e56eSJames Smart 	} context_un;
1104cc0e56eSJames Smart };
1114cc0e56eSJames Smart 
1124cc0e56eSJames Smart struct event_data {
1134cc0e56eSJames Smart 	struct list_head node;
1144cc0e56eSJames Smart 	uint32_t type;
1154cc0e56eSJames Smart 	uint32_t immed_dat;
1164cc0e56eSJames Smart 	void *data;
1174cc0e56eSJames Smart 	uint32_t len;
1184cc0e56eSJames Smart };
1194cc0e56eSJames Smart 
1203b5dd52aSJames Smart #define BUF_SZ_4K 4096
1214cc0e56eSJames Smart #define SLI_CT_ELX_LOOPBACK 0x10
1224cc0e56eSJames Smart 
1234cc0e56eSJames Smart enum ELX_LOOPBACK_CMD {
1244cc0e56eSJames Smart 	ELX_LOOPBACK_XRI_SETUP,
1254cc0e56eSJames Smart 	ELX_LOOPBACK_DATA,
1264cc0e56eSJames Smart };
1274cc0e56eSJames Smart 
1283b5dd52aSJames Smart #define ELX_LOOPBACK_HEADER_SZ \
1293b5dd52aSJames Smart 	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
1303b5dd52aSJames Smart 
1314cc0e56eSJames Smart struct lpfc_dmabufext {
1324cc0e56eSJames Smart 	struct lpfc_dmabuf dma;
1334cc0e56eSJames Smart 	uint32_t size;
1344cc0e56eSJames Smart 	uint32_t flag;
1354cc0e56eSJames Smart };
1364cc0e56eSJames Smart 
137f1c3b0fcSJames Smart /**
1384cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
1394cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
1404cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
1414cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
1424cc0e56eSJames Smart  *
1434cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
1444cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
1454cc0e56eSJames Smart  * ring event handler function without any lock held. This function
1464cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
1474cc0e56eSJames Smart  * context. This function also can be called from another thread which
1484cc0e56eSJames Smart  * cleans up the SLI layer objects.
1494cc0e56eSJames Smart  * This function copies the contents of the response iocb to the
1504cc0e56eSJames Smart  * response iocb memory object provided by the caller of
1514cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1524cc0e56eSJames Smart  * sleeps for the iocb completion.
1534cc0e56eSJames Smart  **/
1544cc0e56eSJames Smart static void
1554cc0e56eSJames Smart lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
1564cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
1574cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
1584cc0e56eSJames Smart {
1594cc0e56eSJames Smart 	unsigned long iflags;
1604cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
1614cc0e56eSJames Smart 	struct fc_bsg_job *job;
1624cc0e56eSJames Smart 	IOCB_t *rsp;
1634cc0e56eSJames Smart 	struct lpfc_dmabuf *bmp;
1644cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
1654cc0e56eSJames Smart 	struct lpfc_bsg_iocb *iocb;
1664cc0e56eSJames Smart 	unsigned long flags;
1674cc0e56eSJames Smart 	int rc = 0;
1684cc0e56eSJames Smart 
1694cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1704cc0e56eSJames Smart 	dd_data = cmdiocbq->context1;
1714cc0e56eSJames Smart 	if (!dd_data) {
1724cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1734cc0e56eSJames Smart 		return;
1744cc0e56eSJames Smart 	}
1754cc0e56eSJames Smart 
1764cc0e56eSJames Smart 	iocb = &dd_data->context_un.iocb;
1774cc0e56eSJames Smart 	job = iocb->set_job;
1784cc0e56eSJames Smart 	job->dd_data = NULL; /* so timeout handler does not reply */
1794cc0e56eSJames Smart 
1804cc0e56eSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflags);
1814cc0e56eSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
1824cc0e56eSJames Smart 	if (cmdiocbq->context2 && rspiocbq)
1834cc0e56eSJames Smart 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
1844cc0e56eSJames Smart 		       &rspiocbq->iocb, sizeof(IOCB_t));
1854cc0e56eSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflags);
1864cc0e56eSJames Smart 
1874cc0e56eSJames Smart 	bmp = iocb->bmp;
1884cc0e56eSJames Smart 	rspiocbq = iocb->rspiocbq;
1894cc0e56eSJames Smart 	rsp = &rspiocbq->iocb;
1904cc0e56eSJames Smart 	ndlp = iocb->ndlp;
1914cc0e56eSJames Smart 
1924cc0e56eSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1934cc0e56eSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
1944cc0e56eSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
1954cc0e56eSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1964cc0e56eSJames Smart 
1974cc0e56eSJames Smart 	if (rsp->ulpStatus) {
1984cc0e56eSJames Smart 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1994cc0e56eSJames Smart 			switch (rsp->un.ulpWord[4] & 0xff) {
2004cc0e56eSJames Smart 			case IOERR_SEQUENCE_TIMEOUT:
2014cc0e56eSJames Smart 				rc = -ETIMEDOUT;
2024cc0e56eSJames Smart 				break;
2034cc0e56eSJames Smart 			case IOERR_INVALID_RPI:
2044cc0e56eSJames Smart 				rc = -EFAULT;
2054cc0e56eSJames Smart 				break;
2064cc0e56eSJames Smart 			default:
2074cc0e56eSJames Smart 				rc = -EACCES;
2084cc0e56eSJames Smart 				break;
2094cc0e56eSJames Smart 			}
2104cc0e56eSJames Smart 		} else
2114cc0e56eSJames Smart 			rc = -EACCES;
2124cc0e56eSJames Smart 	} else
2134cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len =
2144cc0e56eSJames Smart 			rsp->un.genreq64.bdl.bdeSize;
2154cc0e56eSJames Smart 
2164cc0e56eSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2174cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
2184cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
2194cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
2204cc0e56eSJames Smart 	kfree(bmp);
2214cc0e56eSJames Smart 	kfree(dd_data);
2224cc0e56eSJames Smart 	/* make error code available to userspace */
2234cc0e56eSJames Smart 	job->reply->result = rc;
2244cc0e56eSJames Smart 	/* complete the job back to userspace */
2254cc0e56eSJames Smart 	job->job_done(job);
2264cc0e56eSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2274cc0e56eSJames Smart 	return;
2284cc0e56eSJames Smart }
2294cc0e56eSJames Smart 
2304cc0e56eSJames Smart /**
2314cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
232f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
2333b5dd52aSJames Smart  **/
234f1c3b0fcSJames Smart static int
2354cc0e56eSJames Smart lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
236f1c3b0fcSJames Smart {
237f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
238f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
239f1c3b0fcSJames Smart 	struct lpfc_rport_data *rdata = job->rport->dd_data;
240f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
241f1c3b0fcSJames Smart 	struct ulp_bde64 *bpl = NULL;
242f1c3b0fcSJames Smart 	uint32_t timeout;
243f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq = NULL;
244f1c3b0fcSJames Smart 	struct lpfc_iocbq *rspiocbq = NULL;
245f1c3b0fcSJames Smart 	IOCB_t *cmd;
246f1c3b0fcSJames Smart 	IOCB_t *rsp;
247f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bmp = NULL;
248f1c3b0fcSJames Smart 	int request_nseg;
249f1c3b0fcSJames Smart 	int reply_nseg;
250f1c3b0fcSJames Smart 	struct scatterlist *sgel = NULL;
251f1c3b0fcSJames Smart 	int numbde;
252f1c3b0fcSJames Smart 	dma_addr_t busaddr;
2534cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
2544cc0e56eSJames Smart 	uint32_t creg_val;
255f1c3b0fcSJames Smart 	int rc = 0;
256f1c3b0fcSJames Smart 
257f1c3b0fcSJames Smart 	/* in case no data is transferred */
258f1c3b0fcSJames Smart 	job->reply->reply_payload_rcv_len = 0;
259f1c3b0fcSJames Smart 
2604cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
2614cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2624cc0e56eSJames Smart 	if (!dd_data) {
2634cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2644cc0e56eSJames Smart 				"2733 Failed allocation of dd_data\n");
2654cc0e56eSJames Smart 		rc = -ENOMEM;
2664cc0e56eSJames Smart 		goto no_dd_data;
2674cc0e56eSJames Smart 	}
2684cc0e56eSJames Smart 
269f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
2704cc0e56eSJames Smart 		rc = -ENODEV;
2714cc0e56eSJames Smart 		goto no_ndlp;
2724cc0e56eSJames Smart 	}
2734cc0e56eSJames Smart 
2744cc0e56eSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2754cc0e56eSJames Smart 	if (!bmp) {
2764cc0e56eSJames Smart 		rc = -ENOMEM;
2774cc0e56eSJames Smart 		goto free_ndlp;
278f1c3b0fcSJames Smart 	}
279f1c3b0fcSJames Smart 
280f1c3b0fcSJames Smart 	if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
281f1c3b0fcSJames Smart 		rc = -ENODEV;
2824cc0e56eSJames Smart 		goto free_bmp;
283f1c3b0fcSJames Smart 	}
284f1c3b0fcSJames Smart 
285f1c3b0fcSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
286f1c3b0fcSJames Smart 	if (!cmdiocbq) {
287f1c3b0fcSJames Smart 		rc = -ENOMEM;
2884cc0e56eSJames Smart 		goto free_bmp;
289f1c3b0fcSJames Smart 	}
290f1c3b0fcSJames Smart 
2914cc0e56eSJames Smart 	cmd = &cmdiocbq->iocb;
292f1c3b0fcSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
293f1c3b0fcSJames Smart 	if (!rspiocbq) {
294f1c3b0fcSJames Smart 		rc = -ENOMEM;
295f1c3b0fcSJames Smart 		goto free_cmdiocbq;
296f1c3b0fcSJames Smart 	}
297f1c3b0fcSJames Smart 
298f1c3b0fcSJames Smart 	rsp = &rspiocbq->iocb;
299f1c3b0fcSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
300f1c3b0fcSJames Smart 	if (!bmp->virt) {
301f1c3b0fcSJames Smart 		rc = -ENOMEM;
3024cc0e56eSJames Smart 		goto free_rspiocbq;
303f1c3b0fcSJames Smart 	}
304f1c3b0fcSJames Smart 
305f1c3b0fcSJames Smart 	INIT_LIST_HEAD(&bmp->list);
306f1c3b0fcSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
307f1c3b0fcSJames Smart 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
308f1c3b0fcSJames Smart 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
309f1c3b0fcSJames Smart 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
310f1c3b0fcSJames Smart 		busaddr = sg_dma_address(sgel);
311f1c3b0fcSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
312f1c3b0fcSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
313f1c3b0fcSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
314f1c3b0fcSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
315f1c3b0fcSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
316f1c3b0fcSJames Smart 		bpl++;
317f1c3b0fcSJames Smart 	}
318f1c3b0fcSJames Smart 
319f1c3b0fcSJames Smart 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
320f1c3b0fcSJames Smart 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
321f1c3b0fcSJames Smart 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
322f1c3b0fcSJames Smart 		busaddr = sg_dma_address(sgel);
323f1c3b0fcSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
324f1c3b0fcSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
325f1c3b0fcSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
326f1c3b0fcSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
327f1c3b0fcSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
328f1c3b0fcSJames Smart 		bpl++;
329f1c3b0fcSJames Smart 	}
330f1c3b0fcSJames Smart 
331f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
332f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
333f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
334f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
335f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
336f1c3b0fcSJames Smart 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
337f1c3b0fcSJames Smart 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
338f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
339f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
3406a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
3416a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
342f1c3b0fcSJames Smart 	cmd->ulpBdeCount = 1;
343f1c3b0fcSJames Smart 	cmd->ulpLe = 1;
344f1c3b0fcSJames Smart 	cmd->ulpClass = CLASS3;
345f1c3b0fcSJames Smart 	cmd->ulpContext = ndlp->nlp_rpi;
346f1c3b0fcSJames Smart 	cmd->ulpOwner = OWN_CHIP;
347f1c3b0fcSJames Smart 	cmdiocbq->vport = phba->pport;
3484cc0e56eSJames Smart 	cmdiocbq->context3 = bmp;
349f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
350f1c3b0fcSJames Smart 	timeout = phba->fc_ratov * 2;
3514cc0e56eSJames Smart 	cmd->ulpTimeout = timeout;
352f1c3b0fcSJames Smart 
3534cc0e56eSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
3544cc0e56eSJames Smart 	cmdiocbq->context1 = dd_data;
3554cc0e56eSJames Smart 	cmdiocbq->context2 = rspiocbq;
3564cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
3574cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
3584cc0e56eSJames Smart 	dd_data->context_un.iocb.rspiocbq = rspiocbq;
3594cc0e56eSJames Smart 	dd_data->context_un.iocb.set_job = job;
3604cc0e56eSJames Smart 	dd_data->context_un.iocb.bmp = bmp;
3614cc0e56eSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
362f1c3b0fcSJames Smart 
3634cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3644cc0e56eSJames Smart 		creg_val = readl(phba->HCregaddr);
3654cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3664cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
3674cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
3684cc0e56eSJames Smart 	}
3694cc0e56eSJames Smart 
3704cc0e56eSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
3714cc0e56eSJames Smart 
3724cc0e56eSJames Smart 	if (rc == IOCB_SUCCESS)
3734cc0e56eSJames Smart 		return 0; /* done for now */
3744cc0e56eSJames Smart 
3754cc0e56eSJames Smart 	/* iocb failed so cleanup */
376f1c3b0fcSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
377f1c3b0fcSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
378f1c3b0fcSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
379f1c3b0fcSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
380f1c3b0fcSJames Smart 
381f1c3b0fcSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
3824cc0e56eSJames Smart 
383f1c3b0fcSJames Smart free_rspiocbq:
384f1c3b0fcSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
385f1c3b0fcSJames Smart free_cmdiocbq:
386f1c3b0fcSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
3874cc0e56eSJames Smart free_bmp:
3884cc0e56eSJames Smart 	kfree(bmp);
3894cc0e56eSJames Smart free_ndlp:
390f1c3b0fcSJames Smart 	lpfc_nlp_put(ndlp);
3914cc0e56eSJames Smart no_ndlp:
3924cc0e56eSJames Smart 	kfree(dd_data);
3934cc0e56eSJames Smart no_dd_data:
394f1c3b0fcSJames Smart 	/* make error code available to userspace */
395f1c3b0fcSJames Smart 	job->reply->result = rc;
3964cc0e56eSJames Smart 	job->dd_data = NULL;
3974cc0e56eSJames Smart 	return rc;
3984cc0e56eSJames Smart }
3994cc0e56eSJames Smart 
4004cc0e56eSJames Smart /**
4014cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
4024cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
4034cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
4044cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
4054cc0e56eSJames Smart  *
4064cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
4074cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp function. This function is called by the
4084cc0e56eSJames Smart  * ring event handler function without any lock held. This function
4094cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
4104cc0e56eSJames Smart  * context. This function also can be called from other thread which
4114cc0e56eSJames Smart  * cleans up the SLI layer objects.
4123b5dd52aSJames Smart  * This function copies the contents of the response iocb to the
4134cc0e56eSJames Smart  * response iocb memory object provided by the caller of
4144cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4154cc0e56eSJames Smart  * sleeps for the iocb completion.
4164cc0e56eSJames Smart  **/
4174cc0e56eSJames Smart static void
4184cc0e56eSJames Smart lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
4194cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
4204cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
4214cc0e56eSJames Smart {
4224cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
4234cc0e56eSJames Smart 	struct fc_bsg_job *job;
4244cc0e56eSJames Smart 	IOCB_t *rsp;
4254cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
4264cc0e56eSJames Smart 	struct lpfc_dmabuf *pbuflist = NULL;
4274cc0e56eSJames Smart 	struct fc_bsg_ctels_reply *els_reply;
4284cc0e56eSJames Smart 	uint8_t *rjt_data;
4294cc0e56eSJames Smart 	unsigned long flags;
4304cc0e56eSJames Smart 	int rc = 0;
4314cc0e56eSJames Smart 
4324cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
4334cc0e56eSJames Smart 	dd_data = cmdiocbq->context1;
4344cc0e56eSJames Smart 	/* normal completion and timeout crossed paths, already done */
4354cc0e56eSJames Smart 	if (!dd_data) {
4364cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
4374cc0e56eSJames Smart 		return;
4384cc0e56eSJames Smart 	}
4394cc0e56eSJames Smart 
4404cc0e56eSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4414cc0e56eSJames Smart 	if (cmdiocbq->context2 && rspiocbq)
4424cc0e56eSJames Smart 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4434cc0e56eSJames Smart 		       &rspiocbq->iocb, sizeof(IOCB_t));
4444cc0e56eSJames Smart 
4454cc0e56eSJames Smart 	job = dd_data->context_un.iocb.set_job;
4464cc0e56eSJames Smart 	cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
4474cc0e56eSJames Smart 	rspiocbq = dd_data->context_un.iocb.rspiocbq;
4484cc0e56eSJames Smart 	rsp = &rspiocbq->iocb;
4494cc0e56eSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
4504cc0e56eSJames Smart 
4514cc0e56eSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4524cc0e56eSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
4534cc0e56eSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4544cc0e56eSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4554cc0e56eSJames Smart 
4564cc0e56eSJames Smart 	if (job->reply->result == -EAGAIN)
4574cc0e56eSJames Smart 		rc = -EAGAIN;
4584cc0e56eSJames Smart 	else if (rsp->ulpStatus == IOSTAT_SUCCESS)
4594cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len =
4604cc0e56eSJames Smart 			rsp->un.elsreq64.bdl.bdeSize;
4614cc0e56eSJames Smart 	else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
4624cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len =
4634cc0e56eSJames Smart 			sizeof(struct fc_bsg_ctels_reply);
4644cc0e56eSJames Smart 		/* LS_RJT data returned in word 4 */
4654cc0e56eSJames Smart 		rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
4664cc0e56eSJames Smart 		els_reply = &job->reply->reply_data.ctels_reply;
4674cc0e56eSJames Smart 		els_reply->status = FC_CTELS_STATUS_REJECT;
4684cc0e56eSJames Smart 		els_reply->rjt_data.action = rjt_data[3];
4694cc0e56eSJames Smart 		els_reply->rjt_data.reason_code = rjt_data[2];
4704cc0e56eSJames Smart 		els_reply->rjt_data.reason_explanation = rjt_data[1];
4714cc0e56eSJames Smart 		els_reply->rjt_data.vendor_unique = rjt_data[0];
4724cc0e56eSJames Smart 	} else
4734cc0e56eSJames Smart 		rc = -EIO;
4744cc0e56eSJames Smart 
4754cc0e56eSJames Smart 	pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
4764cc0e56eSJames Smart 	lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
4774cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
4784cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
4794cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
4804cc0e56eSJames Smart 	kfree(dd_data);
4814cc0e56eSJames Smart 	/* make error code available to userspace */
4824cc0e56eSJames Smart 	job->reply->result = rc;
4834cc0e56eSJames Smart 	job->dd_data = NULL;
484f1c3b0fcSJames Smart 	/* complete the job back to userspace */
485f1c3b0fcSJames Smart 	job->job_done(job);
4864cc0e56eSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4874cc0e56eSJames Smart 	return;
488f1c3b0fcSJames Smart }
489f1c3b0fcSJames Smart 
490f1c3b0fcSJames Smart /**
491f1c3b0fcSJames Smart  * lpfc_bsg_rport_els - send an ELS command from a bsg request
492f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
4933b5dd52aSJames Smart  **/
494f1c3b0fcSJames Smart static int
495f1c3b0fcSJames Smart lpfc_bsg_rport_els(struct fc_bsg_job *job)
496f1c3b0fcSJames Smart {
497f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
498f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
499f1c3b0fcSJames Smart 	struct lpfc_rport_data *rdata = job->rport->dd_data;
500f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
501f1c3b0fcSJames Smart 	uint32_t elscmd;
502f1c3b0fcSJames Smart 	uint32_t cmdsize;
503f1c3b0fcSJames Smart 	uint32_t rspsize;
504f1c3b0fcSJames Smart 	struct lpfc_iocbq *rspiocbq;
505f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq;
506f1c3b0fcSJames Smart 	IOCB_t *rsp;
507f1c3b0fcSJames Smart 	uint16_t rpi = 0;
508f1c3b0fcSJames Smart 	struct lpfc_dmabuf *pcmd;
509f1c3b0fcSJames Smart 	struct lpfc_dmabuf *prsp;
510f1c3b0fcSJames Smart 	struct lpfc_dmabuf *pbuflist = NULL;
511f1c3b0fcSJames Smart 	struct ulp_bde64 *bpl;
512f1c3b0fcSJames Smart 	int request_nseg;
513f1c3b0fcSJames Smart 	int reply_nseg;
514f1c3b0fcSJames Smart 	struct scatterlist *sgel = NULL;
515f1c3b0fcSJames Smart 	int numbde;
516f1c3b0fcSJames Smart 	dma_addr_t busaddr;
5174cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
5184cc0e56eSJames Smart 	uint32_t creg_val;
519f1c3b0fcSJames Smart 	int rc = 0;
520f1c3b0fcSJames Smart 
521f1c3b0fcSJames Smart 	/* in case no data is transferred */
522f1c3b0fcSJames Smart 	job->reply->reply_payload_rcv_len = 0;
523f1c3b0fcSJames Smart 
5244cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
5254cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5264cc0e56eSJames Smart 	if (!dd_data) {
5274cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5284cc0e56eSJames Smart 				"2735 Failed allocation of dd_data\n");
5294cc0e56eSJames Smart 		rc = -ENOMEM;
5304cc0e56eSJames Smart 		goto no_dd_data;
5314cc0e56eSJames Smart 	}
5324cc0e56eSJames Smart 
533f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
534f1c3b0fcSJames Smart 		rc = -ENODEV;
5354cc0e56eSJames Smart 		goto free_dd_data;
536f1c3b0fcSJames Smart 	}
537f1c3b0fcSJames Smart 
538f1c3b0fcSJames Smart 	elscmd = job->request->rqst_data.r_els.els_code;
539f1c3b0fcSJames Smart 	cmdsize = job->request_payload.payload_len;
540f1c3b0fcSJames Smart 	rspsize = job->reply_payload.payload_len;
541f1c3b0fcSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
542f1c3b0fcSJames Smart 	if (!rspiocbq) {
543f1c3b0fcSJames Smart 		lpfc_nlp_put(ndlp);
544f1c3b0fcSJames Smart 		rc = -ENOMEM;
5454cc0e56eSJames Smart 		goto free_dd_data;
546f1c3b0fcSJames Smart 	}
547f1c3b0fcSJames Smart 
548f1c3b0fcSJames Smart 	rsp = &rspiocbq->iocb;
549f1c3b0fcSJames Smart 	rpi = ndlp->nlp_rpi;
550f1c3b0fcSJames Smart 
5514cc0e56eSJames Smart 	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
552f1c3b0fcSJames Smart 				      ndlp->nlp_DID, elscmd);
553f1c3b0fcSJames Smart 	if (!cmdiocbq) {
5544cc0e56eSJames Smart 		rc = -EIO;
5554cc0e56eSJames Smart 		goto free_rspiocbq;
556f1c3b0fcSJames Smart 	}
557f1c3b0fcSJames Smart 
5584cc0e56eSJames Smart 	/* prep els iocb set context1 to the ndlp, context2 to the command
5594cc0e56eSJames Smart 	 * dmabuf, context3 holds the data dmabuf
5604cc0e56eSJames Smart 	 */
561f1c3b0fcSJames Smart 	pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
562f1c3b0fcSJames Smart 	prsp = (struct lpfc_dmabuf *) pcmd->list.next;
563f1c3b0fcSJames Smart 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
564f1c3b0fcSJames Smart 	kfree(pcmd);
565f1c3b0fcSJames Smart 	lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
566f1c3b0fcSJames Smart 	kfree(prsp);
567f1c3b0fcSJames Smart 	cmdiocbq->context2 = NULL;
568f1c3b0fcSJames Smart 
569f1c3b0fcSJames Smart 	pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
570f1c3b0fcSJames Smart 	bpl = (struct ulp_bde64 *) pbuflist->virt;
571f1c3b0fcSJames Smart 
572f1c3b0fcSJames Smart 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
573f1c3b0fcSJames Smart 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
574f1c3b0fcSJames Smart 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
575f1c3b0fcSJames Smart 		busaddr = sg_dma_address(sgel);
576f1c3b0fcSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
577f1c3b0fcSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
578f1c3b0fcSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
579f1c3b0fcSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
580f1c3b0fcSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
581f1c3b0fcSJames Smart 		bpl++;
582f1c3b0fcSJames Smart 	}
583f1c3b0fcSJames Smart 
584f1c3b0fcSJames Smart 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
585f1c3b0fcSJames Smart 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
586f1c3b0fcSJames Smart 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
587f1c3b0fcSJames Smart 		busaddr = sg_dma_address(sgel);
588f1c3b0fcSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
589f1c3b0fcSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
590f1c3b0fcSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
591f1c3b0fcSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
592f1c3b0fcSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
593f1c3b0fcSJames Smart 		bpl++;
594f1c3b0fcSJames Smart 	}
595f1c3b0fcSJames Smart 	cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
596f1c3b0fcSJames Smart 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
597f1c3b0fcSJames Smart 	cmdiocbq->iocb.ulpContext = rpi;
598f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
599f1c3b0fcSJames Smart 	cmdiocbq->context1 = NULL;
600f1c3b0fcSJames Smart 	cmdiocbq->context2 = NULL;
601f1c3b0fcSJames Smart 
6024cc0e56eSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
6034cc0e56eSJames Smart 	cmdiocbq->context1 = dd_data;
6044cc0e56eSJames Smart 	cmdiocbq->context2 = rspiocbq;
6054cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
6064cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
6074cc0e56eSJames Smart 	dd_data->context_un.iocb.rspiocbq = rspiocbq;
6084cc0e56eSJames Smart 	dd_data->context_un.iocb.set_job = job;
6094cc0e56eSJames Smart 	dd_data->context_un.iocb.bmp = NULL;;
6104cc0e56eSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
611f1c3b0fcSJames Smart 
6124cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
6134cc0e56eSJames Smart 		creg_val = readl(phba->HCregaddr);
6144cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
6154cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
6164cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
6174cc0e56eSJames Smart 	}
6184cc0e56eSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
619f1c3b0fcSJames Smart 	lpfc_nlp_put(ndlp);
6204cc0e56eSJames Smart 	if (rc == IOCB_SUCCESS)
6214cc0e56eSJames Smart 		return 0; /* done for now */
6224cc0e56eSJames Smart 
623f1c3b0fcSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
624f1c3b0fcSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
625f1c3b0fcSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
626f1c3b0fcSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
627f1c3b0fcSJames Smart 
6284cc0e56eSJames Smart 	lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
629f1c3b0fcSJames Smart 
6304cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
631f1c3b0fcSJames Smart 
6324cc0e56eSJames Smart free_rspiocbq:
633f1c3b0fcSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
634f1c3b0fcSJames Smart 
6354cc0e56eSJames Smart free_dd_data:
6364cc0e56eSJames Smart 	kfree(dd_data);
6374cc0e56eSJames Smart 
6384cc0e56eSJames Smart no_dd_data:
639f1c3b0fcSJames Smart 	/* make error code available to userspace */
640f1c3b0fcSJames Smart 	job->reply->result = rc;
6414cc0e56eSJames Smart 	job->dd_data = NULL;
6424cc0e56eSJames Smart 	return rc;
643f1c3b0fcSJames Smart }
644f1c3b0fcSJames Smart 
6453b5dd52aSJames Smart /**
6463b5dd52aSJames Smart  * lpfc_bsg_event_free - frees an allocated event structure
6473b5dd52aSJames Smart  * @kref: Pointer to a kref.
6483b5dd52aSJames Smart  *
6493b5dd52aSJames Smart  * Called from kref_put. Back cast the kref into an event structure address.
6503b5dd52aSJames Smart  * Free any events to get, delete associated nodes, free any events to see,
6513b5dd52aSJames Smart  * free any data then free the event itself.
6523b5dd52aSJames Smart  **/
653f1c3b0fcSJames Smart static void
6544cc0e56eSJames Smart lpfc_bsg_event_free(struct kref *kref)
655f1c3b0fcSJames Smart {
6564cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
6574cc0e56eSJames Smart 						  kref);
658f1c3b0fcSJames Smart 	struct event_data *ed;
659f1c3b0fcSJames Smart 
660f1c3b0fcSJames Smart 	list_del(&evt->node);
661f1c3b0fcSJames Smart 
662f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_get)) {
663f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
664f1c3b0fcSJames Smart 		list_del(&ed->node);
665f1c3b0fcSJames Smart 		kfree(ed->data);
666f1c3b0fcSJames Smart 		kfree(ed);
667f1c3b0fcSJames Smart 	}
668f1c3b0fcSJames Smart 
669f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_see)) {
670f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
671f1c3b0fcSJames Smart 		list_del(&ed->node);
672f1c3b0fcSJames Smart 		kfree(ed->data);
673f1c3b0fcSJames Smart 		kfree(ed);
674f1c3b0fcSJames Smart 	}
675f1c3b0fcSJames Smart 
676f1c3b0fcSJames Smart 	kfree(evt);
677f1c3b0fcSJames Smart }
678f1c3b0fcSJames Smart 
6793b5dd52aSJames Smart /**
6803b5dd52aSJames Smart  * lpfc_bsg_event_ref - increments the kref for an event
6813b5dd52aSJames Smart  * @evt: Pointer to an event structure.
6823b5dd52aSJames Smart  **/
683f1c3b0fcSJames Smart static inline void
6844cc0e56eSJames Smart lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
685f1c3b0fcSJames Smart {
6864cc0e56eSJames Smart 	kref_get(&evt->kref);
687f1c3b0fcSJames Smart }
688f1c3b0fcSJames Smart 
6893b5dd52aSJames Smart /**
6903b5dd52aSJames Smart  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
6913b5dd52aSJames Smart  * @evt: Pointer to an event structure.
6923b5dd52aSJames Smart  **/
693f1c3b0fcSJames Smart static inline void
6944cc0e56eSJames Smart lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
695f1c3b0fcSJames Smart {
6964cc0e56eSJames Smart 	kref_put(&evt->kref, lpfc_bsg_event_free);
697f1c3b0fcSJames Smart }
698f1c3b0fcSJames Smart 
6993b5dd52aSJames Smart /**
7003b5dd52aSJames Smart  * lpfc_bsg_event_new - allocate and initialize a event structure
7013b5dd52aSJames Smart  * @ev_mask: Mask of events.
7023b5dd52aSJames Smart  * @ev_reg_id: Event reg id.
7033b5dd52aSJames Smart  * @ev_req_id: Event request id.
7043b5dd52aSJames Smart  **/
7054cc0e56eSJames Smart static struct lpfc_bsg_event *
7064cc0e56eSJames Smart lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
7074cc0e56eSJames Smart {
7084cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
709f1c3b0fcSJames Smart 
7104cc0e56eSJames Smart 	if (!evt)
7114cc0e56eSJames Smart 		return NULL;
7124cc0e56eSJames Smart 
7134cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_get);
7144cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_see);
7154cc0e56eSJames Smart 	evt->type_mask = ev_mask;
7164cc0e56eSJames Smart 	evt->req_id = ev_req_id;
7174cc0e56eSJames Smart 	evt->reg_id = ev_reg_id;
7184cc0e56eSJames Smart 	evt->wait_time_stamp = jiffies;
7194cc0e56eSJames Smart 	init_waitqueue_head(&evt->wq);
7204cc0e56eSJames Smart 	kref_init(&evt->kref);
7214cc0e56eSJames Smart 	return evt;
7224cc0e56eSJames Smart }
7234cc0e56eSJames Smart 
7243b5dd52aSJames Smart /**
7253b5dd52aSJames Smart  * diag_cmd_data_free - Frees an lpfc dma buffer extension
7263b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
7273b5dd52aSJames Smart  * @mlist: Pointer to an lpfc dma buffer extension.
7283b5dd52aSJames Smart  **/
7294cc0e56eSJames Smart static int
7303b5dd52aSJames Smart diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
7314cc0e56eSJames Smart {
7324cc0e56eSJames Smart 	struct lpfc_dmabufext *mlast;
7334cc0e56eSJames Smart 	struct pci_dev *pcidev;
7344cc0e56eSJames Smart 	struct list_head head, *curr, *next;
7354cc0e56eSJames Smart 
7364cc0e56eSJames Smart 	if ((!mlist) || (!lpfc_is_link_up(phba) &&
7374cc0e56eSJames Smart 		(phba->link_flag & LS_LOOPBACK_MODE))) {
7384cc0e56eSJames Smart 		return 0;
7394cc0e56eSJames Smart 	}
7404cc0e56eSJames Smart 
7414cc0e56eSJames Smart 	pcidev = phba->pcidev;
7424cc0e56eSJames Smart 	list_add_tail(&head, &mlist->dma.list);
7434cc0e56eSJames Smart 
7444cc0e56eSJames Smart 	list_for_each_safe(curr, next, &head) {
7454cc0e56eSJames Smart 		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
7464cc0e56eSJames Smart 		if (mlast->dma.virt)
7474cc0e56eSJames Smart 			dma_free_coherent(&pcidev->dev,
7484cc0e56eSJames Smart 					  mlast->size,
7494cc0e56eSJames Smart 					  mlast->dma.virt,
7504cc0e56eSJames Smart 					  mlast->dma.phys);
7514cc0e56eSJames Smart 		kfree(mlast);
7524cc0e56eSJames Smart 	}
7534cc0e56eSJames Smart 	return 0;
7544cc0e56eSJames Smart }
755f1c3b0fcSJames Smart 
756f1c3b0fcSJames Smart /**
757f1c3b0fcSJames Smart  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
758f1c3b0fcSJames Smart  * @phba:
759f1c3b0fcSJames Smart  * @pring:
760f1c3b0fcSJames Smart  * @piocbq:
761f1c3b0fcSJames Smart  *
762f1c3b0fcSJames Smart  * This function is called when an unsolicited CT command is received.  It
7634cc0e56eSJames Smart  * forwards the event to any processes registered to receive CT events.
7643b5dd52aSJames Smart  **/
7654fede78fSJames Smart int
766f1c3b0fcSJames Smart lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
767f1c3b0fcSJames Smart 			struct lpfc_iocbq *piocbq)
768f1c3b0fcSJames Smart {
769f1c3b0fcSJames Smart 	uint32_t evt_req_id = 0;
770f1c3b0fcSJames Smart 	uint32_t cmd;
771f1c3b0fcSJames Smart 	uint32_t len;
772f1c3b0fcSJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
7734cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
774f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
775f1c3b0fcSJames Smart 	struct lpfc_iocbq *iocbq;
776f1c3b0fcSJames Smart 	size_t offset = 0;
777f1c3b0fcSJames Smart 	struct list_head head;
778f1c3b0fcSJames Smart 	struct ulp_bde64 *bde;
779f1c3b0fcSJames Smart 	dma_addr_t dma_addr;
780f1c3b0fcSJames Smart 	int i;
781f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
782f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
783f1c3b0fcSJames Smart 	struct lpfc_hbq_entry *hbqe;
784f1c3b0fcSJames Smart 	struct lpfc_sli_ct_request *ct_req;
7854cc0e56eSJames Smart 	struct fc_bsg_job *job = NULL;
7864fede78fSJames Smart 	unsigned long flags;
7874cc0e56eSJames Smart 	int size = 0;
788f1c3b0fcSJames Smart 
789f1c3b0fcSJames Smart 	INIT_LIST_HEAD(&head);
790f1c3b0fcSJames Smart 	list_add_tail(&head, &piocbq->list);
791f1c3b0fcSJames Smart 
792f1c3b0fcSJames Smart 	if (piocbq->iocb.ulpBdeCount == 0 ||
793f1c3b0fcSJames Smart 	    piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
794f1c3b0fcSJames Smart 		goto error_ct_unsol_exit;
795f1c3b0fcSJames Smart 
7964cc0e56eSJames Smart 	if (phba->link_state == LPFC_HBA_ERROR ||
7974cc0e56eSJames Smart 		(!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
7984cc0e56eSJames Smart 		goto error_ct_unsol_exit;
7994cc0e56eSJames Smart 
800f1c3b0fcSJames Smart 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
801f1c3b0fcSJames Smart 		dmabuf = bdeBuf1;
802f1c3b0fcSJames Smart 	else {
803f1c3b0fcSJames Smart 		dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
804f1c3b0fcSJames Smart 				    piocbq->iocb.un.cont64[0].addrLow);
805f1c3b0fcSJames Smart 		dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
806f1c3b0fcSJames Smart 	}
8074cc0e56eSJames Smart 	if (dmabuf == NULL)
8084cc0e56eSJames Smart 		goto error_ct_unsol_exit;
809f1c3b0fcSJames Smart 	ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
810f1c3b0fcSJames Smart 	evt_req_id = ct_req->FsType;
811f1c3b0fcSJames Smart 	cmd = ct_req->CommandResponse.bits.CmdRsp;
812f1c3b0fcSJames Smart 	len = ct_req->CommandResponse.bits.Size;
813f1c3b0fcSJames Smart 	if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
814f1c3b0fcSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
815f1c3b0fcSJames Smart 
8164fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
817f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
8184cc0e56eSJames Smart 		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
8194cc0e56eSJames Smart 			evt->req_id != evt_req_id)
820f1c3b0fcSJames Smart 			continue;
821f1c3b0fcSJames Smart 
8224cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
8234cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
824f1c3b0fcSJames Smart 		evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
8254cc0e56eSJames Smart 		if (evt_dat == NULL) {
8264cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
8274cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
828f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
829f1c3b0fcSJames Smart 					"2614 Memory allocation failed for "
830f1c3b0fcSJames Smart 					"CT event\n");
831f1c3b0fcSJames Smart 			break;
832f1c3b0fcSJames Smart 		}
833f1c3b0fcSJames Smart 
834f1c3b0fcSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
835f1c3b0fcSJames Smart 			/* take accumulated byte count from the last iocbq */
836f1c3b0fcSJames Smart 			iocbq = list_entry(head.prev, typeof(*iocbq), list);
837f1c3b0fcSJames Smart 			evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
838f1c3b0fcSJames Smart 		} else {
839f1c3b0fcSJames Smart 			list_for_each_entry(iocbq, &head, list) {
840f1c3b0fcSJames Smart 				for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
841f1c3b0fcSJames Smart 					evt_dat->len +=
842f1c3b0fcSJames Smart 					iocbq->iocb.un.cont64[i].tus.f.bdeSize;
843f1c3b0fcSJames Smart 			}
844f1c3b0fcSJames Smart 		}
845f1c3b0fcSJames Smart 
846f1c3b0fcSJames Smart 		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
8474cc0e56eSJames Smart 		if (evt_dat->data == NULL) {
848f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
849f1c3b0fcSJames Smart 					"2615 Memory allocation failed for "
850f1c3b0fcSJames Smart 					"CT event data, size %d\n",
851f1c3b0fcSJames Smart 					evt_dat->len);
852f1c3b0fcSJames Smart 			kfree(evt_dat);
8534fede78fSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
8544cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
8554fede78fSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
856f1c3b0fcSJames Smart 			goto error_ct_unsol_exit;
857f1c3b0fcSJames Smart 		}
858f1c3b0fcSJames Smart 
859f1c3b0fcSJames Smart 		list_for_each_entry(iocbq, &head, list) {
8604cc0e56eSJames Smart 			size = 0;
861f1c3b0fcSJames Smart 			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
862f1c3b0fcSJames Smart 				bdeBuf1 = iocbq->context2;
863f1c3b0fcSJames Smart 				bdeBuf2 = iocbq->context3;
864f1c3b0fcSJames Smart 			}
865f1c3b0fcSJames Smart 			for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
866f1c3b0fcSJames Smart 				if (phba->sli3_options &
867f1c3b0fcSJames Smart 				    LPFC_SLI3_HBQ_ENABLED) {
868f1c3b0fcSJames Smart 					if (i == 0) {
869f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
870f1c3b0fcSJames Smart 						  &iocbq->iocb.un.ulpWord[0];
871f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
872f1c3b0fcSJames Smart 						dmabuf = bdeBuf1;
873f1c3b0fcSJames Smart 					} else if (i == 1) {
874f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
875f1c3b0fcSJames Smart 							&iocbq->iocb.unsli3.
876f1c3b0fcSJames Smart 							sli3Words[4];
877f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
878f1c3b0fcSJames Smart 						dmabuf = bdeBuf2;
879f1c3b0fcSJames Smart 					}
880f1c3b0fcSJames Smart 					if ((offset + size) > evt_dat->len)
881f1c3b0fcSJames Smart 						size = evt_dat->len - offset;
882f1c3b0fcSJames Smart 				} else {
883f1c3b0fcSJames Smart 					size = iocbq->iocb.un.cont64[i].
884f1c3b0fcSJames Smart 						tus.f.bdeSize;
885f1c3b0fcSJames Smart 					bde = &iocbq->iocb.un.cont64[i];
886f1c3b0fcSJames Smart 					dma_addr = getPaddr(bde->addrHigh,
887f1c3b0fcSJames Smart 							    bde->addrLow);
888f1c3b0fcSJames Smart 					dmabuf = lpfc_sli_ringpostbuf_get(phba,
889f1c3b0fcSJames Smart 							pring, dma_addr);
890f1c3b0fcSJames Smart 				}
891f1c3b0fcSJames Smart 				if (!dmabuf) {
892f1c3b0fcSJames Smart 					lpfc_printf_log(phba, KERN_ERR,
893f1c3b0fcSJames Smart 						LOG_LIBDFC, "2616 No dmabuf "
894f1c3b0fcSJames Smart 						"found for iocbq 0x%p\n",
895f1c3b0fcSJames Smart 						iocbq);
896f1c3b0fcSJames Smart 					kfree(evt_dat->data);
897f1c3b0fcSJames Smart 					kfree(evt_dat);
8984fede78fSJames Smart 					spin_lock_irqsave(&phba->ct_ev_lock,
8994fede78fSJames Smart 						flags);
9004cc0e56eSJames Smart 					lpfc_bsg_event_unref(evt);
9014fede78fSJames Smart 					spin_unlock_irqrestore(
9024fede78fSJames Smart 						&phba->ct_ev_lock, flags);
903f1c3b0fcSJames Smart 					goto error_ct_unsol_exit;
904f1c3b0fcSJames Smart 				}
905f1c3b0fcSJames Smart 				memcpy((char *)(evt_dat->data) + offset,
906f1c3b0fcSJames Smart 				       dmabuf->virt, size);
907f1c3b0fcSJames Smart 				offset += size;
908f1c3b0fcSJames Smart 				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
909f1c3b0fcSJames Smart 				    !(phba->sli3_options &
910f1c3b0fcSJames Smart 				      LPFC_SLI3_HBQ_ENABLED)) {
911f1c3b0fcSJames Smart 					lpfc_sli_ringpostbuf_put(phba, pring,
912f1c3b0fcSJames Smart 								 dmabuf);
913f1c3b0fcSJames Smart 				} else {
914f1c3b0fcSJames Smart 					switch (cmd) {
9154cc0e56eSJames Smart 					case ELX_LOOPBACK_DATA:
9163b5dd52aSJames Smart 						diag_cmd_data_free(phba,
9174cc0e56eSJames Smart 						(struct lpfc_dmabufext *)
9184cc0e56eSJames Smart 							dmabuf);
9194cc0e56eSJames Smart 						break;
920f1c3b0fcSJames Smart 					case ELX_LOOPBACK_XRI_SETUP:
9214cc0e56eSJames Smart 						if ((phba->sli_rev ==
9224cc0e56eSJames Smart 							LPFC_SLI_REV2) ||
9234cc0e56eSJames Smart 							(phba->sli3_options &
9244cc0e56eSJames Smart 							LPFC_SLI3_HBQ_ENABLED
9254cc0e56eSJames Smart 							)) {
9264cc0e56eSJames Smart 							lpfc_in_buf_free(phba,
9274cc0e56eSJames Smart 									dmabuf);
9284cc0e56eSJames Smart 						} else {
929f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
930f1c3b0fcSJames Smart 									 pring,
931f1c3b0fcSJames Smart 									 1);
9324cc0e56eSJames Smart 						}
933f1c3b0fcSJames Smart 						break;
934f1c3b0fcSJames Smart 					default:
935f1c3b0fcSJames Smart 						if (!(phba->sli3_options &
936f1c3b0fcSJames Smart 						      LPFC_SLI3_HBQ_ENABLED))
937f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
938f1c3b0fcSJames Smart 									 pring,
939f1c3b0fcSJames Smart 									 1);
940f1c3b0fcSJames Smart 						break;
941f1c3b0fcSJames Smart 					}
942f1c3b0fcSJames Smart 				}
943f1c3b0fcSJames Smart 			}
944f1c3b0fcSJames Smart 		}
945f1c3b0fcSJames Smart 
9464fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
947f1c3b0fcSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4) {
948f1c3b0fcSJames Smart 			evt_dat->immed_dat = phba->ctx_idx;
949f1c3b0fcSJames Smart 			phba->ctx_idx = (phba->ctx_idx + 1) % 64;
950f1c3b0fcSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].oxid =
951f1c3b0fcSJames Smart 						piocbq->iocb.ulpContext;
952f1c3b0fcSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].SID =
953f1c3b0fcSJames Smart 				piocbq->iocb.un.rcvels.remoteID;
954f1c3b0fcSJames Smart 		} else
955f1c3b0fcSJames Smart 			evt_dat->immed_dat = piocbq->iocb.ulpContext;
956f1c3b0fcSJames Smart 
957f1c3b0fcSJames Smart 		evt_dat->type = FC_REG_CT_EVENT;
958f1c3b0fcSJames Smart 		list_add(&evt_dat->node, &evt->events_to_see);
9594cc0e56eSJames Smart 		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
960f1c3b0fcSJames Smart 			wake_up_interruptible(&evt->wq);
9614cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
962f1c3b0fcSJames Smart 			break;
963f1c3b0fcSJames Smart 		}
9644cc0e56eSJames Smart 
9654cc0e56eSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
9664cc0e56eSJames Smart 		lpfc_bsg_event_unref(evt);
9674cc0e56eSJames Smart 
9684cc0e56eSJames Smart 		job = evt->set_job;
9694cc0e56eSJames Smart 		evt->set_job = NULL;
9704cc0e56eSJames Smart 		if (job) {
9714cc0e56eSJames Smart 			job->reply->reply_payload_rcv_len = size;
9724cc0e56eSJames Smart 			/* make error code available to userspace */
9734cc0e56eSJames Smart 			job->reply->result = 0;
9744cc0e56eSJames Smart 			job->dd_data = NULL;
9754cc0e56eSJames Smart 			/* complete the job back to userspace */
9764cc0e56eSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
9774cc0e56eSJames Smart 			job->job_done(job);
9784cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
9794cc0e56eSJames Smart 		}
9804cc0e56eSJames Smart 	}
9814fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
982f1c3b0fcSJames Smart 
983f1c3b0fcSJames Smart error_ct_unsol_exit:
984f1c3b0fcSJames Smart 	if (!list_empty(&head))
985f1c3b0fcSJames Smart 		list_del(&head);
9864cc0e56eSJames Smart 	if (evt_req_id == SLI_CT_ELX_LOOPBACK)
9874cc0e56eSJames Smart 		return 0;
9884fede78fSJames Smart 	return 1;
989f1c3b0fcSJames Smart }
990f1c3b0fcSJames Smart 
991f1c3b0fcSJames Smart /**
9924cc0e56eSJames Smart  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
993f1c3b0fcSJames Smart  * @job: SET_EVENT fc_bsg_job
9943b5dd52aSJames Smart  **/
995f1c3b0fcSJames Smart static int
9964cc0e56eSJames Smart lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
997f1c3b0fcSJames Smart {
998f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
999f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
1000f1c3b0fcSJames Smart 	struct set_ct_event *event_req;
10014cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
1002f1c3b0fcSJames Smart 	int rc = 0;
10034cc0e56eSJames Smart 	struct bsg_job_data *dd_data = NULL;
10044cc0e56eSJames Smart 	uint32_t ev_mask;
10054cc0e56eSJames Smart 	unsigned long flags;
1006f1c3b0fcSJames Smart 
1007f1c3b0fcSJames Smart 	if (job->request_len <
1008f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1009f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1010f1c3b0fcSJames Smart 				"2612 Received SET_CT_EVENT below minimum "
1011f1c3b0fcSJames Smart 				"size\n");
10124cc0e56eSJames Smart 		rc = -EINVAL;
10134cc0e56eSJames Smart 		goto job_error;
10144cc0e56eSJames Smart 	}
10154cc0e56eSJames Smart 
10164cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
10174cc0e56eSJames Smart 	if (dd_data == NULL) {
10184cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
10194cc0e56eSJames Smart 				"2734 Failed allocation of dd_data\n");
10204cc0e56eSJames Smart 		rc = -ENOMEM;
10214cc0e56eSJames Smart 		goto job_error;
1022f1c3b0fcSJames Smart 	}
1023f1c3b0fcSJames Smart 
1024f1c3b0fcSJames Smart 	event_req = (struct set_ct_event *)
1025f1c3b0fcSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
10264cc0e56eSJames Smart 	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
10274cc0e56eSJames Smart 				FC_REG_EVENT_MASK);
10284fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1029f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1030f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
10314cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1032f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1033f1c3b0fcSJames Smart 			break;
1034f1c3b0fcSJames Smart 		}
1035f1c3b0fcSJames Smart 	}
10364fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1037f1c3b0fcSJames Smart 
1038f1c3b0fcSJames Smart 	if (&evt->node == &phba->ct_ev_waiters) {
1039f1c3b0fcSJames Smart 		/* no event waiting struct yet - first call */
10404cc0e56eSJames Smart 		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1041f1c3b0fcSJames Smart 					event_req->ev_req_id);
1042f1c3b0fcSJames Smart 		if (!evt) {
1043f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1044f1c3b0fcSJames Smart 					"2617 Failed allocation of event "
1045f1c3b0fcSJames Smart 					"waiter\n");
10464cc0e56eSJames Smart 			rc = -ENOMEM;
10474cc0e56eSJames Smart 			goto job_error;
1048f1c3b0fcSJames Smart 		}
1049f1c3b0fcSJames Smart 
10504fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1051f1c3b0fcSJames Smart 		list_add(&evt->node, &phba->ct_ev_waiters);
10524cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
1053f1c3b0fcSJames Smart 		evt->wait_time_stamp = jiffies;
10544cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
10554cc0e56eSJames Smart 	}
1056f1c3b0fcSJames Smart 
10574fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
10584cc0e56eSJames Smart 	evt->waiting = 1;
10594cc0e56eSJames Smart 	dd_data->type = TYPE_EVT;
10604cc0e56eSJames Smart 	dd_data->context_un.evt = evt;
10614cc0e56eSJames Smart 	evt->set_job = job; /* for unsolicited command */
10624cc0e56eSJames Smart 	job->dd_data = dd_data; /* for fc transport timeout callback*/
10634fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
10644cc0e56eSJames Smart 	return 0; /* call job done later */
1065f1c3b0fcSJames Smart 
10664cc0e56eSJames Smart job_error:
10674cc0e56eSJames Smart 	if (dd_data != NULL)
10684cc0e56eSJames Smart 		kfree(dd_data);
1069f1c3b0fcSJames Smart 
10704cc0e56eSJames Smart 	job->dd_data = NULL;
10714cc0e56eSJames Smart 	return rc;
1072f1c3b0fcSJames Smart }
1073f1c3b0fcSJames Smart 
1074f1c3b0fcSJames Smart /**
10754cc0e56eSJames Smart  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1076f1c3b0fcSJames Smart  * @job: GET_EVENT fc_bsg_job
10773b5dd52aSJames Smart  **/
1078f1c3b0fcSJames Smart static int
10794cc0e56eSJames Smart lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1080f1c3b0fcSJames Smart {
1081f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1082f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
1083f1c3b0fcSJames Smart 	struct get_ct_event *event_req;
1084f1c3b0fcSJames Smart 	struct get_ct_event_reply *event_reply;
10854cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
1086f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
10874fede78fSJames Smart 	unsigned long flags;
10884cc0e56eSJames Smart 	uint32_t rc = 0;
1089f1c3b0fcSJames Smart 
1090f1c3b0fcSJames Smart 	if (job->request_len <
1091f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1092f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1093f1c3b0fcSJames Smart 				"2613 Received GET_CT_EVENT request below "
1094f1c3b0fcSJames Smart 				"minimum size\n");
10954cc0e56eSJames Smart 		rc = -EINVAL;
10964cc0e56eSJames Smart 		goto job_error;
1097f1c3b0fcSJames Smart 	}
1098f1c3b0fcSJames Smart 
1099f1c3b0fcSJames Smart 	event_req = (struct get_ct_event *)
1100f1c3b0fcSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
1101f1c3b0fcSJames Smart 
1102f1c3b0fcSJames Smart 	event_reply = (struct get_ct_event_reply *)
1103f1c3b0fcSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
11044fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1105f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1106f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
1107f1c3b0fcSJames Smart 			if (list_empty(&evt->events_to_get))
1108f1c3b0fcSJames Smart 				break;
11094cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1110f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1111f1c3b0fcSJames Smart 			evt_dat = list_entry(evt->events_to_get.prev,
1112f1c3b0fcSJames Smart 					     struct event_data, node);
1113f1c3b0fcSJames Smart 			list_del(&evt_dat->node);
1114f1c3b0fcSJames Smart 			break;
1115f1c3b0fcSJames Smart 		}
1116f1c3b0fcSJames Smart 	}
11174fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1118f1c3b0fcSJames Smart 
11194cc0e56eSJames Smart 	/* The app may continue to ask for event data until it gets
11204cc0e56eSJames Smart 	 * an error indicating that there isn't anymore
11214cc0e56eSJames Smart 	 */
11224cc0e56eSJames Smart 	if (evt_dat == NULL) {
1123f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len = 0;
1124f1c3b0fcSJames Smart 		rc = -ENOENT;
11254cc0e56eSJames Smart 		goto job_error;
1126f1c3b0fcSJames Smart 	}
1127f1c3b0fcSJames Smart 
11284cc0e56eSJames Smart 	if (evt_dat->len > job->request_payload.payload_len) {
11294cc0e56eSJames Smart 		evt_dat->len = job->request_payload.payload_len;
1130f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1131f1c3b0fcSJames Smart 				"2618 Truncated event data at %d "
1132f1c3b0fcSJames Smart 				"bytes\n",
11334cc0e56eSJames Smart 				job->request_payload.payload_len);
1134f1c3b0fcSJames Smart 	}
1135f1c3b0fcSJames Smart 
11364cc0e56eSJames Smart 	event_reply->type = evt_dat->type;
1137f1c3b0fcSJames Smart 	event_reply->immed_data = evt_dat->immed_dat;
1138f1c3b0fcSJames Smart 	if (evt_dat->len > 0)
1139f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len =
11404cc0e56eSJames Smart 			sg_copy_from_buffer(job->request_payload.sg_list,
11414cc0e56eSJames Smart 					    job->request_payload.sg_cnt,
1142f1c3b0fcSJames Smart 					    evt_dat->data, evt_dat->len);
1143f1c3b0fcSJames Smart 	else
1144f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len = 0;
1145f1c3b0fcSJames Smart 
11464cc0e56eSJames Smart 	if (evt_dat) {
1147f1c3b0fcSJames Smart 		kfree(evt_dat->data);
1148f1c3b0fcSJames Smart 		kfree(evt_dat);
11494cc0e56eSJames Smart 	}
11504cc0e56eSJames Smart 
11514fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
11524cc0e56eSJames Smart 	lpfc_bsg_event_unref(evt);
11534fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
11544cc0e56eSJames Smart 	job->dd_data = NULL;
11554cc0e56eSJames Smart 	job->reply->result = 0;
1156f1c3b0fcSJames Smart 	job->job_done(job);
11574cc0e56eSJames Smart 	return 0;
1158f1c3b0fcSJames Smart 
11594cc0e56eSJames Smart job_error:
11604cc0e56eSJames Smart 	job->dd_data = NULL;
11614cc0e56eSJames Smart 	job->reply->result = rc;
1162f1c3b0fcSJames Smart 	return rc;
1163f1c3b0fcSJames Smart }
1164f1c3b0fcSJames Smart 
1165f1c3b0fcSJames Smart /**
11663b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
11673b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
11683b5dd52aSJames Smart  * @cmdiocbq: Pointer to command iocb.
11693b5dd52aSJames Smart  * @rspiocbq: Pointer to response iocb.
11703b5dd52aSJames Smart  *
11713b5dd52aSJames Smart  * This function is the completion handler for iocbs issued using
11723b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp function. This function is called by the
11733b5dd52aSJames Smart  * ring event handler function without any lock held. This function
11743b5dd52aSJames Smart  * can be called from both worker thread context and interrupt
11753b5dd52aSJames Smart  * context. This function also can be called from other thread which
11763b5dd52aSJames Smart  * cleans up the SLI layer objects.
11773b5dd52aSJames Smart  * This function copy the contents of the response iocb to the
11783b5dd52aSJames Smart  * response iocb memory object provided by the caller of
11793b5dd52aSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
11803b5dd52aSJames Smart  * sleeps for the iocb completion.
11813b5dd52aSJames Smart  **/
11823b5dd52aSJames Smart static void
11833b5dd52aSJames Smart lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
11843b5dd52aSJames Smart 			struct lpfc_iocbq *cmdiocbq,
11853b5dd52aSJames Smart 			struct lpfc_iocbq *rspiocbq)
11863b5dd52aSJames Smart {
11873b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
11883b5dd52aSJames Smart 	struct fc_bsg_job *job;
11893b5dd52aSJames Smart 	IOCB_t *rsp;
11903b5dd52aSJames Smart 	struct lpfc_dmabuf *bmp;
11913b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp;
11923b5dd52aSJames Smart 	unsigned long flags;
11933b5dd52aSJames Smart 	int rc = 0;
11943b5dd52aSJames Smart 
11953b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
11963b5dd52aSJames Smart 	dd_data = cmdiocbq->context1;
11973b5dd52aSJames Smart 	/* normal completion and timeout crossed paths, already done */
11983b5dd52aSJames Smart 	if (!dd_data) {
11993b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
12003b5dd52aSJames Smart 		return;
12013b5dd52aSJames Smart 	}
12023b5dd52aSJames Smart 
12033b5dd52aSJames Smart 	job = dd_data->context_un.iocb.set_job;
12043b5dd52aSJames Smart 	bmp = dd_data->context_un.iocb.bmp;
12053b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
12063b5dd52aSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
12073b5dd52aSJames Smart 
12083b5dd52aSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
12093b5dd52aSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
12103b5dd52aSJames Smart 
12113b5dd52aSJames Smart 	if (rsp->ulpStatus) {
12123b5dd52aSJames Smart 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
12133b5dd52aSJames Smart 			switch (rsp->un.ulpWord[4] & 0xff) {
12143b5dd52aSJames Smart 			case IOERR_SEQUENCE_TIMEOUT:
12153b5dd52aSJames Smart 				rc = -ETIMEDOUT;
12163b5dd52aSJames Smart 				break;
12173b5dd52aSJames Smart 			case IOERR_INVALID_RPI:
12183b5dd52aSJames Smart 				rc = -EFAULT;
12193b5dd52aSJames Smart 				break;
12203b5dd52aSJames Smart 			default:
12213b5dd52aSJames Smart 				rc = -EACCES;
12223b5dd52aSJames Smart 				break;
12233b5dd52aSJames Smart 			}
12243b5dd52aSJames Smart 		} else
12253b5dd52aSJames Smart 			rc = -EACCES;
12263b5dd52aSJames Smart 	} else
12273b5dd52aSJames Smart 		job->reply->reply_payload_rcv_len =
12283b5dd52aSJames Smart 			rsp->un.genreq64.bdl.bdeSize;
12293b5dd52aSJames Smart 
12303b5dd52aSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
12313b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
12323b5dd52aSJames Smart 	lpfc_nlp_put(ndlp);
12333b5dd52aSJames Smart 	kfree(bmp);
12343b5dd52aSJames Smart 	kfree(dd_data);
12353b5dd52aSJames Smart 	/* make error code available to userspace */
12363b5dd52aSJames Smart 	job->reply->result = rc;
12373b5dd52aSJames Smart 	job->dd_data = NULL;
12383b5dd52aSJames Smart 	/* complete the job back to userspace */
12393b5dd52aSJames Smart 	job->job_done(job);
12403b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
12413b5dd52aSJames Smart 	return;
12423b5dd52aSJames Smart }
12433b5dd52aSJames Smart 
12443b5dd52aSJames Smart /**
12453b5dd52aSJames Smart  * lpfc_issue_ct_rsp - issue a ct response
12463b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
12473b5dd52aSJames Smart  * @job: Pointer to the job object.
12483b5dd52aSJames Smart  * @tag: tag index value into the ports context exchange array.
12493b5dd52aSJames Smart  * @bmp: Pointer to a dma buffer descriptor.
12503b5dd52aSJames Smart  * @num_entry: Number of enties in the bde.
12513b5dd52aSJames Smart  **/
12523b5dd52aSJames Smart static int
12533b5dd52aSJames Smart lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
12543b5dd52aSJames Smart 		  struct lpfc_dmabuf *bmp, int num_entry)
12553b5dd52aSJames Smart {
12563b5dd52aSJames Smart 	IOCB_t *icmd;
12573b5dd52aSJames Smart 	struct lpfc_iocbq *ctiocb = NULL;
12583b5dd52aSJames Smart 	int rc = 0;
12593b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp = NULL;
12603b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
12613b5dd52aSJames Smart 	uint32_t creg_val;
12623b5dd52aSJames Smart 
12633b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
12643b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
12653b5dd52aSJames Smart 	if (!dd_data) {
12663b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
12673b5dd52aSJames Smart 				"2736 Failed allocation of dd_data\n");
12683b5dd52aSJames Smart 		rc = -ENOMEM;
12693b5dd52aSJames Smart 		goto no_dd_data;
12703b5dd52aSJames Smart 	}
12713b5dd52aSJames Smart 
12723b5dd52aSJames Smart 	/* Allocate buffer for  command iocb */
12733b5dd52aSJames Smart 	ctiocb = lpfc_sli_get_iocbq(phba);
12743b5dd52aSJames Smart 	if (!ctiocb) {
12753b5dd52aSJames Smart 		rc = ENOMEM;
12763b5dd52aSJames Smart 		goto no_ctiocb;
12773b5dd52aSJames Smart 	}
12783b5dd52aSJames Smart 
12793b5dd52aSJames Smart 	icmd = &ctiocb->iocb;
12803b5dd52aSJames Smart 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
12813b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
12823b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
12833b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
12843b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
12853b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
12863b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
12873b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
12883b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
12893b5dd52aSJames Smart 
12903b5dd52aSJames Smart 	/* Fill in rest of iocb */
12913b5dd52aSJames Smart 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
12923b5dd52aSJames Smart 	icmd->ulpBdeCount = 1;
12933b5dd52aSJames Smart 	icmd->ulpLe = 1;
12943b5dd52aSJames Smart 	icmd->ulpClass = CLASS3;
12953b5dd52aSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
12963b5dd52aSJames Smart 		/* Do not issue unsol response if oxid not marked as valid */
12973b5dd52aSJames Smart 		if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
12983b5dd52aSJames Smart 			rc = IOCB_ERROR;
12993b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
13003b5dd52aSJames Smart 		}
13013b5dd52aSJames Smart 		icmd->ulpContext = phba->ct_ctx[tag].oxid;
13023b5dd52aSJames Smart 		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
13033b5dd52aSJames Smart 		if (!ndlp) {
13043b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13053b5dd52aSJames Smart 				 "2721 ndlp null for oxid %x SID %x\n",
13063b5dd52aSJames Smart 					icmd->ulpContext,
13073b5dd52aSJames Smart 					phba->ct_ctx[tag].SID);
13083b5dd52aSJames Smart 			rc = IOCB_ERROR;
13093b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
13103b5dd52aSJames Smart 		}
13113b5dd52aSJames Smart 		icmd->un.ulpWord[3] = ndlp->nlp_rpi;
13123b5dd52aSJames Smart 		/* The exchange is done, mark the entry as invalid */
13133b5dd52aSJames Smart 		phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
13143b5dd52aSJames Smart 	} else
13153b5dd52aSJames Smart 		icmd->ulpContext = (ushort) tag;
13163b5dd52aSJames Smart 
13173b5dd52aSJames Smart 	icmd->ulpTimeout = phba->fc_ratov * 2;
13183b5dd52aSJames Smart 
13193b5dd52aSJames Smart 	/* Xmit CT response on exchange <xid> */
13203b5dd52aSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13213b5dd52aSJames Smart 			"2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
13223b5dd52aSJames Smart 			icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
13233b5dd52aSJames Smart 
13243b5dd52aSJames Smart 	ctiocb->iocb_cmpl = NULL;
13253b5dd52aSJames Smart 	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
13263b5dd52aSJames Smart 	ctiocb->vport = phba->pport;
13273b5dd52aSJames Smart 	ctiocb->context3 = bmp;
13283b5dd52aSJames Smart 
13293b5dd52aSJames Smart 	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
13303b5dd52aSJames Smart 	ctiocb->context1 = dd_data;
13313b5dd52aSJames Smart 	ctiocb->context2 = NULL;
13323b5dd52aSJames Smart 	dd_data->type = TYPE_IOCB;
13333b5dd52aSJames Smart 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
13343b5dd52aSJames Smart 	dd_data->context_un.iocb.rspiocbq = NULL;
13353b5dd52aSJames Smart 	dd_data->context_un.iocb.set_job = job;
13363b5dd52aSJames Smart 	dd_data->context_un.iocb.bmp = bmp;
13373b5dd52aSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
13383b5dd52aSJames Smart 
13393b5dd52aSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
13403b5dd52aSJames Smart 		creg_val = readl(phba->HCregaddr);
13413b5dd52aSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
13423b5dd52aSJames Smart 		writel(creg_val, phba->HCregaddr);
13433b5dd52aSJames Smart 		readl(phba->HCregaddr); /* flush */
13443b5dd52aSJames Smart 	}
13453b5dd52aSJames Smart 
13463b5dd52aSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13473b5dd52aSJames Smart 
13483b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
13493b5dd52aSJames Smart 		return 0; /* done for now */
13503b5dd52aSJames Smart 
13513b5dd52aSJames Smart issue_ct_rsp_exit:
13523b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, ctiocb);
13533b5dd52aSJames Smart no_ctiocb:
13543b5dd52aSJames Smart 	kfree(dd_data);
13553b5dd52aSJames Smart no_dd_data:
13563b5dd52aSJames Smart 	return rc;
13573b5dd52aSJames Smart }
13583b5dd52aSJames Smart 
13593b5dd52aSJames Smart /**
13603b5dd52aSJames Smart  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
13613b5dd52aSJames Smart  * @job: SEND_MGMT_RESP fc_bsg_job
13623b5dd52aSJames Smart  **/
13633b5dd52aSJames Smart static int
13643b5dd52aSJames Smart lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
13653b5dd52aSJames Smart {
13663b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
13673b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
13683b5dd52aSJames Smart 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
13693b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
13703b5dd52aSJames Smart 	struct ulp_bde64 *bpl;
13713b5dd52aSJames Smart 	struct lpfc_dmabuf *bmp = NULL;
13723b5dd52aSJames Smart 	struct scatterlist *sgel = NULL;
13733b5dd52aSJames Smart 	int request_nseg;
13743b5dd52aSJames Smart 	int numbde;
13753b5dd52aSJames Smart 	dma_addr_t busaddr;
13763b5dd52aSJames Smart 	uint32_t tag = mgmt_resp->tag;
13773b5dd52aSJames Smart 	unsigned long reqbfrcnt =
13783b5dd52aSJames Smart 			(unsigned long)job->request_payload.payload_len;
13793b5dd52aSJames Smart 	int rc = 0;
13803b5dd52aSJames Smart 
13813b5dd52aSJames Smart 	/* in case no data is transferred */
13823b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
13833b5dd52aSJames Smart 
13843b5dd52aSJames Smart 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
13853b5dd52aSJames Smart 		rc = -ERANGE;
13863b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
13873b5dd52aSJames Smart 	}
13883b5dd52aSJames Smart 
13893b5dd52aSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
13903b5dd52aSJames Smart 	if (!bmp) {
13913b5dd52aSJames Smart 		rc = -ENOMEM;
13923b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
13933b5dd52aSJames Smart 	}
13943b5dd52aSJames Smart 
13953b5dd52aSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
13963b5dd52aSJames Smart 	if (!bmp->virt) {
13973b5dd52aSJames Smart 		rc = -ENOMEM;
13983b5dd52aSJames Smart 		goto send_mgmt_rsp_free_bmp;
13993b5dd52aSJames Smart 	}
14003b5dd52aSJames Smart 
14013b5dd52aSJames Smart 	INIT_LIST_HEAD(&bmp->list);
14023b5dd52aSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
14033b5dd52aSJames Smart 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
14043b5dd52aSJames Smart 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
14053b5dd52aSJames Smart 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
14063b5dd52aSJames Smart 		busaddr = sg_dma_address(sgel);
14073b5dd52aSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
14083b5dd52aSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
14093b5dd52aSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
14103b5dd52aSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
14113b5dd52aSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
14123b5dd52aSJames Smart 		bpl++;
14133b5dd52aSJames Smart 	}
14143b5dd52aSJames Smart 
14153b5dd52aSJames Smart 	rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
14163b5dd52aSJames Smart 
14173b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
14183b5dd52aSJames Smart 		return 0; /* done for now */
14193b5dd52aSJames Smart 
14203b5dd52aSJames Smart 	/* TBD need to handle a timeout */
14213b5dd52aSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
14223b5dd52aSJames Smart 			  job->request_payload.sg_cnt, DMA_TO_DEVICE);
14233b5dd52aSJames Smart 	rc = -EACCES;
14243b5dd52aSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
14253b5dd52aSJames Smart 
14263b5dd52aSJames Smart send_mgmt_rsp_free_bmp:
14273b5dd52aSJames Smart 	kfree(bmp);
14283b5dd52aSJames Smart send_mgmt_rsp_exit:
14293b5dd52aSJames Smart 	/* make error code available to userspace */
14303b5dd52aSJames Smart 	job->reply->result = rc;
14313b5dd52aSJames Smart 	job->dd_data = NULL;
14323b5dd52aSJames Smart 	return rc;
14333b5dd52aSJames Smart }
14343b5dd52aSJames Smart 
14353b5dd52aSJames Smart /**
14363b5dd52aSJames Smart  * lpfc_bsg_diag_mode - process a LPFC_BSG_VENDOR_DIAG_MODE bsg vendor command
14373b5dd52aSJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
14383b5dd52aSJames Smart  *
14393b5dd52aSJames Smart  * This function is responsible for placing a port into diagnostic loopback
14403b5dd52aSJames Smart  * mode in order to perform a diagnostic loopback test.
14413b5dd52aSJames Smart  * All new scsi requests are blocked, a small delay is used to allow the
14423b5dd52aSJames Smart  * scsi requests to complete then the link is brought down. If the link is
14433b5dd52aSJames Smart  * is placed in loopback mode then scsi requests are again allowed
14443b5dd52aSJames Smart  * so the scsi mid-layer doesn't give up on the port.
14453b5dd52aSJames Smart  * All of this is done in-line.
14463b5dd52aSJames Smart  */
14473b5dd52aSJames Smart static int
14483b5dd52aSJames Smart lpfc_bsg_diag_mode(struct fc_bsg_job *job)
14493b5dd52aSJames Smart {
14503b5dd52aSJames Smart 	struct Scsi_Host *shost = job->shost;
14513b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
14523b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
14533b5dd52aSJames Smart 	struct diag_mode_set *loopback_mode;
14543b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
14553b5dd52aSJames Smart 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
14563b5dd52aSJames Smart 	uint32_t link_flags;
14573b5dd52aSJames Smart 	uint32_t timeout;
14583b5dd52aSJames Smart 	struct lpfc_vport **vports;
14593b5dd52aSJames Smart 	LPFC_MBOXQ_t *pmboxq;
14603b5dd52aSJames Smart 	int mbxstatus;
14613b5dd52aSJames Smart 	int i = 0;
14623b5dd52aSJames Smart 	int rc = 0;
14633b5dd52aSJames Smart 
14643b5dd52aSJames Smart 	/* no data to return just the return code */
14653b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
14663b5dd52aSJames Smart 
14673b5dd52aSJames Smart 	if (job->request_len <
14683b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_set)) {
14693b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
14703b5dd52aSJames Smart 				"2738 Received DIAG MODE request below minimum "
14713b5dd52aSJames Smart 				"size\n");
14723b5dd52aSJames Smart 		rc = -EINVAL;
14733b5dd52aSJames Smart 		goto job_error;
14743b5dd52aSJames Smart 	}
14753b5dd52aSJames Smart 
14763b5dd52aSJames Smart 	loopback_mode = (struct diag_mode_set *)
14773b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
14783b5dd52aSJames Smart 	link_flags = loopback_mode->type;
14793b5dd52aSJames Smart 	timeout = loopback_mode->timeout;
14803b5dd52aSJames Smart 
14813b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
14823b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
14833b5dd52aSJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
14843b5dd52aSJames Smart 		rc = -EACCES;
14853b5dd52aSJames Smart 		goto job_error;
14863b5dd52aSJames Smart 	}
14873b5dd52aSJames Smart 
14883b5dd52aSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14893b5dd52aSJames Smart 	if (!pmboxq) {
14903b5dd52aSJames Smart 		rc = -ENOMEM;
14913b5dd52aSJames Smart 		goto job_error;
14923b5dd52aSJames Smart 	}
14933b5dd52aSJames Smart 
14943b5dd52aSJames Smart 	vports = lpfc_create_vport_work_array(phba);
14953b5dd52aSJames Smart 	if (vports) {
14963b5dd52aSJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14973b5dd52aSJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
14983b5dd52aSJames Smart 			scsi_block_requests(shost);
14993b5dd52aSJames Smart 		}
15003b5dd52aSJames Smart 
15013b5dd52aSJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
15023b5dd52aSJames Smart 	} else {
15033b5dd52aSJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
15043b5dd52aSJames Smart 		scsi_block_requests(shost);
15053b5dd52aSJames Smart 	}
15063b5dd52aSJames Smart 
15073b5dd52aSJames Smart 	while (pring->txcmplq_cnt) {
15083b5dd52aSJames Smart 		if (i++ > 500)	/* wait up to 5 seconds */
15093b5dd52aSJames Smart 			break;
15103b5dd52aSJames Smart 
15113b5dd52aSJames Smart 		msleep(10);
15123b5dd52aSJames Smart 	}
15133b5dd52aSJames Smart 
15143b5dd52aSJames Smart 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
15153b5dd52aSJames Smart 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
15163b5dd52aSJames Smart 	pmboxq->u.mb.mbxOwner = OWN_HOST;
15173b5dd52aSJames Smart 
15183b5dd52aSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
15193b5dd52aSJames Smart 
15203b5dd52aSJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
15213b5dd52aSJames Smart 		/* wait for link down before proceeding */
15223b5dd52aSJames Smart 		i = 0;
15233b5dd52aSJames Smart 		while (phba->link_state != LPFC_LINK_DOWN) {
15243b5dd52aSJames Smart 			if (i++ > timeout) {
15253b5dd52aSJames Smart 				rc = -ETIMEDOUT;
15263b5dd52aSJames Smart 				goto loopback_mode_exit;
15273b5dd52aSJames Smart 			}
15283b5dd52aSJames Smart 
15293b5dd52aSJames Smart 			msleep(10);
15303b5dd52aSJames Smart 		}
15313b5dd52aSJames Smart 
15323b5dd52aSJames Smart 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
15333b5dd52aSJames Smart 		if (link_flags == INTERNAL_LOOP_BACK)
15343b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
15353b5dd52aSJames Smart 		else
15363b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags =
15373b5dd52aSJames Smart 				FLAGS_TOPOLOGY_MODE_LOOP;
15383b5dd52aSJames Smart 
15393b5dd52aSJames Smart 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
15403b5dd52aSJames Smart 		pmboxq->u.mb.mbxOwner = OWN_HOST;
15413b5dd52aSJames Smart 
15423b5dd52aSJames Smart 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
15433b5dd52aSJames Smart 						     LPFC_MBOX_TMO);
15443b5dd52aSJames Smart 
15453b5dd52aSJames Smart 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
15463b5dd52aSJames Smart 			rc = -ENODEV;
15473b5dd52aSJames Smart 		else {
15483b5dd52aSJames Smart 			phba->link_flag |= LS_LOOPBACK_MODE;
15493b5dd52aSJames Smart 			/* wait for the link attention interrupt */
15503b5dd52aSJames Smart 			msleep(100);
15513b5dd52aSJames Smart 
15523b5dd52aSJames Smart 			i = 0;
15533b5dd52aSJames Smart 			while (phba->link_state != LPFC_HBA_READY) {
15543b5dd52aSJames Smart 				if (i++ > timeout) {
15553b5dd52aSJames Smart 					rc = -ETIMEDOUT;
15563b5dd52aSJames Smart 					break;
15573b5dd52aSJames Smart 				}
15583b5dd52aSJames Smart 
15593b5dd52aSJames Smart 				msleep(10);
15603b5dd52aSJames Smart 			}
15613b5dd52aSJames Smart 		}
15623b5dd52aSJames Smart 
15633b5dd52aSJames Smart 	} else
15643b5dd52aSJames Smart 		rc = -ENODEV;
15653b5dd52aSJames Smart 
15663b5dd52aSJames Smart loopback_mode_exit:
15673b5dd52aSJames Smart 	vports = lpfc_create_vport_work_array(phba);
15683b5dd52aSJames Smart 	if (vports) {
15693b5dd52aSJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
15703b5dd52aSJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
15713b5dd52aSJames Smart 			scsi_unblock_requests(shost);
15723b5dd52aSJames Smart 		}
15733b5dd52aSJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
15743b5dd52aSJames Smart 	} else {
15753b5dd52aSJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
15763b5dd52aSJames Smart 		scsi_unblock_requests(shost);
15773b5dd52aSJames Smart 	}
15783b5dd52aSJames Smart 
15793b5dd52aSJames Smart 	/*
15803b5dd52aSJames Smart 	 * Let SLI layer release mboxq if mbox command completed after timeout.
15813b5dd52aSJames Smart 	 */
15823b5dd52aSJames Smart 	if (mbxstatus != MBX_TIMEOUT)
15833b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
15843b5dd52aSJames Smart 
15853b5dd52aSJames Smart job_error:
15863b5dd52aSJames Smart 	/* make error code available to userspace */
15873b5dd52aSJames Smart 	job->reply->result = rc;
15883b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
15893b5dd52aSJames Smart 	if (rc == 0)
15903b5dd52aSJames Smart 		job->job_done(job);
15913b5dd52aSJames Smart 	return rc;
15923b5dd52aSJames Smart }
15933b5dd52aSJames Smart 
15943b5dd52aSJames Smart /**
15953b5dd52aSJames Smart  * lpfcdiag_loop_self_reg - obtains a remote port login id
15963b5dd52aSJames Smart  * @phba: Pointer to HBA context object
15973b5dd52aSJames Smart  * @rpi: Pointer to a remote port login id
15983b5dd52aSJames Smart  *
15993b5dd52aSJames Smart  * This function obtains a remote port login id so the diag loopback test
16003b5dd52aSJames Smart  * can send and receive its own unsolicited CT command.
16013b5dd52aSJames Smart  **/
16023b5dd52aSJames Smart static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t * rpi)
16033b5dd52aSJames Smart {
16043b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
16053b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuff;
16063b5dd52aSJames Smart 	int status;
16073b5dd52aSJames Smart 
16083b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16093b5dd52aSJames Smart 	if (!mbox)
16103b5dd52aSJames Smart 		return ENOMEM;
16113b5dd52aSJames Smart 
16123b5dd52aSJames Smart 	status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
16133b5dd52aSJames Smart 				(uint8_t *)&phba->pport->fc_sparam, mbox, 0);
16143b5dd52aSJames Smart 	if (status) {
16153b5dd52aSJames Smart 		mempool_free(mbox, phba->mbox_mem_pool);
16163b5dd52aSJames Smart 		return ENOMEM;
16173b5dd52aSJames Smart 	}
16183b5dd52aSJames Smart 
16193b5dd52aSJames Smart 	dmabuff = (struct lpfc_dmabuf *) mbox->context1;
16203b5dd52aSJames Smart 	mbox->context1 = NULL;
16213b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
16223b5dd52aSJames Smart 
16233b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
16243b5dd52aSJames Smart 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
16253b5dd52aSJames Smart 		kfree(dmabuff);
16263b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
16273b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
16283b5dd52aSJames Smart 		return ENODEV;
16293b5dd52aSJames Smart 	}
16303b5dd52aSJames Smart 
16313b5dd52aSJames Smart 	*rpi = mbox->u.mb.un.varWords[0];
16323b5dd52aSJames Smart 
16333b5dd52aSJames Smart 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
16343b5dd52aSJames Smart 	kfree(dmabuff);
16353b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
16363b5dd52aSJames Smart 	return 0;
16373b5dd52aSJames Smart }
16383b5dd52aSJames Smart 
16393b5dd52aSJames Smart /**
16403b5dd52aSJames Smart  * lpfcdiag_loop_self_unreg - unregs from the rpi
16413b5dd52aSJames Smart  * @phba: Pointer to HBA context object
16423b5dd52aSJames Smart  * @rpi: Remote port login id
16433b5dd52aSJames Smart  *
16443b5dd52aSJames Smart  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
16453b5dd52aSJames Smart  **/
16463b5dd52aSJames Smart static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
16473b5dd52aSJames Smart {
16483b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
16493b5dd52aSJames Smart 	int status;
16503b5dd52aSJames Smart 
16513b5dd52aSJames Smart 	/* Allocate mboxq structure */
16523b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16533b5dd52aSJames Smart 	if (mbox == NULL)
16543b5dd52aSJames Smart 		return ENOMEM;
16553b5dd52aSJames Smart 
16563b5dd52aSJames Smart 	lpfc_unreg_login(phba, 0, rpi, mbox);
16573b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
16583b5dd52aSJames Smart 
16593b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
16603b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
16613b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
16623b5dd52aSJames Smart 		return EIO;
16633b5dd52aSJames Smart 	}
16643b5dd52aSJames Smart 
16653b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
16663b5dd52aSJames Smart 	return 0;
16673b5dd52aSJames Smart }
16683b5dd52aSJames Smart 
16693b5dd52aSJames Smart /**
16703b5dd52aSJames Smart  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
16713b5dd52aSJames Smart  * @phba: Pointer to HBA context object
16723b5dd52aSJames Smart  * @rpi: Remote port login id
16733b5dd52aSJames Smart  * @txxri: Pointer to transmit exchange id
16743b5dd52aSJames Smart  * @rxxri: Pointer to response exchabge id
16753b5dd52aSJames Smart  *
16763b5dd52aSJames Smart  * This function obtains the transmit and receive ids required to send
16773b5dd52aSJames Smart  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
16783b5dd52aSJames Smart  * flags are used to the unsolicted response handler is able to process
16793b5dd52aSJames Smart  * the ct command sent on the same port.
16803b5dd52aSJames Smart  **/
16813b5dd52aSJames Smart static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
16823b5dd52aSJames Smart 			 uint16_t *txxri, uint16_t * rxxri)
16833b5dd52aSJames Smart {
16843b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
16853b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
16863b5dd52aSJames Smart 	IOCB_t *cmd, *rsp;
16873b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuf;
16883b5dd52aSJames Smart 	struct ulp_bde64 *bpl = NULL;
16893b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq = NULL;
16903b5dd52aSJames Smart 	int ret_val = 0;
16913b5dd52aSJames Smart 	unsigned long flags;
16923b5dd52aSJames Smart 
16933b5dd52aSJames Smart 	*txxri = 0;
16943b5dd52aSJames Smart 	*rxxri = 0;
16953b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
16963b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
16973b5dd52aSJames Smart 	if (!evt)
16983b5dd52aSJames Smart 		return ENOMEM;
16993b5dd52aSJames Smart 
17003b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
17013b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
17023b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
17033b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
17043b5dd52aSJames Smart 
17053b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
17063b5dd52aSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
17073b5dd52aSJames Smart 
17083b5dd52aSJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
17093b5dd52aSJames Smart 	if (dmabuf) {
17103b5dd52aSJames Smart 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
17113b5dd52aSJames Smart 		INIT_LIST_HEAD(&dmabuf->list);
17123b5dd52aSJames Smart 		bpl = (struct ulp_bde64 *) dmabuf->virt;
17133b5dd52aSJames Smart 		memset(bpl, 0, sizeof(*bpl));
17143b5dd52aSJames Smart 		ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
17153b5dd52aSJames Smart 		bpl->addrHigh =
17163b5dd52aSJames Smart 			le32_to_cpu(putPaddrHigh(dmabuf->phys + sizeof(*bpl)));
17173b5dd52aSJames Smart 		bpl->addrLow =
17183b5dd52aSJames Smart 			le32_to_cpu(putPaddrLow(dmabuf->phys + sizeof(*bpl)));
17193b5dd52aSJames Smart 		bpl->tus.f.bdeFlags = 0;
17203b5dd52aSJames Smart 		bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
17213b5dd52aSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
17223b5dd52aSJames Smart 	}
17233b5dd52aSJames Smart 
17243b5dd52aSJames Smart 	if (cmdiocbq == NULL || rspiocbq == NULL ||
17253b5dd52aSJames Smart 	    dmabuf == NULL || bpl == NULL || ctreq == NULL) {
17263b5dd52aSJames Smart 		ret_val = ENOMEM;
17273b5dd52aSJames Smart 		goto err_get_xri_exit;
17283b5dd52aSJames Smart 	}
17293b5dd52aSJames Smart 
17303b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
17313b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
17323b5dd52aSJames Smart 
17333b5dd52aSJames Smart 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
17343b5dd52aSJames Smart 
17353b5dd52aSJames Smart 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
17363b5dd52aSJames Smart 	ctreq->RevisionId.bits.InId = 0;
17373b5dd52aSJames Smart 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
17383b5dd52aSJames Smart 	ctreq->FsSubType = 0;
17393b5dd52aSJames Smart 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
17403b5dd52aSJames Smart 	ctreq->CommandResponse.bits.Size = 0;
17413b5dd52aSJames Smart 
17423b5dd52aSJames Smart 
17433b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
17443b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
17453b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
17463b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
17473b5dd52aSJames Smart 
17483b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = LA;
17493b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
17503b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
17513b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
17523b5dd52aSJames Smart 
17533b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
17543b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
17553b5dd52aSJames Smart 	cmd->ulpLe = 1;
17563b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
17573b5dd52aSJames Smart 	cmd->ulpContext = rpi;
17583b5dd52aSJames Smart 
17593b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
17603b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
17613b5dd52aSJames Smart 
17623b5dd52aSJames Smart 	ret_val = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
17633b5dd52aSJames Smart 				rspiocbq,
17643b5dd52aSJames Smart 				(phba->fc_ratov * 2)
17653b5dd52aSJames Smart 				+ LPFC_DRVR_TIMEOUT);
17663b5dd52aSJames Smart 	if (ret_val)
17673b5dd52aSJames Smart 		goto err_get_xri_exit;
17683b5dd52aSJames Smart 
17693b5dd52aSJames Smart 	*txxri =  rsp->ulpContext;
17703b5dd52aSJames Smart 
17713b5dd52aSJames Smart 	evt->waiting = 1;
17723b5dd52aSJames Smart 	evt->wait_time_stamp = jiffies;
17733b5dd52aSJames Smart 	ret_val = wait_event_interruptible_timeout(
17743b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
17753b5dd52aSJames Smart 		((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
17763b5dd52aSJames Smart 	if (list_empty(&evt->events_to_see))
17773b5dd52aSJames Smart 		ret_val = (ret_val) ? EINTR : ETIMEDOUT;
17783b5dd52aSJames Smart 	else {
17793b5dd52aSJames Smart 		ret_val = IOCB_SUCCESS;
17803b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
17813b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
17823b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
17833b5dd52aSJames Smart 		*rxxri = (list_entry(evt->events_to_get.prev,
17843b5dd52aSJames Smart 				     typeof(struct event_data),
17853b5dd52aSJames Smart 				     node))->immed_dat;
17863b5dd52aSJames Smart 	}
17873b5dd52aSJames Smart 	evt->waiting = 0;
17883b5dd52aSJames Smart 
17893b5dd52aSJames Smart err_get_xri_exit:
17903b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
17913b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
17923b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
17933b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
17943b5dd52aSJames Smart 
17953b5dd52aSJames Smart 	if (dmabuf) {
17963b5dd52aSJames Smart 		if (dmabuf->virt)
17973b5dd52aSJames Smart 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
17983b5dd52aSJames Smart 		kfree(dmabuf);
17993b5dd52aSJames Smart 	}
18003b5dd52aSJames Smart 
18013b5dd52aSJames Smart 	if (cmdiocbq && (ret_val != IOCB_TIMEDOUT))
18023b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
18033b5dd52aSJames Smart 	if (rspiocbq)
18043b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
18053b5dd52aSJames Smart 	return ret_val;
18063b5dd52aSJames Smart }
18073b5dd52aSJames Smart 
18083b5dd52aSJames Smart /**
18093b5dd52aSJames Smart  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
18103b5dd52aSJames Smart  * @phba: Pointer to HBA context object
18113b5dd52aSJames Smart  * @bpl: Pointer to 64 bit bde structure
18123b5dd52aSJames Smart  * @size: Number of bytes to process
18133b5dd52aSJames Smart  * @nocopydata: Flag to copy user data into the allocated buffer
18143b5dd52aSJames Smart  *
18153b5dd52aSJames Smart  * This function allocates page size buffers and populates an lpfc_dmabufext.
18163b5dd52aSJames Smart  * If allowed the user data pointed to with indataptr is copied into the kernel
18173b5dd52aSJames Smart  * memory. The chained list of page size buffers is returned.
18183b5dd52aSJames Smart  **/
18193b5dd52aSJames Smart static struct lpfc_dmabufext *
18203b5dd52aSJames Smart diag_cmd_data_alloc(struct lpfc_hba *phba,
18213b5dd52aSJames Smart 		   struct ulp_bde64 *bpl, uint32_t size,
18223b5dd52aSJames Smart 		   int nocopydata)
18233b5dd52aSJames Smart {
18243b5dd52aSJames Smart 	struct lpfc_dmabufext *mlist = NULL;
18253b5dd52aSJames Smart 	struct lpfc_dmabufext *dmp;
18263b5dd52aSJames Smart 	int cnt, offset = 0, i = 0;
18273b5dd52aSJames Smart 	struct pci_dev *pcidev;
18283b5dd52aSJames Smart 
18293b5dd52aSJames Smart 	pcidev = phba->pcidev;
18303b5dd52aSJames Smart 
18313b5dd52aSJames Smart 	while (size) {
18323b5dd52aSJames Smart 		/* We get chunks of 4K */
18333b5dd52aSJames Smart 		if (size > BUF_SZ_4K)
18343b5dd52aSJames Smart 			cnt = BUF_SZ_4K;
18353b5dd52aSJames Smart 		else
18363b5dd52aSJames Smart 			cnt = size;
18373b5dd52aSJames Smart 
18383b5dd52aSJames Smart 		/* allocate struct lpfc_dmabufext buffer header */
18393b5dd52aSJames Smart 		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
18403b5dd52aSJames Smart 		if (!dmp)
18413b5dd52aSJames Smart 			goto out;
18423b5dd52aSJames Smart 
18433b5dd52aSJames Smart 		INIT_LIST_HEAD(&dmp->dma.list);
18443b5dd52aSJames Smart 
18453b5dd52aSJames Smart 		/* Queue it to a linked list */
18463b5dd52aSJames Smart 		if (mlist)
18473b5dd52aSJames Smart 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
18483b5dd52aSJames Smart 		else
18493b5dd52aSJames Smart 			mlist = dmp;
18503b5dd52aSJames Smart 
18513b5dd52aSJames Smart 		/* allocate buffer */
18523b5dd52aSJames Smart 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
18533b5dd52aSJames Smart 						   cnt,
18543b5dd52aSJames Smart 						   &(dmp->dma.phys),
18553b5dd52aSJames Smart 						   GFP_KERNEL);
18563b5dd52aSJames Smart 
18573b5dd52aSJames Smart 		if (!dmp->dma.virt)
18583b5dd52aSJames Smart 			goto out;
18593b5dd52aSJames Smart 
18603b5dd52aSJames Smart 		dmp->size = cnt;
18613b5dd52aSJames Smart 
18623b5dd52aSJames Smart 		if (nocopydata) {
18633b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
18643b5dd52aSJames Smart 			pci_dma_sync_single_for_device(phba->pcidev,
18653b5dd52aSJames Smart 				dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
18663b5dd52aSJames Smart 
18673b5dd52aSJames Smart 		} else {
18683b5dd52aSJames Smart 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
18693b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
18703b5dd52aSJames Smart 		}
18713b5dd52aSJames Smart 
18723b5dd52aSJames Smart 		/* build buffer ptr list for IOCB */
18733b5dd52aSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
18743b5dd52aSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
18753b5dd52aSJames Smart 		bpl->tus.f.bdeSize = (ushort) cnt;
18763b5dd52aSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
18773b5dd52aSJames Smart 		bpl++;
18783b5dd52aSJames Smart 
18793b5dd52aSJames Smart 		i++;
18803b5dd52aSJames Smart 		offset += cnt;
18813b5dd52aSJames Smart 		size -= cnt;
18823b5dd52aSJames Smart 	}
18833b5dd52aSJames Smart 
18843b5dd52aSJames Smart 	mlist->flag = i;
18853b5dd52aSJames Smart 	return mlist;
18863b5dd52aSJames Smart out:
18873b5dd52aSJames Smart 	diag_cmd_data_free(phba, mlist);
18883b5dd52aSJames Smart 	return NULL;
18893b5dd52aSJames Smart }
18903b5dd52aSJames Smart 
18913b5dd52aSJames Smart /**
18923b5dd52aSJames Smart  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
18933b5dd52aSJames Smart  * @phba: Pointer to HBA context object
18943b5dd52aSJames Smart  * @rxxri: Receive exchange id
18953b5dd52aSJames Smart  * @len: Number of data bytes
18963b5dd52aSJames Smart  *
18973b5dd52aSJames Smart  * This function allocates and posts a data buffer of sufficient size to recieve
18983b5dd52aSJames Smart  * an unsolicted CT command.
18993b5dd52aSJames Smart  **/
19003b5dd52aSJames Smart static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
19013b5dd52aSJames Smart 			     size_t len)
19023b5dd52aSJames Smart {
19033b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
19043b5dd52aSJames Smart 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
19053b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq;
19063b5dd52aSJames Smart 	IOCB_t *cmd = NULL;
19073b5dd52aSJames Smart 	struct list_head head, *curr, *next;
19083b5dd52aSJames Smart 	struct lpfc_dmabuf *rxbmp;
19093b5dd52aSJames Smart 	struct lpfc_dmabuf *dmp;
19103b5dd52aSJames Smart 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
19113b5dd52aSJames Smart 	struct ulp_bde64 *rxbpl = NULL;
19123b5dd52aSJames Smart 	uint32_t num_bde;
19133b5dd52aSJames Smart 	struct lpfc_dmabufext *rxbuffer = NULL;
19143b5dd52aSJames Smart 	int ret_val = 0;
19153b5dd52aSJames Smart 	int i = 0;
19163b5dd52aSJames Smart 
19173b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
19183b5dd52aSJames Smart 	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
19193b5dd52aSJames Smart 	if (rxbmp != NULL) {
19203b5dd52aSJames Smart 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
19213b5dd52aSJames Smart 		INIT_LIST_HEAD(&rxbmp->list);
19223b5dd52aSJames Smart 		rxbpl = (struct ulp_bde64 *) rxbmp->virt;
19233b5dd52aSJames Smart 		rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
19243b5dd52aSJames Smart 	}
19253b5dd52aSJames Smart 
19263b5dd52aSJames Smart 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
19273b5dd52aSJames Smart 		ret_val = ENOMEM;
19283b5dd52aSJames Smart 		goto err_post_rxbufs_exit;
19293b5dd52aSJames Smart 	}
19303b5dd52aSJames Smart 
19313b5dd52aSJames Smart 	/* Queue buffers for the receive exchange */
19323b5dd52aSJames Smart 	num_bde = (uint32_t)rxbuffer->flag;
19333b5dd52aSJames Smart 	dmp = &rxbuffer->dma;
19343b5dd52aSJames Smart 
19353b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
19363b5dd52aSJames Smart 	i = 0;
19373b5dd52aSJames Smart 
19383b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
19393b5dd52aSJames Smart 	list_add_tail(&head, &dmp->list);
19403b5dd52aSJames Smart 	list_for_each_safe(curr, next, &head) {
19413b5dd52aSJames Smart 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
19423b5dd52aSJames Smart 		list_del(curr);
19433b5dd52aSJames Smart 
19443b5dd52aSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
19453b5dd52aSJames Smart 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
19463b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrHigh =
19473b5dd52aSJames Smart 				putPaddrHigh(mp[i]->phys);
19483b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrLow =
19493b5dd52aSJames Smart 				putPaddrLow(mp[i]->phys);
19503b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
19513b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
19523b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
19533b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
19543b5dd52aSJames Smart 			cmd->ulpPU = 0;
19553b5dd52aSJames Smart 			cmd->ulpLe = 1;
19563b5dd52aSJames Smart 			cmd->ulpBdeCount = 1;
19573b5dd52aSJames Smart 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
19583b5dd52aSJames Smart 
19593b5dd52aSJames Smart 		} else {
19603b5dd52aSJames Smart 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
19613b5dd52aSJames Smart 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
19623b5dd52aSJames Smart 			cmd->un.cont64[i].tus.f.bdeSize =
19633b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
19643b5dd52aSJames Smart 					cmd->ulpBdeCount = ++i;
19653b5dd52aSJames Smart 
19663b5dd52aSJames Smart 			if ((--num_bde > 0) && (i < 2))
19673b5dd52aSJames Smart 				continue;
19683b5dd52aSJames Smart 
19693b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
19703b5dd52aSJames Smart 			cmd->ulpLe = 1;
19713b5dd52aSJames Smart 		}
19723b5dd52aSJames Smart 
19733b5dd52aSJames Smart 		cmd->ulpClass = CLASS3;
19743b5dd52aSJames Smart 		cmd->ulpContext = rxxri;
19753b5dd52aSJames Smart 
19763b5dd52aSJames Smart 		ret_val = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
19773b5dd52aSJames Smart 
19783b5dd52aSJames Smart 		if (ret_val == IOCB_ERROR) {
19793b5dd52aSJames Smart 			diag_cmd_data_free(phba,
19803b5dd52aSJames Smart 				(struct lpfc_dmabufext *)mp[0]);
19813b5dd52aSJames Smart 			if (mp[1])
19823b5dd52aSJames Smart 				diag_cmd_data_free(phba,
19833b5dd52aSJames Smart 					  (struct lpfc_dmabufext *)mp[1]);
19843b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
19853b5dd52aSJames Smart 			ret_val = EIO;
19863b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
19873b5dd52aSJames Smart 		}
19883b5dd52aSJames Smart 
19893b5dd52aSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
19903b5dd52aSJames Smart 		if (mp[1]) {
19913b5dd52aSJames Smart 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
19923b5dd52aSJames Smart 			mp[1] = NULL;
19933b5dd52aSJames Smart 		}
19943b5dd52aSJames Smart 
19953b5dd52aSJames Smart 		/* The iocb was freed by lpfc_sli_issue_iocb */
19963b5dd52aSJames Smart 		cmdiocbq = lpfc_sli_get_iocbq(phba);
19973b5dd52aSJames Smart 		if (!cmdiocbq) {
19983b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
19993b5dd52aSJames Smart 			ret_val = EIO;
20003b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
20013b5dd52aSJames Smart 		}
20023b5dd52aSJames Smart 
20033b5dd52aSJames Smart 		cmd = &cmdiocbq->iocb;
20043b5dd52aSJames Smart 		i = 0;
20053b5dd52aSJames Smart 	}
20063b5dd52aSJames Smart 	list_del(&head);
20073b5dd52aSJames Smart 
20083b5dd52aSJames Smart err_post_rxbufs_exit:
20093b5dd52aSJames Smart 
20103b5dd52aSJames Smart 	if (rxbmp) {
20113b5dd52aSJames Smart 		if (rxbmp->virt)
20123b5dd52aSJames Smart 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
20133b5dd52aSJames Smart 		kfree(rxbmp);
20143b5dd52aSJames Smart 	}
20153b5dd52aSJames Smart 
20163b5dd52aSJames Smart 	if (cmdiocbq)
20173b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
20183b5dd52aSJames Smart 	return ret_val;
20193b5dd52aSJames Smart }
20203b5dd52aSJames Smart 
20213b5dd52aSJames Smart /**
20223b5dd52aSJames Smart  * lpfc_bsg_diag_test - with a port in loopback issues a Ct cmd to itself
20233b5dd52aSJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
20243b5dd52aSJames Smart  *
20253b5dd52aSJames Smart  * This function receives a user data buffer to be transmitted and received on
20263b5dd52aSJames Smart  * the same port, the link must be up and in loopback mode prior
20273b5dd52aSJames Smart  * to being called.
20283b5dd52aSJames Smart  * 1. A kernel buffer is allocated to copy the user data into.
20293b5dd52aSJames Smart  * 2. The port registers with "itself".
20303b5dd52aSJames Smart  * 3. The transmit and receive exchange ids are obtained.
20313b5dd52aSJames Smart  * 4. The receive exchange id is posted.
20323b5dd52aSJames Smart  * 5. A new els loopback event is created.
20333b5dd52aSJames Smart  * 6. The command and response iocbs are allocated.
20343b5dd52aSJames Smart  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
20353b5dd52aSJames Smart  *
20363b5dd52aSJames Smart  * This function is meant to be called n times while the port is in loopback
20373b5dd52aSJames Smart  * so it is the apps responsibility to issue a reset to take the port out
20383b5dd52aSJames Smart  * of loopback mode.
20393b5dd52aSJames Smart  **/
20403b5dd52aSJames Smart static int
20413b5dd52aSJames Smart lpfc_bsg_diag_test(struct fc_bsg_job *job)
20423b5dd52aSJames Smart {
20433b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
20443b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
20453b5dd52aSJames Smart 	struct diag_mode_test *diag_mode;
20463b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
20473b5dd52aSJames Smart 	struct event_data *evdat;
20483b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
20493b5dd52aSJames Smart 	uint32_t size;
20503b5dd52aSJames Smart 	uint32_t full_size;
20513b5dd52aSJames Smart 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
20523b5dd52aSJames Smart 	uint16_t rpi;
20533b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
20543b5dd52aSJames Smart 	IOCB_t *cmd, *rsp;
20553b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq;
20563b5dd52aSJames Smart 	struct lpfc_dmabuf *txbmp;
20573b5dd52aSJames Smart 	struct ulp_bde64 *txbpl = NULL;
20583b5dd52aSJames Smart 	struct lpfc_dmabufext *txbuffer = NULL;
20593b5dd52aSJames Smart 	struct list_head head;
20603b5dd52aSJames Smart 	struct lpfc_dmabuf  *curr;
20613b5dd52aSJames Smart 	uint16_t txxri, rxxri;
20623b5dd52aSJames Smart 	uint32_t num_bde;
20633b5dd52aSJames Smart 	uint8_t *ptr = NULL, *rx_databuf = NULL;
20643b5dd52aSJames Smart 	int rc = 0;
20653b5dd52aSJames Smart 	unsigned long flags;
20663b5dd52aSJames Smart 	void *dataout = NULL;
20673b5dd52aSJames Smart 	uint32_t total_mem;
20683b5dd52aSJames Smart 
20693b5dd52aSJames Smart 	/* in case no data is returned return just the return code */
20703b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
20713b5dd52aSJames Smart 
20723b5dd52aSJames Smart 	if (job->request_len <
20733b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
20743b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20753b5dd52aSJames Smart 				"2739 Received DIAG TEST request below minimum "
20763b5dd52aSJames Smart 				"size\n");
20773b5dd52aSJames Smart 		rc = -EINVAL;
20783b5dd52aSJames Smart 		goto loopback_test_exit;
20793b5dd52aSJames Smart 	}
20803b5dd52aSJames Smart 
20813b5dd52aSJames Smart 	if (job->request_payload.payload_len !=
20823b5dd52aSJames Smart 		job->reply_payload.payload_len) {
20833b5dd52aSJames Smart 		rc = -EINVAL;
20843b5dd52aSJames Smart 		goto loopback_test_exit;
20853b5dd52aSJames Smart 	}
20863b5dd52aSJames Smart 
20873b5dd52aSJames Smart 	diag_mode = (struct diag_mode_test *)
20883b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
20893b5dd52aSJames Smart 
20903b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
20913b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
20923b5dd52aSJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
20933b5dd52aSJames Smart 		rc = -EACCES;
20943b5dd52aSJames Smart 		goto loopback_test_exit;
20953b5dd52aSJames Smart 	}
20963b5dd52aSJames Smart 
20973b5dd52aSJames Smart 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
20983b5dd52aSJames Smart 		rc = -EACCES;
20993b5dd52aSJames Smart 		goto loopback_test_exit;
21003b5dd52aSJames Smart 	}
21013b5dd52aSJames Smart 
21023b5dd52aSJames Smart 	size = job->request_payload.payload_len;
21033b5dd52aSJames Smart 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
21043b5dd52aSJames Smart 
21053b5dd52aSJames Smart 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
21063b5dd52aSJames Smart 		rc = -ERANGE;
21073b5dd52aSJames Smart 		goto loopback_test_exit;
21083b5dd52aSJames Smart 	}
21093b5dd52aSJames Smart 
21103b5dd52aSJames Smart 	if (size >= BUF_SZ_4K) {
21113b5dd52aSJames Smart 		/*
21123b5dd52aSJames Smart 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
21133b5dd52aSJames Smart 		 * then we allocate 64k and re-use that buffer over and over to
21143b5dd52aSJames Smart 		 * xfer the whole block. This is because Linux kernel has a
21153b5dd52aSJames Smart 		 * problem allocating more than 120k of kernel space memory. Saw
21163b5dd52aSJames Smart 		 * problem with GET_FCPTARGETMAPPING...
21173b5dd52aSJames Smart 		 */
21183b5dd52aSJames Smart 		if (size <= (64 * 1024))
21193b5dd52aSJames Smart 			total_mem = size;
21203b5dd52aSJames Smart 		else
21213b5dd52aSJames Smart 			total_mem = 64 * 1024;
21223b5dd52aSJames Smart 	} else
21233b5dd52aSJames Smart 		/* Allocate memory for ioctl data */
21243b5dd52aSJames Smart 		total_mem = BUF_SZ_4K;
21253b5dd52aSJames Smart 
21263b5dd52aSJames Smart 	dataout = kmalloc(total_mem, GFP_KERNEL);
21273b5dd52aSJames Smart 	if (dataout == NULL) {
21283b5dd52aSJames Smart 		rc = -ENOMEM;
21293b5dd52aSJames Smart 		goto loopback_test_exit;
21303b5dd52aSJames Smart 	}
21313b5dd52aSJames Smart 
21323b5dd52aSJames Smart 	ptr = dataout;
21333b5dd52aSJames Smart 	ptr += ELX_LOOPBACK_HEADER_SZ;
21343b5dd52aSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
21353b5dd52aSJames Smart 				job->request_payload.sg_cnt,
21363b5dd52aSJames Smart 				ptr, size);
21373b5dd52aSJames Smart 
21383b5dd52aSJames Smart 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
21393b5dd52aSJames Smart 	if (rc) {
21403b5dd52aSJames Smart 		rc = -ENOMEM;
21413b5dd52aSJames Smart 		goto loopback_test_exit;
21423b5dd52aSJames Smart 	}
21433b5dd52aSJames Smart 
21443b5dd52aSJames Smart 	rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
21453b5dd52aSJames Smart 	if (rc) {
21463b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
21473b5dd52aSJames Smart 		rc = -ENOMEM;
21483b5dd52aSJames Smart 		goto loopback_test_exit;
21493b5dd52aSJames Smart 	}
21503b5dd52aSJames Smart 
21513b5dd52aSJames Smart 	rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
21523b5dd52aSJames Smart 	if (rc) {
21533b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
21543b5dd52aSJames Smart 		rc = -ENOMEM;
21553b5dd52aSJames Smart 		goto loopback_test_exit;
21563b5dd52aSJames Smart 	}
21573b5dd52aSJames Smart 
21583b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
21593b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
21603b5dd52aSJames Smart 	if (!evt) {
21613b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
21623b5dd52aSJames Smart 		rc = -ENOMEM;
21633b5dd52aSJames Smart 		goto loopback_test_exit;
21643b5dd52aSJames Smart 	}
21653b5dd52aSJames Smart 
21663b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
21673b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
21683b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
21693b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
21703b5dd52aSJames Smart 
21713b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
21723b5dd52aSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
21733b5dd52aSJames Smart 	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
21743b5dd52aSJames Smart 
21753b5dd52aSJames Smart 	if (txbmp) {
21763b5dd52aSJames Smart 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
21773b5dd52aSJames Smart 		INIT_LIST_HEAD(&txbmp->list);
21783b5dd52aSJames Smart 		txbpl = (struct ulp_bde64 *) txbmp->virt;
21793b5dd52aSJames Smart 		if (txbpl)
21803b5dd52aSJames Smart 			txbuffer = diag_cmd_data_alloc(phba,
21813b5dd52aSJames Smart 							txbpl, full_size, 0);
21823b5dd52aSJames Smart 	}
21833b5dd52aSJames Smart 
21843b5dd52aSJames Smart 	if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer) {
21853b5dd52aSJames Smart 		rc = -ENOMEM;
21863b5dd52aSJames Smart 		goto err_loopback_test_exit;
21873b5dd52aSJames Smart 	}
21883b5dd52aSJames Smart 
21893b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
21903b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
21913b5dd52aSJames Smart 
21923b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
21933b5dd52aSJames Smart 	list_add_tail(&head, &txbuffer->dma.list);
21943b5dd52aSJames Smart 	list_for_each_entry(curr, &head, list) {
21953b5dd52aSJames Smart 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
21963b5dd52aSJames Smart 		if (current_offset == 0) {
21973b5dd52aSJames Smart 			ctreq = curr->virt;
21983b5dd52aSJames Smart 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
21993b5dd52aSJames Smart 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
22003b5dd52aSJames Smart 			ctreq->RevisionId.bits.InId = 0;
22013b5dd52aSJames Smart 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
22023b5dd52aSJames Smart 			ctreq->FsSubType = 0;
22033b5dd52aSJames Smart 			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
22043b5dd52aSJames Smart 			ctreq->CommandResponse.bits.Size   = size;
22053b5dd52aSJames Smart 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
22063b5dd52aSJames Smart 		} else
22073b5dd52aSJames Smart 			segment_offset = 0;
22083b5dd52aSJames Smart 
22093b5dd52aSJames Smart 		BUG_ON(segment_offset >= segment_len);
22103b5dd52aSJames Smart 		memcpy(curr->virt + segment_offset,
22113b5dd52aSJames Smart 			ptr + current_offset,
22123b5dd52aSJames Smart 			segment_len - segment_offset);
22133b5dd52aSJames Smart 
22143b5dd52aSJames Smart 		current_offset += segment_len - segment_offset;
22153b5dd52aSJames Smart 		BUG_ON(current_offset > size);
22163b5dd52aSJames Smart 	}
22173b5dd52aSJames Smart 	list_del(&head);
22183b5dd52aSJames Smart 
22193b5dd52aSJames Smart 	/* Build the XMIT_SEQUENCE iocb */
22203b5dd52aSJames Smart 
22213b5dd52aSJames Smart 	num_bde = (uint32_t)txbuffer->flag;
22223b5dd52aSJames Smart 
22233b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
22243b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
22253b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
22263b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
22273b5dd52aSJames Smart 
22283b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
22293b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
22303b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
22313b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
22323b5dd52aSJames Smart 
22333b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
22343b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
22353b5dd52aSJames Smart 	cmd->ulpLe = 1;
22363b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
22373b5dd52aSJames Smart 	cmd->ulpContext = txxri;
22383b5dd52aSJames Smart 
22393b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
22403b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
22413b5dd52aSJames Smart 
22423b5dd52aSJames Smart 	rc = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq, rspiocbq,
22433b5dd52aSJames Smart 				      (phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT);
22443b5dd52aSJames Smart 
22453b5dd52aSJames Smart 	if ((rc != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
22463b5dd52aSJames Smart 		rc = -EIO;
22473b5dd52aSJames Smart 		goto err_loopback_test_exit;
22483b5dd52aSJames Smart 	}
22493b5dd52aSJames Smart 
22503b5dd52aSJames Smart 	evt->waiting = 1;
22513b5dd52aSJames Smart 	rc = wait_event_interruptible_timeout(
22523b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
22533b5dd52aSJames Smart 		((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
22543b5dd52aSJames Smart 	evt->waiting = 0;
22553b5dd52aSJames Smart 	if (list_empty(&evt->events_to_see))
22563b5dd52aSJames Smart 		rc = (rc) ? -EINTR : -ETIMEDOUT;
22573b5dd52aSJames Smart 	else {
22583b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
22593b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
22603b5dd52aSJames Smart 		evdat = list_entry(evt->events_to_get.prev,
22613b5dd52aSJames Smart 				   typeof(*evdat), node);
22623b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
22633b5dd52aSJames Smart 		rx_databuf = evdat->data;
22643b5dd52aSJames Smart 		if (evdat->len != full_size) {
22653b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
22663b5dd52aSJames Smart 				"1603 Loopback test did not receive expected "
22673b5dd52aSJames Smart 				"data length. actual length 0x%x expected "
22683b5dd52aSJames Smart 				"length 0x%x\n",
22693b5dd52aSJames Smart 				evdat->len, full_size);
22703b5dd52aSJames Smart 			rc = -EIO;
22713b5dd52aSJames Smart 		} else if (rx_databuf == NULL)
22723b5dd52aSJames Smart 			rc = -EIO;
22733b5dd52aSJames Smart 		else {
22743b5dd52aSJames Smart 			rc = IOCB_SUCCESS;
22753b5dd52aSJames Smart 			/* skip over elx loopback header */
22763b5dd52aSJames Smart 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
22773b5dd52aSJames Smart 			job->reply->reply_payload_rcv_len =
22783b5dd52aSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
22793b5dd52aSJames Smart 						    job->reply_payload.sg_cnt,
22803b5dd52aSJames Smart 						    rx_databuf, size);
22813b5dd52aSJames Smart 			job->reply->reply_payload_rcv_len = size;
22823b5dd52aSJames Smart 		}
22833b5dd52aSJames Smart 	}
22843b5dd52aSJames Smart 
22853b5dd52aSJames Smart err_loopback_test_exit:
22863b5dd52aSJames Smart 	lpfcdiag_loop_self_unreg(phba, rpi);
22873b5dd52aSJames Smart 
22883b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
22893b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
22903b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
22913b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
22923b5dd52aSJames Smart 
22933b5dd52aSJames Smart 	if (cmdiocbq != NULL)
22943b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
22953b5dd52aSJames Smart 
22963b5dd52aSJames Smart 	if (rspiocbq != NULL)
22973b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
22983b5dd52aSJames Smart 
22993b5dd52aSJames Smart 	if (txbmp != NULL) {
23003b5dd52aSJames Smart 		if (txbpl != NULL) {
23013b5dd52aSJames Smart 			if (txbuffer != NULL)
23023b5dd52aSJames Smart 				diag_cmd_data_free(phba, txbuffer);
23033b5dd52aSJames Smart 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
23043b5dd52aSJames Smart 		}
23053b5dd52aSJames Smart 		kfree(txbmp);
23063b5dd52aSJames Smart 	}
23073b5dd52aSJames Smart 
23083b5dd52aSJames Smart loopback_test_exit:
23093b5dd52aSJames Smart 	kfree(dataout);
23103b5dd52aSJames Smart 	/* make error code available to userspace */
23113b5dd52aSJames Smart 	job->reply->result = rc;
23123b5dd52aSJames Smart 	job->dd_data = NULL;
23133b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
23143b5dd52aSJames Smart 	if (rc == 0)
23153b5dd52aSJames Smart 		job->job_done(job);
23163b5dd52aSJames Smart 	return rc;
23173b5dd52aSJames Smart }
23183b5dd52aSJames Smart 
23193b5dd52aSJames Smart /**
23203b5dd52aSJames Smart  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
23213b5dd52aSJames Smart  * @job: GET_DFC_REV fc_bsg_job
23223b5dd52aSJames Smart  **/
23233b5dd52aSJames Smart static int
23243b5dd52aSJames Smart lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
23253b5dd52aSJames Smart {
23263b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
23273b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
23283b5dd52aSJames Smart 	struct get_mgmt_rev *event_req;
23293b5dd52aSJames Smart 	struct get_mgmt_rev_reply *event_reply;
23303b5dd52aSJames Smart 	int rc = 0;
23313b5dd52aSJames Smart 
23323b5dd52aSJames Smart 	if (job->request_len <
23333b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
23343b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
23353b5dd52aSJames Smart 				"2740 Received GET_DFC_REV request below "
23363b5dd52aSJames Smart 				"minimum size\n");
23373b5dd52aSJames Smart 		rc = -EINVAL;
23383b5dd52aSJames Smart 		goto job_error;
23393b5dd52aSJames Smart 	}
23403b5dd52aSJames Smart 
23413b5dd52aSJames Smart 	event_req = (struct get_mgmt_rev *)
23423b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
23433b5dd52aSJames Smart 
23443b5dd52aSJames Smart 	event_reply = (struct get_mgmt_rev_reply *)
23453b5dd52aSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
23463b5dd52aSJames Smart 
23473b5dd52aSJames Smart 	if (job->reply_len <
23483b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
23493b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
23503b5dd52aSJames Smart 				"2741 Received GET_DFC_REV reply below "
23513b5dd52aSJames Smart 				"minimum size\n");
23523b5dd52aSJames Smart 		rc = -EINVAL;
23533b5dd52aSJames Smart 		goto job_error;
23543b5dd52aSJames Smart 	}
23553b5dd52aSJames Smart 
23563b5dd52aSJames Smart 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
23573b5dd52aSJames Smart 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
23583b5dd52aSJames Smart job_error:
23593b5dd52aSJames Smart 	job->reply->result = rc;
23603b5dd52aSJames Smart 	if (rc == 0)
23613b5dd52aSJames Smart 		job->job_done(job);
23623b5dd52aSJames Smart 	return rc;
23633b5dd52aSJames Smart }
23643b5dd52aSJames Smart 
23653b5dd52aSJames Smart /**
23663b5dd52aSJames Smart  * lpfc_bsg_wake_mbox_wait - lpfc_bsg_issue_mbox mbox completion handler
23673b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
23683b5dd52aSJames Smart  * @pmboxq: Pointer to mailbox command.
23693b5dd52aSJames Smart  *
23703b5dd52aSJames Smart  * This is completion handler function for mailbox commands issued from
23713b5dd52aSJames Smart  * lpfc_bsg_issue_mbox function. This function is called by the
23723b5dd52aSJames Smart  * mailbox event handler function with no lock held. This function
23733b5dd52aSJames Smart  * will wake up thread waiting on the wait queue pointed by context1
23743b5dd52aSJames Smart  * of the mailbox.
23753b5dd52aSJames Smart  **/
23763b5dd52aSJames Smart void
23773b5dd52aSJames Smart lpfc_bsg_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
23783b5dd52aSJames Smart {
23793b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
23803b5dd52aSJames Smart 	MAILBOX_t *pmb;
23813b5dd52aSJames Smart 	MAILBOX_t *mb;
23823b5dd52aSJames Smart 	struct fc_bsg_job *job;
23833b5dd52aSJames Smart 	uint32_t size;
23843b5dd52aSJames Smart 	unsigned long flags;
23853b5dd52aSJames Smart 
23863b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
23873b5dd52aSJames Smart 	dd_data = pmboxq->context1;
23883b5dd52aSJames Smart 	if (!dd_data) {
23893b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
23903b5dd52aSJames Smart 		return;
23913b5dd52aSJames Smart 	}
23923b5dd52aSJames Smart 
23933b5dd52aSJames Smart 	pmb = &dd_data->context_un.mbox.pmboxq->u.mb;
23943b5dd52aSJames Smart 	mb = dd_data->context_un.mbox.mb;
23953b5dd52aSJames Smart 	job = dd_data->context_un.mbox.set_job;
23963b5dd52aSJames Smart 	memcpy(mb, pmb, sizeof(*pmb));
23973b5dd52aSJames Smart 	size = job->request_payload.payload_len;
23983b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len =
23993b5dd52aSJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
24003b5dd52aSJames Smart 				job->reply_payload.sg_cnt,
24013b5dd52aSJames Smart 				mb, size);
24023b5dd52aSJames Smart 	job->reply->result = 0;
24033b5dd52aSJames Smart 	dd_data->context_un.mbox.set_job = NULL;
24043b5dd52aSJames Smart 	job->dd_data = NULL;
24053b5dd52aSJames Smart 	job->job_done(job);
24063b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
24073b5dd52aSJames Smart 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
24083b5dd52aSJames Smart 	kfree(mb);
24093b5dd52aSJames Smart 	kfree(dd_data);
24103b5dd52aSJames Smart 	return;
24113b5dd52aSJames Smart }
24123b5dd52aSJames Smart 
24133b5dd52aSJames Smart /**
24143b5dd52aSJames Smart  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
24153b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
24163b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
24173b5dd52aSJames Smart  * @vport: Pointer to a vport object.
24183b5dd52aSJames Smart  *
24193b5dd52aSJames Smart  * Some commands require the port to be offline, some may not be called from
24203b5dd52aSJames Smart  * the application.
24213b5dd52aSJames Smart  **/
24223b5dd52aSJames Smart static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
24233b5dd52aSJames Smart 	MAILBOX_t *mb, struct lpfc_vport *vport)
24243b5dd52aSJames Smart {
24253b5dd52aSJames Smart 	/* return negative error values for bsg job */
24263b5dd52aSJames Smart 	switch (mb->mbxCommand) {
24273b5dd52aSJames Smart 	/* Offline only */
24283b5dd52aSJames Smart 	case MBX_INIT_LINK:
24293b5dd52aSJames Smart 	case MBX_DOWN_LINK:
24303b5dd52aSJames Smart 	case MBX_CONFIG_LINK:
24313b5dd52aSJames Smart 	case MBX_CONFIG_RING:
24323b5dd52aSJames Smart 	case MBX_RESET_RING:
24333b5dd52aSJames Smart 	case MBX_UNREG_LOGIN:
24343b5dd52aSJames Smart 	case MBX_CLEAR_LA:
24353b5dd52aSJames Smart 	case MBX_DUMP_CONTEXT:
24363b5dd52aSJames Smart 	case MBX_RUN_DIAGS:
24373b5dd52aSJames Smart 	case MBX_RESTART:
24383b5dd52aSJames Smart 	case MBX_SET_MASK:
24393b5dd52aSJames Smart 		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
24403b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
24413b5dd52aSJames Smart 				"2743 Command 0x%x is illegal in on-line "
24423b5dd52aSJames Smart 				"state\n",
24433b5dd52aSJames Smart 				mb->mbxCommand);
24443b5dd52aSJames Smart 			return -EPERM;
24453b5dd52aSJames Smart 		}
24463b5dd52aSJames Smart 	case MBX_WRITE_NV:
24473b5dd52aSJames Smart 	case MBX_WRITE_VPARMS:
24483b5dd52aSJames Smart 	case MBX_LOAD_SM:
24493b5dd52aSJames Smart 	case MBX_READ_NV:
24503b5dd52aSJames Smart 	case MBX_READ_CONFIG:
24513b5dd52aSJames Smart 	case MBX_READ_RCONFIG:
24523b5dd52aSJames Smart 	case MBX_READ_STATUS:
24533b5dd52aSJames Smart 	case MBX_READ_XRI:
24543b5dd52aSJames Smart 	case MBX_READ_REV:
24553b5dd52aSJames Smart 	case MBX_READ_LNK_STAT:
24563b5dd52aSJames Smart 	case MBX_DUMP_MEMORY:
24573b5dd52aSJames Smart 	case MBX_DOWN_LOAD:
24583b5dd52aSJames Smart 	case MBX_UPDATE_CFG:
24593b5dd52aSJames Smart 	case MBX_KILL_BOARD:
24603b5dd52aSJames Smart 	case MBX_LOAD_AREA:
24613b5dd52aSJames Smart 	case MBX_LOAD_EXP_ROM:
24623b5dd52aSJames Smart 	case MBX_BEACON:
24633b5dd52aSJames Smart 	case MBX_DEL_LD_ENTRY:
24643b5dd52aSJames Smart 	case MBX_SET_DEBUG:
24653b5dd52aSJames Smart 	case MBX_WRITE_WWN:
24663b5dd52aSJames Smart 	case MBX_SLI4_CONFIG:
24673b5dd52aSJames Smart 	case MBX_READ_EVENT_LOG_STATUS:
24683b5dd52aSJames Smart 	case MBX_WRITE_EVENT_LOG:
24693b5dd52aSJames Smart 	case MBX_PORT_CAPABILITIES:
24703b5dd52aSJames Smart 	case MBX_PORT_IOV_CONTROL:
24713b5dd52aSJames Smart 		break;
24723b5dd52aSJames Smart 	case MBX_SET_VARIABLE:
2473e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2474e2aed29fSJames Smart 			"1226 mbox: set_variable 0x%x, 0x%x\n",
2475e2aed29fSJames Smart 			mb->un.varWords[0],
2476e2aed29fSJames Smart 			mb->un.varWords[1]);
2477e2aed29fSJames Smart 		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
2478e2aed29fSJames Smart 			&& (mb->un.varWords[1] == 1)) {
2479e2aed29fSJames Smart 			phba->wait_4_mlo_maint_flg = 1;
2480e2aed29fSJames Smart 		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
2481e2aed29fSJames Smart 			phba->link_flag &= ~LS_LOOPBACK_MODE;
2482e2aed29fSJames Smart 			phba->fc_topology = TOPOLOGY_PT_PT;
2483e2aed29fSJames Smart 		}
2484e2aed29fSJames Smart 		break;
24853b5dd52aSJames Smart 	case MBX_RUN_BIU_DIAG64:
24863b5dd52aSJames Smart 	case MBX_READ_EVENT_LOG:
24873b5dd52aSJames Smart 	case MBX_READ_SPARM64:
24883b5dd52aSJames Smart 	case MBX_READ_LA:
24893b5dd52aSJames Smart 	case MBX_READ_LA64:
24903b5dd52aSJames Smart 	case MBX_REG_LOGIN:
24913b5dd52aSJames Smart 	case MBX_REG_LOGIN64:
24923b5dd52aSJames Smart 	case MBX_CONFIG_PORT:
24933b5dd52aSJames Smart 	case MBX_RUN_BIU_DIAG:
24943b5dd52aSJames Smart 	default:
24953b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
24963b5dd52aSJames Smart 			"2742 Unknown Command 0x%x\n",
24973b5dd52aSJames Smart 			mb->mbxCommand);
24983b5dd52aSJames Smart 		return -EPERM;
24993b5dd52aSJames Smart 	}
25003b5dd52aSJames Smart 
25013b5dd52aSJames Smart 	return 0; /* ok */
25023b5dd52aSJames Smart }
25033b5dd52aSJames Smart 
25043b5dd52aSJames Smart /**
25053b5dd52aSJames Smart  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
25063b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
25073b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
25083b5dd52aSJames Smart  * @vport: Pointer to a vport object.
25093b5dd52aSJames Smart  *
25103b5dd52aSJames Smart  * Allocate a tracking object, mailbox command memory, get a mailbox
25113b5dd52aSJames Smart  * from the mailbox pool, copy the caller mailbox command.
25123b5dd52aSJames Smart  *
25133b5dd52aSJames Smart  * If offline and the sli is active we need to poll for the command (port is
25143b5dd52aSJames Smart  * being reset) and com-plete the job, otherwise issue the mailbox command and
25153b5dd52aSJames Smart  * let our completion handler finish the command.
25163b5dd52aSJames Smart  **/
25173b5dd52aSJames Smart static uint32_t
25183b5dd52aSJames Smart lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
25193b5dd52aSJames Smart 	struct lpfc_vport *vport)
25203b5dd52aSJames Smart {
25213b5dd52aSJames Smart 	LPFC_MBOXQ_t *pmboxq;
25223b5dd52aSJames Smart 	MAILBOX_t *pmb;
25233b5dd52aSJames Smart 	MAILBOX_t *mb;
25243b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
25253b5dd52aSJames Smart 	uint32_t size;
25263b5dd52aSJames Smart 	int rc = 0;
25273b5dd52aSJames Smart 
25283b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
25293b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
25303b5dd52aSJames Smart 	if (!dd_data) {
25313b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
25323b5dd52aSJames Smart 				"2727 Failed allocation of dd_data\n");
25333b5dd52aSJames Smart 		return -ENOMEM;
25343b5dd52aSJames Smart 	}
25353b5dd52aSJames Smart 
25363b5dd52aSJames Smart 	mb = kzalloc(PAGE_SIZE, GFP_KERNEL);
25373b5dd52aSJames Smart 	if (!mb) {
25383b5dd52aSJames Smart 		kfree(dd_data);
25393b5dd52aSJames Smart 		return -ENOMEM;
25403b5dd52aSJames Smart 	}
25413b5dd52aSJames Smart 
25423b5dd52aSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
25433b5dd52aSJames Smart 	if (!pmboxq) {
25443b5dd52aSJames Smart 		kfree(dd_data);
25453b5dd52aSJames Smart 		kfree(mb);
25463b5dd52aSJames Smart 		return -ENOMEM;
25473b5dd52aSJames Smart 	}
25483b5dd52aSJames Smart 
25493b5dd52aSJames Smart 	size = job->request_payload.payload_len;
25503b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len =
25513b5dd52aSJames Smart 		sg_copy_to_buffer(job->request_payload.sg_list,
25523b5dd52aSJames Smart 				job->request_payload.sg_cnt,
25533b5dd52aSJames Smart 				mb, size);
25543b5dd52aSJames Smart 
25553b5dd52aSJames Smart 	rc = lpfc_bsg_check_cmd_access(phba, mb, vport);
25563b5dd52aSJames Smart 	if (rc != 0) {
25573b5dd52aSJames Smart 		kfree(dd_data);
25583b5dd52aSJames Smart 		kfree(mb);
25593b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
25603b5dd52aSJames Smart 		return rc; /* must be negative */
25613b5dd52aSJames Smart 	}
25623b5dd52aSJames Smart 
25633b5dd52aSJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
25643b5dd52aSJames Smart 	pmb = &pmboxq->u.mb;
25653b5dd52aSJames Smart 	memcpy(pmb, mb, sizeof(*pmb));
25663b5dd52aSJames Smart 	pmb->mbxOwner = OWN_HOST;
25673b5dd52aSJames Smart 	pmboxq->context1 = NULL;
25683b5dd52aSJames Smart 	pmboxq->vport = vport;
25693b5dd52aSJames Smart 
25703b5dd52aSJames Smart 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
25713b5dd52aSJames Smart 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
25723b5dd52aSJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
25733b5dd52aSJames Smart 		if (rc != MBX_SUCCESS) {
25743b5dd52aSJames Smart 			if (rc != MBX_TIMEOUT) {
25753b5dd52aSJames Smart 				kfree(dd_data);
25763b5dd52aSJames Smart 				kfree(mb);
25773b5dd52aSJames Smart 				mempool_free(pmboxq, phba->mbox_mem_pool);
25783b5dd52aSJames Smart 			}
25793b5dd52aSJames Smart 			return  (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
25803b5dd52aSJames Smart 		}
25813b5dd52aSJames Smart 
25823b5dd52aSJames Smart 		memcpy(mb, pmb, sizeof(*pmb));
25833b5dd52aSJames Smart 		job->reply->reply_payload_rcv_len =
25843b5dd52aSJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
25853b5dd52aSJames Smart 					job->reply_payload.sg_cnt,
25863b5dd52aSJames Smart 					mb, size);
25873b5dd52aSJames Smart 		kfree(dd_data);
25883b5dd52aSJames Smart 		kfree(mb);
25893b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
25903b5dd52aSJames Smart 		/* not waiting mbox already done */
25913b5dd52aSJames Smart 		return 0;
25923b5dd52aSJames Smart 	}
25933b5dd52aSJames Smart 
25943b5dd52aSJames Smart 	/* setup wake call as IOCB callback */
25953b5dd52aSJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_wake_mbox_wait;
25963b5dd52aSJames Smart 	/* setup context field to pass wait_queue pointer to wake function */
25973b5dd52aSJames Smart 	pmboxq->context1 = dd_data;
25983b5dd52aSJames Smart 	dd_data->type = TYPE_MBOX;
25993b5dd52aSJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
26003b5dd52aSJames Smart 	dd_data->context_un.mbox.mb = mb;
26013b5dd52aSJames Smart 	dd_data->context_un.mbox.set_job = job;
26023b5dd52aSJames Smart 	job->dd_data = dd_data;
26033b5dd52aSJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
26043b5dd52aSJames Smart 	if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
26053b5dd52aSJames Smart 		kfree(dd_data);
26063b5dd52aSJames Smart 		kfree(mb);
26073b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
26083b5dd52aSJames Smart 		return -EIO;
26093b5dd52aSJames Smart 	}
26103b5dd52aSJames Smart 
26113b5dd52aSJames Smart 	return 1;
26123b5dd52aSJames Smart }
26133b5dd52aSJames Smart 
26143b5dd52aSJames Smart /**
26153b5dd52aSJames Smart  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
26163b5dd52aSJames Smart  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
26173b5dd52aSJames Smart  **/
26183b5dd52aSJames Smart static int
26193b5dd52aSJames Smart lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
26203b5dd52aSJames Smart {
26213b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
26223b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
26233b5dd52aSJames Smart 	int rc = 0;
26243b5dd52aSJames Smart 
26253b5dd52aSJames Smart 	/* in case no data is transferred */
26263b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
26273b5dd52aSJames Smart 	if (job->request_len <
26283b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
26293b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
26303b5dd52aSJames Smart 				"2737 Received MBOX_REQ request below "
26313b5dd52aSJames Smart 				"minimum size\n");
26323b5dd52aSJames Smart 		rc = -EINVAL;
26333b5dd52aSJames Smart 		goto job_error;
26343b5dd52aSJames Smart 	}
26353b5dd52aSJames Smart 
26363b5dd52aSJames Smart 	if (job->request_payload.payload_len != PAGE_SIZE) {
26373b5dd52aSJames Smart 		rc = -EINVAL;
26383b5dd52aSJames Smart 		goto job_error;
26393b5dd52aSJames Smart 	}
26403b5dd52aSJames Smart 
26413b5dd52aSJames Smart 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
26423b5dd52aSJames Smart 		rc = -EAGAIN;
26433b5dd52aSJames Smart 		goto job_error;
26443b5dd52aSJames Smart 	}
26453b5dd52aSJames Smart 
26463b5dd52aSJames Smart 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
26473b5dd52aSJames Smart 
26483b5dd52aSJames Smart job_error:
26493b5dd52aSJames Smart 	if (rc == 0) {
26503b5dd52aSJames Smart 		/* job done */
26513b5dd52aSJames Smart 		job->reply->result = 0;
26523b5dd52aSJames Smart 		job->dd_data = NULL;
26533b5dd52aSJames Smart 		job->job_done(job);
26543b5dd52aSJames Smart 	} else if (rc == 1)
26553b5dd52aSJames Smart 		/* job submitted, will complete later*/
26563b5dd52aSJames Smart 		rc = 0; /* return zero, no error */
26573b5dd52aSJames Smart 	else {
26583b5dd52aSJames Smart 		/* some error occurred */
26593b5dd52aSJames Smart 		job->reply->result = rc;
26603b5dd52aSJames Smart 		job->dd_data = NULL;
26613b5dd52aSJames Smart 	}
26623b5dd52aSJames Smart 
26633b5dd52aSJames Smart 	return rc;
26643b5dd52aSJames Smart }
26653b5dd52aSJames Smart 
26663b5dd52aSJames Smart /**
2667e2aed29fSJames Smart  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
2668e2aed29fSJames Smart  * @phba: Pointer to HBA context object.
2669e2aed29fSJames Smart  * @cmdiocbq: Pointer to command iocb.
2670e2aed29fSJames Smart  * @rspiocbq: Pointer to response iocb.
2671e2aed29fSJames Smart  *
2672e2aed29fSJames Smart  * This function is the completion handler for iocbs issued using
2673e2aed29fSJames Smart  * lpfc_menlo_cmd function. This function is called by the
2674e2aed29fSJames Smart  * ring event handler function without any lock held. This function
2675e2aed29fSJames Smart  * can be called from both worker thread context and interrupt
2676e2aed29fSJames Smart  * context. This function also can be called from another thread which
2677e2aed29fSJames Smart  * cleans up the SLI layer objects.
2678e2aed29fSJames Smart  * This function copies the contents of the response iocb to the
2679e2aed29fSJames Smart  * response iocb memory object provided by the caller of
2680e2aed29fSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
2681e2aed29fSJames Smart  * sleeps for the iocb completion.
2682e2aed29fSJames Smart  **/
2683e2aed29fSJames Smart static void
2684e2aed29fSJames Smart lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
2685e2aed29fSJames Smart 			struct lpfc_iocbq *cmdiocbq,
2686e2aed29fSJames Smart 			struct lpfc_iocbq *rspiocbq)
2687e2aed29fSJames Smart {
2688e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
2689e2aed29fSJames Smart 	struct fc_bsg_job *job;
2690e2aed29fSJames Smart 	IOCB_t *rsp;
2691e2aed29fSJames Smart 	struct lpfc_dmabuf *bmp;
2692e2aed29fSJames Smart 	struct lpfc_bsg_menlo *menlo;
2693e2aed29fSJames Smart 	unsigned long flags;
2694e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
2695e2aed29fSJames Smart 	int rc = 0;
2696e2aed29fSJames Smart 
2697e2aed29fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2698e2aed29fSJames Smart 	dd_data = cmdiocbq->context1;
2699e2aed29fSJames Smart 	if (!dd_data) {
2700e2aed29fSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2701e2aed29fSJames Smart 		return;
2702e2aed29fSJames Smart 	}
2703e2aed29fSJames Smart 
2704e2aed29fSJames Smart 	menlo = &dd_data->context_un.menlo;
2705e2aed29fSJames Smart 	job = menlo->set_job;
2706e2aed29fSJames Smart 	job->dd_data = NULL; /* so timeout handler does not reply */
2707e2aed29fSJames Smart 
2708e2aed29fSJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
2709e2aed29fSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
2710e2aed29fSJames Smart 	if (cmdiocbq->context2 && rspiocbq)
2711e2aed29fSJames Smart 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
2712e2aed29fSJames Smart 		       &rspiocbq->iocb, sizeof(IOCB_t));
2713e2aed29fSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
2714e2aed29fSJames Smart 
2715e2aed29fSJames Smart 	bmp = menlo->bmp;
2716e2aed29fSJames Smart 	rspiocbq = menlo->rspiocbq;
2717e2aed29fSJames Smart 	rsp = &rspiocbq->iocb;
2718e2aed29fSJames Smart 
2719e2aed29fSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
2720e2aed29fSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2721e2aed29fSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
2722e2aed29fSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2723e2aed29fSJames Smart 
2724e2aed29fSJames Smart 	/* always return the xri, this would be used in the case
2725e2aed29fSJames Smart 	 * of a menlo download to allow the data to be sent as a continuation
2726e2aed29fSJames Smart 	 * of the exchange.
2727e2aed29fSJames Smart 	 */
2728e2aed29fSJames Smart 	menlo_resp = (struct menlo_response *)
2729e2aed29fSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
2730e2aed29fSJames Smart 	menlo_resp->xri = rsp->ulpContext;
2731e2aed29fSJames Smart 	if (rsp->ulpStatus) {
2732e2aed29fSJames Smart 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
2733e2aed29fSJames Smart 			switch (rsp->un.ulpWord[4] & 0xff) {
2734e2aed29fSJames Smart 			case IOERR_SEQUENCE_TIMEOUT:
2735e2aed29fSJames Smart 				rc = -ETIMEDOUT;
2736e2aed29fSJames Smart 				break;
2737e2aed29fSJames Smart 			case IOERR_INVALID_RPI:
2738e2aed29fSJames Smart 				rc = -EFAULT;
2739e2aed29fSJames Smart 				break;
2740e2aed29fSJames Smart 			default:
2741e2aed29fSJames Smart 				rc = -EACCES;
2742e2aed29fSJames Smart 				break;
2743e2aed29fSJames Smart 			}
2744e2aed29fSJames Smart 		} else
2745e2aed29fSJames Smart 			rc = -EACCES;
2746e2aed29fSJames Smart 	} else
2747e2aed29fSJames Smart 		job->reply->reply_payload_rcv_len =
2748e2aed29fSJames Smart 			rsp->un.genreq64.bdl.bdeSize;
2749e2aed29fSJames Smart 
2750e2aed29fSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2751e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
2752e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
2753e2aed29fSJames Smart 	kfree(bmp);
2754e2aed29fSJames Smart 	kfree(dd_data);
2755e2aed29fSJames Smart 	/* make error code available to userspace */
2756e2aed29fSJames Smart 	job->reply->result = rc;
2757e2aed29fSJames Smart 	/* complete the job back to userspace */
2758e2aed29fSJames Smart 	job->job_done(job);
2759e2aed29fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2760e2aed29fSJames Smart 	return;
2761e2aed29fSJames Smart }
2762e2aed29fSJames Smart 
2763e2aed29fSJames Smart /**
2764e2aed29fSJames Smart  * lpfc_menlo_cmd - send an ioctl for menlo hardware
2765e2aed29fSJames Smart  * @job: fc_bsg_job to handle
2766e2aed29fSJames Smart  *
2767e2aed29fSJames Smart  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
2768e2aed29fSJames Smart  * all the command completions will return the xri for the command.
2769e2aed29fSJames Smart  * For menlo data requests a gen request 64 CX is used to continue the exchange
2770e2aed29fSJames Smart  * supplied in the menlo request header xri field.
2771e2aed29fSJames Smart  **/
2772e2aed29fSJames Smart static int
2773e2aed29fSJames Smart lpfc_menlo_cmd(struct fc_bsg_job *job)
2774e2aed29fSJames Smart {
2775e2aed29fSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2776e2aed29fSJames Smart 	struct lpfc_hba *phba = vport->phba;
2777e2aed29fSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2778e2aed29fSJames Smart 	IOCB_t *cmd, *rsp;
2779e2aed29fSJames Smart 	int rc = 0;
2780e2aed29fSJames Smart 	struct menlo_command *menlo_cmd;
2781e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
2782e2aed29fSJames Smart 	struct lpfc_dmabuf *bmp = NULL;
2783e2aed29fSJames Smart 	int request_nseg;
2784e2aed29fSJames Smart 	int reply_nseg;
2785e2aed29fSJames Smart 	struct scatterlist *sgel = NULL;
2786e2aed29fSJames Smart 	int numbde;
2787e2aed29fSJames Smart 	dma_addr_t busaddr;
2788e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
2789e2aed29fSJames Smart 	struct ulp_bde64 *bpl = NULL;
2790e2aed29fSJames Smart 
2791e2aed29fSJames Smart 	/* in case no data is returned return just the return code */
2792e2aed29fSJames Smart 	job->reply->reply_payload_rcv_len = 0;
2793e2aed29fSJames Smart 
2794e2aed29fSJames Smart 	if (job->request_len <
2795e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) +
2796e2aed29fSJames Smart 		sizeof(struct menlo_command)) {
2797e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2798e2aed29fSJames Smart 				"2784 Received MENLO_CMD request below "
2799e2aed29fSJames Smart 				"minimum size\n");
2800e2aed29fSJames Smart 		rc = -ERANGE;
2801e2aed29fSJames Smart 		goto no_dd_data;
2802e2aed29fSJames Smart 	}
2803e2aed29fSJames Smart 
2804e2aed29fSJames Smart 	if (job->reply_len <
2805e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
2806e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2807e2aed29fSJames Smart 				"2785 Received MENLO_CMD reply below "
2808e2aed29fSJames Smart 				"minimum size\n");
2809e2aed29fSJames Smart 		rc = -ERANGE;
2810e2aed29fSJames Smart 		goto no_dd_data;
2811e2aed29fSJames Smart 	}
2812e2aed29fSJames Smart 
2813e2aed29fSJames Smart 	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
2814e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2815e2aed29fSJames Smart 				"2786 Adapter does not support menlo "
2816e2aed29fSJames Smart 				"commands\n");
2817e2aed29fSJames Smart 		rc = -EPERM;
2818e2aed29fSJames Smart 		goto no_dd_data;
2819e2aed29fSJames Smart 	}
2820e2aed29fSJames Smart 
2821e2aed29fSJames Smart 	menlo_cmd = (struct menlo_command *)
2822e2aed29fSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
2823e2aed29fSJames Smart 
2824e2aed29fSJames Smart 	menlo_resp = (struct menlo_response *)
2825e2aed29fSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
2826e2aed29fSJames Smart 
2827e2aed29fSJames Smart 	/* allocate our bsg tracking structure */
2828e2aed29fSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
2829e2aed29fSJames Smart 	if (!dd_data) {
2830e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2831e2aed29fSJames Smart 				"2787 Failed allocation of dd_data\n");
2832e2aed29fSJames Smart 		rc = -ENOMEM;
2833e2aed29fSJames Smart 		goto no_dd_data;
2834e2aed29fSJames Smart 	}
2835e2aed29fSJames Smart 
2836e2aed29fSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2837e2aed29fSJames Smart 	if (!bmp) {
2838e2aed29fSJames Smart 		rc = -ENOMEM;
2839e2aed29fSJames Smart 		goto free_dd;
2840e2aed29fSJames Smart 	}
2841e2aed29fSJames Smart 
2842e2aed29fSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2843e2aed29fSJames Smart 	if (!cmdiocbq) {
2844e2aed29fSJames Smart 		rc = -ENOMEM;
2845e2aed29fSJames Smart 		goto free_bmp;
2846e2aed29fSJames Smart 	}
2847e2aed29fSJames Smart 
2848e2aed29fSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
2849e2aed29fSJames Smart 	if (!rspiocbq) {
2850e2aed29fSJames Smart 		rc = -ENOMEM;
2851e2aed29fSJames Smart 		goto free_cmdiocbq;
2852e2aed29fSJames Smart 	}
2853e2aed29fSJames Smart 
2854e2aed29fSJames Smart 	rsp = &rspiocbq->iocb;
2855e2aed29fSJames Smart 
2856e2aed29fSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
2857e2aed29fSJames Smart 	if (!bmp->virt) {
2858e2aed29fSJames Smart 		rc = -ENOMEM;
2859e2aed29fSJames Smart 		goto free_rspiocbq;
2860e2aed29fSJames Smart 	}
2861e2aed29fSJames Smart 
2862e2aed29fSJames Smart 	INIT_LIST_HEAD(&bmp->list);
2863e2aed29fSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
2864e2aed29fSJames Smart 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
2865e2aed29fSJames Smart 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
2866e2aed29fSJames Smart 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
2867e2aed29fSJames Smart 		busaddr = sg_dma_address(sgel);
2868e2aed29fSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2869e2aed29fSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
2870e2aed29fSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
2871e2aed29fSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
2872e2aed29fSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
2873e2aed29fSJames Smart 		bpl++;
2874e2aed29fSJames Smart 	}
2875e2aed29fSJames Smart 
2876e2aed29fSJames Smart 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
2877e2aed29fSJames Smart 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2878e2aed29fSJames Smart 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
2879e2aed29fSJames Smart 		busaddr = sg_dma_address(sgel);
2880e2aed29fSJames Smart 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2881e2aed29fSJames Smart 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
2882e2aed29fSJames Smart 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
2883e2aed29fSJames Smart 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
2884e2aed29fSJames Smart 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
2885e2aed29fSJames Smart 		bpl++;
2886e2aed29fSJames Smart 	}
2887e2aed29fSJames Smart 
2888e2aed29fSJames Smart 	cmd = &cmdiocbq->iocb;
2889e2aed29fSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
2890e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
2891e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
2892e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2893e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
2894e2aed29fSJames Smart 	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
2895e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
2896e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
2897e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
2898e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
2899e2aed29fSJames Smart 	cmd->ulpBdeCount = 1;
2900e2aed29fSJames Smart 	cmd->ulpClass = CLASS3;
2901e2aed29fSJames Smart 	cmd->ulpOwner = OWN_CHIP;
2902e2aed29fSJames Smart 	cmd->ulpLe = 1; /* Limited Edition */
2903e2aed29fSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2904e2aed29fSJames Smart 	cmdiocbq->vport = phba->pport;
2905e2aed29fSJames Smart 	/* We want the firmware to timeout before we do */
2906e2aed29fSJames Smart 	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
2907e2aed29fSJames Smart 	cmdiocbq->context3 = bmp;
2908e2aed29fSJames Smart 	cmdiocbq->context2 = rspiocbq;
2909e2aed29fSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
2910e2aed29fSJames Smart 	cmdiocbq->context1 = dd_data;
2911e2aed29fSJames Smart 	cmdiocbq->context2 = rspiocbq;
2912e2aed29fSJames Smart 	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
2913e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
2914e2aed29fSJames Smart 		cmd->ulpPU = MENLO_PU; /* 3 */
2915e2aed29fSJames Smart 		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
2916e2aed29fSJames Smart 		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
2917e2aed29fSJames Smart 	} else {
2918e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
2919e2aed29fSJames Smart 		cmd->ulpPU = 1;
2920e2aed29fSJames Smart 		cmd->un.ulpWord[4] = 0;
2921e2aed29fSJames Smart 		cmd->ulpContext = menlo_cmd->xri;
2922e2aed29fSJames Smart 	}
2923e2aed29fSJames Smart 
2924e2aed29fSJames Smart 	dd_data->type = TYPE_MENLO;
2925e2aed29fSJames Smart 	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
2926e2aed29fSJames Smart 	dd_data->context_un.menlo.rspiocbq = rspiocbq;
2927e2aed29fSJames Smart 	dd_data->context_un.menlo.set_job = job;
2928e2aed29fSJames Smart 	dd_data->context_un.menlo.bmp = bmp;
2929e2aed29fSJames Smart 
2930e2aed29fSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2931e2aed29fSJames Smart 		MENLO_TIMEOUT - 5);
2932e2aed29fSJames Smart 	if (rc == IOCB_SUCCESS)
2933e2aed29fSJames Smart 		return 0; /* done for now */
2934e2aed29fSJames Smart 
2935e2aed29fSJames Smart 	/* iocb failed so cleanup */
2936e2aed29fSJames Smart 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
2937e2aed29fSJames Smart 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
2938e2aed29fSJames Smart 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
2939e2aed29fSJames Smart 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2940e2aed29fSJames Smart 
2941e2aed29fSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
2942e2aed29fSJames Smart 
2943e2aed29fSJames Smart free_rspiocbq:
2944e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, rspiocbq);
2945e2aed29fSJames Smart free_cmdiocbq:
2946e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
2947e2aed29fSJames Smart free_bmp:
2948e2aed29fSJames Smart 	kfree(bmp);
2949e2aed29fSJames Smart free_dd:
2950e2aed29fSJames Smart 	kfree(dd_data);
2951e2aed29fSJames Smart no_dd_data:
2952e2aed29fSJames Smart 	/* make error code available to userspace */
2953e2aed29fSJames Smart 	job->reply->result = rc;
2954e2aed29fSJames Smart 	job->dd_data = NULL;
2955e2aed29fSJames Smart 	return rc;
2956e2aed29fSJames Smart }
2957e2aed29fSJames Smart /**
2958f1c3b0fcSJames Smart  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
2959f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
29603b5dd52aSJames Smart  **/
2961f1c3b0fcSJames Smart static int
2962f1c3b0fcSJames Smart lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
2963f1c3b0fcSJames Smart {
2964f1c3b0fcSJames Smart 	int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
29654cc0e56eSJames Smart 	int rc;
2966f1c3b0fcSJames Smart 
2967f1c3b0fcSJames Smart 	switch (command) {
2968f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
29694cc0e56eSJames Smart 		rc = lpfc_bsg_hba_set_event(job);
2970f1c3b0fcSJames Smart 		break;
2971f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
29724cc0e56eSJames Smart 		rc = lpfc_bsg_hba_get_event(job);
2973f1c3b0fcSJames Smart 		break;
29743b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
29753b5dd52aSJames Smart 		rc = lpfc_bsg_send_mgmt_rsp(job);
29763b5dd52aSJames Smart 		break;
29773b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE:
29783b5dd52aSJames Smart 		rc = lpfc_bsg_diag_mode(job);
29793b5dd52aSJames Smart 		break;
29803b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_DIAG_TEST:
29813b5dd52aSJames Smart 		rc = lpfc_bsg_diag_test(job);
29823b5dd52aSJames Smart 		break;
29833b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
29843b5dd52aSJames Smart 		rc = lpfc_bsg_get_dfc_rev(job);
29853b5dd52aSJames Smart 		break;
29863b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_MBOX:
29873b5dd52aSJames Smart 		rc = lpfc_bsg_mbox_cmd(job);
29883b5dd52aSJames Smart 		break;
2989e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_CMD:
2990e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_DATA:
2991e2aed29fSJames Smart 		rc = lpfc_menlo_cmd(job);
2992e2aed29fSJames Smart 		break;
2993f1c3b0fcSJames Smart 	default:
29944cc0e56eSJames Smart 		rc = -EINVAL;
29954cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len = 0;
29964cc0e56eSJames Smart 		/* make error code available to userspace */
29974cc0e56eSJames Smart 		job->reply->result = rc;
29984cc0e56eSJames Smart 		break;
2999f1c3b0fcSJames Smart 	}
30004cc0e56eSJames Smart 
30014cc0e56eSJames Smart 	return rc;
3002f1c3b0fcSJames Smart }
3003f1c3b0fcSJames Smart 
3004f1c3b0fcSJames Smart /**
3005f1c3b0fcSJames Smart  * lpfc_bsg_request - handle a bsg request from the FC transport
3006f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
30073b5dd52aSJames Smart  **/
3008f1c3b0fcSJames Smart int
3009f1c3b0fcSJames Smart lpfc_bsg_request(struct fc_bsg_job *job)
3010f1c3b0fcSJames Smart {
3011f1c3b0fcSJames Smart 	uint32_t msgcode;
30124cc0e56eSJames Smart 	int rc;
3013f1c3b0fcSJames Smart 
3014f1c3b0fcSJames Smart 	msgcode = job->request->msgcode;
3015f1c3b0fcSJames Smart 	switch (msgcode) {
3016f1c3b0fcSJames Smart 	case FC_BSG_HST_VENDOR:
3017f1c3b0fcSJames Smart 		rc = lpfc_bsg_hst_vendor(job);
3018f1c3b0fcSJames Smart 		break;
3019f1c3b0fcSJames Smart 	case FC_BSG_RPT_ELS:
3020f1c3b0fcSJames Smart 		rc = lpfc_bsg_rport_els(job);
3021f1c3b0fcSJames Smart 		break;
3022f1c3b0fcSJames Smart 	case FC_BSG_RPT_CT:
30234cc0e56eSJames Smart 		rc = lpfc_bsg_send_mgmt_cmd(job);
3024f1c3b0fcSJames Smart 		break;
3025f1c3b0fcSJames Smart 	default:
30264cc0e56eSJames Smart 		rc = -EINVAL;
30274cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len = 0;
30284cc0e56eSJames Smart 		/* make error code available to userspace */
30294cc0e56eSJames Smart 		job->reply->result = rc;
3030f1c3b0fcSJames Smart 		break;
3031f1c3b0fcSJames Smart 	}
3032f1c3b0fcSJames Smart 
3033f1c3b0fcSJames Smart 	return rc;
3034f1c3b0fcSJames Smart }
3035f1c3b0fcSJames Smart 
3036f1c3b0fcSJames Smart /**
3037f1c3b0fcSJames Smart  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
3038f1c3b0fcSJames Smart  * @job: fc_bsg_job that has timed out
3039f1c3b0fcSJames Smart  *
3040f1c3b0fcSJames Smart  * This function just aborts the job's IOCB.  The aborted IOCB will return to
3041f1c3b0fcSJames Smart  * the waiting function which will handle passing the error back to userspace
30423b5dd52aSJames Smart  **/
3043f1c3b0fcSJames Smart int
3044f1c3b0fcSJames Smart lpfc_bsg_timeout(struct fc_bsg_job *job)
3045f1c3b0fcSJames Smart {
3046f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
3047f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
30484cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocb;
30494cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
30504cc0e56eSJames Smart 	struct lpfc_bsg_iocb *iocb;
30513b5dd52aSJames Smart 	struct lpfc_bsg_mbox *mbox;
3052e2aed29fSJames Smart 	struct lpfc_bsg_menlo *menlo;
3053f1c3b0fcSJames Smart 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
30544cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
30554cc0e56eSJames Smart 	unsigned long flags;
3056f1c3b0fcSJames Smart 
30574cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
30584cc0e56eSJames Smart 	dd_data = (struct bsg_job_data *)job->dd_data;
30594cc0e56eSJames Smart 	/* timeout and completion crossed paths if no dd_data */
30604cc0e56eSJames Smart 	if (!dd_data) {
30614cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
30624cc0e56eSJames Smart 		return 0;
30634cc0e56eSJames Smart 	}
30644cc0e56eSJames Smart 
30654cc0e56eSJames Smart 	switch (dd_data->type) {
30664cc0e56eSJames Smart 	case TYPE_IOCB:
30674cc0e56eSJames Smart 		iocb = &dd_data->context_un.iocb;
30684cc0e56eSJames Smart 		cmdiocb = iocb->cmdiocbq;
30694cc0e56eSJames Smart 		/* hint to completion handler that the job timed out */
30704cc0e56eSJames Smart 		job->reply->result = -EAGAIN;
30714cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
30724cc0e56eSJames Smart 		/* this will call our completion handler */
30734cc0e56eSJames Smart 		spin_lock_irq(&phba->hbalock);
3074f1c3b0fcSJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
30754cc0e56eSJames Smart 		spin_unlock_irq(&phba->hbalock);
30764cc0e56eSJames Smart 		break;
30774cc0e56eSJames Smart 	case TYPE_EVT:
30784cc0e56eSJames Smart 		evt = dd_data->context_un.evt;
30794cc0e56eSJames Smart 		/* this event has no job anymore */
30804cc0e56eSJames Smart 		evt->set_job = NULL;
30814cc0e56eSJames Smart 		job->dd_data = NULL;
30824cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len = 0;
30834cc0e56eSJames Smart 		/* Return -EAGAIN which is our way of signallying the
30844cc0e56eSJames Smart 		 * app to retry.
30854cc0e56eSJames Smart 		 */
30864cc0e56eSJames Smart 		job->reply->result = -EAGAIN;
30874cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
30884cc0e56eSJames Smart 		job->job_done(job);
30894cc0e56eSJames Smart 		break;
30903b5dd52aSJames Smart 	case TYPE_MBOX:
30913b5dd52aSJames Smart 		mbox = &dd_data->context_un.mbox;
30923b5dd52aSJames Smart 		/* this mbox has no job anymore */
30933b5dd52aSJames Smart 		mbox->set_job = NULL;
30943b5dd52aSJames Smart 		job->dd_data = NULL;
30953b5dd52aSJames Smart 		job->reply->reply_payload_rcv_len = 0;
30963b5dd52aSJames Smart 		job->reply->result = -EAGAIN;
30973b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
30983b5dd52aSJames Smart 		job->job_done(job);
30993b5dd52aSJames Smart 		break;
3100e2aed29fSJames Smart 	case TYPE_MENLO:
3101e2aed29fSJames Smart 		menlo = &dd_data->context_un.menlo;
3102e2aed29fSJames Smart 		cmdiocb = menlo->cmdiocbq;
3103e2aed29fSJames Smart 		/* hint to completion handler that the job timed out */
3104e2aed29fSJames Smart 		job->reply->result = -EAGAIN;
3105e2aed29fSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3106e2aed29fSJames Smart 		/* this will call our completion handler */
3107e2aed29fSJames Smart 		spin_lock_irq(&phba->hbalock);
3108e2aed29fSJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
3109e2aed29fSJames Smart 		spin_unlock_irq(&phba->hbalock);
3110e2aed29fSJames Smart 		break;
31114cc0e56eSJames Smart 	default:
31124cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
31134cc0e56eSJames Smart 		break;
31144cc0e56eSJames Smart 	}
3115f1c3b0fcSJames Smart 
31164cc0e56eSJames Smart 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
31174cc0e56eSJames Smart 	 * otherwise an error message will be displayed on the console
31184cc0e56eSJames Smart 	 * so always return success (zero)
31194cc0e56eSJames Smart 	 */
3120f1c3b0fcSJames Smart 	return 0;
3121f1c3b0fcSJames Smart }
3122