xref: /linux/drivers/scsi/lpfc/lpfc_bsg.c (revision d5ce53b7dd497f8e5a5e4bbc736312b34fe452bd)
1f1c3b0fcSJames Smart /*******************************************************************
2f1c3b0fcSJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3f1c3b0fcSJames Smart  * Fibre Channel Host Bus Adapters.                                *
4b99570ddSJames Smart  * Copyright (C) 2009-2012 Emulex.  All rights reserved.           *
5f1c3b0fcSJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
6f1c3b0fcSJames Smart  * www.emulex.com                                                  *
7f1c3b0fcSJames Smart  *                                                                 *
8f1c3b0fcSJames Smart  * This program is free software; you can redistribute it and/or   *
9f1c3b0fcSJames Smart  * modify it under the terms of version 2 of the GNU General       *
10f1c3b0fcSJames Smart  * Public License as published by the Free Software Foundation.    *
11f1c3b0fcSJames Smart  * This program is distributed in the hope that it will be useful. *
12f1c3b0fcSJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13f1c3b0fcSJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14f1c3b0fcSJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15f1c3b0fcSJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16f1c3b0fcSJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17f1c3b0fcSJames Smart  * more details, a copy of which can be found in the file COPYING  *
18f1c3b0fcSJames Smart  * included with this package.                                     *
19f1c3b0fcSJames Smart  *******************************************************************/
20f1c3b0fcSJames Smart 
21f1c3b0fcSJames Smart #include <linux/interrupt.h>
22f1c3b0fcSJames Smart #include <linux/mempool.h>
23f1c3b0fcSJames Smart #include <linux/pci.h>
245a0e3ad6STejun Heo #include <linux/slab.h>
25277e76f1SJames Smart #include <linux/delay.h>
267ad20aa9SJames Smart #include <linux/list.h>
27f1c3b0fcSJames Smart 
28f1c3b0fcSJames Smart #include <scsi/scsi.h>
29f1c3b0fcSJames Smart #include <scsi/scsi_host.h>
30f1c3b0fcSJames Smart #include <scsi/scsi_transport_fc.h>
31f1c3b0fcSJames Smart #include <scsi/scsi_bsg_fc.h>
326a9c52cfSJames Smart #include <scsi/fc/fc_fs.h>
33f1c3b0fcSJames Smart 
34f1c3b0fcSJames Smart #include "lpfc_hw4.h"
35f1c3b0fcSJames Smart #include "lpfc_hw.h"
36f1c3b0fcSJames Smart #include "lpfc_sli.h"
37f1c3b0fcSJames Smart #include "lpfc_sli4.h"
38f1c3b0fcSJames Smart #include "lpfc_nl.h"
394fede78fSJames Smart #include "lpfc_bsg.h"
40f1c3b0fcSJames Smart #include "lpfc_disc.h"
41f1c3b0fcSJames Smart #include "lpfc_scsi.h"
42f1c3b0fcSJames Smart #include "lpfc.h"
43f1c3b0fcSJames Smart #include "lpfc_logmsg.h"
44f1c3b0fcSJames Smart #include "lpfc_crtn.h"
45b76f2dc9SJames Smart #include "lpfc_debugfs.h"
46f1c3b0fcSJames Smart #include "lpfc_vport.h"
47f1c3b0fcSJames Smart #include "lpfc_version.h"
48f1c3b0fcSJames Smart 
494cc0e56eSJames Smart struct lpfc_bsg_event {
504cc0e56eSJames Smart 	struct list_head node;
514cc0e56eSJames Smart 	struct kref kref;
524cc0e56eSJames Smart 	wait_queue_head_t wq;
534cc0e56eSJames Smart 
544cc0e56eSJames Smart 	/* Event type and waiter identifiers */
554cc0e56eSJames Smart 	uint32_t type_mask;
564cc0e56eSJames Smart 	uint32_t req_id;
574cc0e56eSJames Smart 	uint32_t reg_id;
584cc0e56eSJames Smart 
594cc0e56eSJames Smart 	/* next two flags are here for the auto-delete logic */
604cc0e56eSJames Smart 	unsigned long wait_time_stamp;
614cc0e56eSJames Smart 	int waiting;
624cc0e56eSJames Smart 
634cc0e56eSJames Smart 	/* seen and not seen events */
644cc0e56eSJames Smart 	struct list_head events_to_get;
654cc0e56eSJames Smart 	struct list_head events_to_see;
664cc0e56eSJames Smart 
67a33c4f7bSJames Smart 	/* driver data associated with the job */
68a33c4f7bSJames Smart 	void *dd_data;
694cc0e56eSJames Smart };
704cc0e56eSJames Smart 
714cc0e56eSJames Smart struct lpfc_bsg_iocb {
724cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocbq;
73a33c4f7bSJames Smart 	struct lpfc_dmabuf *rmp;
744cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
754cc0e56eSJames Smart };
764cc0e56eSJames Smart 
773b5dd52aSJames Smart struct lpfc_bsg_mbox {
783b5dd52aSJames Smart 	LPFC_MBOXQ_t *pmboxq;
793b5dd52aSJames Smart 	MAILBOX_t *mb;
807ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
817a470277SJames Smart 	uint8_t *ext; /* extended mailbox data */
827a470277SJames Smart 	uint32_t mbOffset; /* from app */
837a470277SJames Smart 	uint32_t inExtWLen; /* from app */
84c7495937SJames Smart 	uint32_t outExtWLen; /* from app */
853b5dd52aSJames Smart };
863b5dd52aSJames Smart 
87e2aed29fSJames Smart #define MENLO_DID 0x0000FC0E
88e2aed29fSJames Smart 
89e2aed29fSJames Smart struct lpfc_bsg_menlo {
90e2aed29fSJames Smart 	struct lpfc_iocbq *cmdiocbq;
91a33c4f7bSJames Smart 	struct lpfc_dmabuf *rmp;
92e2aed29fSJames Smart };
93e2aed29fSJames Smart 
944cc0e56eSJames Smart #define TYPE_EVT 	1
954cc0e56eSJames Smart #define TYPE_IOCB	2
963b5dd52aSJames Smart #define TYPE_MBOX	3
97e2aed29fSJames Smart #define TYPE_MENLO	4
984cc0e56eSJames Smart struct bsg_job_data {
994cc0e56eSJames Smart 	uint32_t type;
100a33c4f7bSJames Smart 	struct fc_bsg_job *set_job; /* job waiting for this iocb to finish */
1014cc0e56eSJames Smart 	union {
1024cc0e56eSJames Smart 		struct lpfc_bsg_event *evt;
1034cc0e56eSJames Smart 		struct lpfc_bsg_iocb iocb;
1043b5dd52aSJames Smart 		struct lpfc_bsg_mbox mbox;
105e2aed29fSJames Smart 		struct lpfc_bsg_menlo menlo;
1064cc0e56eSJames Smart 	} context_un;
1074cc0e56eSJames Smart };
1084cc0e56eSJames Smart 
1094cc0e56eSJames Smart struct event_data {
1104cc0e56eSJames Smart 	struct list_head node;
1114cc0e56eSJames Smart 	uint32_t type;
1124cc0e56eSJames Smart 	uint32_t immed_dat;
1134cc0e56eSJames Smart 	void *data;
1144cc0e56eSJames Smart 	uint32_t len;
1154cc0e56eSJames Smart };
1164cc0e56eSJames Smart 
1173b5dd52aSJames Smart #define BUF_SZ_4K 4096
1184cc0e56eSJames Smart #define SLI_CT_ELX_LOOPBACK 0x10
1194cc0e56eSJames Smart 
1204cc0e56eSJames Smart enum ELX_LOOPBACK_CMD {
1214cc0e56eSJames Smart 	ELX_LOOPBACK_XRI_SETUP,
1224cc0e56eSJames Smart 	ELX_LOOPBACK_DATA,
1234cc0e56eSJames Smart };
1244cc0e56eSJames Smart 
1253b5dd52aSJames Smart #define ELX_LOOPBACK_HEADER_SZ \
1263b5dd52aSJames Smart 	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
1273b5dd52aSJames Smart 
1284cc0e56eSJames Smart struct lpfc_dmabufext {
1294cc0e56eSJames Smart 	struct lpfc_dmabuf dma;
1304cc0e56eSJames Smart 	uint32_t size;
1314cc0e56eSJames Smart 	uint32_t flag;
1324cc0e56eSJames Smart };
1334cc0e56eSJames Smart 
134a33c4f7bSJames Smart static void
135a33c4f7bSJames Smart lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
136a33c4f7bSJames Smart {
137a33c4f7bSJames Smart 	struct lpfc_dmabuf *mlast, *next_mlast;
138a33c4f7bSJames Smart 
139a33c4f7bSJames Smart 	if (mlist) {
140a33c4f7bSJames Smart 		list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
141a33c4f7bSJames Smart 					 list) {
142a33c4f7bSJames Smart 			lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
143a33c4f7bSJames Smart 			list_del(&mlast->list);
144a33c4f7bSJames Smart 			kfree(mlast);
145a33c4f7bSJames Smart 		}
146a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
147a33c4f7bSJames Smart 		kfree(mlist);
148a33c4f7bSJames Smart 	}
149a33c4f7bSJames Smart 	return;
150a33c4f7bSJames Smart }
151a33c4f7bSJames Smart 
152a33c4f7bSJames Smart static struct lpfc_dmabuf *
153a33c4f7bSJames Smart lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
154a33c4f7bSJames Smart 		       int outbound_buffers, struct ulp_bde64 *bpl,
155a33c4f7bSJames Smart 		       int *bpl_entries)
156a33c4f7bSJames Smart {
157a33c4f7bSJames Smart 	struct lpfc_dmabuf *mlist = NULL;
158a33c4f7bSJames Smart 	struct lpfc_dmabuf *mp;
159a33c4f7bSJames Smart 	unsigned int bytes_left = size;
160a33c4f7bSJames Smart 
161a33c4f7bSJames Smart 	/* Verify we can support the size specified */
162a33c4f7bSJames Smart 	if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
163a33c4f7bSJames Smart 		return NULL;
164a33c4f7bSJames Smart 
165a33c4f7bSJames Smart 	/* Determine the number of dma buffers to allocate */
166a33c4f7bSJames Smart 	*bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
167a33c4f7bSJames Smart 			size/LPFC_BPL_SIZE);
168a33c4f7bSJames Smart 
169a33c4f7bSJames Smart 	/* Allocate dma buffer and place in BPL passed */
170a33c4f7bSJames Smart 	while (bytes_left) {
171a33c4f7bSJames Smart 		/* Allocate dma buffer  */
172a33c4f7bSJames Smart 		mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
173a33c4f7bSJames Smart 		if (!mp) {
174a33c4f7bSJames Smart 			if (mlist)
175a33c4f7bSJames Smart 				lpfc_free_bsg_buffers(phba, mlist);
176a33c4f7bSJames Smart 			return NULL;
177a33c4f7bSJames Smart 		}
178a33c4f7bSJames Smart 
179a33c4f7bSJames Smart 		INIT_LIST_HEAD(&mp->list);
180a33c4f7bSJames Smart 		mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
181a33c4f7bSJames Smart 
182a33c4f7bSJames Smart 		if (!mp->virt) {
183a33c4f7bSJames Smart 			kfree(mp);
184a33c4f7bSJames Smart 			if (mlist)
185a33c4f7bSJames Smart 				lpfc_free_bsg_buffers(phba, mlist);
186a33c4f7bSJames Smart 			return NULL;
187a33c4f7bSJames Smart 		}
188a33c4f7bSJames Smart 
189a33c4f7bSJames Smart 		/* Queue it to a linked list */
190a33c4f7bSJames Smart 		if (!mlist)
191a33c4f7bSJames Smart 			mlist = mp;
192a33c4f7bSJames Smart 		else
193a33c4f7bSJames Smart 			list_add_tail(&mp->list, &mlist->list);
194a33c4f7bSJames Smart 
195a33c4f7bSJames Smart 		/* Add buffer to buffer pointer list */
196a33c4f7bSJames Smart 		if (outbound_buffers)
197a33c4f7bSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
198a33c4f7bSJames Smart 		else
199a33c4f7bSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
200a33c4f7bSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
201a33c4f7bSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
202a33c4f7bSJames Smart 		bpl->tus.f.bdeSize = (uint16_t)
203a33c4f7bSJames Smart 			(bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
204a33c4f7bSJames Smart 			 bytes_left);
205a33c4f7bSJames Smart 		bytes_left -= bpl->tus.f.bdeSize;
206a33c4f7bSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
207a33c4f7bSJames Smart 		bpl++;
208a33c4f7bSJames Smart 	}
209a33c4f7bSJames Smart 	return mlist;
210a33c4f7bSJames Smart }
211a33c4f7bSJames Smart 
212a33c4f7bSJames Smart static unsigned int
213a33c4f7bSJames Smart lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
214a33c4f7bSJames Smart 		   struct fc_bsg_buffer *bsg_buffers,
215a33c4f7bSJames Smart 		   unsigned int bytes_to_transfer, int to_buffers)
216a33c4f7bSJames Smart {
217a33c4f7bSJames Smart 
218a33c4f7bSJames Smart 	struct lpfc_dmabuf *mp;
219a33c4f7bSJames Smart 	unsigned int transfer_bytes, bytes_copied = 0;
220a33c4f7bSJames Smart 	unsigned int sg_offset, dma_offset;
221a33c4f7bSJames Smart 	unsigned char *dma_address, *sg_address;
222a33c4f7bSJames Smart 	LIST_HEAD(temp_list);
223*d5ce53b7SJames Smart 	struct sg_mapping_iter miter;
224*d5ce53b7SJames Smart 	unsigned long flags;
225*d5ce53b7SJames Smart 	unsigned int sg_flags = SG_MITER_ATOMIC;
226*d5ce53b7SJames Smart 	bool sg_valid;
227a33c4f7bSJames Smart 
228a33c4f7bSJames Smart 	list_splice_init(&dma_buffers->list, &temp_list);
229a33c4f7bSJames Smart 	list_add(&dma_buffers->list, &temp_list);
230a33c4f7bSJames Smart 	sg_offset = 0;
231*d5ce53b7SJames Smart 	if (to_buffers)
232*d5ce53b7SJames Smart 		sg_flags |= SG_MITER_FROM_SG;
233*d5ce53b7SJames Smart 	else
234*d5ce53b7SJames Smart 		sg_flags |= SG_MITER_TO_SG;
235*d5ce53b7SJames Smart 	sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
236*d5ce53b7SJames Smart 		       sg_flags);
237*d5ce53b7SJames Smart 	local_irq_save(flags);
238*d5ce53b7SJames Smart 	sg_valid = sg_miter_next(&miter);
239a33c4f7bSJames Smart 	list_for_each_entry(mp, &temp_list, list) {
240a33c4f7bSJames Smart 		dma_offset = 0;
241*d5ce53b7SJames Smart 		while (bytes_to_transfer && sg_valid &&
242a33c4f7bSJames Smart 		       (dma_offset < LPFC_BPL_SIZE)) {
243a33c4f7bSJames Smart 			dma_address = mp->virt + dma_offset;
244a33c4f7bSJames Smart 			if (sg_offset) {
245a33c4f7bSJames Smart 				/* Continue previous partial transfer of sg */
246*d5ce53b7SJames Smart 				sg_address = miter.addr + sg_offset;
247*d5ce53b7SJames Smart 				transfer_bytes = miter.length - sg_offset;
248a33c4f7bSJames Smart 			} else {
249*d5ce53b7SJames Smart 				sg_address = miter.addr;
250*d5ce53b7SJames Smart 				transfer_bytes = miter.length;
251a33c4f7bSJames Smart 			}
252a33c4f7bSJames Smart 			if (bytes_to_transfer < transfer_bytes)
253a33c4f7bSJames Smart 				transfer_bytes = bytes_to_transfer;
254a33c4f7bSJames Smart 			if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
255a33c4f7bSJames Smart 				transfer_bytes = LPFC_BPL_SIZE - dma_offset;
256a33c4f7bSJames Smart 			if (to_buffers)
257a33c4f7bSJames Smart 				memcpy(dma_address, sg_address, transfer_bytes);
258a33c4f7bSJames Smart 			else
259a33c4f7bSJames Smart 				memcpy(sg_address, dma_address, transfer_bytes);
260a33c4f7bSJames Smart 			dma_offset += transfer_bytes;
261a33c4f7bSJames Smart 			sg_offset += transfer_bytes;
262a33c4f7bSJames Smart 			bytes_to_transfer -= transfer_bytes;
263a33c4f7bSJames Smart 			bytes_copied += transfer_bytes;
264*d5ce53b7SJames Smart 			if (sg_offset >= miter.length) {
265a33c4f7bSJames Smart 				sg_offset = 0;
266*d5ce53b7SJames Smart 				sg_valid = sg_miter_next(&miter);
267a33c4f7bSJames Smart 			}
268a33c4f7bSJames Smart 		}
269a33c4f7bSJames Smart 	}
270*d5ce53b7SJames Smart 	sg_miter_stop(&miter);
271*d5ce53b7SJames Smart 	local_irq_restore(flags);
272a33c4f7bSJames Smart 	list_del_init(&dma_buffers->list);
273a33c4f7bSJames Smart 	list_splice(&temp_list, &dma_buffers->list);
274a33c4f7bSJames Smart 	return bytes_copied;
275a33c4f7bSJames Smart }
276a33c4f7bSJames Smart 
277f1c3b0fcSJames Smart /**
2784cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
2794cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
2804cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
2814cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
2824cc0e56eSJames Smart  *
2834cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
2844cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
2854cc0e56eSJames Smart  * ring event handler function without any lock held. This function
2864cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
2874cc0e56eSJames Smart  * context. This function also can be called from another thread which
2884cc0e56eSJames Smart  * cleans up the SLI layer objects.
2894cc0e56eSJames Smart  * This function copies the contents of the response iocb to the
2904cc0e56eSJames Smart  * response iocb memory object provided by the caller of
2914cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
2924cc0e56eSJames Smart  * sleeps for the iocb completion.
2934cc0e56eSJames Smart  **/
2944cc0e56eSJames Smart static void
2954cc0e56eSJames Smart lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
2964cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
2974cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
2984cc0e56eSJames Smart {
2994cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
3004cc0e56eSJames Smart 	struct fc_bsg_job *job;
3014cc0e56eSJames Smart 	IOCB_t *rsp;
302a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
3034cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
3044cc0e56eSJames Smart 	struct lpfc_bsg_iocb *iocb;
3054cc0e56eSJames Smart 	unsigned long flags;
306a33c4f7bSJames Smart 	unsigned int rsp_size;
3074cc0e56eSJames Smart 	int rc = 0;
3084cc0e56eSJames Smart 
309a33c4f7bSJames Smart 	dd_data = cmdiocbq->context1;
310a33c4f7bSJames Smart 
311a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3124cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
313a33c4f7bSJames Smart 	job = dd_data->set_job;
314a33c4f7bSJames Smart 	if (job) {
315a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job */
316a33c4f7bSJames Smart 		job->dd_data = NULL;
3174cc0e56eSJames Smart 	}
318a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3194cc0e56eSJames Smart 
3204cc0e56eSJames Smart 	iocb = &dd_data->context_un.iocb;
321a33c4f7bSJames Smart 	ndlp = iocb->ndlp;
322a33c4f7bSJames Smart 	rmp = iocb->rmp;
323a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
324a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
3254cc0e56eSJames Smart 	rsp = &rspiocbq->iocb;
3264cc0e56eSJames Smart 
327a33c4f7bSJames Smart 	/* Copy the completed data or set the error status */
3284cc0e56eSJames Smart 
329a33c4f7bSJames Smart 	if (job) {
3304cc0e56eSJames Smart 		if (rsp->ulpStatus) {
3314cc0e56eSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
332e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
3334cc0e56eSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
3344cc0e56eSJames Smart 					rc = -ETIMEDOUT;
3354cc0e56eSJames Smart 					break;
3364cc0e56eSJames Smart 				case IOERR_INVALID_RPI:
3374cc0e56eSJames Smart 					rc = -EFAULT;
3384cc0e56eSJames Smart 					break;
3394cc0e56eSJames Smart 				default:
3404cc0e56eSJames Smart 					rc = -EACCES;
3414cc0e56eSJames Smart 					break;
3424cc0e56eSJames Smart 				}
343a33c4f7bSJames Smart 			} else {
3444cc0e56eSJames Smart 				rc = -EACCES;
345a33c4f7bSJames Smart 			}
346a33c4f7bSJames Smart 		} else {
347a33c4f7bSJames Smart 			rsp_size = rsp->un.genreq64.bdl.bdeSize;
3484cc0e56eSJames Smart 			job->reply->reply_payload_rcv_len =
349a33c4f7bSJames Smart 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
350a33c4f7bSJames Smart 						   rsp_size, 0);
351a33c4f7bSJames Smart 		}
352a33c4f7bSJames Smart 	}
3534cc0e56eSJames Smart 
354a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
355a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
3564cc0e56eSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
357a33c4f7bSJames Smart 	kfree(bmp);
3584cc0e56eSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
3594cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
3604cc0e56eSJames Smart 	kfree(dd_data);
361a33c4f7bSJames Smart 
362a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
363a33c4f7bSJames Smart 
364a33c4f7bSJames Smart 	if (job) {
3654cc0e56eSJames Smart 		job->reply->result = rc;
3664cc0e56eSJames Smart 		job->job_done(job);
367a33c4f7bSJames Smart 	}
3684cc0e56eSJames Smart 	return;
3694cc0e56eSJames Smart }
3704cc0e56eSJames Smart 
3714cc0e56eSJames Smart /**
3724cc0e56eSJames Smart  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
373f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
3743b5dd52aSJames Smart  **/
375f1c3b0fcSJames Smart static int
3764cc0e56eSJames Smart lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
377f1c3b0fcSJames Smart {
378f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
379f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
380f1c3b0fcSJames Smart 	struct lpfc_rport_data *rdata = job->rport->dd_data;
381f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
382f1c3b0fcSJames Smart 	struct ulp_bde64 *bpl = NULL;
383f1c3b0fcSJames Smart 	uint32_t timeout;
384f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq = NULL;
385f1c3b0fcSJames Smart 	IOCB_t *cmd;
386a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
387f1c3b0fcSJames Smart 	int request_nseg;
388f1c3b0fcSJames Smart 	int reply_nseg;
3894cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
3904cc0e56eSJames Smart 	uint32_t creg_val;
391f1c3b0fcSJames Smart 	int rc = 0;
392d439d286SJames Smart 	int iocb_stat;
393f1c3b0fcSJames Smart 
394f1c3b0fcSJames Smart 	/* in case no data is transferred */
395f1c3b0fcSJames Smart 	job->reply->reply_payload_rcv_len = 0;
396f1c3b0fcSJames Smart 
3974cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
3984cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3994cc0e56eSJames Smart 	if (!dd_data) {
4004cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4014cc0e56eSJames Smart 				"2733 Failed allocation of dd_data\n");
4024cc0e56eSJames Smart 		rc = -ENOMEM;
4034cc0e56eSJames Smart 		goto no_dd_data;
4044cc0e56eSJames Smart 	}
4054cc0e56eSJames Smart 
406f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
4074cc0e56eSJames Smart 		rc = -ENODEV;
4084cc0e56eSJames Smart 		goto no_ndlp;
4094cc0e56eSJames Smart 	}
4104cc0e56eSJames Smart 
411f1c3b0fcSJames Smart 	if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
412f1c3b0fcSJames Smart 		rc = -ENODEV;
413a33c4f7bSJames Smart 		goto free_ndlp;
414f1c3b0fcSJames Smart 	}
415f1c3b0fcSJames Smart 
416f1c3b0fcSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
417f1c3b0fcSJames Smart 	if (!cmdiocbq) {
418f1c3b0fcSJames Smart 		rc = -ENOMEM;
419a33c4f7bSJames Smart 		goto free_ndlp;
420f1c3b0fcSJames Smart 	}
421f1c3b0fcSJames Smart 
4224cc0e56eSJames Smart 	cmd = &cmdiocbq->iocb;
423a33c4f7bSJames Smart 
424a33c4f7bSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
425a33c4f7bSJames Smart 	if (!bmp) {
426f1c3b0fcSJames Smart 		rc = -ENOMEM;
427be858b65SJames Smart 		goto free_cmdiocbq;
428f1c3b0fcSJames Smart 	}
429a33c4f7bSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
430a33c4f7bSJames Smart 	if (!bmp->virt) {
431a33c4f7bSJames Smart 		rc = -ENOMEM;
432a33c4f7bSJames Smart 		goto free_bmp;
433f1c3b0fcSJames Smart 	}
434f1c3b0fcSJames Smart 
435a33c4f7bSJames Smart 	INIT_LIST_HEAD(&bmp->list);
436a33c4f7bSJames Smart 
437a33c4f7bSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
438a33c4f7bSJames Smart 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
439a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
440a33c4f7bSJames Smart 				     1, bpl, &request_nseg);
441a33c4f7bSJames Smart 	if (!cmp) {
442a33c4f7bSJames Smart 		rc = -ENOMEM;
443a33c4f7bSJames Smart 		goto free_bmp;
444a33c4f7bSJames Smart 	}
445a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
446a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
447a33c4f7bSJames Smart 
448a33c4f7bSJames Smart 	bpl += request_nseg;
449a33c4f7bSJames Smart 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
450a33c4f7bSJames Smart 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
451a33c4f7bSJames Smart 				     bpl, &reply_nseg);
452a33c4f7bSJames Smart 	if (!rmp) {
453a33c4f7bSJames Smart 		rc = -ENOMEM;
454a33c4f7bSJames Smart 		goto free_cmp;
455f1c3b0fcSJames Smart 	}
456f1c3b0fcSJames Smart 
457f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
458f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
459f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
460f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
461f1c3b0fcSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
462f1c3b0fcSJames Smart 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
463f1c3b0fcSJames Smart 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
464f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
465f1c3b0fcSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
4666a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
4676a9c52cfSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
468f1c3b0fcSJames Smart 	cmd->ulpBdeCount = 1;
469f1c3b0fcSJames Smart 	cmd->ulpLe = 1;
470f1c3b0fcSJames Smart 	cmd->ulpClass = CLASS3;
471f1c3b0fcSJames Smart 	cmd->ulpContext = ndlp->nlp_rpi;
4726d368e53SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
4736d368e53SJames Smart 		cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
474f1c3b0fcSJames Smart 	cmd->ulpOwner = OWN_CHIP;
475f1c3b0fcSJames Smart 	cmdiocbq->vport = phba->pport;
4764cc0e56eSJames Smart 	cmdiocbq->context3 = bmp;
477f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
478f1c3b0fcSJames Smart 	timeout = phba->fc_ratov * 2;
4794cc0e56eSJames Smart 	cmd->ulpTimeout = timeout;
480f1c3b0fcSJames Smart 
4814cc0e56eSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
482a33c4f7bSJames Smart 	cmdiocbq->context1 = dd_data;
483a33c4f7bSJames Smart 	cmdiocbq->context2 = cmp;
484a33c4f7bSJames Smart 	cmdiocbq->context3 = bmp;
485*d5ce53b7SJames Smart 	cmdiocbq->context_un.ndlp = ndlp;
4864cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
487a33c4f7bSJames Smart 	dd_data->set_job = job;
4884cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
489a33c4f7bSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
490a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = rmp;
491a33c4f7bSJames Smart 	job->dd_data = dd_data;
492f1c3b0fcSJames Smart 
4934cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4949940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
4959940b97bSJames Smart 			rc = -EIO ;
496a33c4f7bSJames Smart 			goto free_rmp;
4979940b97bSJames Smart 		}
4984cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4994cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
5004cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
5014cc0e56eSJames Smart 	}
5024cc0e56eSJames Smart 
503d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
504d439d286SJames Smart 	if (iocb_stat == IOCB_SUCCESS)
5054cc0e56eSJames Smart 		return 0; /* done for now */
506d439d286SJames Smart 	else if (iocb_stat == IOCB_BUSY)
507d439d286SJames Smart 		rc = -EAGAIN;
5082a9bf3d0SJames Smart 	else
509d439d286SJames Smart 		rc = -EIO;
5102a9bf3d0SJames Smart 
5114cc0e56eSJames Smart 	/* iocb failed so cleanup */
512f1c3b0fcSJames Smart 
513a33c4f7bSJames Smart free_rmp:
514a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
515a33c4f7bSJames Smart free_cmp:
516a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
517a33c4f7bSJames Smart free_bmp:
518a33c4f7bSJames Smart 	if (bmp->virt)
519f1c3b0fcSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
520a33c4f7bSJames Smart 	kfree(bmp);
521f1c3b0fcSJames Smart free_cmdiocbq:
522f1c3b0fcSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5234cc0e56eSJames Smart free_ndlp:
524f1c3b0fcSJames Smart 	lpfc_nlp_put(ndlp);
5254cc0e56eSJames Smart no_ndlp:
5264cc0e56eSJames Smart 	kfree(dd_data);
5274cc0e56eSJames Smart no_dd_data:
528f1c3b0fcSJames Smart 	/* make error code available to userspace */
529f1c3b0fcSJames Smart 	job->reply->result = rc;
5304cc0e56eSJames Smart 	job->dd_data = NULL;
5314cc0e56eSJames Smart 	return rc;
5324cc0e56eSJames Smart }
5334cc0e56eSJames Smart 
5344cc0e56eSJames Smart /**
5354cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
5364cc0e56eSJames Smart  * @phba: Pointer to HBA context object.
5374cc0e56eSJames Smart  * @cmdiocbq: Pointer to command iocb.
5384cc0e56eSJames Smart  * @rspiocbq: Pointer to response iocb.
5394cc0e56eSJames Smart  *
5404cc0e56eSJames Smart  * This function is the completion handler for iocbs issued using
5414cc0e56eSJames Smart  * lpfc_bsg_rport_els_cmp function. This function is called by the
5424cc0e56eSJames Smart  * ring event handler function without any lock held. This function
5434cc0e56eSJames Smart  * can be called from both worker thread context and interrupt
5444cc0e56eSJames Smart  * context. This function also can be called from other thread which
5454cc0e56eSJames Smart  * cleans up the SLI layer objects.
5463b5dd52aSJames Smart  * This function copies the contents of the response iocb to the
5474cc0e56eSJames Smart  * response iocb memory object provided by the caller of
5484cc0e56eSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
5494cc0e56eSJames Smart  * sleeps for the iocb completion.
5504cc0e56eSJames Smart  **/
5514cc0e56eSJames Smart static void
5524cc0e56eSJames Smart lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
5534cc0e56eSJames Smart 			struct lpfc_iocbq *cmdiocbq,
5544cc0e56eSJames Smart 			struct lpfc_iocbq *rspiocbq)
5554cc0e56eSJames Smart {
5564cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
5574cc0e56eSJames Smart 	struct fc_bsg_job *job;
5584cc0e56eSJames Smart 	IOCB_t *rsp;
5594cc0e56eSJames Smart 	struct lpfc_nodelist *ndlp;
560a33c4f7bSJames Smart 	struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
5614cc0e56eSJames Smart 	struct fc_bsg_ctels_reply *els_reply;
5624cc0e56eSJames Smart 	uint8_t *rjt_data;
5634cc0e56eSJames Smart 	unsigned long flags;
564a33c4f7bSJames Smart 	unsigned int rsp_size;
5654cc0e56eSJames Smart 	int rc = 0;
5664cc0e56eSJames Smart 
5674cc0e56eSJames Smart 	dd_data = cmdiocbq->context1;
5684cc0e56eSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
569a33c4f7bSJames Smart 	cmdiocbq->context1 = ndlp;
5704cc0e56eSJames Smart 
571a33c4f7bSJames Smart 	/* Determine if job has been aborted */
572a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
573a33c4f7bSJames Smart 	job = dd_data->set_job;
574a33c4f7bSJames Smart 	if (job) {
575a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
576a33c4f7bSJames Smart 		job->dd_data = NULL;
577a33c4f7bSJames Smart 	}
578a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5794cc0e56eSJames Smart 
580a33c4f7bSJames Smart 	rsp = &rspiocbq->iocb;
581a33c4f7bSJames Smart 	pcmd = (struct lpfc_dmabuf *)cmdiocbq->context2;
582a33c4f7bSJames Smart 	prsp = (struct lpfc_dmabuf *)pcmd->list.next;
583a33c4f7bSJames Smart 
584a33c4f7bSJames Smart 	/* Copy the completed job data or determine the job status if job is
585a33c4f7bSJames Smart 	 * still active
586a33c4f7bSJames Smart 	 */
587a33c4f7bSJames Smart 
588a33c4f7bSJames Smart 	if (job) {
589a33c4f7bSJames Smart 		if (rsp->ulpStatus == IOSTAT_SUCCESS) {
590a33c4f7bSJames Smart 			rsp_size = rsp->un.elsreq64.bdl.bdeSize;
5914cc0e56eSJames Smart 			job->reply->reply_payload_rcv_len =
592a33c4f7bSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
593a33c4f7bSJames Smart 						    job->reply_payload.sg_cnt,
594a33c4f7bSJames Smart 						    prsp->virt,
595a33c4f7bSJames Smart 						    rsp_size);
596a33c4f7bSJames Smart 		} else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
5974cc0e56eSJames Smart 			job->reply->reply_payload_rcv_len =
5984cc0e56eSJames Smart 				sizeof(struct fc_bsg_ctels_reply);
5994cc0e56eSJames Smart 			/* LS_RJT data returned in word 4 */
6004cc0e56eSJames Smart 			rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
6014cc0e56eSJames Smart 			els_reply = &job->reply->reply_data.ctels_reply;
6024cc0e56eSJames Smart 			els_reply->status = FC_CTELS_STATUS_REJECT;
6034cc0e56eSJames Smart 			els_reply->rjt_data.action = rjt_data[3];
6044cc0e56eSJames Smart 			els_reply->rjt_data.reason_code = rjt_data[2];
6054cc0e56eSJames Smart 			els_reply->rjt_data.reason_explanation = rjt_data[1];
6064cc0e56eSJames Smart 			els_reply->rjt_data.vendor_unique = rjt_data[0];
607a33c4f7bSJames Smart 		} else {
6084cc0e56eSJames Smart 			rc = -EIO;
609a33c4f7bSJames Smart 		}
610a33c4f7bSJames Smart 	}
6114cc0e56eSJames Smart 
6124cc0e56eSJames Smart 	lpfc_nlp_put(ndlp);
613a33c4f7bSJames Smart 	lpfc_els_free_iocb(phba, cmdiocbq);
6144cc0e56eSJames Smart 	kfree(dd_data);
615a33c4f7bSJames Smart 
616a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
617a33c4f7bSJames Smart 
618a33c4f7bSJames Smart 	if (job) {
6194cc0e56eSJames Smart 		job->reply->result = rc;
620f1c3b0fcSJames Smart 		job->job_done(job);
621a33c4f7bSJames Smart 	}
6224cc0e56eSJames Smart 	return;
623f1c3b0fcSJames Smart }
624f1c3b0fcSJames Smart 
625f1c3b0fcSJames Smart /**
626f1c3b0fcSJames Smart  * lpfc_bsg_rport_els - send an ELS command from a bsg request
627f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
6283b5dd52aSJames Smart  **/
629f1c3b0fcSJames Smart static int
630f1c3b0fcSJames Smart lpfc_bsg_rport_els(struct fc_bsg_job *job)
631f1c3b0fcSJames Smart {
632f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
633f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
634f1c3b0fcSJames Smart 	struct lpfc_rport_data *rdata = job->rport->dd_data;
635f1c3b0fcSJames Smart 	struct lpfc_nodelist *ndlp = rdata->pnode;
636f1c3b0fcSJames Smart 	uint32_t elscmd;
637f1c3b0fcSJames Smart 	uint32_t cmdsize;
638f1c3b0fcSJames Smart 	uint32_t rspsize;
639f1c3b0fcSJames Smart 	struct lpfc_iocbq *cmdiocbq;
640f1c3b0fcSJames Smart 	uint16_t rpi = 0;
6414cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
6424cc0e56eSJames Smart 	uint32_t creg_val;
643f1c3b0fcSJames Smart 	int rc = 0;
644f1c3b0fcSJames Smart 
645f1c3b0fcSJames Smart 	/* in case no data is transferred */
646f1c3b0fcSJames Smart 	job->reply->reply_payload_rcv_len = 0;
647f1c3b0fcSJames Smart 
648a33c4f7bSJames Smart 	/* verify the els command is not greater than the
649a33c4f7bSJames Smart 	 * maximum ELS transfer size.
650a33c4f7bSJames Smart 	 */
651a33c4f7bSJames Smart 
652a33c4f7bSJames Smart 	if (job->request_payload.payload_len > FCELSSIZE) {
653a33c4f7bSJames Smart 		rc = -EINVAL;
654a33c4f7bSJames Smart 		goto no_dd_data;
655a33c4f7bSJames Smart 	}
656a33c4f7bSJames Smart 
6574cc0e56eSJames Smart 	/* allocate our bsg tracking structure */
6584cc0e56eSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
6594cc0e56eSJames Smart 	if (!dd_data) {
6604cc0e56eSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
6614cc0e56eSJames Smart 				"2735 Failed allocation of dd_data\n");
6624cc0e56eSJames Smart 		rc = -ENOMEM;
6634cc0e56eSJames Smart 		goto no_dd_data;
6644cc0e56eSJames Smart 	}
6654cc0e56eSJames Smart 
666a33c4f7bSJames Smart 	elscmd = job->request->rqst_data.r_els.els_code;
667a33c4f7bSJames Smart 	cmdsize = job->request_payload.payload_len;
668a33c4f7bSJames Smart 	rspsize = job->reply_payload.payload_len;
669a33c4f7bSJames Smart 
670f1c3b0fcSJames Smart 	if (!lpfc_nlp_get(ndlp)) {
671f1c3b0fcSJames Smart 		rc = -ENODEV;
6724cc0e56eSJames Smart 		goto free_dd_data;
673f1c3b0fcSJames Smart 	}
674f1c3b0fcSJames Smart 
675a33c4f7bSJames Smart 	/* We will use the allocated dma buffers by prep els iocb for command
676a33c4f7bSJames Smart 	 * and response to ensure if the job times out and the request is freed,
677a33c4f7bSJames Smart 	 * we won't be dma into memory that is no longer allocated to for the
678a33c4f7bSJames Smart 	 * request.
679a33c4f7bSJames Smart 	 */
680f1c3b0fcSJames Smart 
6814cc0e56eSJames Smart 	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
682f1c3b0fcSJames Smart 				      ndlp->nlp_DID, elscmd);
683f1c3b0fcSJames Smart 	if (!cmdiocbq) {
6844cc0e56eSJames Smart 		rc = -EIO;
685a33c4f7bSJames Smart 		goto release_ndlp;
686f1c3b0fcSJames Smart 	}
687f1c3b0fcSJames Smart 
688a33c4f7bSJames Smart 	rpi = ndlp->nlp_rpi;
689f1c3b0fcSJames Smart 
690a33c4f7bSJames Smart 	/* Transfer the request payload to allocated command dma buffer */
691f1c3b0fcSJames Smart 
692a33c4f7bSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
693a33c4f7bSJames Smart 			  job->request_payload.sg_cnt,
694a33c4f7bSJames Smart 			  ((struct lpfc_dmabuf *)cmdiocbq->context2)->virt,
695a33c4f7bSJames Smart 			  cmdsize);
696f1c3b0fcSJames Smart 
6973ef6d24cSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
6983ef6d24cSJames Smart 		cmdiocbq->iocb.ulpContext = phba->sli4_hba.rpi_ids[rpi];
6993ef6d24cSJames Smart 	else
700f1c3b0fcSJames Smart 		cmdiocbq->iocb.ulpContext = rpi;
701f1c3b0fcSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
7024cc0e56eSJames Smart 	cmdiocbq->context1 = dd_data;
70393d1379eSJames Smart 	cmdiocbq->context_un.ndlp = ndlp;
704a33c4f7bSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
7054cc0e56eSJames Smart 	dd_data->type = TYPE_IOCB;
706a33c4f7bSJames Smart 	dd_data->set_job = job;
7074cc0e56eSJames Smart 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
7084cc0e56eSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
709a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = NULL;
710a33c4f7bSJames Smart 	job->dd_data = dd_data;
711f1c3b0fcSJames Smart 
7124cc0e56eSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7139940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
7149940b97bSJames Smart 			rc = -EIO;
7159940b97bSJames Smart 			goto linkdown_err;
7169940b97bSJames Smart 		}
7174cc0e56eSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7184cc0e56eSJames Smart 		writel(creg_val, phba->HCregaddr);
7194cc0e56eSJames Smart 		readl(phba->HCregaddr); /* flush */
7204cc0e56eSJames Smart 	}
721a33c4f7bSJames Smart 
7224cc0e56eSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
723a33c4f7bSJames Smart 
7244cc0e56eSJames Smart 	if (rc == IOCB_SUCCESS)
7254cc0e56eSJames Smart 		return 0; /* done for now */
7262a9bf3d0SJames Smart 	else if (rc == IOCB_BUSY)
727d439d286SJames Smart 		rc = -EAGAIN;
7282a9bf3d0SJames Smart 	else
729d439d286SJames Smart 		rc = -EIO;
7304cc0e56eSJames Smart 
7319940b97bSJames Smart linkdown_err:
732f1c3b0fcSJames Smart 
733a33c4f7bSJames Smart 	cmdiocbq->context1 = ndlp;
734a33c4f7bSJames Smart 	lpfc_els_free_iocb(phba, cmdiocbq);
735f1c3b0fcSJames Smart 
736a33c4f7bSJames Smart release_ndlp:
737a33c4f7bSJames Smart 	lpfc_nlp_put(ndlp);
738f1c3b0fcSJames Smart 
7394cc0e56eSJames Smart free_dd_data:
7404cc0e56eSJames Smart 	kfree(dd_data);
7414cc0e56eSJames Smart 
7424cc0e56eSJames Smart no_dd_data:
743f1c3b0fcSJames Smart 	/* make error code available to userspace */
744f1c3b0fcSJames Smart 	job->reply->result = rc;
7454cc0e56eSJames Smart 	job->dd_data = NULL;
7464cc0e56eSJames Smart 	return rc;
747f1c3b0fcSJames Smart }
748f1c3b0fcSJames Smart 
7493b5dd52aSJames Smart /**
7503b5dd52aSJames Smart  * lpfc_bsg_event_free - frees an allocated event structure
7513b5dd52aSJames Smart  * @kref: Pointer to a kref.
7523b5dd52aSJames Smart  *
7533b5dd52aSJames Smart  * Called from kref_put. Back cast the kref into an event structure address.
7543b5dd52aSJames Smart  * Free any events to get, delete associated nodes, free any events to see,
7553b5dd52aSJames Smart  * free any data then free the event itself.
7563b5dd52aSJames Smart  **/
757f1c3b0fcSJames Smart static void
7584cc0e56eSJames Smart lpfc_bsg_event_free(struct kref *kref)
759f1c3b0fcSJames Smart {
7604cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
7614cc0e56eSJames Smart 						  kref);
762f1c3b0fcSJames Smart 	struct event_data *ed;
763f1c3b0fcSJames Smart 
764f1c3b0fcSJames Smart 	list_del(&evt->node);
765f1c3b0fcSJames Smart 
766f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_get)) {
767f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
768f1c3b0fcSJames Smart 		list_del(&ed->node);
769f1c3b0fcSJames Smart 		kfree(ed->data);
770f1c3b0fcSJames Smart 		kfree(ed);
771f1c3b0fcSJames Smart 	}
772f1c3b0fcSJames Smart 
773f1c3b0fcSJames Smart 	while (!list_empty(&evt->events_to_see)) {
774f1c3b0fcSJames Smart 		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
775f1c3b0fcSJames Smart 		list_del(&ed->node);
776f1c3b0fcSJames Smart 		kfree(ed->data);
777f1c3b0fcSJames Smart 		kfree(ed);
778f1c3b0fcSJames Smart 	}
779f1c3b0fcSJames Smart 
780a33c4f7bSJames Smart 	kfree(evt->dd_data);
781f1c3b0fcSJames Smart 	kfree(evt);
782f1c3b0fcSJames Smart }
783f1c3b0fcSJames Smart 
7843b5dd52aSJames Smart /**
7853b5dd52aSJames Smart  * lpfc_bsg_event_ref - increments the kref for an event
7863b5dd52aSJames Smart  * @evt: Pointer to an event structure.
7873b5dd52aSJames Smart  **/
788f1c3b0fcSJames Smart static inline void
7894cc0e56eSJames Smart lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
790f1c3b0fcSJames Smart {
7914cc0e56eSJames Smart 	kref_get(&evt->kref);
792f1c3b0fcSJames Smart }
793f1c3b0fcSJames Smart 
7943b5dd52aSJames Smart /**
7953b5dd52aSJames Smart  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
7963b5dd52aSJames Smart  * @evt: Pointer to an event structure.
7973b5dd52aSJames Smart  **/
798f1c3b0fcSJames Smart static inline void
7994cc0e56eSJames Smart lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
800f1c3b0fcSJames Smart {
8014cc0e56eSJames Smart 	kref_put(&evt->kref, lpfc_bsg_event_free);
802f1c3b0fcSJames Smart }
803f1c3b0fcSJames Smart 
8043b5dd52aSJames Smart /**
8053b5dd52aSJames Smart  * lpfc_bsg_event_new - allocate and initialize a event structure
8063b5dd52aSJames Smart  * @ev_mask: Mask of events.
8073b5dd52aSJames Smart  * @ev_reg_id: Event reg id.
8083b5dd52aSJames Smart  * @ev_req_id: Event request id.
8093b5dd52aSJames Smart  **/
8104cc0e56eSJames Smart static struct lpfc_bsg_event *
8114cc0e56eSJames Smart lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
8124cc0e56eSJames Smart {
8134cc0e56eSJames Smart 	struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
814f1c3b0fcSJames Smart 
8154cc0e56eSJames Smart 	if (!evt)
8164cc0e56eSJames Smart 		return NULL;
8174cc0e56eSJames Smart 
8184cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_get);
8194cc0e56eSJames Smart 	INIT_LIST_HEAD(&evt->events_to_see);
8204cc0e56eSJames Smart 	evt->type_mask = ev_mask;
8214cc0e56eSJames Smart 	evt->req_id = ev_req_id;
8224cc0e56eSJames Smart 	evt->reg_id = ev_reg_id;
8234cc0e56eSJames Smart 	evt->wait_time_stamp = jiffies;
824a33c4f7bSJames Smart 	evt->dd_data = NULL;
8254cc0e56eSJames Smart 	init_waitqueue_head(&evt->wq);
8264cc0e56eSJames Smart 	kref_init(&evt->kref);
8274cc0e56eSJames Smart 	return evt;
8284cc0e56eSJames Smart }
8294cc0e56eSJames Smart 
8303b5dd52aSJames Smart /**
8313b5dd52aSJames Smart  * diag_cmd_data_free - Frees an lpfc dma buffer extension
8323b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
8333b5dd52aSJames Smart  * @mlist: Pointer to an lpfc dma buffer extension.
8343b5dd52aSJames Smart  **/
8354cc0e56eSJames Smart static int
8363b5dd52aSJames Smart diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
8374cc0e56eSJames Smart {
8384cc0e56eSJames Smart 	struct lpfc_dmabufext *mlast;
8394cc0e56eSJames Smart 	struct pci_dev *pcidev;
8404cc0e56eSJames Smart 	struct list_head head, *curr, *next;
8414cc0e56eSJames Smart 
8424cc0e56eSJames Smart 	if ((!mlist) || (!lpfc_is_link_up(phba) &&
8434cc0e56eSJames Smart 		(phba->link_flag & LS_LOOPBACK_MODE))) {
8444cc0e56eSJames Smart 		return 0;
8454cc0e56eSJames Smart 	}
8464cc0e56eSJames Smart 
8474cc0e56eSJames Smart 	pcidev = phba->pcidev;
8484cc0e56eSJames Smart 	list_add_tail(&head, &mlist->dma.list);
8494cc0e56eSJames Smart 
8504cc0e56eSJames Smart 	list_for_each_safe(curr, next, &head) {
8514cc0e56eSJames Smart 		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
8524cc0e56eSJames Smart 		if (mlast->dma.virt)
8534cc0e56eSJames Smart 			dma_free_coherent(&pcidev->dev,
8544cc0e56eSJames Smart 					  mlast->size,
8554cc0e56eSJames Smart 					  mlast->dma.virt,
8564cc0e56eSJames Smart 					  mlast->dma.phys);
8574cc0e56eSJames Smart 		kfree(mlast);
8584cc0e56eSJames Smart 	}
8594cc0e56eSJames Smart 	return 0;
8604cc0e56eSJames Smart }
861f1c3b0fcSJames Smart 
862f1c3b0fcSJames Smart /**
863f1c3b0fcSJames Smart  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
864f1c3b0fcSJames Smart  * @phba:
865f1c3b0fcSJames Smart  * @pring:
866f1c3b0fcSJames Smart  * @piocbq:
867f1c3b0fcSJames Smart  *
868f1c3b0fcSJames Smart  * This function is called when an unsolicited CT command is received.  It
8694cc0e56eSJames Smart  * forwards the event to any processes registered to receive CT events.
8703b5dd52aSJames Smart  **/
8714fede78fSJames Smart int
872f1c3b0fcSJames Smart lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
873f1c3b0fcSJames Smart 			struct lpfc_iocbq *piocbq)
874f1c3b0fcSJames Smart {
875f1c3b0fcSJames Smart 	uint32_t evt_req_id = 0;
876f1c3b0fcSJames Smart 	uint32_t cmd;
877f1c3b0fcSJames Smart 	uint32_t len;
878f1c3b0fcSJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
8794cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
880f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
881f1c3b0fcSJames Smart 	struct lpfc_iocbq *iocbq;
882f1c3b0fcSJames Smart 	size_t offset = 0;
883f1c3b0fcSJames Smart 	struct list_head head;
884f1c3b0fcSJames Smart 	struct ulp_bde64 *bde;
885f1c3b0fcSJames Smart 	dma_addr_t dma_addr;
886f1c3b0fcSJames Smart 	int i;
887f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
888f1c3b0fcSJames Smart 	struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
889f1c3b0fcSJames Smart 	struct lpfc_hbq_entry *hbqe;
890f1c3b0fcSJames Smart 	struct lpfc_sli_ct_request *ct_req;
8914cc0e56eSJames Smart 	struct fc_bsg_job *job = NULL;
892a33c4f7bSJames Smart 	struct bsg_job_data *dd_data = NULL;
8934fede78fSJames Smart 	unsigned long flags;
8944cc0e56eSJames Smart 	int size = 0;
895f1c3b0fcSJames Smart 
896f1c3b0fcSJames Smart 	INIT_LIST_HEAD(&head);
897f1c3b0fcSJames Smart 	list_add_tail(&head, &piocbq->list);
898f1c3b0fcSJames Smart 
899f1c3b0fcSJames Smart 	if (piocbq->iocb.ulpBdeCount == 0 ||
900f1c3b0fcSJames Smart 	    piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
901f1c3b0fcSJames Smart 		goto error_ct_unsol_exit;
902f1c3b0fcSJames Smart 
9034cc0e56eSJames Smart 	if (phba->link_state == LPFC_HBA_ERROR ||
9044cc0e56eSJames Smart 		(!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
9054cc0e56eSJames Smart 		goto error_ct_unsol_exit;
9064cc0e56eSJames Smart 
907f1c3b0fcSJames Smart 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
908f1c3b0fcSJames Smart 		dmabuf = bdeBuf1;
909f1c3b0fcSJames Smart 	else {
910f1c3b0fcSJames Smart 		dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
911f1c3b0fcSJames Smart 				    piocbq->iocb.un.cont64[0].addrLow);
912f1c3b0fcSJames Smart 		dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
913f1c3b0fcSJames Smart 	}
9144cc0e56eSJames Smart 	if (dmabuf == NULL)
9154cc0e56eSJames Smart 		goto error_ct_unsol_exit;
916f1c3b0fcSJames Smart 	ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
917f1c3b0fcSJames Smart 	evt_req_id = ct_req->FsType;
918f1c3b0fcSJames Smart 	cmd = ct_req->CommandResponse.bits.CmdRsp;
919f1c3b0fcSJames Smart 	len = ct_req->CommandResponse.bits.Size;
920f1c3b0fcSJames Smart 	if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
921f1c3b0fcSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
922f1c3b0fcSJames Smart 
9234fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
924f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
9254cc0e56eSJames Smart 		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
9264cc0e56eSJames Smart 			evt->req_id != evt_req_id)
927f1c3b0fcSJames Smart 			continue;
928f1c3b0fcSJames Smart 
9294cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
9304cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
931f1c3b0fcSJames Smart 		evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
9324cc0e56eSJames Smart 		if (evt_dat == NULL) {
9334cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
9344cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
935f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
936f1c3b0fcSJames Smart 					"2614 Memory allocation failed for "
937f1c3b0fcSJames Smart 					"CT event\n");
938f1c3b0fcSJames Smart 			break;
939f1c3b0fcSJames Smart 		}
940f1c3b0fcSJames Smart 
941f1c3b0fcSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
942f1c3b0fcSJames Smart 			/* take accumulated byte count from the last iocbq */
943f1c3b0fcSJames Smart 			iocbq = list_entry(head.prev, typeof(*iocbq), list);
944f1c3b0fcSJames Smart 			evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
945f1c3b0fcSJames Smart 		} else {
946f1c3b0fcSJames Smart 			list_for_each_entry(iocbq, &head, list) {
947f1c3b0fcSJames Smart 				for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
948f1c3b0fcSJames Smart 					evt_dat->len +=
949f1c3b0fcSJames Smart 					iocbq->iocb.un.cont64[i].tus.f.bdeSize;
950f1c3b0fcSJames Smart 			}
951f1c3b0fcSJames Smart 		}
952f1c3b0fcSJames Smart 
953f1c3b0fcSJames Smart 		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
9544cc0e56eSJames Smart 		if (evt_dat->data == NULL) {
955f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
956f1c3b0fcSJames Smart 					"2615 Memory allocation failed for "
957f1c3b0fcSJames Smart 					"CT event data, size %d\n",
958f1c3b0fcSJames Smart 					evt_dat->len);
959f1c3b0fcSJames Smart 			kfree(evt_dat);
9604fede78fSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
9614cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
9624fede78fSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
963f1c3b0fcSJames Smart 			goto error_ct_unsol_exit;
964f1c3b0fcSJames Smart 		}
965f1c3b0fcSJames Smart 
966f1c3b0fcSJames Smart 		list_for_each_entry(iocbq, &head, list) {
9674cc0e56eSJames Smart 			size = 0;
968f1c3b0fcSJames Smart 			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
969f1c3b0fcSJames Smart 				bdeBuf1 = iocbq->context2;
970f1c3b0fcSJames Smart 				bdeBuf2 = iocbq->context3;
971f1c3b0fcSJames Smart 			}
972f1c3b0fcSJames Smart 			for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
973f1c3b0fcSJames Smart 				if (phba->sli3_options &
974f1c3b0fcSJames Smart 				    LPFC_SLI3_HBQ_ENABLED) {
975f1c3b0fcSJames Smart 					if (i == 0) {
976f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
977f1c3b0fcSJames Smart 						  &iocbq->iocb.un.ulpWord[0];
978f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
979f1c3b0fcSJames Smart 						dmabuf = bdeBuf1;
980f1c3b0fcSJames Smart 					} else if (i == 1) {
981f1c3b0fcSJames Smart 						hbqe = (struct lpfc_hbq_entry *)
982f1c3b0fcSJames Smart 							&iocbq->iocb.unsli3.
983f1c3b0fcSJames Smart 							sli3Words[4];
984f1c3b0fcSJames Smart 						size = hbqe->bde.tus.f.bdeSize;
985f1c3b0fcSJames Smart 						dmabuf = bdeBuf2;
986f1c3b0fcSJames Smart 					}
987f1c3b0fcSJames Smart 					if ((offset + size) > evt_dat->len)
988f1c3b0fcSJames Smart 						size = evt_dat->len - offset;
989f1c3b0fcSJames Smart 				} else {
990f1c3b0fcSJames Smart 					size = iocbq->iocb.un.cont64[i].
991f1c3b0fcSJames Smart 						tus.f.bdeSize;
992f1c3b0fcSJames Smart 					bde = &iocbq->iocb.un.cont64[i];
993f1c3b0fcSJames Smart 					dma_addr = getPaddr(bde->addrHigh,
994f1c3b0fcSJames Smart 							    bde->addrLow);
995f1c3b0fcSJames Smart 					dmabuf = lpfc_sli_ringpostbuf_get(phba,
996f1c3b0fcSJames Smart 							pring, dma_addr);
997f1c3b0fcSJames Smart 				}
998f1c3b0fcSJames Smart 				if (!dmabuf) {
999f1c3b0fcSJames Smart 					lpfc_printf_log(phba, KERN_ERR,
1000f1c3b0fcSJames Smart 						LOG_LIBDFC, "2616 No dmabuf "
1001f1c3b0fcSJames Smart 						"found for iocbq 0x%p\n",
1002f1c3b0fcSJames Smart 						iocbq);
1003f1c3b0fcSJames Smart 					kfree(evt_dat->data);
1004f1c3b0fcSJames Smart 					kfree(evt_dat);
10054fede78fSJames Smart 					spin_lock_irqsave(&phba->ct_ev_lock,
10064fede78fSJames Smart 						flags);
10074cc0e56eSJames Smart 					lpfc_bsg_event_unref(evt);
10084fede78fSJames Smart 					spin_unlock_irqrestore(
10094fede78fSJames Smart 						&phba->ct_ev_lock, flags);
1010f1c3b0fcSJames Smart 					goto error_ct_unsol_exit;
1011f1c3b0fcSJames Smart 				}
1012f1c3b0fcSJames Smart 				memcpy((char *)(evt_dat->data) + offset,
1013f1c3b0fcSJames Smart 				       dmabuf->virt, size);
1014f1c3b0fcSJames Smart 				offset += size;
1015f1c3b0fcSJames Smart 				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1016f1c3b0fcSJames Smart 				    !(phba->sli3_options &
1017f1c3b0fcSJames Smart 				      LPFC_SLI3_HBQ_ENABLED)) {
1018f1c3b0fcSJames Smart 					lpfc_sli_ringpostbuf_put(phba, pring,
1019f1c3b0fcSJames Smart 								 dmabuf);
1020f1c3b0fcSJames Smart 				} else {
1021f1c3b0fcSJames Smart 					switch (cmd) {
10224cc0e56eSJames Smart 					case ELX_LOOPBACK_DATA:
10231b51197dSJames Smart 						if (phba->sli_rev <
10241b51197dSJames Smart 						    LPFC_SLI_REV4)
10253b5dd52aSJames Smart 							diag_cmd_data_free(phba,
10261b51197dSJames Smart 							(struct lpfc_dmabufext
10271b51197dSJames Smart 							 *)dmabuf);
10284cc0e56eSJames Smart 						break;
1029f1c3b0fcSJames Smart 					case ELX_LOOPBACK_XRI_SETUP:
10304cc0e56eSJames Smart 						if ((phba->sli_rev ==
10314cc0e56eSJames Smart 							LPFC_SLI_REV2) ||
10324cc0e56eSJames Smart 							(phba->sli3_options &
10334cc0e56eSJames Smart 							LPFC_SLI3_HBQ_ENABLED
10344cc0e56eSJames Smart 							)) {
10354cc0e56eSJames Smart 							lpfc_in_buf_free(phba,
10364cc0e56eSJames Smart 									dmabuf);
10374cc0e56eSJames Smart 						} else {
1038f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
1039f1c3b0fcSJames Smart 									 pring,
1040f1c3b0fcSJames Smart 									 1);
10414cc0e56eSJames Smart 						}
1042f1c3b0fcSJames Smart 						break;
1043f1c3b0fcSJames Smart 					default:
1044f1c3b0fcSJames Smart 						if (!(phba->sli3_options &
1045f1c3b0fcSJames Smart 						      LPFC_SLI3_HBQ_ENABLED))
1046f1c3b0fcSJames Smart 							lpfc_post_buffer(phba,
1047f1c3b0fcSJames Smart 									 pring,
1048f1c3b0fcSJames Smart 									 1);
1049f1c3b0fcSJames Smart 						break;
1050f1c3b0fcSJames Smart 					}
1051f1c3b0fcSJames Smart 				}
1052f1c3b0fcSJames Smart 			}
1053f1c3b0fcSJames Smart 		}
1054f1c3b0fcSJames Smart 
10554fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1056f1c3b0fcSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4) {
1057f1c3b0fcSJames Smart 			evt_dat->immed_dat = phba->ctx_idx;
10586dd9e31cSJames Smart 			phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1059589a52d6SJames Smart 			/* Provide warning for over-run of the ct_ctx array */
10606dd9e31cSJames Smart 			if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1061589a52d6SJames Smart 			    UNSOL_VALID)
1062589a52d6SJames Smart 				lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1063589a52d6SJames Smart 						"2717 CT context array entry "
1064589a52d6SJames Smart 						"[%d] over-run: oxid:x%x, "
1065589a52d6SJames Smart 						"sid:x%x\n", phba->ctx_idx,
1066589a52d6SJames Smart 						phba->ct_ctx[
1067589a52d6SJames Smart 						    evt_dat->immed_dat].oxid,
1068589a52d6SJames Smart 						phba->ct_ctx[
1069589a52d6SJames Smart 						    evt_dat->immed_dat].SID);
10707851fe2cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].rxid =
1071f1c3b0fcSJames Smart 				piocbq->iocb.ulpContext;
10727851fe2cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].oxid =
10737851fe2cSJames Smart 				piocbq->iocb.unsli3.rcvsli3.ox_id;
1074f1c3b0fcSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].SID =
1075f1c3b0fcSJames Smart 				piocbq->iocb.un.rcvels.remoteID;
10766dd9e31cSJames Smart 			phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1077f1c3b0fcSJames Smart 		} else
1078f1c3b0fcSJames Smart 			evt_dat->immed_dat = piocbq->iocb.ulpContext;
1079f1c3b0fcSJames Smart 
1080f1c3b0fcSJames Smart 		evt_dat->type = FC_REG_CT_EVENT;
1081f1c3b0fcSJames Smart 		list_add(&evt_dat->node, &evt->events_to_see);
10824cc0e56eSJames Smart 		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1083f1c3b0fcSJames Smart 			wake_up_interruptible(&evt->wq);
10844cc0e56eSJames Smart 			lpfc_bsg_event_unref(evt);
1085f1c3b0fcSJames Smart 			break;
1086f1c3b0fcSJames Smart 		}
10874cc0e56eSJames Smart 
10884cc0e56eSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
10894cc0e56eSJames Smart 
1090a33c4f7bSJames Smart 		dd_data = (struct bsg_job_data *)evt->dd_data;
1091a33c4f7bSJames Smart 		job = dd_data->set_job;
1092a33c4f7bSJames Smart 		dd_data->set_job = NULL;
1093a33c4f7bSJames Smart 		lpfc_bsg_event_unref(evt);
10944cc0e56eSJames Smart 		if (job) {
10954cc0e56eSJames Smart 			job->reply->reply_payload_rcv_len = size;
10964cc0e56eSJames Smart 			/* make error code available to userspace */
10974cc0e56eSJames Smart 			job->reply->result = 0;
10984cc0e56eSJames Smart 			job->dd_data = NULL;
10994cc0e56eSJames Smart 			/* complete the job back to userspace */
11004cc0e56eSJames Smart 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
11014cc0e56eSJames Smart 			job->job_done(job);
11024cc0e56eSJames Smart 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
11034cc0e56eSJames Smart 		}
11044cc0e56eSJames Smart 	}
11054fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1106f1c3b0fcSJames Smart 
1107f1c3b0fcSJames Smart error_ct_unsol_exit:
1108f1c3b0fcSJames Smart 	if (!list_empty(&head))
1109f1c3b0fcSJames Smart 		list_del(&head);
11101b51197dSJames Smart 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
11111b51197dSJames Smart 	    (evt_req_id == SLI_CT_ELX_LOOPBACK))
11124cc0e56eSJames Smart 		return 0;
11134fede78fSJames Smart 	return 1;
1114f1c3b0fcSJames Smart }
1115f1c3b0fcSJames Smart 
1116f1c3b0fcSJames Smart /**
11176dd9e31cSJames Smart  * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
11186dd9e31cSJames Smart  * @phba: Pointer to HBA context object.
11196dd9e31cSJames Smart  * @dmabuf: pointer to a dmabuf that describes the FC sequence
11206dd9e31cSJames Smart  *
11216dd9e31cSJames Smart  * This function handles abort to the CT command toward management plane
11226dd9e31cSJames Smart  * for SLI4 port.
11236dd9e31cSJames Smart  *
11246dd9e31cSJames Smart  * If the pending context of a CT command to management plane present, clears
11256dd9e31cSJames Smart  * such context and returns 1 for handled; otherwise, it returns 0 indicating
11266dd9e31cSJames Smart  * no context exists.
11276dd9e31cSJames Smart  **/
11286dd9e31cSJames Smart int
11296dd9e31cSJames Smart lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
11306dd9e31cSJames Smart {
11316dd9e31cSJames Smart 	struct fc_frame_header fc_hdr;
11326dd9e31cSJames Smart 	struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
11336dd9e31cSJames Smart 	int ctx_idx, handled = 0;
11346dd9e31cSJames Smart 	uint16_t oxid, rxid;
11356dd9e31cSJames Smart 	uint32_t sid;
11366dd9e31cSJames Smart 
11376dd9e31cSJames Smart 	memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11386dd9e31cSJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
11396dd9e31cSJames Smart 	oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
11406dd9e31cSJames Smart 	rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
11416dd9e31cSJames Smart 
11426dd9e31cSJames Smart 	for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
11436dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
11446dd9e31cSJames Smart 			continue;
11456dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].rxid != rxid)
11466dd9e31cSJames Smart 			continue;
11476dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].oxid != oxid)
11486dd9e31cSJames Smart 			continue;
11496dd9e31cSJames Smart 		if (phba->ct_ctx[ctx_idx].SID != sid)
11506dd9e31cSJames Smart 			continue;
11516dd9e31cSJames Smart 		phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
11526dd9e31cSJames Smart 		handled = 1;
11536dd9e31cSJames Smart 	}
11546dd9e31cSJames Smart 	return handled;
11556dd9e31cSJames Smart }
11566dd9e31cSJames Smart 
11576dd9e31cSJames Smart /**
11584cc0e56eSJames Smart  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1159f1c3b0fcSJames Smart  * @job: SET_EVENT fc_bsg_job
11603b5dd52aSJames Smart  **/
1161f1c3b0fcSJames Smart static int
11624cc0e56eSJames Smart lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1163f1c3b0fcSJames Smart {
1164f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1165f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
1166f1c3b0fcSJames Smart 	struct set_ct_event *event_req;
11674cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
1168f1c3b0fcSJames Smart 	int rc = 0;
11694cc0e56eSJames Smart 	struct bsg_job_data *dd_data = NULL;
11704cc0e56eSJames Smart 	uint32_t ev_mask;
11714cc0e56eSJames Smart 	unsigned long flags;
1172f1c3b0fcSJames Smart 
1173f1c3b0fcSJames Smart 	if (job->request_len <
1174f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1175f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1176f1c3b0fcSJames Smart 				"2612 Received SET_CT_EVENT below minimum "
1177f1c3b0fcSJames Smart 				"size\n");
11784cc0e56eSJames Smart 		rc = -EINVAL;
11794cc0e56eSJames Smart 		goto job_error;
11804cc0e56eSJames Smart 	}
11814cc0e56eSJames Smart 
1182f1c3b0fcSJames Smart 	event_req = (struct set_ct_event *)
1183f1c3b0fcSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
11844cc0e56eSJames Smart 	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
11854cc0e56eSJames Smart 				FC_REG_EVENT_MASK);
11864fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1187f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1188f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
11894cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1190f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1191a33c4f7bSJames Smart 			dd_data = (struct bsg_job_data *)evt->dd_data;
1192f1c3b0fcSJames Smart 			break;
1193f1c3b0fcSJames Smart 		}
1194f1c3b0fcSJames Smart 	}
11954fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1196f1c3b0fcSJames Smart 
1197f1c3b0fcSJames Smart 	if (&evt->node == &phba->ct_ev_waiters) {
1198f1c3b0fcSJames Smart 		/* no event waiting struct yet - first call */
1199a33c4f7bSJames Smart 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1200a33c4f7bSJames Smart 		if (dd_data == NULL) {
1201a33c4f7bSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1202a33c4f7bSJames Smart 					"2734 Failed allocation of dd_data\n");
1203a33c4f7bSJames Smart 			rc = -ENOMEM;
1204a33c4f7bSJames Smart 			goto job_error;
1205a33c4f7bSJames Smart 		}
12064cc0e56eSJames Smart 		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1207f1c3b0fcSJames Smart 					event_req->ev_req_id);
1208f1c3b0fcSJames Smart 		if (!evt) {
1209f1c3b0fcSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1210f1c3b0fcSJames Smart 					"2617 Failed allocation of event "
1211f1c3b0fcSJames Smart 					"waiter\n");
12124cc0e56eSJames Smart 			rc = -ENOMEM;
12134cc0e56eSJames Smart 			goto job_error;
1214f1c3b0fcSJames Smart 		}
1215a33c4f7bSJames Smart 		dd_data->type = TYPE_EVT;
1216a33c4f7bSJames Smart 		dd_data->set_job = NULL;
1217a33c4f7bSJames Smart 		dd_data->context_un.evt = evt;
1218a33c4f7bSJames Smart 		evt->dd_data = (void *)dd_data;
12194fede78fSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1220f1c3b0fcSJames Smart 		list_add(&evt->node, &phba->ct_ev_waiters);
12214cc0e56eSJames Smart 		lpfc_bsg_event_ref(evt);
1222f1c3b0fcSJames Smart 		evt->wait_time_stamp = jiffies;
12234cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
12244cc0e56eSJames Smart 	}
1225f1c3b0fcSJames Smart 
12264fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
12274cc0e56eSJames Smart 	evt->waiting = 1;
1228a33c4f7bSJames Smart 	dd_data->set_job = job; /* for unsolicited command */
12294cc0e56eSJames Smart 	job->dd_data = dd_data; /* for fc transport timeout callback*/
12304fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
12314cc0e56eSJames Smart 	return 0; /* call job done later */
1232f1c3b0fcSJames Smart 
12334cc0e56eSJames Smart job_error:
12344cc0e56eSJames Smart 	if (dd_data != NULL)
12354cc0e56eSJames Smart 		kfree(dd_data);
1236f1c3b0fcSJames Smart 
12374cc0e56eSJames Smart 	job->dd_data = NULL;
12384cc0e56eSJames Smart 	return rc;
1239f1c3b0fcSJames Smart }
1240f1c3b0fcSJames Smart 
1241f1c3b0fcSJames Smart /**
12424cc0e56eSJames Smart  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1243f1c3b0fcSJames Smart  * @job: GET_EVENT fc_bsg_job
12443b5dd52aSJames Smart  **/
1245f1c3b0fcSJames Smart static int
12464cc0e56eSJames Smart lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1247f1c3b0fcSJames Smart {
1248f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1249f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
1250f1c3b0fcSJames Smart 	struct get_ct_event *event_req;
1251f1c3b0fcSJames Smart 	struct get_ct_event_reply *event_reply;
12524cc0e56eSJames Smart 	struct lpfc_bsg_event *evt;
1253f1c3b0fcSJames Smart 	struct event_data *evt_dat = NULL;
12544fede78fSJames Smart 	unsigned long flags;
12554cc0e56eSJames Smart 	uint32_t rc = 0;
1256f1c3b0fcSJames Smart 
1257f1c3b0fcSJames Smart 	if (job->request_len <
1258f1c3b0fcSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1259f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1260f1c3b0fcSJames Smart 				"2613 Received GET_CT_EVENT request below "
1261f1c3b0fcSJames Smart 				"minimum size\n");
12624cc0e56eSJames Smart 		rc = -EINVAL;
12634cc0e56eSJames Smart 		goto job_error;
1264f1c3b0fcSJames Smart 	}
1265f1c3b0fcSJames Smart 
1266f1c3b0fcSJames Smart 	event_req = (struct get_ct_event *)
1267f1c3b0fcSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
1268f1c3b0fcSJames Smart 
1269f1c3b0fcSJames Smart 	event_reply = (struct get_ct_event_reply *)
1270f1c3b0fcSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
12714fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1272f1c3b0fcSJames Smart 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1273f1c3b0fcSJames Smart 		if (evt->reg_id == event_req->ev_reg_id) {
1274f1c3b0fcSJames Smart 			if (list_empty(&evt->events_to_get))
1275f1c3b0fcSJames Smart 				break;
12764cc0e56eSJames Smart 			lpfc_bsg_event_ref(evt);
1277f1c3b0fcSJames Smart 			evt->wait_time_stamp = jiffies;
1278f1c3b0fcSJames Smart 			evt_dat = list_entry(evt->events_to_get.prev,
1279f1c3b0fcSJames Smart 					     struct event_data, node);
1280f1c3b0fcSJames Smart 			list_del(&evt_dat->node);
1281f1c3b0fcSJames Smart 			break;
1282f1c3b0fcSJames Smart 		}
1283f1c3b0fcSJames Smart 	}
12844fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1285f1c3b0fcSJames Smart 
12864cc0e56eSJames Smart 	/* The app may continue to ask for event data until it gets
12874cc0e56eSJames Smart 	 * an error indicating that there isn't anymore
12884cc0e56eSJames Smart 	 */
12894cc0e56eSJames Smart 	if (evt_dat == NULL) {
1290f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len = 0;
1291f1c3b0fcSJames Smart 		rc = -ENOENT;
12924cc0e56eSJames Smart 		goto job_error;
1293f1c3b0fcSJames Smart 	}
1294f1c3b0fcSJames Smart 
12954cc0e56eSJames Smart 	if (evt_dat->len > job->request_payload.payload_len) {
12964cc0e56eSJames Smart 		evt_dat->len = job->request_payload.payload_len;
1297f1c3b0fcSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1298f1c3b0fcSJames Smart 				"2618 Truncated event data at %d "
1299f1c3b0fcSJames Smart 				"bytes\n",
13004cc0e56eSJames Smart 				job->request_payload.payload_len);
1301f1c3b0fcSJames Smart 	}
1302f1c3b0fcSJames Smart 
13034cc0e56eSJames Smart 	event_reply->type = evt_dat->type;
1304f1c3b0fcSJames Smart 	event_reply->immed_data = evt_dat->immed_dat;
1305f1c3b0fcSJames Smart 	if (evt_dat->len > 0)
1306f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len =
13074cc0e56eSJames Smart 			sg_copy_from_buffer(job->request_payload.sg_list,
13084cc0e56eSJames Smart 					    job->request_payload.sg_cnt,
1309f1c3b0fcSJames Smart 					    evt_dat->data, evt_dat->len);
1310f1c3b0fcSJames Smart 	else
1311f1c3b0fcSJames Smart 		job->reply->reply_payload_rcv_len = 0;
1312f1c3b0fcSJames Smart 
13134cc0e56eSJames Smart 	if (evt_dat) {
1314f1c3b0fcSJames Smart 		kfree(evt_dat->data);
1315f1c3b0fcSJames Smart 		kfree(evt_dat);
13164cc0e56eSJames Smart 	}
13174cc0e56eSJames Smart 
13184fede78fSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
13194cc0e56eSJames Smart 	lpfc_bsg_event_unref(evt);
13204fede78fSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
13214cc0e56eSJames Smart 	job->dd_data = NULL;
13224cc0e56eSJames Smart 	job->reply->result = 0;
1323f1c3b0fcSJames Smart 	job->job_done(job);
13244cc0e56eSJames Smart 	return 0;
1325f1c3b0fcSJames Smart 
13264cc0e56eSJames Smart job_error:
13274cc0e56eSJames Smart 	job->dd_data = NULL;
13284cc0e56eSJames Smart 	job->reply->result = rc;
1329f1c3b0fcSJames Smart 	return rc;
1330f1c3b0fcSJames Smart }
1331f1c3b0fcSJames Smart 
1332f1c3b0fcSJames Smart /**
13333b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
13343b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
13353b5dd52aSJames Smart  * @cmdiocbq: Pointer to command iocb.
13363b5dd52aSJames Smart  * @rspiocbq: Pointer to response iocb.
13373b5dd52aSJames Smart  *
13383b5dd52aSJames Smart  * This function is the completion handler for iocbs issued using
13393b5dd52aSJames Smart  * lpfc_issue_ct_rsp_cmp function. This function is called by the
13403b5dd52aSJames Smart  * ring event handler function without any lock held. This function
13413b5dd52aSJames Smart  * can be called from both worker thread context and interrupt
13423b5dd52aSJames Smart  * context. This function also can be called from other thread which
13433b5dd52aSJames Smart  * cleans up the SLI layer objects.
13443b5dd52aSJames Smart  * This function copy the contents of the response iocb to the
13453b5dd52aSJames Smart  * response iocb memory object provided by the caller of
13463b5dd52aSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
13473b5dd52aSJames Smart  * sleeps for the iocb completion.
13483b5dd52aSJames Smart  **/
13493b5dd52aSJames Smart static void
13503b5dd52aSJames Smart lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
13513b5dd52aSJames Smart 			struct lpfc_iocbq *cmdiocbq,
13523b5dd52aSJames Smart 			struct lpfc_iocbq *rspiocbq)
13533b5dd52aSJames Smart {
13543b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
13553b5dd52aSJames Smart 	struct fc_bsg_job *job;
13563b5dd52aSJames Smart 	IOCB_t *rsp;
1357a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp;
13583b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp;
13593b5dd52aSJames Smart 	unsigned long flags;
13603b5dd52aSJames Smart 	int rc = 0;
13613b5dd52aSJames Smart 
1362a33c4f7bSJames Smart 	dd_data = cmdiocbq->context1;
1363a33c4f7bSJames Smart 
1364a33c4f7bSJames Smart 	/* Determine if job has been aborted */
13653b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1366a33c4f7bSJames Smart 	job = dd_data->set_job;
1367a33c4f7bSJames Smart 	if (job) {
1368a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
1369a33c4f7bSJames Smart 		job->dd_data = NULL;
13703b5dd52aSJames Smart 	}
1371a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
13723b5dd52aSJames Smart 
13733b5dd52aSJames Smart 	ndlp = dd_data->context_un.iocb.ndlp;
1374a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
1375a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
1376a33c4f7bSJames Smart 	rsp = &rspiocbq->iocb;
13773b5dd52aSJames Smart 
1378a33c4f7bSJames Smart 	/* Copy the completed job data or set the error status */
13793b5dd52aSJames Smart 
1380a33c4f7bSJames Smart 	if (job) {
13813b5dd52aSJames Smart 		if (rsp->ulpStatus) {
13823b5dd52aSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1383e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
13843b5dd52aSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
13853b5dd52aSJames Smart 					rc = -ETIMEDOUT;
13863b5dd52aSJames Smart 					break;
13873b5dd52aSJames Smart 				case IOERR_INVALID_RPI:
13883b5dd52aSJames Smart 					rc = -EFAULT;
13893b5dd52aSJames Smart 					break;
13903b5dd52aSJames Smart 				default:
13913b5dd52aSJames Smart 					rc = -EACCES;
13923b5dd52aSJames Smart 					break;
13933b5dd52aSJames Smart 				}
1394a33c4f7bSJames Smart 			} else {
13953b5dd52aSJames Smart 				rc = -EACCES;
1396a33c4f7bSJames Smart 			}
1397a33c4f7bSJames Smart 		} else {
1398a33c4f7bSJames Smart 			job->reply->reply_payload_rcv_len = 0;
1399a33c4f7bSJames Smart 		}
1400a33c4f7bSJames Smart 	}
14013b5dd52aSJames Smart 
1402a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
14033b5dd52aSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1404a33c4f7bSJames Smart 	kfree(bmp);
14053b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
14063b5dd52aSJames Smart 	lpfc_nlp_put(ndlp);
14073b5dd52aSJames Smart 	kfree(dd_data);
1408a33c4f7bSJames Smart 
1409a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
1410a33c4f7bSJames Smart 
1411a33c4f7bSJames Smart 	if (job) {
14123b5dd52aSJames Smart 		job->reply->result = rc;
14133b5dd52aSJames Smart 		job->job_done(job);
1414a33c4f7bSJames Smart 	}
14153b5dd52aSJames Smart 	return;
14163b5dd52aSJames Smart }
14173b5dd52aSJames Smart 
14183b5dd52aSJames Smart /**
14193b5dd52aSJames Smart  * lpfc_issue_ct_rsp - issue a ct response
14203b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
14213b5dd52aSJames Smart  * @job: Pointer to the job object.
14223b5dd52aSJames Smart  * @tag: tag index value into the ports context exchange array.
14233b5dd52aSJames Smart  * @bmp: Pointer to a dma buffer descriptor.
14243b5dd52aSJames Smart  * @num_entry: Number of enties in the bde.
14253b5dd52aSJames Smart  **/
14263b5dd52aSJames Smart static int
14273b5dd52aSJames Smart lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1428a33c4f7bSJames Smart 		  struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1429a33c4f7bSJames Smart 		  int num_entry)
14303b5dd52aSJames Smart {
14313b5dd52aSJames Smart 	IOCB_t *icmd;
14323b5dd52aSJames Smart 	struct lpfc_iocbq *ctiocb = NULL;
14333b5dd52aSJames Smart 	int rc = 0;
14343b5dd52aSJames Smart 	struct lpfc_nodelist *ndlp = NULL;
14353b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
14363b5dd52aSJames Smart 	uint32_t creg_val;
14373b5dd52aSJames Smart 
14383b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
14393b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
14403b5dd52aSJames Smart 	if (!dd_data) {
14413b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
14423b5dd52aSJames Smart 				"2736 Failed allocation of dd_data\n");
14433b5dd52aSJames Smart 		rc = -ENOMEM;
14443b5dd52aSJames Smart 		goto no_dd_data;
14453b5dd52aSJames Smart 	}
14463b5dd52aSJames Smart 
14473b5dd52aSJames Smart 	/* Allocate buffer for  command iocb */
14483b5dd52aSJames Smart 	ctiocb = lpfc_sli_get_iocbq(phba);
14493b5dd52aSJames Smart 	if (!ctiocb) {
1450d439d286SJames Smart 		rc = -ENOMEM;
14513b5dd52aSJames Smart 		goto no_ctiocb;
14523b5dd52aSJames Smart 	}
14533b5dd52aSJames Smart 
14543b5dd52aSJames Smart 	icmd = &ctiocb->iocb;
14553b5dd52aSJames Smart 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
14563b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
14573b5dd52aSJames Smart 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
14583b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
14593b5dd52aSJames Smart 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
14603b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
14613b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14623b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
14633b5dd52aSJames Smart 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
14643b5dd52aSJames Smart 
14653b5dd52aSJames Smart 	/* Fill in rest of iocb */
14663b5dd52aSJames Smart 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
14673b5dd52aSJames Smart 	icmd->ulpBdeCount = 1;
14683b5dd52aSJames Smart 	icmd->ulpLe = 1;
14693b5dd52aSJames Smart 	icmd->ulpClass = CLASS3;
14703b5dd52aSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
14713b5dd52aSJames Smart 		/* Do not issue unsol response if oxid not marked as valid */
14726dd9e31cSJames Smart 		if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
14733b5dd52aSJames Smart 			rc = IOCB_ERROR;
14743b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
14753b5dd52aSJames Smart 		}
14767851fe2cSJames Smart 		icmd->ulpContext = phba->ct_ctx[tag].rxid;
14777851fe2cSJames Smart 		icmd->unsli3.rcvsli3.ox_id = phba->ct_ctx[tag].oxid;
14783b5dd52aSJames Smart 		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
14793b5dd52aSJames Smart 		if (!ndlp) {
14803b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14813b5dd52aSJames Smart 				 "2721 ndlp null for oxid %x SID %x\n",
14823b5dd52aSJames Smart 					icmd->ulpContext,
14833b5dd52aSJames Smart 					phba->ct_ctx[tag].SID);
14843b5dd52aSJames Smart 			rc = IOCB_ERROR;
14853b5dd52aSJames Smart 			goto issue_ct_rsp_exit;
14863b5dd52aSJames Smart 		}
1487589a52d6SJames Smart 
1488589a52d6SJames Smart 		/* Check if the ndlp is active */
1489589a52d6SJames Smart 		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1490a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1491589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1492589a52d6SJames Smart 		}
1493589a52d6SJames Smart 
1494589a52d6SJames Smart 		/* get a refernece count so the ndlp doesn't go away while
1495589a52d6SJames Smart 		 * we respond
1496589a52d6SJames Smart 		 */
1497589a52d6SJames Smart 		if (!lpfc_nlp_get(ndlp)) {
1498a33c4f7bSJames Smart 			rc = IOCB_ERROR;
1499589a52d6SJames Smart 			goto issue_ct_rsp_exit;
1500589a52d6SJames Smart 		}
1501589a52d6SJames Smart 
15027851fe2cSJames Smart 		icmd->un.ulpWord[3] =
15036d368e53SJames Smart 				phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15046d368e53SJames Smart 
15053b5dd52aSJames Smart 		/* The exchange is done, mark the entry as invalid */
15066dd9e31cSJames Smart 		phba->ct_ctx[tag].valid = UNSOL_INVALID;
15073b5dd52aSJames Smart 	} else
15083b5dd52aSJames Smart 		icmd->ulpContext = (ushort) tag;
15093b5dd52aSJames Smart 
15103b5dd52aSJames Smart 	icmd->ulpTimeout = phba->fc_ratov * 2;
15113b5dd52aSJames Smart 
15123b5dd52aSJames Smart 	/* Xmit CT response on exchange <xid> */
15133b5dd52aSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
15147851fe2cSJames Smart 		"2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
15157851fe2cSJames Smart 		icmd->ulpContext, icmd->ulpIoTag, tag, phba->link_state);
15163b5dd52aSJames Smart 
15173b5dd52aSJames Smart 	ctiocb->iocb_cmpl = NULL;
15183b5dd52aSJames Smart 	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
15193b5dd52aSJames Smart 	ctiocb->vport = phba->pport;
1520a33c4f7bSJames Smart 	ctiocb->context1 = dd_data;
1521a33c4f7bSJames Smart 	ctiocb->context2 = cmp;
15223b5dd52aSJames Smart 	ctiocb->context3 = bmp;
1523*d5ce53b7SJames Smart 	ctiocb->context_un.ndlp = ndlp;
15243b5dd52aSJames Smart 	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1525a33c4f7bSJames Smart 
15263b5dd52aSJames Smart 	dd_data->type = TYPE_IOCB;
1527a33c4f7bSJames Smart 	dd_data->set_job = job;
15283b5dd52aSJames Smart 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
15293b5dd52aSJames Smart 	dd_data->context_un.iocb.ndlp = ndlp;
1530a33c4f7bSJames Smart 	dd_data->context_un.iocb.rmp = NULL;
1531a33c4f7bSJames Smart 	job->dd_data = dd_data;
15323b5dd52aSJames Smart 
15333b5dd52aSJames Smart 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
15349940b97bSJames Smart 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
15359940b97bSJames Smart 			rc = -IOCB_ERROR;
15369940b97bSJames Smart 			goto issue_ct_rsp_exit;
15379940b97bSJames Smart 		}
15383b5dd52aSJames Smart 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
15393b5dd52aSJames Smart 		writel(creg_val, phba->HCregaddr);
15403b5dd52aSJames Smart 		readl(phba->HCregaddr); /* flush */
15413b5dd52aSJames Smart 	}
15423b5dd52aSJames Smart 
15433b5dd52aSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15443b5dd52aSJames Smart 
15453b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
15463b5dd52aSJames Smart 		return 0; /* done for now */
15473b5dd52aSJames Smart 
15483b5dd52aSJames Smart issue_ct_rsp_exit:
15493b5dd52aSJames Smart 	lpfc_sli_release_iocbq(phba, ctiocb);
15503b5dd52aSJames Smart no_ctiocb:
15513b5dd52aSJames Smart 	kfree(dd_data);
15523b5dd52aSJames Smart no_dd_data:
15533b5dd52aSJames Smart 	return rc;
15543b5dd52aSJames Smart }
15553b5dd52aSJames Smart 
15563b5dd52aSJames Smart /**
15573b5dd52aSJames Smart  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
15583b5dd52aSJames Smart  * @job: SEND_MGMT_RESP fc_bsg_job
15593b5dd52aSJames Smart  **/
15603b5dd52aSJames Smart static int
15613b5dd52aSJames Smart lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
15623b5dd52aSJames Smart {
15633b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
15643b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
15653b5dd52aSJames Smart 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
15663b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
15673b5dd52aSJames Smart 	struct ulp_bde64 *bpl;
1568a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1569a33c4f7bSJames Smart 	int bpl_entries;
15703b5dd52aSJames Smart 	uint32_t tag = mgmt_resp->tag;
15713b5dd52aSJames Smart 	unsigned long reqbfrcnt =
15723b5dd52aSJames Smart 			(unsigned long)job->request_payload.payload_len;
15733b5dd52aSJames Smart 	int rc = 0;
15743b5dd52aSJames Smart 
15753b5dd52aSJames Smart 	/* in case no data is transferred */
15763b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
15773b5dd52aSJames Smart 
15783b5dd52aSJames Smart 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
15793b5dd52aSJames Smart 		rc = -ERANGE;
15803b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
15813b5dd52aSJames Smart 	}
15823b5dd52aSJames Smart 
15833b5dd52aSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
15843b5dd52aSJames Smart 	if (!bmp) {
15853b5dd52aSJames Smart 		rc = -ENOMEM;
15863b5dd52aSJames Smart 		goto send_mgmt_rsp_exit;
15873b5dd52aSJames Smart 	}
15883b5dd52aSJames Smart 
15893b5dd52aSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
15903b5dd52aSJames Smart 	if (!bmp->virt) {
15913b5dd52aSJames Smart 		rc = -ENOMEM;
15923b5dd52aSJames Smart 		goto send_mgmt_rsp_free_bmp;
15933b5dd52aSJames Smart 	}
15943b5dd52aSJames Smart 
15953b5dd52aSJames Smart 	INIT_LIST_HEAD(&bmp->list);
15963b5dd52aSJames Smart 	bpl = (struct ulp_bde64 *) bmp->virt;
1597a33c4f7bSJames Smart 	bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1598a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1599a33c4f7bSJames Smart 				     1, bpl, &bpl_entries);
1600a33c4f7bSJames Smart 	if (!cmp) {
1601a33c4f7bSJames Smart 		rc = -ENOMEM;
1602a33c4f7bSJames Smart 		goto send_mgmt_rsp_free_bmp;
16033b5dd52aSJames Smart 	}
1604a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
1605a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
16063b5dd52aSJames Smart 
1607a33c4f7bSJames Smart 	rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
16083b5dd52aSJames Smart 
16093b5dd52aSJames Smart 	if (rc == IOCB_SUCCESS)
16103b5dd52aSJames Smart 		return 0; /* done for now */
16113b5dd52aSJames Smart 
16123b5dd52aSJames Smart 	rc = -EACCES;
1613a33c4f7bSJames Smart 
1614a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
16153b5dd52aSJames Smart 
16163b5dd52aSJames Smart send_mgmt_rsp_free_bmp:
1617a33c4f7bSJames Smart 	if (bmp->virt)
1618a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
16193b5dd52aSJames Smart 	kfree(bmp);
16203b5dd52aSJames Smart send_mgmt_rsp_exit:
16213b5dd52aSJames Smart 	/* make error code available to userspace */
16223b5dd52aSJames Smart 	job->reply->result = rc;
16233b5dd52aSJames Smart 	job->dd_data = NULL;
16243b5dd52aSJames Smart 	return rc;
16253b5dd52aSJames Smart }
16263b5dd52aSJames Smart 
16273b5dd52aSJames Smart /**
16287ad20aa9SJames Smart  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
16297ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
16303b5dd52aSJames Smart  *
16317ad20aa9SJames Smart  * This function is responsible for preparing driver for diag loopback
16327ad20aa9SJames Smart  * on device.
16333b5dd52aSJames Smart  */
16343b5dd52aSJames Smart static int
163588a2cfbbSJames Smart lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
16363b5dd52aSJames Smart {
16373b5dd52aSJames Smart 	struct lpfc_vport **vports;
16387ad20aa9SJames Smart 	struct Scsi_Host *shost;
16397ad20aa9SJames Smart 	struct lpfc_sli *psli;
16407ad20aa9SJames Smart 	struct lpfc_sli_ring *pring;
16413b5dd52aSJames Smart 	int i = 0;
16423b5dd52aSJames Smart 
16437ad20aa9SJames Smart 	psli = &phba->sli;
16447ad20aa9SJames Smart 	if (!psli)
16457ad20aa9SJames Smart 		return -ENODEV;
16463b5dd52aSJames Smart 
16477ad20aa9SJames Smart 	pring = &psli->ring[LPFC_FCP_RING];
16487ad20aa9SJames Smart 	if (!pring)
16497ad20aa9SJames Smart 		return -ENODEV;
16503b5dd52aSJames Smart 
16513b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
16523b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
16537ad20aa9SJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
16547ad20aa9SJames Smart 		return -EACCES;
16553b5dd52aSJames Smart 
16563b5dd52aSJames Smart 	vports = lpfc_create_vport_work_array(phba);
16573b5dd52aSJames Smart 	if (vports) {
16583b5dd52aSJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16593b5dd52aSJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
16603b5dd52aSJames Smart 			scsi_block_requests(shost);
16613b5dd52aSJames Smart 		}
16623b5dd52aSJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
16633b5dd52aSJames Smart 	} else {
16643b5dd52aSJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
16653b5dd52aSJames Smart 		scsi_block_requests(shost);
16663b5dd52aSJames Smart 	}
16673b5dd52aSJames Smart 
16680e9bb8d7SJames Smart 	while (!list_empty(&pring->txcmplq)) {
16693b5dd52aSJames Smart 		if (i++ > 500)  /* wait up to 5 seconds */
16703b5dd52aSJames Smart 			break;
16713b5dd52aSJames Smart 		msleep(10);
16723b5dd52aSJames Smart 	}
16737ad20aa9SJames Smart 	return 0;
16747ad20aa9SJames Smart }
16753b5dd52aSJames Smart 
16767ad20aa9SJames Smart /**
16777ad20aa9SJames Smart  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
16787ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
16797ad20aa9SJames Smart  *
16807ad20aa9SJames Smart  * This function is responsible for driver exit processing of setting up
16817ad20aa9SJames Smart  * diag loopback mode on device.
16827ad20aa9SJames Smart  */
16837ad20aa9SJames Smart static void
16847ad20aa9SJames Smart lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
16857ad20aa9SJames Smart {
16867ad20aa9SJames Smart 	struct Scsi_Host *shost;
16877ad20aa9SJames Smart 	struct lpfc_vport **vports;
16887ad20aa9SJames Smart 	int i;
16897ad20aa9SJames Smart 
16907ad20aa9SJames Smart 	vports = lpfc_create_vport_work_array(phba);
16917ad20aa9SJames Smart 	if (vports) {
16927ad20aa9SJames Smart 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
16937ad20aa9SJames Smart 			shost = lpfc_shost_from_vport(vports[i]);
16947ad20aa9SJames Smart 			scsi_unblock_requests(shost);
16957ad20aa9SJames Smart 		}
16967ad20aa9SJames Smart 		lpfc_destroy_vport_work_array(phba, vports);
16977ad20aa9SJames Smart 	} else {
16987ad20aa9SJames Smart 		shost = lpfc_shost_from_vport(phba->pport);
16997ad20aa9SJames Smart 		scsi_unblock_requests(shost);
17007ad20aa9SJames Smart 	}
17017ad20aa9SJames Smart 	return;
17027ad20aa9SJames Smart }
17037ad20aa9SJames Smart 
17047ad20aa9SJames Smart /**
17057ad20aa9SJames Smart  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
17067ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
17077ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
17087ad20aa9SJames Smart  *
17097ad20aa9SJames Smart  * This function is responsible for placing an sli3  port into diagnostic
17107ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
17117ad20aa9SJames Smart  * All new scsi requests are blocked, a small delay is used to allow the
17127ad20aa9SJames Smart  * scsi requests to complete then the link is brought down. If the link is
17137ad20aa9SJames Smart  * is placed in loopback mode then scsi requests are again allowed
17147ad20aa9SJames Smart  * so the scsi mid-layer doesn't give up on the port.
17157ad20aa9SJames Smart  * All of this is done in-line.
17167ad20aa9SJames Smart  */
17177ad20aa9SJames Smart static int
17187ad20aa9SJames Smart lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
17197ad20aa9SJames Smart {
17207ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
17217ad20aa9SJames Smart 	uint32_t link_flags;
17227ad20aa9SJames Smart 	uint32_t timeout;
17231b51197dSJames Smart 	LPFC_MBOXQ_t *pmboxq  = NULL;
1724b76f2dc9SJames Smart 	int mbxstatus = MBX_SUCCESS;
17257ad20aa9SJames Smart 	int i = 0;
17267ad20aa9SJames Smart 	int rc = 0;
17277ad20aa9SJames Smart 
17287ad20aa9SJames Smart 	/* no data to return just the return code */
17297ad20aa9SJames Smart 	job->reply->reply_payload_rcv_len = 0;
17307ad20aa9SJames Smart 
17317ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
17327ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
17337ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
17347ad20aa9SJames Smart 				"2738 Received DIAG MODE request size:%d "
17357ad20aa9SJames Smart 				"below the minimum size:%d\n",
17367ad20aa9SJames Smart 				job->request_len,
17377ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
17387ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
17397ad20aa9SJames Smart 		rc = -EINVAL;
17407ad20aa9SJames Smart 		goto job_error;
17417ad20aa9SJames Smart 	}
17427ad20aa9SJames Smart 
174388a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
17447ad20aa9SJames Smart 	if (rc)
17457ad20aa9SJames Smart 		goto job_error;
17467ad20aa9SJames Smart 
17477ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
17487ad20aa9SJames Smart 	loopback_mode = (struct diag_mode_set *)
17497ad20aa9SJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
17507ad20aa9SJames Smart 	link_flags = loopback_mode->type;
17517ad20aa9SJames Smart 	timeout = loopback_mode->timeout * 100;
17527ad20aa9SJames Smart 
17537ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17547ad20aa9SJames Smart 	if (!pmboxq) {
17557ad20aa9SJames Smart 		rc = -ENOMEM;
17567ad20aa9SJames Smart 		goto loopback_mode_exit;
17577ad20aa9SJames Smart 	}
17583b5dd52aSJames Smart 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
17593b5dd52aSJames Smart 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
17603b5dd52aSJames Smart 	pmboxq->u.mb.mbxOwner = OWN_HOST;
17613b5dd52aSJames Smart 
17623b5dd52aSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
17633b5dd52aSJames Smart 
17643b5dd52aSJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
17653b5dd52aSJames Smart 		/* wait for link down before proceeding */
17663b5dd52aSJames Smart 		i = 0;
17673b5dd52aSJames Smart 		while (phba->link_state != LPFC_LINK_DOWN) {
17683b5dd52aSJames Smart 			if (i++ > timeout) {
17693b5dd52aSJames Smart 				rc = -ETIMEDOUT;
17703b5dd52aSJames Smart 				goto loopback_mode_exit;
17713b5dd52aSJames Smart 			}
17723b5dd52aSJames Smart 			msleep(10);
17733b5dd52aSJames Smart 		}
17743b5dd52aSJames Smart 
17753b5dd52aSJames Smart 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
17763b5dd52aSJames Smart 		if (link_flags == INTERNAL_LOOP_BACK)
17773b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
17783b5dd52aSJames Smart 		else
17793b5dd52aSJames Smart 			pmboxq->u.mb.un.varInitLnk.link_flags =
17803b5dd52aSJames Smart 				FLAGS_TOPOLOGY_MODE_LOOP;
17813b5dd52aSJames Smart 
17823b5dd52aSJames Smart 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
17833b5dd52aSJames Smart 		pmboxq->u.mb.mbxOwner = OWN_HOST;
17843b5dd52aSJames Smart 
17853b5dd52aSJames Smart 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
17863b5dd52aSJames Smart 						     LPFC_MBOX_TMO);
17873b5dd52aSJames Smart 
17883b5dd52aSJames Smart 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
17893b5dd52aSJames Smart 			rc = -ENODEV;
17903b5dd52aSJames Smart 		else {
17911b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
17923b5dd52aSJames Smart 			phba->link_flag |= LS_LOOPBACK_MODE;
17931b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
17943b5dd52aSJames Smart 			/* wait for the link attention interrupt */
17953b5dd52aSJames Smart 			msleep(100);
17963b5dd52aSJames Smart 
17973b5dd52aSJames Smart 			i = 0;
17983b5dd52aSJames Smart 			while (phba->link_state != LPFC_HBA_READY) {
17993b5dd52aSJames Smart 				if (i++ > timeout) {
18003b5dd52aSJames Smart 					rc = -ETIMEDOUT;
18013b5dd52aSJames Smart 					break;
18023b5dd52aSJames Smart 				}
18033b5dd52aSJames Smart 
18043b5dd52aSJames Smart 				msleep(10);
18053b5dd52aSJames Smart 			}
18063b5dd52aSJames Smart 		}
18073b5dd52aSJames Smart 
18083b5dd52aSJames Smart 	} else
18093b5dd52aSJames Smart 		rc = -ENODEV;
18103b5dd52aSJames Smart 
18113b5dd52aSJames Smart loopback_mode_exit:
18127ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
18133b5dd52aSJames Smart 
18143b5dd52aSJames Smart 	/*
18153b5dd52aSJames Smart 	 * Let SLI layer release mboxq if mbox command completed after timeout.
18163b5dd52aSJames Smart 	 */
18171b51197dSJames Smart 	if (pmboxq && mbxstatus != MBX_TIMEOUT)
18183b5dd52aSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
18193b5dd52aSJames Smart 
18203b5dd52aSJames Smart job_error:
18213b5dd52aSJames Smart 	/* make error code available to userspace */
18223b5dd52aSJames Smart 	job->reply->result = rc;
18233b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
18243b5dd52aSJames Smart 	if (rc == 0)
18253b5dd52aSJames Smart 		job->job_done(job);
18263b5dd52aSJames Smart 	return rc;
18273b5dd52aSJames Smart }
18283b5dd52aSJames Smart 
18293b5dd52aSJames Smart /**
18307ad20aa9SJames Smart  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
18317ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
18327ad20aa9SJames Smart  * @diag: Flag for set link to diag or nomral operation state.
18337ad20aa9SJames Smart  *
18347ad20aa9SJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
18357ad20aa9SJames Smart  * link to either diag state or normal operation state.
18367ad20aa9SJames Smart  */
18377ad20aa9SJames Smart static int
18387ad20aa9SJames Smart lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
18397ad20aa9SJames Smart {
18407ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
18417ad20aa9SJames Smart 	struct lpfc_mbx_set_link_diag_state *link_diag_state;
18427ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
18437ad20aa9SJames Smart 	int mbxstatus = MBX_SUCCESS, rc;
18447ad20aa9SJames Smart 
18457ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
18467ad20aa9SJames Smart 	if (!pmboxq)
18477ad20aa9SJames Smart 		return -ENOMEM;
18487ad20aa9SJames Smart 
18497ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
18507ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
18517ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
18527ad20aa9SJames Smart 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
18537ad20aa9SJames Smart 				req_len, LPFC_SLI4_MBX_EMBED);
18547ad20aa9SJames Smart 	if (alloc_len != req_len) {
18557ad20aa9SJames Smart 		rc = -ENOMEM;
18567ad20aa9SJames Smart 		goto link_diag_state_set_out;
18577ad20aa9SJames Smart 	}
18581b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
18591b51197dSJames Smart 			"3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
18601b51197dSJames Smart 			diag, phba->sli4_hba.lnk_info.lnk_tp,
18611b51197dSJames Smart 			phba->sli4_hba.lnk_info.lnk_no);
18621b51197dSJames Smart 
18637ad20aa9SJames Smart 	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
18649731592bSJames Smart 	bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
18659731592bSJames Smart 	       LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
18667ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
18671b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
18687ad20aa9SJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
18691b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
18707ad20aa9SJames Smart 	if (diag)
18717ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
18727ad20aa9SJames Smart 		       &link_diag_state->u.req, 1);
18737ad20aa9SJames Smart 	else
18747ad20aa9SJames Smart 		bf_set(lpfc_mbx_set_diag_state_diag,
18757ad20aa9SJames Smart 		       &link_diag_state->u.req, 0);
18767ad20aa9SJames Smart 
18777ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
18787ad20aa9SJames Smart 
18797ad20aa9SJames Smart 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
18807ad20aa9SJames Smart 		rc = 0;
18817ad20aa9SJames Smart 	else
18827ad20aa9SJames Smart 		rc = -ENODEV;
18837ad20aa9SJames Smart 
18847ad20aa9SJames Smart link_diag_state_set_out:
18857ad20aa9SJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
18867ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
18877ad20aa9SJames Smart 
18887ad20aa9SJames Smart 	return rc;
18897ad20aa9SJames Smart }
18907ad20aa9SJames Smart 
18917ad20aa9SJames Smart /**
18921b51197dSJames Smart  * lpfc_sli4_bsg_set_internal_loopback - set sli4 internal loopback diagnostic
18931b51197dSJames Smart  * @phba: Pointer to HBA context object.
18941b51197dSJames Smart  *
18951b51197dSJames Smart  * This function is responsible for issuing a sli4 mailbox command for setting
18961b51197dSJames Smart  * up internal loopback diagnostic.
18971b51197dSJames Smart  */
18981b51197dSJames Smart static int
18991b51197dSJames Smart lpfc_sli4_bsg_set_internal_loopback(struct lpfc_hba *phba)
19001b51197dSJames Smart {
19011b51197dSJames Smart 	LPFC_MBOXQ_t *pmboxq;
19021b51197dSJames Smart 	uint32_t req_len, alloc_len;
19031b51197dSJames Smart 	struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
19041b51197dSJames Smart 	int mbxstatus = MBX_SUCCESS, rc = 0;
19051b51197dSJames Smart 
19061b51197dSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
19071b51197dSJames Smart 	if (!pmboxq)
19081b51197dSJames Smart 		return -ENOMEM;
19091b51197dSJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
19101b51197dSJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
19111b51197dSJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
19121b51197dSJames Smart 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
19131b51197dSJames Smart 				req_len, LPFC_SLI4_MBX_EMBED);
19141b51197dSJames Smart 	if (alloc_len != req_len) {
19151b51197dSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
19161b51197dSJames Smart 		return -ENOMEM;
19171b51197dSJames Smart 	}
19181b51197dSJames Smart 	link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
19191b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_num,
19201b51197dSJames Smart 	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_no);
19211b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_state_link_type,
19221b51197dSJames Smart 	       &link_diag_loopback->u.req, phba->sli4_hba.lnk_info.lnk_tp);
19231b51197dSJames Smart 	bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
19243ef6d24cSJames Smart 	       LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
19251b51197dSJames Smart 
19261b51197dSJames Smart 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
19271b51197dSJames Smart 	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
19281b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
19291b51197dSJames Smart 				"3127 Failed setup loopback mode mailbox "
19301b51197dSJames Smart 				"command, rc:x%x, status:x%x\n", mbxstatus,
19311b51197dSJames Smart 				pmboxq->u.mb.mbxStatus);
19321b51197dSJames Smart 		rc = -ENODEV;
19331b51197dSJames Smart 	}
19341b51197dSJames Smart 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
19351b51197dSJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
19361b51197dSJames Smart 	return rc;
19371b51197dSJames Smart }
19381b51197dSJames Smart 
19391b51197dSJames Smart /**
19401b51197dSJames Smart  * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
19411b51197dSJames Smart  * @phba: Pointer to HBA context object.
19421b51197dSJames Smart  *
19431b51197dSJames Smart  * This function set up SLI4 FC port registrations for diagnostic run, which
19441b51197dSJames Smart  * includes all the rpis, vfi, and also vpi.
19451b51197dSJames Smart  */
19461b51197dSJames Smart static int
19471b51197dSJames Smart lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
19481b51197dSJames Smart {
19491b51197dSJames Smart 	int rc;
19501b51197dSJames Smart 
19511b51197dSJames Smart 	if (phba->pport->fc_flag & FC_VFI_REGISTERED) {
19521b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
19531b51197dSJames Smart 				"3136 Port still had vfi registered: "
19541b51197dSJames Smart 				"mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
19551b51197dSJames Smart 				phba->pport->fc_myDID, phba->fcf.fcfi,
19561b51197dSJames Smart 				phba->sli4_hba.vfi_ids[phba->pport->vfi],
19571b51197dSJames Smart 				phba->vpi_ids[phba->pport->vpi]);
19581b51197dSJames Smart 		return -EINVAL;
19591b51197dSJames Smart 	}
19601b51197dSJames Smart 	rc = lpfc_issue_reg_vfi(phba->pport);
19611b51197dSJames Smart 	return rc;
19621b51197dSJames Smart }
19631b51197dSJames Smart 
19641b51197dSJames Smart /**
19657ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
19667ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
19677ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
19687ad20aa9SJames Smart  *
19697ad20aa9SJames Smart  * This function is responsible for placing an sli4 port into diagnostic
19707ad20aa9SJames Smart  * loopback mode in order to perform a diagnostic loopback test.
19717ad20aa9SJames Smart  */
19727ad20aa9SJames Smart static int
19737ad20aa9SJames Smart lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
19747ad20aa9SJames Smart {
19757ad20aa9SJames Smart 	struct diag_mode_set *loopback_mode;
19761b51197dSJames Smart 	uint32_t link_flags, timeout;
19771b51197dSJames Smart 	int i, rc = 0;
19787ad20aa9SJames Smart 
19797ad20aa9SJames Smart 	/* no data to return just the return code */
19807ad20aa9SJames Smart 	job->reply->reply_payload_rcv_len = 0;
19817ad20aa9SJames Smart 
19827ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
19837ad20aa9SJames Smart 	    sizeof(struct diag_mode_set)) {
19847ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
19857ad20aa9SJames Smart 				"3011 Received DIAG MODE request size:%d "
19867ad20aa9SJames Smart 				"below the minimum size:%d\n",
19877ad20aa9SJames Smart 				job->request_len,
19887ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
19897ad20aa9SJames Smart 				sizeof(struct diag_mode_set)));
19907ad20aa9SJames Smart 		rc = -EINVAL;
19917ad20aa9SJames Smart 		goto job_error;
19927ad20aa9SJames Smart 	}
19937ad20aa9SJames Smart 
199488a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
19957ad20aa9SJames Smart 	if (rc)
19967ad20aa9SJames Smart 		goto job_error;
19977ad20aa9SJames Smart 
19981b51197dSJames Smart 	/* indicate we are in loobpack diagnostic mode */
19991b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
20001b51197dSJames Smart 	phba->link_flag |= LS_LOOPBACK_MODE;
20011b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
20021b51197dSJames Smart 
20031b51197dSJames Smart 	/* reset port to start frome scratch */
20041b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
20051b51197dSJames Smart 	if (rc)
20061b51197dSJames Smart 		goto job_error;
20071b51197dSJames Smart 
20087ad20aa9SJames Smart 	/* bring the link to diagnostic mode */
20091b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20101b51197dSJames Smart 			"3129 Bring link to diagnostic state.\n");
20117ad20aa9SJames Smart 	loopback_mode = (struct diag_mode_set *)
20127ad20aa9SJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
20137ad20aa9SJames Smart 	link_flags = loopback_mode->type;
20147ad20aa9SJames Smart 	timeout = loopback_mode->timeout * 100;
20157ad20aa9SJames Smart 
20167ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
20171b51197dSJames Smart 	if (rc) {
20181b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
20191b51197dSJames Smart 				"3130 Failed to bring link to diagnostic "
20201b51197dSJames Smart 				"state, rc:x%x\n", rc);
20217ad20aa9SJames Smart 		goto loopback_mode_exit;
20221b51197dSJames Smart 	}
20237ad20aa9SJames Smart 
20247ad20aa9SJames Smart 	/* wait for link down before proceeding */
20257ad20aa9SJames Smart 	i = 0;
20267ad20aa9SJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
20277ad20aa9SJames Smart 		if (i++ > timeout) {
20287ad20aa9SJames Smart 			rc = -ETIMEDOUT;
20291b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20301b51197dSJames Smart 					"3131 Timeout waiting for link to "
20311b51197dSJames Smart 					"diagnostic mode, timeout:%d ms\n",
20321b51197dSJames Smart 					timeout * 10);
20337ad20aa9SJames Smart 			goto loopback_mode_exit;
20347ad20aa9SJames Smart 		}
20357ad20aa9SJames Smart 		msleep(10);
20367ad20aa9SJames Smart 	}
20377ad20aa9SJames Smart 
20381b51197dSJames Smart 	/* set up loopback mode */
20391b51197dSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20401b51197dSJames Smart 			"3132 Set up loopback mode:x%x\n", link_flags);
20411b51197dSJames Smart 
20421b51197dSJames Smart 	if (link_flags == INTERNAL_LOOP_BACK)
20431b51197dSJames Smart 		rc = lpfc_sli4_bsg_set_internal_loopback(phba);
20441b51197dSJames Smart 	else if (link_flags == EXTERNAL_LOOP_BACK)
20451b51197dSJames Smart 		rc = lpfc_hba_init_link_fc_topology(phba,
20461b51197dSJames Smart 						    FLAGS_TOPOLOGY_MODE_PT_PT,
20471b51197dSJames Smart 						    MBX_NOWAIT);
20487ad20aa9SJames Smart 	else {
20491b51197dSJames Smart 		rc = -EINVAL;
20501b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
20511b51197dSJames Smart 				"3141 Loopback mode:x%x not supported\n",
20521b51197dSJames Smart 				link_flags);
20531b51197dSJames Smart 		goto loopback_mode_exit;
20541b51197dSJames Smart 	}
20551b51197dSJames Smart 
20561b51197dSJames Smart 	if (!rc) {
20577ad20aa9SJames Smart 		/* wait for the link attention interrupt */
20587ad20aa9SJames Smart 		msleep(100);
20597ad20aa9SJames Smart 		i = 0;
20601b51197dSJames Smart 		while (phba->link_state < LPFC_LINK_UP) {
20611b51197dSJames Smart 			if (i++ > timeout) {
20621b51197dSJames Smart 				rc = -ETIMEDOUT;
20631b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20641b51197dSJames Smart 					"3137 Timeout waiting for link up "
20651b51197dSJames Smart 					"in loopback mode, timeout:%d ms\n",
20661b51197dSJames Smart 					timeout * 10);
20671b51197dSJames Smart 				break;
20681b51197dSJames Smart 			}
20691b51197dSJames Smart 			msleep(10);
20701b51197dSJames Smart 		}
20711b51197dSJames Smart 	}
20721b51197dSJames Smart 
20731b51197dSJames Smart 	/* port resource registration setup for loopback diagnostic */
20741b51197dSJames Smart 	if (!rc) {
20751b51197dSJames Smart 		/* set up a none zero myDID for loopback test */
20761b51197dSJames Smart 		phba->pport->fc_myDID = 1;
20771b51197dSJames Smart 		rc = lpfc_sli4_diag_fcport_reg_setup(phba);
20781b51197dSJames Smart 	} else
20791b51197dSJames Smart 		goto loopback_mode_exit;
20801b51197dSJames Smart 
20811b51197dSJames Smart 	if (!rc) {
20821b51197dSJames Smart 		/* wait for the port ready */
20831b51197dSJames Smart 		msleep(100);
20841b51197dSJames Smart 		i = 0;
20857ad20aa9SJames Smart 		while (phba->link_state != LPFC_HBA_READY) {
20867ad20aa9SJames Smart 			if (i++ > timeout) {
20877ad20aa9SJames Smart 				rc = -ETIMEDOUT;
20881b51197dSJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
20891b51197dSJames Smart 					"3133 Timeout waiting for port "
20901b51197dSJames Smart 					"loopback mode ready, timeout:%d ms\n",
20911b51197dSJames Smart 					timeout * 10);
20927ad20aa9SJames Smart 				break;
20937ad20aa9SJames Smart 			}
20947ad20aa9SJames Smart 			msleep(10);
20957ad20aa9SJames Smart 		}
20967ad20aa9SJames Smart 	}
20977ad20aa9SJames Smart 
20987ad20aa9SJames Smart loopback_mode_exit:
20991b51197dSJames Smart 	/* clear loopback diagnostic mode */
21001b51197dSJames Smart 	if (rc) {
21011b51197dSJames Smart 		spin_lock_irq(&phba->hbalock);
21021b51197dSJames Smart 		phba->link_flag &= ~LS_LOOPBACK_MODE;
21031b51197dSJames Smart 		spin_unlock_irq(&phba->hbalock);
21041b51197dSJames Smart 	}
21057ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
21067ad20aa9SJames Smart 
21077ad20aa9SJames Smart job_error:
21087ad20aa9SJames Smart 	/* make error code available to userspace */
21097ad20aa9SJames Smart 	job->reply->result = rc;
21107ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
21117ad20aa9SJames Smart 	if (rc == 0)
21127ad20aa9SJames Smart 		job->job_done(job);
21137ad20aa9SJames Smart 	return rc;
21147ad20aa9SJames Smart }
21157ad20aa9SJames Smart 
21167ad20aa9SJames Smart /**
21177ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
21187ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE
21197ad20aa9SJames Smart  *
21207ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
21217ad20aa9SJames Smart  * command from the user to proper driver action routines.
21227ad20aa9SJames Smart  */
21237ad20aa9SJames Smart static int
21247ad20aa9SJames Smart lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
21257ad20aa9SJames Smart {
21267ad20aa9SJames Smart 	struct Scsi_Host *shost;
21277ad20aa9SJames Smart 	struct lpfc_vport *vport;
21287ad20aa9SJames Smart 	struct lpfc_hba *phba;
21297ad20aa9SJames Smart 	int rc;
21307ad20aa9SJames Smart 
21317ad20aa9SJames Smart 	shost = job->shost;
21327ad20aa9SJames Smart 	if (!shost)
21337ad20aa9SJames Smart 		return -ENODEV;
21347ad20aa9SJames Smart 	vport = (struct lpfc_vport *)job->shost->hostdata;
21357ad20aa9SJames Smart 	if (!vport)
21367ad20aa9SJames Smart 		return -ENODEV;
21377ad20aa9SJames Smart 	phba = vport->phba;
21387ad20aa9SJames Smart 	if (!phba)
21397ad20aa9SJames Smart 		return -ENODEV;
21407ad20aa9SJames Smart 
21417ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
21427ad20aa9SJames Smart 		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
21437ad20aa9SJames Smart 	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
21447ad20aa9SJames Smart 		 LPFC_SLI_INTF_IF_TYPE_2)
21457ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
21467ad20aa9SJames Smart 	else
21477ad20aa9SJames Smart 		rc = -ENODEV;
21487ad20aa9SJames Smart 
21497ad20aa9SJames Smart 	return rc;
21507ad20aa9SJames Smart }
21517ad20aa9SJames Smart 
21527ad20aa9SJames Smart /**
21537ad20aa9SJames Smart  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
21547ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
21557ad20aa9SJames Smart  *
21567ad20aa9SJames Smart  * This function is responsible for responding to check and dispatch bsg diag
21577ad20aa9SJames Smart  * command from the user to proper driver action routines.
21587ad20aa9SJames Smart  */
21597ad20aa9SJames Smart static int
21607ad20aa9SJames Smart lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
21617ad20aa9SJames Smart {
21627ad20aa9SJames Smart 	struct Scsi_Host *shost;
21637ad20aa9SJames Smart 	struct lpfc_vport *vport;
21647ad20aa9SJames Smart 	struct lpfc_hba *phba;
21651b51197dSJames Smart 	struct diag_mode_set *loopback_mode_end_cmd;
21661b51197dSJames Smart 	uint32_t timeout;
21671b51197dSJames Smart 	int rc, i;
21687ad20aa9SJames Smart 
21697ad20aa9SJames Smart 	shost = job->shost;
21707ad20aa9SJames Smart 	if (!shost)
21717ad20aa9SJames Smart 		return -ENODEV;
21727ad20aa9SJames Smart 	vport = (struct lpfc_vport *)job->shost->hostdata;
21737ad20aa9SJames Smart 	if (!vport)
21747ad20aa9SJames Smart 		return -ENODEV;
21757ad20aa9SJames Smart 	phba = vport->phba;
21767ad20aa9SJames Smart 	if (!phba)
21777ad20aa9SJames Smart 		return -ENODEV;
21787ad20aa9SJames Smart 
21797ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
21807ad20aa9SJames Smart 		return -ENODEV;
21817ad20aa9SJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
21827ad20aa9SJames Smart 	    LPFC_SLI_INTF_IF_TYPE_2)
21837ad20aa9SJames Smart 		return -ENODEV;
21847ad20aa9SJames Smart 
21851b51197dSJames Smart 	/* clear loopback diagnostic mode */
21861b51197dSJames Smart 	spin_lock_irq(&phba->hbalock);
21871b51197dSJames Smart 	phba->link_flag &= ~LS_LOOPBACK_MODE;
21881b51197dSJames Smart 	spin_unlock_irq(&phba->hbalock);
21891b51197dSJames Smart 	loopback_mode_end_cmd = (struct diag_mode_set *)
21901b51197dSJames Smart 			job->request->rqst_data.h_vendor.vendor_cmd;
21911b51197dSJames Smart 	timeout = loopback_mode_end_cmd->timeout * 100;
21921b51197dSJames Smart 
21937ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
21941b51197dSJames Smart 	if (rc) {
21951b51197dSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
21961b51197dSJames Smart 				"3139 Failed to bring link to diagnostic "
21971b51197dSJames Smart 				"state, rc:x%x\n", rc);
21981b51197dSJames Smart 		goto loopback_mode_end_exit;
21991b51197dSJames Smart 	}
22007ad20aa9SJames Smart 
22011b51197dSJames Smart 	/* wait for link down before proceeding */
22021b51197dSJames Smart 	i = 0;
22031b51197dSJames Smart 	while (phba->link_state != LPFC_LINK_DOWN) {
22041b51197dSJames Smart 		if (i++ > timeout) {
22051b51197dSJames Smart 			rc = -ETIMEDOUT;
22061b51197dSJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
22071b51197dSJames Smart 					"3140 Timeout waiting for link to "
22081b51197dSJames Smart 					"diagnostic mode_end, timeout:%d ms\n",
22091b51197dSJames Smart 					timeout * 10);
22101b51197dSJames Smart 			/* there is nothing much we can do here */
22111b51197dSJames Smart 			break;
22121b51197dSJames Smart 		}
22131b51197dSJames Smart 		msleep(10);
22141b51197dSJames Smart 	}
22157ad20aa9SJames Smart 
22161b51197dSJames Smart 	/* reset port resource registrations */
22171b51197dSJames Smart 	rc = lpfc_selective_reset(phba);
22181b51197dSJames Smart 	phba->pport->fc_myDID = 0;
22191b51197dSJames Smart 
22201b51197dSJames Smart loopback_mode_end_exit:
22211b51197dSJames Smart 	/* make return code available to userspace */
22221b51197dSJames Smart 	job->reply->result = rc;
22231b51197dSJames Smart 	/* complete the job back to userspace if no error */
22241b51197dSJames Smart 	if (rc == 0)
22251b51197dSJames Smart 		job->job_done(job);
22267ad20aa9SJames Smart 	return rc;
22277ad20aa9SJames Smart }
22287ad20aa9SJames Smart 
22297ad20aa9SJames Smart /**
22307ad20aa9SJames Smart  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
22317ad20aa9SJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
22327ad20aa9SJames Smart  *
22337ad20aa9SJames Smart  * This function is to perform SLI4 diag link test request from the user
22347ad20aa9SJames Smart  * applicaiton.
22357ad20aa9SJames Smart  */
22367ad20aa9SJames Smart static int
22377ad20aa9SJames Smart lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
22387ad20aa9SJames Smart {
22397ad20aa9SJames Smart 	struct Scsi_Host *shost;
22407ad20aa9SJames Smart 	struct lpfc_vport *vport;
22417ad20aa9SJames Smart 	struct lpfc_hba *phba;
22427ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq;
22437ad20aa9SJames Smart 	struct sli4_link_diag *link_diag_test_cmd;
22447ad20aa9SJames Smart 	uint32_t req_len, alloc_len;
22457ad20aa9SJames Smart 	uint32_t timeout;
22467ad20aa9SJames Smart 	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
22477ad20aa9SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
22487ad20aa9SJames Smart 	uint32_t shdr_status, shdr_add_status;
22497ad20aa9SJames Smart 	struct diag_status *diag_status_reply;
22507ad20aa9SJames Smart 	int mbxstatus, rc = 0;
22517ad20aa9SJames Smart 
22527ad20aa9SJames Smart 	shost = job->shost;
22537ad20aa9SJames Smart 	if (!shost) {
22547ad20aa9SJames Smart 		rc = -ENODEV;
22557ad20aa9SJames Smart 		goto job_error;
22567ad20aa9SJames Smart 	}
22577ad20aa9SJames Smart 	vport = (struct lpfc_vport *)job->shost->hostdata;
22587ad20aa9SJames Smart 	if (!vport) {
22597ad20aa9SJames Smart 		rc = -ENODEV;
22607ad20aa9SJames Smart 		goto job_error;
22617ad20aa9SJames Smart 	}
22627ad20aa9SJames Smart 	phba = vport->phba;
22637ad20aa9SJames Smart 	if (!phba) {
22647ad20aa9SJames Smart 		rc = -ENODEV;
22657ad20aa9SJames Smart 		goto job_error;
22667ad20aa9SJames Smart 	}
22677ad20aa9SJames Smart 
22687ad20aa9SJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
22697ad20aa9SJames Smart 		rc = -ENODEV;
22707ad20aa9SJames Smart 		goto job_error;
22717ad20aa9SJames Smart 	}
22727ad20aa9SJames Smart 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
22737ad20aa9SJames Smart 	    LPFC_SLI_INTF_IF_TYPE_2) {
22747ad20aa9SJames Smart 		rc = -ENODEV;
22757ad20aa9SJames Smart 		goto job_error;
22767ad20aa9SJames Smart 	}
22777ad20aa9SJames Smart 
22787ad20aa9SJames Smart 	if (job->request_len < sizeof(struct fc_bsg_request) +
22797ad20aa9SJames Smart 	    sizeof(struct sli4_link_diag)) {
22807ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
22817ad20aa9SJames Smart 				"3013 Received LINK DIAG TEST request "
22827ad20aa9SJames Smart 				" size:%d below the minimum size:%d\n",
22837ad20aa9SJames Smart 				job->request_len,
22847ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
22857ad20aa9SJames Smart 				sizeof(struct sli4_link_diag)));
22867ad20aa9SJames Smart 		rc = -EINVAL;
22877ad20aa9SJames Smart 		goto job_error;
22887ad20aa9SJames Smart 	}
22897ad20aa9SJames Smart 
229088a2cfbbSJames Smart 	rc = lpfc_bsg_diag_mode_enter(phba);
22917ad20aa9SJames Smart 	if (rc)
22927ad20aa9SJames Smart 		goto job_error;
22937ad20aa9SJames Smart 
22947ad20aa9SJames Smart 	link_diag_test_cmd = (struct sli4_link_diag *)
22957ad20aa9SJames Smart 			 job->request->rqst_data.h_vendor.vendor_cmd;
22967ad20aa9SJames Smart 	timeout = link_diag_test_cmd->timeout * 100;
22977ad20aa9SJames Smart 
22987ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
22997ad20aa9SJames Smart 
23007ad20aa9SJames Smart 	if (rc)
23017ad20aa9SJames Smart 		goto job_error;
23027ad20aa9SJames Smart 
23037ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
23047ad20aa9SJames Smart 	if (!pmboxq) {
23057ad20aa9SJames Smart 		rc = -ENOMEM;
23067ad20aa9SJames Smart 		goto link_diag_test_exit;
23077ad20aa9SJames Smart 	}
23087ad20aa9SJames Smart 
23097ad20aa9SJames Smart 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
23107ad20aa9SJames Smart 		   sizeof(struct lpfc_sli4_cfg_mhdr));
23117ad20aa9SJames Smart 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
23127ad20aa9SJames Smart 				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
23137ad20aa9SJames Smart 				     req_len, LPFC_SLI4_MBX_EMBED);
23147ad20aa9SJames Smart 	if (alloc_len != req_len) {
23157ad20aa9SJames Smart 		rc = -ENOMEM;
23167ad20aa9SJames Smart 		goto link_diag_test_exit;
23177ad20aa9SJames Smart 	}
23187ad20aa9SJames Smart 	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
23197ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
23201b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_no);
23217ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
23221b51197dSJames Smart 	       phba->sli4_hba.lnk_info.lnk_tp);
23237ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
23247ad20aa9SJames Smart 	       link_diag_test_cmd->test_id);
23257ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
23267ad20aa9SJames Smart 	       link_diag_test_cmd->loops);
23277ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
23287ad20aa9SJames Smart 	       link_diag_test_cmd->test_version);
23297ad20aa9SJames Smart 	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
23307ad20aa9SJames Smart 	       link_diag_test_cmd->error_action);
23317ad20aa9SJames Smart 
23327ad20aa9SJames Smart 	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
23337ad20aa9SJames Smart 
23347ad20aa9SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)
23357ad20aa9SJames Smart 		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
23367ad20aa9SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
23377ad20aa9SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
23387ad20aa9SJames Smart 	if (shdr_status || shdr_add_status || mbxstatus) {
23397ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
23407ad20aa9SJames Smart 				"3010 Run link diag test mailbox failed with "
23417ad20aa9SJames Smart 				"mbx_status x%x status x%x, add_status x%x\n",
23427ad20aa9SJames Smart 				mbxstatus, shdr_status, shdr_add_status);
23437ad20aa9SJames Smart 	}
23447ad20aa9SJames Smart 
23457ad20aa9SJames Smart 	diag_status_reply = (struct diag_status *)
23467ad20aa9SJames Smart 			    job->reply->reply_data.vendor_reply.vendor_rsp;
23477ad20aa9SJames Smart 
23487ad20aa9SJames Smart 	if (job->reply_len <
23497ad20aa9SJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
23507ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
23517ad20aa9SJames Smart 				"3012 Received Run link diag test reply "
23527ad20aa9SJames Smart 				"below minimum size (%d): reply_len:%d\n",
23537ad20aa9SJames Smart 				(int)(sizeof(struct fc_bsg_request) +
23547ad20aa9SJames Smart 				sizeof(struct diag_status)),
23557ad20aa9SJames Smart 				job->reply_len);
23567ad20aa9SJames Smart 		rc = -EINVAL;
23577ad20aa9SJames Smart 		goto job_error;
23587ad20aa9SJames Smart 	}
23597ad20aa9SJames Smart 
23607ad20aa9SJames Smart 	diag_status_reply->mbox_status = mbxstatus;
23617ad20aa9SJames Smart 	diag_status_reply->shdr_status = shdr_status;
23627ad20aa9SJames Smart 	diag_status_reply->shdr_add_status = shdr_add_status;
23637ad20aa9SJames Smart 
23647ad20aa9SJames Smart link_diag_test_exit:
23657ad20aa9SJames Smart 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
23667ad20aa9SJames Smart 
23677ad20aa9SJames Smart 	if (pmboxq)
23687ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
23697ad20aa9SJames Smart 
23707ad20aa9SJames Smart 	lpfc_bsg_diag_mode_exit(phba);
23717ad20aa9SJames Smart 
23727ad20aa9SJames Smart job_error:
23737ad20aa9SJames Smart 	/* make error code available to userspace */
23747ad20aa9SJames Smart 	job->reply->result = rc;
23757ad20aa9SJames Smart 	/* complete the job back to userspace if no error */
23767ad20aa9SJames Smart 	if (rc == 0)
23777ad20aa9SJames Smart 		job->job_done(job);
23787ad20aa9SJames Smart 	return rc;
23797ad20aa9SJames Smart }
23807ad20aa9SJames Smart 
23817ad20aa9SJames Smart /**
23823b5dd52aSJames Smart  * lpfcdiag_loop_self_reg - obtains a remote port login id
23833b5dd52aSJames Smart  * @phba: Pointer to HBA context object
23843b5dd52aSJames Smart  * @rpi: Pointer to a remote port login id
23853b5dd52aSJames Smart  *
23863b5dd52aSJames Smart  * This function obtains a remote port login id so the diag loopback test
23873b5dd52aSJames Smart  * can send and receive its own unsolicited CT command.
23883b5dd52aSJames Smart  **/
23893b5dd52aSJames Smart static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
23903b5dd52aSJames Smart {
23913b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
23923b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuff;
23933b5dd52aSJames Smart 	int status;
23943b5dd52aSJames Smart 
23953b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
23963b5dd52aSJames Smart 	if (!mbox)
2397d439d286SJames Smart 		return -ENOMEM;
23983b5dd52aSJames Smart 
23991b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
24003b5dd52aSJames Smart 		status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
24011b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
24021b51197dSJames Smart 				mbox, *rpi);
24031b51197dSJames Smart 	else {
24041b51197dSJames Smart 		*rpi = lpfc_sli4_alloc_rpi(phba);
24051b51197dSJames Smart 		status = lpfc_reg_rpi(phba, phba->pport->vpi,
24061b51197dSJames Smart 				phba->pport->fc_myDID,
24071b51197dSJames Smart 				(uint8_t *)&phba->pport->fc_sparam,
24081b51197dSJames Smart 				mbox, *rpi);
24091b51197dSJames Smart 	}
24101b51197dSJames Smart 
24113b5dd52aSJames Smart 	if (status) {
24123b5dd52aSJames Smart 		mempool_free(mbox, phba->mbox_mem_pool);
24134042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
24144042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2415d439d286SJames Smart 		return -ENOMEM;
24163b5dd52aSJames Smart 	}
24173b5dd52aSJames Smart 
24183b5dd52aSJames Smart 	dmabuff = (struct lpfc_dmabuf *) mbox->context1;
24193b5dd52aSJames Smart 	mbox->context1 = NULL;
2420d439d286SJames Smart 	mbox->context2 = NULL;
24213b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
24223b5dd52aSJames Smart 
24233b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
24243b5dd52aSJames Smart 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
24253b5dd52aSJames Smart 		kfree(dmabuff);
24263b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
24273b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
24284042629eSJames Smart 		if (phba->sli_rev == LPFC_SLI_REV4)
24294042629eSJames Smart 			lpfc_sli4_free_rpi(phba, *rpi);
2430d439d286SJames Smart 		return -ENODEV;
24313b5dd52aSJames Smart 	}
24323b5dd52aSJames Smart 
24331b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
24343b5dd52aSJames Smart 		*rpi = mbox->u.mb.un.varWords[0];
24353b5dd52aSJames Smart 
24363b5dd52aSJames Smart 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
24373b5dd52aSJames Smart 	kfree(dmabuff);
24383b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
24393b5dd52aSJames Smart 	return 0;
24403b5dd52aSJames Smart }
24413b5dd52aSJames Smart 
24423b5dd52aSJames Smart /**
24433b5dd52aSJames Smart  * lpfcdiag_loop_self_unreg - unregs from the rpi
24443b5dd52aSJames Smart  * @phba: Pointer to HBA context object
24453b5dd52aSJames Smart  * @rpi: Remote port login id
24463b5dd52aSJames Smart  *
24473b5dd52aSJames Smart  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
24483b5dd52aSJames Smart  **/
24493b5dd52aSJames Smart static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
24503b5dd52aSJames Smart {
24513b5dd52aSJames Smart 	LPFC_MBOXQ_t *mbox;
24523b5dd52aSJames Smart 	int status;
24533b5dd52aSJames Smart 
24543b5dd52aSJames Smart 	/* Allocate mboxq structure */
24553b5dd52aSJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
24563b5dd52aSJames Smart 	if (mbox == NULL)
2457d439d286SJames Smart 		return -ENOMEM;
24583b5dd52aSJames Smart 
24591b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
24603b5dd52aSJames Smart 		lpfc_unreg_login(phba, 0, rpi, mbox);
24611b51197dSJames Smart 	else
24621b51197dSJames Smart 		lpfc_unreg_login(phba, phba->pport->vpi,
24631b51197dSJames Smart 				 phba->sli4_hba.rpi_ids[rpi], mbox);
24641b51197dSJames Smart 
24653b5dd52aSJames Smart 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
24663b5dd52aSJames Smart 
24673b5dd52aSJames Smart 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
24683b5dd52aSJames Smart 		if (status != MBX_TIMEOUT)
24693b5dd52aSJames Smart 			mempool_free(mbox, phba->mbox_mem_pool);
2470d439d286SJames Smart 		return -EIO;
24713b5dd52aSJames Smart 	}
24723b5dd52aSJames Smart 	mempool_free(mbox, phba->mbox_mem_pool);
24734042629eSJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4)
24744042629eSJames Smart 		lpfc_sli4_free_rpi(phba, rpi);
24753b5dd52aSJames Smart 	return 0;
24763b5dd52aSJames Smart }
24773b5dd52aSJames Smart 
24783b5dd52aSJames Smart /**
24793b5dd52aSJames Smart  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
24803b5dd52aSJames Smart  * @phba: Pointer to HBA context object
24813b5dd52aSJames Smart  * @rpi: Remote port login id
24823b5dd52aSJames Smart  * @txxri: Pointer to transmit exchange id
24833b5dd52aSJames Smart  * @rxxri: Pointer to response exchabge id
24843b5dd52aSJames Smart  *
24853b5dd52aSJames Smart  * This function obtains the transmit and receive ids required to send
24863b5dd52aSJames Smart  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
24873b5dd52aSJames Smart  * flags are used to the unsolicted response handler is able to process
24883b5dd52aSJames Smart  * the ct command sent on the same port.
24893b5dd52aSJames Smart  **/
24903b5dd52aSJames Smart static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
24913b5dd52aSJames Smart 			 uint16_t *txxri, uint16_t * rxxri)
24923b5dd52aSJames Smart {
24933b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
24943b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
24953b5dd52aSJames Smart 	IOCB_t *cmd, *rsp;
24963b5dd52aSJames Smart 	struct lpfc_dmabuf *dmabuf;
24973b5dd52aSJames Smart 	struct ulp_bde64 *bpl = NULL;
24983b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq = NULL;
24993b5dd52aSJames Smart 	int ret_val = 0;
2500d439d286SJames Smart 	int time_left;
2501515e0aa2SJames Smart 	int iocb_stat = 0;
25023b5dd52aSJames Smart 	unsigned long flags;
25033b5dd52aSJames Smart 
25043b5dd52aSJames Smart 	*txxri = 0;
25053b5dd52aSJames Smart 	*rxxri = 0;
25063b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
25073b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
25083b5dd52aSJames Smart 	if (!evt)
2509d439d286SJames Smart 		return -ENOMEM;
25103b5dd52aSJames Smart 
25113b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
25123b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
25133b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
25143b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
25153b5dd52aSJames Smart 
25163b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
25173b5dd52aSJames Smart 	rspiocbq = lpfc_sli_get_iocbq(phba);
25183b5dd52aSJames Smart 
25193b5dd52aSJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
25203b5dd52aSJames Smart 	if (dmabuf) {
25213b5dd52aSJames Smart 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2522c7495937SJames Smart 		if (dmabuf->virt) {
25233b5dd52aSJames Smart 			INIT_LIST_HEAD(&dmabuf->list);
25243b5dd52aSJames Smart 			bpl = (struct ulp_bde64 *) dmabuf->virt;
25253b5dd52aSJames Smart 			memset(bpl, 0, sizeof(*bpl));
25263b5dd52aSJames Smart 			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
25273b5dd52aSJames Smart 			bpl->addrHigh =
2528c7495937SJames Smart 				le32_to_cpu(putPaddrHigh(dmabuf->phys +
2529c7495937SJames Smart 					sizeof(*bpl)));
25303b5dd52aSJames Smart 			bpl->addrLow =
2531c7495937SJames Smart 				le32_to_cpu(putPaddrLow(dmabuf->phys +
2532c7495937SJames Smart 					sizeof(*bpl)));
25333b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
25343b5dd52aSJames Smart 			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
25353b5dd52aSJames Smart 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
25363b5dd52aSJames Smart 		}
2537c7495937SJames Smart 	}
25383b5dd52aSJames Smart 
25393b5dd52aSJames Smart 	if (cmdiocbq == NULL || rspiocbq == NULL ||
2540c7495937SJames Smart 	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2541c7495937SJames Smart 		dmabuf->virt == NULL) {
2542d439d286SJames Smart 		ret_val = -ENOMEM;
25433b5dd52aSJames Smart 		goto err_get_xri_exit;
25443b5dd52aSJames Smart 	}
25453b5dd52aSJames Smart 
25463b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
25473b5dd52aSJames Smart 	rsp = &rspiocbq->iocb;
25483b5dd52aSJames Smart 
25493b5dd52aSJames Smart 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
25503b5dd52aSJames Smart 
25513b5dd52aSJames Smart 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
25523b5dd52aSJames Smart 	ctreq->RevisionId.bits.InId = 0;
25533b5dd52aSJames Smart 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
25543b5dd52aSJames Smart 	ctreq->FsSubType = 0;
25553b5dd52aSJames Smart 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
25563b5dd52aSJames Smart 	ctreq->CommandResponse.bits.Size = 0;
25573b5dd52aSJames Smart 
25583b5dd52aSJames Smart 
25593b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
25603b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
25613b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
25623b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
25633b5dd52aSJames Smart 
25643b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = LA;
25653b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
25663b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
25673b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
25683b5dd52aSJames Smart 
25693b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
25703b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
25713b5dd52aSJames Smart 	cmd->ulpLe = 1;
25723b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
25733b5dd52aSJames Smart 	cmd->ulpContext = rpi;
25743b5dd52aSJames Smart 
25753b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
25763b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
25773b5dd52aSJames Smart 
2578d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
25793b5dd52aSJames Smart 				rspiocbq,
25803b5dd52aSJames Smart 				(phba->fc_ratov * 2)
25813b5dd52aSJames Smart 				+ LPFC_DRVR_TIMEOUT);
2582d439d286SJames Smart 	if (iocb_stat) {
2583d439d286SJames Smart 		ret_val = -EIO;
25843b5dd52aSJames Smart 		goto err_get_xri_exit;
2585d439d286SJames Smart 	}
25863b5dd52aSJames Smart 	*txxri =  rsp->ulpContext;
25873b5dd52aSJames Smart 
25883b5dd52aSJames Smart 	evt->waiting = 1;
25893b5dd52aSJames Smart 	evt->wait_time_stamp = jiffies;
2590d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
25913b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
2592256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
2593256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
25943b5dd52aSJames Smart 	if (list_empty(&evt->events_to_see))
2595d439d286SJames Smart 		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
25963b5dd52aSJames Smart 	else {
25973b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
25983b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
25993b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26003b5dd52aSJames Smart 		*rxxri = (list_entry(evt->events_to_get.prev,
26013b5dd52aSJames Smart 				     typeof(struct event_data),
26023b5dd52aSJames Smart 				     node))->immed_dat;
26033b5dd52aSJames Smart 	}
26043b5dd52aSJames Smart 	evt->waiting = 0;
26053b5dd52aSJames Smart 
26063b5dd52aSJames Smart err_get_xri_exit:
26073b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
26083b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
26093b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
26103b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
26113b5dd52aSJames Smart 
26123b5dd52aSJames Smart 	if (dmabuf) {
26133b5dd52aSJames Smart 		if (dmabuf->virt)
26143b5dd52aSJames Smart 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
26153b5dd52aSJames Smart 		kfree(dmabuf);
26163b5dd52aSJames Smart 	}
26173b5dd52aSJames Smart 
2618d439d286SJames Smart 	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
26193b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
26203b5dd52aSJames Smart 	if (rspiocbq)
26213b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
26223b5dd52aSJames Smart 	return ret_val;
26233b5dd52aSJames Smart }
26243b5dd52aSJames Smart 
26253b5dd52aSJames Smart /**
26267ad20aa9SJames Smart  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
26277ad20aa9SJames Smart  * @phba: Pointer to HBA context object
26287ad20aa9SJames Smart  *
26297ad20aa9SJames Smart  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
26307ad20aa9SJames Smart  * retruns the pointer to the buffer.
26317ad20aa9SJames Smart  **/
26327ad20aa9SJames Smart static struct lpfc_dmabuf *
26337ad20aa9SJames Smart lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
26347ad20aa9SJames Smart {
26357ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
26367ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
26377ad20aa9SJames Smart 
26387ad20aa9SJames Smart 	/* allocate dma buffer struct */
26397ad20aa9SJames Smart 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
26407ad20aa9SJames Smart 	if (!dmabuf)
26417ad20aa9SJames Smart 		return NULL;
26427ad20aa9SJames Smart 
26437ad20aa9SJames Smart 	INIT_LIST_HEAD(&dmabuf->list);
26447ad20aa9SJames Smart 
26457ad20aa9SJames Smart 	/* now, allocate dma buffer */
26467ad20aa9SJames Smart 	dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
26477ad20aa9SJames Smart 					  &(dmabuf->phys), GFP_KERNEL);
26487ad20aa9SJames Smart 
26497ad20aa9SJames Smart 	if (!dmabuf->virt) {
26507ad20aa9SJames Smart 		kfree(dmabuf);
26517ad20aa9SJames Smart 		return NULL;
26527ad20aa9SJames Smart 	}
26537ad20aa9SJames Smart 	memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
26547ad20aa9SJames Smart 
26557ad20aa9SJames Smart 	return dmabuf;
26567ad20aa9SJames Smart }
26577ad20aa9SJames Smart 
26587ad20aa9SJames Smart /**
26597ad20aa9SJames Smart  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
26607ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
26617ad20aa9SJames Smart  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
26627ad20aa9SJames Smart  *
26637ad20aa9SJames Smart  * This routine just simply frees a dma buffer and its associated buffer
26647ad20aa9SJames Smart  * descriptor referred by @dmabuf.
26657ad20aa9SJames Smart  **/
26667ad20aa9SJames Smart static void
26677ad20aa9SJames Smart lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
26687ad20aa9SJames Smart {
26697ad20aa9SJames Smart 	struct pci_dev *pcidev = phba->pcidev;
26707ad20aa9SJames Smart 
26717ad20aa9SJames Smart 	if (!dmabuf)
26727ad20aa9SJames Smart 		return;
26737ad20aa9SJames Smart 
26747ad20aa9SJames Smart 	if (dmabuf->virt)
26757ad20aa9SJames Smart 		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
26767ad20aa9SJames Smart 				  dmabuf->virt, dmabuf->phys);
26777ad20aa9SJames Smart 	kfree(dmabuf);
26787ad20aa9SJames Smart 	return;
26797ad20aa9SJames Smart }
26807ad20aa9SJames Smart 
26817ad20aa9SJames Smart /**
26827ad20aa9SJames Smart  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
26837ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
26847ad20aa9SJames Smart  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
26857ad20aa9SJames Smart  *
26867ad20aa9SJames Smart  * This routine just simply frees all dma buffers and their associated buffer
26877ad20aa9SJames Smart  * descriptors referred by @dmabuf_list.
26887ad20aa9SJames Smart  **/
26897ad20aa9SJames Smart static void
26907ad20aa9SJames Smart lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
26917ad20aa9SJames Smart 			    struct list_head *dmabuf_list)
26927ad20aa9SJames Smart {
26937ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
26947ad20aa9SJames Smart 
26957ad20aa9SJames Smart 	if (list_empty(dmabuf_list))
26967ad20aa9SJames Smart 		return;
26977ad20aa9SJames Smart 
26987ad20aa9SJames Smart 	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
26997ad20aa9SJames Smart 		list_del_init(&dmabuf->list);
27007ad20aa9SJames Smart 		lpfc_bsg_dma_page_free(phba, dmabuf);
27017ad20aa9SJames Smart 	}
27027ad20aa9SJames Smart 	return;
27037ad20aa9SJames Smart }
27047ad20aa9SJames Smart 
27057ad20aa9SJames Smart /**
27063b5dd52aSJames Smart  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
27073b5dd52aSJames Smart  * @phba: Pointer to HBA context object
27083b5dd52aSJames Smart  * @bpl: Pointer to 64 bit bde structure
27093b5dd52aSJames Smart  * @size: Number of bytes to process
27103b5dd52aSJames Smart  * @nocopydata: Flag to copy user data into the allocated buffer
27113b5dd52aSJames Smart  *
27123b5dd52aSJames Smart  * This function allocates page size buffers and populates an lpfc_dmabufext.
27133b5dd52aSJames Smart  * If allowed the user data pointed to with indataptr is copied into the kernel
27143b5dd52aSJames Smart  * memory. The chained list of page size buffers is returned.
27153b5dd52aSJames Smart  **/
27163b5dd52aSJames Smart static struct lpfc_dmabufext *
27173b5dd52aSJames Smart diag_cmd_data_alloc(struct lpfc_hba *phba,
27183b5dd52aSJames Smart 		   struct ulp_bde64 *bpl, uint32_t size,
27193b5dd52aSJames Smart 		   int nocopydata)
27203b5dd52aSJames Smart {
27213b5dd52aSJames Smart 	struct lpfc_dmabufext *mlist = NULL;
27223b5dd52aSJames Smart 	struct lpfc_dmabufext *dmp;
27233b5dd52aSJames Smart 	int cnt, offset = 0, i = 0;
27243b5dd52aSJames Smart 	struct pci_dev *pcidev;
27253b5dd52aSJames Smart 
27263b5dd52aSJames Smart 	pcidev = phba->pcidev;
27273b5dd52aSJames Smart 
27283b5dd52aSJames Smart 	while (size) {
27293b5dd52aSJames Smart 		/* We get chunks of 4K */
27303b5dd52aSJames Smart 		if (size > BUF_SZ_4K)
27313b5dd52aSJames Smart 			cnt = BUF_SZ_4K;
27323b5dd52aSJames Smart 		else
27333b5dd52aSJames Smart 			cnt = size;
27343b5dd52aSJames Smart 
27353b5dd52aSJames Smart 		/* allocate struct lpfc_dmabufext buffer header */
27363b5dd52aSJames Smart 		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
27373b5dd52aSJames Smart 		if (!dmp)
27383b5dd52aSJames Smart 			goto out;
27393b5dd52aSJames Smart 
27403b5dd52aSJames Smart 		INIT_LIST_HEAD(&dmp->dma.list);
27413b5dd52aSJames Smart 
27423b5dd52aSJames Smart 		/* Queue it to a linked list */
27433b5dd52aSJames Smart 		if (mlist)
27443b5dd52aSJames Smart 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
27453b5dd52aSJames Smart 		else
27463b5dd52aSJames Smart 			mlist = dmp;
27473b5dd52aSJames Smart 
27483b5dd52aSJames Smart 		/* allocate buffer */
27493b5dd52aSJames Smart 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
27503b5dd52aSJames Smart 						   cnt,
27513b5dd52aSJames Smart 						   &(dmp->dma.phys),
27523b5dd52aSJames Smart 						   GFP_KERNEL);
27533b5dd52aSJames Smart 
27543b5dd52aSJames Smart 		if (!dmp->dma.virt)
27553b5dd52aSJames Smart 			goto out;
27563b5dd52aSJames Smart 
27573b5dd52aSJames Smart 		dmp->size = cnt;
27583b5dd52aSJames Smart 
27593b5dd52aSJames Smart 		if (nocopydata) {
27603b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = 0;
27613b5dd52aSJames Smart 			pci_dma_sync_single_for_device(phba->pcidev,
27623b5dd52aSJames Smart 				dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
27633b5dd52aSJames Smart 
27643b5dd52aSJames Smart 		} else {
27653b5dd52aSJames Smart 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
27663b5dd52aSJames Smart 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
27673b5dd52aSJames Smart 		}
27683b5dd52aSJames Smart 
27693b5dd52aSJames Smart 		/* build buffer ptr list for IOCB */
27703b5dd52aSJames Smart 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
27713b5dd52aSJames Smart 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
27723b5dd52aSJames Smart 		bpl->tus.f.bdeSize = (ushort) cnt;
27733b5dd52aSJames Smart 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
27743b5dd52aSJames Smart 		bpl++;
27753b5dd52aSJames Smart 
27763b5dd52aSJames Smart 		i++;
27773b5dd52aSJames Smart 		offset += cnt;
27783b5dd52aSJames Smart 		size -= cnt;
27793b5dd52aSJames Smart 	}
27803b5dd52aSJames Smart 
27813b5dd52aSJames Smart 	mlist->flag = i;
27823b5dd52aSJames Smart 	return mlist;
27833b5dd52aSJames Smart out:
27843b5dd52aSJames Smart 	diag_cmd_data_free(phba, mlist);
27853b5dd52aSJames Smart 	return NULL;
27863b5dd52aSJames Smart }
27873b5dd52aSJames Smart 
27883b5dd52aSJames Smart /**
27893b5dd52aSJames Smart  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
27903b5dd52aSJames Smart  * @phba: Pointer to HBA context object
27913b5dd52aSJames Smart  * @rxxri: Receive exchange id
27923b5dd52aSJames Smart  * @len: Number of data bytes
27933b5dd52aSJames Smart  *
279425985edcSLucas De Marchi  * This function allocates and posts a data buffer of sufficient size to receive
27953b5dd52aSJames Smart  * an unsolicted CT command.
27963b5dd52aSJames Smart  **/
27973b5dd52aSJames Smart static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
27983b5dd52aSJames Smart 			     size_t len)
27993b5dd52aSJames Smart {
28003b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
28013b5dd52aSJames Smart 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
28023b5dd52aSJames Smart 	struct lpfc_iocbq *cmdiocbq;
28033b5dd52aSJames Smart 	IOCB_t *cmd = NULL;
28043b5dd52aSJames Smart 	struct list_head head, *curr, *next;
28053b5dd52aSJames Smart 	struct lpfc_dmabuf *rxbmp;
28063b5dd52aSJames Smart 	struct lpfc_dmabuf *dmp;
28073b5dd52aSJames Smart 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
28083b5dd52aSJames Smart 	struct ulp_bde64 *rxbpl = NULL;
28093b5dd52aSJames Smart 	uint32_t num_bde;
28103b5dd52aSJames Smart 	struct lpfc_dmabufext *rxbuffer = NULL;
28113b5dd52aSJames Smart 	int ret_val = 0;
2812d439d286SJames Smart 	int iocb_stat;
28133b5dd52aSJames Smart 	int i = 0;
28143b5dd52aSJames Smart 
28153b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
28163b5dd52aSJames Smart 	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
28173b5dd52aSJames Smart 	if (rxbmp != NULL) {
28183b5dd52aSJames Smart 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2819c7495937SJames Smart 		if (rxbmp->virt) {
28203b5dd52aSJames Smart 			INIT_LIST_HEAD(&rxbmp->list);
28213b5dd52aSJames Smart 			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
28223b5dd52aSJames Smart 			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
28233b5dd52aSJames Smart 		}
2824c7495937SJames Smart 	}
28253b5dd52aSJames Smart 
28263b5dd52aSJames Smart 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
2827d439d286SJames Smart 		ret_val = -ENOMEM;
28283b5dd52aSJames Smart 		goto err_post_rxbufs_exit;
28293b5dd52aSJames Smart 	}
28303b5dd52aSJames Smart 
28313b5dd52aSJames Smart 	/* Queue buffers for the receive exchange */
28323b5dd52aSJames Smart 	num_bde = (uint32_t)rxbuffer->flag;
28333b5dd52aSJames Smart 	dmp = &rxbuffer->dma;
28343b5dd52aSJames Smart 
28353b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
28363b5dd52aSJames Smart 	i = 0;
28373b5dd52aSJames Smart 
28383b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
28393b5dd52aSJames Smart 	list_add_tail(&head, &dmp->list);
28403b5dd52aSJames Smart 	list_for_each_safe(curr, next, &head) {
28413b5dd52aSJames Smart 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
28423b5dd52aSJames Smart 		list_del(curr);
28433b5dd52aSJames Smart 
28443b5dd52aSJames Smart 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
28453b5dd52aSJames Smart 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
28463b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrHigh =
28473b5dd52aSJames Smart 				putPaddrHigh(mp[i]->phys);
28483b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.addrLow =
28493b5dd52aSJames Smart 				putPaddrLow(mp[i]->phys);
28503b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
28513b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
28523b5dd52aSJames Smart 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
28533b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
28543b5dd52aSJames Smart 			cmd->ulpPU = 0;
28553b5dd52aSJames Smart 			cmd->ulpLe = 1;
28563b5dd52aSJames Smart 			cmd->ulpBdeCount = 1;
28573b5dd52aSJames Smart 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
28583b5dd52aSJames Smart 
28593b5dd52aSJames Smart 		} else {
28603b5dd52aSJames Smart 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
28613b5dd52aSJames Smart 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
28623b5dd52aSJames Smart 			cmd->un.cont64[i].tus.f.bdeSize =
28633b5dd52aSJames Smart 				((struct lpfc_dmabufext *)mp[i])->size;
28643b5dd52aSJames Smart 					cmd->ulpBdeCount = ++i;
28653b5dd52aSJames Smart 
28663b5dd52aSJames Smart 			if ((--num_bde > 0) && (i < 2))
28673b5dd52aSJames Smart 				continue;
28683b5dd52aSJames Smart 
28693b5dd52aSJames Smart 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
28703b5dd52aSJames Smart 			cmd->ulpLe = 1;
28713b5dd52aSJames Smart 		}
28723b5dd52aSJames Smart 
28733b5dd52aSJames Smart 		cmd->ulpClass = CLASS3;
28743b5dd52aSJames Smart 		cmd->ulpContext = rxxri;
28753b5dd52aSJames Smart 
2876d439d286SJames Smart 		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2877d439d286SJames Smart 						0);
2878d439d286SJames Smart 		if (iocb_stat == IOCB_ERROR) {
28793b5dd52aSJames Smart 			diag_cmd_data_free(phba,
28803b5dd52aSJames Smart 				(struct lpfc_dmabufext *)mp[0]);
28813b5dd52aSJames Smart 			if (mp[1])
28823b5dd52aSJames Smart 				diag_cmd_data_free(phba,
28833b5dd52aSJames Smart 					  (struct lpfc_dmabufext *)mp[1]);
28843b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2885d439d286SJames Smart 			ret_val = -EIO;
28863b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
28873b5dd52aSJames Smart 		}
28883b5dd52aSJames Smart 
28893b5dd52aSJames Smart 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
28903b5dd52aSJames Smart 		if (mp[1]) {
28913b5dd52aSJames Smart 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
28923b5dd52aSJames Smart 			mp[1] = NULL;
28933b5dd52aSJames Smart 		}
28943b5dd52aSJames Smart 
28953b5dd52aSJames Smart 		/* The iocb was freed by lpfc_sli_issue_iocb */
28963b5dd52aSJames Smart 		cmdiocbq = lpfc_sli_get_iocbq(phba);
28973b5dd52aSJames Smart 		if (!cmdiocbq) {
28983b5dd52aSJames Smart 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2899d439d286SJames Smart 			ret_val = -EIO;
29003b5dd52aSJames Smart 			goto err_post_rxbufs_exit;
29013b5dd52aSJames Smart 		}
29023b5dd52aSJames Smart 
29033b5dd52aSJames Smart 		cmd = &cmdiocbq->iocb;
29043b5dd52aSJames Smart 		i = 0;
29053b5dd52aSJames Smart 	}
29063b5dd52aSJames Smart 	list_del(&head);
29073b5dd52aSJames Smart 
29083b5dd52aSJames Smart err_post_rxbufs_exit:
29093b5dd52aSJames Smart 
29103b5dd52aSJames Smart 	if (rxbmp) {
29113b5dd52aSJames Smart 		if (rxbmp->virt)
29123b5dd52aSJames Smart 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
29133b5dd52aSJames Smart 		kfree(rxbmp);
29143b5dd52aSJames Smart 	}
29153b5dd52aSJames Smart 
29163b5dd52aSJames Smart 	if (cmdiocbq)
29173b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
29183b5dd52aSJames Smart 	return ret_val;
29193b5dd52aSJames Smart }
29203b5dd52aSJames Smart 
29213b5dd52aSJames Smart /**
29227ad20aa9SJames Smart  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
29233b5dd52aSJames Smart  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
29243b5dd52aSJames Smart  *
29253b5dd52aSJames Smart  * This function receives a user data buffer to be transmitted and received on
29263b5dd52aSJames Smart  * the same port, the link must be up and in loopback mode prior
29273b5dd52aSJames Smart  * to being called.
29283b5dd52aSJames Smart  * 1. A kernel buffer is allocated to copy the user data into.
29293b5dd52aSJames Smart  * 2. The port registers with "itself".
29303b5dd52aSJames Smart  * 3. The transmit and receive exchange ids are obtained.
29313b5dd52aSJames Smart  * 4. The receive exchange id is posted.
29323b5dd52aSJames Smart  * 5. A new els loopback event is created.
29333b5dd52aSJames Smart  * 6. The command and response iocbs are allocated.
29343b5dd52aSJames Smart  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
29353b5dd52aSJames Smart  *
29363b5dd52aSJames Smart  * This function is meant to be called n times while the port is in loopback
29373b5dd52aSJames Smart  * so it is the apps responsibility to issue a reset to take the port out
29383b5dd52aSJames Smart  * of loopback mode.
29393b5dd52aSJames Smart  **/
29403b5dd52aSJames Smart static int
29417ad20aa9SJames Smart lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
29423b5dd52aSJames Smart {
29433b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
29443b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
29453b5dd52aSJames Smart 	struct diag_mode_test *diag_mode;
29463b5dd52aSJames Smart 	struct lpfc_bsg_event *evt;
29473b5dd52aSJames Smart 	struct event_data *evdat;
29483b5dd52aSJames Smart 	struct lpfc_sli *psli = &phba->sli;
29493b5dd52aSJames Smart 	uint32_t size;
29503b5dd52aSJames Smart 	uint32_t full_size;
29513b5dd52aSJames Smart 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
29524042629eSJames Smart 	uint16_t rpi = 0;
29531b51197dSJames Smart 	struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
29541b51197dSJames Smart 	IOCB_t *cmd, *rsp = NULL;
29553b5dd52aSJames Smart 	struct lpfc_sli_ct_request *ctreq;
29563b5dd52aSJames Smart 	struct lpfc_dmabuf *txbmp;
29573b5dd52aSJames Smart 	struct ulp_bde64 *txbpl = NULL;
29583b5dd52aSJames Smart 	struct lpfc_dmabufext *txbuffer = NULL;
29593b5dd52aSJames Smart 	struct list_head head;
29603b5dd52aSJames Smart 	struct lpfc_dmabuf  *curr;
29611b51197dSJames Smart 	uint16_t txxri = 0, rxxri;
29623b5dd52aSJames Smart 	uint32_t num_bde;
29633b5dd52aSJames Smart 	uint8_t *ptr = NULL, *rx_databuf = NULL;
29643b5dd52aSJames Smart 	int rc = 0;
2965d439d286SJames Smart 	int time_left;
2966d439d286SJames Smart 	int iocb_stat;
29673b5dd52aSJames Smart 	unsigned long flags;
29683b5dd52aSJames Smart 	void *dataout = NULL;
29693b5dd52aSJames Smart 	uint32_t total_mem;
29703b5dd52aSJames Smart 
29713b5dd52aSJames Smart 	/* in case no data is returned return just the return code */
29723b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
29733b5dd52aSJames Smart 
29743b5dd52aSJames Smart 	if (job->request_len <
29753b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
29763b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
29773b5dd52aSJames Smart 				"2739 Received DIAG TEST request below minimum "
29783b5dd52aSJames Smart 				"size\n");
29793b5dd52aSJames Smart 		rc = -EINVAL;
29803b5dd52aSJames Smart 		goto loopback_test_exit;
29813b5dd52aSJames Smart 	}
29823b5dd52aSJames Smart 
29833b5dd52aSJames Smart 	if (job->request_payload.payload_len !=
29843b5dd52aSJames Smart 		job->reply_payload.payload_len) {
29853b5dd52aSJames Smart 		rc = -EINVAL;
29863b5dd52aSJames Smart 		goto loopback_test_exit;
29873b5dd52aSJames Smart 	}
29883b5dd52aSJames Smart 	diag_mode = (struct diag_mode_test *)
29893b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
29903b5dd52aSJames Smart 
29913b5dd52aSJames Smart 	if ((phba->link_state == LPFC_HBA_ERROR) ||
29923b5dd52aSJames Smart 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
29933b5dd52aSJames Smart 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
29943b5dd52aSJames Smart 		rc = -EACCES;
29953b5dd52aSJames Smart 		goto loopback_test_exit;
29963b5dd52aSJames Smart 	}
29973b5dd52aSJames Smart 
29983b5dd52aSJames Smart 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
29993b5dd52aSJames Smart 		rc = -EACCES;
30003b5dd52aSJames Smart 		goto loopback_test_exit;
30013b5dd52aSJames Smart 	}
30023b5dd52aSJames Smart 
30033b5dd52aSJames Smart 	size = job->request_payload.payload_len;
30043b5dd52aSJames Smart 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
30053b5dd52aSJames Smart 
30063b5dd52aSJames Smart 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
30073b5dd52aSJames Smart 		rc = -ERANGE;
30083b5dd52aSJames Smart 		goto loopback_test_exit;
30093b5dd52aSJames Smart 	}
30103b5dd52aSJames Smart 
301163e801ceSJames Smart 	if (full_size >= BUF_SZ_4K) {
30123b5dd52aSJames Smart 		/*
30133b5dd52aSJames Smart 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
30143b5dd52aSJames Smart 		 * then we allocate 64k and re-use that buffer over and over to
30153b5dd52aSJames Smart 		 * xfer the whole block. This is because Linux kernel has a
30163b5dd52aSJames Smart 		 * problem allocating more than 120k of kernel space memory. Saw
30173b5dd52aSJames Smart 		 * problem with GET_FCPTARGETMAPPING...
30183b5dd52aSJames Smart 		 */
30193b5dd52aSJames Smart 		if (size <= (64 * 1024))
302063e801ceSJames Smart 			total_mem = full_size;
30213b5dd52aSJames Smart 		else
30223b5dd52aSJames Smart 			total_mem = 64 * 1024;
30233b5dd52aSJames Smart 	} else
30243b5dd52aSJames Smart 		/* Allocate memory for ioctl data */
30253b5dd52aSJames Smart 		total_mem = BUF_SZ_4K;
30263b5dd52aSJames Smart 
30273b5dd52aSJames Smart 	dataout = kmalloc(total_mem, GFP_KERNEL);
30283b5dd52aSJames Smart 	if (dataout == NULL) {
30293b5dd52aSJames Smart 		rc = -ENOMEM;
30303b5dd52aSJames Smart 		goto loopback_test_exit;
30313b5dd52aSJames Smart 	}
30323b5dd52aSJames Smart 
30333b5dd52aSJames Smart 	ptr = dataout;
30343b5dd52aSJames Smart 	ptr += ELX_LOOPBACK_HEADER_SZ;
30353b5dd52aSJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
30363b5dd52aSJames Smart 				job->request_payload.sg_cnt,
30373b5dd52aSJames Smart 				ptr, size);
30383b5dd52aSJames Smart 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
3039d439d286SJames Smart 	if (rc)
30403b5dd52aSJames Smart 		goto loopback_test_exit;
30413b5dd52aSJames Smart 
30421b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
30433b5dd52aSJames Smart 		rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
30443b5dd52aSJames Smart 		if (rc) {
30453b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
30463b5dd52aSJames Smart 			goto loopback_test_exit;
30473b5dd52aSJames Smart 		}
30483b5dd52aSJames Smart 
30493b5dd52aSJames Smart 		rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
30503b5dd52aSJames Smart 		if (rc) {
30513b5dd52aSJames Smart 			lpfcdiag_loop_self_unreg(phba, rpi);
30523b5dd52aSJames Smart 			goto loopback_test_exit;
30533b5dd52aSJames Smart 		}
30541b51197dSJames Smart 	}
30553b5dd52aSJames Smart 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
30563b5dd52aSJames Smart 				SLI_CT_ELX_LOOPBACK);
30573b5dd52aSJames Smart 	if (!evt) {
30583b5dd52aSJames Smart 		lpfcdiag_loop_self_unreg(phba, rpi);
30593b5dd52aSJames Smart 		rc = -ENOMEM;
30603b5dd52aSJames Smart 		goto loopback_test_exit;
30613b5dd52aSJames Smart 	}
30623b5dd52aSJames Smart 
30633b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
30643b5dd52aSJames Smart 	list_add(&evt->node, &phba->ct_ev_waiters);
30653b5dd52aSJames Smart 	lpfc_bsg_event_ref(evt);
30663b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
30673b5dd52aSJames Smart 
30683b5dd52aSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
30691b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
30703b5dd52aSJames Smart 		rspiocbq = lpfc_sli_get_iocbq(phba);
30713b5dd52aSJames Smart 	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
30723b5dd52aSJames Smart 
30733b5dd52aSJames Smart 	if (txbmp) {
30743b5dd52aSJames Smart 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3075c7495937SJames Smart 		if (txbmp->virt) {
30763b5dd52aSJames Smart 			INIT_LIST_HEAD(&txbmp->list);
30773b5dd52aSJames Smart 			txbpl = (struct ulp_bde64 *) txbmp->virt;
30783b5dd52aSJames Smart 			txbuffer = diag_cmd_data_alloc(phba,
30793b5dd52aSJames Smart 							txbpl, full_size, 0);
30803b5dd52aSJames Smart 		}
3081c7495937SJames Smart 	}
30823b5dd52aSJames Smart 
30831b51197dSJames Smart 	if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
30841b51197dSJames Smart 		rc = -ENOMEM;
30851b51197dSJames Smart 		goto err_loopback_test_exit;
30861b51197dSJames Smart 	}
30871b51197dSJames Smart 	if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
30883b5dd52aSJames Smart 		rc = -ENOMEM;
30893b5dd52aSJames Smart 		goto err_loopback_test_exit;
30903b5dd52aSJames Smart 	}
30913b5dd52aSJames Smart 
30923b5dd52aSJames Smart 	cmd = &cmdiocbq->iocb;
30931b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
30943b5dd52aSJames Smart 		rsp = &rspiocbq->iocb;
30953b5dd52aSJames Smart 
30963b5dd52aSJames Smart 	INIT_LIST_HEAD(&head);
30973b5dd52aSJames Smart 	list_add_tail(&head, &txbuffer->dma.list);
30983b5dd52aSJames Smart 	list_for_each_entry(curr, &head, list) {
30993b5dd52aSJames Smart 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
31003b5dd52aSJames Smart 		if (current_offset == 0) {
31013b5dd52aSJames Smart 			ctreq = curr->virt;
31023b5dd52aSJames Smart 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
31033b5dd52aSJames Smart 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
31043b5dd52aSJames Smart 			ctreq->RevisionId.bits.InId = 0;
31053b5dd52aSJames Smart 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
31063b5dd52aSJames Smart 			ctreq->FsSubType = 0;
31073b5dd52aSJames Smart 			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
31083b5dd52aSJames Smart 			ctreq->CommandResponse.bits.Size   = size;
31093b5dd52aSJames Smart 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
31103b5dd52aSJames Smart 		} else
31113b5dd52aSJames Smart 			segment_offset = 0;
31123b5dd52aSJames Smart 
31133b5dd52aSJames Smart 		BUG_ON(segment_offset >= segment_len);
31143b5dd52aSJames Smart 		memcpy(curr->virt + segment_offset,
31153b5dd52aSJames Smart 			ptr + current_offset,
31163b5dd52aSJames Smart 			segment_len - segment_offset);
31173b5dd52aSJames Smart 
31183b5dd52aSJames Smart 		current_offset += segment_len - segment_offset;
31193b5dd52aSJames Smart 		BUG_ON(current_offset > size);
31203b5dd52aSJames Smart 	}
31213b5dd52aSJames Smart 	list_del(&head);
31223b5dd52aSJames Smart 
31233b5dd52aSJames Smart 	/* Build the XMIT_SEQUENCE iocb */
31243b5dd52aSJames Smart 	num_bde = (uint32_t)txbuffer->flag;
31253b5dd52aSJames Smart 
31263b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
31273b5dd52aSJames Smart 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
31283b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
31293b5dd52aSJames Smart 	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
31303b5dd52aSJames Smart 
31313b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
31323b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
31333b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
31343b5dd52aSJames Smart 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
31353b5dd52aSJames Smart 
31363b5dd52aSJames Smart 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
31373b5dd52aSJames Smart 	cmd->ulpBdeCount = 1;
31383b5dd52aSJames Smart 	cmd->ulpLe = 1;
31393b5dd52aSJames Smart 	cmd->ulpClass = CLASS3;
31403b5dd52aSJames Smart 
31411b51197dSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4) {
31421b51197dSJames Smart 		cmd->ulpContext = txxri;
31431b51197dSJames Smart 	} else {
31441b51197dSJames Smart 		cmd->un.xseq64.bdl.ulpIoTag32 = 0;
31451b51197dSJames Smart 		cmd->un.ulpWord[3] = phba->sli4_hba.rpi_ids[rpi];
31461b51197dSJames Smart 		cmdiocbq->context3 = txbmp;
31471b51197dSJames Smart 		cmdiocbq->sli4_xritag = NO_XRI;
31481b51197dSJames Smart 		cmd->unsli3.rcvsli3.ox_id = 0xffff;
31491b51197dSJames Smart 	}
31503b5dd52aSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
31513b5dd52aSJames Smart 	cmdiocbq->vport = phba->pport;
3152d439d286SJames Smart 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3153d439d286SJames Smart 					     rspiocbq, (phba->fc_ratov * 2) +
3154d439d286SJames Smart 					     LPFC_DRVR_TIMEOUT);
31553b5dd52aSJames Smart 
31561b51197dSJames Smart 	if ((iocb_stat != IOCB_SUCCESS) || ((phba->sli_rev < LPFC_SLI_REV4) &&
31571b51197dSJames Smart 					   (rsp->ulpStatus != IOCB_SUCCESS))) {
31581b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
31591b51197dSJames Smart 				"3126 Failed loopback test issue iocb: "
31601b51197dSJames Smart 				"iocb_stat:x%x\n", iocb_stat);
31613b5dd52aSJames Smart 		rc = -EIO;
31623b5dd52aSJames Smart 		goto err_loopback_test_exit;
31633b5dd52aSJames Smart 	}
31643b5dd52aSJames Smart 
31653b5dd52aSJames Smart 	evt->waiting = 1;
3166d439d286SJames Smart 	time_left = wait_event_interruptible_timeout(
31673b5dd52aSJames Smart 		evt->wq, !list_empty(&evt->events_to_see),
3168256ec0d0SJames Smart 		msecs_to_jiffies(1000 *
3169256ec0d0SJames Smart 			((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT)));
31703b5dd52aSJames Smart 	evt->waiting = 0;
31711b51197dSJames Smart 	if (list_empty(&evt->events_to_see)) {
3172d439d286SJames Smart 		rc = (time_left) ? -EINTR : -ETIMEDOUT;
31731b51197dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
31741b51197dSJames Smart 				"3125 Not receiving unsolicited event, "
31751b51197dSJames Smart 				"rc:x%x\n", rc);
31761b51197dSJames Smart 	} else {
31773b5dd52aSJames Smart 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
31783b5dd52aSJames Smart 		list_move(evt->events_to_see.prev, &evt->events_to_get);
31793b5dd52aSJames Smart 		evdat = list_entry(evt->events_to_get.prev,
31803b5dd52aSJames Smart 				   typeof(*evdat), node);
31813b5dd52aSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
31823b5dd52aSJames Smart 		rx_databuf = evdat->data;
31833b5dd52aSJames Smart 		if (evdat->len != full_size) {
31843b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
31853b5dd52aSJames Smart 				"1603 Loopback test did not receive expected "
31863b5dd52aSJames Smart 				"data length. actual length 0x%x expected "
31873b5dd52aSJames Smart 				"length 0x%x\n",
31883b5dd52aSJames Smart 				evdat->len, full_size);
31893b5dd52aSJames Smart 			rc = -EIO;
31903b5dd52aSJames Smart 		} else if (rx_databuf == NULL)
31913b5dd52aSJames Smart 			rc = -EIO;
31923b5dd52aSJames Smart 		else {
31933b5dd52aSJames Smart 			rc = IOCB_SUCCESS;
31943b5dd52aSJames Smart 			/* skip over elx loopback header */
31953b5dd52aSJames Smart 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
31963b5dd52aSJames Smart 			job->reply->reply_payload_rcv_len =
31973b5dd52aSJames Smart 				sg_copy_from_buffer(job->reply_payload.sg_list,
31983b5dd52aSJames Smart 						    job->reply_payload.sg_cnt,
31993b5dd52aSJames Smart 						    rx_databuf, size);
32003b5dd52aSJames Smart 			job->reply->reply_payload_rcv_len = size;
32013b5dd52aSJames Smart 		}
32023b5dd52aSJames Smart 	}
32033b5dd52aSJames Smart 
32043b5dd52aSJames Smart err_loopback_test_exit:
32053b5dd52aSJames Smart 	lpfcdiag_loop_self_unreg(phba, rpi);
32063b5dd52aSJames Smart 
32073b5dd52aSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
32083b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* release ref */
32093b5dd52aSJames Smart 	lpfc_bsg_event_unref(evt); /* delete */
32103b5dd52aSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
32113b5dd52aSJames Smart 
32123b5dd52aSJames Smart 	if (cmdiocbq != NULL)
32133b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, cmdiocbq);
32143b5dd52aSJames Smart 
32153b5dd52aSJames Smart 	if (rspiocbq != NULL)
32163b5dd52aSJames Smart 		lpfc_sli_release_iocbq(phba, rspiocbq);
32173b5dd52aSJames Smart 
32183b5dd52aSJames Smart 	if (txbmp != NULL) {
32193b5dd52aSJames Smart 		if (txbpl != NULL) {
32203b5dd52aSJames Smart 			if (txbuffer != NULL)
32213b5dd52aSJames Smart 				diag_cmd_data_free(phba, txbuffer);
32223b5dd52aSJames Smart 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
32233b5dd52aSJames Smart 		}
32243b5dd52aSJames Smart 		kfree(txbmp);
32253b5dd52aSJames Smart 	}
32263b5dd52aSJames Smart 
32273b5dd52aSJames Smart loopback_test_exit:
32283b5dd52aSJames Smart 	kfree(dataout);
32293b5dd52aSJames Smart 	/* make error code available to userspace */
32303b5dd52aSJames Smart 	job->reply->result = rc;
32313b5dd52aSJames Smart 	job->dd_data = NULL;
32323b5dd52aSJames Smart 	/* complete the job back to userspace if no error */
32331b51197dSJames Smart 	if (rc == IOCB_SUCCESS)
32343b5dd52aSJames Smart 		job->job_done(job);
32353b5dd52aSJames Smart 	return rc;
32363b5dd52aSJames Smart }
32373b5dd52aSJames Smart 
32383b5dd52aSJames Smart /**
32393b5dd52aSJames Smart  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
32403b5dd52aSJames Smart  * @job: GET_DFC_REV fc_bsg_job
32413b5dd52aSJames Smart  **/
32423b5dd52aSJames Smart static int
32433b5dd52aSJames Smart lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
32443b5dd52aSJames Smart {
32453b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
32463b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
32473b5dd52aSJames Smart 	struct get_mgmt_rev *event_req;
32483b5dd52aSJames Smart 	struct get_mgmt_rev_reply *event_reply;
32493b5dd52aSJames Smart 	int rc = 0;
32503b5dd52aSJames Smart 
32513b5dd52aSJames Smart 	if (job->request_len <
32523b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
32533b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
32543b5dd52aSJames Smart 				"2740 Received GET_DFC_REV request below "
32553b5dd52aSJames Smart 				"minimum size\n");
32563b5dd52aSJames Smart 		rc = -EINVAL;
32573b5dd52aSJames Smart 		goto job_error;
32583b5dd52aSJames Smart 	}
32593b5dd52aSJames Smart 
32603b5dd52aSJames Smart 	event_req = (struct get_mgmt_rev *)
32613b5dd52aSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
32623b5dd52aSJames Smart 
32633b5dd52aSJames Smart 	event_reply = (struct get_mgmt_rev_reply *)
32643b5dd52aSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
32653b5dd52aSJames Smart 
32663b5dd52aSJames Smart 	if (job->reply_len <
32673b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
32683b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
32693b5dd52aSJames Smart 				"2741 Received GET_DFC_REV reply below "
32703b5dd52aSJames Smart 				"minimum size\n");
32713b5dd52aSJames Smart 		rc = -EINVAL;
32723b5dd52aSJames Smart 		goto job_error;
32733b5dd52aSJames Smart 	}
32743b5dd52aSJames Smart 
32753b5dd52aSJames Smart 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
32763b5dd52aSJames Smart 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
32773b5dd52aSJames Smart job_error:
32783b5dd52aSJames Smart 	job->reply->result = rc;
32793b5dd52aSJames Smart 	if (rc == 0)
32803b5dd52aSJames Smart 		job->job_done(job);
32813b5dd52aSJames Smart 	return rc;
32823b5dd52aSJames Smart }
32833b5dd52aSJames Smart 
32843b5dd52aSJames Smart /**
32857ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
32863b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
32873b5dd52aSJames Smart  * @pmboxq: Pointer to mailbox command.
32883b5dd52aSJames Smart  *
32893b5dd52aSJames Smart  * This is completion handler function for mailbox commands issued from
32903b5dd52aSJames Smart  * lpfc_bsg_issue_mbox function. This function is called by the
32913b5dd52aSJames Smart  * mailbox event handler function with no lock held. This function
32923b5dd52aSJames Smart  * will wake up thread waiting on the wait queue pointed by context1
32933b5dd52aSJames Smart  * of the mailbox.
32943b5dd52aSJames Smart  **/
32953b5dd52aSJames Smart void
32967ad20aa9SJames Smart lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
32973b5dd52aSJames Smart {
32983b5dd52aSJames Smart 	struct bsg_job_data *dd_data;
32993b5dd52aSJames Smart 	struct fc_bsg_job *job;
33003b5dd52aSJames Smart 	uint32_t size;
33013b5dd52aSJames Smart 	unsigned long flags;
33027ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
33033b5dd52aSJames Smart 
33043b5dd52aSJames Smart 	dd_data = pmboxq->context1;
33053b5dd52aSJames Smart 
33067ad20aa9SJames Smart 	/*
33077ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
33087ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
33097a470277SJames Smart 	 */
33107ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
33117ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
33127ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3313515e0aa2SJames Smart 
3314a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3315a33c4f7bSJames Smart 
3316a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3317a33c4f7bSJames Smart 	job = dd_data->set_job;
3318a33c4f7bSJames Smart 	if (job) {
3319a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3320a33c4f7bSJames Smart 		job->dd_data = NULL;
3321a33c4f7bSJames Smart 	}
3322a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3323a33c4f7bSJames Smart 
3324a33c4f7bSJames Smart 	/* Copy the mailbox data to the job if it is still active */
3325a33c4f7bSJames Smart 
33265a6f133eSJames Smart 	if (job) {
33277a470277SJames Smart 		size = job->reply_payload.payload_len;
33283b5dd52aSJames Smart 		job->reply->reply_payload_rcv_len =
33293b5dd52aSJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
33303b5dd52aSJames Smart 					    job->reply_payload.sg_cnt,
33317ad20aa9SJames Smart 					    pmb_buf, size);
3332b6e3b9c6SJames Smart 	}
33337a470277SJames Smart 
3334a33c4f7bSJames Smart 	dd_data->set_job = NULL;
33353b5dd52aSJames Smart 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
33367ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
33373b5dd52aSJames Smart 	kfree(dd_data);
33387ad20aa9SJames Smart 
3339a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3340a33c4f7bSJames Smart 
33417ad20aa9SJames Smart 	if (job) {
33427ad20aa9SJames Smart 		job->reply->result = 0;
33437ad20aa9SJames Smart 		job->job_done(job);
33447ad20aa9SJames Smart 	}
33453b5dd52aSJames Smart 	return;
33463b5dd52aSJames Smart }
33473b5dd52aSJames Smart 
33483b5dd52aSJames Smart /**
33493b5dd52aSJames Smart  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
33503b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
33513b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
33523b5dd52aSJames Smart  * @vport: Pointer to a vport object.
33533b5dd52aSJames Smart  *
33543b5dd52aSJames Smart  * Some commands require the port to be offline, some may not be called from
33553b5dd52aSJames Smart  * the application.
33563b5dd52aSJames Smart  **/
33573b5dd52aSJames Smart static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
33583b5dd52aSJames Smart 	MAILBOX_t *mb, struct lpfc_vport *vport)
33593b5dd52aSJames Smart {
33603b5dd52aSJames Smart 	/* return negative error values for bsg job */
33613b5dd52aSJames Smart 	switch (mb->mbxCommand) {
33623b5dd52aSJames Smart 	/* Offline only */
33633b5dd52aSJames Smart 	case MBX_INIT_LINK:
33643b5dd52aSJames Smart 	case MBX_DOWN_LINK:
33653b5dd52aSJames Smart 	case MBX_CONFIG_LINK:
33663b5dd52aSJames Smart 	case MBX_CONFIG_RING:
33673b5dd52aSJames Smart 	case MBX_RESET_RING:
33683b5dd52aSJames Smart 	case MBX_UNREG_LOGIN:
33693b5dd52aSJames Smart 	case MBX_CLEAR_LA:
33703b5dd52aSJames Smart 	case MBX_DUMP_CONTEXT:
33713b5dd52aSJames Smart 	case MBX_RUN_DIAGS:
33723b5dd52aSJames Smart 	case MBX_RESTART:
33733b5dd52aSJames Smart 	case MBX_SET_MASK:
33743b5dd52aSJames Smart 		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
33753b5dd52aSJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
33763b5dd52aSJames Smart 				"2743 Command 0x%x is illegal in on-line "
33773b5dd52aSJames Smart 				"state\n",
33783b5dd52aSJames Smart 				mb->mbxCommand);
33793b5dd52aSJames Smart 			return -EPERM;
33803b5dd52aSJames Smart 		}
33813b5dd52aSJames Smart 	case MBX_WRITE_NV:
33823b5dd52aSJames Smart 	case MBX_WRITE_VPARMS:
33833b5dd52aSJames Smart 	case MBX_LOAD_SM:
33843b5dd52aSJames Smart 	case MBX_READ_NV:
33853b5dd52aSJames Smart 	case MBX_READ_CONFIG:
33863b5dd52aSJames Smart 	case MBX_READ_RCONFIG:
33873b5dd52aSJames Smart 	case MBX_READ_STATUS:
33883b5dd52aSJames Smart 	case MBX_READ_XRI:
33893b5dd52aSJames Smart 	case MBX_READ_REV:
33903b5dd52aSJames Smart 	case MBX_READ_LNK_STAT:
33913b5dd52aSJames Smart 	case MBX_DUMP_MEMORY:
33923b5dd52aSJames Smart 	case MBX_DOWN_LOAD:
33933b5dd52aSJames Smart 	case MBX_UPDATE_CFG:
33943b5dd52aSJames Smart 	case MBX_KILL_BOARD:
33953b5dd52aSJames Smart 	case MBX_LOAD_AREA:
33963b5dd52aSJames Smart 	case MBX_LOAD_EXP_ROM:
33973b5dd52aSJames Smart 	case MBX_BEACON:
33983b5dd52aSJames Smart 	case MBX_DEL_LD_ENTRY:
33993b5dd52aSJames Smart 	case MBX_SET_DEBUG:
34003b5dd52aSJames Smart 	case MBX_WRITE_WWN:
34013b5dd52aSJames Smart 	case MBX_SLI4_CONFIG:
3402c7495937SJames Smart 	case MBX_READ_EVENT_LOG:
34033b5dd52aSJames Smart 	case MBX_READ_EVENT_LOG_STATUS:
34043b5dd52aSJames Smart 	case MBX_WRITE_EVENT_LOG:
34053b5dd52aSJames Smart 	case MBX_PORT_CAPABILITIES:
34063b5dd52aSJames Smart 	case MBX_PORT_IOV_CONTROL:
34077a470277SJames Smart 	case MBX_RUN_BIU_DIAG64:
34083b5dd52aSJames Smart 		break;
34093b5dd52aSJames Smart 	case MBX_SET_VARIABLE:
3410e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3411e2aed29fSJames Smart 			"1226 mbox: set_variable 0x%x, 0x%x\n",
3412e2aed29fSJames Smart 			mb->un.varWords[0],
3413e2aed29fSJames Smart 			mb->un.varWords[1]);
3414e2aed29fSJames Smart 		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3415e2aed29fSJames Smart 			&& (mb->un.varWords[1] == 1)) {
3416e2aed29fSJames Smart 			phba->wait_4_mlo_maint_flg = 1;
3417e2aed29fSJames Smart 		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
34181b51197dSJames Smart 			spin_lock_irq(&phba->hbalock);
3419e2aed29fSJames Smart 			phba->link_flag &= ~LS_LOOPBACK_MODE;
34201b51197dSJames Smart 			spin_unlock_irq(&phba->hbalock);
342176a95d75SJames Smart 			phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3422e2aed29fSJames Smart 		}
3423e2aed29fSJames Smart 		break;
34243b5dd52aSJames Smart 	case MBX_READ_SPARM64:
342576a95d75SJames Smart 	case MBX_READ_TOPOLOGY:
34263b5dd52aSJames Smart 	case MBX_REG_LOGIN:
34273b5dd52aSJames Smart 	case MBX_REG_LOGIN64:
34283b5dd52aSJames Smart 	case MBX_CONFIG_PORT:
34293b5dd52aSJames Smart 	case MBX_RUN_BIU_DIAG:
34303b5dd52aSJames Smart 	default:
34313b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
34323b5dd52aSJames Smart 			"2742 Unknown Command 0x%x\n",
34333b5dd52aSJames Smart 			mb->mbxCommand);
34343b5dd52aSJames Smart 		return -EPERM;
34353b5dd52aSJames Smart 	}
34363b5dd52aSJames Smart 
34373b5dd52aSJames Smart 	return 0; /* ok */
34383b5dd52aSJames Smart }
34393b5dd52aSJames Smart 
34403b5dd52aSJames Smart /**
34417ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
34427ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
34437ad20aa9SJames Smart  *
34447ad20aa9SJames Smart  * This is routine clean up and reset BSG handling of multi-buffer mbox
34457ad20aa9SJames Smart  * command session.
34467ad20aa9SJames Smart  **/
34477ad20aa9SJames Smart static void
34487ad20aa9SJames Smart lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
34497ad20aa9SJames Smart {
34507ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
34517ad20aa9SJames Smart 		return;
34527ad20aa9SJames Smart 
34537ad20aa9SJames Smart 	/* free all memory, including dma buffers */
34547ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
34557ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
34567ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
34577ad20aa9SJames Smart 	/* multi-buffer write mailbox command pass-through complete */
34587ad20aa9SJames Smart 	memset((char *)&phba->mbox_ext_buf_ctx, 0,
34597ad20aa9SJames Smart 	       sizeof(struct lpfc_mbox_ext_buf_ctx));
34607ad20aa9SJames Smart 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
34617ad20aa9SJames Smart 
34627ad20aa9SJames Smart 	return;
34637ad20aa9SJames Smart }
34647ad20aa9SJames Smart 
34657ad20aa9SJames Smart /**
34667ad20aa9SJames Smart  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
34677ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
34687ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
34697ad20aa9SJames Smart  *
34707ad20aa9SJames Smart  * This is routine handles BSG job for mailbox commands completions with
34717ad20aa9SJames Smart  * multiple external buffers.
34727ad20aa9SJames Smart  **/
34737ad20aa9SJames Smart static struct fc_bsg_job *
34747ad20aa9SJames Smart lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
34757ad20aa9SJames Smart {
34767ad20aa9SJames Smart 	struct bsg_job_data *dd_data;
34777ad20aa9SJames Smart 	struct fc_bsg_job *job;
34787ad20aa9SJames Smart 	uint8_t *pmb, *pmb_buf;
34797ad20aa9SJames Smart 	unsigned long flags;
34807ad20aa9SJames Smart 	uint32_t size;
34817ad20aa9SJames Smart 	int rc = 0;
3482026abb87SJames Smart 	struct lpfc_dmabuf *dmabuf;
3483026abb87SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3484026abb87SJames Smart 	uint8_t *pmbx;
34857ad20aa9SJames Smart 
34867ad20aa9SJames Smart 	dd_data = pmboxq->context1;
3487a33c4f7bSJames Smart 
3488a33c4f7bSJames Smart 	/* Determine if job has been aborted */
3489a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3490a33c4f7bSJames Smart 	job = dd_data->set_job;
3491a33c4f7bSJames Smart 	if (job) {
3492a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
3493a33c4f7bSJames Smart 		job->dd_data = NULL;
34947ad20aa9SJames Smart 	}
3495a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
34967ad20aa9SJames Smart 
34977ad20aa9SJames Smart 	/*
34987ad20aa9SJames Smart 	 * The outgoing buffer is readily referred from the dma buffer,
34997ad20aa9SJames Smart 	 * just need to get header part from mailboxq structure.
35007ad20aa9SJames Smart 	 */
3501a33c4f7bSJames Smart 
35027ad20aa9SJames Smart 	pmb = (uint8_t *)&pmboxq->u.mb;
35037ad20aa9SJames Smart 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3504026abb87SJames Smart 	/* Copy the byte swapped response mailbox back to the user */
35057ad20aa9SJames Smart 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3506026abb87SJames Smart 	/* if there is any non-embedded extended data copy that too */
3507026abb87SJames Smart 	dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3508026abb87SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3509026abb87SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3510026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3511026abb87SJames Smart 		pmbx = (uint8_t *)dmabuf->virt;
3512026abb87SJames Smart 		/* byte swap the extended data following the mailbox command */
3513026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3514026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
3515026abb87SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3516026abb87SJames Smart 	}
35177ad20aa9SJames Smart 
3518a33c4f7bSJames Smart 	/* Complete the job if the job is still active */
3519a33c4f7bSJames Smart 
35207ad20aa9SJames Smart 	if (job) {
35217ad20aa9SJames Smart 		size = job->reply_payload.payload_len;
35227ad20aa9SJames Smart 		job->reply->reply_payload_rcv_len =
35237ad20aa9SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
35247ad20aa9SJames Smart 					    job->reply_payload.sg_cnt,
35257ad20aa9SJames Smart 					    pmb_buf, size);
3526a33c4f7bSJames Smart 
35277ad20aa9SJames Smart 		/* result for successful */
35287ad20aa9SJames Smart 		job->reply->result = 0;
3529a33c4f7bSJames Smart 
35307ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
35317ad20aa9SJames Smart 				"2937 SLI_CONFIG ext-buffer maibox command "
35327ad20aa9SJames Smart 				"(x%x/x%x) complete bsg job done, bsize:%d\n",
35337ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
35347ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, size);
3535b76f2dc9SJames Smart 		lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3536b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.nembType,
3537b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mboxType,
3538b76f2dc9SJames Smart 					dma_ebuf, sta_pos_addr,
3539b76f2dc9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3540a33c4f7bSJames Smart 	} else {
35417ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
35427ad20aa9SJames Smart 				"2938 SLI_CONFIG ext-buffer maibox "
35437ad20aa9SJames Smart 				"command (x%x/x%x) failure, rc:x%x\n",
35447ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.nembType,
35457ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.mboxType, rc);
3546a33c4f7bSJames Smart 	}
3547a33c4f7bSJames Smart 
3548a33c4f7bSJames Smart 
35497ad20aa9SJames Smart 	/* state change */
35507ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
35517ad20aa9SJames Smart 	kfree(dd_data);
35527ad20aa9SJames Smart 	return job;
35537ad20aa9SJames Smart }
35547ad20aa9SJames Smart 
35557ad20aa9SJames Smart /**
35567ad20aa9SJames Smart  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
35577ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
35587ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
35597ad20aa9SJames Smart  *
35607ad20aa9SJames Smart  * This is completion handler function for mailbox read commands with multiple
35617ad20aa9SJames Smart  * external buffers.
35627ad20aa9SJames Smart  **/
35637ad20aa9SJames Smart static void
35647ad20aa9SJames Smart lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
35657ad20aa9SJames Smart {
35667ad20aa9SJames Smart 	struct fc_bsg_job *job;
35677ad20aa9SJames Smart 
3568a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3569a33c4f7bSJames Smart 
35707ad20aa9SJames Smart 	/* handle the BSG job with mailbox command */
3571a33c4f7bSJames Smart 	if (!job)
35727ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
35737ad20aa9SJames Smart 
35747ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
35757ad20aa9SJames Smart 			"2939 SLI_CONFIG ext-buffer rd maibox command "
35767ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
35777ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
35787ad20aa9SJames Smart 
35797ad20aa9SJames Smart 	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
35807ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
35817ad20aa9SJames Smart 
35827ad20aa9SJames Smart 	/* free base driver mailbox structure memory */
35837ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
35847ad20aa9SJames Smart 
3585a33c4f7bSJames Smart 	/* if the job is still active, call job done */
35867ad20aa9SJames Smart 	if (job)
35877ad20aa9SJames Smart 		job->job_done(job);
35887ad20aa9SJames Smart 
35897ad20aa9SJames Smart 	return;
35907ad20aa9SJames Smart }
35917ad20aa9SJames Smart 
35927ad20aa9SJames Smart /**
35937ad20aa9SJames Smart  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
35947ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
35957ad20aa9SJames Smart  * @pmboxq: Pointer to mailbox command.
35967ad20aa9SJames Smart  *
35977ad20aa9SJames Smart  * This is completion handler function for mailbox write commands with multiple
35987ad20aa9SJames Smart  * external buffers.
35997ad20aa9SJames Smart  **/
36007ad20aa9SJames Smart static void
36017ad20aa9SJames Smart lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
36027ad20aa9SJames Smart {
36037ad20aa9SJames Smart 	struct fc_bsg_job *job;
36047ad20aa9SJames Smart 
3605a33c4f7bSJames Smart 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3606a33c4f7bSJames Smart 
36077ad20aa9SJames Smart 	/* handle the BSG job with the mailbox command */
3608a33c4f7bSJames Smart 	if (!job)
36097ad20aa9SJames Smart 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
36107ad20aa9SJames Smart 
36117ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36127ad20aa9SJames Smart 			"2940 SLI_CONFIG ext-buffer wr maibox command "
36137ad20aa9SJames Smart 			"complete, ctxState:x%x, mbxStatus:x%x\n",
36147ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
36157ad20aa9SJames Smart 
36167ad20aa9SJames Smart 	/* free all memory, including dma buffers */
36177ad20aa9SJames Smart 	mempool_free(pmboxq, phba->mbox_mem_pool);
36187ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
36197ad20aa9SJames Smart 
3620a33c4f7bSJames Smart 	/* if the job is still active, call job done */
36217ad20aa9SJames Smart 	if (job)
36227ad20aa9SJames Smart 		job->job_done(job);
36237ad20aa9SJames Smart 
36247ad20aa9SJames Smart 	return;
36257ad20aa9SJames Smart }
36267ad20aa9SJames Smart 
36277ad20aa9SJames Smart static void
36287ad20aa9SJames Smart lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
36297ad20aa9SJames Smart 				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
36307ad20aa9SJames Smart 				struct lpfc_dmabuf *ext_dmabuf)
36317ad20aa9SJames Smart {
36327ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
36337ad20aa9SJames Smart 
36347ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
36357ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
36367ad20aa9SJames Smart 
36377ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
36387ad20aa9SJames Smart 		if (index == 0) {
36397ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
36407ad20aa9SJames Smart 				mse[index].pa_hi =
36417ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
36427ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
36437ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
36447ad20aa9SJames Smart 				mse[index].pa_lo =
36457ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
36467ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
36477ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36487ad20aa9SJames Smart 					"2943 SLI_CONFIG(mse)[%d], "
36497ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
36507ad20aa9SJames Smart 					index,
36517ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36527ad20aa9SJames Smart 					mse[index].buf_len,
36537ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36547ad20aa9SJames Smart 					mse[index].pa_hi,
36557ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36567ad20aa9SJames Smart 					mse[index].pa_lo);
36577ad20aa9SJames Smart 		} else {
36587ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
36597ad20aa9SJames Smart 				mse[index].pa_hi =
36607ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
36617ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb0_subsys.
36627ad20aa9SJames Smart 				mse[index].pa_lo =
36637ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
36647ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36657ad20aa9SJames Smart 					"2944 SLI_CONFIG(mse)[%d], "
36667ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
36677ad20aa9SJames Smart 					index,
36687ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36697ad20aa9SJames Smart 					mse[index].buf_len,
36707ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36717ad20aa9SJames Smart 					mse[index].pa_hi,
36727ad20aa9SJames Smart 					sli_cfg_mbx->un.sli_config_emb0_subsys.
36737ad20aa9SJames Smart 					mse[index].pa_lo);
36747ad20aa9SJames Smart 		}
36757ad20aa9SJames Smart 	} else {
36767ad20aa9SJames Smart 		if (index == 0) {
36777ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
36787ad20aa9SJames Smart 				hbd[index].pa_hi =
36797ad20aa9SJames Smart 				putPaddrHigh(mbx_dmabuf->phys +
36807ad20aa9SJames Smart 					     sizeof(MAILBOX_t));
36817ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
36827ad20aa9SJames Smart 				hbd[index].pa_lo =
36837ad20aa9SJames Smart 				putPaddrLow(mbx_dmabuf->phys +
36847ad20aa9SJames Smart 					    sizeof(MAILBOX_t));
36857ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
36867ad20aa9SJames Smart 					"3007 SLI_CONFIG(hbd)[%d], "
36877ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
36887ad20aa9SJames Smart 				index,
36897ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
36907ad20aa9SJames Smart 				&sli_cfg_mbx->un.
36917ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
36927ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
36937ad20aa9SJames Smart 				hbd[index].pa_hi,
36947ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
36957ad20aa9SJames Smart 				hbd[index].pa_lo);
36967ad20aa9SJames Smart 
36977ad20aa9SJames Smart 		} else {
36987ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
36997ad20aa9SJames Smart 				hbd[index].pa_hi =
37007ad20aa9SJames Smart 				putPaddrHigh(ext_dmabuf->phys);
37017ad20aa9SJames Smart 			sli_cfg_mbx->un.sli_config_emb1_subsys.
37027ad20aa9SJames Smart 				hbd[index].pa_lo =
37037ad20aa9SJames Smart 				putPaddrLow(ext_dmabuf->phys);
37047ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37057ad20aa9SJames Smart 					"3008 SLI_CONFIG(hbd)[%d], "
37067ad20aa9SJames Smart 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
37077ad20aa9SJames Smart 				index,
37087ad20aa9SJames Smart 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
37097ad20aa9SJames Smart 				&sli_cfg_mbx->un.
37107ad20aa9SJames Smart 				sli_config_emb1_subsys.hbd[index]),
37117ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
37127ad20aa9SJames Smart 				hbd[index].pa_hi,
37137ad20aa9SJames Smart 				sli_cfg_mbx->un.sli_config_emb1_subsys.
37147ad20aa9SJames Smart 				hbd[index].pa_lo);
37157ad20aa9SJames Smart 		}
37167ad20aa9SJames Smart 	}
37177ad20aa9SJames Smart 	return;
37187ad20aa9SJames Smart }
37197ad20aa9SJames Smart 
37207ad20aa9SJames Smart /**
37217ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
37227ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
37237ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
37247ad20aa9SJames Smart  * @nemb_tp: Enumerate of non-embedded mailbox command type.
37257ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
37267ad20aa9SJames Smart  *
37277ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
37287ad20aa9SJames Smart  * non-embedded external bufffers.
37297ad20aa9SJames Smart  **/
37307ad20aa9SJames Smart static int
37317ad20aa9SJames Smart lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
37327ad20aa9SJames Smart 			      enum nemb_type nemb_tp,
37337ad20aa9SJames Smart 			      struct lpfc_dmabuf *dmabuf)
37347ad20aa9SJames Smart {
37357ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
37367ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
37377ad20aa9SJames Smart 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
37387ad20aa9SJames Smart 	uint32_t ext_buf_cnt, ext_buf_index;
37397ad20aa9SJames Smart 	struct lpfc_dmabuf *ext_dmabuf = NULL;
37407ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
37417ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
37427ad20aa9SJames Smart 	MAILBOX_t *pmb;
37437ad20aa9SJames Smart 	uint8_t *pmbx;
37447ad20aa9SJames Smart 	int rc, i;
37457ad20aa9SJames Smart 
37467ad20aa9SJames Smart 	mbox_req =
37477ad20aa9SJames Smart 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
37487ad20aa9SJames Smart 
37497ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
37507ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
37517ad20aa9SJames Smart 
37527ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
37537ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
37547ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
37557ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
37567ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
37577ad20aa9SJames Smart 					"2945 Handled SLI_CONFIG(mse) rd, "
37587ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
37597ad20aa9SJames Smart 					ext_buf_cnt,
37607ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
37617ad20aa9SJames Smart 			rc = -ERANGE;
37627ad20aa9SJames Smart 			goto job_error;
37637ad20aa9SJames Smart 		}
37647ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37657ad20aa9SJames Smart 				"2941 Handled SLI_CONFIG(mse) rd, "
37667ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
37677ad20aa9SJames Smart 	} else {
37687ad20aa9SJames Smart 		/* sanity check on interface type for support */
37697ad20aa9SJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
37707ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2) {
37717ad20aa9SJames Smart 			rc = -ENODEV;
37727ad20aa9SJames Smart 			goto job_error;
37737ad20aa9SJames Smart 		}
37747ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
37757ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
37767ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
37777ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
37787ad20aa9SJames Smart 					"2946 Handled SLI_CONFIG(hbd) rd, "
37797ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
37807ad20aa9SJames Smart 					ext_buf_cnt,
37817ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
37827ad20aa9SJames Smart 			rc = -ERANGE;
37837ad20aa9SJames Smart 			goto job_error;
37847ad20aa9SJames Smart 		}
37857ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
37867ad20aa9SJames Smart 				"2942 Handled SLI_CONFIG(hbd) rd, "
37877ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
37887ad20aa9SJames Smart 	}
37897ad20aa9SJames Smart 
3790b76f2dc9SJames Smart 	/* before dma descriptor setup */
3791b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3792b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
3793b76f2dc9SJames Smart 
37947ad20aa9SJames Smart 	/* reject non-embedded mailbox command with none external buffer */
37957ad20aa9SJames Smart 	if (ext_buf_cnt == 0) {
37967ad20aa9SJames Smart 		rc = -EPERM;
37977ad20aa9SJames Smart 		goto job_error;
37987ad20aa9SJames Smart 	} else if (ext_buf_cnt > 1) {
37997ad20aa9SJames Smart 		/* additional external read buffers */
38007ad20aa9SJames Smart 		for (i = 1; i < ext_buf_cnt; i++) {
38017ad20aa9SJames Smart 			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
38027ad20aa9SJames Smart 			if (!ext_dmabuf) {
38037ad20aa9SJames Smart 				rc = -ENOMEM;
38047ad20aa9SJames Smart 				goto job_error;
38057ad20aa9SJames Smart 			}
38067ad20aa9SJames Smart 			list_add_tail(&ext_dmabuf->list,
38077ad20aa9SJames Smart 				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
38087ad20aa9SJames Smart 		}
38097ad20aa9SJames Smart 	}
38107ad20aa9SJames Smart 
38117ad20aa9SJames Smart 	/* bsg tracking structure */
38127ad20aa9SJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
38137ad20aa9SJames Smart 	if (!dd_data) {
38147ad20aa9SJames Smart 		rc = -ENOMEM;
38157ad20aa9SJames Smart 		goto job_error;
38167ad20aa9SJames Smart 	}
38177ad20aa9SJames Smart 
38187ad20aa9SJames Smart 	/* mailbox command structure for base driver */
38197ad20aa9SJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
38207ad20aa9SJames Smart 	if (!pmboxq) {
38217ad20aa9SJames Smart 		rc = -ENOMEM;
38227ad20aa9SJames Smart 		goto job_error;
38237ad20aa9SJames Smart 	}
38247ad20aa9SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
38257ad20aa9SJames Smart 
38267ad20aa9SJames Smart 	/* for the first external buffer */
38277ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
38287ad20aa9SJames Smart 
38297ad20aa9SJames Smart 	/* for the rest of external buffer descriptors if any */
38307ad20aa9SJames Smart 	if (ext_buf_cnt > 1) {
38317ad20aa9SJames Smart 		ext_buf_index = 1;
38327ad20aa9SJames Smart 		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
38337ad20aa9SJames Smart 				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
38347ad20aa9SJames Smart 			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
38357ad20aa9SJames Smart 						ext_buf_index, dmabuf,
38367ad20aa9SJames Smart 						curr_dmabuf);
38377ad20aa9SJames Smart 			ext_buf_index++;
38387ad20aa9SJames Smart 		}
38397ad20aa9SJames Smart 	}
38407ad20aa9SJames Smart 
3841b76f2dc9SJames Smart 	/* after dma descriptor setup */
3842b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
3843b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
3844b76f2dc9SJames Smart 
38457ad20aa9SJames Smart 	/* construct base driver mbox command */
38467ad20aa9SJames Smart 	pmb = &pmboxq->u.mb;
38477ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
38487ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
38497ad20aa9SJames Smart 	pmb->mbxOwner = OWN_HOST;
38507ad20aa9SJames Smart 	pmboxq->vport = phba->pport;
38517ad20aa9SJames Smart 
38527ad20aa9SJames Smart 	/* multi-buffer handling context */
38537ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
38547ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
38557ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
38567ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
38577ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
38587ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
38597ad20aa9SJames Smart 
38607ad20aa9SJames Smart 	/* callback for multi-buffer read mailbox command */
38617ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
38627ad20aa9SJames Smart 
38637ad20aa9SJames Smart 	/* context fields to callback function */
38647ad20aa9SJames Smart 	pmboxq->context1 = dd_data;
38657ad20aa9SJames Smart 	dd_data->type = TYPE_MBOX;
3866a33c4f7bSJames Smart 	dd_data->set_job = job;
38677ad20aa9SJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
38687ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
38697ad20aa9SJames Smart 	job->dd_data = dd_data;
38707ad20aa9SJames Smart 
38717ad20aa9SJames Smart 	/* state change */
38727ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
38737ad20aa9SJames Smart 
3874026abb87SJames Smart 	/*
3875026abb87SJames Smart 	 * Non-embedded mailbox subcommand data gets byte swapped here because
3876026abb87SJames Smart 	 * the lower level driver code only does the first 64 mailbox words.
3877026abb87SJames Smart 	 */
3878026abb87SJames Smart 	if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
3879026abb87SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
3880026abb87SJames Smart 		(nemb_tp == nemb_mse))
3881026abb87SJames Smart 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3882026abb87SJames Smart 			&pmbx[sizeof(MAILBOX_t)],
3883026abb87SJames Smart 				sli_cfg_mbx->un.sli_config_emb0_subsys.
3884026abb87SJames Smart 					mse[0].buf_len);
3885026abb87SJames Smart 
38867ad20aa9SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
38877ad20aa9SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
38887ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
38897ad20aa9SJames Smart 				"2947 Issued SLI_CONFIG ext-buffer "
38907ad20aa9SJames Smart 				"maibox command, rc:x%x\n", rc);
389188a2cfbbSJames Smart 		return SLI_CONFIG_HANDLED;
38927ad20aa9SJames Smart 	}
38937ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
38947ad20aa9SJames Smart 			"2948 Failed to issue SLI_CONFIG ext-buffer "
38957ad20aa9SJames Smart 			"maibox command, rc:x%x\n", rc);
38967ad20aa9SJames Smart 	rc = -EPIPE;
38977ad20aa9SJames Smart 
38987ad20aa9SJames Smart job_error:
38997ad20aa9SJames Smart 	if (pmboxq)
39007ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
39017ad20aa9SJames Smart 	lpfc_bsg_dma_page_list_free(phba,
39027ad20aa9SJames Smart 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
39037ad20aa9SJames Smart 	kfree(dd_data);
39047ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
39057ad20aa9SJames Smart 	return rc;
39067ad20aa9SJames Smart }
39077ad20aa9SJames Smart 
39087ad20aa9SJames Smart /**
39097ad20aa9SJames Smart  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
39107ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
39117ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
39127ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
39137ad20aa9SJames Smart  *
39147ad20aa9SJames Smart  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
39157ad20aa9SJames Smart  * non-embedded external bufffers.
39167ad20aa9SJames Smart  **/
39177ad20aa9SJames Smart static int
39187ad20aa9SJames Smart lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
39197ad20aa9SJames Smart 			       enum nemb_type nemb_tp,
39207ad20aa9SJames Smart 			       struct lpfc_dmabuf *dmabuf)
39217ad20aa9SJames Smart {
39227ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
39237ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
39247ad20aa9SJames Smart 	uint32_t ext_buf_cnt;
39257ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
39267ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
39277ad20aa9SJames Smart 	MAILBOX_t *pmb;
39287ad20aa9SJames Smart 	uint8_t *mbx;
392988a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED, i;
39307ad20aa9SJames Smart 
39317ad20aa9SJames Smart 	mbox_req =
39327ad20aa9SJames Smart 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
39337ad20aa9SJames Smart 
39347ad20aa9SJames Smart 	/* pointer to the start of mailbox command */
39357ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
39367ad20aa9SJames Smart 
39377ad20aa9SJames Smart 	if (nemb_tp == nemb_mse) {
39387ad20aa9SJames Smart 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
39397ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
39407ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
39417ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3942026abb87SJames Smart 					"2953 Failed SLI_CONFIG(mse) wr, "
39437ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
39447ad20aa9SJames Smart 					ext_buf_cnt,
39457ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
39467ad20aa9SJames Smart 			return -ERANGE;
39477ad20aa9SJames Smart 		}
39487ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39497ad20aa9SJames Smart 				"2949 Handled SLI_CONFIG(mse) wr, "
39507ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
39517ad20aa9SJames Smart 	} else {
39527ad20aa9SJames Smart 		/* sanity check on interface type for support */
39537ad20aa9SJames Smart 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
39547ad20aa9SJames Smart 		    LPFC_SLI_INTF_IF_TYPE_2)
39557ad20aa9SJames Smart 			return -ENODEV;
39567ad20aa9SJames Smart 		/* nemb_tp == nemb_hbd */
39577ad20aa9SJames Smart 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
39587ad20aa9SJames Smart 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
39597ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3960026abb87SJames Smart 					"2954 Failed SLI_CONFIG(hbd) wr, "
39617ad20aa9SJames Smart 					"ext_buf_cnt(%d) out of range(%d)\n",
39627ad20aa9SJames Smart 					ext_buf_cnt,
39637ad20aa9SJames Smart 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
39647ad20aa9SJames Smart 			return -ERANGE;
39657ad20aa9SJames Smart 		}
39667ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39677ad20aa9SJames Smart 				"2950 Handled SLI_CONFIG(hbd) wr, "
39687ad20aa9SJames Smart 				"ext_buf_cnt:%d\n", ext_buf_cnt);
39697ad20aa9SJames Smart 	}
39707ad20aa9SJames Smart 
3971b76f2dc9SJames Smart 	/* before dma buffer descriptor setup */
3972b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3973b76f2dc9SJames Smart 					sta_pre_addr, dmabuf, ext_buf_cnt);
3974b76f2dc9SJames Smart 
39757ad20aa9SJames Smart 	if (ext_buf_cnt == 0)
39767ad20aa9SJames Smart 		return -EPERM;
39777ad20aa9SJames Smart 
39787ad20aa9SJames Smart 	/* for the first external buffer */
39797ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
39807ad20aa9SJames Smart 
3981b76f2dc9SJames Smart 	/* after dma descriptor setup */
3982b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
3983b76f2dc9SJames Smart 					sta_pos_addr, dmabuf, ext_buf_cnt);
3984b76f2dc9SJames Smart 
39857ad20aa9SJames Smart 	/* log for looking forward */
39867ad20aa9SJames Smart 	for (i = 1; i < ext_buf_cnt; i++) {
39877ad20aa9SJames Smart 		if (nemb_tp == nemb_mse)
39887ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39897ad20aa9SJames Smart 				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
39907ad20aa9SJames Smart 				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
39917ad20aa9SJames Smart 				mse[i].buf_len);
39927ad20aa9SJames Smart 		else
39937ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
39947ad20aa9SJames Smart 				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
39957ad20aa9SJames Smart 				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
39967ad20aa9SJames Smart 				&sli_cfg_mbx->un.sli_config_emb1_subsys.
39977ad20aa9SJames Smart 				hbd[i]));
39987ad20aa9SJames Smart 	}
39997ad20aa9SJames Smart 
40007ad20aa9SJames Smart 	/* multi-buffer handling context */
40017ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
40027ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
40037ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
40047ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
40057ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
40067ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
40077ad20aa9SJames Smart 
40087ad20aa9SJames Smart 	if (ext_buf_cnt == 1) {
40097ad20aa9SJames Smart 		/* bsg tracking structure */
40107ad20aa9SJames Smart 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
40117ad20aa9SJames Smart 		if (!dd_data) {
40127ad20aa9SJames Smart 			rc = -ENOMEM;
40137ad20aa9SJames Smart 			goto job_error;
40147ad20aa9SJames Smart 		}
40157ad20aa9SJames Smart 
40167ad20aa9SJames Smart 		/* mailbox command structure for base driver */
40177ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
40187ad20aa9SJames Smart 		if (!pmboxq) {
40197ad20aa9SJames Smart 			rc = -ENOMEM;
40207ad20aa9SJames Smart 			goto job_error;
40217ad20aa9SJames Smart 		}
40227ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
40237ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
40247ad20aa9SJames Smart 		mbx = (uint8_t *)dmabuf->virt;
40257ad20aa9SJames Smart 		memcpy(pmb, mbx, sizeof(*pmb));
40267ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
40277ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
40287ad20aa9SJames Smart 
40297ad20aa9SJames Smart 		/* callback for multi-buffer read mailbox command */
40307ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
40317ad20aa9SJames Smart 
40327ad20aa9SJames Smart 		/* context fields to callback function */
40337ad20aa9SJames Smart 		pmboxq->context1 = dd_data;
40347ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4035a33c4f7bSJames Smart 		dd_data->set_job = job;
40367ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
40377ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
40387ad20aa9SJames Smart 		job->dd_data = dd_data;
40397ad20aa9SJames Smart 
40407ad20aa9SJames Smart 		/* state change */
40417ad20aa9SJames Smart 
4042a33c4f7bSJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
40437ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
40447ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
40457ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
40467ad20aa9SJames Smart 					"2955 Issued SLI_CONFIG ext-buffer "
40477ad20aa9SJames Smart 					"maibox command, rc:x%x\n", rc);
404888a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
40497ad20aa9SJames Smart 		}
40507ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
40517ad20aa9SJames Smart 				"2956 Failed to issue SLI_CONFIG ext-buffer "
40527ad20aa9SJames Smart 				"maibox command, rc:x%x\n", rc);
40537ad20aa9SJames Smart 		rc = -EPIPE;
4054026abb87SJames Smart 		goto job_error;
40557ad20aa9SJames Smart 	}
40567ad20aa9SJames Smart 
405788a2cfbbSJames Smart 	/* wait for additoinal external buffers */
4058a33c4f7bSJames Smart 
405988a2cfbbSJames Smart 	job->reply->result = 0;
406088a2cfbbSJames Smart 	job->job_done(job);
406188a2cfbbSJames Smart 	return SLI_CONFIG_HANDLED;
406288a2cfbbSJames Smart 
40637ad20aa9SJames Smart job_error:
40647ad20aa9SJames Smart 	if (pmboxq)
40657ad20aa9SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
40667ad20aa9SJames Smart 	kfree(dd_data);
40677ad20aa9SJames Smart 
40687ad20aa9SJames Smart 	return rc;
40697ad20aa9SJames Smart }
40707ad20aa9SJames Smart 
40717ad20aa9SJames Smart /**
40727ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
40737ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
40747ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
40757ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
40767ad20aa9SJames Smart  *
40777ad20aa9SJames Smart  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
40787ad20aa9SJames Smart  * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
40797ad20aa9SJames Smart  * with embedded sussystem 0x1 and opcodes with external HBDs.
40807ad20aa9SJames Smart  **/
40817ad20aa9SJames Smart static int
40827ad20aa9SJames Smart lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
40837ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
40847ad20aa9SJames Smart {
40857ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
40867ad20aa9SJames Smart 	uint32_t subsys;
40877ad20aa9SJames Smart 	uint32_t opcode;
40887ad20aa9SJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
40897ad20aa9SJames Smart 
4090026abb87SJames Smart 	/* state change on new multi-buffer pass-through mailbox command */
40917ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
40927ad20aa9SJames Smart 
40937ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
40947ad20aa9SJames Smart 
40957ad20aa9SJames Smart 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
40967ad20aa9SJames Smart 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
40977ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
40987ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
40997ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
41007ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
41017ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
41027ad20aa9SJames Smart 			switch (opcode) {
41037ad20aa9SJames Smart 			case FCOE_OPCODE_READ_FCF:
41047ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41057ad20aa9SJames Smart 						"2957 Handled SLI_CONFIG "
41067ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
41077ad20aa9SJames Smart 						opcode);
41087ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
41097ad20aa9SJames Smart 							nemb_mse, dmabuf);
41107ad20aa9SJames Smart 				break;
41117ad20aa9SJames Smart 			case FCOE_OPCODE_ADD_FCF:
41127ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41137ad20aa9SJames Smart 						"2958 Handled SLI_CONFIG "
41147ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
41157ad20aa9SJames Smart 						opcode);
41167ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
41177ad20aa9SJames Smart 							nemb_mse, dmabuf);
41187ad20aa9SJames Smart 				break;
41197ad20aa9SJames Smart 			default:
41207ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4121026abb87SJames Smart 						"2959 Reject SLI_CONFIG "
41227ad20aa9SJames Smart 						"subsys_fcoe, opcode:x%x\n",
41237ad20aa9SJames Smart 						opcode);
4124026abb87SJames Smart 				rc = -EPERM;
4125026abb87SJames Smart 				break;
4126026abb87SJames Smart 			}
4127026abb87SJames Smart 		} else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4128026abb87SJames Smart 			switch (opcode) {
4129026abb87SJames Smart 			case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4130b99570ddSJames Smart 			case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4131026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4132026abb87SJames Smart 						"3106 Handled SLI_CONFIG "
41336b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4134026abb87SJames Smart 						opcode);
4135026abb87SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4136026abb87SJames Smart 							nemb_mse, dmabuf);
4137026abb87SJames Smart 				break;
4138026abb87SJames Smart 			default:
4139026abb87SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4140026abb87SJames Smart 						"3107 Reject SLI_CONFIG "
41416b5151fdSJames Smart 						"subsys_comn, opcode:x%x\n",
4142026abb87SJames Smart 						opcode);
4143026abb87SJames Smart 				rc = -EPERM;
41447ad20aa9SJames Smart 				break;
41457ad20aa9SJames Smart 			}
41467ad20aa9SJames Smart 		} else {
41477ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4148026abb87SJames Smart 					"2977 Reject SLI_CONFIG "
41497ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
41507ad20aa9SJames Smart 					subsys, opcode);
4151026abb87SJames Smart 			rc = -EPERM;
41527ad20aa9SJames Smart 		}
41537ad20aa9SJames Smart 	} else {
41547ad20aa9SJames Smart 		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
41557ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
41567ad20aa9SJames Smart 		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
41577ad20aa9SJames Smart 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
41587ad20aa9SJames Smart 		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
41597ad20aa9SJames Smart 			switch (opcode) {
41607ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT:
41617ad20aa9SJames Smart 			case COMN_OPCODE_READ_OBJECT_LIST:
41627ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41637ad20aa9SJames Smart 						"2960 Handled SLI_CONFIG "
41647ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
41657ad20aa9SJames Smart 						opcode);
41667ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
41677ad20aa9SJames Smart 							nemb_hbd, dmabuf);
41687ad20aa9SJames Smart 				break;
41697ad20aa9SJames Smart 			case COMN_OPCODE_WRITE_OBJECT:
41707ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41717ad20aa9SJames Smart 						"2961 Handled SLI_CONFIG "
41727ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
41737ad20aa9SJames Smart 						opcode);
41747ad20aa9SJames Smart 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
41757ad20aa9SJames Smart 							nemb_hbd, dmabuf);
41767ad20aa9SJames Smart 				break;
41777ad20aa9SJames Smart 			default:
41787ad20aa9SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
41797ad20aa9SJames Smart 						"2962 Not handled SLI_CONFIG "
41807ad20aa9SJames Smart 						"subsys_comn, opcode:x%x\n",
41817ad20aa9SJames Smart 						opcode);
41827ad20aa9SJames Smart 				rc = SLI_CONFIG_NOT_HANDLED;
41837ad20aa9SJames Smart 				break;
41847ad20aa9SJames Smart 			}
41857ad20aa9SJames Smart 		} else {
41867ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4187026abb87SJames Smart 					"2978 Not handled SLI_CONFIG "
41887ad20aa9SJames Smart 					"subsys:x%d, opcode:x%x\n",
41897ad20aa9SJames Smart 					subsys, opcode);
41907ad20aa9SJames Smart 			rc = SLI_CONFIG_NOT_HANDLED;
41917ad20aa9SJames Smart 		}
41927ad20aa9SJames Smart 	}
4193026abb87SJames Smart 
4194026abb87SJames Smart 	/* state reset on not handled new multi-buffer mailbox command */
4195026abb87SJames Smart 	if (rc != SLI_CONFIG_HANDLED)
4196026abb87SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4197026abb87SJames Smart 
41987ad20aa9SJames Smart 	return rc;
41997ad20aa9SJames Smart }
42007ad20aa9SJames Smart 
42017ad20aa9SJames Smart /**
42027ad20aa9SJames Smart  * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
42037ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
42047ad20aa9SJames Smart  *
42057ad20aa9SJames Smart  * This routine is for requesting to abort a pass-through mailbox command with
42067ad20aa9SJames Smart  * multiple external buffers due to error condition.
42077ad20aa9SJames Smart  **/
42087ad20aa9SJames Smart static void
42097ad20aa9SJames Smart lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
42107ad20aa9SJames Smart {
42117ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
42127ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
42137ad20aa9SJames Smart 	else
42147ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
42157ad20aa9SJames Smart 	return;
42167ad20aa9SJames Smart }
42177ad20aa9SJames Smart 
42187ad20aa9SJames Smart /**
42197ad20aa9SJames Smart  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
42207ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
42217ad20aa9SJames Smart  * @dmabuf: Pointer to a DMA buffer descriptor.
42227ad20aa9SJames Smart  *
42237ad20aa9SJames Smart  * This routine extracts the next mailbox read external buffer back to
42247ad20aa9SJames Smart  * user space through BSG.
42257ad20aa9SJames Smart  **/
42267ad20aa9SJames Smart static int
42277ad20aa9SJames Smart lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
42287ad20aa9SJames Smart {
42297ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
42307ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf;
42317ad20aa9SJames Smart 	uint8_t *pbuf;
42327ad20aa9SJames Smart 	uint32_t size;
42337ad20aa9SJames Smart 	uint32_t index;
42347ad20aa9SJames Smart 
42357ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
42367ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
42377ad20aa9SJames Smart 
42387ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
42397ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
42407ad20aa9SJames Smart 
42417ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
42427ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
42437ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
42447ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42457ad20aa9SJames Smart 				"2963 SLI_CONFIG (mse) ext-buffer rd get "
42467ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
42477ad20aa9SJames Smart 	} else {
42487ad20aa9SJames Smart 		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
42497ad20aa9SJames Smart 			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
42507ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42517ad20aa9SJames Smart 				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
42527ad20aa9SJames Smart 				"buffer[%d], size:%d\n", index, size);
42537ad20aa9SJames Smart 	}
42547ad20aa9SJames Smart 	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
42557ad20aa9SJames Smart 		return -EPIPE;
42567ad20aa9SJames Smart 	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
42577ad20aa9SJames Smart 				  struct lpfc_dmabuf, list);
42587ad20aa9SJames Smart 	list_del_init(&dmabuf->list);
4259b76f2dc9SJames Smart 
4260b76f2dc9SJames Smart 	/* after dma buffer descriptor setup */
4261b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4262b76f2dc9SJames Smart 					mbox_rd, dma_ebuf, sta_pos_addr,
4263b76f2dc9SJames Smart 					dmabuf, index);
4264b76f2dc9SJames Smart 
42657ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
42667ad20aa9SJames Smart 	job->reply->reply_payload_rcv_len =
42677ad20aa9SJames Smart 		sg_copy_from_buffer(job->reply_payload.sg_list,
42687ad20aa9SJames Smart 				    job->reply_payload.sg_cnt,
42697ad20aa9SJames Smart 				    pbuf, size);
42707ad20aa9SJames Smart 
42717ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
42727ad20aa9SJames Smart 
42737ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
42747ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
42757ad20aa9SJames Smart 				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
42767ad20aa9SJames Smart 				"command session done\n");
42777ad20aa9SJames Smart 		lpfc_bsg_mbox_ext_session_reset(phba);
42787ad20aa9SJames Smart 	}
42797ad20aa9SJames Smart 
42807ad20aa9SJames Smart 	job->reply->result = 0;
42817ad20aa9SJames Smart 	job->job_done(job);
42827ad20aa9SJames Smart 
42837ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
42847ad20aa9SJames Smart }
42857ad20aa9SJames Smart 
42867ad20aa9SJames Smart /**
42877ad20aa9SJames Smart  * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
42887ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
42897ad20aa9SJames Smart  * @dmabuf: Pointer to a DMA buffer descriptor.
42907ad20aa9SJames Smart  *
42917ad20aa9SJames Smart  * This routine sets up the next mailbox read external buffer obtained
42927ad20aa9SJames Smart  * from user space through BSG.
42937ad20aa9SJames Smart  **/
42947ad20aa9SJames Smart static int
42957ad20aa9SJames Smart lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
42967ad20aa9SJames Smart 			struct lpfc_dmabuf *dmabuf)
42977ad20aa9SJames Smart {
42987ad20aa9SJames Smart 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
42997ad20aa9SJames Smart 	struct bsg_job_data *dd_data = NULL;
43007ad20aa9SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL;
43017ad20aa9SJames Smart 	MAILBOX_t *pmb;
43027ad20aa9SJames Smart 	enum nemb_type nemb_tp;
43037ad20aa9SJames Smart 	uint8_t *pbuf;
43047ad20aa9SJames Smart 	uint32_t size;
43057ad20aa9SJames Smart 	uint32_t index;
43067ad20aa9SJames Smart 	int rc;
43077ad20aa9SJames Smart 
43087ad20aa9SJames Smart 	index = phba->mbox_ext_buf_ctx.seqNum;
43097ad20aa9SJames Smart 	phba->mbox_ext_buf_ctx.seqNum++;
43107ad20aa9SJames Smart 	nemb_tp = phba->mbox_ext_buf_ctx.nembType;
43117ad20aa9SJames Smart 
43127ad20aa9SJames Smart 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
43137ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
43147ad20aa9SJames Smart 
43157ad20aa9SJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
43167ad20aa9SJames Smart 	if (!dd_data) {
43177ad20aa9SJames Smart 		rc = -ENOMEM;
43187ad20aa9SJames Smart 		goto job_error;
43197ad20aa9SJames Smart 	}
43207ad20aa9SJames Smart 
43217ad20aa9SJames Smart 	pbuf = (uint8_t *)dmabuf->virt;
43227ad20aa9SJames Smart 	size = job->request_payload.payload_len;
43237ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
43247ad20aa9SJames Smart 			  job->request_payload.sg_cnt,
43257ad20aa9SJames Smart 			  pbuf, size);
43267ad20aa9SJames Smart 
43277ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
43287ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43297ad20aa9SJames Smart 				"2966 SLI_CONFIG (mse) ext-buffer wr set "
43307ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
43317ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
43327ad20aa9SJames Smart 
43337ad20aa9SJames Smart 	} else {
43347ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43357ad20aa9SJames Smart 				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
43367ad20aa9SJames Smart 				"buffer[%d], size:%d\n",
43377ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.seqNum, size);
43387ad20aa9SJames Smart 
43397ad20aa9SJames Smart 	}
43407ad20aa9SJames Smart 
43417ad20aa9SJames Smart 	/* set up external buffer descriptor and add to external buffer list */
43427ad20aa9SJames Smart 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
43437ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.mbx_dmabuf,
43447ad20aa9SJames Smart 					dmabuf);
43457ad20aa9SJames Smart 	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
43467ad20aa9SJames Smart 
4347b76f2dc9SJames Smart 	/* after write dma buffer */
4348b76f2dc9SJames Smart 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4349b76f2dc9SJames Smart 					mbox_wr, dma_ebuf, sta_pos_addr,
4350b76f2dc9SJames Smart 					dmabuf, index);
4351b76f2dc9SJames Smart 
43527ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
43537ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43547ad20aa9SJames Smart 				"2968 SLI_CONFIG ext-buffer wr all %d "
43557ad20aa9SJames Smart 				"ebuffers received\n",
43567ad20aa9SJames Smart 				phba->mbox_ext_buf_ctx.numBuf);
43577ad20aa9SJames Smart 		/* mailbox command structure for base driver */
43587ad20aa9SJames Smart 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
43597ad20aa9SJames Smart 		if (!pmboxq) {
43607ad20aa9SJames Smart 			rc = -ENOMEM;
43617ad20aa9SJames Smart 			goto job_error;
43627ad20aa9SJames Smart 		}
43637ad20aa9SJames Smart 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
43647ad20aa9SJames Smart 		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
43657ad20aa9SJames Smart 		pmb = &pmboxq->u.mb;
43667ad20aa9SJames Smart 		memcpy(pmb, pbuf, sizeof(*pmb));
43677ad20aa9SJames Smart 		pmb->mbxOwner = OWN_HOST;
43687ad20aa9SJames Smart 		pmboxq->vport = phba->pport;
43697ad20aa9SJames Smart 
43707ad20aa9SJames Smart 		/* callback for multi-buffer write mailbox command */
43717ad20aa9SJames Smart 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
43727ad20aa9SJames Smart 
43737ad20aa9SJames Smart 		/* context fields to callback function */
43747ad20aa9SJames Smart 		pmboxq->context1 = dd_data;
43757ad20aa9SJames Smart 		dd_data->type = TYPE_MBOX;
4376a33c4f7bSJames Smart 		dd_data->set_job = job;
43777ad20aa9SJames Smart 		dd_data->context_un.mbox.pmboxq = pmboxq;
43787ad20aa9SJames Smart 		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
43797ad20aa9SJames Smart 		job->dd_data = dd_data;
43807ad20aa9SJames Smart 
43817ad20aa9SJames Smart 		/* state change */
43827ad20aa9SJames Smart 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
43837ad20aa9SJames Smart 
43847ad20aa9SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
43857ad20aa9SJames Smart 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
43867ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
43877ad20aa9SJames Smart 					"2969 Issued SLI_CONFIG ext-buffer "
43887ad20aa9SJames Smart 					"maibox command, rc:x%x\n", rc);
438988a2cfbbSJames Smart 			return SLI_CONFIG_HANDLED;
43907ad20aa9SJames Smart 		}
43917ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
43927ad20aa9SJames Smart 				"2970 Failed to issue SLI_CONFIG ext-buffer "
43937ad20aa9SJames Smart 				"maibox command, rc:x%x\n", rc);
43947ad20aa9SJames Smart 		rc = -EPIPE;
43957ad20aa9SJames Smart 		goto job_error;
43967ad20aa9SJames Smart 	}
43977ad20aa9SJames Smart 
43987ad20aa9SJames Smart 	/* wait for additoinal external buffers */
43997ad20aa9SJames Smart 	job->reply->result = 0;
44007ad20aa9SJames Smart 	job->job_done(job);
44017ad20aa9SJames Smart 	return SLI_CONFIG_HANDLED;
44027ad20aa9SJames Smart 
44037ad20aa9SJames Smart job_error:
44047ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
44057ad20aa9SJames Smart 	kfree(dd_data);
44067ad20aa9SJames Smart 
44077ad20aa9SJames Smart 	return rc;
44087ad20aa9SJames Smart }
44097ad20aa9SJames Smart 
44107ad20aa9SJames Smart /**
44117ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
44127ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
44137ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
44147ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
44157ad20aa9SJames Smart  *
44167ad20aa9SJames Smart  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
44177ad20aa9SJames Smart  * command with multiple non-embedded external buffers.
44187ad20aa9SJames Smart  **/
44197ad20aa9SJames Smart static int
44207ad20aa9SJames Smart lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
44217ad20aa9SJames Smart 			     struct lpfc_dmabuf *dmabuf)
44227ad20aa9SJames Smart {
44237ad20aa9SJames Smart 	int rc;
44247ad20aa9SJames Smart 
44257ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44267ad20aa9SJames Smart 			"2971 SLI_CONFIG buffer (type:x%x)\n",
44277ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mboxType);
44287ad20aa9SJames Smart 
44297ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
44307ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
44317ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
44327ad20aa9SJames Smart 					"2972 SLI_CONFIG rd buffer state "
44337ad20aa9SJames Smart 					"mismatch:x%x\n",
44347ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
44357ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
44367ad20aa9SJames Smart 			return -EPIPE;
44377ad20aa9SJames Smart 		}
44387ad20aa9SJames Smart 		rc = lpfc_bsg_read_ebuf_get(phba, job);
44397ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
44407ad20aa9SJames Smart 			lpfc_bsg_dma_page_free(phba, dmabuf);
44417ad20aa9SJames Smart 	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
44427ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
44437ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
44447ad20aa9SJames Smart 					"2973 SLI_CONFIG wr buffer state "
44457ad20aa9SJames Smart 					"mismatch:x%x\n",
44467ad20aa9SJames Smart 					phba->mbox_ext_buf_ctx.state);
44477ad20aa9SJames Smart 			lpfc_bsg_mbox_ext_abort(phba);
44487ad20aa9SJames Smart 			return -EPIPE;
44497ad20aa9SJames Smart 		}
44507ad20aa9SJames Smart 		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
44517ad20aa9SJames Smart 	}
44527ad20aa9SJames Smart 	return rc;
44537ad20aa9SJames Smart }
44547ad20aa9SJames Smart 
44557ad20aa9SJames Smart /**
44567ad20aa9SJames Smart  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
44577ad20aa9SJames Smart  * @phba: Pointer to HBA context object.
44587ad20aa9SJames Smart  * @mb: Pointer to a BSG mailbox object.
44597ad20aa9SJames Smart  * @dmabuff: Pointer to a DMA buffer descriptor.
44607ad20aa9SJames Smart  *
44617ad20aa9SJames Smart  * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
44627ad20aa9SJames Smart  * (0x9B) mailbox commands and external buffers.
44637ad20aa9SJames Smart  **/
44647ad20aa9SJames Smart static int
44657ad20aa9SJames Smart lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
44667ad20aa9SJames Smart 			    struct lpfc_dmabuf *dmabuf)
44677ad20aa9SJames Smart {
44687ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
446988a2cfbbSJames Smart 	int rc = SLI_CONFIG_NOT_HANDLED;
44707ad20aa9SJames Smart 
44717ad20aa9SJames Smart 	mbox_req =
44727ad20aa9SJames Smart 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
44737ad20aa9SJames Smart 
44747ad20aa9SJames Smart 	/* mbox command with/without single external buffer */
44757ad20aa9SJames Smart 	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
447688a2cfbbSJames Smart 		return rc;
44777ad20aa9SJames Smart 
44787ad20aa9SJames Smart 	/* mbox command and first external buffer */
44797ad20aa9SJames Smart 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
44807ad20aa9SJames Smart 		if (mbox_req->extSeqNum == 1) {
44817ad20aa9SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
44827ad20aa9SJames Smart 					"2974 SLI_CONFIG mailbox: tag:%d, "
44837ad20aa9SJames Smart 					"seq:%d\n", mbox_req->extMboxTag,
44847ad20aa9SJames Smart 					mbox_req->extSeqNum);
44857ad20aa9SJames Smart 			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
44867ad20aa9SJames Smart 			return rc;
44877ad20aa9SJames Smart 		} else
44887ad20aa9SJames Smart 			goto sli_cfg_ext_error;
44897ad20aa9SJames Smart 	}
44907ad20aa9SJames Smart 
44917ad20aa9SJames Smart 	/*
44927ad20aa9SJames Smart 	 * handle additional external buffers
44937ad20aa9SJames Smart 	 */
44947ad20aa9SJames Smart 
44957ad20aa9SJames Smart 	/* check broken pipe conditions */
44967ad20aa9SJames Smart 	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
44977ad20aa9SJames Smart 		goto sli_cfg_ext_error;
44987ad20aa9SJames Smart 	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
44997ad20aa9SJames Smart 		goto sli_cfg_ext_error;
45007ad20aa9SJames Smart 	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
45017ad20aa9SJames Smart 		goto sli_cfg_ext_error;
45027ad20aa9SJames Smart 
45037ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
45047ad20aa9SJames Smart 			"2975 SLI_CONFIG mailbox external buffer: "
45057ad20aa9SJames Smart 			"extSta:x%x, tag:%d, seq:%d\n",
45067ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
45077ad20aa9SJames Smart 			mbox_req->extSeqNum);
45087ad20aa9SJames Smart 	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
45097ad20aa9SJames Smart 	return rc;
45107ad20aa9SJames Smart 
45117ad20aa9SJames Smart sli_cfg_ext_error:
45127ad20aa9SJames Smart 	/* all other cases, broken pipe */
45137ad20aa9SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
45147ad20aa9SJames Smart 			"2976 SLI_CONFIG mailbox broken pipe: "
45157ad20aa9SJames Smart 			"ctxSta:x%x, ctxNumBuf:%d "
45167ad20aa9SJames Smart 			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
45177ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state,
45187ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.numBuf,
45197ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.mbxTag,
45207ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.seqNum,
45217ad20aa9SJames Smart 			mbox_req->extMboxTag, mbox_req->extSeqNum);
45227ad20aa9SJames Smart 
45237ad20aa9SJames Smart 	lpfc_bsg_mbox_ext_session_reset(phba);
45247ad20aa9SJames Smart 
45257ad20aa9SJames Smart 	return -EPIPE;
45267ad20aa9SJames Smart }
45277ad20aa9SJames Smart 
45287ad20aa9SJames Smart /**
45293b5dd52aSJames Smart  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
45303b5dd52aSJames Smart  * @phba: Pointer to HBA context object.
45313b5dd52aSJames Smart  * @mb: Pointer to a mailbox object.
45323b5dd52aSJames Smart  * @vport: Pointer to a vport object.
45333b5dd52aSJames Smart  *
45343b5dd52aSJames Smart  * Allocate a tracking object, mailbox command memory, get a mailbox
45353b5dd52aSJames Smart  * from the mailbox pool, copy the caller mailbox command.
45363b5dd52aSJames Smart  *
45373b5dd52aSJames Smart  * If offline and the sli is active we need to poll for the command (port is
45383b5dd52aSJames Smart  * being reset) and com-plete the job, otherwise issue the mailbox command and
45393b5dd52aSJames Smart  * let our completion handler finish the command.
45403b5dd52aSJames Smart  **/
45413b5dd52aSJames Smart static uint32_t
45423b5dd52aSJames Smart lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
45433b5dd52aSJames Smart 	struct lpfc_vport *vport)
45443b5dd52aSJames Smart {
45457a470277SJames Smart 	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
45467a470277SJames Smart 	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
45477a470277SJames Smart 	/* a 4k buffer to hold the mb and extended data from/to the bsg */
45487ad20aa9SJames Smart 	uint8_t *pmbx = NULL;
45497a470277SJames Smart 	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
45507ad20aa9SJames Smart 	struct lpfc_dmabuf *dmabuf = NULL;
45517ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
4552b6e3b9c6SJames Smart 	struct READ_EVENT_LOG_VAR *rdEventLog;
4553b6e3b9c6SJames Smart 	uint32_t transmit_length, receive_length, mode;
45547ad20aa9SJames Smart 	struct lpfc_mbx_sli4_config *sli4_config;
4555b6e3b9c6SJames Smart 	struct lpfc_mbx_nembed_cmd *nembed_sge;
4556b6e3b9c6SJames Smart 	struct mbox_header *header;
4557b6e3b9c6SJames Smart 	struct ulp_bde64 *bde;
45587a470277SJames Smart 	uint8_t *ext = NULL;
45593b5dd52aSJames Smart 	int rc = 0;
45607a470277SJames Smart 	uint8_t *from;
45617ad20aa9SJames Smart 	uint32_t size;
45627ad20aa9SJames Smart 
45637a470277SJames Smart 	/* in case no data is transferred */
45647a470277SJames Smart 	job->reply->reply_payload_rcv_len = 0;
45657a470277SJames Smart 
4566b6e3b9c6SJames Smart 	/* sanity check to protect driver */
4567b6e3b9c6SJames Smart 	if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4568b6e3b9c6SJames Smart 	    job->request_payload.payload_len > BSG_MBOX_SIZE) {
4569b6e3b9c6SJames Smart 		rc = -ERANGE;
4570b6e3b9c6SJames Smart 		goto job_done;
4571b6e3b9c6SJames Smart 	}
4572b6e3b9c6SJames Smart 
45737ad20aa9SJames Smart 	/*
45747ad20aa9SJames Smart 	 * Don't allow mailbox commands to be sent when blocked or when in
45757ad20aa9SJames Smart 	 * the middle of discovery
45767ad20aa9SJames Smart 	 */
45777ad20aa9SJames Smart 	 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
45787ad20aa9SJames Smart 		rc = -EAGAIN;
45797ad20aa9SJames Smart 		goto job_done;
45807ad20aa9SJames Smart 	}
45817ad20aa9SJames Smart 
45827ad20aa9SJames Smart 	mbox_req =
45837ad20aa9SJames Smart 	    (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
45847ad20aa9SJames Smart 
45857a470277SJames Smart 	/* check if requested extended data lengths are valid */
4586b6e3b9c6SJames Smart 	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4587b6e3b9c6SJames Smart 	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
45887a470277SJames Smart 		rc = -ERANGE;
45897a470277SJames Smart 		goto job_done;
45907a470277SJames Smart 	}
45913b5dd52aSJames Smart 
45927ad20aa9SJames Smart 	dmabuf = lpfc_bsg_dma_page_alloc(phba);
45937ad20aa9SJames Smart 	if (!dmabuf || !dmabuf->virt) {
45947ad20aa9SJames Smart 		rc = -ENOMEM;
45957ad20aa9SJames Smart 		goto job_done;
45967ad20aa9SJames Smart 	}
45977ad20aa9SJames Smart 
45987ad20aa9SJames Smart 	/* Get the mailbox command or external buffer from BSG */
45997ad20aa9SJames Smart 	pmbx = (uint8_t *)dmabuf->virt;
46007ad20aa9SJames Smart 	size = job->request_payload.payload_len;
46017ad20aa9SJames Smart 	sg_copy_to_buffer(job->request_payload.sg_list,
46027ad20aa9SJames Smart 			  job->request_payload.sg_cnt, pmbx, size);
46037ad20aa9SJames Smart 
46047ad20aa9SJames Smart 	/* Handle possible SLI_CONFIG with non-embedded payloads */
46057ad20aa9SJames Smart 	if (phba->sli_rev == LPFC_SLI_REV4) {
46067ad20aa9SJames Smart 		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
46077ad20aa9SJames Smart 		if (rc == SLI_CONFIG_HANDLED)
46087ad20aa9SJames Smart 			goto job_cont;
46097ad20aa9SJames Smart 		if (rc)
46107ad20aa9SJames Smart 			goto job_done;
46117ad20aa9SJames Smart 		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
46127ad20aa9SJames Smart 	}
46137ad20aa9SJames Smart 
46147ad20aa9SJames Smart 	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
46157ad20aa9SJames Smart 	if (rc != 0)
46167ad20aa9SJames Smart 		goto job_done; /* must be negative */
46177ad20aa9SJames Smart 
46183b5dd52aSJames Smart 	/* allocate our bsg tracking structure */
46193b5dd52aSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
46203b5dd52aSJames Smart 	if (!dd_data) {
46213b5dd52aSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
46223b5dd52aSJames Smart 				"2727 Failed allocation of dd_data\n");
46237a470277SJames Smart 		rc = -ENOMEM;
46247a470277SJames Smart 		goto job_done;
46253b5dd52aSJames Smart 	}
46263b5dd52aSJames Smart 
46273b5dd52aSJames Smart 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
46283b5dd52aSJames Smart 	if (!pmboxq) {
46297a470277SJames Smart 		rc = -ENOMEM;
46307a470277SJames Smart 		goto job_done;
46313b5dd52aSJames Smart 	}
46327a470277SJames Smart 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
46333b5dd52aSJames Smart 
46343b5dd52aSJames Smart 	pmb = &pmboxq->u.mb;
46357ad20aa9SJames Smart 	memcpy(pmb, pmbx, sizeof(*pmb));
46363b5dd52aSJames Smart 	pmb->mbxOwner = OWN_HOST;
46373b5dd52aSJames Smart 	pmboxq->vport = vport;
46383b5dd52aSJames Smart 
4639c7495937SJames Smart 	/* If HBA encountered an error attention, allow only DUMP
4640c7495937SJames Smart 	 * or RESTART mailbox commands until the HBA is restarted.
4641c7495937SJames Smart 	 */
4642c7495937SJames Smart 	if (phba->pport->stopped &&
4643c7495937SJames Smart 	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
4644c7495937SJames Smart 	    pmb->mbxCommand != MBX_RESTART &&
4645c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
4646c7495937SJames Smart 	    pmb->mbxCommand != MBX_WRITE_WWN)
4647c7495937SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4648c7495937SJames Smart 				"2797 mbox: Issued mailbox cmd "
4649c7495937SJames Smart 				"0x%x while in stopped state.\n",
4650c7495937SJames Smart 				pmb->mbxCommand);
4651c7495937SJames Smart 
46527a470277SJames Smart 	/* extended mailbox commands will need an extended buffer */
4653c7495937SJames Smart 	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
46547ad20aa9SJames Smart 		from = pmbx;
46557ad20aa9SJames Smart 		ext = from + sizeof(MAILBOX_t);
46567a470277SJames Smart 		pmboxq->context2 = ext;
46577a470277SJames Smart 		pmboxq->in_ext_byte_len =
46587a470277SJames Smart 			mbox_req->inExtWLen * sizeof(uint32_t);
46597a470277SJames Smart 		pmboxq->out_ext_byte_len =
4660c7495937SJames Smart 			mbox_req->outExtWLen * sizeof(uint32_t);
46617a470277SJames Smart 		pmboxq->mbox_offset_word = mbox_req->mbOffset;
46627a470277SJames Smart 	}
46637a470277SJames Smart 
46647a470277SJames Smart 	/* biu diag will need a kernel buffer to transfer the data
46657a470277SJames Smart 	 * allocate our own buffer and setup the mailbox command to
46667a470277SJames Smart 	 * use ours
46677a470277SJames Smart 	 */
46687a470277SJames Smart 	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4669b6e3b9c6SJames Smart 		transmit_length = pmb->un.varWords[1];
4670b6e3b9c6SJames Smart 		receive_length = pmb->un.varWords[4];
4671c7495937SJames Smart 		/* transmit length cannot be greater than receive length or
4672c7495937SJames Smart 		 * mailbox extension size
4673c7495937SJames Smart 		 */
4674c7495937SJames Smart 		if ((transmit_length > receive_length) ||
467588a2cfbbSJames Smart 			(transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4676c7495937SJames Smart 			rc = -ERANGE;
4677c7495937SJames Smart 			goto job_done;
4678c7495937SJames Smart 		}
46797a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
46807ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
46817a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
46827ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
46837a470277SJames Smart 
46847a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
46857ad20aa9SJames Smart 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
46867ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
46877a470277SJames Smart 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
46887ad20aa9SJames Smart 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
46897ad20aa9SJames Smart 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4690c7495937SJames Smart 	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4691b6e3b9c6SJames Smart 		rdEventLog = &pmb->un.varRdEventLog;
4692b6e3b9c6SJames Smart 		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4693b6e3b9c6SJames Smart 		mode = bf_get(lpfc_event_log, rdEventLog);
4694c7495937SJames Smart 
4695c7495937SJames Smart 		/* receive length cannot be greater than mailbox
4696c7495937SJames Smart 		 * extension size
4697c7495937SJames Smart 		 */
469888a2cfbbSJames Smart 		if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4699c7495937SJames Smart 			rc = -ERANGE;
4700c7495937SJames Smart 			goto job_done;
4701c7495937SJames Smart 		}
4702c7495937SJames Smart 
4703c7495937SJames Smart 		/* mode zero uses a bde like biu diags command */
4704c7495937SJames Smart 		if (mode == 0) {
47057ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
47067ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
47077ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
47087ad20aa9SJames Smart 							+ sizeof(MAILBOX_t));
4709c7495937SJames Smart 		}
4710c7495937SJames Smart 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
47113ef6d24cSJames Smart 		/* Let type 4 (well known data) through because the data is
47123ef6d24cSJames Smart 		 * returned in varwords[4-8]
47133ef6d24cSJames Smart 		 * otherwise check the recieve length and fetch the buffer addr
47143ef6d24cSJames Smart 		 */
47153ef6d24cSJames Smart 		if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
47163ef6d24cSJames Smart 			(pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
4717c7495937SJames Smart 			/* rebuild the command for sli4 using our own buffers
4718c7495937SJames Smart 			* like we do for biu diags
4719c7495937SJames Smart 			*/
4720b6e3b9c6SJames Smart 			receive_length = pmb->un.varWords[2];
4721c7495937SJames Smart 			/* receive length cannot be greater than mailbox
4722c7495937SJames Smart 			 * extension size
4723c7495937SJames Smart 			 */
47247ad20aa9SJames Smart 			if (receive_length == 0) {
4725c7495937SJames Smart 				rc = -ERANGE;
4726c7495937SJames Smart 				goto job_done;
4727c7495937SJames Smart 			}
47287ad20aa9SJames Smart 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
47297ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
47307ad20aa9SJames Smart 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
47317ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4732c7495937SJames Smart 		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4733c7495937SJames Smart 			pmb->un.varUpdateCfg.co) {
4734b6e3b9c6SJames Smart 			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4735c7495937SJames Smart 
4736c7495937SJames Smart 			/* bde size cannot be greater than mailbox ext size */
473788a2cfbbSJames Smart 			if (bde->tus.f.bdeSize >
473888a2cfbbSJames Smart 			    BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4739c7495937SJames Smart 				rc = -ERANGE;
4740c7495937SJames Smart 				goto job_done;
4741c7495937SJames Smart 			}
47427ad20aa9SJames Smart 			bde->addrHigh = putPaddrHigh(dmabuf->phys
47437ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
47447ad20aa9SJames Smart 			bde->addrLow = putPaddrLow(dmabuf->phys
47457ad20aa9SJames Smart 						+ sizeof(MAILBOX_t));
4746515e0aa2SJames Smart 		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
47477ad20aa9SJames Smart 			/* Handling non-embedded SLI_CONFIG mailbox command */
47487ad20aa9SJames Smart 			sli4_config = &pmboxq->u.mqe.un.sli4_config;
47497ad20aa9SJames Smart 			if (!bf_get(lpfc_mbox_hdr_emb,
47507ad20aa9SJames Smart 			    &sli4_config->header.cfg_mhdr)) {
47517ad20aa9SJames Smart 				/* rebuild the command for sli4 using our
47527ad20aa9SJames Smart 				 * own buffers like we do for biu diags
4753515e0aa2SJames Smart 				 */
47547ad20aa9SJames Smart 				header = (struct mbox_header *)
47557ad20aa9SJames Smart 						&pmb->un.varWords[0];
4756515e0aa2SJames Smart 				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4757515e0aa2SJames Smart 						&pmb->un.varWords[0];
4758515e0aa2SJames Smart 				receive_length = nembed_sge->sge[0].length;
4759515e0aa2SJames Smart 
47607ad20aa9SJames Smart 				/* receive length cannot be greater than
47617ad20aa9SJames Smart 				 * mailbox extension size
4762515e0aa2SJames Smart 				 */
4763515e0aa2SJames Smart 				if ((receive_length == 0) ||
476488a2cfbbSJames Smart 				    (receive_length >
476588a2cfbbSJames Smart 				     BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4766515e0aa2SJames Smart 					rc = -ERANGE;
4767515e0aa2SJames Smart 					goto job_done;
4768515e0aa2SJames Smart 				}
4769515e0aa2SJames Smart 
47707ad20aa9SJames Smart 				nembed_sge->sge[0].pa_hi =
47717ad20aa9SJames Smart 						putPaddrHigh(dmabuf->phys
47727ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
47737ad20aa9SJames Smart 				nembed_sge->sge[0].pa_lo =
47747ad20aa9SJames Smart 						putPaddrLow(dmabuf->phys
47757ad20aa9SJames Smart 						   + sizeof(MAILBOX_t));
4776515e0aa2SJames Smart 			}
4777c7495937SJames Smart 		}
4778c7495937SJames Smart 	}
4779c7495937SJames Smart 
47807ad20aa9SJames Smart 	dd_data->context_un.mbox.dmabuffers = dmabuf;
47813b5dd52aSJames Smart 
47823b5dd52aSJames Smart 	/* setup wake call as IOCB callback */
47837ad20aa9SJames Smart 	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
47847a470277SJames Smart 
47853b5dd52aSJames Smart 	/* setup context field to pass wait_queue pointer to wake function */
47863b5dd52aSJames Smart 	pmboxq->context1 = dd_data;
47873b5dd52aSJames Smart 	dd_data->type = TYPE_MBOX;
4788a33c4f7bSJames Smart 	dd_data->set_job = job;
47893b5dd52aSJames Smart 	dd_data->context_un.mbox.pmboxq = pmboxq;
47907ad20aa9SJames Smart 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
47917a470277SJames Smart 	dd_data->context_un.mbox.ext = ext;
47927a470277SJames Smart 	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
47937a470277SJames Smart 	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4794c7495937SJames Smart 	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
47953b5dd52aSJames Smart 	job->dd_data = dd_data;
47967a470277SJames Smart 
47977a470277SJames Smart 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
47987a470277SJames Smart 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
47997a470277SJames Smart 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
48007a470277SJames Smart 		if (rc != MBX_SUCCESS) {
48017a470277SJames Smart 			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
48027a470277SJames Smart 			goto job_done;
48033b5dd52aSJames Smart 		}
48043b5dd52aSJames Smart 
48057a470277SJames Smart 		/* job finished, copy the data */
48067ad20aa9SJames Smart 		memcpy(pmbx, pmb, sizeof(*pmb));
48077a470277SJames Smart 		job->reply->reply_payload_rcv_len =
48087a470277SJames Smart 			sg_copy_from_buffer(job->reply_payload.sg_list,
48097a470277SJames Smart 					    job->reply_payload.sg_cnt,
48107ad20aa9SJames Smart 					    pmbx, size);
48117a470277SJames Smart 		/* not waiting mbox already done */
48127a470277SJames Smart 		rc = 0;
48137a470277SJames Smart 		goto job_done;
48147a470277SJames Smart 	}
48157a470277SJames Smart 
48167a470277SJames Smart 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
48177a470277SJames Smart 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
48187a470277SJames Smart 		return 1; /* job started */
48197a470277SJames Smart 
48207a470277SJames Smart job_done:
48217a470277SJames Smart 	/* common exit for error or job completed inline */
48227a470277SJames Smart 	if (pmboxq)
48237a470277SJames Smart 		mempool_free(pmboxq, phba->mbox_mem_pool);
48247ad20aa9SJames Smart 	lpfc_bsg_dma_page_free(phba, dmabuf);
48257a470277SJames Smart 	kfree(dd_data);
48267a470277SJames Smart 
48277ad20aa9SJames Smart job_cont:
48287a470277SJames Smart 	return rc;
48293b5dd52aSJames Smart }
48303b5dd52aSJames Smart 
48313b5dd52aSJames Smart /**
48323b5dd52aSJames Smart  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
48333b5dd52aSJames Smart  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
48343b5dd52aSJames Smart  **/
48353b5dd52aSJames Smart static int
48363b5dd52aSJames Smart lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
48373b5dd52aSJames Smart {
48383b5dd52aSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
48393b5dd52aSJames Smart 	struct lpfc_hba *phba = vport->phba;
48407ad20aa9SJames Smart 	struct dfc_mbox_req *mbox_req;
48413b5dd52aSJames Smart 	int rc = 0;
48423b5dd52aSJames Smart 
48437ad20aa9SJames Smart 	/* mix-and-match backward compatibility */
48443b5dd52aSJames Smart 	job->reply->reply_payload_rcv_len = 0;
48453b5dd52aSJames Smart 	if (job->request_len <
48463b5dd52aSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
48477ad20aa9SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
48481051e9b3SMasanari Iida 				"2737 Mix-and-match backward compatibility "
48497ad20aa9SJames Smart 				"between MBOX_REQ old size:%d and "
48507ad20aa9SJames Smart 				"new request size:%d\n",
48517ad20aa9SJames Smart 				(int)(job->request_len -
48527ad20aa9SJames Smart 				      sizeof(struct fc_bsg_request)),
48537ad20aa9SJames Smart 				(int)sizeof(struct dfc_mbox_req));
48547ad20aa9SJames Smart 		mbox_req = (struct dfc_mbox_req *)
48557ad20aa9SJames Smart 				job->request->rqst_data.h_vendor.vendor_cmd;
48567ad20aa9SJames Smart 		mbox_req->extMboxTag = 0;
48577ad20aa9SJames Smart 		mbox_req->extSeqNum = 0;
48583b5dd52aSJames Smart 	}
48593b5dd52aSJames Smart 
48603b5dd52aSJames Smart 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
48613b5dd52aSJames Smart 
48623b5dd52aSJames Smart 	if (rc == 0) {
48633b5dd52aSJames Smart 		/* job done */
48643b5dd52aSJames Smart 		job->reply->result = 0;
48653b5dd52aSJames Smart 		job->dd_data = NULL;
48663b5dd52aSJames Smart 		job->job_done(job);
48673b5dd52aSJames Smart 	} else if (rc == 1)
48683b5dd52aSJames Smart 		/* job submitted, will complete later*/
48693b5dd52aSJames Smart 		rc = 0; /* return zero, no error */
48703b5dd52aSJames Smart 	else {
48713b5dd52aSJames Smart 		/* some error occurred */
48723b5dd52aSJames Smart 		job->reply->result = rc;
48733b5dd52aSJames Smart 		job->dd_data = NULL;
48743b5dd52aSJames Smart 	}
48753b5dd52aSJames Smart 
48763b5dd52aSJames Smart 	return rc;
48773b5dd52aSJames Smart }
48783b5dd52aSJames Smart 
48793b5dd52aSJames Smart /**
4880e2aed29fSJames Smart  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4881e2aed29fSJames Smart  * @phba: Pointer to HBA context object.
4882e2aed29fSJames Smart  * @cmdiocbq: Pointer to command iocb.
4883e2aed29fSJames Smart  * @rspiocbq: Pointer to response iocb.
4884e2aed29fSJames Smart  *
4885e2aed29fSJames Smart  * This function is the completion handler for iocbs issued using
4886e2aed29fSJames Smart  * lpfc_menlo_cmd function. This function is called by the
4887e2aed29fSJames Smart  * ring event handler function without any lock held. This function
4888e2aed29fSJames Smart  * can be called from both worker thread context and interrupt
4889e2aed29fSJames Smart  * context. This function also can be called from another thread which
4890e2aed29fSJames Smart  * cleans up the SLI layer objects.
4891e2aed29fSJames Smart  * This function copies the contents of the response iocb to the
4892e2aed29fSJames Smart  * response iocb memory object provided by the caller of
4893e2aed29fSJames Smart  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4894e2aed29fSJames Smart  * sleeps for the iocb completion.
4895e2aed29fSJames Smart  **/
4896e2aed29fSJames Smart static void
4897e2aed29fSJames Smart lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4898e2aed29fSJames Smart 			struct lpfc_iocbq *cmdiocbq,
4899e2aed29fSJames Smart 			struct lpfc_iocbq *rspiocbq)
4900e2aed29fSJames Smart {
4901e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
4902e2aed29fSJames Smart 	struct fc_bsg_job *job;
4903e2aed29fSJames Smart 	IOCB_t *rsp;
4904a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
4905e2aed29fSJames Smart 	struct lpfc_bsg_menlo *menlo;
4906e2aed29fSJames Smart 	unsigned long flags;
4907e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
4908a33c4f7bSJames Smart 	unsigned int rsp_size;
4909e2aed29fSJames Smart 	int rc = 0;
4910e2aed29fSJames Smart 
4911e2aed29fSJames Smart 	dd_data = cmdiocbq->context1;
4912a33c4f7bSJames Smart 	cmp = cmdiocbq->context2;
4913a33c4f7bSJames Smart 	bmp = cmdiocbq->context3;
4914e2aed29fSJames Smart 	menlo = &dd_data->context_un.menlo;
4915a33c4f7bSJames Smart 	rmp = menlo->rmp;
4916e2aed29fSJames Smart 	rsp = &rspiocbq->iocb;
4917e2aed29fSJames Smart 
4918a33c4f7bSJames Smart 	/* Determine if job has been aborted */
4919a33c4f7bSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
4920a33c4f7bSJames Smart 	job = dd_data->set_job;
4921a33c4f7bSJames Smart 	if (job) {
4922a33c4f7bSJames Smart 		/* Prevent timeout handling from trying to abort job  */
4923a33c4f7bSJames Smart 		job->dd_data = NULL;
4924a33c4f7bSJames Smart 	}
4925a33c4f7bSJames Smart 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4926e2aed29fSJames Smart 
4927a33c4f7bSJames Smart 	/* Copy the job data or set the failing status for the job */
4928a33c4f7bSJames Smart 
4929a33c4f7bSJames Smart 	if (job) {
4930e2aed29fSJames Smart 		/* always return the xri, this would be used in the case
4931a33c4f7bSJames Smart 		 * of a menlo download to allow the data to be sent as a
4932a33c4f7bSJames Smart 		 * continuation of the exchange.
4933e2aed29fSJames Smart 		 */
4934a33c4f7bSJames Smart 
4935e2aed29fSJames Smart 		menlo_resp = (struct menlo_response *)
4936e2aed29fSJames Smart 			job->reply->reply_data.vendor_reply.vendor_rsp;
4937e2aed29fSJames Smart 		menlo_resp->xri = rsp->ulpContext;
4938e2aed29fSJames Smart 		if (rsp->ulpStatus) {
4939e2aed29fSJames Smart 			if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4940e3d2b802SJames Smart 				switch (rsp->un.ulpWord[4] & IOERR_PARAM_MASK) {
4941e2aed29fSJames Smart 				case IOERR_SEQUENCE_TIMEOUT:
4942e2aed29fSJames Smart 					rc = -ETIMEDOUT;
4943e2aed29fSJames Smart 					break;
4944e2aed29fSJames Smart 				case IOERR_INVALID_RPI:
4945e2aed29fSJames Smart 					rc = -EFAULT;
4946e2aed29fSJames Smart 					break;
4947e2aed29fSJames Smart 				default:
4948e2aed29fSJames Smart 					rc = -EACCES;
4949e2aed29fSJames Smart 					break;
4950e2aed29fSJames Smart 				}
4951a33c4f7bSJames Smart 			} else {
4952e2aed29fSJames Smart 				rc = -EACCES;
4953a33c4f7bSJames Smart 			}
4954a33c4f7bSJames Smart 		} else {
4955a33c4f7bSJames Smart 			rsp_size = rsp->un.genreq64.bdl.bdeSize;
4956e2aed29fSJames Smart 			job->reply->reply_payload_rcv_len =
4957a33c4f7bSJames Smart 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
4958a33c4f7bSJames Smart 						   rsp_size, 0);
4959a33c4f7bSJames Smart 		}
4960e2aed29fSJames Smart 
4961a33c4f7bSJames Smart 	}
4962a33c4f7bSJames Smart 
4963e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
4964a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
4965a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
4966a33c4f7bSJames Smart 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4967e2aed29fSJames Smart 	kfree(bmp);
4968e2aed29fSJames Smart 	kfree(dd_data);
4969a33c4f7bSJames Smart 
4970a33c4f7bSJames Smart 	/* Complete the job if active */
4971a33c4f7bSJames Smart 
4972a33c4f7bSJames Smart 	if (job) {
4973e2aed29fSJames Smart 		job->reply->result = rc;
4974e2aed29fSJames Smart 		job->job_done(job);
4975a33c4f7bSJames Smart 	}
4976a33c4f7bSJames Smart 
4977e2aed29fSJames Smart 	return;
4978e2aed29fSJames Smart }
4979e2aed29fSJames Smart 
4980e2aed29fSJames Smart /**
4981e2aed29fSJames Smart  * lpfc_menlo_cmd - send an ioctl for menlo hardware
4982e2aed29fSJames Smart  * @job: fc_bsg_job to handle
4983e2aed29fSJames Smart  *
4984e2aed29fSJames Smart  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4985e2aed29fSJames Smart  * all the command completions will return the xri for the command.
4986e2aed29fSJames Smart  * For menlo data requests a gen request 64 CX is used to continue the exchange
4987e2aed29fSJames Smart  * supplied in the menlo request header xri field.
4988e2aed29fSJames Smart  **/
4989e2aed29fSJames Smart static int
4990e2aed29fSJames Smart lpfc_menlo_cmd(struct fc_bsg_job *job)
4991e2aed29fSJames Smart {
4992e2aed29fSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4993e2aed29fSJames Smart 	struct lpfc_hba *phba = vport->phba;
4994a33c4f7bSJames Smart 	struct lpfc_iocbq *cmdiocbq;
4995a33c4f7bSJames Smart 	IOCB_t *cmd;
4996e2aed29fSJames Smart 	int rc = 0;
4997e2aed29fSJames Smart 	struct menlo_command *menlo_cmd;
4998e2aed29fSJames Smart 	struct menlo_response *menlo_resp;
4999a33c4f7bSJames Smart 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
5000e2aed29fSJames Smart 	int request_nseg;
5001e2aed29fSJames Smart 	int reply_nseg;
5002e2aed29fSJames Smart 	struct bsg_job_data *dd_data;
5003e2aed29fSJames Smart 	struct ulp_bde64 *bpl = NULL;
5004e2aed29fSJames Smart 
5005e2aed29fSJames Smart 	/* in case no data is returned return just the return code */
5006e2aed29fSJames Smart 	job->reply->reply_payload_rcv_len = 0;
5007e2aed29fSJames Smart 
5008e2aed29fSJames Smart 	if (job->request_len <
5009e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) +
5010e2aed29fSJames Smart 		sizeof(struct menlo_command)) {
5011e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5012e2aed29fSJames Smart 				"2784 Received MENLO_CMD request below "
5013e2aed29fSJames Smart 				"minimum size\n");
5014e2aed29fSJames Smart 		rc = -ERANGE;
5015e2aed29fSJames Smart 		goto no_dd_data;
5016e2aed29fSJames Smart 	}
5017e2aed29fSJames Smart 
5018e2aed29fSJames Smart 	if (job->reply_len <
5019e2aed29fSJames Smart 	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
5020e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5021e2aed29fSJames Smart 				"2785 Received MENLO_CMD reply below "
5022e2aed29fSJames Smart 				"minimum size\n");
5023e2aed29fSJames Smart 		rc = -ERANGE;
5024e2aed29fSJames Smart 		goto no_dd_data;
5025e2aed29fSJames Smart 	}
5026e2aed29fSJames Smart 
5027e2aed29fSJames Smart 	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
5028e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5029e2aed29fSJames Smart 				"2786 Adapter does not support menlo "
5030e2aed29fSJames Smart 				"commands\n");
5031e2aed29fSJames Smart 		rc = -EPERM;
5032e2aed29fSJames Smart 		goto no_dd_data;
5033e2aed29fSJames Smart 	}
5034e2aed29fSJames Smart 
5035e2aed29fSJames Smart 	menlo_cmd = (struct menlo_command *)
5036e2aed29fSJames Smart 		job->request->rqst_data.h_vendor.vendor_cmd;
5037e2aed29fSJames Smart 
5038e2aed29fSJames Smart 	menlo_resp = (struct menlo_response *)
5039e2aed29fSJames Smart 		job->reply->reply_data.vendor_reply.vendor_rsp;
5040e2aed29fSJames Smart 
5041e2aed29fSJames Smart 	/* allocate our bsg tracking structure */
5042e2aed29fSJames Smart 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
5043e2aed29fSJames Smart 	if (!dd_data) {
5044e2aed29fSJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5045e2aed29fSJames Smart 				"2787 Failed allocation of dd_data\n");
5046e2aed29fSJames Smart 		rc = -ENOMEM;
5047e2aed29fSJames Smart 		goto no_dd_data;
5048e2aed29fSJames Smart 	}
5049e2aed29fSJames Smart 
5050e2aed29fSJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5051e2aed29fSJames Smart 	if (!bmp) {
5052e2aed29fSJames Smart 		rc = -ENOMEM;
5053e2aed29fSJames Smart 		goto free_dd;
5054e2aed29fSJames Smart 	}
5055e2aed29fSJames Smart 
5056a33c4f7bSJames Smart 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
5057a33c4f7bSJames Smart 	if (!bmp->virt) {
5058e2aed29fSJames Smart 		rc = -ENOMEM;
5059e2aed29fSJames Smart 		goto free_bmp;
5060e2aed29fSJames Smart 	}
5061e2aed29fSJames Smart 
5062e2aed29fSJames Smart 	INIT_LIST_HEAD(&bmp->list);
5063a33c4f7bSJames Smart 
5064e2aed29fSJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
5065a33c4f7bSJames Smart 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
5066a33c4f7bSJames Smart 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
5067a33c4f7bSJames Smart 				     1, bpl, &request_nseg);
5068a33c4f7bSJames Smart 	if (!cmp) {
5069a33c4f7bSJames Smart 		rc = -ENOMEM;
5070a33c4f7bSJames Smart 		goto free_bmp;
5071a33c4f7bSJames Smart 	}
5072a33c4f7bSJames Smart 	lpfc_bsg_copy_data(cmp, &job->request_payload,
5073a33c4f7bSJames Smart 			   job->request_payload.payload_len, 1);
5074a33c4f7bSJames Smart 
5075a33c4f7bSJames Smart 	bpl += request_nseg;
5076a33c4f7bSJames Smart 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
5077a33c4f7bSJames Smart 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
5078a33c4f7bSJames Smart 				     bpl, &reply_nseg);
5079a33c4f7bSJames Smart 	if (!rmp) {
5080a33c4f7bSJames Smart 		rc = -ENOMEM;
5081a33c4f7bSJames Smart 		goto free_cmp;
5082e2aed29fSJames Smart 	}
5083e2aed29fSJames Smart 
5084a33c4f7bSJames Smart 	cmdiocbq = lpfc_sli_get_iocbq(phba);
5085a33c4f7bSJames Smart 	if (!cmdiocbq) {
5086a33c4f7bSJames Smart 		rc = -ENOMEM;
5087a33c4f7bSJames Smart 		goto free_rmp;
5088e2aed29fSJames Smart 	}
5089e2aed29fSJames Smart 
5090e2aed29fSJames Smart 	cmd = &cmdiocbq->iocb;
5091e2aed29fSJames Smart 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
5092e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
5093e2aed29fSJames Smart 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
5094e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
5095e2aed29fSJames Smart 	cmd->un.genreq64.bdl.bdeSize =
5096e2aed29fSJames Smart 	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
5097e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
5098e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
5099e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
5100e2aed29fSJames Smart 	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
5101e2aed29fSJames Smart 	cmd->ulpBdeCount = 1;
5102e2aed29fSJames Smart 	cmd->ulpClass = CLASS3;
5103e2aed29fSJames Smart 	cmd->ulpOwner = OWN_CHIP;
5104e2aed29fSJames Smart 	cmd->ulpLe = 1; /* Limited Edition */
5105e2aed29fSJames Smart 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
5106e2aed29fSJames Smart 	cmdiocbq->vport = phba->pport;
5107e2aed29fSJames Smart 	/* We want the firmware to timeout before we do */
5108e2aed29fSJames Smart 	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
5109e2aed29fSJames Smart 	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
5110e2aed29fSJames Smart 	cmdiocbq->context1 = dd_data;
5111a33c4f7bSJames Smart 	cmdiocbq->context2 = cmp;
5112a33c4f7bSJames Smart 	cmdiocbq->context3 = bmp;
5113e2aed29fSJames Smart 	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
5114e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
5115e2aed29fSJames Smart 		cmd->ulpPU = MENLO_PU; /* 3 */
5116e2aed29fSJames Smart 		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
5117e2aed29fSJames Smart 		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
5118e2aed29fSJames Smart 	} else {
5119e2aed29fSJames Smart 		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
5120e2aed29fSJames Smart 		cmd->ulpPU = 1;
5121e2aed29fSJames Smart 		cmd->un.ulpWord[4] = 0;
5122e2aed29fSJames Smart 		cmd->ulpContext = menlo_cmd->xri;
5123e2aed29fSJames Smart 	}
5124e2aed29fSJames Smart 
5125e2aed29fSJames Smart 	dd_data->type = TYPE_MENLO;
5126a33c4f7bSJames Smart 	dd_data->set_job = job;
5127e2aed29fSJames Smart 	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
5128a33c4f7bSJames Smart 	dd_data->context_un.menlo.rmp = rmp;
5129a33c4f7bSJames Smart 	job->dd_data = dd_data;
5130e2aed29fSJames Smart 
5131e2aed29fSJames Smart 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
5132e2aed29fSJames Smart 		MENLO_TIMEOUT - 5);
5133e2aed29fSJames Smart 	if (rc == IOCB_SUCCESS)
5134e2aed29fSJames Smart 		return 0; /* done for now */
5135e2aed29fSJames Smart 
5136e2aed29fSJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocbq);
5137a33c4f7bSJames Smart 
5138a33c4f7bSJames Smart free_rmp:
5139a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, rmp);
5140a33c4f7bSJames Smart free_cmp:
5141a33c4f7bSJames Smart 	lpfc_free_bsg_buffers(phba, cmp);
5142e2aed29fSJames Smart free_bmp:
5143a33c4f7bSJames Smart 	if (bmp->virt)
5144a33c4f7bSJames Smart 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
5145e2aed29fSJames Smart 	kfree(bmp);
5146e2aed29fSJames Smart free_dd:
5147e2aed29fSJames Smart 	kfree(dd_data);
5148e2aed29fSJames Smart no_dd_data:
5149e2aed29fSJames Smart 	/* make error code available to userspace */
5150e2aed29fSJames Smart 	job->reply->result = rc;
5151e2aed29fSJames Smart 	job->dd_data = NULL;
5152e2aed29fSJames Smart 	return rc;
5153e2aed29fSJames Smart }
5154b6e3b9c6SJames Smart 
5155e2aed29fSJames Smart /**
5156f1c3b0fcSJames Smart  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5157f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
51583b5dd52aSJames Smart  **/
5159f1c3b0fcSJames Smart static int
5160f1c3b0fcSJames Smart lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
5161f1c3b0fcSJames Smart {
5162f1c3b0fcSJames Smart 	int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
51634cc0e56eSJames Smart 	int rc;
5164f1c3b0fcSJames Smart 
5165f1c3b0fcSJames Smart 	switch (command) {
5166f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
51674cc0e56eSJames Smart 		rc = lpfc_bsg_hba_set_event(job);
5168f1c3b0fcSJames Smart 		break;
5169f1c3b0fcSJames Smart 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
51704cc0e56eSJames Smart 		rc = lpfc_bsg_hba_get_event(job);
5171f1c3b0fcSJames Smart 		break;
51723b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
51733b5dd52aSJames Smart 		rc = lpfc_bsg_send_mgmt_rsp(job);
51743b5dd52aSJames Smart 		break;
51753b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE:
51767ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_mode(job);
51773b5dd52aSJames Smart 		break;
51787ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_MODE_END:
51797ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_diag_mode_end(job);
51807ad20aa9SJames Smart 		break;
51817ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
51827ad20aa9SJames Smart 		rc = lpfc_bsg_diag_loopback_run(job);
51837ad20aa9SJames Smart 		break;
51847ad20aa9SJames Smart 	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
51857ad20aa9SJames Smart 		rc = lpfc_sli4_bsg_link_diag_test(job);
51863b5dd52aSJames Smart 		break;
51873b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
51883b5dd52aSJames Smart 		rc = lpfc_bsg_get_dfc_rev(job);
51893b5dd52aSJames Smart 		break;
51903b5dd52aSJames Smart 	case LPFC_BSG_VENDOR_MBOX:
51913b5dd52aSJames Smart 		rc = lpfc_bsg_mbox_cmd(job);
51923b5dd52aSJames Smart 		break;
5193e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_CMD:
5194e2aed29fSJames Smart 	case LPFC_BSG_VENDOR_MENLO_DATA:
5195e2aed29fSJames Smart 		rc = lpfc_menlo_cmd(job);
5196e2aed29fSJames Smart 		break;
5197f1c3b0fcSJames Smart 	default:
51984cc0e56eSJames Smart 		rc = -EINVAL;
51994cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len = 0;
52004cc0e56eSJames Smart 		/* make error code available to userspace */
52014cc0e56eSJames Smart 		job->reply->result = rc;
52024cc0e56eSJames Smart 		break;
5203f1c3b0fcSJames Smart 	}
52044cc0e56eSJames Smart 
52054cc0e56eSJames Smart 	return rc;
5206f1c3b0fcSJames Smart }
5207f1c3b0fcSJames Smart 
5208f1c3b0fcSJames Smart /**
5209f1c3b0fcSJames Smart  * lpfc_bsg_request - handle a bsg request from the FC transport
5210f1c3b0fcSJames Smart  * @job: fc_bsg_job to handle
52113b5dd52aSJames Smart  **/
5212f1c3b0fcSJames Smart int
5213f1c3b0fcSJames Smart lpfc_bsg_request(struct fc_bsg_job *job)
5214f1c3b0fcSJames Smart {
5215f1c3b0fcSJames Smart 	uint32_t msgcode;
52164cc0e56eSJames Smart 	int rc;
5217f1c3b0fcSJames Smart 
5218f1c3b0fcSJames Smart 	msgcode = job->request->msgcode;
5219f1c3b0fcSJames Smart 	switch (msgcode) {
5220f1c3b0fcSJames Smart 	case FC_BSG_HST_VENDOR:
5221f1c3b0fcSJames Smart 		rc = lpfc_bsg_hst_vendor(job);
5222f1c3b0fcSJames Smart 		break;
5223f1c3b0fcSJames Smart 	case FC_BSG_RPT_ELS:
5224f1c3b0fcSJames Smart 		rc = lpfc_bsg_rport_els(job);
5225f1c3b0fcSJames Smart 		break;
5226f1c3b0fcSJames Smart 	case FC_BSG_RPT_CT:
52274cc0e56eSJames Smart 		rc = lpfc_bsg_send_mgmt_cmd(job);
5228f1c3b0fcSJames Smart 		break;
5229f1c3b0fcSJames Smart 	default:
52304cc0e56eSJames Smart 		rc = -EINVAL;
52314cc0e56eSJames Smart 		job->reply->reply_payload_rcv_len = 0;
52324cc0e56eSJames Smart 		/* make error code available to userspace */
52334cc0e56eSJames Smart 		job->reply->result = rc;
5234f1c3b0fcSJames Smart 		break;
5235f1c3b0fcSJames Smart 	}
5236f1c3b0fcSJames Smart 
5237f1c3b0fcSJames Smart 	return rc;
5238f1c3b0fcSJames Smart }
5239f1c3b0fcSJames Smart 
5240f1c3b0fcSJames Smart /**
5241f1c3b0fcSJames Smart  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5242f1c3b0fcSJames Smart  * @job: fc_bsg_job that has timed out
5243f1c3b0fcSJames Smart  *
5244f1c3b0fcSJames Smart  * This function just aborts the job's IOCB.  The aborted IOCB will return to
5245f1c3b0fcSJames Smart  * the waiting function which will handle passing the error back to userspace
52463b5dd52aSJames Smart  **/
5247f1c3b0fcSJames Smart int
5248f1c3b0fcSJames Smart lpfc_bsg_timeout(struct fc_bsg_job *job)
5249f1c3b0fcSJames Smart {
5250f1c3b0fcSJames Smart 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
5251f1c3b0fcSJames Smart 	struct lpfc_hba *phba = vport->phba;
52524cc0e56eSJames Smart 	struct lpfc_iocbq *cmdiocb;
5253f1c3b0fcSJames Smart 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
52544cc0e56eSJames Smart 	struct bsg_job_data *dd_data;
52554cc0e56eSJames Smart 	unsigned long flags;
5256a33c4f7bSJames Smart 	int rc = 0;
5257a33c4f7bSJames Smart 	LIST_HEAD(completions);
5258a33c4f7bSJames Smart 	struct lpfc_iocbq *check_iocb, *next_iocb;
5259a33c4f7bSJames Smart 
5260a33c4f7bSJames Smart 	/* if job's driver data is NULL, the command completed or is in the
5261a33c4f7bSJames Smart 	 * the process of completing.  In this case, return status to request
5262a33c4f7bSJames Smart 	 * so the timeout is retried.  This avoids double completion issues
5263a33c4f7bSJames Smart 	 * and the request will be pulled off the timer queue when the
5264a33c4f7bSJames Smart 	 * command's completion handler executes.  Otherwise, prevent the
5265a33c4f7bSJames Smart 	 * command's completion handler from executing the job done callback
5266a33c4f7bSJames Smart 	 * and continue processing to abort the outstanding the command.
5267a33c4f7bSJames Smart 	 */
5268f1c3b0fcSJames Smart 
52694cc0e56eSJames Smart 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
52704cc0e56eSJames Smart 	dd_data = (struct bsg_job_data *)job->dd_data;
5271a33c4f7bSJames Smart 	if (dd_data) {
5272a33c4f7bSJames Smart 		dd_data->set_job = NULL;
5273a33c4f7bSJames Smart 		job->dd_data = NULL;
5274a33c4f7bSJames Smart 	} else {
52754cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5276a33c4f7bSJames Smart 		return -EAGAIN;
52774cc0e56eSJames Smart 	}
52784cc0e56eSJames Smart 
52794cc0e56eSJames Smart 	switch (dd_data->type) {
52804cc0e56eSJames Smart 	case TYPE_IOCB:
5281a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5282a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5283a33c4f7bSJames Smart 		 * Otherwise, call abort iotag
5284a33c4f7bSJames Smart 		 */
5285a33c4f7bSJames Smart 
5286a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.iocb.cmdiocbq;
52874cc0e56eSJames Smart 		spin_lock_irq(&phba->hbalock);
5288a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5289a33c4f7bSJames Smart 					 list) {
5290a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5291a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5292a33c4f7bSJames Smart 				break;
5293a33c4f7bSJames Smart 			}
5294a33c4f7bSJames Smart 		}
5295a33c4f7bSJames Smart 		if (list_empty(&completions))
5296f1c3b0fcSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
52974cc0e56eSJames Smart 		spin_unlock_irq(&phba->hbalock);
5298a33c4f7bSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5299a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5300a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5301a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5302a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5303a33c4f7bSJames Smart 		}
53044cc0e56eSJames Smart 		break;
5305a33c4f7bSJames Smart 
53064cc0e56eSJames Smart 	case TYPE_EVT:
53074cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
53084cc0e56eSJames Smart 		break;
5309a33c4f7bSJames Smart 
53103b5dd52aSJames Smart 	case TYPE_MBOX:
5311a33c4f7bSJames Smart 		/* Update the ext buf ctx state if needed */
5312a33c4f7bSJames Smart 
53137ad20aa9SJames Smart 		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
53147ad20aa9SJames Smart 			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5315a33c4f7bSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
53163b5dd52aSJames Smart 		break;
5317e2aed29fSJames Smart 	case TYPE_MENLO:
5318a33c4f7bSJames Smart 		/* Check to see if IOCB was issued to the port or not. If not,
5319a33c4f7bSJames Smart 		 * remove it from the txq queue and call cancel iocbs.
5320a33c4f7bSJames Smart 		 * Otherwise, call abort iotag.
5321a33c4f7bSJames Smart 		 */
5322a33c4f7bSJames Smart 
5323a33c4f7bSJames Smart 		cmdiocb = dd_data->context_un.menlo.cmdiocbq;
5324e2aed29fSJames Smart 		spin_lock_irq(&phba->hbalock);
5325a33c4f7bSJames Smart 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5326a33c4f7bSJames Smart 					 list) {
5327a33c4f7bSJames Smart 			if (check_iocb == cmdiocb) {
5328a33c4f7bSJames Smart 				list_move_tail(&check_iocb->list, &completions);
5329a33c4f7bSJames Smart 				break;
5330a33c4f7bSJames Smart 			}
5331a33c4f7bSJames Smart 		}
5332a33c4f7bSJames Smart 		if (list_empty(&completions))
5333e2aed29fSJames Smart 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
5334e2aed29fSJames Smart 		spin_unlock_irq(&phba->hbalock);
5335a33c4f7bSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5336a33c4f7bSJames Smart 		if (!list_empty(&completions)) {
5337a33c4f7bSJames Smart 			lpfc_sli_cancel_iocbs(phba, &completions,
5338a33c4f7bSJames Smart 					      IOSTAT_LOCAL_REJECT,
5339a33c4f7bSJames Smart 					      IOERR_SLI_ABORTED);
5340a33c4f7bSJames Smart 		}
5341e2aed29fSJames Smart 		break;
53424cc0e56eSJames Smart 	default:
53434cc0e56eSJames Smart 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
53444cc0e56eSJames Smart 		break;
53454cc0e56eSJames Smart 	}
5346f1c3b0fcSJames Smart 
53474cc0e56eSJames Smart 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
53484cc0e56eSJames Smart 	 * otherwise an error message will be displayed on the console
53494cc0e56eSJames Smart 	 * so always return success (zero)
53504cc0e56eSJames Smart 	 */
5351a33c4f7bSJames Smart 	return rc;
5352f1c3b0fcSJames Smart }
5353