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