xref: /linux/drivers/scsi/lpfc/lpfc_bsg.c (revision 191e2f749370ea7f319c2b1d626e459f4ee37384)
1f1c3b0fcSJames Smart /*******************************************************************
2f1c3b0fcSJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3f1c3b0fcSJames Smart  * Fibre Channel Host Bus Adapters.                                *
4cf8037f8SJames Smart  * Copyright (C) 2017-2018 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 "
1043f1c3b0fcSJames Smart 						"found for iocbq 0x%p\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 	if (dd_data != NULL)
12804cc0e56eSJames Smart 		kfree(dd_data);
1281f1c3b0fcSJames Smart 
12824cc0e56eSJames Smart 	job->dd_data = NULL;
12834cc0e56eSJames Smart 	return rc;
1284f1c3b0fcSJames Smart }
1285f1c3b0fcSJames Smart 
1286f1c3b0fcSJames Smart /**
12874cc0e56eSJames Smart  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1288f1c3b0fcSJames Smart  * @job: GET_EVENT fc_bsg_job
12893b5dd52aSJames Smart  **/
1290f1c3b0fcSJames Smart static int
129175cc8cfcSJohannes Thumshirn lpfc_bsg_hba_get_event(struct bsg_job *job)
1292f1c3b0fcSJames Smart {
1293cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1294f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
129501e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
129601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
1297f1c3b0fcSJames Smart 	struct get_ct_event *event_req;
1298f1c3b0fcSJames Smart 	struct get_ct_event_reply *event_reply;
12999a803a74SJames Smart 	struct lpfc_bsg_event *evt, *evt_next;
1300f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
13014fede78fSJames Smart 	unsigned long flags;
13024cc0e56eSJames Smart 	uint32_t rc = 0;
1303f1c3b0fcSJames Smart 
1304f1c3b0fcSJames Smart 	if (job->request_len <
1305f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1306f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1307f1c3b0fcSJames Smart 				"2613 Received GET_CT_EVENT request below "
1308f1c3b0fcSJames Smart 				"minimum size\n");
13094cc0e56eSJames Smart 		rc = -EINVAL;
13104cc0e56eSJames Smart 		goto job_error;
1311f1c3b0fcSJames Smart 	}
1312f1c3b0fcSJames Smart 
1313f1c3b0fcSJames Smart 	event_req = (struct get_ct_event *)
131401e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1315f1c3b0fcSJames Smart 
1316f1c3b0fcSJames Smart 	event_reply = (struct get_ct_event_reply *)
131701e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
13184fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
13199a803a74SJames Smart 	list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1320f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
1321f1c3b0fcSJames Smart 			if (list_empty(&evt->events_to_get))
1322f1c3b0fcSJames Smart 				break;
13234cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1324f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1325f1c3b0fcSJames Smart 			evt_dat = list_entry(evt->events_to_get.prev,
1326f1c3b0fcSJames Smart 					     struct event_data, node);
1327f1c3b0fcSJames Smart 			list_del(&evt_dat->node);
1328f1c3b0fcSJames Smart 			break;
1329f1c3b0fcSJames Smart 		}
1330f1c3b0fcSJames Smart 	}
13314fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1332f1c3b0fcSJames Smart 
13334cc0e56eSJames Smart 	/* The app may continue to ask for event data until it gets
13344cc0e56eSJames Smart 	 * an error indicating that there isn't anymore
13354cc0e56eSJames Smart 	 */
13364cc0e56eSJames Smart 	if (evt_dat == NULL) {
133701e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
1338f1c3b0fcSJames Smart 		rc = -ENOENT;
13394cc0e56eSJames Smart 		goto job_error;
1340f1c3b0fcSJames Smart 	}
1341f1c3b0fcSJames Smart 
13424cc0e56eSJames Smart 	if (evt_dat->len > job->request_payload.payload_len) {
13434cc0e56eSJames Smart 		evt_dat->len = job->request_payload.payload_len;
1344f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1345f1c3b0fcSJames Smart 				"2618 Truncated event data at %d "
1346f1c3b0fcSJames Smart 				"bytes\n",
13474cc0e56eSJames Smart 				job->request_payload.payload_len);
1348f1c3b0fcSJames Smart 	}
1349f1c3b0fcSJames Smart 
13504cc0e56eSJames Smart 	event_reply->type = evt_dat->type;
1351f1c3b0fcSJames Smart 	event_reply->immed_data = evt_dat->immed_dat;
1352f1c3b0fcSJames Smart 	if (evt_dat->len > 0)
135301e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
13544cc0e56eSJames Smart 			sg_copy_from_buffer(job->request_payload.sg_list,
13554cc0e56eSJames Smart 					    job->request_payload.sg_cnt,
1356f1c3b0fcSJames Smart 					    evt_dat->data, evt_dat->len);
1357f1c3b0fcSJames Smart 	else
135801e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
1359f1c3b0fcSJames Smart 
13604cc0e56eSJames Smart 	if (evt_dat) {
1361f1c3b0fcSJames Smart 		kfree(evt_dat->data);
1362f1c3b0fcSJames Smart 		kfree(evt_dat);
13634cc0e56eSJames Smart 	}
13644cc0e56eSJames Smart 
13654fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
13664cc0e56eSJames Smart 	lpfc_bsg_event_unref(evt);
13674fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
13684cc0e56eSJames Smart 	job->dd_data = NULL;
136901e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
137006548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
13711abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
13724cc0e56eSJames Smart 	return 0;
1373f1c3b0fcSJames Smart 
13744cc0e56eSJames Smart job_error:
13754cc0e56eSJames Smart 	job->dd_data = NULL;
137601e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
1377f1c3b0fcSJames Smart 	return rc;
1378f1c3b0fcSJames Smart }
1379f1c3b0fcSJames Smart 
1380f1c3b0fcSJames Smart /**
13813b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
13823b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
13833b5dd52aSJames Smart  * @cmdiocbq: Pointer to command iocb.
13843b5dd52aSJames Smart  * @rspiocbq: Pointer to response iocb.
13853b5dd52aSJames Smart  *
13863b5dd52aSJames Smart  * This function is the completion handler for iocbs issued using
13873b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp function. This function is called by the
13883b5dd52aSJames Smart  * ring event handler function without any lock held. This function
13893b5dd52aSJames Smart  * can be called from both worker thread context and interrupt
13903b5dd52aSJames Smart  * context. This function also can be called from other thread which
13913b5dd52aSJames Smart  * cleans up the SLI layer objects.
13923b5dd52aSJames Smart  * This function copy the contents of the response iocb to the
13933b5dd52aSJames Smart  * response iocb memory object provided by the caller of
13943b5dd52aSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13953b5dd52aSJames Smart  * sleeps for the iocb completion.
13963b5dd52aSJames Smart  **/
13973b5dd52aSJames Smart static void
13983b5dd52aSJames Smart lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
13993b5dd52aSJames Smart 			struct lpfc_iocbq *cmdiocbq,
14003b5dd52aSJames Smart 			struct lpfc_iocbq *rspiocbq)
14013b5dd52aSJames Smart {
14023b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
140375cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
140401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
14053b5dd52aSJames Smart 	IOCB_t *rsp;
1406a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp;
14073b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp;
14083b5dd52aSJames Smart 	unsigned long flags;
14093b5dd52aSJames Smart 	int rc = 0;
14103b5dd52aSJames Smart 
1411a33c4f7bSJames Smart 	dd_data = cmdiocbq->context1;
1412a33c4f7bSJames Smart 
1413a33c4f7bSJames Smart 	/* Determine if job has been aborted */
14143b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1415a33c4f7bSJames Smart 	job = dd_data->set_job;
1416a33c4f7bSJames Smart 	if (job) {
1417a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
1418a33c4f7bSJames Smart 		job->dd_data = NULL;
14193b5dd52aSJames Smart 	}
1420a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
14213b5dd52aSJames Smart 
1422b5a9b2dfSJames Smart 	/* Close the timeout handler abort window */
1423b5a9b2dfSJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
14241b8d11abSJames Smart 	cmdiocbq->iocb_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1425b5a9b2dfSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1426b5a9b2dfSJames Smart 
14273b5dd52aSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
1428a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
1429a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
1430a33c4f7bSJames Smart 	rsp = &rspiocbq->iocb;
14313b5dd52aSJames Smart 
1432a33c4f7bSJames Smart 	/* Copy the completed job data or set the error status */
14333b5dd52aSJames Smart 
1434a33c4f7bSJames Smart 	if (job) {
143501e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
14363b5dd52aSJames Smart 		if (rsp->ulpStatus) {
14373b5dd52aSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1438e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
14393b5dd52aSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
14403b5dd52aSJames Smart 					rc = -ETIMEDOUT;
14413b5dd52aSJames Smart 					break;
14423b5dd52aSJames Smart 				case IOERR_INVALID_RPI:
14433b5dd52aSJames Smart 					rc = -EFAULT;
14443b5dd52aSJames Smart 					break;
14453b5dd52aSJames Smart 				default:
14463b5dd52aSJames Smart 					rc = -EACCES;
14473b5dd52aSJames Smart 					break;
14483b5dd52aSJames Smart 				}
1449a33c4f7bSJames Smart 			} else {
14503b5dd52aSJames Smart 				rc = -EACCES;
1451a33c4f7bSJames Smart 			}
1452a33c4f7bSJames Smart 		} else {
145301e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len = 0;
1454a33c4f7bSJames Smart 		}
1455a33c4f7bSJames Smart 	}
14563b5dd52aSJames Smart 
1457a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
14583b5dd52aSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1459a33c4f7bSJames Smart 	kfree(bmp);
14603b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
14613b5dd52aSJames Smart 	lpfc_nlp_put(ndlp);
14623b5dd52aSJames Smart 	kfree(dd_data);
1463a33c4f7bSJames Smart 
1464a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
1465a33c4f7bSJames Smart 
1466a33c4f7bSJames Smart 	if (job) {
146701e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
146806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
14691abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
1470a33c4f7bSJames Smart 	}
14713b5dd52aSJames Smart 	return;
14723b5dd52aSJames Smart }
14733b5dd52aSJames Smart 
14743b5dd52aSJames Smart /**
14753b5dd52aSJames Smart  * lpfc_issue_ct_rsp - issue a ct response
14763b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
14773b5dd52aSJames Smart  * @job: Pointer to the job object.
14783b5dd52aSJames Smart  * @tag: tag index value into the ports context exchange array.
14793b5dd52aSJames Smart  * @bmp: Pointer to a dma buffer descriptor.
14803b5dd52aSJames Smart  * @num_entry: Number of enties in the bde.
14813b5dd52aSJames Smart  **/
14823b5dd52aSJames Smart static int
148375cc8cfcSJohannes Thumshirn lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1484a33c4f7bSJames Smart 		  struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1485a33c4f7bSJames Smart 		  int num_entry)
14863b5dd52aSJames Smart {
14873b5dd52aSJames Smart 	IOCB_t *icmd;
14883b5dd52aSJames Smart 	struct lpfc_iocbq *ctiocb = NULL;
14893b5dd52aSJames Smart 	int rc = 0;
14903b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp = NULL;
14913b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
1492b5a9b2dfSJames Smart 	unsigned long flags;
14933b5dd52aSJames Smart 	uint32_t creg_val;
14943b5dd52aSJames Smart 
14953b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
14963b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
14973b5dd52aSJames Smart 	if (!dd_data) {
14983b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
14993b5dd52aSJames Smart 				"2736 Failed allocation of dd_data\n");
15003b5dd52aSJames Smart 		rc = -ENOMEM;
15013b5dd52aSJames Smart 		goto no_dd_data;
15023b5dd52aSJames Smart 	}
15033b5dd52aSJames Smart 
15043b5dd52aSJames Smart 	/* Allocate buffer for  command iocb */
15053b5dd52aSJames Smart 	ctiocb = lpfc_sli_get_iocbq(phba);
15063b5dd52aSJames Smart 	if (!ctiocb) {
1507d439d286SJames Smart 		rc = -ENOMEM;
15083b5dd52aSJames Smart 		goto no_ctiocb;
15093b5dd52aSJames Smart 	}
15103b5dd52aSJames Smart 
15113b5dd52aSJames Smart 	icmd = &ctiocb->iocb;
15123b5dd52aSJames Smart 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
15133b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
15143b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
15153b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
15163b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
15173b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
15183b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15193b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
15203b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
15213b5dd52aSJames Smart 
15223b5dd52aSJames Smart 	/* Fill in rest of iocb */
15233b5dd52aSJames Smart 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
15243b5dd52aSJames Smart 	icmd->ulpBdeCount = 1;
15253b5dd52aSJames Smart 	icmd->ulpLe = 1;
15263b5dd52aSJames Smart 	icmd->ulpClass = CLASS3;
15273b5dd52aSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
15283b5dd52aSJames Smart 		/* Do not issue unsol response if oxid not marked as valid */
15296dd9e31cSJames Smart 		if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
15303b5dd52aSJames Smart 			rc = IOCB_ERROR;
15313b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
15323b5dd52aSJames Smart 		}
15337851fe2cSJames Smart 		icmd->ulpContext = phba->ct_ctx[tag].rxid;
15347851fe2cSJames Smart 		icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
15353b5dd52aSJames Smart 		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
15363b5dd52aSJames Smart 		if (!ndlp) {
15373b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
15383b5dd52aSJames Smart 				 "2721 ndlp null for oxid %x SID %x\n",
15393b5dd52aSJames Smart 					icmd->ulpContext,
15403b5dd52aSJames Smart 					phba->ct_ctx[tag].SID);
15413b5dd52aSJames Smart 			rc = IOCB_ERROR;
15423b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
15433b5dd52aSJames Smart 		}
1544589a52d6SJames Smart 
1545589a52d6SJames Smart 		/* Check if the ndlp is active */
1546589a52d6SJames Smart 		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1547a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1548589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1549589a52d6SJames Smart 		}
1550589a52d6SJames Smart 
1551589a52d6SJames Smart 		/* get a refernece count so the ndlp doesn't go away while
1552589a52d6SJames Smart 		 * we respond
1553589a52d6SJames Smart 		 */
1554589a52d6SJames Smart 		if (!lpfc_nlp_get(ndlp)) {
1555a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1556589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1557589a52d6SJames Smart 		}
1558589a52d6SJames Smart 
15597851fe2cSJames Smart 		icmd->un.ulpWord[3] =
15606d368e53SJames Smart 				phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15616d368e53SJames Smart 
15623b5dd52aSJames Smart 		/* The exchange is done, mark the entry as invalid */
15636dd9e31cSJames Smart 		phba->ct_ctx[tag].valid = UNSOL_INVALID;
15643b5dd52aSJames Smart 	} else
15653b5dd52aSJames Smart 		icmd->ulpContext = (ushort) tag;
15663b5dd52aSJames Smart 
15673b5dd52aSJames Smart 	icmd->ulpTimeout = phba->fc_ratov * 2;
15683b5dd52aSJames Smart 
15693b5dd52aSJames Smart 	/* Xmit CT response on exchange <xid> */
15703b5dd52aSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
15717851fe2cSJames Smart 		"2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
15727851fe2cSJames Smart 		icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
15733b5dd52aSJames Smart 
15743b5dd52aSJames Smart 	ctiocb->iocb_cmpl = NULL;
15753b5dd52aSJames Smart 	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
15763b5dd52aSJames Smart 	ctiocb->vport = phba->pport;
1577a33c4f7bSJames Smart 	ctiocb->context1 = dd_data;
1578a33c4f7bSJames Smart 	ctiocb->context2 = cmp;
15793b5dd52aSJames Smart 	ctiocb->context3 = bmp;
1580d5ce53b7SJames Smart 	ctiocb->context_un.ndlp = ndlp;
15813b5dd52aSJames Smart 	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1582a33c4f7bSJames Smart 
15833b5dd52aSJames Smart 	dd_data->type = TYPE_IOCB;
1584a33c4f7bSJames Smart 	dd_data->set_job = job;
15853b5dd52aSJames Smart 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
15863b5dd52aSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
1587a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = NULL;
1588a33c4f7bSJames Smart 	job->dd_data = dd_data;
15893b5dd52aSJames Smart 
15903b5dd52aSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
15919940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
15929940b97bSJames Smart 			rc = -IOCB_ERROR;
15939940b97bSJames Smart 			goto issue_ct_rsp_exit;
15949940b97bSJames Smart 		}
15953b5dd52aSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
15963b5dd52aSJames Smart 		writel(creg_val, phba->HCregaddr);
15973b5dd52aSJames Smart 		readl(phba->HCregaddr); /* flush */
15983b5dd52aSJames Smart 	}
15993b5dd52aSJames Smart 
16003b5dd52aSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
16013b5dd52aSJames Smart 
1602b5a9b2dfSJames Smart 	if (rc == IOCB_SUCCESS) {
1603b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
1604b5a9b2dfSJames Smart 		/* make sure the I/O had not been completed/released */
1605b5a9b2dfSJames Smart 		if (ctiocb->iocb_flag & LPFC_IO_LIBDFC) {
1606b5a9b2dfSJames Smart 			/* open up abort window to timeout handler */
16071b8d11abSJames Smart 			ctiocb->iocb_flag |= LPFC_IO_CMD_OUTSTANDING;
1608b5a9b2dfSJames Smart 		}
1609b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
16103b5dd52aSJames Smart 		return 0; /* done for now */
1611b5a9b2dfSJames Smart 	}
1612b5a9b2dfSJames Smart 
1613b5a9b2dfSJames Smart 	/* iocb failed so cleanup */
1614b5a9b2dfSJames Smart 	job->dd_data = NULL;
16153b5dd52aSJames Smart 
16163b5dd52aSJames Smart issue_ct_rsp_exit:
16173b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, ctiocb);
16183b5dd52aSJames Smart no_ctiocb:
16193b5dd52aSJames Smart 	kfree(dd_data);
16203b5dd52aSJames Smart no_dd_data:
16213b5dd52aSJames Smart 	return rc;
16223b5dd52aSJames Smart }
16233b5dd52aSJames Smart 
16243b5dd52aSJames Smart /**
16253b5dd52aSJames Smart  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
16263b5dd52aSJames Smart  * @job: SEND_MGMT_RESP fc_bsg_job
16273b5dd52aSJames Smart  **/
16283b5dd52aSJames Smart static int
162975cc8cfcSJohannes Thumshirn lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
16303b5dd52aSJames Smart {
1631cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
16323b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
163301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
163401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
16353b5dd52aSJames Smart 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
163601e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
16373b5dd52aSJames Smart 	struct ulp_bde64 *bpl;
1638a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1639a33c4f7bSJames Smart 	int bpl_entries;
16403b5dd52aSJames Smart 	uint32_t tag = mgmt_resp->tag;
16413b5dd52aSJames Smart 	unsigned long reqbfrcnt =
16423b5dd52aSJames Smart 			(unsigned long)job->request_payload.payload_len;
16433b5dd52aSJames Smart 	int rc = 0;
16443b5dd52aSJames Smart 
16453b5dd52aSJames Smart 	/* in case no data is transferred */
164601e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
16473b5dd52aSJames Smart 
16483b5dd52aSJames Smart 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
16493b5dd52aSJames Smart 		rc = -ERANGE;
16503b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
16513b5dd52aSJames Smart 	}
16523b5dd52aSJames Smart 
16533b5dd52aSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
16543b5dd52aSJames Smart 	if (!bmp) {
16553b5dd52aSJames Smart 		rc = -ENOMEM;
16563b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
16573b5dd52aSJames Smart 	}
16583b5dd52aSJames Smart 
16593b5dd52aSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
16603b5dd52aSJames Smart 	if (!bmp->virt) {
16613b5dd52aSJames Smart 		rc = -ENOMEM;
16623b5dd52aSJames Smart 		goto send_mgmt_rsp_free_bmp;
16633b5dd52aSJames Smart 	}
16643b5dd52aSJames Smart 
16653b5dd52aSJames Smart 	INIT_LIST_HEAD(&bmp->list);
16663b5dd52aSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
1667a33c4f7bSJames Smart 	bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1668a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1669a33c4f7bSJames Smart 				     1, bpl, &bpl_entries);
1670a33c4f7bSJames Smart 	if (!cmp) {
1671a33c4f7bSJames Smart 		rc = -ENOMEM;
1672a33c4f7bSJames Smart 		goto send_mgmt_rsp_free_bmp;
16733b5dd52aSJames Smart 	}
1674a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
1675a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
16763b5dd52aSJames Smart 
1677a33c4f7bSJames Smart 	rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
16783b5dd52aSJames Smart 
16793b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
16803b5dd52aSJames Smart 		return 0; /* done for now */
16813b5dd52aSJames Smart 
16823b5dd52aSJames Smart 	rc = -EACCES;
1683a33c4f7bSJames Smart 
1684a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
16853b5dd52aSJames Smart 
16863b5dd52aSJames Smart send_mgmt_rsp_free_bmp:
1687a33c4f7bSJames Smart 	if (bmp->virt)
1688a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
16893b5dd52aSJames Smart 	kfree(bmp);
16903b5dd52aSJames Smart send_mgmt_rsp_exit:
16913b5dd52aSJames Smart 	/* make error code available to userspace */
169201e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
16933b5dd52aSJames Smart 	job->dd_data = NULL;
16943b5dd52aSJames Smart 	return rc;
16953b5dd52aSJames Smart }
16963b5dd52aSJames Smart 
16973b5dd52aSJames Smart /**
16987ad20aa9SJames Smart  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
16997ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17003b5dd52aSJames Smart  *
17017ad20aa9SJames Smart  * This function is responsible for preparing driver for diag loopback
17027ad20aa9SJames Smart  * on device.
17033b5dd52aSJames Smart  */
17043b5dd52aSJames Smart static int
170588a2cfbbSJames Smart lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
17063b5dd52aSJames Smart {
17073b5dd52aSJames Smart 	struct lpfc_vport **vports;
17087ad20aa9SJames Smart 	struct Scsi_Host *shost;
17097ad20aa9SJames Smart 	struct lpfc_sli *psli;
1710895427bdSJames Smart 	struct lpfc_queue *qp = NULL;
17117ad20aa9SJames Smart 	struct lpfc_sli_ring *pring;
17123b5dd52aSJames Smart 	int i = 0;
17133b5dd52aSJames Smart 
17147ad20aa9SJames Smart 	psli = &phba->sli;
17157ad20aa9SJames Smart 	if (!psli)
17167ad20aa9SJames Smart 		return -ENODEV;
17173b5dd52aSJames Smart 
17183b5dd52aSJames Smart 
17193b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
17203b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
17217ad20aa9SJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
17227ad20aa9SJames Smart 		return -EACCES;
17233b5dd52aSJames Smart 
17243b5dd52aSJames Smart 	vports = lpfc_create_vport_work_array(phba);
17253b5dd52aSJames Smart 	if (vports) {
17263b5dd52aSJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17273b5dd52aSJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
17283b5dd52aSJames Smart 			scsi_block_requests(shost);
17293b5dd52aSJames Smart 		}
17303b5dd52aSJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
17313b5dd52aSJames Smart 	} else {
17323b5dd52aSJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
17333b5dd52aSJames Smart 		scsi_block_requests(shost);
17343b5dd52aSJames Smart 	}
17353b5dd52aSJames Smart 
1736895427bdSJames Smart 	if (phba->sli_rev != LPFC_SLI_REV4) {
1737895427bdSJames Smart 		pring = &psli->sli3_ring[LPFC_FCP_RING];
1738895427bdSJames Smart 		lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1739895427bdSJames Smart 		return 0;
1740895427bdSJames Smart 	}
1741895427bdSJames Smart 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1742895427bdSJames Smart 		pring = qp->pring;
1743895427bdSJames Smart 		if (!pring || (pring->ringno != LPFC_FCP_RING))
1744895427bdSJames Smart 			continue;
1745895427bdSJames Smart 		if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1746895427bdSJames Smart 				      &pring->ring_lock))
17473b5dd52aSJames Smart 			break;
17483b5dd52aSJames Smart 	}
17497ad20aa9SJames Smart 	return 0;
17507ad20aa9SJames Smart }
17513b5dd52aSJames Smart 
17527ad20aa9SJames Smart /**
17537ad20aa9SJames Smart  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
17547ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17557ad20aa9SJames Smart  *
17567ad20aa9SJames Smart  * This function is responsible for driver exit processing of setting up
17577ad20aa9SJames Smart  * diag loopback mode on device.
17587ad20aa9SJames Smart  */
17597ad20aa9SJames Smart static void
17607ad20aa9SJames Smart lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
17617ad20aa9SJames Smart {
17627ad20aa9SJames Smart 	struct Scsi_Host *shost;
17637ad20aa9SJames Smart 	struct lpfc_vport **vports;
17647ad20aa9SJames Smart 	int i;
17657ad20aa9SJames Smart 
17667ad20aa9SJames Smart 	vports = lpfc_create_vport_work_array(phba);
17677ad20aa9SJames Smart 	if (vports) {
17687ad20aa9SJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
17697ad20aa9SJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
17707ad20aa9SJames Smart 			scsi_unblock_requests(shost);
17717ad20aa9SJames Smart 		}
17727ad20aa9SJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
17737ad20aa9SJames Smart 	} else {
17747ad20aa9SJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
17757ad20aa9SJames Smart 		scsi_unblock_requests(shost);
17767ad20aa9SJames Smart 	}
17777ad20aa9SJames Smart 	return;
17787ad20aa9SJames Smart }
17797ad20aa9SJames Smart 
17807ad20aa9SJames Smart /**
17817ad20aa9SJames Smart  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
17827ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17837ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
17847ad20aa9SJames Smart  *
17857ad20aa9SJames Smart  * This function is responsible for placing an sli3  port into diagnostic
17867ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
17877ad20aa9SJames Smart  * All new scsi requests are blocked, a small delay is used to allow the
17887ad20aa9SJames Smart  * scsi requests to complete then the link is brought down. If the link is
17897ad20aa9SJames Smart  * is placed in loopback mode then scsi requests are again allowed
17907ad20aa9SJames Smart  * so the scsi mid-layer doesn't give up on the port.
17917ad20aa9SJames Smart  * All of this is done in-line.
17927ad20aa9SJames Smart  */
17937ad20aa9SJames Smart static int
179475cc8cfcSJohannes Thumshirn lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
17957ad20aa9SJames Smart {
179601e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
179701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
17987ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
17997ad20aa9SJames Smart 	uint32_t link_flags;
18007ad20aa9SJames Smart 	uint32_t timeout;
18011b51197dSJames Smart 	LPFC_MBOXQ_t *pmboxq  = NULL;
1802b76f2dc9SJames Smart 	int mbxstatus = MBX_SUCCESS;
18037ad20aa9SJames Smart 	int i = 0;
18047ad20aa9SJames Smart 	int rc = 0;
18057ad20aa9SJames Smart 
18067ad20aa9SJames Smart 	/* no data to return just the return code */
180701e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
18087ad20aa9SJames Smart 
18097ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
18107ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
18117ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
18127ad20aa9SJames Smart 				"2738 Received DIAG MODE request size:%d "
18137ad20aa9SJames Smart 				"below the minimum size:%d\n",
18147ad20aa9SJames Smart 				job->request_len,
18157ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
18167ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
18177ad20aa9SJames Smart 		rc = -EINVAL;
18187ad20aa9SJames Smart 		goto job_error;
18197ad20aa9SJames Smart 	}
18207ad20aa9SJames Smart 
182188a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
18227ad20aa9SJames Smart 	if (rc)
18237ad20aa9SJames Smart 		goto job_error;
18247ad20aa9SJames Smart 
18257ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
18267ad20aa9SJames Smart 	loopback_mode = (struct diag_mode_set *)
182701e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
18287ad20aa9SJames Smart 	link_flags = loopback_mode->type;
18297ad20aa9SJames Smart 	timeout = loopback_mode->timeout * 100;
18307ad20aa9SJames Smart 
18317ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18327ad20aa9SJames Smart 	if (!pmboxq) {
18337ad20aa9SJames Smart 		rc = -ENOMEM;
18347ad20aa9SJames Smart 		goto loopback_mode_exit;
18357ad20aa9SJames Smart 	}
18363b5dd52aSJames Smart 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
18373b5dd52aSJames Smart 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
18383b5dd52aSJames Smart 	pmboxq->u.mb.mbxOwner = OWN_HOST;
18393b5dd52aSJames Smart 
18403b5dd52aSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
18413b5dd52aSJames Smart 
18423b5dd52aSJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
18433b5dd52aSJames Smart 		/* wait for link down before proceeding */
18443b5dd52aSJames Smart 		i = 0;
18453b5dd52aSJames Smart 		while (phba->link_state != LPFC_LINK_DOWN) {
18463b5dd52aSJames Smart 			if (i++ > timeout) {
18473b5dd52aSJames Smart 				rc = -ETIMEDOUT;
18483b5dd52aSJames Smart 				goto loopback_mode_exit;
18493b5dd52aSJames Smart 			}
18503b5dd52aSJames Smart 			msleep(10);
18513b5dd52aSJames Smart 		}
18523b5dd52aSJames Smart 
18533b5dd52aSJames Smart 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
18543b5dd52aSJames Smart 		if (link_flags == INTERNAL_LOOP_BACK)
18553b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
18563b5dd52aSJames Smart 		else
18573b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags =
18583b5dd52aSJames Smart 				FLAGS_TOPOLOGY_MODE_LOOP;
18593b5dd52aSJames Smart 
18603b5dd52aSJames Smart 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
18613b5dd52aSJames Smart 		pmboxq->u.mb.mbxOwner = OWN_HOST;
18623b5dd52aSJames Smart 
18633b5dd52aSJames Smart 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
18643b5dd52aSJames Smart 						     LPFC_MBOX_TMO);
18653b5dd52aSJames Smart 
18663b5dd52aSJames Smart 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
18673b5dd52aSJames Smart 			rc = -ENODEV;
18683b5dd52aSJames Smart 		else {
18691b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
18703b5dd52aSJames Smart 			phba->link_flag |= LS_LOOPBACK_MODE;
18711b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
18723b5dd52aSJames Smart 			/* wait for the link attention interrupt */
18733b5dd52aSJames Smart 			msleep(100);
18743b5dd52aSJames Smart 
18753b5dd52aSJames Smart 			i = 0;
18763b5dd52aSJames Smart 			while (phba->link_state != LPFC_HBA_READY) {
18773b5dd52aSJames Smart 				if (i++ > timeout) {
18783b5dd52aSJames Smart 					rc = -ETIMEDOUT;
18793b5dd52aSJames Smart 					break;
18803b5dd52aSJames Smart 				}
18813b5dd52aSJames Smart 
18823b5dd52aSJames Smart 				msleep(10);
18833b5dd52aSJames Smart 			}
18843b5dd52aSJames Smart 		}
18853b5dd52aSJames Smart 
18863b5dd52aSJames Smart 	} else
18873b5dd52aSJames Smart 		rc = -ENODEV;
18883b5dd52aSJames Smart 
18893b5dd52aSJames Smart loopback_mode_exit:
18907ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
18913b5dd52aSJames Smart 
18923b5dd52aSJames Smart 	/*
18933b5dd52aSJames Smart 	 * Let SLI layer release mboxq if mbox command completed after timeout.
18943b5dd52aSJames Smart 	 */
18951b51197dSJames Smart 	if (pmboxq && mbxstatus != MBX_TIMEOUT)
18963b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
18973b5dd52aSJames Smart 
18983b5dd52aSJames Smart job_error:
18993b5dd52aSJames Smart 	/* make error code available to userspace */
190001e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
19013b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
19023b5dd52aSJames Smart 	if (rc == 0)
190306548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
19041abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
19053b5dd52aSJames Smart 	return rc;
19063b5dd52aSJames Smart }
19073b5dd52aSJames Smart 
19083b5dd52aSJames Smart /**
19097ad20aa9SJames Smart  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
19107ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
19117ad20aa9SJames Smart  * @diag: Flag for set link to diag or nomral operation state.
19127ad20aa9SJames Smart  *
19137ad20aa9SJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
19147ad20aa9SJames Smart  * link to either diag state or normal operation state.
19157ad20aa9SJames Smart  */
19167ad20aa9SJames Smart static int
19177ad20aa9SJames Smart lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
19187ad20aa9SJames Smart {
19197ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
19207ad20aa9SJames Smart 	struct lpfc_mbx_set_link_diag_state *link_diag_state;
19217ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
19227ad20aa9SJames Smart 	int mbxstatus = MBX_SUCCESS, rc;
19237ad20aa9SJames Smart 
19247ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19257ad20aa9SJames Smart 	if (!pmboxq)
19267ad20aa9SJames Smart 		return -ENOMEM;
19277ad20aa9SJames Smart 
19287ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
19297ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
19307ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19317ad20aa9SJames Smart 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
19327ad20aa9SJames Smart 				req_len, LPFC_SLI4_MBX_EMBED);
19337ad20aa9SJames Smart 	if (alloc_len != req_len) {
19347ad20aa9SJames Smart 		rc = -ENOMEM;
19357ad20aa9SJames Smart 		goto link_diag_state_set_out;
19367ad20aa9SJames Smart 	}
19371b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
19381b51197dSJames Smart 			"3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
19391b51197dSJames Smart 			diag, phba->sli4_hba.lnk_info.lnk_tp,
19401b51197dSJames Smart 			phba->sli4_hba.lnk_info.lnk_no);
19411b51197dSJames Smart 
19427ad20aa9SJames Smart 	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
19439731592bSJames Smart 	bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
19449731592bSJames Smart 	       LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
19457ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
19461b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
19477ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
19481b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
19497ad20aa9SJames Smart 	if (diag)
19507ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
19517ad20aa9SJames Smart 		       &link_diag_state->u.req, 1);
19527ad20aa9SJames Smart 	else
19537ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
19547ad20aa9SJames Smart 		       &link_diag_state->u.req, 0);
19557ad20aa9SJames Smart 
19567ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
19577ad20aa9SJames Smart 
19587ad20aa9SJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
19597ad20aa9SJames Smart 		rc = 0;
19607ad20aa9SJames Smart 	else
19617ad20aa9SJames Smart 		rc = -ENODEV;
19627ad20aa9SJames Smart 
19637ad20aa9SJames Smart link_diag_state_set_out:
19647ad20aa9SJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
19657ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
19667ad20aa9SJames Smart 
19677ad20aa9SJames Smart 	return rc;
19687ad20aa9SJames Smart }
19697ad20aa9SJames Smart 
19707ad20aa9SJames Smart /**
19711b51197dSJames Smart  * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
19721b51197dSJames Smart  * @phba: Pointer to HBA context object.
19731b51197dSJames Smart  *
19741b51197dSJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
19751b51197dSJames Smart  * up internal loopback diagnostic.
19761b51197dSJames Smart  */
19771b51197dSJames Smart static int
19781b51197dSJames Smart lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
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,
19991b51197dSJames Smart 	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
20001b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_type,
20011b51197dSJames Smart 	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
20021b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
20033ef6d24cSJames Smart 	       LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
20041b51197dSJames Smart 
20051b51197dSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
20061b51197dSJames Smart 	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
20071b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20081b51197dSJames Smart 				"3127 Failed setup loopback mode mailbox "
20091b51197dSJames Smart 				"command, rc:x%x, status:x%x\n", mbxstatus,
20101b51197dSJames Smart 				pmboxq->u.mb.mbxStatus);
20111b51197dSJames Smart 		rc = -ENODEV;
20121b51197dSJames Smart 	}
20131b51197dSJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
20141b51197dSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
20151b51197dSJames Smart 	return rc;
20161b51197dSJames Smart }
20171b51197dSJames Smart 
20181b51197dSJames Smart /**
20191b51197dSJames Smart  * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
20201b51197dSJames Smart  * @phba: Pointer to HBA context object.
20211b51197dSJames Smart  *
20221b51197dSJames Smart  * This function set up SLI4 FC port registrations for diagnostic run, which
20231b51197dSJames Smart  * includes all the rpis, vfi, and also vpi.
20241b51197dSJames Smart  */
20251b51197dSJames Smart static int
20261b51197dSJames Smart lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
20271b51197dSJames Smart {
20281b51197dSJames Smart 	int rc;
20291b51197dSJames Smart 
20301b51197dSJames Smart 	if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
20311b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20321b51197dSJames Smart 				"3136 Port still had vfi registered: "
20331b51197dSJames Smart 				"mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
20341b51197dSJames Smart 				phba->pport->fc_myDID, phba->fcf.fcfi,
20351b51197dSJames Smart 				phba->sli4_hba.vfi_ids[phba->pport->vfi],
20361b51197dSJames Smart 				phba->vpi_ids[phba->pport->vpi]);
20371b51197dSJames Smart 		return -EINVAL;
20381b51197dSJames Smart 	}
20391b51197dSJames Smart 	rc = lpfc_issue_reg_vfi(phba->pport);
20401b51197dSJames Smart 	return rc;
20411b51197dSJames Smart }
20421b51197dSJames Smart 
20431b51197dSJames Smart /**
20447ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
20457ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
20467ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
20477ad20aa9SJames Smart  *
20487ad20aa9SJames Smart  * This function is responsible for placing an sli4 port into diagnostic
20497ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
20507ad20aa9SJames Smart  */
20517ad20aa9SJames Smart static int
205275cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
20537ad20aa9SJames Smart {
205401e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
205501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
20567ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
20571b51197dSJames Smart 	uint32_t link_flags, timeout;
20581b51197dSJames Smart 	int i, rc = 0;
20597ad20aa9SJames Smart 
20607ad20aa9SJames Smart 	/* no data to return just the return code */
206101e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
20627ad20aa9SJames Smart 
20637ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
20647ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
20657ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20667ad20aa9SJames Smart 				"3011 Received DIAG MODE request size:%d "
20677ad20aa9SJames Smart 				"below the minimum size:%d\n",
20687ad20aa9SJames Smart 				job->request_len,
20697ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
20707ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
20717ad20aa9SJames Smart 		rc = -EINVAL;
20727ad20aa9SJames Smart 		goto job_error;
20737ad20aa9SJames Smart 	}
20747ad20aa9SJames Smart 
207588a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
20767ad20aa9SJames Smart 	if (rc)
20777ad20aa9SJames Smart 		goto job_error;
20787ad20aa9SJames Smart 
20791b51197dSJames Smart 	/* indicate we are in loobpack diagnostic mode */
20801b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
20811b51197dSJames Smart 	phba->link_flag |= LS_LOOPBACK_MODE;
20821b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
20831b51197dSJames Smart 
20841b51197dSJames Smart 	/* reset port to start frome scratch */
20851b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
20861b51197dSJames Smart 	if (rc)
20871b51197dSJames Smart 		goto job_error;
20881b51197dSJames Smart 
20897ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
20901b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20911b51197dSJames Smart 			"3129 Bring link to diagnostic state.\n");
20927ad20aa9SJames Smart 	loopback_mode = (struct diag_mode_set *)
209301e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
20947ad20aa9SJames Smart 	link_flags = loopback_mode->type;
20957ad20aa9SJames Smart 	timeout = loopback_mode->timeout * 100;
20967ad20aa9SJames Smart 
20977ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
20981b51197dSJames Smart 	if (rc) {
20991b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
21001b51197dSJames Smart 				"3130 Failed to bring link to diagnostic "
21011b51197dSJames Smart 				"state, rc:x%x\n", rc);
21027ad20aa9SJames Smart 		goto loopback_mode_exit;
21031b51197dSJames Smart 	}
21047ad20aa9SJames Smart 
21057ad20aa9SJames Smart 	/* wait for link down before proceeding */
21067ad20aa9SJames Smart 	i = 0;
21077ad20aa9SJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
21087ad20aa9SJames Smart 		if (i++ > timeout) {
21097ad20aa9SJames Smart 			rc = -ETIMEDOUT;
21101b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21111b51197dSJames Smart 					"3131 Timeout waiting for link to "
21121b51197dSJames Smart 					"diagnostic mode, timeout:%d ms\n",
21131b51197dSJames Smart 					timeout * 10);
21147ad20aa9SJames Smart 			goto loopback_mode_exit;
21157ad20aa9SJames Smart 		}
21167ad20aa9SJames Smart 		msleep(10);
21177ad20aa9SJames Smart 	}
21187ad20aa9SJames Smart 
21191b51197dSJames Smart 	/* set up loopback mode */
21201b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21211b51197dSJames Smart 			"3132 Set up loopback mode:x%x\n", link_flags);
21221b51197dSJames Smart 
21231b51197dSJames Smart 	if (link_flags == INTERNAL_LOOP_BACK)
21241b51197dSJames Smart 		rc = lpfc_sli4_bsg_set_internal_loopback(phba);
21251b51197dSJames Smart 	else if (link_flags == EXTERNAL_LOOP_BACK)
21261b51197dSJames Smart 		rc = lpfc_hba_init_link_fc_topology(phba,
21271b51197dSJames Smart 						    FLAGS_TOPOLOGY_MODE_PT_PT,
21281b51197dSJames Smart 						    MBX_NOWAIT);
21297ad20aa9SJames Smart 	else {
21301b51197dSJames Smart 		rc = -EINVAL;
21311b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
21321b51197dSJames Smart 				"3141 Loopback mode:x%x not supported\n",
21331b51197dSJames Smart 				link_flags);
21341b51197dSJames Smart 		goto loopback_mode_exit;
21351b51197dSJames Smart 	}
21361b51197dSJames Smart 
21371b51197dSJames Smart 	if (!rc) {
21387ad20aa9SJames Smart 		/* wait for the link attention interrupt */
21397ad20aa9SJames Smart 		msleep(100);
21407ad20aa9SJames Smart 		i = 0;
21411b51197dSJames Smart 		while (phba->link_state < LPFC_LINK_UP) {
21421b51197dSJames Smart 			if (i++ > timeout) {
21431b51197dSJames Smart 				rc = -ETIMEDOUT;
21441b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21451b51197dSJames Smart 					"3137 Timeout waiting for link up "
21461b51197dSJames Smart 					"in loopback mode, timeout:%d ms\n",
21471b51197dSJames Smart 					timeout * 10);
21481b51197dSJames Smart 				break;
21491b51197dSJames Smart 			}
21501b51197dSJames Smart 			msleep(10);
21511b51197dSJames Smart 		}
21521b51197dSJames Smart 	}
21531b51197dSJames Smart 
21541b51197dSJames Smart 	/* port resource registration setup for loopback diagnostic */
21551b51197dSJames Smart 	if (!rc) {
21561b51197dSJames Smart 		/* set up a none zero myDID for loopback test */
21571b51197dSJames Smart 		phba->pport->fc_myDID = 1;
21581b51197dSJames Smart 		rc = lpfc_sli4_diag_fcport_reg_setup(phba);
21591b51197dSJames Smart 	} else
21601b51197dSJames Smart 		goto loopback_mode_exit;
21611b51197dSJames Smart 
21621b51197dSJames Smart 	if (!rc) {
21631b51197dSJames Smart 		/* wait for the port ready */
21641b51197dSJames Smart 		msleep(100);
21651b51197dSJames Smart 		i = 0;
21667ad20aa9SJames Smart 		while (phba->link_state != LPFC_HBA_READY) {
21677ad20aa9SJames Smart 			if (i++ > timeout) {
21687ad20aa9SJames Smart 				rc = -ETIMEDOUT;
21691b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
21701b51197dSJames Smart 					"3133 Timeout waiting for port "
21711b51197dSJames Smart 					"loopback mode ready, timeout:%d ms\n",
21721b51197dSJames Smart 					timeout * 10);
21737ad20aa9SJames Smart 				break;
21747ad20aa9SJames Smart 			}
21757ad20aa9SJames Smart 			msleep(10);
21767ad20aa9SJames Smart 		}
21777ad20aa9SJames Smart 	}
21787ad20aa9SJames Smart 
21797ad20aa9SJames Smart loopback_mode_exit:
21801b51197dSJames Smart 	/* clear loopback diagnostic mode */
21811b51197dSJames Smart 	if (rc) {
21821b51197dSJames Smart 		spin_lock_irq(&phba->hbalock);
21831b51197dSJames Smart 		phba->link_flag &= ~LS_LOOPBACK_MODE;
21841b51197dSJames Smart 		spin_unlock_irq(&phba->hbalock);
21851b51197dSJames Smart 	}
21867ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
21877ad20aa9SJames Smart 
21887ad20aa9SJames Smart job_error:
21897ad20aa9SJames Smart 	/* make error code available to userspace */
219001e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
21917ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
21927ad20aa9SJames Smart 	if (rc == 0)
219306548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
21941abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
21957ad20aa9SJames Smart 	return rc;
21967ad20aa9SJames Smart }
21977ad20aa9SJames Smart 
21987ad20aa9SJames Smart /**
21997ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
22007ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
22017ad20aa9SJames Smart  *
22027ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
22037ad20aa9SJames Smart  * command from the user to proper driver action routines.
22047ad20aa9SJames Smart  */
22057ad20aa9SJames Smart static int
220675cc8cfcSJohannes Thumshirn lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
22077ad20aa9SJames Smart {
22087ad20aa9SJames Smart 	struct Scsi_Host *shost;
22097ad20aa9SJames Smart 	struct lpfc_vport *vport;
22107ad20aa9SJames Smart 	struct lpfc_hba *phba;
22117ad20aa9SJames Smart 	int rc;
22127ad20aa9SJames Smart 
2213cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
22147ad20aa9SJames Smart 	if (!shost)
22157ad20aa9SJames Smart 		return -ENODEV;
2216cd21c605SJohannes Thumshirn 	vport = shost_priv(shost);
22177ad20aa9SJames Smart 	if (!vport)
22187ad20aa9SJames Smart 		return -ENODEV;
22197ad20aa9SJames Smart 	phba = vport->phba;
22207ad20aa9SJames Smart 	if (!phba)
22217ad20aa9SJames Smart 		return -ENODEV;
22227ad20aa9SJames Smart 
22237ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
22247ad20aa9SJames Smart 		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
22257ad20aa9SJames Smart 	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
22267ad20aa9SJames Smart 		 LPFC_SLI_INTF_IF_TYPE_2)
22277ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
22287ad20aa9SJames Smart 	else
22297ad20aa9SJames Smart 		rc = -ENODEV;
22307ad20aa9SJames Smart 
22317ad20aa9SJames Smart 	return rc;
22327ad20aa9SJames Smart }
22337ad20aa9SJames Smart 
22347ad20aa9SJames Smart /**
22357ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
22367ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
22377ad20aa9SJames Smart  *
22387ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
22397ad20aa9SJames Smart  * command from the user to proper driver action routines.
22407ad20aa9SJames Smart  */
22417ad20aa9SJames Smart static int
224275cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
22437ad20aa9SJames Smart {
224401e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
224501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
22467ad20aa9SJames Smart 	struct Scsi_Host *shost;
22477ad20aa9SJames Smart 	struct lpfc_vport *vport;
22487ad20aa9SJames Smart 	struct lpfc_hba *phba;
22491b51197dSJames Smart 	struct diag_mode_set *loopback_mode_end_cmd;
22501b51197dSJames Smart 	uint32_t timeout;
22511b51197dSJames Smart 	int rc, i;
22527ad20aa9SJames Smart 
2253cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
22547ad20aa9SJames Smart 	if (!shost)
22557ad20aa9SJames Smart 		return -ENODEV;
2256cd21c605SJohannes Thumshirn 	vport = shost_priv(shost);
22577ad20aa9SJames Smart 	if (!vport)
22587ad20aa9SJames Smart 		return -ENODEV;
22597ad20aa9SJames Smart 	phba = vport->phba;
22607ad20aa9SJames Smart 	if (!phba)
22617ad20aa9SJames Smart 		return -ENODEV;
22627ad20aa9SJames Smart 
22637ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
22647ad20aa9SJames Smart 		return -ENODEV;
22657ad20aa9SJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
22667ad20aa9SJames Smart 	    LPFC_SLI_INTF_IF_TYPE_2)
22677ad20aa9SJames Smart 		return -ENODEV;
22687ad20aa9SJames Smart 
22691b51197dSJames Smart 	/* clear loopback diagnostic mode */
22701b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
22711b51197dSJames Smart 	phba->link_flag &= ~LS_LOOPBACK_MODE;
22721b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
22731b51197dSJames Smart 	loopback_mode_end_cmd = (struct diag_mode_set *)
227401e0e15cSJohannes Thumshirn 			bsg_request->rqst_data.h_vendor.vendor_cmd;
22751b51197dSJames Smart 	timeout = loopback_mode_end_cmd->timeout * 100;
22761b51197dSJames Smart 
22777ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
22781b51197dSJames Smart 	if (rc) {
22791b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
22801b51197dSJames Smart 				"3139 Failed to bring link to diagnostic "
22811b51197dSJames Smart 				"state, rc:x%x\n", rc);
22821b51197dSJames Smart 		goto loopback_mode_end_exit;
22831b51197dSJames Smart 	}
22847ad20aa9SJames Smart 
22851b51197dSJames Smart 	/* wait for link down before proceeding */
22861b51197dSJames Smart 	i = 0;
22871b51197dSJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
22881b51197dSJames Smart 		if (i++ > timeout) {
22891b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
22901b51197dSJames Smart 					"3140 Timeout waiting for link to "
22911b51197dSJames Smart 					"diagnostic mode_end, timeout:%d ms\n",
22921b51197dSJames Smart 					timeout * 10);
22931b51197dSJames Smart 			/* there is nothing much we can do here */
22941b51197dSJames Smart 			break;
22951b51197dSJames Smart 		}
22961b51197dSJames Smart 		msleep(10);
22971b51197dSJames Smart 	}
22987ad20aa9SJames Smart 
22991b51197dSJames Smart 	/* reset port resource registrations */
23001b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
23011b51197dSJames Smart 	phba->pport->fc_myDID = 0;
23021b51197dSJames Smart 
23031b51197dSJames Smart loopback_mode_end_exit:
23041b51197dSJames Smart 	/* make return code available to userspace */
230501e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
23061b51197dSJames Smart 	/* complete the job back to userspace if no error */
23071b51197dSJames Smart 	if (rc == 0)
230806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
23091abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
23107ad20aa9SJames Smart 	return rc;
23117ad20aa9SJames Smart }
23127ad20aa9SJames Smart 
23137ad20aa9SJames Smart /**
23147ad20aa9SJames Smart  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
23157ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
23167ad20aa9SJames Smart  *
23177ad20aa9SJames Smart  * This function is to perform SLI4 diag link test request from the user
23187ad20aa9SJames Smart  * applicaiton.
23197ad20aa9SJames Smart  */
23207ad20aa9SJames Smart static int
232175cc8cfcSJohannes Thumshirn lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
23227ad20aa9SJames Smart {
232301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
232401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
23257ad20aa9SJames Smart 	struct Scsi_Host *shost;
23267ad20aa9SJames Smart 	struct lpfc_vport *vport;
23277ad20aa9SJames Smart 	struct lpfc_hba *phba;
23287ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
23297ad20aa9SJames Smart 	struct sli4_link_diag *link_diag_test_cmd;
23307ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
23317ad20aa9SJames Smart 	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
23327ad20aa9SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
23337ad20aa9SJames Smart 	uint32_t shdr_status, shdr_add_status;
23347ad20aa9SJames Smart 	struct diag_status *diag_status_reply;
23357ad20aa9SJames Smart 	int mbxstatus, rc = 0;
23367ad20aa9SJames Smart 
2337cd21c605SJohannes Thumshirn 	shost = fc_bsg_to_shost(job);
23387ad20aa9SJames Smart 	if (!shost) {
23397ad20aa9SJames Smart 		rc = -ENODEV;
23407ad20aa9SJames Smart 		goto job_error;
23417ad20aa9SJames Smart 	}
2342cd21c605SJohannes Thumshirn 	vport = shost_priv(shost);
23437ad20aa9SJames Smart 	if (!vport) {
23447ad20aa9SJames Smart 		rc = -ENODEV;
23457ad20aa9SJames Smart 		goto job_error;
23467ad20aa9SJames Smart 	}
23477ad20aa9SJames Smart 	phba = vport->phba;
23487ad20aa9SJames Smart 	if (!phba) {
23497ad20aa9SJames Smart 		rc = -ENODEV;
23507ad20aa9SJames Smart 		goto job_error;
23517ad20aa9SJames Smart 	}
23527ad20aa9SJames Smart 
23537ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
23547ad20aa9SJames Smart 		rc = -ENODEV;
23557ad20aa9SJames Smart 		goto job_error;
23567ad20aa9SJames Smart 	}
23577ad20aa9SJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
23587ad20aa9SJames Smart 	    LPFC_SLI_INTF_IF_TYPE_2) {
23597ad20aa9SJames Smart 		rc = -ENODEV;
23607ad20aa9SJames Smart 		goto job_error;
23617ad20aa9SJames Smart 	}
23627ad20aa9SJames Smart 
23637ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
23647ad20aa9SJames Smart 	    sizeof(struct sli4_link_diag)) {
23657ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
23667ad20aa9SJames Smart 				"3013 Received LINK DIAG TEST request "
23677ad20aa9SJames Smart 				" size:%d below the minimum size:%d\n",
23687ad20aa9SJames Smart 				job->request_len,
23697ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
23707ad20aa9SJames Smart 				sizeof(struct sli4_link_diag)));
23717ad20aa9SJames Smart 		rc = -EINVAL;
23727ad20aa9SJames Smart 		goto job_error;
23737ad20aa9SJames Smart 	}
23747ad20aa9SJames Smart 
237588a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
23767ad20aa9SJames Smart 	if (rc)
23777ad20aa9SJames Smart 		goto job_error;
23787ad20aa9SJames Smart 
23797ad20aa9SJames Smart 	link_diag_test_cmd = (struct sli4_link_diag *)
238001e0e15cSJohannes Thumshirn 			 bsg_request->rqst_data.h_vendor.vendor_cmd;
23817ad20aa9SJames Smart 
23827ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
23837ad20aa9SJames Smart 
23847ad20aa9SJames Smart 	if (rc)
23857ad20aa9SJames Smart 		goto job_error;
23867ad20aa9SJames Smart 
23877ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
238844ed33e6SGustavo A. R. Silva 	if (!pmboxq)
23897ad20aa9SJames Smart 		goto link_diag_test_exit;
23907ad20aa9SJames Smart 
23917ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
23927ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
23937ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
23947ad20aa9SJames Smart 				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
23957ad20aa9SJames Smart 				     req_len, LPFC_SLI4_MBX_EMBED);
239644ed33e6SGustavo A. R. Silva 	if (alloc_len != req_len)
23977ad20aa9SJames Smart 		goto link_diag_test_exit;
239844ed33e6SGustavo A. R. Silva 
23997ad20aa9SJames Smart 	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
24007ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
24011b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
24027ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
24031b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
24047ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
24057ad20aa9SJames Smart 	       link_diag_test_cmd->test_id);
24067ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
24077ad20aa9SJames Smart 	       link_diag_test_cmd->loops);
24087ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
24097ad20aa9SJames Smart 	       link_diag_test_cmd->test_version);
24107ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
24117ad20aa9SJames Smart 	       link_diag_test_cmd->error_action);
24127ad20aa9SJames Smart 
24137ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
24147ad20aa9SJames Smart 
24157ad20aa9SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)
24167ad20aa9SJames Smart 		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
24177ad20aa9SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
24187ad20aa9SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
24197ad20aa9SJames Smart 	if (shdr_status || shdr_add_status || mbxstatus) {
24207ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
24217ad20aa9SJames Smart 				"3010 Run link diag test mailbox failed with "
24227ad20aa9SJames Smart 				"mbx_status x%x status x%x, add_status x%x\n",
24237ad20aa9SJames Smart 				mbxstatus, shdr_status, shdr_add_status);
24247ad20aa9SJames Smart 	}
24257ad20aa9SJames Smart 
24267ad20aa9SJames Smart 	diag_status_reply = (struct diag_status *)
242701e0e15cSJohannes Thumshirn 			    bsg_reply->reply_data.vendor_reply.vendor_rsp;
24287ad20aa9SJames Smart 
24297ad20aa9SJames Smart 	if (job->reply_len <
24307ad20aa9SJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
24317ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
24327ad20aa9SJames Smart 				"3012 Received Run link diag test reply "
24337ad20aa9SJames Smart 				"below minimum size (%d): reply_len:%d\n",
24347ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
24357ad20aa9SJames Smart 				sizeof(struct diag_status)),
24367ad20aa9SJames Smart 				job->reply_len);
24377ad20aa9SJames Smart 		rc = -EINVAL;
24387ad20aa9SJames Smart 		goto job_error;
24397ad20aa9SJames Smart 	}
24407ad20aa9SJames Smart 
24417ad20aa9SJames Smart 	diag_status_reply->mbox_status = mbxstatus;
24427ad20aa9SJames Smart 	diag_status_reply->shdr_status = shdr_status;
24437ad20aa9SJames Smart 	diag_status_reply->shdr_add_status = shdr_add_status;
24447ad20aa9SJames Smart 
24457ad20aa9SJames Smart link_diag_test_exit:
24467ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
24477ad20aa9SJames Smart 
24487ad20aa9SJames Smart 	if (pmboxq)
24497ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
24507ad20aa9SJames Smart 
24517ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
24527ad20aa9SJames Smart 
24537ad20aa9SJames Smart job_error:
24547ad20aa9SJames Smart 	/* make error code available to userspace */
245501e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
24567ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
24577ad20aa9SJames Smart 	if (rc == 0)
245806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
24591abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
24607ad20aa9SJames Smart 	return rc;
24617ad20aa9SJames Smart }
24627ad20aa9SJames Smart 
24637ad20aa9SJames Smart /**
24643b5dd52aSJames Smart  * lpfcdiag_loop_self_reg - obtains a remote port login id
24653b5dd52aSJames Smart  * @phba: Pointer to HBA context object
24663b5dd52aSJames Smart  * @rpi: Pointer to a remote port login id
24673b5dd52aSJames Smart  *
24683b5dd52aSJames Smart  * This function obtains a remote port login id so the diag loopback test
24693b5dd52aSJames Smart  * can send and receive its own unsolicited CT command.
24703b5dd52aSJames Smart  **/
24713b5dd52aSJames Smart static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
24723b5dd52aSJames Smart {
24733b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
24743b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuff;
24753b5dd52aSJames Smart 	int status;
24763b5dd52aSJames Smart 
24773b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
24783b5dd52aSJames Smart 	if (!mbox)
2479d439d286SJames Smart 		return -ENOMEM;
24803b5dd52aSJames Smart 
24811b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
24823b5dd52aSJames Smart 		status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
24831b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
24841b51197dSJames Smart 				mbox, *rpi);
24851b51197dSJames Smart 	else {
24861b51197dSJames Smart 		*rpi = lpfc_sli4_alloc_rpi(phba);
24879d3d340dSJames Smart 		if (*rpi == LPFC_RPI_ALLOC_ERROR) {
24889d3d340dSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
24899d3d340dSJames Smart 			return -EBUSY;
24909d3d340dSJames Smart 		}
24911b51197dSJames Smart 		status = lpfc_reg_rpi(phba, phba->pport->vpi,
24921b51197dSJames Smart 				phba->pport->fc_myDID,
24931b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
24941b51197dSJames Smart 				mbox, *rpi);
24951b51197dSJames Smart 	}
24961b51197dSJames Smart 
24973b5dd52aSJames Smart 	if (status) {
24983b5dd52aSJames Smart 		mempool_free(mbox, phba->mbox_mem_pool);
24994042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
25004042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2501d439d286SJames Smart 		return -ENOMEM;
25023b5dd52aSJames Smart 	}
25033b5dd52aSJames Smart 
25043b5dd52aSJames Smart 	dmabuff = (struct lpfc_dmabuf *) mbox->context1;
25053b5dd52aSJames Smart 	mbox->context1 = NULL;
2506d439d286SJames Smart 	mbox->context2 = NULL;
25073b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
25083b5dd52aSJames Smart 
25093b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
25103b5dd52aSJames Smart 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
25113b5dd52aSJames Smart 		kfree(dmabuff);
25123b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
25133b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
25144042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
25154042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2516d439d286SJames Smart 		return -ENODEV;
25173b5dd52aSJames Smart 	}
25183b5dd52aSJames Smart 
25191b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
25203b5dd52aSJames Smart 		*rpi = mbox->u.mb.un.varWords[0];
25213b5dd52aSJames Smart 
25223b5dd52aSJames Smart 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
25233b5dd52aSJames Smart 	kfree(dmabuff);
25243b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
25253b5dd52aSJames Smart 	return 0;
25263b5dd52aSJames Smart }
25273b5dd52aSJames Smart 
25283b5dd52aSJames Smart /**
25293b5dd52aSJames Smart  * lpfcdiag_loop_self_unreg - unregs from the rpi
25303b5dd52aSJames Smart  * @phba: Pointer to HBA context object
25313b5dd52aSJames Smart  * @rpi: Remote port login id
25323b5dd52aSJames Smart  *
25333b5dd52aSJames Smart  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
25343b5dd52aSJames Smart  **/
25353b5dd52aSJames Smart static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
25363b5dd52aSJames Smart {
25373b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
25383b5dd52aSJames Smart 	int status;
25393b5dd52aSJames Smart 
25403b5dd52aSJames Smart 	/* Allocate mboxq structure */
25413b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
25423b5dd52aSJames Smart 	if (mbox == NULL)
2543d439d286SJames Smart 		return -ENOMEM;
25443b5dd52aSJames Smart 
25451b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
25463b5dd52aSJames Smart 		lpfc_unreg_login(phba, 0, rpi, mbox);
25471b51197dSJames Smart 	else
25481b51197dSJames Smart 		lpfc_unreg_login(phba, phba->pport->vpi,
25491b51197dSJames Smart 				 phba->sli4_hba.rpi_ids[rpi], mbox);
25501b51197dSJames Smart 
25513b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
25523b5dd52aSJames Smart 
25533b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
25543b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
25553b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
2556d439d286SJames Smart 		return -EIO;
25573b5dd52aSJames Smart 	}
25583b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
25594042629eSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
25604042629eSJames Smart 		lpfc_sli4_free_rpi(phba, rpi);
25613b5dd52aSJames Smart 	return 0;
25623b5dd52aSJames Smart }
25633b5dd52aSJames Smart 
25643b5dd52aSJames Smart /**
25653b5dd52aSJames Smart  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
25663b5dd52aSJames Smart  * @phba: Pointer to HBA context object
25673b5dd52aSJames Smart  * @rpi: Remote port login id
25683b5dd52aSJames Smart  * @txxri: Pointer to transmit exchange id
25693b5dd52aSJames Smart  * @rxxri: Pointer to response exchabge id
25703b5dd52aSJames Smart  *
25713b5dd52aSJames Smart  * This function obtains the transmit and receive ids required to send
25723b5dd52aSJames Smart  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
25733b5dd52aSJames Smart  * flags are used to the unsolicted response handler is able to process
25743b5dd52aSJames Smart  * the ct command sent on the same port.
25753b5dd52aSJames Smart  **/
25763b5dd52aSJames Smart static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
25773b5dd52aSJames Smart 			 uint16_t *txxri, uint16_t * rxxri)
25783b5dd52aSJames Smart {
25793b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
25803b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
25813b5dd52aSJames Smart 	IOCB_t *cmd, *rsp;
25823b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuf;
25833b5dd52aSJames Smart 	struct ulp_bde64 *bpl = NULL;
25843b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq = NULL;
25853b5dd52aSJames Smart 	int ret_val = 0;
2586d439d286SJames Smart 	int time_left;
25875a0916b4SJames Smart 	int iocb_stat = IOCB_SUCCESS;
25883b5dd52aSJames Smart 	unsigned long flags;
25893b5dd52aSJames Smart 
25903b5dd52aSJames Smart 	*txxri = 0;
25913b5dd52aSJames Smart 	*rxxri = 0;
25923b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
25933b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
25943b5dd52aSJames Smart 	if (!evt)
2595d439d286SJames Smart 		return -ENOMEM;
25963b5dd52aSJames Smart 
25973b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
25983b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
25993b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
26003b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26013b5dd52aSJames Smart 
26023b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
26033b5dd52aSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
26043b5dd52aSJames Smart 
26053b5dd52aSJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
26063b5dd52aSJames Smart 	if (dmabuf) {
26073b5dd52aSJames Smart 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2608c7495937SJames Smart 		if (dmabuf->virt) {
26093b5dd52aSJames Smart 			INIT_LIST_HEAD(&dmabuf->list);
26103b5dd52aSJames Smart 			bpl = (struct ulp_bde64 *) dmabuf->virt;
26113b5dd52aSJames Smart 			memset(bpl, 0, sizeof(*bpl));
26123b5dd52aSJames Smart 			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
26133b5dd52aSJames Smart 			bpl->addrHigh =
2614c7495937SJames Smart 				le32_to_cpu(putPaddrHigh(dmabuf->phys +
2615c7495937SJames Smart 					sizeof(*bpl)));
26163b5dd52aSJames Smart 			bpl->addrLow =
2617c7495937SJames Smart 				le32_to_cpu(putPaddrLow(dmabuf->phys +
2618c7495937SJames Smart 					sizeof(*bpl)));
26193b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
26203b5dd52aSJames Smart 			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
26213b5dd52aSJames Smart 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
26223b5dd52aSJames Smart 		}
2623c7495937SJames Smart 	}
26243b5dd52aSJames Smart 
26253b5dd52aSJames Smart 	if (cmdiocbq == NULL || rspiocbq == NULL ||
2626c7495937SJames Smart 	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2627c7495937SJames Smart 		dmabuf->virt == NULL) {
2628d439d286SJames Smart 		ret_val = -ENOMEM;
26293b5dd52aSJames Smart 		goto err_get_xri_exit;
26303b5dd52aSJames Smart 	}
26313b5dd52aSJames Smart 
26323b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
26333b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
26343b5dd52aSJames Smart 
26353b5dd52aSJames Smart 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
26363b5dd52aSJames Smart 
26373b5dd52aSJames Smart 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
26383b5dd52aSJames Smart 	ctreq->RevisionId.bits.InId = 0;
26393b5dd52aSJames Smart 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
26403b5dd52aSJames Smart 	ctreq->FsSubType = 0;
26413b5dd52aSJames Smart 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
26423b5dd52aSJames Smart 	ctreq->CommandResponse.bits.Size = 0;
26433b5dd52aSJames Smart 
26443b5dd52aSJames Smart 
26453b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
26463b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
26473b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
26483b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
26493b5dd52aSJames Smart 
26503b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = LA;
26513b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
26523b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
26533b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
26543b5dd52aSJames Smart 
26553b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
26563b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
26573b5dd52aSJames Smart 	cmd->ulpLe = 1;
26583b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
26593b5dd52aSJames Smart 	cmd->ulpContext = rpi;
26603b5dd52aSJames Smart 
26613b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
26623b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
26635a0916b4SJames Smart 	cmdiocbq->iocb_cmpl = NULL;
26643b5dd52aSJames Smart 
2665d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
26663b5dd52aSJames Smart 				rspiocbq,
26673b5dd52aSJames Smart 				(phba->fc_ratov * 2)
26683b5dd52aSJames Smart 				+ LPFC_DRVR_TIMEOUT);
266953151bbbSJames Smart 	if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOSTAT_SUCCESS)) {
2670d439d286SJames Smart 		ret_val = -EIO;
26713b5dd52aSJames Smart 		goto err_get_xri_exit;
2672d439d286SJames Smart 	}
26733b5dd52aSJames Smart 	*txxri =  rsp->ulpContext;
26743b5dd52aSJames Smart 
26753b5dd52aSJames Smart 	evt->waiting = 1;
26763b5dd52aSJames Smart 	evt->wait_time_stamp = jiffies;
2677d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
26783b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
2679256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
2680256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
26813b5dd52aSJames Smart 	if (list_empty(&evt->events_to_see))
2682d439d286SJames Smart 		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
26833b5dd52aSJames Smart 	else {
26843b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
26853b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
26863b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26873b5dd52aSJames Smart 		*rxxri = (list_entry(evt->events_to_get.prev,
26883b5dd52aSJames Smart 				     typeof(struct event_data),
26893b5dd52aSJames Smart 				     node))->immed_dat;
26903b5dd52aSJames Smart 	}
26913b5dd52aSJames Smart 	evt->waiting = 0;
26923b5dd52aSJames Smart 
26933b5dd52aSJames Smart err_get_xri_exit:
26943b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
26953b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
26963b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
26973b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26983b5dd52aSJames Smart 
26993b5dd52aSJames Smart 	if (dmabuf) {
27003b5dd52aSJames Smart 		if (dmabuf->virt)
27013b5dd52aSJames Smart 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
27023b5dd52aSJames Smart 		kfree(dmabuf);
27033b5dd52aSJames Smart 	}
27043b5dd52aSJames Smart 
2705d439d286SJames Smart 	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
27063b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
27073b5dd52aSJames Smart 	if (rspiocbq)
27083b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
27093b5dd52aSJames Smart 	return ret_val;
27103b5dd52aSJames Smart }
27113b5dd52aSJames Smart 
27123b5dd52aSJames Smart /**
27137ad20aa9SJames Smart  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
27147ad20aa9SJames Smart  * @phba: Pointer to HBA context object
27157ad20aa9SJames Smart  *
27162ea259eeSJames Smart  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
27179e03aa2fSJoe Perches  * returns the pointer to the buffer.
27187ad20aa9SJames Smart  **/
27197ad20aa9SJames Smart static struct lpfc_dmabuf *
27207ad20aa9SJames Smart lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
27217ad20aa9SJames Smart {
27227ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
27237ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
27247ad20aa9SJames Smart 
27257ad20aa9SJames Smart 	/* allocate dma buffer struct */
27267ad20aa9SJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
27277ad20aa9SJames Smart 	if (!dmabuf)
27287ad20aa9SJames Smart 		return NULL;
27297ad20aa9SJames Smart 
27307ad20aa9SJames Smart 	INIT_LIST_HEAD(&dmabuf->list);
27317ad20aa9SJames Smart 
27327ad20aa9SJames Smart 	/* now, allocate dma buffer */
27331aee383dSJoe Perches 	dmabuf->virt = dma_zalloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
27347ad20aa9SJames Smart 					   &(dmabuf->phys), GFP_KERNEL);
27357ad20aa9SJames Smart 
27367ad20aa9SJames Smart 	if (!dmabuf->virt) {
27377ad20aa9SJames Smart 		kfree(dmabuf);
27387ad20aa9SJames Smart 		return NULL;
27397ad20aa9SJames Smart 	}
27407ad20aa9SJames Smart 
27417ad20aa9SJames Smart 	return dmabuf;
27427ad20aa9SJames Smart }
27437ad20aa9SJames Smart 
27447ad20aa9SJames Smart /**
27457ad20aa9SJames Smart  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
27467ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
27477ad20aa9SJames Smart  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
27487ad20aa9SJames Smart  *
27497ad20aa9SJames Smart  * This routine just simply frees a dma buffer and its associated buffer
27507ad20aa9SJames Smart  * descriptor referred by @dmabuf.
27517ad20aa9SJames Smart  **/
27527ad20aa9SJames Smart static void
27537ad20aa9SJames Smart lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
27547ad20aa9SJames Smart {
27557ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
27567ad20aa9SJames Smart 
27577ad20aa9SJames Smart 	if (!dmabuf)
27587ad20aa9SJames Smart 		return;
27597ad20aa9SJames Smart 
27607ad20aa9SJames Smart 	if (dmabuf->virt)
27617ad20aa9SJames Smart 		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
27627ad20aa9SJames Smart 				  dmabuf->virt, dmabuf->phys);
27637ad20aa9SJames Smart 	kfree(dmabuf);
27647ad20aa9SJames Smart 	return;
27657ad20aa9SJames Smart }
27667ad20aa9SJames Smart 
27677ad20aa9SJames Smart /**
27687ad20aa9SJames Smart  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
27697ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
27707ad20aa9SJames Smart  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
27717ad20aa9SJames Smart  *
27727ad20aa9SJames Smart  * This routine just simply frees all dma buffers and their associated buffer
27737ad20aa9SJames Smart  * descriptors referred by @dmabuf_list.
27747ad20aa9SJames Smart  **/
27757ad20aa9SJames Smart static void
27767ad20aa9SJames Smart lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
27777ad20aa9SJames Smart 			    struct list_head *dmabuf_list)
27787ad20aa9SJames Smart {
27797ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
27807ad20aa9SJames Smart 
27817ad20aa9SJames Smart 	if (list_empty(dmabuf_list))
27827ad20aa9SJames Smart 		return;
27837ad20aa9SJames Smart 
27847ad20aa9SJames Smart 	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
27857ad20aa9SJames Smart 		list_del_init(&dmabuf->list);
27867ad20aa9SJames Smart 		lpfc_bsg_dma_page_free(phba, dmabuf);
27877ad20aa9SJames Smart 	}
27887ad20aa9SJames Smart 	return;
27897ad20aa9SJames Smart }
27907ad20aa9SJames Smart 
27917ad20aa9SJames Smart /**
27923b5dd52aSJames Smart  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
27933b5dd52aSJames Smart  * @phba: Pointer to HBA context object
27943b5dd52aSJames Smart  * @bpl: Pointer to 64 bit bde structure
27953b5dd52aSJames Smart  * @size: Number of bytes to process
27963b5dd52aSJames Smart  * @nocopydata: Flag to copy user data into the allocated buffer
27973b5dd52aSJames Smart  *
27983b5dd52aSJames Smart  * This function allocates page size buffers and populates an lpfc_dmabufext.
27993b5dd52aSJames Smart  * If allowed the user data pointed to with indataptr is copied into the kernel
28003b5dd52aSJames Smart  * memory. The chained list of page size buffers is returned.
28013b5dd52aSJames Smart  **/
28023b5dd52aSJames Smart static struct lpfc_dmabufext *
28033b5dd52aSJames Smart diag_cmd_data_alloc(struct lpfc_hba *phba,
28043b5dd52aSJames Smart 		   struct ulp_bde64 *bpl, uint32_t size,
28053b5dd52aSJames Smart 		   int nocopydata)
28063b5dd52aSJames Smart {
28073b5dd52aSJames Smart 	struct lpfc_dmabufext *mlist = NULL;
28083b5dd52aSJames Smart 	struct lpfc_dmabufext *dmp;
28093b5dd52aSJames Smart 	int cnt, offset = 0, i = 0;
28103b5dd52aSJames Smart 	struct pci_dev *pcidev;
28113b5dd52aSJames Smart 
28123b5dd52aSJames Smart 	pcidev = phba->pcidev;
28133b5dd52aSJames Smart 
28143b5dd52aSJames Smart 	while (size) {
28153b5dd52aSJames Smart 		/* We get chunks of 4K */
28163b5dd52aSJames Smart 		if (size > BUF_SZ_4K)
28173b5dd52aSJames Smart 			cnt = BUF_SZ_4K;
28183b5dd52aSJames Smart 		else
28193b5dd52aSJames Smart 			cnt = size;
28203b5dd52aSJames Smart 
28213b5dd52aSJames Smart 		/* allocate struct lpfc_dmabufext buffer header */
28223b5dd52aSJames Smart 		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
28233b5dd52aSJames Smart 		if (!dmp)
28243b5dd52aSJames Smart 			goto out;
28253b5dd52aSJames Smart 
28263b5dd52aSJames Smart 		INIT_LIST_HEAD(&dmp->dma.list);
28273b5dd52aSJames Smart 
28283b5dd52aSJames Smart 		/* Queue it to a linked list */
28293b5dd52aSJames Smart 		if (mlist)
28303b5dd52aSJames Smart 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
28313b5dd52aSJames Smart 		else
28323b5dd52aSJames Smart 			mlist = dmp;
28333b5dd52aSJames Smart 
28343b5dd52aSJames Smart 		/* allocate buffer */
28353b5dd52aSJames Smart 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
28363b5dd52aSJames Smart 						   cnt,
28373b5dd52aSJames Smart 						   &(dmp->dma.phys),
28383b5dd52aSJames Smart 						   GFP_KERNEL);
28393b5dd52aSJames Smart 
28403b5dd52aSJames Smart 		if (!dmp->dma.virt)
28413b5dd52aSJames Smart 			goto out;
28423b5dd52aSJames Smart 
28433b5dd52aSJames Smart 		dmp->size = cnt;
28443b5dd52aSJames Smart 
28453b5dd52aSJames Smart 		if (nocopydata) {
28463b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
28473b5dd52aSJames Smart 		} else {
28483b5dd52aSJames Smart 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
28493b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
28503b5dd52aSJames Smart 		}
28513b5dd52aSJames Smart 
28523b5dd52aSJames Smart 		/* build buffer ptr list for IOCB */
28533b5dd52aSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
28543b5dd52aSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
28553b5dd52aSJames Smart 		bpl->tus.f.bdeSize = (ushort) cnt;
28563b5dd52aSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
28573b5dd52aSJames Smart 		bpl++;
28583b5dd52aSJames Smart 
28593b5dd52aSJames Smart 		i++;
28603b5dd52aSJames Smart 		offset += cnt;
28613b5dd52aSJames Smart 		size -= cnt;
28623b5dd52aSJames Smart 	}
28633b5dd52aSJames Smart 
2864a2fc4aefSJames Smart 	if (mlist) {
28653b5dd52aSJames Smart 		mlist->flag = i;
28663b5dd52aSJames Smart 		return mlist;
2867a2fc4aefSJames Smart 	}
28683b5dd52aSJames Smart out:
28693b5dd52aSJames Smart 	diag_cmd_data_free(phba, mlist);
28703b5dd52aSJames Smart 	return NULL;
28713b5dd52aSJames Smart }
28723b5dd52aSJames Smart 
28733b5dd52aSJames Smart /**
28743b5dd52aSJames Smart  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
28753b5dd52aSJames Smart  * @phba: Pointer to HBA context object
28763b5dd52aSJames Smart  * @rxxri: Receive exchange id
28773b5dd52aSJames Smart  * @len: Number of data bytes
28783b5dd52aSJames Smart  *
287925985edcSLucas De Marchi  * This function allocates and posts a data buffer of sufficient size to receive
28803b5dd52aSJames Smart  * an unsolicted CT command.
28813b5dd52aSJames Smart  **/
28823b5dd52aSJames Smart static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
28833b5dd52aSJames Smart 			     size_t len)
28843b5dd52aSJames Smart {
2885895427bdSJames Smart 	struct lpfc_sli_ring *pring;
28863b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq;
28873b5dd52aSJames Smart 	IOCB_t *cmd = NULL;
28883b5dd52aSJames Smart 	struct list_head head, *curr, *next;
28893b5dd52aSJames Smart 	struct lpfc_dmabuf *rxbmp;
28903b5dd52aSJames Smart 	struct lpfc_dmabuf *dmp;
28913b5dd52aSJames Smart 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
28923b5dd52aSJames Smart 	struct ulp_bde64 *rxbpl = NULL;
28933b5dd52aSJames Smart 	uint32_t num_bde;
28943b5dd52aSJames Smart 	struct lpfc_dmabufext *rxbuffer = NULL;
28953b5dd52aSJames Smart 	int ret_val = 0;
2896d439d286SJames Smart 	int iocb_stat;
28973b5dd52aSJames Smart 	int i = 0;
28983b5dd52aSJames Smart 
2899895427bdSJames Smart 	pring = lpfc_phba_elsring(phba);
2900895427bdSJames Smart 
29013b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
29023b5dd52aSJames Smart 	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
29033b5dd52aSJames Smart 	if (rxbmp != NULL) {
29043b5dd52aSJames Smart 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2905c7495937SJames Smart 		if (rxbmp->virt) {
29063b5dd52aSJames Smart 			INIT_LIST_HEAD(&rxbmp->list);
29073b5dd52aSJames Smart 			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
29083b5dd52aSJames Smart 			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
29093b5dd52aSJames Smart 		}
2910c7495937SJames Smart 	}
29113b5dd52aSJames Smart 
29121234a6d5SDick Kennedy 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2913d439d286SJames Smart 		ret_val = -ENOMEM;
29143b5dd52aSJames Smart 		goto err_post_rxbufs_exit;
29153b5dd52aSJames Smart 	}
29163b5dd52aSJames Smart 
29173b5dd52aSJames Smart 	/* Queue buffers for the receive exchange */
29183b5dd52aSJames Smart 	num_bde = (uint32_t)rxbuffer->flag;
29193b5dd52aSJames Smart 	dmp = &rxbuffer->dma;
29203b5dd52aSJames Smart 
29213b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
29223b5dd52aSJames Smart 	i = 0;
29233b5dd52aSJames Smart 
29243b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
29253b5dd52aSJames Smart 	list_add_tail(&head, &dmp->list);
29263b5dd52aSJames Smart 	list_for_each_safe(curr, next, &head) {
29273b5dd52aSJames Smart 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
29283b5dd52aSJames Smart 		list_del(curr);
29293b5dd52aSJames Smart 
29303b5dd52aSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
29313b5dd52aSJames Smart 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
29323b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrHigh =
29333b5dd52aSJames Smart 				putPaddrHigh(mp[i]->phys);
29343b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrLow =
29353b5dd52aSJames Smart 				putPaddrLow(mp[i]->phys);
29363b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
29373b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
29383b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
29393b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
29403b5dd52aSJames Smart 			cmd->ulpPU = 0;
29413b5dd52aSJames Smart 			cmd->ulpLe = 1;
29423b5dd52aSJames Smart 			cmd->ulpBdeCount = 1;
29433b5dd52aSJames Smart 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
29443b5dd52aSJames Smart 
29453b5dd52aSJames Smart 		} else {
29463b5dd52aSJames Smart 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
29473b5dd52aSJames Smart 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
29483b5dd52aSJames Smart 			cmd->un.cont64[i].tus.f.bdeSize =
29493b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
29503b5dd52aSJames Smart 					cmd->ulpBdeCount = ++i;
29513b5dd52aSJames Smart 
29523b5dd52aSJames Smart 			if ((--num_bde > 0) && (i < 2))
29533b5dd52aSJames Smart 				continue;
29543b5dd52aSJames Smart 
29553b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
29563b5dd52aSJames Smart 			cmd->ulpLe = 1;
29573b5dd52aSJames Smart 		}
29583b5dd52aSJames Smart 
29593b5dd52aSJames Smart 		cmd->ulpClass = CLASS3;
29603b5dd52aSJames Smart 		cmd->ulpContext = rxxri;
29613b5dd52aSJames Smart 
2962d439d286SJames Smart 		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2963d439d286SJames Smart 						0);
2964d439d286SJames Smart 		if (iocb_stat == IOCB_ERROR) {
29653b5dd52aSJames Smart 			diag_cmd_data_free(phba,
29663b5dd52aSJames Smart 				(struct lpfc_dmabufext *)mp[0]);
29673b5dd52aSJames Smart 			if (mp[1])
29683b5dd52aSJames Smart 				diag_cmd_data_free(phba,
29693b5dd52aSJames Smart 					  (struct lpfc_dmabufext *)mp[1]);
29703b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2971d439d286SJames Smart 			ret_val = -EIO;
29723b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
29733b5dd52aSJames Smart 		}
29743b5dd52aSJames Smart 
29753b5dd52aSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
29763b5dd52aSJames Smart 		if (mp[1]) {
29773b5dd52aSJames Smart 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
29783b5dd52aSJames Smart 			mp[1] = NULL;
29793b5dd52aSJames Smart 		}
29803b5dd52aSJames Smart 
29813b5dd52aSJames Smart 		/* The iocb was freed by lpfc_sli_issue_iocb */
29823b5dd52aSJames Smart 		cmdiocbq = lpfc_sli_get_iocbq(phba);
29833b5dd52aSJames Smart 		if (!cmdiocbq) {
29843b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2985d439d286SJames Smart 			ret_val = -EIO;
29863b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
29873b5dd52aSJames Smart 		}
29883b5dd52aSJames Smart 
29893b5dd52aSJames Smart 		cmd = &cmdiocbq->iocb;
29903b5dd52aSJames Smart 		i = 0;
29913b5dd52aSJames Smart 	}
29923b5dd52aSJames Smart 	list_del(&head);
29933b5dd52aSJames Smart 
29943b5dd52aSJames Smart err_post_rxbufs_exit:
29953b5dd52aSJames Smart 
29963b5dd52aSJames Smart 	if (rxbmp) {
29973b5dd52aSJames Smart 		if (rxbmp->virt)
29983b5dd52aSJames Smart 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
29993b5dd52aSJames Smart 		kfree(rxbmp);
30003b5dd52aSJames Smart 	}
30013b5dd52aSJames Smart 
30023b5dd52aSJames Smart 	if (cmdiocbq)
30033b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
30043b5dd52aSJames Smart 	return ret_val;
30053b5dd52aSJames Smart }
30063b5dd52aSJames Smart 
30073b5dd52aSJames Smart /**
30087ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
30093b5dd52aSJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
30103b5dd52aSJames Smart  *
30113b5dd52aSJames Smart  * This function receives a user data buffer to be transmitted and received on
30123b5dd52aSJames Smart  * the same port, the link must be up and in loopback mode prior
30133b5dd52aSJames Smart  * to being called.
30143b5dd52aSJames Smart  * 1. A kernel buffer is allocated to copy the user data into.
30153b5dd52aSJames Smart  * 2. The port registers with "itself".
30163b5dd52aSJames Smart  * 3. The transmit and receive exchange ids are obtained.
30173b5dd52aSJames Smart  * 4. The receive exchange id is posted.
30183b5dd52aSJames Smart  * 5. A new els loopback event is created.
30193b5dd52aSJames Smart  * 6. The command and response iocbs are allocated.
30203b5dd52aSJames Smart  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
30213b5dd52aSJames Smart  *
30223b5dd52aSJames Smart  * This function is meant to be called n times while the port is in loopback
30233b5dd52aSJames Smart  * so it is the apps responsibility to issue a reset to take the port out
30243b5dd52aSJames Smart  * of loopback mode.
30253b5dd52aSJames Smart  **/
30263b5dd52aSJames Smart static int
302775cc8cfcSJohannes Thumshirn lpfc_bsg_diag_loopback_run(struct bsg_job *job)
30283b5dd52aSJames Smart {
3029cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
303001e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
30313b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
30323b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
30333b5dd52aSJames Smart 	struct event_data *evdat;
30343b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
30353b5dd52aSJames Smart 	uint32_t size;
30363b5dd52aSJames Smart 	uint32_t full_size;
30373b5dd52aSJames Smart 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
30384042629eSJames Smart 	uint16_t rpi = 0;
30391b51197dSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
30401b51197dSJames Smart 	IOCB_t *cmd, *rsp = NULL;
30413b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq;
30423b5dd52aSJames Smart 	struct lpfc_dmabuf *txbmp;
30433b5dd52aSJames Smart 	struct ulp_bde64 *txbpl = NULL;
30443b5dd52aSJames Smart 	struct lpfc_dmabufext *txbuffer = NULL;
30453b5dd52aSJames Smart 	struct list_head head;
30463b5dd52aSJames Smart 	struct lpfc_dmabuf  *curr;
30471b51197dSJames Smart 	uint16_t txxri = 0, rxxri;
30483b5dd52aSJames Smart 	uint32_t num_bde;
30493b5dd52aSJames Smart 	uint8_t *ptr = NULL, *rx_databuf = NULL;
30503b5dd52aSJames Smart 	int rc = 0;
3051d439d286SJames Smart 	int time_left;
30525a0916b4SJames Smart 	int iocb_stat = IOCB_SUCCESS;
30533b5dd52aSJames Smart 	unsigned long flags;
30543b5dd52aSJames Smart 	void *dataout = NULL;
30553b5dd52aSJames Smart 	uint32_t total_mem;
30563b5dd52aSJames Smart 
30573b5dd52aSJames Smart 	/* in case no data is returned return just the return code */
305801e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
30593b5dd52aSJames Smart 
30603b5dd52aSJames Smart 	if (job->request_len <
30613b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
30623b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
30633b5dd52aSJames Smart 				"2739 Received DIAG TEST request below minimum "
30643b5dd52aSJames Smart 				"size\n");
30653b5dd52aSJames Smart 		rc = -EINVAL;
30663b5dd52aSJames Smart 		goto loopback_test_exit;
30673b5dd52aSJames Smart 	}
30683b5dd52aSJames Smart 
30693b5dd52aSJames Smart 	if (job->request_payload.payload_len !=
30703b5dd52aSJames Smart 		job->reply_payload.payload_len) {
30713b5dd52aSJames Smart 		rc = -EINVAL;
30723b5dd52aSJames Smart 		goto loopback_test_exit;
30733b5dd52aSJames Smart 	}
30743b5dd52aSJames Smart 
30753b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
30763b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
30773b5dd52aSJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
30783b5dd52aSJames Smart 		rc = -EACCES;
30793b5dd52aSJames Smart 		goto loopback_test_exit;
30803b5dd52aSJames Smart 	}
30813b5dd52aSJames Smart 
30823b5dd52aSJames Smart 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
30833b5dd52aSJames Smart 		rc = -EACCES;
30843b5dd52aSJames Smart 		goto loopback_test_exit;
30853b5dd52aSJames Smart 	}
30863b5dd52aSJames Smart 
30873b5dd52aSJames Smart 	size = job->request_payload.payload_len;
30883b5dd52aSJames Smart 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
30893b5dd52aSJames Smart 
30903b5dd52aSJames Smart 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
30913b5dd52aSJames Smart 		rc = -ERANGE;
30923b5dd52aSJames Smart 		goto loopback_test_exit;
30933b5dd52aSJames Smart 	}
30943b5dd52aSJames Smart 
309563e801ceSJames Smart 	if (full_size >= BUF_SZ_4K) {
30963b5dd52aSJames Smart 		/*
30973b5dd52aSJames Smart 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
30983b5dd52aSJames Smart 		 * then we allocate 64k and re-use that buffer over and over to
30993b5dd52aSJames Smart 		 * xfer the whole block. This is because Linux kernel has a
31003b5dd52aSJames Smart 		 * problem allocating more than 120k of kernel space memory. Saw
31013b5dd52aSJames Smart 		 * problem with GET_FCPTARGETMAPPING...
31023b5dd52aSJames Smart 		 */
31033b5dd52aSJames Smart 		if (size <= (64 * 1024))
310463e801ceSJames Smart 			total_mem = full_size;
31053b5dd52aSJames Smart 		else
31063b5dd52aSJames Smart 			total_mem = 64 * 1024;
31073b5dd52aSJames Smart 	} else
31083b5dd52aSJames Smart 		/* Allocate memory for ioctl data */
31093b5dd52aSJames Smart 		total_mem = BUF_SZ_4K;
31103b5dd52aSJames Smart 
31113b5dd52aSJames Smart 	dataout = kmalloc(total_mem, GFP_KERNEL);
31123b5dd52aSJames Smart 	if (dataout == NULL) {
31133b5dd52aSJames Smart 		rc = -ENOMEM;
31143b5dd52aSJames Smart 		goto loopback_test_exit;
31153b5dd52aSJames Smart 	}
31163b5dd52aSJames Smart 
31173b5dd52aSJames Smart 	ptr = dataout;
31183b5dd52aSJames Smart 	ptr += ELX_LOOPBACK_HEADER_SZ;
31193b5dd52aSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
31203b5dd52aSJames Smart 				job->request_payload.sg_cnt,
31213b5dd52aSJames Smart 				ptr, size);
31223b5dd52aSJames Smart 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
3123d439d286SJames Smart 	if (rc)
31243b5dd52aSJames Smart 		goto loopback_test_exit;
31253b5dd52aSJames Smart 
31261b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
31273b5dd52aSJames Smart 		rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
31283b5dd52aSJames Smart 		if (rc) {
31293b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
31303b5dd52aSJames Smart 			goto loopback_test_exit;
31313b5dd52aSJames Smart 		}
31323b5dd52aSJames Smart 
31333b5dd52aSJames Smart 		rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
31343b5dd52aSJames Smart 		if (rc) {
31353b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
31363b5dd52aSJames Smart 			goto loopback_test_exit;
31373b5dd52aSJames Smart 		}
31381b51197dSJames Smart 	}
31393b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
31403b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
31413b5dd52aSJames Smart 	if (!evt) {
31423b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
31433b5dd52aSJames Smart 		rc = -ENOMEM;
31443b5dd52aSJames Smart 		goto loopback_test_exit;
31453b5dd52aSJames Smart 	}
31463b5dd52aSJames Smart 
31473b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
31483b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
31493b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
31503b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
31513b5dd52aSJames Smart 
31523b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
31531b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
31543b5dd52aSJames Smart 		rspiocbq = lpfc_sli_get_iocbq(phba);
31553b5dd52aSJames Smart 	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
31563b5dd52aSJames Smart 
31573b5dd52aSJames Smart 	if (txbmp) {
31583b5dd52aSJames Smart 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3159c7495937SJames Smart 		if (txbmp->virt) {
31603b5dd52aSJames Smart 			INIT_LIST_HEAD(&txbmp->list);
31613b5dd52aSJames Smart 			txbpl = (struct ulp_bde64 *) txbmp->virt;
31623b5dd52aSJames Smart 			txbuffer = diag_cmd_data_alloc(phba,
31633b5dd52aSJames Smart 							txbpl, full_size, 0);
31643b5dd52aSJames Smart 		}
3165c7495937SJames Smart 	}
31663b5dd52aSJames Smart 
31671b51197dSJames Smart 	if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
31681b51197dSJames Smart 		rc = -ENOMEM;
31691b51197dSJames Smart 		goto err_loopback_test_exit;
31701b51197dSJames Smart 	}
31711b51197dSJames Smart 	if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
31723b5dd52aSJames Smart 		rc = -ENOMEM;
31733b5dd52aSJames Smart 		goto err_loopback_test_exit;
31743b5dd52aSJames Smart 	}
31753b5dd52aSJames Smart 
31763b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
31771b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
31783b5dd52aSJames Smart 		rsp = &rspiocbq->iocb;
31793b5dd52aSJames Smart 
31803b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
31813b5dd52aSJames Smart 	list_add_tail(&head, &txbuffer->dma.list);
31823b5dd52aSJames Smart 	list_for_each_entry(curr, &head, list) {
31833b5dd52aSJames Smart 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
31843b5dd52aSJames Smart 		if (current_offset == 0) {
31853b5dd52aSJames Smart 			ctreq = curr->virt;
31863b5dd52aSJames Smart 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
31873b5dd52aSJames Smart 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
31883b5dd52aSJames Smart 			ctreq->RevisionId.bits.InId = 0;
31893b5dd52aSJames Smart 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
31903b5dd52aSJames Smart 			ctreq->FsSubType = 0;
31913b5dd52aSJames Smart 			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
31923b5dd52aSJames Smart 			ctreq->CommandResponse.bits.Size   = size;
31933b5dd52aSJames Smart 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
31943b5dd52aSJames Smart 		} else
31953b5dd52aSJames Smart 			segment_offset = 0;
31963b5dd52aSJames Smart 
31973b5dd52aSJames Smart 		BUG_ON(segment_offset >= segment_len);
31983b5dd52aSJames Smart 		memcpy(curr->virt + segment_offset,
31993b5dd52aSJames Smart 			ptr + current_offset,
32003b5dd52aSJames Smart 			segment_len - segment_offset);
32013b5dd52aSJames Smart 
32023b5dd52aSJames Smart 		current_offset += segment_len - segment_offset;
32033b5dd52aSJames Smart 		BUG_ON(current_offset > size);
32043b5dd52aSJames Smart 	}
32053b5dd52aSJames Smart 	list_del(&head);
32063b5dd52aSJames Smart 
32073b5dd52aSJames Smart 	/* Build the XMIT_SEQUENCE iocb */
32083b5dd52aSJames Smart 	num_bde = (uint32_t)txbuffer->flag;
32093b5dd52aSJames Smart 
32103b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
32113b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
32123b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
32133b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
32143b5dd52aSJames Smart 
32153b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
32163b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
32173b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
32183b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
32193b5dd52aSJames Smart 
32203b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
32213b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
32223b5dd52aSJames Smart 	cmd->ulpLe = 1;
32233b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
32243b5dd52aSJames Smart 
32251b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
32261b51197dSJames Smart 		cmd->ulpContext = txxri;
32271b51197dSJames Smart 	} else {
32281b51197dSJames Smart 		cmd->un.xseq64.bdl.ulpIoTag32 = 0;
32291b51197dSJames Smart 		cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
32301b51197dSJames Smart 		cmdiocbq->context3 = txbmp;
32311b51197dSJames Smart 		cmdiocbq->sli4_xritag = NO_XRI;
32321b51197dSJames Smart 		cmd->unsli3.rcvsli3.ox_id = 0xffff;
32331b51197dSJames Smart 	}
32343b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
32356c7cf486SJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LOOPBACK;
32363b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
32375a0916b4SJames Smart 	cmdiocbq->iocb_cmpl = NULL;
3238d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3239d439d286SJames Smart 					     rspiocbq, (phba->fc_ratov * 2) +
3240d439d286SJames Smart 					     LPFC_DRVR_TIMEOUT);
32413b5dd52aSJames Smart 
324253151bbbSJames Smart 	if ((iocb_stat != IOCB_SUCCESS) ||
324353151bbbSJames Smart 	    ((phba->sli_rev < LPFC_SLI_REV4) &&
324453151bbbSJames Smart 	     (rsp->ulpStatus != IOSTAT_SUCCESS))) {
32451b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
32461b51197dSJames Smart 				"3126 Failed loopback test issue iocb: "
32471b51197dSJames Smart 				"iocb_stat:x%x\n", iocb_stat);
32483b5dd52aSJames Smart 		rc = -EIO;
32493b5dd52aSJames Smart 		goto err_loopback_test_exit;
32503b5dd52aSJames Smart 	}
32513b5dd52aSJames Smart 
32523b5dd52aSJames Smart 	evt->waiting = 1;
3253d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
32543b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
3255256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
3256256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
32573b5dd52aSJames Smart 	evt->waiting = 0;
32581b51197dSJames Smart 	if (list_empty(&evt->events_to_see)) {
3259d439d286SJames Smart 		rc = (time_left) ? -EINTR : -ETIMEDOUT;
32601b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
32611b51197dSJames Smart 				"3125 Not receiving unsolicited event, "
32621b51197dSJames Smart 				"rc:x%x\n", rc);
32631b51197dSJames Smart 	} else {
32643b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
32653b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
32663b5dd52aSJames Smart 		evdat = list_entry(evt->events_to_get.prev,
32673b5dd52aSJames Smart 				   typeof(*evdat), node);
32683b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
32693b5dd52aSJames Smart 		rx_databuf = evdat->data;
32703b5dd52aSJames Smart 		if (evdat->len != full_size) {
32713b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
32723b5dd52aSJames Smart 				"1603 Loopback test did not receive expected "
32733b5dd52aSJames Smart 				"data length. actual length 0x%x expected "
32743b5dd52aSJames Smart 				"length 0x%x\n",
32753b5dd52aSJames Smart 				evdat->len, full_size);
32763b5dd52aSJames Smart 			rc = -EIO;
32773b5dd52aSJames Smart 		} else if (rx_databuf == NULL)
32783b5dd52aSJames Smart 			rc = -EIO;
32793b5dd52aSJames Smart 		else {
32803b5dd52aSJames Smart 			rc = IOCB_SUCCESS;
32813b5dd52aSJames Smart 			/* skip over elx loopback header */
32823b5dd52aSJames Smart 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
328301e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
32843b5dd52aSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
32853b5dd52aSJames Smart 						    job->reply_payload.sg_cnt,
32863b5dd52aSJames Smart 						    rx_databuf, size);
328701e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len = size;
32883b5dd52aSJames Smart 		}
32893b5dd52aSJames Smart 	}
32903b5dd52aSJames Smart 
32913b5dd52aSJames Smart err_loopback_test_exit:
32923b5dd52aSJames Smart 	lpfcdiag_loop_self_unreg(phba, rpi);
32933b5dd52aSJames Smart 
32943b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
32953b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
32963b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
32973b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
32983b5dd52aSJames Smart 
32995a0916b4SJames Smart 	if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
33003b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
33013b5dd52aSJames Smart 
33023b5dd52aSJames Smart 	if (rspiocbq != NULL)
33033b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
33043b5dd52aSJames Smart 
33053b5dd52aSJames Smart 	if (txbmp != NULL) {
33063b5dd52aSJames Smart 		if (txbpl != NULL) {
33073b5dd52aSJames Smart 			if (txbuffer != NULL)
33083b5dd52aSJames Smart 				diag_cmd_data_free(phba, txbuffer);
33093b5dd52aSJames Smart 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
33103b5dd52aSJames Smart 		}
33113b5dd52aSJames Smart 		kfree(txbmp);
33123b5dd52aSJames Smart 	}
33133b5dd52aSJames Smart 
33143b5dd52aSJames Smart loopback_test_exit:
33153b5dd52aSJames Smart 	kfree(dataout);
33163b5dd52aSJames Smart 	/* make error code available to userspace */
331701e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
33183b5dd52aSJames Smart 	job->dd_data = NULL;
33193b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
33201b51197dSJames Smart 	if (rc == IOCB_SUCCESS)
332106548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
33221abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
33233b5dd52aSJames Smart 	return rc;
33243b5dd52aSJames Smart }
33253b5dd52aSJames Smart 
33263b5dd52aSJames Smart /**
33273b5dd52aSJames Smart  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
33283b5dd52aSJames Smart  * @job: GET_DFC_REV fc_bsg_job
33293b5dd52aSJames Smart  **/
33303b5dd52aSJames Smart static int
333175cc8cfcSJohannes Thumshirn lpfc_bsg_get_dfc_rev(struct bsg_job *job)
33323b5dd52aSJames Smart {
3333cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
333401e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
33353b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
33363b5dd52aSJames Smart 	struct get_mgmt_rev_reply *event_reply;
33373b5dd52aSJames Smart 	int rc = 0;
33383b5dd52aSJames Smart 
33393b5dd52aSJames Smart 	if (job->request_len <
33403b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
33413b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
33423b5dd52aSJames Smart 				"2740 Received GET_DFC_REV request below "
33433b5dd52aSJames Smart 				"minimum size\n");
33443b5dd52aSJames Smart 		rc = -EINVAL;
33453b5dd52aSJames Smart 		goto job_error;
33463b5dd52aSJames Smart 	}
33473b5dd52aSJames Smart 
33483b5dd52aSJames Smart 	event_reply = (struct get_mgmt_rev_reply *)
334901e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
33503b5dd52aSJames Smart 
33513b5dd52aSJames Smart 	if (job->reply_len <
33523b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
33533b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
33543b5dd52aSJames Smart 				"2741 Received GET_DFC_REV reply below "
33553b5dd52aSJames Smart 				"minimum size\n");
33563b5dd52aSJames Smart 		rc = -EINVAL;
33573b5dd52aSJames Smart 		goto job_error;
33583b5dd52aSJames Smart 	}
33593b5dd52aSJames Smart 
33603b5dd52aSJames Smart 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
33613b5dd52aSJames Smart 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
33623b5dd52aSJames Smart job_error:
336301e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
33643b5dd52aSJames Smart 	if (rc == 0)
336506548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
33661abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
33673b5dd52aSJames Smart 	return rc;
33683b5dd52aSJames Smart }
33693b5dd52aSJames Smart 
33703b5dd52aSJames Smart /**
33717ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
33723b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
33733b5dd52aSJames Smart  * @pmboxq: Pointer to mailbox command.
33743b5dd52aSJames Smart  *
33753b5dd52aSJames Smart  * This is completion handler function for mailbox commands issued from
33763b5dd52aSJames Smart  * lpfc_bsg_issue_mbox function. This function is called by the
33773b5dd52aSJames Smart  * mailbox event handler function with no lock held. This function
33783b5dd52aSJames Smart  * will wake up thread waiting on the wait queue pointed by context1
33793b5dd52aSJames Smart  * of the mailbox.
33803b5dd52aSJames Smart  **/
33819ab9b134SRashika Kheria static void
33827ad20aa9SJames Smart lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
33833b5dd52aSJames Smart {
33843b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
338501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
338675cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
33873b5dd52aSJames Smart 	uint32_t size;
33883b5dd52aSJames Smart 	unsigned long flags;
33897ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
33903b5dd52aSJames Smart 
33913b5dd52aSJames Smart 	dd_data = pmboxq->context1;
33923b5dd52aSJames Smart 
33937ad20aa9SJames Smart 	/*
33947ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
33957ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
33967a470277SJames Smart 	 */
33977ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
33987ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
33997ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3400515e0aa2SJames Smart 
3401a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3402a33c4f7bSJames Smart 
3403a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3404a33c4f7bSJames Smart 	job = dd_data->set_job;
3405a33c4f7bSJames Smart 	if (job) {
3406a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3407a33c4f7bSJames Smart 		job->dd_data = NULL;
3408a33c4f7bSJames Smart 	}
3409a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3410a33c4f7bSJames Smart 
3411a33c4f7bSJames Smart 	/* Copy the mailbox data to the job if it is still active */
3412a33c4f7bSJames Smart 
34135a6f133eSJames Smart 	if (job) {
341401e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
34157a470277SJames Smart 		size = job->reply_payload.payload_len;
341601e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
34173b5dd52aSJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
34183b5dd52aSJames Smart 					    job->reply_payload.sg_cnt,
34197ad20aa9SJames Smart 					    pmb_buf, size);
3420b6e3b9c6SJames Smart 	}
34217a470277SJames Smart 
3422a33c4f7bSJames Smart 	dd_data->set_job = NULL;
34233b5dd52aSJames Smart 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
34247ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
34253b5dd52aSJames Smart 	kfree(dd_data);
34267ad20aa9SJames Smart 
3427a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3428a33c4f7bSJames Smart 
34297ad20aa9SJames Smart 	if (job) {
343001e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
343106548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
34321abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
34337ad20aa9SJames Smart 	}
34343b5dd52aSJames Smart 	return;
34353b5dd52aSJames Smart }
34363b5dd52aSJames Smart 
34373b5dd52aSJames Smart /**
34383b5dd52aSJames Smart  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
34393b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
34403b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
34413b5dd52aSJames Smart  * @vport: Pointer to a vport object.
34423b5dd52aSJames Smart  *
34433b5dd52aSJames Smart  * Some commands require the port to be offline, some may not be called from
34443b5dd52aSJames Smart  * the application.
34453b5dd52aSJames Smart  **/
34463b5dd52aSJames Smart static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
34473b5dd52aSJames Smart 	MAILBOX_t *mb, struct lpfc_vport *vport)
34483b5dd52aSJames Smart {
34493b5dd52aSJames Smart 	/* return negative error values for bsg job */
34503b5dd52aSJames Smart 	switch (mb->mbxCommand) {
34513b5dd52aSJames Smart 	/* Offline only */
34523b5dd52aSJames Smart 	case MBX_INIT_LINK:
34533b5dd52aSJames Smart 	case MBX_DOWN_LINK:
34543b5dd52aSJames Smart 	case MBX_CONFIG_LINK:
34553b5dd52aSJames Smart 	case MBX_CONFIG_RING:
34563b5dd52aSJames Smart 	case MBX_RESET_RING:
34573b5dd52aSJames Smart 	case MBX_UNREG_LOGIN:
34583b5dd52aSJames Smart 	case MBX_CLEAR_LA:
34593b5dd52aSJames Smart 	case MBX_DUMP_CONTEXT:
34603b5dd52aSJames Smart 	case MBX_RUN_DIAGS:
34613b5dd52aSJames Smart 	case MBX_RESTART:
34623b5dd52aSJames Smart 	case MBX_SET_MASK:
34633b5dd52aSJames Smart 		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
34643b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
34653b5dd52aSJames Smart 				"2743 Command 0x%x is illegal in on-line "
34663b5dd52aSJames Smart 				"state\n",
34673b5dd52aSJames Smart 				mb->mbxCommand);
34683b5dd52aSJames Smart 			return -EPERM;
34693b5dd52aSJames Smart 		}
34703b5dd52aSJames Smart 	case MBX_WRITE_NV:
34713b5dd52aSJames Smart 	case MBX_WRITE_VPARMS:
34723b5dd52aSJames Smart 	case MBX_LOAD_SM:
34733b5dd52aSJames Smart 	case MBX_READ_NV:
34743b5dd52aSJames Smart 	case MBX_READ_CONFIG:
34753b5dd52aSJames Smart 	case MBX_READ_RCONFIG:
34763b5dd52aSJames Smart 	case MBX_READ_STATUS:
34773b5dd52aSJames Smart 	case MBX_READ_XRI:
34783b5dd52aSJames Smart 	case MBX_READ_REV:
34793b5dd52aSJames Smart 	case MBX_READ_LNK_STAT:
34803b5dd52aSJames Smart 	case MBX_DUMP_MEMORY:
34813b5dd52aSJames Smart 	case MBX_DOWN_LOAD:
34823b5dd52aSJames Smart 	case MBX_UPDATE_CFG:
34833b5dd52aSJames Smart 	case MBX_KILL_BOARD:
348406f35551SJames Smart 	case MBX_READ_TOPOLOGY:
34853b5dd52aSJames Smart 	case MBX_LOAD_AREA:
34863b5dd52aSJames Smart 	case MBX_LOAD_EXP_ROM:
34873b5dd52aSJames Smart 	case MBX_BEACON:
34883b5dd52aSJames Smart 	case MBX_DEL_LD_ENTRY:
34893b5dd52aSJames Smart 	case MBX_SET_DEBUG:
34903b5dd52aSJames Smart 	case MBX_WRITE_WWN:
34913b5dd52aSJames Smart 	case MBX_SLI4_CONFIG:
3492c7495937SJames Smart 	case MBX_READ_EVENT_LOG:
34933b5dd52aSJames Smart 	case MBX_READ_EVENT_LOG_STATUS:
34943b5dd52aSJames Smart 	case MBX_WRITE_EVENT_LOG:
34953b5dd52aSJames Smart 	case MBX_PORT_CAPABILITIES:
34963b5dd52aSJames Smart 	case MBX_PORT_IOV_CONTROL:
34977a470277SJames Smart 	case MBX_RUN_BIU_DIAG64:
34983b5dd52aSJames Smart 		break;
34993b5dd52aSJames Smart 	case MBX_SET_VARIABLE:
3500e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3501e2aed29fSJames Smart 			"1226 mbox: set_variable 0x%x, 0x%x\n",
3502e2aed29fSJames Smart 			mb->un.varWords[0],
3503e2aed29fSJames Smart 			mb->un.varWords[1]);
3504e2aed29fSJames Smart 		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3505e2aed29fSJames Smart 			&& (mb->un.varWords[1] == 1)) {
3506e2aed29fSJames Smart 			phba->wait_4_mlo_maint_flg = 1;
3507e2aed29fSJames Smart 		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
35081b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
3509e2aed29fSJames Smart 			phba->link_flag &= ~LS_LOOPBACK_MODE;
35101b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
351176a95d75SJames Smart 			phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3512e2aed29fSJames Smart 		}
3513e2aed29fSJames Smart 		break;
35143b5dd52aSJames Smart 	case MBX_READ_SPARM64:
35153b5dd52aSJames Smart 	case MBX_REG_LOGIN:
35163b5dd52aSJames Smart 	case MBX_REG_LOGIN64:
35173b5dd52aSJames Smart 	case MBX_CONFIG_PORT:
35183b5dd52aSJames Smart 	case MBX_RUN_BIU_DIAG:
35193b5dd52aSJames Smart 	default:
35203b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
35213b5dd52aSJames Smart 			"2742 Unknown Command 0x%x\n",
35223b5dd52aSJames Smart 			mb->mbxCommand);
35233b5dd52aSJames Smart 		return -EPERM;
35243b5dd52aSJames Smart 	}
35253b5dd52aSJames Smart 
35263b5dd52aSJames Smart 	return 0; /* ok */
35273b5dd52aSJames Smart }
35283b5dd52aSJames Smart 
35293b5dd52aSJames Smart /**
35307ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
35317ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
35327ad20aa9SJames Smart  *
35337ad20aa9SJames Smart  * This is routine clean up and reset BSG handling of multi-buffer mbox
35347ad20aa9SJames Smart  * command session.
35357ad20aa9SJames Smart  **/
35367ad20aa9SJames Smart static void
35377ad20aa9SJames Smart lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
35387ad20aa9SJames Smart {
35397ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
35407ad20aa9SJames Smart 		return;
35417ad20aa9SJames Smart 
35427ad20aa9SJames Smart 	/* free all memory, including dma buffers */
35437ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
35447ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
35457ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
35467ad20aa9SJames Smart 	/* multi-buffer write mailbox command pass-through complete */
35477ad20aa9SJames Smart 	memset((char *)&phba->mbox_ext_buf_ctx, 0,
35487ad20aa9SJames Smart 	       sizeof(struct lpfc_mbox_ext_buf_ctx));
35497ad20aa9SJames Smart 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
35507ad20aa9SJames Smart 
35517ad20aa9SJames Smart 	return;
35527ad20aa9SJames Smart }
35537ad20aa9SJames Smart 
35547ad20aa9SJames Smart /**
35557ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
35567ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
35577ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
35587ad20aa9SJames Smart  *
35597ad20aa9SJames Smart  * This is routine handles BSG job for mailbox commands completions with
35607ad20aa9SJames Smart  * multiple external buffers.
35617ad20aa9SJames Smart  **/
356275cc8cfcSJohannes Thumshirn static struct bsg_job *
35637ad20aa9SJames Smart lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
35647ad20aa9SJames Smart {
35657ad20aa9SJames Smart 	struct bsg_job_data *dd_data;
356675cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
356701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
35687ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
35697ad20aa9SJames Smart 	unsigned long flags;
35707ad20aa9SJames Smart 	uint32_t size;
35717ad20aa9SJames Smart 	int rc = 0;
3572026abb87SJames Smart 	struct lpfc_dmabuf *dmabuf;
3573026abb87SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3574026abb87SJames Smart 	uint8_t *pmbx;
35757ad20aa9SJames Smart 
35767ad20aa9SJames Smart 	dd_data = pmboxq->context1;
3577a33c4f7bSJames Smart 
3578a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3579a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3580a33c4f7bSJames Smart 	job = dd_data->set_job;
3581a33c4f7bSJames Smart 	if (job) {
358201e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
3583a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3584a33c4f7bSJames Smart 		job->dd_data = NULL;
35857ad20aa9SJames Smart 	}
3586a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
35877ad20aa9SJames Smart 
35887ad20aa9SJames Smart 	/*
35897ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
35907ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
35917ad20aa9SJames Smart 	 */
3592a33c4f7bSJames Smart 
35937ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
35947ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3595026abb87SJames Smart 	/* Copy the byte swapped response mailbox back to the user */
35967ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3597026abb87SJames Smart 	/* if there is any non-embedded extended data copy that too */
3598026abb87SJames Smart 	dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3599026abb87SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3600026abb87SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3601026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3602026abb87SJames Smart 		pmbx = (uint8_t *)dmabuf->virt;
3603026abb87SJames Smart 		/* byte swap the extended data following the mailbox command */
3604026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3605026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
3606026abb87SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3607026abb87SJames Smart 	}
36087ad20aa9SJames Smart 
3609a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3610a33c4f7bSJames Smart 
36117ad20aa9SJames Smart 	if (job) {
36127ad20aa9SJames Smart 		size = job->reply_payload.payload_len;
361301e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
36147ad20aa9SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
36157ad20aa9SJames Smart 					    job->reply_payload.sg_cnt,
36167ad20aa9SJames Smart 					    pmb_buf, size);
3617a33c4f7bSJames Smart 
36187ad20aa9SJames Smart 		/* result for successful */
361901e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
3620a33c4f7bSJames Smart 
36217ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36227afc0ce9SColin Ian King 				"2937 SLI_CONFIG ext-buffer mailbox command "
36237ad20aa9SJames Smart 				"(x%x/x%x) complete bsg job done, bsize:%d\n",
36247ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
36257ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, size);
3626b76f2dc9SJames Smart 		lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3627b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.nembType,
3628b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mboxType,
3629b76f2dc9SJames Smart 					dma_ebuf, sta_pos_addr,
3630b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3631a33c4f7bSJames Smart 	} else {
36327ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
36337afc0ce9SColin Ian King 				"2938 SLI_CONFIG ext-buffer mailbox "
36347ad20aa9SJames Smart 				"command (x%x/x%x) failure, rc:x%x\n",
36357ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
36367ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, rc);
3637a33c4f7bSJames Smart 	}
3638a33c4f7bSJames Smart 
3639a33c4f7bSJames Smart 
36407ad20aa9SJames Smart 	/* state change */
36417ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
36427ad20aa9SJames Smart 	kfree(dd_data);
36437ad20aa9SJames Smart 	return job;
36447ad20aa9SJames Smart }
36457ad20aa9SJames Smart 
36467ad20aa9SJames Smart /**
36477ad20aa9SJames Smart  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
36487ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
36497ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
36507ad20aa9SJames Smart  *
36517ad20aa9SJames Smart  * This is completion handler function for mailbox read commands with multiple
36527ad20aa9SJames Smart  * external buffers.
36537ad20aa9SJames Smart  **/
36547ad20aa9SJames Smart static void
36557ad20aa9SJames Smart lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
36567ad20aa9SJames Smart {
365775cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
36581abaede7SJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
36597ad20aa9SJames Smart 
3660a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3661a33c4f7bSJames Smart 
36627ad20aa9SJames Smart 	/* handle the BSG job with mailbox command */
3663a33c4f7bSJames Smart 	if (!job)
36647ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
36657ad20aa9SJames Smart 
36667ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36677afc0ce9SColin Ian King 			"2939 SLI_CONFIG ext-buffer rd mailbox command "
36687ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
36697ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
36707ad20aa9SJames Smart 
36717ad20aa9SJames Smart 	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
36727ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
36737ad20aa9SJames Smart 
36747ad20aa9SJames Smart 	/* free base driver mailbox structure memory */
36757ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
36767ad20aa9SJames Smart 
3677a33c4f7bSJames Smart 	/* if the job is still active, call job done */
36781abaede7SJohannes Thumshirn 	if (job) {
36791abaede7SJohannes Thumshirn 		bsg_reply = job->reply;
368006548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
36811abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
36821abaede7SJohannes Thumshirn 	}
36837ad20aa9SJames Smart 	return;
36847ad20aa9SJames Smart }
36857ad20aa9SJames Smart 
36867ad20aa9SJames Smart /**
36877ad20aa9SJames Smart  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
36887ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
36897ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
36907ad20aa9SJames Smart  *
36917ad20aa9SJames Smart  * This is completion handler function for mailbox write commands with multiple
36927ad20aa9SJames Smart  * external buffers.
36937ad20aa9SJames Smart  **/
36947ad20aa9SJames Smart static void
36957ad20aa9SJames Smart lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
36967ad20aa9SJames Smart {
369775cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
36981abaede7SJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
36997ad20aa9SJames Smart 
3700a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3701a33c4f7bSJames Smart 
37027ad20aa9SJames Smart 	/* handle the BSG job with the mailbox command */
3703a33c4f7bSJames Smart 	if (!job)
37047ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
37057ad20aa9SJames Smart 
37067ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37077afc0ce9SColin Ian King 			"2940 SLI_CONFIG ext-buffer wr mailbox command "
37087ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
37097ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
37107ad20aa9SJames Smart 
37117ad20aa9SJames Smart 	/* free all memory, including dma buffers */
37127ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
37137ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
37147ad20aa9SJames Smart 
3715a33c4f7bSJames Smart 	/* if the job is still active, call job done */
37161abaede7SJohannes Thumshirn 	if (job) {
37171abaede7SJohannes Thumshirn 		bsg_reply = job->reply;
371806548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
37191abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
37201abaede7SJohannes Thumshirn 	}
37217ad20aa9SJames Smart 
37227ad20aa9SJames Smart 	return;
37237ad20aa9SJames Smart }
37247ad20aa9SJames Smart 
37257ad20aa9SJames Smart static void
37267ad20aa9SJames Smart lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
37277ad20aa9SJames Smart 				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
37287ad20aa9SJames Smart 				struct lpfc_dmabuf *ext_dmabuf)
37297ad20aa9SJames Smart {
37307ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
37317ad20aa9SJames Smart 
37327ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
37337ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
37347ad20aa9SJames Smart 
37357ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
37367ad20aa9SJames Smart 		if (index == 0) {
37377ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
37387ad20aa9SJames Smart 				mse[index].pa_hi =
37397ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
37407ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
37417ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
37427ad20aa9SJames Smart 				mse[index].pa_lo =
37437ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
37447ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
37457ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37467ad20aa9SJames Smart 					"2943 SLI_CONFIG(mse)[%d], "
37477ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
37487ad20aa9SJames Smart 					index,
37497ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37507ad20aa9SJames Smart 					mse[index].buf_len,
37517ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37527ad20aa9SJames Smart 					mse[index].pa_hi,
37537ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37547ad20aa9SJames Smart 					mse[index].pa_lo);
37557ad20aa9SJames Smart 		} else {
37567ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
37577ad20aa9SJames Smart 				mse[index].pa_hi =
37587ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
37597ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
37607ad20aa9SJames Smart 				mse[index].pa_lo =
37617ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
37627ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37637ad20aa9SJames Smart 					"2944 SLI_CONFIG(mse)[%d], "
37647ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
37657ad20aa9SJames Smart 					index,
37667ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37677ad20aa9SJames Smart 					mse[index].buf_len,
37687ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37697ad20aa9SJames Smart 					mse[index].pa_hi,
37707ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
37717ad20aa9SJames Smart 					mse[index].pa_lo);
37727ad20aa9SJames Smart 		}
37737ad20aa9SJames Smart 	} else {
37747ad20aa9SJames Smart 		if (index == 0) {
37757ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
37767ad20aa9SJames Smart 				hbd[index].pa_hi =
37777ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
37787ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
37797ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
37807ad20aa9SJames Smart 				hbd[index].pa_lo =
37817ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
37827ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
37837ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37847ad20aa9SJames Smart 					"3007 SLI_CONFIG(hbd)[%d], "
37857ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
37867ad20aa9SJames Smart 				index,
37877ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
37887ad20aa9SJames Smart 				&sli_cfg_mbx->un.
37897ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
37907ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
37917ad20aa9SJames Smart 				hbd[index].pa_hi,
37927ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
37937ad20aa9SJames Smart 				hbd[index].pa_lo);
37947ad20aa9SJames Smart 
37957ad20aa9SJames Smart 		} else {
37967ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
37977ad20aa9SJames Smart 				hbd[index].pa_hi =
37987ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
37997ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
38007ad20aa9SJames Smart 				hbd[index].pa_lo =
38017ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
38027ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38037ad20aa9SJames Smart 					"3008 SLI_CONFIG(hbd)[%d], "
38047ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
38057ad20aa9SJames Smart 				index,
38067ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
38077ad20aa9SJames Smart 				&sli_cfg_mbx->un.
38087ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
38097ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38107ad20aa9SJames Smart 				hbd[index].pa_hi,
38117ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
38127ad20aa9SJames Smart 				hbd[index].pa_lo);
38137ad20aa9SJames Smart 		}
38147ad20aa9SJames Smart 	}
38157ad20aa9SJames Smart 	return;
38167ad20aa9SJames Smart }
38177ad20aa9SJames Smart 
38187ad20aa9SJames Smart /**
38197ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
38207ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
38217ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
38227ad20aa9SJames Smart  * @nemb_tp: Enumerate of non-embedded mailbox command type.
38237ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
38247ad20aa9SJames Smart  *
38257ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
38267ad20aa9SJames Smart  * non-embedded external bufffers.
38277ad20aa9SJames Smart  **/
38287ad20aa9SJames Smart static int
382975cc8cfcSJohannes Thumshirn lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
38307ad20aa9SJames Smart 			      enum nemb_type nemb_tp,
38317ad20aa9SJames Smart 			      struct lpfc_dmabuf *dmabuf)
38327ad20aa9SJames Smart {
383301e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
38347ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
38357ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
38367ad20aa9SJames Smart 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
38377ad20aa9SJames Smart 	uint32_t ext_buf_cnt, ext_buf_index;
38387ad20aa9SJames Smart 	struct lpfc_dmabuf *ext_dmabuf = NULL;
38397ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
38407ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
38417ad20aa9SJames Smart 	MAILBOX_t *pmb;
38427ad20aa9SJames Smart 	uint8_t *pmbx;
38437ad20aa9SJames Smart 	int rc, i;
38447ad20aa9SJames Smart 
38457ad20aa9SJames Smart 	mbox_req =
384601e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
38477ad20aa9SJames Smart 
38487ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
38497ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
38507ad20aa9SJames Smart 
38517ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
38527ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
38537ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
38547ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
38557ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
38567ad20aa9SJames Smart 					"2945 Handled SLI_CONFIG(mse) rd, "
38577ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
38587ad20aa9SJames Smart 					ext_buf_cnt,
38597ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
38607ad20aa9SJames Smart 			rc = -ERANGE;
38617ad20aa9SJames Smart 			goto job_error;
38627ad20aa9SJames Smart 		}
38637ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38647ad20aa9SJames Smart 				"2941 Handled SLI_CONFIG(mse) rd, "
38657ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
38667ad20aa9SJames Smart 	} else {
38677ad20aa9SJames Smart 		/* sanity check on interface type for support */
386827d6ac0aSJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
38697ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2) {
38707ad20aa9SJames Smart 			rc = -ENODEV;
38717ad20aa9SJames Smart 			goto job_error;
38727ad20aa9SJames Smart 		}
38737ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
38747ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
38757ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
38767ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
38777ad20aa9SJames Smart 					"2946 Handled SLI_CONFIG(hbd) rd, "
38787ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
38797ad20aa9SJames Smart 					ext_buf_cnt,
38807ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
38817ad20aa9SJames Smart 			rc = -ERANGE;
38827ad20aa9SJames Smart 			goto job_error;
38837ad20aa9SJames Smart 		}
38847ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38857ad20aa9SJames Smart 				"2942 Handled SLI_CONFIG(hbd) rd, "
38867ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
38877ad20aa9SJames Smart 	}
38887ad20aa9SJames Smart 
3889b76f2dc9SJames Smart 	/* before dma descriptor setup */
3890b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3891b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
3892b76f2dc9SJames Smart 
38937ad20aa9SJames Smart 	/* reject non-embedded mailbox command with none external buffer */
38947ad20aa9SJames Smart 	if (ext_buf_cnt == 0) {
38957ad20aa9SJames Smart 		rc = -EPERM;
38967ad20aa9SJames Smart 		goto job_error;
38977ad20aa9SJames Smart 	} else if (ext_buf_cnt > 1) {
38987ad20aa9SJames Smart 		/* additional external read buffers */
38997ad20aa9SJames Smart 		for (i = 1; i < ext_buf_cnt; i++) {
39007ad20aa9SJames Smart 			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
39017ad20aa9SJames Smart 			if (!ext_dmabuf) {
39027ad20aa9SJames Smart 				rc = -ENOMEM;
39037ad20aa9SJames Smart 				goto job_error;
39047ad20aa9SJames Smart 			}
39057ad20aa9SJames Smart 			list_add_tail(&ext_dmabuf->list,
39067ad20aa9SJames Smart 				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
39077ad20aa9SJames Smart 		}
39087ad20aa9SJames Smart 	}
39097ad20aa9SJames Smart 
39107ad20aa9SJames Smart 	/* bsg tracking structure */
39117ad20aa9SJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
39127ad20aa9SJames Smart 	if (!dd_data) {
39137ad20aa9SJames Smart 		rc = -ENOMEM;
39147ad20aa9SJames Smart 		goto job_error;
39157ad20aa9SJames Smart 	}
39167ad20aa9SJames Smart 
39177ad20aa9SJames Smart 	/* mailbox command structure for base driver */
39187ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
39197ad20aa9SJames Smart 	if (!pmboxq) {
39207ad20aa9SJames Smart 		rc = -ENOMEM;
39217ad20aa9SJames Smart 		goto job_error;
39227ad20aa9SJames Smart 	}
39237ad20aa9SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
39247ad20aa9SJames Smart 
39257ad20aa9SJames Smart 	/* for the first external buffer */
39267ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
39277ad20aa9SJames Smart 
39287ad20aa9SJames Smart 	/* for the rest of external buffer descriptors if any */
39297ad20aa9SJames Smart 	if (ext_buf_cnt > 1) {
39307ad20aa9SJames Smart 		ext_buf_index = 1;
39317ad20aa9SJames Smart 		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
39327ad20aa9SJames Smart 				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
39337ad20aa9SJames Smart 			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
39347ad20aa9SJames Smart 						ext_buf_index, dmabuf,
39357ad20aa9SJames Smart 						curr_dmabuf);
39367ad20aa9SJames Smart 			ext_buf_index++;
39377ad20aa9SJames Smart 		}
39387ad20aa9SJames Smart 	}
39397ad20aa9SJames Smart 
3940b76f2dc9SJames Smart 	/* after dma descriptor setup */
3941b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3942b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
3943b76f2dc9SJames Smart 
39447ad20aa9SJames Smart 	/* construct base driver mbox command */
39457ad20aa9SJames Smart 	pmb = &pmboxq->u.mb;
39467ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
39477ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
39487ad20aa9SJames Smart 	pmb->mbxOwner = OWN_HOST;
39497ad20aa9SJames Smart 	pmboxq->vport = phba->pport;
39507ad20aa9SJames Smart 
39517ad20aa9SJames Smart 	/* multi-buffer handling context */
39527ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
39537ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
39547ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
39557ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
39567ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
39577ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
39587ad20aa9SJames Smart 
39597ad20aa9SJames Smart 	/* callback for multi-buffer read mailbox command */
39607ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
39617ad20aa9SJames Smart 
39627ad20aa9SJames Smart 	/* context fields to callback function */
39637ad20aa9SJames Smart 	pmboxq->context1 = dd_data;
39647ad20aa9SJames Smart 	dd_data->type = TYPE_MBOX;
3965a33c4f7bSJames Smart 	dd_data->set_job = job;
39667ad20aa9SJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
39677ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
39687ad20aa9SJames Smart 	job->dd_data = dd_data;
39697ad20aa9SJames Smart 
39707ad20aa9SJames Smart 	/* state change */
39717ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
39727ad20aa9SJames Smart 
3973026abb87SJames Smart 	/*
3974026abb87SJames Smart 	 * Non-embedded mailbox subcommand data gets byte swapped here because
3975026abb87SJames Smart 	 * the lower level driver code only does the first 64 mailbox words.
3976026abb87SJames Smart 	 */
3977026abb87SJames Smart 	if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3978026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3979026abb87SJames Smart 		(nemb_tp == nemb_mse))
3980026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3981026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
3982026abb87SJames Smart 				sli_cfg_mbx->un.sli_config_emb0_subsys.
3983026abb87SJames Smart 					mse[0].buf_len);
3984026abb87SJames Smart 
39857ad20aa9SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
39867ad20aa9SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
39877ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39887ad20aa9SJames Smart 				"2947 Issued SLI_CONFIG ext-buffer "
39897afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
399088a2cfbbSJames Smart 		return SLI_CONFIG_HANDLED;
39917ad20aa9SJames Smart 	}
39927ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
39937ad20aa9SJames Smart 			"2948 Failed to issue SLI_CONFIG ext-buffer "
39947afc0ce9SColin Ian King 			"mailbox command, rc:x%x\n", rc);
39957ad20aa9SJames Smart 	rc = -EPIPE;
39967ad20aa9SJames Smart 
39977ad20aa9SJames Smart job_error:
39987ad20aa9SJames Smart 	if (pmboxq)
39997ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
40007ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
40017ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
40027ad20aa9SJames Smart 	kfree(dd_data);
40037ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
40047ad20aa9SJames Smart 	return rc;
40057ad20aa9SJames Smart }
40067ad20aa9SJames Smart 
40077ad20aa9SJames Smart /**
40087ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
40097ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
40107ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
40117ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
40127ad20aa9SJames Smart  *
40137ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
40147ad20aa9SJames Smart  * non-embedded external bufffers.
40157ad20aa9SJames Smart  **/
40167ad20aa9SJames Smart static int
401775cc8cfcSJohannes Thumshirn lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
40187ad20aa9SJames Smart 			       enum nemb_type nemb_tp,
40197ad20aa9SJames Smart 			       struct lpfc_dmabuf *dmabuf)
40207ad20aa9SJames Smart {
402101e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
402201e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
40237ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
40247ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
40257ad20aa9SJames Smart 	uint32_t ext_buf_cnt;
40267ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
40277ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
40287ad20aa9SJames Smart 	MAILBOX_t *pmb;
40297ad20aa9SJames Smart 	uint8_t *mbx;
403088a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED, i;
40317ad20aa9SJames Smart 
40327ad20aa9SJames Smart 	mbox_req =
403301e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
40347ad20aa9SJames Smart 
40357ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
40367ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
40377ad20aa9SJames Smart 
40387ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
40397ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
40407ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
40417ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
40427ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4043026abb87SJames Smart 					"2953 Failed SLI_CONFIG(mse) wr, "
40447ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
40457ad20aa9SJames Smart 					ext_buf_cnt,
40467ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
40477ad20aa9SJames Smart 			return -ERANGE;
40487ad20aa9SJames Smart 		}
40497ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40507ad20aa9SJames Smart 				"2949 Handled SLI_CONFIG(mse) wr, "
40517ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
40527ad20aa9SJames Smart 	} else {
40537ad20aa9SJames Smart 		/* sanity check on interface type for support */
405427d6ac0aSJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
40557ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2)
40567ad20aa9SJames Smart 			return -ENODEV;
40577ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
40587ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
40597ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
40607ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4061026abb87SJames Smart 					"2954 Failed SLI_CONFIG(hbd) wr, "
40627ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
40637ad20aa9SJames Smart 					ext_buf_cnt,
40647ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
40657ad20aa9SJames Smart 			return -ERANGE;
40667ad20aa9SJames Smart 		}
40677ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40687ad20aa9SJames Smart 				"2950 Handled SLI_CONFIG(hbd) wr, "
40697ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
40707ad20aa9SJames Smart 	}
40717ad20aa9SJames Smart 
4072b76f2dc9SJames Smart 	/* before dma buffer descriptor setup */
4073b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4074b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
4075b76f2dc9SJames Smart 
40767ad20aa9SJames Smart 	if (ext_buf_cnt == 0)
40777ad20aa9SJames Smart 		return -EPERM;
40787ad20aa9SJames Smart 
40797ad20aa9SJames Smart 	/* for the first external buffer */
40807ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
40817ad20aa9SJames Smart 
4082b76f2dc9SJames Smart 	/* after dma descriptor setup */
4083b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4084b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
4085b76f2dc9SJames Smart 
40867ad20aa9SJames Smart 	/* log for looking forward */
40877ad20aa9SJames Smart 	for (i = 1; i < ext_buf_cnt; i++) {
40887ad20aa9SJames Smart 		if (nemb_tp == nemb_mse)
40897ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40907ad20aa9SJames Smart 				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
40917ad20aa9SJames Smart 				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
40927ad20aa9SJames Smart 				mse[i].buf_len);
40937ad20aa9SJames Smart 		else
40947ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40957ad20aa9SJames Smart 				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
40967ad20aa9SJames Smart 				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
40977ad20aa9SJames Smart 				&sli_cfg_mbx->un.sli_config_emb1_subsys.
40987ad20aa9SJames Smart 				hbd[i]));
40997ad20aa9SJames Smart 	}
41007ad20aa9SJames Smart 
41017ad20aa9SJames Smart 	/* multi-buffer handling context */
41027ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
41037ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
41047ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
41057ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
41067ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
41077ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
41087ad20aa9SJames Smart 
41097ad20aa9SJames Smart 	if (ext_buf_cnt == 1) {
41107ad20aa9SJames Smart 		/* bsg tracking structure */
41117ad20aa9SJames Smart 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
41127ad20aa9SJames Smart 		if (!dd_data) {
41137ad20aa9SJames Smart 			rc = -ENOMEM;
41147ad20aa9SJames Smart 			goto job_error;
41157ad20aa9SJames Smart 		}
41167ad20aa9SJames Smart 
41177ad20aa9SJames Smart 		/* mailbox command structure for base driver */
41187ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
41197ad20aa9SJames Smart 		if (!pmboxq) {
41207ad20aa9SJames Smart 			rc = -ENOMEM;
41217ad20aa9SJames Smart 			goto job_error;
41227ad20aa9SJames Smart 		}
41237ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
41247ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
41257ad20aa9SJames Smart 		mbx = (uint8_t *)dmabuf->virt;
41267ad20aa9SJames Smart 		memcpy(pmb, mbx, sizeof(*pmb));
41277ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
41287ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
41297ad20aa9SJames Smart 
41307ad20aa9SJames Smart 		/* callback for multi-buffer read mailbox command */
41317ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
41327ad20aa9SJames Smart 
41337ad20aa9SJames Smart 		/* context fields to callback function */
41347ad20aa9SJames Smart 		pmboxq->context1 = dd_data;
41357ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4136a33c4f7bSJames Smart 		dd_data->set_job = job;
41377ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
41387ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
41397ad20aa9SJames Smart 		job->dd_data = dd_data;
41407ad20aa9SJames Smart 
41417ad20aa9SJames Smart 		/* state change */
41427ad20aa9SJames Smart 
4143a33c4f7bSJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
41447ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
41457ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
41467ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41477ad20aa9SJames Smart 					"2955 Issued SLI_CONFIG ext-buffer "
41487afc0ce9SColin Ian King 					"mailbox command, rc:x%x\n", rc);
414988a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
41507ad20aa9SJames Smart 		}
41517ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
41527ad20aa9SJames Smart 				"2956 Failed to issue SLI_CONFIG ext-buffer "
41537afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
41547ad20aa9SJames Smart 		rc = -EPIPE;
4155026abb87SJames Smart 		goto job_error;
41567ad20aa9SJames Smart 	}
41577ad20aa9SJames Smart 
415888a2cfbbSJames Smart 	/* wait for additoinal external buffers */
4159a33c4f7bSJames Smart 
416001e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
416106548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
41621abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
416388a2cfbbSJames Smart 	return SLI_CONFIG_HANDLED;
416488a2cfbbSJames Smart 
41657ad20aa9SJames Smart job_error:
41667ad20aa9SJames Smart 	if (pmboxq)
41677ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
41687ad20aa9SJames Smart 	kfree(dd_data);
41697ad20aa9SJames Smart 
41707ad20aa9SJames Smart 	return rc;
41717ad20aa9SJames Smart }
41727ad20aa9SJames Smart 
41737ad20aa9SJames Smart /**
41747ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
41757ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
41767ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
41777ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
41787ad20aa9SJames Smart  *
41797ad20aa9SJames Smart  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
41807ad20aa9SJames Smart  * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
41817ad20aa9SJames Smart  * with embedded sussystem 0x1 and opcodes with external HBDs.
41827ad20aa9SJames Smart  **/
41837ad20aa9SJames Smart static int
418475cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
41857ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
41867ad20aa9SJames Smart {
41877ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
41887ad20aa9SJames Smart 	uint32_t subsys;
41897ad20aa9SJames Smart 	uint32_t opcode;
41907ad20aa9SJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
41917ad20aa9SJames Smart 
4192026abb87SJames Smart 	/* state change on new multi-buffer pass-through mailbox command */
41937ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
41947ad20aa9SJames Smart 
41957ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
41967ad20aa9SJames Smart 
41977ad20aa9SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
41987ad20aa9SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
41997ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
42007ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
42017ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
42027ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
42037ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
42047ad20aa9SJames Smart 			switch (opcode) {
42057ad20aa9SJames Smart 			case FCOE_OPCODE_READ_FCF:
42062a2719d3SJames Smart 			case FCOE_OPCODE_GET_DPORT_RESULTS:
42077ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42087ad20aa9SJames Smart 						"2957 Handled SLI_CONFIG "
42097ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42107ad20aa9SJames Smart 						opcode);
42117ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
42127ad20aa9SJames Smart 							nemb_mse, dmabuf);
42137ad20aa9SJames Smart 				break;
42147ad20aa9SJames Smart 			case FCOE_OPCODE_ADD_FCF:
42152a2719d3SJames Smart 			case FCOE_OPCODE_SET_DPORT_MODE:
42162a2719d3SJames Smart 			case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
42177ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42187ad20aa9SJames Smart 						"2958 Handled SLI_CONFIG "
42197ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42207ad20aa9SJames Smart 						opcode);
42217ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
42227ad20aa9SJames Smart 							nemb_mse, dmabuf);
42237ad20aa9SJames Smart 				break;
42247ad20aa9SJames Smart 			default:
42257ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4226026abb87SJames Smart 						"2959 Reject SLI_CONFIG "
42277ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
42287ad20aa9SJames Smart 						opcode);
4229026abb87SJames Smart 				rc = -EPERM;
4230026abb87SJames Smart 				break;
4231026abb87SJames Smart 			}
4232026abb87SJames Smart 		} else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4233026abb87SJames Smart 			switch (opcode) {
4234026abb87SJames Smart 			case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4235b99570ddSJames Smart 			case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4236c6377970SJames Smart 			case COMN_OPCODE_GET_PROFILE_CONFIG:
4237026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4238026abb87SJames Smart 						"3106 Handled SLI_CONFIG "
42396b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4240026abb87SJames Smart 						opcode);
4241026abb87SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4242026abb87SJames Smart 							nemb_mse, dmabuf);
4243026abb87SJames Smart 				break;
4244026abb87SJames Smart 			default:
4245026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4246026abb87SJames Smart 						"3107 Reject SLI_CONFIG "
42476b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4248026abb87SJames Smart 						opcode);
4249026abb87SJames Smart 				rc = -EPERM;
42507ad20aa9SJames Smart 				break;
42517ad20aa9SJames Smart 			}
42527ad20aa9SJames Smart 		} else {
42537ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4254026abb87SJames Smart 					"2977 Reject SLI_CONFIG "
42557ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
42567ad20aa9SJames Smart 					subsys, opcode);
4257026abb87SJames Smart 			rc = -EPERM;
42587ad20aa9SJames Smart 		}
42597ad20aa9SJames Smart 	} else {
42607ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
42617ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
42627ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
42637ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
42647ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
42657ad20aa9SJames Smart 			switch (opcode) {
42667ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT:
42677ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT_LIST:
42687ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42697ad20aa9SJames Smart 						"2960 Handled SLI_CONFIG "
42707ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
42717ad20aa9SJames Smart 						opcode);
42727ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
42737ad20aa9SJames Smart 							nemb_hbd, dmabuf);
42747ad20aa9SJames Smart 				break;
42757ad20aa9SJames Smart 			case COMN_OPCODE_WRITE_OBJECT:
42767ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42777ad20aa9SJames Smart 						"2961 Handled SLI_CONFIG "
42787ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
42797ad20aa9SJames Smart 						opcode);
42807ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
42817ad20aa9SJames Smart 							nemb_hbd, dmabuf);
42827ad20aa9SJames Smart 				break;
42837ad20aa9SJames Smart 			default:
42847ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42857ad20aa9SJames Smart 						"2962 Not handled SLI_CONFIG "
42867ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
42877ad20aa9SJames Smart 						opcode);
42887ad20aa9SJames Smart 				rc = SLI_CONFIG_NOT_HANDLED;
42897ad20aa9SJames Smart 				break;
42907ad20aa9SJames Smart 			}
42917ad20aa9SJames Smart 		} else {
42927ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4293026abb87SJames Smart 					"2978 Not handled SLI_CONFIG "
42947ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
42957ad20aa9SJames Smart 					subsys, opcode);
42967ad20aa9SJames Smart 			rc = SLI_CONFIG_NOT_HANDLED;
42977ad20aa9SJames Smart 		}
42987ad20aa9SJames Smart 	}
4299026abb87SJames Smart 
4300026abb87SJames Smart 	/* state reset on not handled new multi-buffer mailbox command */
4301026abb87SJames Smart 	if (rc != SLI_CONFIG_HANDLED)
4302026abb87SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4303026abb87SJames Smart 
43047ad20aa9SJames Smart 	return rc;
43057ad20aa9SJames Smart }
43067ad20aa9SJames Smart 
43077ad20aa9SJames Smart /**
43087ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
43097ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
43107ad20aa9SJames Smart  *
43117ad20aa9SJames Smart  * This routine is for requesting to abort a pass-through mailbox command with
43127ad20aa9SJames Smart  * multiple external buffers due to error condition.
43137ad20aa9SJames Smart  **/
43147ad20aa9SJames Smart static void
43157ad20aa9SJames Smart lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
43167ad20aa9SJames Smart {
43177ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
43187ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
43197ad20aa9SJames Smart 	else
43207ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
43217ad20aa9SJames Smart 	return;
43227ad20aa9SJames Smart }
43237ad20aa9SJames Smart 
43247ad20aa9SJames Smart /**
43257ad20aa9SJames Smart  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
43267ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
43277ad20aa9SJames Smart  * @dmabuf: Pointer to a DMA buffer descriptor.
43287ad20aa9SJames Smart  *
43297ad20aa9SJames Smart  * This routine extracts the next mailbox read external buffer back to
43307ad20aa9SJames Smart  * user space through BSG.
43317ad20aa9SJames Smart  **/
43327ad20aa9SJames Smart static int
433375cc8cfcSJohannes Thumshirn lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
43347ad20aa9SJames Smart {
433501e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
43367ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
43377ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
43387ad20aa9SJames Smart 	uint8_t *pbuf;
43397ad20aa9SJames Smart 	uint32_t size;
43407ad20aa9SJames Smart 	uint32_t index;
43417ad20aa9SJames Smart 
43427ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
43437ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
43447ad20aa9SJames Smart 
43457ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
43467ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
43477ad20aa9SJames Smart 
43487ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
43497ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
43507ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
43517ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43527ad20aa9SJames Smart 				"2963 SLI_CONFIG (mse) ext-buffer rd get "
43537ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
43547ad20aa9SJames Smart 	} else {
43557ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
43567ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
43577ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43587ad20aa9SJames Smart 				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
43597ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
43607ad20aa9SJames Smart 	}
43617ad20aa9SJames Smart 	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
43627ad20aa9SJames Smart 		return -EPIPE;
43637ad20aa9SJames Smart 	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
43647ad20aa9SJames Smart 				  struct lpfc_dmabuf, list);
43657ad20aa9SJames Smart 	list_del_init(&dmabuf->list);
4366b76f2dc9SJames Smart 
4367b76f2dc9SJames Smart 	/* after dma buffer descriptor setup */
4368b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4369b76f2dc9SJames Smart 					mbox_rd, dma_ebuf, sta_pos_addr,
4370b76f2dc9SJames Smart 					dmabuf, index);
4371b76f2dc9SJames Smart 
43727ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
437301e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len =
43747ad20aa9SJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
43757ad20aa9SJames Smart 				    job->reply_payload.sg_cnt,
43767ad20aa9SJames Smart 				    pbuf, size);
43777ad20aa9SJames Smart 
43787ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
43797ad20aa9SJames Smart 
43807ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
43817ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43827ad20aa9SJames Smart 				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
43837ad20aa9SJames Smart 				"command session done\n");
43847ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
43857ad20aa9SJames Smart 	}
43867ad20aa9SJames Smart 
438701e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
438806548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
43891abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
43907ad20aa9SJames Smart 
43917ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
43927ad20aa9SJames Smart }
43937ad20aa9SJames Smart 
43947ad20aa9SJames Smart /**
43957ad20aa9SJames Smart  * lpfc_bsg_write_ebuf_set - set the next mailbox write 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 sets up the next mailbox read external buffer obtained
44007ad20aa9SJames Smart  * from user space through BSG.
44017ad20aa9SJames Smart  **/
44027ad20aa9SJames Smart static int
440375cc8cfcSJohannes Thumshirn lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
44047ad20aa9SJames Smart 			struct lpfc_dmabuf *dmabuf)
44057ad20aa9SJames Smart {
440601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
44077ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
44087ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
44097ad20aa9SJames Smart 	MAILBOX_t *pmb;
44107ad20aa9SJames Smart 	enum nemb_type nemb_tp;
44117ad20aa9SJames Smart 	uint8_t *pbuf;
44127ad20aa9SJames Smart 	uint32_t size;
44137ad20aa9SJames Smart 	uint32_t index;
44147ad20aa9SJames Smart 	int rc;
44157ad20aa9SJames Smart 
44167ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
44177ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
44187ad20aa9SJames Smart 	nemb_tp = phba->mbox_ext_buf_ctx.nembType;
44197ad20aa9SJames Smart 
44207ad20aa9SJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
44217ad20aa9SJames Smart 	if (!dd_data) {
44227ad20aa9SJames Smart 		rc = -ENOMEM;
44237ad20aa9SJames Smart 		goto job_error;
44247ad20aa9SJames Smart 	}
44257ad20aa9SJames Smart 
44267ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
44277ad20aa9SJames Smart 	size = job->request_payload.payload_len;
44287ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
44297ad20aa9SJames Smart 			  job->request_payload.sg_cnt,
44307ad20aa9SJames Smart 			  pbuf, size);
44317ad20aa9SJames Smart 
44327ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
44337ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44347ad20aa9SJames Smart 				"2966 SLI_CONFIG (mse) ext-buffer wr set "
44357ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
44367ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
44377ad20aa9SJames Smart 
44387ad20aa9SJames Smart 	} else {
44397ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44407ad20aa9SJames Smart 				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
44417ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
44427ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
44437ad20aa9SJames Smart 
44447ad20aa9SJames Smart 	}
44457ad20aa9SJames Smart 
44467ad20aa9SJames Smart 	/* set up external buffer descriptor and add to external buffer list */
44477ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
44487ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf,
44497ad20aa9SJames Smart 					dmabuf);
44507ad20aa9SJames Smart 	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
44517ad20aa9SJames Smart 
4452b76f2dc9SJames Smart 	/* after write dma buffer */
4453b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4454b76f2dc9SJames Smart 					mbox_wr, dma_ebuf, sta_pos_addr,
4455b76f2dc9SJames Smart 					dmabuf, index);
4456b76f2dc9SJames Smart 
44577ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
44587ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44597ad20aa9SJames Smart 				"2968 SLI_CONFIG ext-buffer wr all %d "
44607ad20aa9SJames Smart 				"ebuffers received\n",
44617ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.numBuf);
44627ad20aa9SJames Smart 		/* mailbox command structure for base driver */
44637ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
44647ad20aa9SJames Smart 		if (!pmboxq) {
44657ad20aa9SJames Smart 			rc = -ENOMEM;
44667ad20aa9SJames Smart 			goto job_error;
44677ad20aa9SJames Smart 		}
44687ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
44697ad20aa9SJames Smart 		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
44707ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
44717ad20aa9SJames Smart 		memcpy(pmb, pbuf, sizeof(*pmb));
44727ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
44737ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
44747ad20aa9SJames Smart 
44757ad20aa9SJames Smart 		/* callback for multi-buffer write mailbox command */
44767ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
44777ad20aa9SJames Smart 
44787ad20aa9SJames Smart 		/* context fields to callback function */
44797ad20aa9SJames Smart 		pmboxq->context1 = dd_data;
44807ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4481a33c4f7bSJames Smart 		dd_data->set_job = job;
44827ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
44837ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
44847ad20aa9SJames Smart 		job->dd_data = dd_data;
44857ad20aa9SJames Smart 
44867ad20aa9SJames Smart 		/* state change */
44877ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
44887ad20aa9SJames Smart 
44897ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
44907ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
44917ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44927ad20aa9SJames Smart 					"2969 Issued SLI_CONFIG ext-buffer "
44937afc0ce9SColin Ian King 					"mailbox command, rc:x%x\n", rc);
449488a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
44957ad20aa9SJames Smart 		}
44967ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
44977ad20aa9SJames Smart 				"2970 Failed to issue SLI_CONFIG ext-buffer "
44987afc0ce9SColin Ian King 				"mailbox command, rc:x%x\n", rc);
44997ad20aa9SJames Smart 		rc = -EPIPE;
45007ad20aa9SJames Smart 		goto job_error;
45017ad20aa9SJames Smart 	}
45027ad20aa9SJames Smart 
45037ad20aa9SJames Smart 	/* wait for additoinal external buffers */
450401e0e15cSJohannes Thumshirn 	bsg_reply->result = 0;
450506548160SJohannes Thumshirn 	bsg_job_done(job, bsg_reply->result,
45061abaede7SJohannes Thumshirn 		       bsg_reply->reply_payload_rcv_len);
45077ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
45087ad20aa9SJames Smart 
45097ad20aa9SJames Smart job_error:
45107ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
45117ad20aa9SJames Smart 	kfree(dd_data);
45127ad20aa9SJames Smart 
45137ad20aa9SJames Smart 	return rc;
45147ad20aa9SJames Smart }
45157ad20aa9SJames Smart 
45167ad20aa9SJames Smart /**
45177ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
45187ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
45197ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
45207ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
45217ad20aa9SJames Smart  *
45227ad20aa9SJames Smart  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
45237ad20aa9SJames Smart  * command with multiple non-embedded external buffers.
45247ad20aa9SJames Smart  **/
45257ad20aa9SJames Smart static int
452675cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
45277ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
45287ad20aa9SJames Smart {
45297ad20aa9SJames Smart 	int rc;
45307ad20aa9SJames Smart 
45317ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45327ad20aa9SJames Smart 			"2971 SLI_CONFIG buffer (type:x%x)\n",
45337ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mboxType);
45347ad20aa9SJames Smart 
45357ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
45367ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
45377ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
45387ad20aa9SJames Smart 					"2972 SLI_CONFIG rd buffer state "
45397ad20aa9SJames Smart 					"mismatch:x%x\n",
45407ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
45417ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
45427ad20aa9SJames Smart 			return -EPIPE;
45437ad20aa9SJames Smart 		}
45447ad20aa9SJames Smart 		rc = lpfc_bsg_read_ebuf_get(phba, job);
45457ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
45467ad20aa9SJames Smart 			lpfc_bsg_dma_page_free(phba, dmabuf);
45477ad20aa9SJames Smart 	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
45487ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
45497ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
45507ad20aa9SJames Smart 					"2973 SLI_CONFIG wr buffer state "
45517ad20aa9SJames Smart 					"mismatch:x%x\n",
45527ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
45537ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
45547ad20aa9SJames Smart 			return -EPIPE;
45557ad20aa9SJames Smart 		}
45567ad20aa9SJames Smart 		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
45577ad20aa9SJames Smart 	}
45587ad20aa9SJames Smart 	return rc;
45597ad20aa9SJames Smart }
45607ad20aa9SJames Smart 
45617ad20aa9SJames Smart /**
45627ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
45637ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
45647ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
45657ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
45667ad20aa9SJames Smart  *
45677ad20aa9SJames Smart  * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
45687ad20aa9SJames Smart  * (0x9B) mailbox commands and external buffers.
45697ad20aa9SJames Smart  **/
45707ad20aa9SJames Smart static int
457175cc8cfcSJohannes Thumshirn lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
45727ad20aa9SJames Smart 			    struct lpfc_dmabuf *dmabuf)
45737ad20aa9SJames Smart {
457401e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
45757ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
457688a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
45777ad20aa9SJames Smart 
45787ad20aa9SJames Smart 	mbox_req =
457901e0e15cSJohannes Thumshirn 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
45807ad20aa9SJames Smart 
45817ad20aa9SJames Smart 	/* mbox command with/without single external buffer */
45827ad20aa9SJames Smart 	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
458388a2cfbbSJames Smart 		return rc;
45847ad20aa9SJames Smart 
45857ad20aa9SJames Smart 	/* mbox command and first external buffer */
45867ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
45877ad20aa9SJames Smart 		if (mbox_req->extSeqNum == 1) {
45887ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45897ad20aa9SJames Smart 					"2974 SLI_CONFIG mailbox: tag:%d, "
45907ad20aa9SJames Smart 					"seq:%d\n", mbox_req->extMboxTag,
45917ad20aa9SJames Smart 					mbox_req->extSeqNum);
45927ad20aa9SJames Smart 			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
45937ad20aa9SJames Smart 			return rc;
45947ad20aa9SJames Smart 		} else
45957ad20aa9SJames Smart 			goto sli_cfg_ext_error;
45967ad20aa9SJames Smart 	}
45977ad20aa9SJames Smart 
45987ad20aa9SJames Smart 	/*
45997ad20aa9SJames Smart 	 * handle additional external buffers
46007ad20aa9SJames Smart 	 */
46017ad20aa9SJames Smart 
46027ad20aa9SJames Smart 	/* check broken pipe conditions */
46037ad20aa9SJames Smart 	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
46047ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46057ad20aa9SJames Smart 	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
46067ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46077ad20aa9SJames Smart 	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
46087ad20aa9SJames Smart 		goto sli_cfg_ext_error;
46097ad20aa9SJames Smart 
46107ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
46117ad20aa9SJames Smart 			"2975 SLI_CONFIG mailbox external buffer: "
46127ad20aa9SJames Smart 			"extSta:x%x, tag:%d, seq:%d\n",
46137ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
46147ad20aa9SJames Smart 			mbox_req->extSeqNum);
46157ad20aa9SJames Smart 	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
46167ad20aa9SJames Smart 	return rc;
46177ad20aa9SJames Smart 
46187ad20aa9SJames Smart sli_cfg_ext_error:
46197ad20aa9SJames Smart 	/* all other cases, broken pipe */
46207ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
46217ad20aa9SJames Smart 			"2976 SLI_CONFIG mailbox broken pipe: "
46227ad20aa9SJames Smart 			"ctxSta:x%x, ctxNumBuf:%d "
46237ad20aa9SJames Smart 			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
46247ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state,
46257ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.numBuf,
46267ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbxTag,
46277ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.seqNum,
46287ad20aa9SJames Smart 			mbox_req->extMboxTag, mbox_req->extSeqNum);
46297ad20aa9SJames Smart 
46307ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
46317ad20aa9SJames Smart 
46327ad20aa9SJames Smart 	return -EPIPE;
46337ad20aa9SJames Smart }
46347ad20aa9SJames Smart 
46357ad20aa9SJames Smart /**
46363b5dd52aSJames Smart  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
46373b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
46383b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
46393b5dd52aSJames Smart  * @vport: Pointer to a vport object.
46403b5dd52aSJames Smart  *
46413b5dd52aSJames Smart  * Allocate a tracking object, mailbox command memory, get a mailbox
46423b5dd52aSJames Smart  * from the mailbox pool, copy the caller mailbox command.
46433b5dd52aSJames Smart  *
46443b5dd52aSJames Smart  * If offline and the sli is active we need to poll for the command (port is
46453b5dd52aSJames Smart  * being reset) and com-plete the job, otherwise issue the mailbox command and
46463b5dd52aSJames Smart  * let our completion handler finish the command.
46473b5dd52aSJames Smart  **/
4648a2fc4aefSJames Smart static int
464975cc8cfcSJohannes Thumshirn lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
46503b5dd52aSJames Smart 	struct lpfc_vport *vport)
46513b5dd52aSJames Smart {
465201e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
465301e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
46547a470277SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
46557a470277SJames Smart 	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
46567a470277SJames Smart 	/* a 4k buffer to hold the mb and extended data from/to the bsg */
46577ad20aa9SJames Smart 	uint8_t *pmbx = NULL;
46587a470277SJames Smart 	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
46597ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
46607ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
4661b6e3b9c6SJames Smart 	struct READ_EVENT_LOG_VAR *rdEventLog;
4662b6e3b9c6SJames Smart 	uint32_t transmit_length, receive_length, mode;
46637ad20aa9SJames Smart 	struct lpfc_mbx_sli4_config *sli4_config;
4664b6e3b9c6SJames Smart 	struct lpfc_mbx_nembed_cmd *nembed_sge;
4665b6e3b9c6SJames Smart 	struct ulp_bde64 *bde;
46667a470277SJames Smart 	uint8_t *ext = NULL;
46673b5dd52aSJames Smart 	int rc = 0;
46687a470277SJames Smart 	uint8_t *from;
46697ad20aa9SJames Smart 	uint32_t size;
46707ad20aa9SJames Smart 
46717a470277SJames Smart 	/* in case no data is transferred */
467201e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
46737a470277SJames Smart 
4674b6e3b9c6SJames Smart 	/* sanity check to protect driver */
4675b6e3b9c6SJames Smart 	if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4676b6e3b9c6SJames Smart 	    job->request_payload.payload_len > BSG_MBOX_SIZE) {
4677b6e3b9c6SJames Smart 		rc = -ERANGE;
4678b6e3b9c6SJames Smart 		goto job_done;
4679b6e3b9c6SJames Smart 	}
4680b6e3b9c6SJames Smart 
46817ad20aa9SJames Smart 	/*
46827ad20aa9SJames Smart 	 * Don't allow mailbox commands to be sent when blocked or when in
46837ad20aa9SJames Smart 	 * the middle of discovery
46847ad20aa9SJames Smart 	 */
46857ad20aa9SJames Smart 	 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
46867ad20aa9SJames Smart 		rc = -EAGAIN;
46877ad20aa9SJames Smart 		goto job_done;
46887ad20aa9SJames Smart 	}
46897ad20aa9SJames Smart 
46907ad20aa9SJames Smart 	mbox_req =
469101e0e15cSJohannes Thumshirn 	    (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
46927ad20aa9SJames Smart 
46937a470277SJames Smart 	/* check if requested extended data lengths are valid */
4694b6e3b9c6SJames Smart 	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4695b6e3b9c6SJames Smart 	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
46967a470277SJames Smart 		rc = -ERANGE;
46977a470277SJames Smart 		goto job_done;
46987a470277SJames Smart 	}
46993b5dd52aSJames Smart 
47007ad20aa9SJames Smart 	dmabuf = lpfc_bsg_dma_page_alloc(phba);
47017ad20aa9SJames Smart 	if (!dmabuf || !dmabuf->virt) {
47027ad20aa9SJames Smart 		rc = -ENOMEM;
47037ad20aa9SJames Smart 		goto job_done;
47047ad20aa9SJames Smart 	}
47057ad20aa9SJames Smart 
47067ad20aa9SJames Smart 	/* Get the mailbox command or external buffer from BSG */
47077ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
47087ad20aa9SJames Smart 	size = job->request_payload.payload_len;
47097ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
47107ad20aa9SJames Smart 			  job->request_payload.sg_cnt, pmbx, size);
47117ad20aa9SJames Smart 
47127ad20aa9SJames Smart 	/* Handle possible SLI_CONFIG with non-embedded payloads */
47137ad20aa9SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
47147ad20aa9SJames Smart 		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
47157ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
47167ad20aa9SJames Smart 			goto job_cont;
47177ad20aa9SJames Smart 		if (rc)
47187ad20aa9SJames Smart 			goto job_done;
47197ad20aa9SJames Smart 		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
47207ad20aa9SJames Smart 	}
47217ad20aa9SJames Smart 
47227ad20aa9SJames Smart 	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
47237ad20aa9SJames Smart 	if (rc != 0)
47247ad20aa9SJames Smart 		goto job_done; /* must be negative */
47257ad20aa9SJames Smart 
47263b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
47273b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
47283b5dd52aSJames Smart 	if (!dd_data) {
47293b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
47303b5dd52aSJames Smart 				"2727 Failed allocation of dd_data\n");
47317a470277SJames Smart 		rc = -ENOMEM;
47327a470277SJames Smart 		goto job_done;
47333b5dd52aSJames Smart 	}
47343b5dd52aSJames Smart 
47353b5dd52aSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
47363b5dd52aSJames Smart 	if (!pmboxq) {
47377a470277SJames Smart 		rc = -ENOMEM;
47387a470277SJames Smart 		goto job_done;
47393b5dd52aSJames Smart 	}
47407a470277SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
47413b5dd52aSJames Smart 
47423b5dd52aSJames Smart 	pmb = &pmboxq->u.mb;
47437ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
47443b5dd52aSJames Smart 	pmb->mbxOwner = OWN_HOST;
47453b5dd52aSJames Smart 	pmboxq->vport = vport;
47463b5dd52aSJames Smart 
4747c7495937SJames Smart 	/* If HBA encountered an error attention, allow only DUMP
4748c7495937SJames Smart 	 * or RESTART mailbox commands until the HBA is restarted.
4749c7495937SJames Smart 	 */
4750c7495937SJames Smart 	if (phba->pport->stopped &&
4751c7495937SJames Smart 	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
4752c7495937SJames Smart 	    pmb->mbxCommand != MBX_RESTART &&
4753c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
4754c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_WWN)
4755c7495937SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4756c7495937SJames Smart 				"2797 mbox: Issued mailbox cmd "
4757c7495937SJames Smart 				"0x%x while in stopped state.\n",
4758c7495937SJames Smart 				pmb->mbxCommand);
4759c7495937SJames Smart 
47607a470277SJames Smart 	/* extended mailbox commands will need an extended buffer */
4761c7495937SJames Smart 	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
47627ad20aa9SJames Smart 		from = pmbx;
47637ad20aa9SJames Smart 		ext = from + sizeof(MAILBOX_t);
47647a470277SJames Smart 		pmboxq->context2 = ext;
47657a470277SJames Smart 		pmboxq->in_ext_byte_len =
47667a470277SJames Smart 			mbox_req->inExtWLen * sizeof(uint32_t);
47677a470277SJames Smart 		pmboxq->out_ext_byte_len =
4768c7495937SJames Smart 			mbox_req->outExtWLen * sizeof(uint32_t);
47697a470277SJames Smart 		pmboxq->mbox_offset_word = mbox_req->mbOffset;
47707a470277SJames Smart 	}
47717a470277SJames Smart 
47727a470277SJames Smart 	/* biu diag will need a kernel buffer to transfer the data
47737a470277SJames Smart 	 * allocate our own buffer and setup the mailbox command to
47747a470277SJames Smart 	 * use ours
47757a470277SJames Smart 	 */
47767a470277SJames Smart 	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4777b6e3b9c6SJames Smart 		transmit_length = pmb->un.varWords[1];
4778b6e3b9c6SJames Smart 		receive_length = pmb->un.varWords[4];
4779c7495937SJames Smart 		/* transmit length cannot be greater than receive length or
4780c7495937SJames Smart 		 * mailbox extension size
4781c7495937SJames Smart 		 */
4782c7495937SJames Smart 		if ((transmit_length > receive_length) ||
478388a2cfbbSJames Smart 			(transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4784c7495937SJames Smart 			rc = -ERANGE;
4785c7495937SJames Smart 			goto job_done;
4786c7495937SJames Smart 		}
47877a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
47887ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
47897a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
47907ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
47917a470277SJames Smart 
47927a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
47937ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
47947ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
47957a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
47967ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
47977ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4798c7495937SJames Smart 	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4799b6e3b9c6SJames Smart 		rdEventLog = &pmb->un.varRdEventLog;
4800b6e3b9c6SJames Smart 		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4801b6e3b9c6SJames Smart 		mode = bf_get(lpfc_event_log, rdEventLog);
4802c7495937SJames Smart 
4803c7495937SJames Smart 		/* receive length cannot be greater than mailbox
4804c7495937SJames Smart 		 * extension size
4805c7495937SJames Smart 		 */
480688a2cfbbSJames Smart 		if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4807c7495937SJames Smart 			rc = -ERANGE;
4808c7495937SJames Smart 			goto job_done;
4809c7495937SJames Smart 		}
4810c7495937SJames Smart 
4811c7495937SJames Smart 		/* mode zero uses a bde like biu diags command */
4812c7495937SJames Smart 		if (mode == 0) {
48137ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
48147ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
48157ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
48167ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
4817c7495937SJames Smart 		}
4818c7495937SJames Smart 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
48193ef6d24cSJames Smart 		/* Let type 4 (well known data) through because the data is
48203ef6d24cSJames Smart 		 * returned in varwords[4-8]
48213ef6d24cSJames Smart 		 * otherwise check the recieve length and fetch the buffer addr
48223ef6d24cSJames Smart 		 */
48233ef6d24cSJames Smart 		if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
48243ef6d24cSJames Smart 			(pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4825c7495937SJames Smart 			/* rebuild the command for sli4 using our own buffers
4826c7495937SJames Smart 			* like we do for biu diags
4827c7495937SJames Smart 			*/
4828b6e3b9c6SJames Smart 			receive_length = pmb->un.varWords[2];
4829c7495937SJames Smart 			/* receive length cannot be greater than mailbox
4830c7495937SJames Smart 			 * extension size
4831c7495937SJames Smart 			 */
48327ad20aa9SJames Smart 			if (receive_length == 0) {
4833c7495937SJames Smart 				rc = -ERANGE;
4834c7495937SJames Smart 				goto job_done;
4835c7495937SJames Smart 			}
48367ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
48377ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
48387ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
48397ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4840c7495937SJames Smart 		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4841c7495937SJames Smart 			pmb->un.varUpdateCfg.co) {
4842b6e3b9c6SJames Smart 			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4843c7495937SJames Smart 
4844c7495937SJames Smart 			/* bde size cannot be greater than mailbox ext size */
484588a2cfbbSJames Smart 			if (bde->tus.f.bdeSize >
484688a2cfbbSJames Smart 			    BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4847c7495937SJames Smart 				rc = -ERANGE;
4848c7495937SJames Smart 				goto job_done;
4849c7495937SJames Smart 			}
48507ad20aa9SJames Smart 			bde->addrHigh = putPaddrHigh(dmabuf->phys
48517ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
48527ad20aa9SJames Smart 			bde->addrLow = putPaddrLow(dmabuf->phys
48537ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4854515e0aa2SJames Smart 		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
48557ad20aa9SJames Smart 			/* Handling non-embedded SLI_CONFIG mailbox command */
48567ad20aa9SJames Smart 			sli4_config = &pmboxq->u.mqe.un.sli4_config;
48577ad20aa9SJames Smart 			if (!bf_get(lpfc_mbox_hdr_emb,
48587ad20aa9SJames Smart 			    &sli4_config->header.cfg_mhdr)) {
48597ad20aa9SJames Smart 				/* rebuild the command for sli4 using our
48607ad20aa9SJames Smart 				 * own buffers like we do for biu diags
4861515e0aa2SJames Smart 				 */
4862515e0aa2SJames Smart 				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4863515e0aa2SJames Smart 						&pmb->un.varWords[0];
4864515e0aa2SJames Smart 				receive_length = nembed_sge->sge[0].length;
4865515e0aa2SJames Smart 
48667ad20aa9SJames Smart 				/* receive length cannot be greater than
48677ad20aa9SJames Smart 				 * mailbox extension size
4868515e0aa2SJames Smart 				 */
4869515e0aa2SJames Smart 				if ((receive_length == 0) ||
487088a2cfbbSJames Smart 				    (receive_length >
487188a2cfbbSJames Smart 				     BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4872515e0aa2SJames Smart 					rc = -ERANGE;
4873515e0aa2SJames Smart 					goto job_done;
4874515e0aa2SJames Smart 				}
4875515e0aa2SJames Smart 
48767ad20aa9SJames Smart 				nembed_sge->sge[0].pa_hi =
48777ad20aa9SJames Smart 						putPaddrHigh(dmabuf->phys
48787ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
48797ad20aa9SJames Smart 				nembed_sge->sge[0].pa_lo =
48807ad20aa9SJames Smart 						putPaddrLow(dmabuf->phys
48817ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
4882515e0aa2SJames Smart 			}
4883c7495937SJames Smart 		}
4884c7495937SJames Smart 	}
4885c7495937SJames Smart 
48867ad20aa9SJames Smart 	dd_data->context_un.mbox.dmabuffers = dmabuf;
48873b5dd52aSJames Smart 
48883b5dd52aSJames Smart 	/* setup wake call as IOCB callback */
48897ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
48907a470277SJames Smart 
48913b5dd52aSJames Smart 	/* setup context field to pass wait_queue pointer to wake function */
48923b5dd52aSJames Smart 	pmboxq->context1 = dd_data;
48933b5dd52aSJames Smart 	dd_data->type = TYPE_MBOX;
4894a33c4f7bSJames Smart 	dd_data->set_job = job;
48953b5dd52aSJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
48967ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
48977a470277SJames Smart 	dd_data->context_un.mbox.ext = ext;
48987a470277SJames Smart 	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
48997a470277SJames Smart 	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4900c7495937SJames Smart 	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
49013b5dd52aSJames Smart 	job->dd_data = dd_data;
49027a470277SJames Smart 
49037a470277SJames Smart 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
49047a470277SJames Smart 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
49057a470277SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
49067a470277SJames Smart 		if (rc != MBX_SUCCESS) {
49077a470277SJames Smart 			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
49087a470277SJames Smart 			goto job_done;
49093b5dd52aSJames Smart 		}
49103b5dd52aSJames Smart 
49117a470277SJames Smart 		/* job finished, copy the data */
49127ad20aa9SJames Smart 		memcpy(pmbx, pmb, sizeof(*pmb));
491301e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len =
49147a470277SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
49157a470277SJames Smart 					    job->reply_payload.sg_cnt,
49167ad20aa9SJames Smart 					    pmbx, size);
49177a470277SJames Smart 		/* not waiting mbox already done */
49187a470277SJames Smart 		rc = 0;
49197a470277SJames Smart 		goto job_done;
49207a470277SJames Smart 	}
49217a470277SJames Smart 
49227a470277SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
49237a470277SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
49247a470277SJames Smart 		return 1; /* job started */
49257a470277SJames Smart 
49267a470277SJames Smart job_done:
49277a470277SJames Smart 	/* common exit for error or job completed inline */
49287a470277SJames Smart 	if (pmboxq)
49297a470277SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
49307ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
49317a470277SJames Smart 	kfree(dd_data);
49327a470277SJames Smart 
49337ad20aa9SJames Smart job_cont:
49347a470277SJames Smart 	return rc;
49353b5dd52aSJames Smart }
49363b5dd52aSJames Smart 
49373b5dd52aSJames Smart /**
49383b5dd52aSJames Smart  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
49393b5dd52aSJames Smart  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
49403b5dd52aSJames Smart  **/
49413b5dd52aSJames Smart static int
494275cc8cfcSJohannes Thumshirn lpfc_bsg_mbox_cmd(struct bsg_job *job)
49433b5dd52aSJames Smart {
4944cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
494501e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
494601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
49473b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
49487ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
49493b5dd52aSJames Smart 	int rc = 0;
49503b5dd52aSJames Smart 
49517ad20aa9SJames Smart 	/* mix-and-match backward compatibility */
495201e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
49533b5dd52aSJames Smart 	if (job->request_len <
49543b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
49557ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
49561051e9b3SMasanari Iida 				"2737 Mix-and-match backward compatibility "
49577ad20aa9SJames Smart 				"between MBOX_REQ old size:%d and "
49587ad20aa9SJames Smart 				"new request size:%d\n",
49597ad20aa9SJames Smart 				(int)(job->request_len -
49607ad20aa9SJames Smart 				      sizeof(struct fc_bsg_request)),
49617ad20aa9SJames Smart 				(int)sizeof(struct dfc_mbox_req));
49627ad20aa9SJames Smart 		mbox_req = (struct dfc_mbox_req *)
496301e0e15cSJohannes Thumshirn 				bsg_request->rqst_data.h_vendor.vendor_cmd;
49647ad20aa9SJames Smart 		mbox_req->extMboxTag = 0;
49657ad20aa9SJames Smart 		mbox_req->extSeqNum = 0;
49663b5dd52aSJames Smart 	}
49673b5dd52aSJames Smart 
49683b5dd52aSJames Smart 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
49693b5dd52aSJames Smart 
49703b5dd52aSJames Smart 	if (rc == 0) {
49713b5dd52aSJames Smart 		/* job done */
497201e0e15cSJohannes Thumshirn 		bsg_reply->result = 0;
49733b5dd52aSJames Smart 		job->dd_data = NULL;
497406548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
49751abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
49763b5dd52aSJames Smart 	} else if (rc == 1)
49773b5dd52aSJames Smart 		/* job submitted, will complete later*/
49783b5dd52aSJames Smart 		rc = 0; /* return zero, no error */
49793b5dd52aSJames Smart 	else {
49803b5dd52aSJames Smart 		/* some error occurred */
498101e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
49823b5dd52aSJames Smart 		job->dd_data = NULL;
49833b5dd52aSJames Smart 	}
49843b5dd52aSJames Smart 
49853b5dd52aSJames Smart 	return rc;
49863b5dd52aSJames Smart }
49873b5dd52aSJames Smart 
49883b5dd52aSJames Smart /**
4989e2aed29fSJames Smart  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4990e2aed29fSJames Smart  * @phba: Pointer to HBA context object.
4991e2aed29fSJames Smart  * @cmdiocbq: Pointer to command iocb.
4992e2aed29fSJames Smart  * @rspiocbq: Pointer to response iocb.
4993e2aed29fSJames Smart  *
4994e2aed29fSJames Smart  * This function is the completion handler for iocbs issued using
4995e2aed29fSJames Smart  * lpfc_menlo_cmd function. This function is called by the
4996e2aed29fSJames Smart  * ring event handler function without any lock held. This function
4997e2aed29fSJames Smart  * can be called from both worker thread context and interrupt
4998e2aed29fSJames Smart  * context. This function also can be called from another thread which
4999e2aed29fSJames Smart  * cleans up the SLI layer objects.
5000e2aed29fSJames Smart  * This function copies the contents of the response iocb to the
5001e2aed29fSJames Smart  * response iocb memory object provided by the caller of
5002e2aed29fSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5003e2aed29fSJames Smart  * sleeps for the iocb completion.
5004e2aed29fSJames Smart  **/
5005e2aed29fSJames Smart static void
5006e2aed29fSJames Smart lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
5007e2aed29fSJames Smart 			struct lpfc_iocbq *cmdiocbq,
5008e2aed29fSJames Smart 			struct lpfc_iocbq *rspiocbq)
5009e2aed29fSJames Smart {
5010e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
501175cc8cfcSJohannes Thumshirn 	struct bsg_job *job;
501201e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply;
5013e2aed29fSJames Smart 	IOCB_t *rsp;
5014a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
5015e2aed29fSJames Smart 	struct lpfc_bsg_menlo *menlo;
5016e2aed29fSJames Smart 	unsigned long flags;
5017e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
5018a33c4f7bSJames Smart 	unsigned int rsp_size;
5019e2aed29fSJames Smart 	int rc = 0;
5020e2aed29fSJames Smart 
5021e2aed29fSJames Smart 	dd_data = cmdiocbq->context1;
5022a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
5023a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
5024e2aed29fSJames Smart 	menlo = &dd_data->context_un.menlo;
5025a33c4f7bSJames Smart 	rmp = menlo->rmp;
5026e2aed29fSJames Smart 	rsp = &rspiocbq->iocb;
5027e2aed29fSJames Smart 
5028a33c4f7bSJames Smart 	/* Determine if job has been aborted */
5029a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
5030a33c4f7bSJames Smart 	job = dd_data->set_job;
5031a33c4f7bSJames Smart 	if (job) {
503201e0e15cSJohannes Thumshirn 		bsg_reply = job->reply;
5033a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
5034a33c4f7bSJames Smart 		job->dd_data = NULL;
5035a33c4f7bSJames Smart 	}
5036a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5037e2aed29fSJames Smart 
5038a33c4f7bSJames Smart 	/* Copy the job data or set the failing status for the job */
5039a33c4f7bSJames Smart 
5040a33c4f7bSJames Smart 	if (job) {
5041e2aed29fSJames Smart 		/* always return the xri, this would be used in the case
5042a33c4f7bSJames Smart 		 * of a menlo download to allow the data to be sent as a
5043a33c4f7bSJames Smart 		 * continuation of the exchange.
5044e2aed29fSJames Smart 		 */
5045a33c4f7bSJames Smart 
5046e2aed29fSJames Smart 		menlo_resp = (struct menlo_response *)
504701e0e15cSJohannes Thumshirn 			bsg_reply->reply_data.vendor_reply.vendor_rsp;
5048e2aed29fSJames Smart 		menlo_resp->xri = rsp->ulpContext;
5049e2aed29fSJames Smart 		if (rsp->ulpStatus) {
5050e2aed29fSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
5051e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
5052e2aed29fSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
5053e2aed29fSJames Smart 					rc = -ETIMEDOUT;
5054e2aed29fSJames Smart 					break;
5055e2aed29fSJames Smart 				case IOERR_INVALID_RPI:
5056e2aed29fSJames Smart 					rc = -EFAULT;
5057e2aed29fSJames Smart 					break;
5058e2aed29fSJames Smart 				default:
5059e2aed29fSJames Smart 					rc = -EACCES;
5060e2aed29fSJames Smart 					break;
5061e2aed29fSJames Smart 				}
5062a33c4f7bSJames Smart 			} else {
5063e2aed29fSJames Smart 				rc = -EACCES;
5064a33c4f7bSJames Smart 			}
5065a33c4f7bSJames Smart 		} else {
5066a33c4f7bSJames Smart 			rsp_size = rsp->un.genreq64.bdl.bdeSize;
506701e0e15cSJohannes Thumshirn 			bsg_reply->reply_payload_rcv_len =
5068a33c4f7bSJames Smart 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
5069a33c4f7bSJames Smart 						   rsp_size, 0);
5070a33c4f7bSJames Smart 		}
5071e2aed29fSJames Smart 
5072a33c4f7bSJames Smart 	}
5073a33c4f7bSJames Smart 
5074e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5075a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
5076a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
5077a33c4f7bSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5078e2aed29fSJames Smart 	kfree(bmp);
5079e2aed29fSJames Smart 	kfree(dd_data);
5080a33c4f7bSJames Smart 
5081a33c4f7bSJames Smart 	/* Complete the job if active */
5082a33c4f7bSJames Smart 
5083a33c4f7bSJames Smart 	if (job) {
508401e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
508506548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
50861abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
5087a33c4f7bSJames Smart 	}
5088a33c4f7bSJames Smart 
5089e2aed29fSJames Smart 	return;
5090e2aed29fSJames Smart }
5091e2aed29fSJames Smart 
5092e2aed29fSJames Smart /**
5093e2aed29fSJames Smart  * lpfc_menlo_cmd - send an ioctl for menlo hardware
5094e2aed29fSJames Smart  * @job: fc_bsg_job to handle
5095e2aed29fSJames Smart  *
5096e2aed29fSJames Smart  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
5097e2aed29fSJames Smart  * all the command completions will return the xri for the command.
5098e2aed29fSJames Smart  * For menlo data requests a gen request 64 CX is used to continue the exchange
5099e2aed29fSJames Smart  * supplied in the menlo request header xri field.
5100e2aed29fSJames Smart  **/
5101e2aed29fSJames Smart static int
510275cc8cfcSJohannes Thumshirn lpfc_menlo_cmd(struct bsg_job *job)
5103e2aed29fSJames Smart {
5104cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
510501e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
510601e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5107e2aed29fSJames Smart 	struct lpfc_hba *phba = vport->phba;
5108a33c4f7bSJames Smart 	struct lpfc_iocbq *cmdiocbq;
5109a33c4f7bSJames Smart 	IOCB_t *cmd;
5110e2aed29fSJames Smart 	int rc = 0;
5111e2aed29fSJames Smart 	struct menlo_command *menlo_cmd;
5112a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
5113e2aed29fSJames Smart 	int request_nseg;
5114e2aed29fSJames Smart 	int reply_nseg;
5115e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
5116e2aed29fSJames Smart 	struct ulp_bde64 *bpl = NULL;
5117e2aed29fSJames Smart 
5118e2aed29fSJames Smart 	/* in case no data is returned return just the return code */
511901e0e15cSJohannes Thumshirn 	bsg_reply->reply_payload_rcv_len = 0;
5120e2aed29fSJames Smart 
5121e2aed29fSJames Smart 	if (job->request_len <
5122e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) +
5123e2aed29fSJames Smart 		sizeof(struct menlo_command)) {
5124e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5125e2aed29fSJames Smart 				"2784 Received MENLO_CMD request below "
5126e2aed29fSJames Smart 				"minimum size\n");
5127e2aed29fSJames Smart 		rc = -ERANGE;
5128e2aed29fSJames Smart 		goto no_dd_data;
5129e2aed29fSJames Smart 	}
5130e2aed29fSJames Smart 
5131e2aed29fSJames Smart 	if (job->reply_len <
5132e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5133e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5134e2aed29fSJames Smart 				"2785 Received MENLO_CMD reply below "
5135e2aed29fSJames Smart 				"minimum size\n");
5136e2aed29fSJames Smart 		rc = -ERANGE;
5137e2aed29fSJames Smart 		goto no_dd_data;
5138e2aed29fSJames Smart 	}
5139e2aed29fSJames Smart 
5140e2aed29fSJames Smart 	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5141e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5142e2aed29fSJames Smart 				"2786 Adapter does not support menlo "
5143e2aed29fSJames Smart 				"commands\n");
5144e2aed29fSJames Smart 		rc = -EPERM;
5145e2aed29fSJames Smart 		goto no_dd_data;
5146e2aed29fSJames Smart 	}
5147e2aed29fSJames Smart 
5148e2aed29fSJames Smart 	menlo_cmd = (struct menlo_command *)
514901e0e15cSJohannes Thumshirn 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5150e2aed29fSJames Smart 
5151e2aed29fSJames Smart 	/* allocate our bsg tracking structure */
5152e2aed29fSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5153e2aed29fSJames Smart 	if (!dd_data) {
5154e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5155e2aed29fSJames Smart 				"2787 Failed allocation of dd_data\n");
5156e2aed29fSJames Smart 		rc = -ENOMEM;
5157e2aed29fSJames Smart 		goto no_dd_data;
5158e2aed29fSJames Smart 	}
5159e2aed29fSJames Smart 
5160e2aed29fSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5161e2aed29fSJames Smart 	if (!bmp) {
5162e2aed29fSJames Smart 		rc = -ENOMEM;
5163e2aed29fSJames Smart 		goto free_dd;
5164e2aed29fSJames Smart 	}
5165e2aed29fSJames Smart 
5166a33c4f7bSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5167a33c4f7bSJames Smart 	if (!bmp->virt) {
5168e2aed29fSJames Smart 		rc = -ENOMEM;
5169e2aed29fSJames Smart 		goto free_bmp;
5170e2aed29fSJames Smart 	}
5171e2aed29fSJames Smart 
5172e2aed29fSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5173a33c4f7bSJames Smart 
5174e2aed29fSJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
5175a33c4f7bSJames Smart 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5176a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5177a33c4f7bSJames Smart 				     1, bpl, &request_nseg);
5178a33c4f7bSJames Smart 	if (!cmp) {
5179a33c4f7bSJames Smart 		rc = -ENOMEM;
5180a33c4f7bSJames Smart 		goto free_bmp;
5181a33c4f7bSJames Smart 	}
5182a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
5183a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
5184a33c4f7bSJames Smart 
5185a33c4f7bSJames Smart 	bpl += request_nseg;
5186a33c4f7bSJames Smart 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5187a33c4f7bSJames Smart 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5188a33c4f7bSJames Smart 				     bpl, &reply_nseg);
5189a33c4f7bSJames Smart 	if (!rmp) {
5190a33c4f7bSJames Smart 		rc = -ENOMEM;
5191a33c4f7bSJames Smart 		goto free_cmp;
5192e2aed29fSJames Smart 	}
5193e2aed29fSJames Smart 
5194a33c4f7bSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
5195a33c4f7bSJames Smart 	if (!cmdiocbq) {
5196a33c4f7bSJames Smart 		rc = -ENOMEM;
5197a33c4f7bSJames Smart 		goto free_rmp;
5198e2aed29fSJames Smart 	}
5199e2aed29fSJames Smart 
5200e2aed29fSJames Smart 	cmd = &cmdiocbq->iocb;
5201e2aed29fSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5202e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5203e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5204e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5205e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
5206e2aed29fSJames Smart 	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5207e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5208e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5209e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5210e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5211e2aed29fSJames Smart 	cmd->ulpBdeCount = 1;
5212e2aed29fSJames Smart 	cmd->ulpClass = CLASS3;
5213e2aed29fSJames Smart 	cmd->ulpOwner = OWN_CHIP;
5214e2aed29fSJames Smart 	cmd->ulpLe = 1; /* Limited Edition */
5215e2aed29fSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5216e2aed29fSJames Smart 	cmdiocbq->vport = phba->pport;
5217e2aed29fSJames Smart 	/* We want the firmware to timeout before we do */
5218e2aed29fSJames Smart 	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
5219e2aed29fSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5220e2aed29fSJames Smart 	cmdiocbq->context1 = dd_data;
5221a33c4f7bSJames Smart 	cmdiocbq->context2 = cmp;
5222a33c4f7bSJames Smart 	cmdiocbq->context3 = bmp;
5223e2aed29fSJames Smart 	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5224e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5225e2aed29fSJames Smart 		cmd->ulpPU = MENLO_PU; /* 3 */
5226e2aed29fSJames Smart 		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5227e2aed29fSJames Smart 		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5228e2aed29fSJames Smart 	} else {
5229e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5230e2aed29fSJames Smart 		cmd->ulpPU = 1;
5231e2aed29fSJames Smart 		cmd->un.ulpWord[4] = 0;
5232e2aed29fSJames Smart 		cmd->ulpContext = menlo_cmd->xri;
5233e2aed29fSJames Smart 	}
5234e2aed29fSJames Smart 
5235e2aed29fSJames Smart 	dd_data->type = TYPE_MENLO;
5236a33c4f7bSJames Smart 	dd_data->set_job = job;
5237e2aed29fSJames Smart 	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5238a33c4f7bSJames Smart 	dd_data->context_un.menlo.rmp = rmp;
5239a33c4f7bSJames Smart 	job->dd_data = dd_data;
5240e2aed29fSJames Smart 
5241e2aed29fSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5242e2aed29fSJames Smart 		MENLO_TIMEOUT - 5);
5243e2aed29fSJames Smart 	if (rc == IOCB_SUCCESS)
5244e2aed29fSJames Smart 		return 0; /* done for now */
5245e2aed29fSJames Smart 
5246e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5247a33c4f7bSJames Smart 
5248a33c4f7bSJames Smart free_rmp:
5249a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
5250a33c4f7bSJames Smart free_cmp:
5251a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
5252e2aed29fSJames Smart free_bmp:
5253a33c4f7bSJames Smart 	if (bmp->virt)
5254a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5255e2aed29fSJames Smart 	kfree(bmp);
5256e2aed29fSJames Smart free_dd:
5257e2aed29fSJames Smart 	kfree(dd_data);
5258e2aed29fSJames Smart no_dd_data:
5259e2aed29fSJames Smart 	/* make error code available to userspace */
526001e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
5261e2aed29fSJames Smart 	job->dd_data = NULL;
5262e2aed29fSJames Smart 	return rc;
5263e2aed29fSJames Smart }
5264b6e3b9c6SJames Smart 
5265c691816eSJames Smart static int
526675cc8cfcSJohannes Thumshirn lpfc_forced_link_speed(struct bsg_job *job)
5267c691816eSJames Smart {
5268cd21c605SJohannes Thumshirn 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5269c691816eSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5270c691816eSJames Smart 	struct lpfc_hba *phba = vport->phba;
527101e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5272c691816eSJames Smart 	struct forced_link_speed_support_reply *forced_reply;
5273c691816eSJames Smart 	int rc = 0;
5274c691816eSJames Smart 
5275c691816eSJames Smart 	if (job->request_len <
5276c691816eSJames Smart 	    sizeof(struct fc_bsg_request) +
5277c691816eSJames Smart 	    sizeof(struct get_forced_link_speed_support)) {
5278c691816eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5279c691816eSJames Smart 				"0048 Received FORCED_LINK_SPEED request "
5280c691816eSJames Smart 				"below minimum size\n");
5281c691816eSJames Smart 		rc = -EINVAL;
5282c691816eSJames Smart 		goto job_error;
5283c691816eSJames Smart 	}
5284c691816eSJames Smart 
5285c691816eSJames Smart 	forced_reply = (struct forced_link_speed_support_reply *)
528601e0e15cSJohannes Thumshirn 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5287c691816eSJames Smart 
5288c691816eSJames Smart 	if (job->reply_len <
5289c691816eSJames Smart 	    sizeof(struct fc_bsg_request) +
5290c691816eSJames Smart 	    sizeof(struct forced_link_speed_support_reply)) {
5291c691816eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5292c691816eSJames Smart 				"0049 Received FORCED_LINK_SPEED reply below "
5293c691816eSJames Smart 				"minimum size\n");
5294c691816eSJames Smart 		rc = -EINVAL;
5295c691816eSJames Smart 		goto job_error;
5296c691816eSJames Smart 	}
5297c691816eSJames Smart 
5298c691816eSJames Smart 	forced_reply->supported = (phba->hba_flag & HBA_FORCED_LINK_SPEED)
5299c691816eSJames Smart 				   ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5300c691816eSJames Smart 				   : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5301c691816eSJames Smart job_error:
530201e0e15cSJohannes Thumshirn 	bsg_reply->result = rc;
5303c691816eSJames Smart 	if (rc == 0)
530406548160SJohannes Thumshirn 		bsg_job_done(job, bsg_reply->result,
53051abaede7SJohannes Thumshirn 			       bsg_reply->reply_payload_rcv_len);
5306c691816eSJames Smart 	return rc;
5307c691816eSJames Smart }
5308c691816eSJames Smart 
5309e2aed29fSJames Smart /**
5310d2cc9bcdSJames Smart  * lpfc_check_fwlog_support: Check FW log support on the adapter
5311d2cc9bcdSJames Smart  * @phba: Pointer to HBA context object.
5312d2cc9bcdSJames Smart  *
5313d2cc9bcdSJames Smart  * Check if FW Logging support by the adapter
5314d2cc9bcdSJames Smart  **/
5315d2cc9bcdSJames Smart int
5316d2cc9bcdSJames Smart lpfc_check_fwlog_support(struct lpfc_hba *phba)
5317d2cc9bcdSJames Smart {
5318d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5319d2cc9bcdSJames Smart 
5320d2cc9bcdSJames Smart 	ras_fwlog = &phba->ras_fwlog;
5321d2cc9bcdSJames Smart 
5322d2cc9bcdSJames Smart 	if (ras_fwlog->ras_hwsupport == false)
5323d2cc9bcdSJames Smart 		return -EACCES;
5324d2cc9bcdSJames Smart 	else if (ras_fwlog->ras_enabled == false)
5325d2cc9bcdSJames Smart 		return -EPERM;
5326d2cc9bcdSJames Smart 	else
5327d2cc9bcdSJames Smart 		return 0;
5328d2cc9bcdSJames Smart }
5329d2cc9bcdSJames Smart 
5330d2cc9bcdSJames Smart /**
5331d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_config: Get RAS configuration settings
5332d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5333d2cc9bcdSJames Smart  *
5334d2cc9bcdSJames Smart  * Get RAS configuration values set.
5335d2cc9bcdSJames Smart  **/
5336d2cc9bcdSJames Smart static int
5337d2cc9bcdSJames Smart lpfc_bsg_get_ras_config(struct bsg_job *job)
5338d2cc9bcdSJames Smart {
5339d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5340d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5341d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5342d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5343d2cc9bcdSJames Smart 	struct lpfc_bsg_get_ras_config_reply *ras_reply;
5344d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5345d2cc9bcdSJames Smart 	int rc = 0;
5346d2cc9bcdSJames Smart 
5347d2cc9bcdSJames Smart 	if (job->request_len <
5348d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5349d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_ras_req)) {
5350d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5351d2cc9bcdSJames Smart 				"6181 Received RAS_LOG request "
5352d2cc9bcdSJames Smart 				"below minimum size\n");
5353d2cc9bcdSJames Smart 		rc = -EINVAL;
5354d2cc9bcdSJames Smart 		goto ras_job_error;
5355d2cc9bcdSJames Smart 	}
5356d2cc9bcdSJames Smart 
5357d2cc9bcdSJames Smart 	/* Check FW log status */
5358d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5359d2cc9bcdSJames Smart 	if (rc == -EACCES || rc == -EPERM)
5360d2cc9bcdSJames Smart 		goto ras_job_error;
5361d2cc9bcdSJames Smart 
5362d2cc9bcdSJames Smart 	ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5363d2cc9bcdSJames Smart 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5364d2cc9bcdSJames Smart 
5365d2cc9bcdSJames Smart 	/* Current logging state */
5366d2cc9bcdSJames Smart 	if (ras_fwlog->ras_active == true)
5367d2cc9bcdSJames Smart 		ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5368d2cc9bcdSJames Smart 	else
5369d2cc9bcdSJames Smart 		ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5370d2cc9bcdSJames Smart 
5371d2cc9bcdSJames Smart 	ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5372d2cc9bcdSJames Smart 	ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5373d2cc9bcdSJames Smart 
5374d2cc9bcdSJames Smart ras_job_error:
5375d2cc9bcdSJames Smart 	/* make error code available to userspace */
5376d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5377d2cc9bcdSJames Smart 
5378d2cc9bcdSJames Smart 	/* complete the job back to userspace */
5379d2cc9bcdSJames Smart 	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5380d2cc9bcdSJames Smart 	return rc;
5381d2cc9bcdSJames Smart }
5382d2cc9bcdSJames Smart 
5383d2cc9bcdSJames Smart /**
5384d2cc9bcdSJames Smart  * lpfc_ras_stop_fwlog: Disable FW logging by the adapter
5385d2cc9bcdSJames Smart  * @phba: Pointer to HBA context object.
5386d2cc9bcdSJames Smart  *
5387d2cc9bcdSJames Smart  * Disable FW logging into host memory on the adapter. To
5388d2cc9bcdSJames Smart  * be done before reading logs from the host memory.
5389d2cc9bcdSJames Smart  **/
5390d2cc9bcdSJames Smart static void
5391d2cc9bcdSJames Smart lpfc_ras_stop_fwlog(struct lpfc_hba *phba)
5392d2cc9bcdSJames Smart {
5393d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5394d2cc9bcdSJames Smart 
5395d2cc9bcdSJames Smart 	ras_fwlog->ras_active = false;
5396d2cc9bcdSJames Smart 
5397d2cc9bcdSJames Smart 	/* Disable FW logging to host memory */
5398d2cc9bcdSJames Smart 	writel(LPFC_CTL_PDEV_CTL_DDL_RAS,
5399d2cc9bcdSJames Smart 	       phba->sli4_hba.conf_regs_memmap_p + LPFC_CTL_PDEV_CTL_OFFSET);
5400d2cc9bcdSJames Smart }
5401d2cc9bcdSJames Smart 
5402d2cc9bcdSJames Smart /**
5403d2cc9bcdSJames Smart  * lpfc_bsg_set_ras_config: Set FW logging parameters
5404d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5405d2cc9bcdSJames Smart  *
5406d2cc9bcdSJames Smart  * Set log-level parameters for FW-logging in host memory
5407d2cc9bcdSJames Smart  **/
5408d2cc9bcdSJames Smart static int
5409d2cc9bcdSJames Smart lpfc_bsg_set_ras_config(struct bsg_job *job)
5410d2cc9bcdSJames Smart {
5411d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5412d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5413d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5414d2cc9bcdSJames Smart 	struct lpfc_bsg_set_ras_config_req *ras_req;
5415d2cc9bcdSJames Smart 	struct fc_bsg_request *bsg_request = job->request;
5416d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5417d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5418d2cc9bcdSJames Smart 	uint8_t action = 0, log_level = 0;
5419*191e2f74SJames Smart 	int rc = 0, action_status = 0;
5420d2cc9bcdSJames Smart 
5421d2cc9bcdSJames Smart 	if (job->request_len <
5422d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5423d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_set_ras_config_req)) {
5424d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5425d2cc9bcdSJames Smart 				"6182 Received RAS_LOG request "
5426d2cc9bcdSJames Smart 				"below minimum size\n");
5427d2cc9bcdSJames Smart 		rc = -EINVAL;
5428d2cc9bcdSJames Smart 		goto ras_job_error;
5429d2cc9bcdSJames Smart 	}
5430d2cc9bcdSJames Smart 
5431d2cc9bcdSJames Smart 	/* Check FW log status */
5432d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5433d2cc9bcdSJames Smart 	if (rc == -EACCES || rc == -EPERM)
5434d2cc9bcdSJames Smart 		goto ras_job_error;
5435d2cc9bcdSJames Smart 
5436d2cc9bcdSJames Smart 	ras_req = (struct lpfc_bsg_set_ras_config_req *)
5437d2cc9bcdSJames Smart 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5438d2cc9bcdSJames Smart 	action = ras_req->action;
5439d2cc9bcdSJames Smart 	log_level = ras_req->log_level;
5440d2cc9bcdSJames Smart 
5441d2cc9bcdSJames Smart 	if (action == LPFC_RASACTION_STOP_LOGGING) {
5442d2cc9bcdSJames Smart 		/* Check if already disabled */
5443d2cc9bcdSJames Smart 		if (ras_fwlog->ras_active == false) {
5444d2cc9bcdSJames Smart 			rc = -ESRCH;
5445d2cc9bcdSJames Smart 			goto ras_job_error;
5446d2cc9bcdSJames Smart 		}
5447d2cc9bcdSJames Smart 
5448d2cc9bcdSJames Smart 		/* Disable logging */
5449d2cc9bcdSJames Smart 		lpfc_ras_stop_fwlog(phba);
5450d2cc9bcdSJames Smart 	} else {
5451d2cc9bcdSJames Smart 		/*action = LPFC_RASACTION_START_LOGGING*/
5452*191e2f74SJames Smart 
5453*191e2f74SJames Smart 		/* Even though FW-logging is active re-initialize
5454*191e2f74SJames Smart 		 * FW-logging with new log-level. Return status
5455*191e2f74SJames Smart 		 * "Logging already Running" to caller.
5456*191e2f74SJames Smart 		 **/
5457*191e2f74SJames Smart 		if (ras_fwlog->ras_active)
5458*191e2f74SJames Smart 			action_status = -EINPROGRESS;
5459d2cc9bcdSJames Smart 
5460d2cc9bcdSJames Smart 		/* Enable logging */
5461d2cc9bcdSJames Smart 		rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5462d2cc9bcdSJames Smart 					      LPFC_RAS_ENABLE_LOGGING);
5463*191e2f74SJames Smart 		if (rc) {
5464d2cc9bcdSJames Smart 			rc = -EINVAL;
5465*191e2f74SJames Smart 			goto ras_job_error;
5466*191e2f74SJames Smart 		}
5467*191e2f74SJames Smart 
5468*191e2f74SJames Smart 		/* Check if FW-logging is re-initialized */
5469*191e2f74SJames Smart 		if (action_status == -EINPROGRESS)
5470*191e2f74SJames Smart 			rc = action_status;
5471d2cc9bcdSJames Smart 	}
5472d2cc9bcdSJames Smart ras_job_error:
5473d2cc9bcdSJames Smart 	/* make error code available to userspace */
5474d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5475d2cc9bcdSJames Smart 
5476d2cc9bcdSJames Smart 	/* complete the job back to userspace */
5477d2cc9bcdSJames Smart 	bsg_job_done(job, bsg_reply->result,
5478d2cc9bcdSJames Smart 		       bsg_reply->reply_payload_rcv_len);
5479d2cc9bcdSJames Smart 
5480d2cc9bcdSJames Smart 	return rc;
5481d2cc9bcdSJames Smart }
5482d2cc9bcdSJames Smart 
5483d2cc9bcdSJames Smart /**
5484d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_lwpd: Get log write position data
5485d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5486d2cc9bcdSJames Smart  *
5487d2cc9bcdSJames Smart  * Get Offset/Wrap count of the log message written
5488d2cc9bcdSJames Smart  * in host memory
5489d2cc9bcdSJames Smart  **/
5490d2cc9bcdSJames Smart static int
5491d2cc9bcdSJames Smart lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5492d2cc9bcdSJames Smart {
5493d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5494d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5495d2cc9bcdSJames Smart 	struct lpfc_bsg_get_ras_lwpd *ras_reply;
5496d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5497d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5498d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5499*191e2f74SJames Smart 	u32 *lwpd_ptr = NULL;
5500d2cc9bcdSJames Smart 	int rc = 0;
5501d2cc9bcdSJames Smart 
5502d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5503d2cc9bcdSJames Smart 	if (rc == -EACCES || rc == -EPERM)
5504d2cc9bcdSJames Smart 		goto ras_job_error;
5505d2cc9bcdSJames Smart 
5506d2cc9bcdSJames Smart 	if (job->request_len <
5507d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5508d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_ras_req)) {
5509d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5510d2cc9bcdSJames Smart 				"6183 Received RAS_LOG request "
5511d2cc9bcdSJames Smart 				"below minimum size\n");
5512d2cc9bcdSJames Smart 		rc = -EINVAL;
5513d2cc9bcdSJames Smart 		goto ras_job_error;
5514d2cc9bcdSJames Smart 	}
5515d2cc9bcdSJames Smart 
5516d2cc9bcdSJames Smart 	ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5517d2cc9bcdSJames Smart 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5518d2cc9bcdSJames Smart 
5519*191e2f74SJames Smart 	/* Get lwpd offset */
5520*191e2f74SJames Smart 	lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5521*191e2f74SJames Smart 	ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5522d2cc9bcdSJames Smart 
5523*191e2f74SJames Smart 	/* Get wrap count */
5524*191e2f74SJames Smart 	ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5525d2cc9bcdSJames Smart 
5526d2cc9bcdSJames Smart ras_job_error:
5527d2cc9bcdSJames Smart 	/* make error code available to userspace */
5528d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5529d2cc9bcdSJames Smart 
5530d2cc9bcdSJames Smart 	/* complete the job back to userspace */
5531d2cc9bcdSJames Smart 	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5532d2cc9bcdSJames Smart 
5533d2cc9bcdSJames Smart 	return rc;
5534d2cc9bcdSJames Smart }
5535d2cc9bcdSJames Smart 
5536d2cc9bcdSJames Smart /**
5537d2cc9bcdSJames Smart  * lpfc_bsg_get_ras_fwlog: Read FW log
5538d2cc9bcdSJames Smart  * @job: fc_bsg_job to handle
5539d2cc9bcdSJames Smart  *
5540d2cc9bcdSJames Smart  * Copy the FW log into the passed buffer.
5541d2cc9bcdSJames Smart  **/
5542d2cc9bcdSJames Smart static int
5543d2cc9bcdSJames Smart lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5544d2cc9bcdSJames Smart {
5545d2cc9bcdSJames Smart 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5546d2cc9bcdSJames Smart 	struct lpfc_vport *vport = shost_priv(shost);
5547d2cc9bcdSJames Smart 	struct lpfc_hba *phba = vport->phba;
5548d2cc9bcdSJames Smart 	struct fc_bsg_request *bsg_request = job->request;
5549d2cc9bcdSJames Smart 	struct fc_bsg_reply *bsg_reply = job->reply;
5550d2cc9bcdSJames Smart 	struct lpfc_bsg_get_fwlog_req *ras_req;
5551*191e2f74SJames Smart 	u32 rd_offset, rd_index, offset;
5552*191e2f74SJames Smart 	void *src, *fwlog_buff;
5553d2cc9bcdSJames Smart 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5554d2cc9bcdSJames Smart 	struct lpfc_dmabuf *dmabuf, *next;
5555d2cc9bcdSJames Smart 	int rc = 0;
5556d2cc9bcdSJames Smart 
5557d2cc9bcdSJames Smart 	ras_fwlog = &phba->ras_fwlog;
5558d2cc9bcdSJames Smart 
5559d2cc9bcdSJames Smart 	rc = lpfc_check_fwlog_support(phba);
5560d2cc9bcdSJames Smart 	if (rc == -EACCES || rc == -EPERM)
5561d2cc9bcdSJames Smart 		goto ras_job_error;
5562d2cc9bcdSJames Smart 
5563d2cc9bcdSJames Smart 	/* Logging to be stopped before reading */
5564d2cc9bcdSJames Smart 	if (ras_fwlog->ras_active == true) {
5565d2cc9bcdSJames Smart 		rc = -EINPROGRESS;
5566d2cc9bcdSJames Smart 		goto ras_job_error;
5567d2cc9bcdSJames Smart 	}
5568d2cc9bcdSJames Smart 
5569d2cc9bcdSJames Smart 	if (job->request_len <
5570d2cc9bcdSJames Smart 	    sizeof(struct fc_bsg_request) +
5571d2cc9bcdSJames Smart 	    sizeof(struct lpfc_bsg_get_fwlog_req)) {
5572d2cc9bcdSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5573d2cc9bcdSJames Smart 				"6184 Received RAS_LOG request "
5574d2cc9bcdSJames Smart 				"below minimum size\n");
5575d2cc9bcdSJames Smart 		rc = -EINVAL;
5576d2cc9bcdSJames Smart 		goto ras_job_error;
5577d2cc9bcdSJames Smart 	}
5578d2cc9bcdSJames Smart 
5579d2cc9bcdSJames Smart 	ras_req = (struct lpfc_bsg_get_fwlog_req *)
5580d2cc9bcdSJames Smart 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5581d2cc9bcdSJames Smart 	rd_offset = ras_req->read_offset;
5582d2cc9bcdSJames Smart 
5583d2cc9bcdSJames Smart 	/* Allocate memory to read fw log*/
5584d2cc9bcdSJames Smart 	fwlog_buff = vmalloc(ras_req->read_size);
5585d2cc9bcdSJames Smart 	if (!fwlog_buff) {
5586d2cc9bcdSJames Smart 		rc = -ENOMEM;
5587d2cc9bcdSJames Smart 		goto ras_job_error;
5588d2cc9bcdSJames Smart 	}
5589d2cc9bcdSJames Smart 
5590d2cc9bcdSJames Smart 	rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5591d2cc9bcdSJames Smart 	offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5592d2cc9bcdSJames Smart 
5593d2cc9bcdSJames Smart 	list_for_each_entry_safe(dmabuf, next,
5594d2cc9bcdSJames Smart 			      &ras_fwlog->fwlog_buff_list, list) {
5595d2cc9bcdSJames Smart 
5596d2cc9bcdSJames Smart 		if (dmabuf->buffer_tag < rd_index)
5597d2cc9bcdSJames Smart 			continue;
5598d2cc9bcdSJames Smart 
5599d2cc9bcdSJames Smart 		src = dmabuf->virt + offset;
5600*191e2f74SJames Smart 		memcpy(fwlog_buff, src, ras_req->read_size);
5601d2cc9bcdSJames Smart 		break;
5602d2cc9bcdSJames Smart 	}
5603d2cc9bcdSJames Smart 
5604d2cc9bcdSJames Smart 	bsg_reply->reply_payload_rcv_len =
5605d2cc9bcdSJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
5606d2cc9bcdSJames Smart 				    job->reply_payload.sg_cnt,
5607d2cc9bcdSJames Smart 				    fwlog_buff, ras_req->read_size);
5608d2cc9bcdSJames Smart 
5609d2cc9bcdSJames Smart 	vfree(fwlog_buff);
5610d2cc9bcdSJames Smart 
5611d2cc9bcdSJames Smart ras_job_error:
5612d2cc9bcdSJames Smart 	bsg_reply->result = rc;
5613d2cc9bcdSJames Smart 	bsg_job_done(job, bsg_reply->result, bsg_reply->reply_payload_rcv_len);
5614d2cc9bcdSJames Smart 
5615d2cc9bcdSJames Smart 	return rc;
5616d2cc9bcdSJames Smart }
5617d2cc9bcdSJames Smart 
5618d2cc9bcdSJames Smart 
5619d2cc9bcdSJames Smart /**
5620f1c3b0fcSJames Smart  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5621f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
56223b5dd52aSJames Smart  **/
5623f1c3b0fcSJames Smart static int
562475cc8cfcSJohannes Thumshirn lpfc_bsg_hst_vendor(struct bsg_job *job)
5625f1c3b0fcSJames Smart {
562601e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
562701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
562801e0e15cSJohannes Thumshirn 	int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
56294cc0e56eSJames Smart 	int rc;
5630f1c3b0fcSJames Smart 
5631f1c3b0fcSJames Smart 	switch (command) {
5632f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
56334cc0e56eSJames Smart 		rc = lpfc_bsg_hba_set_event(job);
5634f1c3b0fcSJames Smart 		break;
5635f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
56364cc0e56eSJames Smart 		rc = lpfc_bsg_hba_get_event(job);
5637f1c3b0fcSJames Smart 		break;
56383b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
56393b5dd52aSJames Smart 		rc = lpfc_bsg_send_mgmt_rsp(job);
56403b5dd52aSJames Smart 		break;
56413b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE:
56427ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_mode(job);
56433b5dd52aSJames Smart 		break;
56447ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE_END:
56457ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_mode_end(job);
56467ad20aa9SJames Smart 		break;
56477ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
56487ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_run(job);
56497ad20aa9SJames Smart 		break;
56507ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
56517ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_link_diag_test(job);
56523b5dd52aSJames Smart 		break;
56533b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
56543b5dd52aSJames Smart 		rc = lpfc_bsg_get_dfc_rev(job);
56553b5dd52aSJames Smart 		break;
56563b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_MBOX:
56573b5dd52aSJames Smart 		rc = lpfc_bsg_mbox_cmd(job);
56583b5dd52aSJames Smart 		break;
5659e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_CMD:
5660e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_DATA:
5661e2aed29fSJames Smart 		rc = lpfc_menlo_cmd(job);
5662e2aed29fSJames Smart 		break;
5663c691816eSJames Smart 	case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5664c691816eSJames Smart 		rc = lpfc_forced_link_speed(job);
5665c691816eSJames Smart 		break;
5666d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5667d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_lwpd(job);
5668d2cc9bcdSJames Smart 		break;
5669d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5670d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_fwlog(job);
5671d2cc9bcdSJames Smart 		break;
5672d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5673d2cc9bcdSJames Smart 		rc = lpfc_bsg_get_ras_config(job);
5674d2cc9bcdSJames Smart 		break;
5675d2cc9bcdSJames Smart 	case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5676d2cc9bcdSJames Smart 		rc = lpfc_bsg_set_ras_config(job);
5677d2cc9bcdSJames Smart 		break;
5678f1c3b0fcSJames Smart 	default:
56794cc0e56eSJames Smart 		rc = -EINVAL;
568001e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
56814cc0e56eSJames Smart 		/* make error code available to userspace */
568201e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
56834cc0e56eSJames Smart 		break;
5684f1c3b0fcSJames Smart 	}
56854cc0e56eSJames Smart 
56864cc0e56eSJames Smart 	return rc;
5687f1c3b0fcSJames Smart }
5688f1c3b0fcSJames Smart 
5689f1c3b0fcSJames Smart /**
5690f1c3b0fcSJames Smart  * lpfc_bsg_request - handle a bsg request from the FC transport
5691d2cc9bcdSJames Smart  * @job: bsg_job to handle
56923b5dd52aSJames Smart  **/
5693f1c3b0fcSJames Smart int
569475cc8cfcSJohannes Thumshirn lpfc_bsg_request(struct bsg_job *job)
5695f1c3b0fcSJames Smart {
569601e0e15cSJohannes Thumshirn 	struct fc_bsg_request *bsg_request = job->request;
569701e0e15cSJohannes Thumshirn 	struct fc_bsg_reply *bsg_reply = job->reply;
5698f1c3b0fcSJames Smart 	uint32_t msgcode;
56994cc0e56eSJames Smart 	int rc;
5700f1c3b0fcSJames Smart 
570101e0e15cSJohannes Thumshirn 	msgcode = bsg_request->msgcode;
5702f1c3b0fcSJames Smart 	switch (msgcode) {
5703f1c3b0fcSJames Smart 	case FC_BSG_HST_VENDOR:
5704f1c3b0fcSJames Smart 		rc = lpfc_bsg_hst_vendor(job);
5705f1c3b0fcSJames Smart 		break;
5706f1c3b0fcSJames Smart 	case FC_BSG_RPT_ELS:
5707f1c3b0fcSJames Smart 		rc = lpfc_bsg_rport_els(job);
5708f1c3b0fcSJames Smart 		break;
5709f1c3b0fcSJames Smart 	case FC_BSG_RPT_CT:
57104cc0e56eSJames Smart 		rc = lpfc_bsg_send_mgmt_cmd(job);
5711f1c3b0fcSJames Smart 		break;
5712f1c3b0fcSJames Smart 	default:
57134cc0e56eSJames Smart 		rc = -EINVAL;
571401e0e15cSJohannes Thumshirn 		bsg_reply->reply_payload_rcv_len = 0;
57154cc0e56eSJames Smart 		/* make error code available to userspace */
571601e0e15cSJohannes Thumshirn 		bsg_reply->result = rc;
5717f1c3b0fcSJames Smart 		break;
5718f1c3b0fcSJames Smart 	}
5719f1c3b0fcSJames Smart 
5720f1c3b0fcSJames Smart 	return rc;
5721f1c3b0fcSJames Smart }
5722f1c3b0fcSJames Smart 
5723f1c3b0fcSJames Smart /**
5724f1c3b0fcSJames Smart  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5725d2cc9bcdSJames Smart  * @job: bsg_job that has timed out
5726f1c3b0fcSJames Smart  *
5727f1c3b0fcSJames Smart  * This function just aborts the job's IOCB.  The aborted IOCB will return to
5728f1c3b0fcSJames Smart  * the waiting function which will handle passing the error back to userspace
57293b5dd52aSJames Smart  **/
5730f1c3b0fcSJames Smart int
573175cc8cfcSJohannes Thumshirn lpfc_bsg_timeout(struct bsg_job *job)
5732f1c3b0fcSJames Smart {
5733cd21c605SJohannes Thumshirn 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5734f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
57354cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocb;
5736895427bdSJames Smart 	struct lpfc_sli_ring *pring;
57374cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
57384cc0e56eSJames Smart 	unsigned long flags;
5739a33c4f7bSJames Smart 	int rc = 0;
5740a33c4f7bSJames Smart 	LIST_HEAD(completions);
5741a33c4f7bSJames Smart 	struct lpfc_iocbq *check_iocb, *next_iocb;
5742a33c4f7bSJames Smart 
5743895427bdSJames Smart 	pring = lpfc_phba_elsring(phba);
57441234a6d5SDick Kennedy 	if (unlikely(!pring))
57451234a6d5SDick Kennedy 		return -EIO;
5746895427bdSJames Smart 
5747a33c4f7bSJames Smart 	/* if job's driver data is NULL, the command completed or is in the
5748a33c4f7bSJames Smart 	 * the process of completing.  In this case, return status to request
5749a33c4f7bSJames Smart 	 * so the timeout is retried.  This avoids double completion issues
5750a33c4f7bSJames Smart 	 * and the request will be pulled off the timer queue when the
5751a33c4f7bSJames Smart 	 * command's completion handler executes.  Otherwise, prevent the
5752a33c4f7bSJames Smart 	 * command's completion handler from executing the job done callback
5753a33c4f7bSJames Smart 	 * and continue processing to abort the outstanding the command.
5754a33c4f7bSJames Smart 	 */
5755f1c3b0fcSJames Smart 
57564cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
57574cc0e56eSJames Smart 	dd_data = (struct bsg_job_data *)job->dd_data;
5758a33c4f7bSJames Smart 	if (dd_data) {
5759a33c4f7bSJames Smart 		dd_data->set_job = NULL;
5760a33c4f7bSJames Smart 		job->dd_data = NULL;
5761a33c4f7bSJames Smart 	} else {
57624cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5763a33c4f7bSJames Smart 		return -EAGAIN;
57644cc0e56eSJames Smart 	}
57654cc0e56eSJames Smart 
57664cc0e56eSJames Smart 	switch (dd_data->type) {
57674cc0e56eSJames Smart 	case TYPE_IOCB:
5768a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5769a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5770a33c4f7bSJames Smart 		 * Otherwise, call abort iotag
5771a33c4f7bSJames Smart 		 */
5772a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5773b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5774b5a9b2dfSJames Smart 
5775b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
5776b5a9b2dfSJames Smart 		/* make sure the I/O abort window is still open */
57771b8d11abSJames Smart 		if (!(cmdiocb->iocb_flag & LPFC_IO_CMD_OUTSTANDING)) {
5778b5a9b2dfSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, flags);
5779b5a9b2dfSJames Smart 			return -EAGAIN;
5780b5a9b2dfSJames Smart 		}
5781a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5782a33c4f7bSJames Smart 					 list) {
5783a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5784a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5785a33c4f7bSJames Smart 				break;
5786a33c4f7bSJames Smart 			}
5787a33c4f7bSJames Smart 		}
5788a33c4f7bSJames Smart 		if (list_empty(&completions))
5789f1c3b0fcSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5790b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
5791a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5792a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5793a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5794a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5795a33c4f7bSJames Smart 		}
57964cc0e56eSJames Smart 		break;
5797a33c4f7bSJames Smart 
57984cc0e56eSJames Smart 	case TYPE_EVT:
57994cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
58004cc0e56eSJames Smart 		break;
5801a33c4f7bSJames Smart 
58023b5dd52aSJames Smart 	case TYPE_MBOX:
5803a33c4f7bSJames Smart 		/* Update the ext buf ctx state if needed */
5804a33c4f7bSJames Smart 
58057ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
58067ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5807a33c4f7bSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
58083b5dd52aSJames Smart 		break;
5809e2aed29fSJames Smart 	case TYPE_MENLO:
5810a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5811a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5812a33c4f7bSJames Smart 		 * Otherwise, call abort iotag.
5813a33c4f7bSJames Smart 		 */
5814a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5815b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5816b5a9b2dfSJames Smart 
5817b5a9b2dfSJames Smart 		spin_lock_irqsave(&phba->hbalock, flags);
5818a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5819a33c4f7bSJames Smart 					 list) {
5820a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5821a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5822a33c4f7bSJames Smart 				break;
5823a33c4f7bSJames Smart 			}
5824a33c4f7bSJames Smart 		}
5825a33c4f7bSJames Smart 		if (list_empty(&completions))
5826e2aed29fSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5827b5a9b2dfSJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
5828a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5829a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5830a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5831a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5832a33c4f7bSJames Smart 		}
5833e2aed29fSJames Smart 		break;
58344cc0e56eSJames Smart 	default:
58354cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
58364cc0e56eSJames Smart 		break;
58374cc0e56eSJames Smart 	}
5838f1c3b0fcSJames Smart 
58394cc0e56eSJames Smart 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
58404cc0e56eSJames Smart 	 * otherwise an error message will be displayed on the console
58414cc0e56eSJames Smart 	 * so always return success (zero)
58424cc0e56eSJames Smart 	 */
5843a33c4f7bSJames Smart 	return rc;
5844f1c3b0fcSJames Smart }
5845