xref: /linux/drivers/scsi/lpfc/lpfc_bsg.c (revision 970f69b6bf71562df5afcefb89c77b4e971d68de)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2024 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2009-2015 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  *                                                                 *
10  * This program is free software; you can redistribute it and/or   *
11  * modify it under the terms of version 2 of the GNU General       *
12  * Public License as published by the Free Software Foundation.    *
13  * This program is distributed in the hope that it will be useful. *
14  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
15  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
16  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
17  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
18  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
19  * more details, a copy of which can be found in the file COPYING  *
20  * included with this package.                                     *
21  *******************************************************************/
22 
23 #include <linux/interrupt.h>
24 #include <linux/mempool.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/list.h>
29 #include <linux/bsg-lib.h>
30 #include <linux/vmalloc.h>
31 
32 #include <scsi/scsi.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport_fc.h>
35 #include <scsi/scsi_bsg_fc.h>
36 #include <scsi/fc/fc_fs.h>
37 
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_bsg.h"
44 #include "lpfc_disc.h"
45 #include "lpfc_scsi.h"
46 #include "lpfc.h"
47 #include "lpfc_logmsg.h"
48 #include "lpfc_crtn.h"
49 #include "lpfc_debugfs.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_version.h"
52 
53 struct lpfc_bsg_event {
54 	struct list_head node;
55 	struct kref kref;
56 	wait_queue_head_t wq;
57 
58 	/* Event type and waiter identifiers */
59 	uint32_t type_mask;
60 	uint32_t req_id;
61 	uint32_t reg_id;
62 
63 	/* next two flags are here for the auto-delete logic */
64 	unsigned long wait_time_stamp;
65 	int waiting;
66 
67 	/* seen and not seen events */
68 	struct list_head events_to_get;
69 	struct list_head events_to_see;
70 
71 	/* driver data associated with the job */
72 	void *dd_data;
73 };
74 
75 struct lpfc_bsg_iocb {
76 	struct lpfc_iocbq *cmdiocbq;
77 	struct lpfc_dmabuf *rmp;
78 	struct lpfc_nodelist *ndlp;
79 };
80 
81 struct lpfc_bsg_mbox {
82 	LPFC_MBOXQ_t *pmboxq;
83 	MAILBOX_t *mb;
84 	struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
85 	uint8_t *ext; /* extended mailbox data */
86 	uint32_t mbOffset; /* from app */
87 	uint32_t inExtWLen; /* from app */
88 	uint32_t outExtWLen; /* from app */
89 };
90 
91 #define TYPE_EVT 	1
92 #define TYPE_IOCB	2
93 #define TYPE_MBOX	3
94 struct bsg_job_data {
95 	uint32_t type;
96 	struct bsg_job *set_job; /* job waiting for this iocb to finish */
97 	union {
98 		struct lpfc_bsg_event *evt;
99 		struct lpfc_bsg_iocb iocb;
100 		struct lpfc_bsg_mbox mbox;
101 	} context_un;
102 };
103 
104 struct event_data {
105 	struct list_head node;
106 	uint32_t type;
107 	uint32_t immed_dat;
108 	void *data;
109 	uint32_t len;
110 };
111 
112 #define BUF_SZ_4K 4096
113 #define SLI_CT_ELX_LOOPBACK 0x10
114 
115 enum ELX_LOOPBACK_CMD {
116 	ELX_LOOPBACK_XRI_SETUP,
117 	ELX_LOOPBACK_DATA,
118 };
119 
120 #define ELX_LOOPBACK_HEADER_SZ \
121 	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
122 
123 /* For non-embedded read object command */
124 #define READ_OBJ_EMB0_SCHEME_0 {1, 10, 256, 128}
125 #define READ_OBJ_EMB0_SCHEME_1 {11, LPFC_EMB0_MAX_RD_OBJ_HBD_CNT, 512, 192}
126 static const struct lpfc_read_object_cmd_scheme {
127 	u32 min_hbd_cnt;
128 	u32 max_hbd_cnt;
129 	u32 cmd_size;
130 	u32 payload_word_offset;
131 }  rd_obj_scheme[2] = {READ_OBJ_EMB0_SCHEME_0, READ_OBJ_EMB0_SCHEME_1};
132 
133 struct lpfc_dmabufext {
134 	struct lpfc_dmabuf dma;
135 	uint32_t size;
136 	uint32_t flag;
137 };
138 
139 static void
140 lpfc_free_bsg_buffers(struct lpfc_hba *phba, struct lpfc_dmabuf *mlist)
141 {
142 	struct lpfc_dmabuf *mlast, *next_mlast;
143 
144 	if (mlist) {
145 		list_for_each_entry_safe(mlast, next_mlast, &mlist->list,
146 					 list) {
147 			list_del(&mlast->list);
148 			lpfc_mbuf_free(phba, mlast->virt, mlast->phys);
149 			kfree(mlast);
150 		}
151 		lpfc_mbuf_free(phba, mlist->virt, mlist->phys);
152 		kfree(mlist);
153 	}
154 	return;
155 }
156 
157 static struct lpfc_dmabuf *
158 lpfc_alloc_bsg_buffers(struct lpfc_hba *phba, unsigned int size,
159 		       int outbound_buffers, struct ulp_bde64 *bpl,
160 		       int *bpl_entries)
161 {
162 	struct lpfc_dmabuf *mlist = NULL;
163 	struct lpfc_dmabuf *mp;
164 	unsigned int bytes_left = size;
165 
166 	/* Verify we can support the size specified */
167 	if (!size || (size > (*bpl_entries * LPFC_BPL_SIZE)))
168 		return NULL;
169 
170 	/* Determine the number of dma buffers to allocate */
171 	*bpl_entries = (size % LPFC_BPL_SIZE ? size/LPFC_BPL_SIZE + 1 :
172 			size/LPFC_BPL_SIZE);
173 
174 	/* Allocate dma buffer and place in BPL passed */
175 	while (bytes_left) {
176 		/* Allocate dma buffer  */
177 		mp = kmalloc_obj(struct lpfc_dmabuf);
178 		if (!mp) {
179 			if (mlist)
180 				lpfc_free_bsg_buffers(phba, mlist);
181 			return NULL;
182 		}
183 
184 		INIT_LIST_HEAD(&mp->list);
185 		mp->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &(mp->phys));
186 
187 		if (!mp->virt) {
188 			kfree(mp);
189 			if (mlist)
190 				lpfc_free_bsg_buffers(phba, mlist);
191 			return NULL;
192 		}
193 
194 		/* Queue it to a linked list */
195 		if (!mlist)
196 			mlist = mp;
197 		else
198 			list_add_tail(&mp->list, &mlist->list);
199 
200 		/* Add buffer to buffer pointer list */
201 		if (outbound_buffers)
202 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
203 		else
204 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
205 		bpl->addrLow = le32_to_cpu(putPaddrLow(mp->phys));
206 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(mp->phys));
207 		bpl->tus.f.bdeSize = (uint16_t)
208 			(bytes_left >= LPFC_BPL_SIZE ? LPFC_BPL_SIZE :
209 			 bytes_left);
210 		bytes_left -= bpl->tus.f.bdeSize;
211 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
212 		bpl++;
213 	}
214 	return mlist;
215 }
216 
217 static unsigned int
218 lpfc_bsg_copy_data(struct lpfc_dmabuf *dma_buffers,
219 		   struct bsg_buffer *bsg_buffers,
220 		   unsigned int bytes_to_transfer, int to_buffers)
221 {
222 
223 	struct lpfc_dmabuf *mp;
224 	unsigned int transfer_bytes, bytes_copied = 0;
225 	unsigned int sg_offset, dma_offset;
226 	unsigned char *dma_address, *sg_address;
227 	LIST_HEAD(temp_list);
228 	struct sg_mapping_iter miter;
229 	unsigned long flags;
230 	unsigned int sg_flags = SG_MITER_ATOMIC;
231 	bool sg_valid;
232 
233 	list_splice_init(&dma_buffers->list, &temp_list);
234 	list_add(&dma_buffers->list, &temp_list);
235 	sg_offset = 0;
236 	if (to_buffers)
237 		sg_flags |= SG_MITER_FROM_SG;
238 	else
239 		sg_flags |= SG_MITER_TO_SG;
240 	sg_miter_start(&miter, bsg_buffers->sg_list, bsg_buffers->sg_cnt,
241 		       sg_flags);
242 	local_irq_save(flags);
243 	sg_valid = sg_miter_next(&miter);
244 	list_for_each_entry(mp, &temp_list, list) {
245 		dma_offset = 0;
246 		while (bytes_to_transfer && sg_valid &&
247 		       (dma_offset < LPFC_BPL_SIZE)) {
248 			dma_address = mp->virt + dma_offset;
249 			if (sg_offset) {
250 				/* Continue previous partial transfer of sg */
251 				sg_address = miter.addr + sg_offset;
252 				transfer_bytes = miter.length - sg_offset;
253 			} else {
254 				sg_address = miter.addr;
255 				transfer_bytes = miter.length;
256 			}
257 			if (bytes_to_transfer < transfer_bytes)
258 				transfer_bytes = bytes_to_transfer;
259 			if (transfer_bytes > (LPFC_BPL_SIZE - dma_offset))
260 				transfer_bytes = LPFC_BPL_SIZE - dma_offset;
261 			if (to_buffers)
262 				memcpy(dma_address, sg_address, transfer_bytes);
263 			else
264 				memcpy(sg_address, dma_address, transfer_bytes);
265 			dma_offset += transfer_bytes;
266 			sg_offset += transfer_bytes;
267 			bytes_to_transfer -= transfer_bytes;
268 			bytes_copied += transfer_bytes;
269 			if (sg_offset >= miter.length) {
270 				sg_offset = 0;
271 				sg_valid = sg_miter_next(&miter);
272 			}
273 		}
274 	}
275 	sg_miter_stop(&miter);
276 	local_irq_restore(flags);
277 	list_del_init(&dma_buffers->list);
278 	list_splice(&temp_list, &dma_buffers->list);
279 	return bytes_copied;
280 }
281 
282 /**
283  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
284  * @phba: Pointer to HBA context object.
285  * @cmdiocbq: Pointer to command iocb.
286  * @rspiocbq: Pointer to response iocb.
287  *
288  * This function is the completion handler for iocbs issued using
289  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
290  * ring event handler function without any lock held. This function
291  * can be called from both worker thread context and interrupt
292  * context. This function also can be called from another thread which
293  * cleans up the SLI layer objects.
294  * This function copies the contents of the response iocb to the
295  * response iocb memory object provided by the caller of
296  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
297  * sleeps for the iocb completion.
298  **/
299 static void
300 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
301 			struct lpfc_iocbq *cmdiocbq,
302 			struct lpfc_iocbq *rspiocbq)
303 {
304 	struct bsg_job_data *dd_data;
305 	struct bsg_job *job;
306 	struct fc_bsg_reply *bsg_reply;
307 	struct lpfc_dmabuf *bmp, *cmp, *rmp;
308 	struct lpfc_nodelist *ndlp;
309 	struct lpfc_bsg_iocb *iocb;
310 	unsigned long flags;
311 	int rc = 0;
312 	u32 ulp_status, ulp_word4, total_data_placed;
313 
314 	dd_data = cmdiocbq->context_un.dd_data;
315 
316 	/* Determine if job has been aborted */
317 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
318 	job = dd_data->set_job;
319 	if (job) {
320 		bsg_reply = job->reply;
321 		/* Prevent timeout handling from trying to abort job */
322 		job->dd_data = NULL;
323 	}
324 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
325 
326 	/* Close the timeout handler abort window */
327 	spin_lock_irqsave(&phba->hbalock, flags);
328 	cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
329 	spin_unlock_irqrestore(&phba->hbalock, flags);
330 
331 	iocb = &dd_data->context_un.iocb;
332 	ndlp = iocb->cmdiocbq->ndlp;
333 	rmp = iocb->rmp;
334 	cmp = cmdiocbq->cmd_dmabuf;
335 	bmp = cmdiocbq->bpl_dmabuf;
336 	ulp_status = get_job_ulpstatus(phba, rspiocbq);
337 	ulp_word4 = get_job_word4(phba, rspiocbq);
338 	total_data_placed = get_job_data_placed(phba, rspiocbq);
339 
340 	/* Copy the completed data or set the error status */
341 
342 	if (job) {
343 		if (ulp_status) {
344 			if (ulp_status == IOSTAT_LOCAL_REJECT) {
345 				switch (ulp_word4 & IOERR_PARAM_MASK) {
346 				case IOERR_SEQUENCE_TIMEOUT:
347 					rc = -ETIMEDOUT;
348 					break;
349 				case IOERR_INVALID_RPI:
350 					rc = -EFAULT;
351 					break;
352 				default:
353 					rc = -EACCES;
354 					break;
355 				}
356 			} else {
357 				rc = -EACCES;
358 			}
359 		} else {
360 			bsg_reply->reply_payload_rcv_len =
361 				lpfc_bsg_copy_data(rmp, &job->reply_payload,
362 						   total_data_placed, 0);
363 		}
364 	}
365 
366 	lpfc_free_bsg_buffers(phba, cmp);
367 	lpfc_free_bsg_buffers(phba, rmp);
368 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
369 	kfree(bmp);
370 	lpfc_nlp_put(ndlp);
371 	lpfc_sli_release_iocbq(phba, cmdiocbq);
372 	kfree(dd_data);
373 
374 	/* Complete the job if the job is still active */
375 
376 	if (job) {
377 		bsg_reply->result = rc;
378 		bsg_job_done(job, bsg_reply->result,
379 			       bsg_reply->reply_payload_rcv_len);
380 	}
381 	return;
382 }
383 
384 /**
385  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
386  * @job: fc_bsg_job to handle
387  **/
388 static int
389 lpfc_bsg_send_mgmt_cmd(struct bsg_job *job)
390 {
391 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
392 	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
393 	struct lpfc_hba *phba = vport->phba;
394 	struct lpfc_nodelist *ndlp = rdata->pnode;
395 	struct fc_bsg_reply *bsg_reply = job->reply;
396 	struct ulp_bde64 *bpl = NULL;
397 	struct lpfc_iocbq *cmdiocbq = NULL;
398 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL, *rmp = NULL;
399 	int request_nseg, reply_nseg;
400 	u32 num_entry;
401 	struct bsg_job_data *dd_data;
402 	unsigned long flags;
403 	uint32_t creg_val;
404 	int rc = 0;
405 	int iocb_stat;
406 	u16 ulp_context;
407 
408 	/* in case no data is transferred */
409 	bsg_reply->reply_payload_rcv_len = 0;
410 
411 	if (test_bit(NLP_PLOGI_SND, &ndlp->nlp_flag) ||
412 	    test_bit(NLP_PRLI_SND, &ndlp->nlp_flag) ||
413 	    test_bit(NLP_ADISC_SND, &ndlp->nlp_flag) ||
414 	    test_bit(NLP_LOGO_SND, &ndlp->nlp_flag) ||
415 	    test_bit(NLP_RNID_SND, &ndlp->nlp_flag))
416 		return -ENODEV;
417 
418 	/* allocate our bsg tracking structure */
419 	dd_data = kmalloc_obj(struct bsg_job_data);
420 	if (!dd_data) {
421 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
422 				"2733 Failed allocation of dd_data\n");
423 		rc = -ENOMEM;
424 		goto no_dd_data;
425 	}
426 
427 	cmdiocbq = lpfc_sli_get_iocbq(phba);
428 	if (!cmdiocbq) {
429 		rc = -ENOMEM;
430 		goto free_dd;
431 	}
432 
433 	bmp = kmalloc_obj(struct lpfc_dmabuf);
434 	if (!bmp) {
435 		rc = -ENOMEM;
436 		goto free_cmdiocbq;
437 	}
438 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
439 	if (!bmp->virt) {
440 		rc = -ENOMEM;
441 		goto free_bmp;
442 	}
443 
444 	INIT_LIST_HEAD(&bmp->list);
445 
446 	bpl = (struct ulp_bde64 *) bmp->virt;
447 	request_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64);
448 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
449 				     1, bpl, &request_nseg);
450 	if (!cmp) {
451 		rc = -ENOMEM;
452 		goto free_bmp;
453 	}
454 	lpfc_bsg_copy_data(cmp, &job->request_payload,
455 			   job->request_payload.payload_len, 1);
456 
457 	bpl += request_nseg;
458 	reply_nseg = LPFC_BPL_SIZE/sizeof(struct ulp_bde64) - request_nseg;
459 	rmp = lpfc_alloc_bsg_buffers(phba, job->reply_payload.payload_len, 0,
460 				     bpl, &reply_nseg);
461 	if (!rmp) {
462 		rc = -ENOMEM;
463 		goto free_cmp;
464 	}
465 
466 	num_entry = request_nseg + reply_nseg;
467 
468 	if (phba->sli_rev == LPFC_SLI_REV4)
469 		ulp_context = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
470 	else
471 		ulp_context = ndlp->nlp_rpi;
472 
473 	lpfc_sli_prep_gen_req(phba, cmdiocbq, bmp, ulp_context, num_entry,
474 			      phba->fc_ratov * 2);
475 
476 	cmdiocbq->num_bdes = num_entry;
477 	cmdiocbq->vport = phba->pport;
478 	cmdiocbq->cmd_dmabuf = cmp;
479 	cmdiocbq->bpl_dmabuf = bmp;
480 	cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
481 
482 	cmdiocbq->cmd_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
483 	cmdiocbq->context_un.dd_data = dd_data;
484 
485 	dd_data->type = TYPE_IOCB;
486 	dd_data->set_job = job;
487 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
488 	dd_data->context_un.iocb.rmp = rmp;
489 	job->dd_data = dd_data;
490 
491 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
492 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
493 			rc = -EIO ;
494 			goto free_rmp;
495 		}
496 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
497 		writel(creg_val, phba->HCregaddr);
498 		readl(phba->HCregaddr); /* flush */
499 	}
500 
501 	cmdiocbq->ndlp = lpfc_nlp_get(ndlp);
502 	if (!cmdiocbq->ndlp) {
503 		rc = -ENODEV;
504 		goto free_rmp;
505 	}
506 
507 	iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
508 	if (iocb_stat == IOCB_SUCCESS) {
509 		spin_lock_irqsave(&phba->hbalock, flags);
510 		/* make sure the I/O had not been completed yet */
511 		if (cmdiocbq->cmd_flag & LPFC_IO_LIBDFC) {
512 			/* open up abort window to timeout handler */
513 			cmdiocbq->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
514 		}
515 		spin_unlock_irqrestore(&phba->hbalock, flags);
516 		return 0; /* done for now */
517 	} else if (iocb_stat == IOCB_BUSY) {
518 		rc = -EAGAIN;
519 	} else {
520 		rc = -EIO;
521 	}
522 
523 	/* iocb failed so cleanup */
524 	lpfc_nlp_put(ndlp);
525 
526 free_rmp:
527 	lpfc_free_bsg_buffers(phba, rmp);
528 free_cmp:
529 	lpfc_free_bsg_buffers(phba, cmp);
530 free_bmp:
531 	if (bmp->virt)
532 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
533 	kfree(bmp);
534 free_cmdiocbq:
535 	lpfc_sli_release_iocbq(phba, cmdiocbq);
536 free_dd:
537 	kfree(dd_data);
538 no_dd_data:
539 	/* make error code available to userspace */
540 	bsg_reply->result = rc;
541 	job->dd_data = NULL;
542 	return rc;
543 }
544 
545 /**
546  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
547  * @phba: Pointer to HBA context object.
548  * @cmdiocbq: Pointer to command iocb.
549  * @rspiocbq: Pointer to response iocb.
550  *
551  * This function is the completion handler for iocbs issued using
552  * lpfc_bsg_rport_els_cmp function. This function is called by the
553  * ring event handler function without any lock held. This function
554  * can be called from both worker thread context and interrupt
555  * context. This function also can be called from other thread which
556  * cleans up the SLI layer objects.
557  * This function copies the contents of the response iocb to the
558  * response iocb memory object provided by the caller of
559  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
560  * sleeps for the iocb completion.
561  **/
562 static void
563 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
564 			struct lpfc_iocbq *cmdiocbq,
565 			struct lpfc_iocbq *rspiocbq)
566 {
567 	struct bsg_job_data *dd_data;
568 	struct bsg_job *job;
569 	struct fc_bsg_reply *bsg_reply;
570 	struct lpfc_nodelist *ndlp;
571 	struct lpfc_dmabuf *pcmd = NULL, *prsp = NULL;
572 	struct fc_bsg_ctels_reply *els_reply;
573 	uint8_t *rjt_data;
574 	unsigned long flags;
575 	unsigned int rsp_size;
576 	int rc = 0;
577 	u32 ulp_status, ulp_word4, total_data_placed;
578 
579 	dd_data = cmdiocbq->context_un.dd_data;
580 	ndlp = dd_data->context_un.iocb.ndlp;
581 	cmdiocbq->ndlp = ndlp;
582 
583 	/* Determine if job has been aborted */
584 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
585 	job = dd_data->set_job;
586 	if (job) {
587 		bsg_reply = job->reply;
588 		/* Prevent timeout handling from trying to abort job  */
589 		job->dd_data = NULL;
590 	}
591 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
592 
593 	/* Close the timeout handler abort window */
594 	spin_lock_irqsave(&phba->hbalock, flags);
595 	cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
596 	spin_unlock_irqrestore(&phba->hbalock, flags);
597 
598 	ulp_status = get_job_ulpstatus(phba, rspiocbq);
599 	ulp_word4 = get_job_word4(phba, rspiocbq);
600 	total_data_placed = get_job_data_placed(phba, rspiocbq);
601 	pcmd = cmdiocbq->cmd_dmabuf;
602 	prsp = (struct lpfc_dmabuf *)pcmd->list.next;
603 
604 	/* Copy the completed job data or determine the job status if job is
605 	 * still active
606 	 */
607 
608 	if (job) {
609 		if (ulp_status == IOSTAT_SUCCESS) {
610 			rsp_size = total_data_placed;
611 			bsg_reply->reply_payload_rcv_len =
612 				sg_copy_from_buffer(job->reply_payload.sg_list,
613 						    job->reply_payload.sg_cnt,
614 						    prsp->virt,
615 						    rsp_size);
616 		} else if (ulp_status == IOSTAT_LS_RJT) {
617 			bsg_reply->reply_payload_rcv_len =
618 				sizeof(struct fc_bsg_ctels_reply);
619 			/* LS_RJT data returned in word 4 */
620 			rjt_data = (uint8_t *)&ulp_word4;
621 			els_reply = &bsg_reply->reply_data.ctels_reply;
622 			els_reply->status = FC_CTELS_STATUS_REJECT;
623 			els_reply->rjt_data.action = rjt_data[3];
624 			els_reply->rjt_data.reason_code = rjt_data[2];
625 			els_reply->rjt_data.reason_explanation = rjt_data[1];
626 			els_reply->rjt_data.vendor_unique = rjt_data[0];
627 		} else if (ulp_status == IOSTAT_LOCAL_REJECT &&
628 			   (ulp_word4 & IOERR_PARAM_MASK) ==
629 			   IOERR_SEQUENCE_TIMEOUT) {
630 			rc = -ETIMEDOUT;
631 		} else {
632 			rc = -EIO;
633 		}
634 	}
635 
636 	lpfc_els_free_iocb(phba, cmdiocbq);
637 
638 	lpfc_nlp_put(ndlp);
639 	kfree(dd_data);
640 
641 	/* Complete the job if the job is still active */
642 
643 	if (job) {
644 		bsg_reply->result = rc;
645 		bsg_job_done(job, bsg_reply->result,
646 			       bsg_reply->reply_payload_rcv_len);
647 	}
648 	return;
649 }
650 
651 /**
652  * lpfc_bsg_rport_els - send an ELS command from a bsg request
653  * @job: fc_bsg_job to handle
654  **/
655 static int
656 lpfc_bsg_rport_els(struct bsg_job *job)
657 {
658 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
659 	struct lpfc_hba *phba = vport->phba;
660 	struct lpfc_rport_data *rdata = fc_bsg_to_rport(job)->dd_data;
661 	struct lpfc_nodelist *ndlp = rdata->pnode;
662 	struct fc_bsg_request *bsg_request = job->request;
663 	struct fc_bsg_reply *bsg_reply = job->reply;
664 	uint32_t elscmd;
665 	uint32_t cmdsize;
666 	struct lpfc_iocbq *cmdiocbq;
667 	uint16_t rpi = 0;
668 	struct bsg_job_data *dd_data;
669 	unsigned long flags;
670 	uint32_t creg_val;
671 	int rc = 0;
672 
673 	/* in case no data is transferred */
674 	bsg_reply->reply_payload_rcv_len = 0;
675 
676 	/* verify the els command is not greater than the
677 	 * maximum ELS transfer size.
678 	 */
679 
680 	if (job->request_payload.payload_len > FCELSSIZE) {
681 		rc = -EINVAL;
682 		goto no_dd_data;
683 	}
684 
685 	/* allocate our bsg tracking structure */
686 	dd_data = kmalloc_obj(struct bsg_job_data);
687 	if (!dd_data) {
688 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
689 				"2735 Failed allocation of dd_data\n");
690 		rc = -ENOMEM;
691 		goto no_dd_data;
692 	}
693 
694 	elscmd = bsg_request->rqst_data.r_els.els_code;
695 	cmdsize = job->request_payload.payload_len;
696 
697 	if (!lpfc_nlp_get(ndlp)) {
698 		rc = -ENODEV;
699 		goto free_dd_data;
700 	}
701 
702 	/* We will use the allocated dma buffers by prep els iocb for command
703 	 * and response to ensure if the job times out and the request is freed,
704 	 * we won't be dma into memory that is no longer allocated to for the
705 	 * request.
706 	 */
707 	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
708 				      ndlp->nlp_DID, elscmd);
709 	if (!cmdiocbq) {
710 		rc = -EIO;
711 		goto release_ndlp;
712 	}
713 
714 	/* Transfer the request payload to allocated command dma buffer */
715 	sg_copy_to_buffer(job->request_payload.sg_list,
716 			  job->request_payload.sg_cnt,
717 			  cmdiocbq->cmd_dmabuf->virt,
718 			  cmdsize);
719 
720 	rpi = ndlp->nlp_rpi;
721 
722 	if (phba->sli_rev == LPFC_SLI_REV4)
723 		bf_set(wqe_ctxt_tag, &cmdiocbq->wqe.generic.wqe_com,
724 		       phba->sli4_hba.rpi_ids[rpi]);
725 	else
726 		cmdiocbq->iocb.ulpContext = rpi;
727 	cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
728 	cmdiocbq->context_un.dd_data = dd_data;
729 	cmdiocbq->ndlp = ndlp;
730 	cmdiocbq->cmd_cmpl = lpfc_bsg_rport_els_cmp;
731 	dd_data->type = TYPE_IOCB;
732 	dd_data->set_job = job;
733 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
734 	dd_data->context_un.iocb.ndlp = ndlp;
735 	dd_data->context_un.iocb.rmp = NULL;
736 	job->dd_data = dd_data;
737 
738 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
739 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
740 			rc = -EIO;
741 			goto linkdown_err;
742 		}
743 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
744 		writel(creg_val, phba->HCregaddr);
745 		readl(phba->HCregaddr); /* flush */
746 	}
747 
748 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
749 	if (rc == IOCB_SUCCESS) {
750 		spin_lock_irqsave(&phba->hbalock, flags);
751 		/* make sure the I/O had not been completed/released */
752 		if (cmdiocbq->cmd_flag & LPFC_IO_LIBDFC) {
753 			/* open up abort window to timeout handler */
754 			cmdiocbq->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
755 		}
756 		spin_unlock_irqrestore(&phba->hbalock, flags);
757 		return 0; /* done for now */
758 	} else if (rc == IOCB_BUSY) {
759 		rc = -EAGAIN;
760 	} else {
761 		rc = -EIO;
762 	}
763 
764 	/* I/O issue failed.  Cleanup resources. */
765 
766 linkdown_err:
767 	lpfc_els_free_iocb(phba, cmdiocbq);
768 
769 release_ndlp:
770 	lpfc_nlp_put(ndlp);
771 
772 free_dd_data:
773 	kfree(dd_data);
774 
775 no_dd_data:
776 	/* make error code available to userspace */
777 	bsg_reply->result = rc;
778 	job->dd_data = NULL;
779 	return rc;
780 }
781 
782 /**
783  * lpfc_bsg_event_free - frees an allocated event structure
784  * @kref: Pointer to a kref.
785  *
786  * Called from kref_put. Back cast the kref into an event structure address.
787  * Free any events to get, delete associated nodes, free any events to see,
788  * free any data then free the event itself.
789  **/
790 static void
791 lpfc_bsg_event_free(struct kref *kref)
792 {
793 	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
794 						  kref);
795 	struct event_data *ed;
796 
797 	list_del(&evt->node);
798 
799 	while (!list_empty(&evt->events_to_get)) {
800 		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
801 		list_del(&ed->node);
802 		kfree(ed->data);
803 		kfree(ed);
804 	}
805 
806 	while (!list_empty(&evt->events_to_see)) {
807 		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
808 		list_del(&ed->node);
809 		kfree(ed->data);
810 		kfree(ed);
811 	}
812 
813 	kfree(evt->dd_data);
814 	kfree(evt);
815 }
816 
817 /**
818  * lpfc_bsg_event_ref - increments the kref for an event
819  * @evt: Pointer to an event structure.
820  **/
821 static inline void
822 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
823 {
824 	kref_get(&evt->kref);
825 }
826 
827 /**
828  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
829  * @evt: Pointer to an event structure.
830  **/
831 static inline void
832 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
833 {
834 	kref_put(&evt->kref, lpfc_bsg_event_free);
835 }
836 
837 /**
838  * lpfc_bsg_event_new - allocate and initialize a event structure
839  * @ev_mask: Mask of events.
840  * @ev_reg_id: Event reg id.
841  * @ev_req_id: Event request id.
842  **/
843 static struct lpfc_bsg_event *
844 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
845 {
846 	struct lpfc_bsg_event *evt = kzalloc_obj(*evt);
847 
848 	if (!evt)
849 		return NULL;
850 
851 	INIT_LIST_HEAD(&evt->events_to_get);
852 	INIT_LIST_HEAD(&evt->events_to_see);
853 	evt->type_mask = ev_mask;
854 	evt->req_id = ev_req_id;
855 	evt->reg_id = ev_reg_id;
856 	evt->wait_time_stamp = jiffies;
857 	evt->dd_data = NULL;
858 	init_waitqueue_head(&evt->wq);
859 	kref_init(&evt->kref);
860 	return evt;
861 }
862 
863 /**
864  * diag_cmd_data_free - Frees an lpfc dma buffer extension
865  * @phba: Pointer to HBA context object.
866  * @mlist: Pointer to an lpfc dma buffer extension.
867  **/
868 static int
869 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
870 {
871 	struct lpfc_dmabufext *mlast;
872 	struct pci_dev *pcidev;
873 	struct list_head head, *curr, *next;
874 
875 	if ((!mlist) || (!lpfc_is_link_up(phba) &&
876 		(phba->link_flag & LS_LOOPBACK_MODE))) {
877 		return 0;
878 	}
879 
880 	pcidev = phba->pcidev;
881 	list_add_tail(&head, &mlist->dma.list);
882 
883 	list_for_each_safe(curr, next, &head) {
884 		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
885 		if (mlast->dma.virt)
886 			dma_free_coherent(&pcidev->dev,
887 					  mlast->size,
888 					  mlast->dma.virt,
889 					  mlast->dma.phys);
890 		kfree(mlast);
891 	}
892 	return 0;
893 }
894 
895 /*
896  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
897  *
898  * This function is called when an unsolicited CT command is received.  It
899  * forwards the event to any processes registered to receive CT events.
900  **/
901 int
902 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
903 			struct lpfc_iocbq *piocbq)
904 {
905 	uint32_t evt_req_id = 0;
906 	u16 cmd;
907 	struct lpfc_dmabuf *dmabuf = NULL;
908 	struct lpfc_bsg_event *evt;
909 	struct event_data *evt_dat = NULL;
910 	struct lpfc_iocbq *iocbq;
911 	IOCB_t *iocb = NULL;
912 	size_t offset = 0;
913 	struct list_head head;
914 	struct ulp_bde64 *bde;
915 	dma_addr_t dma_addr;
916 	int i;
917 	struct lpfc_dmabuf *bdeBuf1 = piocbq->cmd_dmabuf;
918 	struct lpfc_dmabuf *bdeBuf2 = piocbq->bpl_dmabuf;
919 	struct lpfc_sli_ct_request *ct_req;
920 	struct bsg_job *job = NULL;
921 	struct fc_bsg_reply *bsg_reply;
922 	struct bsg_job_data *dd_data = NULL;
923 	unsigned long flags;
924 	int size = 0;
925 	u32 bde_count = 0;
926 
927 	INIT_LIST_HEAD(&head);
928 	list_add_tail(&head, &piocbq->list);
929 
930 	ct_req = (struct lpfc_sli_ct_request *)bdeBuf1->virt;
931 	evt_req_id = ct_req->FsType;
932 	cmd = be16_to_cpu(ct_req->CommandResponse.bits.CmdRsp);
933 
934 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
935 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
936 		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
937 			evt->req_id != evt_req_id)
938 			continue;
939 
940 		lpfc_bsg_event_ref(evt);
941 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
942 		evt_dat = kzalloc_obj(*evt_dat);
943 		if (evt_dat == NULL) {
944 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
945 			lpfc_bsg_event_unref(evt);
946 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
947 					"2614 Memory allocation failed for "
948 					"CT event\n");
949 			break;
950 		}
951 
952 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
953 			/* take accumulated byte count from the last iocbq */
954 			iocbq = list_entry(head.prev, typeof(*iocbq), list);
955 			if (phba->sli_rev == LPFC_SLI_REV4)
956 				evt_dat->len = iocbq->wcqe_cmpl.total_data_placed;
957 			else
958 				evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
959 		} else {
960 			list_for_each_entry(iocbq, &head, list) {
961 				iocb = &iocbq->iocb;
962 				for (i = 0; i < iocb->ulpBdeCount;
963 				     i++)
964 					evt_dat->len +=
965 					iocb->un.cont64[i].tus.f.bdeSize;
966 			}
967 		}
968 
969 		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
970 		if (evt_dat->data == NULL) {
971 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
972 					"2615 Memory allocation failed for "
973 					"CT event data, size %d\n",
974 					evt_dat->len);
975 			kfree(evt_dat);
976 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
977 			lpfc_bsg_event_unref(evt);
978 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
979 			goto error_ct_unsol_exit;
980 		}
981 
982 		list_for_each_entry(iocbq, &head, list) {
983 			size = 0;
984 			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
985 				bdeBuf1 = iocbq->cmd_dmabuf;
986 				bdeBuf2 = iocbq->bpl_dmabuf;
987 			}
988 			if (phba->sli_rev == LPFC_SLI_REV4)
989 				bde_count = iocbq->wcqe_cmpl.word3;
990 			else
991 				bde_count = iocbq->iocb.ulpBdeCount;
992 			for (i = 0; i < bde_count; i++) {
993 				if (phba->sli3_options &
994 				    LPFC_SLI3_HBQ_ENABLED) {
995 					if (i == 0) {
996 						size = iocbq->wqe.gen_req.bde.tus.f.bdeSize;
997 						dmabuf = bdeBuf1;
998 					} else if (i == 1) {
999 						size = iocbq->unsol_rcv_len;
1000 						dmabuf = bdeBuf2;
1001 					}
1002 					if ((offset + size) > evt_dat->len)
1003 						size = evt_dat->len - offset;
1004 				} else {
1005 					size = iocbq->iocb.un.cont64[i].
1006 						tus.f.bdeSize;
1007 					bde = &iocbq->iocb.un.cont64[i];
1008 					dma_addr = getPaddr(bde->addrHigh,
1009 							    bde->addrLow);
1010 					dmabuf = lpfc_sli_ringpostbuf_get(phba,
1011 							pring, dma_addr);
1012 				}
1013 				if (!dmabuf) {
1014 					lpfc_printf_log(phba, KERN_ERR,
1015 						LOG_LIBDFC, "2616 No dmabuf "
1016 						"found for iocbq x%px\n",
1017 						iocbq);
1018 					kfree(evt_dat->data);
1019 					kfree(evt_dat);
1020 					spin_lock_irqsave(&phba->ct_ev_lock,
1021 						flags);
1022 					lpfc_bsg_event_unref(evt);
1023 					spin_unlock_irqrestore(
1024 						&phba->ct_ev_lock, flags);
1025 					goto error_ct_unsol_exit;
1026 				}
1027 				memcpy((char *)(evt_dat->data) + offset,
1028 				       dmabuf->virt, size);
1029 				offset += size;
1030 				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
1031 				    !(phba->sli3_options &
1032 				      LPFC_SLI3_HBQ_ENABLED)) {
1033 					lpfc_sli_ringpostbuf_put(phba, pring,
1034 								 dmabuf);
1035 				} else {
1036 					switch (cmd) {
1037 					case ELX_LOOPBACK_DATA:
1038 						if (phba->sli_rev <
1039 						    LPFC_SLI_REV4)
1040 							diag_cmd_data_free(phba,
1041 							(struct lpfc_dmabufext
1042 							 *)dmabuf);
1043 						break;
1044 					case ELX_LOOPBACK_XRI_SETUP:
1045 						if ((phba->sli_rev ==
1046 							LPFC_SLI_REV2) ||
1047 							(phba->sli3_options &
1048 							LPFC_SLI3_HBQ_ENABLED
1049 							)) {
1050 							lpfc_in_buf_free(phba,
1051 									dmabuf);
1052 						} else {
1053 							lpfc_sli3_post_buffer(phba,
1054 									      pring,
1055 									      1);
1056 						}
1057 						break;
1058 					default:
1059 						if (!(phba->sli3_options &
1060 						      LPFC_SLI3_HBQ_ENABLED))
1061 							lpfc_sli3_post_buffer(phba,
1062 									      pring,
1063 									      1);
1064 						break;
1065 					}
1066 				}
1067 			}
1068 		}
1069 
1070 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1071 		if (phba->sli_rev == LPFC_SLI_REV4) {
1072 			evt_dat->immed_dat = phba->ctx_idx;
1073 			phba->ctx_idx = (phba->ctx_idx + 1) % LPFC_CT_CTX_MAX;
1074 			/* Provide warning for over-run of the ct_ctx array */
1075 			if (phba->ct_ctx[evt_dat->immed_dat].valid ==
1076 			    UNSOL_VALID)
1077 				lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1078 						"2717 CT context array entry "
1079 						"[%d] over-run: oxid:x%x, "
1080 						"sid:x%x\n", phba->ctx_idx,
1081 						phba->ct_ctx[
1082 						    evt_dat->immed_dat].oxid,
1083 						phba->ct_ctx[
1084 						    evt_dat->immed_dat].SID);
1085 			phba->ct_ctx[evt_dat->immed_dat].rxid =
1086 				get_job_ulpcontext(phba, piocbq);
1087 			phba->ct_ctx[evt_dat->immed_dat].oxid =
1088 				get_job_rcvoxid(phba, piocbq);
1089 			phba->ct_ctx[evt_dat->immed_dat].SID =
1090 				bf_get(wqe_els_did,
1091 				       &piocbq->wqe.xmit_els_rsp.wqe_dest);
1092 			phba->ct_ctx[evt_dat->immed_dat].valid = UNSOL_VALID;
1093 		} else
1094 			evt_dat->immed_dat = get_job_ulpcontext(phba, piocbq);
1095 
1096 		evt_dat->type = FC_REG_CT_EVENT;
1097 		list_add(&evt_dat->node, &evt->events_to_see);
1098 		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
1099 			wake_up_interruptible(&evt->wq);
1100 			lpfc_bsg_event_unref(evt);
1101 			break;
1102 		}
1103 
1104 		list_move(evt->events_to_see.prev, &evt->events_to_get);
1105 
1106 		dd_data = (struct bsg_job_data *)evt->dd_data;
1107 		job = dd_data->set_job;
1108 		dd_data->set_job = NULL;
1109 		lpfc_bsg_event_unref(evt);
1110 		if (job) {
1111 			bsg_reply = job->reply;
1112 			bsg_reply->reply_payload_rcv_len = size;
1113 			/* make error code available to userspace */
1114 			bsg_reply->result = 0;
1115 			job->dd_data = NULL;
1116 			/* complete the job back to userspace */
1117 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1118 			bsg_job_done(job, bsg_reply->result,
1119 				       bsg_reply->reply_payload_rcv_len);
1120 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
1121 		}
1122 	}
1123 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1124 
1125 error_ct_unsol_exit:
1126 	if (!list_empty(&head))
1127 		list_del(&head);
1128 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
1129 	    (evt_req_id == SLI_CT_ELX_LOOPBACK))
1130 		return 0;
1131 	return 1;
1132 }
1133 
1134 /**
1135  * lpfc_bsg_ct_unsol_abort - handler ct abort to management plane
1136  * @phba: Pointer to HBA context object.
1137  * @dmabuf: pointer to a dmabuf that describes the FC sequence
1138  *
1139  * This function handles abort to the CT command toward management plane
1140  * for SLI4 port.
1141  *
1142  * If the pending context of a CT command to management plane present, clears
1143  * such context and returns 1 for handled; otherwise, it returns 0 indicating
1144  * no context exists.
1145  **/
1146 int
1147 lpfc_bsg_ct_unsol_abort(struct lpfc_hba *phba, struct hbq_dmabuf *dmabuf)
1148 {
1149 	struct fc_frame_header fc_hdr;
1150 	struct fc_frame_header *fc_hdr_ptr = &fc_hdr;
1151 	int ctx_idx, handled = 0;
1152 	uint16_t oxid, rxid;
1153 	uint32_t sid;
1154 
1155 	memcpy(fc_hdr_ptr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
1156 	sid = sli4_sid_from_fc_hdr(fc_hdr_ptr);
1157 	oxid = be16_to_cpu(fc_hdr_ptr->fh_ox_id);
1158 	rxid = be16_to_cpu(fc_hdr_ptr->fh_rx_id);
1159 
1160 	for (ctx_idx = 0; ctx_idx < LPFC_CT_CTX_MAX; ctx_idx++) {
1161 		if (phba->ct_ctx[ctx_idx].valid != UNSOL_VALID)
1162 			continue;
1163 		if (phba->ct_ctx[ctx_idx].rxid != rxid)
1164 			continue;
1165 		if (phba->ct_ctx[ctx_idx].oxid != oxid)
1166 			continue;
1167 		if (phba->ct_ctx[ctx_idx].SID != sid)
1168 			continue;
1169 		phba->ct_ctx[ctx_idx].valid = UNSOL_INVALID;
1170 		handled = 1;
1171 	}
1172 	return handled;
1173 }
1174 
1175 /**
1176  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1177  * @job: SET_EVENT fc_bsg_job
1178  **/
1179 static int
1180 lpfc_bsg_hba_set_event(struct bsg_job *job)
1181 {
1182 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1183 	struct lpfc_hba *phba = vport->phba;
1184 	struct fc_bsg_request *bsg_request = job->request;
1185 	struct set_ct_event *event_req;
1186 	struct lpfc_bsg_event *evt;
1187 	int rc = 0;
1188 	struct bsg_job_data *dd_data = NULL;
1189 	uint32_t ev_mask;
1190 	unsigned long flags;
1191 
1192 	if (job->request_len <
1193 	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1194 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1195 				"2612 Received SET_CT_EVENT below minimum "
1196 				"size\n");
1197 		rc = -EINVAL;
1198 		goto job_error;
1199 	}
1200 
1201 	event_req = (struct set_ct_event *)
1202 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1203 	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1204 				FC_REG_EVENT_MASK);
1205 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1206 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1207 		if (evt->reg_id == event_req->ev_reg_id) {
1208 			lpfc_bsg_event_ref(evt);
1209 			evt->wait_time_stamp = jiffies;
1210 			dd_data = (struct bsg_job_data *)evt->dd_data;
1211 			break;
1212 		}
1213 	}
1214 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1215 
1216 	if (&evt->node == &phba->ct_ev_waiters) {
1217 		/* no event waiting struct yet - first call */
1218 		dd_data = kmalloc_obj(struct bsg_job_data);
1219 		if (dd_data == NULL) {
1220 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1221 					"2734 Failed allocation of dd_data\n");
1222 			rc = -ENOMEM;
1223 			goto job_error;
1224 		}
1225 		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1226 					event_req->ev_req_id);
1227 		if (!evt) {
1228 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1229 					"2617 Failed allocation of event "
1230 					"waiter\n");
1231 			rc = -ENOMEM;
1232 			goto job_error;
1233 		}
1234 		dd_data->type = TYPE_EVT;
1235 		dd_data->set_job = NULL;
1236 		dd_data->context_un.evt = evt;
1237 		evt->dd_data = (void *)dd_data;
1238 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1239 		list_add(&evt->node, &phba->ct_ev_waiters);
1240 		lpfc_bsg_event_ref(evt);
1241 		evt->wait_time_stamp = jiffies;
1242 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1243 	}
1244 
1245 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1246 	evt->waiting = 1;
1247 	dd_data->set_job = job; /* for unsolicited command */
1248 	job->dd_data = dd_data; /* for fc transport timeout callback*/
1249 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1250 	return 0; /* call job done later */
1251 
1252 job_error:
1253 	kfree(dd_data);
1254 	job->dd_data = NULL;
1255 	return rc;
1256 }
1257 
1258 /**
1259  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1260  * @job: GET_EVENT fc_bsg_job
1261  **/
1262 static int
1263 lpfc_bsg_hba_get_event(struct bsg_job *job)
1264 {
1265 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1266 	struct lpfc_hba *phba = vport->phba;
1267 	struct fc_bsg_request *bsg_request = job->request;
1268 	struct fc_bsg_reply *bsg_reply = job->reply;
1269 	struct get_ct_event *event_req;
1270 	struct get_ct_event_reply *event_reply;
1271 	struct lpfc_bsg_event *evt, *evt_next;
1272 	struct event_data *evt_dat = NULL;
1273 	unsigned long flags;
1274 	uint32_t rc = 0;
1275 
1276 	if (job->request_len <
1277 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1278 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1279 				"2613 Received GET_CT_EVENT request below "
1280 				"minimum size\n");
1281 		rc = -EINVAL;
1282 		goto job_error;
1283 	}
1284 
1285 	event_req = (struct get_ct_event *)
1286 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1287 
1288 	event_reply = (struct get_ct_event_reply *)
1289 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
1290 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1291 	list_for_each_entry_safe(evt, evt_next, &phba->ct_ev_waiters, node) {
1292 		if (evt->reg_id == event_req->ev_reg_id) {
1293 			if (list_empty(&evt->events_to_get))
1294 				break;
1295 			lpfc_bsg_event_ref(evt);
1296 			evt->wait_time_stamp = jiffies;
1297 			evt_dat = list_entry(evt->events_to_get.prev,
1298 					     struct event_data, node);
1299 			list_del(&evt_dat->node);
1300 			break;
1301 		}
1302 	}
1303 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1304 
1305 	/* The app may continue to ask for event data until it gets
1306 	 * an error indicating that there isn't anymore
1307 	 */
1308 	if (evt_dat == NULL) {
1309 		bsg_reply->reply_payload_rcv_len = 0;
1310 		rc = -ENOENT;
1311 		goto job_error;
1312 	}
1313 
1314 	if (evt_dat->len > job->request_payload.payload_len) {
1315 		evt_dat->len = job->request_payload.payload_len;
1316 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1317 				"2618 Truncated event data at %d "
1318 				"bytes\n",
1319 				job->request_payload.payload_len);
1320 	}
1321 
1322 	event_reply->type = evt_dat->type;
1323 	event_reply->immed_data = evt_dat->immed_dat;
1324 	if (evt_dat->len > 0)
1325 		bsg_reply->reply_payload_rcv_len =
1326 			sg_copy_from_buffer(job->request_payload.sg_list,
1327 					    job->request_payload.sg_cnt,
1328 					    evt_dat->data, evt_dat->len);
1329 	else
1330 		bsg_reply->reply_payload_rcv_len = 0;
1331 
1332 	kfree(evt_dat->data);
1333 	kfree(evt_dat);
1334 
1335 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1336 	lpfc_bsg_event_unref(evt);
1337 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1338 	job->dd_data = NULL;
1339 	bsg_reply->result = 0;
1340 	bsg_job_done(job, bsg_reply->result,
1341 		       bsg_reply->reply_payload_rcv_len);
1342 	return 0;
1343 
1344 job_error:
1345 	job->dd_data = NULL;
1346 	bsg_reply->result = rc;
1347 	return rc;
1348 }
1349 
1350 /**
1351  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1352  * @phba: Pointer to HBA context object.
1353  * @cmdiocbq: Pointer to command iocb.
1354  * @rspiocbq: Pointer to response iocb.
1355  *
1356  * This function is the completion handler for iocbs issued using
1357  * lpfc_issue_ct_rsp_cmp function. This function is called by the
1358  * ring event handler function without any lock held. This function
1359  * can be called from both worker thread context and interrupt
1360  * context. This function also can be called from other thread which
1361  * cleans up the SLI layer objects.
1362  * This function copy the contents of the response iocb to the
1363  * response iocb memory object provided by the caller of
1364  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1365  * sleeps for the iocb completion.
1366  **/
1367 static void
1368 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1369 			struct lpfc_iocbq *cmdiocbq,
1370 			struct lpfc_iocbq *rspiocbq)
1371 {
1372 	struct bsg_job_data *dd_data;
1373 	struct bsg_job *job;
1374 	struct fc_bsg_reply *bsg_reply;
1375 	struct lpfc_dmabuf *bmp, *cmp;
1376 	struct lpfc_nodelist *ndlp;
1377 	unsigned long flags;
1378 	int rc = 0;
1379 	u32 ulp_status, ulp_word4;
1380 
1381 	dd_data = cmdiocbq->context_un.dd_data;
1382 
1383 	/* Determine if job has been aborted */
1384 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1385 	job = dd_data->set_job;
1386 	if (job) {
1387 		/* Prevent timeout handling from trying to abort job  */
1388 		job->dd_data = NULL;
1389 	}
1390 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1391 
1392 	/* Close the timeout handler abort window */
1393 	spin_lock_irqsave(&phba->hbalock, flags);
1394 	cmdiocbq->cmd_flag &= ~LPFC_IO_CMD_OUTSTANDING;
1395 	spin_unlock_irqrestore(&phba->hbalock, flags);
1396 
1397 	ndlp = dd_data->context_un.iocb.ndlp;
1398 	cmp = cmdiocbq->cmd_dmabuf;
1399 	bmp = cmdiocbq->bpl_dmabuf;
1400 
1401 	ulp_status = get_job_ulpstatus(phba, rspiocbq);
1402 	ulp_word4 = get_job_word4(phba, rspiocbq);
1403 
1404 	/* Copy the completed job data or set the error status */
1405 
1406 	if (job) {
1407 		bsg_reply = job->reply;
1408 		if (ulp_status) {
1409 			if (ulp_status == IOSTAT_LOCAL_REJECT) {
1410 				switch (ulp_word4 & IOERR_PARAM_MASK) {
1411 				case IOERR_SEQUENCE_TIMEOUT:
1412 					rc = -ETIMEDOUT;
1413 					break;
1414 				case IOERR_INVALID_RPI:
1415 					rc = -EFAULT;
1416 					break;
1417 				default:
1418 					rc = -EACCES;
1419 					break;
1420 				}
1421 			} else {
1422 				rc = -EACCES;
1423 			}
1424 		} else {
1425 			bsg_reply->reply_payload_rcv_len = 0;
1426 		}
1427 	}
1428 
1429 	lpfc_free_bsg_buffers(phba, cmp);
1430 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1431 	kfree(bmp);
1432 	lpfc_sli_release_iocbq(phba, cmdiocbq);
1433 	lpfc_nlp_put(ndlp);
1434 	kfree(dd_data);
1435 
1436 	/* Complete the job if the job is still active */
1437 
1438 	if (job) {
1439 		bsg_reply->result = rc;
1440 		bsg_job_done(job, bsg_reply->result,
1441 			       bsg_reply->reply_payload_rcv_len);
1442 	}
1443 	return;
1444 }
1445 
1446 /**
1447  * lpfc_issue_ct_rsp - issue a ct response
1448  * @phba: Pointer to HBA context object.
1449  * @job: Pointer to the job object.
1450  * @tag: tag index value into the ports context exchange array.
1451  * @cmp: Pointer to a cmp dma buffer descriptor.
1452  * @bmp: Pointer to a bmp dma buffer descriptor.
1453  * @num_entry: Number of enties in the bde.
1454  **/
1455 static int
1456 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct bsg_job *job, uint32_t tag,
1457 		  struct lpfc_dmabuf *cmp, struct lpfc_dmabuf *bmp,
1458 		  int num_entry)
1459 {
1460 	struct lpfc_iocbq *ctiocb = NULL;
1461 	int rc = 0;
1462 	struct lpfc_nodelist *ndlp = NULL;
1463 	struct bsg_job_data *dd_data;
1464 	unsigned long flags;
1465 	uint32_t creg_val;
1466 	u16 ulp_context, iotag;
1467 
1468 	ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1469 	if (!ndlp) {
1470 		lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1471 				"2721 ndlp null for oxid %x SID %x\n",
1472 				phba->ct_ctx[tag].rxid,
1473 				phba->ct_ctx[tag].SID);
1474 		return IOCB_ERROR;
1475 	}
1476 
1477 	/* allocate our bsg tracking structure */
1478 	dd_data = kmalloc_obj(struct bsg_job_data);
1479 	if (!dd_data) {
1480 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1481 				"2736 Failed allocation of dd_data\n");
1482 		rc = -ENOMEM;
1483 		goto no_dd_data;
1484 	}
1485 
1486 	/* Allocate buffer for  command iocb */
1487 	ctiocb = lpfc_sli_get_iocbq(phba);
1488 	if (!ctiocb) {
1489 		rc = -ENOMEM;
1490 		goto no_ctiocb;
1491 	}
1492 
1493 	if (phba->sli_rev == LPFC_SLI_REV4) {
1494 		/* Do not issue unsol response if oxid not marked as valid */
1495 		if (phba->ct_ctx[tag].valid != UNSOL_VALID) {
1496 			rc = IOCB_ERROR;
1497 			goto issue_ct_rsp_exit;
1498 		}
1499 
1500 		lpfc_sli_prep_xmit_seq64(phba, ctiocb, bmp,
1501 					 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
1502 					 phba->ct_ctx[tag].oxid, num_entry,
1503 					 FC_RCTL_DD_SOL_CTL, 1,
1504 					 CMD_XMIT_SEQUENCE64_WQE);
1505 
1506 		/* The exchange is done, mark the entry as invalid */
1507 		phba->ct_ctx[tag].valid = UNSOL_INVALID;
1508 		iotag = get_wqe_reqtag(ctiocb);
1509 	} else {
1510 		lpfc_sli_prep_xmit_seq64(phba, ctiocb, bmp, 0, tag, num_entry,
1511 					 FC_RCTL_DD_SOL_CTL, 1,
1512 					 CMD_XMIT_SEQUENCE64_CX);
1513 		ctiocb->num_bdes = num_entry;
1514 		iotag = ctiocb->iocb.ulpIoTag;
1515 	}
1516 
1517 	ulp_context = get_job_ulpcontext(phba, ctiocb);
1518 
1519 	/* Xmit CT response on exchange <xid> */
1520 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1521 			"2722 Xmit CT response on exchange x%x Data: x%x x%x x%x\n",
1522 			ulp_context, iotag, tag, phba->link_state);
1523 
1524 	ctiocb->cmd_flag |= LPFC_IO_LIBDFC;
1525 	ctiocb->vport = phba->pport;
1526 	ctiocb->context_un.dd_data = dd_data;
1527 	ctiocb->cmd_dmabuf = cmp;
1528 	ctiocb->bpl_dmabuf = bmp;
1529 	ctiocb->ndlp = ndlp;
1530 	ctiocb->cmd_cmpl = lpfc_issue_ct_rsp_cmp;
1531 
1532 	dd_data->type = TYPE_IOCB;
1533 	dd_data->set_job = job;
1534 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
1535 	dd_data->context_un.iocb.ndlp = lpfc_nlp_get(ndlp);
1536 	if (!dd_data->context_un.iocb.ndlp) {
1537 		rc = -IOCB_ERROR;
1538 		goto issue_ct_rsp_exit;
1539 	}
1540 	dd_data->context_un.iocb.rmp = NULL;
1541 	job->dd_data = dd_data;
1542 
1543 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1544 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1545 			rc = -IOCB_ERROR;
1546 			goto issue_ct_rsp_exit;
1547 		}
1548 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1549 		writel(creg_val, phba->HCregaddr);
1550 		readl(phba->HCregaddr); /* flush */
1551 	}
1552 
1553 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1554 	if (rc == IOCB_SUCCESS) {
1555 		spin_lock_irqsave(&phba->hbalock, flags);
1556 		/* make sure the I/O had not been completed/released */
1557 		if (ctiocb->cmd_flag & LPFC_IO_LIBDFC) {
1558 			/* open up abort window to timeout handler */
1559 			ctiocb->cmd_flag |= LPFC_IO_CMD_OUTSTANDING;
1560 		}
1561 		spin_unlock_irqrestore(&phba->hbalock, flags);
1562 		return 0; /* done for now */
1563 	}
1564 
1565 	/* iocb failed so cleanup */
1566 	job->dd_data = NULL;
1567 	lpfc_nlp_put(ndlp);
1568 
1569 issue_ct_rsp_exit:
1570 	lpfc_sli_release_iocbq(phba, ctiocb);
1571 no_ctiocb:
1572 	kfree(dd_data);
1573 no_dd_data:
1574 	return rc;
1575 }
1576 
1577 /**
1578  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1579  * @job: SEND_MGMT_RESP fc_bsg_job
1580  **/
1581 static int
1582 lpfc_bsg_send_mgmt_rsp(struct bsg_job *job)
1583 {
1584 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
1585 	struct lpfc_hba *phba = vport->phba;
1586 	struct fc_bsg_request *bsg_request = job->request;
1587 	struct fc_bsg_reply *bsg_reply = job->reply;
1588 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1589 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1590 	struct ulp_bde64 *bpl;
1591 	struct lpfc_dmabuf *bmp = NULL, *cmp = NULL;
1592 	int bpl_entries;
1593 	uint32_t tag = mgmt_resp->tag;
1594 	unsigned long reqbfrcnt =
1595 			(unsigned long)job->request_payload.payload_len;
1596 	int rc = 0;
1597 
1598 	/* in case no data is transferred */
1599 	bsg_reply->reply_payload_rcv_len = 0;
1600 
1601 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1602 		rc = -ERANGE;
1603 		goto send_mgmt_rsp_exit;
1604 	}
1605 
1606 	bmp = kmalloc_obj(struct lpfc_dmabuf);
1607 	if (!bmp) {
1608 		rc = -ENOMEM;
1609 		goto send_mgmt_rsp_exit;
1610 	}
1611 
1612 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1613 	if (!bmp->virt) {
1614 		rc = -ENOMEM;
1615 		goto send_mgmt_rsp_free_bmp;
1616 	}
1617 
1618 	INIT_LIST_HEAD(&bmp->list);
1619 	bpl = (struct ulp_bde64 *) bmp->virt;
1620 	bpl_entries = (LPFC_BPL_SIZE/sizeof(struct ulp_bde64));
1621 	cmp = lpfc_alloc_bsg_buffers(phba, job->request_payload.payload_len,
1622 				     1, bpl, &bpl_entries);
1623 	if (!cmp) {
1624 		rc = -ENOMEM;
1625 		goto send_mgmt_rsp_free_bmp;
1626 	}
1627 	lpfc_bsg_copy_data(cmp, &job->request_payload,
1628 			   job->request_payload.payload_len, 1);
1629 
1630 	rc = lpfc_issue_ct_rsp(phba, job, tag, cmp, bmp, bpl_entries);
1631 
1632 	if (rc == IOCB_SUCCESS)
1633 		return 0; /* done for now */
1634 
1635 	rc = -EACCES;
1636 
1637 	lpfc_free_bsg_buffers(phba, cmp);
1638 
1639 send_mgmt_rsp_free_bmp:
1640 	if (bmp->virt)
1641 		lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1642 	kfree(bmp);
1643 send_mgmt_rsp_exit:
1644 	/* make error code available to userspace */
1645 	bsg_reply->result = rc;
1646 	job->dd_data = NULL;
1647 	return rc;
1648 }
1649 
1650 /**
1651  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1652  * @phba: Pointer to HBA context object.
1653  *
1654  * This function is responsible for preparing driver for diag loopback
1655  * on device.
1656  */
1657 static int
1658 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba)
1659 {
1660 	struct lpfc_vport **vports;
1661 	struct Scsi_Host *shost;
1662 	struct lpfc_sli *psli;
1663 	struct lpfc_queue *qp = NULL;
1664 	struct lpfc_sli_ring *pring;
1665 	int i = 0;
1666 
1667 	psli = &phba->sli;
1668 	if (!psli)
1669 		return -ENODEV;
1670 
1671 
1672 	if ((phba->link_state == LPFC_HBA_ERROR) ||
1673 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1674 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1675 		return -EACCES;
1676 
1677 	vports = lpfc_create_vport_work_array(phba);
1678 	if (vports) {
1679 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1680 			shost = lpfc_shost_from_vport(vports[i]);
1681 			scsi_block_requests(shost);
1682 		}
1683 		lpfc_destroy_vport_work_array(phba, vports);
1684 	} else {
1685 		shost = lpfc_shost_from_vport(phba->pport);
1686 		scsi_block_requests(shost);
1687 	}
1688 
1689 	if (phba->sli_rev != LPFC_SLI_REV4) {
1690 		pring = &psli->sli3_ring[LPFC_FCP_RING];
1691 		lpfc_emptyq_wait(phba, &pring->txcmplq, &phba->hbalock);
1692 		return 0;
1693 	}
1694 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
1695 		pring = qp->pring;
1696 		if (!pring || (pring->ringno != LPFC_FCP_RING))
1697 			continue;
1698 		if (!lpfc_emptyq_wait(phba, &pring->txcmplq,
1699 				      &pring->ring_lock))
1700 			break;
1701 	}
1702 	return 0;
1703 }
1704 
1705 /**
1706  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1707  * @phba: Pointer to HBA context object.
1708  *
1709  * This function is responsible for driver exit processing of setting up
1710  * diag loopback mode on device.
1711  */
1712 static void
1713 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1714 {
1715 	struct Scsi_Host *shost;
1716 	struct lpfc_vport **vports;
1717 	int i;
1718 
1719 	vports = lpfc_create_vport_work_array(phba);
1720 	if (vports) {
1721 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1722 			shost = lpfc_shost_from_vport(vports[i]);
1723 			scsi_unblock_requests(shost);
1724 		}
1725 		lpfc_destroy_vport_work_array(phba, vports);
1726 	} else {
1727 		shost = lpfc_shost_from_vport(phba->pport);
1728 		scsi_unblock_requests(shost);
1729 	}
1730 	return;
1731 }
1732 
1733 /**
1734  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1735  * @phba: Pointer to HBA context object.
1736  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1737  *
1738  * This function is responsible for placing an sli3  port into diagnostic
1739  * loopback mode in order to perform a diagnostic loopback test.
1740  * All new scsi requests are blocked, a small delay is used to allow the
1741  * scsi requests to complete then the link is brought down. If the link is
1742  * is placed in loopback mode then scsi requests are again allowed
1743  * so the scsi mid-layer doesn't give up on the port.
1744  * All of this is done in-line.
1745  */
1746 static int
1747 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
1748 {
1749 	struct fc_bsg_request *bsg_request = job->request;
1750 	struct fc_bsg_reply *bsg_reply = job->reply;
1751 	struct diag_mode_set *loopback_mode;
1752 	uint32_t link_flags;
1753 	uint32_t timeout;
1754 	LPFC_MBOXQ_t *pmboxq  = NULL;
1755 	int mbxstatus = MBX_SUCCESS;
1756 	int i = 0;
1757 	int rc = 0;
1758 
1759 	/* no data to return just the return code */
1760 	bsg_reply->reply_payload_rcv_len = 0;
1761 
1762 	if (job->request_len < sizeof(struct fc_bsg_request) +
1763 	    sizeof(struct diag_mode_set)) {
1764 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1765 				"2738 Received DIAG MODE request size:%d "
1766 				"below the minimum size:%d\n",
1767 				job->request_len,
1768 				(int)(sizeof(struct fc_bsg_request) +
1769 				sizeof(struct diag_mode_set)));
1770 		rc = -EINVAL;
1771 		goto job_error;
1772 	}
1773 
1774 	rc = lpfc_bsg_diag_mode_enter(phba);
1775 	if (rc)
1776 		goto job_error;
1777 
1778 	/* bring the link to diagnostic mode */
1779 	loopback_mode = (struct diag_mode_set *)
1780 		bsg_request->rqst_data.h_vendor.vendor_cmd;
1781 	link_flags = loopback_mode->type;
1782 	timeout = loopback_mode->timeout * 100;
1783 
1784 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1785 	if (!pmboxq) {
1786 		rc = -ENOMEM;
1787 		goto loopback_mode_exit;
1788 	}
1789 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1790 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1791 	pmboxq->u.mb.mbxOwner = OWN_HOST;
1792 
1793 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1794 
1795 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1796 		/* wait for link down before proceeding */
1797 		i = 0;
1798 		while (phba->link_state != LPFC_LINK_DOWN) {
1799 			if (i++ > timeout) {
1800 				rc = -ETIMEDOUT;
1801 				goto loopback_mode_exit;
1802 			}
1803 			msleep(10);
1804 		}
1805 
1806 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1807 		if (link_flags == INTERNAL_LOOP_BACK)
1808 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1809 		else
1810 			pmboxq->u.mb.un.varInitLnk.link_flags =
1811 				FLAGS_TOPOLOGY_MODE_LOOP;
1812 
1813 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1814 		pmboxq->u.mb.mbxOwner = OWN_HOST;
1815 
1816 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1817 						     LPFC_MBOX_TMO);
1818 
1819 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1820 			rc = -ENODEV;
1821 		else {
1822 			spin_lock_irq(&phba->hbalock);
1823 			phba->link_flag |= LS_LOOPBACK_MODE;
1824 			spin_unlock_irq(&phba->hbalock);
1825 			/* wait for the link attention interrupt */
1826 			msleep(100);
1827 
1828 			i = 0;
1829 			while (phba->link_state != LPFC_HBA_READY) {
1830 				if (i++ > timeout) {
1831 					rc = -ETIMEDOUT;
1832 					break;
1833 				}
1834 
1835 				msleep(10);
1836 			}
1837 		}
1838 
1839 	} else
1840 		rc = -ENODEV;
1841 
1842 loopback_mode_exit:
1843 	lpfc_bsg_diag_mode_exit(phba);
1844 
1845 	/*
1846 	 * Let SLI layer release mboxq if mbox command completed after timeout.
1847 	 */
1848 	if (pmboxq && mbxstatus != MBX_TIMEOUT)
1849 		mempool_free(pmboxq, phba->mbox_mem_pool);
1850 
1851 job_error:
1852 	/* make error code available to userspace */
1853 	bsg_reply->result = rc;
1854 	/* complete the job back to userspace if no error */
1855 	if (rc == 0)
1856 		bsg_job_done(job, bsg_reply->result,
1857 			       bsg_reply->reply_payload_rcv_len);
1858 	return rc;
1859 }
1860 
1861 /**
1862  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1863  * @phba: Pointer to HBA context object.
1864  * @diag: Flag for set link to diag or nomral operation state.
1865  *
1866  * This function is responsible for issuing a sli4 mailbox command for setting
1867  * link to either diag state or normal operation state.
1868  */
1869 static int
1870 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1871 {
1872 	LPFC_MBOXQ_t *pmboxq;
1873 	struct lpfc_mbx_set_link_diag_state *link_diag_state;
1874 	uint32_t req_len, alloc_len;
1875 	int mbxstatus = MBX_SUCCESS, rc;
1876 
1877 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1878 	if (!pmboxq)
1879 		return -ENOMEM;
1880 
1881 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1882 		   sizeof(struct lpfc_sli4_cfg_mhdr));
1883 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1884 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1885 				req_len, LPFC_SLI4_MBX_EMBED);
1886 	if (alloc_len != req_len) {
1887 		rc = -ENOMEM;
1888 		goto link_diag_state_set_out;
1889 	}
1890 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
1891 			"3128 Set link to diagnostic state:x%x (x%x/x%x)\n",
1892 			diag, phba->sli4_hba.lnk_info.lnk_tp,
1893 			phba->sli4_hba.lnk_info.lnk_no);
1894 
1895 	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1896 	bf_set(lpfc_mbx_set_diag_state_diag_bit_valid, &link_diag_state->u.req,
1897 	       LPFC_DIAG_STATE_DIAG_BIT_VALID_CHANGE);
1898 	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1899 	       phba->sli4_hba.lnk_info.lnk_no);
1900 	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1901 	       phba->sli4_hba.lnk_info.lnk_tp);
1902 	if (diag)
1903 		bf_set(lpfc_mbx_set_diag_state_diag,
1904 		       &link_diag_state->u.req, 1);
1905 	else
1906 		bf_set(lpfc_mbx_set_diag_state_diag,
1907 		       &link_diag_state->u.req, 0);
1908 
1909 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1910 
1911 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1912 		rc = 0;
1913 	else
1914 		rc = -ENODEV;
1915 
1916 link_diag_state_set_out:
1917 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1918 		mempool_free(pmboxq, phba->mbox_mem_pool);
1919 
1920 	return rc;
1921 }
1922 
1923 /**
1924  * lpfc_sli4_bsg_set_loopback_mode - set sli4 internal loopback diagnostic
1925  * @phba: Pointer to HBA context object.
1926  * @mode: loopback mode to set
1927  * @link_no: link number for loopback mode to set
1928  *
1929  * This function is responsible for issuing a sli4 mailbox command for setting
1930  * up loopback diagnostic for a link.
1931  */
1932 static int
1933 lpfc_sli4_bsg_set_loopback_mode(struct lpfc_hba *phba, int mode,
1934 				uint32_t link_no)
1935 {
1936 	LPFC_MBOXQ_t *pmboxq;
1937 	uint32_t req_len, alloc_len;
1938 	struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1939 	int mbxstatus = MBX_SUCCESS, rc = 0;
1940 
1941 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1942 	if (!pmboxq)
1943 		return -ENOMEM;
1944 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1945 		   sizeof(struct lpfc_sli4_cfg_mhdr));
1946 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1947 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1948 				req_len, LPFC_SLI4_MBX_EMBED);
1949 	if (alloc_len != req_len) {
1950 		mempool_free(pmboxq, phba->mbox_mem_pool);
1951 		return -ENOMEM;
1952 	}
1953 	link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1954 	bf_set(lpfc_mbx_set_diag_state_link_num,
1955 	       &link_diag_loopback->u.req, link_no);
1956 
1957 	if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
1958 		bf_set(lpfc_mbx_set_diag_state_link_type,
1959 		       &link_diag_loopback->u.req, LPFC_LNK_FC_TRUNKED);
1960 	} else {
1961 		bf_set(lpfc_mbx_set_diag_state_link_type,
1962 		       &link_diag_loopback->u.req,
1963 		       phba->sli4_hba.lnk_info.lnk_tp);
1964 	}
1965 
1966 	bf_set(lpfc_mbx_set_diag_lpbk_type, &link_diag_loopback->u.req,
1967 	       mode);
1968 
1969 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1970 	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus)) {
1971 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1972 				"3127 Failed setup loopback mode mailbox "
1973 				"command, rc:x%x, status:x%x\n", mbxstatus,
1974 				pmboxq->u.mb.mbxStatus);
1975 		rc = -ENODEV;
1976 	}
1977 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1978 		mempool_free(pmboxq, phba->mbox_mem_pool);
1979 	return rc;
1980 }
1981 
1982 /**
1983  * lpfc_sli4_diag_fcport_reg_setup - setup port registrations for diagnostic
1984  * @phba: Pointer to HBA context object.
1985  *
1986  * This function set up SLI4 FC port registrations for diagnostic run, which
1987  * includes all the rpis, vfi, and also vpi.
1988  */
1989 static int
1990 lpfc_sli4_diag_fcport_reg_setup(struct lpfc_hba *phba)
1991 {
1992 	if (test_bit(FC_VFI_REGISTERED, &phba->pport->fc_flag)) {
1993 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1994 				"3136 Port still had vfi registered: "
1995 				"mydid:x%x, fcfi:%d, vfi:%d, vpi:%d\n",
1996 				phba->pport->fc_myDID, phba->fcf.fcfi,
1997 				phba->sli4_hba.vfi_ids[phba->pport->vfi],
1998 				phba->vpi_ids[phba->pport->vpi]);
1999 		return -EINVAL;
2000 	}
2001 	return lpfc_issue_reg_vfi(phba->pport);
2002 }
2003 
2004 /**
2005  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
2006  * @phba: Pointer to HBA context object.
2007  * @job: LPFC_BSG_VENDOR_DIAG_MODE
2008  *
2009  * This function is responsible for placing an sli4 port into diagnostic
2010  * loopback mode in order to perform a diagnostic loopback test.
2011  */
2012 static int
2013 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct bsg_job *job)
2014 {
2015 	struct fc_bsg_request *bsg_request = job->request;
2016 	struct fc_bsg_reply *bsg_reply = job->reply;
2017 	struct diag_mode_set *loopback_mode;
2018 	uint32_t link_flags, timeout, link_no;
2019 	int i, rc = 0;
2020 
2021 	/* no data to return just the return code */
2022 	bsg_reply->reply_payload_rcv_len = 0;
2023 
2024 	if (job->request_len < sizeof(struct fc_bsg_request) +
2025 	    sizeof(struct diag_mode_set)) {
2026 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2027 				"3011 Received DIAG MODE request size:%d "
2028 				"below the minimum size:%d\n",
2029 				job->request_len,
2030 				(int)(sizeof(struct fc_bsg_request) +
2031 				sizeof(struct diag_mode_set)));
2032 		rc = -EINVAL;
2033 		goto job_done;
2034 	}
2035 
2036 	loopback_mode = (struct diag_mode_set *)
2037 		bsg_request->rqst_data.h_vendor.vendor_cmd;
2038 	link_flags = loopback_mode->type;
2039 	timeout = loopback_mode->timeout * 100;
2040 
2041 	if (loopback_mode->physical_link == -1)
2042 		link_no = phba->sli4_hba.lnk_info.lnk_no;
2043 	else
2044 		link_no = loopback_mode->physical_link;
2045 
2046 	if (link_flags == DISABLE_LOOP_BACK) {
2047 		rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2048 					LPFC_DIAG_LOOPBACK_TYPE_DISABLE,
2049 					link_no);
2050 		if (!rc) {
2051 			/* Unset the need disable bit */
2052 			phba->sli4_hba.conf_trunk &= ~((1 << link_no) << 4);
2053 		}
2054 		goto job_done;
2055 	} else {
2056 		/* Check if we need to disable the loopback state */
2057 		if (phba->sli4_hba.conf_trunk & ((1 << link_no) << 4)) {
2058 			rc = -EPERM;
2059 			goto job_done;
2060 		}
2061 	}
2062 
2063 	rc = lpfc_bsg_diag_mode_enter(phba);
2064 	if (rc)
2065 		goto job_done;
2066 
2067 	/* indicate we are in loopback diagnostic mode */
2068 	spin_lock_irq(&phba->hbalock);
2069 	phba->link_flag |= LS_LOOPBACK_MODE;
2070 	spin_unlock_irq(&phba->hbalock);
2071 
2072 	/* reset port to start from scratch */
2073 	rc = lpfc_selective_reset(phba);
2074 	if (rc)
2075 		goto job_done;
2076 
2077 	/* bring the link to diagnostic mode */
2078 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2079 			"3129 Bring link to diagnostic state.\n");
2080 
2081 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2082 	if (rc) {
2083 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2084 				"3130 Failed to bring link to diagnostic "
2085 				"state, rc:x%x\n", rc);
2086 		goto loopback_mode_exit;
2087 	}
2088 
2089 	/* wait for link down before proceeding */
2090 	i = 0;
2091 	while (phba->link_state != LPFC_LINK_DOWN) {
2092 		if (i++ > timeout) {
2093 			rc = -ETIMEDOUT;
2094 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2095 					"3131 Timeout waiting for link to "
2096 					"diagnostic mode, timeout:%d ms\n",
2097 					timeout * 10);
2098 			goto loopback_mode_exit;
2099 		}
2100 		msleep(10);
2101 	}
2102 
2103 	/* set up loopback mode */
2104 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2105 			"3132 Set up loopback mode:x%x\n", link_flags);
2106 
2107 	switch (link_flags) {
2108 	case INTERNAL_LOOP_BACK:
2109 		if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2110 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2111 					LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2112 					link_no);
2113 		} else {
2114 			/* Trunk is configured, but link is not in this trunk */
2115 			if (phba->sli4_hba.conf_trunk) {
2116 				rc = -ELNRNG;
2117 				goto loopback_mode_exit;
2118 			}
2119 
2120 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2121 					LPFC_DIAG_LOOPBACK_TYPE_INTERNAL,
2122 					link_no);
2123 		}
2124 
2125 		if (!rc) {
2126 			/* Set the need disable bit */
2127 			phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2128 		}
2129 
2130 		break;
2131 	case EXTERNAL_LOOP_BACK:
2132 		if (phba->sli4_hba.conf_trunk & (1 << link_no)) {
2133 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2134 				LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL_TRUNKED,
2135 				link_no);
2136 		} else {
2137 			/* Trunk is configured, but link is not in this trunk */
2138 			if (phba->sli4_hba.conf_trunk) {
2139 				rc = -ELNRNG;
2140 				goto loopback_mode_exit;
2141 			}
2142 
2143 			rc = lpfc_sli4_bsg_set_loopback_mode(phba,
2144 						LPFC_DIAG_LOOPBACK_TYPE_SERDES,
2145 						link_no);
2146 		}
2147 
2148 		if (!rc) {
2149 			/* Set the need disable bit */
2150 			phba->sli4_hba.conf_trunk |= (1 << link_no) << 4;
2151 		}
2152 
2153 		break;
2154 	default:
2155 		rc = -EINVAL;
2156 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2157 				"3141 Loopback mode:x%x not supported\n",
2158 				link_flags);
2159 		goto loopback_mode_exit;
2160 	}
2161 
2162 	if (!rc) {
2163 		/* wait for the link attention interrupt */
2164 		msleep(100);
2165 		i = 0;
2166 		while (phba->link_state < LPFC_LINK_UP) {
2167 			if (i++ > timeout) {
2168 				rc = -ETIMEDOUT;
2169 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2170 					"3137 Timeout waiting for link up "
2171 					"in loopback mode, timeout:%d ms\n",
2172 					timeout * 10);
2173 				break;
2174 			}
2175 			msleep(10);
2176 		}
2177 	}
2178 
2179 	/* port resource registration setup for loopback diagnostic */
2180 	if (!rc) {
2181 		/* set up a none zero myDID for loopback test */
2182 		phba->pport->fc_myDID = 1;
2183 		rc = lpfc_sli4_diag_fcport_reg_setup(phba);
2184 	} else
2185 		goto loopback_mode_exit;
2186 
2187 	if (!rc) {
2188 		/* wait for the port ready */
2189 		msleep(100);
2190 		i = 0;
2191 		while (phba->link_state != LPFC_HBA_READY) {
2192 			if (i++ > timeout) {
2193 				rc = -ETIMEDOUT;
2194 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2195 					"3133 Timeout waiting for port "
2196 					"loopback mode ready, timeout:%d ms\n",
2197 					timeout * 10);
2198 				break;
2199 			}
2200 			msleep(10);
2201 		}
2202 	}
2203 
2204 loopback_mode_exit:
2205 	/* clear loopback diagnostic mode */
2206 	if (rc) {
2207 		spin_lock_irq(&phba->hbalock);
2208 		phba->link_flag &= ~LS_LOOPBACK_MODE;
2209 		spin_unlock_irq(&phba->hbalock);
2210 	}
2211 	lpfc_bsg_diag_mode_exit(phba);
2212 
2213 job_done:
2214 	/* make error code available to userspace */
2215 	bsg_reply->result = rc;
2216 	/* complete the job back to userspace if no error */
2217 	if (rc == 0)
2218 		bsg_job_done(job, bsg_reply->result,
2219 			       bsg_reply->reply_payload_rcv_len);
2220 	return rc;
2221 }
2222 
2223 /**
2224  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
2225  * @job: LPFC_BSG_VENDOR_DIAG_MODE
2226  *
2227  * This function is responsible for responding to check and dispatch bsg diag
2228  * command from the user to proper driver action routines.
2229  */
2230 static int
2231 lpfc_bsg_diag_loopback_mode(struct bsg_job *job)
2232 {
2233 	struct Scsi_Host *shost;
2234 	struct lpfc_vport *vport;
2235 	struct lpfc_hba *phba;
2236 	int rc;
2237 
2238 	shost = fc_bsg_to_shost(job);
2239 	if (!shost)
2240 		return -ENODEV;
2241 	vport = shost_priv(shost);
2242 	if (!vport)
2243 		return -ENODEV;
2244 	phba = vport->phba;
2245 	if (!phba)
2246 		return -ENODEV;
2247 
2248 	if (phba->sli_rev < LPFC_SLI_REV4)
2249 		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
2250 	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
2251 		 LPFC_SLI_INTF_IF_TYPE_2)
2252 		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
2253 	else
2254 		rc = -ENODEV;
2255 
2256 	return rc;
2257 }
2258 
2259 /**
2260  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
2261  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
2262  *
2263  * This function is responsible for responding to check and dispatch bsg diag
2264  * command from the user to proper driver action routines.
2265  */
2266 static int
2267 lpfc_sli4_bsg_diag_mode_end(struct bsg_job *job)
2268 {
2269 	struct fc_bsg_request *bsg_request = job->request;
2270 	struct fc_bsg_reply *bsg_reply = job->reply;
2271 	struct Scsi_Host *shost;
2272 	struct lpfc_vport *vport;
2273 	struct lpfc_hba *phba;
2274 	struct diag_mode_set *loopback_mode_end_cmd;
2275 	uint32_t timeout;
2276 	int rc, i;
2277 
2278 	shost = fc_bsg_to_shost(job);
2279 	if (!shost)
2280 		return -ENODEV;
2281 	vport = shost_priv(shost);
2282 	if (!vport)
2283 		return -ENODEV;
2284 	phba = vport->phba;
2285 	if (!phba)
2286 		return -ENODEV;
2287 
2288 	if (phba->sli_rev < LPFC_SLI_REV4)
2289 		return -ENODEV;
2290 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2291 	    LPFC_SLI_INTF_IF_TYPE_2)
2292 		return -ENODEV;
2293 
2294 	/* clear loopback diagnostic mode */
2295 	spin_lock_irq(&phba->hbalock);
2296 	phba->link_flag &= ~LS_LOOPBACK_MODE;
2297 	spin_unlock_irq(&phba->hbalock);
2298 	loopback_mode_end_cmd = (struct diag_mode_set *)
2299 			bsg_request->rqst_data.h_vendor.vendor_cmd;
2300 	timeout = loopback_mode_end_cmd->timeout * 100;
2301 
2302 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2303 	if (rc) {
2304 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2305 				"3139 Failed to bring link to diagnostic "
2306 				"state, rc:x%x\n", rc);
2307 		goto loopback_mode_end_exit;
2308 	}
2309 
2310 	/* wait for link down before proceeding */
2311 	i = 0;
2312 	while (phba->link_state != LPFC_LINK_DOWN) {
2313 		if (i++ > timeout) {
2314 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
2315 					"3140 Timeout waiting for link to "
2316 					"diagnostic mode_end, timeout:%d ms\n",
2317 					timeout * 10);
2318 			/* there is nothing much we can do here */
2319 			break;
2320 		}
2321 		msleep(10);
2322 	}
2323 
2324 	/* reset port resource registrations */
2325 	rc = lpfc_selective_reset(phba);
2326 	phba->pport->fc_myDID = 0;
2327 
2328 loopback_mode_end_exit:
2329 	/* make return code available to userspace */
2330 	bsg_reply->result = rc;
2331 	/* complete the job back to userspace if no error */
2332 	if (rc == 0)
2333 		bsg_job_done(job, bsg_reply->result,
2334 			       bsg_reply->reply_payload_rcv_len);
2335 	return rc;
2336 }
2337 
2338 /**
2339  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
2340  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
2341  *
2342  * This function is to perform SLI4 diag link test request from the user
2343  * applicaiton.
2344  */
2345 static int
2346 lpfc_sli4_bsg_link_diag_test(struct bsg_job *job)
2347 {
2348 	struct fc_bsg_request *bsg_request = job->request;
2349 	struct fc_bsg_reply *bsg_reply = job->reply;
2350 	struct Scsi_Host *shost;
2351 	struct lpfc_vport *vport;
2352 	struct lpfc_hba *phba;
2353 	LPFC_MBOXQ_t *pmboxq;
2354 	struct sli4_link_diag *link_diag_test_cmd;
2355 	uint32_t req_len, alloc_len;
2356 	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
2357 	union lpfc_sli4_cfg_shdr *shdr;
2358 	uint32_t shdr_status, shdr_add_status;
2359 	struct diag_status *diag_status_reply;
2360 	int mbxstatus, rc = -ENODEV, rc1 = 0;
2361 
2362 	shost = fc_bsg_to_shost(job);
2363 	if (!shost)
2364 		goto job_error;
2365 
2366 	vport = shost_priv(shost);
2367 	if (!vport)
2368 		goto job_error;
2369 
2370 	phba = vport->phba;
2371 	if (!phba)
2372 		goto job_error;
2373 
2374 
2375 	if (phba->sli_rev < LPFC_SLI_REV4)
2376 		goto job_error;
2377 
2378 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
2379 	    LPFC_SLI_INTF_IF_TYPE_2)
2380 		goto job_error;
2381 
2382 	if (job->request_len < sizeof(struct fc_bsg_request) +
2383 	    sizeof(struct sli4_link_diag)) {
2384 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2385 				"3013 Received LINK DIAG TEST request "
2386 				" size:%d below the minimum size:%d\n",
2387 				job->request_len,
2388 				(int)(sizeof(struct fc_bsg_request) +
2389 				sizeof(struct sli4_link_diag)));
2390 		rc = -EINVAL;
2391 		goto job_error;
2392 	}
2393 
2394 	rc = lpfc_bsg_diag_mode_enter(phba);
2395 	if (rc)
2396 		goto job_error;
2397 
2398 	link_diag_test_cmd = (struct sli4_link_diag *)
2399 			 bsg_request->rqst_data.h_vendor.vendor_cmd;
2400 
2401 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
2402 
2403 	if (rc)
2404 		goto job_error;
2405 
2406 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2407 	if (!pmboxq) {
2408 		rc = -ENOMEM;
2409 		goto link_diag_test_exit;
2410 	}
2411 
2412 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2413 		   sizeof(struct lpfc_sli4_cfg_mhdr));
2414 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2415 				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2416 				     req_len, LPFC_SLI4_MBX_EMBED);
2417 	if (alloc_len != req_len) {
2418 		rc = -ENOMEM;
2419 		goto link_diag_test_exit;
2420 	}
2421 
2422 	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2423 	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2424 	       phba->sli4_hba.lnk_info.lnk_no);
2425 	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2426 	       phba->sli4_hba.lnk_info.lnk_tp);
2427 	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2428 	       link_diag_test_cmd->test_id);
2429 	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2430 	       link_diag_test_cmd->loops);
2431 	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2432 	       link_diag_test_cmd->test_version);
2433 	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2434 	       link_diag_test_cmd->error_action);
2435 
2436 	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2437 
2438 	shdr = (union lpfc_sli4_cfg_shdr *)
2439 		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2440 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2441 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2442 	if (shdr_status || shdr_add_status || mbxstatus) {
2443 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2444 				"3010 Run link diag test mailbox failed with "
2445 				"mbx_status x%x status x%x, add_status x%x\n",
2446 				mbxstatus, shdr_status, shdr_add_status);
2447 	}
2448 
2449 	diag_status_reply = (struct diag_status *)
2450 			    bsg_reply->reply_data.vendor_reply.vendor_rsp;
2451 
2452 	if (job->reply_len < sizeof(*bsg_reply) + sizeof(*diag_status_reply)) {
2453 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2454 				"3012 Received Run link diag test reply "
2455 				"below minimum size (%d): reply_len:%d\n",
2456 				(int)(sizeof(*bsg_reply) +
2457 				sizeof(*diag_status_reply)),
2458 				job->reply_len);
2459 		rc = -EINVAL;
2460 		goto job_error;
2461 	}
2462 
2463 	diag_status_reply->mbox_status = mbxstatus;
2464 	diag_status_reply->shdr_status = shdr_status;
2465 	diag_status_reply->shdr_add_status = shdr_add_status;
2466 
2467 link_diag_test_exit:
2468 	rc1 = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2469 
2470 	if (pmboxq)
2471 		mempool_free(pmboxq, phba->mbox_mem_pool);
2472 
2473 	lpfc_bsg_diag_mode_exit(phba);
2474 
2475 job_error:
2476 	/* make error code available to userspace */
2477 	if (rc1 && !rc)
2478 		rc = rc1;
2479 	bsg_reply->result = rc;
2480 	/* complete the job back to userspace if no error */
2481 	if (rc == 0)
2482 		bsg_job_done(job, bsg_reply->result,
2483 			       bsg_reply->reply_payload_rcv_len);
2484 	return rc;
2485 }
2486 
2487 /**
2488  * lpfcdiag_loop_self_reg - obtains a remote port login id
2489  * @phba: Pointer to HBA context object
2490  * @rpi: Pointer to a remote port login id
2491  *
2492  * This function obtains a remote port login id so the diag loopback test
2493  * can send and receive its own unsolicited CT command.
2494  **/
2495 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2496 {
2497 	LPFC_MBOXQ_t *mbox;
2498 	struct lpfc_dmabuf *dmabuff;
2499 	int status;
2500 
2501 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2502 	if (!mbox)
2503 		return -ENOMEM;
2504 
2505 	if (phba->sli_rev < LPFC_SLI_REV4)
2506 		status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2507 				(uint8_t *)&phba->pport->fc_sparam,
2508 				mbox, *rpi);
2509 	else {
2510 		*rpi = lpfc_sli4_alloc_rpi(phba);
2511 		if (*rpi == LPFC_RPI_ALLOC_ERROR) {
2512 			mempool_free(mbox, phba->mbox_mem_pool);
2513 			return -EBUSY;
2514 		}
2515 		status = lpfc_reg_rpi(phba, phba->pport->vpi,
2516 				phba->pport->fc_myDID,
2517 				(uint8_t *)&phba->pport->fc_sparam,
2518 				mbox, *rpi);
2519 	}
2520 
2521 	if (status) {
2522 		mempool_free(mbox, phba->mbox_mem_pool);
2523 		if (phba->sli_rev == LPFC_SLI_REV4)
2524 			lpfc_sli4_free_rpi(phba, *rpi);
2525 		return -ENOMEM;
2526 	}
2527 
2528 	dmabuff = mbox->ctx_buf;
2529 	mbox->ctx_buf = NULL;
2530 	mbox->ctx_ndlp = NULL;
2531 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2532 
2533 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2534 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2535 		kfree(dmabuff);
2536 		if (status != MBX_TIMEOUT)
2537 			mempool_free(mbox, phba->mbox_mem_pool);
2538 		if (phba->sli_rev == LPFC_SLI_REV4)
2539 			lpfc_sli4_free_rpi(phba, *rpi);
2540 		return -ENODEV;
2541 	}
2542 
2543 	if (phba->sli_rev < LPFC_SLI_REV4)
2544 		*rpi = mbox->u.mb.un.varWords[0];
2545 
2546 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2547 	kfree(dmabuff);
2548 	mempool_free(mbox, phba->mbox_mem_pool);
2549 	return 0;
2550 }
2551 
2552 /**
2553  * lpfcdiag_loop_self_unreg - unregs from the rpi
2554  * @phba: Pointer to HBA context object
2555  * @rpi: Remote port login id
2556  *
2557  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2558  **/
2559 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2560 {
2561 	LPFC_MBOXQ_t *mbox;
2562 	int status;
2563 
2564 	/* Allocate mboxq structure */
2565 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2566 	if (mbox == NULL)
2567 		return -ENOMEM;
2568 
2569 	if (phba->sli_rev < LPFC_SLI_REV4)
2570 		lpfc_unreg_login(phba, 0, rpi, mbox);
2571 	else
2572 		lpfc_unreg_login(phba, phba->pport->vpi,
2573 				 phba->sli4_hba.rpi_ids[rpi], mbox);
2574 
2575 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2576 
2577 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2578 		if (status != MBX_TIMEOUT)
2579 			mempool_free(mbox, phba->mbox_mem_pool);
2580 		return -EIO;
2581 	}
2582 	mempool_free(mbox, phba->mbox_mem_pool);
2583 	if (phba->sli_rev == LPFC_SLI_REV4)
2584 		lpfc_sli4_free_rpi(phba, rpi);
2585 	return 0;
2586 }
2587 
2588 /**
2589  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2590  * @phba: Pointer to HBA context object
2591  * @rpi: Remote port login id
2592  * @txxri: Pointer to transmit exchange id
2593  * @rxxri: Pointer to response exchabge id
2594  *
2595  * This function obtains the transmit and receive ids required to send
2596  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2597  * flags are used to the unsolicited response handler is able to process
2598  * the ct command sent on the same port.
2599  **/
2600 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2601 			 uint16_t *txxri, uint16_t * rxxri)
2602 {
2603 	struct lpfc_bsg_event *evt;
2604 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2605 	struct lpfc_dmabuf *dmabuf;
2606 	struct ulp_bde64 *bpl = NULL;
2607 	struct lpfc_sli_ct_request *ctreq = NULL;
2608 	int ret_val = 0;
2609 	int time_left;
2610 	int iocb_stat = IOCB_SUCCESS;
2611 	unsigned long flags;
2612 	u32 status;
2613 
2614 	*txxri = 0;
2615 	*rxxri = 0;
2616 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2617 				SLI_CT_ELX_LOOPBACK);
2618 	if (!evt)
2619 		return -ENOMEM;
2620 
2621 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2622 	list_add(&evt->node, &phba->ct_ev_waiters);
2623 	lpfc_bsg_event_ref(evt);
2624 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2625 
2626 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2627 	rspiocbq = lpfc_sli_get_iocbq(phba);
2628 
2629 	dmabuf = kmalloc_obj(struct lpfc_dmabuf);
2630 	if (dmabuf) {
2631 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2632 		if (dmabuf->virt) {
2633 			INIT_LIST_HEAD(&dmabuf->list);
2634 			bpl = (struct ulp_bde64 *) dmabuf->virt;
2635 			memset(bpl, 0, sizeof(*bpl));
2636 			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2637 			bpl->addrHigh =
2638 				le32_to_cpu(putPaddrHigh(dmabuf->phys +
2639 					sizeof(*bpl)));
2640 			bpl->addrLow =
2641 				le32_to_cpu(putPaddrLow(dmabuf->phys +
2642 					sizeof(*bpl)));
2643 			bpl->tus.f.bdeFlags = 0;
2644 			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2645 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
2646 		}
2647 	}
2648 
2649 	if (cmdiocbq == NULL || rspiocbq == NULL ||
2650 	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2651 		dmabuf->virt == NULL) {
2652 		ret_val = -ENOMEM;
2653 		goto err_get_xri_exit;
2654 	}
2655 
2656 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2657 
2658 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2659 	ctreq->RevisionId.bits.InId = 0;
2660 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2661 	ctreq->FsSubType = 0;
2662 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2663 	ctreq->CommandResponse.bits.Size = 0;
2664 
2665 	cmdiocbq->bpl_dmabuf = dmabuf;
2666 	cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
2667 	cmdiocbq->vport = phba->pport;
2668 	cmdiocbq->cmd_cmpl = NULL;
2669 
2670 	lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, dmabuf, rpi, 0, 1,
2671 				 FC_RCTL_DD_SOL_CTL, 0, CMD_XMIT_SEQUENCE64_CR);
2672 
2673 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2674 					     rspiocbq, (phba->fc_ratov * 2)
2675 					     + LPFC_DRVR_TIMEOUT);
2676 
2677 	status = get_job_ulpstatus(phba, rspiocbq);
2678 	if (iocb_stat != IOCB_SUCCESS || status != IOCB_SUCCESS) {
2679 		ret_val = -EIO;
2680 		goto err_get_xri_exit;
2681 	}
2682 	*txxri = get_job_ulpcontext(phba, rspiocbq);
2683 
2684 	evt->waiting = 1;
2685 	evt->wait_time_stamp = jiffies;
2686 	time_left = wait_event_interruptible_timeout(
2687 		evt->wq, !list_empty(&evt->events_to_see),
2688 		secs_to_jiffies(phba->fc_ratov * 2 + LPFC_DRVR_TIMEOUT));
2689 	if (list_empty(&evt->events_to_see))
2690 		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2691 	else {
2692 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
2693 		list_move(evt->events_to_see.prev, &evt->events_to_get);
2694 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2695 		*rxxri = (list_entry(evt->events_to_get.prev,
2696 				     typeof(struct event_data),
2697 				     node))->immed_dat;
2698 	}
2699 	evt->waiting = 0;
2700 
2701 err_get_xri_exit:
2702 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2703 	lpfc_bsg_event_unref(evt); /* release ref */
2704 	lpfc_bsg_event_unref(evt); /* delete */
2705 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2706 
2707 	if (dmabuf) {
2708 		if (dmabuf->virt)
2709 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2710 		kfree(dmabuf);
2711 	}
2712 
2713 	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2714 		lpfc_sli_release_iocbq(phba, cmdiocbq);
2715 	if (rspiocbq)
2716 		lpfc_sli_release_iocbq(phba, rspiocbq);
2717 	return ret_val;
2718 }
2719 
2720 /**
2721  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2722  * @phba: Pointer to HBA context object
2723  *
2724  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and
2725  * returns the pointer to the buffer.
2726  **/
2727 static struct lpfc_dmabuf *
2728 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2729 {
2730 	struct lpfc_dmabuf *dmabuf;
2731 	struct pci_dev *pcidev = phba->pcidev;
2732 
2733 	/* allocate dma buffer struct */
2734 	dmabuf = kmalloc_obj(struct lpfc_dmabuf);
2735 	if (!dmabuf)
2736 		return NULL;
2737 
2738 	INIT_LIST_HEAD(&dmabuf->list);
2739 
2740 	/* now, allocate dma buffer */
2741 	dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2742 					  &(dmabuf->phys), GFP_KERNEL);
2743 
2744 	if (!dmabuf->virt) {
2745 		kfree(dmabuf);
2746 		return NULL;
2747 	}
2748 
2749 	return dmabuf;
2750 }
2751 
2752 /**
2753  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2754  * @phba: Pointer to HBA context object.
2755  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2756  *
2757  * This routine just simply frees a dma buffer and its associated buffer
2758  * descriptor referred by @dmabuf.
2759  **/
2760 static void
2761 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2762 {
2763 	struct pci_dev *pcidev = phba->pcidev;
2764 
2765 	if (!dmabuf)
2766 		return;
2767 
2768 	if (dmabuf->virt)
2769 		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2770 				  dmabuf->virt, dmabuf->phys);
2771 	kfree(dmabuf);
2772 	return;
2773 }
2774 
2775 /**
2776  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2777  * @phba: Pointer to HBA context object.
2778  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2779  *
2780  * This routine just simply frees all dma buffers and their associated buffer
2781  * descriptors referred by @dmabuf_list.
2782  **/
2783 static void
2784 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2785 			    struct list_head *dmabuf_list)
2786 {
2787 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2788 
2789 	if (list_empty(dmabuf_list))
2790 		return;
2791 
2792 	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2793 		list_del_init(&dmabuf->list);
2794 		lpfc_bsg_dma_page_free(phba, dmabuf);
2795 	}
2796 	return;
2797 }
2798 
2799 /**
2800  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2801  * @phba: Pointer to HBA context object
2802  * @bpl: Pointer to 64 bit bde structure
2803  * @size: Number of bytes to process
2804  * @nocopydata: Flag to copy user data into the allocated buffer
2805  *
2806  * This function allocates page size buffers and populates an lpfc_dmabufext.
2807  * If allowed the user data pointed to with indataptr is copied into the kernel
2808  * memory. The chained list of page size buffers is returned.
2809  **/
2810 static struct lpfc_dmabufext *
2811 diag_cmd_data_alloc(struct lpfc_hba *phba,
2812 		   struct ulp_bde64 *bpl, uint32_t size,
2813 		   int nocopydata)
2814 {
2815 	struct lpfc_dmabufext *mlist = NULL;
2816 	struct lpfc_dmabufext *dmp;
2817 	int cnt, offset = 0, i = 0;
2818 	struct pci_dev *pcidev;
2819 
2820 	pcidev = phba->pcidev;
2821 
2822 	while (size) {
2823 		/* We get chunks of 4K */
2824 		if (size > BUF_SZ_4K)
2825 			cnt = BUF_SZ_4K;
2826 		else
2827 			cnt = size;
2828 
2829 		/* allocate struct lpfc_dmabufext buffer header */
2830 		dmp = kmalloc_obj(struct lpfc_dmabufext);
2831 		if (!dmp)
2832 			goto out;
2833 
2834 		INIT_LIST_HEAD(&dmp->dma.list);
2835 
2836 		/* Queue it to a linked list */
2837 		if (mlist)
2838 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
2839 		else
2840 			mlist = dmp;
2841 
2842 		/* allocate buffer */
2843 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2844 						   cnt,
2845 						   &(dmp->dma.phys),
2846 						   GFP_KERNEL);
2847 
2848 		if (!dmp->dma.virt)
2849 			goto out;
2850 
2851 		dmp->size = cnt;
2852 
2853 		if (nocopydata) {
2854 			bpl->tus.f.bdeFlags = 0;
2855 		} else {
2856 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
2857 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2858 		}
2859 
2860 		/* build buffer ptr list for IOCB */
2861 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2862 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2863 		bpl->tus.f.bdeSize = (ushort) cnt;
2864 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
2865 		bpl++;
2866 
2867 		i++;
2868 		offset += cnt;
2869 		size -= cnt;
2870 	}
2871 
2872 	if (mlist) {
2873 		mlist->flag = i;
2874 		return mlist;
2875 	}
2876 out:
2877 	diag_cmd_data_free(phba, mlist);
2878 	return NULL;
2879 }
2880 
2881 /**
2882  * lpfcdiag_sli3_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2883  * @phba: Pointer to HBA context object
2884  * @rxxri: Receive exchange id
2885  * @len: Number of data bytes
2886  *
2887  * This function allocates and posts a data buffer of sufficient size to receive
2888  * an unsolicited CT command.
2889  **/
2890 static int lpfcdiag_sli3_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2891 					  size_t len)
2892 {
2893 	struct lpfc_sli_ring *pring;
2894 	struct lpfc_iocbq *cmdiocbq;
2895 	IOCB_t *cmd = NULL;
2896 	struct list_head head, *curr, *next;
2897 	struct lpfc_dmabuf *rxbmp;
2898 	struct lpfc_dmabuf *dmp;
2899 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2900 	struct ulp_bde64 *rxbpl = NULL;
2901 	uint32_t num_bde;
2902 	struct lpfc_dmabufext *rxbuffer = NULL;
2903 	int ret_val = 0;
2904 	int iocb_stat;
2905 	int i = 0;
2906 
2907 	pring = lpfc_phba_elsring(phba);
2908 
2909 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2910 	rxbmp = kmalloc_obj(struct lpfc_dmabuf);
2911 	if (rxbmp != NULL) {
2912 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2913 		if (rxbmp->virt) {
2914 			INIT_LIST_HEAD(&rxbmp->list);
2915 			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2916 			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2917 		}
2918 	}
2919 
2920 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer || !pring) {
2921 		ret_val = -ENOMEM;
2922 		goto err_post_rxbufs_exit;
2923 	}
2924 
2925 	/* Queue buffers for the receive exchange */
2926 	num_bde = (uint32_t)rxbuffer->flag;
2927 	dmp = &rxbuffer->dma;
2928 	cmd = &cmdiocbq->iocb;
2929 	i = 0;
2930 
2931 	INIT_LIST_HEAD(&head);
2932 	list_add_tail(&head, &dmp->list);
2933 	list_for_each_safe(curr, next, &head) {
2934 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2935 		list_del(curr);
2936 
2937 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2938 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2939 			cmd->un.quexri64cx.buff.bde.addrHigh =
2940 				putPaddrHigh(mp[i]->phys);
2941 			cmd->un.quexri64cx.buff.bde.addrLow =
2942 				putPaddrLow(mp[i]->phys);
2943 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2944 				((struct lpfc_dmabufext *)mp[i])->size;
2945 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2946 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
2947 			cmd->ulpPU = 0;
2948 			cmd->ulpLe = 1;
2949 			cmd->ulpBdeCount = 1;
2950 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2951 
2952 		} else {
2953 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2954 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2955 			cmd->un.cont64[i].tus.f.bdeSize =
2956 				((struct lpfc_dmabufext *)mp[i])->size;
2957 			cmd->ulpBdeCount = ++i;
2958 
2959 			if ((--num_bde > 0) && (i < 2))
2960 				continue;
2961 
2962 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2963 			cmd->ulpLe = 1;
2964 		}
2965 
2966 		cmd->ulpClass = CLASS3;
2967 		cmd->ulpContext = rxxri;
2968 
2969 		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2970 						0);
2971 		if (iocb_stat == IOCB_ERROR) {
2972 			diag_cmd_data_free(phba,
2973 				(struct lpfc_dmabufext *)mp[0]);
2974 			if (mp[1])
2975 				diag_cmd_data_free(phba,
2976 					  (struct lpfc_dmabufext *)mp[1]);
2977 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2978 			ret_val = -EIO;
2979 			goto err_post_rxbufs_exit;
2980 		}
2981 
2982 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2983 		if (mp[1]) {
2984 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2985 			mp[1] = NULL;
2986 		}
2987 
2988 		/* The iocb was freed by lpfc_sli_issue_iocb */
2989 		cmdiocbq = lpfc_sli_get_iocbq(phba);
2990 		if (!cmdiocbq) {
2991 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2992 			ret_val = -EIO;
2993 			goto err_post_rxbufs_exit;
2994 		}
2995 		cmd = &cmdiocbq->iocb;
2996 		i = 0;
2997 	}
2998 	list_del(&head);
2999 
3000 err_post_rxbufs_exit:
3001 
3002 	if (rxbmp) {
3003 		if (rxbmp->virt)
3004 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
3005 		kfree(rxbmp);
3006 	}
3007 
3008 	if (cmdiocbq)
3009 		lpfc_sli_release_iocbq(phba, cmdiocbq);
3010 	return ret_val;
3011 }
3012 
3013 /**
3014  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
3015  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
3016  *
3017  * This function receives a user data buffer to be transmitted and received on
3018  * the same port, the link must be up and in loopback mode prior
3019  * to being called.
3020  * 1. A kernel buffer is allocated to copy the user data into.
3021  * 2. The port registers with "itself".
3022  * 3. The transmit and receive exchange ids are obtained.
3023  * 4. The receive exchange id is posted.
3024  * 5. A new els loopback event is created.
3025  * 6. The command and response iocbs are allocated.
3026  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
3027  *
3028  * This function is meant to be called n times while the port is in loopback
3029  * so it is the apps responsibility to issue a reset to take the port out
3030  * of loopback mode.
3031  **/
3032 static int
3033 lpfc_bsg_diag_loopback_run(struct bsg_job *job)
3034 {
3035 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3036 	struct fc_bsg_reply *bsg_reply = job->reply;
3037 	struct lpfc_hba *phba = vport->phba;
3038 	struct lpfc_bsg_event *evt;
3039 	struct event_data *evdat;
3040 	struct lpfc_sli *psli = &phba->sli;
3041 	uint32_t size;
3042 	uint32_t full_size;
3043 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
3044 	uint16_t rpi = 0;
3045 	struct lpfc_iocbq *cmdiocbq, *rspiocbq = NULL;
3046 	union lpfc_wqe128 *cmdwqe, *rspwqe;
3047 	struct lpfc_sli_ct_request *ctreq;
3048 	struct lpfc_dmabuf *txbmp;
3049 	struct ulp_bde64 *txbpl = NULL;
3050 	struct lpfc_dmabufext *txbuffer = NULL;
3051 	struct list_head head;
3052 	struct lpfc_dmabuf  *curr;
3053 	uint16_t txxri = 0, rxxri;
3054 	uint32_t num_bde;
3055 	uint8_t *ptr = NULL, *rx_databuf = NULL;
3056 	int rc = 0;
3057 	int time_left;
3058 	int iocb_stat = IOCB_SUCCESS;
3059 	unsigned long flags;
3060 	void *dataout = NULL;
3061 	uint32_t total_mem;
3062 
3063 	/* in case no data is returned return just the return code */
3064 	bsg_reply->reply_payload_rcv_len = 0;
3065 
3066 	if (job->request_len <
3067 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
3068 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3069 				"2739 Received DIAG TEST request below minimum "
3070 				"size\n");
3071 		rc = -EINVAL;
3072 		goto loopback_test_exit;
3073 	}
3074 
3075 	if (job->request_payload.payload_len !=
3076 		job->reply_payload.payload_len) {
3077 		rc = -EINVAL;
3078 		goto loopback_test_exit;
3079 	}
3080 
3081 	if ((phba->link_state == LPFC_HBA_ERROR) ||
3082 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
3083 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
3084 		rc = -EACCES;
3085 		goto loopback_test_exit;
3086 	}
3087 
3088 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
3089 		rc = -EACCES;
3090 		goto loopback_test_exit;
3091 	}
3092 
3093 	size = job->request_payload.payload_len;
3094 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
3095 
3096 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
3097 		rc = -ERANGE;
3098 		goto loopback_test_exit;
3099 	}
3100 
3101 	if (full_size >= BUF_SZ_4K) {
3102 		/*
3103 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
3104 		 * then we allocate 64k and re-use that buffer over and over to
3105 		 * xfer the whole block. This is because Linux kernel has a
3106 		 * problem allocating more than 120k of kernel space memory. Saw
3107 		 * problem with GET_FCPTARGETMAPPING...
3108 		 */
3109 		if (size <= (64 * 1024))
3110 			total_mem = full_size;
3111 		else
3112 			total_mem = 64 * 1024;
3113 	} else
3114 		/* Allocate memory for ioctl data */
3115 		total_mem = BUF_SZ_4K;
3116 
3117 	dataout = kmalloc(total_mem, GFP_KERNEL);
3118 	if (dataout == NULL) {
3119 		rc = -ENOMEM;
3120 		goto loopback_test_exit;
3121 	}
3122 
3123 	ptr = dataout;
3124 	ptr += ELX_LOOPBACK_HEADER_SZ;
3125 	sg_copy_to_buffer(job->request_payload.sg_list,
3126 				job->request_payload.sg_cnt,
3127 				ptr, size);
3128 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
3129 	if (rc)
3130 		goto loopback_test_exit;
3131 
3132 	if (phba->sli_rev < LPFC_SLI_REV4) {
3133 		rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
3134 		if (rc) {
3135 			lpfcdiag_loop_self_unreg(phba, rpi);
3136 			goto loopback_test_exit;
3137 		}
3138 
3139 		rc = lpfcdiag_sli3_loop_post_rxbufs(phba, rxxri, full_size);
3140 		if (rc) {
3141 			lpfcdiag_loop_self_unreg(phba, rpi);
3142 			goto loopback_test_exit;
3143 		}
3144 	}
3145 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
3146 				SLI_CT_ELX_LOOPBACK);
3147 	if (!evt) {
3148 		lpfcdiag_loop_self_unreg(phba, rpi);
3149 		rc = -ENOMEM;
3150 		goto loopback_test_exit;
3151 	}
3152 
3153 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3154 	list_add(&evt->node, &phba->ct_ev_waiters);
3155 	lpfc_bsg_event_ref(evt);
3156 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3157 
3158 	cmdiocbq = lpfc_sli_get_iocbq(phba);
3159 	if (phba->sli_rev < LPFC_SLI_REV4)
3160 		rspiocbq = lpfc_sli_get_iocbq(phba);
3161 	txbmp = kmalloc_obj(struct lpfc_dmabuf);
3162 
3163 	if (txbmp) {
3164 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
3165 		if (txbmp->virt) {
3166 			INIT_LIST_HEAD(&txbmp->list);
3167 			txbpl = (struct ulp_bde64 *) txbmp->virt;
3168 			txbuffer = diag_cmd_data_alloc(phba,
3169 							txbpl, full_size, 0);
3170 		}
3171 	}
3172 
3173 	if (!cmdiocbq || !txbmp || !txbpl || !txbuffer || !txbmp->virt) {
3174 		rc = -ENOMEM;
3175 		goto err_loopback_test_exit;
3176 	}
3177 	if ((phba->sli_rev < LPFC_SLI_REV4) && !rspiocbq) {
3178 		rc = -ENOMEM;
3179 		goto err_loopback_test_exit;
3180 	}
3181 
3182 	cmdwqe = &cmdiocbq->wqe;
3183 	memset(cmdwqe, 0, sizeof(*cmdwqe));
3184 	if (phba->sli_rev < LPFC_SLI_REV4) {
3185 		rspwqe = &rspiocbq->wqe;
3186 		memset(rspwqe, 0, sizeof(*rspwqe));
3187 	}
3188 
3189 	INIT_LIST_HEAD(&head);
3190 	list_add_tail(&head, &txbuffer->dma.list);
3191 	list_for_each_entry(curr, &head, list) {
3192 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
3193 		if (current_offset == 0) {
3194 			ctreq = curr->virt;
3195 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
3196 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
3197 			ctreq->RevisionId.bits.InId = 0;
3198 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
3199 			ctreq->FsSubType = 0;
3200 			ctreq->CommandResponse.bits.CmdRsp = cpu_to_be16(ELX_LOOPBACK_DATA);
3201 			ctreq->CommandResponse.bits.Size   = cpu_to_be16(size);
3202 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
3203 		} else
3204 			segment_offset = 0;
3205 
3206 		BUG_ON(segment_offset >= segment_len);
3207 		memcpy(curr->virt + segment_offset,
3208 			ptr + current_offset,
3209 			segment_len - segment_offset);
3210 
3211 		current_offset += segment_len - segment_offset;
3212 		BUG_ON(current_offset > size);
3213 	}
3214 	list_del(&head);
3215 
3216 	/* Build the XMIT_SEQUENCE iocb */
3217 	num_bde = (uint32_t)txbuffer->flag;
3218 
3219 	cmdiocbq->num_bdes = num_bde;
3220 	cmdiocbq->cmd_flag |= LPFC_IO_LIBDFC;
3221 	cmdiocbq->cmd_flag |= LPFC_IO_LOOPBACK;
3222 	if (phba->cfg_vmid_app_header)
3223 		cmdiocbq->cmd_flag |= LPFC_IO_VMID;
3224 
3225 	cmdiocbq->vport = phba->pport;
3226 	cmdiocbq->cmd_cmpl = NULL;
3227 	cmdiocbq->bpl_dmabuf = txbmp;
3228 
3229 	if (phba->sli_rev < LPFC_SLI_REV4) {
3230 		lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp, 0, txxri,
3231 					 num_bde, FC_RCTL_DD_UNSOL_CTL, 1,
3232 					 CMD_XMIT_SEQUENCE64_CX);
3233 
3234 	} else {
3235 		lpfc_sli_prep_xmit_seq64(phba, cmdiocbq, txbmp,
3236 					 phba->sli4_hba.rpi_ids[rpi], 0xffff,
3237 					 full_size, FC_RCTL_DD_UNSOL_CTL, 1,
3238 					 CMD_XMIT_SEQUENCE64_WQE);
3239 		cmdiocbq->sli4_xritag = NO_XRI;
3240 	}
3241 
3242 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
3243 					     rspiocbq, (phba->fc_ratov * 2) +
3244 					     LPFC_DRVR_TIMEOUT);
3245 	if (iocb_stat != IOCB_SUCCESS ||
3246 	    (phba->sli_rev < LPFC_SLI_REV4 &&
3247 	     (get_job_ulpstatus(phba, rspiocbq) != IOSTAT_SUCCESS))) {
3248 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3249 				"3126 Failed loopback test issue iocb: "
3250 				"iocb_stat:x%x\n", iocb_stat);
3251 		rc = -EIO;
3252 		goto err_loopback_test_exit;
3253 	}
3254 
3255 	evt->waiting = 1;
3256 	time_left = wait_event_interruptible_timeout(
3257 		evt->wq, !list_empty(&evt->events_to_see),
3258 		secs_to_jiffies(phba->fc_ratov * 2 + LPFC_DRVR_TIMEOUT));
3259 	evt->waiting = 0;
3260 	if (list_empty(&evt->events_to_see)) {
3261 		rc = (time_left) ? -EINTR : -ETIMEDOUT;
3262 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3263 				"3125 Not receiving unsolicited event, "
3264 				"rc:x%x\n", rc);
3265 	} else {
3266 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
3267 		list_move(evt->events_to_see.prev, &evt->events_to_get);
3268 		evdat = list_entry(evt->events_to_get.prev,
3269 				   typeof(*evdat), node);
3270 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3271 		rx_databuf = evdat->data;
3272 		if (evdat->len != full_size) {
3273 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3274 				"1603 Loopback test did not receive expected "
3275 				"data length. actual length 0x%x expected "
3276 				"length 0x%x\n",
3277 				evdat->len, full_size);
3278 			rc = -EIO;
3279 		} else if (rx_databuf == NULL)
3280 			rc = -EIO;
3281 		else {
3282 			rc = IOCB_SUCCESS;
3283 			/* skip over elx loopback header */
3284 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
3285 			bsg_reply->reply_payload_rcv_len =
3286 				sg_copy_from_buffer(job->reply_payload.sg_list,
3287 						    job->reply_payload.sg_cnt,
3288 						    rx_databuf, size);
3289 			bsg_reply->reply_payload_rcv_len = size;
3290 		}
3291 	}
3292 
3293 err_loopback_test_exit:
3294 	lpfcdiag_loop_self_unreg(phba, rpi);
3295 
3296 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3297 	lpfc_bsg_event_unref(evt); /* release ref */
3298 	lpfc_bsg_event_unref(evt); /* delete */
3299 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3300 
3301 	if ((cmdiocbq != NULL) && (iocb_stat != IOCB_TIMEDOUT))
3302 		lpfc_sli_release_iocbq(phba, cmdiocbq);
3303 
3304 	if (rspiocbq != NULL)
3305 		lpfc_sli_release_iocbq(phba, rspiocbq);
3306 
3307 	if (txbmp != NULL) {
3308 		if (txbpl != NULL) {
3309 			if (txbuffer != NULL)
3310 				diag_cmd_data_free(phba, txbuffer);
3311 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
3312 		}
3313 		kfree(txbmp);
3314 	}
3315 
3316 loopback_test_exit:
3317 	kfree(dataout);
3318 	/* make error code available to userspace */
3319 	bsg_reply->result = rc;
3320 	job->dd_data = NULL;
3321 	/* complete the job back to userspace if no error */
3322 	if (rc == IOCB_SUCCESS)
3323 		bsg_job_done(job, bsg_reply->result,
3324 			       bsg_reply->reply_payload_rcv_len);
3325 	return rc;
3326 }
3327 
3328 /**
3329  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
3330  * @job: GET_DFC_REV fc_bsg_job
3331  **/
3332 static int
3333 lpfc_bsg_get_dfc_rev(struct bsg_job *job)
3334 {
3335 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
3336 	struct fc_bsg_reply *bsg_reply = job->reply;
3337 	struct lpfc_hba *phba = vport->phba;
3338 	struct get_mgmt_rev_reply *event_reply;
3339 	int rc = 0;
3340 
3341 	if (job->request_len <
3342 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
3343 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3344 				"2740 Received GET_DFC_REV request below "
3345 				"minimum size\n");
3346 		rc = -EINVAL;
3347 		goto job_error;
3348 	}
3349 
3350 	event_reply = (struct get_mgmt_rev_reply *)
3351 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
3352 
3353 	if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
3354 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3355 				"2741 Received GET_DFC_REV reply below "
3356 				"minimum size\n");
3357 		rc = -EINVAL;
3358 		goto job_error;
3359 	}
3360 
3361 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
3362 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
3363 job_error:
3364 	bsg_reply->result = rc;
3365 	if (rc == 0)
3366 		bsg_job_done(job, bsg_reply->result,
3367 			       bsg_reply->reply_payload_rcv_len);
3368 	return rc;
3369 }
3370 
3371 /**
3372  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
3373  * @phba: Pointer to HBA context object.
3374  * @pmboxq: Pointer to mailbox command.
3375  *
3376  * This is completion handler function for mailbox commands issued from
3377  * lpfc_bsg_issue_mbox function. This function is called by the
3378  * mailbox event handler function with no lock held. This function
3379  * will wake up thread waiting on the wait queue pointed by dd_data
3380  * of the mailbox.
3381  **/
3382 static void
3383 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3384 {
3385 	struct bsg_job_data *dd_data;
3386 	struct fc_bsg_reply *bsg_reply;
3387 	struct bsg_job *job;
3388 	uint32_t size;
3389 	unsigned long flags;
3390 	uint8_t *pmb, *pmb_buf;
3391 
3392 	dd_data = pmboxq->ctx_u.dd_data;
3393 
3394 	/*
3395 	 * The outgoing buffer is readily referred from the dma buffer,
3396 	 * just need to get header part from mailboxq structure.
3397 	 */
3398 	pmb = (uint8_t *)&pmboxq->u.mb;
3399 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3400 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3401 
3402 	/* Determine if job has been aborted */
3403 
3404 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3405 	job = dd_data->set_job;
3406 	if (job) {
3407 		/* Prevent timeout handling from trying to abort job  */
3408 		job->dd_data = NULL;
3409 	}
3410 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3411 
3412 	/* Copy the mailbox data to the job if it is still active */
3413 
3414 	if (job) {
3415 		bsg_reply = job->reply;
3416 		size = job->reply_payload.payload_len;
3417 		bsg_reply->reply_payload_rcv_len =
3418 			sg_copy_from_buffer(job->reply_payload.sg_list,
3419 					    job->reply_payload.sg_cnt,
3420 					    pmb_buf, size);
3421 	}
3422 
3423 	dd_data->set_job = NULL;
3424 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3425 	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3426 	kfree(dd_data);
3427 
3428 	/* Complete the job if the job is still active */
3429 
3430 	if (job) {
3431 		bsg_reply->result = 0;
3432 		bsg_job_done(job, bsg_reply->result,
3433 			       bsg_reply->reply_payload_rcv_len);
3434 	}
3435 	return;
3436 }
3437 
3438 /**
3439  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3440  * @phba: Pointer to HBA context object.
3441  * @mb: Pointer to a mailbox object.
3442  * @vport: Pointer to a vport object.
3443  *
3444  * Some commands require the port to be offline, some may not be called from
3445  * the application.
3446  **/
3447 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3448 	MAILBOX_t *mb, struct lpfc_vport *vport)
3449 {
3450 	/* return negative error values for bsg job */
3451 	switch (mb->mbxCommand) {
3452 	/* Offline only */
3453 	case MBX_INIT_LINK:
3454 	case MBX_DOWN_LINK:
3455 	case MBX_CONFIG_LINK:
3456 	case MBX_CONFIG_RING:
3457 	case MBX_RESET_RING:
3458 	case MBX_UNREG_LOGIN:
3459 	case MBX_CLEAR_LA:
3460 	case MBX_DUMP_CONTEXT:
3461 	case MBX_RUN_DIAGS:
3462 	case MBX_RESTART:
3463 	case MBX_SET_MASK:
3464 		if (!test_bit(FC_OFFLINE_MODE, &vport->fc_flag)) {
3465 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3466 				"2743 Command 0x%x is illegal in on-line "
3467 				"state\n",
3468 				mb->mbxCommand);
3469 			return -EPERM;
3470 		}
3471 		break;
3472 	case MBX_WRITE_NV:
3473 	case MBX_WRITE_VPARMS:
3474 	case MBX_LOAD_SM:
3475 	case MBX_READ_NV:
3476 	case MBX_READ_CONFIG:
3477 	case MBX_READ_RCONFIG:
3478 	case MBX_READ_STATUS:
3479 	case MBX_READ_XRI:
3480 	case MBX_READ_REV:
3481 	case MBX_READ_LNK_STAT:
3482 	case MBX_DUMP_MEMORY:
3483 	case MBX_DOWN_LOAD:
3484 	case MBX_UPDATE_CFG:
3485 	case MBX_KILL_BOARD:
3486 	case MBX_READ_TOPOLOGY:
3487 	case MBX_LOAD_AREA:
3488 	case MBX_LOAD_EXP_ROM:
3489 	case MBX_BEACON:
3490 	case MBX_DEL_LD_ENTRY:
3491 	case MBX_SET_DEBUG:
3492 	case MBX_WRITE_WWN:
3493 	case MBX_SLI4_CONFIG:
3494 	case MBX_READ_EVENT_LOG:
3495 	case MBX_READ_EVENT_LOG_STATUS:
3496 	case MBX_WRITE_EVENT_LOG:
3497 	case MBX_PORT_CAPABILITIES:
3498 	case MBX_PORT_IOV_CONTROL:
3499 	case MBX_RUN_BIU_DIAG64:
3500 		break;
3501 	case MBX_SET_VARIABLE:
3502 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3503 			"1226 mbox: set_variable 0x%x, 0x%x\n",
3504 			mb->un.varWords[0],
3505 			mb->un.varWords[1]);
3506 		break;
3507 	case MBX_READ_SPARM64:
3508 	case MBX_REG_LOGIN:
3509 	case MBX_REG_LOGIN64:
3510 	case MBX_CONFIG_PORT:
3511 	case MBX_RUN_BIU_DIAG:
3512 	default:
3513 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3514 			"2742 Unknown Command 0x%x\n",
3515 			mb->mbxCommand);
3516 		return -EPERM;
3517 	}
3518 
3519 	return 0; /* ok */
3520 }
3521 
3522 /**
3523  * lpfc_bsg_mbox_ext_session_reset - clean up context of multi-buffer mbox session
3524  * @phba: Pointer to HBA context object.
3525  *
3526  * This is routine clean up and reset BSG handling of multi-buffer mbox
3527  * command session.
3528  **/
3529 static void
3530 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3531 {
3532 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3533 		return;
3534 
3535 	/* free all memory, including dma buffers */
3536 	lpfc_bsg_dma_page_list_free(phba,
3537 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3538 	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3539 	/* multi-buffer write mailbox command pass-through complete */
3540 	memset((char *)&phba->mbox_ext_buf_ctx, 0,
3541 	       sizeof(struct lpfc_mbox_ext_buf_ctx));
3542 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3543 
3544 	return;
3545 }
3546 
3547 /**
3548  * lpfc_rd_obj_emb0_handle_job - Handles completion for non-embedded
3549  *                               READ_OBJECT_V0 mailbox commands
3550  * @phba: pointer to lpfc_hba data struct
3551  * @pmb_buf: pointer to mailbox buffer
3552  * @sli_cfg_mbx: pointer to SLI_CONFIG mailbox memory region
3553  * @job: pointer to bsg_job struct
3554  * @bsg_reply: point to bsg_reply struct
3555  *
3556  * Given a non-embedded READ_OBJECT_V0's HBD_CNT, this routine copies
3557  * a READ_OBJECT_V0 mailbox command's read data payload into a bsg_job
3558  * structure for passing back to application layer.
3559  *
3560  * Return codes
3561  *      0 - successful
3562  *      -EINVAL - invalid HBD_CNT
3563  *      -ENODEV - pointer to bsg_job struct is NULL
3564  **/
3565 static int
3566 lpfc_rd_obj_emb0_handle_job(struct lpfc_hba *phba, u8 *pmb_buf,
3567 			    struct lpfc_sli_config_mbox *sli_cfg_mbx,
3568 			    struct bsg_job *job,
3569 			    struct fc_bsg_reply *bsg_reply)
3570 {
3571 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3572 	struct lpfc_sli_config_emb0_subsys *emb0_subsys;
3573 	u32 hbd_cnt;
3574 	u32 dma_buf_len;
3575 	u8 i = 0;
3576 	size_t extra_bytes;
3577 	off_t skip = 0;
3578 
3579 	if (!job) {
3580 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3581 				"2496 NULL job\n");
3582 		return -ENODEV;
3583 	}
3584 
3585 	if (!bsg_reply) {
3586 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3587 				"2498 NULL bsg_reply\n");
3588 		return -ENODEV;
3589 	}
3590 
3591 	emb0_subsys = &sli_cfg_mbx->un.sli_config_emb0_subsys;
3592 
3593 	hbd_cnt = bsg_bf_get(lpfc_emb0_subcmnd_rd_obj_hbd_cnt,
3594 			     emb0_subsys);
3595 
3596 	/* Calculate where the read object's read data payload is located based
3597 	 * on HBD count scheme.
3598 	 */
3599 	if (hbd_cnt >= rd_obj_scheme[0].min_hbd_cnt &&
3600 	    hbd_cnt <= rd_obj_scheme[0].max_hbd_cnt) {
3601 		skip = rd_obj_scheme[0].payload_word_offset * 4;
3602 	} else if (hbd_cnt >= rd_obj_scheme[1].min_hbd_cnt &&
3603 		   hbd_cnt <= rd_obj_scheme[1].max_hbd_cnt) {
3604 		skip = rd_obj_scheme[1].payload_word_offset * 4;
3605 	} else {
3606 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3607 				"2497 bad hbd_count 0x%08x\n",
3608 				hbd_cnt);
3609 		return -EINVAL;
3610 	}
3611 
3612 	/* Copy SLI_CONFIG command and READ_OBJECT response first */
3613 	bsg_reply->reply_payload_rcv_len =
3614 		sg_copy_from_buffer(job->reply_payload.sg_list,
3615 				    job->reply_payload.sg_cnt,
3616 				    pmb_buf, skip);
3617 
3618 	/* Copy data from hbds */
3619 	list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3620 				 &phba->mbox_ext_buf_ctx.ext_dmabuf_list,
3621 				 list) {
3622 		dma_buf_len = emb0_subsys->hbd[i].buf_len;
3623 
3624 		/* Use sg_copy_buffer to specify a skip offset */
3625 		extra_bytes = sg_copy_buffer(job->reply_payload.sg_list,
3626 					     job->reply_payload.sg_cnt,
3627 					     curr_dmabuf->virt,
3628 					     dma_buf_len, skip, false);
3629 
3630 		bsg_reply->reply_payload_rcv_len += extra_bytes;
3631 
3632 		skip += extra_bytes;
3633 
3634 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3635 				"2499 copied hbd[%d] "
3636 				"0x%zx bytes\n",
3637 				i, extra_bytes);
3638 		i++;
3639 	}
3640 
3641 	return 0;
3642 }
3643 
3644 /**
3645  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3646  * @phba: Pointer to HBA context object.
3647  * @pmboxq: Pointer to mailbox command.
3648  *
3649  * This is routine handles BSG job for mailbox commands completions with
3650  * multiple external buffers.
3651  **/
3652 static struct bsg_job *
3653 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3654 {
3655 	struct bsg_job_data *dd_data;
3656 	struct bsg_job *job;
3657 	struct fc_bsg_reply *bsg_reply = NULL;
3658 	uint8_t *pmb, *pmb_buf;
3659 	unsigned long flags;
3660 	u32 size, opcode;
3661 	int rc = 0;
3662 	struct lpfc_dmabuf *dmabuf;
3663 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3664 	uint8_t *pmbx;
3665 
3666 	dd_data = pmboxq->ctx_u.dd_data;
3667 
3668 	/* Determine if job has been aborted */
3669 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3670 	job = dd_data->set_job;
3671 	if (job) {
3672 		bsg_reply = job->reply;
3673 		/* Prevent timeout handling from trying to abort job  */
3674 		job->dd_data = NULL;
3675 	}
3676 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3677 
3678 	/*
3679 	 * The outgoing buffer is readily referred from the dma buffer,
3680 	 * just need to get header part from mailboxq structure.
3681 	 */
3682 
3683 	pmb = (uint8_t *)&pmboxq->u.mb;
3684 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3685 	/* Copy the byte swapped response mailbox back to the user */
3686 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3687 	/* if there is any non-embedded extended data copy that too */
3688 	dmabuf = phba->mbox_ext_buf_ctx.mbx_dmabuf;
3689 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3690 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3691 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3692 		pmbx = (uint8_t *)dmabuf->virt;
3693 		/* byte swap the extended data following the mailbox command */
3694 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
3695 			&pmbx[sizeof(MAILBOX_t)],
3696 			sli_cfg_mbx->un.sli_config_emb0_subsys.mse[0].buf_len);
3697 
3698 		/* Special handling for non-embedded READ_OBJECT */
3699 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
3700 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
3701 		switch (opcode) {
3702 		case COMN_OPCODE_READ_OBJECT:
3703 			if (job) {
3704 				rc = lpfc_rd_obj_emb0_handle_job(phba, pmb_buf,
3705 								 sli_cfg_mbx,
3706 								 job,
3707 								 bsg_reply);
3708 				bsg_reply->result = rc;
3709 				goto done;
3710 			}
3711 			break;
3712 		default:
3713 			break;
3714 		}
3715 	}
3716 
3717 	/* Complete the job if the job is still active */
3718 
3719 	if (job) {
3720 		size = job->reply_payload.payload_len;
3721 		bsg_reply->reply_payload_rcv_len =
3722 			sg_copy_from_buffer(job->reply_payload.sg_list,
3723 					    job->reply_payload.sg_cnt,
3724 					    pmb_buf, size);
3725 
3726 		/* result for successful */
3727 		bsg_reply->result = 0;
3728 done:
3729 
3730 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3731 				"2937 SLI_CONFIG ext-buffer mailbox command "
3732 				"(x%x/x%x) complete bsg job done, bsize:%d\n",
3733 				phba->mbox_ext_buf_ctx.nembType,
3734 				phba->mbox_ext_buf_ctx.mboxType,
3735 				job->reply_payload.payload_len);
3736 		lpfc_idiag_mbxacc_dump_bsg_mbox(phba,
3737 					phba->mbox_ext_buf_ctx.nembType,
3738 					phba->mbox_ext_buf_ctx.mboxType,
3739 					dma_ebuf, sta_pos_addr,
3740 					phba->mbox_ext_buf_ctx.mbx_dmabuf, 0);
3741 	} else {
3742 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3743 				"2938 SLI_CONFIG ext-buffer mailbox "
3744 				"command (x%x/x%x) failure, rc:x%x\n",
3745 				phba->mbox_ext_buf_ctx.nembType,
3746 				phba->mbox_ext_buf_ctx.mboxType, rc);
3747 	}
3748 
3749 
3750 	/* state change */
3751 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3752 	kfree(dd_data);
3753 	return job;
3754 }
3755 
3756 /**
3757  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3758  * @phba: Pointer to HBA context object.
3759  * @pmboxq: Pointer to mailbox command.
3760  *
3761  * This is completion handler function for mailbox read commands with multiple
3762  * external buffers.
3763  **/
3764 static void
3765 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3766 {
3767 	struct bsg_job *job;
3768 	struct fc_bsg_reply *bsg_reply;
3769 
3770 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3771 
3772 	/* handle the BSG job with mailbox command */
3773 	if (!job)
3774 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3775 
3776 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3777 			"2939 SLI_CONFIG ext-buffer rd mailbox command "
3778 			"complete, ctxState:x%x, mbxStatus:x%x\n",
3779 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3780 
3781 	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3782 		lpfc_bsg_mbox_ext_session_reset(phba);
3783 
3784 	/* free base driver mailbox structure memory */
3785 	mempool_free(pmboxq, phba->mbox_mem_pool);
3786 
3787 	/* if the job is still active, call job done */
3788 	if (job) {
3789 		bsg_reply = job->reply;
3790 		bsg_job_done(job, bsg_reply->result,
3791 			       bsg_reply->reply_payload_rcv_len);
3792 	}
3793 	return;
3794 }
3795 
3796 /**
3797  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3798  * @phba: Pointer to HBA context object.
3799  * @pmboxq: Pointer to mailbox command.
3800  *
3801  * This is completion handler function for mailbox write commands with multiple
3802  * external buffers.
3803  **/
3804 static void
3805 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3806 {
3807 	struct bsg_job *job;
3808 	struct fc_bsg_reply *bsg_reply;
3809 
3810 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3811 
3812 	/* handle the BSG job with the mailbox command */
3813 	if (!job)
3814 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3815 
3816 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3817 			"2940 SLI_CONFIG ext-buffer wr mailbox command "
3818 			"complete, ctxState:x%x, mbxStatus:x%x\n",
3819 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3820 
3821 	/* free all memory, including dma buffers */
3822 	mempool_free(pmboxq, phba->mbox_mem_pool);
3823 	lpfc_bsg_mbox_ext_session_reset(phba);
3824 
3825 	/* if the job is still active, call job done */
3826 	if (job) {
3827 		bsg_reply = job->reply;
3828 		bsg_job_done(job, bsg_reply->result,
3829 			       bsg_reply->reply_payload_rcv_len);
3830 	}
3831 
3832 	return;
3833 }
3834 
3835 static void
3836 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3837 				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3838 				struct lpfc_dmabuf *ext_dmabuf)
3839 {
3840 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3841 
3842 	/* pointer to the start of mailbox command */
3843 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3844 
3845 	if (nemb_tp == nemb_mse) {
3846 		if (index == 0) {
3847 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3848 				mse[index].pa_hi =
3849 				putPaddrHigh(mbx_dmabuf->phys +
3850 					     sizeof(MAILBOX_t));
3851 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3852 				mse[index].pa_lo =
3853 				putPaddrLow(mbx_dmabuf->phys +
3854 					    sizeof(MAILBOX_t));
3855 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3856 					"2943 SLI_CONFIG(mse)[%d], "
3857 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3858 					index,
3859 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3860 					mse[index].buf_len,
3861 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3862 					mse[index].pa_hi,
3863 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3864 					mse[index].pa_lo);
3865 		} else {
3866 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3867 				mse[index].pa_hi =
3868 				putPaddrHigh(ext_dmabuf->phys);
3869 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3870 				mse[index].pa_lo =
3871 				putPaddrLow(ext_dmabuf->phys);
3872 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3873 					"2944 SLI_CONFIG(mse)[%d], "
3874 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3875 					index,
3876 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3877 					mse[index].buf_len,
3878 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3879 					mse[index].pa_hi,
3880 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3881 					mse[index].pa_lo);
3882 		}
3883 	} else {
3884 		if (index == 0) {
3885 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3886 				hbd[index].pa_hi =
3887 				putPaddrHigh(mbx_dmabuf->phys +
3888 					     sizeof(MAILBOX_t));
3889 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3890 				hbd[index].pa_lo =
3891 				putPaddrLow(mbx_dmabuf->phys +
3892 					    sizeof(MAILBOX_t));
3893 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3894 					"3007 SLI_CONFIG(hbd)[%d], "
3895 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3896 				index,
3897 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3898 				&sli_cfg_mbx->un.
3899 				sli_config_emb1_subsys.hbd[index]),
3900 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3901 				hbd[index].pa_hi,
3902 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3903 				hbd[index].pa_lo);
3904 
3905 		} else {
3906 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3907 				hbd[index].pa_hi =
3908 				putPaddrHigh(ext_dmabuf->phys);
3909 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3910 				hbd[index].pa_lo =
3911 				putPaddrLow(ext_dmabuf->phys);
3912 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3913 					"3008 SLI_CONFIG(hbd)[%d], "
3914 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3915 				index,
3916 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3917 				&sli_cfg_mbx->un.
3918 				sli_config_emb1_subsys.hbd[index]),
3919 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3920 				hbd[index].pa_hi,
3921 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3922 				hbd[index].pa_lo);
3923 		}
3924 	}
3925 	return;
3926 }
3927 
3928 /**
3929  * lpfc_bsg_sli_cfg_read_cmd_ext - sli_config non-embedded mailbox cmd read
3930  * @phba: Pointer to HBA context object.
3931  * @job: Pointer to the job object.
3932  * @nemb_tp: Enumerate of non-embedded mailbox command type.
3933  * @dmabuf: Pointer to a DMA buffer descriptor.
3934  *
3935  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3936  * non-embedded external buffers.
3937  **/
3938 static int
3939 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
3940 			      enum nemb_type nemb_tp,
3941 			      struct lpfc_dmabuf *dmabuf)
3942 {
3943 	struct fc_bsg_request *bsg_request = job->request;
3944 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3945 	struct lpfc_sli_config_emb0_subsys *emb0_subsys;
3946 	struct list_head *ext_dmabuf_list;
3947 	struct dfc_mbox_req *mbox_req;
3948 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3949 	u32 ext_buf_cnt, ext_buf_index, hbd_cnt;
3950 	struct lpfc_dmabuf *ext_dmabuf = NULL;
3951 	struct bsg_job_data *dd_data = NULL;
3952 	LPFC_MBOXQ_t *pmboxq = NULL;
3953 	MAILBOX_t *pmb;
3954 	u8 *pmbx, opcode;
3955 	int rc, i;
3956 
3957 	mbox_req =
3958 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
3959 
3960 	/* pointer to the start of mailbox command */
3961 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3962 
3963 	if (nemb_tp == nemb_mse) {
3964 		emb0_subsys = &sli_cfg_mbx->un.sli_config_emb0_subsys;
3965 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3966 			&emb0_subsys->sli_config_hdr);
3967 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3968 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3969 					"2945 Handled SLI_CONFIG(mse) rd, "
3970 					"ext_buf_cnt(%d) out of range(%d)\n",
3971 					ext_buf_cnt,
3972 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
3973 			rc = -ERANGE;
3974 			goto job_error;
3975 		}
3976 
3977 		/* Special handling for non-embedded READ_OBJECT */
3978 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode, emb0_subsys);
3979 		switch (opcode) {
3980 		case COMN_OPCODE_READ_OBJECT:
3981 			hbd_cnt = bsg_bf_get(lpfc_emb0_subcmnd_rd_obj_hbd_cnt,
3982 					     emb0_subsys);
3983 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3984 					"2449 SLI_CONFIG(mse) rd non-embedded "
3985 					"hbd count = %d\n",
3986 					hbd_cnt);
3987 
3988 			ext_dmabuf_list =
3989 					&phba->mbox_ext_buf_ctx.ext_dmabuf_list;
3990 
3991 			/* Allocate hbds */
3992 			for (i = 0; i < hbd_cnt; i++) {
3993 				ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3994 				if (!ext_dmabuf) {
3995 					rc = -ENOMEM;
3996 					goto job_error;
3997 				}
3998 				list_add_tail(&ext_dmabuf->list,
3999 					      ext_dmabuf_list);
4000 			}
4001 
4002 			/* Fill out the physical memory addresses for the
4003 			 * hbds
4004 			 */
4005 			i = 0;
4006 			list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
4007 						 ext_dmabuf_list, list) {
4008 				emb0_subsys->hbd[i].pa_hi =
4009 					putPaddrHigh(curr_dmabuf->phys);
4010 				emb0_subsys->hbd[i].pa_lo =
4011 					putPaddrLow(curr_dmabuf->phys);
4012 
4013 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4014 						"2495 SLI_CONFIG(hbd)[%d], "
4015 						"bufLen:%d, addrHi:x%x, "
4016 						"addrLo:x%x\n", i,
4017 						emb0_subsys->hbd[i].buf_len,
4018 						emb0_subsys->hbd[i].pa_hi,
4019 						emb0_subsys->hbd[i].pa_lo);
4020 				i++;
4021 			}
4022 			break;
4023 		default:
4024 			break;
4025 		}
4026 
4027 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4028 				"2941 Handled SLI_CONFIG(mse) rd, "
4029 				"ext_buf_cnt:%d\n", ext_buf_cnt);
4030 	} else {
4031 		/* sanity check on interface type for support */
4032 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4033 		    LPFC_SLI_INTF_IF_TYPE_2) {
4034 			rc = -ENODEV;
4035 			goto job_error;
4036 		}
4037 		/* nemb_tp == nemb_hbd */
4038 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4039 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4040 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4041 					"2946 Handled SLI_CONFIG(hbd) rd, "
4042 					"ext_buf_cnt(%d) out of range(%d)\n",
4043 					ext_buf_cnt,
4044 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
4045 			rc = -ERANGE;
4046 			goto job_error;
4047 		}
4048 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4049 				"2942 Handled SLI_CONFIG(hbd) rd, "
4050 				"ext_buf_cnt:%d\n", ext_buf_cnt);
4051 	}
4052 
4053 	/* before dma descriptor setup */
4054 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
4055 					sta_pre_addr, dmabuf, ext_buf_cnt);
4056 
4057 	/* reject non-embedded mailbox command with none external buffer */
4058 	if (ext_buf_cnt == 0) {
4059 		rc = -EPERM;
4060 		goto job_error;
4061 	} else if (ext_buf_cnt > 1) {
4062 		/* additional external read buffers */
4063 		for (i = 1; i < ext_buf_cnt; i++) {
4064 			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
4065 			if (!ext_dmabuf) {
4066 				rc = -ENOMEM;
4067 				goto job_error;
4068 			}
4069 			list_add_tail(&ext_dmabuf->list,
4070 				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4071 		}
4072 	}
4073 
4074 	/* bsg tracking structure */
4075 	dd_data = kmalloc_obj(struct bsg_job_data);
4076 	if (!dd_data) {
4077 		rc = -ENOMEM;
4078 		goto job_error;
4079 	}
4080 
4081 	/* mailbox command structure for base driver */
4082 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4083 	if (!pmboxq) {
4084 		rc = -ENOMEM;
4085 		goto job_error;
4086 	}
4087 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4088 
4089 	/* for the first external buffer */
4090 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4091 
4092 	/* for the rest of external buffer descriptors if any */
4093 	if (ext_buf_cnt > 1) {
4094 		ext_buf_index = 1;
4095 		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
4096 				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
4097 			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
4098 						ext_buf_index, dmabuf,
4099 						curr_dmabuf);
4100 			ext_buf_index++;
4101 		}
4102 	}
4103 
4104 	/* after dma descriptor setup */
4105 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_rd, dma_mbox,
4106 					sta_pos_addr, dmabuf, ext_buf_cnt);
4107 
4108 	/* construct base driver mbox command */
4109 	pmb = &pmboxq->u.mb;
4110 	pmbx = (uint8_t *)dmabuf->virt;
4111 	memcpy(pmb, pmbx, sizeof(*pmb));
4112 	pmb->mbxOwner = OWN_HOST;
4113 	pmboxq->vport = phba->pport;
4114 
4115 	/* multi-buffer handling context */
4116 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4117 	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
4118 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4119 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4120 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4121 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4122 
4123 	/* callback for multi-buffer read mailbox command */
4124 	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
4125 
4126 	/* context fields to callback function */
4127 	pmboxq->ctx_u.dd_data = dd_data;
4128 	dd_data->type = TYPE_MBOX;
4129 	dd_data->set_job = job;
4130 	dd_data->context_un.mbox.pmboxq = pmboxq;
4131 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4132 	job->dd_data = dd_data;
4133 
4134 	/* state change */
4135 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4136 
4137 	/*
4138 	 * Non-embedded mailbox subcommand data gets byte swapped here because
4139 	 * the lower level driver code only does the first 64 mailbox words.
4140 	 */
4141 	if ((!bsg_bf_get(lpfc_mbox_hdr_emb,
4142 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) &&
4143 		(nemb_tp == nemb_mse))
4144 		lpfc_sli_pcimem_bcopy(&pmbx[sizeof(MAILBOX_t)],
4145 			&pmbx[sizeof(MAILBOX_t)],
4146 				sli_cfg_mbx->un.sli_config_emb0_subsys.
4147 					mse[0].buf_len);
4148 
4149 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4150 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4151 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4152 				"2947 Issued SLI_CONFIG ext-buffer "
4153 				"mailbox command, rc:x%x\n", rc);
4154 		return SLI_CONFIG_HANDLED;
4155 	}
4156 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4157 			"2948 Failed to issue SLI_CONFIG ext-buffer "
4158 			"mailbox command, rc:x%x\n", rc);
4159 	rc = -EPIPE;
4160 
4161 job_error:
4162 	if (pmboxq)
4163 		mempool_free(pmboxq, phba->mbox_mem_pool);
4164 	lpfc_bsg_dma_page_list_free(phba,
4165 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4166 	kfree(dd_data);
4167 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4168 	return rc;
4169 }
4170 
4171 /**
4172  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
4173  * @phba: Pointer to HBA context object.
4174  * @job: Pointer to the job object.
4175  * @nemb_tp: Enumerate of non-embedded mailbox command type.
4176  * @dmabuf: Pointer to a DMA buffer descriptor.
4177  *
4178  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
4179  * non-embedded external buffers.
4180  **/
4181 static int
4182 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct bsg_job *job,
4183 			       enum nemb_type nemb_tp,
4184 			       struct lpfc_dmabuf *dmabuf)
4185 {
4186 	struct fc_bsg_request *bsg_request = job->request;
4187 	struct fc_bsg_reply *bsg_reply = job->reply;
4188 	struct dfc_mbox_req *mbox_req;
4189 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
4190 	uint32_t ext_buf_cnt;
4191 	struct bsg_job_data *dd_data = NULL;
4192 	LPFC_MBOXQ_t *pmboxq = NULL;
4193 	MAILBOX_t *pmb;
4194 	uint8_t *mbx;
4195 	int rc = SLI_CONFIG_NOT_HANDLED, i;
4196 
4197 	mbox_req =
4198 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4199 
4200 	/* pointer to the start of mailbox command */
4201 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4202 
4203 	if (nemb_tp == nemb_mse) {
4204 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
4205 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
4206 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
4207 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4208 					"2953 Failed SLI_CONFIG(mse) wr, "
4209 					"ext_buf_cnt(%d) out of range(%d)\n",
4210 					ext_buf_cnt,
4211 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
4212 			return -ERANGE;
4213 		}
4214 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4215 				"2949 Handled SLI_CONFIG(mse) wr, "
4216 				"ext_buf_cnt:%d\n", ext_buf_cnt);
4217 	} else {
4218 		/* sanity check on interface type for support */
4219 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
4220 		    LPFC_SLI_INTF_IF_TYPE_2)
4221 			return -ENODEV;
4222 		/* nemb_tp == nemb_hbd */
4223 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
4224 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
4225 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4226 					"2954 Failed SLI_CONFIG(hbd) wr, "
4227 					"ext_buf_cnt(%d) out of range(%d)\n",
4228 					ext_buf_cnt,
4229 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
4230 			return -ERANGE;
4231 		}
4232 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4233 				"2950 Handled SLI_CONFIG(hbd) wr, "
4234 				"ext_buf_cnt:%d\n", ext_buf_cnt);
4235 	}
4236 
4237 	/* before dma buffer descriptor setup */
4238 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4239 					sta_pre_addr, dmabuf, ext_buf_cnt);
4240 
4241 	if (ext_buf_cnt == 0)
4242 		return -EPERM;
4243 
4244 	/* for the first external buffer */
4245 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
4246 
4247 	/* after dma descriptor setup */
4248 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, nemb_tp, mbox_wr, dma_mbox,
4249 					sta_pos_addr, dmabuf, ext_buf_cnt);
4250 
4251 	/* log for looking forward */
4252 	for (i = 1; i < ext_buf_cnt; i++) {
4253 		if (nemb_tp == nemb_mse)
4254 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4255 				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
4256 				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
4257 				mse[i].buf_len);
4258 		else
4259 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4260 				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
4261 				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4262 				&sli_cfg_mbx->un.sli_config_emb1_subsys.
4263 				hbd[i]));
4264 	}
4265 
4266 	/* multi-buffer handling context */
4267 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
4268 	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
4269 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
4270 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
4271 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
4272 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
4273 
4274 	if (ext_buf_cnt == 1) {
4275 		/* bsg tracking structure */
4276 		dd_data = kmalloc_obj(struct bsg_job_data);
4277 		if (!dd_data) {
4278 			rc = -ENOMEM;
4279 			goto job_error;
4280 		}
4281 
4282 		/* mailbox command structure for base driver */
4283 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4284 		if (!pmboxq) {
4285 			rc = -ENOMEM;
4286 			goto job_error;
4287 		}
4288 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4289 		pmb = &pmboxq->u.mb;
4290 		mbx = (uint8_t *)dmabuf->virt;
4291 		memcpy(pmb, mbx, sizeof(*pmb));
4292 		pmb->mbxOwner = OWN_HOST;
4293 		pmboxq->vport = phba->pport;
4294 
4295 		/* callback for multi-buffer read mailbox command */
4296 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4297 
4298 		/* context fields to callback function */
4299 		pmboxq->ctx_u.dd_data = dd_data;
4300 		dd_data->type = TYPE_MBOX;
4301 		dd_data->set_job = job;
4302 		dd_data->context_un.mbox.pmboxq = pmboxq;
4303 		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
4304 		job->dd_data = dd_data;
4305 
4306 		/* state change */
4307 
4308 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4309 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4310 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4311 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4312 					"2955 Issued SLI_CONFIG ext-buffer "
4313 					"mailbox command, rc:x%x\n", rc);
4314 			return SLI_CONFIG_HANDLED;
4315 		}
4316 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4317 				"2956 Failed to issue SLI_CONFIG ext-buffer "
4318 				"mailbox command, rc:x%x\n", rc);
4319 		rc = -EPIPE;
4320 		goto job_error;
4321 	}
4322 
4323 	/* wait for additional external buffers */
4324 
4325 	bsg_reply->result = 0;
4326 	bsg_job_done(job, bsg_reply->result,
4327 		       bsg_reply->reply_payload_rcv_len);
4328 	return SLI_CONFIG_HANDLED;
4329 
4330 job_error:
4331 	if (pmboxq)
4332 		mempool_free(pmboxq, phba->mbox_mem_pool);
4333 	kfree(dd_data);
4334 
4335 	return rc;
4336 }
4337 
4338 /**
4339  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
4340  * @phba: Pointer to HBA context object.
4341  * @job: Pointer to the job object.
4342  * @dmabuf: Pointer to a DMA buffer descriptor.
4343  *
4344  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
4345  * external buffers, including both 0x9B with non-embedded MSEs and 0x9B
4346  * with embedded subsystem 0x1 and opcodes with external HBDs.
4347  **/
4348 static int
4349 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4350 			     struct lpfc_dmabuf *dmabuf)
4351 {
4352 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
4353 	uint32_t subsys;
4354 	uint32_t opcode;
4355 	int rc = SLI_CONFIG_NOT_HANDLED;
4356 
4357 	/* state change on new multi-buffer pass-through mailbox command */
4358 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
4359 
4360 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
4361 
4362 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
4363 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
4364 		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
4365 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
4366 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
4367 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
4368 		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
4369 			switch (opcode) {
4370 			case FCOE_OPCODE_READ_FCF:
4371 			case FCOE_OPCODE_GET_DPORT_RESULTS:
4372 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4373 						"2957 Handled SLI_CONFIG "
4374 						"subsys_fcoe, opcode:x%x\n",
4375 						opcode);
4376 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4377 							nemb_mse, dmabuf);
4378 				break;
4379 			case FCOE_OPCODE_ADD_FCF:
4380 			case FCOE_OPCODE_SET_DPORT_MODE:
4381 			case LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE:
4382 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4383 						"2958 Handled SLI_CONFIG "
4384 						"subsys_fcoe, opcode:x%x\n",
4385 						opcode);
4386 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4387 							nemb_mse, dmabuf);
4388 				break;
4389 			default:
4390 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4391 						"2959 Reject SLI_CONFIG "
4392 						"subsys_fcoe, opcode:x%x\n",
4393 						opcode);
4394 				rc = -EPERM;
4395 				break;
4396 			}
4397 		} else if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4398 			switch (opcode) {
4399 			case COMN_OPCODE_GET_CNTL_ADDL_ATTRIBUTES:
4400 			case COMN_OPCODE_GET_CNTL_ATTRIBUTES:
4401 			case COMN_OPCODE_GET_PROFILE_CONFIG:
4402 			case COMN_OPCODE_SET_FEATURES:
4403 			case COMN_OPCODE_READ_OBJECT:
4404 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4405 						"3106 Handled SLI_CONFIG "
4406 						"subsys_comn, opcode:x%x\n",
4407 						opcode);
4408 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4409 							nemb_mse, dmabuf);
4410 				break;
4411 			default:
4412 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4413 						"3107 Reject SLI_CONFIG "
4414 						"subsys_comn, opcode:x%x\n",
4415 						opcode);
4416 				rc = -EPERM;
4417 				break;
4418 			}
4419 		} else {
4420 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4421 					"2977 Reject SLI_CONFIG "
4422 					"subsys:x%d, opcode:x%x\n",
4423 					subsys, opcode);
4424 			rc = -EPERM;
4425 		}
4426 	} else {
4427 		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
4428 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
4429 		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
4430 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
4431 		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
4432 			switch (opcode) {
4433 			case COMN_OPCODE_READ_OBJECT:
4434 			case COMN_OPCODE_READ_OBJECT_LIST:
4435 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4436 						"2960 Handled SLI_CONFIG "
4437 						"subsys_comn, opcode:x%x\n",
4438 						opcode);
4439 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
4440 							nemb_hbd, dmabuf);
4441 				break;
4442 			case COMN_OPCODE_WRITE_OBJECT:
4443 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4444 						"2961 Handled SLI_CONFIG "
4445 						"subsys_comn, opcode:x%x\n",
4446 						opcode);
4447 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
4448 							nemb_hbd, dmabuf);
4449 				break;
4450 			default:
4451 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4452 						"2962 Not handled SLI_CONFIG "
4453 						"subsys_comn, opcode:x%x\n",
4454 						opcode);
4455 				rc = SLI_CONFIG_NOT_HANDLED;
4456 				break;
4457 			}
4458 		} else {
4459 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4460 					"2978 Not handled SLI_CONFIG "
4461 					"subsys:x%d, opcode:x%x\n",
4462 					subsys, opcode);
4463 			rc = SLI_CONFIG_NOT_HANDLED;
4464 		}
4465 	}
4466 
4467 	/* state reset on not handled new multi-buffer mailbox command */
4468 	if (rc != SLI_CONFIG_HANDLED)
4469 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
4470 
4471 	return rc;
4472 }
4473 
4474 /**
4475  * lpfc_bsg_mbox_ext_abort - request to abort mbox command with ext buffers
4476  * @phba: Pointer to HBA context object.
4477  *
4478  * This routine is for requesting to abort a pass-through mailbox command with
4479  * multiple external buffers due to error condition.
4480  **/
4481 static void
4482 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
4483 {
4484 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4485 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4486 	else
4487 		lpfc_bsg_mbox_ext_session_reset(phba);
4488 	return;
4489 }
4490 
4491 /**
4492  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
4493  * @phba: Pointer to HBA context object.
4494  * @job: Pointer to the job object.
4495  *
4496  * This routine extracts the next mailbox read external buffer back to
4497  * user space through BSG.
4498  **/
4499 static int
4500 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct bsg_job *job)
4501 {
4502 	struct fc_bsg_reply *bsg_reply = job->reply;
4503 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
4504 	struct lpfc_dmabuf *dmabuf;
4505 	uint8_t *pbuf;
4506 	uint32_t size;
4507 	uint32_t index;
4508 
4509 	index = phba->mbox_ext_buf_ctx.seqNum;
4510 	phba->mbox_ext_buf_ctx.seqNum++;
4511 
4512 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
4513 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4514 
4515 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4516 		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
4517 			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
4518 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4519 				"2963 SLI_CONFIG (mse) ext-buffer rd get "
4520 				"buffer[%d], size:%d\n", index, size);
4521 	} else {
4522 		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
4523 			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
4524 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4525 				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
4526 				"buffer[%d], size:%d\n", index, size);
4527 	}
4528 	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
4529 		return -EPIPE;
4530 	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
4531 				  struct lpfc_dmabuf, list);
4532 	list_del_init(&dmabuf->list);
4533 
4534 	/* after dma buffer descriptor setup */
4535 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4536 					mbox_rd, dma_ebuf, sta_pos_addr,
4537 					dmabuf, index);
4538 
4539 	pbuf = (uint8_t *)dmabuf->virt;
4540 	bsg_reply->reply_payload_rcv_len =
4541 		sg_copy_from_buffer(job->reply_payload.sg_list,
4542 				    job->reply_payload.sg_cnt,
4543 				    pbuf, size);
4544 
4545 	lpfc_bsg_dma_page_free(phba, dmabuf);
4546 
4547 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4548 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4549 				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
4550 				"command session done\n");
4551 		lpfc_bsg_mbox_ext_session_reset(phba);
4552 	}
4553 
4554 	bsg_reply->result = 0;
4555 	bsg_job_done(job, bsg_reply->result,
4556 		       bsg_reply->reply_payload_rcv_len);
4557 
4558 	return SLI_CONFIG_HANDLED;
4559 }
4560 
4561 /**
4562  * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
4563  * @phba: Pointer to HBA context object.
4564  * @job: Pointer to the job object.
4565  * @dmabuf: Pointer to a DMA buffer descriptor.
4566  *
4567  * This routine sets up the next mailbox read external buffer obtained
4568  * from user space through BSG.
4569  **/
4570 static int
4571 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct bsg_job *job,
4572 			struct lpfc_dmabuf *dmabuf)
4573 {
4574 	struct fc_bsg_reply *bsg_reply = job->reply;
4575 	struct bsg_job_data *dd_data = NULL;
4576 	LPFC_MBOXQ_t *pmboxq = NULL;
4577 	MAILBOX_t *pmb;
4578 	enum nemb_type nemb_tp;
4579 	uint8_t *pbuf;
4580 	uint32_t size;
4581 	uint32_t index;
4582 	int rc;
4583 
4584 	index = phba->mbox_ext_buf_ctx.seqNum;
4585 	phba->mbox_ext_buf_ctx.seqNum++;
4586 	nemb_tp = phba->mbox_ext_buf_ctx.nembType;
4587 
4588 	pbuf = (uint8_t *)dmabuf->virt;
4589 	size = job->request_payload.payload_len;
4590 	sg_copy_to_buffer(job->request_payload.sg_list,
4591 			  job->request_payload.sg_cnt,
4592 			  pbuf, size);
4593 
4594 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
4595 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4596 				"2966 SLI_CONFIG (mse) ext-buffer wr set "
4597 				"buffer[%d], size:%d\n",
4598 				phba->mbox_ext_buf_ctx.seqNum, size);
4599 
4600 	} else {
4601 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4602 				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
4603 				"buffer[%d], size:%d\n",
4604 				phba->mbox_ext_buf_ctx.seqNum, size);
4605 
4606 	}
4607 
4608 	/* set up external buffer descriptor and add to external buffer list */
4609 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
4610 					phba->mbox_ext_buf_ctx.mbx_dmabuf,
4611 					dmabuf);
4612 	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4613 
4614 	/* after write dma buffer */
4615 	lpfc_idiag_mbxacc_dump_bsg_mbox(phba, phba->mbox_ext_buf_ctx.nembType,
4616 					mbox_wr, dma_ebuf, sta_pos_addr,
4617 					dmabuf, index);
4618 
4619 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
4620 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4621 				"2968 SLI_CONFIG ext-buffer wr all %d "
4622 				"ebuffers received\n",
4623 				phba->mbox_ext_buf_ctx.numBuf);
4624 
4625 		dd_data = kmalloc_obj(struct bsg_job_data);
4626 		if (!dd_data) {
4627 			rc = -ENOMEM;
4628 			goto job_error;
4629 		}
4630 
4631 		/* mailbox command structure for base driver */
4632 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4633 		if (!pmboxq) {
4634 			rc = -ENOMEM;
4635 			goto job_error;
4636 		}
4637 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4638 		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
4639 		pmb = &pmboxq->u.mb;
4640 		memcpy(pmb, pbuf, sizeof(*pmb));
4641 		pmb->mbxOwner = OWN_HOST;
4642 		pmboxq->vport = phba->pport;
4643 
4644 		/* callback for multi-buffer write mailbox command */
4645 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
4646 
4647 		/* context fields to callback function */
4648 		pmboxq->ctx_u.dd_data = dd_data;
4649 		dd_data->type = TYPE_MBOX;
4650 		dd_data->set_job = job;
4651 		dd_data->context_un.mbox.pmboxq = pmboxq;
4652 		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
4653 		job->dd_data = dd_data;
4654 
4655 		/* state change */
4656 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
4657 
4658 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4659 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
4660 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4661 					"2969 Issued SLI_CONFIG ext-buffer "
4662 					"mailbox command, rc:x%x\n", rc);
4663 			return SLI_CONFIG_HANDLED;
4664 		}
4665 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4666 				"2970 Failed to issue SLI_CONFIG ext-buffer "
4667 				"mailbox command, rc:x%x\n", rc);
4668 		rc = -EPIPE;
4669 		goto job_error;
4670 	}
4671 
4672 	/* wait for additional external buffers */
4673 	bsg_reply->result = 0;
4674 	bsg_job_done(job, bsg_reply->result,
4675 		       bsg_reply->reply_payload_rcv_len);
4676 	return SLI_CONFIG_HANDLED;
4677 
4678 job_error:
4679 	if (pmboxq)
4680 		mempool_free(pmboxq, phba->mbox_mem_pool);
4681 	lpfc_bsg_dma_page_free(phba, dmabuf);
4682 	kfree(dd_data);
4683 
4684 	return rc;
4685 }
4686 
4687 /**
4688  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
4689  * @phba: Pointer to HBA context object.
4690  * @job: Pointer to the job object.
4691  * @dmabuf: Pointer to a DMA buffer descriptor.
4692  *
4693  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
4694  * command with multiple non-embedded external buffers.
4695  **/
4696 static int
4697 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct bsg_job *job,
4698 			     struct lpfc_dmabuf *dmabuf)
4699 {
4700 	int rc;
4701 
4702 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4703 			"2971 SLI_CONFIG buffer (type:x%x)\n",
4704 			phba->mbox_ext_buf_ctx.mboxType);
4705 
4706 	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4707 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4708 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4709 					"2972 SLI_CONFIG rd buffer state "
4710 					"mismatch:x%x\n",
4711 					phba->mbox_ext_buf_ctx.state);
4712 			lpfc_bsg_mbox_ext_abort(phba);
4713 			return -EPIPE;
4714 		}
4715 		rc = lpfc_bsg_read_ebuf_get(phba, job);
4716 		if (rc == SLI_CONFIG_HANDLED)
4717 			lpfc_bsg_dma_page_free(phba, dmabuf);
4718 	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4719 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4720 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4721 					"2973 SLI_CONFIG wr buffer state "
4722 					"mismatch:x%x\n",
4723 					phba->mbox_ext_buf_ctx.state);
4724 			lpfc_bsg_mbox_ext_abort(phba);
4725 			return -EPIPE;
4726 		}
4727 		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4728 	}
4729 	return rc;
4730 }
4731 
4732 /**
4733  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4734  * @phba: Pointer to HBA context object.
4735  * @job: Pointer to the job object.
4736  * @dmabuf: Pointer to a DMA buffer descriptor.
4737  *
4738  * This routine checks and handles non-embedded multi-buffer SLI_CONFIG
4739  * (0x9B) mailbox commands and external buffers.
4740  **/
4741 static int
4742 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct bsg_job *job,
4743 			    struct lpfc_dmabuf *dmabuf)
4744 {
4745 	struct fc_bsg_request *bsg_request = job->request;
4746 	struct dfc_mbox_req *mbox_req;
4747 	int rc = SLI_CONFIG_NOT_HANDLED;
4748 
4749 	mbox_req =
4750 	   (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4751 
4752 	/* mbox command with/without single external buffer */
4753 	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4754 		return rc;
4755 
4756 	/* mbox command and first external buffer */
4757 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4758 		if (mbox_req->extSeqNum == 1) {
4759 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4760 					"2974 SLI_CONFIG mailbox: tag:%d, "
4761 					"seq:%d\n", mbox_req->extMboxTag,
4762 					mbox_req->extSeqNum);
4763 			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4764 			return rc;
4765 		} else
4766 			goto sli_cfg_ext_error;
4767 	}
4768 
4769 	/*
4770 	 * handle additional external buffers
4771 	 */
4772 
4773 	/* check broken pipe conditions */
4774 	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4775 		goto sli_cfg_ext_error;
4776 	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4777 		goto sli_cfg_ext_error;
4778 	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4779 		goto sli_cfg_ext_error;
4780 
4781 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4782 			"2975 SLI_CONFIG mailbox external buffer: "
4783 			"extSta:x%x, tag:%d, seq:%d\n",
4784 			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4785 			mbox_req->extSeqNum);
4786 	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4787 	return rc;
4788 
4789 sli_cfg_ext_error:
4790 	/* all other cases, broken pipe */
4791 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4792 			"2976 SLI_CONFIG mailbox broken pipe: "
4793 			"ctxSta:x%x, ctxNumBuf:%d "
4794 			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4795 			phba->mbox_ext_buf_ctx.state,
4796 			phba->mbox_ext_buf_ctx.numBuf,
4797 			phba->mbox_ext_buf_ctx.mbxTag,
4798 			phba->mbox_ext_buf_ctx.seqNum,
4799 			mbox_req->extMboxTag, mbox_req->extSeqNum);
4800 
4801 	lpfc_bsg_mbox_ext_session_reset(phba);
4802 
4803 	return -EPIPE;
4804 }
4805 
4806 /**
4807  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4808  * @phba: Pointer to HBA context object.
4809  * @job: Pointer to the job object.
4810  * @vport: Pointer to a vport object.
4811  *
4812  * Allocate a tracking object, mailbox command memory, get a mailbox
4813  * from the mailbox pool, copy the caller mailbox command.
4814  *
4815  * If offline and the sli is active we need to poll for the command (port is
4816  * being reset) and complete the job, otherwise issue the mailbox command and
4817  * let our completion handler finish the command.
4818  **/
4819 static int
4820 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct bsg_job *job,
4821 	struct lpfc_vport *vport)
4822 {
4823 	struct fc_bsg_request *bsg_request = job->request;
4824 	struct fc_bsg_reply *bsg_reply = job->reply;
4825 	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4826 	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4827 	/* a 4k buffer to hold the mb and extended data from/to the bsg */
4828 	uint8_t *pmbx = NULL;
4829 	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4830 	struct lpfc_dmabuf *dmabuf = NULL;
4831 	struct dfc_mbox_req *mbox_req;
4832 	struct READ_EVENT_LOG_VAR *rdEventLog;
4833 	uint32_t transmit_length, receive_length, mode;
4834 	struct lpfc_mbx_sli4_config *sli4_config;
4835 	struct lpfc_mbx_nembed_cmd *nembed_sge;
4836 	struct ulp_bde64 *bde;
4837 	uint8_t *ext = NULL;
4838 	int rc = 0;
4839 	uint8_t *from;
4840 	uint32_t size;
4841 
4842 	/* in case no data is transferred */
4843 	bsg_reply->reply_payload_rcv_len = 0;
4844 
4845 	/* sanity check to protect driver */
4846 	if (job->request_payload.payload_len > BSG_MBOX_SIZE) {
4847 		rc = -ERANGE;
4848 		goto job_done;
4849 	}
4850 
4851 	/*
4852 	 * Don't allow mailbox commands to be sent when blocked or when in
4853 	 * the middle of discovery
4854 	 */
4855 	if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4856 		rc = -EAGAIN;
4857 		goto job_done;
4858 	}
4859 
4860 	mbox_req =
4861 	    (struct dfc_mbox_req *)bsg_request->rqst_data.h_vendor.vendor_cmd;
4862 
4863 	/* check if requested extended data lengths are valid */
4864 	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4865 	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4866 		rc = -ERANGE;
4867 		goto job_done;
4868 	}
4869 
4870 	dmabuf = lpfc_bsg_dma_page_alloc(phba);
4871 	if (!dmabuf || !dmabuf->virt) {
4872 		rc = -ENOMEM;
4873 		goto job_done;
4874 	}
4875 
4876 	/* Get the mailbox command or external buffer from BSG */
4877 	pmbx = (uint8_t *)dmabuf->virt;
4878 	size = job->request_payload.payload_len;
4879 	sg_copy_to_buffer(job->request_payload.sg_list,
4880 			  job->request_payload.sg_cnt, pmbx, size);
4881 
4882 	/* Handle possible SLI_CONFIG with non-embedded payloads */
4883 	if (phba->sli_rev == LPFC_SLI_REV4) {
4884 		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4885 		if (rc == SLI_CONFIG_HANDLED)
4886 			goto job_cont;
4887 		if (rc)
4888 			goto job_done;
4889 		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4890 	}
4891 
4892 	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4893 	if (rc != 0)
4894 		goto job_done; /* must be negative */
4895 
4896 	/* allocate our bsg tracking structure */
4897 	dd_data = kmalloc_obj(struct bsg_job_data);
4898 	if (!dd_data) {
4899 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4900 				"2727 Failed allocation of dd_data\n");
4901 		rc = -ENOMEM;
4902 		goto job_done;
4903 	}
4904 
4905 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4906 	if (!pmboxq) {
4907 		rc = -ENOMEM;
4908 		goto job_done;
4909 	}
4910 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4911 
4912 	pmb = &pmboxq->u.mb;
4913 	memcpy(pmb, pmbx, sizeof(*pmb));
4914 	pmb->mbxOwner = OWN_HOST;
4915 	pmboxq->vport = vport;
4916 
4917 	/* non-embedded SLI_CONFIG requests already parsed, check others */
4918 	if (unlikely(job->reply_payload.payload_len > BSG_MBOX_SIZE)) {
4919 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4920 				"2729 Cmd x%x (x%x/x%x) request has "
4921 				"out-of-range reply payload length x%x\n",
4922 				pmb->mbxCommand,
4923 				lpfc_sli_config_mbox_subsys_get(phba, pmboxq),
4924 				lpfc_sli_config_mbox_opcode_get(phba, pmboxq),
4925 				job->reply_payload.payload_len);
4926 		rc = -ERANGE;
4927 		goto job_done;
4928 	}
4929 
4930 	/* If HBA encountered an error attention, allow only DUMP
4931 	 * or RESTART mailbox commands until the HBA is restarted.
4932 	 */
4933 	if (phba->pport->stopped &&
4934 	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
4935 	    pmb->mbxCommand != MBX_RESTART &&
4936 	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
4937 	    pmb->mbxCommand != MBX_WRITE_WWN)
4938 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4939 				"2797 mbox: Issued mailbox cmd "
4940 				"0x%x while in stopped state.\n",
4941 				pmb->mbxCommand);
4942 
4943 	/* extended mailbox commands will need an extended buffer */
4944 	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4945 		from = pmbx;
4946 		ext = from + sizeof(MAILBOX_t);
4947 		pmboxq->ext_buf = ext;
4948 		pmboxq->in_ext_byte_len =
4949 			mbox_req->inExtWLen * sizeof(uint32_t);
4950 		pmboxq->out_ext_byte_len =
4951 			mbox_req->outExtWLen * sizeof(uint32_t);
4952 		pmboxq->mbox_offset_word = mbox_req->mbOffset;
4953 	}
4954 
4955 	/* biu diag will need a kernel buffer to transfer the data
4956 	 * allocate our own buffer and setup the mailbox command to
4957 	 * use ours
4958 	 */
4959 	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4960 		transmit_length = pmb->un.varWords[1];
4961 		receive_length = pmb->un.varWords[4];
4962 		/* transmit length cannot be greater than receive length or
4963 		 * mailbox extension size
4964 		 */
4965 		if ((transmit_length > receive_length) ||
4966 			(transmit_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
4967 			rc = -ERANGE;
4968 			goto job_done;
4969 		}
4970 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4971 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4972 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4973 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4974 
4975 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4976 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4977 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4978 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4979 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4980 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4981 	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4982 		rdEventLog = &pmb->un.varRdEventLog;
4983 		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4984 		mode = bf_get(lpfc_event_log, rdEventLog);
4985 
4986 		/* receive length cannot be greater than mailbox
4987 		 * extension size
4988 		 */
4989 		if (receive_length > BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
4990 			rc = -ERANGE;
4991 			goto job_done;
4992 		}
4993 
4994 		/* mode zero uses a bde like biu diags command */
4995 		if (mode == 0) {
4996 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4997 							+ sizeof(MAILBOX_t));
4998 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4999 							+ sizeof(MAILBOX_t));
5000 		}
5001 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
5002 		/* Let type 4 (well known data) through because the data is
5003 		 * returned in varwords[4-8]
5004 		 * otherwise check the receive length and fetch the buffer addr
5005 		 */
5006 		if ((pmb->mbxCommand == MBX_DUMP_MEMORY) &&
5007 			(pmb->un.varDmp.type != DMP_WELL_KNOWN)) {
5008 			/* rebuild the command for sli4 using our own buffers
5009 			* like we do for biu diags
5010 			*/
5011 			receive_length = pmb->un.varWords[2];
5012 			/* receive length cannot be greater than mailbox
5013 			 * extension size
5014 			 */
5015 			if (receive_length == 0) {
5016 				rc = -ERANGE;
5017 				goto job_done;
5018 			}
5019 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
5020 						+ sizeof(MAILBOX_t));
5021 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
5022 						+ sizeof(MAILBOX_t));
5023 		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
5024 			pmb->un.varUpdateCfg.co) {
5025 			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
5026 
5027 			/* bde size cannot be greater than mailbox ext size */
5028 			if (bde->tus.f.bdeSize >
5029 			    BSG_MBOX_SIZE - sizeof(MAILBOX_t)) {
5030 				rc = -ERANGE;
5031 				goto job_done;
5032 			}
5033 			bde->addrHigh = putPaddrHigh(dmabuf->phys
5034 						+ sizeof(MAILBOX_t));
5035 			bde->addrLow = putPaddrLow(dmabuf->phys
5036 						+ sizeof(MAILBOX_t));
5037 		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
5038 			/* Handling non-embedded SLI_CONFIG mailbox command */
5039 			sli4_config = &pmboxq->u.mqe.un.sli4_config;
5040 			if (!bf_get(lpfc_mbox_hdr_emb,
5041 			    &sli4_config->header.cfg_mhdr)) {
5042 				/* rebuild the command for sli4 using our
5043 				 * own buffers like we do for biu diags
5044 				 */
5045 				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
5046 						&pmb->un.varWords[0];
5047 				receive_length = nembed_sge->sge[0].length;
5048 
5049 				/* receive length cannot be greater than
5050 				 * mailbox extension size
5051 				 */
5052 				if ((receive_length == 0) ||
5053 				    (receive_length >
5054 				     BSG_MBOX_SIZE - sizeof(MAILBOX_t))) {
5055 					rc = -ERANGE;
5056 					goto job_done;
5057 				}
5058 
5059 				nembed_sge->sge[0].pa_hi =
5060 						putPaddrHigh(dmabuf->phys
5061 						   + sizeof(MAILBOX_t));
5062 				nembed_sge->sge[0].pa_lo =
5063 						putPaddrLow(dmabuf->phys
5064 						   + sizeof(MAILBOX_t));
5065 			}
5066 		}
5067 	}
5068 
5069 	dd_data->context_un.mbox.dmabuffers = dmabuf;
5070 
5071 	/* setup wake call as IOCB callback */
5072 	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
5073 
5074 	/* setup context field to pass wait_queue pointer to wake function */
5075 	pmboxq->ctx_u.dd_data = dd_data;
5076 	dd_data->type = TYPE_MBOX;
5077 	dd_data->set_job = job;
5078 	dd_data->context_un.mbox.pmboxq = pmboxq;
5079 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
5080 	dd_data->context_un.mbox.ext = ext;
5081 	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
5082 	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
5083 	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
5084 	job->dd_data = dd_data;
5085 
5086 	if (test_bit(FC_OFFLINE_MODE, &vport->fc_flag) ||
5087 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
5088 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
5089 		if (rc != MBX_SUCCESS) {
5090 			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
5091 			goto job_done;
5092 		}
5093 
5094 		/* job finished, copy the data */
5095 		memcpy(pmbx, pmb, sizeof(*pmb));
5096 		bsg_reply->reply_payload_rcv_len =
5097 			sg_copy_from_buffer(job->reply_payload.sg_list,
5098 					    job->reply_payload.sg_cnt,
5099 					    pmbx, size);
5100 		/* not waiting mbox already done */
5101 		rc = 0;
5102 		goto job_done;
5103 	}
5104 
5105 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
5106 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
5107 		return 1; /* job started */
5108 
5109 job_done:
5110 	/* common exit for error or job completed inline */
5111 	if (pmboxq)
5112 		mempool_free(pmboxq, phba->mbox_mem_pool);
5113 	lpfc_bsg_dma_page_free(phba, dmabuf);
5114 	kfree(dd_data);
5115 
5116 job_cont:
5117 	return rc;
5118 }
5119 
5120 /**
5121  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
5122  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
5123  **/
5124 static int
5125 lpfc_bsg_mbox_cmd(struct bsg_job *job)
5126 {
5127 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5128 	struct fc_bsg_request *bsg_request = job->request;
5129 	struct fc_bsg_reply *bsg_reply = job->reply;
5130 	struct lpfc_hba *phba = vport->phba;
5131 	struct dfc_mbox_req *mbox_req;
5132 	int rc = 0;
5133 
5134 	/* mix-and-match backward compatibility */
5135 	bsg_reply->reply_payload_rcv_len = 0;
5136 	if (job->request_len <
5137 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
5138 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
5139 				"2737 Mix-and-match backward compatibility "
5140 				"between MBOX_REQ old size:%d and "
5141 				"new request size:%d\n",
5142 				(int)(job->request_len -
5143 				      sizeof(struct fc_bsg_request)),
5144 				(int)sizeof(struct dfc_mbox_req));
5145 		mbox_req = (struct dfc_mbox_req *)
5146 				bsg_request->rqst_data.h_vendor.vendor_cmd;
5147 		mbox_req->extMboxTag = 0;
5148 		mbox_req->extSeqNum = 0;
5149 	}
5150 
5151 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
5152 
5153 	if (rc == 0) {
5154 		/* job done */
5155 		bsg_reply->result = 0;
5156 		job->dd_data = NULL;
5157 		bsg_job_done(job, bsg_reply->result,
5158 			       bsg_reply->reply_payload_rcv_len);
5159 	} else if (rc == 1)
5160 		/* job submitted, will complete later*/
5161 		rc = 0; /* return zero, no error */
5162 	else {
5163 		/* some error occurred */
5164 		bsg_reply->result = rc;
5165 		job->dd_data = NULL;
5166 	}
5167 
5168 	return rc;
5169 }
5170 
5171 static int
5172 lpfc_forced_link_speed(struct bsg_job *job)
5173 {
5174 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5175 	struct lpfc_vport *vport = shost_priv(shost);
5176 	struct lpfc_hba *phba = vport->phba;
5177 	struct fc_bsg_reply *bsg_reply = job->reply;
5178 	struct forced_link_speed_support_reply *forced_reply;
5179 	int rc = 0;
5180 
5181 	if (job->request_len <
5182 	    sizeof(struct fc_bsg_request) +
5183 	    sizeof(struct get_forced_link_speed_support)) {
5184 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5185 				"0048 Received FORCED_LINK_SPEED request "
5186 				"below minimum size\n");
5187 		rc = -EINVAL;
5188 		goto job_error;
5189 	}
5190 
5191 	forced_reply = (struct forced_link_speed_support_reply *)
5192 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5193 
5194 	if (job->reply_len < sizeof(*bsg_reply) + sizeof(*forced_reply)) {
5195 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5196 				"0049 Received FORCED_LINK_SPEED reply below "
5197 				"minimum size\n");
5198 		rc = -EINVAL;
5199 		goto job_error;
5200 	}
5201 
5202 	forced_reply->supported = test_bit(HBA_FORCED_LINK_SPEED,
5203 					   &phba->hba_flag)
5204 				   ? LPFC_FORCED_LINK_SPEED_SUPPORTED
5205 				   : LPFC_FORCED_LINK_SPEED_NOT_SUPPORTED;
5206 job_error:
5207 	bsg_reply->result = rc;
5208 	if (rc == 0)
5209 		bsg_job_done(job, bsg_reply->result,
5210 			       bsg_reply->reply_payload_rcv_len);
5211 	return rc;
5212 }
5213 
5214 /**
5215  * lpfc_check_fwlog_support: Check FW log support on the adapter
5216  * @phba: Pointer to HBA context object.
5217  *
5218  * Check if FW Logging support by the adapter
5219  **/
5220 int
5221 lpfc_check_fwlog_support(struct lpfc_hba *phba)
5222 {
5223 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5224 
5225 	ras_fwlog = &phba->ras_fwlog;
5226 
5227 	if (!ras_fwlog->ras_hwsupport)
5228 		return -EACCES;
5229 	else if (!ras_fwlog->ras_enabled)
5230 		return -EPERM;
5231 	else
5232 		return 0;
5233 }
5234 
5235 /**
5236  * lpfc_bsg_get_ras_config: Get RAS configuration settings
5237  * @job: fc_bsg_job to handle
5238  *
5239  * Get RAS configuration values set.
5240  **/
5241 static int
5242 lpfc_bsg_get_ras_config(struct bsg_job *job)
5243 {
5244 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5245 	struct lpfc_vport *vport = shost_priv(shost);
5246 	struct fc_bsg_reply *bsg_reply = job->reply;
5247 	struct lpfc_hba *phba = vport->phba;
5248 	struct lpfc_bsg_get_ras_config_reply *ras_reply;
5249 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5250 	int rc = 0;
5251 
5252 	if (job->request_len <
5253 	    sizeof(struct fc_bsg_request) +
5254 	    sizeof(struct lpfc_bsg_ras_req)) {
5255 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5256 				"6192 FW_LOG request received "
5257 				"below minimum size\n");
5258 		rc = -EINVAL;
5259 		goto ras_job_error;
5260 	}
5261 
5262 	/* Check FW log status */
5263 	rc = lpfc_check_fwlog_support(phba);
5264 	if (rc)
5265 		goto ras_job_error;
5266 
5267 	ras_reply = (struct lpfc_bsg_get_ras_config_reply *)
5268 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5269 
5270 	/* Current logging state */
5271 	spin_lock_irq(&phba->ras_fwlog_lock);
5272 	if (ras_fwlog->state == ACTIVE)
5273 		ras_reply->state = LPFC_RASLOG_STATE_RUNNING;
5274 	else
5275 		ras_reply->state = LPFC_RASLOG_STATE_STOPPED;
5276 	spin_unlock_irq(&phba->ras_fwlog_lock);
5277 
5278 	ras_reply->log_level = phba->ras_fwlog.fw_loglevel;
5279 	ras_reply->log_buff_sz = phba->cfg_ras_fwlog_buffsize;
5280 
5281 ras_job_error:
5282 	/* make error code available to userspace */
5283 	bsg_reply->result = rc;
5284 
5285 	/* complete the job back to userspace */
5286 	if (!rc)
5287 		bsg_job_done(job, bsg_reply->result,
5288 			     bsg_reply->reply_payload_rcv_len);
5289 	return rc;
5290 }
5291 
5292 /**
5293  * lpfc_bsg_set_ras_config: Set FW logging parameters
5294  * @job: fc_bsg_job to handle
5295  *
5296  * Set log-level parameters for FW-logging in host memory
5297  **/
5298 static int
5299 lpfc_bsg_set_ras_config(struct bsg_job *job)
5300 {
5301 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5302 	struct lpfc_vport *vport = shost_priv(shost);
5303 	struct lpfc_hba *phba = vport->phba;
5304 	struct lpfc_bsg_set_ras_config_req *ras_req;
5305 	struct fc_bsg_request *bsg_request = job->request;
5306 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5307 	struct fc_bsg_reply *bsg_reply = job->reply;
5308 	uint8_t action = 0, log_level = 0;
5309 	int rc = 0, action_status = 0;
5310 
5311 	if (job->request_len <
5312 	    sizeof(struct fc_bsg_request) +
5313 	    sizeof(struct lpfc_bsg_set_ras_config_req)) {
5314 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5315 				"6182 Received RAS_LOG request "
5316 				"below minimum size\n");
5317 		rc = -EINVAL;
5318 		goto ras_job_error;
5319 	}
5320 
5321 	/* Check FW log status */
5322 	rc = lpfc_check_fwlog_support(phba);
5323 	if (rc)
5324 		goto ras_job_error;
5325 
5326 	ras_req = (struct lpfc_bsg_set_ras_config_req *)
5327 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5328 	action = ras_req->action;
5329 	log_level = ras_req->log_level;
5330 
5331 	if (action == LPFC_RASACTION_STOP_LOGGING) {
5332 		/* Check if already disabled */
5333 		spin_lock_irq(&phba->ras_fwlog_lock);
5334 		if (ras_fwlog->state != ACTIVE) {
5335 			spin_unlock_irq(&phba->ras_fwlog_lock);
5336 			rc = -ESRCH;
5337 			goto ras_job_error;
5338 		}
5339 		spin_unlock_irq(&phba->ras_fwlog_lock);
5340 
5341 		/* Disable logging */
5342 		lpfc_ras_stop_fwlog(phba);
5343 	} else {
5344 		/*action = LPFC_RASACTION_START_LOGGING*/
5345 
5346 		/* Even though FW-logging is active re-initialize
5347 		 * FW-logging with new log-level. Return status
5348 		 * "Logging already Running" to caller.
5349 		 **/
5350 		spin_lock_irq(&phba->ras_fwlog_lock);
5351 		if (ras_fwlog->state != INACTIVE)
5352 			action_status = -EINPROGRESS;
5353 		spin_unlock_irq(&phba->ras_fwlog_lock);
5354 
5355 		/* Enable logging */
5356 		rc = lpfc_sli4_ras_fwlog_init(phba, log_level,
5357 					      LPFC_RAS_ENABLE_LOGGING);
5358 		if (rc) {
5359 			rc = -EINVAL;
5360 			goto ras_job_error;
5361 		}
5362 
5363 		/* Check if FW-logging is re-initialized */
5364 		if (action_status == -EINPROGRESS)
5365 			rc = action_status;
5366 	}
5367 ras_job_error:
5368 	/* make error code available to userspace */
5369 	bsg_reply->result = rc;
5370 
5371 	/* complete the job back to userspace */
5372 	if (!rc)
5373 		bsg_job_done(job, bsg_reply->result,
5374 			     bsg_reply->reply_payload_rcv_len);
5375 
5376 	return rc;
5377 }
5378 
5379 /**
5380  * lpfc_bsg_get_ras_lwpd: Get log write position data
5381  * @job: fc_bsg_job to handle
5382  *
5383  * Get Offset/Wrap count of the log message written
5384  * in host memory
5385  **/
5386 static int
5387 lpfc_bsg_get_ras_lwpd(struct bsg_job *job)
5388 {
5389 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5390 	struct lpfc_vport *vport = shost_priv(shost);
5391 	struct lpfc_bsg_get_ras_lwpd *ras_reply;
5392 	struct lpfc_hba *phba = vport->phba;
5393 	struct lpfc_ras_fwlog *ras_fwlog = &phba->ras_fwlog;
5394 	struct fc_bsg_reply *bsg_reply = job->reply;
5395 	u32 *lwpd_ptr = NULL;
5396 	int rc = 0;
5397 
5398 	rc = lpfc_check_fwlog_support(phba);
5399 	if (rc)
5400 		goto ras_job_error;
5401 
5402 	if (job->request_len <
5403 	    sizeof(struct fc_bsg_request) +
5404 	    sizeof(struct lpfc_bsg_ras_req)) {
5405 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5406 				"6183 Received RAS_LOG request "
5407 				"below minimum size\n");
5408 		rc = -EINVAL;
5409 		goto ras_job_error;
5410 	}
5411 
5412 	ras_reply = (struct lpfc_bsg_get_ras_lwpd *)
5413 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5414 
5415 	if (!ras_fwlog->lwpd.virt) {
5416 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5417 				"6193 Restart FW Logging\n");
5418 		rc = -EINVAL;
5419 		goto ras_job_error;
5420 	}
5421 
5422 	/* Get lwpd offset */
5423 	lwpd_ptr = (uint32_t *)(ras_fwlog->lwpd.virt);
5424 	ras_reply->offset = be32_to_cpu(*lwpd_ptr & 0xffffffff);
5425 
5426 	/* Get wrap count */
5427 	ras_reply->wrap_count = be32_to_cpu(*(++lwpd_ptr) & 0xffffffff);
5428 
5429 ras_job_error:
5430 	/* make error code available to userspace */
5431 	bsg_reply->result = rc;
5432 
5433 	/* complete the job back to userspace */
5434 	if (!rc)
5435 		bsg_job_done(job, bsg_reply->result,
5436 			     bsg_reply->reply_payload_rcv_len);
5437 
5438 	return rc;
5439 }
5440 
5441 /**
5442  * lpfc_bsg_get_ras_fwlog: Read FW log
5443  * @job: fc_bsg_job to handle
5444  *
5445  * Copy the FW log into the passed buffer.
5446  **/
5447 static int
5448 lpfc_bsg_get_ras_fwlog(struct bsg_job *job)
5449 {
5450 	struct Scsi_Host *shost = fc_bsg_to_shost(job);
5451 	struct lpfc_vport *vport = shost_priv(shost);
5452 	struct lpfc_hba *phba = vport->phba;
5453 	struct fc_bsg_request *bsg_request = job->request;
5454 	struct fc_bsg_reply *bsg_reply = job->reply;
5455 	struct lpfc_bsg_get_fwlog_req *ras_req;
5456 	u32 rd_offset, rd_index, offset;
5457 	void *src, *fwlog_buff;
5458 	struct lpfc_ras_fwlog *ras_fwlog = NULL;
5459 	struct lpfc_dmabuf *dmabuf, *next;
5460 	int rc = 0;
5461 
5462 	ras_fwlog = &phba->ras_fwlog;
5463 
5464 	rc = lpfc_check_fwlog_support(phba);
5465 	if (rc)
5466 		goto ras_job_error;
5467 
5468 	/* Logging to be stopped before reading */
5469 	spin_lock_irq(&phba->ras_fwlog_lock);
5470 	if (ras_fwlog->state == ACTIVE) {
5471 		spin_unlock_irq(&phba->ras_fwlog_lock);
5472 		rc = -EINPROGRESS;
5473 		goto ras_job_error;
5474 	}
5475 	spin_unlock_irq(&phba->ras_fwlog_lock);
5476 
5477 	if (job->request_len <
5478 	    sizeof(struct fc_bsg_request) +
5479 	    sizeof(struct lpfc_bsg_get_fwlog_req)) {
5480 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5481 				"6184 Received RAS_LOG request "
5482 				"below minimum size\n");
5483 		rc = -EINVAL;
5484 		goto ras_job_error;
5485 	}
5486 
5487 	ras_req = (struct lpfc_bsg_get_fwlog_req *)
5488 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5489 	rd_offset = ras_req->read_offset;
5490 
5491 	/* Allocate memory to read fw log*/
5492 	fwlog_buff = vmalloc(ras_req->read_size);
5493 	if (!fwlog_buff) {
5494 		rc = -ENOMEM;
5495 		goto ras_job_error;
5496 	}
5497 
5498 	rd_index = (rd_offset / LPFC_RAS_MAX_ENTRY_SIZE);
5499 	offset = (rd_offset % LPFC_RAS_MAX_ENTRY_SIZE);
5500 
5501 	list_for_each_entry_safe(dmabuf, next,
5502 			      &ras_fwlog->fwlog_buff_list, list) {
5503 
5504 		if (dmabuf->buffer_tag < rd_index)
5505 			continue;
5506 
5507 		src = dmabuf->virt + offset;
5508 		memcpy(fwlog_buff, src, ras_req->read_size);
5509 		break;
5510 	}
5511 
5512 	bsg_reply->reply_payload_rcv_len =
5513 		sg_copy_from_buffer(job->reply_payload.sg_list,
5514 				    job->reply_payload.sg_cnt,
5515 				    fwlog_buff, ras_req->read_size);
5516 
5517 	vfree(fwlog_buff);
5518 
5519 ras_job_error:
5520 	bsg_reply->result = rc;
5521 	if (!rc)
5522 		bsg_job_done(job, bsg_reply->result,
5523 			     bsg_reply->reply_payload_rcv_len);
5524 
5525 	return rc;
5526 }
5527 
5528 static int
5529 lpfc_get_trunk_info(struct bsg_job *job)
5530 {
5531 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5532 	struct lpfc_hba *phba = vport->phba;
5533 	struct fc_bsg_reply *bsg_reply = job->reply;
5534 	struct lpfc_trunk_info *event_reply;
5535 	int rc = 0;
5536 
5537 	if (job->request_len <
5538 	    sizeof(struct fc_bsg_request) + sizeof(struct get_trunk_info_req)) {
5539 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5540 				"2744 Received GET TRUNK _INFO request below "
5541 				"minimum size\n");
5542 		rc = -EINVAL;
5543 		goto job_error;
5544 	}
5545 
5546 	event_reply = (struct lpfc_trunk_info *)
5547 		bsg_reply->reply_data.vendor_reply.vendor_rsp;
5548 
5549 	if (job->reply_len < sizeof(*bsg_reply) + sizeof(*event_reply)) {
5550 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
5551 				"2728 Received GET TRUNK _INFO reply below "
5552 				"minimum size\n");
5553 		rc = -EINVAL;
5554 		goto job_error;
5555 	}
5556 	if (event_reply == NULL) {
5557 		rc = -EINVAL;
5558 		goto job_error;
5559 	}
5560 
5561 	bsg_bf_set(lpfc_trunk_info_link_status, event_reply,
5562 		   (phba->link_state >= LPFC_LINK_UP) ? 1 : 0);
5563 
5564 	bsg_bf_set(lpfc_trunk_info_trunk_active0, event_reply,
5565 		   (phba->trunk_link.link0.state == LPFC_LINK_UP) ? 1 : 0);
5566 
5567 	bsg_bf_set(lpfc_trunk_info_trunk_active1, event_reply,
5568 		   (phba->trunk_link.link1.state == LPFC_LINK_UP) ? 1 : 0);
5569 
5570 	bsg_bf_set(lpfc_trunk_info_trunk_active2, event_reply,
5571 		   (phba->trunk_link.link2.state == LPFC_LINK_UP) ? 1 : 0);
5572 
5573 	bsg_bf_set(lpfc_trunk_info_trunk_active3, event_reply,
5574 		   (phba->trunk_link.link3.state == LPFC_LINK_UP) ? 1 : 0);
5575 
5576 	bsg_bf_set(lpfc_trunk_info_trunk_config0, event_reply,
5577 		   bf_get(lpfc_conf_trunk_port0, &phba->sli4_hba));
5578 
5579 	bsg_bf_set(lpfc_trunk_info_trunk_config1, event_reply,
5580 		   bf_get(lpfc_conf_trunk_port1, &phba->sli4_hba));
5581 
5582 	bsg_bf_set(lpfc_trunk_info_trunk_config2, event_reply,
5583 		   bf_get(lpfc_conf_trunk_port2, &phba->sli4_hba));
5584 
5585 	bsg_bf_set(lpfc_trunk_info_trunk_config3, event_reply,
5586 		   bf_get(lpfc_conf_trunk_port3, &phba->sli4_hba));
5587 
5588 	event_reply->port_speed = phba->sli4_hba.link_state.speed / 1000;
5589 	event_reply->logical_speed =
5590 				phba->sli4_hba.link_state.logical_speed / 1000;
5591 job_error:
5592 	bsg_reply->result = rc;
5593 	if (!rc)
5594 		bsg_job_done(job, bsg_reply->result,
5595 			     bsg_reply->reply_payload_rcv_len);
5596 	return rc;
5597 
5598 }
5599 
5600 static int
5601 lpfc_get_cgnbuf_info(struct bsg_job *job)
5602 {
5603 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5604 	struct lpfc_hba *phba = vport->phba;
5605 	struct fc_bsg_request *bsg_request = job->request;
5606 	struct fc_bsg_reply *bsg_reply = job->reply;
5607 	struct get_cgnbuf_info_req *cgnbuf_req;
5608 	struct lpfc_cgn_info *cp;
5609 	uint8_t *cgn_buff;
5610 	size_t size, cinfosz;
5611 	int  rc = 0;
5612 
5613 	if (job->request_len < sizeof(struct fc_bsg_request) +
5614 	    sizeof(struct get_cgnbuf_info_req)) {
5615 		rc = -ENOMEM;
5616 		goto job_exit;
5617 	}
5618 
5619 	if (!phba->sli4_hba.pc_sli4_params.cmf) {
5620 		rc = -ENOENT;
5621 		goto job_exit;
5622 	}
5623 
5624 	if (!phba->cgn_i || !phba->cgn_i->virt) {
5625 		rc = -ENOENT;
5626 		goto job_exit;
5627 	}
5628 
5629 	cp = phba->cgn_i->virt;
5630 	if (cp->cgn_info_version < LPFC_CGN_INFO_V3) {
5631 		rc = -EPERM;
5632 		goto job_exit;
5633 	}
5634 
5635 	cgnbuf_req = (struct get_cgnbuf_info_req *)
5636 		bsg_request->rqst_data.h_vendor.vendor_cmd;
5637 
5638 	/* For reset or size == 0 */
5639 	bsg_reply->reply_payload_rcv_len = 0;
5640 
5641 	if (cgnbuf_req->reset == LPFC_BSG_CGN_RESET_STAT) {
5642 		lpfc_init_congestion_stat(phba);
5643 		goto job_exit;
5644 	}
5645 
5646 	/* We don't want to include the CRC at the end */
5647 	cinfosz = sizeof(struct lpfc_cgn_info) - sizeof(uint32_t);
5648 
5649 	size = cgnbuf_req->read_size;
5650 	if (!size)
5651 		goto job_exit;
5652 
5653 	if (size < cinfosz) {
5654 		/* Just copy back what we can */
5655 		cinfosz = size;
5656 		rc = -E2BIG;
5657 	}
5658 
5659 	/* Allocate memory to read congestion info */
5660 	cgn_buff = vmalloc(cinfosz);
5661 	if (!cgn_buff) {
5662 		rc = -ENOMEM;
5663 		goto job_exit;
5664 	}
5665 
5666 	memcpy(cgn_buff, cp, cinfosz);
5667 
5668 	bsg_reply->reply_payload_rcv_len =
5669 		sg_copy_from_buffer(job->reply_payload.sg_list,
5670 				    job->reply_payload.sg_cnt,
5671 				    cgn_buff, cinfosz);
5672 
5673 	vfree(cgn_buff);
5674 
5675 job_exit:
5676 	bsg_reply->result = rc;
5677 	if (!rc)
5678 		bsg_job_done(job, bsg_reply->result,
5679 			     bsg_reply->reply_payload_rcv_len);
5680 	else
5681 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
5682 				"2724 GET CGNBUF error: %d\n", rc);
5683 	return rc;
5684 }
5685 
5686 /**
5687  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
5688  * @job: fc_bsg_job to handle
5689  **/
5690 static int
5691 lpfc_bsg_hst_vendor(struct bsg_job *job)
5692 {
5693 	struct fc_bsg_request *bsg_request = job->request;
5694 	struct fc_bsg_reply *bsg_reply = job->reply;
5695 	int command = bsg_request->rqst_data.h_vendor.vendor_cmd[0];
5696 	int rc;
5697 
5698 	switch (command) {
5699 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
5700 		rc = lpfc_bsg_hba_set_event(job);
5701 		break;
5702 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
5703 		rc = lpfc_bsg_hba_get_event(job);
5704 		break;
5705 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
5706 		rc = lpfc_bsg_send_mgmt_rsp(job);
5707 		break;
5708 	case LPFC_BSG_VENDOR_DIAG_MODE:
5709 		rc = lpfc_bsg_diag_loopback_mode(job);
5710 		break;
5711 	case LPFC_BSG_VENDOR_DIAG_MODE_END:
5712 		rc = lpfc_sli4_bsg_diag_mode_end(job);
5713 		break;
5714 	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
5715 		rc = lpfc_bsg_diag_loopback_run(job);
5716 		break;
5717 	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
5718 		rc = lpfc_sli4_bsg_link_diag_test(job);
5719 		break;
5720 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
5721 		rc = lpfc_bsg_get_dfc_rev(job);
5722 		break;
5723 	case LPFC_BSG_VENDOR_MBOX:
5724 		rc = lpfc_bsg_mbox_cmd(job);
5725 		break;
5726 	case LPFC_BSG_VENDOR_FORCED_LINK_SPEED:
5727 		rc = lpfc_forced_link_speed(job);
5728 		break;
5729 	case LPFC_BSG_VENDOR_RAS_GET_LWPD:
5730 		rc = lpfc_bsg_get_ras_lwpd(job);
5731 		break;
5732 	case LPFC_BSG_VENDOR_RAS_GET_FWLOG:
5733 		rc = lpfc_bsg_get_ras_fwlog(job);
5734 		break;
5735 	case LPFC_BSG_VENDOR_RAS_GET_CONFIG:
5736 		rc = lpfc_bsg_get_ras_config(job);
5737 		break;
5738 	case LPFC_BSG_VENDOR_RAS_SET_CONFIG:
5739 		rc = lpfc_bsg_set_ras_config(job);
5740 		break;
5741 	case LPFC_BSG_VENDOR_GET_TRUNK_INFO:
5742 		rc = lpfc_get_trunk_info(job);
5743 		break;
5744 	case LPFC_BSG_VENDOR_GET_CGNBUF_INFO:
5745 		rc = lpfc_get_cgnbuf_info(job);
5746 		break;
5747 	default:
5748 		rc = -EINVAL;
5749 		bsg_reply->reply_payload_rcv_len = 0;
5750 		/* make error code available to userspace */
5751 		bsg_reply->result = rc;
5752 		break;
5753 	}
5754 
5755 	return rc;
5756 }
5757 
5758 /**
5759  * lpfc_bsg_request - handle a bsg request from the FC transport
5760  * @job: bsg_job to handle
5761  **/
5762 int
5763 lpfc_bsg_request(struct bsg_job *job)
5764 {
5765 	struct fc_bsg_request *bsg_request = job->request;
5766 	struct fc_bsg_reply *bsg_reply = job->reply;
5767 	uint32_t msgcode;
5768 	int rc;
5769 
5770 	msgcode = bsg_request->msgcode;
5771 	switch (msgcode) {
5772 	case FC_BSG_HST_VENDOR:
5773 		rc = lpfc_bsg_hst_vendor(job);
5774 		break;
5775 	case FC_BSG_RPT_ELS:
5776 		rc = lpfc_bsg_rport_els(job);
5777 		break;
5778 	case FC_BSG_RPT_CT:
5779 		rc = lpfc_bsg_send_mgmt_cmd(job);
5780 		break;
5781 	default:
5782 		rc = -EINVAL;
5783 		bsg_reply->reply_payload_rcv_len = 0;
5784 		/* make error code available to userspace */
5785 		bsg_reply->result = rc;
5786 		break;
5787 	}
5788 
5789 	return rc;
5790 }
5791 
5792 /**
5793  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
5794  * @job: bsg_job that has timed out
5795  *
5796  * This function just aborts the job's IOCB.  The aborted IOCB will return to
5797  * the waiting function which will handle passing the error back to userspace
5798  **/
5799 int
5800 lpfc_bsg_timeout(struct bsg_job *job)
5801 {
5802 	struct lpfc_vport *vport = shost_priv(fc_bsg_to_shost(job));
5803 	struct lpfc_hba *phba = vport->phba;
5804 	struct lpfc_iocbq *cmdiocb;
5805 	struct lpfc_sli_ring *pring;
5806 	struct bsg_job_data *dd_data;
5807 	unsigned long flags;
5808 	int rc = 0;
5809 	LIST_HEAD(completions);
5810 	struct lpfc_iocbq *check_iocb, *next_iocb;
5811 
5812 	pring = lpfc_phba_elsring(phba);
5813 	if (unlikely(!pring))
5814 		return -EIO;
5815 
5816 	/* if job's driver data is NULL, the command completed or is in the
5817 	 * the process of completing.  In this case, return status to request
5818 	 * so the timeout is retried.  This avoids double completion issues
5819 	 * and the request will be pulled off the timer queue when the
5820 	 * command's completion handler executes.  Otherwise, prevent the
5821 	 * command's completion handler from executing the job done callback
5822 	 * and continue processing to abort the outstanding the command.
5823 	 */
5824 
5825 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
5826 	dd_data = (struct bsg_job_data *)job->dd_data;
5827 	if (dd_data) {
5828 		dd_data->set_job = NULL;
5829 		job->dd_data = NULL;
5830 	} else {
5831 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5832 		return -EAGAIN;
5833 	}
5834 
5835 	switch (dd_data->type) {
5836 	case TYPE_IOCB:
5837 		/* Check to see if IOCB was issued to the port or not. If not,
5838 		 * remove it from the txq queue and call cancel iocbs.
5839 		 * Otherwise, call abort iotag
5840 		 */
5841 		cmdiocb = dd_data->context_un.iocb.cmdiocbq;
5842 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5843 
5844 		spin_lock_irqsave(&phba->hbalock, flags);
5845 		/* make sure the I/O abort window is still open */
5846 		if (!(cmdiocb->cmd_flag & LPFC_IO_CMD_OUTSTANDING)) {
5847 			spin_unlock_irqrestore(&phba->hbalock, flags);
5848 			return -EAGAIN;
5849 		}
5850 		list_for_each_entry_safe(check_iocb, next_iocb, &pring->txq,
5851 					 list) {
5852 			if (check_iocb == cmdiocb) {
5853 				list_move_tail(&check_iocb->list, &completions);
5854 				break;
5855 			}
5856 		}
5857 		if (list_empty(&completions))
5858 			lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb, NULL);
5859 		spin_unlock_irqrestore(&phba->hbalock, flags);
5860 		if (!list_empty(&completions)) {
5861 			lpfc_sli_cancel_iocbs(phba, &completions,
5862 					      IOSTAT_LOCAL_REJECT,
5863 					      IOERR_SLI_ABORTED);
5864 		}
5865 		break;
5866 
5867 	case TYPE_EVT:
5868 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5869 		break;
5870 
5871 	case TYPE_MBOX:
5872 		/* Update the ext buf ctx state if needed */
5873 
5874 		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
5875 			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
5876 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5877 		break;
5878 	default:
5879 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
5880 		break;
5881 	}
5882 
5883 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
5884 	 * otherwise an error message will be displayed on the console
5885 	 * so always return success (zero)
5886 	 */
5887 	return rc;
5888 }
5889