xref: /linux/drivers/scsi/lpfc/lpfc_bsg.c (revision e5fcb81d40d44d3000e5ff8b6c9d87ea36a26faa)
1f1c3b0fcSJames Smart /*******************************************************************
2f1c3b0fcSJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3f1c3b0fcSJames Smart  * Fibre Channel Host Bus Adapters.                                *
495df18c2SJames Smart  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
54ae2ebdeSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6f25e8e79SJames Smart  * Copyright (C) 2009-2015 Emulex.  All rights reserved.           *
7f1c3b0fcSJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
9f1c3b0fcSJames Smart  *                                                                 *
10f1c3b0fcSJames Smart  * This program is free software; you can redistribute it and/or   *
11f1c3b0fcSJames Smart  * modify it under the terms of version 2 of the GNU General       *
12f1c3b0fcSJames Smart  * Public License as published by the Free Software Foundation.    *
13f1c3b0fcSJames Smart  * This program is distributed in the hope that it will be useful. *
14f1c3b0fcSJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15f1c3b0fcSJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16f1c3b0fcSJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17f1c3b0fcSJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18f1c3b0fcSJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19f1c3b0fcSJames Smart  * more details, a copy of which can be found in the file COPYING  *
20f1c3b0fcSJames Smart  * included with this package.                                     *
21f1c3b0fcSJames Smart  *******************************************************************/
22f1c3b0fcSJames Smart 
23f1c3b0fcSJames Smart #include <linux/interrupt.h>
24f1c3b0fcSJames Smart #include <linux/mempool.h>
25f1c3b0fcSJames Smart #include <linux/pci.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
27277e76f1SJames Smart #include <linux/delay.h>
287ad20aa9SJames Smart #include <linux/list.h>
29eb340948SJohannes Thumshirn #include <linux/bsg-lib.h>
30d2cc9bcdSJames Smart #include <linux/vmalloc.h>
31f1c3b0fcSJames Smart 
32f1c3b0fcSJames Smart #include <scsi/scsi.h>
33f1c3b0fcSJames Smart #include <scsi/scsi_host.h>
34f1c3b0fcSJames Smart #include <scsi/scsi_transport_fc.h>
35f1c3b0fcSJames Smart #include <scsi/scsi_bsg_fc.h>
366a9c52cfSJames Smart #include <scsi/fc/fc_fs.h>
37f1c3b0fcSJames Smart 
38f1c3b0fcSJames Smart #include "lpfc_hw4.h"
39f1c3b0fcSJames Smart #include "lpfc_hw.h"
40f1c3b0fcSJames Smart #include "lpfc_sli.h"
41f1c3b0fcSJames Smart #include "lpfc_sli4.h"
42f1c3b0fcSJames Smart #include "lpfc_nl.h"
434fede78fSJames Smart #include "lpfc_bsg.h"
44f1c3b0fcSJames Smart #include "lpfc_disc.h"
45f1c3b0fcSJames Smart #include "lpfc_scsi.h"
46f1c3b0fcSJames Smart #include "lpfc.h"
47f1c3b0fcSJames Smart #include "lpfc_logmsg.h"
48f1c3b0fcSJames Smart #include "lpfc_crtn.h"
49b76f2dc9SJames Smart #include "lpfc_debugfs.h"
50f1c3b0fcSJames Smart #include "lpfc_vport.h"
51f1c3b0fcSJames Smart #include "lpfc_version.h"
52f1c3b0fcSJames Smart 
534cc0e56eSJames Smart struct lpfc_bsg_event {
544cc0e56eSJames Smart 	struct list_head node;
554cc0e56eSJames Smart 	struct kref kref;
564cc0e56eSJames Smart 	wait_queue_head_t wq;
574cc0e56eSJames Smart 
584cc0e56eSJames Smart 	/* Event type and waiter identifiers */
594cc0e56eSJames Smart 	uint32_t type_mask;
604cc0e56eSJames Smart 	uint32_t req_id;
614cc0e56eSJames Smart 	uint32_t reg_id;
624cc0e56eSJames Smart 
634cc0e56eSJames Smart 	/* next two flags are here for the auto-delete logic */
644cc0e56eSJames Smart 	unsigned long wait_time_stamp;
654cc0e56eSJames Smart 	int waiting;
664cc0e56eSJames Smart 
674cc0e56eSJames Smart 	/* seen and not seen events */
684cc0e56eSJames Smart 	struct list_head events_to_get;
694cc0e56eSJames Smart 	struct list_head events_to_see;
704cc0e56eSJames Smart 
71a33c4f7bSJames Smart 	/* driver data associated with the job */
72a33c4f7bSJames Smart 	void *dd_data;
734cc0e56eSJames Smart };
744cc0e56eSJames Smart 
754cc0e56eSJames Smart struct lpfc_bsg_iocb {
764cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocbq;
77a33c4f7bSJames Smart 	struct lpfc_dmabuf *rmp;
784cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
794cc0e56eSJames Smart };
804cc0e56eSJames Smart 
813b5dd52aSJames Smart struct lpfc_bsg_mbox {
823b5dd52aSJames Smart 	LPFC_MBOXQ_t *pmboxq;
833b5dd52aSJames Smart 	MAILBOX_t *mb;
847ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
857a470277SJames Smart 	uint8_t *ext; /* extended mailbox data */
867a470277SJames Smart 	uint32_t mbOffset; /* from app */
877a470277SJames Smart 	uint32_t inExtWLen; /* from app */
88c7495937SJames Smart 	uint32_t outExtWLen; /* from app */
893b5dd52aSJames Smart };
903b5dd52aSJames Smart 
91e2aed29fSJames Smart #define MENLO_DID 0x0000FC0E
92e2aed29fSJames Smart 
93e2aed29fSJames Smart struct lpfc_bsg_menlo {
94e2aed29fSJames Smart 	struct lpfc_iocbq *cmdiocbq;
95a33c4f7bSJames Smart 	struct lpfc_dmabuf *rmp;
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;
10475cc8cfcSJohannes Thumshirn 	struct bsg_job *set_job; /* job waiting for this iocb to finish */
1054cc0e56eSJames Smart 	union {
1064cc0e56eSJames Smart 		struct lpfc_bsg_event *evt;
1074cc0e56eSJames Smart 		struct lpfc_bsg_iocb iocb;
1083b5dd52aSJames Smart 		struct lpfc_bsg_mbox mbox;
109e2aed29fSJames Smart 		struct lpfc_bsg_menlo menlo;
1104cc0e56eSJames Smart 	} context_un;
1114cc0e56eSJames Smart };
1124cc0e56eSJames Smart 
1134cc0e56eSJames Smart struct event_data {
1144cc0e56eSJames Smart 	struct list_head node;
1154cc0e56eSJames Smart 	uint32_t type;
1164cc0e56eSJames Smart 	uint32_t immed_dat;
1174cc0e56eSJames Smart 	void *data;
1184cc0e56eSJames Smart 	uint32_t len;
1194cc0e56eSJames Smart };
1204cc0e56eSJames Smart 
1213b5dd52aSJames Smart #define BUF_SZ_4K 4096
1224cc0e56eSJames Smart #define SLI_CT_ELX_LOOPBACK 0x10
1234cc0e56eSJames Smart 
1244cc0e56eSJames Smart enum ELX_LOOPBACK_CMD {
1254cc0e56eSJames Smart 	ELX_LOOPBACK_XRI_SETUP,
1264cc0e56eSJames Smart 	ELX_LOOPBACK_DATA,
1274cc0e56eSJames Smart };
1284cc0e56eSJames Smart 
1293b5dd52aSJames Smart #define ELX_LOOPBACK_HEADER_SZ \
1303b5dd52aSJames Smart 	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
1313b5dd52aSJames Smart 
1324cc0e56eSJames Smart struct lpfc_dmabufext {
1334cc0e56eSJames Smart 	struct lpfc_dmabuf dma;
1344cc0e56eSJames Smart 	uint32_t size;
1354cc0e56eSJames Smart 	uint32_t flag;
1364cc0e56eSJames Smart };
1374cc0e56eSJames Smart 
138a33c4f7bSJames Smart static void
139a33c4f7bSJames Smart lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
140a33c4f7bSJames Smart {
141a33c4f7bSJames Smart 	struct lpfc_dmabuf *mlast, *next_mlast;
142a33c4f7bSJames Smart 
143a33c4f7bSJames Smart 	if (mlist) {
144a33c4f7bSJames Smart 		list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
145a33c4f7bSJames Smart 					 list) {
146a33c4f7bSJames Smart 			lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
147a33c4f7bSJames Smart 			list_del(&mlast->list);
148a33c4f7bSJames Smart 			kfree(mlast);
149a33c4f7bSJames Smart 		}
150a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
151a33c4f7bSJames Smart 		kfree(mlist);
152a33c4f7bSJames Smart 	}
153a33c4f7bSJames Smart 	return;
154a33c4f7bSJames Smart }
155a33c4f7bSJames Smart 
156a33c4f7bSJames Smart static struct lpfc_dmabuf *
157a33c4f7bSJames Smart lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
158a33c4f7bSJames Smart 		       int outbound_buffers, struct ulp_bde64 *bpl,
159a33c4f7bSJames Smart 		       int *bpl_entries)
160a33c4f7bSJames Smart {
161a33c4f7bSJames Smart 	struct lpfc_dmabuf *mlist = NULL;
162a33c4f7bSJames Smart 	struct lpfc_dmabuf *mp;
163a33c4f7bSJames Smart 	unsigned int bytes_left = size;
164a33c4f7bSJames Smart 
165a33c4f7bSJames Smart 	/* Verify we can support the size specified */
166a33c4f7bSJames Smart 	if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
167a33c4f7bSJames Smart 		return NULL;
168a33c4f7bSJames Smart 
169a33c4f7bSJames Smart 	/* Determine the number of dma buffers to allocate */
170a33c4f7bSJames Smart 	*bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
171a33c4f7bSJames Smart 			size/LPFC_BPL_SIZE);
172a33c4f7bSJames Smart 
173a33c4f7bSJames Smart 	/* Allocate dma buffer and place in BPL passed */
174a33c4f7bSJames Smart 	while (bytes_left) {
175a33c4f7bSJames Smart 		/* Allocate dma buffer  */
176a33c4f7bSJames Smart 		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
177a33c4f7bSJames Smart 		if (!mp) {
178a33c4f7bSJames Smart 			if (mlist)
179a33c4f7bSJames Smart 				lpfc_free_bsg_buffers(phba, mlist);
180a33c4f7bSJames Smart 			return NULL;
181a33c4f7bSJames Smart 		}
182a33c4f7bSJames Smart 
183a33c4f7bSJames Smart 		INIT_LIST_HEAD(&mp->list);
184a33c4f7bSJames Smart 		mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
185a33c4f7bSJames Smart 
186a33c4f7bSJames Smart 		if (!mp->virt) {
187a33c4f7bSJames Smart 			kfree(mp);
188a33c4f7bSJames Smart 			if (mlist)
189a33c4f7bSJames Smart 				lpfc_free_bsg_buffers(phba, mlist);
190a33c4f7bSJames Smart 			return NULL;
191a33c4f7bSJames Smart 		}
192a33c4f7bSJames Smart 
193a33c4f7bSJames Smart 		/* Queue it to a linked list */
194a33c4f7bSJames Smart 		if (!mlist)
195a33c4f7bSJames Smart 			mlist = mp;
196a33c4f7bSJames Smart 		else
197a33c4f7bSJames Smart 			list_add_tail(&mp->list, &mlist->list);
198a33c4f7bSJames Smart 
199a33c4f7bSJames Smart 		/* Add buffer to buffer pointer list */
200a33c4f7bSJames Smart 		if (outbound_buffers)
201a33c4f7bSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
202a33c4f7bSJames Smart 		else
203a33c4f7bSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
204a33c4f7bSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
205a33c4f7bSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
206a33c4f7bSJames Smart 		bpl->tus.f.bdeSize = (uint16_t)
207a33c4f7bSJames Smart 			(bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
208a33c4f7bSJames Smart 			 bytes_left);
209a33c4f7bSJames Smart 		bytes_left -= bpl->tus.f.bdeSize;
210a33c4f7bSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
211a33c4f7bSJames Smart 		bpl++;
212a33c4f7bSJames Smart 	}
213a33c4f7bSJames Smart 	return mlist;
214a33c4f7bSJames Smart }
215a33c4f7bSJames Smart 
216a33c4f7bSJames Smart static unsigned int
217a33c4f7bSJames Smart lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
218eb340948SJohannes Thumshirn 		   struct bsg_buffer *bsg_buffers,
219a33c4f7bSJames Smart 		   unsigned int bytes_to_transfer, int to_buffers)
220a33c4f7bSJames Smart {
221a33c4f7bSJames Smart 
222a33c4f7bSJames Smart 	struct lpfc_dmabuf *mp;
223a33c4f7bSJames Smart 	unsigned int transfer_bytes, bytes_copied = 0;
224a33c4f7bSJames Smart 	unsigned int sg_offset, dma_offset;
225a33c4f7bSJames Smart 	unsigned char *dma_address, *sg_address;
226a33c4f7bSJames Smart 	LIST_HEAD(temp_list);
227d5ce53b7SJames Smart 	struct sg_mapping_iter miter;
228d5ce53b7SJames Smart 	unsigned long flags;
229d5ce53b7SJames Smart 	unsigned int sg_flags = SG_MITER_ATOMIC;
230d5ce53b7SJames Smart 	bool sg_valid;
231a33c4f7bSJames Smart 
232a33c4f7bSJames Smart 	list_splice_init(&dma_buffers->list, &temp_list);
233a33c4f7bSJames Smart 	list_add(&dma_buffers->list, &temp_list);
234a33c4f7bSJames Smart 	sg_offset = 0;
235d5ce53b7SJames Smart 	if (to_buffers)
236d5ce53b7SJames Smart 		sg_flags |= SG_MITER_FROM_SG;
237d5ce53b7SJames Smart 	else
238d5ce53b7SJames Smart 		sg_flags |= SG_MITER_TO_SG;
239d5ce53b7SJames Smart 	sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
240d5ce53b7SJames Smart 		       sg_flags);
241d5ce53b7SJames Smart 	local_irq_save(flags);
242d5ce53b7SJames Smart 	sg_valid = sg_miter_next(&miter);
243a33c4f7bSJames Smart 	list_for_each_entry(mp, &temp_list, list) {
244a33c4f7bSJames Smart 		dma_offset = 0;
245d5ce53b7SJames Smart 		while (bytes_to_transfer && sg_valid &&
246a33c4f7bSJames Smart 		       (dma_offset < LPFC_BPL_SIZE)) {
247a33c4f7bSJames Smart 			dma_address = mp->virt + dma_offset;
248a33c4f7bSJames Smart 			if (sg_offset) {
249a33c4f7bSJames Smart 				/* Continue previous partial transfer of sg */
250d5ce53b7SJames Smart 				sg_address = miter.addr + sg_offset;
251d5ce53b7SJames Smart 				transfer_bytes = miter.length - sg_offset;
252a33c4f7bSJames Smart 			} else {
253d5ce53b7SJames Smart 				sg_address = miter.addr;
254d5ce53b7SJames Smart 				transfer_bytes = miter.length;
255a33c4f7bSJames Smart 			}
256a33c4f7bSJames Smart 			if (bytes_to_transfer < transfer_bytes)
257a33c4f7bSJames Smart 				transfer_bytes = bytes_to_transfer;
258a33c4f7bSJames Smart 			if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
259a33c4f7bSJames Smart 				transfer_bytes = LPFC_BPL_SIZE - dma_offset;
260a33c4f7bSJames Smart 			if (to_buffers)
261a33c4f7bSJames Smart 				memcpy(dma_address, sg_address, transfer_bytes);
262a33c4f7bSJames Smart 			else
263a33c4f7bSJames Smart 				memcpy(sg_address, dma_address, transfer_bytes);
264a33c4f7bSJames Smart 			dma_offset += transfer_bytes;
265a33c4f7bSJames Smart 			sg_offset += transfer_bytes;
266a33c4f7bSJames Smart 			bytes_to_transfer -= transfer_bytes;
267a33c4f7bSJames Smart 			bytes_copied += transfer_bytes;
268d5ce53b7SJames Smart 			if (sg_offset >= miter.length) {
269a33c4f7bSJames Smart 				sg_offset = 0;
270d5ce53b7SJames Smart 				sg_valid = sg_miter_next(&miter);
271a33c4f7bSJames Smart 			}
272a33c4f7bSJames Smart 		}
273a33c4f7bSJames Smart 	}
274d5ce53b7SJames Smart 	sg_miter_stop(&miter);
275d5ce53b7SJames Smart 	local_irq_restore(flags);
276a33c4f7bSJames Smart 	list_del_init(&dma_buffers->list);
277a33c4f7bSJames Smart 	list_splice(&temp_list, &dma_buffers->list);
278a33c4f7bSJames Smart 	return bytes_copied;
279a33c4f7bSJames Smart }
280a33c4f7bSJames Smart 
281f1c3b0fcSJames Smart /**
2824cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
2834cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
2844cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
2854cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
2864cc0e56eSJames Smart  *
2874cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
2884cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
2894cc0e56eSJames Smart  * ring event handler function without any lock held. This function
2904cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
2914cc0e56eSJames Smart  * context. This function also can be called from another thread which
2924cc0e56eSJames Smart  * cleans up the SLI layer objects.
2934cc0e56eSJames Smart  * This function copies the contents of the response iocb to the
2944cc0e56eSJames Smart  * response iocb memory object provided by the caller of
2954cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
2964cc0e56eSJames Smart  * sleeps for the iocb completion.
2974cc0e56eSJames Smart  **/
2984cc0e56eSJames Smart static void
2994cc0e56eSJames Smart lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
3004cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
3014cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
3024cc0e56eSJames Smart {
3034cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
30475cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
30501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
3064cc0e56eSJames Smart 	IOCB_t *rsp;
307a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
3084cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
3094cc0e56eSJames Smart 	struct lpfc_bsg_iocb *iocb;
3104cc0e56eSJames Smart 	unsigned long flags;
311a33c4f7bSJames Smart 	unsigned int rsp_size;
3124cc0e56eSJames Smart 	int rc = 0;
3134cc0e56eSJames Smart 
314a33c4f7bSJames Smart 	dd_data = cmdiocbq->context1;
315a33c4f7bSJames Smart 
316a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3174cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
318a33c4f7bSJames Smart 	job = dd_data->set_job;
319a33c4f7bSJames Smart 	if (job) {
32001e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
321a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job */
322a33c4f7bSJames Smart 		job->dd_data = NULL;
3234cc0e56eSJames Smart 	}
324a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3254cc0e56eSJames Smart 
326b5a9b2dfSJames Smart 	/* Close the timeout handler abort window */
327b5a9b2dfSJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3281b8d11abSJames Smart 	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
329b5a9b2dfSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
330b5a9b2dfSJames Smart 
3314cc0e56eSJames Smart 	iocb = &dd_data->context_un.iocb;
332a33c4f7bSJames Smart 	ndlp = iocb->ndlp;
333a33c4f7bSJames Smart 	rmp = iocb->rmp;
334a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
335a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
3364cc0e56eSJames Smart 	rsp = &rspiocbq->iocb;
3374cc0e56eSJames Smart 
338a33c4f7bSJames Smart 	/* Copy the completed data or set the error status */
3394cc0e56eSJames Smart 
340a33c4f7bSJames Smart 	if (job) {
3414cc0e56eSJames Smart 		if (rsp->ulpStatus) {
3424cc0e56eSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
343e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
3444cc0e56eSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
3454cc0e56eSJames Smart 					rc = -ETIMEDOUT;
3464cc0e56eSJames Smart 					break;
3474cc0e56eSJames Smart 				case IOERR_INVALID_RPI:
3484cc0e56eSJames Smart 					rc = -EFAULT;
3494cc0e56eSJames Smart 					break;
3504cc0e56eSJames Smart 				default:
3514cc0e56eSJames Smart 					rc = -EACCES;
3524cc0e56eSJames Smart 					break;
3534cc0e56eSJames Smart 				}
354a33c4f7bSJames Smart 			} else {
3554cc0e56eSJames Smart 				rc = -EACCES;
356a33c4f7bSJames Smart 			}
357a33c4f7bSJames Smart 		} else {
358a33c4f7bSJames Smart 			rsp_size = rsp->un.genreq64.bdl.bdeSize;
35901e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
360a33c4f7bSJames Smart 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
361a33c4f7bSJames Smart 						   rsp_size, 0);
362a33c4f7bSJames Smart 		}
363a33c4f7bSJames Smart 	}
3644cc0e56eSJames Smart 
365a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
366a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
3674cc0e56eSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
368a33c4f7bSJames Smart 	kfree(bmp);
3694cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
3704cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
3714cc0e56eSJames Smart 	kfree(dd_data);
372a33c4f7bSJames Smart 
373a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
374a33c4f7bSJames Smart 
375a33c4f7bSJames Smart 	if (job) {
37601e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
37706548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
3781abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
379a33c4f7bSJames Smart 	}
3804cc0e56eSJames Smart 	return;
3814cc0e56eSJames Smart }
3824cc0e56eSJames Smart 
3834cc0e56eSJames Smart /**
3844cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
385f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
3863b5dd52aSJames Smart  **/
387f1c3b0fcSJames Smart static int
38875cc8cfcSJohannes Thumshirn lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
389f1c3b0fcSJames Smart {
390cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
391f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
3921d69b122SJohannes Thumshirn 	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
393f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
39401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
395f1c3b0fcSJames Smart 	struct ulp_bde64 *bpl = NULL;
396f1c3b0fcSJames Smart 	uint32_t timeout;
397f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq = NULL;
398f1c3b0fcSJames Smart 	IOCB_t *cmd;
399a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
400f1c3b0fcSJames Smart 	int request_nseg;
401f1c3b0fcSJames Smart 	int reply_nseg;
4024cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
403b5a9b2dfSJames Smart 	unsigned long flags;
4044cc0e56eSJames Smart 	uint32_t creg_val;
405f1c3b0fcSJames Smart 	int rc = 0;
406d439d286SJames Smart 	int iocb_stat;
407f1c3b0fcSJames Smart 
408f1c3b0fcSJames Smart 	/* in case no data is transferred */
40901e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
410f1c3b0fcSJames Smart 
4114cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
4124cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4134cc0e56eSJames Smart 	if (!dd_data) {
4144cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4154cc0e56eSJames Smart 				"2733 Failed allocation of dd_data\n");
4164cc0e56eSJames Smart 		rc = -ENOMEM;
4174cc0e56eSJames Smart 		goto no_dd_data;
4184cc0e56eSJames Smart 	}
4194cc0e56eSJames Smart 
420f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
4214cc0e56eSJames Smart 		rc = -ENODEV;
4224cc0e56eSJames Smart 		goto no_ndlp;
4234cc0e56eSJames Smart 	}
4244cc0e56eSJames Smart 
425f1c3b0fcSJames Smart 	if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
426f1c3b0fcSJames Smart 		rc = -ENODEV;
427a33c4f7bSJames Smart 		goto free_ndlp;
428f1c3b0fcSJames Smart 	}
429f1c3b0fcSJames Smart 
430f1c3b0fcSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
431f1c3b0fcSJames Smart 	if (!cmdiocbq) {
432f1c3b0fcSJames Smart 		rc = -ENOMEM;
433a33c4f7bSJames Smart 		goto free_ndlp;
434f1c3b0fcSJames Smart 	}
435f1c3b0fcSJames Smart 
4364cc0e56eSJames Smart 	cmd = &cmdiocbq->iocb;
437a33c4f7bSJames Smart 
438a33c4f7bSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
439a33c4f7bSJames Smart 	if (!bmp) {
440f1c3b0fcSJames Smart 		rc = -ENOMEM;
441be858b65SJames Smart 		goto free_cmdiocbq;
442f1c3b0fcSJames Smart 	}
443a33c4f7bSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
444a33c4f7bSJames Smart 	if (!bmp->virt) {
445a33c4f7bSJames Smart 		rc = -ENOMEM;
446a33c4f7bSJames Smart 		goto free_bmp;
447f1c3b0fcSJames Smart 	}
448f1c3b0fcSJames Smart 
449a33c4f7bSJames Smart 	INIT_LIST_HEAD(&bmp->list);
450a33c4f7bSJames Smart 
451a33c4f7bSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
452a33c4f7bSJames Smart 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
453a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
454a33c4f7bSJames Smart 				     1, bpl, &request_nseg);
455a33c4f7bSJames Smart 	if (!cmp) {
456a33c4f7bSJames Smart 		rc = -ENOMEM;
457a33c4f7bSJames Smart 		goto free_bmp;
458a33c4f7bSJames Smart 	}
459a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
460a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
461a33c4f7bSJames Smart 
462a33c4f7bSJames Smart 	bpl += request_nseg;
463a33c4f7bSJames Smart 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
464a33c4f7bSJames Smart 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
465a33c4f7bSJames Smart 				     bpl, &reply_nseg);
466a33c4f7bSJames Smart 	if (!rmp) {
467a33c4f7bSJames Smart 		rc = -ENOMEM;
468a33c4f7bSJames Smart 		goto free_cmp;
469f1c3b0fcSJames Smart 	}
470f1c3b0fcSJames Smart 
471f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
472f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
473f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
474f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
475f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
476f1c3b0fcSJames Smart 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
477f1c3b0fcSJames Smart 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
478f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
479f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
4806a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
4816a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
482f1c3b0fcSJames Smart 	cmd->ulpBdeCount = 1;
483f1c3b0fcSJames Smart 	cmd->ulpLe = 1;
484f1c3b0fcSJames Smart 	cmd->ulpClass = CLASS3;
485f1c3b0fcSJames Smart 	cmd->ulpContext = ndlp->nlp_rpi;
4866d368e53SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
4876d368e53SJames Smart 		cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
488f1c3b0fcSJames Smart 	cmd->ulpOwner = OWN_CHIP;
489f1c3b0fcSJames Smart 	cmdiocbq->vport = phba->pport;
4904cc0e56eSJames Smart 	cmdiocbq->context3 = bmp;
491f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
492f1c3b0fcSJames Smart 	timeout = phba->fc_ratov * 2;
4934cc0e56eSJames Smart 	cmd->ulpTimeout = timeout;
494f1c3b0fcSJames Smart 
4954cc0e56eSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
496a33c4f7bSJames Smart 	cmdiocbq->context1 = dd_data;
497a33c4f7bSJames Smart 	cmdiocbq->context2 = cmp;
498a33c4f7bSJames Smart 	cmdiocbq->context3 = bmp;
499d5ce53b7SJames Smart 	cmdiocbq->context_un.ndlp = ndlp;
5004cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
501a33c4f7bSJames Smart 	dd_data->set_job = job;
5024cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
503a33c4f7bSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
504a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = rmp;
505a33c4f7bSJames Smart 	job->dd_data = dd_data;
506f1c3b0fcSJames Smart 
5074cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5089940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
5099940b97bSJames Smart 			rc = -EIO ;
510a33c4f7bSJames Smart 			goto free_rmp;
5119940b97bSJames Smart 		}
5124cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
5134cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
5144cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
5154cc0e56eSJames Smart 	}
5164cc0e56eSJames Smart 
517d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
518b5a9b2dfSJames Smart 
519b5a9b2dfSJames Smart 	if (iocb_stat == IOCB_SUCCESS) {
520b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
521b5a9b2dfSJames Smart 		/* make sure the I/O had not been completed yet */
522b5a9b2dfSJames Smart 		if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
523b5a9b2dfSJames Smart 			/* open up abort window to timeout handler */
5241b8d11abSJames Smart 			cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
525b5a9b2dfSJames Smart 		}
526b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
5274cc0e56eSJames Smart 		return 0; /* done for now */
528b5a9b2dfSJames Smart 	} else if (iocb_stat == IOCB_BUSY) {
529d439d286SJames Smart 		rc = -EAGAIN;
530b5a9b2dfSJames Smart 	} else {
531d439d286SJames Smart 		rc = -EIO;
532b5a9b2dfSJames Smart 	}
5332a9bf3d0SJames Smart 
5344cc0e56eSJames Smart 	/* iocb failed so cleanup */
535b5a9b2dfSJames Smart 	job->dd_data = NULL;
536f1c3b0fcSJames Smart 
537a33c4f7bSJames Smart free_rmp:
538a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
539a33c4f7bSJames Smart free_cmp:
540a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
541a33c4f7bSJames Smart free_bmp:
542a33c4f7bSJames Smart 	if (bmp->virt)
543f1c3b0fcSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
544a33c4f7bSJames Smart 	kfree(bmp);
545f1c3b0fcSJames Smart free_cmdiocbq:
546f1c3b0fcSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5474cc0e56eSJames Smart free_ndlp:
548f1c3b0fcSJames Smart 	lpfc_nlp_put(ndlp);
5494cc0e56eSJames Smart no_ndlp:
5504cc0e56eSJames Smart 	kfree(dd_data);
5514cc0e56eSJames Smart no_dd_data:
552f1c3b0fcSJames Smart 	/* make error code available to userspace */
55301e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
5544cc0e56eSJames Smart 	job->dd_data = NULL;
5554cc0e56eSJames Smart 	return rc;
5564cc0e56eSJames Smart }
5574cc0e56eSJames Smart 
5584cc0e56eSJames Smart /**
5594cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
5604cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
5614cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
5624cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
5634cc0e56eSJames Smart  *
5644cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
5654cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp function. This function is called by the
5664cc0e56eSJames Smart  * ring event handler function without any lock held. This function
5674cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
5684cc0e56eSJames Smart  * context. This function also can be called from other thread which
5694cc0e56eSJames Smart  * cleans up the SLI layer objects.
5703b5dd52aSJames Smart  * This function copies the contents of the response iocb to the
5714cc0e56eSJames Smart  * response iocb memory object provided by the caller of
5724cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5734cc0e56eSJames Smart  * sleeps for the iocb completion.
5744cc0e56eSJames Smart  **/
5754cc0e56eSJames Smart static void
5764cc0e56eSJames Smart lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
5774cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
5784cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
5794cc0e56eSJames Smart {
5804cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
58175cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
58201e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
5834cc0e56eSJames Smart 	IOCB_t *rsp;
5844cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
585a33c4f7bSJames Smart 	struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
5864cc0e56eSJames Smart 	struct fc_bsg_ctels_reply *els_reply;
5874cc0e56eSJames Smart 	uint8_t *rjt_data;
5884cc0e56eSJames Smart 	unsigned long flags;
589a33c4f7bSJames Smart 	unsigned int rsp_size;
5904cc0e56eSJames Smart 	int rc = 0;
5914cc0e56eSJames Smart 
5924cc0e56eSJames Smart 	dd_data = cmdiocbq->context1;
5934cc0e56eSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
594a33c4f7bSJames Smart 	cmdiocbq->context1 = ndlp;
5954cc0e56eSJames Smart 
596a33c4f7bSJames Smart 	/* Determine if job has been aborted */
597a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
598a33c4f7bSJames Smart 	job = dd_data->set_job;
599a33c4f7bSJames Smart 	if (job) {
60001e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
601a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
602a33c4f7bSJames Smart 		job->dd_data = NULL;
603a33c4f7bSJames Smart 	}
604a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
6054cc0e56eSJames Smart 
606b5a9b2dfSJames Smart 	/* Close the timeout handler abort window */
607b5a9b2dfSJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
6081b8d11abSJames Smart 	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
609b5a9b2dfSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
610b5a9b2dfSJames Smart 
611a33c4f7bSJames Smart 	rsp = &rspiocbq->iocb;
612a33c4f7bSJames Smart 	pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
613a33c4f7bSJames Smart 	prsp = (struct lpfc_dmabuf *)pcmd->list.next;
614a33c4f7bSJames Smart 
615a33c4f7bSJames Smart 	/* Copy the completed job data or determine the job status if job is
616a33c4f7bSJames Smart 	 * still active
617a33c4f7bSJames Smart 	 */
618a33c4f7bSJames Smart 
619a33c4f7bSJames Smart 	if (job) {
620a33c4f7bSJames Smart 		if (rsp->ulpStatus == IOSTAT_SUCCESS) {
621a33c4f7bSJames Smart 			rsp_size = rsp->un.elsreq64.bdl.bdeSize;
62201e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
623a33c4f7bSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
624a33c4f7bSJames Smart 						    job->reply_payload.sg_cnt,
625a33c4f7bSJames Smart 						    prsp->virt,
626a33c4f7bSJames Smart 						    rsp_size);
627a33c4f7bSJames Smart 		} else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
62801e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
6294cc0e56eSJames Smart 				sizeof(struct fc_bsg_ctels_reply);
6304cc0e56eSJames Smart 			/* LS_RJT data returned in word 4 */
6314cc0e56eSJames Smart 			rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
63201e0e15cSJohannes Thumshirn 			els_reply = &bsg_reply->reply_data.ctels_reply;
6334cc0e56eSJames Smart 			els_reply->status = FC_CTELS_STATUS_REJECT;
6344cc0e56eSJames Smart 			els_reply->rjt_data.action = rjt_data[3];
6354cc0e56eSJames Smart 			els_reply->rjt_data.reason_code = rjt_data[2];
6364cc0e56eSJames Smart 			els_reply->rjt_data.reason_explanation = rjt_data[1];
6374cc0e56eSJames Smart 			els_reply->rjt_data.vendor_unique = rjt_data[0];
638a33c4f7bSJames Smart 		} else {
6394cc0e56eSJames Smart 			rc = -EIO;
640a33c4f7bSJames Smart 		}
641a33c4f7bSJames Smart 	}
6424cc0e56eSJames Smart 
6434cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
644a33c4f7bSJames Smart 	lpfc_els_free_iocb(phba, cmdiocbq);
6454cc0e56eSJames Smart 	kfree(dd_data);
646a33c4f7bSJames Smart 
647a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
648a33c4f7bSJames Smart 
649a33c4f7bSJames Smart 	if (job) {
65001e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
65106548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
6521abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
653a33c4f7bSJames Smart 	}
6544cc0e56eSJames Smart 	return;
655f1c3b0fcSJames Smart }
656f1c3b0fcSJames Smart 
657f1c3b0fcSJames Smart /**
658f1c3b0fcSJames Smart  * lpfc_bsg_rport_els - send an ELS command from a bsg request
659f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
6603b5dd52aSJames Smart  **/
661f1c3b0fcSJames Smart static int
66275cc8cfcSJohannes Thumshirn lpfc_bsg_rport_els(struct bsg_job *job)
663f1c3b0fcSJames Smart {
664cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
665f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
6661d69b122SJohannes Thumshirn 	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
667f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
66801e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
66901e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
670f1c3b0fcSJames Smart 	uint32_t elscmd;
671f1c3b0fcSJames Smart 	uint32_t cmdsize;
672f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq;
673f1c3b0fcSJames Smart 	uint16_t rpi = 0;
6744cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
675b5a9b2dfSJames Smart 	unsigned long flags;
6764cc0e56eSJames Smart 	uint32_t creg_val;
677f1c3b0fcSJames Smart 	int rc = 0;
678f1c3b0fcSJames Smart 
679f1c3b0fcSJames Smart 	/* in case no data is transferred */
68001e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
681f1c3b0fcSJames Smart 
682a33c4f7bSJames Smart 	/* verify the els command is not greater than the
683a33c4f7bSJames Smart 	 * maximum ELS transfer size.
684a33c4f7bSJames Smart 	 */
685a33c4f7bSJames Smart 
686a33c4f7bSJames Smart 	if (job->request_payload.payload_len > FCELSSIZE) {
687a33c4f7bSJames Smart 		rc = -EINVAL;
688a33c4f7bSJames Smart 		goto no_dd_data;
689a33c4f7bSJames Smart 	}
690a33c4f7bSJames Smart 
6914cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
6924cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
6934cc0e56eSJames Smart 	if (!dd_data) {
6944cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
6954cc0e56eSJames Smart 				"2735 Failed allocation of dd_data\n");
6964cc0e56eSJames Smart 		rc = -ENOMEM;
6974cc0e56eSJames Smart 		goto no_dd_data;
6984cc0e56eSJames Smart 	}
6994cc0e56eSJames Smart 
70001e0e15cSJohannes Thumshirn 	elscmd = bsg_request->rqst_data.r_els.els_code;
701a33c4f7bSJames Smart 	cmdsize = job->request_payload.payload_len;
702a33c4f7bSJames Smart 
703f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
704f1c3b0fcSJames Smart 		rc = -ENODEV;
7054cc0e56eSJames Smart 		goto free_dd_data;
706f1c3b0fcSJames Smart 	}
707f1c3b0fcSJames Smart 
708a33c4f7bSJames Smart 	/* We will use the allocated dma buffers by prep els iocb for command
709a33c4f7bSJames Smart 	 * and response to ensure if the job times out and the request is freed,
710a33c4f7bSJames Smart 	 * we won't be dma into memory that is no longer allocated to for the
711a33c4f7bSJames Smart 	 * request.
712a33c4f7bSJames Smart 	 */
713f1c3b0fcSJames Smart 
7144cc0e56eSJames Smart 	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
715f1c3b0fcSJames Smart 				      ndlp->nlp_DID, elscmd);
716f1c3b0fcSJames Smart 	if (!cmdiocbq) {
7174cc0e56eSJames Smart 		rc = -EIO;
718a33c4f7bSJames Smart 		goto release_ndlp;
719f1c3b0fcSJames Smart 	}
720f1c3b0fcSJames Smart 
721a33c4f7bSJames Smart 	rpi = ndlp->nlp_rpi;
722f1c3b0fcSJames Smart 
723a33c4f7bSJames Smart 	/* Transfer the request payload to allocated command dma buffer */
724f1c3b0fcSJames Smart 
725a33c4f7bSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
726a33c4f7bSJames Smart 			  job->request_payload.sg_cnt,
727a33c4f7bSJames Smart 			  ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
728a33c4f7bSJames Smart 			  cmdsize);
729f1c3b0fcSJames Smart 
7303ef6d24cSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
7313ef6d24cSJames Smart 		cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
7323ef6d24cSJames Smart 	else
733f1c3b0fcSJames Smart 		cmdiocbq->iocb.ulpContext = rpi;
734f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
7354cc0e56eSJames Smart 	cmdiocbq->context1 = dd_data;
73693d1379eSJames Smart 	cmdiocbq->context_un.ndlp = ndlp;
737a33c4f7bSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
7384cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
739a33c4f7bSJames Smart 	dd_data->set_job = job;
7404cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
7414cc0e56eSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
742a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = NULL;
743a33c4f7bSJames Smart 	job->dd_data = dd_data;
744f1c3b0fcSJames Smart 
7454cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7469940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
7479940b97bSJames Smart 			rc = -EIO;
7489940b97bSJames Smart 			goto linkdown_err;
7499940b97bSJames Smart 		}
7504cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7514cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
7524cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
7534cc0e56eSJames Smart 	}
754a33c4f7bSJames Smart 
7554cc0e56eSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
756a33c4f7bSJames Smart 
757b5a9b2dfSJames Smart 	if (rc == IOCB_SUCCESS) {
758b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
759b5a9b2dfSJames Smart 		/* make sure the I/O had not been completed/released */
760b5a9b2dfSJames Smart 		if (cmdiocbq->iocb_flag & LPFC_IO_LIBDFC) {
761b5a9b2dfSJames Smart 			/* open up abort window to timeout handler */
7621b8d11abSJames Smart 			cmdiocbq->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
763b5a9b2dfSJames Smart 		}
764b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
7654cc0e56eSJames Smart 		return 0; /* done for now */
766b5a9b2dfSJames Smart 	} else if (rc == IOCB_BUSY) {
767d439d286SJames Smart 		rc = -EAGAIN;
768b5a9b2dfSJames Smart 	} else {
769d439d286SJames Smart 		rc = -EIO;
770b5a9b2dfSJames Smart 	}
771b5a9b2dfSJames Smart 
772b5a9b2dfSJames Smart 	/* iocb failed so cleanup */
773b5a9b2dfSJames Smart 	job->dd_data = NULL;
7744cc0e56eSJames Smart 
7759940b97bSJames Smart linkdown_err:
776a33c4f7bSJames Smart 	cmdiocbq->context1 = ndlp;
777a33c4f7bSJames Smart 	lpfc_els_free_iocb(phba, cmdiocbq);
778f1c3b0fcSJames Smart 
779a33c4f7bSJames Smart release_ndlp:
780a33c4f7bSJames Smart 	lpfc_nlp_put(ndlp);
781f1c3b0fcSJames Smart 
7824cc0e56eSJames Smart free_dd_data:
7834cc0e56eSJames Smart 	kfree(dd_data);
7844cc0e56eSJames Smart 
7854cc0e56eSJames Smart no_dd_data:
786f1c3b0fcSJames Smart 	/* make error code available to userspace */
78701e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
7884cc0e56eSJames Smart 	job->dd_data = NULL;
7894cc0e56eSJames Smart 	return rc;
790f1c3b0fcSJames Smart }
791f1c3b0fcSJames Smart 
7923b5dd52aSJames Smart /**
7933b5dd52aSJames Smart  * lpfc_bsg_event_free - frees an allocated event structure
7943b5dd52aSJames Smart  * @kref: Pointer to a kref.
7953b5dd52aSJames Smart  *
7963b5dd52aSJames Smart  * Called from kref_put. Back cast the kref into an event structure address.
7973b5dd52aSJames Smart  * Free any events to get, delete associated nodes, free any events to see,
7983b5dd52aSJames Smart  * free any data then free the event itself.
7993b5dd52aSJames Smart  **/
800f1c3b0fcSJames Smart static void
8014cc0e56eSJames Smart lpfc_bsg_event_free(struct kref *kref)
802f1c3b0fcSJames Smart {
8034cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
8044cc0e56eSJames Smart 						  kref);
805f1c3b0fcSJames Smart 	struct event_data *ed;
806f1c3b0fcSJames Smart 
807f1c3b0fcSJames Smart 	list_del(&evt->node);
808f1c3b0fcSJames Smart 
809f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_get)) {
810f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
811f1c3b0fcSJames Smart 		list_del(&ed->node);
812f1c3b0fcSJames Smart 		kfree(ed->data);
813f1c3b0fcSJames Smart 		kfree(ed);
814f1c3b0fcSJames Smart 	}
815f1c3b0fcSJames Smart 
816f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_see)) {
817f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
818f1c3b0fcSJames Smart 		list_del(&ed->node);
819f1c3b0fcSJames Smart 		kfree(ed->data);
820f1c3b0fcSJames Smart 		kfree(ed);
821f1c3b0fcSJames Smart 	}
822f1c3b0fcSJames Smart 
823a33c4f7bSJames Smart 	kfree(evt->dd_data);
824f1c3b0fcSJames Smart 	kfree(evt);
825f1c3b0fcSJames Smart }
826f1c3b0fcSJames Smart 
8273b5dd52aSJames Smart /**
8283b5dd52aSJames Smart  * lpfc_bsg_event_ref - increments the kref for an event
8293b5dd52aSJames Smart  * @evt: Pointer to an event structure.
8303b5dd52aSJames Smart  **/
831f1c3b0fcSJames Smart static inline void
8324cc0e56eSJames Smart lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
833f1c3b0fcSJames Smart {
8344cc0e56eSJames Smart 	kref_get(&evt->kref);
835f1c3b0fcSJames Smart }
836f1c3b0fcSJames Smart 
8373b5dd52aSJames Smart /**
8383b5dd52aSJames Smart  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
8393b5dd52aSJames Smart  * @evt: Pointer to an event structure.
8403b5dd52aSJames Smart  **/
841f1c3b0fcSJames Smart static inline void
8424cc0e56eSJames Smart lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
843f1c3b0fcSJames Smart {
8444cc0e56eSJames Smart 	kref_put(&evt->kref, lpfc_bsg_event_free);
845f1c3b0fcSJames Smart }
846f1c3b0fcSJames Smart 
8473b5dd52aSJames Smart /**
8483b5dd52aSJames Smart  * lpfc_bsg_event_new - allocate and initialize a event structure
8493b5dd52aSJames Smart  * @ev_mask: Mask of events.
8503b5dd52aSJames Smart  * @ev_reg_id: Event reg id.
8513b5dd52aSJames Smart  * @ev_req_id: Event request id.
8523b5dd52aSJames Smart  **/
8534cc0e56eSJames Smart static struct lpfc_bsg_event *
8544cc0e56eSJames Smart lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
8554cc0e56eSJames Smart {
8564cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
857f1c3b0fcSJames Smart 
8584cc0e56eSJames Smart 	if (!evt)
8594cc0e56eSJames Smart 		return NULL;
8604cc0e56eSJames Smart 
8614cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_get);
8624cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_see);
8634cc0e56eSJames Smart 	evt->type_mask = ev_mask;
8644cc0e56eSJames Smart 	evt->req_id = ev_req_id;
8654cc0e56eSJames Smart 	evt->reg_id = ev_reg_id;
8664cc0e56eSJames Smart 	evt->wait_time_stamp = jiffies;
867a33c4f7bSJames Smart 	evt->dd_data = NULL;
8684cc0e56eSJames Smart 	init_waitqueue_head(&evt->wq);
8694cc0e56eSJames Smart 	kref_init(&evt->kref);
8704cc0e56eSJames Smart 	return evt;
8714cc0e56eSJames Smart }
8724cc0e56eSJames Smart 
8733b5dd52aSJames Smart /**
8743b5dd52aSJames Smart  * diag_cmd_data_free - Frees an lpfc dma buffer extension
8753b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
8763b5dd52aSJames Smart  * @mlist: Pointer to an lpfc dma buffer extension.
8773b5dd52aSJames Smart  **/
8784cc0e56eSJames Smart static int
8793b5dd52aSJames Smart diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
8804cc0e56eSJames Smart {
8814cc0e56eSJames Smart 	struct lpfc_dmabufext *mlast;
8824cc0e56eSJames Smart 	struct pci_dev *pcidev;
8834cc0e56eSJames Smart 	struct list_head head, *curr, *next;
8844cc0e56eSJames Smart 
8854cc0e56eSJames Smart 	if ((!mlist) || (!lpfc_is_link_up(phba) &&
8864cc0e56eSJames Smart 		(phba->link_flag & LS_LOOPBACK_MODE))) {
8874cc0e56eSJames Smart 		return 0;
8884cc0e56eSJames Smart 	}
8894cc0e56eSJames Smart 
8904cc0e56eSJames Smart 	pcidev = phba->pcidev;
8914cc0e56eSJames Smart 	list_add_tail(&head, &mlist->dma.list);
8924cc0e56eSJames Smart 
8934cc0e56eSJames Smart 	list_for_each_safe(curr, next, &head) {
8944cc0e56eSJames Smart 		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
8954cc0e56eSJames Smart 		if (mlast->dma.virt)
8964cc0e56eSJames Smart 			dma_free_coherent(&pcidev->dev,
8974cc0e56eSJames Smart 					  mlast->size,
8984cc0e56eSJames Smart 					  mlast->dma.virt,
8994cc0e56eSJames Smart 					  mlast->dma.phys);
9004cc0e56eSJames Smart 		kfree(mlast);
9014cc0e56eSJames Smart 	}
9024cc0e56eSJames Smart 	return 0;
9034cc0e56eSJames Smart }
904f1c3b0fcSJames Smart 
905f1c3b0fcSJames Smart /**
906f1c3b0fcSJames Smart  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
907f1c3b0fcSJames Smart  * @phba:
908f1c3b0fcSJames Smart  * @pring:
909f1c3b0fcSJames Smart  * @piocbq:
910f1c3b0fcSJames Smart  *
911f1c3b0fcSJames Smart  * This function is called when an unsolicited CT command is received.  It
9124cc0e56eSJames Smart  * forwards the event to any processes registered to receive CT events.
9133b5dd52aSJames Smart  **/
9144fede78fSJames Smart int
915f1c3b0fcSJames Smart lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
916f1c3b0fcSJames Smart 			struct lpfc_iocbq *piocbq)
917f1c3b0fcSJames Smart {
918f1c3b0fcSJames Smart 	uint32_t evt_req_id = 0;
919f1c3b0fcSJames Smart 	uint32_t cmd;
920f1c3b0fcSJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
9214cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
922f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
923f1c3b0fcSJames Smart 	struct lpfc_iocbq *iocbq;
924f1c3b0fcSJames Smart 	size_t offset = 0;
925f1c3b0fcSJames Smart 	struct list_head head;
926f1c3b0fcSJames Smart 	struct ulp_bde64 *bde;
927f1c3b0fcSJames Smart 	dma_addr_t dma_addr;
928f1c3b0fcSJames Smart 	int i;
929f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
930f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
931f1c3b0fcSJames Smart 	struct lpfc_hbq_entry *hbqe;
932f1c3b0fcSJames Smart 	struct lpfc_sli_ct_request *ct_req;
93375cc8cfcSJohannes Thumshirn 	struct bsg_job *job = NULL;
93401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
935a33c4f7bSJames Smart 	struct bsg_job_data *dd_data = NULL;
9364fede78fSJames Smart 	unsigned long flags;
9374cc0e56eSJames Smart 	int size = 0;
938f1c3b0fcSJames Smart 
939f1c3b0fcSJames Smart 	INIT_LIST_HEAD(&head);
940f1c3b0fcSJames Smart 	list_add_tail(&head, &piocbq->list);
941f1c3b0fcSJames Smart 
942f1c3b0fcSJames Smart 	if (piocbq->iocb.ulpBdeCount == 0 ||
943f1c3b0fcSJames Smart 	    piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
944f1c3b0fcSJames Smart 		goto error_ct_unsol_exit;
945f1c3b0fcSJames Smart 
9464cc0e56eSJames Smart 	if (phba->link_state == LPFC_HBA_ERROR ||
9474cc0e56eSJames Smart 		(!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
9484cc0e56eSJames Smart 		goto error_ct_unsol_exit;
9494cc0e56eSJames Smart 
950f1c3b0fcSJames Smart 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
951f1c3b0fcSJames Smart 		dmabuf = bdeBuf1;
952f1c3b0fcSJames Smart 	else {
953f1c3b0fcSJames Smart 		dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
954f1c3b0fcSJames Smart 				    piocbq->iocb.un.cont64[0].addrLow);
955f1c3b0fcSJames Smart 		dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
956f1c3b0fcSJames Smart 	}
9574cc0e56eSJames Smart 	if (dmabuf == NULL)
9584cc0e56eSJames Smart 		goto error_ct_unsol_exit;
959f1c3b0fcSJames Smart 	ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
960f1c3b0fcSJames Smart 	evt_req_id = ct_req->FsType;
961f1c3b0fcSJames Smart 	cmd = ct_req->CommandResponse.bits.CmdRsp;
962f1c3b0fcSJames Smart 	if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
963f1c3b0fcSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
964f1c3b0fcSJames Smart 
9654fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
966f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
9674cc0e56eSJames Smart 		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
9684cc0e56eSJames Smart 			evt->req_id != evt_req_id)
969f1c3b0fcSJames Smart 			continue;
970f1c3b0fcSJames Smart 
9714cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
9724cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
973f1c3b0fcSJames Smart 		evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
9744cc0e56eSJames Smart 		if (evt_dat == NULL) {
9754cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
9764cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
977f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
978f1c3b0fcSJames Smart 					"2614 Memory allocation failed for "
979f1c3b0fcSJames Smart 					"CT event\n");
980f1c3b0fcSJames Smart 			break;
981f1c3b0fcSJames Smart 		}
982f1c3b0fcSJames Smart 
983f1c3b0fcSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
984f1c3b0fcSJames Smart 			/* take accumulated byte count from the last iocbq */
985f1c3b0fcSJames Smart 			iocbq = list_entry(head.prev, typeof(*iocbq), list);
986f1c3b0fcSJames Smart 			evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
987f1c3b0fcSJames Smart 		} else {
988f1c3b0fcSJames Smart 			list_for_each_entry(iocbq, &head, list) {
989f1c3b0fcSJames Smart 				for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
990f1c3b0fcSJames Smart 					evt_dat->len +=
991f1c3b0fcSJames Smart 					iocbq->iocb.un.cont64[i].tus.f.bdeSize;
992f1c3b0fcSJames Smart 			}
993f1c3b0fcSJames Smart 		}
994f1c3b0fcSJames Smart 
995f1c3b0fcSJames Smart 		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
9964cc0e56eSJames Smart 		if (evt_dat->data == NULL) {
997f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
998f1c3b0fcSJames Smart 					"2615 Memory allocation failed for "
999f1c3b0fcSJames Smart 					"CT event data, size %d\n",
1000f1c3b0fcSJames Smart 					evt_dat->len);
1001f1c3b0fcSJames Smart 			kfree(evt_dat);
10024fede78fSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
10034cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
10044fede78fSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1005f1c3b0fcSJames Smart 			goto error_ct_unsol_exit;
1006f1c3b0fcSJames Smart 		}
1007f1c3b0fcSJames Smart 
1008f1c3b0fcSJames Smart 		list_for_each_entry(iocbq, &head, list) {
10094cc0e56eSJames Smart 			size = 0;
1010f1c3b0fcSJames Smart 			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1011f1c3b0fcSJames Smart 				bdeBuf1 = iocbq->context2;
1012f1c3b0fcSJames Smart 				bdeBuf2 = iocbq->context3;
1013f1c3b0fcSJames Smart 			}
1014f1c3b0fcSJames Smart 			for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
1015f1c3b0fcSJames Smart 				if (phba->sli3_options &
1016f1c3b0fcSJames Smart 				    LPFC_SLI3_HBQ_ENABLED) {
1017f1c3b0fcSJames Smart 					if (i == 0) {
1018f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
1019f1c3b0fcSJames Smart 						  &iocbq->iocb.un.ulpWord[0];
1020f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
1021f1c3b0fcSJames Smart 						dmabuf = bdeBuf1;
1022f1c3b0fcSJames Smart 					} else if (i == 1) {
1023f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
1024f1c3b0fcSJames Smart 							&iocbq->iocb.unsli3.
1025f1c3b0fcSJames Smart 							sli3Words[4];
1026f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
1027f1c3b0fcSJames Smart 						dmabuf = bdeBuf2;
1028f1c3b0fcSJames Smart 					}
1029f1c3b0fcSJames Smart 					if ((offset + size) > evt_dat->len)
1030f1c3b0fcSJames Smart 						size = evt_dat->len - offset;
1031f1c3b0fcSJames Smart 				} else {
1032f1c3b0fcSJames Smart 					size = iocbq->iocb.un.cont64[i].
1033f1c3b0fcSJames Smart 						tus.f.bdeSize;
1034f1c3b0fcSJames Smart 					bde = &iocbq->iocb.un.cont64[i];
1035f1c3b0fcSJames Smart 					dma_addr = getPaddr(bde->addrHigh,
1036f1c3b0fcSJames Smart 							    bde->addrLow);
1037f1c3b0fcSJames Smart 					dmabuf = lpfc_sli_ringpostbuf_get(phba,
1038f1c3b0fcSJames Smart 							pring, dma_addr);
1039f1c3b0fcSJames Smart 				}
1040f1c3b0fcSJames Smart 				if (!dmabuf) {
1041f1c3b0fcSJames Smart 					lpfc_printf_log(phba, KERN_ERR,
1042f1c3b0fcSJames Smart 						LOG_LIBDFC, "2616 No dmabuf "
104332350664SJames Smart 						"found for iocbq x%px\n",
1044f1c3b0fcSJames Smart 						iocbq);
1045f1c3b0fcSJames Smart 					kfree(evt_dat->data);
1046f1c3b0fcSJames Smart 					kfree(evt_dat);
10474fede78fSJames Smart 					spin_lock_irqsave(&phba->ct_ev_lock,
10484fede78fSJames Smart 						flags);
10494cc0e56eSJames Smart 					lpfc_bsg_event_unref(evt);
10504fede78fSJames Smart 					spin_unlock_irqrestore(
10514fede78fSJames Smart 						&phba->ct_ev_lock, flags);
1052f1c3b0fcSJames Smart 					goto error_ct_unsol_exit;
1053f1c3b0fcSJames Smart 				}
1054f1c3b0fcSJames Smart 				memcpy((char *)(evt_dat->data) + offset,
1055f1c3b0fcSJames Smart 				       dmabuf->virt, size);
1056f1c3b0fcSJames Smart 				offset += size;
1057f1c3b0fcSJames Smart 				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1058f1c3b0fcSJames Smart 				    !(phba->sli3_options &
1059f1c3b0fcSJames Smart 				      LPFC_SLI3_HBQ_ENABLED)) {
1060f1c3b0fcSJames Smart 					lpfc_sli_ringpostbuf_put(phba, pring,
1061f1c3b0fcSJames Smart 								 dmabuf);
1062f1c3b0fcSJames Smart 				} else {
1063f1c3b0fcSJames Smart 					switch (cmd) {
10644cc0e56eSJames Smart 					case ELX_LOOPBACK_DATA:
10651b51197dSJames Smart 						if (phba->sli_rev <
10661b51197dSJames Smart 						    LPFC_SLI_REV4)
10673b5dd52aSJames Smart 							diag_cmd_data_free(phba,
10681b51197dSJames Smart 							(struct lpfc_dmabufext
10691b51197dSJames Smart 							 *)dmabuf);
10704cc0e56eSJames Smart 						break;
1071f1c3b0fcSJames Smart 					case ELX_LOOPBACK_XRI_SETUP:
10724cc0e56eSJames Smart 						if ((phba->sli_rev ==
10734cc0e56eSJames Smart 							LPFC_SLI_REV2) ||
10744cc0e56eSJames Smart 							(phba->sli3_options &
10754cc0e56eSJames Smart 							LPFC_SLI3_HBQ_ENABLED
10764cc0e56eSJames Smart 							)) {
10774cc0e56eSJames Smart 							lpfc_in_buf_free(phba,
10784cc0e56eSJames Smart 									dmabuf);
10794cc0e56eSJames Smart 						} else {
1080f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
1081f1c3b0fcSJames Smart 									 pring,
1082f1c3b0fcSJames Smart 									 1);
10834cc0e56eSJames Smart 						}
1084f1c3b0fcSJames Smart 						break;
1085f1c3b0fcSJames Smart 					default:
1086f1c3b0fcSJames Smart 						if (!(phba->sli3_options &
1087f1c3b0fcSJames Smart 						      LPFC_SLI3_HBQ_ENABLED))
1088f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
1089f1c3b0fcSJames Smart 									 pring,
1090f1c3b0fcSJames Smart 									 1);
1091f1c3b0fcSJames Smart 						break;
1092f1c3b0fcSJames Smart 					}
1093f1c3b0fcSJames Smart 				}
1094f1c3b0fcSJames Smart 			}
1095f1c3b0fcSJames Smart 		}
1096f1c3b0fcSJames Smart 
10974fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1098f1c3b0fcSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4) {
1099f1c3b0fcSJames Smart 			evt_dat->immed_dat = phba->ctx_idx;
11006dd9e31cSJames Smart 			phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1101589a52d6SJames Smart 			/* Provide warning for over-run of the ct_ctx array */
11026dd9e31cSJames Smart 			if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1103589a52d6SJames Smart 			    UNSOL_VALID)
1104589a52d6SJames Smart 				lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1105589a52d6SJames Smart 						"2717 CT context array entry "
1106589a52d6SJames Smart 						"[%d] over-run: oxid:x%x, "
1107589a52d6SJames Smart 						"sid:x%x\n", phba->ctx_idx,
1108589a52d6SJames Smart 						phba->ct_ctx[
1109589a52d6SJames Smart 						    evt_dat->immed_dat].oxid,
1110589a52d6SJames Smart 						phba->ct_ctx[
1111589a52d6SJames Smart 						    evt_dat->immed_dat].SID);
11127851fe2cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].rxid =
1113f1c3b0fcSJames Smart 				piocbq->iocb.ulpContext;
11147851fe2cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].oxid =
11157851fe2cSJames Smart 				piocbq->iocb.unsli3.rcvsli3.ox_id;
1116f1c3b0fcSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].SID =
1117f1c3b0fcSJames Smart 				piocbq->iocb.un.rcvels.remoteID;
11186dd9e31cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1119f1c3b0fcSJames Smart 		} else
1120f1c3b0fcSJames Smart 			evt_dat->immed_dat = piocbq->iocb.ulpContext;
1121f1c3b0fcSJames Smart 
1122f1c3b0fcSJames Smart 		evt_dat->type = FC_REG_CT_EVENT;
1123f1c3b0fcSJames Smart 		list_add(&evt_dat->node, &evt->events_to_see);
11244cc0e56eSJames Smart 		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1125f1c3b0fcSJames Smart 			wake_up_interruptible(&evt->wq);
11264cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
1127f1c3b0fcSJames Smart 			break;
1128f1c3b0fcSJames Smart 		}
11294cc0e56eSJames Smart 
11304cc0e56eSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
11314cc0e56eSJames Smart 
1132a33c4f7bSJames Smart 		dd_data = (struct bsg_job_data *)evt->dd_data;
1133a33c4f7bSJames Smart 		job = dd_data->set_job;
1134a33c4f7bSJames Smart 		dd_data->set_job = NULL;
1135a33c4f7bSJames Smart 		lpfc_bsg_event_unref(evt);
11364cc0e56eSJames Smart 		if (job) {
113701e0e15cSJohannes Thumshirn 			bsg_reply = job->reply;
113801e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len = size;
11394cc0e56eSJames Smart 			/* make error code available to userspace */
114001e0e15cSJohannes Thumshirn 			bsg_reply->result = 0;
11414cc0e56eSJames Smart 			job->dd_data = NULL;
11424cc0e56eSJames Smart 			/* complete the job back to userspace */
11434cc0e56eSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
114406548160SJohannes Thumshirn 			bsg_job_done(job, bsg_reply->result,
11451abaede7SJohannes Thumshirn 				       bsg_reply->reply_payload_rcv_len);
11464cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
11474cc0e56eSJames Smart 		}
11484cc0e56eSJames Smart 	}
11494fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1150f1c3b0fcSJames Smart 
1151f1c3b0fcSJames Smart error_ct_unsol_exit:
1152f1c3b0fcSJames Smart 	if (!list_empty(&head))
1153f1c3b0fcSJames Smart 		list_del(&head);
11541b51197dSJames Smart 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
11551b51197dSJames Smart 	    (evt_req_id == SLI_CT_ELX_LOOPBACK))
11564cc0e56eSJames Smart 		return 0;
11574fede78fSJames Smart 	return 1;
1158f1c3b0fcSJames Smart }
1159f1c3b0fcSJames Smart 
1160f1c3b0fcSJames Smart /**
11616dd9e31cSJames Smart  * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
11626dd9e31cSJames Smart  * @phba: Pointer to HBA context object.
11636dd9e31cSJames Smart  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11646dd9e31cSJames Smart  *
11656dd9e31cSJames Smart  * This function handles abort to the CT command toward management plane
11666dd9e31cSJames Smart  * for SLI4 port.
11676dd9e31cSJames Smart  *
11686dd9e31cSJames Smart  * If the pending context of a CT command to management plane present, clears
11696dd9e31cSJames Smart  * such context and returns 1 for handled; otherwise, it returns 0 indicating
11706dd9e31cSJames Smart  * no context exists.
11716dd9e31cSJames Smart  **/
11726dd9e31cSJames Smart int
11736dd9e31cSJames Smart lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
11746dd9e31cSJames Smart {
11756dd9e31cSJames Smart 	struct fc_frame_header fc_hdr;
11766dd9e31cSJames Smart 	struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
11776dd9e31cSJames Smart 	int ctx_idx, handled = 0;
11786dd9e31cSJames Smart 	uint16_t oxid, rxid;
11796dd9e31cSJames Smart 	uint32_t sid;
11806dd9e31cSJames Smart 
11816dd9e31cSJames Smart 	memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11826dd9e31cSJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
11836dd9e31cSJames Smart 	oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
11846dd9e31cSJames Smart 	rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
11856dd9e31cSJames Smart 
11866dd9e31cSJames Smart 	for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
11876dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
11886dd9e31cSJames Smart 			continue;
11896dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].rxid != rxid)
11906dd9e31cSJames Smart 			continue;
11916dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].oxid != oxid)
11926dd9e31cSJames Smart 			continue;
11936dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].SID != sid)
11946dd9e31cSJames Smart 			continue;
11956dd9e31cSJames Smart 		phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
11966dd9e31cSJames Smart 		handled = 1;
11976dd9e31cSJames Smart 	}
11986dd9e31cSJames Smart 	return handled;
11996dd9e31cSJames Smart }
12006dd9e31cSJames Smart 
12016dd9e31cSJames Smart /**
12024cc0e56eSJames Smart  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1203f1c3b0fcSJames Smart  * @job: SET_EVENT fc_bsg_job
12043b5dd52aSJames Smart  **/
1205f1c3b0fcSJames Smart static int
120675cc8cfcSJohannes Thumshirn lpfc_bsg_hba_set_event(struct bsg_job *job)
1207f1c3b0fcSJames Smart {
1208cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1209f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
121001e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
1211f1c3b0fcSJames Smart 	struct set_ct_event *event_req;
12124cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
1213f1c3b0fcSJames Smart 	int rc = 0;
12144cc0e56eSJames Smart 	struct bsg_job_data *dd_data = NULL;
12154cc0e56eSJames Smart 	uint32_t ev_mask;
12164cc0e56eSJames Smart 	unsigned long flags;
1217f1c3b0fcSJames Smart 
1218f1c3b0fcSJames Smart 	if (job->request_len <
1219f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1220f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1221f1c3b0fcSJames Smart 				"2612 Received SET_CT_EVENT below minimum "
1222f1c3b0fcSJames Smart 				"size\n");
12234cc0e56eSJames Smart 		rc = -EINVAL;
12244cc0e56eSJames Smart 		goto job_error;
12254cc0e56eSJames Smart 	}
12264cc0e56eSJames Smart 
1227f1c3b0fcSJames Smart 	event_req = (struct set_ct_event *)
122801e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
12294cc0e56eSJames Smart 	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
12304cc0e56eSJames Smart 				FC_REG_EVENT_MASK);
12314fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1232f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1233f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
12344cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1235f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1236a33c4f7bSJames Smart 			dd_data = (struct bsg_job_data *)evt->dd_data;
1237f1c3b0fcSJames Smart 			break;
1238f1c3b0fcSJames Smart 		}
1239f1c3b0fcSJames Smart 	}
12404fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1241f1c3b0fcSJames Smart 
1242f1c3b0fcSJames Smart 	if (&evt->node == &phba->ct_ev_waiters) {
1243f1c3b0fcSJames Smart 		/* no event waiting struct yet - first call */
1244a33c4f7bSJames Smart 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1245a33c4f7bSJames Smart 		if (dd_data == NULL) {
1246a33c4f7bSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1247a33c4f7bSJames Smart 					"2734 Failed allocation of dd_data\n");
1248a33c4f7bSJames Smart 			rc = -ENOMEM;
1249a33c4f7bSJames Smart 			goto job_error;
1250a33c4f7bSJames Smart 		}
12514cc0e56eSJames Smart 		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1252f1c3b0fcSJames Smart 					event_req->ev_req_id);
1253f1c3b0fcSJames Smart 		if (!evt) {
1254f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1255f1c3b0fcSJames Smart 					"2617 Failed allocation of event "
1256f1c3b0fcSJames Smart 					"waiter\n");
12574cc0e56eSJames Smart 			rc = -ENOMEM;
12584cc0e56eSJames Smart 			goto job_error;
1259f1c3b0fcSJames Smart 		}
1260a33c4f7bSJames Smart 		dd_data->type = TYPE_EVT;
1261a33c4f7bSJames Smart 		dd_data->set_job = NULL;
1262a33c4f7bSJames Smart 		dd_data->context_un.evt = evt;
1263a33c4f7bSJames Smart 		evt->dd_data = (void *)dd_data;
12644fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1265f1c3b0fcSJames Smart 		list_add(&evt->node, &phba->ct_ev_waiters);
12664cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
1267f1c3b0fcSJames Smart 		evt->wait_time_stamp = jiffies;
12684cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
12694cc0e56eSJames Smart 	}
1270f1c3b0fcSJames Smart 
12714fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
12724cc0e56eSJames Smart 	evt->waiting = 1;
1273a33c4f7bSJames Smart 	dd_data->set_job = job; /* for unsolicited command */
12744cc0e56eSJames Smart 	job->dd_data = dd_data; /* for fc transport timeout callback*/
12754fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
12764cc0e56eSJames Smart 	return 0; /* call job done later */
1277f1c3b0fcSJames Smart 
12784cc0e56eSJames Smart job_error:
12794cc0e56eSJames Smart 	kfree(dd_data);
12804cc0e56eSJames Smart 	job->dd_data = NULL;
12814cc0e56eSJames Smart 	return rc;
1282f1c3b0fcSJames Smart }
1283f1c3b0fcSJames Smart 
1284f1c3b0fcSJames Smart /**
12854cc0e56eSJames Smart  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1286f1c3b0fcSJames Smart  * @job: GET_EVENT fc_bsg_job
12873b5dd52aSJames Smart  **/
1288f1c3b0fcSJames Smart static int
128975cc8cfcSJohannes Thumshirn lpfc_bsg_hba_get_event(struct bsg_job *job)
1290f1c3b0fcSJames Smart {
1291cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1292f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
129301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
129401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
1295f1c3b0fcSJames Smart 	struct get_ct_event *event_req;
1296f1c3b0fcSJames Smart 	struct get_ct_event_reply *event_reply;
12979a803a74SJames Smart 	struct lpfc_bsg_event *evt, *evt_next;
1298f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
12994fede78fSJames Smart 	unsigned long flags;
13004cc0e56eSJames Smart 	uint32_t rc = 0;
1301f1c3b0fcSJames Smart 
1302f1c3b0fcSJames Smart 	if (job->request_len <
1303f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1304f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1305f1c3b0fcSJames Smart 				"2613 Received GET_CT_EVENT request below "
1306f1c3b0fcSJames Smart 				"minimum size\n");
13074cc0e56eSJames Smart 		rc = -EINVAL;
13084cc0e56eSJames Smart 		goto job_error;
1309f1c3b0fcSJames Smart 	}
1310f1c3b0fcSJames Smart 
1311f1c3b0fcSJames Smart 	event_req = (struct get_ct_event *)
131201e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1313f1c3b0fcSJames Smart 
1314f1c3b0fcSJames Smart 	event_reply = (struct get_ct_event_reply *)
131501e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
13164fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
13179a803a74SJames Smart 	list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1318f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
1319f1c3b0fcSJames Smart 			if (list_empty(&evt->events_to_get))
1320f1c3b0fcSJames Smart 				break;
13214cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1322f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1323f1c3b0fcSJames Smart 			evt_dat = list_entry(evt->events_to_get.prev,
1324f1c3b0fcSJames Smart 					     struct event_data, node);
1325f1c3b0fcSJames Smart 			list_del(&evt_dat->node);
1326f1c3b0fcSJames Smart 			break;
1327f1c3b0fcSJames Smart 		}
1328f1c3b0fcSJames Smart 	}
13294fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1330f1c3b0fcSJames Smart 
13314cc0e56eSJames Smart 	/* The app may continue to ask for event data until it gets
13324cc0e56eSJames Smart 	 * an error indicating that there isn't anymore
13334cc0e56eSJames Smart 	 */
13344cc0e56eSJames Smart 	if (evt_dat == NULL) {
133501e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
1336f1c3b0fcSJames Smart 		rc = -ENOENT;
13374cc0e56eSJames Smart 		goto job_error;
1338f1c3b0fcSJames Smart 	}
1339f1c3b0fcSJames Smart 
13404cc0e56eSJames Smart 	if (evt_dat->len > job->request_payload.payload_len) {
13414cc0e56eSJames Smart 		evt_dat->len = job->request_payload.payload_len;
1342f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1343f1c3b0fcSJames Smart 				"2618 Truncated event data at %d "
1344f1c3b0fcSJames Smart 				"bytes\n",
13454cc0e56eSJames Smart 				job->request_payload.payload_len);
1346f1c3b0fcSJames Smart 	}
1347f1c3b0fcSJames Smart 
13484cc0e56eSJames Smart 	event_reply->type = evt_dat->type;
1349f1c3b0fcSJames Smart 	event_reply->immed_data = evt_dat->immed_dat;
1350f1c3b0fcSJames Smart 	if (evt_dat->len > 0)
135101e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
13524cc0e56eSJames Smart 			sg_copy_from_buffer(job->request_payload.sg_list,
13534cc0e56eSJames Smart 					    job->request_payload.sg_cnt,
1354f1c3b0fcSJames Smart 					    evt_dat->data, evt_dat->len);
1355f1c3b0fcSJames Smart 	else
135601e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
1357f1c3b0fcSJames Smart 
13584cc0e56eSJames Smart 	if (evt_dat) {
1359f1c3b0fcSJames Smart 		kfree(evt_dat->data);
1360f1c3b0fcSJames Smart 		kfree(evt_dat);
13614cc0e56eSJames Smart 	}
13624cc0e56eSJames Smart 
13634fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
13644cc0e56eSJames Smart 	lpfc_bsg_event_unref(evt);
13654fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
13664cc0e56eSJames Smart 	job->dd_data = NULL;
136701e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
136806548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
13691abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
13704cc0e56eSJames Smart 	return 0;
1371f1c3b0fcSJames Smart 
13724cc0e56eSJames Smart job_error:
13734cc0e56eSJames Smart 	job->dd_data = NULL;
137401e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
1375f1c3b0fcSJames Smart 	return rc;
1376f1c3b0fcSJames Smart }
1377f1c3b0fcSJames Smart 
1378f1c3b0fcSJames Smart /**
13793b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
13803b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
13813b5dd52aSJames Smart  * @cmdiocbq: Pointer to command iocb.
13823b5dd52aSJames Smart  * @rspiocbq: Pointer to response iocb.
13833b5dd52aSJames Smart  *
13843b5dd52aSJames Smart  * This function is the completion handler for iocbs issued using
13853b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp function. This function is called by the
13863b5dd52aSJames Smart  * ring event handler function without any lock held. This function
13873b5dd52aSJames Smart  * can be called from both worker thread context and interrupt
13883b5dd52aSJames Smart  * context. This function also can be called from other thread which
13893b5dd52aSJames Smart  * cleans up the SLI layer objects.
13903b5dd52aSJames Smart  * This function copy the contents of the response iocb to the
13913b5dd52aSJames Smart  * response iocb memory object provided by the caller of
13923b5dd52aSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13933b5dd52aSJames Smart  * sleeps for the iocb completion.
13943b5dd52aSJames Smart  **/
13953b5dd52aSJames Smart static void
13963b5dd52aSJames Smart lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
13973b5dd52aSJames Smart 			struct lpfc_iocbq *cmdiocbq,
13983b5dd52aSJames Smart 			struct lpfc_iocbq *rspiocbq)
13993b5dd52aSJames Smart {
14003b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
140175cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
140201e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
14033b5dd52aSJames Smart 	IOCB_t *rsp;
1404a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp;
14053b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp;
14063b5dd52aSJames Smart 	unsigned long flags;
14073b5dd52aSJames Smart 	int rc = 0;
14083b5dd52aSJames Smart 
1409a33c4f7bSJames Smart 	dd_data = cmdiocbq->context1;
1410a33c4f7bSJames Smart 
1411a33c4f7bSJames Smart 	/* Determine if job has been aborted */
14123b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1413a33c4f7bSJames Smart 	job = dd_data->set_job;
1414a33c4f7bSJames Smart 	if (job) {
1415a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
1416a33c4f7bSJames Smart 		job->dd_data = NULL;
14173b5dd52aSJames Smart 	}
1418a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
14193b5dd52aSJames Smart 
1420b5a9b2dfSJames Smart 	/* Close the timeout handler abort window */
1421b5a9b2dfSJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
14221b8d11abSJames Smart 	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1423b5a9b2dfSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1424b5a9b2dfSJames Smart 
14253b5dd52aSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
1426a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
1427a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
1428a33c4f7bSJames Smart 	rsp = &rspiocbq->iocb;
14293b5dd52aSJames Smart 
1430a33c4f7bSJames Smart 	/* Copy the completed job data or set the error status */
14313b5dd52aSJames Smart 
1432a33c4f7bSJames Smart 	if (job) {
143301e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
14343b5dd52aSJames Smart 		if (rsp->ulpStatus) {
14353b5dd52aSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1436e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
14373b5dd52aSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
14383b5dd52aSJames Smart 					rc = -ETIMEDOUT;
14393b5dd52aSJames Smart 					break;
14403b5dd52aSJames Smart 				case IOERR_INVALID_RPI:
14413b5dd52aSJames Smart 					rc = -EFAULT;
14423b5dd52aSJames Smart 					break;
14433b5dd52aSJames Smart 				default:
14443b5dd52aSJames Smart 					rc = -EACCES;
14453b5dd52aSJames Smart 					break;
14463b5dd52aSJames Smart 				}
1447a33c4f7bSJames Smart 			} else {
14483b5dd52aSJames Smart 				rc = -EACCES;
1449a33c4f7bSJames Smart 			}
1450a33c4f7bSJames Smart 		} else {
145101e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len = 0;
1452a33c4f7bSJames Smart 		}
1453a33c4f7bSJames Smart 	}
14543b5dd52aSJames Smart 
1455a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
14563b5dd52aSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1457a33c4f7bSJames Smart 	kfree(bmp);
14583b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
14593b5dd52aSJames Smart 	lpfc_nlp_put(ndlp);
14603b5dd52aSJames Smart 	kfree(dd_data);
1461a33c4f7bSJames Smart 
1462a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
1463a33c4f7bSJames Smart 
1464a33c4f7bSJames Smart 	if (job) {
146501e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
146606548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
14671abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
1468a33c4f7bSJames Smart 	}
14693b5dd52aSJames Smart 	return;
14703b5dd52aSJames Smart }
14713b5dd52aSJames Smart 
14723b5dd52aSJames Smart /**
14733b5dd52aSJames Smart  * lpfc_issue_ct_rsp - issue a ct response
14743b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
14753b5dd52aSJames Smart  * @job: Pointer to the job object.
14763b5dd52aSJames Smart  * @tag: tag index value into the ports context exchange array.
14773b5dd52aSJames Smart  * @bmp: Pointer to a dma buffer descriptor.
14783b5dd52aSJames Smart  * @num_entry: Number of enties in the bde.
14793b5dd52aSJames Smart  **/
14803b5dd52aSJames Smart static int
148175cc8cfcSJohannes Thumshirn lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1482a33c4f7bSJames Smart 		  struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1483a33c4f7bSJames Smart 		  int num_entry)
14843b5dd52aSJames Smart {
14853b5dd52aSJames Smart 	IOCB_t *icmd;
14863b5dd52aSJames Smart 	struct lpfc_iocbq *ctiocb = NULL;
14873b5dd52aSJames Smart 	int rc = 0;
14883b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp = NULL;
14893b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
1490b5a9b2dfSJames Smart 	unsigned long flags;
14913b5dd52aSJames Smart 	uint32_t creg_val;
14923b5dd52aSJames Smart 
14933b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
14943b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
14953b5dd52aSJames Smart 	if (!dd_data) {
14963b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
14973b5dd52aSJames Smart 				"2736 Failed allocation of dd_data\n");
14983b5dd52aSJames Smart 		rc = -ENOMEM;
14993b5dd52aSJames Smart 		goto no_dd_data;
15003b5dd52aSJames Smart 	}
15013b5dd52aSJames Smart 
15023b5dd52aSJames Smart 	/* Allocate buffer for  command iocb */
15033b5dd52aSJames Smart 	ctiocb = lpfc_sli_get_iocbq(phba);
15043b5dd52aSJames Smart 	if (!ctiocb) {
1505d439d286SJames Smart 		rc = -ENOMEM;
15063b5dd52aSJames Smart 		goto no_ctiocb;
15073b5dd52aSJames Smart 	}
15083b5dd52aSJames Smart 
15093b5dd52aSJames Smart 	icmd = &ctiocb->iocb;
15103b5dd52aSJames Smart 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
15113b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
15123b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
15133b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
15143b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
15153b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
15163b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15173b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
15183b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
15193b5dd52aSJames Smart 
15203b5dd52aSJames Smart 	/* Fill in rest of iocb */
15213b5dd52aSJames Smart 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
15223b5dd52aSJames Smart 	icmd->ulpBdeCount = 1;
15233b5dd52aSJames Smart 	icmd->ulpLe = 1;
15243b5dd52aSJames Smart 	icmd->ulpClass = CLASS3;
15253b5dd52aSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
15263b5dd52aSJames Smart 		/* Do not issue unsol response if oxid not marked as valid */
15276dd9e31cSJames Smart 		if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
15283b5dd52aSJames Smart 			rc = IOCB_ERROR;
15293b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
15303b5dd52aSJames Smart 		}
15317851fe2cSJames Smart 		icmd->ulpContext = phba->ct_ctx[tag].rxid;
15327851fe2cSJames Smart 		icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
15333b5dd52aSJames Smart 		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
15343b5dd52aSJames Smart 		if (!ndlp) {
15353b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
15363b5dd52aSJames Smart 				 "2721 ndlp null for oxid %x SID %x\n",
15373b5dd52aSJames Smart 					icmd->ulpContext,
15383b5dd52aSJames Smart 					phba->ct_ctx[tag].SID);
15393b5dd52aSJames Smart 			rc = IOCB_ERROR;
15403b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
15413b5dd52aSJames Smart 		}
1542589a52d6SJames Smart 
1543589a52d6SJames Smart 		/* Check if the ndlp is active */
1544589a52d6SJames Smart 		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1545a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1546589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1547589a52d6SJames Smart 		}
1548589a52d6SJames Smart 
1549589a52d6SJames Smart 		/* get a refernece count so the ndlp doesn't go away while
1550589a52d6SJames Smart 		 * we respond
1551589a52d6SJames Smart 		 */
1552589a52d6SJames Smart 		if (!lpfc_nlp_get(ndlp)) {
1553a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1554589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1555589a52d6SJames Smart 		}
1556589a52d6SJames Smart 
15577851fe2cSJames Smart 		icmd->un.ulpWord[3] =
15586d368e53SJames Smart 				phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15596d368e53SJames Smart 
15603b5dd52aSJames Smart 		/* The exchange is done, mark the entry as invalid */
15616dd9e31cSJames Smart 		phba->ct_ctx[tag].valid = UNSOL_INVALID;
15623b5dd52aSJames Smart 	} else
15633b5dd52aSJames Smart 		icmd->ulpContext = (ushort) tag;
15643b5dd52aSJames Smart 
15653b5dd52aSJames Smart 	icmd->ulpTimeout = phba->fc_ratov * 2;
15663b5dd52aSJames Smart 
15673b5dd52aSJames Smart 	/* Xmit CT response on exchange <xid> */
15683b5dd52aSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
15697851fe2cSJames Smart 		"2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
15707851fe2cSJames Smart 		icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
15713b5dd52aSJames Smart 
15723b5dd52aSJames Smart 	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
15733b5dd52aSJames Smart 	ctiocb->vport = phba->pport;
1574a33c4f7bSJames Smart 	ctiocb->context1 = dd_data;
1575a33c4f7bSJames Smart 	ctiocb->context2 = cmp;
15763b5dd52aSJames Smart 	ctiocb->context3 = bmp;
1577d5ce53b7SJames Smart 	ctiocb->context_un.ndlp = ndlp;
15783b5dd52aSJames Smart 	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1579a33c4f7bSJames Smart 
15803b5dd52aSJames Smart 	dd_data->type = TYPE_IOCB;
1581a33c4f7bSJames Smart 	dd_data->set_job = job;
15823b5dd52aSJames Smart 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
15833b5dd52aSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
1584a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = NULL;
1585a33c4f7bSJames Smart 	job->dd_data = dd_data;
15863b5dd52aSJames Smart 
15873b5dd52aSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
15889940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
15899940b97bSJames Smart 			rc = -IOCB_ERROR;
15909940b97bSJames Smart 			goto issue_ct_rsp_exit;
15919940b97bSJames Smart 		}
15923b5dd52aSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
15933b5dd52aSJames Smart 		writel(creg_val, phba->HCregaddr);
15943b5dd52aSJames Smart 		readl(phba->HCregaddr); /* flush */
15953b5dd52aSJames Smart 	}
15963b5dd52aSJames Smart 
15973b5dd52aSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15983b5dd52aSJames Smart 
1599b5a9b2dfSJames Smart 	if (rc == IOCB_SUCCESS) {
1600b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
1601b5a9b2dfSJames Smart 		/* make sure the I/O had not been completed/released */
1602b5a9b2dfSJames Smart 		if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1603b5a9b2dfSJames Smart 			/* open up abort window to timeout handler */
16041b8d11abSJames Smart 			ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
1605b5a9b2dfSJames Smart 		}
1606b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
16073b5dd52aSJames Smart 		return 0; /* done for now */
1608b5a9b2dfSJames Smart 	}
1609b5a9b2dfSJames Smart 
1610b5a9b2dfSJames Smart 	/* iocb failed so cleanup */
1611b5a9b2dfSJames Smart 	job->dd_data = NULL;
16123b5dd52aSJames Smart 
16133b5dd52aSJames Smart issue_ct_rsp_exit:
16143b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, ctiocb);
16153b5dd52aSJames Smart no_ctiocb:
16163b5dd52aSJames Smart 	kfree(dd_data);
16173b5dd52aSJames Smart no_dd_data:
16183b5dd52aSJames Smart 	return rc;
16193b5dd52aSJames Smart }
16203b5dd52aSJames Smart 
16213b5dd52aSJames Smart /**
16223b5dd52aSJames Smart  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
16233b5dd52aSJames Smart  * @job: SEND_MGMT_RESP fc_bsg_job
16243b5dd52aSJames Smart  **/
16253b5dd52aSJames Smart static int
162675cc8cfcSJohannes Thumshirn lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
16273b5dd52aSJames Smart {
1628cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
16293b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
163001e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
163101e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
16323b5dd52aSJames Smart 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
163301e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
16343b5dd52aSJames Smart 	struct ulp_bde64 *bpl;
1635a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1636a33c4f7bSJames Smart 	int bpl_entries;
16373b5dd52aSJames Smart 	uint32_t tag = mgmt_resp->tag;
16383b5dd52aSJames Smart 	unsigned long reqbfrcnt =
16393b5dd52aSJames Smart 			(unsigned long)job->request_payload.payload_len;
16403b5dd52aSJames Smart 	int rc = 0;
16413b5dd52aSJames Smart 
16423b5dd52aSJames Smart 	/* in case no data is transferred */
164301e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
16443b5dd52aSJames Smart 
16453b5dd52aSJames Smart 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
16463b5dd52aSJames Smart 		rc = -ERANGE;
16473b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
16483b5dd52aSJames Smart 	}
16493b5dd52aSJames Smart 
16503b5dd52aSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
16513b5dd52aSJames Smart 	if (!bmp) {
16523b5dd52aSJames Smart 		rc = -ENOMEM;
16533b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
16543b5dd52aSJames Smart 	}
16553b5dd52aSJames Smart 
16563b5dd52aSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
16573b5dd52aSJames Smart 	if (!bmp->virt) {
16583b5dd52aSJames Smart 		rc = -ENOMEM;
16593b5dd52aSJames Smart 		goto send_mgmt_rsp_free_bmp;
16603b5dd52aSJames Smart 	}
16613b5dd52aSJames Smart 
16623b5dd52aSJames Smart 	INIT_LIST_HEAD(&bmp->list);
16633b5dd52aSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
1664a33c4f7bSJames Smart 	bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1665a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1666a33c4f7bSJames Smart 				     1, bpl, &bpl_entries);
1667a33c4f7bSJames Smart 	if (!cmp) {
1668a33c4f7bSJames Smart 		rc = -ENOMEM;
1669a33c4f7bSJames Smart 		goto send_mgmt_rsp_free_bmp;
16703b5dd52aSJames Smart 	}
1671a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
1672a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
16733b5dd52aSJames Smart 
1674a33c4f7bSJames Smart 	rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
16753b5dd52aSJames Smart 
16763b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
16773b5dd52aSJames Smart 		return 0; /* done for now */
16783b5dd52aSJames Smart 
16793b5dd52aSJames Smart 	rc = -EACCES;
1680a33c4f7bSJames Smart 
1681a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
16823b5dd52aSJames Smart 
16833b5dd52aSJames Smart send_mgmt_rsp_free_bmp:
1684a33c4f7bSJames Smart 	if (bmp->virt)
1685a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
16863b5dd52aSJames Smart 	kfree(bmp);
16873b5dd52aSJames Smart send_mgmt_rsp_exit:
16883b5dd52aSJames Smart 	/* make error code available to userspace */
168901e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
16903b5dd52aSJames Smart 	job->dd_data = NULL;
16913b5dd52aSJames Smart 	return rc;
16923b5dd52aSJames Smart }
16933b5dd52aSJames Smart 
16943b5dd52aSJames Smart /**
16957ad20aa9SJames Smart  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
16967ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
16973b5dd52aSJames Smart  *
16987ad20aa9SJames Smart  * This function is responsible for preparing driver for diag loopback
16997ad20aa9SJames Smart  * on device.
17003b5dd52aSJames Smart  */
17013b5dd52aSJames Smart static int
170288a2cfbbSJames Smart lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
17033b5dd52aSJames Smart {
17043b5dd52aSJames Smart 	struct lpfc_vport **vports;
17057ad20aa9SJames Smart 	struct Scsi_Host *shost;
17067ad20aa9SJames Smart 	struct lpfc_sli *psli;
1707895427bdSJames Smart 	struct lpfc_queue *qp = NULL;
17087ad20aa9SJames Smart 	struct lpfc_sli_ring *pring;
17093b5dd52aSJames Smart 	int i = 0;
17103b5dd52aSJames Smart 
17117ad20aa9SJames Smart 	psli = &phba->sli;
17127ad20aa9SJames Smart 	if (!psli)
17137ad20aa9SJames Smart 		return -ENODEV;
17143b5dd52aSJames Smart 
17153b5dd52aSJames Smart 
17163b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
17173b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
17187ad20aa9SJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
17197ad20aa9SJames Smart 		return -EACCES;
17203b5dd52aSJames Smart 
17213b5dd52aSJames Smart 	vports = lpfc_create_vport_work_array(phba);
17223b5dd52aSJames Smart 	if (vports) {
17233b5dd52aSJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17243b5dd52aSJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
17253b5dd52aSJames Smart 			scsi_block_requests(shost);
17263b5dd52aSJames Smart 		}
17273b5dd52aSJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
17283b5dd52aSJames Smart 	} else {
17293b5dd52aSJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
17303b5dd52aSJames Smart 		scsi_block_requests(shost);
17313b5dd52aSJames Smart 	}
17323b5dd52aSJames Smart 
1733895427bdSJames Smart 	if (phba->sli_rev != LPFC_SLI_REV4) {
1734895427bdSJames Smart 		pring = &psli->sli3_ring[LPFC_FCP_RING];
1735895427bdSJames Smart 		lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1736895427bdSJames Smart 		return 0;
1737895427bdSJames Smart 	}
1738895427bdSJames Smart 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1739895427bdSJames Smart 		pring = qp->pring;
1740895427bdSJames Smart 		if (!pring || (pring->ringno != LPFC_FCP_RING))
1741895427bdSJames Smart 			continue;
1742895427bdSJames Smart 		if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1743895427bdSJames Smart 				      &pring->ring_lock))
17443b5dd52aSJames Smart 			break;
17453b5dd52aSJames Smart 	}
17467ad20aa9SJames Smart 	return 0;
17477ad20aa9SJames Smart }
17483b5dd52aSJames Smart 
17497ad20aa9SJames Smart /**
17507ad20aa9SJames Smart  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
17517ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17527ad20aa9SJames Smart  *
17537ad20aa9SJames Smart  * This function is responsible for driver exit processing of setting up
17547ad20aa9SJames Smart  * diag loopback mode on device.
17557ad20aa9SJames Smart  */
17567ad20aa9SJames Smart static void
17577ad20aa9SJames Smart lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
17587ad20aa9SJames Smart {
17597ad20aa9SJames Smart 	struct Scsi_Host *shost;
17607ad20aa9SJames Smart 	struct lpfc_vport **vports;
17617ad20aa9SJames Smart 	int i;
17627ad20aa9SJames Smart 
17637ad20aa9SJames Smart 	vports = lpfc_create_vport_work_array(phba);
17647ad20aa9SJames Smart 	if (vports) {
17657ad20aa9SJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17667ad20aa9SJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
17677ad20aa9SJames Smart 			scsi_unblock_requests(shost);
17687ad20aa9SJames Smart 		}
17697ad20aa9SJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
17707ad20aa9SJames Smart 	} else {
17717ad20aa9SJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
17727ad20aa9SJames Smart 		scsi_unblock_requests(shost);
17737ad20aa9SJames Smart 	}
17747ad20aa9SJames Smart 	return;
17757ad20aa9SJames Smart }
17767ad20aa9SJames Smart 
17777ad20aa9SJames Smart /**
17787ad20aa9SJames Smart  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
17797ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17807ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
17817ad20aa9SJames Smart  *
17827ad20aa9SJames Smart  * This function is responsible for placing an sli3  port into diagnostic
17837ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
17847ad20aa9SJames Smart  * All new scsi requests are blocked, a small delay is used to allow the
17857ad20aa9SJames Smart  * scsi requests to complete then the link is brought down. If the link is
17867ad20aa9SJames Smart  * is placed in loopback mode then scsi requests are again allowed
17877ad20aa9SJames Smart  * so the scsi mid-layer doesn't give up on the port.
17887ad20aa9SJames Smart  * All of this is done in-line.
17897ad20aa9SJames Smart  */
17907ad20aa9SJames Smart static int
179175cc8cfcSJohannes Thumshirn lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
17927ad20aa9SJames Smart {
179301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
179401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
17957ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
17967ad20aa9SJames Smart 	uint32_t link_flags;
17977ad20aa9SJames Smart 	uint32_t timeout;
17981b51197dSJames Smart 	LPFC_MBOXQ_t *pmboxq  = NULL;
1799b76f2dc9SJames Smart 	int mbxstatus = MBX_SUCCESS;
18007ad20aa9SJames Smart 	int i = 0;
18017ad20aa9SJames Smart 	int rc = 0;
18027ad20aa9SJames Smart 
18037ad20aa9SJames Smart 	/* no data to return just the return code */
180401e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
18057ad20aa9SJames Smart 
18067ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
18077ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
18087ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
18097ad20aa9SJames Smart 				"2738 Received DIAG MODE request size:%d "
18107ad20aa9SJames Smart 				"below the minimum size:%d\n",
18117ad20aa9SJames Smart 				job->request_len,
18127ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
18137ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
18147ad20aa9SJames Smart 		rc = -EINVAL;
18157ad20aa9SJames Smart 		goto job_error;
18167ad20aa9SJames Smart 	}
18177ad20aa9SJames Smart 
181888a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
18197ad20aa9SJames Smart 	if (rc)
18207ad20aa9SJames Smart 		goto job_error;
18217ad20aa9SJames Smart 
18227ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
18237ad20aa9SJames Smart 	loopback_mode = (struct diag_mode_set *)
182401e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
18257ad20aa9SJames Smart 	link_flags = loopback_mode->type;
18267ad20aa9SJames Smart 	timeout = loopback_mode->timeout * 100;
18277ad20aa9SJames Smart 
18287ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18297ad20aa9SJames Smart 	if (!pmboxq) {
18307ad20aa9SJames Smart 		rc = -ENOMEM;
18317ad20aa9SJames Smart 		goto loopback_mode_exit;
18327ad20aa9SJames Smart 	}
18333b5dd52aSJames Smart 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
18343b5dd52aSJames Smart 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
18353b5dd52aSJames Smart 	pmboxq->u.mb.mbxOwner = OWN_HOST;
18363b5dd52aSJames Smart 
18373b5dd52aSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
18383b5dd52aSJames Smart 
18393b5dd52aSJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
18403b5dd52aSJames Smart 		/* wait for link down before proceeding */
18413b5dd52aSJames Smart 		i = 0;
18423b5dd52aSJames Smart 		while (phba->link_state != LPFC_LINK_DOWN) {
18433b5dd52aSJames Smart 			if (i++ > timeout) {
18443b5dd52aSJames Smart 				rc = -ETIMEDOUT;
18453b5dd52aSJames Smart 				goto loopback_mode_exit;
18463b5dd52aSJames Smart 			}
18473b5dd52aSJames Smart 			msleep(10);
18483b5dd52aSJames Smart 		}
18493b5dd52aSJames Smart 
18503b5dd52aSJames Smart 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
18513b5dd52aSJames Smart 		if (link_flags == INTERNAL_LOOP_BACK)
18523b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
18533b5dd52aSJames Smart 		else
18543b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags =
18553b5dd52aSJames Smart 				FLAGS_TOPOLOGY_MODE_LOOP;
18563b5dd52aSJames Smart 
18573b5dd52aSJames Smart 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
18583b5dd52aSJames Smart 		pmboxq->u.mb.mbxOwner = OWN_HOST;
18593b5dd52aSJames Smart 
18603b5dd52aSJames Smart 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
18613b5dd52aSJames Smart 						     LPFC_MBOX_TMO);
18623b5dd52aSJames Smart 
18633b5dd52aSJames Smart 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
18643b5dd52aSJames Smart 			rc = -ENODEV;
18653b5dd52aSJames Smart 		else {
18661b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
18673b5dd52aSJames Smart 			phba->link_flag |= LS_LOOPBACK_MODE;
18681b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
18693b5dd52aSJames Smart 			/* wait for the link attention interrupt */
18703b5dd52aSJames Smart 			msleep(100);
18713b5dd52aSJames Smart 
18723b5dd52aSJames Smart 			i = 0;
18733b5dd52aSJames Smart 			while (phba->link_state != LPFC_HBA_READY) {
18743b5dd52aSJames Smart 				if (i++ > timeout) {
18753b5dd52aSJames Smart 					rc = -ETIMEDOUT;
18763b5dd52aSJames Smart 					break;
18773b5dd52aSJames Smart 				}
18783b5dd52aSJames Smart 
18793b5dd52aSJames Smart 				msleep(10);
18803b5dd52aSJames Smart 			}
18813b5dd52aSJames Smart 		}
18823b5dd52aSJames Smart 
18833b5dd52aSJames Smart 	} else
18843b5dd52aSJames Smart 		rc = -ENODEV;
18853b5dd52aSJames Smart 
18863b5dd52aSJames Smart loopback_mode_exit:
18877ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
18883b5dd52aSJames Smart 
18893b5dd52aSJames Smart 	/*
18903b5dd52aSJames Smart 	 * Let SLI layer release mboxq if mbox command completed after timeout.
18913b5dd52aSJames Smart 	 */
18921b51197dSJames Smart 	if (pmboxq && mbxstatus != MBX_TIMEOUT)
18933b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
18943b5dd52aSJames Smart 
18953b5dd52aSJames Smart job_error:
18963b5dd52aSJames Smart 	/* make error code available to userspace */
189701e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
18983b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
18993b5dd52aSJames Smart 	if (rc == 0)
190006548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
19011abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
19023b5dd52aSJames Smart 	return rc;
19033b5dd52aSJames Smart }
19043b5dd52aSJames Smart 
19053b5dd52aSJames Smart /**
19067ad20aa9SJames Smart  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
19077ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
19087ad20aa9SJames Smart  * @diag: Flag for set link to diag or nomral operation state.
19097ad20aa9SJames Smart  *
19107ad20aa9SJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
19117ad20aa9SJames Smart  * link to either diag state or normal operation state.
19127ad20aa9SJames Smart  */
19137ad20aa9SJames Smart static int
19147ad20aa9SJames Smart lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
19157ad20aa9SJames Smart {
19167ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
19177ad20aa9SJames Smart 	struct lpfc_mbx_set_link_diag_state *link_diag_state;
19187ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
19197ad20aa9SJames Smart 	int mbxstatus = MBX_SUCCESS, rc;
19207ad20aa9SJames Smart 
19217ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19227ad20aa9SJames Smart 	if (!pmboxq)
19237ad20aa9SJames Smart 		return -ENOMEM;
19247ad20aa9SJames Smart 
19257ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
19267ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
19277ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19287ad20aa9SJames Smart 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
19297ad20aa9SJames Smart 				req_len, LPFC_SLI4_MBX_EMBED);
19307ad20aa9SJames Smart 	if (alloc_len != req_len) {
19317ad20aa9SJames Smart 		rc = -ENOMEM;
19327ad20aa9SJames Smart 		goto link_diag_state_set_out;
19337ad20aa9SJames Smart 	}
19341b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
19351b51197dSJames Smart 			"3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
19361b51197dSJames Smart 			diag, phba->sli4_hba.lnk_info.lnk_tp,
19371b51197dSJames Smart 			phba->sli4_hba.lnk_info.lnk_no);
19381b51197dSJames Smart 
19397ad20aa9SJames Smart 	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
19409731592bSJames Smart 	bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
19419731592bSJames Smart 	       LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
19427ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
19431b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
19447ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
19451b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
19467ad20aa9SJames Smart 	if (diag)
19477ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
19487ad20aa9SJames Smart 		       &link_diag_state->u.req, 1);
19497ad20aa9SJames Smart 	else
19507ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
19517ad20aa9SJames Smart 		       &link_diag_state->u.req, 0);
19527ad20aa9SJames Smart 
19537ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
19547ad20aa9SJames Smart 
19557ad20aa9SJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
19567ad20aa9SJames Smart 		rc = 0;
19577ad20aa9SJames Smart 	else
19587ad20aa9SJames Smart 		rc = -ENODEV;
19597ad20aa9SJames Smart 
19607ad20aa9SJames Smart link_diag_state_set_out:
19617ad20aa9SJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
19627ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
19637ad20aa9SJames Smart 
19647ad20aa9SJames Smart 	return rc;
19657ad20aa9SJames Smart }
19667ad20aa9SJames Smart 
19677ad20aa9SJames Smart /**
19689a66d990SJames Smart  * lpfc_sli4_bsg_set_loopback_mode - set sli4 internal loopback diagnostic
19691b51197dSJames Smart  * @phba: Pointer to HBA context object.
19709a66d990SJames Smart  * @mode: loopback mode to set
19719a66d990SJames Smart  * @link_no: link number for loopback mode to set
19721b51197dSJames Smart  *
19731b51197dSJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
19749a66d990SJames Smart  * up loopback diagnostic for a link.
19751b51197dSJames Smart  */
19761b51197dSJames Smart static int
19779a66d990SJames Smart lpfc_sli4_bsg_set_loopback_mode(struct lpfc_hba *phba, int mode,
19789a66d990SJames Smart 				uint32_t link_no)
19791b51197dSJames Smart {
19801b51197dSJames Smart 	LPFC_MBOXQ_t *pmboxq;
19811b51197dSJames Smart 	uint32_t req_len, alloc_len;
19821b51197dSJames Smart 	struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
19831b51197dSJames Smart 	int mbxstatus = MBX_SUCCESS, rc = 0;
19841b51197dSJames Smart 
19851b51197dSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19861b51197dSJames Smart 	if (!pmboxq)
19871b51197dSJames Smart 		return -ENOMEM;
19881b51197dSJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
19891b51197dSJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
19901b51197dSJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19911b51197dSJames Smart 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
19921b51197dSJames Smart 				req_len, LPFC_SLI4_MBX_EMBED);
19931b51197dSJames Smart 	if (alloc_len != req_len) {
19941b51197dSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
19951b51197dSJames Smart 		return -ENOMEM;
19961b51197dSJames Smart 	}
19971b51197dSJames Smart 	link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
19981b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_num,
19999a66d990SJames Smart 	       &link_diag_loopback->u.req, link_no);
20009a66d990SJames Smart 
20019a66d990SJames Smart 	if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
20021b51197dSJames Smart 		bf_set(lpfc_mbx_set_diag_state_link_type,
20039a66d990SJames Smart 		       &link_diag_loopback->u.req, LPFC_LNK_FC_TRUNKED);
20049a66d990SJames Smart 	} else {
20059a66d990SJames Smart 		bf_set(lpfc_mbx_set_diag_state_link_type,
20069a66d990SJames Smart 		       &link_diag_loopback->u.req,
20079a66d990SJames Smart 		       phba->sli4_hba.lnk_info.lnk_tp);
20089a66d990SJames Smart 	}
20099a66d990SJames Smart 
20101b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
20119a66d990SJames Smart 	       mode);
20121b51197dSJames Smart 
20131b51197dSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
20141b51197dSJames Smart 	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
20151b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20161b51197dSJames Smart 				"3127 Failed setup loopback mode mailbox "
20171b51197dSJames Smart 				"command, rc:x%x, status:x%x\n", mbxstatus,
20181b51197dSJames Smart 				pmboxq->u.mb.mbxStatus);
20191b51197dSJames Smart 		rc = -ENODEV;
20201b51197dSJames Smart 	}
20211b51197dSJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
20221b51197dSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
20231b51197dSJames Smart 	return rc;
20241b51197dSJames Smart }
20251b51197dSJames Smart 
20261b51197dSJames Smart /**
20271b51197dSJames Smart  * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
20281b51197dSJames Smart  * @phba: Pointer to HBA context object.
20291b51197dSJames Smart  *
20301b51197dSJames Smart  * This function set up SLI4 FC port registrations for diagnostic run, which
20311b51197dSJames Smart  * includes all the rpis, vfi, and also vpi.
20321b51197dSJames Smart  */
20331b51197dSJames Smart static int
20341b51197dSJames Smart lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
20351b51197dSJames Smart {
20361b51197dSJames Smart 	int rc;
20371b51197dSJames Smart 
20381b51197dSJames Smart 	if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
20391b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20401b51197dSJames Smart 				"3136 Port still had vfi registered: "
20411b51197dSJames Smart 				"mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
20421b51197dSJames Smart 				phba->pport->fc_myDID, phba->fcf.fcfi,
20431b51197dSJames Smart 				phba->sli4_hba.vfi_ids[phba->pport->vfi],
20441b51197dSJames Smart 				phba->vpi_ids[phba->pport->vpi]);
20451b51197dSJames Smart 		return -EINVAL;
20461b51197dSJames Smart 	}
20471b51197dSJames Smart 	rc = lpfc_issue_reg_vfi(phba->pport);
20481b51197dSJames Smart 	return rc;
20491b51197dSJames Smart }
20501b51197dSJames Smart 
20511b51197dSJames Smart /**
20527ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
20537ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
20547ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
20557ad20aa9SJames Smart  *
20567ad20aa9SJames Smart  * This function is responsible for placing an sli4 port into diagnostic
20577ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
20587ad20aa9SJames Smart  */
20597ad20aa9SJames Smart static int
206075cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
20617ad20aa9SJames Smart {
206201e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
206301e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
20647ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
20659a66d990SJames Smart 	uint32_t link_flags, timeout, link_no;
20661b51197dSJames Smart 	int i, rc = 0;
20677ad20aa9SJames Smart 
20687ad20aa9SJames Smart 	/* no data to return just the return code */
206901e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
20707ad20aa9SJames Smart 
20717ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
20727ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
20737ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20747ad20aa9SJames Smart 				"3011 Received DIAG MODE request size:%d "
20757ad20aa9SJames Smart 				"below the minimum size:%d\n",
20767ad20aa9SJames Smart 				job->request_len,
20777ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
20787ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
20797ad20aa9SJames Smart 		rc = -EINVAL;
20809a66d990SJames Smart 		goto job_done;
20819a66d990SJames Smart 	}
20829a66d990SJames Smart 
20839a66d990SJames Smart 	loopback_mode = (struct diag_mode_set *)
20849a66d990SJames Smart 		bsg_request->rqst_data.h_vendor.vendor_cmd;
20859a66d990SJames Smart 	link_flags = loopback_mode->type;
20869a66d990SJames Smart 	timeout = loopback_mode->timeout * 100;
20879a66d990SJames Smart 
20889a66d990SJames Smart 	if (loopback_mode->physical_link == -1)
20899a66d990SJames Smart 		link_no = phba->sli4_hba.lnk_info.lnk_no;
20909a66d990SJames Smart 	else
20919a66d990SJames Smart 		link_no = loopback_mode->physical_link;
20929a66d990SJames Smart 
20939a66d990SJames Smart 	if (link_flags == DISABLE_LOOP_BACK) {
20949a66d990SJames Smart 		rc = lpfc_sli4_bsg_set_loopback_mode(phba,
20959a66d990SJames Smart 					LPFC_DIAG_LOOPBACK_TYPE_DISABLE,
20969a66d990SJames Smart 					link_no);
20979a66d990SJames Smart 		if (!rc) {
20989a66d990SJames Smart 			/* Unset the need disable bit */
20999a66d990SJames Smart 			phba->sli4_hba.conf_trunk &= ~((1 << link_no) << 4);
21009a66d990SJames Smart 		}
21019a66d990SJames Smart 		goto job_done;
21029a66d990SJames Smart 	} else {
21039a66d990SJames Smart 		/* Check if we need to disable the loopback state */
21049a66d990SJames Smart 		if (phba->sli4_hba.conf_trunk & ((1 << link_no) << 4)) {
21059a66d990SJames Smart 			rc = -EPERM;
21069a66d990SJames Smart 			goto job_done;
21079a66d990SJames Smart 		}
21087ad20aa9SJames Smart 	}
21097ad20aa9SJames Smart 
211088a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
21117ad20aa9SJames Smart 	if (rc)
21129a66d990SJames Smart 		goto job_done;
21137ad20aa9SJames Smart 
21141b51197dSJames Smart 	/* indicate we are in loobpack diagnostic mode */
21151b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
21161b51197dSJames Smart 	phba->link_flag |= LS_LOOPBACK_MODE;
21171b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
21181b51197dSJames Smart 
21191b51197dSJames Smart 	/* reset port to start frome scratch */
21201b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
21211b51197dSJames Smart 	if (rc)
21229a66d990SJames Smart 		goto job_done;
21231b51197dSJames Smart 
21247ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
21251b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21261b51197dSJames Smart 			"3129 Bring link to diagnostic state.\n");
21277ad20aa9SJames Smart 
21287ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
21291b51197dSJames Smart 	if (rc) {
21301b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
21311b51197dSJames Smart 				"3130 Failed to bring link to diagnostic "
21321b51197dSJames Smart 				"state, rc:x%x\n", rc);
21337ad20aa9SJames Smart 		goto loopback_mode_exit;
21341b51197dSJames Smart 	}
21357ad20aa9SJames Smart 
21367ad20aa9SJames Smart 	/* wait for link down before proceeding */
21377ad20aa9SJames Smart 	i = 0;
21387ad20aa9SJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
21397ad20aa9SJames Smart 		if (i++ > timeout) {
21407ad20aa9SJames Smart 			rc = -ETIMEDOUT;
21411b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21421b51197dSJames Smart 					"3131 Timeout waiting for link to "
21431b51197dSJames Smart 					"diagnostic mode, timeout:%d ms\n",
21441b51197dSJames Smart 					timeout * 10);
21457ad20aa9SJames Smart 			goto loopback_mode_exit;
21467ad20aa9SJames Smart 		}
21477ad20aa9SJames Smart 		msleep(10);
21487ad20aa9SJames Smart 	}
21497ad20aa9SJames Smart 
21501b51197dSJames Smart 	/* set up loopback mode */
21511b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21521b51197dSJames Smart 			"3132 Set up loopback mode:x%x\n", link_flags);
21531b51197dSJames Smart 
21549a66d990SJames Smart 	switch (link_flags) {
21559a66d990SJames Smart 	case INTERNAL_LOOP_BACK:
21569a66d990SJames Smart 		if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
21579a66d990SJames Smart 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
21589a66d990SJames Smart 					LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
21599a66d990SJames Smart 					link_no);
21609a66d990SJames Smart 		} else {
21619a66d990SJames Smart 			/* Trunk is configured, but link is not in this trunk */
21629a66d990SJames Smart 			if (phba->sli4_hba.conf_trunk) {
21639a66d990SJames Smart 				rc = -ELNRNG;
21649a66d990SJames Smart 				goto loopback_mode_exit;
21659a66d990SJames Smart 			}
21669a66d990SJames Smart 
21679a66d990SJames Smart 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
21689a66d990SJames Smart 					LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
21699a66d990SJames Smart 					link_no);
21709a66d990SJames Smart 		}
21719a66d990SJames Smart 
21729a66d990SJames Smart 		if (!rc) {
21739a66d990SJames Smart 			/* Set the need disable bit */
21749a66d990SJames Smart 			phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
21759a66d990SJames Smart 		}
21769a66d990SJames Smart 
21779a66d990SJames Smart 		break;
21789a66d990SJames Smart 	case EXTERNAL_LOOP_BACK:
21799a66d990SJames Smart 		if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
21809a66d990SJames Smart 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
21819a66d990SJames Smart 				LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL_TRUNKED,
21829a66d990SJames Smart 				link_no);
21839a66d990SJames Smart 		} else {
21849a66d990SJames Smart 			/* Trunk is configured, but link is not in this trunk */
21859a66d990SJames Smart 			if (phba->sli4_hba.conf_trunk) {
21869a66d990SJames Smart 				rc = -ELNRNG;
21879a66d990SJames Smart 				goto loopback_mode_exit;
21889a66d990SJames Smart 			}
21899a66d990SJames Smart 
21909a66d990SJames Smart 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
21919a66d990SJames Smart 						LPFC_DIAG_LOOPBACK_TYPE_SERDES,
21929a66d990SJames Smart 						link_no);
21939a66d990SJames Smart 		}
21949a66d990SJames Smart 
21959a66d990SJames Smart 		if (!rc) {
21969a66d990SJames Smart 			/* Set the need disable bit */
21979a66d990SJames Smart 			phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
21989a66d990SJames Smart 		}
21999a66d990SJames Smart 
22009a66d990SJames Smart 		break;
22019a66d990SJames Smart 	default:
22021b51197dSJames Smart 		rc = -EINVAL;
22031b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
22041b51197dSJames Smart 				"3141 Loopback mode:x%x not supported\n",
22051b51197dSJames Smart 				link_flags);
22061b51197dSJames Smart 		goto loopback_mode_exit;
22071b51197dSJames Smart 	}
22081b51197dSJames Smart 
22091b51197dSJames Smart 	if (!rc) {
22107ad20aa9SJames Smart 		/* wait for the link attention interrupt */
22117ad20aa9SJames Smart 		msleep(100);
22127ad20aa9SJames Smart 		i = 0;
22131b51197dSJames Smart 		while (phba->link_state < LPFC_LINK_UP) {
22141b51197dSJames Smart 			if (i++ > timeout) {
22151b51197dSJames Smart 				rc = -ETIMEDOUT;
22161b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
22171b51197dSJames Smart 					"3137 Timeout waiting for link up "
22181b51197dSJames Smart 					"in loopback mode, timeout:%d ms\n",
22191b51197dSJames Smart 					timeout * 10);
22201b51197dSJames Smart 				break;
22211b51197dSJames Smart 			}
22221b51197dSJames Smart 			msleep(10);
22231b51197dSJames Smart 		}
22241b51197dSJames Smart 	}
22251b51197dSJames Smart 
22261b51197dSJames Smart 	/* port resource registration setup for loopback diagnostic */
22271b51197dSJames Smart 	if (!rc) {
22281b51197dSJames Smart 		/* set up a none zero myDID for loopback test */
22291b51197dSJames Smart 		phba->pport->fc_myDID = 1;
22301b51197dSJames Smart 		rc = lpfc_sli4_diag_fcport_reg_setup(phba);
22311b51197dSJames Smart 	} else
22321b51197dSJames Smart 		goto loopback_mode_exit;
22331b51197dSJames Smart 
22341b51197dSJames Smart 	if (!rc) {
22351b51197dSJames Smart 		/* wait for the port ready */
22361b51197dSJames Smart 		msleep(100);
22371b51197dSJames Smart 		i = 0;
22387ad20aa9SJames Smart 		while (phba->link_state != LPFC_HBA_READY) {
22397ad20aa9SJames Smart 			if (i++ > timeout) {
22407ad20aa9SJames Smart 				rc = -ETIMEDOUT;
22411b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
22421b51197dSJames Smart 					"3133 Timeout waiting for port "
22431b51197dSJames Smart 					"loopback mode ready, timeout:%d ms\n",
22441b51197dSJames Smart 					timeout * 10);
22457ad20aa9SJames Smart 				break;
22467ad20aa9SJames Smart 			}
22477ad20aa9SJames Smart 			msleep(10);
22487ad20aa9SJames Smart 		}
22497ad20aa9SJames Smart 	}
22507ad20aa9SJames Smart 
22517ad20aa9SJames Smart loopback_mode_exit:
22521b51197dSJames Smart 	/* clear loopback diagnostic mode */
22531b51197dSJames Smart 	if (rc) {
22541b51197dSJames Smart 		spin_lock_irq(&phba->hbalock);
22551b51197dSJames Smart 		phba->link_flag &= ~LS_LOOPBACK_MODE;
22561b51197dSJames Smart 		spin_unlock_irq(&phba->hbalock);
22571b51197dSJames Smart 	}
22587ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
22597ad20aa9SJames Smart 
22609a66d990SJames Smart job_done:
22617ad20aa9SJames Smart 	/* make error code available to userspace */
226201e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
22637ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
22647ad20aa9SJames Smart 	if (rc == 0)
226506548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
22661abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
22677ad20aa9SJames Smart 	return rc;
22687ad20aa9SJames Smart }
22697ad20aa9SJames Smart 
22707ad20aa9SJames Smart /**
22717ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
22727ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
22737ad20aa9SJames Smart  *
22747ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
22757ad20aa9SJames Smart  * command from the user to proper driver action routines.
22767ad20aa9SJames Smart  */
22777ad20aa9SJames Smart static int
227875cc8cfcSJohannes Thumshirn lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
22797ad20aa9SJames Smart {
22807ad20aa9SJames Smart 	struct Scsi_Host *shost;
22817ad20aa9SJames Smart 	struct lpfc_vport *vport;
22827ad20aa9SJames Smart 	struct lpfc_hba *phba;
22837ad20aa9SJames Smart 	int rc;
22847ad20aa9SJames Smart 
2285cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
22867ad20aa9SJames Smart 	if (!shost)
22877ad20aa9SJames Smart 		return -ENODEV;
2288cd21c605SJohannes Thumshirn 	vport = shost_priv(shost);
22897ad20aa9SJames Smart 	if (!vport)
22907ad20aa9SJames Smart 		return -ENODEV;
22917ad20aa9SJames Smart 	phba = vport->phba;
22927ad20aa9SJames Smart 	if (!phba)
22937ad20aa9SJames Smart 		return -ENODEV;
22947ad20aa9SJames Smart 
22957ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
22967ad20aa9SJames Smart 		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2297719162bdSJames Smart 	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
22987ad20aa9SJames Smart 		 LPFC_SLI_INTF_IF_TYPE_2)
22997ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
23007ad20aa9SJames Smart 	else
23017ad20aa9SJames Smart 		rc = -ENODEV;
23027ad20aa9SJames Smart 
23037ad20aa9SJames Smart 	return rc;
23047ad20aa9SJames Smart }
23057ad20aa9SJames Smart 
23067ad20aa9SJames Smart /**
23077ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
23087ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
23097ad20aa9SJames Smart  *
23107ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
23117ad20aa9SJames Smart  * command from the user to proper driver action routines.
23127ad20aa9SJames Smart  */
23137ad20aa9SJames Smart static int
231475cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
23157ad20aa9SJames Smart {
231601e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
231701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
23187ad20aa9SJames Smart 	struct Scsi_Host *shost;
23197ad20aa9SJames Smart 	struct lpfc_vport *vport;
23207ad20aa9SJames Smart 	struct lpfc_hba *phba;
23211b51197dSJames Smart 	struct diag_mode_set *loopback_mode_end_cmd;
23221b51197dSJames Smart 	uint32_t timeout;
23231b51197dSJames Smart 	int rc, i;
23247ad20aa9SJames Smart 
2325cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
23267ad20aa9SJames Smart 	if (!shost)
23277ad20aa9SJames Smart 		return -ENODEV;
2328cd21c605SJohannes Thumshirn 	vport = shost_priv(shost);
23297ad20aa9SJames Smart 	if (!vport)
23307ad20aa9SJames Smart 		return -ENODEV;
23317ad20aa9SJames Smart 	phba = vport->phba;
23327ad20aa9SJames Smart 	if (!phba)
23337ad20aa9SJames Smart 		return -ENODEV;
23347ad20aa9SJames Smart 
23357ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
23367ad20aa9SJames Smart 		return -ENODEV;
2337719162bdSJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
23387ad20aa9SJames Smart 	    LPFC_SLI_INTF_IF_TYPE_2)
23397ad20aa9SJames Smart 		return -ENODEV;
23407ad20aa9SJames Smart 
23411b51197dSJames Smart 	/* clear loopback diagnostic mode */
23421b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
23431b51197dSJames Smart 	phba->link_flag &= ~LS_LOOPBACK_MODE;
23441b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
23451b51197dSJames Smart 	loopback_mode_end_cmd = (struct diag_mode_set *)
234601e0e15cSJohannes Thumshirn 			bsg_request->rqst_data.h_vendor.vendor_cmd;
23471b51197dSJames Smart 	timeout = loopback_mode_end_cmd->timeout * 100;
23481b51197dSJames Smart 
23497ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
23501b51197dSJames Smart 	if (rc) {
23511b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
23521b51197dSJames Smart 				"3139 Failed to bring link to diagnostic "
23531b51197dSJames Smart 				"state, rc:x%x\n", rc);
23541b51197dSJames Smart 		goto loopback_mode_end_exit;
23551b51197dSJames Smart 	}
23567ad20aa9SJames Smart 
23571b51197dSJames Smart 	/* wait for link down before proceeding */
23581b51197dSJames Smart 	i = 0;
23591b51197dSJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
23601b51197dSJames Smart 		if (i++ > timeout) {
23611b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
23621b51197dSJames Smart 					"3140 Timeout waiting for link to "
23631b51197dSJames Smart 					"diagnostic mode_end, timeout:%d ms\n",
23641b51197dSJames Smart 					timeout * 10);
23651b51197dSJames Smart 			/* there is nothing much we can do here */
23661b51197dSJames Smart 			break;
23671b51197dSJames Smart 		}
23681b51197dSJames Smart 		msleep(10);
23691b51197dSJames Smart 	}
23707ad20aa9SJames Smart 
23711b51197dSJames Smart 	/* reset port resource registrations */
23721b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
23731b51197dSJames Smart 	phba->pport->fc_myDID = 0;
23741b51197dSJames Smart 
23751b51197dSJames Smart loopback_mode_end_exit:
23761b51197dSJames Smart 	/* make return code available to userspace */
237701e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
23781b51197dSJames Smart 	/* complete the job back to userspace if no error */
23791b51197dSJames Smart 	if (rc == 0)
238006548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
23811abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
23827ad20aa9SJames Smart 	return rc;
23837ad20aa9SJames Smart }
23847ad20aa9SJames Smart 
23857ad20aa9SJames Smart /**
23867ad20aa9SJames Smart  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
23877ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
23887ad20aa9SJames Smart  *
23897ad20aa9SJames Smart  * This function is to perform SLI4 diag link test request from the user
23907ad20aa9SJames Smart  * applicaiton.
23917ad20aa9SJames Smart  */
23927ad20aa9SJames Smart static int
239375cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
23947ad20aa9SJames Smart {
239501e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
239601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
23977ad20aa9SJames Smart 	struct Scsi_Host *shost;
23987ad20aa9SJames Smart 	struct lpfc_vport *vport;
23997ad20aa9SJames Smart 	struct lpfc_hba *phba;
24007ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
24017ad20aa9SJames Smart 	struct sli4_link_diag *link_diag_test_cmd;
24027ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
24037ad20aa9SJames Smart 	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
24047ad20aa9SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
24057ad20aa9SJames Smart 	uint32_t shdr_status, shdr_add_status;
24067ad20aa9SJames Smart 	struct diag_status *diag_status_reply;
2407*e5fcb81dSDick Kennedy 	int mbxstatus, rc = -ENODEV, rc1 = 0;
24087ad20aa9SJames Smart 
2409cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
2410*e5fcb81dSDick Kennedy 	if (!shost)
24117ad20aa9SJames Smart 		goto job_error;
24127ad20aa9SJames Smart 
2413*e5fcb81dSDick Kennedy 	vport = shost_priv(shost);
2414*e5fcb81dSDick Kennedy 	if (!vport)
24157ad20aa9SJames Smart 		goto job_error;
2416*e5fcb81dSDick Kennedy 
2417*e5fcb81dSDick Kennedy 	phba = vport->phba;
2418*e5fcb81dSDick Kennedy 	if (!phba)
2419*e5fcb81dSDick Kennedy 		goto job_error;
2420*e5fcb81dSDick Kennedy 
2421*e5fcb81dSDick Kennedy 
2422*e5fcb81dSDick Kennedy 	if (phba->sli_rev < LPFC_SLI_REV4)
2423*e5fcb81dSDick Kennedy 		goto job_error;
2424*e5fcb81dSDick Kennedy 
2425719162bdSJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2426*e5fcb81dSDick Kennedy 	    LPFC_SLI_INTF_IF_TYPE_2)
24277ad20aa9SJames Smart 		goto job_error;
24287ad20aa9SJames Smart 
24297ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
24307ad20aa9SJames Smart 	    sizeof(struct sli4_link_diag)) {
24317ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
24327ad20aa9SJames Smart 				"3013 Received LINK DIAG TEST request "
24337ad20aa9SJames Smart 				" size:%d below the minimum size:%d\n",
24347ad20aa9SJames Smart 				job->request_len,
24357ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
24367ad20aa9SJames Smart 				sizeof(struct sli4_link_diag)));
24377ad20aa9SJames Smart 		rc = -EINVAL;
24387ad20aa9SJames Smart 		goto job_error;
24397ad20aa9SJames Smart 	}
24407ad20aa9SJames Smart 
244188a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
24427ad20aa9SJames Smart 	if (rc)
24437ad20aa9SJames Smart 		goto job_error;
24447ad20aa9SJames Smart 
24457ad20aa9SJames Smart 	link_diag_test_cmd = (struct sli4_link_diag *)
244601e0e15cSJohannes Thumshirn 			 bsg_request->rqst_data.h_vendor.vendor_cmd;
24477ad20aa9SJames Smart 
24487ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
24497ad20aa9SJames Smart 
24507ad20aa9SJames Smart 	if (rc)
24517ad20aa9SJames Smart 		goto job_error;
24527ad20aa9SJames Smart 
24537ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
245444ed33e6SGustavo A. R. Silva 	if (!pmboxq)
24557ad20aa9SJames Smart 		goto link_diag_test_exit;
24567ad20aa9SJames Smart 
24577ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
24587ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
24597ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
24607ad20aa9SJames Smart 				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
24617ad20aa9SJames Smart 				     req_len, LPFC_SLI4_MBX_EMBED);
2462*e5fcb81dSDick Kennedy 	if (alloc_len != req_len) {
2463*e5fcb81dSDick Kennedy 		rc = -ENOMEM;
24647ad20aa9SJames Smart 		goto link_diag_test_exit;
2465*e5fcb81dSDick Kennedy 	}
246644ed33e6SGustavo A. R. Silva 
24677ad20aa9SJames Smart 	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
24687ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
24691b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
24707ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
24711b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
24727ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
24737ad20aa9SJames Smart 	       link_diag_test_cmd->test_id);
24747ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
24757ad20aa9SJames Smart 	       link_diag_test_cmd->loops);
24767ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
24777ad20aa9SJames Smart 	       link_diag_test_cmd->test_version);
24787ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
24797ad20aa9SJames Smart 	       link_diag_test_cmd->error_action);
24807ad20aa9SJames Smart 
24817ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
24827ad20aa9SJames Smart 
24837ad20aa9SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)
24847ad20aa9SJames Smart 		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
24857ad20aa9SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
24867ad20aa9SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
24877ad20aa9SJames Smart 	if (shdr_status || shdr_add_status || mbxstatus) {
24887ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
24897ad20aa9SJames Smart 				"3010 Run link diag test mailbox failed with "
24907ad20aa9SJames Smart 				"mbx_status x%x status x%x, add_status x%x\n",
24917ad20aa9SJames Smart 				mbxstatus, shdr_status, shdr_add_status);
24927ad20aa9SJames Smart 	}
24937ad20aa9SJames Smart 
24947ad20aa9SJames Smart 	diag_status_reply = (struct diag_status *)
249501e0e15cSJohannes Thumshirn 			    bsg_reply->reply_data.vendor_reply.vendor_rsp;
24967ad20aa9SJames Smart 
24977ad20aa9SJames Smart 	if (job->reply_len <
24987ad20aa9SJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
24997ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
25007ad20aa9SJames Smart 				"3012 Received Run link diag test reply "
25017ad20aa9SJames Smart 				"below minimum size (%d): reply_len:%d\n",
25027ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
25037ad20aa9SJames Smart 				sizeof(struct diag_status)),
25047ad20aa9SJames Smart 				job->reply_len);
25057ad20aa9SJames Smart 		rc = -EINVAL;
25067ad20aa9SJames Smart 		goto job_error;
25077ad20aa9SJames Smart 	}
25087ad20aa9SJames Smart 
25097ad20aa9SJames Smart 	diag_status_reply->mbox_status = mbxstatus;
25107ad20aa9SJames Smart 	diag_status_reply->shdr_status = shdr_status;
25117ad20aa9SJames Smart 	diag_status_reply->shdr_add_status = shdr_add_status;
25127ad20aa9SJames Smart 
25137ad20aa9SJames Smart link_diag_test_exit:
2514*e5fcb81dSDick Kennedy 	rc1 = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
25157ad20aa9SJames Smart 
25167ad20aa9SJames Smart 	if (pmboxq)
25177ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
25187ad20aa9SJames Smart 
25197ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
25207ad20aa9SJames Smart 
25217ad20aa9SJames Smart job_error:
25227ad20aa9SJames Smart 	/* make error code available to userspace */
2523*e5fcb81dSDick Kennedy 	if (rc1 && !rc)
2524*e5fcb81dSDick Kennedy 		rc = rc1;
252501e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
25267ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
25277ad20aa9SJames Smart 	if (rc == 0)
252806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
25291abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
25307ad20aa9SJames Smart 	return rc;
25317ad20aa9SJames Smart }
25327ad20aa9SJames Smart 
25337ad20aa9SJames Smart /**
25343b5dd52aSJames Smart  * lpfcdiag_loop_self_reg - obtains a remote port login id
25353b5dd52aSJames Smart  * @phba: Pointer to HBA context object
25363b5dd52aSJames Smart  * @rpi: Pointer to a remote port login id
25373b5dd52aSJames Smart  *
25383b5dd52aSJames Smart  * This function obtains a remote port login id so the diag loopback test
25393b5dd52aSJames Smart  * can send and receive its own unsolicited CT command.
25403b5dd52aSJames Smart  **/
25413b5dd52aSJames Smart static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
25423b5dd52aSJames Smart {
25433b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
25443b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuff;
25453b5dd52aSJames Smart 	int status;
25463b5dd52aSJames Smart 
25473b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
25483b5dd52aSJames Smart 	if (!mbox)
2549d439d286SJames Smart 		return -ENOMEM;
25503b5dd52aSJames Smart 
25511b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
25523b5dd52aSJames Smart 		status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
25531b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
25541b51197dSJames Smart 				mbox, *rpi);
25551b51197dSJames Smart 	else {
25561b51197dSJames Smart 		*rpi = lpfc_sli4_alloc_rpi(phba);
25579d3d340dSJames Smart 		if (*rpi == LPFC_RPI_ALLOC_ERROR) {
25589d3d340dSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
25599d3d340dSJames Smart 			return -EBUSY;
25609d3d340dSJames Smart 		}
25611b51197dSJames Smart 		status = lpfc_reg_rpi(phba, phba->pport->vpi,
25621b51197dSJames Smart 				phba->pport->fc_myDID,
25631b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
25641b51197dSJames Smart 				mbox, *rpi);
25651b51197dSJames Smart 	}
25661b51197dSJames Smart 
25673b5dd52aSJames Smart 	if (status) {
25683b5dd52aSJames Smart 		mempool_free(mbox, phba->mbox_mem_pool);
25694042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
25704042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2571d439d286SJames Smart 		return -ENOMEM;
25723b5dd52aSJames Smart 	}
25733b5dd52aSJames Smart 
25743e1f0718SJames Smart 	dmabuff = (struct lpfc_dmabuf *)mbox->ctx_buf;
25753e1f0718SJames Smart 	mbox->ctx_buf = NULL;
25763e1f0718SJames Smart 	mbox->ctx_ndlp = NULL;
25773b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
25783b5dd52aSJames Smart 
25793b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
25803b5dd52aSJames Smart 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
25813b5dd52aSJames Smart 		kfree(dmabuff);
25823b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
25833b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
25844042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
25854042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2586d439d286SJames Smart 		return -ENODEV;
25873b5dd52aSJames Smart 	}
25883b5dd52aSJames Smart 
25891b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
25903b5dd52aSJames Smart 		*rpi = mbox->u.mb.un.varWords[0];
25913b5dd52aSJames Smart 
25923b5dd52aSJames Smart 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
25933b5dd52aSJames Smart 	kfree(dmabuff);
25943b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
25953b5dd52aSJames Smart 	return 0;
25963b5dd52aSJames Smart }
25973b5dd52aSJames Smart 
25983b5dd52aSJames Smart /**
25993b5dd52aSJames Smart  * lpfcdiag_loop_self_unreg - unregs from the rpi
26003b5dd52aSJames Smart  * @phba: Pointer to HBA context object
26013b5dd52aSJames Smart  * @rpi: Remote port login id
26023b5dd52aSJames Smart  *
26033b5dd52aSJames Smart  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
26043b5dd52aSJames Smart  **/
26053b5dd52aSJames Smart static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
26063b5dd52aSJames Smart {
26073b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
26083b5dd52aSJames Smart 	int status;
26093b5dd52aSJames Smart 
26103b5dd52aSJames Smart 	/* Allocate mboxq structure */
26113b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
26123b5dd52aSJames Smart 	if (mbox == NULL)
2613d439d286SJames Smart 		return -ENOMEM;
26143b5dd52aSJames Smart 
26151b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
26163b5dd52aSJames Smart 		lpfc_unreg_login(phba, 0, rpi, mbox);
26171b51197dSJames Smart 	else
26181b51197dSJames Smart 		lpfc_unreg_login(phba, phba->pport->vpi,
26191b51197dSJames Smart 				 phba->sli4_hba.rpi_ids[rpi], mbox);
26201b51197dSJames Smart 
26213b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
26223b5dd52aSJames Smart 
26233b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
26243b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
26253b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
2626d439d286SJames Smart 		return -EIO;
26273b5dd52aSJames Smart 	}
26283b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
26294042629eSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
26304042629eSJames Smart 		lpfc_sli4_free_rpi(phba, rpi);
26313b5dd52aSJames Smart 	return 0;
26323b5dd52aSJames Smart }
26333b5dd52aSJames Smart 
26343b5dd52aSJames Smart /**
26353b5dd52aSJames Smart  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
26363b5dd52aSJames Smart  * @phba: Pointer to HBA context object
26373b5dd52aSJames Smart  * @rpi: Remote port login id
26383b5dd52aSJames Smart  * @txxri: Pointer to transmit exchange id
26393b5dd52aSJames Smart  * @rxxri: Pointer to response exchabge id
26403b5dd52aSJames Smart  *
26413b5dd52aSJames Smart  * This function obtains the transmit and receive ids required to send
26423b5dd52aSJames Smart  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
26433b5dd52aSJames Smart  * flags are used to the unsolicted response handler is able to process
26443b5dd52aSJames Smart  * the ct command sent on the same port.
26453b5dd52aSJames Smart  **/
26463b5dd52aSJames Smart static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
26473b5dd52aSJames Smart 			 uint16_t *txxri, uint16_t * rxxri)
26483b5dd52aSJames Smart {
26493b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
26503b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
26513b5dd52aSJames Smart 	IOCB_t *cmd, *rsp;
26523b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuf;
26533b5dd52aSJames Smart 	struct ulp_bde64 *bpl = NULL;
26543b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq = NULL;
26553b5dd52aSJames Smart 	int ret_val = 0;
2656d439d286SJames Smart 	int time_left;
26575a0916b4SJames Smart 	int iocb_stat = IOCB_SUCCESS;
26583b5dd52aSJames Smart 	unsigned long flags;
26593b5dd52aSJames Smart 
26603b5dd52aSJames Smart 	*txxri = 0;
26613b5dd52aSJames Smart 	*rxxri = 0;
26623b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
26633b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
26643b5dd52aSJames Smart 	if (!evt)
2665d439d286SJames Smart 		return -ENOMEM;
26663b5dd52aSJames Smart 
26673b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
26683b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
26693b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
26703b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26713b5dd52aSJames Smart 
26723b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
26733b5dd52aSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
26743b5dd52aSJames Smart 
26753b5dd52aSJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
26763b5dd52aSJames Smart 	if (dmabuf) {
26773b5dd52aSJames Smart 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2678c7495937SJames Smart 		if (dmabuf->virt) {
26793b5dd52aSJames Smart 			INIT_LIST_HEAD(&dmabuf->list);
26803b5dd52aSJames Smart 			bpl = (struct ulp_bde64 *) dmabuf->virt;
26813b5dd52aSJames Smart 			memset(bpl, 0, sizeof(*bpl));
26823b5dd52aSJames Smart 			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
26833b5dd52aSJames Smart 			bpl->addrHigh =
2684c7495937SJames Smart 				le32_to_cpu(putPaddrHigh(dmabuf->phys +
2685c7495937SJames Smart 					sizeof(*bpl)));
26863b5dd52aSJames Smart 			bpl->addrLow =
2687c7495937SJames Smart 				le32_to_cpu(putPaddrLow(dmabuf->phys +
2688c7495937SJames Smart 					sizeof(*bpl)));
26893b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
26903b5dd52aSJames Smart 			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
26913b5dd52aSJames Smart 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
26923b5dd52aSJames Smart 		}
2693c7495937SJames Smart 	}
26943b5dd52aSJames Smart 
26953b5dd52aSJames Smart 	if (cmdiocbq == NULL || rspiocbq == NULL ||
2696c7495937SJames Smart 	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2697c7495937SJames Smart 		dmabuf->virt == NULL) {
2698d439d286SJames Smart 		ret_val = -ENOMEM;
26993b5dd52aSJames Smart 		goto err_get_xri_exit;
27003b5dd52aSJames Smart 	}
27013b5dd52aSJames Smart 
27023b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
27033b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
27043b5dd52aSJames Smart 
27053b5dd52aSJames Smart 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
27063b5dd52aSJames Smart 
27073b5dd52aSJames Smart 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
27083b5dd52aSJames Smart 	ctreq->RevisionId.bits.InId = 0;
27093b5dd52aSJames Smart 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
27103b5dd52aSJames Smart 	ctreq->FsSubType = 0;
27113b5dd52aSJames Smart 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
27123b5dd52aSJames Smart 	ctreq->CommandResponse.bits.Size = 0;
27133b5dd52aSJames Smart 
27143b5dd52aSJames Smart 
27153b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
27163b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
27173b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
27183b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
27193b5dd52aSJames Smart 
27203b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = LA;
27213b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
27223b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
27233b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
27243b5dd52aSJames Smart 
27253b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
27263b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
27273b5dd52aSJames Smart 	cmd->ulpLe = 1;
27283b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
27293b5dd52aSJames Smart 	cmd->ulpContext = rpi;
27303b5dd52aSJames Smart 
27313b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
27323b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
27335a0916b4SJames Smart 	cmdiocbq->iocb_cmpl = NULL;
27343b5dd52aSJames Smart 
2735d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
27363b5dd52aSJames Smart 				rspiocbq,
27373b5dd52aSJames Smart 				(phba->fc_ratov * 2)
27383b5dd52aSJames Smart 				+ LPFC_DRVR_TIMEOUT);
273953151bbbSJames Smart 	if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
2740d439d286SJames Smart 		ret_val = -EIO;
27413b5dd52aSJames Smart 		goto err_get_xri_exit;
2742d439d286SJames Smart 	}
27433b5dd52aSJames Smart 	*txxri =  rsp->ulpContext;
27443b5dd52aSJames Smart 
27453b5dd52aSJames Smart 	evt->waiting = 1;
27463b5dd52aSJames Smart 	evt->wait_time_stamp = jiffies;
2747d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
27483b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
2749256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
2750256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
27513b5dd52aSJames Smart 	if (list_empty(&evt->events_to_see))
2752d439d286SJames Smart 		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
27533b5dd52aSJames Smart 	else {
27543b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
27553b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
27563b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
27573b5dd52aSJames Smart 		*rxxri = (list_entry(evt->events_to_get.prev,
27583b5dd52aSJames Smart 				     typeof(struct event_data),
27593b5dd52aSJames Smart 				     node))->immed_dat;
27603b5dd52aSJames Smart 	}
27613b5dd52aSJames Smart 	evt->waiting = 0;
27623b5dd52aSJames Smart 
27633b5dd52aSJames Smart err_get_xri_exit:
27643b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
27653b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
27663b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
27673b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
27683b5dd52aSJames Smart 
27693b5dd52aSJames Smart 	if (dmabuf) {
27703b5dd52aSJames Smart 		if (dmabuf->virt)
27713b5dd52aSJames Smart 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
27723b5dd52aSJames Smart 		kfree(dmabuf);
27733b5dd52aSJames Smart 	}
27743b5dd52aSJames Smart 
2775d439d286SJames Smart 	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
27763b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
27773b5dd52aSJames Smart 	if (rspiocbq)
27783b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
27793b5dd52aSJames Smart 	return ret_val;
27803b5dd52aSJames Smart }
27813b5dd52aSJames Smart 
27823b5dd52aSJames Smart /**
27837ad20aa9SJames Smart  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
27847ad20aa9SJames Smart  * @phba: Pointer to HBA context object
27857ad20aa9SJames Smart  *
27862ea259eeSJames Smart  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
27879e03aa2fSJoe Perches  * returns the pointer to the buffer.
27887ad20aa9SJames Smart  **/
27897ad20aa9SJames Smart static struct lpfc_dmabuf *
27907ad20aa9SJames Smart lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
27917ad20aa9SJames Smart {
27927ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
27937ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
27947ad20aa9SJames Smart 
27957ad20aa9SJames Smart 	/* allocate dma buffer struct */
27967ad20aa9SJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
27977ad20aa9SJames Smart 	if (!dmabuf)
27987ad20aa9SJames Smart 		return NULL;
27997ad20aa9SJames Smart 
28007ad20aa9SJames Smart 	INIT_LIST_HEAD(&dmabuf->list);
28017ad20aa9SJames Smart 
28027ad20aa9SJames Smart 	/* now, allocate dma buffer */
2803750afb08SLuis Chamberlain 	dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
28047ad20aa9SJames Smart 					  &(dmabuf->phys), GFP_KERNEL);
28057ad20aa9SJames Smart 
28067ad20aa9SJames Smart 	if (!dmabuf->virt) {
28077ad20aa9SJames Smart 		kfree(dmabuf);
28087ad20aa9SJames Smart 		return NULL;
28097ad20aa9SJames Smart 	}
28107ad20aa9SJames Smart 
28117ad20aa9SJames Smart 	return dmabuf;
28127ad20aa9SJames Smart }
28137ad20aa9SJames Smart 
28147ad20aa9SJames Smart /**
28157ad20aa9SJames Smart  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
28167ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
28177ad20aa9SJames Smart  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
28187ad20aa9SJames Smart  *
28197ad20aa9SJames Smart  * This routine just simply frees a dma buffer and its associated buffer
28207ad20aa9SJames Smart  * descriptor referred by @dmabuf.
28217ad20aa9SJames Smart  **/
28227ad20aa9SJames Smart static void
28237ad20aa9SJames Smart lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
28247ad20aa9SJames Smart {
28257ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
28267ad20aa9SJames Smart 
28277ad20aa9SJames Smart 	if (!dmabuf)
28287ad20aa9SJames Smart 		return;
28297ad20aa9SJames Smart 
28307ad20aa9SJames Smart 	if (dmabuf->virt)
28317ad20aa9SJames Smart 		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
28327ad20aa9SJames Smart 				  dmabuf->virt, dmabuf->phys);
28337ad20aa9SJames Smart 	kfree(dmabuf);
28347ad20aa9SJames Smart 	return;
28357ad20aa9SJames Smart }
28367ad20aa9SJames Smart 
28377ad20aa9SJames Smart /**
28387ad20aa9SJames Smart  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
28397ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
28407ad20aa9SJames Smart  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
28417ad20aa9SJames Smart  *
28427ad20aa9SJames Smart  * This routine just simply frees all dma buffers and their associated buffer
28437ad20aa9SJames Smart  * descriptors referred by @dmabuf_list.
28447ad20aa9SJames Smart  **/
28457ad20aa9SJames Smart static void
28467ad20aa9SJames Smart lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
28477ad20aa9SJames Smart 			    struct list_head *dmabuf_list)
28487ad20aa9SJames Smart {
28497ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
28507ad20aa9SJames Smart 
28517ad20aa9SJames Smart 	if (list_empty(dmabuf_list))
28527ad20aa9SJames Smart 		return;
28537ad20aa9SJames Smart 
28547ad20aa9SJames Smart 	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
28557ad20aa9SJames Smart 		list_del_init(&dmabuf->list);
28567ad20aa9SJames Smart 		lpfc_bsg_dma_page_free(phba, dmabuf);
28577ad20aa9SJames Smart 	}
28587ad20aa9SJames Smart 	return;
28597ad20aa9SJames Smart }
28607ad20aa9SJames Smart 
28617ad20aa9SJames Smart /**
28623b5dd52aSJames Smart  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
28633b5dd52aSJames Smart  * @phba: Pointer to HBA context object
28643b5dd52aSJames Smart  * @bpl: Pointer to 64 bit bde structure
28653b5dd52aSJames Smart  * @size: Number of bytes to process
28663b5dd52aSJames Smart  * @nocopydata: Flag to copy user data into the allocated buffer
28673b5dd52aSJames Smart  *
28683b5dd52aSJames Smart  * This function allocates page size buffers and populates an lpfc_dmabufext.
28693b5dd52aSJames Smart  * If allowed the user data pointed to with indataptr is copied into the kernel
28703b5dd52aSJames Smart  * memory. The chained list of page size buffers is returned.
28713b5dd52aSJames Smart  **/
28723b5dd52aSJames Smart static struct lpfc_dmabufext *
28733b5dd52aSJames Smart diag_cmd_data_alloc(struct lpfc_hba *phba,
28743b5dd52aSJames Smart 		   struct ulp_bde64 *bpl, uint32_t size,
28753b5dd52aSJames Smart 		   int nocopydata)
28763b5dd52aSJames Smart {
28773b5dd52aSJames Smart 	struct lpfc_dmabufext *mlist = NULL;
28783b5dd52aSJames Smart 	struct lpfc_dmabufext *dmp;
28793b5dd52aSJames Smart 	int cnt, offset = 0, i = 0;
28803b5dd52aSJames Smart 	struct pci_dev *pcidev;
28813b5dd52aSJames Smart 
28823b5dd52aSJames Smart 	pcidev = phba->pcidev;
28833b5dd52aSJames Smart 
28843b5dd52aSJames Smart 	while (size) {
28853b5dd52aSJames Smart 		/* We get chunks of 4K */
28863b5dd52aSJames Smart 		if (size > BUF_SZ_4K)
28873b5dd52aSJames Smart 			cnt = BUF_SZ_4K;
28883b5dd52aSJames Smart 		else
28893b5dd52aSJames Smart 			cnt = size;
28903b5dd52aSJames Smart 
28913b5dd52aSJames Smart 		/* allocate struct lpfc_dmabufext buffer header */
28923b5dd52aSJames Smart 		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
28933b5dd52aSJames Smart 		if (!dmp)
28943b5dd52aSJames Smart 			goto out;
28953b5dd52aSJames Smart 
28963b5dd52aSJames Smart 		INIT_LIST_HEAD(&dmp->dma.list);
28973b5dd52aSJames Smart 
28983b5dd52aSJames Smart 		/* Queue it to a linked list */
28993b5dd52aSJames Smart 		if (mlist)
29003b5dd52aSJames Smart 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
29013b5dd52aSJames Smart 		else
29023b5dd52aSJames Smart 			mlist = dmp;
29033b5dd52aSJames Smart 
29043b5dd52aSJames Smart 		/* allocate buffer */
29053b5dd52aSJames Smart 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
29063b5dd52aSJames Smart 						   cnt,
29073b5dd52aSJames Smart 						   &(dmp->dma.phys),
29083b5dd52aSJames Smart 						   GFP_KERNEL);
29093b5dd52aSJames Smart 
29103b5dd52aSJames Smart 		if (!dmp->dma.virt)
29113b5dd52aSJames Smart 			goto out;
29123b5dd52aSJames Smart 
29133b5dd52aSJames Smart 		dmp->size = cnt;
29143b5dd52aSJames Smart 
29153b5dd52aSJames Smart 		if (nocopydata) {
29163b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
29173b5dd52aSJames Smart 		} else {
29183b5dd52aSJames Smart 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
29193b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
29203b5dd52aSJames Smart 		}
29213b5dd52aSJames Smart 
29223b5dd52aSJames Smart 		/* build buffer ptr list for IOCB */
29233b5dd52aSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
29243b5dd52aSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
29253b5dd52aSJames Smart 		bpl->tus.f.bdeSize = (ushort) cnt;
29263b5dd52aSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
29273b5dd52aSJames Smart 		bpl++;
29283b5dd52aSJames Smart 
29293b5dd52aSJames Smart 		i++;
29303b5dd52aSJames Smart 		offset += cnt;
29313b5dd52aSJames Smart 		size -= cnt;
29323b5dd52aSJames Smart 	}
29333b5dd52aSJames Smart 
2934a2fc4aefSJames Smart 	if (mlist) {
29353b5dd52aSJames Smart 		mlist->flag = i;
29363b5dd52aSJames Smart 		return mlist;
2937a2fc4aefSJames Smart 	}
29383b5dd52aSJames Smart out:
29393b5dd52aSJames Smart 	diag_cmd_data_free(phba, mlist);
29403b5dd52aSJames Smart 	return NULL;
29413b5dd52aSJames Smart }
29423b5dd52aSJames Smart 
29433b5dd52aSJames Smart /**
29443b5dd52aSJames Smart  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
29453b5dd52aSJames Smart  * @phba: Pointer to HBA context object
29463b5dd52aSJames Smart  * @rxxri: Receive exchange id
29473b5dd52aSJames Smart  * @len: Number of data bytes
29483b5dd52aSJames Smart  *
294925985edcSLucas De Marchi  * This function allocates and posts a data buffer of sufficient size to receive
29503b5dd52aSJames Smart  * an unsolicted CT command.
29513b5dd52aSJames Smart  **/
29523b5dd52aSJames Smart static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
29533b5dd52aSJames Smart 			     size_t len)
29543b5dd52aSJames Smart {
2955895427bdSJames Smart 	struct lpfc_sli_ring *pring;
29563b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq;
29573b5dd52aSJames Smart 	IOCB_t *cmd = NULL;
29583b5dd52aSJames Smart 	struct list_head head, *curr, *next;
29593b5dd52aSJames Smart 	struct lpfc_dmabuf *rxbmp;
29603b5dd52aSJames Smart 	struct lpfc_dmabuf *dmp;
29613b5dd52aSJames Smart 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
29623b5dd52aSJames Smart 	struct ulp_bde64 *rxbpl = NULL;
29633b5dd52aSJames Smart 	uint32_t num_bde;
29643b5dd52aSJames Smart 	struct lpfc_dmabufext *rxbuffer = NULL;
29653b5dd52aSJames Smart 	int ret_val = 0;
2966d439d286SJames Smart 	int iocb_stat;
29673b5dd52aSJames Smart 	int i = 0;
29683b5dd52aSJames Smart 
2969895427bdSJames Smart 	pring = lpfc_phba_elsring(phba);
2970895427bdSJames Smart 
29713b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
29723b5dd52aSJames Smart 	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
29733b5dd52aSJames Smart 	if (rxbmp != NULL) {
29743b5dd52aSJames Smart 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2975c7495937SJames Smart 		if (rxbmp->virt) {
29763b5dd52aSJames Smart 			INIT_LIST_HEAD(&rxbmp->list);
29773b5dd52aSJames Smart 			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
29783b5dd52aSJames Smart 			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
29793b5dd52aSJames Smart 		}
2980c7495937SJames Smart 	}
29813b5dd52aSJames Smart 
29821234a6d5SDick Kennedy 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2983d439d286SJames Smart 		ret_val = -ENOMEM;
29843b5dd52aSJames Smart 		goto err_post_rxbufs_exit;
29853b5dd52aSJames Smart 	}
29863b5dd52aSJames Smart 
29873b5dd52aSJames Smart 	/* Queue buffers for the receive exchange */
29883b5dd52aSJames Smart 	num_bde = (uint32_t)rxbuffer->flag;
29893b5dd52aSJames Smart 	dmp = &rxbuffer->dma;
29903b5dd52aSJames Smart 
29913b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
29923b5dd52aSJames Smart 	i = 0;
29933b5dd52aSJames Smart 
29943b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
29953b5dd52aSJames Smart 	list_add_tail(&head, &dmp->list);
29963b5dd52aSJames Smart 	list_for_each_safe(curr, next, &head) {
29973b5dd52aSJames Smart 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
29983b5dd52aSJames Smart 		list_del(curr);
29993b5dd52aSJames Smart 
30003b5dd52aSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
30013b5dd52aSJames Smart 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
30023b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrHigh =
30033b5dd52aSJames Smart 				putPaddrHigh(mp[i]->phys);
30043b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrLow =
30053b5dd52aSJames Smart 				putPaddrLow(mp[i]->phys);
30063b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
30073b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
30083b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
30093b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
30103b5dd52aSJames Smart 			cmd->ulpPU = 0;
30113b5dd52aSJames Smart 			cmd->ulpLe = 1;
30123b5dd52aSJames Smart 			cmd->ulpBdeCount = 1;
30133b5dd52aSJames Smart 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
30143b5dd52aSJames Smart 
30153b5dd52aSJames Smart 		} else {
30163b5dd52aSJames Smart 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
30173b5dd52aSJames Smart 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
30183b5dd52aSJames Smart 			cmd->un.cont64[i].tus.f.bdeSize =
30193b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
30203b5dd52aSJames Smart 			cmd->ulpBdeCount = ++i;
30213b5dd52aSJames Smart 
30223b5dd52aSJames Smart 			if ((--num_bde > 0) && (i < 2))
30233b5dd52aSJames Smart 				continue;
30243b5dd52aSJames Smart 
30253b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
30263b5dd52aSJames Smart 			cmd->ulpLe = 1;
30273b5dd52aSJames Smart 		}
30283b5dd52aSJames Smart 
30293b5dd52aSJames Smart 		cmd->ulpClass = CLASS3;
30303b5dd52aSJames Smart 		cmd->ulpContext = rxxri;
30313b5dd52aSJames Smart 
3032d439d286SJames Smart 		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
3033d439d286SJames Smart 						0);
3034d439d286SJames Smart 		if (iocb_stat == IOCB_ERROR) {
30353b5dd52aSJames Smart 			diag_cmd_data_free(phba,
30363b5dd52aSJames Smart 				(struct lpfc_dmabufext *)mp[0]);
30373b5dd52aSJames Smart 			if (mp[1])
30383b5dd52aSJames Smart 				diag_cmd_data_free(phba,
30393b5dd52aSJames Smart 					  (struct lpfc_dmabufext *)mp[1]);
30403b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
3041d439d286SJames Smart 			ret_val = -EIO;
30423b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
30433b5dd52aSJames Smart 		}
30443b5dd52aSJames Smart 
30453b5dd52aSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
30463b5dd52aSJames Smart 		if (mp[1]) {
30473b5dd52aSJames Smart 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
30483b5dd52aSJames Smart 			mp[1] = NULL;
30493b5dd52aSJames Smart 		}
30503b5dd52aSJames Smart 
30513b5dd52aSJames Smart 		/* The iocb was freed by lpfc_sli_issue_iocb */
30523b5dd52aSJames Smart 		cmdiocbq = lpfc_sli_get_iocbq(phba);
30533b5dd52aSJames Smart 		if (!cmdiocbq) {
30543b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
3055d439d286SJames Smart 			ret_val = -EIO;
30563b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
30573b5dd52aSJames Smart 		}
30583b5dd52aSJames Smart 
30593b5dd52aSJames Smart 		cmd = &cmdiocbq->iocb;
30603b5dd52aSJames Smart 		i = 0;
30613b5dd52aSJames Smart 	}
30623b5dd52aSJames Smart 	list_del(&head);
30633b5dd52aSJames Smart 
30643b5dd52aSJames Smart err_post_rxbufs_exit:
30653b5dd52aSJames Smart 
30663b5dd52aSJames Smart 	if (rxbmp) {
30673b5dd52aSJames Smart 		if (rxbmp->virt)
30683b5dd52aSJames Smart 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
30693b5dd52aSJames Smart 		kfree(rxbmp);
30703b5dd52aSJames Smart 	}
30713b5dd52aSJames Smart 
30723b5dd52aSJames Smart 	if (cmdiocbq)
30733b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
30743b5dd52aSJames Smart 	return ret_val;
30753b5dd52aSJames Smart }
30763b5dd52aSJames Smart 
30773b5dd52aSJames Smart /**
30787ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
30793b5dd52aSJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
30803b5dd52aSJames Smart  *
30813b5dd52aSJames Smart  * This function receives a user data buffer to be transmitted and received on
30823b5dd52aSJames Smart  * the same port, the link must be up and in loopback mode prior
30833b5dd52aSJames Smart  * to being called.
30843b5dd52aSJames Smart  * 1. A kernel buffer is allocated to copy the user data into.
30853b5dd52aSJames Smart  * 2. The port registers with "itself".
30863b5dd52aSJames Smart  * 3. The transmit and receive exchange ids are obtained.
30873b5dd52aSJames Smart  * 4. The receive exchange id is posted.
30883b5dd52aSJames Smart  * 5. A new els loopback event is created.
30893b5dd52aSJames Smart  * 6. The command and response iocbs are allocated.
30903b5dd52aSJames Smart  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
30913b5dd52aSJames Smart  *
30923b5dd52aSJames Smart  * This function is meant to be called n times while the port is in loopback
30933b5dd52aSJames Smart  * so it is the apps responsibility to issue a reset to take the port out
30943b5dd52aSJames Smart  * of loopback mode.
30953b5dd52aSJames Smart  **/
30963b5dd52aSJames Smart static int
309775cc8cfcSJohannes Thumshirn lpfc_bsg_diag_loopback_run(struct bsg_job *job)
30983b5dd52aSJames Smart {
3099cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
310001e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
31013b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
31023b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
31033b5dd52aSJames Smart 	struct event_data *evdat;
31043b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
31053b5dd52aSJames Smart 	uint32_t size;
31063b5dd52aSJames Smart 	uint32_t full_size;
31073b5dd52aSJames Smart 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
31084042629eSJames Smart 	uint16_t rpi = 0;
31091b51197dSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
31101b51197dSJames Smart 	IOCB_t *cmd, *rsp = NULL;
31113b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq;
31123b5dd52aSJames Smart 	struct lpfc_dmabuf *txbmp;
31133b5dd52aSJames Smart 	struct ulp_bde64 *txbpl = NULL;
31143b5dd52aSJames Smart 	struct lpfc_dmabufext *txbuffer = NULL;
31153b5dd52aSJames Smart 	struct list_head head;
31163b5dd52aSJames Smart 	struct lpfc_dmabuf  *curr;
31171b51197dSJames Smart 	uint16_t txxri = 0, rxxri;
31183b5dd52aSJames Smart 	uint32_t num_bde;
31193b5dd52aSJames Smart 	uint8_t *ptr = NULL, *rx_databuf = NULL;
31203b5dd52aSJames Smart 	int rc = 0;
3121d439d286SJames Smart 	int time_left;
31225a0916b4SJames Smart 	int iocb_stat = IOCB_SUCCESS;
31233b5dd52aSJames Smart 	unsigned long flags;
31243b5dd52aSJames Smart 	void *dataout = NULL;
31253b5dd52aSJames Smart 	uint32_t total_mem;
31263b5dd52aSJames Smart 
31273b5dd52aSJames Smart 	/* in case no data is returned return just the return code */
312801e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
31293b5dd52aSJames Smart 
31303b5dd52aSJames Smart 	if (job->request_len <
31313b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
31323b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
31333b5dd52aSJames Smart 				"2739 Received DIAG TEST request below minimum "
31343b5dd52aSJames Smart 				"size\n");
31353b5dd52aSJames Smart 		rc = -EINVAL;
31363b5dd52aSJames Smart 		goto loopback_test_exit;
31373b5dd52aSJames Smart 	}
31383b5dd52aSJames Smart 
31393b5dd52aSJames Smart 	if (job->request_payload.payload_len !=
31403b5dd52aSJames Smart 		job->reply_payload.payload_len) {
31413b5dd52aSJames Smart 		rc = -EINVAL;
31423b5dd52aSJames Smart 		goto loopback_test_exit;
31433b5dd52aSJames Smart 	}
31443b5dd52aSJames Smart 
31453b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
31463b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
31473b5dd52aSJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
31483b5dd52aSJames Smart 		rc = -EACCES;
31493b5dd52aSJames Smart 		goto loopback_test_exit;
31503b5dd52aSJames Smart 	}
31513b5dd52aSJames Smart 
31523b5dd52aSJames Smart 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
31533b5dd52aSJames Smart 		rc = -EACCES;
31543b5dd52aSJames Smart 		goto loopback_test_exit;
31553b5dd52aSJames Smart 	}
31563b5dd52aSJames Smart 
31573b5dd52aSJames Smart 	size = job->request_payload.payload_len;
31583b5dd52aSJames Smart 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
31593b5dd52aSJames Smart 
31603b5dd52aSJames Smart 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
31613b5dd52aSJames Smart 		rc = -ERANGE;
31623b5dd52aSJames Smart 		goto loopback_test_exit;
31633b5dd52aSJames Smart 	}
31643b5dd52aSJames Smart 
316563e801ceSJames Smart 	if (full_size >= BUF_SZ_4K) {
31663b5dd52aSJames Smart 		/*
31673b5dd52aSJames Smart 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
31683b5dd52aSJames Smart 		 * then we allocate 64k and re-use that buffer over and over to
31693b5dd52aSJames Smart 		 * xfer the whole block. This is because Linux kernel has a
31703b5dd52aSJames Smart 		 * problem allocating more than 120k of kernel space memory. Saw
31713b5dd52aSJames Smart 		 * problem with GET_FCPTARGETMAPPING...
31723b5dd52aSJames Smart 		 */
31733b5dd52aSJames Smart 		if (size <= (64 * 1024))
317463e801ceSJames Smart 			total_mem = full_size;
31753b5dd52aSJames Smart 		else
31763b5dd52aSJames Smart 			total_mem = 64 * 1024;
31773b5dd52aSJames Smart 	} else
31783b5dd52aSJames Smart 		/* Allocate memory for ioctl data */
31793b5dd52aSJames Smart 		total_mem = BUF_SZ_4K;
31803b5dd52aSJames Smart 
31813b5dd52aSJames Smart 	dataout = kmalloc(total_mem, GFP_KERNEL);
31823b5dd52aSJames Smart 	if (dataout == NULL) {
31833b5dd52aSJames Smart 		rc = -ENOMEM;
31843b5dd52aSJames Smart 		goto loopback_test_exit;
31853b5dd52aSJames Smart 	}
31863b5dd52aSJames Smart 
31873b5dd52aSJames Smart 	ptr = dataout;
31883b5dd52aSJames Smart 	ptr += ELX_LOOPBACK_HEADER_SZ;
31893b5dd52aSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
31903b5dd52aSJames Smart 				job->request_payload.sg_cnt,
31913b5dd52aSJames Smart 				ptr, size);
31923b5dd52aSJames Smart 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
3193d439d286SJames Smart 	if (rc)
31943b5dd52aSJames Smart 		goto loopback_test_exit;
31953b5dd52aSJames Smart 
31961b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
31973b5dd52aSJames Smart 		rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
31983b5dd52aSJames Smart 		if (rc) {
31993b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
32003b5dd52aSJames Smart 			goto loopback_test_exit;
32013b5dd52aSJames Smart 		}
32023b5dd52aSJames Smart 
32033b5dd52aSJames Smart 		rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
32043b5dd52aSJames Smart 		if (rc) {
32053b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
32063b5dd52aSJames Smart 			goto loopback_test_exit;
32073b5dd52aSJames Smart 		}
32081b51197dSJames Smart 	}
32093b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
32103b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
32113b5dd52aSJames Smart 	if (!evt) {
32123b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
32133b5dd52aSJames Smart 		rc = -ENOMEM;
32143b5dd52aSJames Smart 		goto loopback_test_exit;
32153b5dd52aSJames Smart 	}
32163b5dd52aSJames Smart 
32173b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
32183b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
32193b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
32203b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
32213b5dd52aSJames Smart 
32223b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
32231b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
32243b5dd52aSJames Smart 		rspiocbq = lpfc_sli_get_iocbq(phba);
32253b5dd52aSJames Smart 	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
32263b5dd52aSJames Smart 
32273b5dd52aSJames Smart 	if (txbmp) {
32283b5dd52aSJames Smart 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3229c7495937SJames Smart 		if (txbmp->virt) {
32303b5dd52aSJames Smart 			INIT_LIST_HEAD(&txbmp->list);
32313b5dd52aSJames Smart 			txbpl = (struct ulp_bde64 *) txbmp->virt;
32323b5dd52aSJames Smart 			txbuffer = diag_cmd_data_alloc(phba,
32333b5dd52aSJames Smart 							txbpl, full_size, 0);
32343b5dd52aSJames Smart 		}
3235c7495937SJames Smart 	}
32363b5dd52aSJames Smart 
32371b51197dSJames Smart 	if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
32381b51197dSJames Smart 		rc = -ENOMEM;
32391b51197dSJames Smart 		goto err_loopback_test_exit;
32401b51197dSJames Smart 	}
32411b51197dSJames Smart 	if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
32423b5dd52aSJames Smart 		rc = -ENOMEM;
32433b5dd52aSJames Smart 		goto err_loopback_test_exit;
32443b5dd52aSJames Smart 	}
32453b5dd52aSJames Smart 
32463b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
32471b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
32483b5dd52aSJames Smart 		rsp = &rspiocbq->iocb;
32493b5dd52aSJames Smart 
32503b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
32513b5dd52aSJames Smart 	list_add_tail(&head, &txbuffer->dma.list);
32523b5dd52aSJames Smart 	list_for_each_entry(curr, &head, list) {
32533b5dd52aSJames Smart 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
32543b5dd52aSJames Smart 		if (current_offset == 0) {
32553b5dd52aSJames Smart 			ctreq = curr->virt;
32563b5dd52aSJames Smart 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
32573b5dd52aSJames Smart 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
32583b5dd52aSJames Smart 			ctreq->RevisionId.bits.InId = 0;
32593b5dd52aSJames Smart 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
32603b5dd52aSJames Smart 			ctreq->FsSubType = 0;
32613b5dd52aSJames Smart 			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
32623b5dd52aSJames Smart 			ctreq->CommandResponse.bits.Size   = size;
32633b5dd52aSJames Smart 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
32643b5dd52aSJames Smart 		} else
32653b5dd52aSJames Smart 			segment_offset = 0;
32663b5dd52aSJames Smart 
32673b5dd52aSJames Smart 		BUG_ON(segment_offset >= segment_len);
32683b5dd52aSJames Smart 		memcpy(curr->virt + segment_offset,
32693b5dd52aSJames Smart 			ptr + current_offset,
32703b5dd52aSJames Smart 			segment_len - segment_offset);
32713b5dd52aSJames Smart 
32723b5dd52aSJames Smart 		current_offset += segment_len - segment_offset;
32733b5dd52aSJames Smart 		BUG_ON(current_offset > size);
32743b5dd52aSJames Smart 	}
32753b5dd52aSJames Smart 	list_del(&head);
32763b5dd52aSJames Smart 
32773b5dd52aSJames Smart 	/* Build the XMIT_SEQUENCE iocb */
32783b5dd52aSJames Smart 	num_bde = (uint32_t)txbuffer->flag;
32793b5dd52aSJames Smart 
32803b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
32813b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
32823b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
32833b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
32843b5dd52aSJames Smart 
32853b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
32863b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
32873b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
32883b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
32893b5dd52aSJames Smart 
32903b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
32913b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
32923b5dd52aSJames Smart 	cmd->ulpLe = 1;
32933b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
32943b5dd52aSJames Smart 
32951b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
32961b51197dSJames Smart 		cmd->ulpContext = txxri;
32971b51197dSJames Smart 	} else {
32981b51197dSJames Smart 		cmd->un.xseq64.bdl.ulpIoTag32 = 0;
32991b51197dSJames Smart 		cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
33001b51197dSJames Smart 		cmdiocbq->context3 = txbmp;
33011b51197dSJames Smart 		cmdiocbq->sli4_xritag = NO_XRI;
33021b51197dSJames Smart 		cmd->unsli3.rcvsli3.ox_id = 0xffff;
33031b51197dSJames Smart 	}
33043b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
33056c7cf486SJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LOOPBACK;
33063b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
33075a0916b4SJames Smart 	cmdiocbq->iocb_cmpl = NULL;
3308d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3309d439d286SJames Smart 					     rspiocbq, (phba->fc_ratov * 2) +
3310d439d286SJames Smart 					     LPFC_DRVR_TIMEOUT);
33113b5dd52aSJames Smart 
331253151bbbSJames Smart 	if ((iocb_stat != IOCB_SUCCESS) ||
331353151bbbSJames Smart 	    ((phba->sli_rev < LPFC_SLI_REV4) &&
331453151bbbSJames Smart 	     (rsp->ulpStatus != IOSTAT_SUCCESS))) {
33151b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
33161b51197dSJames Smart 				"3126 Failed loopback test issue iocb: "
33171b51197dSJames Smart 				"iocb_stat:x%x\n", iocb_stat);
33183b5dd52aSJames Smart 		rc = -EIO;
33193b5dd52aSJames Smart 		goto err_loopback_test_exit;
33203b5dd52aSJames Smart 	}
33213b5dd52aSJames Smart 
33223b5dd52aSJames Smart 	evt->waiting = 1;
3323d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
33243b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
3325256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
3326256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
33273b5dd52aSJames Smart 	evt->waiting = 0;
33281b51197dSJames Smart 	if (list_empty(&evt->events_to_see)) {
3329d439d286SJames Smart 		rc = (time_left) ? -EINTR : -ETIMEDOUT;
33301b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
33311b51197dSJames Smart 				"3125 Not receiving unsolicited event, "
33321b51197dSJames Smart 				"rc:x%x\n", rc);
33331b51197dSJames Smart 	} else {
33343b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
33353b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
33363b5dd52aSJames Smart 		evdat = list_entry(evt->events_to_get.prev,
33373b5dd52aSJames Smart 				   typeof(*evdat), node);
33383b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
33393b5dd52aSJames Smart 		rx_databuf = evdat->data;
33403b5dd52aSJames Smart 		if (evdat->len != full_size) {
33413b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
33423b5dd52aSJames Smart 				"1603 Loopback test did not receive expected "
33433b5dd52aSJames Smart 				"data length. actual length 0x%x expected "
33443b5dd52aSJames Smart 				"length 0x%x\n",
33453b5dd52aSJames Smart 				evdat->len, full_size);
33463b5dd52aSJames Smart 			rc = -EIO;
33473b5dd52aSJames Smart 		} else if (rx_databuf == NULL)
33483b5dd52aSJames Smart 			rc = -EIO;
33493b5dd52aSJames Smart 		else {
33503b5dd52aSJames Smart 			rc = IOCB_SUCCESS;
33513b5dd52aSJames Smart 			/* skip over elx loopback header */
33523b5dd52aSJames Smart 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
335301e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
33543b5dd52aSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
33553b5dd52aSJames Smart 						    job->reply_payload.sg_cnt,
33563b5dd52aSJames Smart 						    rx_databuf, size);
335701e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len = size;
33583b5dd52aSJames Smart 		}
33593b5dd52aSJames Smart 	}
33603b5dd52aSJames Smart 
33613b5dd52aSJames Smart err_loopback_test_exit:
33623b5dd52aSJames Smart 	lpfcdiag_loop_self_unreg(phba, rpi);
33633b5dd52aSJames Smart 
33643b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
33653b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
33663b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
33673b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
33683b5dd52aSJames Smart 
33695a0916b4SJames Smart 	if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
33703b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
33713b5dd52aSJames Smart 
33723b5dd52aSJames Smart 	if (rspiocbq != NULL)
33733b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
33743b5dd52aSJames Smart 
33753b5dd52aSJames Smart 	if (txbmp != NULL) {
33763b5dd52aSJames Smart 		if (txbpl != NULL) {
33773b5dd52aSJames Smart 			if (txbuffer != NULL)
33783b5dd52aSJames Smart 				diag_cmd_data_free(phba, txbuffer);
33793b5dd52aSJames Smart 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
33803b5dd52aSJames Smart 		}
33813b5dd52aSJames Smart 		kfree(txbmp);
33823b5dd52aSJames Smart 	}
33833b5dd52aSJames Smart 
33843b5dd52aSJames Smart loopback_test_exit:
33853b5dd52aSJames Smart 	kfree(dataout);
33863b5dd52aSJames Smart 	/* make error code available to userspace */
338701e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
33883b5dd52aSJames Smart 	job->dd_data = NULL;
33893b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
33901b51197dSJames Smart 	if (rc == IOCB_SUCCESS)
339106548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
33921abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
33933b5dd52aSJames Smart 	return rc;
33943b5dd52aSJames Smart }
33953b5dd52aSJames Smart 
33963b5dd52aSJames Smart /**
33973b5dd52aSJames Smart  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
33983b5dd52aSJames Smart  * @job: GET_DFC_REV fc_bsg_job
33993b5dd52aSJames Smart  **/
34003b5dd52aSJames Smart static int
340175cc8cfcSJohannes Thumshirn lpfc_bsg_get_dfc_rev(struct bsg_job *job)
34023b5dd52aSJames Smart {
3403cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
340401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
34053b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
34063b5dd52aSJames Smart 	struct get_mgmt_rev_reply *event_reply;
34073b5dd52aSJames Smart 	int rc = 0;
34083b5dd52aSJames Smart 
34093b5dd52aSJames Smart 	if (job->request_len <
34103b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
34113b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
34123b5dd52aSJames Smart 				"2740 Received GET_DFC_REV request below "
34133b5dd52aSJames Smart 				"minimum size\n");
34143b5dd52aSJames Smart 		rc = -EINVAL;
34153b5dd52aSJames Smart 		goto job_error;
34163b5dd52aSJames Smart 	}
34173b5dd52aSJames Smart 
34183b5dd52aSJames Smart 	event_reply = (struct get_mgmt_rev_reply *)
341901e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
34203b5dd52aSJames Smart 
34213b5dd52aSJames Smart 	if (job->reply_len <
34223b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
34233b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
34243b5dd52aSJames Smart 				"2741 Received GET_DFC_REV reply below "
34253b5dd52aSJames Smart 				"minimum size\n");
34263b5dd52aSJames Smart 		rc = -EINVAL;
34273b5dd52aSJames Smart 		goto job_error;
34283b5dd52aSJames Smart 	}
34293b5dd52aSJames Smart 
34303b5dd52aSJames Smart 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
34313b5dd52aSJames Smart 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
34323b5dd52aSJames Smart job_error:
343301e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
34343b5dd52aSJames Smart 	if (rc == 0)
343506548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
34361abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
34373b5dd52aSJames Smart 	return rc;
34383b5dd52aSJames Smart }
34393b5dd52aSJames Smart 
34403b5dd52aSJames Smart /**
34417ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
34423b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
34433b5dd52aSJames Smart  * @pmboxq: Pointer to mailbox command.
34443b5dd52aSJames Smart  *
34453b5dd52aSJames Smart  * This is completion handler function for mailbox commands issued from
34463b5dd52aSJames Smart  * lpfc_bsg_issue_mbox function. This function is called by the
34473b5dd52aSJames Smart  * mailbox event handler function with no lock held. This function
34483b5dd52aSJames Smart  * will wake up thread waiting on the wait queue pointed by context1
34493b5dd52aSJames Smart  * of the mailbox.
34503b5dd52aSJames Smart  **/
34519ab9b134SRashika Kheria static void
34527ad20aa9SJames Smart lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
34533b5dd52aSJames Smart {
34543b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
345501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
345675cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
34573b5dd52aSJames Smart 	uint32_t size;
34583b5dd52aSJames Smart 	unsigned long flags;
34597ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
34603b5dd52aSJames Smart 
34613e1f0718SJames Smart 	dd_data = pmboxq->ctx_ndlp;
34623b5dd52aSJames Smart 
34637ad20aa9SJames Smart 	/*
34647ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
34657ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
34667a470277SJames Smart 	 */
34677ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
34687ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
34697ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3470515e0aa2SJames Smart 
3471a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3472a33c4f7bSJames Smart 
3473a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3474a33c4f7bSJames Smart 	job = dd_data->set_job;
3475a33c4f7bSJames Smart 	if (job) {
3476a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3477a33c4f7bSJames Smart 		job->dd_data = NULL;
3478a33c4f7bSJames Smart 	}
3479a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3480a33c4f7bSJames Smart 
3481a33c4f7bSJames Smart 	/* Copy the mailbox data to the job if it is still active */
3482a33c4f7bSJames Smart 
34835a6f133eSJames Smart 	if (job) {
348401e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
34857a470277SJames Smart 		size = job->reply_payload.payload_len;
348601e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
34873b5dd52aSJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
34883b5dd52aSJames Smart 					    job->reply_payload.sg_cnt,
34897ad20aa9SJames Smart 					    pmb_buf, size);
3490b6e3b9c6SJames Smart 	}
34917a470277SJames Smart 
3492a33c4f7bSJames Smart 	dd_data->set_job = NULL;
34933b5dd52aSJames Smart 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
34947ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
34953b5dd52aSJames Smart 	kfree(dd_data);
34967ad20aa9SJames Smart 
3497a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3498a33c4f7bSJames Smart 
34997ad20aa9SJames Smart 	if (job) {
350001e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
350106548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
35021abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
35037ad20aa9SJames Smart 	}
35043b5dd52aSJames Smart 	return;
35053b5dd52aSJames Smart }
35063b5dd52aSJames Smart 
35073b5dd52aSJames Smart /**
35083b5dd52aSJames Smart  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
35093b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
35103b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
35113b5dd52aSJames Smart  * @vport: Pointer to a vport object.
35123b5dd52aSJames Smart  *
35133b5dd52aSJames Smart  * Some commands require the port to be offline, some may not be called from
35143b5dd52aSJames Smart  * the application.
35153b5dd52aSJames Smart  **/
35163b5dd52aSJames Smart static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
35173b5dd52aSJames Smart 	MAILBOX_t *mb, struct lpfc_vport *vport)
35183b5dd52aSJames Smart {
35193b5dd52aSJames Smart 	/* return negative error values for bsg job */
35203b5dd52aSJames Smart 	switch (mb->mbxCommand) {
35213b5dd52aSJames Smart 	/* Offline only */
35223b5dd52aSJames Smart 	case MBX_INIT_LINK:
35233b5dd52aSJames Smart 	case MBX_DOWN_LINK:
35243b5dd52aSJames Smart 	case MBX_CONFIG_LINK:
35253b5dd52aSJames Smart 	case MBX_CONFIG_RING:
35263b5dd52aSJames Smart 	case MBX_RESET_RING:
35273b5dd52aSJames Smart 	case MBX_UNREG_LOGIN:
35283b5dd52aSJames Smart 	case MBX_CLEAR_LA:
35293b5dd52aSJames Smart 	case MBX_DUMP_CONTEXT:
35303b5dd52aSJames Smart 	case MBX_RUN_DIAGS:
35313b5dd52aSJames Smart 	case MBX_RESTART:
35323b5dd52aSJames Smart 	case MBX_SET_MASK:
35333b5dd52aSJames Smart 		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
35343b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
35353b5dd52aSJames Smart 				"2743 Command 0x%x is illegal in on-line "
35363b5dd52aSJames Smart 				"state\n",
35373b5dd52aSJames Smart 				mb->mbxCommand);
35383b5dd52aSJames Smart 			return -EPERM;
35393b5dd52aSJames Smart 		}
35403b5dd52aSJames Smart 	case MBX_WRITE_NV:
35413b5dd52aSJames Smart 	case MBX_WRITE_VPARMS:
35423b5dd52aSJames Smart 	case MBX_LOAD_SM:
35433b5dd52aSJames Smart 	case MBX_READ_NV:
35443b5dd52aSJames Smart 	case MBX_READ_CONFIG:
35453b5dd52aSJames Smart 	case MBX_READ_RCONFIG:
35463b5dd52aSJames Smart 	case MBX_READ_STATUS:
35473b5dd52aSJames Smart 	case MBX_READ_XRI:
35483b5dd52aSJames Smart 	case MBX_READ_REV:
35493b5dd52aSJames Smart 	case MBX_READ_LNK_STAT:
35503b5dd52aSJames Smart 	case MBX_DUMP_MEMORY:
35513b5dd52aSJames Smart 	case MBX_DOWN_LOAD:
35523b5dd52aSJames Smart 	case MBX_UPDATE_CFG:
35533b5dd52aSJames Smart 	case MBX_KILL_BOARD:
355406f35551SJames Smart 	case MBX_READ_TOPOLOGY:
35553b5dd52aSJames Smart 	case MBX_LOAD_AREA:
35563b5dd52aSJames Smart 	case MBX_LOAD_EXP_ROM:
35573b5dd52aSJames Smart 	case MBX_BEACON:
35583b5dd52aSJames Smart 	case MBX_DEL_LD_ENTRY:
35593b5dd52aSJames Smart 	case MBX_SET_DEBUG:
35603b5dd52aSJames Smart 	case MBX_WRITE_WWN:
35613b5dd52aSJames Smart 	case MBX_SLI4_CONFIG:
3562c7495937SJames Smart 	case MBX_READ_EVENT_LOG:
35633b5dd52aSJames Smart 	case MBX_READ_EVENT_LOG_STATUS:
35643b5dd52aSJames Smart 	case MBX_WRITE_EVENT_LOG:
35653b5dd52aSJames Smart 	case MBX_PORT_CAPABILITIES:
35663b5dd52aSJames Smart 	case MBX_PORT_IOV_CONTROL:
35677a470277SJames Smart 	case MBX_RUN_BIU_DIAG64:
35683b5dd52aSJames Smart 		break;
35693b5dd52aSJames Smart 	case MBX_SET_VARIABLE:
3570e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3571e2aed29fSJames Smart 			"1226 mbox: set_variable 0x%x, 0x%x\n",
3572e2aed29fSJames Smart 			mb->un.varWords[0],
3573e2aed29fSJames Smart 			mb->un.varWords[1]);
3574e2aed29fSJames Smart 		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3575e2aed29fSJames Smart 			&& (mb->un.varWords[1] == 1)) {
3576e2aed29fSJames Smart 			phba->wait_4_mlo_maint_flg = 1;
3577e2aed29fSJames Smart 		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
35781b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
3579e2aed29fSJames Smart 			phba->link_flag &= ~LS_LOOPBACK_MODE;
35801b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
358176a95d75SJames Smart 			phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3582e2aed29fSJames Smart 		}
3583e2aed29fSJames Smart 		break;
35843b5dd52aSJames Smart 	case MBX_READ_SPARM64:
35853b5dd52aSJames Smart 	case MBX_REG_LOGIN:
35863b5dd52aSJames Smart 	case MBX_REG_LOGIN64:
35873b5dd52aSJames Smart 	case MBX_CONFIG_PORT:
35883b5dd52aSJames Smart 	case MBX_RUN_BIU_DIAG:
35893b5dd52aSJames Smart 	default:
35903b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
35913b5dd52aSJames Smart 			"2742 Unknown Command 0x%x\n",
35923b5dd52aSJames Smart 			mb->mbxCommand);
35933b5dd52aSJames Smart 		return -EPERM;
35943b5dd52aSJames Smart 	}
35953b5dd52aSJames Smart 
35963b5dd52aSJames Smart 	return 0; /* ok */
35973b5dd52aSJames Smart }
35983b5dd52aSJames Smart 
35993b5dd52aSJames Smart /**
36007ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
36017ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
36027ad20aa9SJames Smart  *
36037ad20aa9SJames Smart  * This is routine clean up and reset BSG handling of multi-buffer mbox
36047ad20aa9SJames Smart  * command session.
36057ad20aa9SJames Smart  **/
36067ad20aa9SJames Smart static void
36077ad20aa9SJames Smart lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
36087ad20aa9SJames Smart {
36097ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
36107ad20aa9SJames Smart 		return;
36117ad20aa9SJames Smart 
36127ad20aa9SJames Smart 	/* free all memory, including dma buffers */
36137ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
36147ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
36157ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
36167ad20aa9SJames Smart 	/* multi-buffer write mailbox command pass-through complete */
36177ad20aa9SJames Smart 	memset((char *)&phba->mbox_ext_buf_ctx, 0,
36187ad20aa9SJames Smart 	       sizeof(struct lpfc_mbox_ext_buf_ctx));
36197ad20aa9SJames Smart 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
36207ad20aa9SJames Smart 
36217ad20aa9SJames Smart 	return;
36227ad20aa9SJames Smart }
36237ad20aa9SJames Smart 
36247ad20aa9SJames Smart /**
36257ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
36267ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
36277ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
36287ad20aa9SJames Smart  *
36297ad20aa9SJames Smart  * This is routine handles BSG job for mailbox commands completions with
36307ad20aa9SJames Smart  * multiple external buffers.
36317ad20aa9SJames Smart  **/
363275cc8cfcSJohannes Thumshirn static struct bsg_job *
36337ad20aa9SJames Smart lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
36347ad20aa9SJames Smart {
36357ad20aa9SJames Smart 	struct bsg_job_data *dd_data;
363675cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
363701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
36387ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
36397ad20aa9SJames Smart 	unsigned long flags;
36407ad20aa9SJames Smart 	uint32_t size;
36417ad20aa9SJames Smart 	int rc = 0;
3642026abb87SJames Smart 	struct lpfc_dmabuf *dmabuf;
3643026abb87SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3644026abb87SJames Smart 	uint8_t *pmbx;
36457ad20aa9SJames Smart 
36463e1f0718SJames Smart 	dd_data = pmboxq->ctx_buf;
3647a33c4f7bSJames Smart 
3648a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3649a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3650a33c4f7bSJames Smart 	job = dd_data->set_job;
3651a33c4f7bSJames Smart 	if (job) {
365201e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
3653a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3654a33c4f7bSJames Smart 		job->dd_data = NULL;
36557ad20aa9SJames Smart 	}
3656a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
36577ad20aa9SJames Smart 
36587ad20aa9SJames Smart 	/*
36597ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
36607ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
36617ad20aa9SJames Smart 	 */
3662a33c4f7bSJames Smart 
36637ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
36647ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3665026abb87SJames Smart 	/* Copy the byte swapped response mailbox back to the user */
36667ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3667026abb87SJames Smart 	/* if there is any non-embedded extended data copy that too */
3668026abb87SJames Smart 	dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3669026abb87SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3670026abb87SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3671026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3672026abb87SJames Smart 		pmbx = (uint8_t *)dmabuf->virt;
3673026abb87SJames Smart 		/* byte swap the extended data following the mailbox command */
3674026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3675026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
3676026abb87SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3677026abb87SJames Smart 	}
36787ad20aa9SJames Smart 
3679a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3680a33c4f7bSJames Smart 
36817ad20aa9SJames Smart 	if (job) {
36827ad20aa9SJames Smart 		size = job->reply_payload.payload_len;
368301e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
36847ad20aa9SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
36857ad20aa9SJames Smart 					    job->reply_payload.sg_cnt,
36867ad20aa9SJames Smart 					    pmb_buf, size);
3687a33c4f7bSJames Smart 
36887ad20aa9SJames Smart 		/* result for successful */
368901e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
3690a33c4f7bSJames Smart 
36917ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36927afc0ce9SColin Ian King 				"2937 SLI_CONFIG ext-buffer mailbox command "
36937ad20aa9SJames Smart 				"(x%x/x%x) complete bsg job done, bsize:%d\n",
36947ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
36957ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, size);
3696b76f2dc9SJames Smart 		lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3697b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.nembType,
3698b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mboxType,
3699b76f2dc9SJames Smart 					dma_ebuf, sta_pos_addr,
3700b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3701a33c4f7bSJames Smart 	} else {
37027ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
37037afc0ce9SColin Ian King 				"2938 SLI_CONFIG ext-buffer mailbox "
37047ad20aa9SJames Smart 				"command (x%x/x%x) failure, rc:x%x\n",
37057ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
37067ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, rc);
3707a33c4f7bSJames Smart 	}
3708a33c4f7bSJames Smart 
3709a33c4f7bSJames Smart 
37107ad20aa9SJames Smart 	/* state change */
37117ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
37127ad20aa9SJames Smart 	kfree(dd_data);
37137ad20aa9SJames Smart 	return job;
37147ad20aa9SJames Smart }
37157ad20aa9SJames Smart 
37167ad20aa9SJames Smart /**
37177ad20aa9SJames Smart  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
37187ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
37197ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
37207ad20aa9SJames Smart  *
37217ad20aa9SJames Smart  * This is completion handler function for mailbox read commands with multiple
37227ad20aa9SJames Smart  * external buffers.
37237ad20aa9SJames Smart  **/
37247ad20aa9SJames Smart static void
37257ad20aa9SJames Smart lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
37267ad20aa9SJames Smart {
372775cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
37281abaede7SJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
37297ad20aa9SJames Smart 
3730a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3731a33c4f7bSJames Smart 
37327ad20aa9SJames Smart 	/* handle the BSG job with mailbox command */
3733a33c4f7bSJames Smart 	if (!job)
37347ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
37357ad20aa9SJames Smart 
37367ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37377afc0ce9SColin Ian King 			"2939 SLI_CONFIG ext-buffer rd mailbox command "
37387ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
37397ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
37407ad20aa9SJames Smart 
37417ad20aa9SJames Smart 	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
37427ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
37437ad20aa9SJames Smart 
37447ad20aa9SJames Smart 	/* free base driver mailbox structure memory */
37457ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
37467ad20aa9SJames Smart 
3747a33c4f7bSJames Smart 	/* if the job is still active, call job done */
37481abaede7SJohannes Thumshirn 	if (job) {
37491abaede7SJohannes Thumshirn 		bsg_reply = job->reply;
375006548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
37511abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
37521abaede7SJohannes Thumshirn 	}
37537ad20aa9SJames Smart 	return;
37547ad20aa9SJames Smart }
37557ad20aa9SJames Smart 
37567ad20aa9SJames Smart /**
37577ad20aa9SJames Smart  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
37587ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
37597ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
37607ad20aa9SJames Smart  *
37617ad20aa9SJames Smart  * This is completion handler function for mailbox write commands with multiple
37627ad20aa9SJames Smart  * external buffers.
37637ad20aa9SJames Smart  **/
37647ad20aa9SJames Smart static void
37657ad20aa9SJames Smart lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
37667ad20aa9SJames Smart {
376775cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
37681abaede7SJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
37697ad20aa9SJames Smart 
3770a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3771a33c4f7bSJames Smart 
37727ad20aa9SJames Smart 	/* handle the BSG job with the mailbox command */
3773a33c4f7bSJames Smart 	if (!job)
37747ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
37757ad20aa9SJames Smart 
37767ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37777afc0ce9SColin Ian King 			"2940 SLI_CONFIG ext-buffer wr mailbox command "
37787ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
37797ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
37807ad20aa9SJames Smart 
37817ad20aa9SJames Smart 	/* free all memory, including dma buffers */
37827ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
37837ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
37847ad20aa9SJames Smart 
3785a33c4f7bSJames Smart 	/* if the job is still active, call job done */
37861abaede7SJohannes Thumshirn 	if (job) {
37871abaede7SJohannes Thumshirn 		bsg_reply = job->reply;
378806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
37891abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
37901abaede7SJohannes Thumshirn 	}
37917ad20aa9SJames Smart 
37927ad20aa9SJames Smart 	return;
37937ad20aa9SJames Smart }
37947ad20aa9SJames Smart 
37957ad20aa9SJames Smart static void
37967ad20aa9SJames Smart lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
37977ad20aa9SJames Smart 				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
37987ad20aa9SJames Smart 				struct lpfc_dmabuf *ext_dmabuf)
37997ad20aa9SJames Smart {
38007ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
38017ad20aa9SJames Smart 
38027ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
38037ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
38047ad20aa9SJames Smart 
38057ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
38067ad20aa9SJames Smart 		if (index == 0) {
38077ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
38087ad20aa9SJames Smart 				mse[index].pa_hi =
38097ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
38107ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
38117ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
38127ad20aa9SJames Smart 				mse[index].pa_lo =
38137ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
38147ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
38157ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38167ad20aa9SJames Smart 					"2943 SLI_CONFIG(mse)[%d], "
38177ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
38187ad20aa9SJames Smart 					index,
38197ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38207ad20aa9SJames Smart 					mse[index].buf_len,
38217ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38227ad20aa9SJames Smart 					mse[index].pa_hi,
38237ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38247ad20aa9SJames Smart 					mse[index].pa_lo);
38257ad20aa9SJames Smart 		} else {
38267ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
38277ad20aa9SJames Smart 				mse[index].pa_hi =
38287ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
38297ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
38307ad20aa9SJames Smart 				mse[index].pa_lo =
38317ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
38327ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38337ad20aa9SJames Smart 					"2944 SLI_CONFIG(mse)[%d], "
38347ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
38357ad20aa9SJames Smart 					index,
38367ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38377ad20aa9SJames Smart 					mse[index].buf_len,
38387ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38397ad20aa9SJames Smart 					mse[index].pa_hi,
38407ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
38417ad20aa9SJames Smart 					mse[index].pa_lo);
38427ad20aa9SJames Smart 		}
38437ad20aa9SJames Smart 	} else {
38447ad20aa9SJames Smart 		if (index == 0) {
38457ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
38467ad20aa9SJames Smart 				hbd[index].pa_hi =
38477ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
38487ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
38497ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
38507ad20aa9SJames Smart 				hbd[index].pa_lo =
38517ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
38527ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
38537ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38547ad20aa9SJames Smart 					"3007 SLI_CONFIG(hbd)[%d], "
38557ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
38567ad20aa9SJames Smart 				index,
38577ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
38587ad20aa9SJames Smart 				&sli_cfg_mbx->un.
38597ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
38607ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38617ad20aa9SJames Smart 				hbd[index].pa_hi,
38627ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38637ad20aa9SJames Smart 				hbd[index].pa_lo);
38647ad20aa9SJames Smart 
38657ad20aa9SJames Smart 		} else {
38667ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
38677ad20aa9SJames Smart 				hbd[index].pa_hi =
38687ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
38697ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
38707ad20aa9SJames Smart 				hbd[index].pa_lo =
38717ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
38727ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38737ad20aa9SJames Smart 					"3008 SLI_CONFIG(hbd)[%d], "
38747ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
38757ad20aa9SJames Smart 				index,
38767ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
38777ad20aa9SJames Smart 				&sli_cfg_mbx->un.
38787ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
38797ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38807ad20aa9SJames Smart 				hbd[index].pa_hi,
38817ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38827ad20aa9SJames Smart 				hbd[index].pa_lo);
38837ad20aa9SJames Smart 		}
38847ad20aa9SJames Smart 	}
38857ad20aa9SJames Smart 	return;
38867ad20aa9SJames Smart }
38877ad20aa9SJames Smart 
38887ad20aa9SJames Smart /**
38897ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
38907ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
38917ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
38927ad20aa9SJames Smart  * @nemb_tp: Enumerate of non-embedded mailbox command type.
38937ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
38947ad20aa9SJames Smart  *
38957ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
38967ad20aa9SJames Smart  * non-embedded external bufffers.
38977ad20aa9SJames Smart  **/
38987ad20aa9SJames Smart static int
389975cc8cfcSJohannes Thumshirn lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
39007ad20aa9SJames Smart 			      enum nemb_type nemb_tp,
39017ad20aa9SJames Smart 			      struct lpfc_dmabuf *dmabuf)
39027ad20aa9SJames Smart {
390301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
39047ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
39057ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
39067ad20aa9SJames Smart 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
39077ad20aa9SJames Smart 	uint32_t ext_buf_cnt, ext_buf_index;
39087ad20aa9SJames Smart 	struct lpfc_dmabuf *ext_dmabuf = NULL;
39097ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
39107ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
39117ad20aa9SJames Smart 	MAILBOX_t *pmb;
39127ad20aa9SJames Smart 	uint8_t *pmbx;
39137ad20aa9SJames Smart 	int rc, i;
39147ad20aa9SJames Smart 
39157ad20aa9SJames Smart 	mbox_req =
391601e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
39177ad20aa9SJames Smart 
39187ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
39197ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
39207ad20aa9SJames Smart 
39217ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
39227ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
39237ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
39247ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
39257ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
39267ad20aa9SJames Smart 					"2945 Handled SLI_CONFIG(mse) rd, "
39277ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
39287ad20aa9SJames Smart 					ext_buf_cnt,
39297ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
39307ad20aa9SJames Smart 			rc = -ERANGE;
39317ad20aa9SJames Smart 			goto job_error;
39327ad20aa9SJames Smart 		}
39337ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39347ad20aa9SJames Smart 				"2941 Handled SLI_CONFIG(mse) rd, "
39357ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
39367ad20aa9SJames Smart 	} else {
39377ad20aa9SJames Smart 		/* sanity check on interface type for support */
393827d6ac0aSJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
39397ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2) {
39407ad20aa9SJames Smart 			rc = -ENODEV;
39417ad20aa9SJames Smart 			goto job_error;
39427ad20aa9SJames Smart 		}
39437ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
39447ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
39457ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
39467ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
39477ad20aa9SJames Smart 					"2946 Handled SLI_CONFIG(hbd) rd, "
39487ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
39497ad20aa9SJames Smart 					ext_buf_cnt,
39507ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
39517ad20aa9SJames Smart 			rc = -ERANGE;
39527ad20aa9SJames Smart 			goto job_error;
39537ad20aa9SJames Smart 		}
39547ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39557ad20aa9SJames Smart 				"2942 Handled SLI_CONFIG(hbd) rd, "
39567ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
39577ad20aa9SJames Smart 	}
39587ad20aa9SJames Smart 
3959b76f2dc9SJames Smart 	/* before dma descriptor setup */
3960b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3961b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
3962b76f2dc9SJames Smart 
39637ad20aa9SJames Smart 	/* reject non-embedded mailbox command with none external buffer */
39647ad20aa9SJames Smart 	if (ext_buf_cnt == 0) {
39657ad20aa9SJames Smart 		rc = -EPERM;
39667ad20aa9SJames Smart 		goto job_error;
39677ad20aa9SJames Smart 	} else if (ext_buf_cnt > 1) {
39687ad20aa9SJames Smart 		/* additional external read buffers */
39697ad20aa9SJames Smart 		for (i = 1; i < ext_buf_cnt; i++) {
39707ad20aa9SJames Smart 			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
39717ad20aa9SJames Smart 			if (!ext_dmabuf) {
39727ad20aa9SJames Smart 				rc = -ENOMEM;
39737ad20aa9SJames Smart 				goto job_error;
39747ad20aa9SJames Smart 			}
39757ad20aa9SJames Smart 			list_add_tail(&ext_dmabuf->list,
39767ad20aa9SJames Smart 				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
39777ad20aa9SJames Smart 		}
39787ad20aa9SJames Smart 	}
39797ad20aa9SJames Smart 
39807ad20aa9SJames Smart 	/* bsg tracking structure */
39817ad20aa9SJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
39827ad20aa9SJames Smart 	if (!dd_data) {
39837ad20aa9SJames Smart 		rc = -ENOMEM;
39847ad20aa9SJames Smart 		goto job_error;
39857ad20aa9SJames Smart 	}
39867ad20aa9SJames Smart 
39877ad20aa9SJames Smart 	/* mailbox command structure for base driver */
39887ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
39897ad20aa9SJames Smart 	if (!pmboxq) {
39907ad20aa9SJames Smart 		rc = -ENOMEM;
39917ad20aa9SJames Smart 		goto job_error;
39927ad20aa9SJames Smart 	}
39937ad20aa9SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
39947ad20aa9SJames Smart 
39957ad20aa9SJames Smart 	/* for the first external buffer */
39967ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
39977ad20aa9SJames Smart 
39987ad20aa9SJames Smart 	/* for the rest of external buffer descriptors if any */
39997ad20aa9SJames Smart 	if (ext_buf_cnt > 1) {
40007ad20aa9SJames Smart 		ext_buf_index = 1;
40017ad20aa9SJames Smart 		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
40027ad20aa9SJames Smart 				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
40037ad20aa9SJames Smart 			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
40047ad20aa9SJames Smart 						ext_buf_index, dmabuf,
40057ad20aa9SJames Smart 						curr_dmabuf);
40067ad20aa9SJames Smart 			ext_buf_index++;
40077ad20aa9SJames Smart 		}
40087ad20aa9SJames Smart 	}
40097ad20aa9SJames Smart 
4010b76f2dc9SJames Smart 	/* after dma descriptor setup */
4011b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
4012b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
4013b76f2dc9SJames Smart 
40147ad20aa9SJames Smart 	/* construct base driver mbox command */
40157ad20aa9SJames Smart 	pmb = &pmboxq->u.mb;
40167ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
40177ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
40187ad20aa9SJames Smart 	pmb->mbxOwner = OWN_HOST;
40197ad20aa9SJames Smart 	pmboxq->vport = phba->pport;
40207ad20aa9SJames Smart 
40217ad20aa9SJames Smart 	/* multi-buffer handling context */
40227ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
40237ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
40247ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
40257ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
40267ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
40277ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
40287ad20aa9SJames Smart 
40297ad20aa9SJames Smart 	/* callback for multi-buffer read mailbox command */
40307ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
40317ad20aa9SJames Smart 
40327ad20aa9SJames Smart 	/* context fields to callback function */
40333e1f0718SJames Smart 	pmboxq->ctx_buf = dd_data;
40347ad20aa9SJames Smart 	dd_data->type = TYPE_MBOX;
4035a33c4f7bSJames Smart 	dd_data->set_job = job;
40367ad20aa9SJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
40377ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
40387ad20aa9SJames Smart 	job->dd_data = dd_data;
40397ad20aa9SJames Smart 
40407ad20aa9SJames Smart 	/* state change */
40417ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
40427ad20aa9SJames Smart 
4043026abb87SJames Smart 	/*
4044026abb87SJames Smart 	 * Non-embedded mailbox subcommand data gets byte swapped here because
4045026abb87SJames Smart 	 * the lower level driver code only does the first 64 mailbox words.
4046026abb87SJames Smart 	 */
4047026abb87SJames Smart 	if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
4048026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
4049026abb87SJames Smart 		(nemb_tp == nemb_mse))
4050026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
4051026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
4052026abb87SJames Smart 				sli_cfg_mbx->un.sli_config_emb0_subsys.
4053026abb87SJames Smart 					mse[0].buf_len);
4054026abb87SJames Smart 
40557ad20aa9SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
40567ad20aa9SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
40577ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40587ad20aa9SJames Smart 				"2947 Issued SLI_CONFIG ext-buffer "
40597afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
406088a2cfbbSJames Smart 		return SLI_CONFIG_HANDLED;
40617ad20aa9SJames Smart 	}
40627ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
40637ad20aa9SJames Smart 			"2948 Failed to issue SLI_CONFIG ext-buffer "
40647afc0ce9SColin Ian King 			"mailbox command, rc:x%x\n", rc);
40657ad20aa9SJames Smart 	rc = -EPIPE;
40667ad20aa9SJames Smart 
40677ad20aa9SJames Smart job_error:
40687ad20aa9SJames Smart 	if (pmboxq)
40697ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
40707ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
40717ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
40727ad20aa9SJames Smart 	kfree(dd_data);
40737ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
40747ad20aa9SJames Smart 	return rc;
40757ad20aa9SJames Smart }
40767ad20aa9SJames Smart 
40777ad20aa9SJames Smart /**
40787ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
40797ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
40807ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
40817ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
40827ad20aa9SJames Smart  *
40837ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
40847ad20aa9SJames Smart  * non-embedded external bufffers.
40857ad20aa9SJames Smart  **/
40867ad20aa9SJames Smart static int
408775cc8cfcSJohannes Thumshirn lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
40887ad20aa9SJames Smart 			       enum nemb_type nemb_tp,
40897ad20aa9SJames Smart 			       struct lpfc_dmabuf *dmabuf)
40907ad20aa9SJames Smart {
409101e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
409201e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
40937ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
40947ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
40957ad20aa9SJames Smart 	uint32_t ext_buf_cnt;
40967ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
40977ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
40987ad20aa9SJames Smart 	MAILBOX_t *pmb;
40997ad20aa9SJames Smart 	uint8_t *mbx;
410088a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED, i;
41017ad20aa9SJames Smart 
41027ad20aa9SJames Smart 	mbox_req =
410301e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
41047ad20aa9SJames Smart 
41057ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
41067ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
41077ad20aa9SJames Smart 
41087ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
41097ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
41107ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
41117ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
41127ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4113026abb87SJames Smart 					"2953 Failed SLI_CONFIG(mse) wr, "
41147ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
41157ad20aa9SJames Smart 					ext_buf_cnt,
41167ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
41177ad20aa9SJames Smart 			return -ERANGE;
41187ad20aa9SJames Smart 		}
41197ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41207ad20aa9SJames Smart 				"2949 Handled SLI_CONFIG(mse) wr, "
41217ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
41227ad20aa9SJames Smart 	} else {
41237ad20aa9SJames Smart 		/* sanity check on interface type for support */
412427d6ac0aSJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
41257ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2)
41267ad20aa9SJames Smart 			return -ENODEV;
41277ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
41287ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
41297ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
41307ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4131026abb87SJames Smart 					"2954 Failed SLI_CONFIG(hbd) wr, "
41327ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
41337ad20aa9SJames Smart 					ext_buf_cnt,
41347ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
41357ad20aa9SJames Smart 			return -ERANGE;
41367ad20aa9SJames Smart 		}
41377ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41387ad20aa9SJames Smart 				"2950 Handled SLI_CONFIG(hbd) wr, "
41397ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
41407ad20aa9SJames Smart 	}
41417ad20aa9SJames Smart 
4142b76f2dc9SJames Smart 	/* before dma buffer descriptor setup */
4143b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4144b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
4145b76f2dc9SJames Smart 
41467ad20aa9SJames Smart 	if (ext_buf_cnt == 0)
41477ad20aa9SJames Smart 		return -EPERM;
41487ad20aa9SJames Smart 
41497ad20aa9SJames Smart 	/* for the first external buffer */
41507ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
41517ad20aa9SJames Smart 
4152b76f2dc9SJames Smart 	/* after dma descriptor setup */
4153b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4154b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
4155b76f2dc9SJames Smart 
41567ad20aa9SJames Smart 	/* log for looking forward */
41577ad20aa9SJames Smart 	for (i = 1; i < ext_buf_cnt; i++) {
41587ad20aa9SJames Smart 		if (nemb_tp == nemb_mse)
41597ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41607ad20aa9SJames Smart 				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
41617ad20aa9SJames Smart 				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
41627ad20aa9SJames Smart 				mse[i].buf_len);
41637ad20aa9SJames Smart 		else
41647ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41657ad20aa9SJames Smart 				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
41667ad20aa9SJames Smart 				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
41677ad20aa9SJames Smart 				&sli_cfg_mbx->un.sli_config_emb1_subsys.
41687ad20aa9SJames Smart 				hbd[i]));
41697ad20aa9SJames Smart 	}
41707ad20aa9SJames Smart 
41717ad20aa9SJames Smart 	/* multi-buffer handling context */
41727ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
41737ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
41747ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
41757ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
41767ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
41777ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
41787ad20aa9SJames Smart 
41797ad20aa9SJames Smart 	if (ext_buf_cnt == 1) {
41807ad20aa9SJames Smart 		/* bsg tracking structure */
41817ad20aa9SJames Smart 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
41827ad20aa9SJames Smart 		if (!dd_data) {
41837ad20aa9SJames Smart 			rc = -ENOMEM;
41847ad20aa9SJames Smart 			goto job_error;
41857ad20aa9SJames Smart 		}
41867ad20aa9SJames Smart 
41877ad20aa9SJames Smart 		/* mailbox command structure for base driver */
41887ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
41897ad20aa9SJames Smart 		if (!pmboxq) {
41907ad20aa9SJames Smart 			rc = -ENOMEM;
41917ad20aa9SJames Smart 			goto job_error;
41927ad20aa9SJames Smart 		}
41937ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
41947ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
41957ad20aa9SJames Smart 		mbx = (uint8_t *)dmabuf->virt;
41967ad20aa9SJames Smart 		memcpy(pmb, mbx, sizeof(*pmb));
41977ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
41987ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
41997ad20aa9SJames Smart 
42007ad20aa9SJames Smart 		/* callback for multi-buffer read mailbox command */
42017ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
42027ad20aa9SJames Smart 
42037ad20aa9SJames Smart 		/* context fields to callback function */
42043e1f0718SJames Smart 		pmboxq->ctx_buf = dd_data;
42057ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4206a33c4f7bSJames Smart 		dd_data->set_job = job;
42077ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
42087ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
42097ad20aa9SJames Smart 		job->dd_data = dd_data;
42107ad20aa9SJames Smart 
42117ad20aa9SJames Smart 		/* state change */
42127ad20aa9SJames Smart 
4213a33c4f7bSJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
42147ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
42157ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
42167ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42177ad20aa9SJames Smart 					"2955 Issued SLI_CONFIG ext-buffer "
42187afc0ce9SColin Ian King 					"mailbox command, rc:x%x\n", rc);
421988a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
42207ad20aa9SJames Smart 		}
42217ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
42227ad20aa9SJames Smart 				"2956 Failed to issue SLI_CONFIG ext-buffer "
42237afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
42247ad20aa9SJames Smart 		rc = -EPIPE;
4225026abb87SJames Smart 		goto job_error;
42267ad20aa9SJames Smart 	}
42277ad20aa9SJames Smart 
422888a2cfbbSJames Smart 	/* wait for additoinal external buffers */
4229a33c4f7bSJames Smart 
423001e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
423106548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
42321abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
423388a2cfbbSJames Smart 	return SLI_CONFIG_HANDLED;
423488a2cfbbSJames Smart 
42357ad20aa9SJames Smart job_error:
42367ad20aa9SJames Smart 	if (pmboxq)
42377ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
42387ad20aa9SJames Smart 	kfree(dd_data);
42397ad20aa9SJames Smart 
42407ad20aa9SJames Smart 	return rc;
42417ad20aa9SJames Smart }
42427ad20aa9SJames Smart 
42437ad20aa9SJames Smart /**
42447ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
42457ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
42467ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
42477ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
42487ad20aa9SJames Smart  *
42497ad20aa9SJames Smart  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
42507ad20aa9SJames Smart  * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
42517ad20aa9SJames Smart  * with embedded sussystem 0x1 and opcodes with external HBDs.
42527ad20aa9SJames Smart  **/
42537ad20aa9SJames Smart static int
425475cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
42557ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
42567ad20aa9SJames Smart {
42577ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
42587ad20aa9SJames Smart 	uint32_t subsys;
42597ad20aa9SJames Smart 	uint32_t opcode;
42607ad20aa9SJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
42617ad20aa9SJames Smart 
4262026abb87SJames Smart 	/* state change on new multi-buffer pass-through mailbox command */
42637ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
42647ad20aa9SJames Smart 
42657ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
42667ad20aa9SJames Smart 
42677ad20aa9SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
42687ad20aa9SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
42697ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
42707ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
42717ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
42727ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
42737ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
42747ad20aa9SJames Smart 			switch (opcode) {
42757ad20aa9SJames Smart 			case FCOE_OPCODE_READ_FCF:
42762a2719d3SJames Smart 			case FCOE_OPCODE_GET_DPORT_RESULTS:
42777ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42787ad20aa9SJames Smart 						"2957 Handled SLI_CONFIG "
42797ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42807ad20aa9SJames Smart 						opcode);
42817ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
42827ad20aa9SJames Smart 							nemb_mse, dmabuf);
42837ad20aa9SJames Smart 				break;
42847ad20aa9SJames Smart 			case FCOE_OPCODE_ADD_FCF:
42852a2719d3SJames Smart 			case FCOE_OPCODE_SET_DPORT_MODE:
42862a2719d3SJames Smart 			case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
42877ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42887ad20aa9SJames Smart 						"2958 Handled SLI_CONFIG "
42897ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42907ad20aa9SJames Smart 						opcode);
42917ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
42927ad20aa9SJames Smart 							nemb_mse, dmabuf);
42937ad20aa9SJames Smart 				break;
42947ad20aa9SJames Smart 			default:
42957ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4296026abb87SJames Smart 						"2959 Reject SLI_CONFIG "
42977ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42987ad20aa9SJames Smart 						opcode);
4299026abb87SJames Smart 				rc = -EPERM;
4300026abb87SJames Smart 				break;
4301026abb87SJames Smart 			}
4302026abb87SJames Smart 		} else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4303026abb87SJames Smart 			switch (opcode) {
4304026abb87SJames Smart 			case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4305b99570ddSJames Smart 			case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4306c6377970SJames Smart 			case COMN_OPCODE_GET_PROFILE_CONFIG:
4307026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4308026abb87SJames Smart 						"3106 Handled SLI_CONFIG "
43096b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4310026abb87SJames Smart 						opcode);
4311026abb87SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4312026abb87SJames Smart 							nemb_mse, dmabuf);
4313026abb87SJames Smart 				break;
4314026abb87SJames Smart 			default:
4315026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4316026abb87SJames Smart 						"3107 Reject SLI_CONFIG "
43176b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4318026abb87SJames Smart 						opcode);
4319026abb87SJames Smart 				rc = -EPERM;
43207ad20aa9SJames Smart 				break;
43217ad20aa9SJames Smart 			}
43227ad20aa9SJames Smart 		} else {
43237ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4324026abb87SJames Smart 					"2977 Reject SLI_CONFIG "
43257ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
43267ad20aa9SJames Smart 					subsys, opcode);
4327026abb87SJames Smart 			rc = -EPERM;
43287ad20aa9SJames Smart 		}
43297ad20aa9SJames Smart 	} else {
43307ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
43317ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
43327ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
43337ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
43347ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
43357ad20aa9SJames Smart 			switch (opcode) {
43367ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT:
43377ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT_LIST:
43387ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43397ad20aa9SJames Smart 						"2960 Handled SLI_CONFIG "
43407ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
43417ad20aa9SJames Smart 						opcode);
43427ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
43437ad20aa9SJames Smart 							nemb_hbd, dmabuf);
43447ad20aa9SJames Smart 				break;
43457ad20aa9SJames Smart 			case COMN_OPCODE_WRITE_OBJECT:
43467ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43477ad20aa9SJames Smart 						"2961 Handled SLI_CONFIG "
43487ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
43497ad20aa9SJames Smart 						opcode);
43507ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
43517ad20aa9SJames Smart 							nemb_hbd, dmabuf);
43527ad20aa9SJames Smart 				break;
43537ad20aa9SJames Smart 			default:
43547ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43557ad20aa9SJames Smart 						"2962 Not handled SLI_CONFIG "
43567ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
43577ad20aa9SJames Smart 						opcode);
43587ad20aa9SJames Smart 				rc = SLI_CONFIG_NOT_HANDLED;
43597ad20aa9SJames Smart 				break;
43607ad20aa9SJames Smart 			}
43617ad20aa9SJames Smart 		} else {
43627ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4363026abb87SJames Smart 					"2978 Not handled SLI_CONFIG "
43647ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
43657ad20aa9SJames Smart 					subsys, opcode);
43667ad20aa9SJames Smart 			rc = SLI_CONFIG_NOT_HANDLED;
43677ad20aa9SJames Smart 		}
43687ad20aa9SJames Smart 	}
4369026abb87SJames Smart 
4370026abb87SJames Smart 	/* state reset on not handled new multi-buffer mailbox command */
4371026abb87SJames Smart 	if (rc != SLI_CONFIG_HANDLED)
4372026abb87SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4373026abb87SJames Smart 
43747ad20aa9SJames Smart 	return rc;
43757ad20aa9SJames Smart }
43767ad20aa9SJames Smart 
43777ad20aa9SJames Smart /**
43787ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
43797ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
43807ad20aa9SJames Smart  *
43817ad20aa9SJames Smart  * This routine is for requesting to abort a pass-through mailbox command with
43827ad20aa9SJames Smart  * multiple external buffers due to error condition.
43837ad20aa9SJames Smart  **/
43847ad20aa9SJames Smart static void
43857ad20aa9SJames Smart lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
43867ad20aa9SJames Smart {
43877ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
43887ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
43897ad20aa9SJames Smart 	else
43907ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
43917ad20aa9SJames Smart 	return;
43927ad20aa9SJames Smart }
43937ad20aa9SJames Smart 
43947ad20aa9SJames Smart /**
43957ad20aa9SJames Smart  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
43967ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
43977ad20aa9SJames Smart  * @dmabuf: Pointer to a DMA buffer descriptor.
43987ad20aa9SJames Smart  *
43997ad20aa9SJames Smart  * This routine extracts the next mailbox read external buffer back to
44007ad20aa9SJames Smart  * user space through BSG.
44017ad20aa9SJames Smart  **/
44027ad20aa9SJames Smart static int
440375cc8cfcSJohannes Thumshirn lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
44047ad20aa9SJames Smart {
440501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
44067ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
44077ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
44087ad20aa9SJames Smart 	uint8_t *pbuf;
44097ad20aa9SJames Smart 	uint32_t size;
44107ad20aa9SJames Smart 	uint32_t index;
44117ad20aa9SJames Smart 
44127ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
44137ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
44147ad20aa9SJames Smart 
44157ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
44167ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
44177ad20aa9SJames Smart 
44187ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
44197ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
44207ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
44217ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44227ad20aa9SJames Smart 				"2963 SLI_CONFIG (mse) ext-buffer rd get "
44237ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
44247ad20aa9SJames Smart 	} else {
44257ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
44267ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
44277ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44287ad20aa9SJames Smart 				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
44297ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
44307ad20aa9SJames Smart 	}
44317ad20aa9SJames Smart 	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
44327ad20aa9SJames Smart 		return -EPIPE;
44337ad20aa9SJames Smart 	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
44347ad20aa9SJames Smart 				  struct lpfc_dmabuf, list);
44357ad20aa9SJames Smart 	list_del_init(&dmabuf->list);
4436b76f2dc9SJames Smart 
4437b76f2dc9SJames Smart 	/* after dma buffer descriptor setup */
4438b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4439b76f2dc9SJames Smart 					mbox_rd, dma_ebuf, sta_pos_addr,
4440b76f2dc9SJames Smart 					dmabuf, index);
4441b76f2dc9SJames Smart 
44427ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
444301e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len =
44447ad20aa9SJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
44457ad20aa9SJames Smart 				    job->reply_payload.sg_cnt,
44467ad20aa9SJames Smart 				    pbuf, size);
44477ad20aa9SJames Smart 
44487ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
44497ad20aa9SJames Smart 
44507ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
44517ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44527ad20aa9SJames Smart 				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
44537ad20aa9SJames Smart 				"command session done\n");
44547ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
44557ad20aa9SJames Smart 	}
44567ad20aa9SJames Smart 
445701e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
445806548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
44591abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
44607ad20aa9SJames Smart 
44617ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
44627ad20aa9SJames Smart }
44637ad20aa9SJames Smart 
44647ad20aa9SJames Smart /**
44657ad20aa9SJames Smart  * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
44667ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
44677ad20aa9SJames Smart  * @dmabuf: Pointer to a DMA buffer descriptor.
44687ad20aa9SJames Smart  *
44697ad20aa9SJames Smart  * This routine sets up the next mailbox read external buffer obtained
44707ad20aa9SJames Smart  * from user space through BSG.
44717ad20aa9SJames Smart  **/
44727ad20aa9SJames Smart static int
447375cc8cfcSJohannes Thumshirn lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
44747ad20aa9SJames Smart 			struct lpfc_dmabuf *dmabuf)
44757ad20aa9SJames Smart {
447601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
44777ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
44787ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
44797ad20aa9SJames Smart 	MAILBOX_t *pmb;
44807ad20aa9SJames Smart 	enum nemb_type nemb_tp;
44817ad20aa9SJames Smart 	uint8_t *pbuf;
44827ad20aa9SJames Smart 	uint32_t size;
44837ad20aa9SJames Smart 	uint32_t index;
44847ad20aa9SJames Smart 	int rc;
44857ad20aa9SJames Smart 
44867ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
44877ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
44887ad20aa9SJames Smart 	nemb_tp = phba->mbox_ext_buf_ctx.nembType;
44897ad20aa9SJames Smart 
44907ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
44917ad20aa9SJames Smart 	size = job->request_payload.payload_len;
44927ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
44937ad20aa9SJames Smart 			  job->request_payload.sg_cnt,
44947ad20aa9SJames Smart 			  pbuf, size);
44957ad20aa9SJames Smart 
44967ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
44977ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44987ad20aa9SJames Smart 				"2966 SLI_CONFIG (mse) ext-buffer wr set "
44997ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
45007ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
45017ad20aa9SJames Smart 
45027ad20aa9SJames Smart 	} else {
45037ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45047ad20aa9SJames Smart 				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
45057ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
45067ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
45077ad20aa9SJames Smart 
45087ad20aa9SJames Smart 	}
45097ad20aa9SJames Smart 
45107ad20aa9SJames Smart 	/* set up external buffer descriptor and add to external buffer list */
45117ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
45127ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf,
45137ad20aa9SJames Smart 					dmabuf);
45147ad20aa9SJames Smart 	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
45157ad20aa9SJames Smart 
4516b76f2dc9SJames Smart 	/* after write dma buffer */
4517b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4518b76f2dc9SJames Smart 					mbox_wr, dma_ebuf, sta_pos_addr,
4519b76f2dc9SJames Smart 					dmabuf, index);
4520b76f2dc9SJames Smart 
45217ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
45227ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45237ad20aa9SJames Smart 				"2968 SLI_CONFIG ext-buffer wr all %d "
45247ad20aa9SJames Smart 				"ebuffers received\n",
45257ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.numBuf);
45269a1b0b9aSBo Wu 
45279a1b0b9aSBo Wu 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
45289a1b0b9aSBo Wu 		if (!dd_data) {
45299a1b0b9aSBo Wu 			rc = -ENOMEM;
45309a1b0b9aSBo Wu 			goto job_error;
45319a1b0b9aSBo Wu 		}
45329a1b0b9aSBo Wu 
45337ad20aa9SJames Smart 		/* mailbox command structure for base driver */
45347ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
45357ad20aa9SJames Smart 		if (!pmboxq) {
45367ad20aa9SJames Smart 			rc = -ENOMEM;
45377ad20aa9SJames Smart 			goto job_error;
45387ad20aa9SJames Smart 		}
45397ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
45407ad20aa9SJames Smart 		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
45417ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
45427ad20aa9SJames Smart 		memcpy(pmb, pbuf, sizeof(*pmb));
45437ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
45447ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
45457ad20aa9SJames Smart 
45467ad20aa9SJames Smart 		/* callback for multi-buffer write mailbox command */
45477ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
45487ad20aa9SJames Smart 
45497ad20aa9SJames Smart 		/* context fields to callback function */
45503e1f0718SJames Smart 		pmboxq->ctx_buf = dd_data;
45517ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4552a33c4f7bSJames Smart 		dd_data->set_job = job;
45537ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
45547ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
45557ad20aa9SJames Smart 		job->dd_data = dd_data;
45567ad20aa9SJames Smart 
45577ad20aa9SJames Smart 		/* state change */
45587ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
45597ad20aa9SJames Smart 
45607ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
45617ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
45627ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45637ad20aa9SJames Smart 					"2969 Issued SLI_CONFIG ext-buffer "
45647afc0ce9SColin Ian King 					"mailbox command, rc:x%x\n", rc);
456588a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
45667ad20aa9SJames Smart 		}
45677ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
45687ad20aa9SJames Smart 				"2970 Failed to issue SLI_CONFIG ext-buffer "
45697afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
45707ad20aa9SJames Smart 		rc = -EPIPE;
45717ad20aa9SJames Smart 		goto job_error;
45727ad20aa9SJames Smart 	}
45737ad20aa9SJames Smart 
45747ad20aa9SJames Smart 	/* wait for additoinal external buffers */
457501e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
457606548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
45771abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
45787ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
45797ad20aa9SJames Smart 
45807ad20aa9SJames Smart job_error:
45819a1b0b9aSBo Wu 	if (pmboxq)
45829a1b0b9aSBo Wu 		mempool_free(pmboxq, phba->mbox_mem_pool);
45837ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
45847ad20aa9SJames Smart 	kfree(dd_data);
45857ad20aa9SJames Smart 
45867ad20aa9SJames Smart 	return rc;
45877ad20aa9SJames Smart }
45887ad20aa9SJames Smart 
45897ad20aa9SJames Smart /**
45907ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
45917ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
45927ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
45937ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
45947ad20aa9SJames Smart  *
45957ad20aa9SJames Smart  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
45967ad20aa9SJames Smart  * command with multiple non-embedded external buffers.
45977ad20aa9SJames Smart  **/
45987ad20aa9SJames Smart static int
459975cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
46007ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
46017ad20aa9SJames Smart {
46027ad20aa9SJames Smart 	int rc;
46037ad20aa9SJames Smart 
46047ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
46057ad20aa9SJames Smart 			"2971 SLI_CONFIG buffer (type:x%x)\n",
46067ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mboxType);
46077ad20aa9SJames Smart 
46087ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
46097ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
46107ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
46117ad20aa9SJames Smart 					"2972 SLI_CONFIG rd buffer state "
46127ad20aa9SJames Smart 					"mismatch:x%x\n",
46137ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
46147ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
46157ad20aa9SJames Smart 			return -EPIPE;
46167ad20aa9SJames Smart 		}
46177ad20aa9SJames Smart 		rc = lpfc_bsg_read_ebuf_get(phba, job);
46187ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
46197ad20aa9SJames Smart 			lpfc_bsg_dma_page_free(phba, dmabuf);
46207ad20aa9SJames Smart 	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
46217ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
46227ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
46237ad20aa9SJames Smart 					"2973 SLI_CONFIG wr buffer state "
46247ad20aa9SJames Smart 					"mismatch:x%x\n",
46257ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
46267ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
46277ad20aa9SJames Smart 			return -EPIPE;
46287ad20aa9SJames Smart 		}
46297ad20aa9SJames Smart 		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
46307ad20aa9SJames Smart 	}
46317ad20aa9SJames Smart 	return rc;
46327ad20aa9SJames Smart }
46337ad20aa9SJames Smart 
46347ad20aa9SJames Smart /**
46357ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
46367ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
46377ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
46387ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
46397ad20aa9SJames Smart  *
46407ad20aa9SJames Smart  * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
46417ad20aa9SJames Smart  * (0x9B) mailbox commands and external buffers.
46427ad20aa9SJames Smart  **/
46437ad20aa9SJames Smart static int
464475cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
46457ad20aa9SJames Smart 			    struct lpfc_dmabuf *dmabuf)
46467ad20aa9SJames Smart {
464701e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
46487ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
464988a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
46507ad20aa9SJames Smart 
46517ad20aa9SJames Smart 	mbox_req =
465201e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
46537ad20aa9SJames Smart 
46547ad20aa9SJames Smart 	/* mbox command with/without single external buffer */
46557ad20aa9SJames Smart 	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
465688a2cfbbSJames Smart 		return rc;
46577ad20aa9SJames Smart 
46587ad20aa9SJames Smart 	/* mbox command and first external buffer */
46597ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
46607ad20aa9SJames Smart 		if (mbox_req->extSeqNum == 1) {
46617ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
46627ad20aa9SJames Smart 					"2974 SLI_CONFIG mailbox: tag:%d, "
46637ad20aa9SJames Smart 					"seq:%d\n", mbox_req->extMboxTag,
46647ad20aa9SJames Smart 					mbox_req->extSeqNum);
46657ad20aa9SJames Smart 			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
46667ad20aa9SJames Smart 			return rc;
46677ad20aa9SJames Smart 		} else
46687ad20aa9SJames Smart 			goto sli_cfg_ext_error;
46697ad20aa9SJames Smart 	}
46707ad20aa9SJames Smart 
46717ad20aa9SJames Smart 	/*
46727ad20aa9SJames Smart 	 * handle additional external buffers
46737ad20aa9SJames Smart 	 */
46747ad20aa9SJames Smart 
46757ad20aa9SJames Smart 	/* check broken pipe conditions */
46767ad20aa9SJames Smart 	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
46777ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46787ad20aa9SJames Smart 	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
46797ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46807ad20aa9SJames Smart 	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
46817ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46827ad20aa9SJames Smart 
46837ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
46847ad20aa9SJames Smart 			"2975 SLI_CONFIG mailbox external buffer: "
46857ad20aa9SJames Smart 			"extSta:x%x, tag:%d, seq:%d\n",
46867ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
46877ad20aa9SJames Smart 			mbox_req->extSeqNum);
46887ad20aa9SJames Smart 	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
46897ad20aa9SJames Smart 	return rc;
46907ad20aa9SJames Smart 
46917ad20aa9SJames Smart sli_cfg_ext_error:
46927ad20aa9SJames Smart 	/* all other cases, broken pipe */
46937ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
46947ad20aa9SJames Smart 			"2976 SLI_CONFIG mailbox broken pipe: "
46957ad20aa9SJames Smart 			"ctxSta:x%x, ctxNumBuf:%d "
46967ad20aa9SJames Smart 			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
46977ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state,
46987ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.numBuf,
46997ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbxTag,
47007ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.seqNum,
47017ad20aa9SJames Smart 			mbox_req->extMboxTag, mbox_req->extSeqNum);
47027ad20aa9SJames Smart 
47037ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
47047ad20aa9SJames Smart 
47057ad20aa9SJames Smart 	return -EPIPE;
47067ad20aa9SJames Smart }
47077ad20aa9SJames Smart 
47087ad20aa9SJames Smart /**
47093b5dd52aSJames Smart  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
47103b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
47113b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
47123b5dd52aSJames Smart  * @vport: Pointer to a vport object.
47133b5dd52aSJames Smart  *
47143b5dd52aSJames Smart  * Allocate a tracking object, mailbox command memory, get a mailbox
47153b5dd52aSJames Smart  * from the mailbox pool, copy the caller mailbox command.
47163b5dd52aSJames Smart  *
47173b5dd52aSJames Smart  * If offline and the sli is active we need to poll for the command (port is
47183b5dd52aSJames Smart  * being reset) and com-plete the job, otherwise issue the mailbox command and
47193b5dd52aSJames Smart  * let our completion handler finish the command.
47203b5dd52aSJames Smart  **/
4721a2fc4aefSJames Smart static int
472275cc8cfcSJohannes Thumshirn lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
47233b5dd52aSJames Smart 	struct lpfc_vport *vport)
47243b5dd52aSJames Smart {
472501e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
472601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
47277a470277SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
47287a470277SJames Smart 	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
47297a470277SJames Smart 	/* a 4k buffer to hold the mb and extended data from/to the bsg */
47307ad20aa9SJames Smart 	uint8_t *pmbx = NULL;
47317a470277SJames Smart 	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
47327ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
47337ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
4734b6e3b9c6SJames Smart 	struct READ_EVENT_LOG_VAR *rdEventLog;
4735b6e3b9c6SJames Smart 	uint32_t transmit_length, receive_length, mode;
47367ad20aa9SJames Smart 	struct lpfc_mbx_sli4_config *sli4_config;
4737b6e3b9c6SJames Smart 	struct lpfc_mbx_nembed_cmd *nembed_sge;
4738b6e3b9c6SJames Smart 	struct ulp_bde64 *bde;
47397a470277SJames Smart 	uint8_t *ext = NULL;
47403b5dd52aSJames Smart 	int rc = 0;
47417a470277SJames Smart 	uint8_t *from;
47427ad20aa9SJames Smart 	uint32_t size;
47437ad20aa9SJames Smart 
47447a470277SJames Smart 	/* in case no data is transferred */
474501e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
47467a470277SJames Smart 
4747b6e3b9c6SJames Smart 	/* sanity check to protect driver */
4748b6e3b9c6SJames Smart 	if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4749b6e3b9c6SJames Smart 	    job->request_payload.payload_len > BSG_MBOX_SIZE) {
4750b6e3b9c6SJames Smart 		rc = -ERANGE;
4751b6e3b9c6SJames Smart 		goto job_done;
4752b6e3b9c6SJames Smart 	}
4753b6e3b9c6SJames Smart 
47547ad20aa9SJames Smart 	/*
47557ad20aa9SJames Smart 	 * Don't allow mailbox commands to be sent when blocked or when in
47567ad20aa9SJames Smart 	 * the middle of discovery
47577ad20aa9SJames Smart 	 */
47587ad20aa9SJames Smart 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
47597ad20aa9SJames Smart 		rc = -EAGAIN;
47607ad20aa9SJames Smart 		goto job_done;
47617ad20aa9SJames Smart 	}
47627ad20aa9SJames Smart 
47637ad20aa9SJames Smart 	mbox_req =
476401e0e15cSJohannes Thumshirn 	    (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
47657ad20aa9SJames Smart 
47667a470277SJames Smart 	/* check if requested extended data lengths are valid */
4767b6e3b9c6SJames Smart 	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4768b6e3b9c6SJames Smart 	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
47697a470277SJames Smart 		rc = -ERANGE;
47707a470277SJames Smart 		goto job_done;
47717a470277SJames Smart 	}
47723b5dd52aSJames Smart 
47737ad20aa9SJames Smart 	dmabuf = lpfc_bsg_dma_page_alloc(phba);
47747ad20aa9SJames Smart 	if (!dmabuf || !dmabuf->virt) {
47757ad20aa9SJames Smart 		rc = -ENOMEM;
47767ad20aa9SJames Smart 		goto job_done;
47777ad20aa9SJames Smart 	}
47787ad20aa9SJames Smart 
47797ad20aa9SJames Smart 	/* Get the mailbox command or external buffer from BSG */
47807ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
47817ad20aa9SJames Smart 	size = job->request_payload.payload_len;
47827ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
47837ad20aa9SJames Smart 			  job->request_payload.sg_cnt, pmbx, size);
47847ad20aa9SJames Smart 
47857ad20aa9SJames Smart 	/* Handle possible SLI_CONFIG with non-embedded payloads */
47867ad20aa9SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
47877ad20aa9SJames Smart 		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
47887ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
47897ad20aa9SJames Smart 			goto job_cont;
47907ad20aa9SJames Smart 		if (rc)
47917ad20aa9SJames Smart 			goto job_done;
47927ad20aa9SJames Smart 		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
47937ad20aa9SJames Smart 	}
47947ad20aa9SJames Smart 
47957ad20aa9SJames Smart 	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
47967ad20aa9SJames Smart 	if (rc != 0)
47977ad20aa9SJames Smart 		goto job_done; /* must be negative */
47987ad20aa9SJames Smart 
47993b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
48003b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
48013b5dd52aSJames Smart 	if (!dd_data) {
48023b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
48033b5dd52aSJames Smart 				"2727 Failed allocation of dd_data\n");
48047a470277SJames Smart 		rc = -ENOMEM;
48057a470277SJames Smart 		goto job_done;
48063b5dd52aSJames Smart 	}
48073b5dd52aSJames Smart 
48083b5dd52aSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
48093b5dd52aSJames Smart 	if (!pmboxq) {
48107a470277SJames Smart 		rc = -ENOMEM;
48117a470277SJames Smart 		goto job_done;
48123b5dd52aSJames Smart 	}
48137a470277SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
48143b5dd52aSJames Smart 
48153b5dd52aSJames Smart 	pmb = &pmboxq->u.mb;
48167ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
48173b5dd52aSJames Smart 	pmb->mbxOwner = OWN_HOST;
48183b5dd52aSJames Smart 	pmboxq->vport = vport;
48193b5dd52aSJames Smart 
4820c7495937SJames Smart 	/* If HBA encountered an error attention, allow only DUMP
4821c7495937SJames Smart 	 * or RESTART mailbox commands until the HBA is restarted.
4822c7495937SJames Smart 	 */
4823c7495937SJames Smart 	if (phba->pport->stopped &&
4824c7495937SJames Smart 	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
4825c7495937SJames Smart 	    pmb->mbxCommand != MBX_RESTART &&
4826c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
4827c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_WWN)
4828c7495937SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4829c7495937SJames Smart 				"2797 mbox: Issued mailbox cmd "
4830c7495937SJames Smart 				"0x%x while in stopped state.\n",
4831c7495937SJames Smart 				pmb->mbxCommand);
4832c7495937SJames Smart 
48337a470277SJames Smart 	/* extended mailbox commands will need an extended buffer */
4834c7495937SJames Smart 	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
48357ad20aa9SJames Smart 		from = pmbx;
48367ad20aa9SJames Smart 		ext = from + sizeof(MAILBOX_t);
48373e1f0718SJames Smart 		pmboxq->ctx_buf = ext;
48387a470277SJames Smart 		pmboxq->in_ext_byte_len =
48397a470277SJames Smart 			mbox_req->inExtWLen * sizeof(uint32_t);
48407a470277SJames Smart 		pmboxq->out_ext_byte_len =
4841c7495937SJames Smart 			mbox_req->outExtWLen * sizeof(uint32_t);
48427a470277SJames Smart 		pmboxq->mbox_offset_word = mbox_req->mbOffset;
48437a470277SJames Smart 	}
48447a470277SJames Smart 
48457a470277SJames Smart 	/* biu diag will need a kernel buffer to transfer the data
48467a470277SJames Smart 	 * allocate our own buffer and setup the mailbox command to
48477a470277SJames Smart 	 * use ours
48487a470277SJames Smart 	 */
48497a470277SJames Smart 	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4850b6e3b9c6SJames Smart 		transmit_length = pmb->un.varWords[1];
4851b6e3b9c6SJames Smart 		receive_length = pmb->un.varWords[4];
4852c7495937SJames Smart 		/* transmit length cannot be greater than receive length or
4853c7495937SJames Smart 		 * mailbox extension size
4854c7495937SJames Smart 		 */
4855c7495937SJames Smart 		if ((transmit_length > receive_length) ||
485688a2cfbbSJames Smart 			(transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4857c7495937SJames Smart 			rc = -ERANGE;
4858c7495937SJames Smart 			goto job_done;
4859c7495937SJames Smart 		}
48607a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
48617ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
48627a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
48637ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
48647a470277SJames Smart 
48657a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
48667ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
48677ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
48687a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
48697ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
48707ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4871c7495937SJames Smart 	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4872b6e3b9c6SJames Smart 		rdEventLog = &pmb->un.varRdEventLog;
4873b6e3b9c6SJames Smart 		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4874b6e3b9c6SJames Smart 		mode = bf_get(lpfc_event_log, rdEventLog);
4875c7495937SJames Smart 
4876c7495937SJames Smart 		/* receive length cannot be greater than mailbox
4877c7495937SJames Smart 		 * extension size
4878c7495937SJames Smart 		 */
487988a2cfbbSJames Smart 		if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4880c7495937SJames Smart 			rc = -ERANGE;
4881c7495937SJames Smart 			goto job_done;
4882c7495937SJames Smart 		}
4883c7495937SJames Smart 
4884c7495937SJames Smart 		/* mode zero uses a bde like biu diags command */
4885c7495937SJames Smart 		if (mode == 0) {
48867ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
48877ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
48887ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
48897ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
4890c7495937SJames Smart 		}
4891c7495937SJames Smart 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
48923ef6d24cSJames Smart 		/* Let type 4 (well known data) through because the data is
48933ef6d24cSJames Smart 		 * returned in varwords[4-8]
48943ef6d24cSJames Smart 		 * otherwise check the recieve length and fetch the buffer addr
48953ef6d24cSJames Smart 		 */
48963ef6d24cSJames Smart 		if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
48973ef6d24cSJames Smart 			(pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4898c7495937SJames Smart 			/* rebuild the command for sli4 using our own buffers
4899c7495937SJames Smart 			* like we do for biu diags
4900c7495937SJames Smart 			*/
4901b6e3b9c6SJames Smart 			receive_length = pmb->un.varWords[2];
4902c7495937SJames Smart 			/* receive length cannot be greater than mailbox
4903c7495937SJames Smart 			 * extension size
4904c7495937SJames Smart 			 */
49057ad20aa9SJames Smart 			if (receive_length == 0) {
4906c7495937SJames Smart 				rc = -ERANGE;
4907c7495937SJames Smart 				goto job_done;
4908c7495937SJames Smart 			}
49097ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
49107ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
49117ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
49127ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4913c7495937SJames Smart 		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4914c7495937SJames Smart 			pmb->un.varUpdateCfg.co) {
4915b6e3b9c6SJames Smart 			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4916c7495937SJames Smart 
4917c7495937SJames Smart 			/* bde size cannot be greater than mailbox ext size */
491888a2cfbbSJames Smart 			if (bde->tus.f.bdeSize >
491988a2cfbbSJames Smart 			    BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4920c7495937SJames Smart 				rc = -ERANGE;
4921c7495937SJames Smart 				goto job_done;
4922c7495937SJames Smart 			}
49237ad20aa9SJames Smart 			bde->addrHigh = putPaddrHigh(dmabuf->phys
49247ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
49257ad20aa9SJames Smart 			bde->addrLow = putPaddrLow(dmabuf->phys
49267ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4927515e0aa2SJames Smart 		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
49287ad20aa9SJames Smart 			/* Handling non-embedded SLI_CONFIG mailbox command */
49297ad20aa9SJames Smart 			sli4_config = &pmboxq->u.mqe.un.sli4_config;
49307ad20aa9SJames Smart 			if (!bf_get(lpfc_mbox_hdr_emb,
49317ad20aa9SJames Smart 			    &sli4_config->header.cfg_mhdr)) {
49327ad20aa9SJames Smart 				/* rebuild the command for sli4 using our
49337ad20aa9SJames Smart 				 * own buffers like we do for biu diags
4934515e0aa2SJames Smart 				 */
4935515e0aa2SJames Smart 				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4936515e0aa2SJames Smart 						&pmb->un.varWords[0];
4937515e0aa2SJames Smart 				receive_length = nembed_sge->sge[0].length;
4938515e0aa2SJames Smart 
49397ad20aa9SJames Smart 				/* receive length cannot be greater than
49407ad20aa9SJames Smart 				 * mailbox extension size
4941515e0aa2SJames Smart 				 */
4942515e0aa2SJames Smart 				if ((receive_length == 0) ||
494388a2cfbbSJames Smart 				    (receive_length >
494488a2cfbbSJames Smart 				     BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4945515e0aa2SJames Smart 					rc = -ERANGE;
4946515e0aa2SJames Smart 					goto job_done;
4947515e0aa2SJames Smart 				}
4948515e0aa2SJames Smart 
49497ad20aa9SJames Smart 				nembed_sge->sge[0].pa_hi =
49507ad20aa9SJames Smart 						putPaddrHigh(dmabuf->phys
49517ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
49527ad20aa9SJames Smart 				nembed_sge->sge[0].pa_lo =
49537ad20aa9SJames Smart 						putPaddrLow(dmabuf->phys
49547ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
4955515e0aa2SJames Smart 			}
4956c7495937SJames Smart 		}
4957c7495937SJames Smart 	}
4958c7495937SJames Smart 
49597ad20aa9SJames Smart 	dd_data->context_un.mbox.dmabuffers = dmabuf;
49603b5dd52aSJames Smart 
49613b5dd52aSJames Smart 	/* setup wake call as IOCB callback */
49627ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
49637a470277SJames Smart 
49643b5dd52aSJames Smart 	/* setup context field to pass wait_queue pointer to wake function */
49653e1f0718SJames Smart 	pmboxq->ctx_ndlp = dd_data;
49663b5dd52aSJames Smart 	dd_data->type = TYPE_MBOX;
4967a33c4f7bSJames Smart 	dd_data->set_job = job;
49683b5dd52aSJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
49697ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
49707a470277SJames Smart 	dd_data->context_un.mbox.ext = ext;
49717a470277SJames Smart 	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
49727a470277SJames Smart 	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4973c7495937SJames Smart 	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
49743b5dd52aSJames Smart 	job->dd_data = dd_data;
49757a470277SJames Smart 
49767a470277SJames Smart 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
49777a470277SJames Smart 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
49787a470277SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
49797a470277SJames Smart 		if (rc != MBX_SUCCESS) {
49807a470277SJames Smart 			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
49817a470277SJames Smart 			goto job_done;
49823b5dd52aSJames Smart 		}
49833b5dd52aSJames Smart 
49847a470277SJames Smart 		/* job finished, copy the data */
49857ad20aa9SJames Smart 		memcpy(pmbx, pmb, sizeof(*pmb));
498601e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
49877a470277SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
49887a470277SJames Smart 					    job->reply_payload.sg_cnt,
49897ad20aa9SJames Smart 					    pmbx, size);
49907a470277SJames Smart 		/* not waiting mbox already done */
49917a470277SJames Smart 		rc = 0;
49927a470277SJames Smart 		goto job_done;
49937a470277SJames Smart 	}
49947a470277SJames Smart 
49957a470277SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
49967a470277SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
49977a470277SJames Smart 		return 1; /* job started */
49987a470277SJames Smart 
49997a470277SJames Smart job_done:
50007a470277SJames Smart 	/* common exit for error or job completed inline */
50017a470277SJames Smart 	if (pmboxq)
50027a470277SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
50037ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
50047a470277SJames Smart 	kfree(dd_data);
50057a470277SJames Smart 
50067ad20aa9SJames Smart job_cont:
50077a470277SJames Smart 	return rc;
50083b5dd52aSJames Smart }
50093b5dd52aSJames Smart 
50103b5dd52aSJames Smart /**
50113b5dd52aSJames Smart  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
50123b5dd52aSJames Smart  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
50133b5dd52aSJames Smart  **/
50143b5dd52aSJames Smart static int
501575cc8cfcSJohannes Thumshirn lpfc_bsg_mbox_cmd(struct bsg_job *job)
50163b5dd52aSJames Smart {
5017cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
501801e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
501901e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
50203b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
50217ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
50223b5dd52aSJames Smart 	int rc = 0;
50233b5dd52aSJames Smart 
50247ad20aa9SJames Smart 	/* mix-and-match backward compatibility */
502501e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
50263b5dd52aSJames Smart 	if (job->request_len <
50273b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
50287ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
50291051e9b3SMasanari Iida 				"2737 Mix-and-match backward compatibility "
50307ad20aa9SJames Smart 				"between MBOX_REQ old size:%d and "
50317ad20aa9SJames Smart 				"new request size:%d\n",
50327ad20aa9SJames Smart 				(int)(job->request_len -
50337ad20aa9SJames Smart 				      sizeof(struct fc_bsg_request)),
50347ad20aa9SJames Smart 				(int)sizeof(struct dfc_mbox_req));
50357ad20aa9SJames Smart 		mbox_req = (struct dfc_mbox_req *)
503601e0e15cSJohannes Thumshirn 				bsg_request->rqst_data.h_vendor.vendor_cmd;
50377ad20aa9SJames Smart 		mbox_req->extMboxTag = 0;
50387ad20aa9SJames Smart 		mbox_req->extSeqNum = 0;
50393b5dd52aSJames Smart 	}
50403b5dd52aSJames Smart 
50413b5dd52aSJames Smart 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
50423b5dd52aSJames Smart 
50433b5dd52aSJames Smart 	if (rc == 0) {
50443b5dd52aSJames Smart 		/* job done */
504501e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
50463b5dd52aSJames Smart 		job->dd_data = NULL;
504706548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
50481abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
50493b5dd52aSJames Smart 	} else if (rc == 1)
50503b5dd52aSJames Smart 		/* job submitted, will complete later*/
50513b5dd52aSJames Smart 		rc = 0; /* return zero, no error */
50523b5dd52aSJames Smart 	else {
50533b5dd52aSJames Smart 		/* some error occurred */
505401e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
50553b5dd52aSJames Smart 		job->dd_data = NULL;
50563b5dd52aSJames Smart 	}
50573b5dd52aSJames Smart 
50583b5dd52aSJames Smart 	return rc;
50593b5dd52aSJames Smart }
50603b5dd52aSJames Smart 
50613b5dd52aSJames Smart /**
5062e2aed29fSJames Smart  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
5063e2aed29fSJames Smart  * @phba: Pointer to HBA context object.
5064e2aed29fSJames Smart  * @cmdiocbq: Pointer to command iocb.
5065e2aed29fSJames Smart  * @rspiocbq: Pointer to response iocb.
5066e2aed29fSJames Smart  *
5067e2aed29fSJames Smart  * This function is the completion handler for iocbs issued using
5068e2aed29fSJames Smart  * lpfc_menlo_cmd function. This function is called by the
5069e2aed29fSJames Smart  * ring event handler function without any lock held. This function
5070e2aed29fSJames Smart  * can be called from both worker thread context and interrupt
5071e2aed29fSJames Smart  * context. This function also can be called from another thread which
5072e2aed29fSJames Smart  * cleans up the SLI layer objects.
5073e2aed29fSJames Smart  * This function copies the contents of the response iocb to the
5074e2aed29fSJames Smart  * response iocb memory object provided by the caller of
5075e2aed29fSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5076e2aed29fSJames Smart  * sleeps for the iocb completion.
5077e2aed29fSJames Smart  **/
5078e2aed29fSJames Smart static void
5079e2aed29fSJames Smart lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
5080e2aed29fSJames Smart 			struct lpfc_iocbq *cmdiocbq,
5081e2aed29fSJames Smart 			struct lpfc_iocbq *rspiocbq)
5082e2aed29fSJames Smart {
5083e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
508475cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
508501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
5086e2aed29fSJames Smart 	IOCB_t *rsp;
5087a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
5088e2aed29fSJames Smart 	struct lpfc_bsg_menlo *menlo;
5089e2aed29fSJames Smart 	unsigned long flags;
5090e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
5091a33c4f7bSJames Smart 	unsigned int rsp_size;
5092e2aed29fSJames Smart 	int rc = 0;
5093e2aed29fSJames Smart 
5094e2aed29fSJames Smart 	dd_data = cmdiocbq->context1;
5095a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
5096a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
5097e2aed29fSJames Smart 	menlo = &dd_data->context_un.menlo;
5098a33c4f7bSJames Smart 	rmp = menlo->rmp;
5099e2aed29fSJames Smart 	rsp = &rspiocbq->iocb;
5100e2aed29fSJames Smart 
5101a33c4f7bSJames Smart 	/* Determine if job has been aborted */
5102a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
5103a33c4f7bSJames Smart 	job = dd_data->set_job;
5104a33c4f7bSJames Smart 	if (job) {
510501e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
5106a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
5107a33c4f7bSJames Smart 		job->dd_data = NULL;
5108a33c4f7bSJames Smart 	}
5109a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5110e2aed29fSJames Smart 
5111a33c4f7bSJames Smart 	/* Copy the job data or set the failing status for the job */
5112a33c4f7bSJames Smart 
5113a33c4f7bSJames Smart 	if (job) {
5114e2aed29fSJames Smart 		/* always return the xri, this would be used in the case
5115a33c4f7bSJames Smart 		 * of a menlo download to allow the data to be sent as a
5116a33c4f7bSJames Smart 		 * continuation of the exchange.
5117e2aed29fSJames Smart 		 */
5118a33c4f7bSJames Smart 
5119e2aed29fSJames Smart 		menlo_resp = (struct menlo_response *)
512001e0e15cSJohannes Thumshirn 			bsg_reply->reply_data.vendor_reply.vendor_rsp;
5121e2aed29fSJames Smart 		menlo_resp->xri = rsp->ulpContext;
5122e2aed29fSJames Smart 		if (rsp->ulpStatus) {
5123e2aed29fSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
5124e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
5125e2aed29fSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
5126e2aed29fSJames Smart 					rc = -ETIMEDOUT;
5127e2aed29fSJames Smart 					break;
5128e2aed29fSJames Smart 				case IOERR_INVALID_RPI:
5129e2aed29fSJames Smart 					rc = -EFAULT;
5130e2aed29fSJames Smart 					break;
5131e2aed29fSJames Smart 				default:
5132e2aed29fSJames Smart 					rc = -EACCES;
5133e2aed29fSJames Smart 					break;
5134e2aed29fSJames Smart 				}
5135a33c4f7bSJames Smart 			} else {
5136e2aed29fSJames Smart 				rc = -EACCES;
5137a33c4f7bSJames Smart 			}
5138a33c4f7bSJames Smart 		} else {
5139a33c4f7bSJames Smart 			rsp_size = rsp->un.genreq64.bdl.bdeSize;
514001e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
5141a33c4f7bSJames Smart 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
5142a33c4f7bSJames Smart 						   rsp_size, 0);
5143a33c4f7bSJames Smart 		}
5144e2aed29fSJames Smart 
5145a33c4f7bSJames Smart 	}
5146a33c4f7bSJames Smart 
5147e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5148a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
5149a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
5150a33c4f7bSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5151e2aed29fSJames Smart 	kfree(bmp);
5152e2aed29fSJames Smart 	kfree(dd_data);
5153a33c4f7bSJames Smart 
5154a33c4f7bSJames Smart 	/* Complete the job if active */
5155a33c4f7bSJames Smart 
5156a33c4f7bSJames Smart 	if (job) {
515701e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
515806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
51591abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
5160a33c4f7bSJames Smart 	}
5161a33c4f7bSJames Smart 
5162e2aed29fSJames Smart 	return;
5163e2aed29fSJames Smart }
5164e2aed29fSJames Smart 
5165e2aed29fSJames Smart /**
5166e2aed29fSJames Smart  * lpfc_menlo_cmd - send an ioctl for menlo hardware
5167e2aed29fSJames Smart  * @job: fc_bsg_job to handle
5168e2aed29fSJames Smart  *
5169e2aed29fSJames Smart  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5170e2aed29fSJames Smart  * all the command completions will return the xri for the command.
5171e2aed29fSJames Smart  * For menlo data requests a gen request 64 CX is used to continue the exchange
5172e2aed29fSJames Smart  * supplied in the menlo request header xri field.
5173e2aed29fSJames Smart  **/
5174e2aed29fSJames Smart static int
517575cc8cfcSJohannes Thumshirn lpfc_menlo_cmd(struct bsg_job *job)
5176e2aed29fSJames Smart {
5177cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
517801e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
517901e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5180e2aed29fSJames Smart 	struct lpfc_hba *phba = vport->phba;
5181a33c4f7bSJames Smart 	struct lpfc_iocbq *cmdiocbq;
5182a33c4f7bSJames Smart 	IOCB_t *cmd;
5183e2aed29fSJames Smart 	int rc = 0;
5184e2aed29fSJames Smart 	struct menlo_command *menlo_cmd;
5185a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
5186e2aed29fSJames Smart 	int request_nseg;
5187e2aed29fSJames Smart 	int reply_nseg;
5188e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
5189e2aed29fSJames Smart 	struct ulp_bde64 *bpl = NULL;
5190e2aed29fSJames Smart 
5191e2aed29fSJames Smart 	/* in case no data is returned return just the return code */
519201e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
5193e2aed29fSJames Smart 
5194e2aed29fSJames Smart 	if (job->request_len <
5195e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) +
5196e2aed29fSJames Smart 		sizeof(struct menlo_command)) {
5197e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5198e2aed29fSJames Smart 				"2784 Received MENLO_CMD request below "
5199e2aed29fSJames Smart 				"minimum size\n");
5200e2aed29fSJames Smart 		rc = -ERANGE;
5201e2aed29fSJames Smart 		goto no_dd_data;
5202e2aed29fSJames Smart 	}
5203e2aed29fSJames Smart 
5204e2aed29fSJames Smart 	if (job->reply_len <
5205e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5206e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5207e2aed29fSJames Smart 				"2785 Received MENLO_CMD reply below "
5208e2aed29fSJames Smart 				"minimum size\n");
5209e2aed29fSJames Smart 		rc = -ERANGE;
5210e2aed29fSJames Smart 		goto no_dd_data;
5211e2aed29fSJames Smart 	}
5212e2aed29fSJames Smart 
5213e2aed29fSJames Smart 	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5214e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5215e2aed29fSJames Smart 				"2786 Adapter does not support menlo "
5216e2aed29fSJames Smart 				"commands\n");
5217e2aed29fSJames Smart 		rc = -EPERM;
5218e2aed29fSJames Smart 		goto no_dd_data;
5219e2aed29fSJames Smart 	}
5220e2aed29fSJames Smart 
5221e2aed29fSJames Smart 	menlo_cmd = (struct menlo_command *)
522201e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5223e2aed29fSJames Smart 
5224e2aed29fSJames Smart 	/* allocate our bsg tracking structure */
5225e2aed29fSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5226e2aed29fSJames Smart 	if (!dd_data) {
5227e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5228e2aed29fSJames Smart 				"2787 Failed allocation of dd_data\n");
5229e2aed29fSJames Smart 		rc = -ENOMEM;
5230e2aed29fSJames Smart 		goto no_dd_data;
5231e2aed29fSJames Smart 	}
5232e2aed29fSJames Smart 
5233e2aed29fSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5234e2aed29fSJames Smart 	if (!bmp) {
5235e2aed29fSJames Smart 		rc = -ENOMEM;
5236e2aed29fSJames Smart 		goto free_dd;
5237e2aed29fSJames Smart 	}
5238e2aed29fSJames Smart 
5239a33c4f7bSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5240a33c4f7bSJames Smart 	if (!bmp->virt) {
5241e2aed29fSJames Smart 		rc = -ENOMEM;
5242e2aed29fSJames Smart 		goto free_bmp;
5243e2aed29fSJames Smart 	}
5244e2aed29fSJames Smart 
5245e2aed29fSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5246a33c4f7bSJames Smart 
5247e2aed29fSJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
5248a33c4f7bSJames Smart 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5249a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5250a33c4f7bSJames Smart 				     1, bpl, &request_nseg);
5251a33c4f7bSJames Smart 	if (!cmp) {
5252a33c4f7bSJames Smart 		rc = -ENOMEM;
5253a33c4f7bSJames Smart 		goto free_bmp;
5254a33c4f7bSJames Smart 	}
5255a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
5256a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
5257a33c4f7bSJames Smart 
5258a33c4f7bSJames Smart 	bpl += request_nseg;
5259a33c4f7bSJames Smart 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5260a33c4f7bSJames Smart 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5261a33c4f7bSJames Smart 				     bpl, &reply_nseg);
5262a33c4f7bSJames Smart 	if (!rmp) {
5263a33c4f7bSJames Smart 		rc = -ENOMEM;
5264a33c4f7bSJames Smart 		goto free_cmp;
5265e2aed29fSJames Smart 	}
5266e2aed29fSJames Smart 
5267a33c4f7bSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
5268a33c4f7bSJames Smart 	if (!cmdiocbq) {
5269a33c4f7bSJames Smart 		rc = -ENOMEM;
5270a33c4f7bSJames Smart 		goto free_rmp;
5271e2aed29fSJames Smart 	}
5272e2aed29fSJames Smart 
5273e2aed29fSJames Smart 	cmd = &cmdiocbq->iocb;
5274e2aed29fSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5275e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5276e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5277e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5278e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
5279e2aed29fSJames Smart 	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5280e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5281e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5282e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5283e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5284e2aed29fSJames Smart 	cmd->ulpBdeCount = 1;
5285e2aed29fSJames Smart 	cmd->ulpClass = CLASS3;
5286e2aed29fSJames Smart 	cmd->ulpOwner = OWN_CHIP;
5287e2aed29fSJames Smart 	cmd->ulpLe = 1; /* Limited Edition */
5288e2aed29fSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5289e2aed29fSJames Smart 	cmdiocbq->vport = phba->pport;
5290e2aed29fSJames Smart 	/* We want the firmware to timeout before we do */
5291e2aed29fSJames Smart 	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
5292e2aed29fSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5293e2aed29fSJames Smart 	cmdiocbq->context1 = dd_data;
5294a33c4f7bSJames Smart 	cmdiocbq->context2 = cmp;
5295a33c4f7bSJames Smart 	cmdiocbq->context3 = bmp;
5296e2aed29fSJames Smart 	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5297e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5298e2aed29fSJames Smart 		cmd->ulpPU = MENLO_PU; /* 3 */
5299e2aed29fSJames Smart 		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5300e2aed29fSJames Smart 		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5301e2aed29fSJames Smart 	} else {
5302e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5303e2aed29fSJames Smart 		cmd->ulpPU = 1;
5304e2aed29fSJames Smart 		cmd->un.ulpWord[4] = 0;
5305e2aed29fSJames Smart 		cmd->ulpContext = menlo_cmd->xri;
5306e2aed29fSJames Smart 	}
5307e2aed29fSJames Smart 
5308e2aed29fSJames Smart 	dd_data->type = TYPE_MENLO;
5309a33c4f7bSJames Smart 	dd_data->set_job = job;
5310e2aed29fSJames Smart 	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5311a33c4f7bSJames Smart 	dd_data->context_un.menlo.rmp = rmp;
5312a33c4f7bSJames Smart 	job->dd_data = dd_data;
5313e2aed29fSJames Smart 
5314e2aed29fSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5315e2aed29fSJames Smart 		MENLO_TIMEOUT - 5);
5316e2aed29fSJames Smart 	if (rc == IOCB_SUCCESS)
5317e2aed29fSJames Smart 		return 0; /* done for now */
5318e2aed29fSJames Smart 
5319e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5320a33c4f7bSJames Smart 
5321a33c4f7bSJames Smart free_rmp:
5322a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
5323a33c4f7bSJames Smart free_cmp:
5324a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
5325e2aed29fSJames Smart free_bmp:
5326a33c4f7bSJames Smart 	if (bmp->virt)
5327a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5328e2aed29fSJames Smart 	kfree(bmp);
5329e2aed29fSJames Smart free_dd:
5330e2aed29fSJames Smart 	kfree(dd_data);
5331e2aed29fSJames Smart no_dd_data:
5332e2aed29fSJames Smart 	/* make error code available to userspace */
533301e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
5334e2aed29fSJames Smart 	job->dd_data = NULL;
5335e2aed29fSJames Smart 	return rc;
5336e2aed29fSJames Smart }
5337b6e3b9c6SJames Smart 
5338c691816eSJames Smart static int
533975cc8cfcSJohannes Thumshirn lpfc_forced_link_speed(struct bsg_job *job)
5340c691816eSJames Smart {
5341cd21c605SJohannes Thumshirn 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5342c691816eSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5343c691816eSJames Smart 	struct lpfc_hba *phba = vport->phba;
534401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5345c691816eSJames Smart 	struct forced_link_speed_support_reply *forced_reply;
5346c691816eSJames Smart 	int rc = 0;
5347c691816eSJames Smart 
5348c691816eSJames Smart 	if (job->request_len <
5349c691816eSJames Smart 	    sizeof(struct fc_bsg_request) +
5350c691816eSJames Smart 	    sizeof(struct get_forced_link_speed_support)) {
5351c691816eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5352c691816eSJames Smart 				"0048 Received FORCED_LINK_SPEED request "
5353c691816eSJames Smart 				"below minimum size\n");
5354c691816eSJames Smart 		rc = -EINVAL;
5355c691816eSJames Smart 		goto job_error;
5356c691816eSJames Smart 	}
5357c691816eSJames Smart 
5358c691816eSJames Smart 	forced_reply = (struct forced_link_speed_support_reply *)
535901e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5360c691816eSJames Smart 
5361c691816eSJames Smart 	if (job->reply_len <
5362c691816eSJames Smart 	    sizeof(struct fc_bsg_request) +
5363c691816eSJames Smart 	    sizeof(struct forced_link_speed_support_reply)) {
5364c691816eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5365c691816eSJames Smart 				"0049 Received FORCED_LINK_SPEED reply below "
5366c691816eSJames Smart 				"minimum size\n");
5367c691816eSJames Smart 		rc = -EINVAL;
5368c691816eSJames Smart 		goto job_error;
5369c691816eSJames Smart 	}
5370c691816eSJames Smart 
5371c691816eSJames Smart 	forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
5372c691816eSJames Smart 				   ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5373c691816eSJames Smart 				   : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5374c691816eSJames Smart job_error:
537501e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
5376c691816eSJames Smart 	if (rc == 0)
537706548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
53781abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
5379c691816eSJames Smart 	return rc;
5380c691816eSJames Smart }
5381c691816eSJames Smart 
5382e2aed29fSJames Smart /**
5383d2cc9bcdSJames Smart  * lpfc_check_fwlog_support: Check FW log support on the adapter
5384d2cc9bcdSJames Smart  * @phba: Pointer to HBA context object.
5385d2cc9bcdSJames Smart  *
5386d2cc9bcdSJames Smart  * Check if FW Logging support by the adapter
5387d2cc9bcdSJames Smart  **/
5388d2cc9bcdSJames Smart int
5389d2cc9bcdSJames Smart lpfc_check_fwlog_support(struct lpfc_hba *phba)
5390d2cc9bcdSJames Smart {
5391d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5392d2cc9bcdSJames Smart 
5393d2cc9bcdSJames Smart 	ras_fwlog = &phba->ras_fwlog;
5394d2cc9bcdSJames Smart 
5395d2cc9bcdSJames Smart 	if (ras_fwlog->ras_hwsupport == false)
5396d2cc9bcdSJames Smart 		return -EACCES;
5397d2cc9bcdSJames Smart 	else if (ras_fwlog->ras_enabled == false)
5398d2cc9bcdSJames Smart 		return -EPERM;
5399d2cc9bcdSJames Smart 	else
5400d2cc9bcdSJames Smart 		return 0;
5401d2cc9bcdSJames Smart }
5402d2cc9bcdSJames Smart 
5403d2cc9bcdSJames Smart /**
5404d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_config: Get RAS configuration settings
5405d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5406d2cc9bcdSJames Smart  *
5407d2cc9bcdSJames Smart  * Get RAS configuration values set.
5408d2cc9bcdSJames Smart  **/
5409d2cc9bcdSJames Smart static int
5410d2cc9bcdSJames Smart lpfc_bsg_get_ras_config(struct bsg_job *job)
5411d2cc9bcdSJames Smart {
5412d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5413d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5414d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5415d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5416d2cc9bcdSJames Smart 	struct lpfc_bsg_get_ras_config_reply *ras_reply;
5417d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5418d2cc9bcdSJames Smart 	int rc = 0;
5419d2cc9bcdSJames Smart 
5420d2cc9bcdSJames Smart 	if (job->request_len <
5421d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5422d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_ras_req)) {
5423d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5424cb34990bSJames Smart 				"6192 FW_LOG request received "
5425d2cc9bcdSJames Smart 				"below minimum size\n");
5426d2cc9bcdSJames Smart 		rc = -EINVAL;
5427d2cc9bcdSJames Smart 		goto ras_job_error;
5428d2cc9bcdSJames Smart 	}
5429d2cc9bcdSJames Smart 
5430d2cc9bcdSJames Smart 	/* Check FW log status */
5431d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5432cb34990bSJames Smart 	if (rc)
5433d2cc9bcdSJames Smart 		goto ras_job_error;
5434d2cc9bcdSJames Smart 
5435d2cc9bcdSJames Smart 	ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5436d2cc9bcdSJames Smart 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5437d2cc9bcdSJames Smart 
5438d2cc9bcdSJames Smart 	/* Current logging state */
543995bfc6d8SJames Smart 	spin_lock_irq(&phba->hbalock);
544095bfc6d8SJames Smart 	if (ras_fwlog->state == ACTIVE)
5441d2cc9bcdSJames Smart 		ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5442d2cc9bcdSJames Smart 	else
5443d2cc9bcdSJames Smart 		ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
544495bfc6d8SJames Smart 	spin_unlock_irq(&phba->hbalock);
5445d2cc9bcdSJames Smart 
5446d2cc9bcdSJames Smart 	ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5447d2cc9bcdSJames Smart 	ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5448d2cc9bcdSJames Smart 
5449d2cc9bcdSJames Smart ras_job_error:
5450d2cc9bcdSJames Smart 	/* make error code available to userspace */
5451d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5452d2cc9bcdSJames Smart 
5453d2cc9bcdSJames Smart 	/* complete the job back to userspace */
54546db51abbSJames Smart 	if (!rc)
54556db51abbSJames Smart 		bsg_job_done(job, bsg_reply->result,
54566db51abbSJames Smart 			     bsg_reply->reply_payload_rcv_len);
5457d2cc9bcdSJames Smart 	return rc;
5458d2cc9bcdSJames Smart }
5459d2cc9bcdSJames Smart 
5460d2cc9bcdSJames Smart /**
5461d2cc9bcdSJames Smart  * lpfc_bsg_set_ras_config: Set FW logging parameters
5462d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5463d2cc9bcdSJames Smart  *
5464d2cc9bcdSJames Smart  * Set log-level parameters for FW-logging in host memory
5465d2cc9bcdSJames Smart  **/
5466d2cc9bcdSJames Smart static int
5467d2cc9bcdSJames Smart lpfc_bsg_set_ras_config(struct bsg_job *job)
5468d2cc9bcdSJames Smart {
5469d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5470d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5471d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5472d2cc9bcdSJames Smart 	struct lpfc_bsg_set_ras_config_req *ras_req;
5473d2cc9bcdSJames Smart 	struct fc_bsg_request *bsg_request = job->request;
5474d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5475d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5476d2cc9bcdSJames Smart 	uint8_t action = 0, log_level = 0;
5477191e2f74SJames Smart 	int rc = 0, action_status = 0;
5478d2cc9bcdSJames Smart 
5479d2cc9bcdSJames Smart 	if (job->request_len <
5480d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5481d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_set_ras_config_req)) {
5482d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5483d2cc9bcdSJames Smart 				"6182 Received RAS_LOG request "
5484d2cc9bcdSJames Smart 				"below minimum size\n");
5485d2cc9bcdSJames Smart 		rc = -EINVAL;
5486d2cc9bcdSJames Smart 		goto ras_job_error;
5487d2cc9bcdSJames Smart 	}
5488d2cc9bcdSJames Smart 
5489d2cc9bcdSJames Smart 	/* Check FW log status */
5490d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5491cb34990bSJames Smart 	if (rc)
5492d2cc9bcdSJames Smart 		goto ras_job_error;
5493d2cc9bcdSJames Smart 
5494d2cc9bcdSJames Smart 	ras_req = (struct lpfc_bsg_set_ras_config_req *)
5495d2cc9bcdSJames Smart 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5496d2cc9bcdSJames Smart 	action = ras_req->action;
5497d2cc9bcdSJames Smart 	log_level = ras_req->log_level;
5498d2cc9bcdSJames Smart 
5499d2cc9bcdSJames Smart 	if (action == LPFC_RASACTION_STOP_LOGGING) {
5500d2cc9bcdSJames Smart 		/* Check if already disabled */
550195bfc6d8SJames Smart 		spin_lock_irq(&phba->hbalock);
550295bfc6d8SJames Smart 		if (ras_fwlog->state != ACTIVE) {
550395bfc6d8SJames Smart 			spin_unlock_irq(&phba->hbalock);
5504d2cc9bcdSJames Smart 			rc = -ESRCH;
5505d2cc9bcdSJames Smart 			goto ras_job_error;
5506d2cc9bcdSJames Smart 		}
550795bfc6d8SJames Smart 		spin_unlock_irq(&phba->hbalock);
5508d2cc9bcdSJames Smart 
5509d2cc9bcdSJames Smart 		/* Disable logging */
5510d2cc9bcdSJames Smart 		lpfc_ras_stop_fwlog(phba);
5511d2cc9bcdSJames Smart 	} else {
5512d2cc9bcdSJames Smart 		/*action = LPFC_RASACTION_START_LOGGING*/
5513191e2f74SJames Smart 
5514191e2f74SJames Smart 		/* Even though FW-logging is active re-initialize
5515191e2f74SJames Smart 		 * FW-logging with new log-level. Return status
5516191e2f74SJames Smart 		 * "Logging already Running" to caller.
5517191e2f74SJames Smart 		 **/
551895bfc6d8SJames Smart 		spin_lock_irq(&phba->hbalock);
551995bfc6d8SJames Smart 		if (ras_fwlog->state != INACTIVE)
5520191e2f74SJames Smart 			action_status = -EINPROGRESS;
552195bfc6d8SJames Smart 		spin_unlock_irq(&phba->hbalock);
5522d2cc9bcdSJames Smart 
5523d2cc9bcdSJames Smart 		/* Enable logging */
5524d2cc9bcdSJames Smart 		rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5525d2cc9bcdSJames Smart 					      LPFC_RAS_ENABLE_LOGGING);
5526191e2f74SJames Smart 		if (rc) {
5527d2cc9bcdSJames Smart 			rc = -EINVAL;
5528191e2f74SJames Smart 			goto ras_job_error;
5529191e2f74SJames Smart 		}
5530191e2f74SJames Smart 
5531191e2f74SJames Smart 		/* Check if FW-logging is re-initialized */
5532191e2f74SJames Smart 		if (action_status == -EINPROGRESS)
5533191e2f74SJames Smart 			rc = action_status;
5534d2cc9bcdSJames Smart 	}
5535d2cc9bcdSJames Smart ras_job_error:
5536d2cc9bcdSJames Smart 	/* make error code available to userspace */
5537d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5538d2cc9bcdSJames Smart 
5539d2cc9bcdSJames Smart 	/* complete the job back to userspace */
55406db51abbSJames Smart 	if (!rc)
5541d2cc9bcdSJames Smart 		bsg_job_done(job, bsg_reply->result,
5542d2cc9bcdSJames Smart 			     bsg_reply->reply_payload_rcv_len);
5543d2cc9bcdSJames Smart 
5544d2cc9bcdSJames Smart 	return rc;
5545d2cc9bcdSJames Smart }
5546d2cc9bcdSJames Smart 
5547d2cc9bcdSJames Smart /**
5548d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_lwpd: Get log write position data
5549d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5550d2cc9bcdSJames Smart  *
5551d2cc9bcdSJames Smart  * Get Offset/Wrap count of the log message written
5552d2cc9bcdSJames Smart  * in host memory
5553d2cc9bcdSJames Smart  **/
5554d2cc9bcdSJames Smart static int
5555d2cc9bcdSJames Smart lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5556d2cc9bcdSJames Smart {
5557d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5558d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5559d2cc9bcdSJames Smart 	struct lpfc_bsg_get_ras_lwpd *ras_reply;
5560d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5561d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5562d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5563191e2f74SJames Smart 	u32 *lwpd_ptr = NULL;
5564d2cc9bcdSJames Smart 	int rc = 0;
5565d2cc9bcdSJames Smart 
5566d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5567cb34990bSJames Smart 	if (rc)
5568d2cc9bcdSJames Smart 		goto ras_job_error;
5569d2cc9bcdSJames Smart 
5570d2cc9bcdSJames Smart 	if (job->request_len <
5571d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5572d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_ras_req)) {
5573d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5574d2cc9bcdSJames Smart 				"6183 Received RAS_LOG request "
5575d2cc9bcdSJames Smart 				"below minimum size\n");
5576d2cc9bcdSJames Smart 		rc = -EINVAL;
5577d2cc9bcdSJames Smart 		goto ras_job_error;
5578d2cc9bcdSJames Smart 	}
5579d2cc9bcdSJames Smart 
5580d2cc9bcdSJames Smart 	ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5581d2cc9bcdSJames Smart 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5582d2cc9bcdSJames Smart 
5583cb34990bSJames Smart 	if (!ras_fwlog->lwpd.virt) {
5584cb34990bSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5585cb34990bSJames Smart 				"6193 Restart FW Logging\n");
55861165a5c2SJames Smart 		rc = -EINVAL;
55871165a5c2SJames Smart 		goto ras_job_error;
5588cb34990bSJames Smart 	}
5589cb34990bSJames Smart 
5590191e2f74SJames Smart 	/* Get lwpd offset */
5591191e2f74SJames Smart 	lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5592191e2f74SJames Smart 	ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5593d2cc9bcdSJames Smart 
5594191e2f74SJames Smart 	/* Get wrap count */
5595191e2f74SJames Smart 	ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5596d2cc9bcdSJames Smart 
5597d2cc9bcdSJames Smart ras_job_error:
5598d2cc9bcdSJames Smart 	/* make error code available to userspace */
5599d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5600d2cc9bcdSJames Smart 
5601d2cc9bcdSJames Smart 	/* complete the job back to userspace */
56026db51abbSJames Smart 	if (!rc)
56036db51abbSJames Smart 		bsg_job_done(job, bsg_reply->result,
56046db51abbSJames Smart 			     bsg_reply->reply_payload_rcv_len);
5605d2cc9bcdSJames Smart 
5606d2cc9bcdSJames Smart 	return rc;
5607d2cc9bcdSJames Smart }
5608d2cc9bcdSJames Smart 
5609d2cc9bcdSJames Smart /**
5610d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_fwlog: Read FW log
5611d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5612d2cc9bcdSJames Smart  *
5613d2cc9bcdSJames Smart  * Copy the FW log into the passed buffer.
5614d2cc9bcdSJames Smart  **/
5615d2cc9bcdSJames Smart static int
5616d2cc9bcdSJames Smart lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5617d2cc9bcdSJames Smart {
5618d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5619d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5620d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5621d2cc9bcdSJames Smart 	struct fc_bsg_request *bsg_request = job->request;
5622d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5623d2cc9bcdSJames Smart 	struct lpfc_bsg_get_fwlog_req *ras_req;
5624191e2f74SJames Smart 	u32 rd_offset, rd_index, offset;
5625191e2f74SJames Smart 	void *src, *fwlog_buff;
5626d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5627d2cc9bcdSJames Smart 	struct lpfc_dmabuf *dmabuf, *next;
5628d2cc9bcdSJames Smart 	int rc = 0;
5629d2cc9bcdSJames Smart 
5630d2cc9bcdSJames Smart 	ras_fwlog = &phba->ras_fwlog;
5631d2cc9bcdSJames Smart 
5632d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5633cb34990bSJames Smart 	if (rc)
5634d2cc9bcdSJames Smart 		goto ras_job_error;
5635d2cc9bcdSJames Smart 
5636d2cc9bcdSJames Smart 	/* Logging to be stopped before reading */
563795bfc6d8SJames Smart 	spin_lock_irq(&phba->hbalock);
563895bfc6d8SJames Smart 	if (ras_fwlog->state == ACTIVE) {
563995bfc6d8SJames Smart 		spin_unlock_irq(&phba->hbalock);
5640d2cc9bcdSJames Smart 		rc = -EINPROGRESS;
5641d2cc9bcdSJames Smart 		goto ras_job_error;
5642d2cc9bcdSJames Smart 	}
564395bfc6d8SJames Smart 	spin_unlock_irq(&phba->hbalock);
5644d2cc9bcdSJames Smart 
5645d2cc9bcdSJames Smart 	if (job->request_len <
5646d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5647d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_get_fwlog_req)) {
5648d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5649d2cc9bcdSJames Smart 				"6184 Received RAS_LOG request "
5650d2cc9bcdSJames Smart 				"below minimum size\n");
5651d2cc9bcdSJames Smart 		rc = -EINVAL;
5652d2cc9bcdSJames Smart 		goto ras_job_error;
5653d2cc9bcdSJames Smart 	}
5654d2cc9bcdSJames Smart 
5655d2cc9bcdSJames Smart 	ras_req = (struct lpfc_bsg_get_fwlog_req *)
5656d2cc9bcdSJames Smart 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5657d2cc9bcdSJames Smart 	rd_offset = ras_req->read_offset;
5658d2cc9bcdSJames Smart 
5659d2cc9bcdSJames Smart 	/* Allocate memory to read fw log*/
5660d2cc9bcdSJames Smart 	fwlog_buff = vmalloc(ras_req->read_size);
5661d2cc9bcdSJames Smart 	if (!fwlog_buff) {
5662d2cc9bcdSJames Smart 		rc = -ENOMEM;
5663d2cc9bcdSJames Smart 		goto ras_job_error;
5664d2cc9bcdSJames Smart 	}
5665d2cc9bcdSJames Smart 
5666d2cc9bcdSJames Smart 	rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5667d2cc9bcdSJames Smart 	offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5668d2cc9bcdSJames Smart 
5669d2cc9bcdSJames Smart 	list_for_each_entry_safe(dmabuf, next,
5670d2cc9bcdSJames Smart 			      &ras_fwlog->fwlog_buff_list, list) {
5671d2cc9bcdSJames Smart 
5672d2cc9bcdSJames Smart 		if (dmabuf->buffer_tag < rd_index)
5673d2cc9bcdSJames Smart 			continue;
5674d2cc9bcdSJames Smart 
5675d2cc9bcdSJames Smart 		src = dmabuf->virt + offset;
5676191e2f74SJames Smart 		memcpy(fwlog_buff, src, ras_req->read_size);
5677d2cc9bcdSJames Smart 		break;
5678d2cc9bcdSJames Smart 	}
5679d2cc9bcdSJames Smart 
5680d2cc9bcdSJames Smart 	bsg_reply->reply_payload_rcv_len =
5681d2cc9bcdSJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
5682d2cc9bcdSJames Smart 				    job->reply_payload.sg_cnt,
5683d2cc9bcdSJames Smart 				    fwlog_buff, ras_req->read_size);
5684d2cc9bcdSJames Smart 
5685d2cc9bcdSJames Smart 	vfree(fwlog_buff);
5686d2cc9bcdSJames Smart 
5687d2cc9bcdSJames Smart ras_job_error:
5688d2cc9bcdSJames Smart 	bsg_reply->result = rc;
56896db51abbSJames Smart 	if (!rc)
56906db51abbSJames Smart 		bsg_job_done(job, bsg_reply->result,
56916db51abbSJames Smart 			     bsg_reply->reply_payload_rcv_len);
5692d2cc9bcdSJames Smart 
5693d2cc9bcdSJames Smart 	return rc;
5694d2cc9bcdSJames Smart }
5695d2cc9bcdSJames Smart 
56961dc5ec24SJames Smart static int
56971dc5ec24SJames Smart lpfc_get_trunk_info(struct bsg_job *job)
56981dc5ec24SJames Smart {
56991dc5ec24SJames Smart 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
57001dc5ec24SJames Smart 	struct lpfc_hba *phba = vport->phba;
57011dc5ec24SJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
57021dc5ec24SJames Smart 	struct lpfc_trunk_info *event_reply;
57031dc5ec24SJames Smart 	int rc = 0;
57041dc5ec24SJames Smart 
57051dc5ec24SJames Smart 	if (job->request_len <
57061dc5ec24SJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
57071dc5ec24SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
57081dc5ec24SJames Smart 				"2744 Received GET TRUNK _INFO request below "
57091dc5ec24SJames Smart 				"minimum size\n");
57101dc5ec24SJames Smart 		rc = -EINVAL;
57111dc5ec24SJames Smart 		goto job_error;
57121dc5ec24SJames Smart 	}
57131dc5ec24SJames Smart 
57141dc5ec24SJames Smart 	event_reply = (struct lpfc_trunk_info *)
57151dc5ec24SJames Smart 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
57161dc5ec24SJames Smart 
57171dc5ec24SJames Smart 	if (job->reply_len <
57181dc5ec24SJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct lpfc_trunk_info)) {
57191dc5ec24SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
57201dc5ec24SJames Smart 				"2728 Received GET TRUNK _INFO reply below "
57211dc5ec24SJames Smart 				"minimum size\n");
57221dc5ec24SJames Smart 		rc = -EINVAL;
57231dc5ec24SJames Smart 		goto job_error;
57241dc5ec24SJames Smart 	}
57251dc5ec24SJames Smart 	if (event_reply == NULL) {
57261dc5ec24SJames Smart 		rc = -EINVAL;
57271dc5ec24SJames Smart 		goto job_error;
57281dc5ec24SJames Smart 	}
57291dc5ec24SJames Smart 
57301dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
57311dc5ec24SJames Smart 		   (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
57321dc5ec24SJames Smart 
57331dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
57341dc5ec24SJames Smart 		   (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
57351dc5ec24SJames Smart 
57361dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
57371dc5ec24SJames Smart 		   (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
57381dc5ec24SJames Smart 
57391dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
57401dc5ec24SJames Smart 		   (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
57411dc5ec24SJames Smart 
57421dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
57431dc5ec24SJames Smart 		   (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
57441dc5ec24SJames Smart 
57451dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
57461dc5ec24SJames Smart 		   bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
57471dc5ec24SJames Smart 
57481dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
57491dc5ec24SJames Smart 		   bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
57501dc5ec24SJames Smart 
57511dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
57521dc5ec24SJames Smart 		   bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
57531dc5ec24SJames Smart 
57541dc5ec24SJames Smart 	bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
57551dc5ec24SJames Smart 		   bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
57561dc5ec24SJames Smart 
57571dc5ec24SJames Smart 	event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
57581dc5ec24SJames Smart 	event_reply->logical_speed =
5759b8e6f136SJames Smart 				phba->sli4_hba.link_state.logical_speed / 1000;
57601dc5ec24SJames Smart job_error:
57611dc5ec24SJames Smart 	bsg_reply->result = rc;
57626db51abbSJames Smart 	if (!rc)
57631dc5ec24SJames Smart 		bsg_job_done(job, bsg_reply->result,
57641dc5ec24SJames Smart 			     bsg_reply->reply_payload_rcv_len);
57651dc5ec24SJames Smart 	return rc;
57661dc5ec24SJames Smart 
57671dc5ec24SJames Smart }
5768d2cc9bcdSJames Smart 
5769d2cc9bcdSJames Smart /**
5770f1c3b0fcSJames Smart  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5771f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
57723b5dd52aSJames Smart  **/
5773f1c3b0fcSJames Smart static int
577475cc8cfcSJohannes Thumshirn lpfc_bsg_hst_vendor(struct bsg_job *job)
5775f1c3b0fcSJames Smart {
577601e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
577701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
577801e0e15cSJohannes Thumshirn 	int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
57794cc0e56eSJames Smart 	int rc;
5780f1c3b0fcSJames Smart 
5781f1c3b0fcSJames Smart 	switch (command) {
5782f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
57834cc0e56eSJames Smart 		rc = lpfc_bsg_hba_set_event(job);
5784f1c3b0fcSJames Smart 		break;
5785f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
57864cc0e56eSJames Smart 		rc = lpfc_bsg_hba_get_event(job);
5787f1c3b0fcSJames Smart 		break;
57883b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
57893b5dd52aSJames Smart 		rc = lpfc_bsg_send_mgmt_rsp(job);
57903b5dd52aSJames Smart 		break;
57913b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE:
57927ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_mode(job);
57933b5dd52aSJames Smart 		break;
57947ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE_END:
57957ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_mode_end(job);
57967ad20aa9SJames Smart 		break;
57977ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
57987ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_run(job);
57997ad20aa9SJames Smart 		break;
58007ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
58017ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_link_diag_test(job);
58023b5dd52aSJames Smart 		break;
58033b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
58043b5dd52aSJames Smart 		rc = lpfc_bsg_get_dfc_rev(job);
58053b5dd52aSJames Smart 		break;
58063b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_MBOX:
58073b5dd52aSJames Smart 		rc = lpfc_bsg_mbox_cmd(job);
58083b5dd52aSJames Smart 		break;
5809e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_CMD:
5810e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_DATA:
5811e2aed29fSJames Smart 		rc = lpfc_menlo_cmd(job);
5812e2aed29fSJames Smart 		break;
5813c691816eSJames Smart 	case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5814c691816eSJames Smart 		rc = lpfc_forced_link_speed(job);
5815c691816eSJames Smart 		break;
5816d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5817d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_lwpd(job);
5818d2cc9bcdSJames Smart 		break;
5819d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5820d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_fwlog(job);
5821d2cc9bcdSJames Smart 		break;
5822d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5823d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_config(job);
5824d2cc9bcdSJames Smart 		break;
5825d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5826d2cc9bcdSJames Smart 		rc = lpfc_bsg_set_ras_config(job);
5827d2cc9bcdSJames Smart 		break;
58281dc5ec24SJames Smart 	case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
58291dc5ec24SJames Smart 		rc = lpfc_get_trunk_info(job);
58301dc5ec24SJames Smart 		break;
5831f1c3b0fcSJames Smart 	default:
58324cc0e56eSJames Smart 		rc = -EINVAL;
583301e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
58344cc0e56eSJames Smart 		/* make error code available to userspace */
583501e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
58364cc0e56eSJames Smart 		break;
5837f1c3b0fcSJames Smart 	}
58384cc0e56eSJames Smart 
58394cc0e56eSJames Smart 	return rc;
5840f1c3b0fcSJames Smart }
5841f1c3b0fcSJames Smart 
5842f1c3b0fcSJames Smart /**
5843f1c3b0fcSJames Smart  * lpfc_bsg_request - handle a bsg request from the FC transport
5844d2cc9bcdSJames Smart  * @job: bsg_job to handle
58453b5dd52aSJames Smart  **/
5846f1c3b0fcSJames Smart int
584775cc8cfcSJohannes Thumshirn lpfc_bsg_request(struct bsg_job *job)
5848f1c3b0fcSJames Smart {
584901e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
585001e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5851f1c3b0fcSJames Smart 	uint32_t msgcode;
58524cc0e56eSJames Smart 	int rc;
5853f1c3b0fcSJames Smart 
585401e0e15cSJohannes Thumshirn 	msgcode = bsg_request->msgcode;
5855f1c3b0fcSJames Smart 	switch (msgcode) {
5856f1c3b0fcSJames Smart 	case FC_BSG_HST_VENDOR:
5857f1c3b0fcSJames Smart 		rc = lpfc_bsg_hst_vendor(job);
5858f1c3b0fcSJames Smart 		break;
5859f1c3b0fcSJames Smart 	case FC_BSG_RPT_ELS:
5860f1c3b0fcSJames Smart 		rc = lpfc_bsg_rport_els(job);
5861f1c3b0fcSJames Smart 		break;
5862f1c3b0fcSJames Smart 	case FC_BSG_RPT_CT:
58634cc0e56eSJames Smart 		rc = lpfc_bsg_send_mgmt_cmd(job);
5864f1c3b0fcSJames Smart 		break;
5865f1c3b0fcSJames Smart 	default:
58664cc0e56eSJames Smart 		rc = -EINVAL;
586701e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
58684cc0e56eSJames Smart 		/* make error code available to userspace */
586901e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
5870f1c3b0fcSJames Smart 		break;
5871f1c3b0fcSJames Smart 	}
5872f1c3b0fcSJames Smart 
5873f1c3b0fcSJames Smart 	return rc;
5874f1c3b0fcSJames Smart }
5875f1c3b0fcSJames Smart 
5876f1c3b0fcSJames Smart /**
5877f1c3b0fcSJames Smart  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5878d2cc9bcdSJames Smart  * @job: bsg_job that has timed out
5879f1c3b0fcSJames Smart  *
5880f1c3b0fcSJames Smart  * This function just aborts the job's IOCB.  The aborted IOCB will return to
5881f1c3b0fcSJames Smart  * the waiting function which will handle passing the error back to userspace
58823b5dd52aSJames Smart  **/
5883f1c3b0fcSJames Smart int
588475cc8cfcSJohannes Thumshirn lpfc_bsg_timeout(struct bsg_job *job)
5885f1c3b0fcSJames Smart {
5886cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5887f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
58884cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocb;
5889895427bdSJames Smart 	struct lpfc_sli_ring *pring;
58904cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
58914cc0e56eSJames Smart 	unsigned long flags;
5892a33c4f7bSJames Smart 	int rc = 0;
5893a33c4f7bSJames Smart 	LIST_HEAD(completions);
5894a33c4f7bSJames Smart 	struct lpfc_iocbq *check_iocb, *next_iocb;
5895a33c4f7bSJames Smart 
5896895427bdSJames Smart 	pring = lpfc_phba_elsring(phba);
58971234a6d5SDick Kennedy 	if (unlikely(!pring))
58981234a6d5SDick Kennedy 		return -EIO;
5899895427bdSJames Smart 
5900a33c4f7bSJames Smart 	/* if job's driver data is NULL, the command completed or is in the
5901a33c4f7bSJames Smart 	 * the process of completing.  In this case, return status to request
5902a33c4f7bSJames Smart 	 * so the timeout is retried.  This avoids double completion issues
5903a33c4f7bSJames Smart 	 * and the request will be pulled off the timer queue when the
5904a33c4f7bSJames Smart 	 * command's completion handler executes.  Otherwise, prevent the
5905a33c4f7bSJames Smart 	 * command's completion handler from executing the job done callback
5906a33c4f7bSJames Smart 	 * and continue processing to abort the outstanding the command.
5907a33c4f7bSJames Smart 	 */
5908f1c3b0fcSJames Smart 
59094cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
59104cc0e56eSJames Smart 	dd_data = (struct bsg_job_data *)job->dd_data;
5911a33c4f7bSJames Smart 	if (dd_data) {
5912a33c4f7bSJames Smart 		dd_data->set_job = NULL;
5913a33c4f7bSJames Smart 		job->dd_data = NULL;
5914a33c4f7bSJames Smart 	} else {
59154cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5916a33c4f7bSJames Smart 		return -EAGAIN;
59174cc0e56eSJames Smart 	}
59184cc0e56eSJames Smart 
59194cc0e56eSJames Smart 	switch (dd_data->type) {
59204cc0e56eSJames Smart 	case TYPE_IOCB:
5921a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5922a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5923a33c4f7bSJames Smart 		 * Otherwise, call abort iotag
5924a33c4f7bSJames Smart 		 */
5925a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5926b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5927b5a9b2dfSJames Smart 
5928b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
5929b5a9b2dfSJames Smart 		/* make sure the I/O abort window is still open */
59301b8d11abSJames Smart 		if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
5931b5a9b2dfSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, flags);
5932b5a9b2dfSJames Smart 			return -EAGAIN;
5933b5a9b2dfSJames Smart 		}
5934a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5935a33c4f7bSJames Smart 					 list) {
5936a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5937a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5938a33c4f7bSJames Smart 				break;
5939a33c4f7bSJames Smart 			}
5940a33c4f7bSJames Smart 		}
5941a33c4f7bSJames Smart 		if (list_empty(&completions))
5942f1c3b0fcSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5943b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
5944a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5945a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5946a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5947a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5948a33c4f7bSJames Smart 		}
59494cc0e56eSJames Smart 		break;
5950a33c4f7bSJames Smart 
59514cc0e56eSJames Smart 	case TYPE_EVT:
59524cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
59534cc0e56eSJames Smart 		break;
5954a33c4f7bSJames Smart 
59553b5dd52aSJames Smart 	case TYPE_MBOX:
5956a33c4f7bSJames Smart 		/* Update the ext buf ctx state if needed */
5957a33c4f7bSJames Smart 
59587ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
59597ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5960a33c4f7bSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
59613b5dd52aSJames Smart 		break;
5962e2aed29fSJames Smart 	case TYPE_MENLO:
5963a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5964a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5965a33c4f7bSJames Smart 		 * Otherwise, call abort iotag.
5966a33c4f7bSJames Smart 		 */
5967a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5968b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5969b5a9b2dfSJames Smart 
5970b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
5971a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5972a33c4f7bSJames Smart 					 list) {
5973a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5974a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5975a33c4f7bSJames Smart 				break;
5976a33c4f7bSJames Smart 			}
5977a33c4f7bSJames Smart 		}
5978a33c4f7bSJames Smart 		if (list_empty(&completions))
5979e2aed29fSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5980b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
5981a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5982a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5983a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5984a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5985a33c4f7bSJames Smart 		}
5986e2aed29fSJames Smart 		break;
59874cc0e56eSJames Smart 	default:
59884cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
59894cc0e56eSJames Smart 		break;
59904cc0e56eSJames Smart 	}
5991f1c3b0fcSJames Smart 
59924cc0e56eSJames Smart 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
59934cc0e56eSJames Smart 	 * otherwise an error message will be displayed on the console
59944cc0e56eSJames Smart 	 * so always return success (zero)
59954cc0e56eSJames Smart 	 */
5996a33c4f7bSJames Smart 	return rc;
5997f1c3b0fcSJames Smart }
5998