• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 /* See Fibre Channel protocol T11 FC-LS for details */
24 #include <linux/blkdev.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <uapi/scsi/fc/fc_fs.h>
34 #include <uapi/scsi/fc/fc_els.h>
35 
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
47 #include "lpfc_debugfs.h"
48 
49 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
50 			  struct lpfc_iocbq *);
51 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
52 			struct lpfc_iocbq *);
53 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
54 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
55 				struct lpfc_nodelist *ndlp, uint8_t retry);
56 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
57 				  struct lpfc_iocbq *iocb);
58 
59 static int lpfc_max_els_tries = 3;
60 
61 /**
62  * lpfc_els_chk_latt - Check host link attention event for a vport
63  * @vport: pointer to a host virtual N_Port data structure.
64  *
65  * This routine checks whether there is an outstanding host link
66  * attention event during the discovery process with the @vport. It is done
67  * by reading the HBA's Host Attention (HA) register. If there is any host
68  * link attention events during this @vport's discovery process, the @vport
69  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
70  * be issued if the link state is not already in host link cleared state,
71  * and a return code shall indicate whether the host link attention event
72  * had happened.
73  *
74  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
75  * state in LPFC_VPORT_READY, the request for checking host link attention
76  * event will be ignored and a return code shall indicate no host link
77  * attention event had happened.
78  *
79  * Return codes
80  *   0 - no host link attention event happened
81  *   1 - host link attention event happened
82  **/
83 int
lpfc_els_chk_latt(struct lpfc_vport * vport)84 lpfc_els_chk_latt(struct lpfc_vport *vport)
85 {
86 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
87 	struct lpfc_hba  *phba = vport->phba;
88 	uint32_t ha_copy;
89 
90 	if (vport->port_state >= LPFC_VPORT_READY ||
91 	    phba->link_state == LPFC_LINK_DOWN ||
92 	    phba->sli_rev > LPFC_SLI_REV3)
93 		return 0;
94 
95 	/* Read the HBA Host Attention Register */
96 	if (lpfc_readl(phba->HAregaddr, &ha_copy))
97 		return 1;
98 
99 	if (!(ha_copy & HA_LATT))
100 		return 0;
101 
102 	/* Pending Link Event during Discovery */
103 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
104 			 "0237 Pending Link Event during "
105 			 "Discovery: State x%x\n",
106 			 phba->pport->port_state);
107 
108 	/* CLEAR_LA should re-enable link attention events and
109 	 * we should then immediately take a LATT event. The
110 	 * LATT processing should call lpfc_linkdown() which
111 	 * will cleanup any left over in-progress discovery
112 	 * events.
113 	 */
114 	spin_lock_irq(shost->host_lock);
115 	vport->fc_flag |= FC_ABORT_DISCOVERY;
116 	spin_unlock_irq(shost->host_lock);
117 
118 	if (phba->link_state != LPFC_CLEAR_LA)
119 		lpfc_issue_clear_la(phba, vport);
120 
121 	return 1;
122 }
123 
124 /**
125  * lpfc_prep_els_iocb - Allocate and prepare a lpfc iocb data structure
126  * @vport: pointer to a host virtual N_Port data structure.
127  * @expectRsp: flag indicating whether response is expected.
128  * @cmdSize: size of the ELS command.
129  * @retry: number of retries to the command IOCB when it fails.
130  * @ndlp: pointer to a node-list data structure.
131  * @did: destination identifier.
132  * @elscmd: the ELS command code.
133  *
134  * This routine is used for allocating a lpfc-IOCB data structure from
135  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
136  * passed into the routine for discovery state machine to issue an Extended
137  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
138  * and preparation routine that is used by all the discovery state machine
139  * routines and the ELS command-specific fields will be later set up by
140  * the individual discovery machine routines after calling this routine
141  * allocating and preparing a generic IOCB data structure. It fills in the
142  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
143  * payload and response payload (if expected). The reference count on the
144  * ndlp is incremented by 1 and the reference to the ndlp is put into
145  * context1 of the IOCB data structure for this IOCB to hold the ndlp
146  * reference for the command's callback function to access later.
147  *
148  * Return code
149  *   Pointer to the newly allocated/prepared els iocb data structure
150  *   NULL - when els iocb data structure allocation/preparation failed
151  **/
152 struct lpfc_iocbq *
lpfc_prep_els_iocb(struct lpfc_vport * vport,uint8_t expectRsp,uint16_t cmdSize,uint8_t retry,struct lpfc_nodelist * ndlp,uint32_t did,uint32_t elscmd)153 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
154 		   uint16_t cmdSize, uint8_t retry,
155 		   struct lpfc_nodelist *ndlp, uint32_t did,
156 		   uint32_t elscmd)
157 {
158 	struct lpfc_hba  *phba = vport->phba;
159 	struct lpfc_iocbq *elsiocb;
160 	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
161 	struct ulp_bde64 *bpl;
162 	IOCB_t *icmd;
163 
164 
165 	if (!lpfc_is_link_up(phba))
166 		return NULL;
167 
168 	/* Allocate buffer for  command iocb */
169 	elsiocb = lpfc_sli_get_iocbq(phba);
170 
171 	if (elsiocb == NULL)
172 		return NULL;
173 
174 	/*
175 	 * If this command is for fabric controller and HBA running
176 	 * in FIP mode send FLOGI, FDISC and LOGO as FIP frames.
177 	 */
178 	if ((did == Fabric_DID) &&
179 		(phba->hba_flag & HBA_FIP_SUPPORT) &&
180 		((elscmd == ELS_CMD_FLOGI) ||
181 		 (elscmd == ELS_CMD_FDISC) ||
182 		 (elscmd == ELS_CMD_LOGO)))
183 		switch (elscmd) {
184 		case ELS_CMD_FLOGI:
185 		elsiocb->iocb_flag |=
186 			((LPFC_ELS_ID_FLOGI << LPFC_FIP_ELS_ID_SHIFT)
187 					& LPFC_FIP_ELS_ID_MASK);
188 		break;
189 		case ELS_CMD_FDISC:
190 		elsiocb->iocb_flag |=
191 			((LPFC_ELS_ID_FDISC << LPFC_FIP_ELS_ID_SHIFT)
192 					& LPFC_FIP_ELS_ID_MASK);
193 		break;
194 		case ELS_CMD_LOGO:
195 		elsiocb->iocb_flag |=
196 			((LPFC_ELS_ID_LOGO << LPFC_FIP_ELS_ID_SHIFT)
197 					& LPFC_FIP_ELS_ID_MASK);
198 		break;
199 		}
200 	else
201 		elsiocb->iocb_flag &= ~LPFC_FIP_ELS_ID_MASK;
202 
203 	icmd = &elsiocb->iocb;
204 
205 	/* fill in BDEs for command */
206 	/* Allocate buffer for command payload */
207 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
208 	if (pcmd)
209 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
210 	if (!pcmd || !pcmd->virt)
211 		goto els_iocb_free_pcmb_exit;
212 
213 	INIT_LIST_HEAD(&pcmd->list);
214 
215 	/* Allocate buffer for response payload */
216 	if (expectRsp) {
217 		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
218 		if (prsp)
219 			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
220 						     &prsp->phys);
221 		if (!prsp || !prsp->virt)
222 			goto els_iocb_free_prsp_exit;
223 		INIT_LIST_HEAD(&prsp->list);
224 	} else
225 		prsp = NULL;
226 
227 	/* Allocate buffer for Buffer ptr list */
228 	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
229 	if (pbuflist)
230 		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
231 						 &pbuflist->phys);
232 	if (!pbuflist || !pbuflist->virt)
233 		goto els_iocb_free_pbuf_exit;
234 
235 	INIT_LIST_HEAD(&pbuflist->list);
236 
237 	if (expectRsp) {
238 		icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
239 		icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
240 		icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
241 		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
242 
243 		icmd->un.elsreq64.remoteID = did;		/* DID */
244 		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
245 		if (elscmd == ELS_CMD_FLOGI)
246 			icmd->ulpTimeout = FF_DEF_RATOV * 2;
247 		else if (elscmd == ELS_CMD_LOGO)
248 			icmd->ulpTimeout = phba->fc_ratov;
249 		else
250 			icmd->ulpTimeout = phba->fc_ratov * 2;
251 	} else {
252 		icmd->un.xseq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
253 		icmd->un.xseq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
254 		icmd->un.xseq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
255 		icmd->un.xseq64.bdl.bdeSize = sizeof(struct ulp_bde64);
256 		icmd->un.xseq64.xmit_els_remoteID = did;	/* DID */
257 		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
258 	}
259 	icmd->ulpBdeCount = 1;
260 	icmd->ulpLe = 1;
261 	icmd->ulpClass = CLASS3;
262 
263 	/*
264 	 * If we have NPIV enabled, we want to send ELS traffic by VPI.
265 	 * For SLI4, since the driver controls VPIs we also want to include
266 	 * all ELS pt2pt protocol traffic as well.
267 	 */
268 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) ||
269 		((phba->sli_rev == LPFC_SLI_REV4) &&
270 		    (vport->fc_flag & FC_PT2PT))) {
271 
272 		if (expectRsp) {
273 			icmd->un.elsreq64.myID = vport->fc_myDID;
274 
275 			/* For ELS_REQUEST64_CR, use the VPI by default */
276 			icmd->ulpContext = phba->vpi_ids[vport->vpi];
277 		}
278 
279 		icmd->ulpCt_h = 0;
280 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
281 		if (elscmd == ELS_CMD_ECHO)
282 			icmd->ulpCt_l = 0; /* context = invalid RPI */
283 		else
284 			icmd->ulpCt_l = 1; /* context = VPI */
285 	}
286 
287 	bpl = (struct ulp_bde64 *) pbuflist->virt;
288 	bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
289 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
290 	bpl->tus.f.bdeSize = cmdSize;
291 	bpl->tus.f.bdeFlags = 0;
292 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
293 
294 	if (expectRsp) {
295 		bpl++;
296 		bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
297 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
298 		bpl->tus.f.bdeSize = FCELSSIZE;
299 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
300 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
301 	}
302 
303 	/* prevent preparing iocb with NULL ndlp reference */
304 	elsiocb->context1 = lpfc_nlp_get(ndlp);
305 	if (!elsiocb->context1)
306 		goto els_iocb_free_pbuf_exit;
307 	elsiocb->context2 = pcmd;
308 	elsiocb->context3 = pbuflist;
309 	elsiocb->retry = retry;
310 	elsiocb->vport = vport;
311 	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
312 
313 	if (prsp) {
314 		list_add(&prsp->list, &pcmd->list);
315 	}
316 	if (expectRsp) {
317 		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
318 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
319 				 "0116 Xmit ELS command x%x to remote "
320 				 "NPORT x%x I/O tag: x%x, port state:x%x "
321 				 "rpi x%x fc_flag:x%x\n",
322 				 elscmd, did, elsiocb->iotag,
323 				 vport->port_state, ndlp->nlp_rpi,
324 				 vport->fc_flag);
325 	} else {
326 		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
327 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
328 				 "0117 Xmit ELS response x%x to remote "
329 				 "NPORT x%x I/O tag: x%x, size: x%x "
330 				 "port_state x%x  rpi x%x fc_flag x%x\n",
331 				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
332 				 cmdSize, vport->port_state,
333 				 ndlp->nlp_rpi, vport->fc_flag);
334 	}
335 	return elsiocb;
336 
337 els_iocb_free_pbuf_exit:
338 	if (expectRsp)
339 		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
340 	kfree(pbuflist);
341 
342 els_iocb_free_prsp_exit:
343 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
344 	kfree(prsp);
345 
346 els_iocb_free_pcmb_exit:
347 	kfree(pcmd);
348 	lpfc_sli_release_iocbq(phba, elsiocb);
349 	return NULL;
350 }
351 
352 /**
353  * lpfc_issue_fabric_reglogin - Issue fabric registration login for a vport
354  * @vport: pointer to a host virtual N_Port data structure.
355  *
356  * This routine issues a fabric registration login for a @vport. An
357  * active ndlp node with Fabric_DID must already exist for this @vport.
358  * The routine invokes two mailbox commands to carry out fabric registration
359  * login through the HBA firmware: the first mailbox command requests the
360  * HBA to perform link configuration for the @vport; and the second mailbox
361  * command requests the HBA to perform the actual fabric registration login
362  * with the @vport.
363  *
364  * Return code
365  *   0 - successfully issued fabric registration login for @vport
366  *   -ENXIO -- failed to issue fabric registration login for @vport
367  **/
368 int
lpfc_issue_fabric_reglogin(struct lpfc_vport * vport)369 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
370 {
371 	struct lpfc_hba  *phba = vport->phba;
372 	LPFC_MBOXQ_t *mbox;
373 	struct lpfc_dmabuf *mp;
374 	struct lpfc_nodelist *ndlp;
375 	struct serv_parm *sp;
376 	int rc;
377 	int err = 0;
378 
379 	sp = &phba->fc_fabparam;
380 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
381 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
382 		err = 1;
383 		goto fail;
384 	}
385 
386 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
387 	if (!mbox) {
388 		err = 2;
389 		goto fail;
390 	}
391 
392 	vport->port_state = LPFC_FABRIC_CFG_LINK;
393 	lpfc_config_link(phba, mbox);
394 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
395 	mbox->vport = vport;
396 
397 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
398 	if (rc == MBX_NOT_FINISHED) {
399 		err = 3;
400 		goto fail_free_mbox;
401 	}
402 
403 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
404 	if (!mbox) {
405 		err = 4;
406 		goto fail;
407 	}
408 	rc = lpfc_reg_rpi(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
409 			  ndlp->nlp_rpi);
410 	if (rc) {
411 		err = 5;
412 		goto fail_free_mbox;
413 	}
414 
415 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
416 	mbox->vport = vport;
417 	/* increment the reference count on ndlp to hold reference
418 	 * for the callback routine.
419 	 */
420 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
421 
422 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
423 	if (rc == MBX_NOT_FINISHED) {
424 		err = 6;
425 		goto fail_issue_reg_login;
426 	}
427 
428 	return 0;
429 
430 fail_issue_reg_login:
431 	/* decrement the reference count on ndlp just incremented
432 	 * for the failed mbox command.
433 	 */
434 	lpfc_nlp_put(ndlp);
435 	mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
436 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
437 	kfree(mp);
438 fail_free_mbox:
439 	mempool_free(mbox, phba->mbox_mem_pool);
440 
441 fail:
442 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
443 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
444 			 "0249 Cannot issue Register Fabric login: Err %d\n",
445 			 err);
446 	return -ENXIO;
447 }
448 
449 /**
450  * lpfc_issue_reg_vfi - Register VFI for this vport's fabric login
451  * @vport: pointer to a host virtual N_Port data structure.
452  *
453  * This routine issues a REG_VFI mailbox for the vfi, vpi, fcfi triplet for
454  * the @vport. This mailbox command is necessary for SLI4 port only.
455  *
456  * Return code
457  *   0 - successfully issued REG_VFI for @vport
458  *   A failure code otherwise.
459  **/
460 int
lpfc_issue_reg_vfi(struct lpfc_vport * vport)461 lpfc_issue_reg_vfi(struct lpfc_vport *vport)
462 {
463 	struct lpfc_hba  *phba = vport->phba;
464 	LPFC_MBOXQ_t *mboxq = NULL;
465 	struct lpfc_nodelist *ndlp;
466 	struct lpfc_dmabuf *dmabuf = NULL;
467 	int rc = 0;
468 
469 	/* move forward in case of SLI4 FC port loopback test and pt2pt mode */
470 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
471 	    !(phba->link_flag & LS_LOOPBACK_MODE) &&
472 	    !(vport->fc_flag & FC_PT2PT)) {
473 		ndlp = lpfc_findnode_did(vport, Fabric_DID);
474 		if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
475 			rc = -ENODEV;
476 			goto fail;
477 		}
478 	}
479 
480 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
481 	if (!mboxq) {
482 		rc = -ENOMEM;
483 		goto fail;
484 	}
485 
486 	/* Supply CSP's only if we are fabric connect or pt-to-pt connect */
487 	if ((vport->fc_flag & FC_FABRIC) || (vport->fc_flag & FC_PT2PT)) {
488 		dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
489 		if (!dmabuf) {
490 			rc = -ENOMEM;
491 			goto fail;
492 		}
493 		dmabuf->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &dmabuf->phys);
494 		if (!dmabuf->virt) {
495 			rc = -ENOMEM;
496 			goto fail;
497 		}
498 		memcpy(dmabuf->virt, &phba->fc_fabparam,
499 		       sizeof(struct serv_parm));
500 	}
501 
502 	vport->port_state = LPFC_FABRIC_CFG_LINK;
503 	if (dmabuf)
504 		lpfc_reg_vfi(mboxq, vport, dmabuf->phys);
505 	else
506 		lpfc_reg_vfi(mboxq, vport, 0);
507 
508 	mboxq->mbox_cmpl = lpfc_mbx_cmpl_reg_vfi;
509 	mboxq->vport = vport;
510 	mboxq->ctx_buf = dmabuf;
511 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
512 	if (rc == MBX_NOT_FINISHED) {
513 		rc = -ENXIO;
514 		goto fail;
515 	}
516 	return 0;
517 
518 fail:
519 	if (mboxq)
520 		mempool_free(mboxq, phba->mbox_mem_pool);
521 	if (dmabuf) {
522 		if (dmabuf->virt)
523 			lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
524 		kfree(dmabuf);
525 	}
526 
527 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
528 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
529 			 "0289 Issue Register VFI failed: Err %d\n", rc);
530 	return rc;
531 }
532 
533 /**
534  * lpfc_issue_unreg_vfi - Unregister VFI for this vport's fabric login
535  * @vport: pointer to a host virtual N_Port data structure.
536  *
537  * This routine issues a UNREG_VFI mailbox with the vfi, vpi, fcfi triplet for
538  * the @vport. This mailbox command is necessary for SLI4 port only.
539  *
540  * Return code
541  *   0 - successfully issued REG_VFI for @vport
542  *   A failure code otherwise.
543  **/
544 int
lpfc_issue_unreg_vfi(struct lpfc_vport * vport)545 lpfc_issue_unreg_vfi(struct lpfc_vport *vport)
546 {
547 	struct lpfc_hba *phba = vport->phba;
548 	struct Scsi_Host *shost;
549 	LPFC_MBOXQ_t *mboxq;
550 	int rc;
551 
552 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
553 	if (!mboxq) {
554 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
555 				"2556 UNREG_VFI mbox allocation failed"
556 				"HBA state x%x\n", phba->pport->port_state);
557 		return -ENOMEM;
558 	}
559 
560 	lpfc_unreg_vfi(mboxq, vport);
561 	mboxq->vport = vport;
562 	mboxq->mbox_cmpl = lpfc_unregister_vfi_cmpl;
563 
564 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
565 	if (rc == MBX_NOT_FINISHED) {
566 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
567 				"2557 UNREG_VFI issue mbox failed rc x%x "
568 				"HBA state x%x\n",
569 				rc, phba->pport->port_state);
570 		mempool_free(mboxq, phba->mbox_mem_pool);
571 		return -EIO;
572 	}
573 
574 	shost = lpfc_shost_from_vport(vport);
575 	spin_lock_irq(shost->host_lock);
576 	vport->fc_flag &= ~FC_VFI_REGISTERED;
577 	spin_unlock_irq(shost->host_lock);
578 	return 0;
579 }
580 
581 /**
582  * lpfc_check_clean_addr_bit - Check whether assigned FCID is clean.
583  * @vport: pointer to a host virtual N_Port data structure.
584  * @sp: pointer to service parameter data structure.
585  *
586  * This routine is called from FLOGI/FDISC completion handler functions.
587  * lpfc_check_clean_addr_bit return 1 when FCID/Fabric portname/ Fabric
588  * node nodename is changed in the completion service parameter else return
589  * 0. This function also set flag in the vport data structure to delay
590  * NP_Port discovery after the FLOGI/FDISC completion if Clean address bit
591  * in FLOGI/FDISC response is cleared and FCID/Fabric portname/ Fabric
592  * node nodename is changed in the completion service parameter.
593  *
594  * Return code
595  *   0 - FCID and Fabric Nodename and Fabric portname is not changed.
596  *   1 - FCID or Fabric Nodename or Fabric portname is changed.
597  *
598  **/
599 static uint8_t
lpfc_check_clean_addr_bit(struct lpfc_vport * vport,struct serv_parm * sp)600 lpfc_check_clean_addr_bit(struct lpfc_vport *vport,
601 		struct serv_parm *sp)
602 {
603 	struct lpfc_hba *phba = vport->phba;
604 	uint8_t fabric_param_changed = 0;
605 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
606 
607 	if ((vport->fc_prevDID != vport->fc_myDID) ||
608 		memcmp(&vport->fabric_portname, &sp->portName,
609 			sizeof(struct lpfc_name)) ||
610 		memcmp(&vport->fabric_nodename, &sp->nodeName,
611 			sizeof(struct lpfc_name)) ||
612 		(vport->vport_flag & FAWWPN_PARAM_CHG)) {
613 		fabric_param_changed = 1;
614 		vport->vport_flag &= ~FAWWPN_PARAM_CHG;
615 	}
616 	/*
617 	 * Word 1 Bit 31 in common service parameter is overloaded.
618 	 * Word 1 Bit 31 in FLOGI request is multiple NPort request
619 	 * Word 1 Bit 31 in FLOGI response is clean address bit
620 	 *
621 	 * If fabric parameter is changed and clean address bit is
622 	 * cleared delay nport discovery if
623 	 * - vport->fc_prevDID != 0 (not initial discovery) OR
624 	 * - lpfc_delay_discovery module parameter is set.
625 	 */
626 	if (fabric_param_changed && !sp->cmn.clean_address_bit &&
627 	    (vport->fc_prevDID || phba->cfg_delay_discovery)) {
628 		spin_lock_irq(shost->host_lock);
629 		vport->fc_flag |= FC_DISC_DELAYED;
630 		spin_unlock_irq(shost->host_lock);
631 	}
632 
633 	return fabric_param_changed;
634 }
635 
636 
637 /**
638  * lpfc_cmpl_els_flogi_fabric - Completion function for flogi to a fabric port
639  * @vport: pointer to a host virtual N_Port data structure.
640  * @ndlp: pointer to a node-list data structure.
641  * @sp: pointer to service parameter data structure.
642  * @irsp: pointer to the IOCB within the lpfc response IOCB.
643  *
644  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
645  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
646  * port in a fabric topology. It properly sets up the parameters to the @ndlp
647  * from the IOCB response. It also check the newly assigned N_Port ID to the
648  * @vport against the previously assigned N_Port ID. If it is different from
649  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
650  * is invoked on all the remaining nodes with the @vport to unregister the
651  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
652  * is invoked to register login to the fabric.
653  *
654  * Return code
655  *   0 - Success (currently, always return 0)
656  **/
657 static int
lpfc_cmpl_els_flogi_fabric(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct serv_parm * sp,IOCB_t * irsp)658 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
659 			   struct serv_parm *sp, IOCB_t *irsp)
660 {
661 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
662 	struct lpfc_hba  *phba = vport->phba;
663 	struct lpfc_nodelist *np;
664 	struct lpfc_nodelist *next_np;
665 	uint8_t fabric_param_changed;
666 
667 	spin_lock_irq(shost->host_lock);
668 	vport->fc_flag |= FC_FABRIC;
669 	spin_unlock_irq(shost->host_lock);
670 
671 	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
672 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
673 		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
674 
675 	phba->fc_edtovResol = sp->cmn.edtovResolution;
676 	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
677 
678 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
679 		spin_lock_irq(shost->host_lock);
680 		vport->fc_flag |= FC_PUBLIC_LOOP;
681 		spin_unlock_irq(shost->host_lock);
682 	}
683 
684 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
685 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
686 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
687 	ndlp->nlp_class_sup = 0;
688 	if (sp->cls1.classValid)
689 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
690 	if (sp->cls2.classValid)
691 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
692 	if (sp->cls3.classValid)
693 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
694 	if (sp->cls4.classValid)
695 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
696 	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
697 				sp->cmn.bbRcvSizeLsb;
698 
699 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
700 	if (fabric_param_changed) {
701 		/* Reset FDMI attribute masks based on config parameter */
702 		if (phba->cfg_enable_SmartSAN ||
703 		    (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) {
704 			/* Setup appropriate attribute masks */
705 			vport->fdmi_hba_mask = LPFC_FDMI2_HBA_ATTR;
706 			if (phba->cfg_enable_SmartSAN)
707 				vport->fdmi_port_mask = LPFC_FDMI2_SMART_ATTR;
708 			else
709 				vport->fdmi_port_mask = LPFC_FDMI2_PORT_ATTR;
710 		} else {
711 			vport->fdmi_hba_mask = 0;
712 			vport->fdmi_port_mask = 0;
713 		}
714 
715 	}
716 	memcpy(&vport->fabric_portname, &sp->portName,
717 			sizeof(struct lpfc_name));
718 	memcpy(&vport->fabric_nodename, &sp->nodeName,
719 			sizeof(struct lpfc_name));
720 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
721 
722 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
723 		if (sp->cmn.response_multiple_NPort) {
724 			lpfc_printf_vlog(vport, KERN_WARNING,
725 					 LOG_ELS | LOG_VPORT,
726 					 "1816 FLOGI NPIV supported, "
727 					 "response data 0x%x\n",
728 					 sp->cmn.response_multiple_NPort);
729 			spin_lock_irq(&phba->hbalock);
730 			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
731 			spin_unlock_irq(&phba->hbalock);
732 		} else {
733 			/* Because we asked f/w for NPIV it still expects us
734 			to call reg_vnpid atleast for the physcial host */
735 			lpfc_printf_vlog(vport, KERN_WARNING,
736 					 LOG_ELS | LOG_VPORT,
737 					 "1817 Fabric does not support NPIV "
738 					 "- configuring single port mode.\n");
739 			spin_lock_irq(&phba->hbalock);
740 			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
741 			spin_unlock_irq(&phba->hbalock);
742 		}
743 	}
744 
745 	/*
746 	 * For FC we need to do some special processing because of the SLI
747 	 * Port's default settings of the Common Service Parameters.
748 	 */
749 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
750 	    (phba->sli4_hba.lnk_info.lnk_tp == LPFC_LNK_TYPE_FC)) {
751 		/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
752 		if (fabric_param_changed)
753 			lpfc_unregister_fcf_prep(phba);
754 
755 		/* This should just update the VFI CSPs*/
756 		if (vport->fc_flag & FC_VFI_REGISTERED)
757 			lpfc_issue_reg_vfi(vport);
758 	}
759 
760 	if (fabric_param_changed &&
761 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
762 
763 		/* If our NportID changed, we need to ensure all
764 		 * remaining NPORTs get unreg_login'ed.
765 		 */
766 		list_for_each_entry_safe(np, next_np,
767 					&vport->fc_nodes, nlp_listp) {
768 			if (!NLP_CHK_NODE_ACT(np))
769 				continue;
770 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
771 				   !(np->nlp_flag & NLP_NPR_ADISC))
772 				continue;
773 			spin_lock_irq(shost->host_lock);
774 			np->nlp_flag &= ~NLP_NPR_ADISC;
775 			spin_unlock_irq(shost->host_lock);
776 			lpfc_unreg_rpi(vport, np);
777 		}
778 		lpfc_cleanup_pending_mbox(vport);
779 
780 		if (phba->sli_rev == LPFC_SLI_REV4) {
781 			lpfc_sli4_unreg_all_rpis(vport);
782 			lpfc_mbx_unreg_vpi(vport);
783 			spin_lock_irq(shost->host_lock);
784 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
785 			spin_unlock_irq(shost->host_lock);
786 		}
787 
788 		/*
789 		 * For SLI3 and SLI4, the VPI needs to be reregistered in
790 		 * response to this fabric parameter change event.
791 		 */
792 		spin_lock_irq(shost->host_lock);
793 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
794 		spin_unlock_irq(shost->host_lock);
795 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
796 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
797 			/*
798 			 * Driver needs to re-reg VPI in order for f/w
799 			 * to update the MAC address.
800 			 */
801 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
802 			lpfc_register_new_vport(phba, vport, ndlp);
803 			return 0;
804 	}
805 
806 	if (phba->sli_rev < LPFC_SLI_REV4) {
807 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
808 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
809 		    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
810 			lpfc_register_new_vport(phba, vport, ndlp);
811 		else
812 			lpfc_issue_fabric_reglogin(vport);
813 	} else {
814 		ndlp->nlp_type |= NLP_FABRIC;
815 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
816 		if ((!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) &&
817 			(vport->vpi_state & LPFC_VPI_REGISTERED)) {
818 			lpfc_start_fdiscs(phba);
819 			lpfc_do_scr_ns_plogi(phba, vport);
820 		} else if (vport->fc_flag & FC_VFI_REGISTERED)
821 			lpfc_issue_init_vpi(vport);
822 		else {
823 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
824 					"3135 Need register VFI: (x%x/%x)\n",
825 					vport->fc_prevDID, vport->fc_myDID);
826 			lpfc_issue_reg_vfi(vport);
827 		}
828 	}
829 	return 0;
830 }
831 
832 /**
833  * lpfc_cmpl_els_flogi_nport - Completion function for flogi to an N_Port
834  * @vport: pointer to a host virtual N_Port data structure.
835  * @ndlp: pointer to a node-list data structure.
836  * @sp: pointer to service parameter data structure.
837  *
838  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
839  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
840  * in a point-to-point topology. First, the @vport's N_Port Name is compared
841  * with the received N_Port Name: if the @vport's N_Port Name is greater than
842  * the received N_Port Name lexicographically, this node shall assign local
843  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
844  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
845  * this node shall just wait for the remote node to issue PLOGI and assign
846  * N_Port IDs.
847  *
848  * Return code
849  *   0 - Success
850  *   -ENXIO - Fail
851  **/
852 static int
lpfc_cmpl_els_flogi_nport(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct serv_parm * sp)853 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
854 			  struct serv_parm *sp)
855 {
856 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
857 	struct lpfc_hba  *phba = vport->phba;
858 	LPFC_MBOXQ_t *mbox;
859 	int rc;
860 
861 	spin_lock_irq(shost->host_lock);
862 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
863 	vport->fc_flag |= FC_PT2PT;
864 	spin_unlock_irq(shost->host_lock);
865 
866 	/* If we are pt2pt with another NPort, force NPIV off! */
867 	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
868 
869 	/* If physical FC port changed, unreg VFI and ALL VPIs / RPIs */
870 	if ((phba->sli_rev == LPFC_SLI_REV4) && phba->fc_topology_changed) {
871 		lpfc_unregister_fcf_prep(phba);
872 
873 		spin_lock_irq(shost->host_lock);
874 		vport->fc_flag &= ~FC_VFI_REGISTERED;
875 		spin_unlock_irq(shost->host_lock);
876 		phba->fc_topology_changed = 0;
877 	}
878 
879 	rc = memcmp(&vport->fc_portname, &sp->portName,
880 		    sizeof(vport->fc_portname));
881 
882 	if (rc >= 0) {
883 		/* This side will initiate the PLOGI */
884 		spin_lock_irq(shost->host_lock);
885 		vport->fc_flag |= FC_PT2PT_PLOGI;
886 		spin_unlock_irq(shost->host_lock);
887 
888 		/*
889 		 * N_Port ID cannot be 0, set our Id to LocalID
890 		 * the other side will be RemoteID.
891 		 */
892 
893 		/* not equal */
894 		if (rc)
895 			vport->fc_myDID = PT2PT_LocalID;
896 
897 		/* Decrement ndlp reference count indicating that ndlp can be
898 		 * safely released when other references to it are done.
899 		 */
900 		lpfc_nlp_put(ndlp);
901 
902 		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
903 		if (!ndlp) {
904 			/*
905 			 * Cannot find existing Fabric ndlp, so allocate a
906 			 * new one
907 			 */
908 			ndlp = lpfc_nlp_init(vport, PT2PT_RemoteID);
909 			if (!ndlp)
910 				goto fail;
911 		} else if (!NLP_CHK_NODE_ACT(ndlp)) {
912 			ndlp = lpfc_enable_node(vport, ndlp,
913 						NLP_STE_UNUSED_NODE);
914 			if(!ndlp)
915 				goto fail;
916 		}
917 
918 		memcpy(&ndlp->nlp_portname, &sp->portName,
919 		       sizeof(struct lpfc_name));
920 		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
921 		       sizeof(struct lpfc_name));
922 		/* Set state will put ndlp onto node list if not already done */
923 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
924 		spin_lock_irq(shost->host_lock);
925 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
926 		spin_unlock_irq(shost->host_lock);
927 
928 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
929 		if (!mbox)
930 			goto fail;
931 
932 		lpfc_config_link(phba, mbox);
933 
934 		mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
935 		mbox->vport = vport;
936 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
937 		if (rc == MBX_NOT_FINISHED) {
938 			mempool_free(mbox, phba->mbox_mem_pool);
939 			goto fail;
940 		}
941 	} else {
942 		/* This side will wait for the PLOGI, decrement ndlp reference
943 		 * count indicating that ndlp can be released when other
944 		 * references to it are done.
945 		 */
946 		lpfc_nlp_put(ndlp);
947 
948 		/* Start discovery - this should just do CLEAR_LA */
949 		lpfc_disc_start(vport);
950 	}
951 
952 	return 0;
953 fail:
954 	return -ENXIO;
955 }
956 
957 /**
958  * lpfc_cmpl_els_flogi - Completion callback function for flogi
959  * @phba: pointer to lpfc hba data structure.
960  * @cmdiocb: pointer to lpfc command iocb data structure.
961  * @rspiocb: pointer to lpfc response iocb data structure.
962  *
963  * This routine is the top-level completion callback function for issuing
964  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
965  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
966  * retry has been made (either immediately or delayed with lpfc_els_retry()
967  * returning 1), the command IOCB will be released and function returned.
968  * If the retry attempt has been given up (possibly reach the maximum
969  * number of retries), one additional decrement of ndlp reference shall be
970  * invoked before going out after releasing the command IOCB. This will
971  * actually release the remote node (Note, lpfc_els_free_iocb() will also
972  * invoke one decrement of ndlp reference count). If no error reported in
973  * the IOCB status, the command Port ID field is used to determine whether
974  * this is a point-to-point topology or a fabric topology: if the Port ID
975  * field is assigned, it is a fabric topology; otherwise, it is a
976  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
977  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
978  * specific topology completion conditions.
979  **/
980 static void
lpfc_cmpl_els_flogi(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)981 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
982 		    struct lpfc_iocbq *rspiocb)
983 {
984 	struct lpfc_vport *vport = cmdiocb->vport;
985 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
986 	IOCB_t *irsp = &rspiocb->iocb;
987 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
988 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
989 	struct serv_parm *sp;
990 	uint16_t fcf_index;
991 	int rc;
992 
993 	/* Check to see if link went down during discovery */
994 	if (lpfc_els_chk_latt(vport)) {
995 		/* One additional decrement on node reference count to
996 		 * trigger the release of the node
997 		 */
998 		lpfc_nlp_put(ndlp);
999 		goto out;
1000 	}
1001 
1002 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1003 		"FLOGI cmpl:      status:x%x/x%x state:x%x",
1004 		irsp->ulpStatus, irsp->un.ulpWord[4],
1005 		vport->port_state);
1006 
1007 	if (irsp->ulpStatus) {
1008 		/*
1009 		 * In case of FIP mode, perform roundrobin FCF failover
1010 		 * due to new FCF discovery
1011 		 */
1012 		if ((phba->hba_flag & HBA_FIP_SUPPORT) &&
1013 		    (phba->fcf.fcf_flag & FCF_DISCOVERY)) {
1014 			if (phba->link_state < LPFC_LINK_UP)
1015 				goto stop_rr_fcf_flogi;
1016 			if ((phba->fcoe_cvl_eventtag_attn ==
1017 			     phba->fcoe_cvl_eventtag) &&
1018 			    (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1019 			    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1020 			    IOERR_SLI_ABORTED))
1021 				goto stop_rr_fcf_flogi;
1022 			else
1023 				phba->fcoe_cvl_eventtag_attn =
1024 					phba->fcoe_cvl_eventtag;
1025 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1026 					"2611 FLOGI failed on FCF (x%x), "
1027 					"status:x%x/x%x, tmo:x%x, perform "
1028 					"roundrobin FCF failover\n",
1029 					phba->fcf.current_rec.fcf_indx,
1030 					irsp->ulpStatus, irsp->un.ulpWord[4],
1031 					irsp->ulpTimeout);
1032 			lpfc_sli4_set_fcf_flogi_fail(phba,
1033 					phba->fcf.current_rec.fcf_indx);
1034 			fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
1035 			rc = lpfc_sli4_fcf_rr_next_proc(vport, fcf_index);
1036 			if (rc)
1037 				goto out;
1038 		}
1039 
1040 stop_rr_fcf_flogi:
1041 		/* FLOGI failure */
1042 		if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1043 		      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1044 					IOERR_LOOP_OPEN_FAILURE)))
1045 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1046 					 "2858 FLOGI failure Status:x%x/x%x TMO"
1047 					 ":x%x Data x%x x%x\n",
1048 					 irsp->ulpStatus, irsp->un.ulpWord[4],
1049 					 irsp->ulpTimeout, phba->hba_flag,
1050 					 phba->fcf.fcf_flag);
1051 
1052 		/* Check for retry */
1053 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1054 			goto out;
1055 
1056 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_TRACE_EVENT,
1057 				 "0150 FLOGI failure Status:x%x/x%x "
1058 				 "xri x%x TMO:x%x\n",
1059 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1060 				 cmdiocb->sli4_xritag, irsp->ulpTimeout);
1061 
1062 		/* If this is not a loop open failure, bail out */
1063 		if (!(irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
1064 		      ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
1065 					IOERR_LOOP_OPEN_FAILURE)))
1066 			goto flogifail;
1067 
1068 		/* FLOGI failed, so there is no fabric */
1069 		spin_lock_irq(shost->host_lock);
1070 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
1071 		spin_unlock_irq(shost->host_lock);
1072 
1073 		/* If private loop, then allow max outstanding els to be
1074 		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
1075 		 * alpa map would take too long otherwise.
1076 		 */
1077 		if (phba->alpa_map[0] == 0)
1078 			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
1079 		if ((phba->sli_rev == LPFC_SLI_REV4) &&
1080 		    (!(vport->fc_flag & FC_VFI_REGISTERED) ||
1081 		     (vport->fc_prevDID != vport->fc_myDID) ||
1082 			phba->fc_topology_changed)) {
1083 			if (vport->fc_flag & FC_VFI_REGISTERED) {
1084 				if (phba->fc_topology_changed) {
1085 					lpfc_unregister_fcf_prep(phba);
1086 					spin_lock_irq(shost->host_lock);
1087 					vport->fc_flag &= ~FC_VFI_REGISTERED;
1088 					spin_unlock_irq(shost->host_lock);
1089 					phba->fc_topology_changed = 0;
1090 				} else {
1091 					lpfc_sli4_unreg_all_rpis(vport);
1092 				}
1093 			}
1094 
1095 			/* Do not register VFI if the driver aborted FLOGI */
1096 			if (!lpfc_error_lost_link(irsp))
1097 				lpfc_issue_reg_vfi(vport);
1098 			lpfc_nlp_put(ndlp);
1099 			goto out;
1100 		}
1101 		goto flogifail;
1102 	}
1103 	spin_lock_irq(shost->host_lock);
1104 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
1105 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
1106 	spin_unlock_irq(shost->host_lock);
1107 
1108 	/*
1109 	 * The FLogI succeeded.  Sync the data for the CPU before
1110 	 * accessing it.
1111 	 */
1112 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
1113 	if (!prsp)
1114 		goto out;
1115 	sp = prsp->virt + sizeof(uint32_t);
1116 
1117 	/* FLOGI completes successfully */
1118 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1119 			 "0101 FLOGI completes successfully, I/O tag:x%x, "
1120 			 "xri x%x Data: x%x x%x x%x x%x x%x %x\n",
1121 			 cmdiocb->iotag, cmdiocb->sli4_xritag,
1122 			 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
1123 			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution,
1124 			 vport->port_state, vport->fc_flag);
1125 
1126 	if (vport->port_state == LPFC_FLOGI) {
1127 		/*
1128 		 * If Common Service Parameters indicate Nport
1129 		 * we are point to point, if Fport we are Fabric.
1130 		 */
1131 		if (sp->cmn.fPort)
1132 			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
1133 		else if (!(phba->hba_flag & HBA_FCOE_MODE))
1134 			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
1135 		else {
1136 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1137 				"2831 FLOGI response with cleared Fabric "
1138 				"bit fcf_index 0x%x "
1139 				"Switch Name %02x%02x%02x%02x%02x%02x%02x%02x "
1140 				"Fabric Name "
1141 				"%02x%02x%02x%02x%02x%02x%02x%02x\n",
1142 				phba->fcf.current_rec.fcf_indx,
1143 				phba->fcf.current_rec.switch_name[0],
1144 				phba->fcf.current_rec.switch_name[1],
1145 				phba->fcf.current_rec.switch_name[2],
1146 				phba->fcf.current_rec.switch_name[3],
1147 				phba->fcf.current_rec.switch_name[4],
1148 				phba->fcf.current_rec.switch_name[5],
1149 				phba->fcf.current_rec.switch_name[6],
1150 				phba->fcf.current_rec.switch_name[7],
1151 				phba->fcf.current_rec.fabric_name[0],
1152 				phba->fcf.current_rec.fabric_name[1],
1153 				phba->fcf.current_rec.fabric_name[2],
1154 				phba->fcf.current_rec.fabric_name[3],
1155 				phba->fcf.current_rec.fabric_name[4],
1156 				phba->fcf.current_rec.fabric_name[5],
1157 				phba->fcf.current_rec.fabric_name[6],
1158 				phba->fcf.current_rec.fabric_name[7]);
1159 			lpfc_nlp_put(ndlp);
1160 			spin_lock_irq(&phba->hbalock);
1161 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1162 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1163 			spin_unlock_irq(&phba->hbalock);
1164 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1165 			goto out;
1166 		}
1167 		if (!rc) {
1168 			/* Mark the FCF discovery process done */
1169 			if (phba->hba_flag & HBA_FIP_SUPPORT)
1170 				lpfc_printf_vlog(vport, KERN_INFO, LOG_FIP |
1171 						LOG_ELS,
1172 						"2769 FLOGI to FCF (x%x) "
1173 						"completed successfully\n",
1174 						phba->fcf.current_rec.fcf_indx);
1175 			spin_lock_irq(&phba->hbalock);
1176 			phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1177 			phba->hba_flag &= ~(FCF_RR_INPROG | HBA_DEVLOSS_TMO);
1178 			spin_unlock_irq(&phba->hbalock);
1179 			phba->fcf.fcf_redisc_attempted = 0; /* reset */
1180 			goto out;
1181 		}
1182 	} else if (vport->port_state > LPFC_FLOGI &&
1183 		   vport->fc_flag & FC_PT2PT) {
1184 		/*
1185 		 * In a p2p topology, it is possible that discovery has
1186 		 * already progressed, and this completion can be ignored.
1187 		 * Recheck the indicated topology.
1188 		 */
1189 		if (!sp->cmn.fPort)
1190 			goto out;
1191 	}
1192 
1193 flogifail:
1194 	spin_lock_irq(&phba->hbalock);
1195 	phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
1196 	spin_unlock_irq(&phba->hbalock);
1197 
1198 	lpfc_nlp_put(ndlp);
1199 
1200 	if (!lpfc_error_lost_link(irsp)) {
1201 		/* FLOGI failed, so just use loop map to make discovery list */
1202 		lpfc_disc_list_loopmap(vport);
1203 
1204 		/* Start discovery */
1205 		lpfc_disc_start(vport);
1206 	} else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
1207 			(((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1208 			 IOERR_SLI_ABORTED) &&
1209 			((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
1210 			 IOERR_SLI_DOWN))) &&
1211 			(phba->link_state != LPFC_CLEAR_LA)) {
1212 		/* If FLOGI failed enable link interrupt. */
1213 		lpfc_issue_clear_la(phba, vport);
1214 	}
1215 out:
1216 	lpfc_els_free_iocb(phba, cmdiocb);
1217 }
1218 
1219 /**
1220  * lpfc_cmpl_els_link_down - Completion callback function for ELS command
1221  *                           aborted during a link down
1222  * @phba: pointer to lpfc hba data structure.
1223  * @cmdiocb: pointer to lpfc command iocb data structure.
1224  * @rspiocb: pointer to lpfc response iocb data structure.
1225  *
1226  */
1227 static void
lpfc_cmpl_els_link_down(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1228 lpfc_cmpl_els_link_down(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1229 			struct lpfc_iocbq *rspiocb)
1230 {
1231 	IOCB_t *irsp;
1232 	uint32_t *pcmd;
1233 	uint32_t cmd;
1234 
1235 	pcmd = (uint32_t *)(((struct lpfc_dmabuf *)cmdiocb->context2)->virt);
1236 	cmd = *pcmd;
1237 	irsp = &rspiocb->iocb;
1238 
1239 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1240 			"6445 ELS completes after LINK_DOWN: "
1241 			" Status %x/%x cmd x%x flg x%x\n",
1242 			irsp->ulpStatus, irsp->un.ulpWord[4], cmd,
1243 			cmdiocb->iocb_flag);
1244 
1245 	if (cmdiocb->iocb_flag & LPFC_IO_FABRIC) {
1246 		cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
1247 		atomic_dec(&phba->fabric_iocb_count);
1248 	}
1249 	lpfc_els_free_iocb(phba, cmdiocb);
1250 }
1251 
1252 /**
1253  * lpfc_issue_els_flogi - Issue an flogi iocb command for a vport
1254  * @vport: pointer to a host virtual N_Port data structure.
1255  * @ndlp: pointer to a node-list data structure.
1256  * @retry: number of retries to the command IOCB.
1257  *
1258  * This routine issues a Fabric Login (FLOGI) Request ELS command
1259  * for a @vport. The initiator service parameters are put into the payload
1260  * of the FLOGI Request IOCB and the top-level callback function pointer
1261  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
1262  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
1263  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
1264  *
1265  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1266  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1267  * will be stored into the context1 field of the IOCB for the completion
1268  * callback function to the FLOGI ELS command.
1269  *
1270  * Return code
1271  *   0 - successfully issued flogi iocb for @vport
1272  *   1 - failed to issue flogi iocb for @vport
1273  **/
1274 static int
lpfc_issue_els_flogi(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)1275 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1276 		     uint8_t retry)
1277 {
1278 	struct lpfc_hba  *phba = vport->phba;
1279 	struct serv_parm *sp;
1280 	IOCB_t *icmd;
1281 	struct lpfc_iocbq *elsiocb;
1282 	struct lpfc_iocbq defer_flogi_acc;
1283 	uint8_t *pcmd;
1284 	uint16_t cmdsize;
1285 	uint32_t tmo, did;
1286 	int rc;
1287 
1288 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1289 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1290 				     ndlp->nlp_DID, ELS_CMD_FLOGI);
1291 
1292 	if (!elsiocb)
1293 		return 1;
1294 
1295 	icmd = &elsiocb->iocb;
1296 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1297 
1298 	/* For FLOGI request, remainder of payload is service parameters */
1299 	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
1300 	pcmd += sizeof(uint32_t);
1301 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1302 	sp = (struct serv_parm *) pcmd;
1303 
1304 	/* Setup CSPs accordingly for Fabric */
1305 	sp->cmn.e_d_tov = 0;
1306 	sp->cmn.w2.r_a_tov = 0;
1307 	sp->cmn.virtual_fabric_support = 0;
1308 	sp->cls1.classValid = 0;
1309 	if (sp->cmn.fcphLow < FC_PH3)
1310 		sp->cmn.fcphLow = FC_PH3;
1311 	if (sp->cmn.fcphHigh < FC_PH3)
1312 		sp->cmn.fcphHigh = FC_PH3;
1313 
1314 	if  (phba->sli_rev == LPFC_SLI_REV4) {
1315 		if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
1316 		    LPFC_SLI_INTF_IF_TYPE_0) {
1317 			elsiocb->iocb.ulpCt_h = ((SLI4_CT_FCFI >> 1) & 1);
1318 			elsiocb->iocb.ulpCt_l = (SLI4_CT_FCFI & 1);
1319 			/* FLOGI needs to be 3 for WQE FCFI */
1320 			/* Set the fcfi to the fcfi we registered with */
1321 			elsiocb->iocb.ulpContext = phba->fcf.fcfi;
1322 		}
1323 		/* Can't do SLI4 class2 without support sequence coalescing */
1324 		sp->cls2.classValid = 0;
1325 		sp->cls2.seqDelivery = 0;
1326 	} else {
1327 		/* Historical, setting sequential-delivery bit for SLI3 */
1328 		sp->cls2.seqDelivery = (sp->cls2.classValid) ? 1 : 0;
1329 		sp->cls3.seqDelivery = (sp->cls3.classValid) ? 1 : 0;
1330 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
1331 			sp->cmn.request_multiple_Nport = 1;
1332 			/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
1333 			icmd->ulpCt_h = 1;
1334 			icmd->ulpCt_l = 0;
1335 		} else
1336 			sp->cmn.request_multiple_Nport = 0;
1337 	}
1338 
1339 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
1340 		icmd->un.elsreq64.myID = 0;
1341 		icmd->un.elsreq64.fl = 1;
1342 	}
1343 
1344 	tmo = phba->fc_ratov;
1345 	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
1346 	lpfc_set_disctmo(vport);
1347 	phba->fc_ratov = tmo;
1348 
1349 	phba->fc_stat.elsXmitFLOGI++;
1350 	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
1351 
1352 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1353 		"Issue FLOGI:     opt:x%x",
1354 		phba->sli3_options, 0, 0);
1355 
1356 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
1357 
1358 	phba->hba_flag |= HBA_FLOGI_ISSUED;
1359 
1360 	/* Check for a deferred FLOGI ACC condition */
1361 	if (phba->defer_flogi_acc_flag) {
1362 		did = vport->fc_myDID;
1363 		vport->fc_myDID = Fabric_DID;
1364 
1365 		memset(&defer_flogi_acc, 0, sizeof(struct lpfc_iocbq));
1366 
1367 		defer_flogi_acc.iocb.ulpContext = phba->defer_flogi_acc_rx_id;
1368 		defer_flogi_acc.iocb.unsli3.rcvsli3.ox_id =
1369 						phba->defer_flogi_acc_ox_id;
1370 
1371 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1372 				 "3354 Xmit deferred FLOGI ACC: rx_id: x%x,"
1373 				 " ox_id: x%x, hba_flag x%x\n",
1374 				 phba->defer_flogi_acc_rx_id,
1375 				 phba->defer_flogi_acc_ox_id, phba->hba_flag);
1376 
1377 		/* Send deferred FLOGI ACC */
1378 		lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, &defer_flogi_acc,
1379 				 ndlp, NULL);
1380 
1381 		phba->defer_flogi_acc_flag = false;
1382 
1383 		vport->fc_myDID = did;
1384 	}
1385 
1386 	if (rc == IOCB_ERROR) {
1387 		lpfc_els_free_iocb(phba, elsiocb);
1388 		return 1;
1389 	}
1390 	return 0;
1391 }
1392 
1393 /**
1394  * lpfc_els_abort_flogi - Abort all outstanding flogi iocbs
1395  * @phba: pointer to lpfc hba data structure.
1396  *
1397  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
1398  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
1399  * list and issues an abort IOCB commond on each outstanding IOCB that
1400  * contains a active Fabric_DID ndlp. Note that this function is to issue
1401  * the abort IOCB command on all the outstanding IOCBs, thus when this
1402  * function returns, it does not guarantee all the IOCBs are actually aborted.
1403  *
1404  * Return code
1405  *   0 - Successfully issued abort iocb on all outstanding flogis (Always 0)
1406  **/
1407 int
lpfc_els_abort_flogi(struct lpfc_hba * phba)1408 lpfc_els_abort_flogi(struct lpfc_hba *phba)
1409 {
1410 	struct lpfc_sli_ring *pring;
1411 	struct lpfc_iocbq *iocb, *next_iocb;
1412 	struct lpfc_nodelist *ndlp;
1413 	IOCB_t *icmd;
1414 
1415 	/* Abort outstanding I/O on NPort <nlp_DID> */
1416 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
1417 			"0201 Abort outstanding I/O on NPort x%x\n",
1418 			Fabric_DID);
1419 
1420 	pring = lpfc_phba_elsring(phba);
1421 	if (unlikely(!pring))
1422 		return -EIO;
1423 
1424 	/*
1425 	 * Check the txcmplq for an iocb that matches the nport the driver is
1426 	 * searching for.
1427 	 */
1428 	spin_lock_irq(&phba->hbalock);
1429 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1430 		icmd = &iocb->iocb;
1431 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
1432 			ndlp = (struct lpfc_nodelist *)(iocb->context1);
1433 			if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1434 			    (ndlp->nlp_DID == Fabric_DID))
1435 				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1436 		}
1437 	}
1438 	spin_unlock_irq(&phba->hbalock);
1439 
1440 	return 0;
1441 }
1442 
1443 /**
1444  * lpfc_initial_flogi - Issue an initial fabric login for a vport
1445  * @vport: pointer to a host virtual N_Port data structure.
1446  *
1447  * This routine issues an initial Fabric Login (FLOGI) for the @vport
1448  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1449  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1450  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1451  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
1452  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
1453  * @vport.
1454  *
1455  * Return code
1456  *   0 - failed to issue initial flogi for @vport
1457  *   1 - successfully issued initial flogi for @vport
1458  **/
1459 int
lpfc_initial_flogi(struct lpfc_vport * vport)1460 lpfc_initial_flogi(struct lpfc_vport *vport)
1461 {
1462 	struct lpfc_nodelist *ndlp;
1463 
1464 	vport->port_state = LPFC_FLOGI;
1465 	lpfc_set_disctmo(vport);
1466 
1467 	/* First look for the Fabric ndlp */
1468 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1469 	if (!ndlp) {
1470 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1471 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1472 		if (!ndlp)
1473 			return 0;
1474 		/* Set the node type */
1475 		ndlp->nlp_type |= NLP_FABRIC;
1476 		/* Put ndlp onto node list */
1477 		lpfc_enqueue_node(vport, ndlp);
1478 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
1479 		/* re-setup ndlp without removing from node list */
1480 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1481 		if (!ndlp)
1482 			return 0;
1483 	}
1484 
1485 	if (lpfc_issue_els_flogi(vport, ndlp, 0)) {
1486 		/* This decrement of reference count to node shall kick off
1487 		 * the release of the node.
1488 		 */
1489 		lpfc_nlp_put(ndlp);
1490 		return 0;
1491 	}
1492 	return 1;
1493 }
1494 
1495 /**
1496  * lpfc_initial_fdisc - Issue an initial fabric discovery for a vport
1497  * @vport: pointer to a host virtual N_Port data structure.
1498  *
1499  * This routine issues an initial Fabric Discover (FDISC) for the @vport
1500  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
1501  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
1502  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
1503  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
1504  * is then invoked with the @vport and the ndlp to perform the FDISC for the
1505  * @vport.
1506  *
1507  * Return code
1508  *   0 - failed to issue initial fdisc for @vport
1509  *   1 - successfully issued initial fdisc for @vport
1510  **/
1511 int
lpfc_initial_fdisc(struct lpfc_vport * vport)1512 lpfc_initial_fdisc(struct lpfc_vport *vport)
1513 {
1514 	struct lpfc_nodelist *ndlp;
1515 
1516 	/* First look for the Fabric ndlp */
1517 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
1518 	if (!ndlp) {
1519 		/* Cannot find existing Fabric ndlp, so allocate a new one */
1520 		ndlp = lpfc_nlp_init(vport, Fabric_DID);
1521 		if (!ndlp)
1522 			return 0;
1523 		/* Put ndlp onto node list */
1524 		lpfc_enqueue_node(vport, ndlp);
1525 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
1526 		/* re-setup ndlp without removing from node list */
1527 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
1528 		if (!ndlp)
1529 			return 0;
1530 	}
1531 
1532 	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
1533 		/* decrement node reference count to trigger the release of
1534 		 * the node.
1535 		 */
1536 		lpfc_nlp_put(ndlp);
1537 		return 0;
1538 	}
1539 	return 1;
1540 }
1541 
1542 /**
1543  * lpfc_more_plogi - Check and issue remaining plogis for a vport
1544  * @vport: pointer to a host virtual N_Port data structure.
1545  *
1546  * This routine checks whether there are more remaining Port Logins
1547  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1548  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1549  * to issue ELS PLOGIs up to the configured discover threads with the
1550  * @vport (@vport->cfg_discovery_threads). The function also decrement
1551  * the @vport's num_disc_node by 1 if it is not already 0.
1552  **/
1553 void
lpfc_more_plogi(struct lpfc_vport * vport)1554 lpfc_more_plogi(struct lpfc_vport *vport)
1555 {
1556 	if (vport->num_disc_nodes)
1557 		vport->num_disc_nodes--;
1558 
1559 	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
1560 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1561 			 "0232 Continue discovery with %d PLOGIs to go "
1562 			 "Data: x%x x%x x%x\n",
1563 			 vport->num_disc_nodes, vport->fc_plogi_cnt,
1564 			 vport->fc_flag, vport->port_state);
1565 	/* Check to see if there are more PLOGIs to be sent */
1566 	if (vport->fc_flag & FC_NLP_MORE)
1567 		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
1568 		lpfc_els_disc_plogi(vport);
1569 
1570 	return;
1571 }
1572 
1573 /**
1574  * lpfc_plogi_confirm_nport - Confirm pologi wwpn matches stored ndlp
1575  * @phba: pointer to lpfc hba data structure.
1576  * @prsp: pointer to response IOCB payload.
1577  * @ndlp: pointer to a node-list data structure.
1578  *
1579  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1580  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1581  * The following cases are considered N_Port confirmed:
1582  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1583  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1584  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1585  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1586  * 1) if there is a node on vport list other than the @ndlp with the same
1587  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1588  * on that node to release the RPI associated with the node; 2) if there is
1589  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1590  * into, a new node shall be allocated (or activated). In either case, the
1591  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1592  * be released and the new_ndlp shall be put on to the vport node list and
1593  * its pointer returned as the confirmed node.
1594  *
1595  * Note that before the @ndlp got "released", the keepDID from not-matching
1596  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1597  * of the @ndlp. This is because the release of @ndlp is actually to put it
1598  * into an inactive state on the vport node list and the vport node list
1599  * management algorithm does not allow two node with a same DID.
1600  *
1601  * Return code
1602  *   pointer to the PLOGI N_Port @ndlp
1603  **/
1604 static struct lpfc_nodelist *
lpfc_plogi_confirm_nport(struct lpfc_hba * phba,uint32_t * prsp,struct lpfc_nodelist * ndlp)1605 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1606 			 struct lpfc_nodelist *ndlp)
1607 {
1608 	struct lpfc_vport *vport = ndlp->vport;
1609 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1610 	struct lpfc_nodelist *new_ndlp;
1611 	struct lpfc_rport_data *rdata;
1612 	struct fc_rport *rport;
1613 	struct serv_parm *sp;
1614 	uint8_t  name[sizeof(struct lpfc_name)];
1615 	uint32_t rc, keepDID = 0, keep_nlp_flag = 0;
1616 	uint32_t keep_new_nlp_flag = 0;
1617 	uint16_t keep_nlp_state;
1618 	u32 keep_nlp_fc4_type = 0;
1619 	struct lpfc_nvme_rport *keep_nrport = NULL;
1620 	int  put_node;
1621 	int  put_rport;
1622 	unsigned long *active_rrqs_xri_bitmap = NULL;
1623 
1624 	/* Fabric nodes can have the same WWPN so we don't bother searching
1625 	 * by WWPN.  Just return the ndlp that was given to us.
1626 	 */
1627 	if (ndlp->nlp_type & NLP_FABRIC)
1628 		return ndlp;
1629 
1630 	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1631 	memset(name, 0, sizeof(struct lpfc_name));
1632 
1633 	/* Now we find out if the NPort we are logging into, matches the WWPN
1634 	 * we have for that ndlp. If not, we have some work to do.
1635 	 */
1636 	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1637 
1638 	/* return immediately if the WWPN matches ndlp */
1639 	if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1640 		return ndlp;
1641 
1642 	if (phba->sli_rev == LPFC_SLI_REV4) {
1643 		active_rrqs_xri_bitmap = mempool_alloc(phba->active_rrq_pool,
1644 						       GFP_KERNEL);
1645 		if (active_rrqs_xri_bitmap)
1646 			memset(active_rrqs_xri_bitmap, 0,
1647 			       phba->cfg_rrq_xri_bitmap_sz);
1648 	}
1649 
1650 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1651 			 "3178 PLOGI confirm: ndlp x%x x%x x%x: "
1652 			 "new_ndlp x%x x%x x%x\n",
1653 			 ndlp->nlp_DID, ndlp->nlp_flag,  ndlp->nlp_fc4_type,
1654 			 (new_ndlp ? new_ndlp->nlp_DID : 0),
1655 			 (new_ndlp ? new_ndlp->nlp_flag : 0),
1656 			 (new_ndlp ? new_ndlp->nlp_fc4_type : 0));
1657 
1658 	if (!new_ndlp) {
1659 		rc = memcmp(&ndlp->nlp_portname, name,
1660 			    sizeof(struct lpfc_name));
1661 		if (!rc) {
1662 			if (active_rrqs_xri_bitmap)
1663 				mempool_free(active_rrqs_xri_bitmap,
1664 					     phba->active_rrq_pool);
1665 			return ndlp;
1666 		}
1667 		new_ndlp = lpfc_nlp_init(vport, ndlp->nlp_DID);
1668 		if (!new_ndlp) {
1669 			if (active_rrqs_xri_bitmap)
1670 				mempool_free(active_rrqs_xri_bitmap,
1671 					     phba->active_rrq_pool);
1672 			return ndlp;
1673 		}
1674 	} else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1675 		rc = memcmp(&ndlp->nlp_portname, name,
1676 			    sizeof(struct lpfc_name));
1677 		if (!rc) {
1678 			if (active_rrqs_xri_bitmap)
1679 				mempool_free(active_rrqs_xri_bitmap,
1680 					     phba->active_rrq_pool);
1681 			return ndlp;
1682 		}
1683 		new_ndlp = lpfc_enable_node(vport, new_ndlp,
1684 						NLP_STE_UNUSED_NODE);
1685 		if (!new_ndlp) {
1686 			if (active_rrqs_xri_bitmap)
1687 				mempool_free(active_rrqs_xri_bitmap,
1688 					     phba->active_rrq_pool);
1689 			return ndlp;
1690 		}
1691 		keepDID = new_ndlp->nlp_DID;
1692 		if ((phba->sli_rev == LPFC_SLI_REV4) && active_rrqs_xri_bitmap)
1693 			memcpy(active_rrqs_xri_bitmap,
1694 			       new_ndlp->active_rrqs_xri_bitmap,
1695 			       phba->cfg_rrq_xri_bitmap_sz);
1696 	} else {
1697 		keepDID = new_ndlp->nlp_DID;
1698 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1699 		    active_rrqs_xri_bitmap)
1700 			memcpy(active_rrqs_xri_bitmap,
1701 			       new_ndlp->active_rrqs_xri_bitmap,
1702 			       phba->cfg_rrq_xri_bitmap_sz);
1703 	}
1704 
1705 	/* At this point in this routine, we know new_ndlp will be
1706 	 * returned. however, any previous GID_FTs that were done
1707 	 * would have updated nlp_fc4_type in ndlp, so we must ensure
1708 	 * new_ndlp has the right value.
1709 	 */
1710 	if (vport->fc_flag & FC_FABRIC) {
1711 		keep_nlp_fc4_type = new_ndlp->nlp_fc4_type;
1712 		new_ndlp->nlp_fc4_type = ndlp->nlp_fc4_type;
1713 	}
1714 
1715 	lpfc_unreg_rpi(vport, new_ndlp);
1716 	new_ndlp->nlp_DID = ndlp->nlp_DID;
1717 	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1718 	if (phba->sli_rev == LPFC_SLI_REV4)
1719 		memcpy(new_ndlp->active_rrqs_xri_bitmap,
1720 		       ndlp->active_rrqs_xri_bitmap,
1721 		       phba->cfg_rrq_xri_bitmap_sz);
1722 
1723 	spin_lock_irq(shost->host_lock);
1724 	keep_new_nlp_flag = new_ndlp->nlp_flag;
1725 	keep_nlp_flag = ndlp->nlp_flag;
1726 	new_ndlp->nlp_flag = ndlp->nlp_flag;
1727 
1728 	/* if new_ndlp had NLP_UNREG_INP set, keep it */
1729 	if (keep_new_nlp_flag & NLP_UNREG_INP)
1730 		new_ndlp->nlp_flag |= NLP_UNREG_INP;
1731 	else
1732 		new_ndlp->nlp_flag &= ~NLP_UNREG_INP;
1733 
1734 	/* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
1735 	if (keep_new_nlp_flag & NLP_RPI_REGISTERED)
1736 		new_ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1737 	else
1738 		new_ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1739 
1740 	ndlp->nlp_flag = keep_new_nlp_flag;
1741 
1742 	/* if ndlp had NLP_UNREG_INP set, keep it */
1743 	if (keep_nlp_flag & NLP_UNREG_INP)
1744 		ndlp->nlp_flag |= NLP_UNREG_INP;
1745 	else
1746 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
1747 
1748 	/* if ndlp had NLP_RPI_REGISTERED set, keep it */
1749 	if (keep_nlp_flag & NLP_RPI_REGISTERED)
1750 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
1751 	else
1752 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
1753 
1754 	spin_unlock_irq(shost->host_lock);
1755 
1756 	/* Set nlp_states accordingly */
1757 	keep_nlp_state = new_ndlp->nlp_state;
1758 	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1759 
1760 	/* interchange the nvme remoteport structs */
1761 	keep_nrport = new_ndlp->nrport;
1762 	new_ndlp->nrport = ndlp->nrport;
1763 
1764 	/* Move this back to NPR state */
1765 	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1766 		/* The new_ndlp is replacing ndlp totally, so we need
1767 		 * to put ndlp on UNUSED list and try to free it.
1768 		 */
1769 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1770 			 "3179 PLOGI confirm NEW: %x %x\n",
1771 			 new_ndlp->nlp_DID, keepDID);
1772 
1773 		/* Fix up the rport accordingly */
1774 		rport =  ndlp->rport;
1775 		if (rport) {
1776 			rdata = rport->dd_data;
1777 			if (rdata->pnode == ndlp) {
1778 				/* break the link before dropping the ref */
1779 				ndlp->rport = NULL;
1780 				lpfc_nlp_put(ndlp);
1781 				rdata->pnode = lpfc_nlp_get(new_ndlp);
1782 				new_ndlp->rport = rport;
1783 			}
1784 			new_ndlp->nlp_type = ndlp->nlp_type;
1785 		}
1786 
1787 		/* Fix up the nvme rport */
1788 		if (ndlp->nrport) {
1789 			ndlp->nrport = NULL;
1790 			lpfc_nlp_put(ndlp);
1791 		}
1792 
1793 		/* We shall actually free the ndlp with both nlp_DID and
1794 		 * nlp_portname fields equals 0 to avoid any ndlp on the
1795 		 * nodelist never to be used.
1796 		 */
1797 		if (ndlp->nlp_DID == 0) {
1798 			spin_lock_irq(&phba->ndlp_lock);
1799 			NLP_SET_FREE_REQ(ndlp);
1800 			spin_unlock_irq(&phba->ndlp_lock);
1801 		}
1802 
1803 		/* Two ndlps cannot have the same did on the nodelist.
1804 		 * Note: for this case, ndlp has a NULL WWPN so setting
1805 		 * the nlp_fc4_type isn't required.
1806 		 */
1807 		ndlp->nlp_DID = keepDID;
1808 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1809 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1810 		    active_rrqs_xri_bitmap)
1811 			memcpy(ndlp->active_rrqs_xri_bitmap,
1812 			       active_rrqs_xri_bitmap,
1813 			       phba->cfg_rrq_xri_bitmap_sz);
1814 
1815 		if (!NLP_CHK_NODE_ACT(ndlp))
1816 			lpfc_drop_node(vport, ndlp);
1817 	}
1818 	else {
1819 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1820 			 "3180 PLOGI confirm SWAP: %x %x\n",
1821 			 new_ndlp->nlp_DID, keepDID);
1822 
1823 		lpfc_unreg_rpi(vport, ndlp);
1824 
1825 		/* Two ndlps cannot have the same did and the fc4
1826 		 * type must be transferred because the ndlp is in
1827 		 * flight.
1828 		 */
1829 		ndlp->nlp_DID = keepDID;
1830 		ndlp->nlp_fc4_type = keep_nlp_fc4_type;
1831 
1832 		if (phba->sli_rev == LPFC_SLI_REV4 &&
1833 		    active_rrqs_xri_bitmap)
1834 			memcpy(ndlp->active_rrqs_xri_bitmap,
1835 			       active_rrqs_xri_bitmap,
1836 			       phba->cfg_rrq_xri_bitmap_sz);
1837 
1838 		/* Since we are switching over to the new_ndlp,
1839 		 * reset the old ndlp state
1840 		 */
1841 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
1842 		    (ndlp->nlp_state == NLP_STE_MAPPED_NODE))
1843 			keep_nlp_state = NLP_STE_NPR_NODE;
1844 		lpfc_nlp_set_state(vport, ndlp, keep_nlp_state);
1845 
1846 		/* Previous ndlp no longer active with nvme host transport.
1847 		 * Remove reference from earlier registration unless the
1848 		 * nvme host took care of it.
1849 		 */
1850 		if (ndlp->nrport)
1851 			lpfc_nlp_put(ndlp);
1852 		ndlp->nrport = keep_nrport;
1853 
1854 		/* Fix up the rport accordingly */
1855 		rport = ndlp->rport;
1856 		if (rport) {
1857 			rdata = rport->dd_data;
1858 			put_node = rdata->pnode != NULL;
1859 			put_rport = ndlp->rport != NULL;
1860 			rdata->pnode = NULL;
1861 			ndlp->rport = NULL;
1862 			if (put_node)
1863 				lpfc_nlp_put(ndlp);
1864 			if (put_rport)
1865 				put_device(&rport->dev);
1866 		}
1867 	}
1868 	if (phba->sli_rev == LPFC_SLI_REV4 &&
1869 	    active_rrqs_xri_bitmap)
1870 		mempool_free(active_rrqs_xri_bitmap,
1871 			     phba->active_rrq_pool);
1872 
1873 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS | LOG_NODE,
1874 			 "3173 PLOGI confirm exit: new_ndlp x%x x%x x%x\n",
1875 			 new_ndlp->nlp_DID, new_ndlp->nlp_flag,
1876 			 new_ndlp->nlp_fc4_type);
1877 
1878 	return new_ndlp;
1879 }
1880 
1881 /**
1882  * lpfc_end_rscn - Check and handle more rscn for a vport
1883  * @vport: pointer to a host virtual N_Port data structure.
1884  *
1885  * This routine checks whether more Registration State Change
1886  * Notifications (RSCNs) came in while the discovery state machine was in
1887  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1888  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1889  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1890  * handling the RSCNs.
1891  **/
1892 void
lpfc_end_rscn(struct lpfc_vport * vport)1893 lpfc_end_rscn(struct lpfc_vport *vport)
1894 {
1895 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1896 
1897 	if (vport->fc_flag & FC_RSCN_MODE) {
1898 		/*
1899 		 * Check to see if more RSCNs came in while we were
1900 		 * processing this one.
1901 		 */
1902 		if (vport->fc_rscn_id_cnt ||
1903 		    (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1904 			lpfc_els_handle_rscn(vport);
1905 		else {
1906 			spin_lock_irq(shost->host_lock);
1907 			vport->fc_flag &= ~FC_RSCN_MODE;
1908 			spin_unlock_irq(shost->host_lock);
1909 		}
1910 	}
1911 }
1912 
1913 /**
1914  * lpfc_cmpl_els_rrq - Completion handled for els RRQs.
1915  * @phba: pointer to lpfc hba data structure.
1916  * @cmdiocb: pointer to lpfc command iocb data structure.
1917  * @rspiocb: pointer to lpfc response iocb data structure.
1918  *
1919  * This routine will call the clear rrq function to free the rrq and
1920  * clear the xri's bit in the ndlp's xri_bitmap. If the ndlp does not
1921  * exist then the clear_rrq is still called because the rrq needs to
1922  * be freed.
1923  **/
1924 
1925 static void
lpfc_cmpl_els_rrq(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1926 lpfc_cmpl_els_rrq(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1927 		    struct lpfc_iocbq *rspiocb)
1928 {
1929 	struct lpfc_vport *vport = cmdiocb->vport;
1930 	IOCB_t *irsp;
1931 	struct lpfc_nodelist *ndlp;
1932 	struct lpfc_node_rrq *rrq;
1933 
1934 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1935 	rrq = cmdiocb->context_un.rrq;
1936 	cmdiocb->context_un.rsp_iocb = rspiocb;
1937 
1938 	irsp = &rspiocb->iocb;
1939 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1940 		"RRQ cmpl:      status:x%x/x%x did:x%x",
1941 		irsp->ulpStatus, irsp->un.ulpWord[4],
1942 		irsp->un.elsreq64.remoteID);
1943 
1944 	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1945 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || ndlp != rrq->ndlp) {
1946 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1947 				 "2882 RRQ completes to NPort x%x "
1948 				 "with no ndlp. Data: x%x x%x x%x\n",
1949 				 irsp->un.elsreq64.remoteID,
1950 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1951 				 irsp->ulpIoTag);
1952 		goto out;
1953 	}
1954 
1955 	/* rrq completes to NPort <nlp_DID> */
1956 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1957 			 "2880 RRQ completes to NPort x%x "
1958 			 "Data: x%x x%x x%x x%x x%x\n",
1959 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1960 			 irsp->ulpTimeout, rrq->xritag, rrq->rxid);
1961 
1962 	if (irsp->ulpStatus) {
1963 		/* Check for retry */
1964 		/* RRQ failed Don't print the vport to vport rjts */
1965 		if (irsp->ulpStatus != IOSTAT_LS_RJT ||
1966 			(((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
1967 			((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
1968 			(phba)->pport->cfg_log_verbose & LOG_ELS)
1969 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1970 					 "2881 RRQ failure DID:%06X Status:"
1971 					 "x%x/x%x\n",
1972 					 ndlp->nlp_DID, irsp->ulpStatus,
1973 					 irsp->un.ulpWord[4]);
1974 	}
1975 out:
1976 	if (rrq)
1977 		lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
1978 	lpfc_els_free_iocb(phba, cmdiocb);
1979 	return;
1980 }
1981 /**
1982  * lpfc_cmpl_els_plogi - Completion callback function for plogi
1983  * @phba: pointer to lpfc hba data structure.
1984  * @cmdiocb: pointer to lpfc command iocb data structure.
1985  * @rspiocb: pointer to lpfc response iocb data structure.
1986  *
1987  * This routine is the completion callback function for issuing the Port
1988  * Login (PLOGI) command. For PLOGI completion, there must be an active
1989  * ndlp on the vport node list that matches the remote node ID from the
1990  * PLOGI response IOCB. If such ndlp does not exist, the PLOGI is simply
1991  * ignored and command IOCB released. The PLOGI response IOCB status is
1992  * checked for error conditons. If there is error status reported, PLOGI
1993  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1994  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1995  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1996  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1997  * there are additional N_Port nodes with the vport that need to perform
1998  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1999  * PLOGIs.
2000  **/
2001 static void
lpfc_cmpl_els_plogi(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2002 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2003 		    struct lpfc_iocbq *rspiocb)
2004 {
2005 	struct lpfc_vport *vport = cmdiocb->vport;
2006 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2007 	IOCB_t *irsp;
2008 	struct lpfc_nodelist *ndlp;
2009 	struct lpfc_dmabuf *prsp;
2010 	int disc;
2011 
2012 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2013 	cmdiocb->context_un.rsp_iocb = rspiocb;
2014 
2015 	irsp = &rspiocb->iocb;
2016 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2017 		"PLOGI cmpl:      status:x%x/x%x did:x%x",
2018 		irsp->ulpStatus, irsp->un.ulpWord[4],
2019 		irsp->un.elsreq64.remoteID);
2020 
2021 	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
2022 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2023 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2024 				 "0136 PLOGI completes to NPort x%x "
2025 				 "with no ndlp. Data: x%x x%x x%x\n",
2026 				 irsp->un.elsreq64.remoteID,
2027 				 irsp->ulpStatus, irsp->un.ulpWord[4],
2028 				 irsp->ulpIoTag);
2029 		goto out;
2030 	}
2031 
2032 	/* Since ndlp can be freed in the disc state machine, note if this node
2033 	 * is being used during discovery.
2034 	 */
2035 	spin_lock_irq(shost->host_lock);
2036 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2037 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2038 	spin_unlock_irq(shost->host_lock);
2039 
2040 	/* PLOGI completes to NPort <nlp_DID> */
2041 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2042 			 "0102 PLOGI completes to NPort x%06x "
2043 			 "Data: x%x x%x x%x x%x x%x\n",
2044 			 ndlp->nlp_DID, ndlp->nlp_fc4_type,
2045 			 irsp->ulpStatus, irsp->un.ulpWord[4],
2046 			 disc, vport->num_disc_nodes);
2047 
2048 	/* Check to see if link went down during discovery */
2049 	if (lpfc_els_chk_latt(vport)) {
2050 		spin_lock_irq(shost->host_lock);
2051 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2052 		spin_unlock_irq(shost->host_lock);
2053 		goto out;
2054 	}
2055 
2056 	if (irsp->ulpStatus) {
2057 		/* Check for retry */
2058 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2059 			/* ELS command is being retried */
2060 			if (disc) {
2061 				spin_lock_irq(shost->host_lock);
2062 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2063 				spin_unlock_irq(shost->host_lock);
2064 			}
2065 			goto out;
2066 		}
2067 		/* PLOGI failed Don't print the vport to vport rjts */
2068 		if (irsp->ulpStatus != IOSTAT_LS_RJT ||
2069 			(((irsp->un.ulpWord[4]) >> 16 != LSRJT_INVALID_CMD) &&
2070 			((irsp->un.ulpWord[4]) >> 16 != LSRJT_UNABLE_TPC)) ||
2071 			(phba)->pport->cfg_log_verbose & LOG_ELS)
2072 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2073 				 "2753 PLOGI failure DID:%06X Status:x%x/x%x\n",
2074 				 ndlp->nlp_DID, irsp->ulpStatus,
2075 				 irsp->un.ulpWord[4]);
2076 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2077 		if (!lpfc_error_lost_link(irsp))
2078 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2079 						NLP_EVT_CMPL_PLOGI);
2080 	} else {
2081 		/* Good status, call state machine */
2082 		prsp = list_entry(((struct lpfc_dmabuf *)
2083 				   cmdiocb->context2)->list.next,
2084 				  struct lpfc_dmabuf, list);
2085 		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
2086 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2087 					     NLP_EVT_CMPL_PLOGI);
2088 	}
2089 
2090 	if (disc && vport->num_disc_nodes) {
2091 		/* Check to see if there are more PLOGIs to be sent */
2092 		lpfc_more_plogi(vport);
2093 
2094 		if (vport->num_disc_nodes == 0) {
2095 			spin_lock_irq(shost->host_lock);
2096 			vport->fc_flag &= ~FC_NDISC_ACTIVE;
2097 			spin_unlock_irq(shost->host_lock);
2098 
2099 			lpfc_can_disctmo(vport);
2100 			lpfc_end_rscn(vport);
2101 		}
2102 	}
2103 
2104 out:
2105 	lpfc_els_free_iocb(phba, cmdiocb);
2106 	return;
2107 }
2108 
2109 /**
2110  * lpfc_issue_els_plogi - Issue an plogi iocb command for a vport
2111  * @vport: pointer to a host virtual N_Port data structure.
2112  * @did: destination port identifier.
2113  * @retry: number of retries to the command IOCB.
2114  *
2115  * This routine issues a Port Login (PLOGI) command to a remote N_Port
2116  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
2117  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
2118  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
2119  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
2120  *
2121  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2122  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2123  * will be stored into the context1 field of the IOCB for the completion
2124  * callback function to the PLOGI ELS command.
2125  *
2126  * Return code
2127  *   0 - Successfully issued a plogi for @vport
2128  *   1 - failed to issue a plogi for @vport
2129  **/
2130 int
lpfc_issue_els_plogi(struct lpfc_vport * vport,uint32_t did,uint8_t retry)2131 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
2132 {
2133 	struct lpfc_hba  *phba = vport->phba;
2134 	struct Scsi_Host *shost;
2135 	struct serv_parm *sp;
2136 	struct lpfc_nodelist *ndlp;
2137 	struct lpfc_iocbq *elsiocb;
2138 	uint8_t *pcmd;
2139 	uint16_t cmdsize;
2140 	int ret;
2141 
2142 	ndlp = lpfc_findnode_did(vport, did);
2143 
2144 	if (ndlp) {
2145 		/* Defer the processing of the issue PLOGI until after the
2146 		 * outstanding UNREG_RPI mbox command completes, unless we
2147 		 * are going offline. This logic does not apply for Fabric DIDs
2148 		 */
2149 		if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
2150 		    ((ndlp->nlp_DID & Fabric_DID_MASK) != Fabric_DID_MASK) &&
2151 		    !(vport->fc_flag & FC_OFFLINE_MODE)) {
2152 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2153 					 "4110 Issue PLOGI x%x deferred "
2154 					 "on NPort x%x rpi x%x Data: x%px\n",
2155 					 ndlp->nlp_defer_did, ndlp->nlp_DID,
2156 					 ndlp->nlp_rpi, ndlp);
2157 
2158 			/* We can only defer 1st PLOGI */
2159 			if (ndlp->nlp_defer_did == NLP_EVT_NOTHING_PENDING)
2160 				ndlp->nlp_defer_did = did;
2161 			return 0;
2162 		}
2163 		if (!NLP_CHK_NODE_ACT(ndlp))
2164 			ndlp = NULL;
2165 	}
2166 
2167 	/* If ndlp is not NULL, we will bump the reference count on it */
2168 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
2169 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
2170 				     ELS_CMD_PLOGI);
2171 	if (!elsiocb)
2172 		return 1;
2173 
2174 	shost = lpfc_shost_from_vport(vport);
2175 	spin_lock_irq(shost->host_lock);
2176 	ndlp->nlp_flag &= ~NLP_FCP_PRLI_RJT;
2177 	spin_unlock_irq(shost->host_lock);
2178 
2179 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2180 
2181 	/* For PLOGI request, remainder of payload is service parameters */
2182 	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
2183 	pcmd += sizeof(uint32_t);
2184 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
2185 	sp = (struct serv_parm *) pcmd;
2186 
2187 	/*
2188 	 * If we are a N-port connected to a Fabric, fix-up paramm's so logins
2189 	 * to device on remote loops work.
2190 	 */
2191 	if ((vport->fc_flag & FC_FABRIC) && !(vport->fc_flag & FC_PUBLIC_LOOP))
2192 		sp->cmn.altBbCredit = 1;
2193 
2194 	if (sp->cmn.fcphLow < FC_PH_4_3)
2195 		sp->cmn.fcphLow = FC_PH_4_3;
2196 
2197 	if (sp->cmn.fcphHigh < FC_PH3)
2198 		sp->cmn.fcphHigh = FC_PH3;
2199 
2200 	sp->cmn.valid_vendor_ver_level = 0;
2201 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
2202 	sp->cmn.bbRcvSizeMsb &= 0xF;
2203 
2204 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2205 		"Issue PLOGI:     did:x%x",
2206 		did, 0, 0);
2207 
2208 	/* If our firmware supports this feature, convey that
2209 	 * information to the target using the vendor specific field.
2210 	 */
2211 	if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
2212 		sp->cmn.valid_vendor_ver_level = 1;
2213 		sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
2214 		sp->un.vv.flags = cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
2215 	}
2216 
2217 	phba->fc_stat.elsXmitPLOGI++;
2218 	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
2219 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
2220 
2221 	if (ret == IOCB_ERROR) {
2222 		lpfc_els_free_iocb(phba, elsiocb);
2223 		return 1;
2224 	}
2225 	return 0;
2226 }
2227 
2228 /**
2229  * lpfc_cmpl_els_prli - Completion callback function for prli
2230  * @phba: pointer to lpfc hba data structure.
2231  * @cmdiocb: pointer to lpfc command iocb data structure.
2232  * @rspiocb: pointer to lpfc response iocb data structure.
2233  *
2234  * This routine is the completion callback function for a Process Login
2235  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
2236  * status. If there is error status reported, PRLI retry shall be attempted
2237  * by invoking the lpfc_els_retry() routine. Otherwise, the state
2238  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
2239  * ndlp to mark the PRLI completion.
2240  **/
2241 static void
lpfc_cmpl_els_prli(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2242 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2243 		   struct lpfc_iocbq *rspiocb)
2244 {
2245 	struct lpfc_vport *vport = cmdiocb->vport;
2246 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2247 	IOCB_t *irsp;
2248 	struct lpfc_nodelist *ndlp;
2249 	char *mode;
2250 	u32 loglevel;
2251 
2252 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2253 	cmdiocb->context_un.rsp_iocb = rspiocb;
2254 
2255 	irsp = &(rspiocb->iocb);
2256 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2257 	spin_lock_irq(shost->host_lock);
2258 	ndlp->nlp_flag &= ~NLP_PRLI_SND;
2259 
2260 	/* Driver supports multiple FC4 types.  Counters matter. */
2261 	vport->fc_prli_sent--;
2262 	ndlp->fc4_prli_sent--;
2263 	spin_unlock_irq(shost->host_lock);
2264 
2265 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2266 		"PRLI cmpl:       status:x%x/x%x did:x%x",
2267 		irsp->ulpStatus, irsp->un.ulpWord[4],
2268 		ndlp->nlp_DID);
2269 
2270 	/* PRLI completes to NPort <nlp_DID> */
2271 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2272 			 "0103 PRLI completes to NPort x%06x "
2273 			 "Data: x%x x%x x%x x%x\n",
2274 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2275 			 vport->num_disc_nodes, ndlp->fc4_prli_sent);
2276 
2277 	/* Check to see if link went down during discovery */
2278 	if (lpfc_els_chk_latt(vport))
2279 		goto out;
2280 
2281 	if (irsp->ulpStatus) {
2282 		/* Check for retry */
2283 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2284 			/* ELS command is being retried */
2285 			goto out;
2286 		}
2287 
2288 		/* If we don't send GFT_ID to Fabric, a PRLI error
2289 		 * could be expected.
2290 		 */
2291 		if ((vport->fc_flag & FC_FABRIC) ||
2292 		    (vport->cfg_enable_fc4_type != LPFC_ENABLE_BOTH)) {
2293 			mode = KERN_ERR;
2294 			loglevel =  LOG_TRACE_EVENT;
2295 		} else {
2296 			mode = KERN_INFO;
2297 			loglevel =  LOG_ELS;
2298 		}
2299 
2300 		/* PRLI failed */
2301 		lpfc_printf_vlog(vport, mode, loglevel,
2302 				 "2754 PRLI failure DID:%06X Status:x%x/x%x, "
2303 				 "data: x%x\n",
2304 				 ndlp->nlp_DID, irsp->ulpStatus,
2305 				 irsp->un.ulpWord[4], ndlp->fc4_prli_sent);
2306 
2307 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2308 		if (lpfc_error_lost_link(irsp))
2309 			goto out;
2310 		else
2311 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2312 						NLP_EVT_CMPL_PRLI);
2313 	} else {
2314 		/* Good status, call state machine.  However, if another
2315 		 * PRLI is outstanding, don't call the state machine
2316 		 * because final disposition to Mapped or Unmapped is
2317 		 * completed there.
2318 		 */
2319 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2320 					NLP_EVT_CMPL_PRLI);
2321 	}
2322 
2323 out:
2324 	lpfc_els_free_iocb(phba, cmdiocb);
2325 	return;
2326 }
2327 
2328 /**
2329  * lpfc_issue_els_prli - Issue a prli iocb command for a vport
2330  * @vport: pointer to a host virtual N_Port data structure.
2331  * @ndlp: pointer to a node-list data structure.
2332  * @retry: number of retries to the command IOCB.
2333  *
2334  * This routine issues a Process Login (PRLI) ELS command for the
2335  * @vport. The PRLI service parameters are set up in the payload of the
2336  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
2337  * is put to the IOCB completion callback func field before invoking the
2338  * routine lpfc_sli_issue_iocb() to send out PRLI command.
2339  *
2340  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2341  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2342  * will be stored into the context1 field of the IOCB for the completion
2343  * callback function to the PRLI ELS command.
2344  *
2345  * Return code
2346  *   0 - successfully issued prli iocb command for @vport
2347  *   1 - failed to issue prli iocb command for @vport
2348  **/
2349 int
lpfc_issue_els_prli(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)2350 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2351 		    uint8_t retry)
2352 {
2353 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2354 	struct lpfc_hba *phba = vport->phba;
2355 	PRLI *npr;
2356 	struct lpfc_nvme_prli *npr_nvme;
2357 	struct lpfc_iocbq *elsiocb;
2358 	uint8_t *pcmd;
2359 	uint16_t cmdsize;
2360 	u32 local_nlp_type, elscmd;
2361 
2362 	/*
2363 	 * If we are in RSCN mode, the FC4 types supported from a
2364 	 * previous GFT_ID command may not be accurate. So, if we
2365 	 * are a NVME Initiator, always look for the possibility of
2366 	 * the remote NPort beng a NVME Target.
2367 	 */
2368 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2369 	    vport->fc_flag & FC_RSCN_MODE &&
2370 	    vport->nvmei_support)
2371 		ndlp->nlp_fc4_type |= NLP_FC4_NVME;
2372 	local_nlp_type = ndlp->nlp_fc4_type;
2373 
2374 	/* This routine will issue 1 or 2 PRLIs, so zero all the ndlp
2375 	 * fields here before any of them can complete.
2376 	 */
2377 	ndlp->nlp_type &= ~(NLP_FCP_TARGET | NLP_FCP_INITIATOR);
2378 	ndlp->nlp_type &= ~(NLP_NVME_TARGET | NLP_NVME_INITIATOR);
2379 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
2380 	ndlp->nlp_flag &= ~(NLP_FIRSTBURST | NLP_NPR_2B_DISC);
2381 	ndlp->nvme_fb_size = 0;
2382 
2383  send_next_prli:
2384 	if (local_nlp_type & NLP_FC4_FCP) {
2385 		/* Payload is 4 + 16 = 20 x14 bytes. */
2386 		cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
2387 		elscmd = ELS_CMD_PRLI;
2388 	} else if (local_nlp_type & NLP_FC4_NVME) {
2389 		/* Payload is 4 + 20 = 24 x18 bytes. */
2390 		cmdsize = (sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli));
2391 		elscmd = ELS_CMD_NVMEPRLI;
2392 	} else {
2393 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2394 				 "3083 Unknown FC_TYPE x%x ndlp x%06x\n",
2395 				 ndlp->nlp_fc4_type, ndlp->nlp_DID);
2396 		return 1;
2397 	}
2398 
2399 	/* SLI3 ports don't support NVME.  If this rport is a strict NVME
2400 	 * FC4 type, implicitly LOGO.
2401 	 */
2402 	if (phba->sli_rev == LPFC_SLI_REV3 &&
2403 	    ndlp->nlp_fc4_type == NLP_FC4_NVME) {
2404 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2405 				 "3088 Rport fc4 type 0x%x not supported by SLI3 adapter\n",
2406 				 ndlp->nlp_type);
2407 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
2408 		return 1;
2409 	}
2410 
2411 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2412 				     ndlp->nlp_DID, elscmd);
2413 	if (!elsiocb)
2414 		return 1;
2415 
2416 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2417 
2418 	/* For PRLI request, remainder of payload is service parameters */
2419 	memset(pcmd, 0, cmdsize);
2420 
2421 	if (local_nlp_type & NLP_FC4_FCP) {
2422 		/* Remainder of payload is FCP PRLI parameter page.
2423 		 * Note: this data structure is defined as
2424 		 * BE/LE in the structure definition so no
2425 		 * byte swap call is made.
2426 		 */
2427 		*((uint32_t *)(pcmd)) = ELS_CMD_PRLI;
2428 		pcmd += sizeof(uint32_t);
2429 		npr = (PRLI *)pcmd;
2430 
2431 		/*
2432 		 * If our firmware version is 3.20 or later,
2433 		 * set the following bits for FC-TAPE support.
2434 		 */
2435 		if (phba->vpd.rev.feaLevelHigh >= 0x02) {
2436 			npr->ConfmComplAllowed = 1;
2437 			npr->Retry = 1;
2438 			npr->TaskRetryIdReq = 1;
2439 		}
2440 		npr->estabImagePair = 1;
2441 		npr->readXferRdyDis = 1;
2442 		if (vport->cfg_first_burst_size)
2443 			npr->writeXferRdyDis = 1;
2444 
2445 		/* For FCP support */
2446 		npr->prliType = PRLI_FCP_TYPE;
2447 		npr->initiatorFunc = 1;
2448 		elsiocb->iocb_flag |= LPFC_PRLI_FCP_REQ;
2449 
2450 		/* Remove FCP type - processed. */
2451 		local_nlp_type &= ~NLP_FC4_FCP;
2452 	} else if (local_nlp_type & NLP_FC4_NVME) {
2453 		/* Remainder of payload is NVME PRLI parameter page.
2454 		 * This data structure is the newer definition that
2455 		 * uses bf macros so a byte swap is required.
2456 		 */
2457 		*((uint32_t *)(pcmd)) = ELS_CMD_NVMEPRLI;
2458 		pcmd += sizeof(uint32_t);
2459 		npr_nvme = (struct lpfc_nvme_prli *)pcmd;
2460 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
2461 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
2462 		if (phba->nsler) {
2463 			bf_set(prli_nsler, npr_nvme, 1);
2464 			bf_set(prli_conf, npr_nvme, 1);
2465 		}
2466 
2467 		/* Only initiators request first burst. */
2468 		if ((phba->cfg_nvme_enable_fb) &&
2469 		    !phba->nvmet_support)
2470 			bf_set(prli_fba, npr_nvme, 1);
2471 
2472 		if (phba->nvmet_support) {
2473 			bf_set(prli_tgt, npr_nvme, 1);
2474 			bf_set(prli_disc, npr_nvme, 1);
2475 		} else {
2476 			bf_set(prli_init, npr_nvme, 1);
2477 			bf_set(prli_conf, npr_nvme, 1);
2478 		}
2479 
2480 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
2481 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
2482 		elsiocb->iocb_flag |= LPFC_PRLI_NVME_REQ;
2483 
2484 		/* Remove NVME type - processed. */
2485 		local_nlp_type &= ~NLP_FC4_NVME;
2486 	}
2487 
2488 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2489 		"Issue PRLI:      did:x%x",
2490 		ndlp->nlp_DID, 0, 0);
2491 
2492 	phba->fc_stat.elsXmitPRLI++;
2493 	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
2494 	spin_lock_irq(shost->host_lock);
2495 	ndlp->nlp_flag |= NLP_PRLI_SND;
2496 
2497 	/* The vport counters are used for lpfc_scan_finished, but
2498 	 * the ndlp is used to track outstanding PRLIs for different
2499 	 * FC4 types.
2500 	 */
2501 	vport->fc_prli_sent++;
2502 	ndlp->fc4_prli_sent++;
2503 	spin_unlock_irq(shost->host_lock);
2504 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2505 	    IOCB_ERROR) {
2506 		spin_lock_irq(shost->host_lock);
2507 		ndlp->nlp_flag &= ~NLP_PRLI_SND;
2508 		spin_unlock_irq(shost->host_lock);
2509 		lpfc_els_free_iocb(phba, elsiocb);
2510 		return 1;
2511 	}
2512 
2513 
2514 	/* The driver supports 2 FC4 types.  Make sure
2515 	 * a PRLI is issued for all types before exiting.
2516 	 */
2517 	if (phba->sli_rev == LPFC_SLI_REV4 &&
2518 	    local_nlp_type & (NLP_FC4_FCP | NLP_FC4_NVME))
2519 		goto send_next_prli;
2520 
2521 	return 0;
2522 }
2523 
2524 /**
2525  * lpfc_rscn_disc - Perform rscn discovery for a vport
2526  * @vport: pointer to a host virtual N_Port data structure.
2527  *
2528  * This routine performs Registration State Change Notification (RSCN)
2529  * discovery for a @vport. If the @vport's node port recovery count is not
2530  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
2531  * the nodes that need recovery. If none of the PLOGI were needed through
2532  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
2533  * invoked to check and handle possible more RSCN came in during the period
2534  * of processing the current ones.
2535  **/
2536 static void
lpfc_rscn_disc(struct lpfc_vport * vport)2537 lpfc_rscn_disc(struct lpfc_vport *vport)
2538 {
2539 	lpfc_can_disctmo(vport);
2540 
2541 	/* RSCN discovery */
2542 	/* go thru NPR nodes and issue ELS PLOGIs */
2543 	if (vport->fc_npr_cnt)
2544 		if (lpfc_els_disc_plogi(vport))
2545 			return;
2546 
2547 	lpfc_end_rscn(vport);
2548 }
2549 
2550 /**
2551  * lpfc_adisc_done - Complete the adisc phase of discovery
2552  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
2553  *
2554  * This function is called when the final ADISC is completed during discovery.
2555  * This function handles clearing link attention or issuing reg_vpi depending
2556  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
2557  * discovery.
2558  * This function is called with no locks held.
2559  **/
2560 static void
lpfc_adisc_done(struct lpfc_vport * vport)2561 lpfc_adisc_done(struct lpfc_vport *vport)
2562 {
2563 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
2564 	struct lpfc_hba   *phba = vport->phba;
2565 
2566 	/*
2567 	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
2568 	 * and continue discovery.
2569 	 */
2570 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2571 	    !(vport->fc_flag & FC_RSCN_MODE) &&
2572 	    (phba->sli_rev < LPFC_SLI_REV4)) {
2573 		/* The ADISCs are complete.  Doesn't matter if they
2574 		 * succeeded or failed because the ADISC completion
2575 		 * routine guarantees to call the state machine and
2576 		 * the RPI is either unregistered (failed ADISC response)
2577 		 * or the RPI is still valid and the node is marked
2578 		 * mapped for a target.  The exchanges should be in the
2579 		 * correct state. This code is specific to SLI3.
2580 		 */
2581 		lpfc_issue_clear_la(phba, vport);
2582 		lpfc_issue_reg_vpi(phba, vport);
2583 		return;
2584 	}
2585 	/*
2586 	* For SLI2, we need to set port_state to READY
2587 	* and continue discovery.
2588 	*/
2589 	if (vport->port_state < LPFC_VPORT_READY) {
2590 		/* If we get here, there is nothing to ADISC */
2591 		lpfc_issue_clear_la(phba, vport);
2592 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
2593 			vport->num_disc_nodes = 0;
2594 			/* go thru NPR list, issue ELS PLOGIs */
2595 			if (vport->fc_npr_cnt)
2596 				lpfc_els_disc_plogi(vport);
2597 			if (!vport->num_disc_nodes) {
2598 				spin_lock_irq(shost->host_lock);
2599 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
2600 				spin_unlock_irq(shost->host_lock);
2601 				lpfc_can_disctmo(vport);
2602 				lpfc_end_rscn(vport);
2603 			}
2604 		}
2605 		vport->port_state = LPFC_VPORT_READY;
2606 	} else
2607 		lpfc_rscn_disc(vport);
2608 }
2609 
2610 /**
2611  * lpfc_more_adisc - Issue more adisc as needed
2612  * @vport: pointer to a host virtual N_Port data structure.
2613  *
2614  * This routine determines whether there are more ndlps on a @vport
2615  * node list need to have Address Discover (ADISC) issued. If so, it will
2616  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
2617  * remaining nodes which need to have ADISC sent.
2618  **/
2619 void
lpfc_more_adisc(struct lpfc_vport * vport)2620 lpfc_more_adisc(struct lpfc_vport *vport)
2621 {
2622 	if (vport->num_disc_nodes)
2623 		vport->num_disc_nodes--;
2624 	/* Continue discovery with <num_disc_nodes> ADISCs to go */
2625 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
2626 			 "0210 Continue discovery with %d ADISCs to go "
2627 			 "Data: x%x x%x x%x\n",
2628 			 vport->num_disc_nodes, vport->fc_adisc_cnt,
2629 			 vport->fc_flag, vport->port_state);
2630 	/* Check to see if there are more ADISCs to be sent */
2631 	if (vport->fc_flag & FC_NLP_MORE) {
2632 		lpfc_set_disctmo(vport);
2633 		/* go thru NPR nodes and issue any remaining ELS ADISCs */
2634 		lpfc_els_disc_adisc(vport);
2635 	}
2636 	if (!vport->num_disc_nodes)
2637 		lpfc_adisc_done(vport);
2638 	return;
2639 }
2640 
2641 /**
2642  * lpfc_cmpl_els_adisc - Completion callback function for adisc
2643  * @phba: pointer to lpfc hba data structure.
2644  * @cmdiocb: pointer to lpfc command iocb data structure.
2645  * @rspiocb: pointer to lpfc response iocb data structure.
2646  *
2647  * This routine is the completion function for issuing the Address Discover
2648  * (ADISC) command. It first checks to see whether link went down during
2649  * the discovery process. If so, the node will be marked as node port
2650  * recovery for issuing discover IOCB by the link attention handler and
2651  * exit. Otherwise, the response status is checked. If error was reported
2652  * in the response status, the ADISC command shall be retried by invoking
2653  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
2654  * the response status, the state machine is invoked to set transition
2655  * with respect to NLP_EVT_CMPL_ADISC event.
2656  **/
2657 static void
lpfc_cmpl_els_adisc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2658 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2659 		    struct lpfc_iocbq *rspiocb)
2660 {
2661 	struct lpfc_vport *vport = cmdiocb->vport;
2662 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2663 	IOCB_t *irsp;
2664 	struct lpfc_nodelist *ndlp;
2665 	int  disc;
2666 
2667 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2668 	cmdiocb->context_un.rsp_iocb = rspiocb;
2669 
2670 	irsp = &(rspiocb->iocb);
2671 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2672 
2673 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2674 		"ADISC cmpl:      status:x%x/x%x did:x%x",
2675 		irsp->ulpStatus, irsp->un.ulpWord[4],
2676 		ndlp->nlp_DID);
2677 
2678 	/* Since ndlp can be freed in the disc state machine, note if this node
2679 	 * is being used during discovery.
2680 	 */
2681 	spin_lock_irq(shost->host_lock);
2682 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
2683 	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
2684 	spin_unlock_irq(shost->host_lock);
2685 	/* ADISC completes to NPort <nlp_DID> */
2686 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2687 			 "0104 ADISC completes to NPort x%x "
2688 			 "Data: x%x x%x x%x x%x x%x\n",
2689 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2690 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
2691 	/* Check to see if link went down during discovery */
2692 	if (lpfc_els_chk_latt(vport)) {
2693 		spin_lock_irq(shost->host_lock);
2694 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2695 		spin_unlock_irq(shost->host_lock);
2696 		goto out;
2697 	}
2698 
2699 	if (irsp->ulpStatus) {
2700 		/* Check for retry */
2701 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
2702 			/* ELS command is being retried */
2703 			if (disc) {
2704 				spin_lock_irq(shost->host_lock);
2705 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2706 				spin_unlock_irq(shost->host_lock);
2707 				lpfc_set_disctmo(vport);
2708 			}
2709 			goto out;
2710 		}
2711 		/* ADISC failed */
2712 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2713 				 "2755 ADISC failure DID:%06X Status:x%x/x%x\n",
2714 				 ndlp->nlp_DID, irsp->ulpStatus,
2715 				 irsp->un.ulpWord[4]);
2716 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2717 		if (!lpfc_error_lost_link(irsp))
2718 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2719 						NLP_EVT_CMPL_ADISC);
2720 	} else
2721 		/* Good status, call state machine */
2722 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2723 					NLP_EVT_CMPL_ADISC);
2724 
2725 	/* Check to see if there are more ADISCs to be sent */
2726 	if (disc && vport->num_disc_nodes)
2727 		lpfc_more_adisc(vport);
2728 out:
2729 	lpfc_els_free_iocb(phba, cmdiocb);
2730 	return;
2731 }
2732 
2733 /**
2734  * lpfc_issue_els_adisc - Issue an address discover iocb to an node on a vport
2735  * @vport: pointer to a virtual N_Port data structure.
2736  * @ndlp: pointer to a node-list data structure.
2737  * @retry: number of retries to the command IOCB.
2738  *
2739  * This routine issues an Address Discover (ADISC) for an @ndlp on a
2740  * @vport. It prepares the payload of the ADISC ELS command, updates the
2741  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
2742  * to issue the ADISC ELS command.
2743  *
2744  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2745  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2746  * will be stored into the context1 field of the IOCB for the completion
2747  * callback function to the ADISC ELS command.
2748  *
2749  * Return code
2750  *   0 - successfully issued adisc
2751  *   1 - failed to issue adisc
2752  **/
2753 int
lpfc_issue_els_adisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)2754 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2755 		     uint8_t retry)
2756 {
2757 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2758 	struct lpfc_hba  *phba = vport->phba;
2759 	ADISC *ap;
2760 	struct lpfc_iocbq *elsiocb;
2761 	uint8_t *pcmd;
2762 	uint16_t cmdsize;
2763 
2764 	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
2765 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2766 				     ndlp->nlp_DID, ELS_CMD_ADISC);
2767 	if (!elsiocb)
2768 		return 1;
2769 
2770 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2771 
2772 	/* For ADISC request, remainder of payload is service parameters */
2773 	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
2774 	pcmd += sizeof(uint32_t);
2775 
2776 	/* Fill in ADISC payload */
2777 	ap = (ADISC *) pcmd;
2778 	ap->hardAL_PA = phba->fc_pref_ALPA;
2779 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
2780 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2781 	ap->DID = be32_to_cpu(vport->fc_myDID);
2782 
2783 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2784 		"Issue ADISC:     did:x%x",
2785 		ndlp->nlp_DID, 0, 0);
2786 
2787 	phba->fc_stat.elsXmitADISC++;
2788 	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
2789 	spin_lock_irq(shost->host_lock);
2790 	ndlp->nlp_flag |= NLP_ADISC_SND;
2791 	spin_unlock_irq(shost->host_lock);
2792 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
2793 	    IOCB_ERROR) {
2794 		spin_lock_irq(shost->host_lock);
2795 		ndlp->nlp_flag &= ~NLP_ADISC_SND;
2796 		spin_unlock_irq(shost->host_lock);
2797 		lpfc_els_free_iocb(phba, elsiocb);
2798 		return 1;
2799 	}
2800 	return 0;
2801 }
2802 
2803 /**
2804  * lpfc_cmpl_els_logo - Completion callback function for logo
2805  * @phba: pointer to lpfc hba data structure.
2806  * @cmdiocb: pointer to lpfc command iocb data structure.
2807  * @rspiocb: pointer to lpfc response iocb data structure.
2808  *
2809  * This routine is the completion function for issuing the ELS Logout (LOGO)
2810  * command. If no error status was reported from the LOGO response, the
2811  * state machine of the associated ndlp shall be invoked for transition with
2812  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
2813  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
2814  **/
2815 static void
lpfc_cmpl_els_logo(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2816 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2817 		   struct lpfc_iocbq *rspiocb)
2818 {
2819 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2820 	struct lpfc_vport *vport = ndlp->vport;
2821 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2822 	IOCB_t *irsp;
2823 	struct lpfcMboxq *mbox;
2824 	unsigned long flags;
2825 	uint32_t skip_recovery = 0;
2826 
2827 	/* we pass cmdiocb to state machine which needs rspiocb as well */
2828 	cmdiocb->context_un.rsp_iocb = rspiocb;
2829 
2830 	irsp = &(rspiocb->iocb);
2831 	spin_lock_irq(shost->host_lock);
2832 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
2833 	spin_unlock_irq(shost->host_lock);
2834 
2835 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2836 		"LOGO cmpl:       status:x%x/x%x did:x%x",
2837 		irsp->ulpStatus, irsp->un.ulpWord[4],
2838 		ndlp->nlp_DID);
2839 
2840 	/* LOGO completes to NPort <nlp_DID> */
2841 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2842 			 "0105 LOGO completes to NPort x%x "
2843 			 "Data: x%x x%x x%x x%x\n",
2844 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
2845 			 irsp->ulpTimeout, vport->num_disc_nodes);
2846 
2847 	if (lpfc_els_chk_latt(vport)) {
2848 		skip_recovery = 1;
2849 		goto out;
2850 	}
2851 
2852 	/* Check to see if link went down during discovery */
2853 	if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
2854 	        /* NLP_EVT_DEVICE_RM should unregister the RPI
2855 		 * which should abort all outstanding IOs.
2856 		 */
2857 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
2858 					NLP_EVT_DEVICE_RM);
2859 		skip_recovery = 1;
2860 		goto out;
2861 	}
2862 
2863 	/* The LOGO will not be retried on failure.  A LOGO was
2864 	 * issued to the remote rport and a ACC or RJT or no Answer are
2865 	 * all acceptable.  Note the failure and move forward with
2866 	 * discovery.  The PLOGI will retry.
2867 	 */
2868 	if (irsp->ulpStatus) {
2869 		/* LOGO failed */
2870 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2871 				 "2756 LOGO failure, No Retry DID:%06X Status:x%x/x%x\n",
2872 				 ndlp->nlp_DID, irsp->ulpStatus,
2873 				 irsp->un.ulpWord[4]);
2874 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
2875 		if (lpfc_error_lost_link(irsp)) {
2876 			skip_recovery = 1;
2877 			goto out;
2878 		}
2879 	}
2880 
2881 	/* Call state machine. This will unregister the rpi if needed. */
2882 	lpfc_disc_state_machine(vport, ndlp, cmdiocb, NLP_EVT_CMPL_LOGO);
2883 
2884 out:
2885 	lpfc_els_free_iocb(phba, cmdiocb);
2886 	/* If we are in pt2pt mode, we could rcv new S_ID on PLOGI */
2887 	if ((vport->fc_flag & FC_PT2PT) &&
2888 		!(vport->fc_flag & FC_PT2PT_PLOGI)) {
2889 		phba->pport->fc_myDID = 0;
2890 
2891 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
2892 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
2893 			if (phba->nvmet_support)
2894 				lpfc_nvmet_update_targetport(phba);
2895 			else
2896 				lpfc_nvme_update_localport(phba->pport);
2897 		}
2898 
2899 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2900 		if (mbox) {
2901 			lpfc_config_link(phba, mbox);
2902 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2903 			mbox->vport = vport;
2904 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
2905 				MBX_NOT_FINISHED) {
2906 				mempool_free(mbox, phba->mbox_mem_pool);
2907 				skip_recovery = 1;
2908 			}
2909 		}
2910 	}
2911 
2912 	/*
2913 	 * If the node is a target, the handling attempts to recover the port.
2914 	 * For any other port type, the rpi is unregistered as an implicit
2915 	 * LOGO.
2916 	 */
2917 	if (ndlp->nlp_type & (NLP_FCP_TARGET | NLP_NVME_TARGET) &&
2918 	    skip_recovery == 0) {
2919 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
2920 		spin_lock_irqsave(shost->host_lock, flags);
2921 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
2922 		spin_unlock_irqrestore(shost->host_lock, flags);
2923 
2924 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2925 				 "3187 LOGO completes to NPort x%x: Start "
2926 				 "Recovery Data: x%x x%x x%x x%x\n",
2927 				 ndlp->nlp_DID, irsp->ulpStatus,
2928 				 irsp->un.ulpWord[4], irsp->ulpTimeout,
2929 				 vport->num_disc_nodes);
2930 		lpfc_disc_start(vport);
2931 	}
2932 	return;
2933 }
2934 
2935 /**
2936  * lpfc_issue_els_logo - Issue a logo to an node on a vport
2937  * @vport: pointer to a virtual N_Port data structure.
2938  * @ndlp: pointer to a node-list data structure.
2939  * @retry: number of retries to the command IOCB.
2940  *
2941  * This routine constructs and issues an ELS Logout (LOGO) iocb command
2942  * to a remote node, referred by an @ndlp on a @vport. It constructs the
2943  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
2944  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
2945  *
2946  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2947  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2948  * will be stored into the context1 field of the IOCB for the completion
2949  * callback function to the LOGO ELS command.
2950  *
2951  * Callers of this routine are expected to unregister the RPI first
2952  *
2953  * Return code
2954  *   0 - successfully issued logo
2955  *   1 - failed to issue logo
2956  **/
2957 int
lpfc_issue_els_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)2958 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
2959 		    uint8_t retry)
2960 {
2961 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2962 	struct lpfc_hba  *phba = vport->phba;
2963 	struct lpfc_iocbq *elsiocb;
2964 	uint8_t *pcmd;
2965 	uint16_t cmdsize;
2966 	int rc;
2967 
2968 	spin_lock_irq(shost->host_lock);
2969 	if (ndlp->nlp_flag & NLP_LOGO_SND) {
2970 		spin_unlock_irq(shost->host_lock);
2971 		return 0;
2972 	}
2973 	spin_unlock_irq(shost->host_lock);
2974 
2975 	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
2976 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2977 				     ndlp->nlp_DID, ELS_CMD_LOGO);
2978 	if (!elsiocb)
2979 		return 1;
2980 
2981 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2982 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
2983 	pcmd += sizeof(uint32_t);
2984 
2985 	/* Fill in LOGO payload */
2986 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
2987 	pcmd += sizeof(uint32_t);
2988 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
2989 
2990 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2991 		"Issue LOGO:      did:x%x",
2992 		ndlp->nlp_DID, 0, 0);
2993 
2994 	phba->fc_stat.elsXmitLOGO++;
2995 	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
2996 	spin_lock_irq(shost->host_lock);
2997 	ndlp->nlp_flag |= NLP_LOGO_SND;
2998 	ndlp->nlp_flag &= ~NLP_ISSUE_LOGO;
2999 	spin_unlock_irq(shost->host_lock);
3000 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
3001 	if (rc == IOCB_ERROR) {
3002 		spin_lock_irq(shost->host_lock);
3003 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
3004 		spin_unlock_irq(shost->host_lock);
3005 		lpfc_els_free_iocb(phba, elsiocb);
3006 		return 1;
3007 	}
3008 
3009 	spin_lock_irq(shost->host_lock);
3010 	ndlp->nlp_prev_state = ndlp->nlp_state;
3011 	spin_unlock_irq(shost->host_lock);
3012 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3013 	return 0;
3014 }
3015 
3016 /**
3017  * lpfc_cmpl_els_cmd - Completion callback function for generic els command
3018  * @phba: pointer to lpfc hba data structure.
3019  * @cmdiocb: pointer to lpfc command iocb data structure.
3020  * @rspiocb: pointer to lpfc response iocb data structure.
3021  *
3022  * This routine is a generic completion callback function for ELS commands.
3023  * Specifically, it is the callback function which does not need to perform
3024  * any command specific operations. It is currently used by the ELS command
3025  * issuing routines for RSCN, lpfc_issue_els_rscn, and the ELS Fibre Channel
3026  * Address Resolution Protocol Response (FARPR) routine, lpfc_issue_els_farpr().
3027  * Other than certain debug loggings, this callback function simply invokes the
3028  * lpfc_els_chk_latt() routine to check whether link went down during the
3029  * discovery process.
3030  **/
3031 static void
lpfc_cmpl_els_cmd(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)3032 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3033 		  struct lpfc_iocbq *rspiocb)
3034 {
3035 	struct lpfc_vport *vport = cmdiocb->vport;
3036 	IOCB_t *irsp;
3037 
3038 	irsp = &rspiocb->iocb;
3039 
3040 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3041 			      "ELS cmd cmpl:    status:x%x/x%x did:x%x",
3042 			      irsp->ulpStatus, irsp->un.ulpWord[4],
3043 			      irsp->un.elsreq64.remoteID);
3044 
3045 	/* ELS cmd tag <ulpIoTag> completes */
3046 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3047 			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
3048 			 irsp->ulpIoTag, irsp->ulpStatus,
3049 			 irsp->un.ulpWord[4], irsp->ulpTimeout);
3050 
3051 	/* Check to see if link went down during discovery */
3052 	lpfc_els_chk_latt(vport);
3053 	lpfc_els_free_iocb(phba, cmdiocb);
3054 }
3055 
3056 /**
3057  * lpfc_cmpl_els_disc_cmd - Completion callback function for Discovery ELS cmd
3058  * @phba: pointer to lpfc hba data structure.
3059  * @cmdiocb: pointer to lpfc command iocb data structure.
3060  * @rspiocb: pointer to lpfc response iocb data structure.
3061  *
3062  * This routine is a generic completion callback function for Discovery ELS cmd.
3063  * Currently used by the ELS command issuing routines for the ELS State Change
3064  * Request (SCR), lpfc_issue_els_scr() and the ELS RDF, lpfc_issue_els_rdf().
3065  * These commands will be retried once only for ELS timeout errors.
3066  **/
3067 static void
lpfc_cmpl_els_disc_cmd(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)3068 lpfc_cmpl_els_disc_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3069 		       struct lpfc_iocbq *rspiocb)
3070 {
3071 	struct lpfc_vport *vport = cmdiocb->vport;
3072 	IOCB_t *irsp;
3073 	struct lpfc_els_rdf_rsp *prdf;
3074 	struct lpfc_dmabuf *pcmd, *prsp;
3075 	u32 *pdata;
3076 	u32 cmd;
3077 
3078 	irsp = &rspiocb->iocb;
3079 
3080 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3081 		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
3082 		irsp->ulpStatus, irsp->un.ulpWord[4],
3083 		irsp->un.elsreq64.remoteID);
3084 	/* ELS cmd tag <ulpIoTag> completes */
3085 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3086 			 "0217 ELS cmd tag x%x completes Data: x%x x%x x%x "
3087 			 "x%x\n",
3088 			 irsp->ulpIoTag, irsp->ulpStatus,
3089 			 irsp->un.ulpWord[4], irsp->ulpTimeout,
3090 			 cmdiocb->retry);
3091 
3092 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
3093 	if (!pcmd)
3094 		goto out;
3095 
3096 	pdata = (u32 *)pcmd->virt;
3097 	if (!pdata)
3098 		goto out;
3099 	cmd = *pdata;
3100 
3101 	/* Only 1 retry for ELS Timeout only */
3102 	if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT &&
3103 	    ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3104 	    IOERR_SEQUENCE_TIMEOUT)) {
3105 		cmdiocb->retry++;
3106 		if (cmdiocb->retry <= 1) {
3107 			switch (cmd) {
3108 			case ELS_CMD_SCR:
3109 				lpfc_issue_els_scr(vport, cmdiocb->retry);
3110 				break;
3111 			case ELS_CMD_RDF:
3112 				cmdiocb->context1 = NULL; /* save ndlp refcnt */
3113 				lpfc_issue_els_rdf(vport, cmdiocb->retry);
3114 				break;
3115 			}
3116 			goto out;
3117 		}
3118 		phba->fc_stat.elsRetryExceeded++;
3119 	}
3120 	if (irsp->ulpStatus) {
3121 		/* ELS discovery cmd completes with error */
3122 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
3123 				 "4203 ELS cmd x%x error: x%x x%X\n", cmd,
3124 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
3125 		goto out;
3126 	}
3127 
3128 	/* The RDF response doesn't have any impact on the running driver
3129 	 * but the notification descriptors are dumped here for support.
3130 	 */
3131 	if (cmd == ELS_CMD_RDF) {
3132 		int i;
3133 
3134 		prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
3135 		if (!prsp)
3136 			goto out;
3137 
3138 		prdf = (struct lpfc_els_rdf_rsp *)prsp->virt;
3139 		if (!prdf)
3140 			goto out;
3141 
3142 		for (i = 0; i < ELS_RDF_REG_TAG_CNT &&
3143 			    i < be32_to_cpu(prdf->reg_d1.reg_desc.count); i++)
3144 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3145 				 "4677 Fabric RDF Notification Grant Data: "
3146 				 "0x%08x\n",
3147 				 be32_to_cpu(
3148 					prdf->reg_d1.desc_tags[i]));
3149 	}
3150 
3151 out:
3152 	/* Check to see if link went down during discovery */
3153 	lpfc_els_chk_latt(vport);
3154 	lpfc_els_free_iocb(phba, cmdiocb);
3155 	return;
3156 }
3157 
3158 /**
3159  * lpfc_issue_els_scr - Issue a scr to an node on a vport
3160  * @vport: pointer to a host virtual N_Port data structure.
3161  * @retry: retry counter for the command IOCB.
3162  *
3163  * This routine issues a State Change Request (SCR) to a fabric node
3164  * on a @vport. The remote node is Fabric Controller (0xfffffd). It
3165  * first search the @vport node list to find the matching ndlp. If no such
3166  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
3167  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
3168  * routine is invoked to send the SCR IOCB.
3169  *
3170  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3171  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3172  * will be stored into the context1 field of the IOCB for the completion
3173  * callback function to the SCR ELS command.
3174  *
3175  * Return code
3176  *   0 - Successfully issued scr command
3177  *   1 - Failed to issue scr command
3178  **/
3179 int
lpfc_issue_els_scr(struct lpfc_vport * vport,uint8_t retry)3180 lpfc_issue_els_scr(struct lpfc_vport *vport, uint8_t retry)
3181 {
3182 	struct lpfc_hba  *phba = vport->phba;
3183 	struct lpfc_iocbq *elsiocb;
3184 	uint8_t *pcmd;
3185 	uint16_t cmdsize;
3186 	struct lpfc_nodelist *ndlp;
3187 
3188 	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
3189 
3190 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3191 	if (!ndlp) {
3192 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3193 		if (!ndlp)
3194 			return 1;
3195 		lpfc_enqueue_node(vport, ndlp);
3196 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3197 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3198 		if (!ndlp)
3199 			return 1;
3200 	}
3201 
3202 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3203 				     ndlp->nlp_DID, ELS_CMD_SCR);
3204 
3205 	if (!elsiocb) {
3206 		/* This will trigger the release of the node just
3207 		 * allocated
3208 		 */
3209 		lpfc_nlp_put(ndlp);
3210 		return 1;
3211 	}
3212 
3213 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3214 
3215 	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
3216 	pcmd += sizeof(uint32_t);
3217 
3218 	/* For SCR, remainder of payload is SCR parameter page */
3219 	memset(pcmd, 0, sizeof(SCR));
3220 	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
3221 
3222 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3223 		"Issue SCR:       did:x%x",
3224 		ndlp->nlp_DID, 0, 0);
3225 
3226 	phba->fc_stat.elsXmitSCR++;
3227 	elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3228 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3229 	    IOCB_ERROR) {
3230 		/* The additional lpfc_nlp_put will cause the following
3231 		 * lpfc_els_free_iocb routine to trigger the rlease of
3232 		 * the node.
3233 		 */
3234 		lpfc_nlp_put(ndlp);
3235 		lpfc_els_free_iocb(phba, elsiocb);
3236 		return 1;
3237 	}
3238 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
3239 	 * trigger the release of node.
3240 	 */
3241 	if (!(vport->fc_flag & FC_PT2PT))
3242 		lpfc_nlp_put(ndlp);
3243 	return 0;
3244 }
3245 
3246 /**
3247  * lpfc_issue_els_rscn - Issue an RSCN to the Fabric Controller (Fabric)
3248  *   or the other nport (pt2pt).
3249  * @vport: pointer to a host virtual N_Port data structure.
3250  * @retry: number of retries to the command IOCB.
3251  *
3252  * This routine issues a RSCN to the Fabric Controller (DID 0xFFFFFD)
3253  *  when connected to a fabric, or to the remote port when connected
3254  *  in point-to-point mode. When sent to the Fabric Controller, it will
3255  *  replay the RSCN to registered recipients.
3256  *
3257  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3258  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3259  * will be stored into the context1 field of the IOCB for the completion
3260  * callback function to the RSCN ELS command.
3261  *
3262  * Return code
3263  *   0 - Successfully issued RSCN command
3264  *   1 - Failed to issue RSCN command
3265  **/
3266 int
lpfc_issue_els_rscn(struct lpfc_vport * vport,uint8_t retry)3267 lpfc_issue_els_rscn(struct lpfc_vport *vport, uint8_t retry)
3268 {
3269 	struct lpfc_hba *phba = vport->phba;
3270 	struct lpfc_iocbq *elsiocb;
3271 	struct lpfc_nodelist *ndlp;
3272 	struct {
3273 		struct fc_els_rscn rscn;
3274 		struct fc_els_rscn_page portid;
3275 	} *event;
3276 	uint32_t nportid;
3277 	uint16_t cmdsize = sizeof(*event);
3278 
3279 	/* Not supported for private loop */
3280 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
3281 	    !(vport->fc_flag & FC_PUBLIC_LOOP))
3282 		return 1;
3283 
3284 	if (vport->fc_flag & FC_PT2PT) {
3285 		/* find any mapped nport - that would be the other nport */
3286 		ndlp = lpfc_findnode_mapped(vport);
3287 		if (!ndlp)
3288 			return 1;
3289 	} else {
3290 		nportid = FC_FID_FCTRL;
3291 		/* find the fabric controller node */
3292 		ndlp = lpfc_findnode_did(vport, nportid);
3293 		if (!ndlp) {
3294 			/* if one didn't exist, make one */
3295 			ndlp = lpfc_nlp_init(vport, nportid);
3296 			if (!ndlp)
3297 				return 1;
3298 			lpfc_enqueue_node(vport, ndlp);
3299 		} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3300 			ndlp = lpfc_enable_node(vport, ndlp,
3301 						NLP_STE_UNUSED_NODE);
3302 			if (!ndlp)
3303 				return 1;
3304 		}
3305 	}
3306 
3307 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3308 				     ndlp->nlp_DID, ELS_CMD_RSCN_XMT);
3309 
3310 	if (!elsiocb) {
3311 		/* This will trigger the release of the node just
3312 		 * allocated
3313 		 */
3314 		lpfc_nlp_put(ndlp);
3315 		return 1;
3316 	}
3317 
3318 	event = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
3319 
3320 	event->rscn.rscn_cmd = ELS_RSCN;
3321 	event->rscn.rscn_page_len = sizeof(struct fc_els_rscn_page);
3322 	event->rscn.rscn_plen = cpu_to_be16(cmdsize);
3323 
3324 	nportid = vport->fc_myDID;
3325 	/* appears that page flags must be 0 for fabric to broadcast RSCN */
3326 	event->portid.rscn_page_flags = 0;
3327 	event->portid.rscn_fid[0] = (nportid & 0x00FF0000) >> 16;
3328 	event->portid.rscn_fid[1] = (nportid & 0x0000FF00) >> 8;
3329 	event->portid.rscn_fid[2] = nportid & 0x000000FF;
3330 
3331 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3332 			      "Issue RSCN:       did:x%x",
3333 			      ndlp->nlp_DID, 0, 0);
3334 
3335 	phba->fc_stat.elsXmitRSCN++;
3336 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3337 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3338 	    IOCB_ERROR) {
3339 		/* The additional lpfc_nlp_put will cause the following
3340 		 * lpfc_els_free_iocb routine to trigger the rlease of
3341 		 * the node.
3342 		 */
3343 		lpfc_nlp_put(ndlp);
3344 		lpfc_els_free_iocb(phba, elsiocb);
3345 		return 1;
3346 	}
3347 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
3348 	 * trigger the release of node.
3349 	 */
3350 	if (!(vport->fc_flag & FC_PT2PT))
3351 		lpfc_nlp_put(ndlp);
3352 
3353 	return 0;
3354 }
3355 
3356 /**
3357  * lpfc_issue_els_farpr - Issue a farp to an node on a vport
3358  * @vport: pointer to a host virtual N_Port data structure.
3359  * @nportid: N_Port identifier to the remote node.
3360  * @retry: number of retries to the command IOCB.
3361  *
3362  * This routine issues a Fibre Channel Address Resolution Response
3363  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
3364  * is passed into the function. It first search the @vport node list to find
3365  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
3366  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
3367  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
3368  *
3369  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3370  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3371  * will be stored into the context1 field of the IOCB for the completion
3372  * callback function to the PARPR ELS command.
3373  *
3374  * Return code
3375  *   0 - Successfully issued farpr command
3376  *   1 - Failed to issue farpr command
3377  **/
3378 static int
lpfc_issue_els_farpr(struct lpfc_vport * vport,uint32_t nportid,uint8_t retry)3379 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
3380 {
3381 	struct lpfc_hba  *phba = vport->phba;
3382 	struct lpfc_iocbq *elsiocb;
3383 	FARP *fp;
3384 	uint8_t *pcmd;
3385 	uint32_t *lp;
3386 	uint16_t cmdsize;
3387 	struct lpfc_nodelist *ondlp;
3388 	struct lpfc_nodelist *ndlp;
3389 
3390 	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
3391 
3392 	ndlp = lpfc_findnode_did(vport, nportid);
3393 	if (!ndlp) {
3394 		ndlp = lpfc_nlp_init(vport, nportid);
3395 		if (!ndlp)
3396 			return 1;
3397 		lpfc_enqueue_node(vport, ndlp);
3398 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3399 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3400 		if (!ndlp)
3401 			return 1;
3402 	}
3403 
3404 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3405 				     ndlp->nlp_DID, ELS_CMD_RNID);
3406 	if (!elsiocb) {
3407 		/* This will trigger the release of the node just
3408 		 * allocated
3409 		 */
3410 		lpfc_nlp_put(ndlp);
3411 		return 1;
3412 	}
3413 
3414 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3415 
3416 	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
3417 	pcmd += sizeof(uint32_t);
3418 
3419 	/* Fill in FARPR payload */
3420 	fp = (FARP *) (pcmd);
3421 	memset(fp, 0, sizeof(FARP));
3422 	lp = (uint32_t *) pcmd;
3423 	*lp++ = be32_to_cpu(nportid);
3424 	*lp++ = be32_to_cpu(vport->fc_myDID);
3425 	fp->Rflags = 0;
3426 	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
3427 
3428 	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
3429 	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3430 	ondlp = lpfc_findnode_did(vport, nportid);
3431 	if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
3432 		memcpy(&fp->OportName, &ondlp->nlp_portname,
3433 		       sizeof(struct lpfc_name));
3434 		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
3435 		       sizeof(struct lpfc_name));
3436 	}
3437 
3438 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3439 		"Issue FARPR:     did:x%x",
3440 		ndlp->nlp_DID, 0, 0);
3441 
3442 	phba->fc_stat.elsXmitFARPR++;
3443 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
3444 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3445 	    IOCB_ERROR) {
3446 		/* The additional lpfc_nlp_put will cause the following
3447 		 * lpfc_els_free_iocb routine to trigger the release of
3448 		 * the node.
3449 		 */
3450 		lpfc_nlp_put(ndlp);
3451 		lpfc_els_free_iocb(phba, elsiocb);
3452 		return 1;
3453 	}
3454 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
3455 	 * trigger the release of the node.
3456 	 */
3457 	/* Don't release reference count as RDF is likely outstanding */
3458 	return 0;
3459 }
3460 
3461 /**
3462  * lpfc_issue_els_rdf - Register for diagnostic functions from the fabric.
3463  * @vport: pointer to a host virtual N_Port data structure.
3464  * @retry: retry counter for the command IOCB.
3465  *
3466  * This routine issues an ELS RDF to the Fabric Controller to register
3467  * for diagnostic functions.
3468  *
3469  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3470  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3471  * will be stored into the context1 field of the IOCB for the completion
3472  * callback function to the RDF ELS command.
3473  *
3474  * Return code
3475  *   0 - Successfully issued rdf command
3476  *   1 - Failed to issue rdf command
3477  **/
3478 int
lpfc_issue_els_rdf(struct lpfc_vport * vport,uint8_t retry)3479 lpfc_issue_els_rdf(struct lpfc_vport *vport, uint8_t retry)
3480 {
3481 	struct lpfc_hba *phba = vport->phba;
3482 	struct lpfc_iocbq *elsiocb;
3483 	struct lpfc_els_rdf_req *prdf;
3484 	struct lpfc_nodelist *ndlp;
3485 	uint16_t cmdsize;
3486 
3487 	cmdsize = sizeof(*prdf);
3488 
3489 	ndlp = lpfc_findnode_did(vport, Fabric_Cntl_DID);
3490 	if (!ndlp) {
3491 		ndlp = lpfc_nlp_init(vport, Fabric_Cntl_DID);
3492 		if (!ndlp)
3493 			return -ENODEV;
3494 		lpfc_enqueue_node(vport, ndlp);
3495 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
3496 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3497 		if (!ndlp)
3498 			return -ENODEV;
3499 	}
3500 
3501 	/* RDF ELS is not required on an NPIV VN_Port.  */
3502 	if (vport->port_type == LPFC_NPIV_PORT) {
3503 		lpfc_nlp_put(ndlp);
3504 		return -EACCES;
3505 	}
3506 
3507 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
3508 				     ndlp->nlp_DID, ELS_CMD_RDF);
3509 	if (!elsiocb) {
3510 		/* This will trigger the release of the node just
3511 		 * allocated
3512 		 */
3513 		lpfc_nlp_put(ndlp);
3514 		return -ENOMEM;
3515 	}
3516 
3517 	/* Configure the payload for the supported FPIN events. */
3518 	prdf = (struct lpfc_els_rdf_req *)
3519 		(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
3520 	memset(prdf, 0, cmdsize);
3521 	prdf->rdf.fpin_cmd = ELS_RDF;
3522 	prdf->rdf.desc_len = cpu_to_be32(sizeof(struct lpfc_els_rdf_req) -
3523 					 sizeof(struct fc_els_rdf));
3524 	prdf->reg_d1.reg_desc.desc_tag = cpu_to_be32(ELS_DTAG_FPIN_REGISTER);
3525 	prdf->reg_d1.reg_desc.desc_len = cpu_to_be32(
3526 				FC_TLV_DESC_LENGTH_FROM_SZ(prdf->reg_d1));
3527 	prdf->reg_d1.reg_desc.count = cpu_to_be32(ELS_RDF_REG_TAG_CNT);
3528 	prdf->reg_d1.desc_tags[0] = cpu_to_be32(ELS_DTAG_LNK_INTEGRITY);
3529 	prdf->reg_d1.desc_tags[1] = cpu_to_be32(ELS_DTAG_DELIVERY);
3530 	prdf->reg_d1.desc_tags[2] = cpu_to_be32(ELS_DTAG_PEER_CONGEST);
3531 	prdf->reg_d1.desc_tags[3] = cpu_to_be32(ELS_DTAG_CONGESTION);
3532 
3533 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3534 			      "Issue RDF:       did:x%x",
3535 			      ndlp->nlp_DID, 0, 0);
3536 
3537 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3538 			 "6444 Xmit RDF to remote NPORT x%x\n",
3539 			 ndlp->nlp_DID);
3540 
3541 	elsiocb->iocb_cmpl = lpfc_cmpl_els_disc_cmd;
3542 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
3543 	    IOCB_ERROR) {
3544 		/* The additional lpfc_nlp_put will cause the following
3545 		 * lpfc_els_free_iocb routine to trigger the rlease of
3546 		 * the node.
3547 		 */
3548 		lpfc_nlp_put(ndlp);
3549 		lpfc_els_free_iocb(phba, elsiocb);
3550 		return -EIO;
3551 	}
3552 
3553 	/* An RDF was issued - this put ensures the ndlp is cleaned up
3554 	 * when the RDF completes.
3555 	 */
3556 	lpfc_nlp_put(ndlp);
3557 	return 0;
3558 }
3559 
3560 /**
3561  * lpfc_cancel_retry_delay_tmo - Cancel the timer with delayed iocb-cmd retry
3562  * @vport: pointer to a host virtual N_Port data structure.
3563  * @nlp: pointer to a node-list data structure.
3564  *
3565  * This routine cancels the timer with a delayed IOCB-command retry for
3566  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
3567  * removes the ELS retry event if it presents. In addition, if the
3568  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
3569  * commands are sent for the @vport's nodes that require issuing discovery
3570  * ADISC.
3571  **/
3572 void
lpfc_cancel_retry_delay_tmo(struct lpfc_vport * vport,struct lpfc_nodelist * nlp)3573 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
3574 {
3575 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3576 	struct lpfc_work_evt *evtp;
3577 
3578 	if (!(nlp->nlp_flag & NLP_DELAY_TMO))
3579 		return;
3580 	spin_lock_irq(shost->host_lock);
3581 	nlp->nlp_flag &= ~NLP_DELAY_TMO;
3582 	spin_unlock_irq(shost->host_lock);
3583 	del_timer_sync(&nlp->nlp_delayfunc);
3584 	nlp->nlp_last_elscmd = 0;
3585 	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
3586 		list_del_init(&nlp->els_retry_evt.evt_listp);
3587 		/* Decrement nlp reference count held for the delayed retry */
3588 		evtp = &nlp->els_retry_evt;
3589 		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
3590 	}
3591 	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
3592 		spin_lock_irq(shost->host_lock);
3593 		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
3594 		spin_unlock_irq(shost->host_lock);
3595 		if (vport->num_disc_nodes) {
3596 			if (vport->port_state < LPFC_VPORT_READY) {
3597 				/* Check if there are more ADISCs to be sent */
3598 				lpfc_more_adisc(vport);
3599 			} else {
3600 				/* Check if there are more PLOGIs to be sent */
3601 				lpfc_more_plogi(vport);
3602 				if (vport->num_disc_nodes == 0) {
3603 					spin_lock_irq(shost->host_lock);
3604 					vport->fc_flag &= ~FC_NDISC_ACTIVE;
3605 					spin_unlock_irq(shost->host_lock);
3606 					lpfc_can_disctmo(vport);
3607 					lpfc_end_rscn(vport);
3608 				}
3609 			}
3610 		}
3611 	}
3612 	return;
3613 }
3614 
3615 /**
3616  * lpfc_els_retry_delay - Timer function with a ndlp delayed function timer
3617  * @t: pointer to the timer function associated data (ndlp).
3618  *
3619  * This routine is invoked by the ndlp delayed-function timer to check
3620  * whether there is any pending ELS retry event(s) with the node. If not, it
3621  * simply returns. Otherwise, if there is at least one ELS delayed event, it
3622  * adds the delayed events to the HBA work list and invokes the
3623  * lpfc_worker_wake_up() routine to wake up worker thread to process the
3624  * event. Note that lpfc_nlp_get() is called before posting the event to
3625  * the work list to hold reference count of ndlp so that it guarantees the
3626  * reference to ndlp will still be available when the worker thread gets
3627  * to the event associated with the ndlp.
3628  **/
3629 void
lpfc_els_retry_delay(struct timer_list * t)3630 lpfc_els_retry_delay(struct timer_list *t)
3631 {
3632 	struct lpfc_nodelist *ndlp = from_timer(ndlp, t, nlp_delayfunc);
3633 	struct lpfc_vport *vport = ndlp->vport;
3634 	struct lpfc_hba   *phba = vport->phba;
3635 	unsigned long flags;
3636 	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
3637 
3638 	spin_lock_irqsave(&phba->hbalock, flags);
3639 	if (!list_empty(&evtp->evt_listp)) {
3640 		spin_unlock_irqrestore(&phba->hbalock, flags);
3641 		return;
3642 	}
3643 
3644 	/* We need to hold the node by incrementing the reference
3645 	 * count until the queued work is done
3646 	 */
3647 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
3648 	if (evtp->evt_arg1) {
3649 		evtp->evt = LPFC_EVT_ELS_RETRY;
3650 		list_add_tail(&evtp->evt_listp, &phba->work_list);
3651 		lpfc_worker_wake_up(phba);
3652 	}
3653 	spin_unlock_irqrestore(&phba->hbalock, flags);
3654 	return;
3655 }
3656 
3657 /**
3658  * lpfc_els_retry_delay_handler - Work thread handler for ndlp delayed function
3659  * @ndlp: pointer to a node-list data structure.
3660  *
3661  * This routine is the worker-thread handler for processing the @ndlp delayed
3662  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
3663  * the last ELS command from the associated ndlp and invokes the proper ELS
3664  * function according to the delayed ELS command to retry the command.
3665  **/
3666 void
lpfc_els_retry_delay_handler(struct lpfc_nodelist * ndlp)3667 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
3668 {
3669 	struct lpfc_vport *vport = ndlp->vport;
3670 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3671 	uint32_t cmd, retry;
3672 
3673 	spin_lock_irq(shost->host_lock);
3674 	cmd = ndlp->nlp_last_elscmd;
3675 	ndlp->nlp_last_elscmd = 0;
3676 
3677 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
3678 		spin_unlock_irq(shost->host_lock);
3679 		return;
3680 	}
3681 
3682 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
3683 	spin_unlock_irq(shost->host_lock);
3684 	/*
3685 	 * If a discovery event readded nlp_delayfunc after timer
3686 	 * firing and before processing the timer, cancel the
3687 	 * nlp_delayfunc.
3688 	 */
3689 	del_timer_sync(&ndlp->nlp_delayfunc);
3690 	retry = ndlp->nlp_retry;
3691 	ndlp->nlp_retry = 0;
3692 
3693 	switch (cmd) {
3694 	case ELS_CMD_FLOGI:
3695 		lpfc_issue_els_flogi(vport, ndlp, retry);
3696 		break;
3697 	case ELS_CMD_PLOGI:
3698 		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
3699 			ndlp->nlp_prev_state = ndlp->nlp_state;
3700 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3701 		}
3702 		break;
3703 	case ELS_CMD_ADISC:
3704 		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
3705 			ndlp->nlp_prev_state = ndlp->nlp_state;
3706 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3707 		}
3708 		break;
3709 	case ELS_CMD_PRLI:
3710 	case ELS_CMD_NVMEPRLI:
3711 		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
3712 			ndlp->nlp_prev_state = ndlp->nlp_state;
3713 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
3714 		}
3715 		break;
3716 	case ELS_CMD_LOGO:
3717 		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
3718 			ndlp->nlp_prev_state = ndlp->nlp_state;
3719 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
3720 		}
3721 		break;
3722 	case ELS_CMD_FDISC:
3723 		if (!(vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI))
3724 			lpfc_issue_els_fdisc(vport, ndlp, retry);
3725 		break;
3726 	}
3727 	return;
3728 }
3729 
3730 /**
3731  * lpfc_link_reset - Issue link reset
3732  * @vport: pointer to a virtual N_Port data structure.
3733  *
3734  * This routine performs link reset by sending INIT_LINK mailbox command.
3735  * For SLI-3 adapter, link attention interrupt is enabled before issuing
3736  * INIT_LINK mailbox command.
3737  *
3738  * Return code
3739  *   0 - Link reset initiated successfully
3740  *   1 - Failed to initiate link reset
3741  **/
3742 int
lpfc_link_reset(struct lpfc_vport * vport)3743 lpfc_link_reset(struct lpfc_vport *vport)
3744 {
3745 	struct lpfc_hba *phba = vport->phba;
3746 	LPFC_MBOXQ_t *mbox;
3747 	uint32_t control;
3748 	int rc;
3749 
3750 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3751 			 "2851 Attempt link reset\n");
3752 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3753 	if (!mbox) {
3754 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3755 				"2852 Failed to allocate mbox memory");
3756 		return 1;
3757 	}
3758 
3759 	/* Enable Link attention interrupts */
3760 	if (phba->sli_rev <= LPFC_SLI_REV3) {
3761 		spin_lock_irq(&phba->hbalock);
3762 		phba->sli.sli_flag |= LPFC_PROCESS_LA;
3763 		control = readl(phba->HCregaddr);
3764 		control |= HC_LAINT_ENA;
3765 		writel(control, phba->HCregaddr);
3766 		readl(phba->HCregaddr); /* flush */
3767 		spin_unlock_irq(&phba->hbalock);
3768 	}
3769 
3770 	lpfc_init_link(phba, mbox, phba->cfg_topology,
3771 		       phba->cfg_link_speed);
3772 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3773 	mbox->vport = vport;
3774 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3775 	if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
3776 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3777 				"2853 Failed to issue INIT_LINK "
3778 				"mbox command, rc:x%x\n", rc);
3779 		mempool_free(mbox, phba->mbox_mem_pool);
3780 		return 1;
3781 	}
3782 
3783 	return 0;
3784 }
3785 
3786 /**
3787  * lpfc_els_retry - Make retry decision on an els command iocb
3788  * @phba: pointer to lpfc hba data structure.
3789  * @cmdiocb: pointer to lpfc command iocb data structure.
3790  * @rspiocb: pointer to lpfc response iocb data structure.
3791  *
3792  * This routine makes a retry decision on an ELS command IOCB, which has
3793  * failed. The following ELS IOCBs use this function for retrying the command
3794  * when previously issued command responsed with error status: FLOGI, PLOGI,
3795  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
3796  * returned error status, it makes the decision whether a retry shall be
3797  * issued for the command, and whether a retry shall be made immediately or
3798  * delayed. In the former case, the corresponding ELS command issuing-function
3799  * is called to retry the command. In the later case, the ELS command shall
3800  * be posted to the ndlp delayed event and delayed function timer set to the
3801  * ndlp for the delayed command issusing.
3802  *
3803  * Return code
3804  *   0 - No retry of els command is made
3805  *   1 - Immediate or delayed retry of els command is made
3806  **/
3807 static int
lpfc_els_retry(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)3808 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3809 	       struct lpfc_iocbq *rspiocb)
3810 {
3811 	struct lpfc_vport *vport = cmdiocb->vport;
3812 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3813 	IOCB_t *irsp = &rspiocb->iocb;
3814 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
3815 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3816 	uint32_t *elscmd;
3817 	struct ls_rjt stat;
3818 	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
3819 	int logerr = 0;
3820 	uint32_t cmd = 0;
3821 	uint32_t did;
3822 	int link_reset = 0, rc;
3823 
3824 
3825 	/* Note: context2 may be 0 for internal driver abort
3826 	 * of delays ELS command.
3827 	 */
3828 
3829 	if (pcmd && pcmd->virt) {
3830 		elscmd = (uint32_t *) (pcmd->virt);
3831 		cmd = *elscmd++;
3832 	}
3833 
3834 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
3835 		did = ndlp->nlp_DID;
3836 	else {
3837 		/* We should only hit this case for retrying PLOGI */
3838 		did = irsp->un.elsreq64.remoteID;
3839 		ndlp = lpfc_findnode_did(vport, did);
3840 		if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
3841 		    && (cmd != ELS_CMD_PLOGI))
3842 			return 1;
3843 	}
3844 
3845 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
3846 		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
3847 		*(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
3848 
3849 	switch (irsp->ulpStatus) {
3850 	case IOSTAT_FCP_RSP_ERROR:
3851 		break;
3852 	case IOSTAT_REMOTE_STOP:
3853 		if (phba->sli_rev == LPFC_SLI_REV4) {
3854 			/* This IO was aborted by the target, we don't
3855 			 * know the rxid and because we did not send the
3856 			 * ABTS we cannot generate and RRQ.
3857 			 */
3858 			lpfc_set_rrq_active(phba, ndlp,
3859 					 cmdiocb->sli4_lxritag, 0, 0);
3860 		}
3861 		break;
3862 	case IOSTAT_LOCAL_REJECT:
3863 		switch ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK)) {
3864 		case IOERR_LOOP_OPEN_FAILURE:
3865 			if (cmd == ELS_CMD_FLOGI) {
3866 				if (PCI_DEVICE_ID_HORNET ==
3867 					phba->pcidev->device) {
3868 					phba->fc_topology = LPFC_TOPOLOGY_LOOP;
3869 					phba->pport->fc_myDID = 0;
3870 					phba->alpa_map[0] = 0;
3871 					phba->alpa_map[1] = 0;
3872 				}
3873 			}
3874 			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
3875 				delay = 1000;
3876 			retry = 1;
3877 			break;
3878 
3879 		case IOERR_ILLEGAL_COMMAND:
3880 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3881 					 "0124 Retry illegal cmd x%x "
3882 					 "retry:x%x delay:x%x\n",
3883 					 cmd, cmdiocb->retry, delay);
3884 			retry = 1;
3885 			/* All command's retry policy */
3886 			maxretry = 8;
3887 			if (cmdiocb->retry > 2)
3888 				delay = 1000;
3889 			break;
3890 
3891 		case IOERR_NO_RESOURCES:
3892 			logerr = 1; /* HBA out of resources */
3893 			retry = 1;
3894 			if (cmdiocb->retry > 100)
3895 				delay = 100;
3896 			maxretry = 250;
3897 			break;
3898 
3899 		case IOERR_ILLEGAL_FRAME:
3900 			delay = 100;
3901 			retry = 1;
3902 			break;
3903 
3904 		case IOERR_INVALID_RPI:
3905 			if (cmd == ELS_CMD_PLOGI &&
3906 			    did == NameServer_DID) {
3907 				/* Continue forever if plogi to */
3908 				/* the nameserver fails */
3909 				maxretry = 0;
3910 				delay = 100;
3911 			}
3912 			retry = 1;
3913 			break;
3914 
3915 		case IOERR_SEQUENCE_TIMEOUT:
3916 			if (cmd == ELS_CMD_PLOGI &&
3917 			    did == NameServer_DID &&
3918 			    (cmdiocb->retry + 1) == maxretry) {
3919 				/* Reset the Link */
3920 				link_reset = 1;
3921 				break;
3922 			}
3923 			retry = 1;
3924 			delay = 100;
3925 			break;
3926 		}
3927 		break;
3928 
3929 	case IOSTAT_NPORT_RJT:
3930 	case IOSTAT_FABRIC_RJT:
3931 		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
3932 			retry = 1;
3933 			break;
3934 		}
3935 		break;
3936 
3937 	case IOSTAT_NPORT_BSY:
3938 	case IOSTAT_FABRIC_BSY:
3939 		logerr = 1; /* Fabric / Remote NPort out of resources */
3940 		retry = 1;
3941 		break;
3942 
3943 	case IOSTAT_LS_RJT:
3944 		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
3945 		/* Added for Vendor specifc support
3946 		 * Just keep retrying for these Rsn / Exp codes
3947 		 */
3948 		switch (stat.un.b.lsRjtRsnCode) {
3949 		case LSRJT_UNABLE_TPC:
3950 			/* The driver has a VALID PLOGI but the rport has
3951 			 * rejected the PRLI - can't do it now.  Delay
3952 			 * for 1 second and try again.
3953 			 *
3954 			 * However, if explanation is REQ_UNSUPPORTED there's
3955 			 * no point to retry PRLI.
3956 			 */
3957 			if ((cmd == ELS_CMD_PRLI || cmd == ELS_CMD_NVMEPRLI) &&
3958 			    stat.un.b.lsRjtRsnCodeExp !=
3959 			    LSEXP_REQ_UNSUPPORTED) {
3960 				delay = 1000;
3961 				maxretry = lpfc_max_els_tries + 1;
3962 				retry = 1;
3963 				break;
3964 			}
3965 
3966 			/* Legacy bug fix code for targets with PLOGI delays. */
3967 			if (stat.un.b.lsRjtRsnCodeExp ==
3968 			    LSEXP_CMD_IN_PROGRESS) {
3969 				if (cmd == ELS_CMD_PLOGI) {
3970 					delay = 1000;
3971 					maxretry = 48;
3972 				}
3973 				retry = 1;
3974 				break;
3975 			}
3976 			if (stat.un.b.lsRjtRsnCodeExp ==
3977 			    LSEXP_CANT_GIVE_DATA) {
3978 				if (cmd == ELS_CMD_PLOGI) {
3979 					delay = 1000;
3980 					maxretry = 48;
3981 				}
3982 				retry = 1;
3983 				break;
3984 			}
3985 			if (cmd == ELS_CMD_PLOGI) {
3986 				delay = 1000;
3987 				maxretry = lpfc_max_els_tries + 1;
3988 				retry = 1;
3989 				break;
3990 			}
3991 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
3992 			  (cmd == ELS_CMD_FDISC) &&
3993 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
3994 				lpfc_printf_vlog(vport, KERN_ERR,
3995 						 LOG_TRACE_EVENT,
3996 						 "0125 FDISC Failed (x%x). "
3997 						 "Fabric out of resources\n",
3998 						 stat.un.lsRjtError);
3999 				lpfc_vport_set_state(vport,
4000 						     FC_VPORT_NO_FABRIC_RSCS);
4001 			}
4002 			break;
4003 
4004 		case LSRJT_LOGICAL_BSY:
4005 			if ((cmd == ELS_CMD_PLOGI) ||
4006 			    (cmd == ELS_CMD_PRLI) ||
4007 			    (cmd == ELS_CMD_NVMEPRLI)) {
4008 				delay = 1000;
4009 				maxretry = 48;
4010 			} else if (cmd == ELS_CMD_FDISC) {
4011 				/* FDISC retry policy */
4012 				maxretry = 48;
4013 				if (cmdiocb->retry >= 32)
4014 					delay = 1000;
4015 			}
4016 			retry = 1;
4017 			break;
4018 
4019 		case LSRJT_LOGICAL_ERR:
4020 			/* There are some cases where switches return this
4021 			 * error when they are not ready and should be returning
4022 			 * Logical Busy. We should delay every time.
4023 			 */
4024 			if (cmd == ELS_CMD_FDISC &&
4025 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
4026 				maxretry = 3;
4027 				delay = 1000;
4028 				retry = 1;
4029 			} else if (cmd == ELS_CMD_FLOGI &&
4030 				   stat.un.b.lsRjtRsnCodeExp ==
4031 						LSEXP_NOTHING_MORE) {
4032 				vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
4033 				retry = 1;
4034 				lpfc_printf_vlog(vport, KERN_ERR,
4035 						 LOG_TRACE_EVENT,
4036 						 "0820 FLOGI Failed (x%x). "
4037 						 "BBCredit Not Supported\n",
4038 						 stat.un.lsRjtError);
4039 			}
4040 			break;
4041 
4042 		case LSRJT_PROTOCOL_ERR:
4043 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4044 			  (cmd == ELS_CMD_FDISC) &&
4045 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
4046 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
4047 			  ) {
4048 				lpfc_printf_vlog(vport, KERN_ERR,
4049 						 LOG_TRACE_EVENT,
4050 						 "0122 FDISC Failed (x%x). "
4051 						 "Fabric Detected Bad WWN\n",
4052 						 stat.un.lsRjtError);
4053 				lpfc_vport_set_state(vport,
4054 						     FC_VPORT_FABRIC_REJ_WWN);
4055 			}
4056 			break;
4057 		case LSRJT_VENDOR_UNIQUE:
4058 			if ((stat.un.b.vendorUnique == 0x45) &&
4059 			    (cmd == ELS_CMD_FLOGI)) {
4060 				goto out_retry;
4061 			}
4062 			break;
4063 		case LSRJT_CMD_UNSUPPORTED:
4064 			/* lpfc nvmet returns this type of LS_RJT when it
4065 			 * receives an FCP PRLI because lpfc nvmet only
4066 			 * support NVME.  ELS request is terminated for FCP4
4067 			 * on this rport.
4068 			 */
4069 			if (stat.un.b.lsRjtRsnCodeExp ==
4070 			    LSEXP_REQ_UNSUPPORTED && cmd == ELS_CMD_PRLI) {
4071 				spin_lock_irq(shost->host_lock);
4072 				ndlp->nlp_flag |= NLP_FCP_PRLI_RJT;
4073 				spin_unlock_irq(shost->host_lock);
4074 				retry = 0;
4075 				goto out_retry;
4076 			}
4077 			break;
4078 		}
4079 		break;
4080 
4081 	case IOSTAT_INTERMED_RSP:
4082 	case IOSTAT_BA_RJT:
4083 		break;
4084 
4085 	default:
4086 		break;
4087 	}
4088 
4089 	if (link_reset) {
4090 		rc = lpfc_link_reset(vport);
4091 		if (rc) {
4092 			/* Do not give up. Retry PLOGI one more time and attempt
4093 			 * link reset if PLOGI fails again.
4094 			 */
4095 			retry = 1;
4096 			delay = 100;
4097 			goto out_retry;
4098 		}
4099 		return 1;
4100 	}
4101 
4102 	if (did == FDMI_DID)
4103 		retry = 1;
4104 
4105 	if ((cmd == ELS_CMD_FLOGI) &&
4106 	    (phba->fc_topology != LPFC_TOPOLOGY_LOOP) &&
4107 	    !lpfc_error_lost_link(irsp)) {
4108 		/* FLOGI retry policy */
4109 		retry = 1;
4110 		/* retry FLOGI forever */
4111 		if (phba->link_flag != LS_LOOPBACK_MODE)
4112 			maxretry = 0;
4113 		else
4114 			maxretry = 2;
4115 
4116 		if (cmdiocb->retry >= 100)
4117 			delay = 5000;
4118 		else if (cmdiocb->retry >= 32)
4119 			delay = 1000;
4120 	} else if ((cmd == ELS_CMD_FDISC) && !lpfc_error_lost_link(irsp)) {
4121 		/* retry FDISCs every second up to devloss */
4122 		retry = 1;
4123 		maxretry = vport->cfg_devloss_tmo;
4124 		delay = 1000;
4125 	}
4126 
4127 	cmdiocb->retry++;
4128 	if (maxretry && (cmdiocb->retry >= maxretry)) {
4129 		phba->fc_stat.elsRetryExceeded++;
4130 		retry = 0;
4131 	}
4132 
4133 	if ((vport->load_flag & FC_UNLOADING) != 0)
4134 		retry = 0;
4135 
4136 out_retry:
4137 	if (retry) {
4138 		if ((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_FDISC)) {
4139 			/* Stop retrying PLOGI and FDISC if in FCF discovery */
4140 			if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
4141 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4142 						 "2849 Stop retry ELS command "
4143 						 "x%x to remote NPORT x%x, "
4144 						 "Data: x%x x%x\n", cmd, did,
4145 						 cmdiocb->retry, delay);
4146 				return 0;
4147 			}
4148 		}
4149 
4150 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
4151 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4152 				 "0107 Retry ELS command x%x to remote "
4153 				 "NPORT x%x Data: x%x x%x\n",
4154 				 cmd, did, cmdiocb->retry, delay);
4155 
4156 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
4157 			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
4158 			((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) !=
4159 			IOERR_NO_RESOURCES))) {
4160 			/* Don't reset timer for no resources */
4161 
4162 			/* If discovery / RSCN timer is running, reset it */
4163 			if (timer_pending(&vport->fc_disctmo) ||
4164 			    (vport->fc_flag & FC_RSCN_MODE))
4165 				lpfc_set_disctmo(vport);
4166 		}
4167 
4168 		phba->fc_stat.elsXmitRetry++;
4169 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
4170 			phba->fc_stat.elsDelayRetry++;
4171 			ndlp->nlp_retry = cmdiocb->retry;
4172 
4173 			/* delay is specified in milliseconds */
4174 			mod_timer(&ndlp->nlp_delayfunc,
4175 				jiffies + msecs_to_jiffies(delay));
4176 			spin_lock_irq(shost->host_lock);
4177 			ndlp->nlp_flag |= NLP_DELAY_TMO;
4178 			spin_unlock_irq(shost->host_lock);
4179 
4180 			ndlp->nlp_prev_state = ndlp->nlp_state;
4181 			if ((cmd == ELS_CMD_PRLI) ||
4182 			    (cmd == ELS_CMD_NVMEPRLI))
4183 				lpfc_nlp_set_state(vport, ndlp,
4184 					NLP_STE_PRLI_ISSUE);
4185 			else
4186 				lpfc_nlp_set_state(vport, ndlp,
4187 					NLP_STE_NPR_NODE);
4188 			ndlp->nlp_last_elscmd = cmd;
4189 
4190 			return 1;
4191 		}
4192 		switch (cmd) {
4193 		case ELS_CMD_FLOGI:
4194 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
4195 			return 1;
4196 		case ELS_CMD_FDISC:
4197 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
4198 			return 1;
4199 		case ELS_CMD_PLOGI:
4200 			if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
4201 				ndlp->nlp_prev_state = ndlp->nlp_state;
4202 				lpfc_nlp_set_state(vport, ndlp,
4203 						   NLP_STE_PLOGI_ISSUE);
4204 			}
4205 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
4206 			return 1;
4207 		case ELS_CMD_ADISC:
4208 			ndlp->nlp_prev_state = ndlp->nlp_state;
4209 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
4210 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
4211 			return 1;
4212 		case ELS_CMD_PRLI:
4213 		case ELS_CMD_NVMEPRLI:
4214 			ndlp->nlp_prev_state = ndlp->nlp_state;
4215 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
4216 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
4217 			return 1;
4218 		case ELS_CMD_LOGO:
4219 			ndlp->nlp_prev_state = ndlp->nlp_state;
4220 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_LOGO_ISSUE);
4221 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
4222 			return 1;
4223 		}
4224 	}
4225 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
4226 	if (logerr) {
4227 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4228 			 "0137 No retry ELS command x%x to remote "
4229 			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
4230 			 cmd, did, irsp->ulpStatus,
4231 			 irsp->un.ulpWord[4]);
4232 	}
4233 	else {
4234 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4235 			 "0108 No retry ELS command x%x to remote "
4236 			 "NPORT x%x Retried:%d Error:x%x/%x\n",
4237 			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
4238 			 irsp->un.ulpWord[4]);
4239 	}
4240 	return 0;
4241 }
4242 
4243 /**
4244  * lpfc_els_free_data - Free lpfc dma buffer and data structure with an iocb
4245  * @phba: pointer to lpfc hba data structure.
4246  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
4247  *
4248  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
4249  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
4250  * checks to see whether there is a lpfc DMA buffer associated with the
4251  * response of the command IOCB. If so, it will be released before releasing
4252  * the lpfc DMA buffer associated with the IOCB itself.
4253  *
4254  * Return code
4255  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4256  **/
4257 static int
lpfc_els_free_data(struct lpfc_hba * phba,struct lpfc_dmabuf * buf_ptr1)4258 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
4259 {
4260 	struct lpfc_dmabuf *buf_ptr;
4261 
4262 	/* Free the response before processing the command. */
4263 	if (!list_empty(&buf_ptr1->list)) {
4264 		list_remove_head(&buf_ptr1->list, buf_ptr,
4265 				 struct lpfc_dmabuf,
4266 				 list);
4267 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4268 		kfree(buf_ptr);
4269 	}
4270 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
4271 	kfree(buf_ptr1);
4272 	return 0;
4273 }
4274 
4275 /**
4276  * lpfc_els_free_bpl - Free lpfc dma buffer and data structure with bpl
4277  * @phba: pointer to lpfc hba data structure.
4278  * @buf_ptr: pointer to the lpfc dma buffer data structure.
4279  *
4280  * This routine releases the lpfc Direct Memory Access (DMA) buffer
4281  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
4282  * pool.
4283  *
4284  * Return code
4285  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
4286  **/
4287 static int
lpfc_els_free_bpl(struct lpfc_hba * phba,struct lpfc_dmabuf * buf_ptr)4288 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
4289 {
4290 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4291 	kfree(buf_ptr);
4292 	return 0;
4293 }
4294 
4295 /**
4296  * lpfc_els_free_iocb - Free a command iocb and its associated resources
4297  * @phba: pointer to lpfc hba data structure.
4298  * @elsiocb: pointer to lpfc els command iocb data structure.
4299  *
4300  * This routine frees a command IOCB and its associated resources. The
4301  * command IOCB data structure contains the reference to various associated
4302  * resources, these fields must be set to NULL if the associated reference
4303  * not present:
4304  *   context1 - reference to ndlp
4305  *   context2 - reference to cmd
4306  *   context2->next - reference to rsp
4307  *   context3 - reference to bpl
4308  *
4309  * It first properly decrements the reference count held on ndlp for the
4310  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
4311  * set, it invokes the lpfc_els_free_data() routine to release the Direct
4312  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
4313  * adds the DMA buffer the @phba data structure for the delayed release.
4314  * If reference to the Buffer Pointer List (BPL) is present, the
4315  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
4316  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
4317  * invoked to release the IOCB data structure back to @phba IOCBQ list.
4318  *
4319  * Return code
4320  *   0 - Success (currently, always return 0)
4321  **/
4322 int
lpfc_els_free_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * elsiocb)4323 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
4324 {
4325 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
4326 	struct lpfc_nodelist *ndlp;
4327 
4328 	ndlp = (struct lpfc_nodelist *)elsiocb->context1;
4329 	if (ndlp) {
4330 		if (ndlp->nlp_flag & NLP_DEFER_RM) {
4331 			lpfc_nlp_put(ndlp);
4332 
4333 			/* If the ndlp is not being used by another discovery
4334 			 * thread, free it.
4335 			 */
4336 			if (!lpfc_nlp_not_used(ndlp)) {
4337 				/* If ndlp is being used by another discovery
4338 				 * thread, just clear NLP_DEFER_RM
4339 				 */
4340 				ndlp->nlp_flag &= ~NLP_DEFER_RM;
4341 			}
4342 		}
4343 		else
4344 			lpfc_nlp_put(ndlp);
4345 		elsiocb->context1 = NULL;
4346 	}
4347 	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
4348 	if (elsiocb->context2) {
4349 		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
4350 			/* Firmware could still be in progress of DMAing
4351 			 * payload, so don't free data buffer till after
4352 			 * a hbeat.
4353 			 */
4354 			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
4355 			buf_ptr = elsiocb->context2;
4356 			elsiocb->context2 = NULL;
4357 			if (buf_ptr) {
4358 				buf_ptr1 = NULL;
4359 				spin_lock_irq(&phba->hbalock);
4360 				if (!list_empty(&buf_ptr->list)) {
4361 					list_remove_head(&buf_ptr->list,
4362 						buf_ptr1, struct lpfc_dmabuf,
4363 						list);
4364 					INIT_LIST_HEAD(&buf_ptr1->list);
4365 					list_add_tail(&buf_ptr1->list,
4366 						&phba->elsbuf);
4367 					phba->elsbuf_cnt++;
4368 				}
4369 				INIT_LIST_HEAD(&buf_ptr->list);
4370 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
4371 				phba->elsbuf_cnt++;
4372 				spin_unlock_irq(&phba->hbalock);
4373 			}
4374 		} else {
4375 			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
4376 			lpfc_els_free_data(phba, buf_ptr1);
4377 			elsiocb->context2 = NULL;
4378 		}
4379 	}
4380 
4381 	if (elsiocb->context3) {
4382 		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
4383 		lpfc_els_free_bpl(phba, buf_ptr);
4384 		elsiocb->context3 = NULL;
4385 	}
4386 	lpfc_sli_release_iocbq(phba, elsiocb);
4387 	return 0;
4388 }
4389 
4390 /**
4391  * lpfc_cmpl_els_logo_acc - Completion callback function to logo acc response
4392  * @phba: pointer to lpfc hba data structure.
4393  * @cmdiocb: pointer to lpfc command iocb data structure.
4394  * @rspiocb: pointer to lpfc response iocb data structure.
4395  *
4396  * This routine is the completion callback function to the Logout (LOGO)
4397  * Accept (ACC) Response ELS command. This routine is invoked to indicate
4398  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
4399  * release the ndlp if it has the last reference remaining (reference count
4400  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
4401  * field to NULL to inform the following lpfc_els_free_iocb() routine no
4402  * ndlp reference count needs to be decremented. Otherwise, the ndlp
4403  * reference use-count shall be decremented by the lpfc_els_free_iocb()
4404  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
4405  * IOCB data structure.
4406  **/
4407 static void
lpfc_cmpl_els_logo_acc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)4408 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4409 		       struct lpfc_iocbq *rspiocb)
4410 {
4411 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4412 	struct lpfc_vport *vport = cmdiocb->vport;
4413 	IOCB_t *irsp;
4414 
4415 	irsp = &rspiocb->iocb;
4416 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4417 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
4418 		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
4419 	/* ACC to LOGO completes to NPort <nlp_DID> */
4420 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4421 			 "0109 ACC to LOGO completes to NPort x%x "
4422 			 "Data: x%x x%x x%x\n",
4423 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4424 			 ndlp->nlp_rpi);
4425 
4426 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
4427 		/* NPort Recovery mode or node is just allocated */
4428 		if (!lpfc_nlp_not_used(ndlp)) {
4429 			/* If the ndlp is being used by another discovery
4430 			 * thread, just unregister the RPI.
4431 			 */
4432 			lpfc_unreg_rpi(vport, ndlp);
4433 		} else {
4434 			/* Indicate the node has already released, should
4435 			 * not reference to it from within lpfc_els_free_iocb.
4436 			 */
4437 			cmdiocb->context1 = NULL;
4438 		}
4439 	}
4440 
4441 	/*
4442 	 * The driver received a LOGO from the rport and has ACK'd it.
4443 	 * At this point, the driver is done so release the IOCB
4444 	 */
4445 	lpfc_els_free_iocb(phba, cmdiocb);
4446 }
4447 
4448 /**
4449  * lpfc_mbx_cmpl_dflt_rpi - Completion callbk func for unreg dflt rpi mbox cmd
4450  * @phba: pointer to lpfc hba data structure.
4451  * @pmb: pointer to the driver internal queue element for mailbox command.
4452  *
4453  * This routine is the completion callback function for unregister default
4454  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
4455  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
4456  * decrements the ndlp reference count held for this completion callback
4457  * function. After that, it invokes the lpfc_nlp_not_used() to check
4458  * whether there is only one reference left on the ndlp. If so, it will
4459  * perform one more decrement and trigger the release of the ndlp.
4460  **/
4461 void
lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)4462 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4463 {
4464 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4465 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4466 
4467 	pmb->ctx_buf = NULL;
4468 	pmb->ctx_ndlp = NULL;
4469 
4470 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
4471 	kfree(mp);
4472 	mempool_free(pmb, phba->mbox_mem_pool);
4473 	if (ndlp) {
4474 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4475 				 "0006 rpi%x DID:%x flg:%x %d map:%x x%px\n",
4476 				 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4477 				 kref_read(&ndlp->kref),
4478 				 ndlp->nlp_usg_map, ndlp);
4479 		if (NLP_CHK_NODE_ACT(ndlp)) {
4480 			lpfc_nlp_put(ndlp);
4481 			/* This is the end of the default RPI cleanup logic for
4482 			 * this ndlp. If no other discovery threads are using
4483 			 * this ndlp, free all resources associated with it.
4484 			 */
4485 			lpfc_nlp_not_used(ndlp);
4486 		} else {
4487 			lpfc_drop_node(ndlp->vport, ndlp);
4488 		}
4489 	}
4490 
4491 	return;
4492 }
4493 
4494 /**
4495  * lpfc_cmpl_els_rsp - Completion callback function for els response iocb cmd
4496  * @phba: pointer to lpfc hba data structure.
4497  * @cmdiocb: pointer to lpfc command iocb data structure.
4498  * @rspiocb: pointer to lpfc response iocb data structure.
4499  *
4500  * This routine is the completion callback function for ELS Response IOCB
4501  * command. In normal case, this callback function just properly sets the
4502  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
4503  * field in the command IOCB is not NULL, the referred mailbox command will
4504  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
4505  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
4506  * link down event occurred during the discovery, the lpfc_nlp_not_used()
4507  * routine shall be invoked trying to release the ndlp if no other threads
4508  * are currently referring it.
4509  **/
4510 static void
lpfc_cmpl_els_rsp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)4511 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4512 		  struct lpfc_iocbq *rspiocb)
4513 {
4514 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
4515 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
4516 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
4517 	IOCB_t  *irsp;
4518 	uint8_t *pcmd;
4519 	LPFC_MBOXQ_t *mbox = NULL;
4520 	struct lpfc_dmabuf *mp = NULL;
4521 	uint32_t ls_rjt = 0;
4522 
4523 	irsp = &rspiocb->iocb;
4524 
4525 	if (!vport) {
4526 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4527 				"3177 ELS response failed\n");
4528 		goto out;
4529 	}
4530 	if (cmdiocb->context_un.mbox)
4531 		mbox = cmdiocb->context_un.mbox;
4532 
4533 	/* First determine if this is a LS_RJT cmpl. Note, this callback
4534 	 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
4535 	 */
4536 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
4537 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4538 	    (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
4539 		/* A LS_RJT associated with Default RPI cleanup has its own
4540 		 * separate code path.
4541 		 */
4542 		if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4543 			ls_rjt = 1;
4544 	}
4545 
4546 	/* Check to see if link went down during discovery */
4547 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
4548 		if (mbox) {
4549 			mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4550 			if (mp) {
4551 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
4552 				kfree(mp);
4553 			}
4554 			mempool_free(mbox, phba->mbox_mem_pool);
4555 		}
4556 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
4557 		    (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
4558 			if (lpfc_nlp_not_used(ndlp)) {
4559 				ndlp = NULL;
4560 				/* Indicate the node has already released,
4561 				 * should not reference to it from within
4562 				 * the routine lpfc_els_free_iocb.
4563 				 */
4564 				cmdiocb->context1 = NULL;
4565 			}
4566 		goto out;
4567 	}
4568 
4569 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4570 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
4571 		irsp->ulpStatus, irsp->un.ulpWord[4],
4572 		cmdiocb->iocb.un.elsreq64.remoteID);
4573 	/* ELS response tag <ulpIoTag> completes */
4574 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4575 			 "0110 ELS response tag x%x completes "
4576 			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
4577 			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
4578 			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
4579 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4580 			 ndlp->nlp_rpi);
4581 	if (mbox) {
4582 		if ((rspiocb->iocb.ulpStatus == 0)
4583 		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
4584 			if (!lpfc_unreg_rpi(vport, ndlp) &&
4585 			    (!(vport->fc_flag & FC_PT2PT)) &&
4586 			    (ndlp->nlp_state ==  NLP_STE_PLOGI_ISSUE ||
4587 			     ndlp->nlp_state == NLP_STE_REG_LOGIN_ISSUE)) {
4588 				lpfc_printf_vlog(vport, KERN_INFO,
4589 					LOG_DISCOVERY,
4590 					"0314 PLOGI recov DID x%x "
4591 					"Data: x%x x%x x%x\n",
4592 					ndlp->nlp_DID, ndlp->nlp_state,
4593 					ndlp->nlp_rpi, ndlp->nlp_flag);
4594 				mp = mbox->ctx_buf;
4595 				if (mp) {
4596 					lpfc_mbuf_free(phba, mp->virt,
4597 						       mp->phys);
4598 					kfree(mp);
4599 				}
4600 				mempool_free(mbox, phba->mbox_mem_pool);
4601 				goto out;
4602 			}
4603 
4604 			/* Increment reference count to ndlp to hold the
4605 			 * reference to ndlp for the callback function.
4606 			 */
4607 			mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4608 			mbox->vport = vport;
4609 			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
4610 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4611 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4612 			}
4613 			else {
4614 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
4615 				ndlp->nlp_prev_state = ndlp->nlp_state;
4616 				lpfc_nlp_set_state(vport, ndlp,
4617 					   NLP_STE_REG_LOGIN_ISSUE);
4618 			}
4619 
4620 			ndlp->nlp_flag |= NLP_REG_LOGIN_SEND;
4621 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4622 			    != MBX_NOT_FINISHED)
4623 				goto out;
4624 
4625 			/* Decrement the ndlp reference count we
4626 			 * set for this failed mailbox command.
4627 			 */
4628 			lpfc_nlp_put(ndlp);
4629 			ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4630 
4631 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
4632 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4633 				"0138 ELS rsp: Cannot issue reg_login for x%x "
4634 				"Data: x%x x%x x%x\n",
4635 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4636 				ndlp->nlp_rpi);
4637 
4638 			if (lpfc_nlp_not_used(ndlp)) {
4639 				ndlp = NULL;
4640 				/* Indicate node has already been released,
4641 				 * should not reference to it from within
4642 				 * the routine lpfc_els_free_iocb.
4643 				 */
4644 				cmdiocb->context1 = NULL;
4645 			}
4646 		} else {
4647 			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
4648 			if (!lpfc_error_lost_link(irsp) &&
4649 			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
4650 				if (lpfc_nlp_not_used(ndlp)) {
4651 					ndlp = NULL;
4652 					/* Indicate node has already been
4653 					 * released, should not reference
4654 					 * to it from within the routine
4655 					 * lpfc_els_free_iocb.
4656 					 */
4657 					cmdiocb->context1 = NULL;
4658 				}
4659 			}
4660 		}
4661 		mp = (struct lpfc_dmabuf *)mbox->ctx_buf;
4662 		if (mp) {
4663 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
4664 			kfree(mp);
4665 		}
4666 		mempool_free(mbox, phba->mbox_mem_pool);
4667 	}
4668 out:
4669 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) && shost) {
4670 		spin_lock_irq(shost->host_lock);
4671 		if (mbox)
4672 			ndlp->nlp_flag &= ~NLP_ACC_REGLOGIN;
4673 		ndlp->nlp_flag &= ~NLP_RM_DFLT_RPI;
4674 		spin_unlock_irq(shost->host_lock);
4675 
4676 		/* If the node is not being used by another discovery thread,
4677 		 * and we are sending a reject, we are done with it.
4678 		 * Release driver reference count here and free associated
4679 		 * resources.
4680 		 */
4681 		if (ls_rjt)
4682 			if (lpfc_nlp_not_used(ndlp))
4683 				/* Indicate node has already been released,
4684 				 * should not reference to it from within
4685 				 * the routine lpfc_els_free_iocb.
4686 				 */
4687 				cmdiocb->context1 = NULL;
4688 
4689 	}
4690 
4691 	lpfc_els_free_iocb(phba, cmdiocb);
4692 	return;
4693 }
4694 
4695 /**
4696  * lpfc_els_rsp_acc - Prepare and issue an acc response iocb command
4697  * @vport: pointer to a host virtual N_Port data structure.
4698  * @flag: the els command code to be accepted.
4699  * @oldiocb: pointer to the original lpfc command iocb data structure.
4700  * @ndlp: pointer to a node-list data structure.
4701  * @mbox: pointer to the driver internal queue element for mailbox command.
4702  *
4703  * This routine prepares and issues an Accept (ACC) response IOCB
4704  * command. It uses the @flag to properly set up the IOCB field for the
4705  * specific ACC response command to be issued and invokes the
4706  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
4707  * @mbox pointer is passed in, it will be put into the context_un.mbox
4708  * field of the IOCB for the completion callback function to issue the
4709  * mailbox command to the HBA later when callback is invoked.
4710  *
4711  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4712  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4713  * will be stored into the context1 field of the IOCB for the completion
4714  * callback function to the corresponding response ELS IOCB command.
4715  *
4716  * Return code
4717  *   0 - Successfully issued acc response
4718  *   1 - Failed to issue acc response
4719  **/
4720 int
lpfc_els_rsp_acc(struct lpfc_vport * vport,uint32_t flag,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp,LPFC_MBOXQ_t * mbox)4721 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
4722 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4723 		 LPFC_MBOXQ_t *mbox)
4724 {
4725 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4726 	struct lpfc_hba  *phba = vport->phba;
4727 	IOCB_t *icmd;
4728 	IOCB_t *oldcmd;
4729 	struct lpfc_iocbq *elsiocb;
4730 	uint8_t *pcmd;
4731 	struct serv_parm *sp;
4732 	uint16_t cmdsize;
4733 	int rc;
4734 	ELS_PKT *els_pkt_ptr;
4735 
4736 	oldcmd = &oldiocb->iocb;
4737 
4738 	switch (flag) {
4739 	case ELS_CMD_ACC:
4740 		cmdsize = sizeof(uint32_t);
4741 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4742 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4743 		if (!elsiocb) {
4744 			spin_lock_irq(shost->host_lock);
4745 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4746 			spin_unlock_irq(shost->host_lock);
4747 			return 1;
4748 		}
4749 
4750 		icmd = &elsiocb->iocb;
4751 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
4752 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4753 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4754 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4755 		pcmd += sizeof(uint32_t);
4756 
4757 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4758 			"Issue ACC:       did:x%x flg:x%x",
4759 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
4760 		break;
4761 	case ELS_CMD_FLOGI:
4762 	case ELS_CMD_PLOGI:
4763 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
4764 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4765 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
4766 		if (!elsiocb)
4767 			return 1;
4768 
4769 		icmd = &elsiocb->iocb;
4770 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
4771 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4772 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4773 
4774 		if (mbox)
4775 			elsiocb->context_un.mbox = mbox;
4776 
4777 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4778 		pcmd += sizeof(uint32_t);
4779 		sp = (struct serv_parm *)pcmd;
4780 
4781 		if (flag == ELS_CMD_FLOGI) {
4782 			/* Copy the received service parameters back */
4783 			memcpy(sp, &phba->fc_fabparam,
4784 			       sizeof(struct serv_parm));
4785 
4786 			/* Clear the F_Port bit */
4787 			sp->cmn.fPort = 0;
4788 
4789 			/* Mark all class service parameters as invalid */
4790 			sp->cls1.classValid = 0;
4791 			sp->cls2.classValid = 0;
4792 			sp->cls3.classValid = 0;
4793 			sp->cls4.classValid = 0;
4794 
4795 			/* Copy our worldwide names */
4796 			memcpy(&sp->portName, &vport->fc_sparam.portName,
4797 			       sizeof(struct lpfc_name));
4798 			memcpy(&sp->nodeName, &vport->fc_sparam.nodeName,
4799 			       sizeof(struct lpfc_name));
4800 		} else {
4801 			memcpy(pcmd, &vport->fc_sparam,
4802 			       sizeof(struct serv_parm));
4803 
4804 			sp->cmn.valid_vendor_ver_level = 0;
4805 			memset(sp->un.vendorVersion, 0,
4806 			       sizeof(sp->un.vendorVersion));
4807 			sp->cmn.bbRcvSizeMsb &= 0xF;
4808 
4809 			/* If our firmware supports this feature, convey that
4810 			 * info to the target using the vendor specific field.
4811 			 */
4812 			if (phba->sli.sli_flag & LPFC_SLI_SUPPRESS_RSP) {
4813 				sp->cmn.valid_vendor_ver_level = 1;
4814 				sp->un.vv.vid = cpu_to_be32(LPFC_VV_EMLX_ID);
4815 				sp->un.vv.flags =
4816 					cpu_to_be32(LPFC_VV_SUPPRESS_RSP);
4817 			}
4818 		}
4819 
4820 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4821 			"Issue ACC FLOGI/PLOGI: did:x%x flg:x%x",
4822 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
4823 		break;
4824 	case ELS_CMD_PRLO:
4825 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
4826 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
4827 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
4828 		if (!elsiocb)
4829 			return 1;
4830 
4831 		icmd = &elsiocb->iocb;
4832 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
4833 		icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4834 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4835 
4836 		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
4837 		       sizeof(uint32_t) + sizeof(PRLO));
4838 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
4839 		els_pkt_ptr = (ELS_PKT *) pcmd;
4840 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
4841 
4842 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4843 			"Issue ACC PRLO:  did:x%x flg:x%x",
4844 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
4845 		break;
4846 	default:
4847 		return 1;
4848 	}
4849 	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
4850 		spin_lock_irq(shost->host_lock);
4851 		if (!(ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4852 			ndlp->nlp_flag & NLP_REG_LOGIN_SEND))
4853 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
4854 		spin_unlock_irq(shost->host_lock);
4855 		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
4856 	} else {
4857 		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4858 	}
4859 
4860 	phba->fc_stat.elsXmitACC++;
4861 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4862 	if (rc == IOCB_ERROR) {
4863 		lpfc_els_free_iocb(phba, elsiocb);
4864 		return 1;
4865 	}
4866 	return 0;
4867 }
4868 
4869 /**
4870  * lpfc_els_rsp_reject - Propare and issue a rjt response iocb command
4871  * @vport: pointer to a virtual N_Port data structure.
4872  * @rejectError: reject response to issue
4873  * @oldiocb: pointer to the original lpfc command iocb data structure.
4874  * @ndlp: pointer to a node-list data structure.
4875  * @mbox: pointer to the driver internal queue element for mailbox command.
4876  *
4877  * This routine prepares and issue an Reject (RJT) response IOCB
4878  * command. If a @mbox pointer is passed in, it will be put into the
4879  * context_un.mbox field of the IOCB for the completion callback function
4880  * to issue to the HBA later.
4881  *
4882  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4883  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4884  * will be stored into the context1 field of the IOCB for the completion
4885  * callback function to the reject response ELS IOCB command.
4886  *
4887  * Return code
4888  *   0 - Successfully issued reject response
4889  *   1 - Failed to issue reject response
4890  **/
4891 int
lpfc_els_rsp_reject(struct lpfc_vport * vport,uint32_t rejectError,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp,LPFC_MBOXQ_t * mbox)4892 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
4893 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
4894 		    LPFC_MBOXQ_t *mbox)
4895 {
4896 	struct lpfc_hba  *phba = vport->phba;
4897 	IOCB_t *icmd;
4898 	IOCB_t *oldcmd;
4899 	struct lpfc_iocbq *elsiocb;
4900 	uint8_t *pcmd;
4901 	uint16_t cmdsize;
4902 	int rc;
4903 
4904 	cmdsize = 2 * sizeof(uint32_t);
4905 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4906 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
4907 	if (!elsiocb)
4908 		return 1;
4909 
4910 	icmd = &elsiocb->iocb;
4911 	oldcmd = &oldiocb->iocb;
4912 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
4913 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4914 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4915 
4916 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
4917 	pcmd += sizeof(uint32_t);
4918 	*((uint32_t *) (pcmd)) = rejectError;
4919 
4920 	if (mbox)
4921 		elsiocb->context_un.mbox = mbox;
4922 
4923 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
4924 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4925 			 "0129 Xmit ELS RJT x%x response tag x%x "
4926 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4927 			 "rpi x%x\n",
4928 			 rejectError, elsiocb->iotag,
4929 			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
4930 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
4931 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
4932 		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
4933 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
4934 
4935 	phba->fc_stat.elsXmitLSRJT++;
4936 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4937 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
4938 
4939 	if (rc == IOCB_ERROR) {
4940 		lpfc_els_free_iocb(phba, elsiocb);
4941 		return 1;
4942 	}
4943 	return 0;
4944 }
4945 
4946 /**
4947  * lpfc_els_rsp_adisc_acc - Prepare and issue acc response to adisc iocb cmd
4948  * @vport: pointer to a virtual N_Port data structure.
4949  * @oldiocb: pointer to the original lpfc command iocb data structure.
4950  * @ndlp: pointer to a node-list data structure.
4951  *
4952  * This routine prepares and issues an Accept (ACC) response to Address
4953  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
4954  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
4955  *
4956  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4957  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4958  * will be stored into the context1 field of the IOCB for the completion
4959  * callback function to the ADISC Accept response ELS IOCB command.
4960  *
4961  * Return code
4962  *   0 - Successfully issued acc adisc response
4963  *   1 - Failed to issue adisc acc response
4964  **/
4965 int
lpfc_els_rsp_adisc_acc(struct lpfc_vport * vport,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)4966 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
4967 		       struct lpfc_nodelist *ndlp)
4968 {
4969 	struct lpfc_hba  *phba = vport->phba;
4970 	ADISC *ap;
4971 	IOCB_t *icmd, *oldcmd;
4972 	struct lpfc_iocbq *elsiocb;
4973 	uint8_t *pcmd;
4974 	uint16_t cmdsize;
4975 	int rc;
4976 
4977 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
4978 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4979 				     ndlp->nlp_DID, ELS_CMD_ACC);
4980 	if (!elsiocb)
4981 		return 1;
4982 
4983 	icmd = &elsiocb->iocb;
4984 	oldcmd = &oldiocb->iocb;
4985 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
4986 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
4987 
4988 	/* Xmit ADISC ACC response tag <ulpIoTag> */
4989 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4990 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
4991 			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
4992 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
4993 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4994 			 ndlp->nlp_rpi);
4995 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4996 
4997 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4998 	pcmd += sizeof(uint32_t);
4999 
5000 	ap = (ADISC *) (pcmd);
5001 	ap->hardAL_PA = phba->fc_pref_ALPA;
5002 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5003 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5004 	ap->DID = be32_to_cpu(vport->fc_myDID);
5005 
5006 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5007 		"Issue ACC ADISC: did:x%x flg:x%x",
5008 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
5009 
5010 	phba->fc_stat.elsXmitACC++;
5011 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5012 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5013 	if (rc == IOCB_ERROR) {
5014 		lpfc_els_free_iocb(phba, elsiocb);
5015 		return 1;
5016 	}
5017 
5018 	/* Xmit ELS ACC response tag <ulpIoTag> */
5019 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5020 			 "0128 Xmit ELS ACC response Status: x%x, IoTag: x%x, "
5021 			 "XRI: x%x, DID: x%x, nlp_flag: x%x nlp_state: x%x "
5022 			 "RPI: x%x, fc_flag x%x\n",
5023 			 rc, elsiocb->iotag, elsiocb->sli4_xritag,
5024 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5025 			 ndlp->nlp_rpi, vport->fc_flag);
5026 	return 0;
5027 }
5028 
5029 /**
5030  * lpfc_els_rsp_prli_acc - Prepare and issue acc response to prli iocb cmd
5031  * @vport: pointer to a virtual N_Port data structure.
5032  * @oldiocb: pointer to the original lpfc command iocb data structure.
5033  * @ndlp: pointer to a node-list data structure.
5034  *
5035  * This routine prepares and issues an Accept (ACC) response to Process
5036  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
5037  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
5038  *
5039  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5040  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5041  * will be stored into the context1 field of the IOCB for the completion
5042  * callback function to the PRLI Accept response ELS IOCB command.
5043  *
5044  * Return code
5045  *   0 - Successfully issued acc prli response
5046  *   1 - Failed to issue acc prli response
5047  **/
5048 int
lpfc_els_rsp_prli_acc(struct lpfc_vport * vport,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)5049 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
5050 		      struct lpfc_nodelist *ndlp)
5051 {
5052 	struct lpfc_hba  *phba = vport->phba;
5053 	PRLI *npr;
5054 	struct lpfc_nvme_prli *npr_nvme;
5055 	lpfc_vpd_t *vpd;
5056 	IOCB_t *icmd;
5057 	IOCB_t *oldcmd;
5058 	struct lpfc_iocbq *elsiocb;
5059 	uint8_t *pcmd;
5060 	uint16_t cmdsize;
5061 	uint32_t prli_fc4_req, *req_payload;
5062 	struct lpfc_dmabuf *req_buf;
5063 	int rc;
5064 	u32 elsrspcmd;
5065 
5066 	/* Need the incoming PRLI payload to determine if the ACC is for an
5067 	 * FC4 or NVME PRLI type.  The PRLI type is at word 1.
5068 	 */
5069 	req_buf = (struct lpfc_dmabuf *)oldiocb->context2;
5070 	req_payload = (((uint32_t *)req_buf->virt) + 1);
5071 
5072 	/* PRLI type payload is at byte 3 for FCP or NVME. */
5073 	prli_fc4_req = be32_to_cpu(*req_payload);
5074 	prli_fc4_req = (prli_fc4_req >> 24) & 0xff;
5075 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5076 			 "6127 PRLI_ACC:  Req Type x%x, Word1 x%08x\n",
5077 			 prli_fc4_req, *((uint32_t *)req_payload));
5078 
5079 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5080 		cmdsize = sizeof(uint32_t) + sizeof(PRLI);
5081 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
5082 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5083 		cmdsize = sizeof(uint32_t) + sizeof(struct lpfc_nvme_prli);
5084 		elsrspcmd = (ELS_CMD_ACC | (ELS_CMD_NVMEPRLI & ~ELS_RSP_MASK));
5085 	} else {
5086 		return 1;
5087 	}
5088 
5089 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5090 		ndlp->nlp_DID, elsrspcmd);
5091 	if (!elsiocb)
5092 		return 1;
5093 
5094 	icmd = &elsiocb->iocb;
5095 	oldcmd = &oldiocb->iocb;
5096 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5097 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5098 
5099 	/* Xmit PRLI ACC response tag <ulpIoTag> */
5100 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5101 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
5102 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
5103 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
5104 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5105 			 ndlp->nlp_rpi);
5106 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5107 	memset(pcmd, 0, cmdsize);
5108 
5109 	*((uint32_t *)(pcmd)) = elsrspcmd;
5110 	pcmd += sizeof(uint32_t);
5111 
5112 	/* For PRLI, remainder of payload is PRLI parameter page */
5113 	vpd = &phba->vpd;
5114 
5115 	if (prli_fc4_req == PRLI_FCP_TYPE) {
5116 		/*
5117 		 * If the remote port is a target and our firmware version
5118 		 * is 3.20 or later, set the following bits for FC-TAPE
5119 		 * support.
5120 		 */
5121 		npr = (PRLI *) pcmd;
5122 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5123 		    (vpd->rev.feaLevelHigh >= 0x02)) {
5124 			npr->ConfmComplAllowed = 1;
5125 			npr->Retry = 1;
5126 			npr->TaskRetryIdReq = 1;
5127 		}
5128 		npr->acceptRspCode = PRLI_REQ_EXECUTED;
5129 		npr->estabImagePair = 1;
5130 		npr->readXferRdyDis = 1;
5131 		npr->ConfmComplAllowed = 1;
5132 		npr->prliType = PRLI_FCP_TYPE;
5133 		npr->initiatorFunc = 1;
5134 	} else if (prli_fc4_req & PRLI_NVME_TYPE) {
5135 		/* Respond with an NVME PRLI Type */
5136 		npr_nvme = (struct lpfc_nvme_prli *) pcmd;
5137 		bf_set(prli_type_code, npr_nvme, PRLI_NVME_TYPE);
5138 		bf_set(prli_estabImagePair, npr_nvme, 0);  /* Should be 0 */
5139 		bf_set(prli_acc_rsp_code, npr_nvme, PRLI_REQ_EXECUTED);
5140 		if (phba->nvmet_support) {
5141 			bf_set(prli_tgt, npr_nvme, 1);
5142 			bf_set(prli_disc, npr_nvme, 1);
5143 			if (phba->cfg_nvme_enable_fb) {
5144 				bf_set(prli_fba, npr_nvme, 1);
5145 
5146 				/* TBD.  Target mode needs to post buffers
5147 				 * that support the configured first burst
5148 				 * byte size.
5149 				 */
5150 				bf_set(prli_fb_sz, npr_nvme,
5151 				       phba->cfg_nvmet_fb_size);
5152 			}
5153 		} else {
5154 			bf_set(prli_init, npr_nvme, 1);
5155 		}
5156 
5157 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
5158 				 "6015 NVME issue PRLI ACC word1 x%08x "
5159 				 "word4 x%08x word5 x%08x flag x%x, "
5160 				 "fcp_info x%x nlp_type x%x\n",
5161 				 npr_nvme->word1, npr_nvme->word4,
5162 				 npr_nvme->word5, ndlp->nlp_flag,
5163 				 ndlp->nlp_fcp_info, ndlp->nlp_type);
5164 		npr_nvme->word1 = cpu_to_be32(npr_nvme->word1);
5165 		npr_nvme->word4 = cpu_to_be32(npr_nvme->word4);
5166 		npr_nvme->word5 = cpu_to_be32(npr_nvme->word5);
5167 	} else
5168 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5169 				 "6128 Unknown FC_TYPE x%x x%x ndlp x%06x\n",
5170 				 prli_fc4_req, ndlp->nlp_fc4_type,
5171 				 ndlp->nlp_DID);
5172 
5173 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5174 		"Issue ACC PRLI:  did:x%x flg:x%x",
5175 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
5176 
5177 	phba->fc_stat.elsXmitACC++;
5178 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5179 
5180 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5181 	if (rc == IOCB_ERROR) {
5182 		lpfc_els_free_iocb(phba, elsiocb);
5183 		return 1;
5184 	}
5185 	return 0;
5186 }
5187 
5188 /**
5189  * lpfc_els_rsp_rnid_acc - Issue rnid acc response iocb command
5190  * @vport: pointer to a virtual N_Port data structure.
5191  * @format: rnid command format.
5192  * @oldiocb: pointer to the original lpfc command iocb data structure.
5193  * @ndlp: pointer to a node-list data structure.
5194  *
5195  * This routine issues a Request Node Identification Data (RNID) Accept
5196  * (ACC) response. It constructs the RNID ACC response command according to
5197  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
5198  * issue the response. Note that this command does not need to hold the ndlp
5199  * reference count for the callback. So, the ndlp reference count taken by
5200  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
5201  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
5202  * there is no ndlp reference available.
5203  *
5204  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
5205  * will be incremented by 1 for holding the ndlp and the reference to ndlp
5206  * will be stored into the context1 field of the IOCB for the completion
5207  * callback function. However, for the RNID Accept Response ELS command,
5208  * this is undone later by this routine after the IOCB is allocated.
5209  *
5210  * Return code
5211  *   0 - Successfully issued acc rnid response
5212  *   1 - Failed to issue acc rnid response
5213  **/
5214 static int
lpfc_els_rsp_rnid_acc(struct lpfc_vport * vport,uint8_t format,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)5215 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
5216 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5217 {
5218 	struct lpfc_hba  *phba = vport->phba;
5219 	RNID *rn;
5220 	IOCB_t *icmd, *oldcmd;
5221 	struct lpfc_iocbq *elsiocb;
5222 	uint8_t *pcmd;
5223 	uint16_t cmdsize;
5224 	int rc;
5225 
5226 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
5227 					+ (2 * sizeof(struct lpfc_name));
5228 	if (format)
5229 		cmdsize += sizeof(RNID_TOP_DISC);
5230 
5231 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5232 				     ndlp->nlp_DID, ELS_CMD_ACC);
5233 	if (!elsiocb)
5234 		return 1;
5235 
5236 	icmd = &elsiocb->iocb;
5237 	oldcmd = &oldiocb->iocb;
5238 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
5239 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
5240 
5241 	/* Xmit RNID ACC response tag <ulpIoTag> */
5242 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5243 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
5244 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
5245 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5246 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5247 	pcmd += sizeof(uint32_t);
5248 
5249 	memset(pcmd, 0, sizeof(RNID));
5250 	rn = (RNID *) (pcmd);
5251 	rn->Format = format;
5252 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
5253 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
5254 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
5255 	switch (format) {
5256 	case 0:
5257 		rn->SpecificLen = 0;
5258 		break;
5259 	case RNID_TOPOLOGY_DISC:
5260 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
5261 		memcpy(&rn->un.topologyDisc.portName,
5262 		       &vport->fc_portname, sizeof(struct lpfc_name));
5263 		rn->un.topologyDisc.unitType = RNID_HBA;
5264 		rn->un.topologyDisc.physPort = 0;
5265 		rn->un.topologyDisc.attachedNodes = 0;
5266 		break;
5267 	default:
5268 		rn->CommonLen = 0;
5269 		rn->SpecificLen = 0;
5270 		break;
5271 	}
5272 
5273 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5274 		"Issue ACC RNID:  did:x%x flg:x%x",
5275 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
5276 
5277 	phba->fc_stat.elsXmitACC++;
5278 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5279 
5280 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5281 	if (rc == IOCB_ERROR) {
5282 		lpfc_els_free_iocb(phba, elsiocb);
5283 		return 1;
5284 	}
5285 	return 0;
5286 }
5287 
5288 /**
5289  * lpfc_els_clear_rrq - Clear the rq that this rrq describes.
5290  * @vport: pointer to a virtual N_Port data structure.
5291  * @iocb: pointer to the lpfc command iocb data structure.
5292  * @ndlp: pointer to a node-list data structure.
5293  *
5294  * Return
5295  **/
5296 static void
lpfc_els_clear_rrq(struct lpfc_vport * vport,struct lpfc_iocbq * iocb,struct lpfc_nodelist * ndlp)5297 lpfc_els_clear_rrq(struct lpfc_vport *vport,
5298 		   struct lpfc_iocbq *iocb, struct lpfc_nodelist *ndlp)
5299 {
5300 	struct lpfc_hba  *phba = vport->phba;
5301 	uint8_t *pcmd;
5302 	struct RRQ *rrq;
5303 	uint16_t rxid;
5304 	uint16_t xri;
5305 	struct lpfc_node_rrq *prrq;
5306 
5307 
5308 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) iocb->context2)->virt);
5309 	pcmd += sizeof(uint32_t);
5310 	rrq = (struct RRQ *)pcmd;
5311 	rrq->rrq_exchg = be32_to_cpu(rrq->rrq_exchg);
5312 	rxid = bf_get(rrq_rxid, rrq);
5313 
5314 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5315 			"2883 Clear RRQ for SID:x%x OXID:x%x RXID:x%x"
5316 			" x%x x%x\n",
5317 			be32_to_cpu(bf_get(rrq_did, rrq)),
5318 			bf_get(rrq_oxid, rrq),
5319 			rxid,
5320 			iocb->iotag, iocb->iocb.ulpContext);
5321 
5322 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5323 		"Clear RRQ:  did:x%x flg:x%x exchg:x%.08x",
5324 		ndlp->nlp_DID, ndlp->nlp_flag, rrq->rrq_exchg);
5325 	if (vport->fc_myDID == be32_to_cpu(bf_get(rrq_did, rrq)))
5326 		xri = bf_get(rrq_oxid, rrq);
5327 	else
5328 		xri = rxid;
5329 	prrq = lpfc_get_active_rrq(vport, xri, ndlp->nlp_DID);
5330 	if (prrq)
5331 		lpfc_clr_rrq_active(phba, xri, prrq);
5332 	return;
5333 }
5334 
5335 /**
5336  * lpfc_els_rsp_echo_acc - Issue echo acc response
5337  * @vport: pointer to a virtual N_Port data structure.
5338  * @data: pointer to echo data to return in the accept.
5339  * @oldiocb: pointer to the original lpfc command iocb data structure.
5340  * @ndlp: pointer to a node-list data structure.
5341  *
5342  * Return code
5343  *   0 - Successfully issued acc echo response
5344  *   1 - Failed to issue acc echo response
5345  **/
5346 static int
lpfc_els_rsp_echo_acc(struct lpfc_vport * vport,uint8_t * data,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)5347 lpfc_els_rsp_echo_acc(struct lpfc_vport *vport, uint8_t *data,
5348 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
5349 {
5350 	struct lpfc_hba  *phba = vport->phba;
5351 	struct lpfc_iocbq *elsiocb;
5352 	uint8_t *pcmd;
5353 	uint16_t cmdsize;
5354 	int rc;
5355 
5356 	cmdsize = oldiocb->iocb.unsli3.rcvsli3.acc_len;
5357 
5358 	/* The accumulated length can exceed the BPL_SIZE.  For
5359 	 * now, use this as the limit
5360 	 */
5361 	if (cmdsize > LPFC_BPL_SIZE)
5362 		cmdsize = LPFC_BPL_SIZE;
5363 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
5364 				     ndlp->nlp_DID, ELS_CMD_ACC);
5365 	if (!elsiocb)
5366 		return 1;
5367 
5368 	elsiocb->iocb.ulpContext = oldiocb->iocb.ulpContext;  /* Xri / rx_id */
5369 	elsiocb->iocb.unsli3.rcvsli3.ox_id = oldiocb->iocb.unsli3.rcvsli3.ox_id;
5370 
5371 	/* Xmit ECHO ACC response tag <ulpIoTag> */
5372 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5373 			 "2876 Xmit ECHO ACC response tag x%x xri x%x\n",
5374 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
5375 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5376 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5377 	pcmd += sizeof(uint32_t);
5378 	memcpy(pcmd, data, cmdsize - sizeof(uint32_t));
5379 
5380 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
5381 		"Issue ACC ECHO:  did:x%x flg:x%x",
5382 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
5383 
5384 	phba->fc_stat.elsXmitACC++;
5385 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
5386 
5387 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
5388 	if (rc == IOCB_ERROR) {
5389 		lpfc_els_free_iocb(phba, elsiocb);
5390 		return 1;
5391 	}
5392 	return 0;
5393 }
5394 
5395 /**
5396  * lpfc_els_disc_adisc - Issue remaining adisc iocbs to npr nodes of a vport
5397  * @vport: pointer to a host virtual N_Port data structure.
5398  *
5399  * This routine issues Address Discover (ADISC) ELS commands to those
5400  * N_Ports which are in node port recovery state and ADISC has not been issued
5401  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
5402  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
5403  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
5404  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
5405  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
5406  * IOCBs quit for later pick up. On the other hand, after walking through
5407  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
5408  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
5409  * no more ADISC need to be sent.
5410  *
5411  * Return code
5412  *    The number of N_Ports with adisc issued.
5413  **/
5414 int
lpfc_els_disc_adisc(struct lpfc_vport * vport)5415 lpfc_els_disc_adisc(struct lpfc_vport *vport)
5416 {
5417 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5418 	struct lpfc_nodelist *ndlp, *next_ndlp;
5419 	int sentadisc = 0;
5420 
5421 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
5422 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5423 		if (!NLP_CHK_NODE_ACT(ndlp))
5424 			continue;
5425 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5426 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5427 		    (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
5428 			spin_lock_irq(shost->host_lock);
5429 			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5430 			spin_unlock_irq(shost->host_lock);
5431 			ndlp->nlp_prev_state = ndlp->nlp_state;
5432 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
5433 			lpfc_issue_els_adisc(vport, ndlp, 0);
5434 			sentadisc++;
5435 			vport->num_disc_nodes++;
5436 			if (vport->num_disc_nodes >=
5437 			    vport->cfg_discovery_threads) {
5438 				spin_lock_irq(shost->host_lock);
5439 				vport->fc_flag |= FC_NLP_MORE;
5440 				spin_unlock_irq(shost->host_lock);
5441 				break;
5442 			}
5443 		}
5444 	}
5445 	if (sentadisc == 0) {
5446 		spin_lock_irq(shost->host_lock);
5447 		vport->fc_flag &= ~FC_NLP_MORE;
5448 		spin_unlock_irq(shost->host_lock);
5449 	}
5450 	return sentadisc;
5451 }
5452 
5453 /**
5454  * lpfc_els_disc_plogi - Issue plogi for all npr nodes of a vport before adisc
5455  * @vport: pointer to a host virtual N_Port data structure.
5456  *
5457  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
5458  * which are in node port recovery state, with a @vport. Each time an ELS
5459  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
5460  * the per @vport number of discover count (num_disc_nodes) shall be
5461  * incremented. If the num_disc_nodes reaches a pre-configured threshold
5462  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
5463  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
5464  * later pick up. On the other hand, after walking through all the ndlps with
5465  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
5466  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
5467  * PLOGI need to be sent.
5468  *
5469  * Return code
5470  *   The number of N_Ports with plogi issued.
5471  **/
5472 int
lpfc_els_disc_plogi(struct lpfc_vport * vport)5473 lpfc_els_disc_plogi(struct lpfc_vport *vport)
5474 {
5475 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5476 	struct lpfc_nodelist *ndlp, *next_ndlp;
5477 	int sentplogi = 0;
5478 
5479 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
5480 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
5481 		if (!NLP_CHK_NODE_ACT(ndlp))
5482 			continue;
5483 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
5484 				(ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
5485 				(ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
5486 				(ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
5487 			ndlp->nlp_prev_state = ndlp->nlp_state;
5488 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5489 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5490 			sentplogi++;
5491 			vport->num_disc_nodes++;
5492 			if (vport->num_disc_nodes >=
5493 					vport->cfg_discovery_threads) {
5494 				spin_lock_irq(shost->host_lock);
5495 				vport->fc_flag |= FC_NLP_MORE;
5496 				spin_unlock_irq(shost->host_lock);
5497 				break;
5498 			}
5499 		}
5500 	}
5501 
5502 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5503 			 "6452 Discover PLOGI %d flag x%x\n",
5504 			 sentplogi, vport->fc_flag);
5505 
5506 	if (sentplogi) {
5507 		lpfc_set_disctmo(vport);
5508 	}
5509 	else {
5510 		spin_lock_irq(shost->host_lock);
5511 		vport->fc_flag &= ~FC_NLP_MORE;
5512 		spin_unlock_irq(shost->host_lock);
5513 	}
5514 	return sentplogi;
5515 }
5516 
5517 static uint32_t
lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc * desc,uint32_t word0)5518 lpfc_rdp_res_link_service(struct fc_rdp_link_service_desc *desc,
5519 		uint32_t word0)
5520 {
5521 
5522 	desc->tag = cpu_to_be32(RDP_LINK_SERVICE_DESC_TAG);
5523 	desc->payload.els_req = word0;
5524 	desc->length = cpu_to_be32(sizeof(desc->payload));
5525 
5526 	return sizeof(struct fc_rdp_link_service_desc);
5527 }
5528 
5529 static uint32_t
lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc * desc,uint8_t * page_a0,uint8_t * page_a2)5530 lpfc_rdp_res_sfp_desc(struct fc_rdp_sfp_desc *desc,
5531 		uint8_t *page_a0, uint8_t *page_a2)
5532 {
5533 	uint16_t wavelength;
5534 	uint16_t temperature;
5535 	uint16_t rx_power;
5536 	uint16_t tx_bias;
5537 	uint16_t tx_power;
5538 	uint16_t vcc;
5539 	uint16_t flag = 0;
5540 	struct sff_trasnceiver_codes_byte4 *trasn_code_byte4;
5541 	struct sff_trasnceiver_codes_byte5 *trasn_code_byte5;
5542 
5543 	desc->tag = cpu_to_be32(RDP_SFP_DESC_TAG);
5544 
5545 	trasn_code_byte4 = (struct sff_trasnceiver_codes_byte4 *)
5546 			&page_a0[SSF_TRANSCEIVER_CODE_B4];
5547 	trasn_code_byte5 = (struct sff_trasnceiver_codes_byte5 *)
5548 			&page_a0[SSF_TRANSCEIVER_CODE_B5];
5549 
5550 	if ((trasn_code_byte4->fc_sw_laser) ||
5551 	    (trasn_code_byte5->fc_sw_laser_sl) ||
5552 	    (trasn_code_byte5->fc_sw_laser_sn)) {  /* check if its short WL */
5553 		flag |= (SFP_FLAG_PT_SWLASER << SFP_FLAG_PT_SHIFT);
5554 	} else if (trasn_code_byte4->fc_lw_laser) {
5555 		wavelength = (page_a0[SSF_WAVELENGTH_B1] << 8) |
5556 			page_a0[SSF_WAVELENGTH_B0];
5557 		if (wavelength == SFP_WAVELENGTH_LC1310)
5558 			flag |= SFP_FLAG_PT_LWLASER_LC1310 << SFP_FLAG_PT_SHIFT;
5559 		if (wavelength == SFP_WAVELENGTH_LL1550)
5560 			flag |= SFP_FLAG_PT_LWLASER_LL1550 << SFP_FLAG_PT_SHIFT;
5561 	}
5562 	/* check if its SFP+ */
5563 	flag |= ((page_a0[SSF_IDENTIFIER] == SFF_PG0_IDENT_SFP) ?
5564 			SFP_FLAG_CT_SFP_PLUS : SFP_FLAG_CT_UNKNOWN)
5565 					<< SFP_FLAG_CT_SHIFT;
5566 
5567 	/* check if its OPTICAL */
5568 	flag |= ((page_a0[SSF_CONNECTOR] == SFF_PG0_CONNECTOR_LC) ?
5569 			SFP_FLAG_IS_OPTICAL_PORT : 0)
5570 					<< SFP_FLAG_IS_OPTICAL_SHIFT;
5571 
5572 	temperature = (page_a2[SFF_TEMPERATURE_B1] << 8 |
5573 		page_a2[SFF_TEMPERATURE_B0]);
5574 	vcc = (page_a2[SFF_VCC_B1] << 8 |
5575 		page_a2[SFF_VCC_B0]);
5576 	tx_power = (page_a2[SFF_TXPOWER_B1] << 8 |
5577 		page_a2[SFF_TXPOWER_B0]);
5578 	tx_bias = (page_a2[SFF_TX_BIAS_CURRENT_B1] << 8 |
5579 		page_a2[SFF_TX_BIAS_CURRENT_B0]);
5580 	rx_power = (page_a2[SFF_RXPOWER_B1] << 8 |
5581 		page_a2[SFF_RXPOWER_B0]);
5582 	desc->sfp_info.temperature = cpu_to_be16(temperature);
5583 	desc->sfp_info.rx_power = cpu_to_be16(rx_power);
5584 	desc->sfp_info.tx_bias = cpu_to_be16(tx_bias);
5585 	desc->sfp_info.tx_power = cpu_to_be16(tx_power);
5586 	desc->sfp_info.vcc = cpu_to_be16(vcc);
5587 
5588 	desc->sfp_info.flags = cpu_to_be16(flag);
5589 	desc->length = cpu_to_be32(sizeof(desc->sfp_info));
5590 
5591 	return sizeof(struct fc_rdp_sfp_desc);
5592 }
5593 
5594 static uint32_t
lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc * desc,READ_LNK_VAR * stat)5595 lpfc_rdp_res_link_error(struct fc_rdp_link_error_status_desc *desc,
5596 		READ_LNK_VAR *stat)
5597 {
5598 	uint32_t type;
5599 
5600 	desc->tag = cpu_to_be32(RDP_LINK_ERROR_STATUS_DESC_TAG);
5601 
5602 	type = VN_PT_PHY_PF_PORT << VN_PT_PHY_SHIFT;
5603 
5604 	desc->info.port_type = cpu_to_be32(type);
5605 
5606 	desc->info.link_status.link_failure_cnt =
5607 		cpu_to_be32(stat->linkFailureCnt);
5608 	desc->info.link_status.loss_of_synch_cnt =
5609 		cpu_to_be32(stat->lossSyncCnt);
5610 	desc->info.link_status.loss_of_signal_cnt =
5611 		cpu_to_be32(stat->lossSignalCnt);
5612 	desc->info.link_status.primitive_seq_proto_err =
5613 		cpu_to_be32(stat->primSeqErrCnt);
5614 	desc->info.link_status.invalid_trans_word =
5615 		cpu_to_be32(stat->invalidXmitWord);
5616 	desc->info.link_status.invalid_crc_cnt = cpu_to_be32(stat->crcCnt);
5617 
5618 	desc->length = cpu_to_be32(sizeof(desc->info));
5619 
5620 	return sizeof(struct fc_rdp_link_error_status_desc);
5621 }
5622 
5623 static uint32_t
lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc * desc,READ_LNK_VAR * stat,struct lpfc_vport * vport)5624 lpfc_rdp_res_bbc_desc(struct fc_rdp_bbc_desc *desc, READ_LNK_VAR *stat,
5625 		      struct lpfc_vport *vport)
5626 {
5627 	uint32_t bbCredit;
5628 
5629 	desc->tag = cpu_to_be32(RDP_BBC_DESC_TAG);
5630 
5631 	bbCredit = vport->fc_sparam.cmn.bbCreditLsb |
5632 			(vport->fc_sparam.cmn.bbCreditMsb << 8);
5633 	desc->bbc_info.port_bbc = cpu_to_be32(bbCredit);
5634 	if (vport->phba->fc_topology != LPFC_TOPOLOGY_LOOP) {
5635 		bbCredit = vport->phba->fc_fabparam.cmn.bbCreditLsb |
5636 			(vport->phba->fc_fabparam.cmn.bbCreditMsb << 8);
5637 		desc->bbc_info.attached_port_bbc = cpu_to_be32(bbCredit);
5638 	} else {
5639 		desc->bbc_info.attached_port_bbc = 0;
5640 	}
5641 
5642 	desc->bbc_info.rtt = 0;
5643 	desc->length = cpu_to_be32(sizeof(desc->bbc_info));
5644 
5645 	return sizeof(struct fc_rdp_bbc_desc);
5646 }
5647 
5648 static uint32_t
lpfc_rdp_res_oed_temp_desc(struct lpfc_hba * phba,struct fc_rdp_oed_sfp_desc * desc,uint8_t * page_a2)5649 lpfc_rdp_res_oed_temp_desc(struct lpfc_hba *phba,
5650 			   struct fc_rdp_oed_sfp_desc *desc, uint8_t *page_a2)
5651 {
5652 	uint32_t flags = 0;
5653 
5654 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5655 
5656 	desc->oed_info.hi_alarm = page_a2[SSF_TEMP_HIGH_ALARM];
5657 	desc->oed_info.lo_alarm = page_a2[SSF_TEMP_LOW_ALARM];
5658 	desc->oed_info.hi_warning = page_a2[SSF_TEMP_HIGH_WARNING];
5659 	desc->oed_info.lo_warning = page_a2[SSF_TEMP_LOW_WARNING];
5660 
5661 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5662 		flags |= RDP_OET_HIGH_ALARM;
5663 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5664 		flags |= RDP_OET_LOW_ALARM;
5665 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TEMPERATURE)
5666 		flags |= RDP_OET_HIGH_WARNING;
5667 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TEMPERATURE)
5668 		flags |= RDP_OET_LOW_WARNING;
5669 
5670 	flags |= ((0xf & RDP_OED_TEMPERATURE) << RDP_OED_TYPE_SHIFT);
5671 	desc->oed_info.function_flags = cpu_to_be32(flags);
5672 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
5673 	return sizeof(struct fc_rdp_oed_sfp_desc);
5674 }
5675 
5676 static uint32_t
lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba * phba,struct fc_rdp_oed_sfp_desc * desc,uint8_t * page_a2)5677 lpfc_rdp_res_oed_voltage_desc(struct lpfc_hba *phba,
5678 			      struct fc_rdp_oed_sfp_desc *desc,
5679 			      uint8_t *page_a2)
5680 {
5681 	uint32_t flags = 0;
5682 
5683 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5684 
5685 	desc->oed_info.hi_alarm = page_a2[SSF_VOLTAGE_HIGH_ALARM];
5686 	desc->oed_info.lo_alarm = page_a2[SSF_VOLTAGE_LOW_ALARM];
5687 	desc->oed_info.hi_warning = page_a2[SSF_VOLTAGE_HIGH_WARNING];
5688 	desc->oed_info.lo_warning = page_a2[SSF_VOLTAGE_LOW_WARNING];
5689 
5690 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5691 		flags |= RDP_OET_HIGH_ALARM;
5692 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5693 		flags |= RDP_OET_LOW_ALARM;
5694 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_VOLTAGE)
5695 		flags |= RDP_OET_HIGH_WARNING;
5696 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_VOLTAGE)
5697 		flags |= RDP_OET_LOW_WARNING;
5698 
5699 	flags |= ((0xf & RDP_OED_VOLTAGE) << RDP_OED_TYPE_SHIFT);
5700 	desc->oed_info.function_flags = cpu_to_be32(flags);
5701 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
5702 	return sizeof(struct fc_rdp_oed_sfp_desc);
5703 }
5704 
5705 static uint32_t
lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba * phba,struct fc_rdp_oed_sfp_desc * desc,uint8_t * page_a2)5706 lpfc_rdp_res_oed_txbias_desc(struct lpfc_hba *phba,
5707 			     struct fc_rdp_oed_sfp_desc *desc,
5708 			     uint8_t *page_a2)
5709 {
5710 	uint32_t flags = 0;
5711 
5712 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5713 
5714 	desc->oed_info.hi_alarm = page_a2[SSF_BIAS_HIGH_ALARM];
5715 	desc->oed_info.lo_alarm = page_a2[SSF_BIAS_LOW_ALARM];
5716 	desc->oed_info.hi_warning = page_a2[SSF_BIAS_HIGH_WARNING];
5717 	desc->oed_info.lo_warning = page_a2[SSF_BIAS_LOW_WARNING];
5718 
5719 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5720 		flags |= RDP_OET_HIGH_ALARM;
5721 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXBIAS)
5722 		flags |= RDP_OET_LOW_ALARM;
5723 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXBIAS)
5724 		flags |= RDP_OET_HIGH_WARNING;
5725 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXBIAS)
5726 		flags |= RDP_OET_LOW_WARNING;
5727 
5728 	flags |= ((0xf & RDP_OED_TXBIAS) << RDP_OED_TYPE_SHIFT);
5729 	desc->oed_info.function_flags = cpu_to_be32(flags);
5730 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
5731 	return sizeof(struct fc_rdp_oed_sfp_desc);
5732 }
5733 
5734 static uint32_t
lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba * phba,struct fc_rdp_oed_sfp_desc * desc,uint8_t * page_a2)5735 lpfc_rdp_res_oed_txpower_desc(struct lpfc_hba *phba,
5736 			      struct fc_rdp_oed_sfp_desc *desc,
5737 			      uint8_t *page_a2)
5738 {
5739 	uint32_t flags = 0;
5740 
5741 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5742 
5743 	desc->oed_info.hi_alarm = page_a2[SSF_TXPOWER_HIGH_ALARM];
5744 	desc->oed_info.lo_alarm = page_a2[SSF_TXPOWER_LOW_ALARM];
5745 	desc->oed_info.hi_warning = page_a2[SSF_TXPOWER_HIGH_WARNING];
5746 	desc->oed_info.lo_warning = page_a2[SSF_TXPOWER_LOW_WARNING];
5747 
5748 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5749 		flags |= RDP_OET_HIGH_ALARM;
5750 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_TXPOWER)
5751 		flags |= RDP_OET_LOW_ALARM;
5752 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_TXPOWER)
5753 		flags |= RDP_OET_HIGH_WARNING;
5754 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_TXPOWER)
5755 		flags |= RDP_OET_LOW_WARNING;
5756 
5757 	flags |= ((0xf & RDP_OED_TXPOWER) << RDP_OED_TYPE_SHIFT);
5758 	desc->oed_info.function_flags = cpu_to_be32(flags);
5759 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
5760 	return sizeof(struct fc_rdp_oed_sfp_desc);
5761 }
5762 
5763 
5764 static uint32_t
lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba * phba,struct fc_rdp_oed_sfp_desc * desc,uint8_t * page_a2)5765 lpfc_rdp_res_oed_rxpower_desc(struct lpfc_hba *phba,
5766 			      struct fc_rdp_oed_sfp_desc *desc,
5767 			      uint8_t *page_a2)
5768 {
5769 	uint32_t flags = 0;
5770 
5771 	desc->tag = cpu_to_be32(RDP_OED_DESC_TAG);
5772 
5773 	desc->oed_info.hi_alarm = page_a2[SSF_RXPOWER_HIGH_ALARM];
5774 	desc->oed_info.lo_alarm = page_a2[SSF_RXPOWER_LOW_ALARM];
5775 	desc->oed_info.hi_warning = page_a2[SSF_RXPOWER_HIGH_WARNING];
5776 	desc->oed_info.lo_warning = page_a2[SSF_RXPOWER_LOW_WARNING];
5777 
5778 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5779 		flags |= RDP_OET_HIGH_ALARM;
5780 	if (phba->sfp_alarm & LPFC_TRANSGRESSION_LOW_RXPOWER)
5781 		flags |= RDP_OET_LOW_ALARM;
5782 	if (phba->sfp_warning & LPFC_TRANSGRESSION_HIGH_RXPOWER)
5783 		flags |= RDP_OET_HIGH_WARNING;
5784 	if (phba->sfp_warning & LPFC_TRANSGRESSION_LOW_RXPOWER)
5785 		flags |= RDP_OET_LOW_WARNING;
5786 
5787 	flags |= ((0xf & RDP_OED_RXPOWER) << RDP_OED_TYPE_SHIFT);
5788 	desc->oed_info.function_flags = cpu_to_be32(flags);
5789 	desc->length = cpu_to_be32(sizeof(desc->oed_info));
5790 	return sizeof(struct fc_rdp_oed_sfp_desc);
5791 }
5792 
5793 static uint32_t
lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc * desc,uint8_t * page_a0,struct lpfc_vport * vport)5794 lpfc_rdp_res_opd_desc(struct fc_rdp_opd_sfp_desc *desc,
5795 		      uint8_t *page_a0, struct lpfc_vport *vport)
5796 {
5797 	desc->tag = cpu_to_be32(RDP_OPD_DESC_TAG);
5798 	memcpy(desc->opd_info.vendor_name, &page_a0[SSF_VENDOR_NAME], 16);
5799 	memcpy(desc->opd_info.model_number, &page_a0[SSF_VENDOR_PN], 16);
5800 	memcpy(desc->opd_info.serial_number, &page_a0[SSF_VENDOR_SN], 16);
5801 	memcpy(desc->opd_info.revision, &page_a0[SSF_VENDOR_REV], 4);
5802 	memcpy(desc->opd_info.date, &page_a0[SSF_DATE_CODE], 8);
5803 	desc->length = cpu_to_be32(sizeof(desc->opd_info));
5804 	return sizeof(struct fc_rdp_opd_sfp_desc);
5805 }
5806 
5807 static uint32_t
lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc * desc,READ_LNK_VAR * stat)5808 lpfc_rdp_res_fec_desc(struct fc_fec_rdp_desc *desc, READ_LNK_VAR *stat)
5809 {
5810 	if (bf_get(lpfc_read_link_stat_gec2, stat) == 0)
5811 		return 0;
5812 	desc->tag = cpu_to_be32(RDP_FEC_DESC_TAG);
5813 
5814 	desc->info.CorrectedBlocks =
5815 		cpu_to_be32(stat->fecCorrBlkCount);
5816 	desc->info.UncorrectableBlocks =
5817 		cpu_to_be32(stat->fecUncorrBlkCount);
5818 
5819 	desc->length = cpu_to_be32(sizeof(desc->info));
5820 
5821 	return sizeof(struct fc_fec_rdp_desc);
5822 }
5823 
5824 static uint32_t
lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc * desc,struct lpfc_hba * phba)5825 lpfc_rdp_res_speed(struct fc_rdp_port_speed_desc *desc, struct lpfc_hba *phba)
5826 {
5827 	uint16_t rdp_cap = 0;
5828 	uint16_t rdp_speed;
5829 
5830 	desc->tag = cpu_to_be32(RDP_PORT_SPEED_DESC_TAG);
5831 
5832 	switch (phba->fc_linkspeed) {
5833 	case LPFC_LINK_SPEED_1GHZ:
5834 		rdp_speed = RDP_PS_1GB;
5835 		break;
5836 	case LPFC_LINK_SPEED_2GHZ:
5837 		rdp_speed = RDP_PS_2GB;
5838 		break;
5839 	case LPFC_LINK_SPEED_4GHZ:
5840 		rdp_speed = RDP_PS_4GB;
5841 		break;
5842 	case LPFC_LINK_SPEED_8GHZ:
5843 		rdp_speed = RDP_PS_8GB;
5844 		break;
5845 	case LPFC_LINK_SPEED_10GHZ:
5846 		rdp_speed = RDP_PS_10GB;
5847 		break;
5848 	case LPFC_LINK_SPEED_16GHZ:
5849 		rdp_speed = RDP_PS_16GB;
5850 		break;
5851 	case LPFC_LINK_SPEED_32GHZ:
5852 		rdp_speed = RDP_PS_32GB;
5853 		break;
5854 	case LPFC_LINK_SPEED_64GHZ:
5855 		rdp_speed = RDP_PS_64GB;
5856 		break;
5857 	default:
5858 		rdp_speed = RDP_PS_UNKNOWN;
5859 		break;
5860 	}
5861 
5862 	desc->info.port_speed.speed = cpu_to_be16(rdp_speed);
5863 
5864 	if (phba->lmt & LMT_128Gb)
5865 		rdp_cap |= RDP_PS_128GB;
5866 	if (phba->lmt & LMT_64Gb)
5867 		rdp_cap |= RDP_PS_64GB;
5868 	if (phba->lmt & LMT_32Gb)
5869 		rdp_cap |= RDP_PS_32GB;
5870 	if (phba->lmt & LMT_16Gb)
5871 		rdp_cap |= RDP_PS_16GB;
5872 	if (phba->lmt & LMT_10Gb)
5873 		rdp_cap |= RDP_PS_10GB;
5874 	if (phba->lmt & LMT_8Gb)
5875 		rdp_cap |= RDP_PS_8GB;
5876 	if (phba->lmt & LMT_4Gb)
5877 		rdp_cap |= RDP_PS_4GB;
5878 	if (phba->lmt & LMT_2Gb)
5879 		rdp_cap |= RDP_PS_2GB;
5880 	if (phba->lmt & LMT_1Gb)
5881 		rdp_cap |= RDP_PS_1GB;
5882 
5883 	if (rdp_cap == 0)
5884 		rdp_cap = RDP_CAP_UNKNOWN;
5885 	if (phba->cfg_link_speed != LPFC_USER_LINK_SPEED_AUTO)
5886 		rdp_cap |= RDP_CAP_USER_CONFIGURED;
5887 
5888 	desc->info.port_speed.capabilities = cpu_to_be16(rdp_cap);
5889 	desc->length = cpu_to_be32(sizeof(desc->info));
5890 	return sizeof(struct fc_rdp_port_speed_desc);
5891 }
5892 
5893 static uint32_t
lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc * desc,struct lpfc_vport * vport)5894 lpfc_rdp_res_diag_port_names(struct fc_rdp_port_name_desc *desc,
5895 		struct lpfc_vport *vport)
5896 {
5897 
5898 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5899 
5900 	memcpy(desc->port_names.wwnn, &vport->fc_nodename,
5901 			sizeof(desc->port_names.wwnn));
5902 
5903 	memcpy(desc->port_names.wwpn, &vport->fc_portname,
5904 			sizeof(desc->port_names.wwpn));
5905 
5906 	desc->length = cpu_to_be32(sizeof(desc->port_names));
5907 	return sizeof(struct fc_rdp_port_name_desc);
5908 }
5909 
5910 static uint32_t
lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc * desc,struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)5911 lpfc_rdp_res_attach_port_names(struct fc_rdp_port_name_desc *desc,
5912 		struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5913 {
5914 
5915 	desc->tag = cpu_to_be32(RDP_PORT_NAMES_DESC_TAG);
5916 	if (vport->fc_flag & FC_FABRIC) {
5917 		memcpy(desc->port_names.wwnn, &vport->fabric_nodename,
5918 		       sizeof(desc->port_names.wwnn));
5919 
5920 		memcpy(desc->port_names.wwpn, &vport->fabric_portname,
5921 		       sizeof(desc->port_names.wwpn));
5922 	} else {  /* Point to Point */
5923 		memcpy(desc->port_names.wwnn, &ndlp->nlp_nodename,
5924 		       sizeof(desc->port_names.wwnn));
5925 
5926 		memcpy(desc->port_names.wwpn, &ndlp->nlp_portname,
5927 		       sizeof(desc->port_names.wwpn));
5928 	}
5929 
5930 	desc->length = cpu_to_be32(sizeof(desc->port_names));
5931 	return sizeof(struct fc_rdp_port_name_desc);
5932 }
5933 
5934 static void
lpfc_els_rdp_cmpl(struct lpfc_hba * phba,struct lpfc_rdp_context * rdp_context,int status)5935 lpfc_els_rdp_cmpl(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context,
5936 		int status)
5937 {
5938 	struct lpfc_nodelist *ndlp = rdp_context->ndlp;
5939 	struct lpfc_vport *vport = ndlp->vport;
5940 	struct lpfc_iocbq *elsiocb;
5941 	struct ulp_bde64 *bpl;
5942 	IOCB_t *icmd;
5943 	uint8_t *pcmd;
5944 	struct ls_rjt *stat;
5945 	struct fc_rdp_res_frame *rdp_res;
5946 	uint32_t cmdsize, len;
5947 	uint16_t *flag_ptr;
5948 	int rc;
5949 
5950 	if (status != SUCCESS)
5951 		goto error;
5952 
5953 	/* This will change once we know the true size of the RDP payload */
5954 	cmdsize = sizeof(struct fc_rdp_res_frame);
5955 
5956 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize,
5957 			lpfc_max_els_tries, rdp_context->ndlp,
5958 			rdp_context->ndlp->nlp_DID, ELS_CMD_ACC);
5959 	lpfc_nlp_put(ndlp);
5960 	if (!elsiocb)
5961 		goto free_rdp_context;
5962 
5963 	icmd = &elsiocb->iocb;
5964 	icmd->ulpContext = rdp_context->rx_id;
5965 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
5966 
5967 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5968 			"2171 Xmit RDP response tag x%x xri x%x, "
5969 			"did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x",
5970 			elsiocb->iotag, elsiocb->iocb.ulpContext,
5971 			ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
5972 			ndlp->nlp_rpi);
5973 	rdp_res = (struct fc_rdp_res_frame *)
5974 		(((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5975 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
5976 	memset(pcmd, 0, sizeof(struct fc_rdp_res_frame));
5977 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
5978 
5979 	/* Update Alarm and Warning */
5980 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_ALARM_FLAGS);
5981 	phba->sfp_alarm |= *flag_ptr;
5982 	flag_ptr = (uint16_t *)(rdp_context->page_a2 + SSF_WARNING_FLAGS);
5983 	phba->sfp_warning |= *flag_ptr;
5984 
5985 	/* For RDP payload */
5986 	len = 8;
5987 	len += lpfc_rdp_res_link_service((struct fc_rdp_link_service_desc *)
5988 					 (len + pcmd), ELS_CMD_RDP);
5989 
5990 	len += lpfc_rdp_res_sfp_desc((struct fc_rdp_sfp_desc *)(len + pcmd),
5991 			rdp_context->page_a0, rdp_context->page_a2);
5992 	len += lpfc_rdp_res_speed((struct fc_rdp_port_speed_desc *)(len + pcmd),
5993 				  phba);
5994 	len += lpfc_rdp_res_link_error((struct fc_rdp_link_error_status_desc *)
5995 				       (len + pcmd), &rdp_context->link_stat);
5996 	len += lpfc_rdp_res_diag_port_names((struct fc_rdp_port_name_desc *)
5997 					     (len + pcmd), vport);
5998 	len += lpfc_rdp_res_attach_port_names((struct fc_rdp_port_name_desc *)
5999 					(len + pcmd), vport, ndlp);
6000 	len += lpfc_rdp_res_fec_desc((struct fc_fec_rdp_desc *)(len + pcmd),
6001 			&rdp_context->link_stat);
6002 	len += lpfc_rdp_res_bbc_desc((struct fc_rdp_bbc_desc *)(len + pcmd),
6003 				     &rdp_context->link_stat, vport);
6004 	len += lpfc_rdp_res_oed_temp_desc(phba,
6005 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6006 				rdp_context->page_a2);
6007 	len += lpfc_rdp_res_oed_voltage_desc(phba,
6008 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6009 				rdp_context->page_a2);
6010 	len += lpfc_rdp_res_oed_txbias_desc(phba,
6011 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6012 				rdp_context->page_a2);
6013 	len += lpfc_rdp_res_oed_txpower_desc(phba,
6014 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6015 				rdp_context->page_a2);
6016 	len += lpfc_rdp_res_oed_rxpower_desc(phba,
6017 				(struct fc_rdp_oed_sfp_desc *)(len + pcmd),
6018 				rdp_context->page_a2);
6019 	len += lpfc_rdp_res_opd_desc((struct fc_rdp_opd_sfp_desc *)(len + pcmd),
6020 				     rdp_context->page_a0, vport);
6021 
6022 	rdp_res->length = cpu_to_be32(len - 8);
6023 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6024 
6025 	/* Now that we know the true size of the payload, update the BPL */
6026 	bpl = (struct ulp_bde64 *)
6027 		(((struct lpfc_dmabuf *)(elsiocb->context3))->virt);
6028 	bpl->tus.f.bdeSize = len;
6029 	bpl->tus.f.bdeFlags = 0;
6030 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
6031 
6032 	phba->fc_stat.elsXmitACC++;
6033 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6034 	if (rc == IOCB_ERROR)
6035 		lpfc_els_free_iocb(phba, elsiocb);
6036 
6037 	kfree(rdp_context);
6038 
6039 	return;
6040 error:
6041 	cmdsize = 2 * sizeof(uint32_t);
6042 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, lpfc_max_els_tries,
6043 			ndlp, ndlp->nlp_DID, ELS_CMD_LS_RJT);
6044 	lpfc_nlp_put(ndlp);
6045 	if (!elsiocb)
6046 		goto free_rdp_context;
6047 
6048 	icmd = &elsiocb->iocb;
6049 	icmd->ulpContext = rdp_context->rx_id;
6050 	icmd->unsli3.rcvsli3.ox_id = rdp_context->ox_id;
6051 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6052 
6053 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
6054 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6055 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6056 
6057 	phba->fc_stat.elsXmitLSRJT++;
6058 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6059 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6060 
6061 	if (rc == IOCB_ERROR)
6062 		lpfc_els_free_iocb(phba, elsiocb);
6063 free_rdp_context:
6064 	kfree(rdp_context);
6065 }
6066 
6067 static int
lpfc_get_rdp_info(struct lpfc_hba * phba,struct lpfc_rdp_context * rdp_context)6068 lpfc_get_rdp_info(struct lpfc_hba *phba, struct lpfc_rdp_context *rdp_context)
6069 {
6070 	LPFC_MBOXQ_t *mbox = NULL;
6071 	int rc;
6072 
6073 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6074 	if (!mbox) {
6075 		lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_ELS,
6076 				"7105 failed to allocate mailbox memory");
6077 		return 1;
6078 	}
6079 
6080 	if (lpfc_sli4_dump_page_a0(phba, mbox))
6081 		goto prep_mbox_fail;
6082 	mbox->vport = rdp_context->ndlp->vport;
6083 	mbox->mbox_cmpl = lpfc_mbx_cmpl_rdp_page_a0;
6084 	mbox->ctx_ndlp = (struct lpfc_rdp_context *)rdp_context;
6085 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6086 	if (rc == MBX_NOT_FINISHED)
6087 		goto issue_mbox_fail;
6088 
6089 	return 0;
6090 
6091 prep_mbox_fail:
6092 issue_mbox_fail:
6093 	mempool_free(mbox, phba->mbox_mem_pool);
6094 	return 1;
6095 }
6096 
6097 /*
6098  * lpfc_els_rcv_rdp - Process an unsolicited RDP ELS.
6099  * @vport: pointer to a host virtual N_Port data structure.
6100  * @cmdiocb: pointer to lpfc command iocb data structure.
6101  * @ndlp: pointer to a node-list data structure.
6102  *
6103  * This routine processes an unsolicited RDP(Read Diagnostic Parameters)
6104  * IOCB. First, the payload of the unsolicited RDP is checked.
6105  * Then it will (1) send MBX_DUMP_MEMORY, Embedded DMP_LMSD sub command TYPE-3
6106  * for Page A0, (2) send MBX_DUMP_MEMORY, DMP_LMSD for Page A2,
6107  * (3) send MBX_READ_LNK_STAT to get link stat, (4) Call lpfc_els_rdp_cmpl
6108  * gather all data and send RDP response.
6109  *
6110  * Return code
6111  *   0 - Sent the acc response
6112  *   1 - Sent the reject response.
6113  */
6114 static int
lpfc_els_rcv_rdp(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)6115 lpfc_els_rcv_rdp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6116 		struct lpfc_nodelist *ndlp)
6117 {
6118 	struct lpfc_hba *phba = vport->phba;
6119 	struct lpfc_dmabuf *pcmd;
6120 	uint8_t rjt_err, rjt_expl = LSEXP_NOTHING_MORE;
6121 	struct fc_rdp_req_frame *rdp_req;
6122 	struct lpfc_rdp_context *rdp_context;
6123 	IOCB_t *cmd = NULL;
6124 	struct ls_rjt stat;
6125 
6126 	if (phba->sli_rev < LPFC_SLI_REV4 ||
6127 	    bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6128 						LPFC_SLI_INTF_IF_TYPE_2) {
6129 		rjt_err = LSRJT_UNABLE_TPC;
6130 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6131 		goto error;
6132 	}
6133 
6134 	if (phba->sli_rev < LPFC_SLI_REV4 || (phba->hba_flag & HBA_FCOE_MODE)) {
6135 		rjt_err = LSRJT_UNABLE_TPC;
6136 		rjt_expl = LSEXP_REQ_UNSUPPORTED;
6137 		goto error;
6138 	}
6139 
6140 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6141 	rdp_req = (struct fc_rdp_req_frame *) pcmd->virt;
6142 
6143 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6144 			 "2422 ELS RDP Request "
6145 			 "dec len %d tag x%x port_id %d len %d\n",
6146 			 be32_to_cpu(rdp_req->rdp_des_length),
6147 			 be32_to_cpu(rdp_req->nport_id_desc.tag),
6148 			 be32_to_cpu(rdp_req->nport_id_desc.nport_id),
6149 			 be32_to_cpu(rdp_req->nport_id_desc.length));
6150 
6151 	if (sizeof(struct fc_rdp_nport_desc) !=
6152 			be32_to_cpu(rdp_req->rdp_des_length))
6153 		goto rjt_logerr;
6154 	if (RDP_N_PORT_DESC_TAG != be32_to_cpu(rdp_req->nport_id_desc.tag))
6155 		goto rjt_logerr;
6156 	if (RDP_NPORT_ID_SIZE !=
6157 			be32_to_cpu(rdp_req->nport_id_desc.length))
6158 		goto rjt_logerr;
6159 	rdp_context = kzalloc(sizeof(struct lpfc_rdp_context), GFP_KERNEL);
6160 	if (!rdp_context) {
6161 		rjt_err = LSRJT_UNABLE_TPC;
6162 		goto error;
6163 	}
6164 
6165 	cmd = &cmdiocb->iocb;
6166 	rdp_context->ndlp = lpfc_nlp_get(ndlp);
6167 	rdp_context->ox_id = cmd->unsli3.rcvsli3.ox_id;
6168 	rdp_context->rx_id = cmd->ulpContext;
6169 	rdp_context->cmpl = lpfc_els_rdp_cmpl;
6170 	if (lpfc_get_rdp_info(phba, rdp_context)) {
6171 		lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_ELS,
6172 				 "2423 Unable to send mailbox");
6173 		kfree(rdp_context);
6174 		rjt_err = LSRJT_UNABLE_TPC;
6175 		lpfc_nlp_put(ndlp);
6176 		goto error;
6177 	}
6178 
6179 	return 0;
6180 
6181 rjt_logerr:
6182 	rjt_err = LSRJT_LOGICAL_ERR;
6183 
6184 error:
6185 	memset(&stat, 0, sizeof(stat));
6186 	stat.un.b.lsRjtRsnCode = rjt_err;
6187 	stat.un.b.lsRjtRsnCodeExp = rjt_expl;
6188 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6189 	return 1;
6190 }
6191 
6192 
6193 static void
lpfc_els_lcb_rsp(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6194 lpfc_els_lcb_rsp(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6195 {
6196 	MAILBOX_t *mb;
6197 	IOCB_t *icmd;
6198 	uint8_t *pcmd;
6199 	struct lpfc_iocbq *elsiocb;
6200 	struct lpfc_nodelist *ndlp;
6201 	struct ls_rjt *stat;
6202 	union lpfc_sli4_cfg_shdr *shdr;
6203 	struct lpfc_lcb_context *lcb_context;
6204 	struct fc_lcb_res_frame *lcb_res;
6205 	uint32_t cmdsize, shdr_status, shdr_add_status;
6206 	int rc;
6207 
6208 	mb = &pmb->u.mb;
6209 	lcb_context = (struct lpfc_lcb_context *)pmb->ctx_ndlp;
6210 	ndlp = lcb_context->ndlp;
6211 	pmb->ctx_ndlp = NULL;
6212 	pmb->ctx_buf = NULL;
6213 
6214 	shdr = (union lpfc_sli4_cfg_shdr *)
6215 			&pmb->u.mqe.un.beacon_config.header.cfg_shdr;
6216 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6217 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6218 
6219 	lpfc_printf_log(phba, KERN_INFO, LOG_MBOX,
6220 				"0194 SET_BEACON_CONFIG mailbox "
6221 				"completed with status x%x add_status x%x,"
6222 				" mbx status x%x\n",
6223 				shdr_status, shdr_add_status, mb->mbxStatus);
6224 
6225 	if ((mb->mbxStatus != MBX_SUCCESS) || shdr_status ||
6226 	    (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE) ||
6227 	    (shdr_add_status == ADD_STATUS_INVALID_REQUEST)) {
6228 		mempool_free(pmb, phba->mbox_mem_pool);
6229 		goto error;
6230 	}
6231 
6232 	mempool_free(pmb, phba->mbox_mem_pool);
6233 	cmdsize = sizeof(struct fc_lcb_res_frame);
6234 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6235 			lpfc_max_els_tries, ndlp,
6236 			ndlp->nlp_DID, ELS_CMD_ACC);
6237 
6238 	/* Decrement the ndlp reference count from previous mbox command */
6239 	lpfc_nlp_put(ndlp);
6240 
6241 	if (!elsiocb)
6242 		goto free_lcb_context;
6243 
6244 	lcb_res = (struct fc_lcb_res_frame *)
6245 		(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6246 
6247 	memset(lcb_res, 0, sizeof(struct fc_lcb_res_frame));
6248 	icmd = &elsiocb->iocb;
6249 	icmd->ulpContext = lcb_context->rx_id;
6250 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6251 
6252 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6253 	*((uint32_t *)(pcmd)) = ELS_CMD_ACC;
6254 	lcb_res->lcb_sub_command = lcb_context->sub_command;
6255 	lcb_res->lcb_type = lcb_context->type;
6256 	lcb_res->capability = lcb_context->capability;
6257 	lcb_res->lcb_frequency = lcb_context->frequency;
6258 	lcb_res->lcb_duration = lcb_context->duration;
6259 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6260 	phba->fc_stat.elsXmitACC++;
6261 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6262 	if (rc == IOCB_ERROR)
6263 		lpfc_els_free_iocb(phba, elsiocb);
6264 
6265 	kfree(lcb_context);
6266 	return;
6267 
6268 error:
6269 	cmdsize = sizeof(struct fc_lcb_res_frame);
6270 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
6271 			lpfc_max_els_tries, ndlp,
6272 			ndlp->nlp_DID, ELS_CMD_LS_RJT);
6273 	lpfc_nlp_put(ndlp);
6274 	if (!elsiocb)
6275 		goto free_lcb_context;
6276 
6277 	icmd = &elsiocb->iocb;
6278 	icmd->ulpContext = lcb_context->rx_id;
6279 	icmd->unsli3.rcvsli3.ox_id = lcb_context->ox_id;
6280 	pcmd = (uint8_t *)(((struct lpfc_dmabuf *)elsiocb->context2)->virt);
6281 
6282 	*((uint32_t *)(pcmd)) = ELS_CMD_LS_RJT;
6283 	stat = (struct ls_rjt *)(pcmd + sizeof(uint32_t));
6284 	stat->un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
6285 
6286 	if (shdr_add_status == ADD_STATUS_OPERATION_ALREADY_ACTIVE)
6287 		stat->un.b.lsRjtRsnCodeExp = LSEXP_CMD_IN_PROGRESS;
6288 
6289 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
6290 	phba->fc_stat.elsXmitLSRJT++;
6291 	rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
6292 	if (rc == IOCB_ERROR)
6293 		lpfc_els_free_iocb(phba, elsiocb);
6294 free_lcb_context:
6295 	kfree(lcb_context);
6296 }
6297 
6298 static int
lpfc_sli4_set_beacon(struct lpfc_vport * vport,struct lpfc_lcb_context * lcb_context,uint32_t beacon_state)6299 lpfc_sli4_set_beacon(struct lpfc_vport *vport,
6300 		     struct lpfc_lcb_context *lcb_context,
6301 		     uint32_t beacon_state)
6302 {
6303 	struct lpfc_hba *phba = vport->phba;
6304 	union lpfc_sli4_cfg_shdr *cfg_shdr;
6305 	LPFC_MBOXQ_t *mbox = NULL;
6306 	uint32_t len;
6307 	int rc;
6308 
6309 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6310 	if (!mbox)
6311 		return 1;
6312 
6313 	cfg_shdr = &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
6314 	len = sizeof(struct lpfc_mbx_set_beacon_config) -
6315 		sizeof(struct lpfc_sli4_cfg_mhdr);
6316 	lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6317 			 LPFC_MBOX_OPCODE_SET_BEACON_CONFIG, len,
6318 			 LPFC_SLI4_MBX_EMBED);
6319 	mbox->ctx_ndlp = (void *)lcb_context;
6320 	mbox->vport = phba->pport;
6321 	mbox->mbox_cmpl = lpfc_els_lcb_rsp;
6322 	bf_set(lpfc_mbx_set_beacon_port_num, &mbox->u.mqe.un.beacon_config,
6323 	       phba->sli4_hba.physical_port);
6324 	bf_set(lpfc_mbx_set_beacon_state, &mbox->u.mqe.un.beacon_config,
6325 	       beacon_state);
6326 	mbox->u.mqe.un.beacon_config.word5 = 0;		/* Reserved */
6327 
6328 	/*
6329 	 *	Check bv1s bit before issuing the mailbox
6330 	 *	if bv1s == 1, LCB V1 supported
6331 	 *	else, LCB V0 supported
6332 	 */
6333 
6334 	if (phba->sli4_hba.pc_sli4_params.bv1s) {
6335 		/* COMMON_SET_BEACON_CONFIG_V1 */
6336 		cfg_shdr->request.word9 = BEACON_VERSION_V1;
6337 		lcb_context->capability |= LCB_CAPABILITY_DURATION;
6338 		bf_set(lpfc_mbx_set_beacon_port_type,
6339 		       &mbox->u.mqe.un.beacon_config, 0);
6340 		bf_set(lpfc_mbx_set_beacon_duration_v1,
6341 		       &mbox->u.mqe.un.beacon_config,
6342 		       be16_to_cpu(lcb_context->duration));
6343 	} else {
6344 		/* COMMON_SET_BEACON_CONFIG_V0 */
6345 		if (be16_to_cpu(lcb_context->duration) != 0) {
6346 			mempool_free(mbox, phba->mbox_mem_pool);
6347 			return 1;
6348 		}
6349 		cfg_shdr->request.word9 = BEACON_VERSION_V0;
6350 		lcb_context->capability &=  ~(LCB_CAPABILITY_DURATION);
6351 		bf_set(lpfc_mbx_set_beacon_state,
6352 		       &mbox->u.mqe.un.beacon_config, beacon_state);
6353 		bf_set(lpfc_mbx_set_beacon_port_type,
6354 		       &mbox->u.mqe.un.beacon_config, 1);
6355 		bf_set(lpfc_mbx_set_beacon_duration,
6356 		       &mbox->u.mqe.un.beacon_config,
6357 		       be16_to_cpu(lcb_context->duration));
6358 	}
6359 
6360 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6361 	if (rc == MBX_NOT_FINISHED) {
6362 		mempool_free(mbox, phba->mbox_mem_pool);
6363 		return 1;
6364 	}
6365 
6366 	return 0;
6367 }
6368 
6369 
6370 /**
6371  * lpfc_els_rcv_lcb - Process an unsolicited LCB
6372  * @vport: pointer to a host virtual N_Port data structure.
6373  * @cmdiocb: pointer to lpfc command iocb data structure.
6374  * @ndlp: pointer to a node-list data structure.
6375  *
6376  * This routine processes an unsolicited LCB(LINK CABLE BEACON) IOCB.
6377  * First, the payload of the unsolicited LCB is checked.
6378  * Then based on Subcommand beacon will either turn on or off.
6379  *
6380  * Return code
6381  * 0 - Sent the acc response
6382  * 1 - Sent the reject response.
6383  **/
6384 static int
lpfc_els_rcv_lcb(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)6385 lpfc_els_rcv_lcb(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6386 		 struct lpfc_nodelist *ndlp)
6387 {
6388 	struct lpfc_hba *phba = vport->phba;
6389 	struct lpfc_dmabuf *pcmd;
6390 	uint8_t *lp;
6391 	struct fc_lcb_request_frame *beacon;
6392 	struct lpfc_lcb_context *lcb_context;
6393 	uint8_t state, rjt_err;
6394 	struct ls_rjt stat;
6395 
6396 	pcmd = (struct lpfc_dmabuf *)cmdiocb->context2;
6397 	lp = (uint8_t *)pcmd->virt;
6398 	beacon = (struct fc_lcb_request_frame *)pcmd->virt;
6399 
6400 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6401 			"0192 ELS LCB Data x%x x%x x%x x%x sub x%x "
6402 			"type x%x frequency %x duration x%x\n",
6403 			lp[0], lp[1], lp[2],
6404 			beacon->lcb_command,
6405 			beacon->lcb_sub_command,
6406 			beacon->lcb_type,
6407 			beacon->lcb_frequency,
6408 			be16_to_cpu(beacon->lcb_duration));
6409 
6410 	if (beacon->lcb_sub_command != LPFC_LCB_ON &&
6411 	    beacon->lcb_sub_command != LPFC_LCB_OFF) {
6412 		rjt_err = LSRJT_CMD_UNSUPPORTED;
6413 		goto rjt;
6414 	}
6415 
6416 	if (phba->sli_rev < LPFC_SLI_REV4  ||
6417 	    phba->hba_flag & HBA_FCOE_MODE ||
6418 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) <
6419 	    LPFC_SLI_INTF_IF_TYPE_2)) {
6420 		rjt_err = LSRJT_CMD_UNSUPPORTED;
6421 		goto rjt;
6422 	}
6423 
6424 	lcb_context = kmalloc(sizeof(*lcb_context), GFP_KERNEL);
6425 	if (!lcb_context) {
6426 		rjt_err = LSRJT_UNABLE_TPC;
6427 		goto rjt;
6428 	}
6429 
6430 	state = (beacon->lcb_sub_command == LPFC_LCB_ON) ? 1 : 0;
6431 	lcb_context->sub_command = beacon->lcb_sub_command;
6432 	lcb_context->capability	= 0;
6433 	lcb_context->type = beacon->lcb_type;
6434 	lcb_context->frequency = beacon->lcb_frequency;
6435 	lcb_context->duration = beacon->lcb_duration;
6436 	lcb_context->ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
6437 	lcb_context->rx_id = cmdiocb->iocb.ulpContext;
6438 	lcb_context->ndlp = lpfc_nlp_get(ndlp);
6439 	if (lpfc_sli4_set_beacon(vport, lcb_context, state)) {
6440 		lpfc_printf_vlog(ndlp->vport, KERN_ERR, LOG_TRACE_EVENT,
6441 				 "0193 failed to send mail box");
6442 		kfree(lcb_context);
6443 		lpfc_nlp_put(ndlp);
6444 		rjt_err = LSRJT_UNABLE_TPC;
6445 		goto rjt;
6446 	}
6447 	return 0;
6448 rjt:
6449 	memset(&stat, 0, sizeof(stat));
6450 	stat.un.b.lsRjtRsnCode = rjt_err;
6451 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
6452 	return 1;
6453 }
6454 
6455 
6456 /**
6457  * lpfc_els_flush_rscn - Clean up any rscn activities with a vport
6458  * @vport: pointer to a host virtual N_Port data structure.
6459  *
6460  * This routine cleans up any Registration State Change Notification
6461  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
6462  * @vport together with the host_lock is used to prevent multiple thread
6463  * trying to access the RSCN array on a same @vport at the same time.
6464  **/
6465 void
lpfc_els_flush_rscn(struct lpfc_vport * vport)6466 lpfc_els_flush_rscn(struct lpfc_vport *vport)
6467 {
6468 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6469 	struct lpfc_hba  *phba = vport->phba;
6470 	int i;
6471 
6472 	spin_lock_irq(shost->host_lock);
6473 	if (vport->fc_rscn_flush) {
6474 		/* Another thread is walking fc_rscn_id_list on this vport */
6475 		spin_unlock_irq(shost->host_lock);
6476 		return;
6477 	}
6478 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
6479 	vport->fc_rscn_flush = 1;
6480 	spin_unlock_irq(shost->host_lock);
6481 
6482 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6483 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
6484 		vport->fc_rscn_id_list[i] = NULL;
6485 	}
6486 	spin_lock_irq(shost->host_lock);
6487 	vport->fc_rscn_id_cnt = 0;
6488 	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
6489 	spin_unlock_irq(shost->host_lock);
6490 	lpfc_can_disctmo(vport);
6491 	/* Indicate we are done walking this fc_rscn_id_list */
6492 	vport->fc_rscn_flush = 0;
6493 }
6494 
6495 /**
6496  * lpfc_rscn_payload_check - Check whether there is a pending rscn to a did
6497  * @vport: pointer to a host virtual N_Port data structure.
6498  * @did: remote destination port identifier.
6499  *
6500  * This routine checks whether there is any pending Registration State
6501  * Configuration Notification (RSCN) to a @did on @vport.
6502  *
6503  * Return code
6504  *   None zero - The @did matched with a pending rscn
6505  *   0 - not able to match @did with a pending rscn
6506  **/
6507 int
lpfc_rscn_payload_check(struct lpfc_vport * vport,uint32_t did)6508 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
6509 {
6510 	D_ID ns_did;
6511 	D_ID rscn_did;
6512 	uint32_t *lp;
6513 	uint32_t payload_len, i;
6514 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6515 
6516 	ns_did.un.word = did;
6517 
6518 	/* Never match fabric nodes for RSCNs */
6519 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
6520 		return 0;
6521 
6522 	/* If we are doing a FULL RSCN rediscovery, match everything */
6523 	if (vport->fc_flag & FC_RSCN_DISCOVERY)
6524 		return did;
6525 
6526 	spin_lock_irq(shost->host_lock);
6527 	if (vport->fc_rscn_flush) {
6528 		/* Another thread is walking fc_rscn_id_list on this vport */
6529 		spin_unlock_irq(shost->host_lock);
6530 		return 0;
6531 	}
6532 	/* Indicate we are walking fc_rscn_id_list on this vport */
6533 	vport->fc_rscn_flush = 1;
6534 	spin_unlock_irq(shost->host_lock);
6535 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
6536 		lp = vport->fc_rscn_id_list[i]->virt;
6537 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6538 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
6539 		while (payload_len) {
6540 			rscn_did.un.word = be32_to_cpu(*lp++);
6541 			payload_len -= sizeof(uint32_t);
6542 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
6543 			case RSCN_ADDRESS_FORMAT_PORT:
6544 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6545 				    && (ns_did.un.b.area == rscn_did.un.b.area)
6546 				    && (ns_did.un.b.id == rscn_did.un.b.id))
6547 					goto return_did_out;
6548 				break;
6549 			case RSCN_ADDRESS_FORMAT_AREA:
6550 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
6551 				    && (ns_did.un.b.area == rscn_did.un.b.area))
6552 					goto return_did_out;
6553 				break;
6554 			case RSCN_ADDRESS_FORMAT_DOMAIN:
6555 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
6556 					goto return_did_out;
6557 				break;
6558 			case RSCN_ADDRESS_FORMAT_FABRIC:
6559 				goto return_did_out;
6560 			}
6561 		}
6562 	}
6563 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
6564 	vport->fc_rscn_flush = 0;
6565 	return 0;
6566 return_did_out:
6567 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
6568 	vport->fc_rscn_flush = 0;
6569 	return did;
6570 }
6571 
6572 /**
6573  * lpfc_rscn_recovery_check - Send recovery event to vport nodes matching rscn
6574  * @vport: pointer to a host virtual N_Port data structure.
6575  *
6576  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
6577  * state machine for a @vport's nodes that are with pending RSCN (Registration
6578  * State Change Notification).
6579  *
6580  * Return code
6581  *   0 - Successful (currently alway return 0)
6582  **/
6583 static int
lpfc_rscn_recovery_check(struct lpfc_vport * vport)6584 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
6585 {
6586 	struct lpfc_nodelist *ndlp = NULL;
6587 
6588 	/* Move all affected nodes by pending RSCNs to NPR state. */
6589 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6590 		if (!NLP_CHK_NODE_ACT(ndlp) ||
6591 		    (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
6592 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
6593 			continue;
6594 
6595 		/* NVME Target mode does not do RSCN Recovery. */
6596 		if (vport->phba->nvmet_support)
6597 			continue;
6598 
6599 		/* If we are in the process of doing discovery on this
6600 		 * NPort, let it continue on its own.
6601 		 */
6602 		switch (ndlp->nlp_state) {
6603 		case  NLP_STE_PLOGI_ISSUE:
6604 		case  NLP_STE_ADISC_ISSUE:
6605 		case  NLP_STE_REG_LOGIN_ISSUE:
6606 		case  NLP_STE_PRLI_ISSUE:
6607 		case  NLP_STE_LOGO_ISSUE:
6608 			continue;
6609 		}
6610 
6611 		/* Check to see if we need to NVME rescan this target
6612 		 * remoteport.
6613 		 */
6614 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6615 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6616 			lpfc_nvme_rescan_port(vport, ndlp);
6617 
6618 		lpfc_disc_state_machine(vport, ndlp, NULL,
6619 					NLP_EVT_DEVICE_RECOVERY);
6620 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
6621 	}
6622 	return 0;
6623 }
6624 
6625 /**
6626  * lpfc_send_rscn_event - Send an RSCN event to management application
6627  * @vport: pointer to a host virtual N_Port data structure.
6628  * @cmdiocb: pointer to lpfc command iocb data structure.
6629  *
6630  * lpfc_send_rscn_event sends an RSCN netlink event to management
6631  * applications.
6632  */
6633 static void
lpfc_send_rscn_event(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb)6634 lpfc_send_rscn_event(struct lpfc_vport *vport,
6635 		struct lpfc_iocbq *cmdiocb)
6636 {
6637 	struct lpfc_dmabuf *pcmd;
6638 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6639 	uint32_t *payload_ptr;
6640 	uint32_t payload_len;
6641 	struct lpfc_rscn_event_header *rscn_event_data;
6642 
6643 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6644 	payload_ptr = (uint32_t *) pcmd->virt;
6645 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
6646 
6647 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
6648 		payload_len, GFP_KERNEL);
6649 	if (!rscn_event_data) {
6650 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6651 			"0147 Failed to allocate memory for RSCN event\n");
6652 		return;
6653 	}
6654 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
6655 	rscn_event_data->payload_length = payload_len;
6656 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
6657 		payload_len);
6658 
6659 	fc_host_post_vendor_event(shost,
6660 		fc_get_event_number(),
6661 		sizeof(struct lpfc_rscn_event_header) + payload_len,
6662 		(char *)rscn_event_data,
6663 		LPFC_NL_VENDOR_ID);
6664 
6665 	kfree(rscn_event_data);
6666 }
6667 
6668 /**
6669  * lpfc_els_rcv_rscn - Process an unsolicited rscn iocb
6670  * @vport: pointer to a host virtual N_Port data structure.
6671  * @cmdiocb: pointer to lpfc command iocb data structure.
6672  * @ndlp: pointer to a node-list data structure.
6673  *
6674  * This routine processes an unsolicited RSCN (Registration State Change
6675  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
6676  * to invoke fc_host_post_event() routine to the FC transport layer. If the
6677  * discover state machine is about to begin discovery, it just accepts the
6678  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
6679  * contains N_Port IDs for other vports on this HBA, it just accepts the
6680  * RSCN and ignore processing it. If the state machine is in the recovery
6681  * state, the fc_rscn_id_list of this @vport is walked and the
6682  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
6683  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
6684  * routine is invoked to handle the RSCN event.
6685  *
6686  * Return code
6687  *   0 - Just sent the acc response
6688  *   1 - Sent the acc response and waited for name server completion
6689  **/
6690 static int
lpfc_els_rcv_rscn(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)6691 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
6692 		  struct lpfc_nodelist *ndlp)
6693 {
6694 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6695 	struct lpfc_hba  *phba = vport->phba;
6696 	struct lpfc_dmabuf *pcmd;
6697 	uint32_t *lp, *datap;
6698 	uint32_t payload_len, length, nportid, *cmd;
6699 	int rscn_cnt;
6700 	int rscn_id = 0, hba_id = 0;
6701 	int i, tmo;
6702 
6703 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
6704 	lp = (uint32_t *) pcmd->virt;
6705 
6706 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
6707 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
6708 	/* RSCN received */
6709 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6710 			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
6711 			 vport->fc_flag, payload_len, *lp,
6712 			 vport->fc_rscn_id_cnt);
6713 
6714 	/* Send an RSCN event to the management application */
6715 	lpfc_send_rscn_event(vport, cmdiocb);
6716 
6717 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
6718 		fc_host_post_event(shost, fc_get_event_number(),
6719 			FCH_EVT_RSCN, lp[i]);
6720 
6721 	/* Check if RSCN is coming from a direct-connected remote NPort */
6722 	if (vport->fc_flag & FC_PT2PT) {
6723 		/* If so, just ACC it, no other action needed for now */
6724 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
6725 				 "2024 pt2pt RSCN %08x Data: x%x x%x\n",
6726 				 *lp, vport->fc_flag, payload_len);
6727 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6728 
6729 		/* Check to see if we need to NVME rescan this target
6730 		 * remoteport.
6731 		 */
6732 		if (ndlp->nlp_fc4_type & NLP_FC4_NVME &&
6733 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY))
6734 			lpfc_nvme_rescan_port(vport, ndlp);
6735 		return 0;
6736 	}
6737 
6738 	/* If we are about to begin discovery, just ACC the RSCN.
6739 	 * Discovery processing will satisfy it.
6740 	 */
6741 	if (vport->port_state <= LPFC_NS_QRY) {
6742 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6743 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
6744 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6745 
6746 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6747 		return 0;
6748 	}
6749 
6750 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
6751 	 * just ACC and ignore it.
6752 	 */
6753 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
6754 		!(vport->cfg_peer_port_login)) {
6755 		i = payload_len;
6756 		datap = lp;
6757 		while (i > 0) {
6758 			nportid = *datap++;
6759 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
6760 			i -= sizeof(uint32_t);
6761 			rscn_id++;
6762 			if (lpfc_find_vport_by_did(phba, nportid))
6763 				hba_id++;
6764 		}
6765 		if (rscn_id == hba_id) {
6766 			/* ALL NPortIDs in RSCN are on HBA */
6767 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6768 					 "0219 Ignore RSCN "
6769 					 "Data: x%x x%x x%x x%x\n",
6770 					 vport->fc_flag, payload_len,
6771 					 *lp, vport->fc_rscn_id_cnt);
6772 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6773 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
6774 				ndlp->nlp_DID, vport->port_state,
6775 				ndlp->nlp_flag);
6776 
6777 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
6778 				ndlp, NULL);
6779 			return 0;
6780 		}
6781 	}
6782 
6783 	spin_lock_irq(shost->host_lock);
6784 	if (vport->fc_rscn_flush) {
6785 		/* Another thread is walking fc_rscn_id_list on this vport */
6786 		vport->fc_flag |= FC_RSCN_DISCOVERY;
6787 		spin_unlock_irq(shost->host_lock);
6788 		/* Send back ACC */
6789 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6790 		return 0;
6791 	}
6792 	/* Indicate we are walking fc_rscn_id_list on this vport */
6793 	vport->fc_rscn_flush = 1;
6794 	spin_unlock_irq(shost->host_lock);
6795 	/* Get the array count after successfully have the token */
6796 	rscn_cnt = vport->fc_rscn_id_cnt;
6797 	/* If we are already processing an RSCN, save the received
6798 	 * RSCN payload buffer, cmdiocb->context2 to process later.
6799 	 */
6800 	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
6801 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6802 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
6803 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6804 
6805 		spin_lock_irq(shost->host_lock);
6806 		vport->fc_flag |= FC_RSCN_DEFERRED;
6807 
6808 		/* Restart disctmo if its already running */
6809 		if (vport->fc_flag & FC_DISC_TMO) {
6810 			tmo = ((phba->fc_ratov * 3) + 3);
6811 			mod_timer(&vport->fc_disctmo,
6812 				  jiffies + msecs_to_jiffies(1000 * tmo));
6813 		}
6814 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
6815 		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
6816 			vport->fc_flag |= FC_RSCN_MODE;
6817 			spin_unlock_irq(shost->host_lock);
6818 			if (rscn_cnt) {
6819 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
6820 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
6821 			}
6822 			if ((rscn_cnt) &&
6823 			    (payload_len + length <= LPFC_BPL_SIZE)) {
6824 				*cmd &= ELS_CMD_MASK;
6825 				*cmd |= cpu_to_be32(payload_len + length);
6826 				memcpy(((uint8_t *)cmd) + length, lp,
6827 				       payload_len);
6828 			} else {
6829 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
6830 				vport->fc_rscn_id_cnt++;
6831 				/* If we zero, cmdiocb->context2, the calling
6832 				 * routine will not try to free it.
6833 				 */
6834 				cmdiocb->context2 = NULL;
6835 			}
6836 			/* Deferred RSCN */
6837 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6838 					 "0235 Deferred RSCN "
6839 					 "Data: x%x x%x x%x\n",
6840 					 vport->fc_rscn_id_cnt, vport->fc_flag,
6841 					 vport->port_state);
6842 		} else {
6843 			vport->fc_flag |= FC_RSCN_DISCOVERY;
6844 			spin_unlock_irq(shost->host_lock);
6845 			/* ReDiscovery RSCN */
6846 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6847 					 "0234 ReDiscovery RSCN "
6848 					 "Data: x%x x%x x%x\n",
6849 					 vport->fc_rscn_id_cnt, vport->fc_flag,
6850 					 vport->port_state);
6851 		}
6852 		/* Indicate we are done walking fc_rscn_id_list on this vport */
6853 		vport->fc_rscn_flush = 0;
6854 		/* Send back ACC */
6855 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6856 		/* send RECOVERY event for ALL nodes that match RSCN payload */
6857 		lpfc_rscn_recovery_check(vport);
6858 		return 0;
6859 	}
6860 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
6861 		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
6862 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
6863 
6864 	spin_lock_irq(shost->host_lock);
6865 	vport->fc_flag |= FC_RSCN_MODE;
6866 	spin_unlock_irq(shost->host_lock);
6867 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
6868 	/* Indicate we are done walking fc_rscn_id_list on this vport */
6869 	vport->fc_rscn_flush = 0;
6870 	/*
6871 	 * If we zero, cmdiocb->context2, the calling routine will
6872 	 * not try to free it.
6873 	 */
6874 	cmdiocb->context2 = NULL;
6875 	lpfc_set_disctmo(vport);
6876 	/* Send back ACC */
6877 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
6878 	/* send RECOVERY event for ALL nodes that match RSCN payload */
6879 	lpfc_rscn_recovery_check(vport);
6880 	return lpfc_els_handle_rscn(vport);
6881 }
6882 
6883 /**
6884  * lpfc_els_handle_rscn - Handle rscn for a vport
6885  * @vport: pointer to a host virtual N_Port data structure.
6886  *
6887  * This routine handles the Registration State Configuration Notification
6888  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
6889  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
6890  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
6891  * NameServer shall be issued. If CT command to the NameServer fails to be
6892  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
6893  * RSCN activities with the @vport.
6894  *
6895  * Return code
6896  *   0 - Cleaned up rscn on the @vport
6897  *   1 - Wait for plogi to name server before proceed
6898  **/
6899 int
lpfc_els_handle_rscn(struct lpfc_vport * vport)6900 lpfc_els_handle_rscn(struct lpfc_vport *vport)
6901 {
6902 	struct lpfc_nodelist *ndlp;
6903 	struct lpfc_hba  *phba = vport->phba;
6904 
6905 	/* Ignore RSCN if the port is being torn down. */
6906 	if (vport->load_flag & FC_UNLOADING) {
6907 		lpfc_els_flush_rscn(vport);
6908 		return 0;
6909 	}
6910 
6911 	/* Start timer for RSCN processing */
6912 	lpfc_set_disctmo(vport);
6913 
6914 	/* RSCN processed */
6915 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
6916 			 "0215 RSCN processed Data: x%x x%x x%x x%x x%x x%x\n",
6917 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
6918 			 vport->port_state, vport->num_disc_nodes,
6919 			 vport->gidft_inp);
6920 
6921 	/* To process RSCN, first compare RSCN data with NameServer */
6922 	vport->fc_ns_retry = 0;
6923 	vport->num_disc_nodes = 0;
6924 
6925 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
6926 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
6927 	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
6928 		/* Good ndlp, issue CT Request to NameServer.  Need to
6929 		 * know how many gidfts were issued.  If none, then just
6930 		 * flush the RSCN.  Otherwise, the outstanding requests
6931 		 * need to complete.
6932 		 */
6933 		if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_FT) {
6934 			if (lpfc_issue_gidft(vport) > 0)
6935 				return 1;
6936 		} else if (phba->cfg_ns_query == LPFC_NS_QUERY_GID_PT) {
6937 			if (lpfc_issue_gidpt(vport) > 0)
6938 				return 1;
6939 		} else {
6940 			return 1;
6941 		}
6942 	} else {
6943 		/* Nameserver login in question.  Revalidate. */
6944 		if (ndlp) {
6945 			ndlp = lpfc_enable_node(vport, ndlp,
6946 						NLP_STE_PLOGI_ISSUE);
6947 			if (!ndlp) {
6948 				lpfc_els_flush_rscn(vport);
6949 				return 0;
6950 			}
6951 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
6952 		} else {
6953 			ndlp = lpfc_nlp_init(vport, NameServer_DID);
6954 			if (!ndlp) {
6955 				lpfc_els_flush_rscn(vport);
6956 				return 0;
6957 			}
6958 			ndlp->nlp_prev_state = ndlp->nlp_state;
6959 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
6960 		}
6961 		ndlp->nlp_type |= NLP_FABRIC;
6962 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
6963 		/* Wait for NameServer login cmpl before we can
6964 		 * continue
6965 		 */
6966 		return 1;
6967 	}
6968 
6969 	lpfc_els_flush_rscn(vport);
6970 	return 0;
6971 }
6972 
6973 /**
6974  * lpfc_els_rcv_flogi - Process an unsolicited flogi iocb
6975  * @vport: pointer to a host virtual N_Port data structure.
6976  * @cmdiocb: pointer to lpfc command iocb data structure.
6977  * @ndlp: pointer to a node-list data structure.
6978  *
6979  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
6980  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
6981  * point topology. As an unsolicited FLOGI should not be received in a loop
6982  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
6983  * lpfc_check_sparm() routine is invoked to check the parameters in the
6984  * unsolicited FLOGI. If parameters validation failed, the routine
6985  * lpfc_els_rsp_reject() shall be called with reject reason code set to
6986  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
6987  * FLOGI shall be compared with the Port WWN of the @vport to determine who
6988  * will initiate PLOGI. The higher lexicographical value party shall has
6989  * higher priority (as the winning port) and will initiate PLOGI and
6990  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
6991  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
6992  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
6993  *
6994  * Return code
6995  *   0 - Successfully processed the unsolicited flogi
6996  *   1 - Failed to process the unsolicited flogi
6997  **/
6998 static int
lpfc_els_rcv_flogi(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)6999 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7000 		   struct lpfc_nodelist *ndlp)
7001 {
7002 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7003 	struct lpfc_hba  *phba = vport->phba;
7004 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7005 	uint32_t *lp = (uint32_t *) pcmd->virt;
7006 	IOCB_t *icmd = &cmdiocb->iocb;
7007 	struct serv_parm *sp;
7008 	LPFC_MBOXQ_t *mbox;
7009 	uint32_t cmd, did;
7010 	int rc;
7011 	uint32_t fc_flag = 0;
7012 	uint32_t port_state = 0;
7013 
7014 	cmd = *lp++;
7015 	sp = (struct serv_parm *) lp;
7016 
7017 	/* FLOGI received */
7018 
7019 	lpfc_set_disctmo(vport);
7020 
7021 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
7022 		/* We should never receive a FLOGI in loop mode, ignore it */
7023 		did = icmd->un.elsreq64.remoteID;
7024 
7025 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
7026 		   Loop Mode */
7027 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
7028 				 "0113 An FLOGI ELS command x%x was "
7029 				 "received from DID x%x in Loop Mode\n",
7030 				 cmd, did);
7031 		return 1;
7032 	}
7033 
7034 	(void) lpfc_check_sparm(vport, ndlp, sp, CLASS3, 1);
7035 
7036 	/*
7037 	 * If our portname is greater than the remote portname,
7038 	 * then we initiate Nport login.
7039 	 */
7040 
7041 	rc = memcmp(&vport->fc_portname, &sp->portName,
7042 		    sizeof(struct lpfc_name));
7043 
7044 	if (!rc) {
7045 		if (phba->sli_rev < LPFC_SLI_REV4) {
7046 			mbox = mempool_alloc(phba->mbox_mem_pool,
7047 					     GFP_KERNEL);
7048 			if (!mbox)
7049 				return 1;
7050 			lpfc_linkdown(phba);
7051 			lpfc_init_link(phba, mbox,
7052 				       phba->cfg_topology,
7053 				       phba->cfg_link_speed);
7054 			mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
7055 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7056 			mbox->vport = vport;
7057 			rc = lpfc_sli_issue_mbox(phba, mbox,
7058 						 MBX_NOWAIT);
7059 			lpfc_set_loopback_flag(phba);
7060 			if (rc == MBX_NOT_FINISHED)
7061 				mempool_free(mbox, phba->mbox_mem_pool);
7062 			return 1;
7063 		}
7064 
7065 		/* abort the flogi coming back to ourselves
7066 		 * due to external loopback on the port.
7067 		 */
7068 		lpfc_els_abort_flogi(phba);
7069 		return 0;
7070 
7071 	} else if (rc > 0) {	/* greater than */
7072 		spin_lock_irq(shost->host_lock);
7073 		vport->fc_flag |= FC_PT2PT_PLOGI;
7074 		spin_unlock_irq(shost->host_lock);
7075 
7076 		/* If we have the high WWPN we can assign our own
7077 		 * myDID; otherwise, we have to WAIT for a PLOGI
7078 		 * from the remote NPort to find out what it
7079 		 * will be.
7080 		 */
7081 		vport->fc_myDID = PT2PT_LocalID;
7082 	} else {
7083 		vport->fc_myDID = PT2PT_RemoteID;
7084 	}
7085 
7086 	/*
7087 	 * The vport state should go to LPFC_FLOGI only
7088 	 * AFTER we issue a FLOGI, not receive one.
7089 	 */
7090 	spin_lock_irq(shost->host_lock);
7091 	fc_flag = vport->fc_flag;
7092 	port_state = vport->port_state;
7093 	vport->fc_flag |= FC_PT2PT;
7094 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
7095 
7096 	/* Acking an unsol FLOGI.  Count 1 for link bounce
7097 	 * work-around.
7098 	 */
7099 	vport->rcv_flogi_cnt++;
7100 	spin_unlock_irq(shost->host_lock);
7101 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7102 			 "3311 Rcv Flogi PS x%x new PS x%x "
7103 			 "fc_flag x%x new fc_flag x%x\n",
7104 			 port_state, vport->port_state,
7105 			 fc_flag, vport->fc_flag);
7106 
7107 	/*
7108 	 * We temporarily set fc_myDID to make it look like we are
7109 	 * a Fabric. This is done just so we end up with the right
7110 	 * did / sid on the FLOGI ACC rsp.
7111 	 */
7112 	did = vport->fc_myDID;
7113 	vport->fc_myDID = Fabric_DID;
7114 
7115 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
7116 
7117 	/* Defer ACC response until AFTER we issue a FLOGI */
7118 	if (!(phba->hba_flag & HBA_FLOGI_ISSUED)) {
7119 		phba->defer_flogi_acc_rx_id = cmdiocb->iocb.ulpContext;
7120 		phba->defer_flogi_acc_ox_id =
7121 					cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7122 
7123 		vport->fc_myDID = did;
7124 
7125 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7126 				 "3344 Deferring FLOGI ACC: rx_id: x%x,"
7127 				 " ox_id: x%x, hba_flag x%x\n",
7128 				 phba->defer_flogi_acc_rx_id,
7129 				 phba->defer_flogi_acc_ox_id, phba->hba_flag);
7130 
7131 		phba->defer_flogi_acc_flag = true;
7132 
7133 		return 0;
7134 	}
7135 
7136 	/* Send back ACC */
7137 	lpfc_els_rsp_acc(vport, ELS_CMD_FLOGI, cmdiocb, ndlp, NULL);
7138 
7139 	/* Now lets put fc_myDID back to what its supposed to be */
7140 	vport->fc_myDID = did;
7141 
7142 	return 0;
7143 }
7144 
7145 /**
7146  * lpfc_els_rcv_rnid - Process an unsolicited rnid iocb
7147  * @vport: pointer to a host virtual N_Port data structure.
7148  * @cmdiocb: pointer to lpfc command iocb data structure.
7149  * @ndlp: pointer to a node-list data structure.
7150  *
7151  * This routine processes Request Node Identification Data (RNID) IOCB
7152  * received as an ELS unsolicited event. Only when the RNID specified format
7153  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
7154  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
7155  * Accept (ACC) the RNID ELS command. All the other RNID formats are
7156  * rejected by invoking the lpfc_els_rsp_reject() routine.
7157  *
7158  * Return code
7159  *   0 - Successfully processed rnid iocb (currently always return 0)
7160  **/
7161 static int
lpfc_els_rcv_rnid(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7162 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7163 		  struct lpfc_nodelist *ndlp)
7164 {
7165 	struct lpfc_dmabuf *pcmd;
7166 	uint32_t *lp;
7167 	RNID *rn;
7168 	struct ls_rjt stat;
7169 
7170 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7171 	lp = (uint32_t *) pcmd->virt;
7172 
7173 	lp++;
7174 	rn = (RNID *) lp;
7175 
7176 	/* RNID received */
7177 
7178 	switch (rn->Format) {
7179 	case 0:
7180 	case RNID_TOPOLOGY_DISC:
7181 		/* Send back ACC */
7182 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
7183 		break;
7184 	default:
7185 		/* Reject this request because format not supported */
7186 		stat.un.b.lsRjtRsvd0 = 0;
7187 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7188 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7189 		stat.un.b.vendorUnique = 0;
7190 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7191 			NULL);
7192 	}
7193 	return 0;
7194 }
7195 
7196 /**
7197  * lpfc_els_rcv_echo - Process an unsolicited echo iocb
7198  * @vport: pointer to a host virtual N_Port data structure.
7199  * @cmdiocb: pointer to lpfc command iocb data structure.
7200  * @ndlp: pointer to a node-list data structure.
7201  *
7202  * Return code
7203  *   0 - Successfully processed echo iocb (currently always return 0)
7204  **/
7205 static int
lpfc_els_rcv_echo(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7206 lpfc_els_rcv_echo(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7207 		  struct lpfc_nodelist *ndlp)
7208 {
7209 	uint8_t *pcmd;
7210 
7211 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
7212 
7213 	/* skip over first word of echo command to find echo data */
7214 	pcmd += sizeof(uint32_t);
7215 
7216 	lpfc_els_rsp_echo_acc(vport, pcmd, cmdiocb, ndlp);
7217 	return 0;
7218 }
7219 
7220 /**
7221  * lpfc_els_rcv_lirr - Process an unsolicited lirr iocb
7222  * @vport: pointer to a host virtual N_Port data structure.
7223  * @cmdiocb: pointer to lpfc command iocb data structure.
7224  * @ndlp: pointer to a node-list data structure.
7225  *
7226  * This routine processes a Link Incident Report Registration(LIRR) IOCB
7227  * received as an ELS unsolicited event. Currently, this function just invokes
7228  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
7229  *
7230  * Return code
7231  *   0 - Successfully processed lirr iocb (currently always return 0)
7232  **/
7233 static int
lpfc_els_rcv_lirr(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7234 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7235 		  struct lpfc_nodelist *ndlp)
7236 {
7237 	struct ls_rjt stat;
7238 
7239 	/* For now, unconditionally reject this command */
7240 	stat.un.b.lsRjtRsvd0 = 0;
7241 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7242 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7243 	stat.un.b.vendorUnique = 0;
7244 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7245 	return 0;
7246 }
7247 
7248 /**
7249  * lpfc_els_rcv_rrq - Process an unsolicited rrq iocb
7250  * @vport: pointer to a host virtual N_Port data structure.
7251  * @cmdiocb: pointer to lpfc command iocb data structure.
7252  * @ndlp: pointer to a node-list data structure.
7253  *
7254  * This routine processes a Reinstate Recovery Qualifier (RRQ) IOCB
7255  * received as an ELS unsolicited event. A request to RRQ shall only
7256  * be accepted if the Originator Nx_Port N_Port_ID or the Responder
7257  * Nx_Port N_Port_ID of the target Exchange is the same as the
7258  * N_Port_ID of the Nx_Port that makes the request. If the RRQ is
7259  * not accepted, an LS_RJT with reason code "Unable to perform
7260  * command request" and reason code explanation "Invalid Originator
7261  * S_ID" shall be returned. For now, we just unconditionally accept
7262  * RRQ from the target.
7263  **/
7264 static void
lpfc_els_rcv_rrq(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7265 lpfc_els_rcv_rrq(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7266 		 struct lpfc_nodelist *ndlp)
7267 {
7268 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7269 	if (vport->phba->sli_rev == LPFC_SLI_REV4)
7270 		lpfc_els_clear_rrq(vport, cmdiocb, ndlp);
7271 }
7272 
7273 /**
7274  * lpfc_els_rsp_rls_acc - Completion callbk func for MBX_READ_LNK_STAT mbox cmd
7275  * @phba: pointer to lpfc hba data structure.
7276  * @pmb: pointer to the driver internal queue element for mailbox command.
7277  *
7278  * This routine is the completion callback function for the MBX_READ_LNK_STAT
7279  * mailbox command. This callback function is to actually send the Accept
7280  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
7281  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
7282  * mailbox command, constructs the RPS response with the link statistics
7283  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
7284  * response to the RPS.
7285  *
7286  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7287  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7288  * will be stored into the context1 field of the IOCB for the completion
7289  * callback function to the RPS Accept Response ELS IOCB command.
7290  *
7291  **/
7292 static void
lpfc_els_rsp_rls_acc(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)7293 lpfc_els_rsp_rls_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
7294 {
7295 	MAILBOX_t *mb;
7296 	IOCB_t *icmd;
7297 	struct RLS_RSP *rls_rsp;
7298 	uint8_t *pcmd;
7299 	struct lpfc_iocbq *elsiocb;
7300 	struct lpfc_nodelist *ndlp;
7301 	uint16_t oxid;
7302 	uint16_t rxid;
7303 	uint32_t cmdsize;
7304 
7305 	mb = &pmb->u.mb;
7306 
7307 	ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
7308 	rxid = (uint16_t)((unsigned long)(pmb->ctx_buf) & 0xffff);
7309 	oxid = (uint16_t)(((unsigned long)(pmb->ctx_buf) >> 16) & 0xffff);
7310 	pmb->ctx_buf = NULL;
7311 	pmb->ctx_ndlp = NULL;
7312 
7313 	if (mb->mbxStatus) {
7314 		mempool_free(pmb, phba->mbox_mem_pool);
7315 		return;
7316 	}
7317 
7318 	cmdsize = sizeof(struct RLS_RSP) + sizeof(uint32_t);
7319 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7320 				     lpfc_max_els_tries, ndlp,
7321 				     ndlp->nlp_DID, ELS_CMD_ACC);
7322 
7323 	/* Decrement the ndlp reference count from previous mbox command */
7324 	lpfc_nlp_put(ndlp);
7325 
7326 	if (!elsiocb) {
7327 		mempool_free(pmb, phba->mbox_mem_pool);
7328 		return;
7329 	}
7330 
7331 	icmd = &elsiocb->iocb;
7332 	icmd->ulpContext = rxid;
7333 	icmd->unsli3.rcvsli3.ox_id = oxid;
7334 
7335 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7336 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7337 	pcmd += sizeof(uint32_t); /* Skip past command */
7338 	rls_rsp = (struct RLS_RSP *)pcmd;
7339 
7340 	rls_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
7341 	rls_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
7342 	rls_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
7343 	rls_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
7344 	rls_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
7345 	rls_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
7346 	mempool_free(pmb, phba->mbox_mem_pool);
7347 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
7348 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7349 			 "2874 Xmit ELS RLS ACC response tag x%x xri x%x, "
7350 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
7351 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
7352 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7353 			 ndlp->nlp_rpi);
7354 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7355 	phba->fc_stat.elsXmitACC++;
7356 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7357 		lpfc_els_free_iocb(phba, elsiocb);
7358 }
7359 
7360 /**
7361  * lpfc_els_rcv_rls - Process an unsolicited rls iocb
7362  * @vport: pointer to a host virtual N_Port data structure.
7363  * @cmdiocb: pointer to lpfc command iocb data structure.
7364  * @ndlp: pointer to a node-list data structure.
7365  *
7366  * This routine processes Read Link Status (RLS) IOCB received as an
7367  * ELS unsolicited event. It first checks the remote port state. If the
7368  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7369  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7370  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
7371  * for reading the HBA link statistics. It is for the callback function,
7372  * lpfc_els_rsp_rls_acc(), set to the MBX_READ_LNK_STAT mailbox command
7373  * to actually sending out RPL Accept (ACC) response.
7374  *
7375  * Return codes
7376  *   0 - Successfully processed rls iocb (currently always return 0)
7377  **/
7378 static int
lpfc_els_rcv_rls(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7379 lpfc_els_rcv_rls(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7380 		 struct lpfc_nodelist *ndlp)
7381 {
7382 	struct lpfc_hba *phba = vport->phba;
7383 	LPFC_MBOXQ_t *mbox;
7384 	struct ls_rjt stat;
7385 
7386 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7387 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7388 		/* reject the unsolicited RLS request and done with it */
7389 		goto reject_out;
7390 
7391 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
7392 	if (mbox) {
7393 		lpfc_read_lnk_stat(phba, mbox);
7394 		mbox->ctx_buf = (void *)((unsigned long)
7395 			((cmdiocb->iocb.unsli3.rcvsli3.ox_id << 16) |
7396 			cmdiocb->iocb.ulpContext)); /* rx_id */
7397 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
7398 		mbox->vport = vport;
7399 		mbox->mbox_cmpl = lpfc_els_rsp_rls_acc;
7400 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
7401 			!= MBX_NOT_FINISHED)
7402 			/* Mbox completion will send ELS Response */
7403 			return 0;
7404 		/* Decrement reference count used for the failed mbox
7405 		 * command.
7406 		 */
7407 		lpfc_nlp_put(ndlp);
7408 		mempool_free(mbox, phba->mbox_mem_pool);
7409 	}
7410 reject_out:
7411 	/* issue rejection response */
7412 	stat.un.b.lsRjtRsvd0 = 0;
7413 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7414 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7415 	stat.un.b.vendorUnique = 0;
7416 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7417 	return 0;
7418 }
7419 
7420 /**
7421  * lpfc_els_rcv_rtv - Process an unsolicited rtv iocb
7422  * @vport: pointer to a host virtual N_Port data structure.
7423  * @cmdiocb: pointer to lpfc command iocb data structure.
7424  * @ndlp: pointer to a node-list data structure.
7425  *
7426  * This routine processes Read Timout Value (RTV) IOCB received as an
7427  * ELS unsolicited event. It first checks the remote port state. If the
7428  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
7429  * state, it invokes the lpfc_els_rsl_reject() routine to send the reject
7430  * response. Otherwise, it sends the Accept(ACC) response to a Read Timeout
7431  * Value (RTV) unsolicited IOCB event.
7432  *
7433  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7434  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7435  * will be stored into the context1 field of the IOCB for the completion
7436  * callback function to the RTV Accept Response ELS IOCB command.
7437  *
7438  * Return codes
7439  *   0 - Successfully processed rtv iocb (currently always return 0)
7440  **/
7441 static int
lpfc_els_rcv_rtv(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7442 lpfc_els_rcv_rtv(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7443 		 struct lpfc_nodelist *ndlp)
7444 {
7445 	struct lpfc_hba *phba = vport->phba;
7446 	struct ls_rjt stat;
7447 	struct RTV_RSP *rtv_rsp;
7448 	uint8_t *pcmd;
7449 	struct lpfc_iocbq *elsiocb;
7450 	uint32_t cmdsize;
7451 
7452 
7453 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7454 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
7455 		/* reject the unsolicited RTV request and done with it */
7456 		goto reject_out;
7457 
7458 	cmdsize = sizeof(struct RTV_RSP) + sizeof(uint32_t);
7459 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
7460 				     lpfc_max_els_tries, ndlp,
7461 				     ndlp->nlp_DID, ELS_CMD_ACC);
7462 
7463 	if (!elsiocb)
7464 		return 1;
7465 
7466 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7467 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7468 	pcmd += sizeof(uint32_t); /* Skip past command */
7469 
7470 	/* use the command's xri in the response */
7471 	elsiocb->iocb.ulpContext = cmdiocb->iocb.ulpContext;  /* Xri / rx_id */
7472 	elsiocb->iocb.unsli3.rcvsli3.ox_id = cmdiocb->iocb.unsli3.rcvsli3.ox_id;
7473 
7474 	rtv_rsp = (struct RTV_RSP *)pcmd;
7475 
7476 	/* populate RTV payload */
7477 	rtv_rsp->ratov = cpu_to_be32(phba->fc_ratov * 1000); /* report msecs */
7478 	rtv_rsp->edtov = cpu_to_be32(phba->fc_edtov);
7479 	bf_set(qtov_edtovres, rtv_rsp, phba->fc_edtovResol ? 1 : 0);
7480 	bf_set(qtov_rttov, rtv_rsp, 0); /* Field is for FC ONLY */
7481 	rtv_rsp->qtov = cpu_to_be32(rtv_rsp->qtov);
7482 
7483 	/* Xmit ELS RLS ACC response tag <ulpIoTag> */
7484 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
7485 			 "2875 Xmit ELS RTV ACC response tag x%x xri x%x, "
7486 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x, "
7487 			 "Data: x%x x%x x%x\n",
7488 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
7489 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7490 			 ndlp->nlp_rpi,
7491 			rtv_rsp->ratov, rtv_rsp->edtov, rtv_rsp->qtov);
7492 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7493 	phba->fc_stat.elsXmitACC++;
7494 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) == IOCB_ERROR)
7495 		lpfc_els_free_iocb(phba, elsiocb);
7496 	return 0;
7497 
7498 reject_out:
7499 	/* issue rejection response */
7500 	stat.un.b.lsRjtRsvd0 = 0;
7501 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7502 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7503 	stat.un.b.vendorUnique = 0;
7504 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
7505 	return 0;
7506 }
7507 
7508 /* lpfc_issue_els_rrq - Process an unsolicited rrq iocb
7509  * @vport: pointer to a host virtual N_Port data structure.
7510  * @ndlp: pointer to a node-list data structure.
7511  * @did: DID of the target.
7512  * @rrq: Pointer to the rrq struct.
7513  *
7514  * Build a ELS RRQ command and send it to the target. If the issue_iocb is
7515  * Successful the the completion handler will clear the RRQ.
7516  *
7517  * Return codes
7518  *   0 - Successfully sent rrq els iocb.
7519  *   1 - Failed to send rrq els iocb.
7520  **/
7521 static int
lpfc_issue_els_rrq(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint32_t did,struct lpfc_node_rrq * rrq)7522 lpfc_issue_els_rrq(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
7523 			uint32_t did, struct lpfc_node_rrq *rrq)
7524 {
7525 	struct lpfc_hba  *phba = vport->phba;
7526 	struct RRQ *els_rrq;
7527 	struct lpfc_iocbq *elsiocb;
7528 	uint8_t *pcmd;
7529 	uint16_t cmdsize;
7530 	int ret;
7531 
7532 
7533 	if (ndlp != rrq->ndlp)
7534 		ndlp = rrq->ndlp;
7535 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
7536 		return 1;
7537 
7538 	/* If ndlp is not NULL, we will bump the reference count on it */
7539 	cmdsize = (sizeof(uint32_t) + sizeof(struct RRQ));
7540 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, did,
7541 				     ELS_CMD_RRQ);
7542 	if (!elsiocb)
7543 		return 1;
7544 
7545 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7546 
7547 	/* For RRQ request, remainder of payload is Exchange IDs */
7548 	*((uint32_t *) (pcmd)) = ELS_CMD_RRQ;
7549 	pcmd += sizeof(uint32_t);
7550 	els_rrq = (struct RRQ *) pcmd;
7551 
7552 	bf_set(rrq_oxid, els_rrq, phba->sli4_hba.xri_ids[rrq->xritag]);
7553 	bf_set(rrq_rxid, els_rrq, rrq->rxid);
7554 	bf_set(rrq_did, els_rrq, vport->fc_myDID);
7555 	els_rrq->rrq = cpu_to_be32(els_rrq->rrq);
7556 	els_rrq->rrq_exchg = cpu_to_be32(els_rrq->rrq_exchg);
7557 
7558 
7559 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
7560 		"Issue RRQ:     did:x%x",
7561 		did, rrq->xritag, rrq->rxid);
7562 	elsiocb->context_un.rrq = rrq;
7563 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rrq;
7564 	ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0);
7565 
7566 	if (ret == IOCB_ERROR) {
7567 		lpfc_els_free_iocb(phba, elsiocb);
7568 		return 1;
7569 	}
7570 	return 0;
7571 }
7572 
7573 /**
7574  * lpfc_send_rrq - Sends ELS RRQ if needed.
7575  * @phba: pointer to lpfc hba data structure.
7576  * @rrq: pointer to the active rrq.
7577  *
7578  * This routine will call the lpfc_issue_els_rrq if the rrq is
7579  * still active for the xri. If this function returns a failure then
7580  * the caller needs to clean up the RRQ by calling lpfc_clr_active_rrq.
7581  *
7582  * Returns 0 Success.
7583  *         1 Failure.
7584  **/
7585 int
lpfc_send_rrq(struct lpfc_hba * phba,struct lpfc_node_rrq * rrq)7586 lpfc_send_rrq(struct lpfc_hba *phba, struct lpfc_node_rrq *rrq)
7587 {
7588 	struct lpfc_nodelist *ndlp = lpfc_findnode_did(rrq->vport,
7589 						       rrq->nlp_DID);
7590 	if (!ndlp)
7591 		return 1;
7592 
7593 	if (lpfc_test_rrq_active(phba, ndlp, rrq->xritag))
7594 		return lpfc_issue_els_rrq(rrq->vport, ndlp,
7595 					 rrq->nlp_DID, rrq);
7596 	else
7597 		return 1;
7598 }
7599 
7600 /**
7601  * lpfc_els_rsp_rpl_acc - Issue an accept rpl els command
7602  * @vport: pointer to a host virtual N_Port data structure.
7603  * @cmdsize: size of the ELS command.
7604  * @oldiocb: pointer to the original lpfc command iocb data structure.
7605  * @ndlp: pointer to a node-list data structure.
7606  *
7607  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
7608  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
7609  *
7610  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
7611  * will be incremented by 1 for holding the ndlp and the reference to ndlp
7612  * will be stored into the context1 field of the IOCB for the completion
7613  * callback function to the RPL Accept Response ELS command.
7614  *
7615  * Return code
7616  *   0 - Successfully issued ACC RPL ELS command
7617  *   1 - Failed to issue ACC RPL ELS command
7618  **/
7619 static int
lpfc_els_rsp_rpl_acc(struct lpfc_vport * vport,uint16_t cmdsize,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)7620 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
7621 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
7622 {
7623 	struct lpfc_hba *phba = vport->phba;
7624 	IOCB_t *icmd, *oldcmd;
7625 	RPL_RSP rpl_rsp;
7626 	struct lpfc_iocbq *elsiocb;
7627 	uint8_t *pcmd;
7628 
7629 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
7630 				     ndlp->nlp_DID, ELS_CMD_ACC);
7631 
7632 	if (!elsiocb)
7633 		return 1;
7634 
7635 	icmd = &elsiocb->iocb;
7636 	oldcmd = &oldiocb->iocb;
7637 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri / rx_id */
7638 	icmd->unsli3.rcvsli3.ox_id = oldcmd->unsli3.rcvsli3.ox_id;
7639 
7640 	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
7641 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
7642 	pcmd += sizeof(uint16_t);
7643 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
7644 	pcmd += sizeof(uint16_t);
7645 
7646 	/* Setup the RPL ACC payload */
7647 	rpl_rsp.listLen = be32_to_cpu(1);
7648 	rpl_rsp.index = 0;
7649 	rpl_rsp.port_num_blk.portNum = 0;
7650 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
7651 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
7652 	    sizeof(struct lpfc_name));
7653 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
7654 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
7655 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7656 			 "0120 Xmit ELS RPL ACC response tag x%x "
7657 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
7658 			 "rpi x%x\n",
7659 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
7660 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
7661 			 ndlp->nlp_rpi);
7662 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
7663 	phba->fc_stat.elsXmitACC++;
7664 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
7665 	    IOCB_ERROR) {
7666 		lpfc_els_free_iocb(phba, elsiocb);
7667 		return 1;
7668 	}
7669 	return 0;
7670 }
7671 
7672 /**
7673  * lpfc_els_rcv_rpl - Process an unsolicited rpl iocb
7674  * @vport: pointer to a host virtual N_Port data structure.
7675  * @cmdiocb: pointer to lpfc command iocb data structure.
7676  * @ndlp: pointer to a node-list data structure.
7677  *
7678  * This routine processes Read Port List (RPL) IOCB received as an ELS
7679  * unsolicited event. It first checks the remote port state. If the remote
7680  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
7681  * invokes the lpfc_els_rsp_reject() routine to send reject response.
7682  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
7683  * to accept the RPL.
7684  *
7685  * Return code
7686  *   0 - Successfully processed rpl iocb (currently always return 0)
7687  **/
7688 static int
lpfc_els_rcv_rpl(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7689 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7690 		 struct lpfc_nodelist *ndlp)
7691 {
7692 	struct lpfc_dmabuf *pcmd;
7693 	uint32_t *lp;
7694 	uint32_t maxsize;
7695 	uint16_t cmdsize;
7696 	RPL *rpl;
7697 	struct ls_rjt stat;
7698 
7699 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
7700 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
7701 		/* issue rejection response */
7702 		stat.un.b.lsRjtRsvd0 = 0;
7703 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
7704 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
7705 		stat.un.b.vendorUnique = 0;
7706 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
7707 			NULL);
7708 		/* rejected the unsolicited RPL request and done with it */
7709 		return 0;
7710 	}
7711 
7712 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7713 	lp = (uint32_t *) pcmd->virt;
7714 	rpl = (RPL *) (lp + 1);
7715 	maxsize = be32_to_cpu(rpl->maxsize);
7716 
7717 	/* We support only one port */
7718 	if ((rpl->index == 0) &&
7719 	    ((maxsize == 0) ||
7720 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
7721 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
7722 	} else {
7723 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
7724 	}
7725 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
7726 
7727 	return 0;
7728 }
7729 
7730 /**
7731  * lpfc_els_rcv_farp - Process an unsolicited farp request els command
7732  * @vport: pointer to a virtual N_Port data structure.
7733  * @cmdiocb: pointer to lpfc command iocb data structure.
7734  * @ndlp: pointer to a node-list data structure.
7735  *
7736  * This routine processes Fibre Channel Address Resolution Protocol
7737  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
7738  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
7739  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
7740  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
7741  * remote PortName is compared against the FC PortName stored in the @vport
7742  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
7743  * compared against the FC NodeName stored in the @vport data structure.
7744  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
7745  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
7746  * invoked to send out FARP Response to the remote node. Before sending the
7747  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
7748  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
7749  * routine is invoked to log into the remote port first.
7750  *
7751  * Return code
7752  *   0 - Either the FARP Match Mode not supported or successfully processed
7753  **/
7754 static int
lpfc_els_rcv_farp(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7755 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7756 		  struct lpfc_nodelist *ndlp)
7757 {
7758 	struct lpfc_dmabuf *pcmd;
7759 	uint32_t *lp;
7760 	IOCB_t *icmd;
7761 	FARP *fp;
7762 	uint32_t cnt, did;
7763 
7764 	icmd = &cmdiocb->iocb;
7765 	did = icmd->un.elsreq64.remoteID;
7766 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7767 	lp = (uint32_t *) pcmd->virt;
7768 
7769 	lp++;
7770 	fp = (FARP *) lp;
7771 	/* FARP-REQ received from DID <did> */
7772 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7773 			 "0601 FARP-REQ received from DID x%x\n", did);
7774 	/* We will only support match on WWPN or WWNN */
7775 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
7776 		return 0;
7777 	}
7778 
7779 	cnt = 0;
7780 	/* If this FARP command is searching for my portname */
7781 	if (fp->Mflags & FARP_MATCH_PORT) {
7782 		if (memcmp(&fp->RportName, &vport->fc_portname,
7783 			   sizeof(struct lpfc_name)) == 0)
7784 			cnt = 1;
7785 	}
7786 
7787 	/* If this FARP command is searching for my nodename */
7788 	if (fp->Mflags & FARP_MATCH_NODE) {
7789 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
7790 			   sizeof(struct lpfc_name)) == 0)
7791 			cnt = 1;
7792 	}
7793 
7794 	if (cnt) {
7795 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
7796 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
7797 			/* Log back into the node before sending the FARP. */
7798 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
7799 				ndlp->nlp_prev_state = ndlp->nlp_state;
7800 				lpfc_nlp_set_state(vport, ndlp,
7801 						   NLP_STE_PLOGI_ISSUE);
7802 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
7803 			}
7804 
7805 			/* Send a FARP response to that node */
7806 			if (fp->Rflags & FARP_REQUEST_FARPR)
7807 				lpfc_issue_els_farpr(vport, did, 0);
7808 		}
7809 	}
7810 	return 0;
7811 }
7812 
7813 /**
7814  * lpfc_els_rcv_farpr - Process an unsolicited farp response iocb
7815  * @vport: pointer to a host virtual N_Port data structure.
7816  * @cmdiocb: pointer to lpfc command iocb data structure.
7817  * @ndlp: pointer to a node-list data structure.
7818  *
7819  * This routine processes Fibre Channel Address Resolution Protocol
7820  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
7821  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
7822  * the FARP response request.
7823  *
7824  * Return code
7825  *   0 - Successfully processed FARPR IOCB (currently always return 0)
7826  **/
7827 static int
lpfc_els_rcv_farpr(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)7828 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7829 		   struct lpfc_nodelist  *ndlp)
7830 {
7831 	struct lpfc_dmabuf *pcmd;
7832 	uint32_t *lp;
7833 	IOCB_t *icmd;
7834 	uint32_t did;
7835 
7836 	icmd = &cmdiocb->iocb;
7837 	did = icmd->un.elsreq64.remoteID;
7838 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
7839 	lp = (uint32_t *) pcmd->virt;
7840 
7841 	lp++;
7842 	/* FARP-RSP received from DID <did> */
7843 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7844 			 "0600 FARP-RSP received from DID x%x\n", did);
7845 	/* ACCEPT the Farp resp request */
7846 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
7847 
7848 	return 0;
7849 }
7850 
7851 /**
7852  * lpfc_els_rcv_fan - Process an unsolicited fan iocb command
7853  * @vport: pointer to a host virtual N_Port data structure.
7854  * @cmdiocb: pointer to lpfc command iocb data structure.
7855  * @fan_ndlp: pointer to a node-list data structure.
7856  *
7857  * This routine processes a Fabric Address Notification (FAN) IOCB
7858  * command received as an ELS unsolicited event. The FAN ELS command will
7859  * only be processed on a physical port (i.e., the @vport represents the
7860  * physical port). The fabric NodeName and PortName from the FAN IOCB are
7861  * compared against those in the phba data structure. If any of those is
7862  * different, the lpfc_initial_flogi() routine is invoked to initialize
7863  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
7864  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
7865  * is invoked to register login to the fabric.
7866  *
7867  * Return code
7868  *   0 - Successfully processed fan iocb (currently always return 0).
7869  **/
7870 static int
lpfc_els_rcv_fan(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * fan_ndlp)7871 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
7872 		 struct lpfc_nodelist *fan_ndlp)
7873 {
7874 	struct lpfc_hba *phba = vport->phba;
7875 	uint32_t *lp;
7876 	FAN *fp;
7877 
7878 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
7879 	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
7880 	fp = (FAN *) ++lp;
7881 	/* FAN received; Fan does not have a reply sequence */
7882 	if ((vport == phba->pport) &&
7883 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
7884 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
7885 			    sizeof(struct lpfc_name))) ||
7886 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
7887 			    sizeof(struct lpfc_name)))) {
7888 			/* This port has switched fabrics. FLOGI is required */
7889 			lpfc_issue_init_vfi(vport);
7890 		} else {
7891 			/* FAN verified - skip FLOGI */
7892 			vport->fc_myDID = vport->fc_prevDID;
7893 			if (phba->sli_rev < LPFC_SLI_REV4)
7894 				lpfc_issue_fabric_reglogin(vport);
7895 			else {
7896 				lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
7897 					"3138 Need register VFI: (x%x/%x)\n",
7898 					vport->fc_prevDID, vport->fc_myDID);
7899 				lpfc_issue_reg_vfi(vport);
7900 			}
7901 		}
7902 	}
7903 	return 0;
7904 }
7905 
7906 /**
7907  * lpfc_els_timeout - Handler funciton to the els timer
7908  * @t: timer context used to obtain the vport.
7909  *
7910  * This routine is invoked by the ELS timer after timeout. It posts the ELS
7911  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
7912  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
7913  * up the worker thread. It is for the worker thread to invoke the routine
7914  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
7915  **/
7916 void
lpfc_els_timeout(struct timer_list * t)7917 lpfc_els_timeout(struct timer_list *t)
7918 {
7919 	struct lpfc_vport *vport = from_timer(vport, t, els_tmofunc);
7920 	struct lpfc_hba   *phba = vport->phba;
7921 	uint32_t tmo_posted;
7922 	unsigned long iflag;
7923 
7924 	spin_lock_irqsave(&vport->work_port_lock, iflag);
7925 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
7926 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7927 		vport->work_port_events |= WORKER_ELS_TMO;
7928 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
7929 
7930 	if ((!tmo_posted) && (!(vport->load_flag & FC_UNLOADING)))
7931 		lpfc_worker_wake_up(phba);
7932 	return;
7933 }
7934 
7935 
7936 /**
7937  * lpfc_els_timeout_handler - Process an els timeout event
7938  * @vport: pointer to a virtual N_Port data structure.
7939  *
7940  * This routine is the actual handler function that processes an ELS timeout
7941  * event. It walks the ELS ring to get and abort all the IOCBs (except the
7942  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
7943  * invoking the lpfc_sli_issue_abort_iotag() routine.
7944  **/
7945 void
lpfc_els_timeout_handler(struct lpfc_vport * vport)7946 lpfc_els_timeout_handler(struct lpfc_vport *vport)
7947 {
7948 	struct lpfc_hba  *phba = vport->phba;
7949 	struct lpfc_sli_ring *pring;
7950 	struct lpfc_iocbq *tmp_iocb, *piocb;
7951 	IOCB_t *cmd = NULL;
7952 	struct lpfc_dmabuf *pcmd;
7953 	uint32_t els_command = 0;
7954 	uint32_t timeout;
7955 	uint32_t remote_ID = 0xffffffff;
7956 	LIST_HEAD(abort_list);
7957 
7958 
7959 	timeout = (uint32_t)(phba->fc_ratov << 1);
7960 
7961 	pring = lpfc_phba_elsring(phba);
7962 	if (unlikely(!pring))
7963 		return;
7964 
7965 	if (phba->pport->load_flag & FC_UNLOADING)
7966 		return;
7967 
7968 	spin_lock_irq(&phba->hbalock);
7969 	if (phba->sli_rev == LPFC_SLI_REV4)
7970 		spin_lock(&pring->ring_lock);
7971 
7972 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
7973 		cmd = &piocb->iocb;
7974 
7975 		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
7976 		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
7977 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
7978 			continue;
7979 
7980 		if (piocb->vport != vport)
7981 			continue;
7982 
7983 		pcmd = (struct lpfc_dmabuf *) piocb->context2;
7984 		if (pcmd)
7985 			els_command = *(uint32_t *) (pcmd->virt);
7986 
7987 		if (els_command == ELS_CMD_FARP ||
7988 		    els_command == ELS_CMD_FARPR ||
7989 		    els_command == ELS_CMD_FDISC)
7990 			continue;
7991 
7992 		if (piocb->drvrTimeout > 0) {
7993 			if (piocb->drvrTimeout >= timeout)
7994 				piocb->drvrTimeout -= timeout;
7995 			else
7996 				piocb->drvrTimeout = 0;
7997 			continue;
7998 		}
7999 
8000 		remote_ID = 0xffffffff;
8001 		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
8002 			remote_ID = cmd->un.elsreq64.remoteID;
8003 		else {
8004 			struct lpfc_nodelist *ndlp;
8005 			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
8006 			if (ndlp && NLP_CHK_NODE_ACT(ndlp))
8007 				remote_ID = ndlp->nlp_DID;
8008 		}
8009 		list_add_tail(&piocb->dlist, &abort_list);
8010 	}
8011 	if (phba->sli_rev == LPFC_SLI_REV4)
8012 		spin_unlock(&pring->ring_lock);
8013 	spin_unlock_irq(&phba->hbalock);
8014 
8015 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
8016 		cmd = &piocb->iocb;
8017 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8018 			 "0127 ELS timeout Data: x%x x%x x%x "
8019 			 "x%x\n", els_command,
8020 			 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
8021 		spin_lock_irq(&phba->hbalock);
8022 		list_del_init(&piocb->dlist);
8023 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
8024 		spin_unlock_irq(&phba->hbalock);
8025 	}
8026 
8027 	if (!list_empty(&pring->txcmplq))
8028 		if (!(phba->pport->load_flag & FC_UNLOADING))
8029 			mod_timer(&vport->els_tmofunc,
8030 				  jiffies + msecs_to_jiffies(1000 * timeout));
8031 }
8032 
8033 /**
8034  * lpfc_els_flush_cmd - Clean up the outstanding els commands to a vport
8035  * @vport: pointer to a host virtual N_Port data structure.
8036  *
8037  * This routine is used to clean up all the outstanding ELS commands on a
8038  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
8039  * routine. After that, it walks the ELS transmit queue to remove all the
8040  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
8041  * the IOCBs with a non-NULL completion callback function, the callback
8042  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
8043  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
8044  * callback function, the IOCB will simply be released. Finally, it walks
8045  * the ELS transmit completion queue to issue an abort IOCB to any transmit
8046  * completion queue IOCB that is associated with the @vport and is not
8047  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
8048  * part of the discovery state machine) out to HBA by invoking the
8049  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
8050  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
8051  * the IOCBs are aborted when this function returns.
8052  **/
8053 void
lpfc_els_flush_cmd(struct lpfc_vport * vport)8054 lpfc_els_flush_cmd(struct lpfc_vport *vport)
8055 {
8056 	LIST_HEAD(abort_list);
8057 	struct lpfc_hba  *phba = vport->phba;
8058 	struct lpfc_sli_ring *pring;
8059 	struct lpfc_iocbq *tmp_iocb, *piocb;
8060 	IOCB_t *cmd = NULL;
8061 	unsigned long iflags = 0;
8062 
8063 	lpfc_fabric_abort_vport(vport);
8064 
8065 	/*
8066 	 * For SLI3, only the hbalock is required.  But SLI4 needs to coordinate
8067 	 * with the ring insert operation.  Because lpfc_sli_issue_abort_iotag
8068 	 * ultimately grabs the ring_lock, the driver must splice the list into
8069 	 * a working list and release the locks before calling the abort.
8070 	 */
8071 	spin_lock_irqsave(&phba->hbalock, iflags);
8072 	pring = lpfc_phba_elsring(phba);
8073 
8074 	/* Bail out if we've no ELS wq, like in PCI error recovery case. */
8075 	if (unlikely(!pring)) {
8076 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8077 		return;
8078 	}
8079 
8080 	if (phba->sli_rev == LPFC_SLI_REV4)
8081 		spin_lock(&pring->ring_lock);
8082 
8083 	/* First we need to issue aborts to outstanding cmds on txcmpl */
8084 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
8085 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
8086 			continue;
8087 
8088 		if (piocb->vport != vport)
8089 			continue;
8090 
8091 		if (piocb->iocb_flag & LPFC_DRIVER_ABORTED)
8092 			continue;
8093 
8094 		/* On the ELS ring we can have ELS_REQUESTs or
8095 		 * GEN_REQUESTs waiting for a response.
8096 		 */
8097 		cmd = &piocb->iocb;
8098 		if (cmd->ulpCommand == CMD_ELS_REQUEST64_CR) {
8099 			list_add_tail(&piocb->dlist, &abort_list);
8100 
8101 			/* If the link is down when flushing ELS commands
8102 			 * the firmware will not complete them till after
8103 			 * the link comes back up. This may confuse
8104 			 * discovery for the new link up, so we need to
8105 			 * change the compl routine to just clean up the iocb
8106 			 * and avoid any retry logic.
8107 			 */
8108 			if (phba->link_state == LPFC_LINK_DOWN)
8109 				piocb->iocb_cmpl = lpfc_cmpl_els_link_down;
8110 		}
8111 		if (cmd->ulpCommand == CMD_GEN_REQUEST64_CR)
8112 			list_add_tail(&piocb->dlist, &abort_list);
8113 	}
8114 
8115 	if (phba->sli_rev == LPFC_SLI_REV4)
8116 		spin_unlock(&pring->ring_lock);
8117 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8118 
8119 	/* Abort each txcmpl iocb on aborted list and remove the dlist links. */
8120 	list_for_each_entry_safe(piocb, tmp_iocb, &abort_list, dlist) {
8121 		spin_lock_irqsave(&phba->hbalock, iflags);
8122 		list_del_init(&piocb->dlist);
8123 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
8124 		spin_unlock_irqrestore(&phba->hbalock, iflags);
8125 	}
8126 	if (!list_empty(&abort_list))
8127 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8128 				 "3387 abort list for txq not empty\n");
8129 	INIT_LIST_HEAD(&abort_list);
8130 
8131 	spin_lock_irqsave(&phba->hbalock, iflags);
8132 	if (phba->sli_rev == LPFC_SLI_REV4)
8133 		spin_lock(&pring->ring_lock);
8134 
8135 	/* No need to abort the txq list,
8136 	 * just queue them up for lpfc_sli_cancel_iocbs
8137 	 */
8138 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
8139 		cmd = &piocb->iocb;
8140 
8141 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
8142 			continue;
8143 		}
8144 
8145 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
8146 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
8147 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
8148 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8149 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
8150 			continue;
8151 
8152 		if (piocb->vport != vport)
8153 			continue;
8154 
8155 		list_del_init(&piocb->list);
8156 		list_add_tail(&piocb->list, &abort_list);
8157 	}
8158 
8159 	/* The same holds true for any FLOGI/FDISC on the fabric_iocb_list */
8160 	if (vport == phba->pport) {
8161 		list_for_each_entry_safe(piocb, tmp_iocb,
8162 					 &phba->fabric_iocb_list, list) {
8163 			cmd = &piocb->iocb;
8164 			list_del_init(&piocb->list);
8165 			list_add_tail(&piocb->list, &abort_list);
8166 		}
8167 	}
8168 
8169 	if (phba->sli_rev == LPFC_SLI_REV4)
8170 		spin_unlock(&pring->ring_lock);
8171 	spin_unlock_irqrestore(&phba->hbalock, iflags);
8172 
8173 	/* Cancel all the IOCBs from the completions list */
8174 	lpfc_sli_cancel_iocbs(phba, &abort_list,
8175 			      IOSTAT_LOCAL_REJECT, IOERR_SLI_ABORTED);
8176 
8177 	return;
8178 }
8179 
8180 /**
8181  * lpfc_els_flush_all_cmd - Clean up all the outstanding els commands to a HBA
8182  * @phba: pointer to lpfc hba data structure.
8183  *
8184  * This routine is used to clean up all the outstanding ELS commands on a
8185  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
8186  * routine. After that, it walks the ELS transmit queue to remove all the
8187  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
8188  * the IOCBs with the completion callback function associated, the callback
8189  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
8190  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
8191  * callback function associated, the IOCB will simply be released. Finally,
8192  * it walks the ELS transmit completion queue to issue an abort IOCB to any
8193  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
8194  * management plane IOCBs that are not part of the discovery state machine)
8195  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
8196  **/
8197 void
lpfc_els_flush_all_cmd(struct lpfc_hba * phba)8198 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
8199 {
8200 	struct lpfc_vport *vport;
8201 
8202 	spin_lock_irq(&phba->port_list_lock);
8203 	list_for_each_entry(vport, &phba->port_list, listentry)
8204 		lpfc_els_flush_cmd(vport);
8205 	spin_unlock_irq(&phba->port_list_lock);
8206 
8207 	return;
8208 }
8209 
8210 /**
8211  * lpfc_send_els_failure_event - Posts an ELS command failure event
8212  * @phba: Pointer to hba context object.
8213  * @cmdiocbp: Pointer to command iocb which reported error.
8214  * @rspiocbp: Pointer to response iocb which reported error.
8215  *
8216  * This function sends an event when there is an ELS command
8217  * failure.
8218  **/
8219 void
lpfc_send_els_failure_event(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbp,struct lpfc_iocbq * rspiocbp)8220 lpfc_send_els_failure_event(struct lpfc_hba *phba,
8221 			struct lpfc_iocbq *cmdiocbp,
8222 			struct lpfc_iocbq *rspiocbp)
8223 {
8224 	struct lpfc_vport *vport = cmdiocbp->vport;
8225 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8226 	struct lpfc_lsrjt_event lsrjt_event;
8227 	struct lpfc_fabric_event_header fabric_event;
8228 	struct ls_rjt stat;
8229 	struct lpfc_nodelist *ndlp;
8230 	uint32_t *pcmd;
8231 
8232 	ndlp = cmdiocbp->context1;
8233 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8234 		return;
8235 
8236 	if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
8237 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
8238 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
8239 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
8240 			sizeof(struct lpfc_name));
8241 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
8242 			sizeof(struct lpfc_name));
8243 		pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8244 			cmdiocbp->context2)->virt);
8245 		lsrjt_event.command = (pcmd != NULL) ? *pcmd : 0;
8246 		stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
8247 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
8248 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
8249 		fc_host_post_vendor_event(shost,
8250 			fc_get_event_number(),
8251 			sizeof(lsrjt_event),
8252 			(char *)&lsrjt_event,
8253 			LPFC_NL_VENDOR_ID);
8254 		return;
8255 	}
8256 	if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
8257 		(rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
8258 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
8259 		if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
8260 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
8261 		else
8262 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
8263 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
8264 			sizeof(struct lpfc_name));
8265 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
8266 			sizeof(struct lpfc_name));
8267 		fc_host_post_vendor_event(shost,
8268 			fc_get_event_number(),
8269 			sizeof(fabric_event),
8270 			(char *)&fabric_event,
8271 			LPFC_NL_VENDOR_ID);
8272 		return;
8273 	}
8274 
8275 }
8276 
8277 /**
8278  * lpfc_send_els_event - Posts unsolicited els event
8279  * @vport: Pointer to vport object.
8280  * @ndlp: Pointer FC node object.
8281  * @payload: ELS command code type.
8282  *
8283  * This function posts an event when there is an incoming
8284  * unsolicited ELS command.
8285  **/
8286 static void
lpfc_send_els_event(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint32_t * payload)8287 lpfc_send_els_event(struct lpfc_vport *vport,
8288 		    struct lpfc_nodelist *ndlp,
8289 		    uint32_t *payload)
8290 {
8291 	struct lpfc_els_event_header *els_data = NULL;
8292 	struct lpfc_logo_event *logo_data = NULL;
8293 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
8294 
8295 	if (*payload == ELS_CMD_LOGO) {
8296 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
8297 		if (!logo_data) {
8298 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8299 				"0148 Failed to allocate memory "
8300 				"for LOGO event\n");
8301 			return;
8302 		}
8303 		els_data = &logo_data->header;
8304 	} else {
8305 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
8306 			GFP_KERNEL);
8307 		if (!els_data) {
8308 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8309 				"0149 Failed to allocate memory "
8310 				"for ELS event\n");
8311 			return;
8312 		}
8313 	}
8314 	els_data->event_type = FC_REG_ELS_EVENT;
8315 	switch (*payload) {
8316 	case ELS_CMD_PLOGI:
8317 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
8318 		break;
8319 	case ELS_CMD_PRLO:
8320 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
8321 		break;
8322 	case ELS_CMD_ADISC:
8323 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
8324 		break;
8325 	case ELS_CMD_LOGO:
8326 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
8327 		/* Copy the WWPN in the LOGO payload */
8328 		memcpy(logo_data->logo_wwpn, &payload[2],
8329 			sizeof(struct lpfc_name));
8330 		break;
8331 	default:
8332 		kfree(els_data);
8333 		return;
8334 	}
8335 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
8336 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
8337 	if (*payload == ELS_CMD_LOGO) {
8338 		fc_host_post_vendor_event(shost,
8339 			fc_get_event_number(),
8340 			sizeof(struct lpfc_logo_event),
8341 			(char *)logo_data,
8342 			LPFC_NL_VENDOR_ID);
8343 		kfree(logo_data);
8344 	} else {
8345 		fc_host_post_vendor_event(shost,
8346 			fc_get_event_number(),
8347 			sizeof(struct lpfc_els_event_header),
8348 			(char *)els_data,
8349 			LPFC_NL_VENDOR_ID);
8350 		kfree(els_data);
8351 	}
8352 
8353 	return;
8354 }
8355 
8356 
8357 DECLARE_ENUM2STR_LOOKUP(lpfc_get_tlv_dtag_nm, fc_ls_tlv_dtag,
8358 			FC_LS_TLV_DTAG_INIT);
8359 
8360 DECLARE_ENUM2STR_LOOKUP(lpfc_get_fpin_li_event_nm, fc_fpin_li_event_types,
8361 			FC_FPIN_LI_EVT_TYPES_INIT);
8362 
8363 /**
8364  * lpfc_els_rcv_fpin_li - Process an FPIN Link Integrity Event.
8365  * @vport: Pointer to vport object.
8366  * @tlv:  Pointer to the Link Integrity Notification Descriptor.
8367  *
8368  * This function processes a link integrity FPIN event by
8369  * logging a message
8370  **/
8371 static void
lpfc_els_rcv_fpin_li(struct lpfc_vport * vport,struct fc_tlv_desc * tlv)8372 lpfc_els_rcv_fpin_li(struct lpfc_vport *vport, struct fc_tlv_desc *tlv)
8373 {
8374 	struct fc_fn_li_desc *li = (struct fc_fn_li_desc *)tlv;
8375 	const char *li_evt_str;
8376 	u32 li_evt;
8377 
8378 	li_evt = be16_to_cpu(li->event_type);
8379 	li_evt_str = lpfc_get_fpin_li_event_nm(li_evt);
8380 
8381 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8382 			 "4680 FPIN Link Integrity %s (x%x) "
8383 			 "Detecting PN x%016llx Attached PN x%016llx "
8384 			 "Duration %d mSecs Count %d Port Cnt %d\n",
8385 			 li_evt_str, li_evt,
8386 			 be64_to_cpu(li->detecting_wwpn),
8387 			 be64_to_cpu(li->attached_wwpn),
8388 			 be32_to_cpu(li->event_threshold),
8389 			 be32_to_cpu(li->event_count),
8390 			 be32_to_cpu(li->pname_count));
8391 }
8392 
8393 static void
lpfc_els_rcv_fpin(struct lpfc_vport * vport,struct fc_els_fpin * fpin,u32 fpin_length)8394 lpfc_els_rcv_fpin(struct lpfc_vport *vport, struct fc_els_fpin *fpin,
8395 		  u32 fpin_length)
8396 {
8397 	struct fc_tlv_desc *tlv;
8398 	const char *dtag_nm;
8399 	uint32_t desc_cnt = 0, bytes_remain;
8400 	u32 dtag;
8401 
8402 	/* FPINs handled only if we are in the right discovery state */
8403 	if (vport->port_state < LPFC_DISC_AUTH)
8404 		return;
8405 
8406 	/* make sure there is the full fpin header */
8407 	if (fpin_length < sizeof(struct fc_els_fpin))
8408 		return;
8409 
8410 	tlv = (struct fc_tlv_desc *)&fpin->fpin_desc[0];
8411 	bytes_remain = fpin_length - offsetof(struct fc_els_fpin, fpin_desc);
8412 	bytes_remain = min_t(u32, bytes_remain, be32_to_cpu(fpin->desc_len));
8413 
8414 	/* process each descriptor */
8415 	while (bytes_remain >= FC_TLV_DESC_HDR_SZ &&
8416 	       bytes_remain >= FC_TLV_DESC_SZ_FROM_LENGTH(tlv)) {
8417 
8418 		dtag = be32_to_cpu(tlv->desc_tag);
8419 		switch (dtag) {
8420 		case ELS_DTAG_LNK_INTEGRITY:
8421 			lpfc_els_rcv_fpin_li(vport, tlv);
8422 			break;
8423 		default:
8424 			dtag_nm = lpfc_get_tlv_dtag_nm(dtag);
8425 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8426 					 "4678  skipped FPIN descriptor[%d]: "
8427 					 "tag x%x (%s)\n",
8428 					 desc_cnt, dtag, dtag_nm);
8429 			break;
8430 		}
8431 
8432 		desc_cnt++;
8433 		bytes_remain -= FC_TLV_DESC_SZ_FROM_LENGTH(tlv);
8434 		tlv = fc_tlv_next_desc(tlv);
8435 	}
8436 
8437 	fc_host_fpin_rcv(lpfc_shost_from_vport(vport), fpin_length,
8438 			 (char *)fpin);
8439 }
8440 
8441 /**
8442  * lpfc_els_unsol_buffer - Process an unsolicited event data buffer
8443  * @phba: pointer to lpfc hba data structure.
8444  * @pring: pointer to a SLI ring.
8445  * @vport: pointer to a host virtual N_Port data structure.
8446  * @elsiocb: pointer to lpfc els command iocb data structure.
8447  *
8448  * This routine is used for processing the IOCB associated with a unsolicited
8449  * event. It first determines whether there is an existing ndlp that matches
8450  * the DID from the unsolicited IOCB. If not, it will create a new one with
8451  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
8452  * IOCB is then used to invoke the proper routine and to set up proper state
8453  * of the discovery state machine.
8454  **/
8455 static void
lpfc_els_unsol_buffer(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_vport * vport,struct lpfc_iocbq * elsiocb)8456 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8457 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
8458 {
8459 	struct Scsi_Host  *shost;
8460 	struct lpfc_nodelist *ndlp;
8461 	struct ls_rjt stat;
8462 	uint32_t *payload, payload_len;
8463 	uint32_t cmd, did, newnode;
8464 	uint8_t rjt_exp, rjt_err = 0, init_link = 0;
8465 	IOCB_t *icmd = &elsiocb->iocb;
8466 	LPFC_MBOXQ_t *mbox;
8467 
8468 	if (!vport || !(elsiocb->context2))
8469 		goto dropit;
8470 
8471 	newnode = 0;
8472 	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
8473 	payload_len = elsiocb->iocb.unsli3.rcvsli3.acc_len;
8474 	cmd = *payload;
8475 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
8476 		lpfc_post_buffer(phba, pring, 1);
8477 
8478 	did = icmd->un.rcvels.remoteID;
8479 	if (icmd->ulpStatus) {
8480 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8481 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
8482 			icmd->ulpStatus, icmd->un.ulpWord[4], did);
8483 		goto dropit;
8484 	}
8485 
8486 	/* Check to see if link went down during discovery */
8487 	if (lpfc_els_chk_latt(vport))
8488 		goto dropit;
8489 
8490 	/* Ignore traffic received during vport shutdown. */
8491 	if (vport->load_flag & FC_UNLOADING)
8492 		goto dropit;
8493 
8494 	/* If NPort discovery is delayed drop incoming ELS */
8495 	if ((vport->fc_flag & FC_DISC_DELAYED) &&
8496 			(cmd != ELS_CMD_PLOGI))
8497 		goto dropit;
8498 
8499 	ndlp = lpfc_findnode_did(vport, did);
8500 	if (!ndlp) {
8501 		/* Cannot find existing Fabric ndlp, so allocate a new one */
8502 		ndlp = lpfc_nlp_init(vport, did);
8503 		if (!ndlp)
8504 			goto dropit;
8505 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8506 		newnode = 1;
8507 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8508 			ndlp->nlp_type |= NLP_FABRIC;
8509 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
8510 		ndlp = lpfc_enable_node(vport, ndlp,
8511 					NLP_STE_UNUSED_NODE);
8512 		if (!ndlp)
8513 			goto dropit;
8514 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8515 		newnode = 1;
8516 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
8517 			ndlp->nlp_type |= NLP_FABRIC;
8518 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
8519 		/* This is similar to the new node path */
8520 		ndlp = lpfc_nlp_get(ndlp);
8521 		if (!ndlp)
8522 			goto dropit;
8523 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
8524 		newnode = 1;
8525 	}
8526 
8527 	phba->fc_stat.elsRcvFrame++;
8528 
8529 	/*
8530 	 * Do not process any unsolicited ELS commands
8531 	 * if the ndlp is in DEV_LOSS
8532 	 */
8533 	shost = lpfc_shost_from_vport(vport);
8534 	spin_lock_irq(shost->host_lock);
8535 	if (ndlp->nlp_flag & NLP_IN_DEV_LOSS) {
8536 		spin_unlock_irq(shost->host_lock);
8537 		if (newnode)
8538 			lpfc_nlp_put(ndlp);
8539 		goto dropit;
8540 	}
8541 	spin_unlock_irq(shost->host_lock);
8542 
8543 	elsiocb->context1 = lpfc_nlp_get(ndlp);
8544 	elsiocb->vport = vport;
8545 
8546 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
8547 		cmd &= ELS_CMD_MASK;
8548 	}
8549 	/* ELS command <elsCmd> received from NPORT <did> */
8550 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8551 			 "0112 ELS command x%x received from NPORT x%x "
8552 			 "Data: x%x x%x x%x x%x\n",
8553 			cmd, did, vport->port_state, vport->fc_flag,
8554 			vport->fc_myDID, vport->fc_prevDID);
8555 
8556 	/* reject till our FLOGI completes or PLOGI assigned DID via PT2PT */
8557 	if ((vport->port_state < LPFC_FABRIC_CFG_LINK) &&
8558 	    (cmd != ELS_CMD_FLOGI) &&
8559 	    !((cmd == ELS_CMD_PLOGI) && (vport->fc_flag & FC_PT2PT))) {
8560 		rjt_err = LSRJT_LOGICAL_BSY;
8561 		rjt_exp = LSEXP_NOTHING_MORE;
8562 		goto lsrjt;
8563 	}
8564 
8565 	switch (cmd) {
8566 	case ELS_CMD_PLOGI:
8567 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8568 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
8569 			did, vport->port_state, ndlp->nlp_flag);
8570 
8571 		phba->fc_stat.elsRcvPLOGI++;
8572 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
8573 		if (phba->sli_rev == LPFC_SLI_REV4 &&
8574 		    (phba->pport->fc_flag & FC_PT2PT)) {
8575 			vport->fc_prevDID = vport->fc_myDID;
8576 			/* Our DID needs to be updated before registering
8577 			 * the vfi. This is done in lpfc_rcv_plogi but
8578 			 * that is called after the reg_vfi.
8579 			 */
8580 			vport->fc_myDID = elsiocb->iocb.un.rcvels.parmRo;
8581 			lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
8582 					 "3312 Remote port assigned DID x%x "
8583 					 "%x\n", vport->fc_myDID,
8584 					 vport->fc_prevDID);
8585 		}
8586 
8587 		lpfc_send_els_event(vport, ndlp, payload);
8588 
8589 		/* If Nport discovery is delayed, reject PLOGIs */
8590 		if (vport->fc_flag & FC_DISC_DELAYED) {
8591 			rjt_err = LSRJT_UNABLE_TPC;
8592 			rjt_exp = LSEXP_NOTHING_MORE;
8593 			break;
8594 		}
8595 
8596 		if (vport->port_state < LPFC_DISC_AUTH) {
8597 			if (!(phba->pport->fc_flag & FC_PT2PT) ||
8598 				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
8599 				rjt_err = LSRJT_UNABLE_TPC;
8600 				rjt_exp = LSEXP_NOTHING_MORE;
8601 				break;
8602 			}
8603 		}
8604 
8605 		spin_lock_irq(shost->host_lock);
8606 		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
8607 		spin_unlock_irq(shost->host_lock);
8608 
8609 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
8610 					NLP_EVT_RCV_PLOGI);
8611 
8612 		break;
8613 	case ELS_CMD_FLOGI:
8614 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8615 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
8616 			did, vport->port_state, ndlp->nlp_flag);
8617 
8618 		phba->fc_stat.elsRcvFLOGI++;
8619 
8620 		/* If the driver believes fabric discovery is done and is ready,
8621 		 * bounce the link.  There is some descrepancy.
8622 		 */
8623 		if (vport->port_state >= LPFC_LOCAL_CFG_LINK &&
8624 		    vport->fc_flag & FC_PT2PT &&
8625 		    vport->rcv_flogi_cnt >= 1) {
8626 			rjt_err = LSRJT_LOGICAL_BSY;
8627 			rjt_exp = LSEXP_NOTHING_MORE;
8628 			init_link++;
8629 			goto lsrjt;
8630 		}
8631 
8632 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
8633 		if (newnode)
8634 			lpfc_nlp_put(ndlp);
8635 		break;
8636 	case ELS_CMD_LOGO:
8637 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8638 			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
8639 			did, vport->port_state, ndlp->nlp_flag);
8640 
8641 		phba->fc_stat.elsRcvLOGO++;
8642 		lpfc_send_els_event(vport, ndlp, payload);
8643 		if (vport->port_state < LPFC_DISC_AUTH) {
8644 			rjt_err = LSRJT_UNABLE_TPC;
8645 			rjt_exp = LSEXP_NOTHING_MORE;
8646 			break;
8647 		}
8648 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
8649 		break;
8650 	case ELS_CMD_PRLO:
8651 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8652 			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
8653 			did, vport->port_state, ndlp->nlp_flag);
8654 
8655 		phba->fc_stat.elsRcvPRLO++;
8656 		lpfc_send_els_event(vport, ndlp, payload);
8657 		if (vport->port_state < LPFC_DISC_AUTH) {
8658 			rjt_err = LSRJT_UNABLE_TPC;
8659 			rjt_exp = LSEXP_NOTHING_MORE;
8660 			break;
8661 		}
8662 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
8663 		break;
8664 	case ELS_CMD_LCB:
8665 		phba->fc_stat.elsRcvLCB++;
8666 		lpfc_els_rcv_lcb(vport, elsiocb, ndlp);
8667 		break;
8668 	case ELS_CMD_RDP:
8669 		phba->fc_stat.elsRcvRDP++;
8670 		lpfc_els_rcv_rdp(vport, elsiocb, ndlp);
8671 		break;
8672 	case ELS_CMD_RSCN:
8673 		phba->fc_stat.elsRcvRSCN++;
8674 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
8675 		if (newnode)
8676 			lpfc_nlp_put(ndlp);
8677 		break;
8678 	case ELS_CMD_ADISC:
8679 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8680 			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
8681 			did, vport->port_state, ndlp->nlp_flag);
8682 
8683 		lpfc_send_els_event(vport, ndlp, payload);
8684 		phba->fc_stat.elsRcvADISC++;
8685 		if (vport->port_state < LPFC_DISC_AUTH) {
8686 			rjt_err = LSRJT_UNABLE_TPC;
8687 			rjt_exp = LSEXP_NOTHING_MORE;
8688 			break;
8689 		}
8690 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
8691 					NLP_EVT_RCV_ADISC);
8692 		break;
8693 	case ELS_CMD_PDISC:
8694 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8695 			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
8696 			did, vport->port_state, ndlp->nlp_flag);
8697 
8698 		phba->fc_stat.elsRcvPDISC++;
8699 		if (vport->port_state < LPFC_DISC_AUTH) {
8700 			rjt_err = LSRJT_UNABLE_TPC;
8701 			rjt_exp = LSEXP_NOTHING_MORE;
8702 			break;
8703 		}
8704 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
8705 					NLP_EVT_RCV_PDISC);
8706 		break;
8707 	case ELS_CMD_FARPR:
8708 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8709 			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
8710 			did, vport->port_state, ndlp->nlp_flag);
8711 
8712 		phba->fc_stat.elsRcvFARPR++;
8713 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
8714 		break;
8715 	case ELS_CMD_FARP:
8716 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8717 			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
8718 			did, vport->port_state, ndlp->nlp_flag);
8719 
8720 		phba->fc_stat.elsRcvFARP++;
8721 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
8722 		break;
8723 	case ELS_CMD_FAN:
8724 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8725 			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
8726 			did, vport->port_state, ndlp->nlp_flag);
8727 
8728 		phba->fc_stat.elsRcvFAN++;
8729 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
8730 		break;
8731 	case ELS_CMD_PRLI:
8732 	case ELS_CMD_NVMEPRLI:
8733 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8734 			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
8735 			did, vport->port_state, ndlp->nlp_flag);
8736 
8737 		phba->fc_stat.elsRcvPRLI++;
8738 		if ((vport->port_state < LPFC_DISC_AUTH) &&
8739 		    (vport->fc_flag & FC_FABRIC)) {
8740 			rjt_err = LSRJT_UNABLE_TPC;
8741 			rjt_exp = LSEXP_NOTHING_MORE;
8742 			break;
8743 		}
8744 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
8745 		break;
8746 	case ELS_CMD_LIRR:
8747 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8748 			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
8749 			did, vport->port_state, ndlp->nlp_flag);
8750 
8751 		phba->fc_stat.elsRcvLIRR++;
8752 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
8753 		if (newnode)
8754 			lpfc_nlp_put(ndlp);
8755 		break;
8756 	case ELS_CMD_RLS:
8757 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8758 			"RCV RLS:         did:x%x/ste:x%x flg:x%x",
8759 			did, vport->port_state, ndlp->nlp_flag);
8760 
8761 		phba->fc_stat.elsRcvRLS++;
8762 		lpfc_els_rcv_rls(vport, elsiocb, ndlp);
8763 		if (newnode)
8764 			lpfc_nlp_put(ndlp);
8765 		break;
8766 	case ELS_CMD_RPL:
8767 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8768 			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
8769 			did, vport->port_state, ndlp->nlp_flag);
8770 
8771 		phba->fc_stat.elsRcvRPL++;
8772 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
8773 		if (newnode)
8774 			lpfc_nlp_put(ndlp);
8775 		break;
8776 	case ELS_CMD_RNID:
8777 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8778 			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
8779 			did, vport->port_state, ndlp->nlp_flag);
8780 
8781 		phba->fc_stat.elsRcvRNID++;
8782 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
8783 		if (newnode)
8784 			lpfc_nlp_put(ndlp);
8785 		break;
8786 	case ELS_CMD_RTV:
8787 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8788 			"RCV RTV:        did:x%x/ste:x%x flg:x%x",
8789 			did, vport->port_state, ndlp->nlp_flag);
8790 		phba->fc_stat.elsRcvRTV++;
8791 		lpfc_els_rcv_rtv(vport, elsiocb, ndlp);
8792 		if (newnode)
8793 			lpfc_nlp_put(ndlp);
8794 		break;
8795 	case ELS_CMD_RRQ:
8796 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8797 			"RCV RRQ:         did:x%x/ste:x%x flg:x%x",
8798 			did, vport->port_state, ndlp->nlp_flag);
8799 
8800 		phba->fc_stat.elsRcvRRQ++;
8801 		lpfc_els_rcv_rrq(vport, elsiocb, ndlp);
8802 		if (newnode)
8803 			lpfc_nlp_put(ndlp);
8804 		break;
8805 	case ELS_CMD_ECHO:
8806 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8807 			"RCV ECHO:        did:x%x/ste:x%x flg:x%x",
8808 			did, vport->port_state, ndlp->nlp_flag);
8809 
8810 		phba->fc_stat.elsRcvECHO++;
8811 		lpfc_els_rcv_echo(vport, elsiocb, ndlp);
8812 		if (newnode)
8813 			lpfc_nlp_put(ndlp);
8814 		break;
8815 	case ELS_CMD_REC:
8816 		/* receive this due to exchange closed */
8817 		rjt_err = LSRJT_UNABLE_TPC;
8818 		rjt_exp = LSEXP_INVALID_OX_RX;
8819 		break;
8820 	case ELS_CMD_FPIN:
8821 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8822 				      "RCV FPIN:       did:x%x/ste:x%x flg:x%x",
8823 				      did, vport->port_state, ndlp->nlp_flag);
8824 
8825 		lpfc_els_rcv_fpin(vport, (struct fc_els_fpin *)payload,
8826 				  payload_len);
8827 
8828 		/* There are no replies, so no rjt codes */
8829 		break;
8830 	default:
8831 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
8832 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
8833 			cmd, did, vport->port_state);
8834 
8835 		/* Unsupported ELS command, reject */
8836 		rjt_err = LSRJT_CMD_UNSUPPORTED;
8837 		rjt_exp = LSEXP_NOTHING_MORE;
8838 
8839 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
8840 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8841 				 "0115 Unknown ELS command x%x "
8842 				 "received from NPORT x%x\n", cmd, did);
8843 		if (newnode)
8844 			lpfc_nlp_put(ndlp);
8845 		break;
8846 	}
8847 
8848 lsrjt:
8849 	/* check if need to LS_RJT received ELS cmd */
8850 	if (rjt_err) {
8851 		memset(&stat, 0, sizeof(stat));
8852 		stat.un.b.lsRjtRsnCode = rjt_err;
8853 		stat.un.b.lsRjtRsnCodeExp = rjt_exp;
8854 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
8855 			NULL);
8856 	}
8857 
8858 	lpfc_nlp_put(elsiocb->context1);
8859 	elsiocb->context1 = NULL;
8860 
8861 	/* Special case.  Driver received an unsolicited command that
8862 	 * unsupportable given the driver's current state.  Reset the
8863 	 * link and start over.
8864 	 */
8865 	if (init_link) {
8866 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
8867 		if (!mbox)
8868 			return;
8869 		lpfc_linkdown(phba);
8870 		lpfc_init_link(phba, mbox,
8871 			       phba->cfg_topology,
8872 			       phba->cfg_link_speed);
8873 		mbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
8874 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
8875 		mbox->vport = vport;
8876 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT) ==
8877 		    MBX_NOT_FINISHED)
8878 			mempool_free(mbox, phba->mbox_mem_pool);
8879 	}
8880 
8881 	return;
8882 
8883 dropit:
8884 	if (vport && !(vport->load_flag & FC_UNLOADING))
8885 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
8886 			"0111 Dropping received ELS cmd "
8887 			"Data: x%x x%x x%x\n",
8888 			icmd->ulpStatus, icmd->un.ulpWord[4], icmd->ulpTimeout);
8889 	phba->fc_stat.elsRcvDrop++;
8890 }
8891 
8892 /**
8893  * lpfc_els_unsol_event - Process an unsolicited event from an els sli ring
8894  * @phba: pointer to lpfc hba data structure.
8895  * @pring: pointer to a SLI ring.
8896  * @elsiocb: pointer to lpfc els iocb data structure.
8897  *
8898  * This routine is used to process an unsolicited event received from a SLI
8899  * (Service Level Interface) ring. The actual processing of the data buffer
8900  * associated with the unsolicited event is done by invoking the routine
8901  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
8902  * SLI ring on which the unsolicited event was received.
8903  **/
8904 void
lpfc_els_unsol_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * elsiocb)8905 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8906 		     struct lpfc_iocbq *elsiocb)
8907 {
8908 	struct lpfc_vport *vport = phba->pport;
8909 	IOCB_t *icmd = &elsiocb->iocb;
8910 	dma_addr_t paddr;
8911 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
8912 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
8913 
8914 	elsiocb->context1 = NULL;
8915 	elsiocb->context2 = NULL;
8916 	elsiocb->context3 = NULL;
8917 
8918 	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
8919 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
8920 	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
8921 		   (icmd->un.ulpWord[4] & IOERR_PARAM_MASK) ==
8922 		   IOERR_RCV_BUFFER_WAITING) {
8923 		phba->fc_stat.NoRcvBuf++;
8924 		/* Not enough posted buffers; Try posting more buffers */
8925 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
8926 			lpfc_post_buffer(phba, pring, 0);
8927 		return;
8928 	}
8929 
8930 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
8931 	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
8932 	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
8933 		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
8934 			vport = phba->pport;
8935 		else
8936 			vport = lpfc_find_vport_by_vpid(phba,
8937 						icmd->unsli3.rcvsli3.vpi);
8938 	}
8939 
8940 	/* If there are no BDEs associated
8941 	 * with this IOCB, there is nothing to do.
8942 	 */
8943 	if (icmd->ulpBdeCount == 0)
8944 		return;
8945 
8946 	/* type of ELS cmd is first 32bit word
8947 	 * in packet
8948 	 */
8949 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
8950 		elsiocb->context2 = bdeBuf1;
8951 	} else {
8952 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
8953 				 icmd->un.cont64[0].addrLow);
8954 		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
8955 							     paddr);
8956 	}
8957 
8958 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8959 	/*
8960 	 * The different unsolicited event handlers would tell us
8961 	 * if they are done with "mp" by setting context2 to NULL.
8962 	 */
8963 	if (elsiocb->context2) {
8964 		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
8965 		elsiocb->context2 = NULL;
8966 	}
8967 
8968 	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
8969 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
8970 	    icmd->ulpBdeCount == 2) {
8971 		elsiocb->context2 = bdeBuf2;
8972 		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
8973 		/* free mp if we are done with it */
8974 		if (elsiocb->context2) {
8975 			lpfc_in_buf_free(phba, elsiocb->context2);
8976 			elsiocb->context2 = NULL;
8977 		}
8978 	}
8979 }
8980 
8981 static void
lpfc_start_fdmi(struct lpfc_vport * vport)8982 lpfc_start_fdmi(struct lpfc_vport *vport)
8983 {
8984 	struct lpfc_nodelist *ndlp;
8985 
8986 	/* If this is the first time, allocate an ndlp and initialize
8987 	 * it. Otherwise, make sure the node is enabled and then do the
8988 	 * login.
8989 	 */
8990 	ndlp = lpfc_findnode_did(vport, FDMI_DID);
8991 	if (!ndlp) {
8992 		ndlp = lpfc_nlp_init(vport, FDMI_DID);
8993 		if (ndlp) {
8994 			ndlp->nlp_type |= NLP_FABRIC;
8995 		} else {
8996 			return;
8997 		}
8998 	}
8999 	if (!NLP_CHK_NODE_ACT(ndlp))
9000 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
9001 
9002 	if (ndlp) {
9003 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
9004 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
9005 	}
9006 }
9007 
9008 /**
9009  * lpfc_do_scr_ns_plogi - Issue a plogi to the name server for scr
9010  * @phba: pointer to lpfc hba data structure.
9011  * @vport: pointer to a virtual N_Port data structure.
9012  *
9013  * This routine issues a Port Login (PLOGI) to the Name Server with
9014  * State Change Request (SCR) for a @vport. This routine will create an
9015  * ndlp for the Name Server associated to the @vport if such node does
9016  * not already exist. The PLOGI to Name Server is issued by invoking the
9017  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
9018  * (FDMI) is configured to the @vport, a FDMI node will be created and
9019  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
9020  **/
9021 void
lpfc_do_scr_ns_plogi(struct lpfc_hba * phba,struct lpfc_vport * vport)9022 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
9023 {
9024 	struct lpfc_nodelist *ndlp;
9025 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9026 
9027 	/*
9028 	 * If lpfc_delay_discovery parameter is set and the clean address
9029 	 * bit is cleared and fc fabric parameters chenged, delay FC NPort
9030 	 * discovery.
9031 	 */
9032 	spin_lock_irq(shost->host_lock);
9033 	if (vport->fc_flag & FC_DISC_DELAYED) {
9034 		spin_unlock_irq(shost->host_lock);
9035 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
9036 				"3334 Delay fc port discovery for %d seconds\n",
9037 				phba->fc_ratov);
9038 		mod_timer(&vport->delayed_disc_tmo,
9039 			jiffies + msecs_to_jiffies(1000 * phba->fc_ratov));
9040 		return;
9041 	}
9042 	spin_unlock_irq(shost->host_lock);
9043 
9044 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
9045 	if (!ndlp) {
9046 		ndlp = lpfc_nlp_init(vport, NameServer_DID);
9047 		if (!ndlp) {
9048 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9049 				lpfc_disc_start(vport);
9050 				return;
9051 			}
9052 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9053 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9054 					 "0251 NameServer login: no memory\n");
9055 			return;
9056 		}
9057 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
9058 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
9059 		if (!ndlp) {
9060 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
9061 				lpfc_disc_start(vport);
9062 				return;
9063 			}
9064 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9065 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9066 					"0348 NameServer login: node freed\n");
9067 			return;
9068 		}
9069 	}
9070 	ndlp->nlp_type |= NLP_FABRIC;
9071 
9072 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
9073 
9074 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
9075 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9076 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9077 				 "0252 Cannot issue NameServer login\n");
9078 		return;
9079 	}
9080 
9081 	if ((phba->cfg_enable_SmartSAN ||
9082 	     (phba->cfg_fdmi_on == LPFC_FDMI_SUPPORT)) &&
9083 	     (vport->load_flag & FC_ALLOW_FDMI))
9084 		lpfc_start_fdmi(vport);
9085 }
9086 
9087 /**
9088  * lpfc_cmpl_reg_new_vport - Completion callback function to register new vport
9089  * @phba: pointer to lpfc hba data structure.
9090  * @pmb: pointer to the driver internal queue element for mailbox command.
9091  *
9092  * This routine is the completion callback function to register new vport
9093  * mailbox command. If the new vport mailbox command completes successfully,
9094  * the fabric registration login shall be performed on physical port (the
9095  * new vport created is actually a physical port, with VPI 0) or the port
9096  * login to Name Server for State Change Request (SCR) will be performed
9097  * on virtual port (real virtual port, with VPI greater than 0).
9098  **/
9099 static void
lpfc_cmpl_reg_new_vport(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)9100 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
9101 {
9102 	struct lpfc_vport *vport = pmb->vport;
9103 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
9104 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
9105 	MAILBOX_t *mb = &pmb->u.mb;
9106 	int rc;
9107 
9108 	spin_lock_irq(shost->host_lock);
9109 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
9110 	spin_unlock_irq(shost->host_lock);
9111 
9112 	if (mb->mbxStatus) {
9113 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9114 				"0915 Register VPI failed : Status: x%x"
9115 				" upd bit: x%x \n", mb->mbxStatus,
9116 				 mb->un.varRegVpi.upd);
9117 		if (phba->sli_rev == LPFC_SLI_REV4 &&
9118 			mb->un.varRegVpi.upd)
9119 			goto mbox_err_exit ;
9120 
9121 		switch (mb->mbxStatus) {
9122 		case 0x11:	/* unsupported feature */
9123 		case 0x9603:	/* max_vpi exceeded */
9124 		case 0x9602:	/* Link event since CLEAR_LA */
9125 			/* giving up on vport registration */
9126 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9127 			spin_lock_irq(shost->host_lock);
9128 			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
9129 			spin_unlock_irq(shost->host_lock);
9130 			lpfc_can_disctmo(vport);
9131 			break;
9132 		/* If reg_vpi fail with invalid VPI status, re-init VPI */
9133 		case 0x20:
9134 			spin_lock_irq(shost->host_lock);
9135 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9136 			spin_unlock_irq(shost->host_lock);
9137 			lpfc_init_vpi(phba, pmb, vport->vpi);
9138 			pmb->vport = vport;
9139 			pmb->mbox_cmpl = lpfc_init_vpi_cmpl;
9140 			rc = lpfc_sli_issue_mbox(phba, pmb,
9141 				MBX_NOWAIT);
9142 			if (rc == MBX_NOT_FINISHED) {
9143 				lpfc_printf_vlog(vport, KERN_ERR,
9144 						 LOG_TRACE_EVENT,
9145 					"2732 Failed to issue INIT_VPI"
9146 					" mailbox command\n");
9147 			} else {
9148 				lpfc_nlp_put(ndlp);
9149 				return;
9150 			}
9151 			fallthrough;
9152 		default:
9153 			/* Try to recover from this error */
9154 			if (phba->sli_rev == LPFC_SLI_REV4)
9155 				lpfc_sli4_unreg_all_rpis(vport);
9156 			lpfc_mbx_unreg_vpi(vport);
9157 			spin_lock_irq(shost->host_lock);
9158 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9159 			spin_unlock_irq(shost->host_lock);
9160 			if (mb->mbxStatus == MBX_NOT_FINISHED)
9161 				break;
9162 			if ((vport->port_type == LPFC_PHYSICAL_PORT) &&
9163 			    !(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG)) {
9164 				if (phba->sli_rev == LPFC_SLI_REV4)
9165 					lpfc_issue_init_vfi(vport);
9166 				else
9167 					lpfc_initial_flogi(vport);
9168 			} else {
9169 				lpfc_initial_fdisc(vport);
9170 			}
9171 			break;
9172 		}
9173 	} else {
9174 		spin_lock_irq(shost->host_lock);
9175 		vport->vpi_state |= LPFC_VPI_REGISTERED;
9176 		spin_unlock_irq(shost->host_lock);
9177 		if (vport == phba->pport) {
9178 			if (phba->sli_rev < LPFC_SLI_REV4)
9179 				lpfc_issue_fabric_reglogin(vport);
9180 			else {
9181 				/*
9182 				 * If the physical port is instantiated using
9183 				 * FDISC, do not start vport discovery.
9184 				 */
9185 				if (vport->port_state != LPFC_FDISC)
9186 					lpfc_start_fdiscs(phba);
9187 				lpfc_do_scr_ns_plogi(phba, vport);
9188 			}
9189 		} else
9190 			lpfc_do_scr_ns_plogi(phba, vport);
9191 	}
9192 mbox_err_exit:
9193 	/* Now, we decrement the ndlp reference count held for this
9194 	 * callback function
9195 	 */
9196 	lpfc_nlp_put(ndlp);
9197 
9198 	mempool_free(pmb, phba->mbox_mem_pool);
9199 	return;
9200 }
9201 
9202 /**
9203  * lpfc_register_new_vport - Register a new vport with a HBA
9204  * @phba: pointer to lpfc hba data structure.
9205  * @vport: pointer to a host virtual N_Port data structure.
9206  * @ndlp: pointer to a node-list data structure.
9207  *
9208  * This routine registers the @vport as a new virtual port with a HBA.
9209  * It is done through a registering vpi mailbox command.
9210  **/
9211 void
lpfc_register_new_vport(struct lpfc_hba * phba,struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)9212 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
9213 			struct lpfc_nodelist *ndlp)
9214 {
9215 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9216 	LPFC_MBOXQ_t *mbox;
9217 
9218 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9219 	if (mbox) {
9220 		lpfc_reg_vpi(vport, mbox);
9221 		mbox->vport = vport;
9222 		mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
9223 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
9224 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
9225 		    == MBX_NOT_FINISHED) {
9226 			/* mailbox command not success, decrement ndlp
9227 			 * reference count for this command
9228 			 */
9229 			lpfc_nlp_put(ndlp);
9230 			mempool_free(mbox, phba->mbox_mem_pool);
9231 
9232 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9233 				"0253 Register VPI: Can't send mbox\n");
9234 			goto mbox_err_exit;
9235 		}
9236 	} else {
9237 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9238 				 "0254 Register VPI: no memory\n");
9239 		goto mbox_err_exit;
9240 	}
9241 	return;
9242 
9243 mbox_err_exit:
9244 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9245 	spin_lock_irq(shost->host_lock);
9246 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
9247 	spin_unlock_irq(shost->host_lock);
9248 	return;
9249 }
9250 
9251 /**
9252  * lpfc_cancel_all_vport_retry_delay_timer - Cancel all vport retry delay timer
9253  * @phba: pointer to lpfc hba data structure.
9254  *
9255  * This routine cancels the retry delay timers to all the vports.
9256  **/
9257 void
lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba * phba)9258 lpfc_cancel_all_vport_retry_delay_timer(struct lpfc_hba *phba)
9259 {
9260 	struct lpfc_vport **vports;
9261 	struct lpfc_nodelist *ndlp;
9262 	uint32_t link_state;
9263 	int i;
9264 
9265 	/* Treat this failure as linkdown for all vports */
9266 	link_state = phba->link_state;
9267 	lpfc_linkdown(phba);
9268 	phba->link_state = link_state;
9269 
9270 	vports = lpfc_create_vport_work_array(phba);
9271 
9272 	if (vports) {
9273 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
9274 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
9275 			if (ndlp)
9276 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
9277 			lpfc_els_flush_cmd(vports[i]);
9278 		}
9279 		lpfc_destroy_vport_work_array(phba, vports);
9280 	}
9281 }
9282 
9283 /**
9284  * lpfc_retry_pport_discovery - Start timer to retry FLOGI.
9285  * @phba: pointer to lpfc hba data structure.
9286  *
9287  * This routine abort all pending discovery commands and
9288  * start a timer to retry FLOGI for the physical port
9289  * discovery.
9290  **/
9291 void
lpfc_retry_pport_discovery(struct lpfc_hba * phba)9292 lpfc_retry_pport_discovery(struct lpfc_hba *phba)
9293 {
9294 	struct lpfc_nodelist *ndlp;
9295 	struct Scsi_Host  *shost;
9296 
9297 	/* Cancel the all vports retry delay retry timers */
9298 	lpfc_cancel_all_vport_retry_delay_timer(phba);
9299 
9300 	/* If fabric require FLOGI, then re-instantiate physical login */
9301 	ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
9302 	if (!ndlp)
9303 		return;
9304 
9305 	shost = lpfc_shost_from_vport(phba->pport);
9306 	mod_timer(&ndlp->nlp_delayfunc, jiffies + msecs_to_jiffies(1000));
9307 	spin_lock_irq(shost->host_lock);
9308 	ndlp->nlp_flag |= NLP_DELAY_TMO;
9309 	spin_unlock_irq(shost->host_lock);
9310 	ndlp->nlp_last_elscmd = ELS_CMD_FLOGI;
9311 	phba->pport->port_state = LPFC_FLOGI;
9312 	return;
9313 }
9314 
9315 /**
9316  * lpfc_fabric_login_reqd - Check if FLOGI required.
9317  * @phba: pointer to lpfc hba data structure.
9318  * @cmdiocb: pointer to FDISC command iocb.
9319  * @rspiocb: pointer to FDISC response iocb.
9320  *
9321  * This routine checks if a FLOGI is reguired for FDISC
9322  * to succeed.
9323  **/
9324 static int
lpfc_fabric_login_reqd(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)9325 lpfc_fabric_login_reqd(struct lpfc_hba *phba,
9326 		struct lpfc_iocbq *cmdiocb,
9327 		struct lpfc_iocbq *rspiocb)
9328 {
9329 
9330 	if ((rspiocb->iocb.ulpStatus != IOSTAT_FABRIC_RJT) ||
9331 		(rspiocb->iocb.un.ulpWord[4] != RJT_LOGIN_REQUIRED))
9332 		return 0;
9333 	else
9334 		return 1;
9335 }
9336 
9337 /**
9338  * lpfc_cmpl_els_fdisc - Completion function for fdisc iocb command
9339  * @phba: pointer to lpfc hba data structure.
9340  * @cmdiocb: pointer to lpfc command iocb data structure.
9341  * @rspiocb: pointer to lpfc response iocb data structure.
9342  *
9343  * This routine is the completion callback function to a Fabric Discover
9344  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
9345  * single threaded, each FDISC completion callback function will reset
9346  * the discovery timer for all vports such that the timers will not get
9347  * unnecessary timeout. The function checks the FDISC IOCB status. If error
9348  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
9349  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
9350  * assigned to the vport has been changed with the completion of the FDISC
9351  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
9352  * are unregistered from the HBA, and then the lpfc_register_new_vport()
9353  * routine is invoked to register new vport with the HBA. Otherwise, the
9354  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
9355  * Server for State Change Request (SCR).
9356  **/
9357 static void
lpfc_cmpl_els_fdisc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)9358 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9359 		    struct lpfc_iocbq *rspiocb)
9360 {
9361 	struct lpfc_vport *vport = cmdiocb->vport;
9362 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
9363 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
9364 	struct lpfc_nodelist *np;
9365 	struct lpfc_nodelist *next_np;
9366 	IOCB_t *irsp = &rspiocb->iocb;
9367 	struct lpfc_iocbq *piocb;
9368 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
9369 	struct serv_parm *sp;
9370 	uint8_t fabric_param_changed;
9371 
9372 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9373 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
9374 			 irsp->ulpStatus, irsp->un.ulpWord[4],
9375 			 vport->fc_prevDID);
9376 	/* Since all FDISCs are being single threaded, we
9377 	 * must reset the discovery timer for ALL vports
9378 	 * waiting to send FDISC when one completes.
9379 	 */
9380 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
9381 		lpfc_set_disctmo(piocb->vport);
9382 	}
9383 
9384 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9385 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
9386 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
9387 
9388 	if (irsp->ulpStatus) {
9389 
9390 		if (lpfc_fabric_login_reqd(phba, cmdiocb, rspiocb)) {
9391 			lpfc_retry_pport_discovery(phba);
9392 			goto out;
9393 		}
9394 
9395 		/* Check for retry */
9396 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
9397 			goto out;
9398 		/* FDISC failed */
9399 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9400 				 "0126 FDISC failed. (x%x/x%x)\n",
9401 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
9402 		goto fdisc_failed;
9403 	}
9404 	spin_lock_irq(shost->host_lock);
9405 	vport->fc_flag &= ~FC_VPORT_CVL_RCVD;
9406 	vport->fc_flag &= ~FC_VPORT_LOGO_RCVD;
9407 	vport->fc_flag |= FC_FABRIC;
9408 	if (vport->phba->fc_topology == LPFC_TOPOLOGY_LOOP)
9409 		vport->fc_flag |=  FC_PUBLIC_LOOP;
9410 	spin_unlock_irq(shost->host_lock);
9411 
9412 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
9413 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
9414 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
9415 	if (!prsp)
9416 		goto out;
9417 	sp = prsp->virt + sizeof(uint32_t);
9418 	fabric_param_changed = lpfc_check_clean_addr_bit(vport, sp);
9419 	memcpy(&vport->fabric_portname, &sp->portName,
9420 		sizeof(struct lpfc_name));
9421 	memcpy(&vport->fabric_nodename, &sp->nodeName,
9422 		sizeof(struct lpfc_name));
9423 	if (fabric_param_changed &&
9424 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9425 		/* If our NportID changed, we need to ensure all
9426 		 * remaining NPORTs get unreg_login'ed so we can
9427 		 * issue unreg_vpi.
9428 		 */
9429 		list_for_each_entry_safe(np, next_np,
9430 			&vport->fc_nodes, nlp_listp) {
9431 			if (!NLP_CHK_NODE_ACT(ndlp) ||
9432 			    (np->nlp_state != NLP_STE_NPR_NODE) ||
9433 			    !(np->nlp_flag & NLP_NPR_ADISC))
9434 				continue;
9435 			spin_lock_irq(shost->host_lock);
9436 			np->nlp_flag &= ~NLP_NPR_ADISC;
9437 			spin_unlock_irq(shost->host_lock);
9438 			lpfc_unreg_rpi(vport, np);
9439 		}
9440 		lpfc_cleanup_pending_mbox(vport);
9441 
9442 		if (phba->sli_rev == LPFC_SLI_REV4)
9443 			lpfc_sli4_unreg_all_rpis(vport);
9444 
9445 		lpfc_mbx_unreg_vpi(vport);
9446 		spin_lock_irq(shost->host_lock);
9447 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
9448 		if (phba->sli_rev == LPFC_SLI_REV4)
9449 			vport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
9450 		else
9451 			vport->fc_flag |= FC_LOGO_RCVD_DID_CHNG;
9452 		spin_unlock_irq(shost->host_lock);
9453 	} else if ((phba->sli_rev == LPFC_SLI_REV4) &&
9454 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
9455 		/*
9456 		 * Driver needs to re-reg VPI in order for f/w
9457 		 * to update the MAC address.
9458 		 */
9459 		lpfc_register_new_vport(phba, vport, ndlp);
9460 		goto out;
9461 	}
9462 
9463 	if (vport->fc_flag & FC_VPORT_NEEDS_INIT_VPI)
9464 		lpfc_issue_init_vpi(vport);
9465 	else if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
9466 		lpfc_register_new_vport(phba, vport, ndlp);
9467 	else
9468 		lpfc_do_scr_ns_plogi(phba, vport);
9469 	goto out;
9470 fdisc_failed:
9471 	if (vport->fc_vport &&
9472 	    (vport->fc_vport->vport_state != FC_VPORT_NO_FABRIC_RSCS))
9473 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9474 	/* Cancel discovery timer */
9475 	lpfc_can_disctmo(vport);
9476 	lpfc_nlp_put(ndlp);
9477 out:
9478 	lpfc_els_free_iocb(phba, cmdiocb);
9479 }
9480 
9481 /**
9482  * lpfc_issue_els_fdisc - Issue a fdisc iocb command
9483  * @vport: pointer to a virtual N_Port data structure.
9484  * @ndlp: pointer to a node-list data structure.
9485  * @retry: number of retries to the command IOCB.
9486  *
9487  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
9488  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
9489  * routine to issue the IOCB, which makes sure only one outstanding fabric
9490  * IOCB will be sent off HBA at any given time.
9491  *
9492  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9493  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9494  * will be stored into the context1 field of the IOCB for the completion
9495  * callback function to the FDISC ELS command.
9496  *
9497  * Return code
9498  *   0 - Successfully issued fdisc iocb command
9499  *   1 - Failed to issue fdisc iocb command
9500  **/
9501 static int
lpfc_issue_els_fdisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)9502 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
9503 		     uint8_t retry)
9504 {
9505 	struct lpfc_hba *phba = vport->phba;
9506 	IOCB_t *icmd;
9507 	struct lpfc_iocbq *elsiocb;
9508 	struct serv_parm *sp;
9509 	uint8_t *pcmd;
9510 	uint16_t cmdsize;
9511 	int did = ndlp->nlp_DID;
9512 	int rc;
9513 
9514 	vport->port_state = LPFC_FDISC;
9515 	vport->fc_myDID = 0;
9516 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
9517 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
9518 				     ELS_CMD_FDISC);
9519 	if (!elsiocb) {
9520 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9521 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9522 				 "0255 Issue FDISC: no IOCB\n");
9523 		return 1;
9524 	}
9525 
9526 	icmd = &elsiocb->iocb;
9527 	icmd->un.elsreq64.myID = 0;
9528 	icmd->un.elsreq64.fl = 1;
9529 
9530 	/*
9531 	 * SLI3 ports require a different context type value than SLI4.
9532 	 * Catch SLI3 ports here and override the prep.
9533 	 */
9534 	if (phba->sli_rev == LPFC_SLI_REV3) {
9535 		icmd->ulpCt_h = 1;
9536 		icmd->ulpCt_l = 0;
9537 	}
9538 
9539 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9540 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
9541 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
9542 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
9543 	sp = (struct serv_parm *) pcmd;
9544 	/* Setup CSPs accordingly for Fabric */
9545 	sp->cmn.e_d_tov = 0;
9546 	sp->cmn.w2.r_a_tov = 0;
9547 	sp->cmn.virtual_fabric_support = 0;
9548 	sp->cls1.classValid = 0;
9549 	sp->cls2.seqDelivery = 1;
9550 	sp->cls3.seqDelivery = 1;
9551 
9552 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
9553 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
9554 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
9555 	pcmd += sizeof(uint32_t); /* Port Name */
9556 	memcpy(pcmd, &vport->fc_portname, 8);
9557 	pcmd += sizeof(uint32_t); /* Node Name */
9558 	pcmd += sizeof(uint32_t); /* Node Name */
9559 	memcpy(pcmd, &vport->fc_nodename, 8);
9560 	sp->cmn.valid_vendor_ver_level = 0;
9561 	memset(sp->un.vendorVersion, 0, sizeof(sp->un.vendorVersion));
9562 	lpfc_set_disctmo(vport);
9563 
9564 	phba->fc_stat.elsXmitFDISC++;
9565 	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
9566 
9567 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9568 		"Issue FDISC:     did:x%x",
9569 		did, 0, 0);
9570 
9571 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
9572 	if (rc == IOCB_ERROR) {
9573 		lpfc_els_free_iocb(phba, elsiocb);
9574 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
9575 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
9576 				 "0256 Issue FDISC: Cannot send IOCB\n");
9577 		return 1;
9578 	}
9579 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
9580 	return 0;
9581 }
9582 
9583 /**
9584  * lpfc_cmpl_els_npiv_logo - Completion function with vport logo
9585  * @phba: pointer to lpfc hba data structure.
9586  * @cmdiocb: pointer to lpfc command iocb data structure.
9587  * @rspiocb: pointer to lpfc response iocb data structure.
9588  *
9589  * This routine is the completion callback function to the issuing of a LOGO
9590  * ELS command off a vport. It frees the command IOCB and then decrement the
9591  * reference count held on ndlp for this completion function, indicating that
9592  * the reference to the ndlp is no long needed. Note that the
9593  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
9594  * callback function and an additional explicit ndlp reference decrementation
9595  * will trigger the actual release of the ndlp.
9596  **/
9597 static void
lpfc_cmpl_els_npiv_logo(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)9598 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9599 			struct lpfc_iocbq *rspiocb)
9600 {
9601 	struct lpfc_vport *vport = cmdiocb->vport;
9602 	IOCB_t *irsp;
9603 	struct lpfc_nodelist *ndlp;
9604 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9605 
9606 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
9607 	irsp = &rspiocb->iocb;
9608 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9609 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
9610 		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
9611 
9612 	lpfc_els_free_iocb(phba, cmdiocb);
9613 	vport->unreg_vpi_cmpl = VPORT_ERROR;
9614 
9615 	/* Trigger the release of the ndlp after logo */
9616 	lpfc_nlp_put(ndlp);
9617 
9618 	/* NPIV LOGO completes to NPort <nlp_DID> */
9619 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
9620 			 "2928 NPIV LOGO completes to NPort x%x "
9621 			 "Data: x%x x%x x%x x%x\n",
9622 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
9623 			 irsp->ulpTimeout, vport->num_disc_nodes);
9624 
9625 	if (irsp->ulpStatus == IOSTAT_SUCCESS) {
9626 		spin_lock_irq(shost->host_lock);
9627 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
9628 		vport->fc_flag &= ~FC_FABRIC;
9629 		spin_unlock_irq(shost->host_lock);
9630 		lpfc_can_disctmo(vport);
9631 	}
9632 }
9633 
9634 /**
9635  * lpfc_issue_els_npiv_logo - Issue a logo off a vport
9636  * @vport: pointer to a virtual N_Port data structure.
9637  * @ndlp: pointer to a node-list data structure.
9638  *
9639  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
9640  *
9641  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
9642  * will be incremented by 1 for holding the ndlp and the reference to ndlp
9643  * will be stored into the context1 field of the IOCB for the completion
9644  * callback function to the LOGO ELS command.
9645  *
9646  * Return codes
9647  *   0 - Successfully issued logo off the @vport
9648  *   1 - Failed to issue logo off the @vport
9649  **/
9650 int
lpfc_issue_els_npiv_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)9651 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
9652 {
9653 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
9654 	struct lpfc_hba  *phba = vport->phba;
9655 	struct lpfc_iocbq *elsiocb;
9656 	uint8_t *pcmd;
9657 	uint16_t cmdsize;
9658 
9659 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
9660 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
9661 				     ELS_CMD_LOGO);
9662 	if (!elsiocb)
9663 		return 1;
9664 
9665 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
9666 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
9667 	pcmd += sizeof(uint32_t);
9668 
9669 	/* Fill in LOGO payload */
9670 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
9671 	pcmd += sizeof(uint32_t);
9672 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
9673 
9674 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
9675 		"Issue LOGO npiv  did:x%x flg:x%x",
9676 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
9677 
9678 	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
9679 	spin_lock_irq(shost->host_lock);
9680 	ndlp->nlp_flag |= NLP_LOGO_SND;
9681 	spin_unlock_irq(shost->host_lock);
9682 	if (lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, elsiocb, 0) ==
9683 	    IOCB_ERROR) {
9684 		spin_lock_irq(shost->host_lock);
9685 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
9686 		spin_unlock_irq(shost->host_lock);
9687 		lpfc_els_free_iocb(phba, elsiocb);
9688 		return 1;
9689 	}
9690 	return 0;
9691 }
9692 
9693 /**
9694  * lpfc_fabric_block_timeout - Handler function to the fabric block timer
9695  * @t: timer context used to obtain the lpfc hba.
9696  *
9697  * This routine is invoked by the fabric iocb block timer after
9698  * timeout. It posts the fabric iocb block timeout event by setting the
9699  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
9700  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
9701  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
9702  * posted event WORKER_FABRIC_BLOCK_TMO.
9703  **/
9704 void
lpfc_fabric_block_timeout(struct timer_list * t)9705 lpfc_fabric_block_timeout(struct timer_list *t)
9706 {
9707 	struct lpfc_hba  *phba = from_timer(phba, t, fabric_block_timer);
9708 	unsigned long iflags;
9709 	uint32_t tmo_posted;
9710 
9711 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
9712 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
9713 	if (!tmo_posted)
9714 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
9715 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
9716 
9717 	if (!tmo_posted)
9718 		lpfc_worker_wake_up(phba);
9719 	return;
9720 }
9721 
9722 /**
9723  * lpfc_resume_fabric_iocbs - Issue a fabric iocb from driver internal list
9724  * @phba: pointer to lpfc hba data structure.
9725  *
9726  * This routine issues one fabric iocb from the driver internal list to
9727  * the HBA. It first checks whether it's ready to issue one fabric iocb to
9728  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
9729  * remove one pending fabric iocb from the driver internal list and invokes
9730  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
9731  **/
9732 static void
lpfc_resume_fabric_iocbs(struct lpfc_hba * phba)9733 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
9734 {
9735 	struct lpfc_iocbq *iocb;
9736 	unsigned long iflags;
9737 	int ret;
9738 	IOCB_t *cmd;
9739 
9740 repeat:
9741 	iocb = NULL;
9742 	spin_lock_irqsave(&phba->hbalock, iflags);
9743 	/* Post any pending iocb to the SLI layer */
9744 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
9745 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
9746 				 list);
9747 		if (iocb)
9748 			/* Increment fabric iocb count to hold the position */
9749 			atomic_inc(&phba->fabric_iocb_count);
9750 	}
9751 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9752 	if (iocb) {
9753 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9754 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9755 		iocb->iocb_flag |= LPFC_IO_FABRIC;
9756 
9757 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9758 			"Fabric sched1:   ste:x%x",
9759 			iocb->vport->port_state, 0, 0);
9760 
9761 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9762 
9763 		if (ret == IOCB_ERROR) {
9764 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9765 			iocb->fabric_iocb_cmpl = NULL;
9766 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9767 			cmd = &iocb->iocb;
9768 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
9769 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
9770 			iocb->iocb_cmpl(phba, iocb, iocb);
9771 
9772 			atomic_dec(&phba->fabric_iocb_count);
9773 			goto repeat;
9774 		}
9775 	}
9776 
9777 	return;
9778 }
9779 
9780 /**
9781  * lpfc_unblock_fabric_iocbs - Unblock issuing fabric iocb command
9782  * @phba: pointer to lpfc hba data structure.
9783  *
9784  * This routine unblocks the  issuing fabric iocb command. The function
9785  * will clear the fabric iocb block bit and then invoke the routine
9786  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
9787  * from the driver internal fabric iocb list.
9788  **/
9789 void
lpfc_unblock_fabric_iocbs(struct lpfc_hba * phba)9790 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
9791 {
9792 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9793 
9794 	lpfc_resume_fabric_iocbs(phba);
9795 	return;
9796 }
9797 
9798 /**
9799  * lpfc_block_fabric_iocbs - Block issuing fabric iocb command
9800  * @phba: pointer to lpfc hba data structure.
9801  *
9802  * This routine blocks the issuing fabric iocb for a specified amount of
9803  * time (currently 100 ms). This is done by set the fabric iocb block bit
9804  * and set up a timeout timer for 100ms. When the block bit is set, no more
9805  * fabric iocb will be issued out of the HBA.
9806  **/
9807 static void
lpfc_block_fabric_iocbs(struct lpfc_hba * phba)9808 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
9809 {
9810 	int blocked;
9811 
9812 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9813 	/* Start a timer to unblock fabric iocbs after 100ms */
9814 	if (!blocked)
9815 		mod_timer(&phba->fabric_block_timer,
9816 			  jiffies + msecs_to_jiffies(100));
9817 
9818 	return;
9819 }
9820 
9821 /**
9822  * lpfc_cmpl_fabric_iocb - Completion callback function for fabric iocb
9823  * @phba: pointer to lpfc hba data structure.
9824  * @cmdiocb: pointer to lpfc command iocb data structure.
9825  * @rspiocb: pointer to lpfc response iocb data structure.
9826  *
9827  * This routine is the callback function that is put to the fabric iocb's
9828  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
9829  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
9830  * function first restores and invokes the original iocb's callback function
9831  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
9832  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
9833  **/
9834 static void
lpfc_cmpl_fabric_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)9835 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9836 	struct lpfc_iocbq *rspiocb)
9837 {
9838 	struct ls_rjt stat;
9839 
9840 	BUG_ON((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC);
9841 
9842 	switch (rspiocb->iocb.ulpStatus) {
9843 		case IOSTAT_NPORT_RJT:
9844 		case IOSTAT_FABRIC_RJT:
9845 			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
9846 				lpfc_block_fabric_iocbs(phba);
9847 			}
9848 			break;
9849 
9850 		case IOSTAT_NPORT_BSY:
9851 		case IOSTAT_FABRIC_BSY:
9852 			lpfc_block_fabric_iocbs(phba);
9853 			break;
9854 
9855 		case IOSTAT_LS_RJT:
9856 			stat.un.lsRjtError =
9857 				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
9858 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
9859 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
9860 				lpfc_block_fabric_iocbs(phba);
9861 			break;
9862 	}
9863 
9864 	BUG_ON(atomic_read(&phba->fabric_iocb_count) == 0);
9865 
9866 	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
9867 	cmdiocb->fabric_iocb_cmpl = NULL;
9868 	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
9869 	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
9870 
9871 	atomic_dec(&phba->fabric_iocb_count);
9872 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
9873 		/* Post any pending iocbs to HBA */
9874 		lpfc_resume_fabric_iocbs(phba);
9875 	}
9876 }
9877 
9878 /**
9879  * lpfc_issue_fabric_iocb - Issue a fabric iocb command
9880  * @phba: pointer to lpfc hba data structure.
9881  * @iocb: pointer to lpfc command iocb data structure.
9882  *
9883  * This routine is used as the top-level API for issuing a fabric iocb command
9884  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
9885  * function makes sure that only one fabric bound iocb will be outstanding at
9886  * any given time. As such, this function will first check to see whether there
9887  * is already an outstanding fabric iocb on the wire. If so, it will put the
9888  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
9889  * issued later. Otherwise, it will issue the iocb on the wire and update the
9890  * fabric iocb count it indicate that there is one fabric iocb on the wire.
9891  *
9892  * Note, this implementation has a potential sending out fabric IOCBs out of
9893  * order. The problem is caused by the construction of the "ready" boolen does
9894  * not include the condition that the internal fabric IOCB list is empty. As
9895  * such, it is possible a fabric IOCB issued by this routine might be "jump"
9896  * ahead of the fabric IOCBs in the internal list.
9897  *
9898  * Return code
9899  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
9900  *   IOCB_ERROR - failed to issue fabric iocb
9901  **/
9902 static int
lpfc_issue_fabric_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * iocb)9903 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
9904 {
9905 	unsigned long iflags;
9906 	int ready;
9907 	int ret;
9908 
9909 	BUG_ON(atomic_read(&phba->fabric_iocb_count) > 1);
9910 
9911 	spin_lock_irqsave(&phba->hbalock, iflags);
9912 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
9913 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
9914 
9915 	if (ready)
9916 		/* Increment fabric iocb count to hold the position */
9917 		atomic_inc(&phba->fabric_iocb_count);
9918 	spin_unlock_irqrestore(&phba->hbalock, iflags);
9919 	if (ready) {
9920 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
9921 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
9922 		iocb->iocb_flag |= LPFC_IO_FABRIC;
9923 
9924 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
9925 			"Fabric sched2:   ste:x%x",
9926 			iocb->vport->port_state, 0, 0);
9927 
9928 		ret = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, iocb, 0);
9929 
9930 		if (ret == IOCB_ERROR) {
9931 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
9932 			iocb->fabric_iocb_cmpl = NULL;
9933 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
9934 			atomic_dec(&phba->fabric_iocb_count);
9935 		}
9936 	} else {
9937 		spin_lock_irqsave(&phba->hbalock, iflags);
9938 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
9939 		spin_unlock_irqrestore(&phba->hbalock, iflags);
9940 		ret = IOCB_SUCCESS;
9941 	}
9942 	return ret;
9943 }
9944 
9945 /**
9946  * lpfc_fabric_abort_vport - Abort a vport's iocbs from driver fabric iocb list
9947  * @vport: pointer to a virtual N_Port data structure.
9948  *
9949  * This routine aborts all the IOCBs associated with a @vport from the
9950  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9951  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9952  * list, removes each IOCB associated with the @vport off the list, set the
9953  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9954  * associated with the IOCB.
9955  **/
lpfc_fabric_abort_vport(struct lpfc_vport * vport)9956 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
9957 {
9958 	LIST_HEAD(completions);
9959 	struct lpfc_hba  *phba = vport->phba;
9960 	struct lpfc_iocbq *tmp_iocb, *piocb;
9961 
9962 	spin_lock_irq(&phba->hbalock);
9963 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
9964 				 list) {
9965 
9966 		if (piocb->vport != vport)
9967 			continue;
9968 
9969 		list_move_tail(&piocb->list, &completions);
9970 	}
9971 	spin_unlock_irq(&phba->hbalock);
9972 
9973 	/* Cancel all the IOCBs from the completions list */
9974 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9975 			      IOERR_SLI_ABORTED);
9976 }
9977 
9978 /**
9979  * lpfc_fabric_abort_nport - Abort a ndlp's iocbs from driver fabric iocb list
9980  * @ndlp: pointer to a node-list data structure.
9981  *
9982  * This routine aborts all the IOCBs associated with an @ndlp from the
9983  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
9984  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
9985  * list, removes each IOCB associated with the @ndlp off the list, set the
9986  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
9987  * associated with the IOCB.
9988  **/
lpfc_fabric_abort_nport(struct lpfc_nodelist * ndlp)9989 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
9990 {
9991 	LIST_HEAD(completions);
9992 	struct lpfc_hba  *phba = ndlp->phba;
9993 	struct lpfc_iocbq *tmp_iocb, *piocb;
9994 	struct lpfc_sli_ring *pring;
9995 
9996 	pring = lpfc_phba_elsring(phba);
9997 
9998 	if (unlikely(!pring))
9999 		return;
10000 
10001 	spin_lock_irq(&phba->hbalock);
10002 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
10003 				 list) {
10004 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
10005 
10006 			list_move_tail(&piocb->list, &completions);
10007 		}
10008 	}
10009 	spin_unlock_irq(&phba->hbalock);
10010 
10011 	/* Cancel all the IOCBs from the completions list */
10012 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10013 			      IOERR_SLI_ABORTED);
10014 }
10015 
10016 /**
10017  * lpfc_fabric_abort_hba - Abort all iocbs on driver fabric iocb list
10018  * @phba: pointer to lpfc hba data structure.
10019  *
10020  * This routine aborts all the IOCBs currently on the driver internal
10021  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
10022  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
10023  * list, removes IOCBs off the list, set the status feild to
10024  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
10025  * the IOCB.
10026  **/
lpfc_fabric_abort_hba(struct lpfc_hba * phba)10027 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
10028 {
10029 	LIST_HEAD(completions);
10030 
10031 	spin_lock_irq(&phba->hbalock);
10032 	list_splice_init(&phba->fabric_iocb_list, &completions);
10033 	spin_unlock_irq(&phba->hbalock);
10034 
10035 	/* Cancel all the IOCBs from the completions list */
10036 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
10037 			      IOERR_SLI_ABORTED);
10038 }
10039 
10040 /**
10041  * lpfc_sli4_vport_delete_els_xri_aborted -Remove all ndlp references for vport
10042  * @vport: pointer to lpfc vport data structure.
10043  *
10044  * This routine is invoked by the vport cleanup for deletions and the cleanup
10045  * for an ndlp on removal.
10046  **/
10047 void
lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport * vport)10048 lpfc_sli4_vport_delete_els_xri_aborted(struct lpfc_vport *vport)
10049 {
10050 	struct lpfc_hba *phba = vport->phba;
10051 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
10052 	unsigned long iflag = 0;
10053 
10054 	spin_lock_irqsave(&phba->hbalock, iflag);
10055 	spin_lock(&phba->sli4_hba.sgl_list_lock);
10056 	list_for_each_entry_safe(sglq_entry, sglq_next,
10057 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
10058 		if (sglq_entry->ndlp && sglq_entry->ndlp->vport == vport)
10059 			sglq_entry->ndlp = NULL;
10060 	}
10061 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
10062 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10063 	return;
10064 }
10065 
10066 /**
10067  * lpfc_sli4_els_xri_aborted - Slow-path process of els xri abort
10068  * @phba: pointer to lpfc hba data structure.
10069  * @axri: pointer to the els xri abort wcqe structure.
10070  *
10071  * This routine is invoked by the worker thread to process a SLI4 slow-path
10072  * ELS aborted xri.
10073  **/
10074 void
lpfc_sli4_els_xri_aborted(struct lpfc_hba * phba,struct sli4_wcqe_xri_aborted * axri)10075 lpfc_sli4_els_xri_aborted(struct lpfc_hba *phba,
10076 			  struct sli4_wcqe_xri_aborted *axri)
10077 {
10078 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
10079 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
10080 	uint16_t lxri = 0;
10081 
10082 	struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
10083 	unsigned long iflag = 0;
10084 	struct lpfc_nodelist *ndlp;
10085 	struct lpfc_sli_ring *pring;
10086 
10087 	pring = lpfc_phba_elsring(phba);
10088 
10089 	spin_lock_irqsave(&phba->hbalock, iflag);
10090 	spin_lock(&phba->sli4_hba.sgl_list_lock);
10091 	list_for_each_entry_safe(sglq_entry, sglq_next,
10092 			&phba->sli4_hba.lpfc_abts_els_sgl_list, list) {
10093 		if (sglq_entry->sli4_xritag == xri) {
10094 			list_del(&sglq_entry->list);
10095 			ndlp = sglq_entry->ndlp;
10096 			sglq_entry->ndlp = NULL;
10097 			list_add_tail(&sglq_entry->list,
10098 				&phba->sli4_hba.lpfc_els_sgl_list);
10099 			sglq_entry->state = SGL_FREED;
10100 			spin_unlock(&phba->sli4_hba.sgl_list_lock);
10101 			spin_unlock_irqrestore(&phba->hbalock, iflag);
10102 			lpfc_set_rrq_active(phba, ndlp,
10103 				sglq_entry->sli4_lxritag,
10104 				rxid, 1);
10105 
10106 			/* Check if TXQ queue needs to be serviced */
10107 			if (pring && !list_empty(&pring->txq))
10108 				lpfc_worker_wake_up(phba);
10109 			return;
10110 		}
10111 	}
10112 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
10113 	lxri = lpfc_sli4_xri_inrange(phba, xri);
10114 	if (lxri == NO_XRI) {
10115 		spin_unlock_irqrestore(&phba->hbalock, iflag);
10116 		return;
10117 	}
10118 	spin_lock(&phba->sli4_hba.sgl_list_lock);
10119 	sglq_entry = __lpfc_get_active_sglq(phba, lxri);
10120 	if (!sglq_entry || (sglq_entry->sli4_xritag != xri)) {
10121 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
10122 		spin_unlock_irqrestore(&phba->hbalock, iflag);
10123 		return;
10124 	}
10125 	sglq_entry->state = SGL_XRI_ABORTED;
10126 	spin_unlock(&phba->sli4_hba.sgl_list_lock);
10127 	spin_unlock_irqrestore(&phba->hbalock, iflag);
10128 	return;
10129 }
10130 
10131 /* lpfc_sli_abts_recover_port - Recover a port that failed a BLS_ABORT req.
10132  * @vport: pointer to virtual port object.
10133  * @ndlp: nodelist pointer for the impacted node.
10134  *
10135  * The driver calls this routine in response to an SLI4 XRI ABORT CQE
10136  * or an SLI3 ASYNC_STATUS_CN event from the port.  For either event,
10137  * the driver is required to send a LOGO to the remote node before it
10138  * attempts to recover its login to the remote node.
10139  */
10140 void
lpfc_sli_abts_recover_port(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)10141 lpfc_sli_abts_recover_port(struct lpfc_vport *vport,
10142 			   struct lpfc_nodelist *ndlp)
10143 {
10144 	struct Scsi_Host *shost;
10145 	struct lpfc_hba *phba;
10146 	unsigned long flags = 0;
10147 
10148 	shost = lpfc_shost_from_vport(vport);
10149 	phba = vport->phba;
10150 	if (ndlp->nlp_state != NLP_STE_MAPPED_NODE) {
10151 		lpfc_printf_log(phba, KERN_INFO,
10152 				LOG_SLI, "3093 No rport recovery needed. "
10153 				"rport in state 0x%x\n", ndlp->nlp_state);
10154 		return;
10155 	}
10156 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
10157 			"3094 Start rport recovery on shost id 0x%x "
10158 			"fc_id 0x%06x vpi 0x%x rpi 0x%x state 0x%x "
10159 			"flags 0x%x\n",
10160 			shost->host_no, ndlp->nlp_DID,
10161 			vport->vpi, ndlp->nlp_rpi, ndlp->nlp_state,
10162 			ndlp->nlp_flag);
10163 	/*
10164 	 * The rport is not responding.  Remove the FCP-2 flag to prevent
10165 	 * an ADISC in the follow-up recovery code.
10166 	 */
10167 	spin_lock_irqsave(shost->host_lock, flags);
10168 	ndlp->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
10169 	ndlp->nlp_flag |= NLP_ISSUE_LOGO;
10170 	spin_unlock_irqrestore(shost->host_lock, flags);
10171 	lpfc_unreg_rpi(vport, ndlp);
10172 }
10173 
10174