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