xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_bsg.c (revision 7fe2f639)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2009-2011 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  *                                                                 *
8  * This program is free software; you can redistribute it and/or   *
9  * modify it under the terms of version 2 of the GNU General       *
10  * Public License as published by the Free Software Foundation.    *
11  * This program is distributed in the hope that it will be useful. *
12  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
13  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
14  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
15  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
16  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
17  * more details, a copy of which can be found in the file COPYING  *
18  * included with this package.                                     *
19  *******************************************************************/
20 
21 #include <linux/interrupt.h>
22 #include <linux/mempool.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/delay.h>
26 #include <linux/list.h>
27 
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_transport_fc.h>
31 #include <scsi/scsi_bsg_fc.h>
32 #include <scsi/fc/fc_fs.h>
33 
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_bsg.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_vport.h"
46 #include "lpfc_version.h"
47 
48 struct lpfc_bsg_event {
49 	struct list_head node;
50 	struct kref kref;
51 	wait_queue_head_t wq;
52 
53 	/* Event type and waiter identifiers */
54 	uint32_t type_mask;
55 	uint32_t req_id;
56 	uint32_t reg_id;
57 
58 	/* next two flags are here for the auto-delete logic */
59 	unsigned long wait_time_stamp;
60 	int waiting;
61 
62 	/* seen and not seen events */
63 	struct list_head events_to_get;
64 	struct list_head events_to_see;
65 
66 	/* job waiting for this event to finish */
67 	struct fc_bsg_job *set_job;
68 };
69 
70 struct lpfc_bsg_iocb {
71 	struct lpfc_iocbq *cmdiocbq;
72 	struct lpfc_iocbq *rspiocbq;
73 	struct lpfc_dmabuf *bmp;
74 	struct lpfc_nodelist *ndlp;
75 
76 	/* job waiting for this iocb to finish */
77 	struct fc_bsg_job *set_job;
78 };
79 
80 struct lpfc_bsg_mbox {
81 	LPFC_MBOXQ_t *pmboxq;
82 	MAILBOX_t *mb;
83 	struct lpfc_dmabuf *dmabuffers; /* for BIU diags */
84 	uint8_t *ext; /* extended mailbox data */
85 	uint32_t mbOffset; /* from app */
86 	uint32_t inExtWLen; /* from app */
87 	uint32_t outExtWLen; /* from app */
88 
89 	/* job waiting for this mbox command to finish */
90 	struct fc_bsg_job *set_job;
91 };
92 
93 #define MENLO_DID 0x0000FC0E
94 
95 struct lpfc_bsg_menlo {
96 	struct lpfc_iocbq *cmdiocbq;
97 	struct lpfc_iocbq *rspiocbq;
98 	struct lpfc_dmabuf *bmp;
99 
100 	/* job waiting for this iocb to finish */
101 	struct fc_bsg_job *set_job;
102 };
103 
104 #define TYPE_EVT 	1
105 #define TYPE_IOCB	2
106 #define TYPE_MBOX	3
107 #define TYPE_MENLO	4
108 struct bsg_job_data {
109 	uint32_t type;
110 	union {
111 		struct lpfc_bsg_event *evt;
112 		struct lpfc_bsg_iocb iocb;
113 		struct lpfc_bsg_mbox mbox;
114 		struct lpfc_bsg_menlo menlo;
115 	} context_un;
116 };
117 
118 struct event_data {
119 	struct list_head node;
120 	uint32_t type;
121 	uint32_t immed_dat;
122 	void *data;
123 	uint32_t len;
124 };
125 
126 #define BUF_SZ_4K 4096
127 #define SLI_CT_ELX_LOOPBACK 0x10
128 
129 enum ELX_LOOPBACK_CMD {
130 	ELX_LOOPBACK_XRI_SETUP,
131 	ELX_LOOPBACK_DATA,
132 };
133 
134 #define ELX_LOOPBACK_HEADER_SZ \
135 	(size_t)(&((struct lpfc_sli_ct_request *)NULL)->un)
136 
137 struct lpfc_dmabufext {
138 	struct lpfc_dmabuf dma;
139 	uint32_t size;
140 	uint32_t flag;
141 };
142 
143 /**
144  * lpfc_bsg_send_mgmt_cmd_cmp - lpfc_bsg_send_mgmt_cmd's completion handler
145  * @phba: Pointer to HBA context object.
146  * @cmdiocbq: Pointer to command iocb.
147  * @rspiocbq: Pointer to response iocb.
148  *
149  * This function is the completion handler for iocbs issued using
150  * lpfc_bsg_send_mgmt_cmd function. This function is called by the
151  * ring event handler function without any lock held. This function
152  * can be called from both worker thread context and interrupt
153  * context. This function also can be called from another thread which
154  * cleans up the SLI layer objects.
155  * This function copies the contents of the response iocb to the
156  * response iocb memory object provided by the caller of
157  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
158  * sleeps for the iocb completion.
159  **/
160 static void
161 lpfc_bsg_send_mgmt_cmd_cmp(struct lpfc_hba *phba,
162 			struct lpfc_iocbq *cmdiocbq,
163 			struct lpfc_iocbq *rspiocbq)
164 {
165 	struct bsg_job_data *dd_data;
166 	struct fc_bsg_job *job;
167 	IOCB_t *rsp;
168 	struct lpfc_dmabuf *bmp;
169 	struct lpfc_nodelist *ndlp;
170 	struct lpfc_bsg_iocb *iocb;
171 	unsigned long flags;
172 	int rc = 0;
173 
174 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
175 	dd_data = cmdiocbq->context2;
176 	if (!dd_data) {
177 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
178 		lpfc_sli_release_iocbq(phba, cmdiocbq);
179 		return;
180 	}
181 
182 	iocb = &dd_data->context_un.iocb;
183 	job = iocb->set_job;
184 	job->dd_data = NULL; /* so timeout handler does not reply */
185 
186 	bmp = iocb->bmp;
187 	rsp = &rspiocbq->iocb;
188 	ndlp = cmdiocbq->context1;
189 
190 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
191 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
192 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
193 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
194 
195 	if (rsp->ulpStatus) {
196 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
197 			switch (rsp->un.ulpWord[4] & 0xff) {
198 			case IOERR_SEQUENCE_TIMEOUT:
199 				rc = -ETIMEDOUT;
200 				break;
201 			case IOERR_INVALID_RPI:
202 				rc = -EFAULT;
203 				break;
204 			default:
205 				rc = -EACCES;
206 				break;
207 			}
208 		} else
209 			rc = -EACCES;
210 	} else
211 		job->reply->reply_payload_rcv_len =
212 			rsp->un.genreq64.bdl.bdeSize;
213 
214 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
215 	lpfc_sli_release_iocbq(phba, cmdiocbq);
216 	lpfc_nlp_put(ndlp);
217 	kfree(bmp);
218 	kfree(dd_data);
219 	/* make error code available to userspace */
220 	job->reply->result = rc;
221 	/* complete the job back to userspace */
222 	job->job_done(job);
223 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
224 	return;
225 }
226 
227 /**
228  * lpfc_bsg_send_mgmt_cmd - send a CT command from a bsg request
229  * @job: fc_bsg_job to handle
230  **/
231 static int
232 lpfc_bsg_send_mgmt_cmd(struct fc_bsg_job *job)
233 {
234 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
235 	struct lpfc_hba *phba = vport->phba;
236 	struct lpfc_rport_data *rdata = job->rport->dd_data;
237 	struct lpfc_nodelist *ndlp = rdata->pnode;
238 	struct ulp_bde64 *bpl = NULL;
239 	uint32_t timeout;
240 	struct lpfc_iocbq *cmdiocbq = NULL;
241 	IOCB_t *cmd;
242 	struct lpfc_dmabuf *bmp = NULL;
243 	int request_nseg;
244 	int reply_nseg;
245 	struct scatterlist *sgel = NULL;
246 	int numbde;
247 	dma_addr_t busaddr;
248 	struct bsg_job_data *dd_data;
249 	uint32_t creg_val;
250 	int rc = 0;
251 	int iocb_stat;
252 
253 	/* in case no data is transferred */
254 	job->reply->reply_payload_rcv_len = 0;
255 
256 	/* allocate our bsg tracking structure */
257 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
258 	if (!dd_data) {
259 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
260 				"2733 Failed allocation of dd_data\n");
261 		rc = -ENOMEM;
262 		goto no_dd_data;
263 	}
264 
265 	if (!lpfc_nlp_get(ndlp)) {
266 		rc = -ENODEV;
267 		goto no_ndlp;
268 	}
269 
270 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
271 	if (!bmp) {
272 		rc = -ENOMEM;
273 		goto free_ndlp;
274 	}
275 
276 	if (ndlp->nlp_flag & NLP_ELS_SND_MASK) {
277 		rc = -ENODEV;
278 		goto free_bmp;
279 	}
280 
281 	cmdiocbq = lpfc_sli_get_iocbq(phba);
282 	if (!cmdiocbq) {
283 		rc = -ENOMEM;
284 		goto free_bmp;
285 	}
286 
287 	cmd = &cmdiocbq->iocb;
288 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
289 	if (!bmp->virt) {
290 		rc = -ENOMEM;
291 		goto free_cmdiocbq;
292 	}
293 
294 	INIT_LIST_HEAD(&bmp->list);
295 	bpl = (struct ulp_bde64 *) bmp->virt;
296 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
297 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
298 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
299 		busaddr = sg_dma_address(sgel);
300 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
301 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
302 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
303 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
304 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
305 		bpl++;
306 	}
307 
308 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
309 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
310 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
311 		busaddr = sg_dma_address(sgel);
312 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
313 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
314 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
315 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
316 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
317 		bpl++;
318 	}
319 
320 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
321 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
322 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
323 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
324 	cmd->un.genreq64.bdl.bdeSize =
325 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
326 	cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
327 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
328 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
329 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
330 	cmd->un.genreq64.w5.hcsw.Type = FC_TYPE_CT;
331 	cmd->ulpBdeCount = 1;
332 	cmd->ulpLe = 1;
333 	cmd->ulpClass = CLASS3;
334 	cmd->ulpContext = ndlp->nlp_rpi;
335 	if (phba->sli_rev == LPFC_SLI_REV4)
336 		cmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
337 	cmd->ulpOwner = OWN_CHIP;
338 	cmdiocbq->vport = phba->pport;
339 	cmdiocbq->context3 = bmp;
340 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
341 	timeout = phba->fc_ratov * 2;
342 	cmd->ulpTimeout = timeout;
343 
344 	cmdiocbq->iocb_cmpl = lpfc_bsg_send_mgmt_cmd_cmp;
345 	cmdiocbq->context1 = ndlp;
346 	cmdiocbq->context2 = dd_data;
347 	dd_data->type = TYPE_IOCB;
348 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
349 	dd_data->context_un.iocb.set_job = job;
350 	dd_data->context_un.iocb.bmp = bmp;
351 
352 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
353 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
354 			rc = -EIO ;
355 			goto free_cmdiocbq;
356 		}
357 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
358 		writel(creg_val, phba->HCregaddr);
359 		readl(phba->HCregaddr); /* flush */
360 	}
361 
362 	iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
363 	if (iocb_stat == IOCB_SUCCESS)
364 		return 0; /* done for now */
365 	else if (iocb_stat == IOCB_BUSY)
366 		rc = -EAGAIN;
367 	else
368 		rc = -EIO;
369 
370 
371 	/* iocb failed so cleanup */
372 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
373 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
374 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
375 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
376 
377 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
378 
379 free_cmdiocbq:
380 	lpfc_sli_release_iocbq(phba, cmdiocbq);
381 free_bmp:
382 	kfree(bmp);
383 free_ndlp:
384 	lpfc_nlp_put(ndlp);
385 no_ndlp:
386 	kfree(dd_data);
387 no_dd_data:
388 	/* make error code available to userspace */
389 	job->reply->result = rc;
390 	job->dd_data = NULL;
391 	return rc;
392 }
393 
394 /**
395  * lpfc_bsg_rport_els_cmp - lpfc_bsg_rport_els's completion handler
396  * @phba: Pointer to HBA context object.
397  * @cmdiocbq: Pointer to command iocb.
398  * @rspiocbq: Pointer to response iocb.
399  *
400  * This function is the completion handler for iocbs issued using
401  * lpfc_bsg_rport_els_cmp function. This function is called by the
402  * ring event handler function without any lock held. This function
403  * can be called from both worker thread context and interrupt
404  * context. This function also can be called from other thread which
405  * cleans up the SLI layer objects.
406  * This function copies the contents of the response iocb to the
407  * response iocb memory object provided by the caller of
408  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
409  * sleeps for the iocb completion.
410  **/
411 static void
412 lpfc_bsg_rport_els_cmp(struct lpfc_hba *phba,
413 			struct lpfc_iocbq *cmdiocbq,
414 			struct lpfc_iocbq *rspiocbq)
415 {
416 	struct bsg_job_data *dd_data;
417 	struct fc_bsg_job *job;
418 	IOCB_t *rsp;
419 	struct lpfc_nodelist *ndlp;
420 	struct lpfc_dmabuf *pbuflist = NULL;
421 	struct fc_bsg_ctels_reply *els_reply;
422 	uint8_t *rjt_data;
423 	unsigned long flags;
424 	int rc = 0;
425 
426 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
427 	dd_data = cmdiocbq->context1;
428 	/* normal completion and timeout crossed paths, already done */
429 	if (!dd_data) {
430 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
431 		return;
432 	}
433 
434 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
435 	if (cmdiocbq->context2 && rspiocbq)
436 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
437 		       &rspiocbq->iocb, sizeof(IOCB_t));
438 
439 	job = dd_data->context_un.iocb.set_job;
440 	cmdiocbq = dd_data->context_un.iocb.cmdiocbq;
441 	rspiocbq = dd_data->context_un.iocb.rspiocbq;
442 	rsp = &rspiocbq->iocb;
443 	ndlp = dd_data->context_un.iocb.ndlp;
444 
445 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
446 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
447 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
448 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
449 
450 	if (job->reply->result == -EAGAIN)
451 		rc = -EAGAIN;
452 	else if (rsp->ulpStatus == IOSTAT_SUCCESS)
453 		job->reply->reply_payload_rcv_len =
454 			rsp->un.elsreq64.bdl.bdeSize;
455 	else if (rsp->ulpStatus == IOSTAT_LS_RJT) {
456 		job->reply->reply_payload_rcv_len =
457 			sizeof(struct fc_bsg_ctels_reply);
458 		/* LS_RJT data returned in word 4 */
459 		rjt_data = (uint8_t *)&rsp->un.ulpWord[4];
460 		els_reply = &job->reply->reply_data.ctels_reply;
461 		els_reply->status = FC_CTELS_STATUS_REJECT;
462 		els_reply->rjt_data.action = rjt_data[3];
463 		els_reply->rjt_data.reason_code = rjt_data[2];
464 		els_reply->rjt_data.reason_explanation = rjt_data[1];
465 		els_reply->rjt_data.vendor_unique = rjt_data[0];
466 	} else
467 		rc = -EIO;
468 
469 	pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
470 	lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
471 	lpfc_sli_release_iocbq(phba, rspiocbq);
472 	lpfc_sli_release_iocbq(phba, cmdiocbq);
473 	lpfc_nlp_put(ndlp);
474 	kfree(dd_data);
475 	/* make error code available to userspace */
476 	job->reply->result = rc;
477 	job->dd_data = NULL;
478 	/* complete the job back to userspace */
479 	job->job_done(job);
480 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
481 	return;
482 }
483 
484 /**
485  * lpfc_bsg_rport_els - send an ELS command from a bsg request
486  * @job: fc_bsg_job to handle
487  **/
488 static int
489 lpfc_bsg_rport_els(struct fc_bsg_job *job)
490 {
491 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
492 	struct lpfc_hba *phba = vport->phba;
493 	struct lpfc_rport_data *rdata = job->rport->dd_data;
494 	struct lpfc_nodelist *ndlp = rdata->pnode;
495 	uint32_t elscmd;
496 	uint32_t cmdsize;
497 	uint32_t rspsize;
498 	struct lpfc_iocbq *rspiocbq;
499 	struct lpfc_iocbq *cmdiocbq;
500 	IOCB_t *rsp;
501 	uint16_t rpi = 0;
502 	struct lpfc_dmabuf *pcmd;
503 	struct lpfc_dmabuf *prsp;
504 	struct lpfc_dmabuf *pbuflist = NULL;
505 	struct ulp_bde64 *bpl;
506 	int request_nseg;
507 	int reply_nseg;
508 	struct scatterlist *sgel = NULL;
509 	int numbde;
510 	dma_addr_t busaddr;
511 	struct bsg_job_data *dd_data;
512 	uint32_t creg_val;
513 	int rc = 0;
514 
515 	/* in case no data is transferred */
516 	job->reply->reply_payload_rcv_len = 0;
517 
518 	/* allocate our bsg tracking structure */
519 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
520 	if (!dd_data) {
521 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
522 				"2735 Failed allocation of dd_data\n");
523 		rc = -ENOMEM;
524 		goto no_dd_data;
525 	}
526 
527 	if (!lpfc_nlp_get(ndlp)) {
528 		rc = -ENODEV;
529 		goto free_dd_data;
530 	}
531 
532 	elscmd = job->request->rqst_data.r_els.els_code;
533 	cmdsize = job->request_payload.payload_len;
534 	rspsize = job->reply_payload.payload_len;
535 	rspiocbq = lpfc_sli_get_iocbq(phba);
536 	if (!rspiocbq) {
537 		lpfc_nlp_put(ndlp);
538 		rc = -ENOMEM;
539 		goto free_dd_data;
540 	}
541 
542 	rsp = &rspiocbq->iocb;
543 	rpi = ndlp->nlp_rpi;
544 
545 	cmdiocbq = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp,
546 				      ndlp->nlp_DID, elscmd);
547 	if (!cmdiocbq) {
548 		rc = -EIO;
549 		goto free_rspiocbq;
550 	}
551 
552 	/* prep els iocb set context1 to the ndlp, context2 to the command
553 	 * dmabuf, context3 holds the data dmabuf
554 	 */
555 	pcmd = (struct lpfc_dmabuf *) cmdiocbq->context2;
556 	prsp = (struct lpfc_dmabuf *) pcmd->list.next;
557 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
558 	kfree(pcmd);
559 	lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
560 	kfree(prsp);
561 	cmdiocbq->context2 = NULL;
562 
563 	pbuflist = (struct lpfc_dmabuf *) cmdiocbq->context3;
564 	bpl = (struct ulp_bde64 *) pbuflist->virt;
565 
566 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
567 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
568 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
569 		busaddr = sg_dma_address(sgel);
570 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
571 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
572 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
573 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
574 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
575 		bpl++;
576 	}
577 
578 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
579 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
580 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
581 		busaddr = sg_dma_address(sgel);
582 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
583 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
584 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
585 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
586 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
587 		bpl++;
588 	}
589 	cmdiocbq->iocb.un.elsreq64.bdl.bdeSize =
590 		(request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
591 	cmdiocbq->iocb.ulpContext = rpi;
592 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
593 	cmdiocbq->context1 = NULL;
594 	cmdiocbq->context2 = NULL;
595 
596 	cmdiocbq->iocb_cmpl = lpfc_bsg_rport_els_cmp;
597 	cmdiocbq->context1 = dd_data;
598 	cmdiocbq->context2 = rspiocbq;
599 	dd_data->type = TYPE_IOCB;
600 	dd_data->context_un.iocb.cmdiocbq = cmdiocbq;
601 	dd_data->context_un.iocb.rspiocbq = rspiocbq;
602 	dd_data->context_un.iocb.set_job = job;
603 	dd_data->context_un.iocb.bmp = NULL;
604 	dd_data->context_un.iocb.ndlp = ndlp;
605 
606 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
607 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
608 			rc = -EIO;
609 			goto linkdown_err;
610 		}
611 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
612 		writel(creg_val, phba->HCregaddr);
613 		readl(phba->HCregaddr); /* flush */
614 	}
615 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq, 0);
616 	lpfc_nlp_put(ndlp);
617 	if (rc == IOCB_SUCCESS)
618 		return 0; /* done for now */
619 	else if (rc == IOCB_BUSY)
620 		rc = -EAGAIN;
621 	else
622 		rc = -EIO;
623 
624 linkdown_err:
625 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
626 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
627 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
628 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
629 
630 	lpfc_mbuf_free(phba, pbuflist->virt, pbuflist->phys);
631 
632 	lpfc_sli_release_iocbq(phba, cmdiocbq);
633 
634 free_rspiocbq:
635 	lpfc_sli_release_iocbq(phba, rspiocbq);
636 
637 free_dd_data:
638 	kfree(dd_data);
639 
640 no_dd_data:
641 	/* make error code available to userspace */
642 	job->reply->result = rc;
643 	job->dd_data = NULL;
644 	return rc;
645 }
646 
647 /**
648  * lpfc_bsg_event_free - frees an allocated event structure
649  * @kref: Pointer to a kref.
650  *
651  * Called from kref_put. Back cast the kref into an event structure address.
652  * Free any events to get, delete associated nodes, free any events to see,
653  * free any data then free the event itself.
654  **/
655 static void
656 lpfc_bsg_event_free(struct kref *kref)
657 {
658 	struct lpfc_bsg_event *evt = container_of(kref, struct lpfc_bsg_event,
659 						  kref);
660 	struct event_data *ed;
661 
662 	list_del(&evt->node);
663 
664 	while (!list_empty(&evt->events_to_get)) {
665 		ed = list_entry(evt->events_to_get.next, typeof(*ed), node);
666 		list_del(&ed->node);
667 		kfree(ed->data);
668 		kfree(ed);
669 	}
670 
671 	while (!list_empty(&evt->events_to_see)) {
672 		ed = list_entry(evt->events_to_see.next, typeof(*ed), node);
673 		list_del(&ed->node);
674 		kfree(ed->data);
675 		kfree(ed);
676 	}
677 
678 	kfree(evt);
679 }
680 
681 /**
682  * lpfc_bsg_event_ref - increments the kref for an event
683  * @evt: Pointer to an event structure.
684  **/
685 static inline void
686 lpfc_bsg_event_ref(struct lpfc_bsg_event *evt)
687 {
688 	kref_get(&evt->kref);
689 }
690 
691 /**
692  * lpfc_bsg_event_unref - Uses kref_put to free an event structure
693  * @evt: Pointer to an event structure.
694  **/
695 static inline void
696 lpfc_bsg_event_unref(struct lpfc_bsg_event *evt)
697 {
698 	kref_put(&evt->kref, lpfc_bsg_event_free);
699 }
700 
701 /**
702  * lpfc_bsg_event_new - allocate and initialize a event structure
703  * @ev_mask: Mask of events.
704  * @ev_reg_id: Event reg id.
705  * @ev_req_id: Event request id.
706  **/
707 static struct lpfc_bsg_event *
708 lpfc_bsg_event_new(uint32_t ev_mask, int ev_reg_id, uint32_t ev_req_id)
709 {
710 	struct lpfc_bsg_event *evt = kzalloc(sizeof(*evt), GFP_KERNEL);
711 
712 	if (!evt)
713 		return NULL;
714 
715 	INIT_LIST_HEAD(&evt->events_to_get);
716 	INIT_LIST_HEAD(&evt->events_to_see);
717 	evt->type_mask = ev_mask;
718 	evt->req_id = ev_req_id;
719 	evt->reg_id = ev_reg_id;
720 	evt->wait_time_stamp = jiffies;
721 	init_waitqueue_head(&evt->wq);
722 	kref_init(&evt->kref);
723 	return evt;
724 }
725 
726 /**
727  * diag_cmd_data_free - Frees an lpfc dma buffer extension
728  * @phba: Pointer to HBA context object.
729  * @mlist: Pointer to an lpfc dma buffer extension.
730  **/
731 static int
732 diag_cmd_data_free(struct lpfc_hba *phba, struct lpfc_dmabufext *mlist)
733 {
734 	struct lpfc_dmabufext *mlast;
735 	struct pci_dev *pcidev;
736 	struct list_head head, *curr, *next;
737 
738 	if ((!mlist) || (!lpfc_is_link_up(phba) &&
739 		(phba->link_flag & LS_LOOPBACK_MODE))) {
740 		return 0;
741 	}
742 
743 	pcidev = phba->pcidev;
744 	list_add_tail(&head, &mlist->dma.list);
745 
746 	list_for_each_safe(curr, next, &head) {
747 		mlast = list_entry(curr, struct lpfc_dmabufext , dma.list);
748 		if (mlast->dma.virt)
749 			dma_free_coherent(&pcidev->dev,
750 					  mlast->size,
751 					  mlast->dma.virt,
752 					  mlast->dma.phys);
753 		kfree(mlast);
754 	}
755 	return 0;
756 }
757 
758 /**
759  * lpfc_bsg_ct_unsol_event - process an unsolicited CT command
760  * @phba:
761  * @pring:
762  * @piocbq:
763  *
764  * This function is called when an unsolicited CT command is received.  It
765  * forwards the event to any processes registered to receive CT events.
766  **/
767 int
768 lpfc_bsg_ct_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
769 			struct lpfc_iocbq *piocbq)
770 {
771 	uint32_t evt_req_id = 0;
772 	uint32_t cmd;
773 	uint32_t len;
774 	struct lpfc_dmabuf *dmabuf = NULL;
775 	struct lpfc_bsg_event *evt;
776 	struct event_data *evt_dat = NULL;
777 	struct lpfc_iocbq *iocbq;
778 	size_t offset = 0;
779 	struct list_head head;
780 	struct ulp_bde64 *bde;
781 	dma_addr_t dma_addr;
782 	int i;
783 	struct lpfc_dmabuf *bdeBuf1 = piocbq->context2;
784 	struct lpfc_dmabuf *bdeBuf2 = piocbq->context3;
785 	struct lpfc_hbq_entry *hbqe;
786 	struct lpfc_sli_ct_request *ct_req;
787 	struct fc_bsg_job *job = NULL;
788 	unsigned long flags;
789 	int size = 0;
790 
791 	INIT_LIST_HEAD(&head);
792 	list_add_tail(&head, &piocbq->list);
793 
794 	if (piocbq->iocb.ulpBdeCount == 0 ||
795 	    piocbq->iocb.un.cont64[0].tus.f.bdeSize == 0)
796 		goto error_ct_unsol_exit;
797 
798 	if (phba->link_state == LPFC_HBA_ERROR ||
799 		(!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)))
800 		goto error_ct_unsol_exit;
801 
802 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
803 		dmabuf = bdeBuf1;
804 	else {
805 		dma_addr = getPaddr(piocbq->iocb.un.cont64[0].addrHigh,
806 				    piocbq->iocb.un.cont64[0].addrLow);
807 		dmabuf = lpfc_sli_ringpostbuf_get(phba, pring, dma_addr);
808 	}
809 	if (dmabuf == NULL)
810 		goto error_ct_unsol_exit;
811 	ct_req = (struct lpfc_sli_ct_request *)dmabuf->virt;
812 	evt_req_id = ct_req->FsType;
813 	cmd = ct_req->CommandResponse.bits.CmdRsp;
814 	len = ct_req->CommandResponse.bits.Size;
815 	if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
816 		lpfc_sli_ringpostbuf_put(phba, pring, dmabuf);
817 
818 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
819 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
820 		if (!(evt->type_mask & FC_REG_CT_EVENT) ||
821 			evt->req_id != evt_req_id)
822 			continue;
823 
824 		lpfc_bsg_event_ref(evt);
825 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
826 		evt_dat = kzalloc(sizeof(*evt_dat), GFP_KERNEL);
827 		if (evt_dat == NULL) {
828 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
829 			lpfc_bsg_event_unref(evt);
830 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
831 					"2614 Memory allocation failed for "
832 					"CT event\n");
833 			break;
834 		}
835 
836 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
837 			/* take accumulated byte count from the last iocbq */
838 			iocbq = list_entry(head.prev, typeof(*iocbq), list);
839 			evt_dat->len = iocbq->iocb.unsli3.rcvsli3.acc_len;
840 		} else {
841 			list_for_each_entry(iocbq, &head, list) {
842 				for (i = 0; i < iocbq->iocb.ulpBdeCount; i++)
843 					evt_dat->len +=
844 					iocbq->iocb.un.cont64[i].tus.f.bdeSize;
845 			}
846 		}
847 
848 		evt_dat->data = kzalloc(evt_dat->len, GFP_KERNEL);
849 		if (evt_dat->data == NULL) {
850 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
851 					"2615 Memory allocation failed for "
852 					"CT event data, size %d\n",
853 					evt_dat->len);
854 			kfree(evt_dat);
855 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
856 			lpfc_bsg_event_unref(evt);
857 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
858 			goto error_ct_unsol_exit;
859 		}
860 
861 		list_for_each_entry(iocbq, &head, list) {
862 			size = 0;
863 			if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
864 				bdeBuf1 = iocbq->context2;
865 				bdeBuf2 = iocbq->context3;
866 			}
867 			for (i = 0; i < iocbq->iocb.ulpBdeCount; i++) {
868 				if (phba->sli3_options &
869 				    LPFC_SLI3_HBQ_ENABLED) {
870 					if (i == 0) {
871 						hbqe = (struct lpfc_hbq_entry *)
872 						  &iocbq->iocb.un.ulpWord[0];
873 						size = hbqe->bde.tus.f.bdeSize;
874 						dmabuf = bdeBuf1;
875 					} else if (i == 1) {
876 						hbqe = (struct lpfc_hbq_entry *)
877 							&iocbq->iocb.unsli3.
878 							sli3Words[4];
879 						size = hbqe->bde.tus.f.bdeSize;
880 						dmabuf = bdeBuf2;
881 					}
882 					if ((offset + size) > evt_dat->len)
883 						size = evt_dat->len - offset;
884 				} else {
885 					size = iocbq->iocb.un.cont64[i].
886 						tus.f.bdeSize;
887 					bde = &iocbq->iocb.un.cont64[i];
888 					dma_addr = getPaddr(bde->addrHigh,
889 							    bde->addrLow);
890 					dmabuf = lpfc_sli_ringpostbuf_get(phba,
891 							pring, dma_addr);
892 				}
893 				if (!dmabuf) {
894 					lpfc_printf_log(phba, KERN_ERR,
895 						LOG_LIBDFC, "2616 No dmabuf "
896 						"found for iocbq 0x%p\n",
897 						iocbq);
898 					kfree(evt_dat->data);
899 					kfree(evt_dat);
900 					spin_lock_irqsave(&phba->ct_ev_lock,
901 						flags);
902 					lpfc_bsg_event_unref(evt);
903 					spin_unlock_irqrestore(
904 						&phba->ct_ev_lock, flags);
905 					goto error_ct_unsol_exit;
906 				}
907 				memcpy((char *)(evt_dat->data) + offset,
908 				       dmabuf->virt, size);
909 				offset += size;
910 				if (evt_req_id != SLI_CT_ELX_LOOPBACK &&
911 				    !(phba->sli3_options &
912 				      LPFC_SLI3_HBQ_ENABLED)) {
913 					lpfc_sli_ringpostbuf_put(phba, pring,
914 								 dmabuf);
915 				} else {
916 					switch (cmd) {
917 					case ELX_LOOPBACK_DATA:
918 						diag_cmd_data_free(phba,
919 						(struct lpfc_dmabufext *)
920 							dmabuf);
921 						break;
922 					case ELX_LOOPBACK_XRI_SETUP:
923 						if ((phba->sli_rev ==
924 							LPFC_SLI_REV2) ||
925 							(phba->sli3_options &
926 							LPFC_SLI3_HBQ_ENABLED
927 							)) {
928 							lpfc_in_buf_free(phba,
929 									dmabuf);
930 						} else {
931 							lpfc_post_buffer(phba,
932 									 pring,
933 									 1);
934 						}
935 						break;
936 					default:
937 						if (!(phba->sli3_options &
938 						      LPFC_SLI3_HBQ_ENABLED))
939 							lpfc_post_buffer(phba,
940 									 pring,
941 									 1);
942 						break;
943 					}
944 				}
945 			}
946 		}
947 
948 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
949 		if (phba->sli_rev == LPFC_SLI_REV4) {
950 			evt_dat->immed_dat = phba->ctx_idx;
951 			phba->ctx_idx = (phba->ctx_idx + 1) % 64;
952 			/* Provide warning for over-run of the ct_ctx array */
953 			if (phba->ct_ctx[evt_dat->immed_dat].flags &
954 			    UNSOL_VALID)
955 				lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
956 						"2717 CT context array entry "
957 						"[%d] over-run: oxid:x%x, "
958 						"sid:x%x\n", phba->ctx_idx,
959 						phba->ct_ctx[
960 						    evt_dat->immed_dat].oxid,
961 						phba->ct_ctx[
962 						    evt_dat->immed_dat].SID);
963 			phba->ct_ctx[evt_dat->immed_dat].oxid =
964 						piocbq->iocb.ulpContext;
965 			phba->ct_ctx[evt_dat->immed_dat].SID =
966 				piocbq->iocb.un.rcvels.remoteID;
967 			phba->ct_ctx[evt_dat->immed_dat].flags = UNSOL_VALID;
968 		} else
969 			evt_dat->immed_dat = piocbq->iocb.ulpContext;
970 
971 		evt_dat->type = FC_REG_CT_EVENT;
972 		list_add(&evt_dat->node, &evt->events_to_see);
973 		if (evt_req_id == SLI_CT_ELX_LOOPBACK) {
974 			wake_up_interruptible(&evt->wq);
975 			lpfc_bsg_event_unref(evt);
976 			break;
977 		}
978 
979 		list_move(evt->events_to_see.prev, &evt->events_to_get);
980 		lpfc_bsg_event_unref(evt);
981 
982 		job = evt->set_job;
983 		evt->set_job = NULL;
984 		if (job) {
985 			job->reply->reply_payload_rcv_len = size;
986 			/* make error code available to userspace */
987 			job->reply->result = 0;
988 			job->dd_data = NULL;
989 			/* complete the job back to userspace */
990 			spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
991 			job->job_done(job);
992 			spin_lock_irqsave(&phba->ct_ev_lock, flags);
993 		}
994 	}
995 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
996 
997 error_ct_unsol_exit:
998 	if (!list_empty(&head))
999 		list_del(&head);
1000 	if (evt_req_id == SLI_CT_ELX_LOOPBACK)
1001 		return 0;
1002 	return 1;
1003 }
1004 
1005 /**
1006  * lpfc_bsg_hba_set_event - process a SET_EVENT bsg vendor command
1007  * @job: SET_EVENT fc_bsg_job
1008  **/
1009 static int
1010 lpfc_bsg_hba_set_event(struct fc_bsg_job *job)
1011 {
1012 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1013 	struct lpfc_hba *phba = vport->phba;
1014 	struct set_ct_event *event_req;
1015 	struct lpfc_bsg_event *evt;
1016 	int rc = 0;
1017 	struct bsg_job_data *dd_data = NULL;
1018 	uint32_t ev_mask;
1019 	unsigned long flags;
1020 
1021 	if (job->request_len <
1022 	    sizeof(struct fc_bsg_request) + sizeof(struct set_ct_event)) {
1023 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1024 				"2612 Received SET_CT_EVENT below minimum "
1025 				"size\n");
1026 		rc = -EINVAL;
1027 		goto job_error;
1028 	}
1029 
1030 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1031 	if (dd_data == NULL) {
1032 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1033 				"2734 Failed allocation of dd_data\n");
1034 		rc = -ENOMEM;
1035 		goto job_error;
1036 	}
1037 
1038 	event_req = (struct set_ct_event *)
1039 		job->request->rqst_data.h_vendor.vendor_cmd;
1040 	ev_mask = ((uint32_t)(unsigned long)event_req->type_mask &
1041 				FC_REG_EVENT_MASK);
1042 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1043 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1044 		if (evt->reg_id == event_req->ev_reg_id) {
1045 			lpfc_bsg_event_ref(evt);
1046 			evt->wait_time_stamp = jiffies;
1047 			break;
1048 		}
1049 	}
1050 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1051 
1052 	if (&evt->node == &phba->ct_ev_waiters) {
1053 		/* no event waiting struct yet - first call */
1054 		evt = lpfc_bsg_event_new(ev_mask, event_req->ev_reg_id,
1055 					event_req->ev_req_id);
1056 		if (!evt) {
1057 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1058 					"2617 Failed allocation of event "
1059 					"waiter\n");
1060 			rc = -ENOMEM;
1061 			goto job_error;
1062 		}
1063 
1064 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
1065 		list_add(&evt->node, &phba->ct_ev_waiters);
1066 		lpfc_bsg_event_ref(evt);
1067 		evt->wait_time_stamp = jiffies;
1068 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1069 	}
1070 
1071 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1072 	evt->waiting = 1;
1073 	dd_data->type = TYPE_EVT;
1074 	dd_data->context_un.evt = evt;
1075 	evt->set_job = job; /* for unsolicited command */
1076 	job->dd_data = dd_data; /* for fc transport timeout callback*/
1077 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1078 	return 0; /* call job done later */
1079 
1080 job_error:
1081 	if (dd_data != NULL)
1082 		kfree(dd_data);
1083 
1084 	job->dd_data = NULL;
1085 	return rc;
1086 }
1087 
1088 /**
1089  * lpfc_bsg_hba_get_event - process a GET_EVENT bsg vendor command
1090  * @job: GET_EVENT fc_bsg_job
1091  **/
1092 static int
1093 lpfc_bsg_hba_get_event(struct fc_bsg_job *job)
1094 {
1095 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1096 	struct lpfc_hba *phba = vport->phba;
1097 	struct get_ct_event *event_req;
1098 	struct get_ct_event_reply *event_reply;
1099 	struct lpfc_bsg_event *evt;
1100 	struct event_data *evt_dat = NULL;
1101 	unsigned long flags;
1102 	uint32_t rc = 0;
1103 
1104 	if (job->request_len <
1105 	    sizeof(struct fc_bsg_request) + sizeof(struct get_ct_event)) {
1106 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1107 				"2613 Received GET_CT_EVENT request below "
1108 				"minimum size\n");
1109 		rc = -EINVAL;
1110 		goto job_error;
1111 	}
1112 
1113 	event_req = (struct get_ct_event *)
1114 		job->request->rqst_data.h_vendor.vendor_cmd;
1115 
1116 	event_reply = (struct get_ct_event_reply *)
1117 		job->reply->reply_data.vendor_reply.vendor_rsp;
1118 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1119 	list_for_each_entry(evt, &phba->ct_ev_waiters, node) {
1120 		if (evt->reg_id == event_req->ev_reg_id) {
1121 			if (list_empty(&evt->events_to_get))
1122 				break;
1123 			lpfc_bsg_event_ref(evt);
1124 			evt->wait_time_stamp = jiffies;
1125 			evt_dat = list_entry(evt->events_to_get.prev,
1126 					     struct event_data, node);
1127 			list_del(&evt_dat->node);
1128 			break;
1129 		}
1130 	}
1131 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1132 
1133 	/* The app may continue to ask for event data until it gets
1134 	 * an error indicating that there isn't anymore
1135 	 */
1136 	if (evt_dat == NULL) {
1137 		job->reply->reply_payload_rcv_len = 0;
1138 		rc = -ENOENT;
1139 		goto job_error;
1140 	}
1141 
1142 	if (evt_dat->len > job->request_payload.payload_len) {
1143 		evt_dat->len = job->request_payload.payload_len;
1144 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1145 				"2618 Truncated event data at %d "
1146 				"bytes\n",
1147 				job->request_payload.payload_len);
1148 	}
1149 
1150 	event_reply->type = evt_dat->type;
1151 	event_reply->immed_data = evt_dat->immed_dat;
1152 	if (evt_dat->len > 0)
1153 		job->reply->reply_payload_rcv_len =
1154 			sg_copy_from_buffer(job->request_payload.sg_list,
1155 					    job->request_payload.sg_cnt,
1156 					    evt_dat->data, evt_dat->len);
1157 	else
1158 		job->reply->reply_payload_rcv_len = 0;
1159 
1160 	if (evt_dat) {
1161 		kfree(evt_dat->data);
1162 		kfree(evt_dat);
1163 	}
1164 
1165 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1166 	lpfc_bsg_event_unref(evt);
1167 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1168 	job->dd_data = NULL;
1169 	job->reply->result = 0;
1170 	job->job_done(job);
1171 	return 0;
1172 
1173 job_error:
1174 	job->dd_data = NULL;
1175 	job->reply->result = rc;
1176 	return rc;
1177 }
1178 
1179 /**
1180  * lpfc_issue_ct_rsp_cmp - lpfc_issue_ct_rsp's completion handler
1181  * @phba: Pointer to HBA context object.
1182  * @cmdiocbq: Pointer to command iocb.
1183  * @rspiocbq: Pointer to response iocb.
1184  *
1185  * This function is the completion handler for iocbs issued using
1186  * lpfc_issue_ct_rsp_cmp function. This function is called by the
1187  * ring event handler function without any lock held. This function
1188  * can be called from both worker thread context and interrupt
1189  * context. This function also can be called from other thread which
1190  * cleans up the SLI layer objects.
1191  * This function copy the contents of the response iocb to the
1192  * response iocb memory object provided by the caller of
1193  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
1194  * sleeps for the iocb completion.
1195  **/
1196 static void
1197 lpfc_issue_ct_rsp_cmp(struct lpfc_hba *phba,
1198 			struct lpfc_iocbq *cmdiocbq,
1199 			struct lpfc_iocbq *rspiocbq)
1200 {
1201 	struct bsg_job_data *dd_data;
1202 	struct fc_bsg_job *job;
1203 	IOCB_t *rsp;
1204 	struct lpfc_dmabuf *bmp;
1205 	struct lpfc_nodelist *ndlp;
1206 	unsigned long flags;
1207 	int rc = 0;
1208 
1209 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
1210 	dd_data = cmdiocbq->context2;
1211 	/* normal completion and timeout crossed paths, already done */
1212 	if (!dd_data) {
1213 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1214 		return;
1215 	}
1216 
1217 	job = dd_data->context_un.iocb.set_job;
1218 	bmp = dd_data->context_un.iocb.bmp;
1219 	rsp = &rspiocbq->iocb;
1220 	ndlp = dd_data->context_un.iocb.ndlp;
1221 
1222 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1223 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
1224 
1225 	if (rsp->ulpStatus) {
1226 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
1227 			switch (rsp->un.ulpWord[4] & 0xff) {
1228 			case IOERR_SEQUENCE_TIMEOUT:
1229 				rc = -ETIMEDOUT;
1230 				break;
1231 			case IOERR_INVALID_RPI:
1232 				rc = -EFAULT;
1233 				break;
1234 			default:
1235 				rc = -EACCES;
1236 				break;
1237 			}
1238 		} else
1239 			rc = -EACCES;
1240 	} else
1241 		job->reply->reply_payload_rcv_len =
1242 			rsp->un.genreq64.bdl.bdeSize;
1243 
1244 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1245 	lpfc_sli_release_iocbq(phba, cmdiocbq);
1246 	lpfc_nlp_put(ndlp);
1247 	kfree(bmp);
1248 	kfree(dd_data);
1249 	/* make error code available to userspace */
1250 	job->reply->result = rc;
1251 	job->dd_data = NULL;
1252 	/* complete the job back to userspace */
1253 	job->job_done(job);
1254 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
1255 	return;
1256 }
1257 
1258 /**
1259  * lpfc_issue_ct_rsp - issue a ct response
1260  * @phba: Pointer to HBA context object.
1261  * @job: Pointer to the job object.
1262  * @tag: tag index value into the ports context exchange array.
1263  * @bmp: Pointer to a dma buffer descriptor.
1264  * @num_entry: Number of enties in the bde.
1265  **/
1266 static int
1267 lpfc_issue_ct_rsp(struct lpfc_hba *phba, struct fc_bsg_job *job, uint32_t tag,
1268 		  struct lpfc_dmabuf *bmp, int num_entry)
1269 {
1270 	IOCB_t *icmd;
1271 	struct lpfc_iocbq *ctiocb = NULL;
1272 	int rc = 0;
1273 	struct lpfc_nodelist *ndlp = NULL;
1274 	struct bsg_job_data *dd_data;
1275 	uint32_t creg_val;
1276 
1277 	/* allocate our bsg tracking structure */
1278 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
1279 	if (!dd_data) {
1280 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1281 				"2736 Failed allocation of dd_data\n");
1282 		rc = -ENOMEM;
1283 		goto no_dd_data;
1284 	}
1285 
1286 	/* Allocate buffer for  command iocb */
1287 	ctiocb = lpfc_sli_get_iocbq(phba);
1288 	if (!ctiocb) {
1289 		rc = -ENOMEM;
1290 		goto no_ctiocb;
1291 	}
1292 
1293 	icmd = &ctiocb->iocb;
1294 	icmd->un.xseq64.bdl.ulpIoTag32 = 0;
1295 	icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
1296 	icmd->un.xseq64.bdl.addrLow = putPaddrLow(bmp->phys);
1297 	icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
1298 	icmd->un.xseq64.bdl.bdeSize = (num_entry * sizeof(struct ulp_bde64));
1299 	icmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
1300 	icmd->un.xseq64.w5.hcsw.Dfctl = 0;
1301 	icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_SOL_CTL;
1302 	icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
1303 
1304 	/* Fill in rest of iocb */
1305 	icmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
1306 	icmd->ulpBdeCount = 1;
1307 	icmd->ulpLe = 1;
1308 	icmd->ulpClass = CLASS3;
1309 	if (phba->sli_rev == LPFC_SLI_REV4) {
1310 		/* Do not issue unsol response if oxid not marked as valid */
1311 		if (!(phba->ct_ctx[tag].flags & UNSOL_VALID)) {
1312 			rc = IOCB_ERROR;
1313 			goto issue_ct_rsp_exit;
1314 		}
1315 		icmd->ulpContext = phba->ct_ctx[tag].oxid;
1316 		ndlp = lpfc_findnode_did(phba->pport, phba->ct_ctx[tag].SID);
1317 		if (!ndlp) {
1318 			lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
1319 				 "2721 ndlp null for oxid %x SID %x\n",
1320 					icmd->ulpContext,
1321 					phba->ct_ctx[tag].SID);
1322 			rc = IOCB_ERROR;
1323 			goto issue_ct_rsp_exit;
1324 		}
1325 
1326 		/* Check if the ndlp is active */
1327 		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1328 			rc = -IOCB_ERROR;
1329 			goto issue_ct_rsp_exit;
1330 		}
1331 
1332 		/* get a refernece count so the ndlp doesn't go away while
1333 		 * we respond
1334 		 */
1335 		if (!lpfc_nlp_get(ndlp)) {
1336 			rc = -IOCB_ERROR;
1337 			goto issue_ct_rsp_exit;
1338 		}
1339 
1340 		icmd->un.ulpWord[3] = ndlp->nlp_rpi;
1341 		if (phba->sli_rev == LPFC_SLI_REV4)
1342 			icmd->ulpContext =
1343 				phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
1344 
1345 		/* The exchange is done, mark the entry as invalid */
1346 		phba->ct_ctx[tag].flags &= ~UNSOL_VALID;
1347 	} else
1348 		icmd->ulpContext = (ushort) tag;
1349 
1350 	icmd->ulpTimeout = phba->fc_ratov * 2;
1351 
1352 	/* Xmit CT response on exchange <xid> */
1353 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1354 			"2722 Xmit CT response on exchange x%x Data: x%x x%x\n",
1355 			icmd->ulpContext, icmd->ulpIoTag, phba->link_state);
1356 
1357 	ctiocb->iocb_cmpl = NULL;
1358 	ctiocb->iocb_flag |= LPFC_IO_LIBDFC;
1359 	ctiocb->vport = phba->pport;
1360 	ctiocb->context3 = bmp;
1361 
1362 	ctiocb->iocb_cmpl = lpfc_issue_ct_rsp_cmp;
1363 	ctiocb->context2 = dd_data;
1364 	ctiocb->context1 = ndlp;
1365 	dd_data->type = TYPE_IOCB;
1366 	dd_data->context_un.iocb.cmdiocbq = ctiocb;
1367 	dd_data->context_un.iocb.rspiocbq = NULL;
1368 	dd_data->context_un.iocb.set_job = job;
1369 	dd_data->context_un.iocb.bmp = bmp;
1370 	dd_data->context_un.iocb.ndlp = ndlp;
1371 
1372 	if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1373 		if (lpfc_readl(phba->HCregaddr, &creg_val)) {
1374 			rc = -IOCB_ERROR;
1375 			goto issue_ct_rsp_exit;
1376 		}
1377 		creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
1378 		writel(creg_val, phba->HCregaddr);
1379 		readl(phba->HCregaddr); /* flush */
1380 	}
1381 
1382 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
1383 
1384 	if (rc == IOCB_SUCCESS)
1385 		return 0; /* done for now */
1386 
1387 issue_ct_rsp_exit:
1388 	lpfc_sli_release_iocbq(phba, ctiocb);
1389 no_ctiocb:
1390 	kfree(dd_data);
1391 no_dd_data:
1392 	return rc;
1393 }
1394 
1395 /**
1396  * lpfc_bsg_send_mgmt_rsp - process a SEND_MGMT_RESP bsg vendor command
1397  * @job: SEND_MGMT_RESP fc_bsg_job
1398  **/
1399 static int
1400 lpfc_bsg_send_mgmt_rsp(struct fc_bsg_job *job)
1401 {
1402 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
1403 	struct lpfc_hba *phba = vport->phba;
1404 	struct send_mgmt_resp *mgmt_resp = (struct send_mgmt_resp *)
1405 		job->request->rqst_data.h_vendor.vendor_cmd;
1406 	struct ulp_bde64 *bpl;
1407 	struct lpfc_dmabuf *bmp = NULL;
1408 	struct scatterlist *sgel = NULL;
1409 	int request_nseg;
1410 	int numbde;
1411 	dma_addr_t busaddr;
1412 	uint32_t tag = mgmt_resp->tag;
1413 	unsigned long reqbfrcnt =
1414 			(unsigned long)job->request_payload.payload_len;
1415 	int rc = 0;
1416 
1417 	/* in case no data is transferred */
1418 	job->reply->reply_payload_rcv_len = 0;
1419 
1420 	if (!reqbfrcnt || (reqbfrcnt > (80 * BUF_SZ_4K))) {
1421 		rc = -ERANGE;
1422 		goto send_mgmt_rsp_exit;
1423 	}
1424 
1425 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1426 	if (!bmp) {
1427 		rc = -ENOMEM;
1428 		goto send_mgmt_rsp_exit;
1429 	}
1430 
1431 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
1432 	if (!bmp->virt) {
1433 		rc = -ENOMEM;
1434 		goto send_mgmt_rsp_free_bmp;
1435 	}
1436 
1437 	INIT_LIST_HEAD(&bmp->list);
1438 	bpl = (struct ulp_bde64 *) bmp->virt;
1439 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
1440 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
1441 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
1442 		busaddr = sg_dma_address(sgel);
1443 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1444 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
1445 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
1446 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
1447 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
1448 		bpl++;
1449 	}
1450 
1451 	rc = lpfc_issue_ct_rsp(phba, job, tag, bmp, request_nseg);
1452 
1453 	if (rc == IOCB_SUCCESS)
1454 		return 0; /* done for now */
1455 
1456 	/* TBD need to handle a timeout */
1457 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
1458 			  job->request_payload.sg_cnt, DMA_TO_DEVICE);
1459 	rc = -EACCES;
1460 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
1461 
1462 send_mgmt_rsp_free_bmp:
1463 	kfree(bmp);
1464 send_mgmt_rsp_exit:
1465 	/* make error code available to userspace */
1466 	job->reply->result = rc;
1467 	job->dd_data = NULL;
1468 	return rc;
1469 }
1470 
1471 /**
1472  * lpfc_bsg_diag_mode_enter - process preparing into device diag loopback mode
1473  * @phba: Pointer to HBA context object.
1474  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1475  *
1476  * This function is responsible for preparing driver for diag loopback
1477  * on device.
1478  */
1479 static int
1480 lpfc_bsg_diag_mode_enter(struct lpfc_hba *phba, struct fc_bsg_job *job)
1481 {
1482 	struct lpfc_vport **vports;
1483 	struct Scsi_Host *shost;
1484 	struct lpfc_sli *psli;
1485 	struct lpfc_sli_ring *pring;
1486 	int i = 0;
1487 
1488 	psli = &phba->sli;
1489 	if (!psli)
1490 		return -ENODEV;
1491 
1492 	pring = &psli->ring[LPFC_FCP_RING];
1493 	if (!pring)
1494 		return -ENODEV;
1495 
1496 	if ((phba->link_state == LPFC_HBA_ERROR) ||
1497 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
1498 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE)))
1499 		return -EACCES;
1500 
1501 	vports = lpfc_create_vport_work_array(phba);
1502 	if (vports) {
1503 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1504 			shost = lpfc_shost_from_vport(vports[i]);
1505 			scsi_block_requests(shost);
1506 		}
1507 		lpfc_destroy_vport_work_array(phba, vports);
1508 	} else {
1509 		shost = lpfc_shost_from_vport(phba->pport);
1510 		scsi_block_requests(shost);
1511 	}
1512 
1513 	while (pring->txcmplq_cnt) {
1514 		if (i++ > 500)  /* wait up to 5 seconds */
1515 			break;
1516 		msleep(10);
1517 	}
1518 	return 0;
1519 }
1520 
1521 /**
1522  * lpfc_bsg_diag_mode_exit - exit process from device diag loopback mode
1523  * @phba: Pointer to HBA context object.
1524  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1525  *
1526  * This function is responsible for driver exit processing of setting up
1527  * diag loopback mode on device.
1528  */
1529 static void
1530 lpfc_bsg_diag_mode_exit(struct lpfc_hba *phba)
1531 {
1532 	struct Scsi_Host *shost;
1533 	struct lpfc_vport **vports;
1534 	int i;
1535 
1536 	vports = lpfc_create_vport_work_array(phba);
1537 	if (vports) {
1538 		for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
1539 			shost = lpfc_shost_from_vport(vports[i]);
1540 			scsi_unblock_requests(shost);
1541 		}
1542 		lpfc_destroy_vport_work_array(phba, vports);
1543 	} else {
1544 		shost = lpfc_shost_from_vport(phba->pport);
1545 		scsi_unblock_requests(shost);
1546 	}
1547 	return;
1548 }
1549 
1550 /**
1551  * lpfc_sli3_bsg_diag_loopback_mode - process an sli3 bsg vendor command
1552  * @phba: Pointer to HBA context object.
1553  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1554  *
1555  * This function is responsible for placing an sli3  port into diagnostic
1556  * loopback mode in order to perform a diagnostic loopback test.
1557  * All new scsi requests are blocked, a small delay is used to allow the
1558  * scsi requests to complete then the link is brought down. If the link is
1559  * is placed in loopback mode then scsi requests are again allowed
1560  * so the scsi mid-layer doesn't give up on the port.
1561  * All of this is done in-line.
1562  */
1563 static int
1564 lpfc_sli3_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1565 {
1566 	struct diag_mode_set *loopback_mode;
1567 	uint32_t link_flags;
1568 	uint32_t timeout;
1569 	LPFC_MBOXQ_t *pmboxq;
1570 	int mbxstatus;
1571 	int i = 0;
1572 	int rc = 0;
1573 
1574 	/* no data to return just the return code */
1575 	job->reply->reply_payload_rcv_len = 0;
1576 
1577 	if (job->request_len < sizeof(struct fc_bsg_request) +
1578 	    sizeof(struct diag_mode_set)) {
1579 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1580 				"2738 Received DIAG MODE request size:%d "
1581 				"below the minimum size:%d\n",
1582 				job->request_len,
1583 				(int)(sizeof(struct fc_bsg_request) +
1584 				sizeof(struct diag_mode_set)));
1585 		rc = -EINVAL;
1586 		goto job_error;
1587 	}
1588 
1589 	rc = lpfc_bsg_diag_mode_enter(phba, job);
1590 	if (rc)
1591 		goto job_error;
1592 
1593 	/* bring the link to diagnostic mode */
1594 	loopback_mode = (struct diag_mode_set *)
1595 		job->request->rqst_data.h_vendor.vendor_cmd;
1596 	link_flags = loopback_mode->type;
1597 	timeout = loopback_mode->timeout * 100;
1598 
1599 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1600 	if (!pmboxq) {
1601 		rc = -ENOMEM;
1602 		goto loopback_mode_exit;
1603 	}
1604 	memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1605 	pmboxq->u.mb.mbxCommand = MBX_DOWN_LINK;
1606 	pmboxq->u.mb.mbxOwner = OWN_HOST;
1607 
1608 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1609 
1610 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0)) {
1611 		/* wait for link down before proceeding */
1612 		i = 0;
1613 		while (phba->link_state != LPFC_LINK_DOWN) {
1614 			if (i++ > timeout) {
1615 				rc = -ETIMEDOUT;
1616 				goto loopback_mode_exit;
1617 			}
1618 
1619 			msleep(10);
1620 		}
1621 
1622 		memset((void *)pmboxq, 0, sizeof(LPFC_MBOXQ_t));
1623 		if (link_flags == INTERNAL_LOOP_BACK)
1624 			pmboxq->u.mb.un.varInitLnk.link_flags = FLAGS_LOCAL_LB;
1625 		else
1626 			pmboxq->u.mb.un.varInitLnk.link_flags =
1627 				FLAGS_TOPOLOGY_MODE_LOOP;
1628 
1629 		pmboxq->u.mb.mbxCommand = MBX_INIT_LINK;
1630 		pmboxq->u.mb.mbxOwner = OWN_HOST;
1631 
1632 		mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq,
1633 						     LPFC_MBOX_TMO);
1634 
1635 		if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1636 			rc = -ENODEV;
1637 		else {
1638 			phba->link_flag |= LS_LOOPBACK_MODE;
1639 			/* wait for the link attention interrupt */
1640 			msleep(100);
1641 
1642 			i = 0;
1643 			while (phba->link_state != LPFC_HBA_READY) {
1644 				if (i++ > timeout) {
1645 					rc = -ETIMEDOUT;
1646 					break;
1647 				}
1648 
1649 				msleep(10);
1650 			}
1651 		}
1652 
1653 	} else
1654 		rc = -ENODEV;
1655 
1656 loopback_mode_exit:
1657 	lpfc_bsg_diag_mode_exit(phba);
1658 
1659 	/*
1660 	 * Let SLI layer release mboxq if mbox command completed after timeout.
1661 	 */
1662 	if (mbxstatus != MBX_TIMEOUT)
1663 		mempool_free(pmboxq, phba->mbox_mem_pool);
1664 
1665 job_error:
1666 	/* make error code available to userspace */
1667 	job->reply->result = rc;
1668 	/* complete the job back to userspace if no error */
1669 	if (rc == 0)
1670 		job->job_done(job);
1671 	return rc;
1672 }
1673 
1674 /**
1675  * lpfc_sli4_bsg_set_link_diag_state - set sli4 link diag state
1676  * @phba: Pointer to HBA context object.
1677  * @diag: Flag for set link to diag or nomral operation state.
1678  *
1679  * This function is responsible for issuing a sli4 mailbox command for setting
1680  * link to either diag state or normal operation state.
1681  */
1682 static int
1683 lpfc_sli4_bsg_set_link_diag_state(struct lpfc_hba *phba, uint32_t diag)
1684 {
1685 	LPFC_MBOXQ_t *pmboxq;
1686 	struct lpfc_mbx_set_link_diag_state *link_diag_state;
1687 	uint32_t req_len, alloc_len;
1688 	int mbxstatus = MBX_SUCCESS, rc;
1689 
1690 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1691 	if (!pmboxq)
1692 		return -ENOMEM;
1693 
1694 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
1695 		   sizeof(struct lpfc_sli4_cfg_mhdr));
1696 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1697 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
1698 				req_len, LPFC_SLI4_MBX_EMBED);
1699 	if (alloc_len != req_len) {
1700 		rc = -ENOMEM;
1701 		goto link_diag_state_set_out;
1702 	}
1703 	link_diag_state = &pmboxq->u.mqe.un.link_diag_state;
1704 	bf_set(lpfc_mbx_set_diag_state_link_num, &link_diag_state->u.req,
1705 	       phba->sli4_hba.link_state.number);
1706 	bf_set(lpfc_mbx_set_diag_state_link_type, &link_diag_state->u.req,
1707 	       phba->sli4_hba.link_state.type);
1708 	if (diag)
1709 		bf_set(lpfc_mbx_set_diag_state_diag,
1710 		       &link_diag_state->u.req, 1);
1711 	else
1712 		bf_set(lpfc_mbx_set_diag_state_diag,
1713 		       &link_diag_state->u.req, 0);
1714 
1715 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1716 
1717 	if ((mbxstatus == MBX_SUCCESS) && (pmboxq->u.mb.mbxStatus == 0))
1718 		rc = 0;
1719 	else
1720 		rc = -ENODEV;
1721 
1722 link_diag_state_set_out:
1723 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1724 		mempool_free(pmboxq, phba->mbox_mem_pool);
1725 
1726 	return rc;
1727 }
1728 
1729 /**
1730  * lpfc_sli4_bsg_diag_loopback_mode - process an sli4 bsg vendor command
1731  * @phba: Pointer to HBA context object.
1732  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1733  *
1734  * This function is responsible for placing an sli4 port into diagnostic
1735  * loopback mode in order to perform a diagnostic loopback test.
1736  */
1737 static int
1738 lpfc_sli4_bsg_diag_loopback_mode(struct lpfc_hba *phba, struct fc_bsg_job *job)
1739 {
1740 	struct diag_mode_set *loopback_mode;
1741 	uint32_t link_flags, timeout, req_len, alloc_len;
1742 	struct lpfc_mbx_set_link_diag_loopback *link_diag_loopback;
1743 	LPFC_MBOXQ_t *pmboxq = NULL;
1744 	int mbxstatus, i, rc = 0;
1745 
1746 	/* no data to return just the return code */
1747 	job->reply->reply_payload_rcv_len = 0;
1748 
1749 	if (job->request_len < sizeof(struct fc_bsg_request) +
1750 	    sizeof(struct diag_mode_set)) {
1751 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1752 				"3011 Received DIAG MODE request size:%d "
1753 				"below the minimum size:%d\n",
1754 				job->request_len,
1755 				(int)(sizeof(struct fc_bsg_request) +
1756 				sizeof(struct diag_mode_set)));
1757 		rc = -EINVAL;
1758 		goto job_error;
1759 	}
1760 
1761 	rc = lpfc_bsg_diag_mode_enter(phba, job);
1762 	if (rc)
1763 		goto job_error;
1764 
1765 	/* bring the link to diagnostic mode */
1766 	loopback_mode = (struct diag_mode_set *)
1767 		job->request->rqst_data.h_vendor.vendor_cmd;
1768 	link_flags = loopback_mode->type;
1769 	timeout = loopback_mode->timeout * 100;
1770 
1771 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
1772 	if (rc)
1773 		goto loopback_mode_exit;
1774 
1775 	/* wait for link down before proceeding */
1776 	i = 0;
1777 	while (phba->link_state != LPFC_LINK_DOWN) {
1778 		if (i++ > timeout) {
1779 			rc = -ETIMEDOUT;
1780 			goto loopback_mode_exit;
1781 		}
1782 		msleep(10);
1783 	}
1784 	/* set up loopback mode */
1785 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1786 	if (!pmboxq) {
1787 		rc = -ENOMEM;
1788 		goto loopback_mode_exit;
1789 	}
1790 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_loopback) -
1791 		   sizeof(struct lpfc_sli4_cfg_mhdr));
1792 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
1793 				LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_LOOPBACK,
1794 				req_len, LPFC_SLI4_MBX_EMBED);
1795 	if (alloc_len != req_len) {
1796 		rc = -ENOMEM;
1797 		goto loopback_mode_exit;
1798 	}
1799 	link_diag_loopback = &pmboxq->u.mqe.un.link_diag_loopback;
1800 	bf_set(lpfc_mbx_set_diag_state_link_num,
1801 	       &link_diag_loopback->u.req, phba->sli4_hba.link_state.number);
1802 	bf_set(lpfc_mbx_set_diag_state_link_type,
1803 	       &link_diag_loopback->u.req, phba->sli4_hba.link_state.type);
1804 	if (link_flags == INTERNAL_LOOP_BACK)
1805 		bf_set(lpfc_mbx_set_diag_lpbk_type,
1806 		       &link_diag_loopback->u.req,
1807 		       LPFC_DIAG_LOOPBACK_TYPE_INTERNAL);
1808 	else
1809 		bf_set(lpfc_mbx_set_diag_lpbk_type,
1810 		       &link_diag_loopback->u.req,
1811 		       LPFC_DIAG_LOOPBACK_TYPE_EXTERNAL);
1812 
1813 	mbxstatus = lpfc_sli_issue_mbox_wait(phba, pmboxq, LPFC_MBOX_TMO);
1814 	if ((mbxstatus != MBX_SUCCESS) || (pmboxq->u.mb.mbxStatus))
1815 		rc = -ENODEV;
1816 	else {
1817 		phba->link_flag |= LS_LOOPBACK_MODE;
1818 		/* wait for the link attention interrupt */
1819 		msleep(100);
1820 		i = 0;
1821 		while (phba->link_state != LPFC_HBA_READY) {
1822 			if (i++ > timeout) {
1823 				rc = -ETIMEDOUT;
1824 				break;
1825 			}
1826 			msleep(10);
1827 		}
1828 	}
1829 
1830 loopback_mode_exit:
1831 	lpfc_bsg_diag_mode_exit(phba);
1832 
1833 	/*
1834 	 * Let SLI layer release mboxq if mbox command completed after timeout.
1835 	 */
1836 	if (pmboxq && (mbxstatus != MBX_TIMEOUT))
1837 		mempool_free(pmboxq, phba->mbox_mem_pool);
1838 
1839 job_error:
1840 	/* make error code available to userspace */
1841 	job->reply->result = rc;
1842 	/* complete the job back to userspace if no error */
1843 	if (rc == 0)
1844 		job->job_done(job);
1845 	return rc;
1846 }
1847 
1848 /**
1849  * lpfc_bsg_diag_loopback_mode - bsg vendor command for diag loopback mode
1850  * @job: LPFC_BSG_VENDOR_DIAG_MODE
1851  *
1852  * This function is responsible for responding to check and dispatch bsg diag
1853  * command from the user to proper driver action routines.
1854  */
1855 static int
1856 lpfc_bsg_diag_loopback_mode(struct fc_bsg_job *job)
1857 {
1858 	struct Scsi_Host *shost;
1859 	struct lpfc_vport *vport;
1860 	struct lpfc_hba *phba;
1861 	int rc;
1862 
1863 	shost = job->shost;
1864 	if (!shost)
1865 		return -ENODEV;
1866 	vport = (struct lpfc_vport *)job->shost->hostdata;
1867 	if (!vport)
1868 		return -ENODEV;
1869 	phba = vport->phba;
1870 	if (!phba)
1871 		return -ENODEV;
1872 
1873 	if (phba->sli_rev < LPFC_SLI_REV4)
1874 		rc = lpfc_sli3_bsg_diag_loopback_mode(phba, job);
1875 	else if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1876 		 LPFC_SLI_INTF_IF_TYPE_2)
1877 		rc = lpfc_sli4_bsg_diag_loopback_mode(phba, job);
1878 	else
1879 		rc = -ENODEV;
1880 
1881 	return rc;
1882 
1883 }
1884 
1885 /**
1886  * lpfc_sli4_bsg_diag_mode_end - sli4 bsg vendor command for ending diag mode
1887  * @job: LPFC_BSG_VENDOR_DIAG_MODE_END
1888  *
1889  * This function is responsible for responding to check and dispatch bsg diag
1890  * command from the user to proper driver action routines.
1891  */
1892 static int
1893 lpfc_sli4_bsg_diag_mode_end(struct fc_bsg_job *job)
1894 {
1895 	struct Scsi_Host *shost;
1896 	struct lpfc_vport *vport;
1897 	struct lpfc_hba *phba;
1898 	int rc;
1899 
1900 	shost = job->shost;
1901 	if (!shost)
1902 		return -ENODEV;
1903 	vport = (struct lpfc_vport *)job->shost->hostdata;
1904 	if (!vport)
1905 		return -ENODEV;
1906 	phba = vport->phba;
1907 	if (!phba)
1908 		return -ENODEV;
1909 
1910 	if (phba->sli_rev < LPFC_SLI_REV4)
1911 		return -ENODEV;
1912 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
1913 	    LPFC_SLI_INTF_IF_TYPE_2)
1914 		return -ENODEV;
1915 
1916 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
1917 
1918 	if (!rc)
1919 		rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
1920 
1921 	return rc;
1922 }
1923 
1924 /**
1925  * lpfc_sli4_bsg_link_diag_test - sli4 bsg vendor command for diag link test
1926  * @job: LPFC_BSG_VENDOR_DIAG_LINK_TEST
1927  *
1928  * This function is to perform SLI4 diag link test request from the user
1929  * applicaiton.
1930  */
1931 static int
1932 lpfc_sli4_bsg_link_diag_test(struct fc_bsg_job *job)
1933 {
1934 	struct Scsi_Host *shost;
1935 	struct lpfc_vport *vport;
1936 	struct lpfc_hba *phba;
1937 	LPFC_MBOXQ_t *pmboxq;
1938 	struct sli4_link_diag *link_diag_test_cmd;
1939 	uint32_t req_len, alloc_len;
1940 	uint32_t timeout;
1941 	struct lpfc_mbx_run_link_diag_test *run_link_diag_test;
1942 	union lpfc_sli4_cfg_shdr *shdr;
1943 	uint32_t shdr_status, shdr_add_status;
1944 	struct diag_status *diag_status_reply;
1945 	int mbxstatus, rc = 0;
1946 
1947 	shost = job->shost;
1948 	if (!shost) {
1949 		rc = -ENODEV;
1950 		goto job_error;
1951 	}
1952 	vport = (struct lpfc_vport *)job->shost->hostdata;
1953 	if (!vport) {
1954 		rc = -ENODEV;
1955 		goto job_error;
1956 	}
1957 	phba = vport->phba;
1958 	if (!phba) {
1959 		rc = -ENODEV;
1960 		goto job_error;
1961 	}
1962 
1963 	if (phba->sli_rev < LPFC_SLI_REV4) {
1964 		rc = -ENODEV;
1965 		goto job_error;
1966 	}
1967 	if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
1968 	    LPFC_SLI_INTF_IF_TYPE_2) {
1969 		rc = -ENODEV;
1970 		goto job_error;
1971 	}
1972 
1973 	if (job->request_len < sizeof(struct fc_bsg_request) +
1974 	    sizeof(struct sli4_link_diag)) {
1975 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
1976 				"3013 Received LINK DIAG TEST request "
1977 				" size:%d below the minimum size:%d\n",
1978 				job->request_len,
1979 				(int)(sizeof(struct fc_bsg_request) +
1980 				sizeof(struct sli4_link_diag)));
1981 		rc = -EINVAL;
1982 		goto job_error;
1983 	}
1984 
1985 	rc = lpfc_bsg_diag_mode_enter(phba, job);
1986 	if (rc)
1987 		goto job_error;
1988 
1989 	link_diag_test_cmd = (struct sli4_link_diag *)
1990 			 job->request->rqst_data.h_vendor.vendor_cmd;
1991 	timeout = link_diag_test_cmd->timeout * 100;
1992 
1993 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 1);
1994 
1995 	if (rc)
1996 		goto job_error;
1997 
1998 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1999 	if (!pmboxq) {
2000 		rc = -ENOMEM;
2001 		goto link_diag_test_exit;
2002 	}
2003 
2004 	req_len = (sizeof(struct lpfc_mbx_set_link_diag_state) -
2005 		   sizeof(struct lpfc_sli4_cfg_mhdr));
2006 	alloc_len = lpfc_sli4_config(phba, pmboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
2007 				     LPFC_MBOX_OPCODE_FCOE_LINK_DIAG_STATE,
2008 				     req_len, LPFC_SLI4_MBX_EMBED);
2009 	if (alloc_len != req_len) {
2010 		rc = -ENOMEM;
2011 		goto link_diag_test_exit;
2012 	}
2013 	run_link_diag_test = &pmboxq->u.mqe.un.link_diag_test;
2014 	bf_set(lpfc_mbx_run_diag_test_link_num, &run_link_diag_test->u.req,
2015 	       phba->sli4_hba.link_state.number);
2016 	bf_set(lpfc_mbx_run_diag_test_link_type, &run_link_diag_test->u.req,
2017 	       phba->sli4_hba.link_state.type);
2018 	bf_set(lpfc_mbx_run_diag_test_test_id, &run_link_diag_test->u.req,
2019 	       link_diag_test_cmd->test_id);
2020 	bf_set(lpfc_mbx_run_diag_test_loops, &run_link_diag_test->u.req,
2021 	       link_diag_test_cmd->loops);
2022 	bf_set(lpfc_mbx_run_diag_test_test_ver, &run_link_diag_test->u.req,
2023 	       link_diag_test_cmd->test_version);
2024 	bf_set(lpfc_mbx_run_diag_test_err_act, &run_link_diag_test->u.req,
2025 	       link_diag_test_cmd->error_action);
2026 
2027 	mbxstatus = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
2028 
2029 	shdr = (union lpfc_sli4_cfg_shdr *)
2030 		&pmboxq->u.mqe.un.sli4_config.header.cfg_shdr;
2031 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2032 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2033 	if (shdr_status || shdr_add_status || mbxstatus) {
2034 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2035 				"3010 Run link diag test mailbox failed with "
2036 				"mbx_status x%x status x%x, add_status x%x\n",
2037 				mbxstatus, shdr_status, shdr_add_status);
2038 	}
2039 
2040 	diag_status_reply = (struct diag_status *)
2041 			    job->reply->reply_data.vendor_reply.vendor_rsp;
2042 
2043 	if (job->reply_len <
2044 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_status)) {
2045 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2046 				"3012 Received Run link diag test reply "
2047 				"below minimum size (%d): reply_len:%d\n",
2048 				(int)(sizeof(struct fc_bsg_request) +
2049 				sizeof(struct diag_status)),
2050 				job->reply_len);
2051 		rc = -EINVAL;
2052 		goto job_error;
2053 	}
2054 
2055 	diag_status_reply->mbox_status = mbxstatus;
2056 	diag_status_reply->shdr_status = shdr_status;
2057 	diag_status_reply->shdr_add_status = shdr_add_status;
2058 
2059 link_diag_test_exit:
2060 	rc = lpfc_sli4_bsg_set_link_diag_state(phba, 0);
2061 
2062 	if (pmboxq)
2063 		mempool_free(pmboxq, phba->mbox_mem_pool);
2064 
2065 	lpfc_bsg_diag_mode_exit(phba);
2066 
2067 job_error:
2068 	/* make error code available to userspace */
2069 	job->reply->result = rc;
2070 	/* complete the job back to userspace if no error */
2071 	if (rc == 0)
2072 		job->job_done(job);
2073 	return rc;
2074 }
2075 
2076 /**
2077  * lpfcdiag_loop_self_reg - obtains a remote port login id
2078  * @phba: Pointer to HBA context object
2079  * @rpi: Pointer to a remote port login id
2080  *
2081  * This function obtains a remote port login id so the diag loopback test
2082  * can send and receive its own unsolicited CT command.
2083  **/
2084 static int lpfcdiag_loop_self_reg(struct lpfc_hba *phba, uint16_t *rpi)
2085 {
2086 	LPFC_MBOXQ_t *mbox;
2087 	struct lpfc_dmabuf *dmabuff;
2088 	int status;
2089 
2090 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2091 	if (!mbox)
2092 		return -ENOMEM;
2093 
2094 	if (phba->sli_rev == LPFC_SLI_REV4)
2095 		*rpi = lpfc_sli4_alloc_rpi(phba);
2096 	status = lpfc_reg_rpi(phba, 0, phba->pport->fc_myDID,
2097 			      (uint8_t *)&phba->pport->fc_sparam, mbox, *rpi);
2098 	if (status) {
2099 		mempool_free(mbox, phba->mbox_mem_pool);
2100 		if (phba->sli_rev == LPFC_SLI_REV4)
2101 			lpfc_sli4_free_rpi(phba, *rpi);
2102 		return -ENOMEM;
2103 	}
2104 
2105 	dmabuff = (struct lpfc_dmabuf *) mbox->context1;
2106 	mbox->context1 = NULL;
2107 	mbox->context2 = NULL;
2108 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2109 
2110 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2111 		lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2112 		kfree(dmabuff);
2113 		if (status != MBX_TIMEOUT)
2114 			mempool_free(mbox, phba->mbox_mem_pool);
2115 		if (phba->sli_rev == LPFC_SLI_REV4)
2116 			lpfc_sli4_free_rpi(phba, *rpi);
2117 		return -ENODEV;
2118 	}
2119 
2120 	*rpi = mbox->u.mb.un.varWords[0];
2121 
2122 	lpfc_mbuf_free(phba, dmabuff->virt, dmabuff->phys);
2123 	kfree(dmabuff);
2124 	mempool_free(mbox, phba->mbox_mem_pool);
2125 	return 0;
2126 }
2127 
2128 /**
2129  * lpfcdiag_loop_self_unreg - unregs from the rpi
2130  * @phba: Pointer to HBA context object
2131  * @rpi: Remote port login id
2132  *
2133  * This function unregisters the rpi obtained in lpfcdiag_loop_self_reg
2134  **/
2135 static int lpfcdiag_loop_self_unreg(struct lpfc_hba *phba, uint16_t rpi)
2136 {
2137 	LPFC_MBOXQ_t *mbox;
2138 	int status;
2139 
2140 	/* Allocate mboxq structure */
2141 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2142 	if (mbox == NULL)
2143 		return -ENOMEM;
2144 
2145 	lpfc_unreg_login(phba, 0, rpi, mbox);
2146 	status = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
2147 
2148 	if ((status != MBX_SUCCESS) || (mbox->u.mb.mbxStatus)) {
2149 		if (status != MBX_TIMEOUT)
2150 			mempool_free(mbox, phba->mbox_mem_pool);
2151 		return -EIO;
2152 	}
2153 	mempool_free(mbox, phba->mbox_mem_pool);
2154 	if (phba->sli_rev == LPFC_SLI_REV4)
2155 		lpfc_sli4_free_rpi(phba, rpi);
2156 	return 0;
2157 }
2158 
2159 /**
2160  * lpfcdiag_loop_get_xri - obtains the transmit and receive ids
2161  * @phba: Pointer to HBA context object
2162  * @rpi: Remote port login id
2163  * @txxri: Pointer to transmit exchange id
2164  * @rxxri: Pointer to response exchabge id
2165  *
2166  * This function obtains the transmit and receive ids required to send
2167  * an unsolicited ct command with a payload. A special lpfc FsType and CmdRsp
2168  * flags are used to the unsolicted response handler is able to process
2169  * the ct command sent on the same port.
2170  **/
2171 static int lpfcdiag_loop_get_xri(struct lpfc_hba *phba, uint16_t rpi,
2172 			 uint16_t *txxri, uint16_t * rxxri)
2173 {
2174 	struct lpfc_bsg_event *evt;
2175 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2176 	IOCB_t *cmd, *rsp;
2177 	struct lpfc_dmabuf *dmabuf;
2178 	struct ulp_bde64 *bpl = NULL;
2179 	struct lpfc_sli_ct_request *ctreq = NULL;
2180 	int ret_val = 0;
2181 	int time_left;
2182 	int iocb_stat = 0;
2183 	unsigned long flags;
2184 
2185 	*txxri = 0;
2186 	*rxxri = 0;
2187 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2188 				SLI_CT_ELX_LOOPBACK);
2189 	if (!evt)
2190 		return -ENOMEM;
2191 
2192 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2193 	list_add(&evt->node, &phba->ct_ev_waiters);
2194 	lpfc_bsg_event_ref(evt);
2195 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2196 
2197 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2198 	rspiocbq = lpfc_sli_get_iocbq(phba);
2199 
2200 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2201 	if (dmabuf) {
2202 		dmabuf->virt = lpfc_mbuf_alloc(phba, 0, &dmabuf->phys);
2203 		if (dmabuf->virt) {
2204 			INIT_LIST_HEAD(&dmabuf->list);
2205 			bpl = (struct ulp_bde64 *) dmabuf->virt;
2206 			memset(bpl, 0, sizeof(*bpl));
2207 			ctreq = (struct lpfc_sli_ct_request *)(bpl + 1);
2208 			bpl->addrHigh =
2209 				le32_to_cpu(putPaddrHigh(dmabuf->phys +
2210 					sizeof(*bpl)));
2211 			bpl->addrLow =
2212 				le32_to_cpu(putPaddrLow(dmabuf->phys +
2213 					sizeof(*bpl)));
2214 			bpl->tus.f.bdeFlags = 0;
2215 			bpl->tus.f.bdeSize = ELX_LOOPBACK_HEADER_SZ;
2216 			bpl->tus.w = le32_to_cpu(bpl->tus.w);
2217 		}
2218 	}
2219 
2220 	if (cmdiocbq == NULL || rspiocbq == NULL ||
2221 	    dmabuf == NULL || bpl == NULL || ctreq == NULL ||
2222 		dmabuf->virt == NULL) {
2223 		ret_val = -ENOMEM;
2224 		goto err_get_xri_exit;
2225 	}
2226 
2227 	cmd = &cmdiocbq->iocb;
2228 	rsp = &rspiocbq->iocb;
2229 
2230 	memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2231 
2232 	ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2233 	ctreq->RevisionId.bits.InId = 0;
2234 	ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2235 	ctreq->FsSubType = 0;
2236 	ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_XRI_SETUP;
2237 	ctreq->CommandResponse.bits.Size = 0;
2238 
2239 
2240 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(dmabuf->phys);
2241 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(dmabuf->phys);
2242 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2243 	cmd->un.xseq64.bdl.bdeSize = sizeof(*bpl);
2244 
2245 	cmd->un.xseq64.w5.hcsw.Fctl = LA;
2246 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2247 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2248 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2249 
2250 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2251 	cmd->ulpBdeCount = 1;
2252 	cmd->ulpLe = 1;
2253 	cmd->ulpClass = CLASS3;
2254 	cmd->ulpContext = rpi;
2255 
2256 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2257 	cmdiocbq->vport = phba->pport;
2258 
2259 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2260 				rspiocbq,
2261 				(phba->fc_ratov * 2)
2262 				+ LPFC_DRVR_TIMEOUT);
2263 	if (iocb_stat) {
2264 		ret_val = -EIO;
2265 		goto err_get_xri_exit;
2266 	}
2267 	*txxri =  rsp->ulpContext;
2268 
2269 	evt->waiting = 1;
2270 	evt->wait_time_stamp = jiffies;
2271 	time_left = wait_event_interruptible_timeout(
2272 		evt->wq, !list_empty(&evt->events_to_see),
2273 		((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2274 	if (list_empty(&evt->events_to_see))
2275 		ret_val = (time_left) ? -EINTR : -ETIMEDOUT;
2276 	else {
2277 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
2278 		list_move(evt->events_to_see.prev, &evt->events_to_get);
2279 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2280 		*rxxri = (list_entry(evt->events_to_get.prev,
2281 				     typeof(struct event_data),
2282 				     node))->immed_dat;
2283 	}
2284 	evt->waiting = 0;
2285 
2286 err_get_xri_exit:
2287 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2288 	lpfc_bsg_event_unref(evt); /* release ref */
2289 	lpfc_bsg_event_unref(evt); /* delete */
2290 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2291 
2292 	if (dmabuf) {
2293 		if (dmabuf->virt)
2294 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2295 		kfree(dmabuf);
2296 	}
2297 
2298 	if (cmdiocbq && (iocb_stat != IOCB_TIMEDOUT))
2299 		lpfc_sli_release_iocbq(phba, cmdiocbq);
2300 	if (rspiocbq)
2301 		lpfc_sli_release_iocbq(phba, rspiocbq);
2302 	return ret_val;
2303 }
2304 
2305 /**
2306  * lpfc_bsg_dma_page_alloc - allocate a bsg mbox page sized dma buffers
2307  * @phba: Pointer to HBA context object
2308  *
2309  * This function allocates BSG_MBOX_SIZE (4KB) page size dma buffer and.
2310  * retruns the pointer to the buffer.
2311  **/
2312 static struct lpfc_dmabuf *
2313 lpfc_bsg_dma_page_alloc(struct lpfc_hba *phba)
2314 {
2315 	struct lpfc_dmabuf *dmabuf;
2316 	struct pci_dev *pcidev = phba->pcidev;
2317 
2318 	/* allocate dma buffer struct */
2319 	dmabuf = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2320 	if (!dmabuf)
2321 		return NULL;
2322 
2323 	INIT_LIST_HEAD(&dmabuf->list);
2324 
2325 	/* now, allocate dma buffer */
2326 	dmabuf->virt = dma_alloc_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2327 					  &(dmabuf->phys), GFP_KERNEL);
2328 
2329 	if (!dmabuf->virt) {
2330 		kfree(dmabuf);
2331 		return NULL;
2332 	}
2333 	memset((uint8_t *)dmabuf->virt, 0, BSG_MBOX_SIZE);
2334 
2335 	return dmabuf;
2336 }
2337 
2338 /**
2339  * lpfc_bsg_dma_page_free - free a bsg mbox page sized dma buffer
2340  * @phba: Pointer to HBA context object.
2341  * @dmabuf: Pointer to the bsg mbox page sized dma buffer descriptor.
2342  *
2343  * This routine just simply frees a dma buffer and its associated buffer
2344  * descriptor referred by @dmabuf.
2345  **/
2346 static void
2347 lpfc_bsg_dma_page_free(struct lpfc_hba *phba, struct lpfc_dmabuf *dmabuf)
2348 {
2349 	struct pci_dev *pcidev = phba->pcidev;
2350 
2351 	if (!dmabuf)
2352 		return;
2353 
2354 	if (dmabuf->virt)
2355 		dma_free_coherent(&pcidev->dev, BSG_MBOX_SIZE,
2356 				  dmabuf->virt, dmabuf->phys);
2357 	kfree(dmabuf);
2358 	return;
2359 }
2360 
2361 /**
2362  * lpfc_bsg_dma_page_list_free - free a list of bsg mbox page sized dma buffers
2363  * @phba: Pointer to HBA context object.
2364  * @dmabuf_list: Pointer to a list of bsg mbox page sized dma buffer descs.
2365  *
2366  * This routine just simply frees all dma buffers and their associated buffer
2367  * descriptors referred by @dmabuf_list.
2368  **/
2369 static void
2370 lpfc_bsg_dma_page_list_free(struct lpfc_hba *phba,
2371 			    struct list_head *dmabuf_list)
2372 {
2373 	struct lpfc_dmabuf *dmabuf, *next_dmabuf;
2374 
2375 	if (list_empty(dmabuf_list))
2376 		return;
2377 
2378 	list_for_each_entry_safe(dmabuf, next_dmabuf, dmabuf_list, list) {
2379 		list_del_init(&dmabuf->list);
2380 		lpfc_bsg_dma_page_free(phba, dmabuf);
2381 	}
2382 	return;
2383 }
2384 
2385 /**
2386  * diag_cmd_data_alloc - fills in a bde struct with dma buffers
2387  * @phba: Pointer to HBA context object
2388  * @bpl: Pointer to 64 bit bde structure
2389  * @size: Number of bytes to process
2390  * @nocopydata: Flag to copy user data into the allocated buffer
2391  *
2392  * This function allocates page size buffers and populates an lpfc_dmabufext.
2393  * If allowed the user data pointed to with indataptr is copied into the kernel
2394  * memory. The chained list of page size buffers is returned.
2395  **/
2396 static struct lpfc_dmabufext *
2397 diag_cmd_data_alloc(struct lpfc_hba *phba,
2398 		   struct ulp_bde64 *bpl, uint32_t size,
2399 		   int nocopydata)
2400 {
2401 	struct lpfc_dmabufext *mlist = NULL;
2402 	struct lpfc_dmabufext *dmp;
2403 	int cnt, offset = 0, i = 0;
2404 	struct pci_dev *pcidev;
2405 
2406 	pcidev = phba->pcidev;
2407 
2408 	while (size) {
2409 		/* We get chunks of 4K */
2410 		if (size > BUF_SZ_4K)
2411 			cnt = BUF_SZ_4K;
2412 		else
2413 			cnt = size;
2414 
2415 		/* allocate struct lpfc_dmabufext buffer header */
2416 		dmp = kmalloc(sizeof(struct lpfc_dmabufext), GFP_KERNEL);
2417 		if (!dmp)
2418 			goto out;
2419 
2420 		INIT_LIST_HEAD(&dmp->dma.list);
2421 
2422 		/* Queue it to a linked list */
2423 		if (mlist)
2424 			list_add_tail(&dmp->dma.list, &mlist->dma.list);
2425 		else
2426 			mlist = dmp;
2427 
2428 		/* allocate buffer */
2429 		dmp->dma.virt = dma_alloc_coherent(&pcidev->dev,
2430 						   cnt,
2431 						   &(dmp->dma.phys),
2432 						   GFP_KERNEL);
2433 
2434 		if (!dmp->dma.virt)
2435 			goto out;
2436 
2437 		dmp->size = cnt;
2438 
2439 		if (nocopydata) {
2440 			bpl->tus.f.bdeFlags = 0;
2441 			pci_dma_sync_single_for_device(phba->pcidev,
2442 				dmp->dma.phys, LPFC_BPL_SIZE, PCI_DMA_TODEVICE);
2443 
2444 		} else {
2445 			memset((uint8_t *)dmp->dma.virt, 0, cnt);
2446 			bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2447 		}
2448 
2449 		/* build buffer ptr list for IOCB */
2450 		bpl->addrLow = le32_to_cpu(putPaddrLow(dmp->dma.phys));
2451 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(dmp->dma.phys));
2452 		bpl->tus.f.bdeSize = (ushort) cnt;
2453 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
2454 		bpl++;
2455 
2456 		i++;
2457 		offset += cnt;
2458 		size -= cnt;
2459 	}
2460 
2461 	mlist->flag = i;
2462 	return mlist;
2463 out:
2464 	diag_cmd_data_free(phba, mlist);
2465 	return NULL;
2466 }
2467 
2468 /**
2469  * lpfcdiag_loop_post_rxbufs - post the receive buffers for an unsol CT cmd
2470  * @phba: Pointer to HBA context object
2471  * @rxxri: Receive exchange id
2472  * @len: Number of data bytes
2473  *
2474  * This function allocates and posts a data buffer of sufficient size to receive
2475  * an unsolicted CT command.
2476  **/
2477 static int lpfcdiag_loop_post_rxbufs(struct lpfc_hba *phba, uint16_t rxxri,
2478 			     size_t len)
2479 {
2480 	struct lpfc_sli *psli = &phba->sli;
2481 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
2482 	struct lpfc_iocbq *cmdiocbq;
2483 	IOCB_t *cmd = NULL;
2484 	struct list_head head, *curr, *next;
2485 	struct lpfc_dmabuf *rxbmp;
2486 	struct lpfc_dmabuf *dmp;
2487 	struct lpfc_dmabuf *mp[2] = {NULL, NULL};
2488 	struct ulp_bde64 *rxbpl = NULL;
2489 	uint32_t num_bde;
2490 	struct lpfc_dmabufext *rxbuffer = NULL;
2491 	int ret_val = 0;
2492 	int iocb_stat;
2493 	int i = 0;
2494 
2495 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2496 	rxbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2497 	if (rxbmp != NULL) {
2498 		rxbmp->virt = lpfc_mbuf_alloc(phba, 0, &rxbmp->phys);
2499 		if (rxbmp->virt) {
2500 			INIT_LIST_HEAD(&rxbmp->list);
2501 			rxbpl = (struct ulp_bde64 *) rxbmp->virt;
2502 			rxbuffer = diag_cmd_data_alloc(phba, rxbpl, len, 0);
2503 		}
2504 	}
2505 
2506 	if (!cmdiocbq || !rxbmp || !rxbpl || !rxbuffer) {
2507 		ret_val = -ENOMEM;
2508 		goto err_post_rxbufs_exit;
2509 	}
2510 
2511 	/* Queue buffers for the receive exchange */
2512 	num_bde = (uint32_t)rxbuffer->flag;
2513 	dmp = &rxbuffer->dma;
2514 
2515 	cmd = &cmdiocbq->iocb;
2516 	i = 0;
2517 
2518 	INIT_LIST_HEAD(&head);
2519 	list_add_tail(&head, &dmp->list);
2520 	list_for_each_safe(curr, next, &head) {
2521 		mp[i] = list_entry(curr, struct lpfc_dmabuf, list);
2522 		list_del(curr);
2523 
2524 		if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2525 			mp[i]->buffer_tag = lpfc_sli_get_buffer_tag(phba);
2526 			cmd->un.quexri64cx.buff.bde.addrHigh =
2527 				putPaddrHigh(mp[i]->phys);
2528 			cmd->un.quexri64cx.buff.bde.addrLow =
2529 				putPaddrLow(mp[i]->phys);
2530 			cmd->un.quexri64cx.buff.bde.tus.f.bdeSize =
2531 				((struct lpfc_dmabufext *)mp[i])->size;
2532 			cmd->un.quexri64cx.buff.buffer_tag = mp[i]->buffer_tag;
2533 			cmd->ulpCommand = CMD_QUE_XRI64_CX;
2534 			cmd->ulpPU = 0;
2535 			cmd->ulpLe = 1;
2536 			cmd->ulpBdeCount = 1;
2537 			cmd->unsli3.que_xri64cx_ext_words.ebde_count = 0;
2538 
2539 		} else {
2540 			cmd->un.cont64[i].addrHigh = putPaddrHigh(mp[i]->phys);
2541 			cmd->un.cont64[i].addrLow = putPaddrLow(mp[i]->phys);
2542 			cmd->un.cont64[i].tus.f.bdeSize =
2543 				((struct lpfc_dmabufext *)mp[i])->size;
2544 					cmd->ulpBdeCount = ++i;
2545 
2546 			if ((--num_bde > 0) && (i < 2))
2547 				continue;
2548 
2549 			cmd->ulpCommand = CMD_QUE_XRI_BUF64_CX;
2550 			cmd->ulpLe = 1;
2551 		}
2552 
2553 		cmd->ulpClass = CLASS3;
2554 		cmd->ulpContext = rxxri;
2555 
2556 		iocb_stat = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
2557 						0);
2558 		if (iocb_stat == IOCB_ERROR) {
2559 			diag_cmd_data_free(phba,
2560 				(struct lpfc_dmabufext *)mp[0]);
2561 			if (mp[1])
2562 				diag_cmd_data_free(phba,
2563 					  (struct lpfc_dmabufext *)mp[1]);
2564 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2565 			ret_val = -EIO;
2566 			goto err_post_rxbufs_exit;
2567 		}
2568 
2569 		lpfc_sli_ringpostbuf_put(phba, pring, mp[0]);
2570 		if (mp[1]) {
2571 			lpfc_sli_ringpostbuf_put(phba, pring, mp[1]);
2572 			mp[1] = NULL;
2573 		}
2574 
2575 		/* The iocb was freed by lpfc_sli_issue_iocb */
2576 		cmdiocbq = lpfc_sli_get_iocbq(phba);
2577 		if (!cmdiocbq) {
2578 			dmp = list_entry(next, struct lpfc_dmabuf, list);
2579 			ret_val = -EIO;
2580 			goto err_post_rxbufs_exit;
2581 		}
2582 
2583 		cmd = &cmdiocbq->iocb;
2584 		i = 0;
2585 	}
2586 	list_del(&head);
2587 
2588 err_post_rxbufs_exit:
2589 
2590 	if (rxbmp) {
2591 		if (rxbmp->virt)
2592 			lpfc_mbuf_free(phba, rxbmp->virt, rxbmp->phys);
2593 		kfree(rxbmp);
2594 	}
2595 
2596 	if (cmdiocbq)
2597 		lpfc_sli_release_iocbq(phba, cmdiocbq);
2598 	return ret_val;
2599 }
2600 
2601 /**
2602  * lpfc_bsg_diag_loopback_run - run loopback on a port by issue ct cmd to itself
2603  * @job: LPFC_BSG_VENDOR_DIAG_TEST fc_bsg_job
2604  *
2605  * This function receives a user data buffer to be transmitted and received on
2606  * the same port, the link must be up and in loopback mode prior
2607  * to being called.
2608  * 1. A kernel buffer is allocated to copy the user data into.
2609  * 2. The port registers with "itself".
2610  * 3. The transmit and receive exchange ids are obtained.
2611  * 4. The receive exchange id is posted.
2612  * 5. A new els loopback event is created.
2613  * 6. The command and response iocbs are allocated.
2614  * 7. The cmd iocb FsType is set to elx loopback and the CmdRsp to looppback.
2615  *
2616  * This function is meant to be called n times while the port is in loopback
2617  * so it is the apps responsibility to issue a reset to take the port out
2618  * of loopback mode.
2619  **/
2620 static int
2621 lpfc_bsg_diag_loopback_run(struct fc_bsg_job *job)
2622 {
2623 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2624 	struct lpfc_hba *phba = vport->phba;
2625 	struct diag_mode_test *diag_mode;
2626 	struct lpfc_bsg_event *evt;
2627 	struct event_data *evdat;
2628 	struct lpfc_sli *psli = &phba->sli;
2629 	uint32_t size;
2630 	uint32_t full_size;
2631 	size_t segment_len = 0, segment_offset = 0, current_offset = 0;
2632 	uint16_t rpi = 0;
2633 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
2634 	IOCB_t *cmd, *rsp;
2635 	struct lpfc_sli_ct_request *ctreq;
2636 	struct lpfc_dmabuf *txbmp;
2637 	struct ulp_bde64 *txbpl = NULL;
2638 	struct lpfc_dmabufext *txbuffer = NULL;
2639 	struct list_head head;
2640 	struct lpfc_dmabuf  *curr;
2641 	uint16_t txxri, rxxri;
2642 	uint32_t num_bde;
2643 	uint8_t *ptr = NULL, *rx_databuf = NULL;
2644 	int rc = 0;
2645 	int time_left;
2646 	int iocb_stat;
2647 	unsigned long flags;
2648 	void *dataout = NULL;
2649 	uint32_t total_mem;
2650 
2651 	/* in case no data is returned return just the return code */
2652 	job->reply->reply_payload_rcv_len = 0;
2653 
2654 	if (job->request_len <
2655 	    sizeof(struct fc_bsg_request) + sizeof(struct diag_mode_test)) {
2656 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2657 				"2739 Received DIAG TEST request below minimum "
2658 				"size\n");
2659 		rc = -EINVAL;
2660 		goto loopback_test_exit;
2661 	}
2662 
2663 	if (job->request_payload.payload_len !=
2664 		job->reply_payload.payload_len) {
2665 		rc = -EINVAL;
2666 		goto loopback_test_exit;
2667 	}
2668 
2669 	diag_mode = (struct diag_mode_test *)
2670 		job->request->rqst_data.h_vendor.vendor_cmd;
2671 
2672 	if ((phba->link_state == LPFC_HBA_ERROR) ||
2673 	    (psli->sli_flag & LPFC_BLOCK_MGMT_IO) ||
2674 	    (!(psli->sli_flag & LPFC_SLI_ACTIVE))) {
2675 		rc = -EACCES;
2676 		goto loopback_test_exit;
2677 	}
2678 
2679 	if (!lpfc_is_link_up(phba) || !(phba->link_flag & LS_LOOPBACK_MODE)) {
2680 		rc = -EACCES;
2681 		goto loopback_test_exit;
2682 	}
2683 
2684 	size = job->request_payload.payload_len;
2685 	full_size = size + ELX_LOOPBACK_HEADER_SZ; /* plus the header */
2686 
2687 	if ((size == 0) || (size > 80 * BUF_SZ_4K)) {
2688 		rc = -ERANGE;
2689 		goto loopback_test_exit;
2690 	}
2691 
2692 	if (full_size >= BUF_SZ_4K) {
2693 		/*
2694 		 * Allocate memory for ioctl data. If buffer is bigger than 64k,
2695 		 * then we allocate 64k and re-use that buffer over and over to
2696 		 * xfer the whole block. This is because Linux kernel has a
2697 		 * problem allocating more than 120k of kernel space memory. Saw
2698 		 * problem with GET_FCPTARGETMAPPING...
2699 		 */
2700 		if (size <= (64 * 1024))
2701 			total_mem = full_size;
2702 		else
2703 			total_mem = 64 * 1024;
2704 	} else
2705 		/* Allocate memory for ioctl data */
2706 		total_mem = BUF_SZ_4K;
2707 
2708 	dataout = kmalloc(total_mem, GFP_KERNEL);
2709 	if (dataout == NULL) {
2710 		rc = -ENOMEM;
2711 		goto loopback_test_exit;
2712 	}
2713 
2714 	ptr = dataout;
2715 	ptr += ELX_LOOPBACK_HEADER_SZ;
2716 	sg_copy_to_buffer(job->request_payload.sg_list,
2717 				job->request_payload.sg_cnt,
2718 				ptr, size);
2719 	rc = lpfcdiag_loop_self_reg(phba, &rpi);
2720 	if (rc)
2721 		goto loopback_test_exit;
2722 
2723 	rc = lpfcdiag_loop_get_xri(phba, rpi, &txxri, &rxxri);
2724 	if (rc) {
2725 		lpfcdiag_loop_self_unreg(phba, rpi);
2726 		goto loopback_test_exit;
2727 	}
2728 
2729 	rc = lpfcdiag_loop_post_rxbufs(phba, rxxri, full_size);
2730 	if (rc) {
2731 		lpfcdiag_loop_self_unreg(phba, rpi);
2732 		goto loopback_test_exit;
2733 	}
2734 
2735 	evt = lpfc_bsg_event_new(FC_REG_CT_EVENT, current->pid,
2736 				SLI_CT_ELX_LOOPBACK);
2737 	if (!evt) {
2738 		lpfcdiag_loop_self_unreg(phba, rpi);
2739 		rc = -ENOMEM;
2740 		goto loopback_test_exit;
2741 	}
2742 
2743 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2744 	list_add(&evt->node, &phba->ct_ev_waiters);
2745 	lpfc_bsg_event_ref(evt);
2746 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2747 
2748 	cmdiocbq = lpfc_sli_get_iocbq(phba);
2749 	rspiocbq = lpfc_sli_get_iocbq(phba);
2750 	txbmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
2751 
2752 	if (txbmp) {
2753 		txbmp->virt = lpfc_mbuf_alloc(phba, 0, &txbmp->phys);
2754 		if (txbmp->virt) {
2755 			INIT_LIST_HEAD(&txbmp->list);
2756 			txbpl = (struct ulp_bde64 *) txbmp->virt;
2757 			txbuffer = diag_cmd_data_alloc(phba,
2758 							txbpl, full_size, 0);
2759 		}
2760 	}
2761 
2762 	if (!cmdiocbq || !rspiocbq || !txbmp || !txbpl || !txbuffer ||
2763 		!txbmp->virt) {
2764 		rc = -ENOMEM;
2765 		goto err_loopback_test_exit;
2766 	}
2767 
2768 	cmd = &cmdiocbq->iocb;
2769 	rsp = &rspiocbq->iocb;
2770 
2771 	INIT_LIST_HEAD(&head);
2772 	list_add_tail(&head, &txbuffer->dma.list);
2773 	list_for_each_entry(curr, &head, list) {
2774 		segment_len = ((struct lpfc_dmabufext *)curr)->size;
2775 		if (current_offset == 0) {
2776 			ctreq = curr->virt;
2777 			memset(ctreq, 0, ELX_LOOPBACK_HEADER_SZ);
2778 			ctreq->RevisionId.bits.Revision = SLI_CT_REVISION;
2779 			ctreq->RevisionId.bits.InId = 0;
2780 			ctreq->FsType = SLI_CT_ELX_LOOPBACK;
2781 			ctreq->FsSubType = 0;
2782 			ctreq->CommandResponse.bits.CmdRsp = ELX_LOOPBACK_DATA;
2783 			ctreq->CommandResponse.bits.Size   = size;
2784 			segment_offset = ELX_LOOPBACK_HEADER_SZ;
2785 		} else
2786 			segment_offset = 0;
2787 
2788 		BUG_ON(segment_offset >= segment_len);
2789 		memcpy(curr->virt + segment_offset,
2790 			ptr + current_offset,
2791 			segment_len - segment_offset);
2792 
2793 		current_offset += segment_len - segment_offset;
2794 		BUG_ON(current_offset > size);
2795 	}
2796 	list_del(&head);
2797 
2798 	/* Build the XMIT_SEQUENCE iocb */
2799 
2800 	num_bde = (uint32_t)txbuffer->flag;
2801 
2802 	cmd->un.xseq64.bdl.addrHigh = putPaddrHigh(txbmp->phys);
2803 	cmd->un.xseq64.bdl.addrLow = putPaddrLow(txbmp->phys);
2804 	cmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
2805 	cmd->un.xseq64.bdl.bdeSize = (num_bde * sizeof(struct ulp_bde64));
2806 
2807 	cmd->un.xseq64.w5.hcsw.Fctl = (LS | LA);
2808 	cmd->un.xseq64.w5.hcsw.Dfctl = 0;
2809 	cmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CTL;
2810 	cmd->un.xseq64.w5.hcsw.Type = FC_TYPE_CT;
2811 
2812 	cmd->ulpCommand = CMD_XMIT_SEQUENCE64_CX;
2813 	cmd->ulpBdeCount = 1;
2814 	cmd->ulpLe = 1;
2815 	cmd->ulpClass = CLASS3;
2816 	cmd->ulpContext = txxri;
2817 
2818 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
2819 	cmdiocbq->vport = phba->pport;
2820 
2821 	iocb_stat = lpfc_sli_issue_iocb_wait(phba, LPFC_ELS_RING, cmdiocbq,
2822 					     rspiocbq, (phba->fc_ratov * 2) +
2823 					     LPFC_DRVR_TIMEOUT);
2824 
2825 	if ((iocb_stat != IOCB_SUCCESS) || (rsp->ulpStatus != IOCB_SUCCESS)) {
2826 		rc = -EIO;
2827 		goto err_loopback_test_exit;
2828 	}
2829 
2830 	evt->waiting = 1;
2831 	time_left = wait_event_interruptible_timeout(
2832 		evt->wq, !list_empty(&evt->events_to_see),
2833 		((phba->fc_ratov * 2) + LPFC_DRVR_TIMEOUT) * HZ);
2834 	evt->waiting = 0;
2835 	if (list_empty(&evt->events_to_see))
2836 		rc = (time_left) ? -EINTR : -ETIMEDOUT;
2837 	else {
2838 		spin_lock_irqsave(&phba->ct_ev_lock, flags);
2839 		list_move(evt->events_to_see.prev, &evt->events_to_get);
2840 		evdat = list_entry(evt->events_to_get.prev,
2841 				   typeof(*evdat), node);
2842 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2843 		rx_databuf = evdat->data;
2844 		if (evdat->len != full_size) {
2845 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
2846 				"1603 Loopback test did not receive expected "
2847 				"data length. actual length 0x%x expected "
2848 				"length 0x%x\n",
2849 				evdat->len, full_size);
2850 			rc = -EIO;
2851 		} else if (rx_databuf == NULL)
2852 			rc = -EIO;
2853 		else {
2854 			rc = IOCB_SUCCESS;
2855 			/* skip over elx loopback header */
2856 			rx_databuf += ELX_LOOPBACK_HEADER_SZ;
2857 			job->reply->reply_payload_rcv_len =
2858 				sg_copy_from_buffer(job->reply_payload.sg_list,
2859 						    job->reply_payload.sg_cnt,
2860 						    rx_databuf, size);
2861 			job->reply->reply_payload_rcv_len = size;
2862 		}
2863 	}
2864 
2865 err_loopback_test_exit:
2866 	lpfcdiag_loop_self_unreg(phba, rpi);
2867 
2868 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2869 	lpfc_bsg_event_unref(evt); /* release ref */
2870 	lpfc_bsg_event_unref(evt); /* delete */
2871 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2872 
2873 	if (cmdiocbq != NULL)
2874 		lpfc_sli_release_iocbq(phba, cmdiocbq);
2875 
2876 	if (rspiocbq != NULL)
2877 		lpfc_sli_release_iocbq(phba, rspiocbq);
2878 
2879 	if (txbmp != NULL) {
2880 		if (txbpl != NULL) {
2881 			if (txbuffer != NULL)
2882 				diag_cmd_data_free(phba, txbuffer);
2883 			lpfc_mbuf_free(phba, txbmp->virt, txbmp->phys);
2884 		}
2885 		kfree(txbmp);
2886 	}
2887 
2888 loopback_test_exit:
2889 	kfree(dataout);
2890 	/* make error code available to userspace */
2891 	job->reply->result = rc;
2892 	job->dd_data = NULL;
2893 	/* complete the job back to userspace if no error */
2894 	if (rc == 0)
2895 		job->job_done(job);
2896 	return rc;
2897 }
2898 
2899 /**
2900  * lpfc_bsg_get_dfc_rev - process a GET_DFC_REV bsg vendor command
2901  * @job: GET_DFC_REV fc_bsg_job
2902  **/
2903 static int
2904 lpfc_bsg_get_dfc_rev(struct fc_bsg_job *job)
2905 {
2906 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
2907 	struct lpfc_hba *phba = vport->phba;
2908 	struct get_mgmt_rev *event_req;
2909 	struct get_mgmt_rev_reply *event_reply;
2910 	int rc = 0;
2911 
2912 	if (job->request_len <
2913 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev)) {
2914 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2915 				"2740 Received GET_DFC_REV request below "
2916 				"minimum size\n");
2917 		rc = -EINVAL;
2918 		goto job_error;
2919 	}
2920 
2921 	event_req = (struct get_mgmt_rev *)
2922 		job->request->rqst_data.h_vendor.vendor_cmd;
2923 
2924 	event_reply = (struct get_mgmt_rev_reply *)
2925 		job->reply->reply_data.vendor_reply.vendor_rsp;
2926 
2927 	if (job->reply_len <
2928 	    sizeof(struct fc_bsg_request) + sizeof(struct get_mgmt_rev_reply)) {
2929 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
2930 				"2741 Received GET_DFC_REV reply below "
2931 				"minimum size\n");
2932 		rc = -EINVAL;
2933 		goto job_error;
2934 	}
2935 
2936 	event_reply->info.a_Major = MANAGEMENT_MAJOR_REV;
2937 	event_reply->info.a_Minor = MANAGEMENT_MINOR_REV;
2938 job_error:
2939 	job->reply->result = rc;
2940 	if (rc == 0)
2941 		job->job_done(job);
2942 	return rc;
2943 }
2944 
2945 /**
2946  * lpfc_bsg_issue_mbox_cmpl - lpfc_bsg_issue_mbox mbox completion handler
2947  * @phba: Pointer to HBA context object.
2948  * @pmboxq: Pointer to mailbox command.
2949  *
2950  * This is completion handler function for mailbox commands issued from
2951  * lpfc_bsg_issue_mbox function. This function is called by the
2952  * mailbox event handler function with no lock held. This function
2953  * will wake up thread waiting on the wait queue pointed by context1
2954  * of the mailbox.
2955  **/
2956 void
2957 lpfc_bsg_issue_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2958 {
2959 	struct bsg_job_data *dd_data;
2960 	struct fc_bsg_job *job;
2961 	uint32_t size;
2962 	unsigned long flags;
2963 	uint8_t *pmb, *pmb_buf;
2964 
2965 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
2966 	dd_data = pmboxq->context1;
2967 	/* job already timed out? */
2968 	if (!dd_data) {
2969 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2970 		return;
2971 	}
2972 
2973 	/*
2974 	 * The outgoing buffer is readily referred from the dma buffer,
2975 	 * just need to get header part from mailboxq structure.
2976 	 */
2977 	pmb = (uint8_t *)&pmboxq->u.mb;
2978 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
2979 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
2980 
2981 	job = dd_data->context_un.mbox.set_job;
2982 	if (job) {
2983 		size = job->reply_payload.payload_len;
2984 		job->reply->reply_payload_rcv_len =
2985 			sg_copy_from_buffer(job->reply_payload.sg_list,
2986 					    job->reply_payload.sg_cnt,
2987 					    pmb_buf, size);
2988 		/* need to hold the lock until we set job->dd_data to NULL
2989 		 * to hold off the timeout handler returning to the mid-layer
2990 		 * while we are still processing the job.
2991 		 */
2992 		job->dd_data = NULL;
2993 		dd_data->context_un.mbox.set_job = NULL;
2994 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2995 	} else {
2996 		dd_data->context_un.mbox.set_job = NULL;
2997 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
2998 	}
2999 
3000 	mempool_free(dd_data->context_un.mbox.pmboxq, phba->mbox_mem_pool);
3001 	lpfc_bsg_dma_page_free(phba, dd_data->context_un.mbox.dmabuffers);
3002 	kfree(dd_data);
3003 
3004 	if (job) {
3005 		job->reply->result = 0;
3006 		job->job_done(job);
3007 	}
3008 	return;
3009 }
3010 
3011 /**
3012  * lpfc_bsg_check_cmd_access - test for a supported mailbox command
3013  * @phba: Pointer to HBA context object.
3014  * @mb: Pointer to a mailbox object.
3015  * @vport: Pointer to a vport object.
3016  *
3017  * Some commands require the port to be offline, some may not be called from
3018  * the application.
3019  **/
3020 static int lpfc_bsg_check_cmd_access(struct lpfc_hba *phba,
3021 	MAILBOX_t *mb, struct lpfc_vport *vport)
3022 {
3023 	/* return negative error values for bsg job */
3024 	switch (mb->mbxCommand) {
3025 	/* Offline only */
3026 	case MBX_INIT_LINK:
3027 	case MBX_DOWN_LINK:
3028 	case MBX_CONFIG_LINK:
3029 	case MBX_CONFIG_RING:
3030 	case MBX_RESET_RING:
3031 	case MBX_UNREG_LOGIN:
3032 	case MBX_CLEAR_LA:
3033 	case MBX_DUMP_CONTEXT:
3034 	case MBX_RUN_DIAGS:
3035 	case MBX_RESTART:
3036 	case MBX_SET_MASK:
3037 		if (!(vport->fc_flag & FC_OFFLINE_MODE)) {
3038 			lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3039 				"2743 Command 0x%x is illegal in on-line "
3040 				"state\n",
3041 				mb->mbxCommand);
3042 			return -EPERM;
3043 		}
3044 	case MBX_WRITE_NV:
3045 	case MBX_WRITE_VPARMS:
3046 	case MBX_LOAD_SM:
3047 	case MBX_READ_NV:
3048 	case MBX_READ_CONFIG:
3049 	case MBX_READ_RCONFIG:
3050 	case MBX_READ_STATUS:
3051 	case MBX_READ_XRI:
3052 	case MBX_READ_REV:
3053 	case MBX_READ_LNK_STAT:
3054 	case MBX_DUMP_MEMORY:
3055 	case MBX_DOWN_LOAD:
3056 	case MBX_UPDATE_CFG:
3057 	case MBX_KILL_BOARD:
3058 	case MBX_LOAD_AREA:
3059 	case MBX_LOAD_EXP_ROM:
3060 	case MBX_BEACON:
3061 	case MBX_DEL_LD_ENTRY:
3062 	case MBX_SET_DEBUG:
3063 	case MBX_WRITE_WWN:
3064 	case MBX_SLI4_CONFIG:
3065 	case MBX_READ_EVENT_LOG:
3066 	case MBX_READ_EVENT_LOG_STATUS:
3067 	case MBX_WRITE_EVENT_LOG:
3068 	case MBX_PORT_CAPABILITIES:
3069 	case MBX_PORT_IOV_CONTROL:
3070 	case MBX_RUN_BIU_DIAG64:
3071 		break;
3072 	case MBX_SET_VARIABLE:
3073 		lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3074 			"1226 mbox: set_variable 0x%x, 0x%x\n",
3075 			mb->un.varWords[0],
3076 			mb->un.varWords[1]);
3077 		if ((mb->un.varWords[0] == SETVAR_MLOMNT)
3078 			&& (mb->un.varWords[1] == 1)) {
3079 			phba->wait_4_mlo_maint_flg = 1;
3080 		} else if (mb->un.varWords[0] == SETVAR_MLORST) {
3081 			phba->link_flag &= ~LS_LOOPBACK_MODE;
3082 			phba->fc_topology = LPFC_TOPOLOGY_PT_PT;
3083 		}
3084 		break;
3085 	case MBX_READ_SPARM64:
3086 	case MBX_READ_TOPOLOGY:
3087 	case MBX_REG_LOGIN:
3088 	case MBX_REG_LOGIN64:
3089 	case MBX_CONFIG_PORT:
3090 	case MBX_RUN_BIU_DIAG:
3091 	default:
3092 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
3093 			"2742 Unknown Command 0x%x\n",
3094 			mb->mbxCommand);
3095 		return -EPERM;
3096 	}
3097 
3098 	return 0; /* ok */
3099 }
3100 
3101 /**
3102  * lpfc_bsg_mbox_ext_cleanup - clean up context of multi-buffer mbox session
3103  * @phba: Pointer to HBA context object.
3104  *
3105  * This is routine clean up and reset BSG handling of multi-buffer mbox
3106  * command session.
3107  **/
3108 static void
3109 lpfc_bsg_mbox_ext_session_reset(struct lpfc_hba *phba)
3110 {
3111 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE)
3112 		return;
3113 
3114 	/* free all memory, including dma buffers */
3115 	lpfc_bsg_dma_page_list_free(phba,
3116 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3117 	lpfc_bsg_dma_page_free(phba, phba->mbox_ext_buf_ctx.mbx_dmabuf);
3118 	/* multi-buffer write mailbox command pass-through complete */
3119 	memset((char *)&phba->mbox_ext_buf_ctx, 0,
3120 	       sizeof(struct lpfc_mbox_ext_buf_ctx));
3121 	INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3122 
3123 	return;
3124 }
3125 
3126 /**
3127  * lpfc_bsg_issue_mbox_ext_handle_job - job handler for multi-buffer mbox cmpl
3128  * @phba: Pointer to HBA context object.
3129  * @pmboxq: Pointer to mailbox command.
3130  *
3131  * This is routine handles BSG job for mailbox commands completions with
3132  * multiple external buffers.
3133  **/
3134 static struct fc_bsg_job *
3135 lpfc_bsg_issue_mbox_ext_handle_job(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3136 {
3137 	struct bsg_job_data *dd_data;
3138 	struct fc_bsg_job *job;
3139 	uint8_t *pmb, *pmb_buf;
3140 	unsigned long flags;
3141 	uint32_t size;
3142 	int rc = 0;
3143 
3144 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
3145 	dd_data = pmboxq->context1;
3146 	/* has the job already timed out? */
3147 	if (!dd_data) {
3148 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3149 		job = NULL;
3150 		goto job_done_out;
3151 	}
3152 
3153 	/*
3154 	 * The outgoing buffer is readily referred from the dma buffer,
3155 	 * just need to get header part from mailboxq structure.
3156 	 */
3157 	pmb = (uint8_t *)&pmboxq->u.mb;
3158 	pmb_buf = (uint8_t *)dd_data->context_un.mbox.mb;
3159 	memcpy(pmb_buf, pmb, sizeof(MAILBOX_t));
3160 
3161 	job = dd_data->context_un.mbox.set_job;
3162 	if (job) {
3163 		size = job->reply_payload.payload_len;
3164 		job->reply->reply_payload_rcv_len =
3165 			sg_copy_from_buffer(job->reply_payload.sg_list,
3166 					    job->reply_payload.sg_cnt,
3167 					    pmb_buf, size);
3168 		/* result for successful */
3169 		job->reply->result = 0;
3170 		job->dd_data = NULL;
3171 		/* need to hold the lock util we set job->dd_data to NULL
3172 		 * to hold off the timeout handler from midlayer to take
3173 		 * any action.
3174 		 */
3175 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3176 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3177 				"2937 SLI_CONFIG ext-buffer maibox command "
3178 				"(x%x/x%x) complete bsg job done, bsize:%d\n",
3179 				phba->mbox_ext_buf_ctx.nembType,
3180 				phba->mbox_ext_buf_ctx.mboxType, size);
3181 	} else
3182 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
3183 
3184 job_done_out:
3185 	if (!job)
3186 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3187 				"2938 SLI_CONFIG ext-buffer maibox "
3188 				"command (x%x/x%x) failure, rc:x%x\n",
3189 				phba->mbox_ext_buf_ctx.nembType,
3190 				phba->mbox_ext_buf_ctx.mboxType, rc);
3191 	/* state change */
3192 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_DONE;
3193 	kfree(dd_data);
3194 
3195 	return job;
3196 }
3197 
3198 /**
3199  * lpfc_bsg_issue_read_mbox_ext_cmpl - compl handler for multi-buffer read mbox
3200  * @phba: Pointer to HBA context object.
3201  * @pmboxq: Pointer to mailbox command.
3202  *
3203  * This is completion handler function for mailbox read commands with multiple
3204  * external buffers.
3205  **/
3206 static void
3207 lpfc_bsg_issue_read_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3208 {
3209 	struct fc_bsg_job *job;
3210 
3211 	/* handle the BSG job with mailbox command */
3212 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3213 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3214 
3215 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3216 			"2939 SLI_CONFIG ext-buffer rd maibox command "
3217 			"complete, ctxState:x%x, mbxStatus:x%x\n",
3218 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3219 
3220 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3221 
3222 	if (pmboxq->u.mb.mbxStatus || phba->mbox_ext_buf_ctx.numBuf == 1)
3223 		lpfc_bsg_mbox_ext_session_reset(phba);
3224 
3225 	/* free base driver mailbox structure memory */
3226 	mempool_free(pmboxq, phba->mbox_mem_pool);
3227 
3228 	/* complete the bsg job if we have it */
3229 	if (job)
3230 		job->job_done(job);
3231 
3232 	return;
3233 }
3234 
3235 /**
3236  * lpfc_bsg_issue_write_mbox_ext_cmpl - cmpl handler for multi-buffer write mbox
3237  * @phba: Pointer to HBA context object.
3238  * @pmboxq: Pointer to mailbox command.
3239  *
3240  * This is completion handler function for mailbox write commands with multiple
3241  * external buffers.
3242  **/
3243 static void
3244 lpfc_bsg_issue_write_mbox_ext_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
3245 {
3246 	struct fc_bsg_job *job;
3247 
3248 	/* handle the BSG job with the mailbox command */
3249 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_ABTS)
3250 		pmboxq->u.mb.mbxStatus = MBXERR_ERROR;
3251 
3252 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3253 			"2940 SLI_CONFIG ext-buffer wr maibox command "
3254 			"complete, ctxState:x%x, mbxStatus:x%x\n",
3255 			phba->mbox_ext_buf_ctx.state, pmboxq->u.mb.mbxStatus);
3256 
3257 	job = lpfc_bsg_issue_mbox_ext_handle_job(phba, pmboxq);
3258 
3259 	/* free all memory, including dma buffers */
3260 	mempool_free(pmboxq, phba->mbox_mem_pool);
3261 	lpfc_bsg_mbox_ext_session_reset(phba);
3262 
3263 	/* complete the bsg job if we have it */
3264 	if (job)
3265 		job->job_done(job);
3266 
3267 	return;
3268 }
3269 
3270 static void
3271 lpfc_bsg_sli_cfg_dma_desc_setup(struct lpfc_hba *phba, enum nemb_type nemb_tp,
3272 				uint32_t index, struct lpfc_dmabuf *mbx_dmabuf,
3273 				struct lpfc_dmabuf *ext_dmabuf)
3274 {
3275 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3276 
3277 	/* pointer to the start of mailbox command */
3278 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)mbx_dmabuf->virt;
3279 
3280 	if (nemb_tp == nemb_mse) {
3281 		if (index == 0) {
3282 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3283 				mse[index].pa_hi =
3284 				putPaddrHigh(mbx_dmabuf->phys +
3285 					     sizeof(MAILBOX_t));
3286 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3287 				mse[index].pa_lo =
3288 				putPaddrLow(mbx_dmabuf->phys +
3289 					    sizeof(MAILBOX_t));
3290 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3291 					"2943 SLI_CONFIG(mse)[%d], "
3292 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3293 					index,
3294 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3295 					mse[index].buf_len,
3296 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3297 					mse[index].pa_hi,
3298 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3299 					mse[index].pa_lo);
3300 		} else {
3301 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3302 				mse[index].pa_hi =
3303 				putPaddrHigh(ext_dmabuf->phys);
3304 			sli_cfg_mbx->un.sli_config_emb0_subsys.
3305 				mse[index].pa_lo =
3306 				putPaddrLow(ext_dmabuf->phys);
3307 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3308 					"2944 SLI_CONFIG(mse)[%d], "
3309 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3310 					index,
3311 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3312 					mse[index].buf_len,
3313 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3314 					mse[index].pa_hi,
3315 					sli_cfg_mbx->un.sli_config_emb0_subsys.
3316 					mse[index].pa_lo);
3317 		}
3318 	} else {
3319 		if (index == 0) {
3320 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3321 				hbd[index].pa_hi =
3322 				putPaddrHigh(mbx_dmabuf->phys +
3323 					     sizeof(MAILBOX_t));
3324 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3325 				hbd[index].pa_lo =
3326 				putPaddrLow(mbx_dmabuf->phys +
3327 					    sizeof(MAILBOX_t));
3328 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3329 					"3007 SLI_CONFIG(hbd)[%d], "
3330 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3331 				index,
3332 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3333 				&sli_cfg_mbx->un.
3334 				sli_config_emb1_subsys.hbd[index]),
3335 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3336 				hbd[index].pa_hi,
3337 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3338 				hbd[index].pa_lo);
3339 
3340 		} else {
3341 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3342 				hbd[index].pa_hi =
3343 				putPaddrHigh(ext_dmabuf->phys);
3344 			sli_cfg_mbx->un.sli_config_emb1_subsys.
3345 				hbd[index].pa_lo =
3346 				putPaddrLow(ext_dmabuf->phys);
3347 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3348 					"3008 SLI_CONFIG(hbd)[%d], "
3349 					"bufLen:%d, addrHi:x%x, addrLo:x%x\n",
3350 				index,
3351 				bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3352 				&sli_cfg_mbx->un.
3353 				sli_config_emb1_subsys.hbd[index]),
3354 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3355 				hbd[index].pa_hi,
3356 				sli_cfg_mbx->un.sli_config_emb1_subsys.
3357 				hbd[index].pa_lo);
3358 		}
3359 	}
3360 	return;
3361 }
3362 
3363 /**
3364  * lpfc_bsg_sli_cfg_mse_read_cmd_ext - sli_config non-embedded mailbox cmd read
3365  * @phba: Pointer to HBA context object.
3366  * @mb: Pointer to a BSG mailbox object.
3367  * @nemb_tp: Enumerate of non-embedded mailbox command type.
3368  * @dmabuff: Pointer to a DMA buffer descriptor.
3369  *
3370  * This routine performs SLI_CONFIG (0x9B) read mailbox command operation with
3371  * non-embedded external bufffers.
3372  **/
3373 static int
3374 lpfc_bsg_sli_cfg_read_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3375 			      enum nemb_type nemb_tp,
3376 			      struct lpfc_dmabuf *dmabuf)
3377 {
3378 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3379 	struct dfc_mbox_req *mbox_req;
3380 	struct lpfc_dmabuf *curr_dmabuf, *next_dmabuf;
3381 	uint32_t ext_buf_cnt, ext_buf_index;
3382 	struct lpfc_dmabuf *ext_dmabuf = NULL;
3383 	struct bsg_job_data *dd_data = NULL;
3384 	LPFC_MBOXQ_t *pmboxq = NULL;
3385 	MAILBOX_t *pmb;
3386 	uint8_t *pmbx;
3387 	int rc, i;
3388 
3389 	mbox_req =
3390 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3391 
3392 	/* pointer to the start of mailbox command */
3393 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3394 
3395 	if (nemb_tp == nemb_mse) {
3396 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3397 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3398 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3399 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3400 					"2945 Handled SLI_CONFIG(mse) rd, "
3401 					"ext_buf_cnt(%d) out of range(%d)\n",
3402 					ext_buf_cnt,
3403 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
3404 			rc = -ERANGE;
3405 			goto job_error;
3406 		}
3407 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3408 				"2941 Handled SLI_CONFIG(mse) rd, "
3409 				"ext_buf_cnt:%d\n", ext_buf_cnt);
3410 	} else {
3411 		/* sanity check on interface type for support */
3412 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3413 		    LPFC_SLI_INTF_IF_TYPE_2) {
3414 			rc = -ENODEV;
3415 			goto job_error;
3416 		}
3417 		/* nemb_tp == nemb_hbd */
3418 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3419 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3420 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3421 					"2946 Handled SLI_CONFIG(hbd) rd, "
3422 					"ext_buf_cnt(%d) out of range(%d)\n",
3423 					ext_buf_cnt,
3424 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
3425 			rc = -ERANGE;
3426 			goto job_error;
3427 		}
3428 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3429 				"2942 Handled SLI_CONFIG(hbd) rd, "
3430 				"ext_buf_cnt:%d\n", ext_buf_cnt);
3431 	}
3432 
3433 	/* reject non-embedded mailbox command with none external buffer */
3434 	if (ext_buf_cnt == 0) {
3435 		rc = -EPERM;
3436 		goto job_error;
3437 	} else if (ext_buf_cnt > 1) {
3438 		/* additional external read buffers */
3439 		for (i = 1; i < ext_buf_cnt; i++) {
3440 			ext_dmabuf = lpfc_bsg_dma_page_alloc(phba);
3441 			if (!ext_dmabuf) {
3442 				rc = -ENOMEM;
3443 				goto job_error;
3444 			}
3445 			list_add_tail(&ext_dmabuf->list,
3446 				      &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3447 		}
3448 	}
3449 
3450 	/* bsg tracking structure */
3451 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3452 	if (!dd_data) {
3453 		rc = -ENOMEM;
3454 		goto job_error;
3455 	}
3456 
3457 	/* mailbox command structure for base driver */
3458 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3459 	if (!pmboxq) {
3460 		rc = -ENOMEM;
3461 		goto job_error;
3462 	}
3463 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3464 
3465 	/* for the first external buffer */
3466 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3467 
3468 	/* for the rest of external buffer descriptors if any */
3469 	if (ext_buf_cnt > 1) {
3470 		ext_buf_index = 1;
3471 		list_for_each_entry_safe(curr_dmabuf, next_dmabuf,
3472 				&phba->mbox_ext_buf_ctx.ext_dmabuf_list, list) {
3473 			lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp,
3474 						ext_buf_index, dmabuf,
3475 						curr_dmabuf);
3476 			ext_buf_index++;
3477 		}
3478 	}
3479 
3480 	/* construct base driver mbox command */
3481 	pmb = &pmboxq->u.mb;
3482 	pmbx = (uint8_t *)dmabuf->virt;
3483 	memcpy(pmb, pmbx, sizeof(*pmb));
3484 	pmb->mbxOwner = OWN_HOST;
3485 	pmboxq->vport = phba->pport;
3486 
3487 	/* multi-buffer handling context */
3488 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3489 	phba->mbox_ext_buf_ctx.mboxType = mbox_rd;
3490 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3491 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3492 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3493 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3494 
3495 	/* callback for multi-buffer read mailbox command */
3496 	pmboxq->mbox_cmpl = lpfc_bsg_issue_read_mbox_ext_cmpl;
3497 
3498 	/* context fields to callback function */
3499 	pmboxq->context1 = dd_data;
3500 	dd_data->type = TYPE_MBOX;
3501 	dd_data->context_un.mbox.pmboxq = pmboxq;
3502 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
3503 	dd_data->context_un.mbox.set_job = job;
3504 	job->dd_data = dd_data;
3505 
3506 	/* state change */
3507 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3508 
3509 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3510 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3511 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3512 				"2947 Issued SLI_CONFIG ext-buffer "
3513 				"maibox command, rc:x%x\n", rc);
3514 		return 1;
3515 	}
3516 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3517 			"2948 Failed to issue SLI_CONFIG ext-buffer "
3518 			"maibox command, rc:x%x\n", rc);
3519 	rc = -EPIPE;
3520 
3521 job_error:
3522 	if (pmboxq)
3523 		mempool_free(pmboxq, phba->mbox_mem_pool);
3524 	lpfc_bsg_dma_page_list_free(phba,
3525 				    &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3526 	kfree(dd_data);
3527 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_IDLE;
3528 	return rc;
3529 }
3530 
3531 /**
3532  * lpfc_bsg_sli_cfg_write_cmd_ext - sli_config non-embedded mailbox cmd write
3533  * @phba: Pointer to HBA context object.
3534  * @mb: Pointer to a BSG mailbox object.
3535  * @dmabuff: Pointer to a DMA buffer descriptor.
3536  *
3537  * This routine performs SLI_CONFIG (0x9B) write mailbox command operation with
3538  * non-embedded external bufffers.
3539  **/
3540 static int
3541 lpfc_bsg_sli_cfg_write_cmd_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
3542 			       enum nemb_type nemb_tp,
3543 			       struct lpfc_dmabuf *dmabuf)
3544 {
3545 	struct dfc_mbox_req *mbox_req;
3546 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3547 	uint32_t ext_buf_cnt;
3548 	struct bsg_job_data *dd_data = NULL;
3549 	LPFC_MBOXQ_t *pmboxq = NULL;
3550 	MAILBOX_t *pmb;
3551 	uint8_t *mbx;
3552 	int rc = 0, i;
3553 
3554 	mbox_req =
3555 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
3556 
3557 	/* pointer to the start of mailbox command */
3558 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3559 
3560 	if (nemb_tp == nemb_mse) {
3561 		ext_buf_cnt = bsg_bf_get(lpfc_mbox_hdr_mse_cnt,
3562 			&sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr);
3563 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_MSE) {
3564 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3565 					"2953 Handled SLI_CONFIG(mse) wr, "
3566 					"ext_buf_cnt(%d) out of range(%d)\n",
3567 					ext_buf_cnt,
3568 					LPFC_MBX_SLI_CONFIG_MAX_MSE);
3569 			return -ERANGE;
3570 		}
3571 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3572 				"2949 Handled SLI_CONFIG(mse) wr, "
3573 				"ext_buf_cnt:%d\n", ext_buf_cnt);
3574 	} else {
3575 		/* sanity check on interface type for support */
3576 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3577 		    LPFC_SLI_INTF_IF_TYPE_2)
3578 			return -ENODEV;
3579 		/* nemb_tp == nemb_hbd */
3580 		ext_buf_cnt = sli_cfg_mbx->un.sli_config_emb1_subsys.hbd_count;
3581 		if (ext_buf_cnt > LPFC_MBX_SLI_CONFIG_MAX_HBD) {
3582 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3583 					"2954 Handled SLI_CONFIG(hbd) wr, "
3584 					"ext_buf_cnt(%d) out of range(%d)\n",
3585 					ext_buf_cnt,
3586 					LPFC_MBX_SLI_CONFIG_MAX_HBD);
3587 			return -ERANGE;
3588 		}
3589 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3590 				"2950 Handled SLI_CONFIG(hbd) wr, "
3591 				"ext_buf_cnt:%d\n", ext_buf_cnt);
3592 	}
3593 
3594 	if (ext_buf_cnt == 0)
3595 		return -EPERM;
3596 
3597 	/* for the first external buffer */
3598 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, 0, dmabuf, dmabuf);
3599 
3600 	/* log for looking forward */
3601 	for (i = 1; i < ext_buf_cnt; i++) {
3602 		if (nemb_tp == nemb_mse)
3603 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3604 				"2951 SLI_CONFIG(mse), buf[%d]-length:%d\n",
3605 				i, sli_cfg_mbx->un.sli_config_emb0_subsys.
3606 				mse[i].buf_len);
3607 		else
3608 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3609 				"2952 SLI_CONFIG(hbd), buf[%d]-length:%d\n",
3610 				i, bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3611 				&sli_cfg_mbx->un.sli_config_emb1_subsys.
3612 				hbd[i]));
3613 	}
3614 
3615 	/* multi-buffer handling context */
3616 	phba->mbox_ext_buf_ctx.nembType = nemb_tp;
3617 	phba->mbox_ext_buf_ctx.mboxType = mbox_wr;
3618 	phba->mbox_ext_buf_ctx.numBuf = ext_buf_cnt;
3619 	phba->mbox_ext_buf_ctx.mbxTag = mbox_req->extMboxTag;
3620 	phba->mbox_ext_buf_ctx.seqNum = mbox_req->extSeqNum;
3621 	phba->mbox_ext_buf_ctx.mbx_dmabuf = dmabuf;
3622 
3623 	if (ext_buf_cnt == 1) {
3624 		/* bsg tracking structure */
3625 		dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3626 		if (!dd_data) {
3627 			rc = -ENOMEM;
3628 			goto job_error;
3629 		}
3630 
3631 		/* mailbox command structure for base driver */
3632 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3633 		if (!pmboxq) {
3634 			rc = -ENOMEM;
3635 			goto job_error;
3636 		}
3637 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3638 		pmb = &pmboxq->u.mb;
3639 		mbx = (uint8_t *)dmabuf->virt;
3640 		memcpy(pmb, mbx, sizeof(*pmb));
3641 		pmb->mbxOwner = OWN_HOST;
3642 		pmboxq->vport = phba->pport;
3643 
3644 		/* callback for multi-buffer read mailbox command */
3645 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
3646 
3647 		/* context fields to callback function */
3648 		pmboxq->context1 = dd_data;
3649 		dd_data->type = TYPE_MBOX;
3650 		dd_data->context_un.mbox.pmboxq = pmboxq;
3651 		dd_data->context_un.mbox.mb = (MAILBOX_t *)mbx;
3652 		dd_data->context_un.mbox.set_job = job;
3653 		job->dd_data = dd_data;
3654 
3655 		/* state change */
3656 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3657 
3658 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3659 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3660 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3661 					"2955 Issued SLI_CONFIG ext-buffer "
3662 					"maibox command, rc:x%x\n", rc);
3663 			return 1;
3664 		}
3665 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3666 				"2956 Failed to issue SLI_CONFIG ext-buffer "
3667 				"maibox command, rc:x%x\n", rc);
3668 		rc = -EPIPE;
3669 	}
3670 
3671 job_error:
3672 	if (pmboxq)
3673 		mempool_free(pmboxq, phba->mbox_mem_pool);
3674 	kfree(dd_data);
3675 
3676 	return rc;
3677 }
3678 
3679 /**
3680  * lpfc_bsg_handle_sli_cfg_mbox - handle sli-cfg mailbox cmd with ext buffer
3681  * @phba: Pointer to HBA context object.
3682  * @mb: Pointer to a BSG mailbox object.
3683  * @dmabuff: Pointer to a DMA buffer descriptor.
3684  *
3685  * This routine handles SLI_CONFIG (0x9B) mailbox command with non-embedded
3686  * external bufffers, including both 0x9B with non-embedded MSEs and 0x9B
3687  * with embedded sussystem 0x1 and opcodes with external HBDs.
3688  **/
3689 static int
3690 lpfc_bsg_handle_sli_cfg_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
3691 			     struct lpfc_dmabuf *dmabuf)
3692 {
3693 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3694 	uint32_t subsys;
3695 	uint32_t opcode;
3696 	int rc = SLI_CONFIG_NOT_HANDLED;
3697 
3698 	/* state change */
3699 	phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_HOST;
3700 
3701 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)dmabuf->virt;
3702 
3703 	if (!bsg_bf_get(lpfc_mbox_hdr_emb,
3704 	    &sli_cfg_mbx->un.sli_config_emb0_subsys.sli_config_hdr)) {
3705 		subsys = bsg_bf_get(lpfc_emb0_subcmnd_subsys,
3706 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
3707 		opcode = bsg_bf_get(lpfc_emb0_subcmnd_opcode,
3708 				    &sli_cfg_mbx->un.sli_config_emb0_subsys);
3709 		if (subsys == SLI_CONFIG_SUBSYS_FCOE) {
3710 			switch (opcode) {
3711 			case FCOE_OPCODE_READ_FCF:
3712 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3713 						"2957 Handled SLI_CONFIG "
3714 						"subsys_fcoe, opcode:x%x\n",
3715 						opcode);
3716 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3717 							nemb_mse, dmabuf);
3718 				break;
3719 			case FCOE_OPCODE_ADD_FCF:
3720 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3721 						"2958 Handled SLI_CONFIG "
3722 						"subsys_fcoe, opcode:x%x\n",
3723 						opcode);
3724 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
3725 							nemb_mse, dmabuf);
3726 				break;
3727 			default:
3728 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3729 						"2959 Not handled SLI_CONFIG "
3730 						"subsys_fcoe, opcode:x%x\n",
3731 						opcode);
3732 				rc = SLI_CONFIG_NOT_HANDLED;
3733 				break;
3734 			}
3735 		} else {
3736 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3737 					"2977 Handled SLI_CONFIG "
3738 					"subsys:x%d, opcode:x%x\n",
3739 					subsys, opcode);
3740 			rc = SLI_CONFIG_NOT_HANDLED;
3741 		}
3742 	} else {
3743 		subsys = bsg_bf_get(lpfc_emb1_subcmnd_subsys,
3744 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
3745 		opcode = bsg_bf_get(lpfc_emb1_subcmnd_opcode,
3746 				    &sli_cfg_mbx->un.sli_config_emb1_subsys);
3747 		if (subsys == SLI_CONFIG_SUBSYS_COMN) {
3748 			switch (opcode) {
3749 			case COMN_OPCODE_READ_OBJECT:
3750 			case COMN_OPCODE_READ_OBJECT_LIST:
3751 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3752 						"2960 Handled SLI_CONFIG "
3753 						"subsys_comn, opcode:x%x\n",
3754 						opcode);
3755 				rc = lpfc_bsg_sli_cfg_read_cmd_ext(phba, job,
3756 							nemb_hbd, dmabuf);
3757 				break;
3758 			case COMN_OPCODE_WRITE_OBJECT:
3759 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3760 						"2961 Handled SLI_CONFIG "
3761 						"subsys_comn, opcode:x%x\n",
3762 						opcode);
3763 				rc = lpfc_bsg_sli_cfg_write_cmd_ext(phba, job,
3764 							nemb_hbd, dmabuf);
3765 				break;
3766 			default:
3767 				lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3768 						"2962 Not handled SLI_CONFIG "
3769 						"subsys_comn, opcode:x%x\n",
3770 						opcode);
3771 				rc = SLI_CONFIG_NOT_HANDLED;
3772 				break;
3773 			}
3774 		} else {
3775 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3776 					"2978 Handled SLI_CONFIG "
3777 					"subsys:x%d, opcode:x%x\n",
3778 					subsys, opcode);
3779 			rc = SLI_CONFIG_NOT_HANDLED;
3780 		}
3781 	}
3782 	return rc;
3783 }
3784 
3785 /**
3786  * lpfc_bsg_mbox_ext_abort_req - request to abort mbox command with ext buffers
3787  * @phba: Pointer to HBA context object.
3788  *
3789  * This routine is for requesting to abort a pass-through mailbox command with
3790  * multiple external buffers due to error condition.
3791  **/
3792 static void
3793 lpfc_bsg_mbox_ext_abort(struct lpfc_hba *phba)
3794 {
3795 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
3796 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
3797 	else
3798 		lpfc_bsg_mbox_ext_session_reset(phba);
3799 	return;
3800 }
3801 
3802 /**
3803  * lpfc_bsg_read_ebuf_get - get the next mailbox read external buffer
3804  * @phba: Pointer to HBA context object.
3805  * @dmabuf: Pointer to a DMA buffer descriptor.
3806  *
3807  * This routine extracts the next mailbox read external buffer back to
3808  * user space through BSG.
3809  **/
3810 static int
3811 lpfc_bsg_read_ebuf_get(struct lpfc_hba *phba, struct fc_bsg_job *job)
3812 {
3813 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3814 	struct lpfc_dmabuf *dmabuf;
3815 	uint8_t *pbuf;
3816 	uint32_t size;
3817 	uint32_t index;
3818 
3819 	index = phba->mbox_ext_buf_ctx.seqNum;
3820 	phba->mbox_ext_buf_ctx.seqNum++;
3821 
3822 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
3823 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3824 
3825 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
3826 		size = bsg_bf_get(lpfc_mbox_sli_config_mse_len,
3827 			&sli_cfg_mbx->un.sli_config_emb0_subsys.mse[index]);
3828 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3829 				"2963 SLI_CONFIG (mse) ext-buffer rd get "
3830 				"buffer[%d], size:%d\n", index, size);
3831 	} else {
3832 		size = bsg_bf_get(lpfc_mbox_sli_config_ecmn_hbd_len,
3833 			&sli_cfg_mbx->un.sli_config_emb1_subsys.hbd[index]);
3834 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3835 				"2964 SLI_CONFIG (hbd) ext-buffer rd get "
3836 				"buffer[%d], size:%d\n", index, size);
3837 	}
3838 	if (list_empty(&phba->mbox_ext_buf_ctx.ext_dmabuf_list))
3839 		return -EPIPE;
3840 	dmabuf = list_first_entry(&phba->mbox_ext_buf_ctx.ext_dmabuf_list,
3841 				  struct lpfc_dmabuf, list);
3842 	list_del_init(&dmabuf->list);
3843 	pbuf = (uint8_t *)dmabuf->virt;
3844 	job->reply->reply_payload_rcv_len =
3845 		sg_copy_from_buffer(job->reply_payload.sg_list,
3846 				    job->reply_payload.sg_cnt,
3847 				    pbuf, size);
3848 
3849 	lpfc_bsg_dma_page_free(phba, dmabuf);
3850 
3851 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
3852 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3853 				"2965 SLI_CONFIG (hbd) ext-buffer rd mbox "
3854 				"command session done\n");
3855 		lpfc_bsg_mbox_ext_session_reset(phba);
3856 	}
3857 
3858 	job->reply->result = 0;
3859 	job->job_done(job);
3860 
3861 	return SLI_CONFIG_HANDLED;
3862 }
3863 
3864 /**
3865  * lpfc_bsg_write_ebuf_set - set the next mailbox write external buffer
3866  * @phba: Pointer to HBA context object.
3867  * @dmabuf: Pointer to a DMA buffer descriptor.
3868  *
3869  * This routine sets up the next mailbox read external buffer obtained
3870  * from user space through BSG.
3871  **/
3872 static int
3873 lpfc_bsg_write_ebuf_set(struct lpfc_hba *phba, struct fc_bsg_job *job,
3874 			struct lpfc_dmabuf *dmabuf)
3875 {
3876 	struct lpfc_sli_config_mbox *sli_cfg_mbx;
3877 	struct bsg_job_data *dd_data = NULL;
3878 	LPFC_MBOXQ_t *pmboxq = NULL;
3879 	MAILBOX_t *pmb;
3880 	enum nemb_type nemb_tp;
3881 	uint8_t *pbuf;
3882 	uint32_t size;
3883 	uint32_t index;
3884 	int rc;
3885 
3886 	index = phba->mbox_ext_buf_ctx.seqNum;
3887 	phba->mbox_ext_buf_ctx.seqNum++;
3888 	nemb_tp = phba->mbox_ext_buf_ctx.nembType;
3889 
3890 	sli_cfg_mbx = (struct lpfc_sli_config_mbox *)
3891 			phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3892 
3893 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
3894 	if (!dd_data) {
3895 		rc = -ENOMEM;
3896 		goto job_error;
3897 	}
3898 
3899 	pbuf = (uint8_t *)dmabuf->virt;
3900 	size = job->request_payload.payload_len;
3901 	sg_copy_to_buffer(job->request_payload.sg_list,
3902 			  job->request_payload.sg_cnt,
3903 			  pbuf, size);
3904 
3905 	if (phba->mbox_ext_buf_ctx.nembType == nemb_mse) {
3906 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3907 				"2966 SLI_CONFIG (mse) ext-buffer wr set "
3908 				"buffer[%d], size:%d\n",
3909 				phba->mbox_ext_buf_ctx.seqNum, size);
3910 
3911 	} else {
3912 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3913 				"2967 SLI_CONFIG (hbd) ext-buffer wr set "
3914 				"buffer[%d], size:%d\n",
3915 				phba->mbox_ext_buf_ctx.seqNum, size);
3916 
3917 	}
3918 
3919 	/* set up external buffer descriptor and add to external buffer list */
3920 	lpfc_bsg_sli_cfg_dma_desc_setup(phba, nemb_tp, index,
3921 					phba->mbox_ext_buf_ctx.mbx_dmabuf,
3922 					dmabuf);
3923 	list_add_tail(&dmabuf->list, &phba->mbox_ext_buf_ctx.ext_dmabuf_list);
3924 
3925 	if (phba->mbox_ext_buf_ctx.seqNum == phba->mbox_ext_buf_ctx.numBuf) {
3926 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3927 				"2968 SLI_CONFIG ext-buffer wr all %d "
3928 				"ebuffers received\n",
3929 				phba->mbox_ext_buf_ctx.numBuf);
3930 		/* mailbox command structure for base driver */
3931 		pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3932 		if (!pmboxq) {
3933 			rc = -ENOMEM;
3934 			goto job_error;
3935 		}
3936 		memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
3937 		pbuf = (uint8_t *)phba->mbox_ext_buf_ctx.mbx_dmabuf->virt;
3938 		pmb = &pmboxq->u.mb;
3939 		memcpy(pmb, pbuf, sizeof(*pmb));
3940 		pmb->mbxOwner = OWN_HOST;
3941 		pmboxq->vport = phba->pport;
3942 
3943 		/* callback for multi-buffer write mailbox command */
3944 		pmboxq->mbox_cmpl = lpfc_bsg_issue_write_mbox_ext_cmpl;
3945 
3946 		/* context fields to callback function */
3947 		pmboxq->context1 = dd_data;
3948 		dd_data->type = TYPE_MBOX;
3949 		dd_data->context_un.mbox.pmboxq = pmboxq;
3950 		dd_data->context_un.mbox.mb = (MAILBOX_t *)pbuf;
3951 		dd_data->context_un.mbox.set_job = job;
3952 		job->dd_data = dd_data;
3953 
3954 		/* state change */
3955 		phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_PORT;
3956 
3957 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3958 		if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY)) {
3959 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3960 					"2969 Issued SLI_CONFIG ext-buffer "
3961 					"maibox command, rc:x%x\n", rc);
3962 			return 1;
3963 		}
3964 		lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
3965 				"2970 Failed to issue SLI_CONFIG ext-buffer "
3966 				"maibox command, rc:x%x\n", rc);
3967 		rc = -EPIPE;
3968 		goto job_error;
3969 	}
3970 
3971 	/* wait for additoinal external buffers */
3972 	job->reply->result = 0;
3973 	job->job_done(job);
3974 	return SLI_CONFIG_HANDLED;
3975 
3976 job_error:
3977 	lpfc_bsg_dma_page_free(phba, dmabuf);
3978 	kfree(dd_data);
3979 
3980 	return rc;
3981 }
3982 
3983 /**
3984  * lpfc_bsg_handle_sli_cfg_ebuf - handle ext buffer with sli-cfg mailbox cmd
3985  * @phba: Pointer to HBA context object.
3986  * @mb: Pointer to a BSG mailbox object.
3987  * @dmabuff: Pointer to a DMA buffer descriptor.
3988  *
3989  * This routine handles the external buffer with SLI_CONFIG (0x9B) mailbox
3990  * command with multiple non-embedded external buffers.
3991  **/
3992 static int
3993 lpfc_bsg_handle_sli_cfg_ebuf(struct lpfc_hba *phba, struct fc_bsg_job *job,
3994 			     struct lpfc_dmabuf *dmabuf)
3995 {
3996 	int rc;
3997 
3998 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
3999 			"2971 SLI_CONFIG buffer (type:x%x)\n",
4000 			phba->mbox_ext_buf_ctx.mboxType);
4001 
4002 	if (phba->mbox_ext_buf_ctx.mboxType == mbox_rd) {
4003 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_DONE) {
4004 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4005 					"2972 SLI_CONFIG rd buffer state "
4006 					"mismatch:x%x\n",
4007 					phba->mbox_ext_buf_ctx.state);
4008 			lpfc_bsg_mbox_ext_abort(phba);
4009 			return -EPIPE;
4010 		}
4011 		rc = lpfc_bsg_read_ebuf_get(phba, job);
4012 		if (rc == SLI_CONFIG_HANDLED)
4013 			lpfc_bsg_dma_page_free(phba, dmabuf);
4014 	} else { /* phba->mbox_ext_buf_ctx.mboxType == mbox_wr */
4015 		if (phba->mbox_ext_buf_ctx.state != LPFC_BSG_MBOX_HOST) {
4016 			lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4017 					"2973 SLI_CONFIG wr buffer state "
4018 					"mismatch:x%x\n",
4019 					phba->mbox_ext_buf_ctx.state);
4020 			lpfc_bsg_mbox_ext_abort(phba);
4021 			return -EPIPE;
4022 		}
4023 		rc = lpfc_bsg_write_ebuf_set(phba, job, dmabuf);
4024 	}
4025 	return rc;
4026 }
4027 
4028 /**
4029  * lpfc_bsg_handle_sli_cfg_ext - handle sli-cfg mailbox with external buffer
4030  * @phba: Pointer to HBA context object.
4031  * @mb: Pointer to a BSG mailbox object.
4032  * @dmabuff: Pointer to a DMA buffer descriptor.
4033  *
4034  * This routine checkes and handles non-embedded multi-buffer SLI_CONFIG
4035  * (0x9B) mailbox commands and external buffers.
4036  **/
4037 static int
4038 lpfc_bsg_handle_sli_cfg_ext(struct lpfc_hba *phba, struct fc_bsg_job *job,
4039 			    struct lpfc_dmabuf *dmabuf)
4040 {
4041 	struct dfc_mbox_req *mbox_req;
4042 	int rc;
4043 
4044 	mbox_req =
4045 	   (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4046 
4047 	/* mbox command with/without single external buffer */
4048 	if (mbox_req->extMboxTag == 0 && mbox_req->extSeqNum == 0)
4049 		return SLI_CONFIG_NOT_HANDLED;
4050 
4051 	/* mbox command and first external buffer */
4052 	if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_IDLE) {
4053 		if (mbox_req->extSeqNum == 1) {
4054 			lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4055 					"2974 SLI_CONFIG mailbox: tag:%d, "
4056 					"seq:%d\n", mbox_req->extMboxTag,
4057 					mbox_req->extSeqNum);
4058 			rc = lpfc_bsg_handle_sli_cfg_mbox(phba, job, dmabuf);
4059 			return rc;
4060 		} else
4061 			goto sli_cfg_ext_error;
4062 	}
4063 
4064 	/*
4065 	 * handle additional external buffers
4066 	 */
4067 
4068 	/* check broken pipe conditions */
4069 	if (mbox_req->extMboxTag != phba->mbox_ext_buf_ctx.mbxTag)
4070 		goto sli_cfg_ext_error;
4071 	if (mbox_req->extSeqNum > phba->mbox_ext_buf_ctx.numBuf)
4072 		goto sli_cfg_ext_error;
4073 	if (mbox_req->extSeqNum != phba->mbox_ext_buf_ctx.seqNum + 1)
4074 		goto sli_cfg_ext_error;
4075 
4076 	lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4077 			"2975 SLI_CONFIG mailbox external buffer: "
4078 			"extSta:x%x, tag:%d, seq:%d\n",
4079 			phba->mbox_ext_buf_ctx.state, mbox_req->extMboxTag,
4080 			mbox_req->extSeqNum);
4081 	rc = lpfc_bsg_handle_sli_cfg_ebuf(phba, job, dmabuf);
4082 	return rc;
4083 
4084 sli_cfg_ext_error:
4085 	/* all other cases, broken pipe */
4086 	lpfc_printf_log(phba, KERN_ERR, LOG_LIBDFC,
4087 			"2976 SLI_CONFIG mailbox broken pipe: "
4088 			"ctxSta:x%x, ctxNumBuf:%d "
4089 			"ctxTag:%d, ctxSeq:%d, tag:%d, seq:%d\n",
4090 			phba->mbox_ext_buf_ctx.state,
4091 			phba->mbox_ext_buf_ctx.numBuf,
4092 			phba->mbox_ext_buf_ctx.mbxTag,
4093 			phba->mbox_ext_buf_ctx.seqNum,
4094 			mbox_req->extMboxTag, mbox_req->extSeqNum);
4095 
4096 	lpfc_bsg_mbox_ext_session_reset(phba);
4097 
4098 	return -EPIPE;
4099 }
4100 
4101 /**
4102  * lpfc_bsg_issue_mbox - issues a mailbox command on behalf of an app
4103  * @phba: Pointer to HBA context object.
4104  * @mb: Pointer to a mailbox object.
4105  * @vport: Pointer to a vport object.
4106  *
4107  * Allocate a tracking object, mailbox command memory, get a mailbox
4108  * from the mailbox pool, copy the caller mailbox command.
4109  *
4110  * If offline and the sli is active we need to poll for the command (port is
4111  * being reset) and com-plete the job, otherwise issue the mailbox command and
4112  * let our completion handler finish the command.
4113  **/
4114 static uint32_t
4115 lpfc_bsg_issue_mbox(struct lpfc_hba *phba, struct fc_bsg_job *job,
4116 	struct lpfc_vport *vport)
4117 {
4118 	LPFC_MBOXQ_t *pmboxq = NULL; /* internal mailbox queue */
4119 	MAILBOX_t *pmb; /* shortcut to the pmboxq mailbox */
4120 	/* a 4k buffer to hold the mb and extended data from/to the bsg */
4121 	uint8_t *pmbx = NULL;
4122 	struct bsg_job_data *dd_data = NULL; /* bsg data tracking structure */
4123 	struct lpfc_dmabuf *dmabuf = NULL;
4124 	struct dfc_mbox_req *mbox_req;
4125 	struct READ_EVENT_LOG_VAR *rdEventLog;
4126 	uint32_t transmit_length, receive_length, mode;
4127 	struct lpfc_mbx_sli4_config *sli4_config;
4128 	struct lpfc_mbx_nembed_cmd *nembed_sge;
4129 	struct mbox_header *header;
4130 	struct ulp_bde64 *bde;
4131 	uint8_t *ext = NULL;
4132 	int rc = 0;
4133 	uint8_t *from;
4134 	uint32_t size;
4135 
4136 
4137 	/* in case no data is transferred */
4138 	job->reply->reply_payload_rcv_len = 0;
4139 
4140 	/* sanity check to protect driver */
4141 	if (job->reply_payload.payload_len > BSG_MBOX_SIZE ||
4142 	    job->request_payload.payload_len > BSG_MBOX_SIZE) {
4143 		rc = -ERANGE;
4144 		goto job_done;
4145 	}
4146 
4147 	/*
4148 	 * Don't allow mailbox commands to be sent when blocked or when in
4149 	 * the middle of discovery
4150 	 */
4151 	 if (phba->sli.sli_flag & LPFC_BLOCK_MGMT_IO) {
4152 		rc = -EAGAIN;
4153 		goto job_done;
4154 	}
4155 
4156 	mbox_req =
4157 	    (struct dfc_mbox_req *)job->request->rqst_data.h_vendor.vendor_cmd;
4158 
4159 	/* check if requested extended data lengths are valid */
4160 	if ((mbox_req->inExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t)) ||
4161 	    (mbox_req->outExtWLen > BSG_MBOX_SIZE/sizeof(uint32_t))) {
4162 		rc = -ERANGE;
4163 		goto job_done;
4164 	}
4165 
4166 	dmabuf = lpfc_bsg_dma_page_alloc(phba);
4167 	if (!dmabuf || !dmabuf->virt) {
4168 		rc = -ENOMEM;
4169 		goto job_done;
4170 	}
4171 
4172 	/* Get the mailbox command or external buffer from BSG */
4173 	pmbx = (uint8_t *)dmabuf->virt;
4174 	size = job->request_payload.payload_len;
4175 	sg_copy_to_buffer(job->request_payload.sg_list,
4176 			  job->request_payload.sg_cnt, pmbx, size);
4177 
4178 	/* Handle possible SLI_CONFIG with non-embedded payloads */
4179 	if (phba->sli_rev == LPFC_SLI_REV4) {
4180 		rc = lpfc_bsg_handle_sli_cfg_ext(phba, job, dmabuf);
4181 		if (rc == SLI_CONFIG_HANDLED)
4182 			goto job_cont;
4183 		if (rc)
4184 			goto job_done;
4185 		/* SLI_CONFIG_NOT_HANDLED for other mailbox commands */
4186 	}
4187 
4188 	rc = lpfc_bsg_check_cmd_access(phba, (MAILBOX_t *)pmbx, vport);
4189 	if (rc != 0)
4190 		goto job_done; /* must be negative */
4191 
4192 	/* allocate our bsg tracking structure */
4193 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4194 	if (!dd_data) {
4195 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4196 				"2727 Failed allocation of dd_data\n");
4197 		rc = -ENOMEM;
4198 		goto job_done;
4199 	}
4200 
4201 	pmboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4202 	if (!pmboxq) {
4203 		rc = -ENOMEM;
4204 		goto job_done;
4205 	}
4206 	memset(pmboxq, 0, sizeof(LPFC_MBOXQ_t));
4207 
4208 	pmb = &pmboxq->u.mb;
4209 	memcpy(pmb, pmbx, sizeof(*pmb));
4210 	pmb->mbxOwner = OWN_HOST;
4211 	pmboxq->vport = vport;
4212 
4213 	/* If HBA encountered an error attention, allow only DUMP
4214 	 * or RESTART mailbox commands until the HBA is restarted.
4215 	 */
4216 	if (phba->pport->stopped &&
4217 	    pmb->mbxCommand != MBX_DUMP_MEMORY &&
4218 	    pmb->mbxCommand != MBX_RESTART &&
4219 	    pmb->mbxCommand != MBX_WRITE_VPARMS &&
4220 	    pmb->mbxCommand != MBX_WRITE_WWN)
4221 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
4222 				"2797 mbox: Issued mailbox cmd "
4223 				"0x%x while in stopped state.\n",
4224 				pmb->mbxCommand);
4225 
4226 	/* extended mailbox commands will need an extended buffer */
4227 	if (mbox_req->inExtWLen || mbox_req->outExtWLen) {
4228 		/* any data for the device? */
4229 		if (mbox_req->inExtWLen) {
4230 			from = pmbx;
4231 			ext = from + sizeof(MAILBOX_t);
4232 		}
4233 		pmboxq->context2 = ext;
4234 		pmboxq->in_ext_byte_len =
4235 			mbox_req->inExtWLen * sizeof(uint32_t);
4236 		pmboxq->out_ext_byte_len =
4237 			mbox_req->outExtWLen * sizeof(uint32_t);
4238 		pmboxq->mbox_offset_word = mbox_req->mbOffset;
4239 	}
4240 
4241 	/* biu diag will need a kernel buffer to transfer the data
4242 	 * allocate our own buffer and setup the mailbox command to
4243 	 * use ours
4244 	 */
4245 	if (pmb->mbxCommand == MBX_RUN_BIU_DIAG64) {
4246 		transmit_length = pmb->un.varWords[1];
4247 		receive_length = pmb->un.varWords[4];
4248 		/* transmit length cannot be greater than receive length or
4249 		 * mailbox extension size
4250 		 */
4251 		if ((transmit_length > receive_length) ||
4252 			(transmit_length > MAILBOX_EXT_SIZE)) {
4253 			rc = -ERANGE;
4254 			goto job_done;
4255 		}
4256 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrHigh =
4257 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t));
4258 		pmb->un.varBIUdiag.un.s2.xmit_bde64.addrLow =
4259 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t));
4260 
4261 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrHigh =
4262 			putPaddrHigh(dmabuf->phys + sizeof(MAILBOX_t)
4263 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4264 		pmb->un.varBIUdiag.un.s2.rcv_bde64.addrLow =
4265 			putPaddrLow(dmabuf->phys + sizeof(MAILBOX_t)
4266 			  + pmb->un.varBIUdiag.un.s2.xmit_bde64.tus.f.bdeSize);
4267 	} else if (pmb->mbxCommand == MBX_READ_EVENT_LOG) {
4268 		rdEventLog = &pmb->un.varRdEventLog;
4269 		receive_length = rdEventLog->rcv_bde64.tus.f.bdeSize;
4270 		mode = bf_get(lpfc_event_log, rdEventLog);
4271 
4272 		/* receive length cannot be greater than mailbox
4273 		 * extension size
4274 		 */
4275 		if (receive_length > MAILBOX_EXT_SIZE) {
4276 			rc = -ERANGE;
4277 			goto job_done;
4278 		}
4279 
4280 		/* mode zero uses a bde like biu diags command */
4281 		if (mode == 0) {
4282 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4283 							+ sizeof(MAILBOX_t));
4284 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4285 							+ sizeof(MAILBOX_t));
4286 		}
4287 	} else if (phba->sli_rev == LPFC_SLI_REV4) {
4288 		if (pmb->mbxCommand == MBX_DUMP_MEMORY) {
4289 			/* rebuild the command for sli4 using our own buffers
4290 			* like we do for biu diags
4291 			*/
4292 			receive_length = pmb->un.varWords[2];
4293 			/* receive length cannot be greater than mailbox
4294 			 * extension size
4295 			 */
4296 			if (receive_length == 0) {
4297 				rc = -ERANGE;
4298 				goto job_done;
4299 			}
4300 			pmb->un.varWords[3] = putPaddrLow(dmabuf->phys
4301 						+ sizeof(MAILBOX_t));
4302 			pmb->un.varWords[4] = putPaddrHigh(dmabuf->phys
4303 						+ sizeof(MAILBOX_t));
4304 		} else if ((pmb->mbxCommand == MBX_UPDATE_CFG) &&
4305 			pmb->un.varUpdateCfg.co) {
4306 			bde = (struct ulp_bde64 *)&pmb->un.varWords[4];
4307 
4308 			/* bde size cannot be greater than mailbox ext size */
4309 			if (bde->tus.f.bdeSize > MAILBOX_EXT_SIZE) {
4310 				rc = -ERANGE;
4311 				goto job_done;
4312 			}
4313 			bde->addrHigh = putPaddrHigh(dmabuf->phys
4314 						+ sizeof(MAILBOX_t));
4315 			bde->addrLow = putPaddrLow(dmabuf->phys
4316 						+ sizeof(MAILBOX_t));
4317 		} else if (pmb->mbxCommand == MBX_SLI4_CONFIG) {
4318 			/* Handling non-embedded SLI_CONFIG mailbox command */
4319 			sli4_config = &pmboxq->u.mqe.un.sli4_config;
4320 			if (!bf_get(lpfc_mbox_hdr_emb,
4321 			    &sli4_config->header.cfg_mhdr)) {
4322 				/* rebuild the command for sli4 using our
4323 				 * own buffers like we do for biu diags
4324 				 */
4325 				header = (struct mbox_header *)
4326 						&pmb->un.varWords[0];
4327 				nembed_sge = (struct lpfc_mbx_nembed_cmd *)
4328 						&pmb->un.varWords[0];
4329 				receive_length = nembed_sge->sge[0].length;
4330 
4331 				/* receive length cannot be greater than
4332 				 * mailbox extension size
4333 				 */
4334 				if ((receive_length == 0) ||
4335 				    (receive_length > MAILBOX_EXT_SIZE)) {
4336 					rc = -ERANGE;
4337 					goto job_done;
4338 				}
4339 
4340 				nembed_sge->sge[0].pa_hi =
4341 						putPaddrHigh(dmabuf->phys
4342 						   + sizeof(MAILBOX_t));
4343 				nembed_sge->sge[0].pa_lo =
4344 						putPaddrLow(dmabuf->phys
4345 						   + sizeof(MAILBOX_t));
4346 			}
4347 		}
4348 	}
4349 
4350 	dd_data->context_un.mbox.dmabuffers = dmabuf;
4351 
4352 	/* setup wake call as IOCB callback */
4353 	pmboxq->mbox_cmpl = lpfc_bsg_issue_mbox_cmpl;
4354 
4355 	/* setup context field to pass wait_queue pointer to wake function */
4356 	pmboxq->context1 = dd_data;
4357 	dd_data->type = TYPE_MBOX;
4358 	dd_data->context_un.mbox.pmboxq = pmboxq;
4359 	dd_data->context_un.mbox.mb = (MAILBOX_t *)pmbx;
4360 	dd_data->context_un.mbox.set_job = job;
4361 	dd_data->context_un.mbox.ext = ext;
4362 	dd_data->context_un.mbox.mbOffset = mbox_req->mbOffset;
4363 	dd_data->context_un.mbox.inExtWLen = mbox_req->inExtWLen;
4364 	dd_data->context_un.mbox.outExtWLen = mbox_req->outExtWLen;
4365 	job->dd_data = dd_data;
4366 
4367 	if ((vport->fc_flag & FC_OFFLINE_MODE) ||
4368 	    (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE))) {
4369 		rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_POLL);
4370 		if (rc != MBX_SUCCESS) {
4371 			rc = (rc == MBX_TIMEOUT) ? -ETIME : -ENODEV;
4372 			goto job_done;
4373 		}
4374 
4375 		/* job finished, copy the data */
4376 		memcpy(pmbx, pmb, sizeof(*pmb));
4377 		job->reply->reply_payload_rcv_len =
4378 			sg_copy_from_buffer(job->reply_payload.sg_list,
4379 					    job->reply_payload.sg_cnt,
4380 					    pmbx, size);
4381 		/* not waiting mbox already done */
4382 		rc = 0;
4383 		goto job_done;
4384 	}
4385 
4386 	rc = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4387 	if ((rc == MBX_SUCCESS) || (rc == MBX_BUSY))
4388 		return 1; /* job started */
4389 
4390 job_done:
4391 	/* common exit for error or job completed inline */
4392 	if (pmboxq)
4393 		mempool_free(pmboxq, phba->mbox_mem_pool);
4394 	lpfc_bsg_dma_page_free(phba, dmabuf);
4395 	kfree(dd_data);
4396 
4397 job_cont:
4398 	return rc;
4399 }
4400 
4401 /**
4402  * lpfc_bsg_mbox_cmd - process an fc bsg LPFC_BSG_VENDOR_MBOX command
4403  * @job: MBOX fc_bsg_job for LPFC_BSG_VENDOR_MBOX.
4404  **/
4405 static int
4406 lpfc_bsg_mbox_cmd(struct fc_bsg_job *job)
4407 {
4408 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4409 	struct lpfc_hba *phba = vport->phba;
4410 	struct dfc_mbox_req *mbox_req;
4411 	int rc = 0;
4412 
4413 	/* mix-and-match backward compatibility */
4414 	job->reply->reply_payload_rcv_len = 0;
4415 	if (job->request_len <
4416 	    sizeof(struct fc_bsg_request) + sizeof(struct dfc_mbox_req)) {
4417 		lpfc_printf_log(phba, KERN_INFO, LOG_LIBDFC,
4418 				"2737 Mix-and-match backward compability "
4419 				"between MBOX_REQ old size:%d and "
4420 				"new request size:%d\n",
4421 				(int)(job->request_len -
4422 				      sizeof(struct fc_bsg_request)),
4423 				(int)sizeof(struct dfc_mbox_req));
4424 		mbox_req = (struct dfc_mbox_req *)
4425 				job->request->rqst_data.h_vendor.vendor_cmd;
4426 		mbox_req->extMboxTag = 0;
4427 		mbox_req->extSeqNum = 0;
4428 	}
4429 
4430 	rc = lpfc_bsg_issue_mbox(phba, job, vport);
4431 
4432 	if (rc == 0) {
4433 		/* job done */
4434 		job->reply->result = 0;
4435 		job->dd_data = NULL;
4436 		job->job_done(job);
4437 	} else if (rc == 1)
4438 		/* job submitted, will complete later*/
4439 		rc = 0; /* return zero, no error */
4440 	else {
4441 		/* some error occurred */
4442 		job->reply->result = rc;
4443 		job->dd_data = NULL;
4444 	}
4445 
4446 	return rc;
4447 }
4448 
4449 /**
4450  * lpfc_bsg_menlo_cmd_cmp - lpfc_menlo_cmd completion handler
4451  * @phba: Pointer to HBA context object.
4452  * @cmdiocbq: Pointer to command iocb.
4453  * @rspiocbq: Pointer to response iocb.
4454  *
4455  * This function is the completion handler for iocbs issued using
4456  * lpfc_menlo_cmd function. This function is called by the
4457  * ring event handler function without any lock held. This function
4458  * can be called from both worker thread context and interrupt
4459  * context. This function also can be called from another thread which
4460  * cleans up the SLI layer objects.
4461  * This function copies the contents of the response iocb to the
4462  * response iocb memory object provided by the caller of
4463  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4464  * sleeps for the iocb completion.
4465  **/
4466 static void
4467 lpfc_bsg_menlo_cmd_cmp(struct lpfc_hba *phba,
4468 			struct lpfc_iocbq *cmdiocbq,
4469 			struct lpfc_iocbq *rspiocbq)
4470 {
4471 	struct bsg_job_data *dd_data;
4472 	struct fc_bsg_job *job;
4473 	IOCB_t *rsp;
4474 	struct lpfc_dmabuf *bmp;
4475 	struct lpfc_bsg_menlo *menlo;
4476 	unsigned long flags;
4477 	struct menlo_response *menlo_resp;
4478 	int rc = 0;
4479 
4480 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
4481 	dd_data = cmdiocbq->context1;
4482 	if (!dd_data) {
4483 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4484 		return;
4485 	}
4486 
4487 	menlo = &dd_data->context_un.menlo;
4488 	job = menlo->set_job;
4489 	job->dd_data = NULL; /* so timeout handler does not reply */
4490 
4491 	spin_lock(&phba->hbalock);
4492 	cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4493 	if (cmdiocbq->context2 && rspiocbq)
4494 		memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4495 		       &rspiocbq->iocb, sizeof(IOCB_t));
4496 	spin_unlock(&phba->hbalock);
4497 
4498 	bmp = menlo->bmp;
4499 	rspiocbq = menlo->rspiocbq;
4500 	rsp = &rspiocbq->iocb;
4501 
4502 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4503 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
4504 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4505 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4506 
4507 	/* always return the xri, this would be used in the case
4508 	 * of a menlo download to allow the data to be sent as a continuation
4509 	 * of the exchange.
4510 	 */
4511 	menlo_resp = (struct menlo_response *)
4512 		job->reply->reply_data.vendor_reply.vendor_rsp;
4513 	menlo_resp->xri = rsp->ulpContext;
4514 	if (rsp->ulpStatus) {
4515 		if (rsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4516 			switch (rsp->un.ulpWord[4] & 0xff) {
4517 			case IOERR_SEQUENCE_TIMEOUT:
4518 				rc = -ETIMEDOUT;
4519 				break;
4520 			case IOERR_INVALID_RPI:
4521 				rc = -EFAULT;
4522 				break;
4523 			default:
4524 				rc = -EACCES;
4525 				break;
4526 			}
4527 		} else
4528 			rc = -EACCES;
4529 	} else
4530 		job->reply->reply_payload_rcv_len =
4531 			rsp->un.genreq64.bdl.bdeSize;
4532 
4533 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4534 	lpfc_sli_release_iocbq(phba, rspiocbq);
4535 	lpfc_sli_release_iocbq(phba, cmdiocbq);
4536 	kfree(bmp);
4537 	kfree(dd_data);
4538 	/* make error code available to userspace */
4539 	job->reply->result = rc;
4540 	/* complete the job back to userspace */
4541 	job->job_done(job);
4542 	spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4543 	return;
4544 }
4545 
4546 /**
4547  * lpfc_menlo_cmd - send an ioctl for menlo hardware
4548  * @job: fc_bsg_job to handle
4549  *
4550  * This function issues a gen request 64 CR ioctl for all menlo cmd requests,
4551  * all the command completions will return the xri for the command.
4552  * For menlo data requests a gen request 64 CX is used to continue the exchange
4553  * supplied in the menlo request header xri field.
4554  **/
4555 static int
4556 lpfc_menlo_cmd(struct fc_bsg_job *job)
4557 {
4558 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4559 	struct lpfc_hba *phba = vport->phba;
4560 	struct lpfc_iocbq *cmdiocbq, *rspiocbq;
4561 	IOCB_t *cmd, *rsp;
4562 	int rc = 0;
4563 	struct menlo_command *menlo_cmd;
4564 	struct menlo_response *menlo_resp;
4565 	struct lpfc_dmabuf *bmp = NULL;
4566 	int request_nseg;
4567 	int reply_nseg;
4568 	struct scatterlist *sgel = NULL;
4569 	int numbde;
4570 	dma_addr_t busaddr;
4571 	struct bsg_job_data *dd_data;
4572 	struct ulp_bde64 *bpl = NULL;
4573 
4574 	/* in case no data is returned return just the return code */
4575 	job->reply->reply_payload_rcv_len = 0;
4576 
4577 	if (job->request_len <
4578 	    sizeof(struct fc_bsg_request) +
4579 		sizeof(struct menlo_command)) {
4580 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4581 				"2784 Received MENLO_CMD request below "
4582 				"minimum size\n");
4583 		rc = -ERANGE;
4584 		goto no_dd_data;
4585 	}
4586 
4587 	if (job->reply_len <
4588 	    sizeof(struct fc_bsg_request) + sizeof(struct menlo_response)) {
4589 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4590 				"2785 Received MENLO_CMD reply below "
4591 				"minimum size\n");
4592 		rc = -ERANGE;
4593 		goto no_dd_data;
4594 	}
4595 
4596 	if (!(phba->menlo_flag & HBA_MENLO_SUPPORT)) {
4597 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4598 				"2786 Adapter does not support menlo "
4599 				"commands\n");
4600 		rc = -EPERM;
4601 		goto no_dd_data;
4602 	}
4603 
4604 	menlo_cmd = (struct menlo_command *)
4605 		job->request->rqst_data.h_vendor.vendor_cmd;
4606 
4607 	menlo_resp = (struct menlo_response *)
4608 		job->reply->reply_data.vendor_reply.vendor_rsp;
4609 
4610 	/* allocate our bsg tracking structure */
4611 	dd_data = kmalloc(sizeof(struct bsg_job_data), GFP_KERNEL);
4612 	if (!dd_data) {
4613 		lpfc_printf_log(phba, KERN_WARNING, LOG_LIBDFC,
4614 				"2787 Failed allocation of dd_data\n");
4615 		rc = -ENOMEM;
4616 		goto no_dd_data;
4617 	}
4618 
4619 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4620 	if (!bmp) {
4621 		rc = -ENOMEM;
4622 		goto free_dd;
4623 	}
4624 
4625 	cmdiocbq = lpfc_sli_get_iocbq(phba);
4626 	if (!cmdiocbq) {
4627 		rc = -ENOMEM;
4628 		goto free_bmp;
4629 	}
4630 
4631 	rspiocbq = lpfc_sli_get_iocbq(phba);
4632 	if (!rspiocbq) {
4633 		rc = -ENOMEM;
4634 		goto free_cmdiocbq;
4635 	}
4636 
4637 	rsp = &rspiocbq->iocb;
4638 
4639 	bmp->virt = lpfc_mbuf_alloc(phba, 0, &bmp->phys);
4640 	if (!bmp->virt) {
4641 		rc = -ENOMEM;
4642 		goto free_rspiocbq;
4643 	}
4644 
4645 	INIT_LIST_HEAD(&bmp->list);
4646 	bpl = (struct ulp_bde64 *) bmp->virt;
4647 	request_nseg = pci_map_sg(phba->pcidev, job->request_payload.sg_list,
4648 				  job->request_payload.sg_cnt, DMA_TO_DEVICE);
4649 	for_each_sg(job->request_payload.sg_list, sgel, request_nseg, numbde) {
4650 		busaddr = sg_dma_address(sgel);
4651 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
4652 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
4653 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
4654 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4655 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4656 		bpl++;
4657 	}
4658 
4659 	reply_nseg = pci_map_sg(phba->pcidev, job->reply_payload.sg_list,
4660 				job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4661 	for_each_sg(job->reply_payload.sg_list, sgel, reply_nseg, numbde) {
4662 		busaddr = sg_dma_address(sgel);
4663 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
4664 		bpl->tus.f.bdeSize = sg_dma_len(sgel);
4665 		bpl->tus.w = cpu_to_le32(bpl->tus.w);
4666 		bpl->addrLow = cpu_to_le32(putPaddrLow(busaddr));
4667 		bpl->addrHigh = cpu_to_le32(putPaddrHigh(busaddr));
4668 		bpl++;
4669 	}
4670 
4671 	cmd = &cmdiocbq->iocb;
4672 	cmd->un.genreq64.bdl.ulpIoTag32 = 0;
4673 	cmd->un.genreq64.bdl.addrHigh = putPaddrHigh(bmp->phys);
4674 	cmd->un.genreq64.bdl.addrLow = putPaddrLow(bmp->phys);
4675 	cmd->un.genreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
4676 	cmd->un.genreq64.bdl.bdeSize =
4677 	    (request_nseg + reply_nseg) * sizeof(struct ulp_bde64);
4678 	cmd->un.genreq64.w5.hcsw.Fctl = (SI | LA);
4679 	cmd->un.genreq64.w5.hcsw.Dfctl = 0;
4680 	cmd->un.genreq64.w5.hcsw.Rctl = FC_RCTL_DD_UNSOL_CMD;
4681 	cmd->un.genreq64.w5.hcsw.Type = MENLO_TRANSPORT_TYPE; /* 0xfe */
4682 	cmd->ulpBdeCount = 1;
4683 	cmd->ulpClass = CLASS3;
4684 	cmd->ulpOwner = OWN_CHIP;
4685 	cmd->ulpLe = 1; /* Limited Edition */
4686 	cmdiocbq->iocb_flag |= LPFC_IO_LIBDFC;
4687 	cmdiocbq->vport = phba->pport;
4688 	/* We want the firmware to timeout before we do */
4689 	cmd->ulpTimeout = MENLO_TIMEOUT - 5;
4690 	cmdiocbq->context3 = bmp;
4691 	cmdiocbq->context2 = rspiocbq;
4692 	cmdiocbq->iocb_cmpl = lpfc_bsg_menlo_cmd_cmp;
4693 	cmdiocbq->context1 = dd_data;
4694 	cmdiocbq->context2 = rspiocbq;
4695 	if (menlo_cmd->cmd == LPFC_BSG_VENDOR_MENLO_CMD) {
4696 		cmd->ulpCommand = CMD_GEN_REQUEST64_CR;
4697 		cmd->ulpPU = MENLO_PU; /* 3 */
4698 		cmd->un.ulpWord[4] = MENLO_DID; /* 0x0000FC0E */
4699 		cmd->ulpContext = MENLO_CONTEXT; /* 0 */
4700 	} else {
4701 		cmd->ulpCommand = CMD_GEN_REQUEST64_CX;
4702 		cmd->ulpPU = 1;
4703 		cmd->un.ulpWord[4] = 0;
4704 		cmd->ulpContext = menlo_cmd->xri;
4705 	}
4706 
4707 	dd_data->type = TYPE_MENLO;
4708 	dd_data->context_un.menlo.cmdiocbq = cmdiocbq;
4709 	dd_data->context_un.menlo.rspiocbq = rspiocbq;
4710 	dd_data->context_un.menlo.set_job = job;
4711 	dd_data->context_un.menlo.bmp = bmp;
4712 
4713 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, cmdiocbq,
4714 		MENLO_TIMEOUT - 5);
4715 	if (rc == IOCB_SUCCESS)
4716 		return 0; /* done for now */
4717 
4718 	/* iocb failed so cleanup */
4719 	pci_unmap_sg(phba->pcidev, job->request_payload.sg_list,
4720 		     job->request_payload.sg_cnt, DMA_TO_DEVICE);
4721 	pci_unmap_sg(phba->pcidev, job->reply_payload.sg_list,
4722 		     job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
4723 
4724 	lpfc_mbuf_free(phba, bmp->virt, bmp->phys);
4725 
4726 free_rspiocbq:
4727 	lpfc_sli_release_iocbq(phba, rspiocbq);
4728 free_cmdiocbq:
4729 	lpfc_sli_release_iocbq(phba, cmdiocbq);
4730 free_bmp:
4731 	kfree(bmp);
4732 free_dd:
4733 	kfree(dd_data);
4734 no_dd_data:
4735 	/* make error code available to userspace */
4736 	job->reply->result = rc;
4737 	job->dd_data = NULL;
4738 	return rc;
4739 }
4740 
4741 /**
4742  * lpfc_bsg_hst_vendor - process a vendor-specific fc_bsg_job
4743  * @job: fc_bsg_job to handle
4744  **/
4745 static int
4746 lpfc_bsg_hst_vendor(struct fc_bsg_job *job)
4747 {
4748 	int command = job->request->rqst_data.h_vendor.vendor_cmd[0];
4749 	int rc;
4750 
4751 	switch (command) {
4752 	case LPFC_BSG_VENDOR_SET_CT_EVENT:
4753 		rc = lpfc_bsg_hba_set_event(job);
4754 		break;
4755 	case LPFC_BSG_VENDOR_GET_CT_EVENT:
4756 		rc = lpfc_bsg_hba_get_event(job);
4757 		break;
4758 	case LPFC_BSG_VENDOR_SEND_MGMT_RESP:
4759 		rc = lpfc_bsg_send_mgmt_rsp(job);
4760 		break;
4761 	case LPFC_BSG_VENDOR_DIAG_MODE:
4762 		rc = lpfc_bsg_diag_loopback_mode(job);
4763 		break;
4764 	case LPFC_BSG_VENDOR_DIAG_MODE_END:
4765 		rc = lpfc_sli4_bsg_diag_mode_end(job);
4766 		break;
4767 	case LPFC_BSG_VENDOR_DIAG_RUN_LOOPBACK:
4768 		rc = lpfc_bsg_diag_loopback_run(job);
4769 		break;
4770 	case LPFC_BSG_VENDOR_LINK_DIAG_TEST:
4771 		rc = lpfc_sli4_bsg_link_diag_test(job);
4772 		break;
4773 	case LPFC_BSG_VENDOR_GET_MGMT_REV:
4774 		rc = lpfc_bsg_get_dfc_rev(job);
4775 		break;
4776 	case LPFC_BSG_VENDOR_MBOX:
4777 		rc = lpfc_bsg_mbox_cmd(job);
4778 		break;
4779 	case LPFC_BSG_VENDOR_MENLO_CMD:
4780 	case LPFC_BSG_VENDOR_MENLO_DATA:
4781 		rc = lpfc_menlo_cmd(job);
4782 		break;
4783 	default:
4784 		rc = -EINVAL;
4785 		job->reply->reply_payload_rcv_len = 0;
4786 		/* make error code available to userspace */
4787 		job->reply->result = rc;
4788 		break;
4789 	}
4790 
4791 	return rc;
4792 }
4793 
4794 /**
4795  * lpfc_bsg_request - handle a bsg request from the FC transport
4796  * @job: fc_bsg_job to handle
4797  **/
4798 int
4799 lpfc_bsg_request(struct fc_bsg_job *job)
4800 {
4801 	uint32_t msgcode;
4802 	int rc;
4803 
4804 	msgcode = job->request->msgcode;
4805 	switch (msgcode) {
4806 	case FC_BSG_HST_VENDOR:
4807 		rc = lpfc_bsg_hst_vendor(job);
4808 		break;
4809 	case FC_BSG_RPT_ELS:
4810 		rc = lpfc_bsg_rport_els(job);
4811 		break;
4812 	case FC_BSG_RPT_CT:
4813 		rc = lpfc_bsg_send_mgmt_cmd(job);
4814 		break;
4815 	default:
4816 		rc = -EINVAL;
4817 		job->reply->reply_payload_rcv_len = 0;
4818 		/* make error code available to userspace */
4819 		job->reply->result = rc;
4820 		break;
4821 	}
4822 
4823 	return rc;
4824 }
4825 
4826 /**
4827  * lpfc_bsg_timeout - handle timeout of a bsg request from the FC transport
4828  * @job: fc_bsg_job that has timed out
4829  *
4830  * This function just aborts the job's IOCB.  The aborted IOCB will return to
4831  * the waiting function which will handle passing the error back to userspace
4832  **/
4833 int
4834 lpfc_bsg_timeout(struct fc_bsg_job *job)
4835 {
4836 	struct lpfc_vport *vport = (struct lpfc_vport *)job->shost->hostdata;
4837 	struct lpfc_hba *phba = vport->phba;
4838 	struct lpfc_iocbq *cmdiocb;
4839 	struct lpfc_bsg_event *evt;
4840 	struct lpfc_bsg_iocb *iocb;
4841 	struct lpfc_bsg_mbox *mbox;
4842 	struct lpfc_bsg_menlo *menlo;
4843 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4844 	struct bsg_job_data *dd_data;
4845 	unsigned long flags;
4846 
4847 	spin_lock_irqsave(&phba->ct_ev_lock, flags);
4848 	dd_data = (struct bsg_job_data *)job->dd_data;
4849 	/* timeout and completion crossed paths if no dd_data */
4850 	if (!dd_data) {
4851 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4852 		return 0;
4853 	}
4854 
4855 	switch (dd_data->type) {
4856 	case TYPE_IOCB:
4857 		iocb = &dd_data->context_un.iocb;
4858 		cmdiocb = iocb->cmdiocbq;
4859 		/* hint to completion handler that the job timed out */
4860 		job->reply->result = -EAGAIN;
4861 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4862 		/* this will call our completion handler */
4863 		spin_lock_irq(&phba->hbalock);
4864 		lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
4865 		spin_unlock_irq(&phba->hbalock);
4866 		break;
4867 	case TYPE_EVT:
4868 		evt = dd_data->context_un.evt;
4869 		/* this event has no job anymore */
4870 		evt->set_job = NULL;
4871 		job->dd_data = NULL;
4872 		job->reply->reply_payload_rcv_len = 0;
4873 		/* Return -EAGAIN which is our way of signallying the
4874 		 * app to retry.
4875 		 */
4876 		job->reply->result = -EAGAIN;
4877 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4878 		job->job_done(job);
4879 		break;
4880 	case TYPE_MBOX:
4881 		mbox = &dd_data->context_un.mbox;
4882 		/* this mbox has no job anymore */
4883 		mbox->set_job = NULL;
4884 		job->dd_data = NULL;
4885 		job->reply->reply_payload_rcv_len = 0;
4886 		job->reply->result = -EAGAIN;
4887 		/* the mbox completion handler can now be run */
4888 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4889 		job->job_done(job);
4890 		if (phba->mbox_ext_buf_ctx.state == LPFC_BSG_MBOX_PORT)
4891 			phba->mbox_ext_buf_ctx.state = LPFC_BSG_MBOX_ABTS;
4892 		break;
4893 	case TYPE_MENLO:
4894 		menlo = &dd_data->context_un.menlo;
4895 		cmdiocb = menlo->cmdiocbq;
4896 		/* hint to completion handler that the job timed out */
4897 		job->reply->result = -EAGAIN;
4898 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4899 		/* this will call our completion handler */
4900 		spin_lock_irq(&phba->hbalock);
4901 		lpfc_sli_issue_abort_iotag(phba, pring, cmdiocb);
4902 		spin_unlock_irq(&phba->hbalock);
4903 		break;
4904 	default:
4905 		spin_unlock_irqrestore(&phba->ct_ev_lock, flags);
4906 		break;
4907 	}
4908 
4909 	/* scsi transport fc fc_bsg_job_timeout expects a zero return code,
4910 	 * otherwise an error message will be displayed on the console
4911 	 * so always return success (zero)
4912 	 */
4913 	return 0;
4914 }
4915