• 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) 2004-2008 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21 /* See Fibre Channel protocol T11 FC-LS for details */
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_transport_fc.h>
30 
31 #include "lpfc_hw.h"
32 #include "lpfc_sli.h"
33 #include "lpfc_nl.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
36 #include "lpfc.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_vport.h"
40 #include "lpfc_debugfs.h"
41 
42 static int lpfc_els_retry(struct lpfc_hba *, struct lpfc_iocbq *,
43 			  struct lpfc_iocbq *);
44 static void lpfc_cmpl_fabric_iocb(struct lpfc_hba *, struct lpfc_iocbq *,
45 			struct lpfc_iocbq *);
46 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport);
47 static int lpfc_issue_els_fdisc(struct lpfc_vport *vport,
48 				struct lpfc_nodelist *ndlp, uint8_t retry);
49 static int lpfc_issue_fabric_iocb(struct lpfc_hba *phba,
50 				  struct lpfc_iocbq *iocb);
51 static void lpfc_register_new_vport(struct lpfc_hba *phba,
52 				    struct lpfc_vport *vport,
53 				    struct lpfc_nodelist *ndlp);
54 
55 static int lpfc_max_els_tries = 3;
56 
57 /**
58  * lpfc_els_chk_latt: Check host link attention event for a vport.
59  * @vport: pointer to a host virtual N_Port data structure.
60  *
61  * This routine checks whether there is an outstanding host link
62  * attention event during the discovery process with the @vport. It is done
63  * by reading the HBA's Host Attention (HA) register. If there is any host
64  * link attention events during this @vport's discovery process, the @vport
65  * shall be marked as FC_ABORT_DISCOVERY, a host link attention clear shall
66  * be issued if the link state is not already in host link cleared state,
67  * and a return code shall indicate whether the host link attention event
68  * had happened.
69  *
70  * Note that, if either the host link is in state LPFC_LINK_DOWN or @vport
71  * state in LPFC_VPORT_READY, the request for checking host link attention
72  * event will be ignored and a return code shall indicate no host link
73  * attention event had happened.
74  *
75  * Return codes
76  *   0 - no host link attention event happened
77  *   1 - host link attention event happened
78  **/
79 int
lpfc_els_chk_latt(struct lpfc_vport * vport)80 lpfc_els_chk_latt(struct lpfc_vport *vport)
81 {
82 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
83 	struct lpfc_hba  *phba = vport->phba;
84 	uint32_t ha_copy;
85 
86 	if (vport->port_state >= LPFC_VPORT_READY ||
87 	    phba->link_state == LPFC_LINK_DOWN)
88 		return 0;
89 
90 	/* Read the HBA Host Attention Register */
91 	ha_copy = readl(phba->HAregaddr);
92 
93 	if (!(ha_copy & HA_LATT))
94 		return 0;
95 
96 	/* Pending Link Event during Discovery */
97 	lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
98 			 "0237 Pending Link Event during "
99 			 "Discovery: State x%x\n",
100 			 phba->pport->port_state);
101 
102 	/* CLEAR_LA should re-enable link attention events and
103 	 * we should then imediately take a LATT event. The
104 	 * LATT processing should call lpfc_linkdown() which
105 	 * will cleanup any left over in-progress discovery
106 	 * events.
107 	 */
108 	spin_lock_irq(shost->host_lock);
109 	vport->fc_flag |= FC_ABORT_DISCOVERY;
110 	spin_unlock_irq(shost->host_lock);
111 
112 	if (phba->link_state != LPFC_CLEAR_LA)
113 		lpfc_issue_clear_la(phba, vport);
114 
115 	return 1;
116 }
117 
118 /**
119  * lpfc_prep_els_iocb: Allocate and prepare a lpfc iocb data structure.
120  * @vport: pointer to a host virtual N_Port data structure.
121  * @expectRsp: flag indicating whether response is expected.
122  * @cmdSize: size of the ELS command.
123  * @retry: number of retries to the command IOCB when it fails.
124  * @ndlp: pointer to a node-list data structure.
125  * @did: destination identifier.
126  * @elscmd: the ELS command code.
127  *
128  * This routine is used for allocating a lpfc-IOCB data structure from
129  * the driver lpfc-IOCB free-list and prepare the IOCB with the parameters
130  * passed into the routine for discovery state machine to issue an Extended
131  * Link Service (ELS) commands. It is a generic lpfc-IOCB allocation
132  * and preparation routine that is used by all the discovery state machine
133  * routines and the ELS command-specific fields will be later set up by
134  * the individual discovery machine routines after calling this routine
135  * allocating and preparing a generic IOCB data structure. It fills in the
136  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
137  * payload and response payload (if expected). The reference count on the
138  * ndlp is incremented by 1 and the reference to the ndlp is put into
139  * context1 of the IOCB data structure for this IOCB to hold the ndlp
140  * reference for the command's callback function to access later.
141  *
142  * Return code
143  *   Pointer to the newly allocated/prepared els iocb data structure
144  *   NULL - when els iocb data structure allocation/preparation failed
145  **/
146 static 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)147 lpfc_prep_els_iocb(struct lpfc_vport *vport, uint8_t expectRsp,
148 		   uint16_t cmdSize, uint8_t retry,
149 		   struct lpfc_nodelist *ndlp, uint32_t did,
150 		   uint32_t elscmd)
151 {
152 	struct lpfc_hba  *phba = vport->phba;
153 	struct lpfc_iocbq *elsiocb;
154 	struct lpfc_dmabuf *pcmd, *prsp, *pbuflist;
155 	struct ulp_bde64 *bpl;
156 	IOCB_t *icmd;
157 
158 
159 	if (!lpfc_is_link_up(phba))
160 		return NULL;
161 
162 	/* Allocate buffer for  command iocb */
163 	elsiocb = lpfc_sli_get_iocbq(phba);
164 
165 	if (elsiocb == NULL)
166 		return NULL;
167 
168 	icmd = &elsiocb->iocb;
169 
170 	/* fill in BDEs for command */
171 	/* Allocate buffer for command payload */
172 	pcmd = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
173 	if (pcmd)
174 		pcmd->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &pcmd->phys);
175 	if (!pcmd || !pcmd->virt)
176 		goto els_iocb_free_pcmb_exit;
177 
178 	INIT_LIST_HEAD(&pcmd->list);
179 
180 	/* Allocate buffer for response payload */
181 	if (expectRsp) {
182 		prsp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
183 		if (prsp)
184 			prsp->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
185 						     &prsp->phys);
186 		if (!prsp || !prsp->virt)
187 			goto els_iocb_free_prsp_exit;
188 		INIT_LIST_HEAD(&prsp->list);
189 	} else
190 		prsp = NULL;
191 
192 	/* Allocate buffer for Buffer ptr list */
193 	pbuflist = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
194 	if (pbuflist)
195 		pbuflist->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
196 						 &pbuflist->phys);
197 	if (!pbuflist || !pbuflist->virt)
198 		goto els_iocb_free_pbuf_exit;
199 
200 	INIT_LIST_HEAD(&pbuflist->list);
201 
202 	icmd->un.elsreq64.bdl.addrHigh = putPaddrHigh(pbuflist->phys);
203 	icmd->un.elsreq64.bdl.addrLow = putPaddrLow(pbuflist->phys);
204 	icmd->un.elsreq64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
205 	icmd->un.elsreq64.remoteID = did;	/* DID */
206 	if (expectRsp) {
207 		icmd->un.elsreq64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
208 		icmd->ulpCommand = CMD_ELS_REQUEST64_CR;
209 		icmd->ulpTimeout = phba->fc_ratov * 2;
210 	} else {
211 		icmd->un.elsreq64.bdl.bdeSize = sizeof(struct ulp_bde64);
212 		icmd->ulpCommand = CMD_XMIT_ELS_RSP64_CX;
213 	}
214 	icmd->ulpBdeCount = 1;
215 	icmd->ulpLe = 1;
216 	icmd->ulpClass = CLASS3;
217 
218 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
219 		icmd->un.elsreq64.myID = vport->fc_myDID;
220 
221 		/* For ELS_REQUEST64_CR, use the VPI by default */
222 		icmd->ulpContext = vport->vpi;
223 		icmd->ulpCt_h = 0;
224 		/* The CT field must be 0=INVALID_RPI for the ECHO cmd */
225 		if (elscmd == ELS_CMD_ECHO)
226 			icmd->ulpCt_l = 0; /* context = invalid RPI */
227 		else
228 			icmd->ulpCt_l = 1; /* context = VPI */
229 	}
230 
231 	bpl = (struct ulp_bde64 *) pbuflist->virt;
232 	bpl->addrLow = le32_to_cpu(putPaddrLow(pcmd->phys));
233 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pcmd->phys));
234 	bpl->tus.f.bdeSize = cmdSize;
235 	bpl->tus.f.bdeFlags = 0;
236 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
237 
238 	if (expectRsp) {
239 		bpl++;
240 		bpl->addrLow = le32_to_cpu(putPaddrLow(prsp->phys));
241 		bpl->addrHigh = le32_to_cpu(putPaddrHigh(prsp->phys));
242 		bpl->tus.f.bdeSize = FCELSSIZE;
243 		bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
244 		bpl->tus.w = le32_to_cpu(bpl->tus.w);
245 	}
246 
247 	/* prevent preparing iocb with NULL ndlp reference */
248 	elsiocb->context1 = lpfc_nlp_get(ndlp);
249 	if (!elsiocb->context1)
250 		goto els_iocb_free_pbuf_exit;
251 	elsiocb->context2 = pcmd;
252 	elsiocb->context3 = pbuflist;
253 	elsiocb->retry = retry;
254 	elsiocb->vport = vport;
255 	elsiocb->drvrTimeout = (phba->fc_ratov << 1) + LPFC_DRVR_TIMEOUT;
256 
257 	if (prsp) {
258 		list_add(&prsp->list, &pcmd->list);
259 	}
260 	if (expectRsp) {
261 		/* Xmit ELS command <elsCmd> to remote NPORT <did> */
262 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
263 				 "0116 Xmit ELS command x%x to remote "
264 				 "NPORT x%x I/O tag: x%x, port state: x%x\n",
265 				 elscmd, did, elsiocb->iotag,
266 				 vport->port_state);
267 	} else {
268 		/* Xmit ELS response <elsCmd> to remote NPORT <did> */
269 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
270 				 "0117 Xmit ELS response x%x to remote "
271 				 "NPORT x%x I/O tag: x%x, size: x%x\n",
272 				 elscmd, ndlp->nlp_DID, elsiocb->iotag,
273 				 cmdSize);
274 	}
275 	return elsiocb;
276 
277 els_iocb_free_pbuf_exit:
278 	if (expectRsp)
279 		lpfc_mbuf_free(phba, prsp->virt, prsp->phys);
280 	kfree(pbuflist);
281 
282 els_iocb_free_prsp_exit:
283 	lpfc_mbuf_free(phba, pcmd->virt, pcmd->phys);
284 	kfree(prsp);
285 
286 els_iocb_free_pcmb_exit:
287 	kfree(pcmd);
288 	lpfc_sli_release_iocbq(phba, elsiocb);
289 	return NULL;
290 }
291 
292 /**
293  * lpfc_issue_fabric_reglogin: Issue fabric registration login for a vport.
294  * @vport: pointer to a host virtual N_Port data structure.
295  *
296  * This routine issues a fabric registration login for a @vport. An
297  * active ndlp node with Fabric_DID must already exist for this @vport.
298  * The routine invokes two mailbox commands to carry out fabric registration
299  * login through the HBA firmware: the first mailbox command requests the
300  * HBA to perform link configuration for the @vport; and the second mailbox
301  * command requests the HBA to perform the actual fabric registration login
302  * with the @vport.
303  *
304  * Return code
305  *   0 - successfully issued fabric registration login for @vport
306  *   -ENXIO -- failed to issue fabric registration login for @vport
307  **/
308 static int
lpfc_issue_fabric_reglogin(struct lpfc_vport * vport)309 lpfc_issue_fabric_reglogin(struct lpfc_vport *vport)
310 {
311 	struct lpfc_hba  *phba = vport->phba;
312 	LPFC_MBOXQ_t *mbox;
313 	struct lpfc_dmabuf *mp;
314 	struct lpfc_nodelist *ndlp;
315 	struct serv_parm *sp;
316 	int rc;
317 	int err = 0;
318 
319 	sp = &phba->fc_fabparam;
320 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
321 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
322 		err = 1;
323 		goto fail;
324 	}
325 
326 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
327 	if (!mbox) {
328 		err = 2;
329 		goto fail;
330 	}
331 
332 	vport->port_state = LPFC_FABRIC_CFG_LINK;
333 	lpfc_config_link(phba, mbox);
334 	mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
335 	mbox->vport = vport;
336 
337 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
338 	if (rc == MBX_NOT_FINISHED) {
339 		err = 3;
340 		goto fail_free_mbox;
341 	}
342 
343 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
344 	if (!mbox) {
345 		err = 4;
346 		goto fail;
347 	}
348 	rc = lpfc_reg_login(phba, vport->vpi, Fabric_DID, (uint8_t *)sp, mbox,
349 			    0);
350 	if (rc) {
351 		err = 5;
352 		goto fail_free_mbox;
353 	}
354 
355 	mbox->mbox_cmpl = lpfc_mbx_cmpl_fabric_reg_login;
356 	mbox->vport = vport;
357 	/* increment the reference count on ndlp to hold reference
358 	 * for the callback routine.
359 	 */
360 	mbox->context2 = lpfc_nlp_get(ndlp);
361 
362 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
363 	if (rc == MBX_NOT_FINISHED) {
364 		err = 6;
365 		goto fail_issue_reg_login;
366 	}
367 
368 	return 0;
369 
370 fail_issue_reg_login:
371 	/* decrement the reference count on ndlp just incremented
372 	 * for the failed mbox command.
373 	 */
374 	lpfc_nlp_put(ndlp);
375 	mp = (struct lpfc_dmabuf *) mbox->context1;
376 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
377 	kfree(mp);
378 fail_free_mbox:
379 	mempool_free(mbox, phba->mbox_mem_pool);
380 
381 fail:
382 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
383 	lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
384 		"0249 Cannot issue Register Fabric login: Err %d\n", err);
385 	return -ENXIO;
386 }
387 
388 /**
389  * lpfc_cmpl_els_flogi_fabric: Completion function for flogi to a fabric port.
390  * @vport: pointer to a host virtual N_Port data structure.
391  * @ndlp: pointer to a node-list data structure.
392  * @sp: pointer to service parameter data structure.
393  * @irsp: pointer to the IOCB within the lpfc response IOCB.
394  *
395  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
396  * function to handle the completion of a Fabric Login (FLOGI) into a fabric
397  * port in a fabric topology. It properly sets up the parameters to the @ndlp
398  * from the IOCB response. It also check the newly assigned N_Port ID to the
399  * @vport against the previously assigned N_Port ID. If it is different from
400  * the previously assigned Destination ID (DID), the lpfc_unreg_rpi() routine
401  * is invoked on all the remaining nodes with the @vport to unregister the
402  * Remote Port Indicators (RPIs). Finally, the lpfc_issue_fabric_reglogin()
403  * is invoked to register login to the fabric.
404  *
405  * Return code
406  *   0 - Success (currently, always return 0)
407  **/
408 static int
lpfc_cmpl_els_flogi_fabric(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct serv_parm * sp,IOCB_t * irsp)409 lpfc_cmpl_els_flogi_fabric(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
410 			   struct serv_parm *sp, IOCB_t *irsp)
411 {
412 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
413 	struct lpfc_hba  *phba = vport->phba;
414 	struct lpfc_nodelist *np;
415 	struct lpfc_nodelist *next_np;
416 
417 	spin_lock_irq(shost->host_lock);
418 	vport->fc_flag |= FC_FABRIC;
419 	spin_unlock_irq(shost->host_lock);
420 
421 	phba->fc_edtov = be32_to_cpu(sp->cmn.e_d_tov);
422 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
423 		phba->fc_edtov = (phba->fc_edtov + 999999) / 1000000;
424 
425 	phba->fc_ratov = (be32_to_cpu(sp->cmn.w2.r_a_tov) + 999) / 1000;
426 
427 	if (phba->fc_topology == TOPOLOGY_LOOP) {
428 		spin_lock_irq(shost->host_lock);
429 		vport->fc_flag |= FC_PUBLIC_LOOP;
430 		spin_unlock_irq(shost->host_lock);
431 	} else {
432 		/*
433 		 * If we are a N-port connected to a Fabric, fixup sparam's so
434 		 * logins to devices on remote loops work.
435 		 */
436 		vport->fc_sparam.cmn.altBbCredit = 1;
437 	}
438 
439 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
440 	memcpy(&ndlp->nlp_portname, &sp->portName, sizeof(struct lpfc_name));
441 	memcpy(&ndlp->nlp_nodename, &sp->nodeName, sizeof(struct lpfc_name));
442 	ndlp->nlp_class_sup = 0;
443 	if (sp->cls1.classValid)
444 		ndlp->nlp_class_sup |= FC_COS_CLASS1;
445 	if (sp->cls2.classValid)
446 		ndlp->nlp_class_sup |= FC_COS_CLASS2;
447 	if (sp->cls3.classValid)
448 		ndlp->nlp_class_sup |= FC_COS_CLASS3;
449 	if (sp->cls4.classValid)
450 		ndlp->nlp_class_sup |= FC_COS_CLASS4;
451 	ndlp->nlp_maxframe = ((sp->cmn.bbRcvSizeMsb & 0x0F) << 8) |
452 				sp->cmn.bbRcvSizeLsb;
453 	memcpy(&phba->fc_fabparam, sp, sizeof(struct serv_parm));
454 
455 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
456 		if (sp->cmn.response_multiple_NPort) {
457 			lpfc_printf_vlog(vport, KERN_WARNING,
458 					 LOG_ELS | LOG_VPORT,
459 					 "1816 FLOGI NPIV supported, "
460 					 "response data 0x%x\n",
461 					 sp->cmn.response_multiple_NPort);
462 			phba->link_flag |= LS_NPIV_FAB_SUPPORTED;
463 		} else {
464 			/* Because we asked f/w for NPIV it still expects us
465 			to call reg_vnpid atleast for the physcial host */
466 			lpfc_printf_vlog(vport, KERN_WARNING,
467 					 LOG_ELS | LOG_VPORT,
468 					 "1817 Fabric does not support NPIV "
469 					 "- configuring single port mode.\n");
470 			phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
471 		}
472 	}
473 
474 	if ((vport->fc_prevDID != vport->fc_myDID) &&
475 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
476 
477 		/* If our NportID changed, we need to ensure all
478 		 * remaining NPORTs get unreg_login'ed.
479 		 */
480 		list_for_each_entry_safe(np, next_np,
481 					&vport->fc_nodes, nlp_listp) {
482 			if (!NLP_CHK_NODE_ACT(np))
483 				continue;
484 			if ((np->nlp_state != NLP_STE_NPR_NODE) ||
485 				   !(np->nlp_flag & NLP_NPR_ADISC))
486 				continue;
487 			spin_lock_irq(shost->host_lock);
488 			np->nlp_flag &= ~NLP_NPR_ADISC;
489 			spin_unlock_irq(shost->host_lock);
490 			lpfc_unreg_rpi(vport, np);
491 		}
492 		if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
493 			lpfc_mbx_unreg_vpi(vport);
494 			spin_lock_irq(shost->host_lock);
495 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
496 			spin_unlock_irq(shost->host_lock);
497 		}
498 	}
499 
500 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_REG_LOGIN_ISSUE);
501 
502 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED &&
503 	    vport->fc_flag & FC_VPORT_NEEDS_REG_VPI) {
504 		lpfc_register_new_vport(phba, vport, ndlp);
505 		return 0;
506 	}
507 	lpfc_issue_fabric_reglogin(vport);
508 	return 0;
509 }
510 
511 /**
512  * lpfc_cmpl_els_flogi_nport: Completion function for flogi to an N_Port.
513  * @vport: pointer to a host virtual N_Port data structure.
514  * @ndlp: pointer to a node-list data structure.
515  * @sp: pointer to service parameter data structure.
516  *
517  * This routine is invoked by the lpfc_cmpl_els_flogi() completion callback
518  * function to handle the completion of a Fabric Login (FLOGI) into an N_Port
519  * in a point-to-point topology. First, the @vport's N_Port Name is compared
520  * with the received N_Port Name: if the @vport's N_Port Name is greater than
521  * the received N_Port Name lexicographically, this node shall assign local
522  * N_Port ID (PT2PT_LocalID: 1) and remote N_Port ID (PT2PT_RemoteID: 2) and
523  * will send out Port Login (PLOGI) with the N_Port IDs assigned. Otherwise,
524  * this node shall just wait for the remote node to issue PLOGI and assign
525  * N_Port IDs.
526  *
527  * Return code
528  *   0 - Success
529  *   -ENXIO - Fail
530  **/
531 static int
lpfc_cmpl_els_flogi_nport(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,struct serv_parm * sp)532 lpfc_cmpl_els_flogi_nport(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
533 			  struct serv_parm *sp)
534 {
535 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
536 	struct lpfc_hba  *phba = vport->phba;
537 	LPFC_MBOXQ_t *mbox;
538 	int rc;
539 
540 	spin_lock_irq(shost->host_lock);
541 	vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
542 	spin_unlock_irq(shost->host_lock);
543 
544 	phba->fc_edtov = FF_DEF_EDTOV;
545 	phba->fc_ratov = FF_DEF_RATOV;
546 	rc = memcmp(&vport->fc_portname, &sp->portName,
547 		    sizeof(vport->fc_portname));
548 	if (rc >= 0) {
549 		/* This side will initiate the PLOGI */
550 		spin_lock_irq(shost->host_lock);
551 		vport->fc_flag |= FC_PT2PT_PLOGI;
552 		spin_unlock_irq(shost->host_lock);
553 
554 		/*
555 		 * N_Port ID cannot be 0, set our to LocalID the other
556 		 * side will be RemoteID.
557 		 */
558 
559 		/* not equal */
560 		if (rc)
561 			vport->fc_myDID = PT2PT_LocalID;
562 
563 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
564 		if (!mbox)
565 			goto fail;
566 
567 		lpfc_config_link(phba, mbox);
568 
569 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
570 		mbox->vport = vport;
571 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
572 		if (rc == MBX_NOT_FINISHED) {
573 			mempool_free(mbox, phba->mbox_mem_pool);
574 			goto fail;
575 		}
576 		/* Decrement ndlp reference count indicating that ndlp can be
577 		 * safely released when other references to it are done.
578 		 */
579 		lpfc_nlp_put(ndlp);
580 
581 		ndlp = lpfc_findnode_did(vport, PT2PT_RemoteID);
582 		if (!ndlp) {
583 			/*
584 			 * Cannot find existing Fabric ndlp, so allocate a
585 			 * new one
586 			 */
587 			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
588 			if (!ndlp)
589 				goto fail;
590 			lpfc_nlp_init(vport, ndlp, PT2PT_RemoteID);
591 		} else if (!NLP_CHK_NODE_ACT(ndlp)) {
592 			ndlp = lpfc_enable_node(vport, ndlp,
593 						NLP_STE_UNUSED_NODE);
594 			if(!ndlp)
595 				goto fail;
596 		}
597 
598 		memcpy(&ndlp->nlp_portname, &sp->portName,
599 		       sizeof(struct lpfc_name));
600 		memcpy(&ndlp->nlp_nodename, &sp->nodeName,
601 		       sizeof(struct lpfc_name));
602 		/* Set state will put ndlp onto node list if not already done */
603 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
604 		spin_lock_irq(shost->host_lock);
605 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
606 		spin_unlock_irq(shost->host_lock);
607 	} else
608 		/* This side will wait for the PLOGI, decrement ndlp reference
609 		 * count indicating that ndlp can be released when other
610 		 * references to it are done.
611 		 */
612 		lpfc_nlp_put(ndlp);
613 
614 	/* If we are pt2pt with another NPort, force NPIV off! */
615 	phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
616 
617 	spin_lock_irq(shost->host_lock);
618 	vport->fc_flag |= FC_PT2PT;
619 	spin_unlock_irq(shost->host_lock);
620 
621 	/* Start discovery - this should just do CLEAR_LA */
622 	lpfc_disc_start(vport);
623 	return 0;
624 fail:
625 	return -ENXIO;
626 }
627 
628 /**
629  * lpfc_cmpl_els_flogi: Completion callback function for flogi.
630  * @phba: pointer to lpfc hba data structure.
631  * @cmdiocb: pointer to lpfc command iocb data structure.
632  * @rspiocb: pointer to lpfc response iocb data structure.
633  *
634  * This routine is the top-level completion callback function for issuing
635  * a Fabric Login (FLOGI) command. If the response IOCB reported error,
636  * the lpfc_els_retry() routine shall be invoked to retry the FLOGI. If
637  * retry has been made (either immediately or delayed with lpfc_els_retry()
638  * returning 1), the command IOCB will be released and function returned.
639  * If the retry attempt has been given up (possibly reach the maximum
640  * number of retries), one additional decrement of ndlp reference shall be
641  * invoked before going out after releasing the command IOCB. This will
642  * actually release the remote node (Note, lpfc_els_free_iocb() will also
643  * invoke one decrement of ndlp reference count). If no error reported in
644  * the IOCB status, the command Port ID field is used to determine whether
645  * this is a point-to-point topology or a fabric topology: if the Port ID
646  * field is assigned, it is a fabric topology; otherwise, it is a
647  * point-to-point topology. The routine lpfc_cmpl_els_flogi_fabric() or
648  * lpfc_cmpl_els_flogi_nport() shall be invoked accordingly to handle the
649  * specific topology completion conditions.
650  **/
651 static void
lpfc_cmpl_els_flogi(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)652 lpfc_cmpl_els_flogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
653 		    struct lpfc_iocbq *rspiocb)
654 {
655 	struct lpfc_vport *vport = cmdiocb->vport;
656 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
657 	IOCB_t *irsp = &rspiocb->iocb;
658 	struct lpfc_nodelist *ndlp = cmdiocb->context1;
659 	struct lpfc_dmabuf *pcmd = cmdiocb->context2, *prsp;
660 	struct serv_parm *sp;
661 	int rc;
662 
663 	/* Check to see if link went down during discovery */
664 	if (lpfc_els_chk_latt(vport)) {
665 		/* One additional decrement on node reference count to
666 		 * trigger the release of the node
667 		 */
668 		lpfc_nlp_put(ndlp);
669 		goto out;
670 	}
671 
672 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
673 		"FLOGI cmpl:      status:x%x/x%x state:x%x",
674 		irsp->ulpStatus, irsp->un.ulpWord[4],
675 		vport->port_state);
676 
677 	if (irsp->ulpStatus) {
678 		/* Check for retry */
679 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
680 			goto out;
681 
682 		/* FLOGI failed, so there is no fabric */
683 		spin_lock_irq(shost->host_lock);
684 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
685 		spin_unlock_irq(shost->host_lock);
686 
687 		/* If private loop, then allow max outstanding els to be
688 		 * LPFC_MAX_DISC_THREADS (32). Scanning in the case of no
689 		 * alpa map would take too long otherwise.
690 		 */
691 		if (phba->alpa_map[0] == 0) {
692 			vport->cfg_discovery_threads = LPFC_MAX_DISC_THREADS;
693 		}
694 
695 		/* FLOGI failure */
696 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
697 				 "0100 FLOGI failure Data: x%x x%x "
698 				 "x%x\n",
699 				 irsp->ulpStatus, irsp->un.ulpWord[4],
700 				 irsp->ulpTimeout);
701 		goto flogifail;
702 	}
703 
704 	/*
705 	 * The FLogI succeeded.  Sync the data for the CPU before
706 	 * accessing it.
707 	 */
708 	prsp = list_get_first(&pcmd->list, struct lpfc_dmabuf, list);
709 
710 	sp = prsp->virt + sizeof(uint32_t);
711 
712 	/* FLOGI completes successfully */
713 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
714 			 "0101 FLOGI completes sucessfully "
715 			 "Data: x%x x%x x%x x%x\n",
716 			 irsp->un.ulpWord[4], sp->cmn.e_d_tov,
717 			 sp->cmn.w2.r_a_tov, sp->cmn.edtovResolution);
718 
719 	if (vport->port_state == LPFC_FLOGI) {
720 		/*
721 		 * If Common Service Parameters indicate Nport
722 		 * we are point to point, if Fport we are Fabric.
723 		 */
724 		if (sp->cmn.fPort)
725 			rc = lpfc_cmpl_els_flogi_fabric(vport, ndlp, sp, irsp);
726 		else
727 			rc = lpfc_cmpl_els_flogi_nport(vport, ndlp, sp);
728 
729 		if (!rc)
730 			goto out;
731 	}
732 
733 flogifail:
734 	lpfc_nlp_put(ndlp);
735 
736 	if (!lpfc_error_lost_link(irsp)) {
737 		/* FLOGI failed, so just use loop map to make discovery list */
738 		lpfc_disc_list_loopmap(vport);
739 
740 		/* Start discovery */
741 		lpfc_disc_start(vport);
742 	} else if (((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
743 			((irsp->un.ulpWord[4] != IOERR_SLI_ABORTED) &&
744 			(irsp->un.ulpWord[4] != IOERR_SLI_DOWN))) &&
745 			(phba->link_state != LPFC_CLEAR_LA)) {
746 		/* If FLOGI failed enable link interrupt. */
747 		lpfc_issue_clear_la(phba, vport);
748 	}
749 out:
750 	lpfc_els_free_iocb(phba, cmdiocb);
751 }
752 
753 /**
754  * lpfc_issue_els_flogi: Issue an flogi iocb command for a vport.
755  * @vport: pointer to a host virtual N_Port data structure.
756  * @ndlp: pointer to a node-list data structure.
757  * @retry: number of retries to the command IOCB.
758  *
759  * This routine issues a Fabric Login (FLOGI) Request ELS command
760  * for a @vport. The initiator service parameters are put into the payload
761  * of the FLOGI Request IOCB and the top-level callback function pointer
762  * to lpfc_cmpl_els_flogi() routine is put to the IOCB completion callback
763  * function field. The lpfc_issue_fabric_iocb routine is invoked to send
764  * out FLOGI ELS command with one outstanding fabric IOCB at a time.
765  *
766  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
767  * will be incremented by 1 for holding the ndlp and the reference to ndlp
768  * will be stored into the context1 field of the IOCB for the completion
769  * callback function to the FLOGI ELS command.
770  *
771  * Return code
772  *   0 - successfully issued flogi iocb for @vport
773  *   1 - failed to issue flogi iocb for @vport
774  **/
775 static int
lpfc_issue_els_flogi(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)776 lpfc_issue_els_flogi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
777 		     uint8_t retry)
778 {
779 	struct lpfc_hba  *phba = vport->phba;
780 	struct serv_parm *sp;
781 	IOCB_t *icmd;
782 	struct lpfc_iocbq *elsiocb;
783 	struct lpfc_sli_ring *pring;
784 	uint8_t *pcmd;
785 	uint16_t cmdsize;
786 	uint32_t tmo;
787 	int rc;
788 
789 	pring = &phba->sli.ring[LPFC_ELS_RING];
790 
791 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
792 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
793 				     ndlp->nlp_DID, ELS_CMD_FLOGI);
794 
795 	if (!elsiocb)
796 		return 1;
797 
798 	icmd = &elsiocb->iocb;
799 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
800 
801 	/* For FLOGI request, remainder of payload is service parameters */
802 	*((uint32_t *) (pcmd)) = ELS_CMD_FLOGI;
803 	pcmd += sizeof(uint32_t);
804 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
805 	sp = (struct serv_parm *) pcmd;
806 
807 	/* Setup CSPs accordingly for Fabric */
808 	sp->cmn.e_d_tov = 0;
809 	sp->cmn.w2.r_a_tov = 0;
810 	sp->cls1.classValid = 0;
811 	sp->cls2.seqDelivery = 1;
812 	sp->cls3.seqDelivery = 1;
813 	if (sp->cmn.fcphLow < FC_PH3)
814 		sp->cmn.fcphLow = FC_PH3;
815 	if (sp->cmn.fcphHigh < FC_PH3)
816 		sp->cmn.fcphHigh = FC_PH3;
817 
818 	if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) {
819 		sp->cmn.request_multiple_Nport = 1;
820 
821 		/* For FLOGI, Let FLOGI rsp set the NPortID for VPI 0 */
822 		icmd->ulpCt_h = 1;
823 		icmd->ulpCt_l = 0;
824 	}
825 
826 	if (phba->fc_topology != TOPOLOGY_LOOP) {
827 		icmd->un.elsreq64.myID = 0;
828 		icmd->un.elsreq64.fl = 1;
829 	}
830 
831 	tmo = phba->fc_ratov;
832 	phba->fc_ratov = LPFC_DISC_FLOGI_TMO;
833 	lpfc_set_disctmo(vport);
834 	phba->fc_ratov = tmo;
835 
836 	phba->fc_stat.elsXmitFLOGI++;
837 	elsiocb->iocb_cmpl = lpfc_cmpl_els_flogi;
838 
839 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
840 		"Issue FLOGI:     opt:x%x",
841 		phba->sli3_options, 0, 0);
842 
843 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
844 	if (rc == IOCB_ERROR) {
845 		lpfc_els_free_iocb(phba, elsiocb);
846 		return 1;
847 	}
848 	return 0;
849 }
850 
851 /**
852  * lpfc_els_abort_flogi: Abort all outstanding flogi iocbs.
853  * @phba: pointer to lpfc hba data structure.
854  *
855  * This routine aborts all the outstanding Fabric Login (FLOGI) IOCBs
856  * with a @phba. This routine walks all the outstanding IOCBs on the txcmplq
857  * list and issues an abort IOCB commond on each outstanding IOCB that
858  * contains a active Fabric_DID ndlp. Note that this function is to issue
859  * the abort IOCB command on all the outstanding IOCBs, thus when this
860  * function returns, it does not guarantee all the IOCBs are actually aborted.
861  *
862  * Return code
863  *   0 - Sucessfully issued abort iocb on all outstanding flogis (Always 0)
864  **/
865 int
lpfc_els_abort_flogi(struct lpfc_hba * phba)866 lpfc_els_abort_flogi(struct lpfc_hba *phba)
867 {
868 	struct lpfc_sli_ring *pring;
869 	struct lpfc_iocbq *iocb, *next_iocb;
870 	struct lpfc_nodelist *ndlp;
871 	IOCB_t *icmd;
872 
873 	/* Abort outstanding I/O on NPort <nlp_DID> */
874 	lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY,
875 			"0201 Abort outstanding I/O on NPort x%x\n",
876 			Fabric_DID);
877 
878 	pring = &phba->sli.ring[LPFC_ELS_RING];
879 
880 	/*
881 	 * Check the txcmplq for an iocb that matches the nport the driver is
882 	 * searching for.
883 	 */
884 	spin_lock_irq(&phba->hbalock);
885 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
886 		icmd = &iocb->iocb;
887 		if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR &&
888 		    icmd->un.elsreq64.bdl.ulpIoTag32) {
889 			ndlp = (struct lpfc_nodelist *)(iocb->context1);
890 			if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
891 			    (ndlp->nlp_DID == Fabric_DID))
892 				lpfc_sli_issue_abort_iotag(phba, pring, iocb);
893 		}
894 	}
895 	spin_unlock_irq(&phba->hbalock);
896 
897 	return 0;
898 }
899 
900 /**
901  * lpfc_initial_flogi: Issue an initial fabric login for a vport.
902  * @vport: pointer to a host virtual N_Port data structure.
903  *
904  * This routine issues an initial Fabric Login (FLOGI) for the @vport
905  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
906  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
907  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
908  * it will just be enabled and made active. The lpfc_issue_els_flogi() routine
909  * is then invoked with the @vport and the ndlp to perform the FLOGI for the
910  * @vport.
911  *
912  * Return code
913  *   0 - failed to issue initial flogi for @vport
914  *   1 - successfully issued initial flogi for @vport
915  **/
916 int
lpfc_initial_flogi(struct lpfc_vport * vport)917 lpfc_initial_flogi(struct lpfc_vport *vport)
918 {
919 	struct lpfc_hba *phba = vport->phba;
920 	struct lpfc_nodelist *ndlp;
921 
922 	vport->port_state = LPFC_FLOGI;
923 	lpfc_set_disctmo(vport);
924 
925 	/* First look for the Fabric ndlp */
926 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
927 	if (!ndlp) {
928 		/* Cannot find existing Fabric ndlp, so allocate a new one */
929 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
930 		if (!ndlp)
931 			return 0;
932 		lpfc_nlp_init(vport, ndlp, Fabric_DID);
933 		/* Put ndlp onto node list */
934 		lpfc_enqueue_node(vport, ndlp);
935 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
936 		/* re-setup ndlp without removing from node list */
937 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
938 		if (!ndlp)
939 			return 0;
940 	}
941 
942 	if (lpfc_issue_els_flogi(vport, ndlp, 0))
943 		/* This decrement of reference count to node shall kick off
944 		 * the release of the node.
945 		 */
946 		lpfc_nlp_put(ndlp);
947 
948 	return 1;
949 }
950 
951 /**
952  * lpfc_initial_fdisc: Issue an initial fabric discovery for a vport.
953  * @vport: pointer to a host virtual N_Port data structure.
954  *
955  * This routine issues an initial Fabric Discover (FDISC) for the @vport
956  * specified. It first searches the ndlp with the Fabric_DID (0xfffffe) from
957  * the @vport's ndlp list. If no such ndlp found, it will create an ndlp and
958  * put it into the @vport's ndlp list. If an inactive ndlp found on the list,
959  * it will just be enabled and made active. The lpfc_issue_els_fdisc() routine
960  * is then invoked with the @vport and the ndlp to perform the FDISC for the
961  * @vport.
962  *
963  * Return code
964  *   0 - failed to issue initial fdisc for @vport
965  *   1 - successfully issued initial fdisc for @vport
966  **/
967 int
lpfc_initial_fdisc(struct lpfc_vport * vport)968 lpfc_initial_fdisc(struct lpfc_vport *vport)
969 {
970 	struct lpfc_hba *phba = vport->phba;
971 	struct lpfc_nodelist *ndlp;
972 
973 	/* First look for the Fabric ndlp */
974 	ndlp = lpfc_findnode_did(vport, Fabric_DID);
975 	if (!ndlp) {
976 		/* Cannot find existing Fabric ndlp, so allocate a new one */
977 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
978 		if (!ndlp)
979 			return 0;
980 		lpfc_nlp_init(vport, ndlp, Fabric_DID);
981 		/* Put ndlp onto node list */
982 		lpfc_enqueue_node(vport, ndlp);
983 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
984 		/* re-setup ndlp without removing from node list */
985 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
986 		if (!ndlp)
987 			return 0;
988 	}
989 
990 	if (lpfc_issue_els_fdisc(vport, ndlp, 0)) {
991 		/* decrement node reference count to trigger the release of
992 		 * the node.
993 		 */
994 		lpfc_nlp_put(ndlp);
995 		return 0;
996 	}
997 	return 1;
998 }
999 
1000 /**
1001  * lpfc_more_plogi: Check and issue remaining plogis for a vport.
1002  * @vport: pointer to a host virtual N_Port data structure.
1003  *
1004  * This routine checks whether there are more remaining Port Logins
1005  * (PLOGI) to be issued for the @vport. If so, it will invoke the routine
1006  * lpfc_els_disc_plogi() to go through the Node Port Recovery (NPR) nodes
1007  * to issue ELS PLOGIs up to the configured discover threads with the
1008  * @vport (@vport->cfg_discovery_threads). The function also decrement
1009  * the @vport's num_disc_node by 1 if it is not already 0.
1010  **/
1011 void
lpfc_more_plogi(struct lpfc_vport * vport)1012 lpfc_more_plogi(struct lpfc_vport *vport)
1013 {
1014 	int sentplogi;
1015 
1016 	if (vport->num_disc_nodes)
1017 		vport->num_disc_nodes--;
1018 
1019 	/* Continue discovery with <num_disc_nodes> PLOGIs to go */
1020 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1021 			 "0232 Continue discovery with %d PLOGIs to go "
1022 			 "Data: x%x x%x x%x\n",
1023 			 vport->num_disc_nodes, vport->fc_plogi_cnt,
1024 			 vport->fc_flag, vport->port_state);
1025 	/* Check to see if there are more PLOGIs to be sent */
1026 	if (vport->fc_flag & FC_NLP_MORE)
1027 		/* go thru NPR nodes and issue any remaining ELS PLOGIs */
1028 		sentplogi = lpfc_els_disc_plogi(vport);
1029 
1030 	return;
1031 }
1032 
1033 /**
1034  * lpfc_plogi_confirm_nport: Confirm pologi wwpn matches stored ndlp.
1035  * @phba: pointer to lpfc hba data structure.
1036  * @prsp: pointer to response IOCB payload.
1037  * @ndlp: pointer to a node-list data structure.
1038  *
1039  * This routine checks and indicates whether the WWPN of an N_Port, retrieved
1040  * from a PLOGI, matches the WWPN that is stored in the @ndlp for that N_POrt.
1041  * The following cases are considered N_Port confirmed:
1042  * 1) The N_Port is a Fabric ndlp; 2) The @ndlp is on vport list and matches
1043  * the WWPN of the N_Port logged into; 3) The @ndlp is not on vport list but
1044  * it does not have WWPN assigned either. If the WWPN is confirmed, the
1045  * pointer to the @ndlp will be returned. If the WWPN is not confirmed:
1046  * 1) if there is a node on vport list other than the @ndlp with the same
1047  * WWPN of the N_Port PLOGI logged into, the lpfc_unreg_rpi() will be invoked
1048  * on that node to release the RPI associated with the node; 2) if there is
1049  * no node found on vport list with the same WWPN of the N_Port PLOGI logged
1050  * into, a new node shall be allocated (or activated). In either case, the
1051  * parameters of the @ndlp shall be copied to the new_ndlp, the @ndlp shall
1052  * be released and the new_ndlp shall be put on to the vport node list and
1053  * its pointer returned as the confirmed node.
1054  *
1055  * Note that before the @ndlp got "released", the keepDID from not-matching
1056  * or inactive "new_ndlp" on the vport node list is assigned to the nlp_DID
1057  * of the @ndlp. This is because the release of @ndlp is actually to put it
1058  * into an inactive state on the vport node list and the vport node list
1059  * management algorithm does not allow two node with a same DID.
1060  *
1061  * Return code
1062  *   pointer to the PLOGI N_Port @ndlp
1063  **/
1064 static struct lpfc_nodelist *
lpfc_plogi_confirm_nport(struct lpfc_hba * phba,uint32_t * prsp,struct lpfc_nodelist * ndlp)1065 lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
1066 			 struct lpfc_nodelist *ndlp)
1067 {
1068 	struct lpfc_vport    *vport = ndlp->vport;
1069 	struct lpfc_nodelist *new_ndlp;
1070 	struct lpfc_rport_data *rdata;
1071 	struct fc_rport *rport;
1072 	struct serv_parm *sp;
1073 	uint8_t  name[sizeof(struct lpfc_name)];
1074 	uint32_t rc, keepDID = 0;
1075 
1076 	/* Fabric nodes can have the same WWPN so we don't bother searching
1077 	 * by WWPN.  Just return the ndlp that was given to us.
1078 	 */
1079 	if (ndlp->nlp_type & NLP_FABRIC)
1080 		return ndlp;
1081 
1082 	sp = (struct serv_parm *) ((uint8_t *) prsp + sizeof(uint32_t));
1083 	memset(name, 0, sizeof(struct lpfc_name));
1084 
1085 	/* Now we find out if the NPort we are logging into, matches the WWPN
1086 	 * we have for that ndlp. If not, we have some work to do.
1087 	 */
1088 	new_ndlp = lpfc_findnode_wwpn(vport, &sp->portName);
1089 
1090 	if (new_ndlp == ndlp && NLP_CHK_NODE_ACT(new_ndlp))
1091 		return ndlp;
1092 
1093 	if (!new_ndlp) {
1094 		rc = memcmp(&ndlp->nlp_portname, name,
1095 			    sizeof(struct lpfc_name));
1096 		if (!rc)
1097 			return ndlp;
1098 		new_ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_ATOMIC);
1099 		if (!new_ndlp)
1100 			return ndlp;
1101 		lpfc_nlp_init(vport, new_ndlp, ndlp->nlp_DID);
1102 	} else if (!NLP_CHK_NODE_ACT(new_ndlp)) {
1103 		rc = memcmp(&ndlp->nlp_portname, name,
1104 			    sizeof(struct lpfc_name));
1105 		if (!rc)
1106 			return ndlp;
1107 		new_ndlp = lpfc_enable_node(vport, new_ndlp,
1108 						NLP_STE_UNUSED_NODE);
1109 		if (!new_ndlp)
1110 			return ndlp;
1111 		keepDID = new_ndlp->nlp_DID;
1112 	} else
1113 		keepDID = new_ndlp->nlp_DID;
1114 
1115 	lpfc_unreg_rpi(vport, new_ndlp);
1116 	new_ndlp->nlp_DID = ndlp->nlp_DID;
1117 	new_ndlp->nlp_prev_state = ndlp->nlp_prev_state;
1118 
1119 	if (ndlp->nlp_flag & NLP_NPR_2B_DISC)
1120 		new_ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1121 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1122 
1123 	/* Set state will put new_ndlp on to node list if not already done */
1124 	lpfc_nlp_set_state(vport, new_ndlp, ndlp->nlp_state);
1125 
1126 	/* Move this back to NPR state */
1127 	if (memcmp(&ndlp->nlp_portname, name, sizeof(struct lpfc_name)) == 0) {
1128 		/* The new_ndlp is replacing ndlp totally, so we need
1129 		 * to put ndlp on UNUSED list and try to free it.
1130 		 */
1131 
1132 		/* Fix up the rport accordingly */
1133 		rport =  ndlp->rport;
1134 		if (rport) {
1135 			rdata = rport->dd_data;
1136 			if (rdata->pnode == ndlp) {
1137 				lpfc_nlp_put(ndlp);
1138 				ndlp->rport = NULL;
1139 				rdata->pnode = lpfc_nlp_get(new_ndlp);
1140 				new_ndlp->rport = rport;
1141 			}
1142 			new_ndlp->nlp_type = ndlp->nlp_type;
1143 		}
1144 		/* We shall actually free the ndlp with both nlp_DID and
1145 		 * nlp_portname fields equals 0 to avoid any ndlp on the
1146 		 * nodelist never to be used.
1147 		 */
1148 		if (ndlp->nlp_DID == 0) {
1149 			spin_lock_irq(&phba->ndlp_lock);
1150 			NLP_SET_FREE_REQ(ndlp);
1151 			spin_unlock_irq(&phba->ndlp_lock);
1152 		}
1153 
1154 		/* Two ndlps cannot have the same did on the nodelist */
1155 		ndlp->nlp_DID = keepDID;
1156 		lpfc_drop_node(vport, ndlp);
1157 	}
1158 	else {
1159 		lpfc_unreg_rpi(vport, ndlp);
1160 		/* Two ndlps cannot have the same did */
1161 		ndlp->nlp_DID = keepDID;
1162 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1163 	}
1164 	return new_ndlp;
1165 }
1166 
1167 /**
1168  * lpfc_end_rscn: Check and handle more rscn for a vport.
1169  * @vport: pointer to a host virtual N_Port data structure.
1170  *
1171  * This routine checks whether more Registration State Change
1172  * Notifications (RSCNs) came in while the discovery state machine was in
1173  * the FC_RSCN_MODE. If so, the lpfc_els_handle_rscn() routine will be
1174  * invoked to handle the additional RSCNs for the @vport. Otherwise, the
1175  * FC_RSCN_MODE bit will be cleared with the @vport to mark as the end of
1176  * handling the RSCNs.
1177  **/
1178 void
lpfc_end_rscn(struct lpfc_vport * vport)1179 lpfc_end_rscn(struct lpfc_vport *vport)
1180 {
1181 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1182 
1183 	if (vport->fc_flag & FC_RSCN_MODE) {
1184 		/*
1185 		 * Check to see if more RSCNs came in while we were
1186 		 * processing this one.
1187 		 */
1188 		if (vport->fc_rscn_id_cnt ||
1189 		    (vport->fc_flag & FC_RSCN_DISCOVERY) != 0)
1190 			lpfc_els_handle_rscn(vport);
1191 		else {
1192 			spin_lock_irq(shost->host_lock);
1193 			vport->fc_flag &= ~FC_RSCN_MODE;
1194 			spin_unlock_irq(shost->host_lock);
1195 		}
1196 	}
1197 }
1198 
1199 /**
1200  * lpfc_cmpl_els_plogi: Completion callback function for plogi.
1201  * @phba: pointer to lpfc hba data structure.
1202  * @cmdiocb: pointer to lpfc command iocb data structure.
1203  * @rspiocb: pointer to lpfc response iocb data structure.
1204  *
1205  * This routine is the completion callback function for issuing the Port
1206  * Login (PLOGI) command. For PLOGI completion, there must be an active
1207  * ndlp on the vport node list that matches the remote node ID from the
1208  * PLOGI reponse IOCB. If such ndlp does not exist, the PLOGI is simply
1209  * ignored and command IOCB released. The PLOGI response IOCB status is
1210  * checked for error conditons. If there is error status reported, PLOGI
1211  * retry shall be attempted by invoking the lpfc_els_retry() routine.
1212  * Otherwise, the lpfc_plogi_confirm_nport() routine shall be invoked on
1213  * the ndlp and the NLP_EVT_CMPL_PLOGI state to the Discover State Machine
1214  * (DSM) is set for this PLOGI completion. Finally, it checks whether
1215  * there are additional N_Port nodes with the vport that need to perform
1216  * PLOGI. If so, the lpfc_more_plogi() routine is invoked to issue addition
1217  * PLOGIs.
1218  **/
1219 static void
lpfc_cmpl_els_plogi(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1220 lpfc_cmpl_els_plogi(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1221 		    struct lpfc_iocbq *rspiocb)
1222 {
1223 	struct lpfc_vport *vport = cmdiocb->vport;
1224 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1225 	IOCB_t *irsp;
1226 	struct lpfc_nodelist *ndlp;
1227 	struct lpfc_dmabuf *prsp;
1228 	int disc, rc, did, type;
1229 
1230 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1231 	cmdiocb->context_un.rsp_iocb = rspiocb;
1232 
1233 	irsp = &rspiocb->iocb;
1234 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1235 		"PLOGI cmpl:      status:x%x/x%x did:x%x",
1236 		irsp->ulpStatus, irsp->un.ulpWord[4],
1237 		irsp->un.elsreq64.remoteID);
1238 
1239 	ndlp = lpfc_findnode_did(vport, irsp->un.elsreq64.remoteID);
1240 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
1241 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
1242 				 "0136 PLOGI completes to NPort x%x "
1243 				 "with no ndlp. Data: x%x x%x x%x\n",
1244 				 irsp->un.elsreq64.remoteID,
1245 				 irsp->ulpStatus, irsp->un.ulpWord[4],
1246 				 irsp->ulpIoTag);
1247 		goto out;
1248 	}
1249 
1250 	/* Since ndlp can be freed in the disc state machine, note if this node
1251 	 * is being used during discovery.
1252 	 */
1253 	spin_lock_irq(shost->host_lock);
1254 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1255 	ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
1256 	spin_unlock_irq(shost->host_lock);
1257 	rc   = 0;
1258 
1259 	/* PLOGI completes to NPort <nlp_DID> */
1260 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1261 			 "0102 PLOGI completes to NPort x%x "
1262 			 "Data: x%x x%x x%x x%x x%x\n",
1263 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1264 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1265 	/* Check to see if link went down during discovery */
1266 	if (lpfc_els_chk_latt(vport)) {
1267 		spin_lock_irq(shost->host_lock);
1268 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1269 		spin_unlock_irq(shost->host_lock);
1270 		goto out;
1271 	}
1272 
1273 	/* ndlp could be freed in DSM, save these values now */
1274 	type = ndlp->nlp_type;
1275 	did = ndlp->nlp_DID;
1276 
1277 	if (irsp->ulpStatus) {
1278 		/* Check for retry */
1279 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1280 			/* ELS command is being retried */
1281 			if (disc) {
1282 				spin_lock_irq(shost->host_lock);
1283 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1284 				spin_unlock_irq(shost->host_lock);
1285 			}
1286 			goto out;
1287 		}
1288 		/* PLOGI failed */
1289 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1290 		if (lpfc_error_lost_link(irsp))
1291 			rc = NLP_STE_FREED_NODE;
1292 		else
1293 			rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1294 						     NLP_EVT_CMPL_PLOGI);
1295 	} else {
1296 		/* Good status, call state machine */
1297 		prsp = list_entry(((struct lpfc_dmabuf *)
1298 				   cmdiocb->context2)->list.next,
1299 				  struct lpfc_dmabuf, list);
1300 		ndlp = lpfc_plogi_confirm_nport(phba, prsp->virt, ndlp);
1301 		rc = lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1302 					     NLP_EVT_CMPL_PLOGI);
1303 	}
1304 
1305 	if (disc && vport->num_disc_nodes) {
1306 		/* Check to see if there are more PLOGIs to be sent */
1307 		lpfc_more_plogi(vport);
1308 
1309 		if (vport->num_disc_nodes == 0) {
1310 			spin_lock_irq(shost->host_lock);
1311 			vport->fc_flag &= ~FC_NDISC_ACTIVE;
1312 			spin_unlock_irq(shost->host_lock);
1313 
1314 			lpfc_can_disctmo(vport);
1315 			lpfc_end_rscn(vport);
1316 		}
1317 	}
1318 
1319 out:
1320 	lpfc_els_free_iocb(phba, cmdiocb);
1321 	return;
1322 }
1323 
1324 /**
1325  * lpfc_issue_els_plogi: Issue an plogi iocb command for a vport.
1326  * @vport: pointer to a host virtual N_Port data structure.
1327  * @did: destination port identifier.
1328  * @retry: number of retries to the command IOCB.
1329  *
1330  * This routine issues a Port Login (PLOGI) command to a remote N_Port
1331  * (with the @did) for a @vport. Before issuing a PLOGI to a remote N_Port,
1332  * the ndlp with the remote N_Port DID must exist on the @vport's ndlp list.
1333  * This routine constructs the proper feilds of the PLOGI IOCB and invokes
1334  * the lpfc_sli_issue_iocb() routine to send out PLOGI ELS command.
1335  *
1336  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1337  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1338  * will be stored into the context1 field of the IOCB for the completion
1339  * callback function to the PLOGI ELS command.
1340  *
1341  * Return code
1342  *   0 - Successfully issued a plogi for @vport
1343  *   1 - failed to issue a plogi for @vport
1344  **/
1345 int
lpfc_issue_els_plogi(struct lpfc_vport * vport,uint32_t did,uint8_t retry)1346 lpfc_issue_els_plogi(struct lpfc_vport *vport, uint32_t did, uint8_t retry)
1347 {
1348 	struct lpfc_hba  *phba = vport->phba;
1349 	struct serv_parm *sp;
1350 	IOCB_t *icmd;
1351 	struct lpfc_nodelist *ndlp;
1352 	struct lpfc_iocbq *elsiocb;
1353 	struct lpfc_sli_ring *pring;
1354 	struct lpfc_sli *psli;
1355 	uint8_t *pcmd;
1356 	uint16_t cmdsize;
1357 	int ret;
1358 
1359 	psli = &phba->sli;
1360 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
1361 
1362 	ndlp = lpfc_findnode_did(vport, did);
1363 	if (ndlp && !NLP_CHK_NODE_ACT(ndlp))
1364 		ndlp = NULL;
1365 
1366 	/* If ndlp is not NULL, we will bump the reference count on it */
1367 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
1368 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
1369 				     ELS_CMD_PLOGI);
1370 	if (!elsiocb)
1371 		return 1;
1372 
1373 	icmd = &elsiocb->iocb;
1374 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1375 
1376 	/* For PLOGI request, remainder of payload is service parameters */
1377 	*((uint32_t *) (pcmd)) = ELS_CMD_PLOGI;
1378 	pcmd += sizeof(uint32_t);
1379 	memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
1380 	sp = (struct serv_parm *) pcmd;
1381 
1382 	if (sp->cmn.fcphLow < FC_PH_4_3)
1383 		sp->cmn.fcphLow = FC_PH_4_3;
1384 
1385 	if (sp->cmn.fcphHigh < FC_PH3)
1386 		sp->cmn.fcphHigh = FC_PH3;
1387 
1388 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1389 		"Issue PLOGI:     did:x%x",
1390 		did, 0, 0);
1391 
1392 	phba->fc_stat.elsXmitPLOGI++;
1393 	elsiocb->iocb_cmpl = lpfc_cmpl_els_plogi;
1394 	ret = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1395 
1396 	if (ret == IOCB_ERROR) {
1397 		lpfc_els_free_iocb(phba, elsiocb);
1398 		return 1;
1399 	}
1400 	return 0;
1401 }
1402 
1403 /**
1404  * lpfc_cmpl_els_prli: Completion callback function for prli.
1405  * @phba: pointer to lpfc hba data structure.
1406  * @cmdiocb: pointer to lpfc command iocb data structure.
1407  * @rspiocb: pointer to lpfc response iocb data structure.
1408  *
1409  * This routine is the completion callback function for a Process Login
1410  * (PRLI) ELS command. The PRLI response IOCB status is checked for error
1411  * status. If there is error status reported, PRLI retry shall be attempted
1412  * by invoking the lpfc_els_retry() routine. Otherwise, the state
1413  * NLP_EVT_CMPL_PRLI is sent to the Discover State Machine (DSM) for this
1414  * ndlp to mark the PRLI completion.
1415  **/
1416 static void
lpfc_cmpl_els_prli(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1417 lpfc_cmpl_els_prli(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1418 		   struct lpfc_iocbq *rspiocb)
1419 {
1420 	struct lpfc_vport *vport = cmdiocb->vport;
1421 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1422 	IOCB_t *irsp;
1423 	struct lpfc_sli *psli;
1424 	struct lpfc_nodelist *ndlp;
1425 
1426 	psli = &phba->sli;
1427 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1428 	cmdiocb->context_un.rsp_iocb = rspiocb;
1429 
1430 	irsp = &(rspiocb->iocb);
1431 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1432 	spin_lock_irq(shost->host_lock);
1433 	ndlp->nlp_flag &= ~NLP_PRLI_SND;
1434 	spin_unlock_irq(shost->host_lock);
1435 
1436 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1437 		"PRLI cmpl:       status:x%x/x%x did:x%x",
1438 		irsp->ulpStatus, irsp->un.ulpWord[4],
1439 		ndlp->nlp_DID);
1440 	/* PRLI completes to NPort <nlp_DID> */
1441 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1442 			 "0103 PRLI completes to NPort x%x "
1443 			 "Data: x%x x%x x%x x%x\n",
1444 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1445 			 irsp->ulpTimeout, vport->num_disc_nodes);
1446 
1447 	vport->fc_prli_sent--;
1448 	/* Check to see if link went down during discovery */
1449 	if (lpfc_els_chk_latt(vport))
1450 		goto out;
1451 
1452 	if (irsp->ulpStatus) {
1453 		/* Check for retry */
1454 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1455 			/* ELS command is being retried */
1456 			goto out;
1457 		}
1458 		/* PRLI failed */
1459 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1460 		if (lpfc_error_lost_link(irsp))
1461 			goto out;
1462 		else
1463 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1464 						NLP_EVT_CMPL_PRLI);
1465 	} else
1466 		/* Good status, call state machine */
1467 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1468 					NLP_EVT_CMPL_PRLI);
1469 out:
1470 	lpfc_els_free_iocb(phba, cmdiocb);
1471 	return;
1472 }
1473 
1474 /**
1475  * lpfc_issue_els_prli: Issue a prli iocb command for a vport.
1476  * @vport: pointer to a host virtual N_Port data structure.
1477  * @ndlp: pointer to a node-list data structure.
1478  * @retry: number of retries to the command IOCB.
1479  *
1480  * This routine issues a Process Login (PRLI) ELS command for the
1481  * @vport. The PRLI service parameters are set up in the payload of the
1482  * PRLI Request command and the pointer to lpfc_cmpl_els_prli() routine
1483  * is put to the IOCB completion callback func field before invoking the
1484  * routine lpfc_sli_issue_iocb() to send out PRLI command.
1485  *
1486  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1487  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1488  * will be stored into the context1 field of the IOCB for the completion
1489  * callback function to the PRLI ELS command.
1490  *
1491  * Return code
1492  *   0 - successfully issued prli iocb command for @vport
1493  *   1 - failed to issue prli iocb command for @vport
1494  **/
1495 int
lpfc_issue_els_prli(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)1496 lpfc_issue_els_prli(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1497 		    uint8_t retry)
1498 {
1499 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1500 	struct lpfc_hba *phba = vport->phba;
1501 	PRLI *npr;
1502 	IOCB_t *icmd;
1503 	struct lpfc_iocbq *elsiocb;
1504 	struct lpfc_sli_ring *pring;
1505 	struct lpfc_sli *psli;
1506 	uint8_t *pcmd;
1507 	uint16_t cmdsize;
1508 
1509 	psli = &phba->sli;
1510 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
1511 
1512 	cmdsize = (sizeof(uint32_t) + sizeof(PRLI));
1513 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1514 				     ndlp->nlp_DID, ELS_CMD_PRLI);
1515 	if (!elsiocb)
1516 		return 1;
1517 
1518 	icmd = &elsiocb->iocb;
1519 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1520 
1521 	/* For PRLI request, remainder of payload is service parameters */
1522 	memset(pcmd, 0, (sizeof(PRLI) + sizeof(uint32_t)));
1523 	*((uint32_t *) (pcmd)) = ELS_CMD_PRLI;
1524 	pcmd += sizeof(uint32_t);
1525 
1526 	/* For PRLI, remainder of payload is PRLI parameter page */
1527 	npr = (PRLI *) pcmd;
1528 	/*
1529 	 * If our firmware version is 3.20 or later,
1530 	 * set the following bits for FC-TAPE support.
1531 	 */
1532 	if (phba->vpd.rev.feaLevelHigh >= 0x02) {
1533 		npr->ConfmComplAllowed = 1;
1534 		npr->Retry = 1;
1535 		npr->TaskRetryIdReq = 1;
1536 	}
1537 	npr->estabImagePair = 1;
1538 	npr->readXferRdyDis = 1;
1539 
1540 	/* For FCP support */
1541 	npr->prliType = PRLI_FCP_TYPE;
1542 	npr->initiatorFunc = 1;
1543 
1544 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1545 		"Issue PRLI:      did:x%x",
1546 		ndlp->nlp_DID, 0, 0);
1547 
1548 	phba->fc_stat.elsXmitPRLI++;
1549 	elsiocb->iocb_cmpl = lpfc_cmpl_els_prli;
1550 	spin_lock_irq(shost->host_lock);
1551 	ndlp->nlp_flag |= NLP_PRLI_SND;
1552 	spin_unlock_irq(shost->host_lock);
1553 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1554 		spin_lock_irq(shost->host_lock);
1555 		ndlp->nlp_flag &= ~NLP_PRLI_SND;
1556 		spin_unlock_irq(shost->host_lock);
1557 		lpfc_els_free_iocb(phba, elsiocb);
1558 		return 1;
1559 	}
1560 	vport->fc_prli_sent++;
1561 	return 0;
1562 }
1563 
1564 /**
1565  * lpfc_rscn_disc: Perform rscn discovery for a vport.
1566  * @vport: pointer to a host virtual N_Port data structure.
1567  *
1568  * This routine performs Registration State Change Notification (RSCN)
1569  * discovery for a @vport. If the @vport's node port recovery count is not
1570  * zero, it will invoke the lpfc_els_disc_plogi() to perform PLOGI for all
1571  * the nodes that need recovery. If none of the PLOGI were needed through
1572  * the lpfc_els_disc_plogi() routine, the lpfc_end_rscn() routine shall be
1573  * invoked to check and handle possible more RSCN came in during the period
1574  * of processing the current ones.
1575  **/
1576 static void
lpfc_rscn_disc(struct lpfc_vport * vport)1577 lpfc_rscn_disc(struct lpfc_vport *vport)
1578 {
1579 	lpfc_can_disctmo(vport);
1580 
1581 	/* RSCN discovery */
1582 	/* go thru NPR nodes and issue ELS PLOGIs */
1583 	if (vport->fc_npr_cnt)
1584 		if (lpfc_els_disc_plogi(vport))
1585 			return;
1586 
1587 	lpfc_end_rscn(vport);
1588 }
1589 
1590 /**
1591  * lpfc_adisc_done: Complete the adisc phase of discovery.
1592  * @vport: pointer to lpfc_vport hba data structure that finished all ADISCs.
1593  *
1594  * This function is called when the final ADISC is completed during discovery.
1595  * This function handles clearing link attention or issuing reg_vpi depending
1596  * on whether npiv is enabled. This function also kicks off the PLOGI phase of
1597  * discovery.
1598  * This function is called with no locks held.
1599  **/
1600 static void
lpfc_adisc_done(struct lpfc_vport * vport)1601 lpfc_adisc_done(struct lpfc_vport *vport)
1602 {
1603 	struct Scsi_Host   *shost = lpfc_shost_from_vport(vport);
1604 	struct lpfc_hba   *phba = vport->phba;
1605 
1606 	/*
1607 	 * For NPIV, cmpl_reg_vpi will set port_state to READY,
1608 	 * and continue discovery.
1609 	 */
1610 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1611 	    !(vport->fc_flag & FC_RSCN_MODE)) {
1612 		lpfc_issue_reg_vpi(phba, vport);
1613 		return;
1614 	}
1615 	/*
1616 	* For SLI2, we need to set port_state to READY
1617 	* and continue discovery.
1618 	*/
1619 	if (vport->port_state < LPFC_VPORT_READY) {
1620 		/* If we get here, there is nothing to ADISC */
1621 		if (vport->port_type == LPFC_PHYSICAL_PORT)
1622 			lpfc_issue_clear_la(phba, vport);
1623 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
1624 			vport->num_disc_nodes = 0;
1625 			/* go thru NPR list, issue ELS PLOGIs */
1626 			if (vport->fc_npr_cnt)
1627 				lpfc_els_disc_plogi(vport);
1628 			if (!vport->num_disc_nodes) {
1629 				spin_lock_irq(shost->host_lock);
1630 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
1631 				spin_unlock_irq(shost->host_lock);
1632 				lpfc_can_disctmo(vport);
1633 				lpfc_end_rscn(vport);
1634 			}
1635 		}
1636 		vport->port_state = LPFC_VPORT_READY;
1637 	} else
1638 		lpfc_rscn_disc(vport);
1639 }
1640 
1641 /**
1642  * lpfc_more_adisc: Issue more adisc as needed.
1643  * @vport: pointer to a host virtual N_Port data structure.
1644  *
1645  * This routine determines whether there are more ndlps on a @vport
1646  * node list need to have Address Discover (ADISC) issued. If so, it will
1647  * invoke the lpfc_els_disc_adisc() routine to issue ADISC on the @vport's
1648  * remaining nodes which need to have ADISC sent.
1649  **/
1650 void
lpfc_more_adisc(struct lpfc_vport * vport)1651 lpfc_more_adisc(struct lpfc_vport *vport)
1652 {
1653 	int sentadisc;
1654 
1655 	if (vport->num_disc_nodes)
1656 		vport->num_disc_nodes--;
1657 	/* Continue discovery with <num_disc_nodes> ADISCs to go */
1658 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1659 			 "0210 Continue discovery with %d ADISCs to go "
1660 			 "Data: x%x x%x x%x\n",
1661 			 vport->num_disc_nodes, vport->fc_adisc_cnt,
1662 			 vport->fc_flag, vport->port_state);
1663 	/* Check to see if there are more ADISCs to be sent */
1664 	if (vport->fc_flag & FC_NLP_MORE) {
1665 		lpfc_set_disctmo(vport);
1666 		/* go thru NPR nodes and issue any remaining ELS ADISCs */
1667 		sentadisc = lpfc_els_disc_adisc(vport);
1668 	}
1669 	if (!vport->num_disc_nodes)
1670 		lpfc_adisc_done(vport);
1671 	return;
1672 }
1673 
1674 /**
1675  * lpfc_cmpl_els_adisc: Completion callback function for adisc.
1676  * @phba: pointer to lpfc hba data structure.
1677  * @cmdiocb: pointer to lpfc command iocb data structure.
1678  * @rspiocb: pointer to lpfc response iocb data structure.
1679  *
1680  * This routine is the completion function for issuing the Address Discover
1681  * (ADISC) command. It first checks to see whether link went down during
1682  * the discovery process. If so, the node will be marked as node port
1683  * recovery for issuing discover IOCB by the link attention handler and
1684  * exit. Otherwise, the response status is checked. If error was reported
1685  * in the response status, the ADISC command shall be retried by invoking
1686  * the lpfc_els_retry() routine. Otherwise, if no error was reported in
1687  * the response status, the state machine is invoked to set transition
1688  * with respect to NLP_EVT_CMPL_ADISC event.
1689  **/
1690 static void
lpfc_cmpl_els_adisc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1691 lpfc_cmpl_els_adisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1692 		    struct lpfc_iocbq *rspiocb)
1693 {
1694 	struct lpfc_vport *vport = cmdiocb->vport;
1695 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1696 	IOCB_t *irsp;
1697 	struct lpfc_nodelist *ndlp;
1698 	int  disc;
1699 
1700 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1701 	cmdiocb->context_un.rsp_iocb = rspiocb;
1702 
1703 	irsp = &(rspiocb->iocb);
1704 	ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1705 
1706 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1707 		"ADISC cmpl:      status:x%x/x%x did:x%x",
1708 		irsp->ulpStatus, irsp->un.ulpWord[4],
1709 		ndlp->nlp_DID);
1710 
1711 	/* Since ndlp can be freed in the disc state machine, note if this node
1712 	 * is being used during discovery.
1713 	 */
1714 	spin_lock_irq(shost->host_lock);
1715 	disc = (ndlp->nlp_flag & NLP_NPR_2B_DISC);
1716 	ndlp->nlp_flag &= ~(NLP_ADISC_SND | NLP_NPR_2B_DISC);
1717 	spin_unlock_irq(shost->host_lock);
1718 	/* ADISC completes to NPort <nlp_DID> */
1719 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1720 			 "0104 ADISC completes to NPort x%x "
1721 			 "Data: x%x x%x x%x x%x x%x\n",
1722 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1723 			 irsp->ulpTimeout, disc, vport->num_disc_nodes);
1724 	/* Check to see if link went down during discovery */
1725 	if (lpfc_els_chk_latt(vport)) {
1726 		spin_lock_irq(shost->host_lock);
1727 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1728 		spin_unlock_irq(shost->host_lock);
1729 		goto out;
1730 	}
1731 
1732 	if (irsp->ulpStatus) {
1733 		/* Check for retry */
1734 		if (lpfc_els_retry(phba, cmdiocb, rspiocb)) {
1735 			/* ELS command is being retried */
1736 			if (disc) {
1737 				spin_lock_irq(shost->host_lock);
1738 				ndlp->nlp_flag |= NLP_NPR_2B_DISC;
1739 				spin_unlock_irq(shost->host_lock);
1740 				lpfc_set_disctmo(vport);
1741 			}
1742 			goto out;
1743 		}
1744 		/* ADISC failed */
1745 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1746 		if (!lpfc_error_lost_link(irsp))
1747 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1748 						NLP_EVT_CMPL_ADISC);
1749 	} else
1750 		/* Good status, call state machine */
1751 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1752 					NLP_EVT_CMPL_ADISC);
1753 
1754 	/* Check to see if there are more ADISCs to be sent */
1755 	if (disc && vport->num_disc_nodes)
1756 		lpfc_more_adisc(vport);
1757 out:
1758 	lpfc_els_free_iocb(phba, cmdiocb);
1759 	return;
1760 }
1761 
1762 /**
1763  * lpfc_issue_els_adisc: Issue an address discover iocb to an node on a vport.
1764  * @vport: pointer to a virtual N_Port data structure.
1765  * @ndlp: pointer to a node-list data structure.
1766  * @retry: number of retries to the command IOCB.
1767  *
1768  * This routine issues an Address Discover (ADISC) for an @ndlp on a
1769  * @vport. It prepares the payload of the ADISC ELS command, updates the
1770  * and states of the ndlp, and invokes the lpfc_sli_issue_iocb() routine
1771  * to issue the ADISC ELS command.
1772  *
1773  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1774  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1775  * will be stored into the context1 field of the IOCB for the completion
1776  * callback function to the ADISC ELS command.
1777  *
1778  * Return code
1779  *   0 - successfully issued adisc
1780  *   1 - failed to issue adisc
1781  **/
1782 int
lpfc_issue_els_adisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)1783 lpfc_issue_els_adisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1784 		     uint8_t retry)
1785 {
1786 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1787 	struct lpfc_hba  *phba = vport->phba;
1788 	ADISC *ap;
1789 	IOCB_t *icmd;
1790 	struct lpfc_iocbq *elsiocb;
1791 	struct lpfc_sli *psli = &phba->sli;
1792 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
1793 	uint8_t *pcmd;
1794 	uint16_t cmdsize;
1795 
1796 	cmdsize = (sizeof(uint32_t) + sizeof(ADISC));
1797 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1798 				     ndlp->nlp_DID, ELS_CMD_ADISC);
1799 	if (!elsiocb)
1800 		return 1;
1801 
1802 	icmd = &elsiocb->iocb;
1803 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1804 
1805 	/* For ADISC request, remainder of payload is service parameters */
1806 	*((uint32_t *) (pcmd)) = ELS_CMD_ADISC;
1807 	pcmd += sizeof(uint32_t);
1808 
1809 	/* Fill in ADISC payload */
1810 	ap = (ADISC *) pcmd;
1811 	ap->hardAL_PA = phba->fc_pref_ALPA;
1812 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
1813 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
1814 	ap->DID = be32_to_cpu(vport->fc_myDID);
1815 
1816 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1817 		"Issue ADISC:     did:x%x",
1818 		ndlp->nlp_DID, 0, 0);
1819 
1820 	phba->fc_stat.elsXmitADISC++;
1821 	elsiocb->iocb_cmpl = lpfc_cmpl_els_adisc;
1822 	spin_lock_irq(shost->host_lock);
1823 	ndlp->nlp_flag |= NLP_ADISC_SND;
1824 	spin_unlock_irq(shost->host_lock);
1825 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
1826 		spin_lock_irq(shost->host_lock);
1827 		ndlp->nlp_flag &= ~NLP_ADISC_SND;
1828 		spin_unlock_irq(shost->host_lock);
1829 		lpfc_els_free_iocb(phba, elsiocb);
1830 		return 1;
1831 	}
1832 	return 0;
1833 }
1834 
1835 /**
1836  * lpfc_cmpl_els_logo: Completion callback function for logo.
1837  * @phba: pointer to lpfc hba data structure.
1838  * @cmdiocb: pointer to lpfc command iocb data structure.
1839  * @rspiocb: pointer to lpfc response iocb data structure.
1840  *
1841  * This routine is the completion function for issuing the ELS Logout (LOGO)
1842  * command. If no error status was reported from the LOGO response, the
1843  * state machine of the associated ndlp shall be invoked for transition with
1844  * respect to NLP_EVT_CMPL_LOGO event. Otherwise, if error status was reported,
1845  * the lpfc_els_retry() routine will be invoked to retry the LOGO command.
1846  **/
1847 static void
lpfc_cmpl_els_logo(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)1848 lpfc_cmpl_els_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1849 		   struct lpfc_iocbq *rspiocb)
1850 {
1851 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
1852 	struct lpfc_vport *vport = ndlp->vport;
1853 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1854 	IOCB_t *irsp;
1855 	struct lpfc_sli *psli;
1856 
1857 	psli = &phba->sli;
1858 	/* we pass cmdiocb to state machine which needs rspiocb as well */
1859 	cmdiocb->context_un.rsp_iocb = rspiocb;
1860 
1861 	irsp = &(rspiocb->iocb);
1862 	spin_lock_irq(shost->host_lock);
1863 	ndlp->nlp_flag &= ~NLP_LOGO_SND;
1864 	spin_unlock_irq(shost->host_lock);
1865 
1866 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1867 		"LOGO cmpl:       status:x%x/x%x did:x%x",
1868 		irsp->ulpStatus, irsp->un.ulpWord[4],
1869 		ndlp->nlp_DID);
1870 	/* LOGO completes to NPort <nlp_DID> */
1871 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
1872 			 "0105 LOGO completes to NPort x%x "
1873 			 "Data: x%x x%x x%x x%x\n",
1874 			 ndlp->nlp_DID, irsp->ulpStatus, irsp->un.ulpWord[4],
1875 			 irsp->ulpTimeout, vport->num_disc_nodes);
1876 	/* Check to see if link went down during discovery */
1877 	if (lpfc_els_chk_latt(vport))
1878 		goto out;
1879 
1880 	if (ndlp->nlp_flag & NLP_TARGET_REMOVE) {
1881 	        /* NLP_EVT_DEVICE_RM should unregister the RPI
1882 		 * which should abort all outstanding IOs.
1883 		 */
1884 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1885 					NLP_EVT_DEVICE_RM);
1886 		goto out;
1887 	}
1888 
1889 	if (irsp->ulpStatus) {
1890 		/* Check for retry */
1891 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
1892 			/* ELS command is being retried */
1893 			goto out;
1894 		/* LOGO failed */
1895 		/* Do not call DSM for lpfc_els_abort'ed ELS cmds */
1896 		if (lpfc_error_lost_link(irsp))
1897 			goto out;
1898 		else
1899 			lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1900 						NLP_EVT_CMPL_LOGO);
1901 	} else
1902 		/* Good status, call state machine.
1903 		 * This will unregister the rpi if needed.
1904 		 */
1905 		lpfc_disc_state_machine(vport, ndlp, cmdiocb,
1906 					NLP_EVT_CMPL_LOGO);
1907 out:
1908 	lpfc_els_free_iocb(phba, cmdiocb);
1909 	return;
1910 }
1911 
1912 /**
1913  * lpfc_issue_els_logo: Issue a logo to an node on a vport.
1914  * @vport: pointer to a virtual N_Port data structure.
1915  * @ndlp: pointer to a node-list data structure.
1916  * @retry: number of retries to the command IOCB.
1917  *
1918  * This routine constructs and issues an ELS Logout (LOGO) iocb command
1919  * to a remote node, referred by an @ndlp on a @vport. It constructs the
1920  * payload of the IOCB, properly sets up the @ndlp state, and invokes the
1921  * lpfc_sli_issue_iocb() routine to send out the LOGO ELS command.
1922  *
1923  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
1924  * will be incremented by 1 for holding the ndlp and the reference to ndlp
1925  * will be stored into the context1 field of the IOCB for the completion
1926  * callback function to the LOGO ELS command.
1927  *
1928  * Return code
1929  *   0 - successfully issued logo
1930  *   1 - failed to issue logo
1931  **/
1932 int
lpfc_issue_els_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)1933 lpfc_issue_els_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
1934 		    uint8_t retry)
1935 {
1936 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1937 	struct lpfc_hba  *phba = vport->phba;
1938 	IOCB_t *icmd;
1939 	struct lpfc_iocbq *elsiocb;
1940 	struct lpfc_sli_ring *pring;
1941 	struct lpfc_sli *psli;
1942 	uint8_t *pcmd;
1943 	uint16_t cmdsize;
1944 	int rc;
1945 
1946 	psli = &phba->sli;
1947 	pring = &psli->ring[LPFC_ELS_RING];
1948 
1949 	spin_lock_irq(shost->host_lock);
1950 	if (ndlp->nlp_flag & NLP_LOGO_SND) {
1951 		spin_unlock_irq(shost->host_lock);
1952 		return 0;
1953 	}
1954 	spin_unlock_irq(shost->host_lock);
1955 
1956 	cmdsize = (2 * sizeof(uint32_t)) + sizeof(struct lpfc_name);
1957 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
1958 				     ndlp->nlp_DID, ELS_CMD_LOGO);
1959 	if (!elsiocb)
1960 		return 1;
1961 
1962 	icmd = &elsiocb->iocb;
1963 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
1964 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
1965 	pcmd += sizeof(uint32_t);
1966 
1967 	/* Fill in LOGO payload */
1968 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
1969 	pcmd += sizeof(uint32_t);
1970 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
1971 
1972 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1973 		"Issue LOGO:      did:x%x",
1974 		ndlp->nlp_DID, 0, 0);
1975 
1976 	phba->fc_stat.elsXmitLOGO++;
1977 	elsiocb->iocb_cmpl = lpfc_cmpl_els_logo;
1978 	spin_lock_irq(shost->host_lock);
1979 	ndlp->nlp_flag |= NLP_LOGO_SND;
1980 	spin_unlock_irq(shost->host_lock);
1981 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
1982 
1983 	if (rc == IOCB_ERROR) {
1984 		spin_lock_irq(shost->host_lock);
1985 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
1986 		spin_unlock_irq(shost->host_lock);
1987 		lpfc_els_free_iocb(phba, elsiocb);
1988 		return 1;
1989 	}
1990 	return 0;
1991 }
1992 
1993 /**
1994  * lpfc_cmpl_els_cmd: Completion callback function for generic els command.
1995  * @phba: pointer to lpfc hba data structure.
1996  * @cmdiocb: pointer to lpfc command iocb data structure.
1997  * @rspiocb: pointer to lpfc response iocb data structure.
1998  *
1999  * This routine is a generic completion callback function for ELS commands.
2000  * Specifically, it is the callback function which does not need to perform
2001  * any command specific operations. It is currently used by the ELS command
2002  * issuing routines for the ELS State Change  Request (SCR),
2003  * lpfc_issue_els_scr(), and the ELS Fibre Channel Address Resolution
2004  * Protocol Response (FARPR) routine, lpfc_issue_els_farpr(). Other than
2005  * certain debug loggings, this callback function simply invokes the
2006  * lpfc_els_chk_latt() routine to check whether link went down during the
2007  * discovery process.
2008  **/
2009 static void
lpfc_cmpl_els_cmd(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2010 lpfc_cmpl_els_cmd(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2011 		  struct lpfc_iocbq *rspiocb)
2012 {
2013 	struct lpfc_vport *vport = cmdiocb->vport;
2014 	IOCB_t *irsp;
2015 
2016 	irsp = &rspiocb->iocb;
2017 
2018 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2019 		"ELS cmd cmpl:    status:x%x/x%x did:x%x",
2020 		irsp->ulpStatus, irsp->un.ulpWord[4],
2021 		irsp->un.elsreq64.remoteID);
2022 	/* ELS cmd tag <ulpIoTag> completes */
2023 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2024 			 "0106 ELS cmd tag x%x completes Data: x%x x%x x%x\n",
2025 			 irsp->ulpIoTag, irsp->ulpStatus,
2026 			 irsp->un.ulpWord[4], irsp->ulpTimeout);
2027 	/* Check to see if link went down during discovery */
2028 	lpfc_els_chk_latt(vport);
2029 	lpfc_els_free_iocb(phba, cmdiocb);
2030 	return;
2031 }
2032 
2033 /**
2034  * lpfc_issue_els_scr: Issue a scr to an node on a vport.
2035  * @vport: pointer to a host virtual N_Port data structure.
2036  * @nportid: N_Port identifier to the remote node.
2037  * @retry: number of retries to the command IOCB.
2038  *
2039  * This routine issues a State Change Request (SCR) to a fabric node
2040  * on a @vport. The remote node @nportid is passed into the function. It
2041  * first search the @vport node list to find the matching ndlp. If no such
2042  * ndlp is found, a new ndlp shall be created for this (SCR) purpose. An
2043  * IOCB is allocated, payload prepared, and the lpfc_sli_issue_iocb()
2044  * routine is invoked to send the SCR IOCB.
2045  *
2046  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2047  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2048  * will be stored into the context1 field of the IOCB for the completion
2049  * callback function to the SCR ELS command.
2050  *
2051  * Return code
2052  *   0 - Successfully issued scr command
2053  *   1 - Failed to issue scr command
2054  **/
2055 int
lpfc_issue_els_scr(struct lpfc_vport * vport,uint32_t nportid,uint8_t retry)2056 lpfc_issue_els_scr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2057 {
2058 	struct lpfc_hba  *phba = vport->phba;
2059 	IOCB_t *icmd;
2060 	struct lpfc_iocbq *elsiocb;
2061 	struct lpfc_sli_ring *pring;
2062 	struct lpfc_sli *psli;
2063 	uint8_t *pcmd;
2064 	uint16_t cmdsize;
2065 	struct lpfc_nodelist *ndlp;
2066 
2067 	psli = &phba->sli;
2068 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
2069 	cmdsize = (sizeof(uint32_t) + sizeof(SCR));
2070 
2071 	ndlp = lpfc_findnode_did(vport, nportid);
2072 	if (!ndlp) {
2073 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2074 		if (!ndlp)
2075 			return 1;
2076 		lpfc_nlp_init(vport, ndlp, nportid);
2077 		lpfc_enqueue_node(vport, ndlp);
2078 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
2079 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2080 		if (!ndlp)
2081 			return 1;
2082 	}
2083 
2084 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2085 				     ndlp->nlp_DID, ELS_CMD_SCR);
2086 
2087 	if (!elsiocb) {
2088 		/* This will trigger the release of the node just
2089 		 * allocated
2090 		 */
2091 		lpfc_nlp_put(ndlp);
2092 		return 1;
2093 	}
2094 
2095 	icmd = &elsiocb->iocb;
2096 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2097 
2098 	*((uint32_t *) (pcmd)) = ELS_CMD_SCR;
2099 	pcmd += sizeof(uint32_t);
2100 
2101 	/* For SCR, remainder of payload is SCR parameter page */
2102 	memset(pcmd, 0, sizeof(SCR));
2103 	((SCR *) pcmd)->Function = SCR_FUNC_FULL;
2104 
2105 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2106 		"Issue SCR:       did:x%x",
2107 		ndlp->nlp_DID, 0, 0);
2108 
2109 	phba->fc_stat.elsXmitSCR++;
2110 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2111 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2112 		/* The additional lpfc_nlp_put will cause the following
2113 		 * lpfc_els_free_iocb routine to trigger the rlease of
2114 		 * the node.
2115 		 */
2116 		lpfc_nlp_put(ndlp);
2117 		lpfc_els_free_iocb(phba, elsiocb);
2118 		return 1;
2119 	}
2120 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
2121 	 * trigger the release of node.
2122 	 */
2123 	lpfc_nlp_put(ndlp);
2124 	return 0;
2125 }
2126 
2127 /**
2128  * lpfc_issue_els_farpr: Issue a farp to an node on a vport.
2129  * @vport: pointer to a host virtual N_Port data structure.
2130  * @nportid: N_Port identifier to the remote node.
2131  * @retry: number of retries to the command IOCB.
2132  *
2133  * This routine issues a Fibre Channel Address Resolution Response
2134  * (FARPR) to a node on a vport. The remote node N_Port identifier (@nportid)
2135  * is passed into the function. It first search the @vport node list to find
2136  * the matching ndlp. If no such ndlp is found, a new ndlp shall be created
2137  * for this (FARPR) purpose. An IOCB is allocated, payload prepared, and the
2138  * lpfc_sli_issue_iocb() routine is invoked to send the FARPR ELS command.
2139  *
2140  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
2141  * will be incremented by 1 for holding the ndlp and the reference to ndlp
2142  * will be stored into the context1 field of the IOCB for the completion
2143  * callback function to the PARPR ELS command.
2144  *
2145  * Return code
2146  *   0 - Successfully issued farpr command
2147  *   1 - Failed to issue farpr command
2148  **/
2149 static int
lpfc_issue_els_farpr(struct lpfc_vport * vport,uint32_t nportid,uint8_t retry)2150 lpfc_issue_els_farpr(struct lpfc_vport *vport, uint32_t nportid, uint8_t retry)
2151 {
2152 	struct lpfc_hba  *phba = vport->phba;
2153 	IOCB_t *icmd;
2154 	struct lpfc_iocbq *elsiocb;
2155 	struct lpfc_sli_ring *pring;
2156 	struct lpfc_sli *psli;
2157 	FARP *fp;
2158 	uint8_t *pcmd;
2159 	uint32_t *lp;
2160 	uint16_t cmdsize;
2161 	struct lpfc_nodelist *ondlp;
2162 	struct lpfc_nodelist *ndlp;
2163 
2164 	psli = &phba->sli;
2165 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
2166 	cmdsize = (sizeof(uint32_t) + sizeof(FARP));
2167 
2168 	ndlp = lpfc_findnode_did(vport, nportid);
2169 	if (!ndlp) {
2170 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
2171 		if (!ndlp)
2172 			return 1;
2173 		lpfc_nlp_init(vport, ndlp, nportid);
2174 		lpfc_enqueue_node(vport, ndlp);
2175 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
2176 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
2177 		if (!ndlp)
2178 			return 1;
2179 	}
2180 
2181 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp,
2182 				     ndlp->nlp_DID, ELS_CMD_RNID);
2183 	if (!elsiocb) {
2184 		/* This will trigger the release of the node just
2185 		 * allocated
2186 		 */
2187 		lpfc_nlp_put(ndlp);
2188 		return 1;
2189 	}
2190 
2191 	icmd = &elsiocb->iocb;
2192 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
2193 
2194 	*((uint32_t *) (pcmd)) = ELS_CMD_FARPR;
2195 	pcmd += sizeof(uint32_t);
2196 
2197 	/* Fill in FARPR payload */
2198 	fp = (FARP *) (pcmd);
2199 	memset(fp, 0, sizeof(FARP));
2200 	lp = (uint32_t *) pcmd;
2201 	*lp++ = be32_to_cpu(nportid);
2202 	*lp++ = be32_to_cpu(vport->fc_myDID);
2203 	fp->Rflags = 0;
2204 	fp->Mflags = (FARP_MATCH_PORT | FARP_MATCH_NODE);
2205 
2206 	memcpy(&fp->RportName, &vport->fc_portname, sizeof(struct lpfc_name));
2207 	memcpy(&fp->RnodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
2208 	ondlp = lpfc_findnode_did(vport, nportid);
2209 	if (ondlp && NLP_CHK_NODE_ACT(ondlp)) {
2210 		memcpy(&fp->OportName, &ondlp->nlp_portname,
2211 		       sizeof(struct lpfc_name));
2212 		memcpy(&fp->OnodeName, &ondlp->nlp_nodename,
2213 		       sizeof(struct lpfc_name));
2214 	}
2215 
2216 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2217 		"Issue FARPR:     did:x%x",
2218 		ndlp->nlp_DID, 0, 0);
2219 
2220 	phba->fc_stat.elsXmitFARPR++;
2221 	elsiocb->iocb_cmpl = lpfc_cmpl_els_cmd;
2222 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
2223 		/* The additional lpfc_nlp_put will cause the following
2224 		 * lpfc_els_free_iocb routine to trigger the release of
2225 		 * the node.
2226 		 */
2227 		lpfc_nlp_put(ndlp);
2228 		lpfc_els_free_iocb(phba, elsiocb);
2229 		return 1;
2230 	}
2231 	/* This will cause the callback-function lpfc_cmpl_els_cmd to
2232 	 * trigger the release of the node.
2233 	 */
2234 	lpfc_nlp_put(ndlp);
2235 	return 0;
2236 }
2237 
2238 /**
2239  * lpfc_cancel_retry_delay_tmo: Cancel the timer with delayed iocb-cmd retry.
2240  * @vport: pointer to a host virtual N_Port data structure.
2241  * @nlp: pointer to a node-list data structure.
2242  *
2243  * This routine cancels the timer with a delayed IOCB-command retry for
2244  * a @vport's @ndlp. It stops the timer for the delayed function retrial and
2245  * removes the ELS retry event if it presents. In addition, if the
2246  * NLP_NPR_2B_DISC bit is set in the @nlp's nlp_flag bitmap, ADISC IOCB
2247  * commands are sent for the @vport's nodes that require issuing discovery
2248  * ADISC.
2249  **/
2250 void
lpfc_cancel_retry_delay_tmo(struct lpfc_vport * vport,struct lpfc_nodelist * nlp)2251 lpfc_cancel_retry_delay_tmo(struct lpfc_vport *vport, struct lpfc_nodelist *nlp)
2252 {
2253 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2254 	struct lpfc_work_evt *evtp;
2255 
2256 	if (!(nlp->nlp_flag & NLP_DELAY_TMO))
2257 		return;
2258 	spin_lock_irq(shost->host_lock);
2259 	nlp->nlp_flag &= ~NLP_DELAY_TMO;
2260 	spin_unlock_irq(shost->host_lock);
2261 	del_timer_sync(&nlp->nlp_delayfunc);
2262 	nlp->nlp_last_elscmd = 0;
2263 	if (!list_empty(&nlp->els_retry_evt.evt_listp)) {
2264 		list_del_init(&nlp->els_retry_evt.evt_listp);
2265 		/* Decrement nlp reference count held for the delayed retry */
2266 		evtp = &nlp->els_retry_evt;
2267 		lpfc_nlp_put((struct lpfc_nodelist *)evtp->evt_arg1);
2268 	}
2269 	if (nlp->nlp_flag & NLP_NPR_2B_DISC) {
2270 		spin_lock_irq(shost->host_lock);
2271 		nlp->nlp_flag &= ~NLP_NPR_2B_DISC;
2272 		spin_unlock_irq(shost->host_lock);
2273 		if (vport->num_disc_nodes) {
2274 			if (vport->port_state < LPFC_VPORT_READY) {
2275 				/* Check if there are more ADISCs to be sent */
2276 				lpfc_more_adisc(vport);
2277 			} else {
2278 				/* Check if there are more PLOGIs to be sent */
2279 				lpfc_more_plogi(vport);
2280 				if (vport->num_disc_nodes == 0) {
2281 					spin_lock_irq(shost->host_lock);
2282 					vport->fc_flag &= ~FC_NDISC_ACTIVE;
2283 					spin_unlock_irq(shost->host_lock);
2284 					lpfc_can_disctmo(vport);
2285 					lpfc_end_rscn(vport);
2286 				}
2287 			}
2288 		}
2289 	}
2290 	return;
2291 }
2292 
2293 /**
2294  * lpfc_els_retry_delay: Timer function with a ndlp delayed function timer.
2295  * @ptr: holder for the pointer to the timer function associated data (ndlp).
2296  *
2297  * This routine is invoked by the ndlp delayed-function timer to check
2298  * whether there is any pending ELS retry event(s) with the node. If not, it
2299  * simply returns. Otherwise, if there is at least one ELS delayed event, it
2300  * adds the delayed events to the HBA work list and invokes the
2301  * lpfc_worker_wake_up() routine to wake up worker thread to process the
2302  * event. Note that lpfc_nlp_get() is called before posting the event to
2303  * the work list to hold reference count of ndlp so that it guarantees the
2304  * reference to ndlp will still be available when the worker thread gets
2305  * to the event associated with the ndlp.
2306  **/
2307 void
lpfc_els_retry_delay(unsigned long ptr)2308 lpfc_els_retry_delay(unsigned long ptr)
2309 {
2310 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) ptr;
2311 	struct lpfc_vport *vport = ndlp->vport;
2312 	struct lpfc_hba   *phba = vport->phba;
2313 	unsigned long flags;
2314 	struct lpfc_work_evt  *evtp = &ndlp->els_retry_evt;
2315 
2316 	spin_lock_irqsave(&phba->hbalock, flags);
2317 	if (!list_empty(&evtp->evt_listp)) {
2318 		spin_unlock_irqrestore(&phba->hbalock, flags);
2319 		return;
2320 	}
2321 
2322 	/* We need to hold the node by incrementing the reference
2323 	 * count until the queued work is done
2324 	 */
2325 	evtp->evt_arg1  = lpfc_nlp_get(ndlp);
2326 	if (evtp->evt_arg1) {
2327 		evtp->evt = LPFC_EVT_ELS_RETRY;
2328 		list_add_tail(&evtp->evt_listp, &phba->work_list);
2329 		lpfc_worker_wake_up(phba);
2330 	}
2331 	spin_unlock_irqrestore(&phba->hbalock, flags);
2332 	return;
2333 }
2334 
2335 /**
2336  * lpfc_els_retry_delay_handler: Work thread handler for ndlp delayed function.
2337  * @ndlp: pointer to a node-list data structure.
2338  *
2339  * This routine is the worker-thread handler for processing the @ndlp delayed
2340  * event(s), posted by the lpfc_els_retry_delay() routine. It simply retrieves
2341  * the last ELS command from the associated ndlp and invokes the proper ELS
2342  * function according to the delayed ELS command to retry the command.
2343  **/
2344 void
lpfc_els_retry_delay_handler(struct lpfc_nodelist * ndlp)2345 lpfc_els_retry_delay_handler(struct lpfc_nodelist *ndlp)
2346 {
2347 	struct lpfc_vport *vport = ndlp->vport;
2348 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2349 	uint32_t cmd, did, retry;
2350 
2351 	spin_lock_irq(shost->host_lock);
2352 	did = ndlp->nlp_DID;
2353 	cmd = ndlp->nlp_last_elscmd;
2354 	ndlp->nlp_last_elscmd = 0;
2355 
2356 	if (!(ndlp->nlp_flag & NLP_DELAY_TMO)) {
2357 		spin_unlock_irq(shost->host_lock);
2358 		return;
2359 	}
2360 
2361 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
2362 	spin_unlock_irq(shost->host_lock);
2363 	/*
2364 	 * If a discovery event readded nlp_delayfunc after timer
2365 	 * firing and before processing the timer, cancel the
2366 	 * nlp_delayfunc.
2367 	 */
2368 	del_timer_sync(&ndlp->nlp_delayfunc);
2369 	retry = ndlp->nlp_retry;
2370 
2371 	switch (cmd) {
2372 	case ELS_CMD_FLOGI:
2373 		lpfc_issue_els_flogi(vport, ndlp, retry);
2374 		break;
2375 	case ELS_CMD_PLOGI:
2376 		if (!lpfc_issue_els_plogi(vport, ndlp->nlp_DID, retry)) {
2377 			ndlp->nlp_prev_state = ndlp->nlp_state;
2378 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
2379 		}
2380 		break;
2381 	case ELS_CMD_ADISC:
2382 		if (!lpfc_issue_els_adisc(vport, ndlp, retry)) {
2383 			ndlp->nlp_prev_state = ndlp->nlp_state;
2384 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2385 		}
2386 		break;
2387 	case ELS_CMD_PRLI:
2388 		if (!lpfc_issue_els_prli(vport, ndlp, retry)) {
2389 			ndlp->nlp_prev_state = ndlp->nlp_state;
2390 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2391 		}
2392 		break;
2393 	case ELS_CMD_LOGO:
2394 		if (!lpfc_issue_els_logo(vport, ndlp, retry)) {
2395 			ndlp->nlp_prev_state = ndlp->nlp_state;
2396 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2397 		}
2398 		break;
2399 	case ELS_CMD_FDISC:
2400 		lpfc_issue_els_fdisc(vport, ndlp, retry);
2401 		break;
2402 	}
2403 	return;
2404 }
2405 
2406 /**
2407  * lpfc_els_retry: Make retry decision on an els command iocb.
2408  * @phba: pointer to lpfc hba data structure.
2409  * @cmdiocb: pointer to lpfc command iocb data structure.
2410  * @rspiocb: pointer to lpfc response iocb data structure.
2411  *
2412  * This routine makes a retry decision on an ELS command IOCB, which has
2413  * failed. The following ELS IOCBs use this function for retrying the command
2414  * when previously issued command responsed with error status: FLOGI, PLOGI,
2415  * PRLI, ADISC, LOGO, and FDISC. Based on the ELS command type and the
2416  * returned error status, it makes the decision whether a retry shall be
2417  * issued for the command, and whether a retry shall be made immediately or
2418  * delayed. In the former case, the corresponding ELS command issuing-function
2419  * is called to retry the command. In the later case, the ELS command shall
2420  * be posted to the ndlp delayed event and delayed function timer set to the
2421  * ndlp for the delayed command issusing.
2422  *
2423  * Return code
2424  *   0 - No retry of els command is made
2425  *   1 - Immediate or delayed retry of els command is made
2426  **/
2427 static int
lpfc_els_retry(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2428 lpfc_els_retry(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2429 	       struct lpfc_iocbq *rspiocb)
2430 {
2431 	struct lpfc_vport *vport = cmdiocb->vport;
2432 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
2433 	IOCB_t *irsp = &rspiocb->iocb;
2434 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2435 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
2436 	uint32_t *elscmd;
2437 	struct ls_rjt stat;
2438 	int retry = 0, maxretry = lpfc_max_els_tries, delay = 0;
2439 	int logerr = 0;
2440 	uint32_t cmd = 0;
2441 	uint32_t did;
2442 
2443 
2444 	/* Note: context2 may be 0 for internal driver abort
2445 	 * of delays ELS command.
2446 	 */
2447 
2448 	if (pcmd && pcmd->virt) {
2449 		elscmd = (uint32_t *) (pcmd->virt);
2450 		cmd = *elscmd++;
2451 	}
2452 
2453 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
2454 		did = ndlp->nlp_DID;
2455 	else {
2456 		/* We should only hit this case for retrying PLOGI */
2457 		did = irsp->un.elsreq64.remoteID;
2458 		ndlp = lpfc_findnode_did(vport, did);
2459 		if ((!ndlp || !NLP_CHK_NODE_ACT(ndlp))
2460 		    && (cmd != ELS_CMD_PLOGI))
2461 			return 1;
2462 	}
2463 
2464 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
2465 		"Retry ELS:       wd7:x%x wd4:x%x did:x%x",
2466 		*(((uint32_t *) irsp) + 7), irsp->un.ulpWord[4], ndlp->nlp_DID);
2467 
2468 	switch (irsp->ulpStatus) {
2469 	case IOSTAT_FCP_RSP_ERROR:
2470 	case IOSTAT_REMOTE_STOP:
2471 		break;
2472 
2473 	case IOSTAT_LOCAL_REJECT:
2474 		switch ((irsp->un.ulpWord[4] & 0xff)) {
2475 		case IOERR_LOOP_OPEN_FAILURE:
2476 			if (cmd == ELS_CMD_FLOGI) {
2477 				if (PCI_DEVICE_ID_HORNET ==
2478 					phba->pcidev->device) {
2479 					phba->fc_topology = TOPOLOGY_LOOP;
2480 					phba->pport->fc_myDID = 0;
2481 					phba->alpa_map[0] = 0;
2482 					phba->alpa_map[1] = 0;
2483 				}
2484 			}
2485 			if (cmd == ELS_CMD_PLOGI && cmdiocb->retry == 0)
2486 				delay = 1000;
2487 			retry = 1;
2488 			break;
2489 
2490 		case IOERR_ILLEGAL_COMMAND:
2491 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2492 					 "0124 Retry illegal cmd x%x "
2493 					 "retry:x%x delay:x%x\n",
2494 					 cmd, cmdiocb->retry, delay);
2495 			retry = 1;
2496 			/* All command's retry policy */
2497 			maxretry = 8;
2498 			if (cmdiocb->retry > 2)
2499 				delay = 1000;
2500 			break;
2501 
2502 		case IOERR_NO_RESOURCES:
2503 			logerr = 1; /* HBA out of resources */
2504 			retry = 1;
2505 			if (cmdiocb->retry > 100)
2506 				delay = 100;
2507 			maxretry = 250;
2508 			break;
2509 
2510 		case IOERR_ILLEGAL_FRAME:
2511 			delay = 100;
2512 			retry = 1;
2513 			break;
2514 
2515 		case IOERR_SEQUENCE_TIMEOUT:
2516 		case IOERR_INVALID_RPI:
2517 			retry = 1;
2518 			break;
2519 		}
2520 		break;
2521 
2522 	case IOSTAT_NPORT_RJT:
2523 	case IOSTAT_FABRIC_RJT:
2524 		if (irsp->un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
2525 			retry = 1;
2526 			break;
2527 		}
2528 		break;
2529 
2530 	case IOSTAT_NPORT_BSY:
2531 	case IOSTAT_FABRIC_BSY:
2532 		logerr = 1; /* Fabric / Remote NPort out of resources */
2533 		retry = 1;
2534 		break;
2535 
2536 	case IOSTAT_LS_RJT:
2537 		stat.un.lsRjtError = be32_to_cpu(irsp->un.ulpWord[4]);
2538 		/* Added for Vendor specifc support
2539 		 * Just keep retrying for these Rsn / Exp codes
2540 		 */
2541 		switch (stat.un.b.lsRjtRsnCode) {
2542 		case LSRJT_UNABLE_TPC:
2543 			if (stat.un.b.lsRjtRsnCodeExp ==
2544 			    LSEXP_CMD_IN_PROGRESS) {
2545 				if (cmd == ELS_CMD_PLOGI) {
2546 					delay = 1000;
2547 					maxretry = 48;
2548 				}
2549 				retry = 1;
2550 				break;
2551 			}
2552 			if (cmd == ELS_CMD_PLOGI) {
2553 				delay = 1000;
2554 				maxretry = lpfc_max_els_tries + 1;
2555 				retry = 1;
2556 				break;
2557 			}
2558 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2559 			  (cmd == ELS_CMD_FDISC) &&
2560 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_OUT_OF_RESOURCE)){
2561 				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2562 						 "0125 FDISC Failed (x%x). "
2563 						 "Fabric out of resources\n",
2564 						 stat.un.lsRjtError);
2565 				lpfc_vport_set_state(vport,
2566 						     FC_VPORT_NO_FABRIC_RSCS);
2567 			}
2568 			break;
2569 
2570 		case LSRJT_LOGICAL_BSY:
2571 			if ((cmd == ELS_CMD_PLOGI) ||
2572 			    (cmd == ELS_CMD_PRLI)) {
2573 				delay = 1000;
2574 				maxretry = 48;
2575 			} else if (cmd == ELS_CMD_FDISC) {
2576 				/* FDISC retry policy */
2577 				maxretry = 48;
2578 				if (cmdiocb->retry >= 32)
2579 					delay = 1000;
2580 			}
2581 			retry = 1;
2582 			break;
2583 
2584 		case LSRJT_LOGICAL_ERR:
2585 			/* There are some cases where switches return this
2586 			 * error when they are not ready and should be returning
2587 			 * Logical Busy. We should delay every time.
2588 			 */
2589 			if (cmd == ELS_CMD_FDISC &&
2590 			    stat.un.b.lsRjtRsnCodeExp == LSEXP_PORT_LOGIN_REQ) {
2591 				maxretry = 3;
2592 				delay = 1000;
2593 				retry = 1;
2594 				break;
2595 			}
2596 		case LSRJT_PROTOCOL_ERR:
2597 			if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
2598 			  (cmd == ELS_CMD_FDISC) &&
2599 			  ((stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_PNAME) ||
2600 			  (stat.un.b.lsRjtRsnCodeExp == LSEXP_INVALID_NPORT_ID))
2601 			  ) {
2602 				lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2603 						 "0122 FDISC Failed (x%x). "
2604 						 "Fabric Detected Bad WWN\n",
2605 						 stat.un.lsRjtError);
2606 				lpfc_vport_set_state(vport,
2607 						     FC_VPORT_FABRIC_REJ_WWN);
2608 			}
2609 			break;
2610 		}
2611 		break;
2612 
2613 	case IOSTAT_INTERMED_RSP:
2614 	case IOSTAT_BA_RJT:
2615 		break;
2616 
2617 	default:
2618 		break;
2619 	}
2620 
2621 	if (did == FDMI_DID)
2622 		retry = 1;
2623 
2624 	if ((cmd == ELS_CMD_FLOGI) &&
2625 	    (phba->fc_topology != TOPOLOGY_LOOP) &&
2626 	    !lpfc_error_lost_link(irsp)) {
2627 		/* FLOGI retry policy */
2628 		retry = 1;
2629 		maxretry = 48;
2630 		if (cmdiocb->retry >= 32)
2631 			delay = 1000;
2632 	}
2633 
2634 	if ((++cmdiocb->retry) >= maxretry) {
2635 		phba->fc_stat.elsRetryExceeded++;
2636 		retry = 0;
2637 	}
2638 
2639 	if ((vport->load_flag & FC_UNLOADING) != 0)
2640 		retry = 0;
2641 
2642 	if (retry) {
2643 
2644 		/* Retry ELS command <elsCmd> to remote NPORT <did> */
2645 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2646 				 "0107 Retry ELS command x%x to remote "
2647 				 "NPORT x%x Data: x%x x%x\n",
2648 				 cmd, did, cmdiocb->retry, delay);
2649 
2650 		if (((cmd == ELS_CMD_PLOGI) || (cmd == ELS_CMD_ADISC)) &&
2651 			((irsp->ulpStatus != IOSTAT_LOCAL_REJECT) ||
2652 			((irsp->un.ulpWord[4] & 0xff) != IOERR_NO_RESOURCES))) {
2653 			/* Don't reset timer for no resources */
2654 
2655 			/* If discovery / RSCN timer is running, reset it */
2656 			if (timer_pending(&vport->fc_disctmo) ||
2657 			    (vport->fc_flag & FC_RSCN_MODE))
2658 				lpfc_set_disctmo(vport);
2659 		}
2660 
2661 		phba->fc_stat.elsXmitRetry++;
2662 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) && delay) {
2663 			phba->fc_stat.elsDelayRetry++;
2664 			ndlp->nlp_retry = cmdiocb->retry;
2665 
2666 			/* delay is specified in milliseconds */
2667 			mod_timer(&ndlp->nlp_delayfunc,
2668 				jiffies + msecs_to_jiffies(delay));
2669 			spin_lock_irq(shost->host_lock);
2670 			ndlp->nlp_flag |= NLP_DELAY_TMO;
2671 			spin_unlock_irq(shost->host_lock);
2672 
2673 			ndlp->nlp_prev_state = ndlp->nlp_state;
2674 			if (cmd == ELS_CMD_PRLI)
2675 				lpfc_nlp_set_state(vport, ndlp,
2676 					NLP_STE_REG_LOGIN_ISSUE);
2677 			else
2678 				lpfc_nlp_set_state(vport, ndlp,
2679 					NLP_STE_NPR_NODE);
2680 			ndlp->nlp_last_elscmd = cmd;
2681 
2682 			return 1;
2683 		}
2684 		switch (cmd) {
2685 		case ELS_CMD_FLOGI:
2686 			lpfc_issue_els_flogi(vport, ndlp, cmdiocb->retry);
2687 			return 1;
2688 		case ELS_CMD_FDISC:
2689 			lpfc_issue_els_fdisc(vport, ndlp, cmdiocb->retry);
2690 			return 1;
2691 		case ELS_CMD_PLOGI:
2692 			if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2693 				ndlp->nlp_prev_state = ndlp->nlp_state;
2694 				lpfc_nlp_set_state(vport, ndlp,
2695 						   NLP_STE_PLOGI_ISSUE);
2696 			}
2697 			lpfc_issue_els_plogi(vport, did, cmdiocb->retry);
2698 			return 1;
2699 		case ELS_CMD_ADISC:
2700 			ndlp->nlp_prev_state = ndlp->nlp_state;
2701 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
2702 			lpfc_issue_els_adisc(vport, ndlp, cmdiocb->retry);
2703 			return 1;
2704 		case ELS_CMD_PRLI:
2705 			ndlp->nlp_prev_state = ndlp->nlp_state;
2706 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PRLI_ISSUE);
2707 			lpfc_issue_els_prli(vport, ndlp, cmdiocb->retry);
2708 			return 1;
2709 		case ELS_CMD_LOGO:
2710 			ndlp->nlp_prev_state = ndlp->nlp_state;
2711 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
2712 			lpfc_issue_els_logo(vport, ndlp, cmdiocb->retry);
2713 			return 1;
2714 		}
2715 	}
2716 	/* No retry ELS command <elsCmd> to remote NPORT <did> */
2717 	if (logerr) {
2718 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2719 			 "0137 No retry ELS command x%x to remote "
2720 			 "NPORT x%x: Out of Resources: Error:x%x/%x\n",
2721 			 cmd, did, irsp->ulpStatus,
2722 			 irsp->un.ulpWord[4]);
2723 	}
2724 	else {
2725 		lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2726 			 "0108 No retry ELS command x%x to remote "
2727 			 "NPORT x%x Retried:%d Error:x%x/%x\n",
2728 			 cmd, did, cmdiocb->retry, irsp->ulpStatus,
2729 			 irsp->un.ulpWord[4]);
2730 	}
2731 	return 0;
2732 }
2733 
2734 /**
2735  * lpfc_els_free_data: Free lpfc dma buffer and data structure with an iocb.
2736  * @phba: pointer to lpfc hba data structure.
2737  * @buf_ptr1: pointer to the lpfc DMA buffer data structure.
2738  *
2739  * This routine releases the lpfc DMA (Direct Memory Access) buffer(s)
2740  * associated with a command IOCB back to the lpfc DMA buffer pool. It first
2741  * checks to see whether there is a lpfc DMA buffer associated with the
2742  * response of the command IOCB. If so, it will be released before releasing
2743  * the lpfc DMA buffer associated with the IOCB itself.
2744  *
2745  * Return code
2746  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2747  **/
2748 static int
lpfc_els_free_data(struct lpfc_hba * phba,struct lpfc_dmabuf * buf_ptr1)2749 lpfc_els_free_data(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr1)
2750 {
2751 	struct lpfc_dmabuf *buf_ptr;
2752 
2753 	/* Free the response before processing the command. */
2754 	if (!list_empty(&buf_ptr1->list)) {
2755 		list_remove_head(&buf_ptr1->list, buf_ptr,
2756 				 struct lpfc_dmabuf,
2757 				 list);
2758 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2759 		kfree(buf_ptr);
2760 	}
2761 	lpfc_mbuf_free(phba, buf_ptr1->virt, buf_ptr1->phys);
2762 	kfree(buf_ptr1);
2763 	return 0;
2764 }
2765 
2766 /**
2767  * lpfc_els_free_bpl: Free lpfc dma buffer and data structure with bpl.
2768  * @phba: pointer to lpfc hba data structure.
2769  * @buf_ptr: pointer to the lpfc dma buffer data structure.
2770  *
2771  * This routine releases the lpfc Direct Memory Access (DMA) buffer
2772  * associated with a Buffer Pointer List (BPL) back to the lpfc DMA buffer
2773  * pool.
2774  *
2775  * Return code
2776  *   0 - Successfully released lpfc DMA buffer (currently, always return 0)
2777  **/
2778 static int
lpfc_els_free_bpl(struct lpfc_hba * phba,struct lpfc_dmabuf * buf_ptr)2779 lpfc_els_free_bpl(struct lpfc_hba *phba, struct lpfc_dmabuf *buf_ptr)
2780 {
2781 	lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
2782 	kfree(buf_ptr);
2783 	return 0;
2784 }
2785 
2786 /**
2787  * lpfc_els_free_iocb: Free a command iocb and its associated resources.
2788  * @phba: pointer to lpfc hba data structure.
2789  * @elsiocb: pointer to lpfc els command iocb data structure.
2790  *
2791  * This routine frees a command IOCB and its associated resources. The
2792  * command IOCB data structure contains the reference to various associated
2793  * resources, these fields must be set to NULL if the associated reference
2794  * not present:
2795  *   context1 - reference to ndlp
2796  *   context2 - reference to cmd
2797  *   context2->next - reference to rsp
2798  *   context3 - reference to bpl
2799  *
2800  * It first properly decrements the reference count held on ndlp for the
2801  * IOCB completion callback function. If LPFC_DELAY_MEM_FREE flag is not
2802  * set, it invokes the lpfc_els_free_data() routine to release the Direct
2803  * Memory Access (DMA) buffers associated with the IOCB. Otherwise, it
2804  * adds the DMA buffer the @phba data structure for the delayed release.
2805  * If reference to the Buffer Pointer List (BPL) is present, the
2806  * lpfc_els_free_bpl() routine is invoked to release the DMA memory
2807  * associated with BPL. Finally, the lpfc_sli_release_iocbq() routine is
2808  * invoked to release the IOCB data structure back to @phba IOCBQ list.
2809  *
2810  * Return code
2811  *   0 - Success (currently, always return 0)
2812  **/
2813 int
lpfc_els_free_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * elsiocb)2814 lpfc_els_free_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *elsiocb)
2815 {
2816 	struct lpfc_dmabuf *buf_ptr, *buf_ptr1;
2817 	struct lpfc_nodelist *ndlp;
2818 
2819 	ndlp = (struct lpfc_nodelist *)elsiocb->context1;
2820 	if (ndlp) {
2821 		if (ndlp->nlp_flag & NLP_DEFER_RM) {
2822 			lpfc_nlp_put(ndlp);
2823 
2824 			/* If the ndlp is not being used by another discovery
2825 			 * thread, free it.
2826 			 */
2827 			if (!lpfc_nlp_not_used(ndlp)) {
2828 				/* If ndlp is being used by another discovery
2829 				 * thread, just clear NLP_DEFER_RM
2830 				 */
2831 				ndlp->nlp_flag &= ~NLP_DEFER_RM;
2832 			}
2833 		}
2834 		else
2835 			lpfc_nlp_put(ndlp);
2836 		elsiocb->context1 = NULL;
2837 	}
2838 	/* context2  = cmd,  context2->next = rsp, context3 = bpl */
2839 	if (elsiocb->context2) {
2840 		if (elsiocb->iocb_flag & LPFC_DELAY_MEM_FREE) {
2841 			/* Firmware could still be in progress of DMAing
2842 			 * payload, so don't free data buffer till after
2843 			 * a hbeat.
2844 			 */
2845 			elsiocb->iocb_flag &= ~LPFC_DELAY_MEM_FREE;
2846 			buf_ptr = elsiocb->context2;
2847 			elsiocb->context2 = NULL;
2848 			if (buf_ptr) {
2849 				buf_ptr1 = NULL;
2850 				spin_lock_irq(&phba->hbalock);
2851 				if (!list_empty(&buf_ptr->list)) {
2852 					list_remove_head(&buf_ptr->list,
2853 						buf_ptr1, struct lpfc_dmabuf,
2854 						list);
2855 					INIT_LIST_HEAD(&buf_ptr1->list);
2856 					list_add_tail(&buf_ptr1->list,
2857 						&phba->elsbuf);
2858 					phba->elsbuf_cnt++;
2859 				}
2860 				INIT_LIST_HEAD(&buf_ptr->list);
2861 				list_add_tail(&buf_ptr->list, &phba->elsbuf);
2862 				phba->elsbuf_cnt++;
2863 				spin_unlock_irq(&phba->hbalock);
2864 			}
2865 		} else {
2866 			buf_ptr1 = (struct lpfc_dmabuf *) elsiocb->context2;
2867 			lpfc_els_free_data(phba, buf_ptr1);
2868 		}
2869 	}
2870 
2871 	if (elsiocb->context3) {
2872 		buf_ptr = (struct lpfc_dmabuf *) elsiocb->context3;
2873 		lpfc_els_free_bpl(phba, buf_ptr);
2874 	}
2875 	lpfc_sli_release_iocbq(phba, elsiocb);
2876 	return 0;
2877 }
2878 
2879 /**
2880  * lpfc_cmpl_els_logo_acc: Completion callback function to logo acc response.
2881  * @phba: pointer to lpfc hba data structure.
2882  * @cmdiocb: pointer to lpfc command iocb data structure.
2883  * @rspiocb: pointer to lpfc response iocb data structure.
2884  *
2885  * This routine is the completion callback function to the Logout (LOGO)
2886  * Accept (ACC) Response ELS command. This routine is invoked to indicate
2887  * the completion of the LOGO process. It invokes the lpfc_nlp_not_used() to
2888  * release the ndlp if it has the last reference remaining (reference count
2889  * is 1). If succeeded (meaning ndlp released), it sets the IOCB context1
2890  * field to NULL to inform the following lpfc_els_free_iocb() routine no
2891  * ndlp reference count needs to be decremented. Otherwise, the ndlp
2892  * reference use-count shall be decremented by the lpfc_els_free_iocb()
2893  * routine. Finally, the lpfc_els_free_iocb() is invoked to release the
2894  * IOCB data structure.
2895  **/
2896 static void
lpfc_cmpl_els_logo_acc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2897 lpfc_cmpl_els_logo_acc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2898 		       struct lpfc_iocbq *rspiocb)
2899 {
2900 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2901 	struct lpfc_vport *vport = cmdiocb->vport;
2902 	IOCB_t *irsp;
2903 
2904 	irsp = &rspiocb->iocb;
2905 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
2906 		"ACC LOGO cmpl:   status:x%x/x%x did:x%x",
2907 		irsp->ulpStatus, irsp->un.ulpWord[4], ndlp->nlp_DID);
2908 	/* ACC to LOGO completes to NPort <nlp_DID> */
2909 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
2910 			 "0109 ACC to LOGO completes to NPort x%x "
2911 			 "Data: x%x x%x x%x\n",
2912 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
2913 			 ndlp->nlp_rpi);
2914 
2915 	if (ndlp->nlp_state == NLP_STE_NPR_NODE) {
2916 		/* NPort Recovery mode or node is just allocated */
2917 		if (!lpfc_nlp_not_used(ndlp)) {
2918 			/* If the ndlp is being used by another discovery
2919 			 * thread, just unregister the RPI.
2920 			 */
2921 			lpfc_unreg_rpi(vport, ndlp);
2922 		} else {
2923 			/* Indicate the node has already released, should
2924 			 * not reference to it from within lpfc_els_free_iocb.
2925 			 */
2926 			cmdiocb->context1 = NULL;
2927 		}
2928 	}
2929 	lpfc_els_free_iocb(phba, cmdiocb);
2930 	return;
2931 }
2932 
2933 /**
2934  * lpfc_mbx_cmpl_dflt_rpi: Completion callbk func for unreg dflt rpi mbox cmd.
2935  * @phba: pointer to lpfc hba data structure.
2936  * @pmb: pointer to the driver internal queue element for mailbox command.
2937  *
2938  * This routine is the completion callback function for unregister default
2939  * RPI (Remote Port Index) mailbox command to the @phba. It simply releases
2940  * the associated lpfc Direct Memory Access (DMA) buffer back to the pool and
2941  * decrements the ndlp reference count held for this completion callback
2942  * function. After that, it invokes the lpfc_nlp_not_used() to check
2943  * whether there is only one reference left on the ndlp. If so, it will
2944  * perform one more decrement and trigger the release of the ndlp.
2945  **/
2946 void
lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)2947 lpfc_mbx_cmpl_dflt_rpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2948 {
2949 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
2950 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
2951 
2952 	pmb->context1 = NULL;
2953 	lpfc_mbuf_free(phba, mp->virt, mp->phys);
2954 	kfree(mp);
2955 	mempool_free(pmb, phba->mbox_mem_pool);
2956 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
2957 		lpfc_nlp_put(ndlp);
2958 		/* This is the end of the default RPI cleanup logic for this
2959 		 * ndlp. If no other discovery threads are using this ndlp.
2960 		 * we should free all resources associated with it.
2961 		 */
2962 		lpfc_nlp_not_used(ndlp);
2963 	}
2964 	return;
2965 }
2966 
2967 /**
2968  * lpfc_cmpl_els_rsp: Completion callback function for els response iocb cmd.
2969  * @phba: pointer to lpfc hba data structure.
2970  * @cmdiocb: pointer to lpfc command iocb data structure.
2971  * @rspiocb: pointer to lpfc response iocb data structure.
2972  *
2973  * This routine is the completion callback function for ELS Response IOCB
2974  * command. In normal case, this callback function just properly sets the
2975  * nlp_flag bitmap in the ndlp data structure, if the mbox command reference
2976  * field in the command IOCB is not NULL, the referred mailbox command will
2977  * be send out, and then invokes the lpfc_els_free_iocb() routine to release
2978  * the IOCB. Under error conditions, such as when a LS_RJT is returned or a
2979  * link down event occurred during the discovery, the lpfc_nlp_not_used()
2980  * routine shall be invoked trying to release the ndlp if no other threads
2981  * are currently referring it.
2982  **/
2983 static void
lpfc_cmpl_els_rsp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)2984 lpfc_cmpl_els_rsp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
2985 		  struct lpfc_iocbq *rspiocb)
2986 {
2987 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
2988 	struct lpfc_vport *vport = ndlp ? ndlp->vport : NULL;
2989 	struct Scsi_Host  *shost = vport ? lpfc_shost_from_vport(vport) : NULL;
2990 	IOCB_t  *irsp;
2991 	uint8_t *pcmd;
2992 	LPFC_MBOXQ_t *mbox = NULL;
2993 	struct lpfc_dmabuf *mp = NULL;
2994 	uint32_t ls_rjt = 0;
2995 
2996 	irsp = &rspiocb->iocb;
2997 
2998 	if (cmdiocb->context_un.mbox)
2999 		mbox = cmdiocb->context_un.mbox;
3000 
3001 	/* First determine if this is a LS_RJT cmpl. Note, this callback
3002 	 * function can have cmdiocb->contest1 (ndlp) field set to NULL.
3003 	 */
3004 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) cmdiocb->context2)->virt);
3005 	if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3006 	    (*((uint32_t *) (pcmd)) == ELS_CMD_LS_RJT)) {
3007 		/* A LS_RJT associated with Default RPI cleanup has its own
3008 		 * seperate code path.
3009 		 */
3010 		if (!(ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3011 			ls_rjt = 1;
3012 	}
3013 
3014 	/* Check to see if link went down during discovery */
3015 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) || lpfc_els_chk_latt(vport)) {
3016 		if (mbox) {
3017 			mp = (struct lpfc_dmabuf *) mbox->context1;
3018 			if (mp) {
3019 				lpfc_mbuf_free(phba, mp->virt, mp->phys);
3020 				kfree(mp);
3021 			}
3022 			mempool_free(mbox, phba->mbox_mem_pool);
3023 		}
3024 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
3025 		    (ndlp->nlp_flag & NLP_RM_DFLT_RPI))
3026 			if (lpfc_nlp_not_used(ndlp)) {
3027 				ndlp = NULL;
3028 				/* Indicate the node has already released,
3029 				 * should not reference to it from within
3030 				 * the routine lpfc_els_free_iocb.
3031 				 */
3032 				cmdiocb->context1 = NULL;
3033 			}
3034 		goto out;
3035 	}
3036 
3037 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3038 		"ELS rsp cmpl:    status:x%x/x%x did:x%x",
3039 		irsp->ulpStatus, irsp->un.ulpWord[4],
3040 		cmdiocb->iocb.un.elsreq64.remoteID);
3041 	/* ELS response tag <ulpIoTag> completes */
3042 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3043 			 "0110 ELS response tag x%x completes "
3044 			 "Data: x%x x%x x%x x%x x%x x%x x%x\n",
3045 			 cmdiocb->iocb.ulpIoTag, rspiocb->iocb.ulpStatus,
3046 			 rspiocb->iocb.un.ulpWord[4], rspiocb->iocb.ulpTimeout,
3047 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3048 			 ndlp->nlp_rpi);
3049 	if (mbox) {
3050 		if ((rspiocb->iocb.ulpStatus == 0)
3051 		    && (ndlp->nlp_flag & NLP_ACC_REGLOGIN)) {
3052 			lpfc_unreg_rpi(vport, ndlp);
3053 			/* Increment reference count to ndlp to hold the
3054 			 * reference to ndlp for the callback function.
3055 			 */
3056 			mbox->context2 = lpfc_nlp_get(ndlp);
3057 			mbox->vport = vport;
3058 			if (ndlp->nlp_flag & NLP_RM_DFLT_RPI) {
3059 				mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
3060 				mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
3061 			}
3062 			else {
3063 				mbox->mbox_cmpl = lpfc_mbx_cmpl_reg_login;
3064 				ndlp->nlp_prev_state = ndlp->nlp_state;
3065 				lpfc_nlp_set_state(vport, ndlp,
3066 					   NLP_STE_REG_LOGIN_ISSUE);
3067 			}
3068 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
3069 			    != MBX_NOT_FINISHED)
3070 				goto out;
3071 			else
3072 				/* Decrement the ndlp reference count we
3073 				 * set for this failed mailbox command.
3074 				 */
3075 				lpfc_nlp_put(ndlp);
3076 
3077 			/* ELS rsp: Cannot issue reg_login for <NPortid> */
3078 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3079 				"0138 ELS rsp: Cannot issue reg_login for x%x "
3080 				"Data: x%x x%x x%x\n",
3081 				ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3082 				ndlp->nlp_rpi);
3083 
3084 			if (lpfc_nlp_not_used(ndlp)) {
3085 				ndlp = NULL;
3086 				/* Indicate node has already been released,
3087 				 * should not reference to it from within
3088 				 * the routine lpfc_els_free_iocb.
3089 				 */
3090 				cmdiocb->context1 = NULL;
3091 			}
3092 		} else {
3093 			/* Do not drop node for lpfc_els_abort'ed ELS cmds */
3094 			if (!lpfc_error_lost_link(irsp) &&
3095 			    ndlp->nlp_flag & NLP_ACC_REGLOGIN) {
3096 				if (lpfc_nlp_not_used(ndlp)) {
3097 					ndlp = NULL;
3098 					/* Indicate node has already been
3099 					 * released, should not reference
3100 					 * to it from within the routine
3101 					 * lpfc_els_free_iocb.
3102 					 */
3103 					cmdiocb->context1 = NULL;
3104 				}
3105 			}
3106 		}
3107 		mp = (struct lpfc_dmabuf *) mbox->context1;
3108 		if (mp) {
3109 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
3110 			kfree(mp);
3111 		}
3112 		mempool_free(mbox, phba->mbox_mem_pool);
3113 	}
3114 out:
3115 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
3116 		spin_lock_irq(shost->host_lock);
3117 		ndlp->nlp_flag &= ~(NLP_ACC_REGLOGIN | NLP_RM_DFLT_RPI);
3118 		spin_unlock_irq(shost->host_lock);
3119 
3120 		/* If the node is not being used by another discovery thread,
3121 		 * and we are sending a reject, we are done with it.
3122 		 * Release driver reference count here and free associated
3123 		 * resources.
3124 		 */
3125 		if (ls_rjt)
3126 			if (lpfc_nlp_not_used(ndlp))
3127 				/* Indicate node has already been released,
3128 				 * should not reference to it from within
3129 				 * the routine lpfc_els_free_iocb.
3130 				 */
3131 				cmdiocb->context1 = NULL;
3132 	}
3133 
3134 	lpfc_els_free_iocb(phba, cmdiocb);
3135 	return;
3136 }
3137 
3138 /**
3139  * lpfc_els_rsp_acc: Prepare and issue an acc response iocb command.
3140  * @vport: pointer to a host virtual N_Port data structure.
3141  * @flag: the els command code to be accepted.
3142  * @oldiocb: pointer to the original lpfc command iocb data structure.
3143  * @ndlp: pointer to a node-list data structure.
3144  * @mbox: pointer to the driver internal queue element for mailbox command.
3145  *
3146  * This routine prepares and issues an Accept (ACC) response IOCB
3147  * command. It uses the @flag to properly set up the IOCB field for the
3148  * specific ACC response command to be issued and invokes the
3149  * lpfc_sli_issue_iocb() routine to send out ACC response IOCB. If a
3150  * @mbox pointer is passed in, it will be put into the context_un.mbox
3151  * field of the IOCB for the completion callback function to issue the
3152  * mailbox command to the HBA later when callback is invoked.
3153  *
3154  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3155  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3156  * will be stored into the context1 field of the IOCB for the completion
3157  * callback function to the corresponding response ELS IOCB command.
3158  *
3159  * Return code
3160  *   0 - Successfully issued acc response
3161  *   1 - Failed to issue acc response
3162  **/
3163 int
lpfc_els_rsp_acc(struct lpfc_vport * vport,uint32_t flag,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp,LPFC_MBOXQ_t * mbox)3164 lpfc_els_rsp_acc(struct lpfc_vport *vport, uint32_t flag,
3165 		 struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3166 		 LPFC_MBOXQ_t *mbox)
3167 {
3168 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3169 	struct lpfc_hba  *phba = vport->phba;
3170 	IOCB_t *icmd;
3171 	IOCB_t *oldcmd;
3172 	struct lpfc_iocbq *elsiocb;
3173 	struct lpfc_sli_ring *pring;
3174 	struct lpfc_sli *psli;
3175 	uint8_t *pcmd;
3176 	uint16_t cmdsize;
3177 	int rc;
3178 	ELS_PKT *els_pkt_ptr;
3179 
3180 	psli = &phba->sli;
3181 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
3182 	oldcmd = &oldiocb->iocb;
3183 
3184 	switch (flag) {
3185 	case ELS_CMD_ACC:
3186 		cmdsize = sizeof(uint32_t);
3187 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3188 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3189 		if (!elsiocb) {
3190 			spin_lock_irq(shost->host_lock);
3191 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3192 			spin_unlock_irq(shost->host_lock);
3193 			return 1;
3194 		}
3195 
3196 		icmd = &elsiocb->iocb;
3197 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3198 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3199 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3200 		pcmd += sizeof(uint32_t);
3201 
3202 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3203 			"Issue ACC:       did:x%x flg:x%x",
3204 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3205 		break;
3206 	case ELS_CMD_PLOGI:
3207 		cmdsize = (sizeof(struct serv_parm) + sizeof(uint32_t));
3208 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3209 					     ndlp, ndlp->nlp_DID, ELS_CMD_ACC);
3210 		if (!elsiocb)
3211 			return 1;
3212 
3213 		icmd = &elsiocb->iocb;
3214 		icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3215 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3216 
3217 		if (mbox)
3218 			elsiocb->context_un.mbox = mbox;
3219 
3220 		*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3221 		pcmd += sizeof(uint32_t);
3222 		memcpy(pcmd, &vport->fc_sparam, sizeof(struct serv_parm));
3223 
3224 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3225 			"Issue ACC PLOGI: did:x%x flg:x%x",
3226 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3227 		break;
3228 	case ELS_CMD_PRLO:
3229 		cmdsize = sizeof(uint32_t) + sizeof(PRLO);
3230 		elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry,
3231 					     ndlp, ndlp->nlp_DID, ELS_CMD_PRLO);
3232 		if (!elsiocb)
3233 			return 1;
3234 
3235 		icmd = &elsiocb->iocb;
3236 		icmd->ulpContext = oldcmd->ulpContext; /* Xri */
3237 		pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3238 
3239 		memcpy(pcmd, ((struct lpfc_dmabuf *) oldiocb->context2)->virt,
3240 		       sizeof(uint32_t) + sizeof(PRLO));
3241 		*((uint32_t *) (pcmd)) = ELS_CMD_PRLO_ACC;
3242 		els_pkt_ptr = (ELS_PKT *) pcmd;
3243 		els_pkt_ptr->un.prlo.acceptRspCode = PRLO_REQ_EXECUTED;
3244 
3245 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3246 			"Issue ACC PRLO:  did:x%x flg:x%x",
3247 			ndlp->nlp_DID, ndlp->nlp_flag, 0);
3248 		break;
3249 	default:
3250 		return 1;
3251 	}
3252 	/* Xmit ELS ACC response tag <ulpIoTag> */
3253 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3254 			 "0128 Xmit ELS ACC response tag x%x, XRI: x%x, "
3255 			 "DID: x%x, nlp_flag: x%x nlp_state: x%x RPI: x%x\n",
3256 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3257 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3258 			 ndlp->nlp_rpi);
3259 	if (ndlp->nlp_flag & NLP_LOGO_ACC) {
3260 		spin_lock_irq(shost->host_lock);
3261 		ndlp->nlp_flag &= ~NLP_LOGO_ACC;
3262 		spin_unlock_irq(shost->host_lock);
3263 		elsiocb->iocb_cmpl = lpfc_cmpl_els_logo_acc;
3264 	} else {
3265 		elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3266 	}
3267 
3268 	phba->fc_stat.elsXmitACC++;
3269 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3270 	if (rc == IOCB_ERROR) {
3271 		lpfc_els_free_iocb(phba, elsiocb);
3272 		return 1;
3273 	}
3274 	return 0;
3275 }
3276 
3277 /**
3278  * lpfc_els_rsp_reject: Propare and issue a rjt response iocb command.
3279  * @vport: pointer to a virtual N_Port data structure.
3280  * @rejectError:
3281  * @oldiocb: pointer to the original lpfc command iocb data structure.
3282  * @ndlp: pointer to a node-list data structure.
3283  * @mbox: pointer to the driver internal queue element for mailbox command.
3284  *
3285  * This routine prepares and issue an Reject (RJT) response IOCB
3286  * command. If a @mbox pointer is passed in, it will be put into the
3287  * context_un.mbox field of the IOCB for the completion callback function
3288  * to issue to the HBA later.
3289  *
3290  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3291  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3292  * will be stored into the context1 field of the IOCB for the completion
3293  * callback function to the reject response ELS IOCB command.
3294  *
3295  * Return code
3296  *   0 - Successfully issued reject response
3297  *   1 - Failed to issue reject response
3298  **/
3299 int
lpfc_els_rsp_reject(struct lpfc_vport * vport,uint32_t rejectError,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp,LPFC_MBOXQ_t * mbox)3300 lpfc_els_rsp_reject(struct lpfc_vport *vport, uint32_t rejectError,
3301 		    struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp,
3302 		    LPFC_MBOXQ_t *mbox)
3303 {
3304 	struct lpfc_hba  *phba = vport->phba;
3305 	IOCB_t *icmd;
3306 	IOCB_t *oldcmd;
3307 	struct lpfc_iocbq *elsiocb;
3308 	struct lpfc_sli_ring *pring;
3309 	struct lpfc_sli *psli;
3310 	uint8_t *pcmd;
3311 	uint16_t cmdsize;
3312 	int rc;
3313 
3314 	psli = &phba->sli;
3315 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
3316 
3317 	cmdsize = 2 * sizeof(uint32_t);
3318 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3319 				     ndlp->nlp_DID, ELS_CMD_LS_RJT);
3320 	if (!elsiocb)
3321 		return 1;
3322 
3323 	icmd = &elsiocb->iocb;
3324 	oldcmd = &oldiocb->iocb;
3325 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3326 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3327 
3328 	*((uint32_t *) (pcmd)) = ELS_CMD_LS_RJT;
3329 	pcmd += sizeof(uint32_t);
3330 	*((uint32_t *) (pcmd)) = rejectError;
3331 
3332 	if (mbox)
3333 		elsiocb->context_un.mbox = mbox;
3334 
3335 	/* Xmit ELS RJT <err> response tag <ulpIoTag> */
3336 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3337 			 "0129 Xmit ELS RJT x%x response tag x%x "
3338 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
3339 			 "rpi x%x\n",
3340 			 rejectError, elsiocb->iotag,
3341 			 elsiocb->iocb.ulpContext, ndlp->nlp_DID,
3342 			 ndlp->nlp_flag, ndlp->nlp_state, ndlp->nlp_rpi);
3343 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3344 		"Issue LS_RJT:    did:x%x flg:x%x err:x%x",
3345 		ndlp->nlp_DID, ndlp->nlp_flag, rejectError);
3346 
3347 	phba->fc_stat.elsXmitLSRJT++;
3348 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3349 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3350 
3351 	if (rc == IOCB_ERROR) {
3352 		lpfc_els_free_iocb(phba, elsiocb);
3353 		return 1;
3354 	}
3355 	return 0;
3356 }
3357 
3358 /**
3359  * lpfc_els_rsp_adisc_acc: Prepare and issue acc response to adisc iocb cmd.
3360  * @vport: pointer to a virtual N_Port data structure.
3361  * @oldiocb: pointer to the original lpfc command iocb data structure.
3362  * @ndlp: pointer to a node-list data structure.
3363  *
3364  * This routine prepares and issues an Accept (ACC) response to Address
3365  * Discover (ADISC) ELS command. It simply prepares the payload of the IOCB
3366  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3367  *
3368  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3369  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3370  * will be stored into the context1 field of the IOCB for the completion
3371  * callback function to the ADISC Accept response ELS IOCB command.
3372  *
3373  * Return code
3374  *   0 - Successfully issued acc adisc response
3375  *   1 - Failed to issue adisc acc response
3376  **/
3377 int
lpfc_els_rsp_adisc_acc(struct lpfc_vport * vport,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)3378 lpfc_els_rsp_adisc_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3379 		       struct lpfc_nodelist *ndlp)
3380 {
3381 	struct lpfc_hba  *phba = vport->phba;
3382 	struct lpfc_sli  *psli = &phba->sli;
3383 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
3384 	ADISC *ap;
3385 	IOCB_t *icmd, *oldcmd;
3386 	struct lpfc_iocbq *elsiocb;
3387 	uint8_t *pcmd;
3388 	uint16_t cmdsize;
3389 	int rc;
3390 
3391 	cmdsize = sizeof(uint32_t) + sizeof(ADISC);
3392 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3393 				     ndlp->nlp_DID, ELS_CMD_ACC);
3394 	if (!elsiocb)
3395 		return 1;
3396 
3397 	icmd = &elsiocb->iocb;
3398 	oldcmd = &oldiocb->iocb;
3399 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3400 
3401 	/* Xmit ADISC ACC response tag <ulpIoTag> */
3402 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3403 			 "0130 Xmit ADISC ACC response iotag x%x xri: "
3404 			 "x%x, did x%x, nlp_flag x%x, nlp_state x%x rpi x%x\n",
3405 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3406 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3407 			 ndlp->nlp_rpi);
3408 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3409 
3410 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3411 	pcmd += sizeof(uint32_t);
3412 
3413 	ap = (ADISC *) (pcmd);
3414 	ap->hardAL_PA = phba->fc_pref_ALPA;
3415 	memcpy(&ap->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3416 	memcpy(&ap->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3417 	ap->DID = be32_to_cpu(vport->fc_myDID);
3418 
3419 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3420 		"Issue ACC ADISC: did:x%x flg:x%x",
3421 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3422 
3423 	phba->fc_stat.elsXmitACC++;
3424 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3425 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3426 	if (rc == IOCB_ERROR) {
3427 		lpfc_els_free_iocb(phba, elsiocb);
3428 		return 1;
3429 	}
3430 	return 0;
3431 }
3432 
3433 /**
3434  * lpfc_els_rsp_prli_acc: Prepare and issue acc response to prli iocb cmd.
3435  * @vport: pointer to a virtual N_Port data structure.
3436  * @oldiocb: pointer to the original lpfc command iocb data structure.
3437  * @ndlp: pointer to a node-list data structure.
3438  *
3439  * This routine prepares and issues an Accept (ACC) response to Process
3440  * Login (PRLI) ELS command. It simply prepares the payload of the IOCB
3441  * and invokes the lpfc_sli_issue_iocb() routine to send out the command.
3442  *
3443  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3444  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3445  * will be stored into the context1 field of the IOCB for the completion
3446  * callback function to the PRLI Accept response ELS IOCB command.
3447  *
3448  * Return code
3449  *   0 - Successfully issued acc prli response
3450  *   1 - Failed to issue acc prli response
3451  **/
3452 int
lpfc_els_rsp_prli_acc(struct lpfc_vport * vport,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)3453 lpfc_els_rsp_prli_acc(struct lpfc_vport *vport, struct lpfc_iocbq *oldiocb,
3454 		      struct lpfc_nodelist *ndlp)
3455 {
3456 	struct lpfc_hba  *phba = vport->phba;
3457 	PRLI *npr;
3458 	lpfc_vpd_t *vpd;
3459 	IOCB_t *icmd;
3460 	IOCB_t *oldcmd;
3461 	struct lpfc_iocbq *elsiocb;
3462 	struct lpfc_sli_ring *pring;
3463 	struct lpfc_sli *psli;
3464 	uint8_t *pcmd;
3465 	uint16_t cmdsize;
3466 	int rc;
3467 
3468 	psli = &phba->sli;
3469 	pring = &psli->ring[LPFC_ELS_RING];	/* ELS ring */
3470 
3471 	cmdsize = sizeof(uint32_t) + sizeof(PRLI);
3472 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3473 		ndlp->nlp_DID, (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK)));
3474 	if (!elsiocb)
3475 		return 1;
3476 
3477 	icmd = &elsiocb->iocb;
3478 	oldcmd = &oldiocb->iocb;
3479 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3480 	/* Xmit PRLI ACC response tag <ulpIoTag> */
3481 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3482 			 "0131 Xmit PRLI ACC response tag x%x xri x%x, "
3483 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
3484 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
3485 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
3486 			 ndlp->nlp_rpi);
3487 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3488 
3489 	*((uint32_t *) (pcmd)) = (ELS_CMD_ACC | (ELS_CMD_PRLI & ~ELS_RSP_MASK));
3490 	pcmd += sizeof(uint32_t);
3491 
3492 	/* For PRLI, remainder of payload is PRLI parameter page */
3493 	memset(pcmd, 0, sizeof(PRLI));
3494 
3495 	npr = (PRLI *) pcmd;
3496 	vpd = &phba->vpd;
3497 	/*
3498 	 * If the remote port is a target and our firmware version is 3.20 or
3499 	 * later, set the following bits for FC-TAPE support.
3500 	 */
3501 	if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
3502 	    (vpd->rev.feaLevelHigh >= 0x02)) {
3503 		npr->ConfmComplAllowed = 1;
3504 		npr->Retry = 1;
3505 		npr->TaskRetryIdReq = 1;
3506 	}
3507 
3508 	npr->acceptRspCode = PRLI_REQ_EXECUTED;
3509 	npr->estabImagePair = 1;
3510 	npr->readXferRdyDis = 1;
3511 	npr->ConfmComplAllowed = 1;
3512 
3513 	npr->prliType = PRLI_FCP_TYPE;
3514 	npr->initiatorFunc = 1;
3515 
3516 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3517 		"Issue ACC PRLI:  did:x%x flg:x%x",
3518 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3519 
3520 	phba->fc_stat.elsXmitACC++;
3521 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3522 
3523 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3524 	if (rc == IOCB_ERROR) {
3525 		lpfc_els_free_iocb(phba, elsiocb);
3526 		return 1;
3527 	}
3528 	return 0;
3529 }
3530 
3531 /**
3532  * lpfc_els_rsp_rnid_acc: Issue rnid acc response iocb command.
3533  * @vport: pointer to a virtual N_Port data structure.
3534  * @format: rnid command format.
3535  * @oldiocb: pointer to the original lpfc command iocb data structure.
3536  * @ndlp: pointer to a node-list data structure.
3537  *
3538  * This routine issues a Request Node Identification Data (RNID) Accept
3539  * (ACC) response. It constructs the RNID ACC response command according to
3540  * the proper @format and then calls the lpfc_sli_issue_iocb() routine to
3541  * issue the response. Note that this command does not need to hold the ndlp
3542  * reference count for the callback. So, the ndlp reference count taken by
3543  * the lpfc_prep_els_iocb() routine is put back and the context1 field of
3544  * IOCB is set to NULL to indicate to the lpfc_els_free_iocb() routine that
3545  * there is no ndlp reference available.
3546  *
3547  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
3548  * will be incremented by 1 for holding the ndlp and the reference to ndlp
3549  * will be stored into the context1 field of the IOCB for the completion
3550  * callback function. However, for the RNID Accept Response ELS command,
3551  * this is undone later by this routine after the IOCB is allocated.
3552  *
3553  * Return code
3554  *   0 - Successfully issued acc rnid response
3555  *   1 - Failed to issue acc rnid response
3556  **/
3557 static int
lpfc_els_rsp_rnid_acc(struct lpfc_vport * vport,uint8_t format,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)3558 lpfc_els_rsp_rnid_acc(struct lpfc_vport *vport, uint8_t format,
3559 		      struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
3560 {
3561 	struct lpfc_hba  *phba = vport->phba;
3562 	RNID *rn;
3563 	IOCB_t *icmd, *oldcmd;
3564 	struct lpfc_iocbq *elsiocb;
3565 	struct lpfc_sli_ring *pring;
3566 	struct lpfc_sli *psli;
3567 	uint8_t *pcmd;
3568 	uint16_t cmdsize;
3569 	int rc;
3570 
3571 	psli = &phba->sli;
3572 	pring = &psli->ring[LPFC_ELS_RING];
3573 
3574 	cmdsize = sizeof(uint32_t) + sizeof(uint32_t)
3575 					+ (2 * sizeof(struct lpfc_name));
3576 	if (format)
3577 		cmdsize += sizeof(RNID_TOP_DISC);
3578 
3579 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
3580 				     ndlp->nlp_DID, ELS_CMD_ACC);
3581 	if (!elsiocb)
3582 		return 1;
3583 
3584 	icmd = &elsiocb->iocb;
3585 	oldcmd = &oldiocb->iocb;
3586 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
3587 	/* Xmit RNID ACC response tag <ulpIoTag> */
3588 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
3589 			 "0132 Xmit RNID ACC response tag x%x xri x%x\n",
3590 			 elsiocb->iotag, elsiocb->iocb.ulpContext);
3591 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
3592 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
3593 	pcmd += sizeof(uint32_t);
3594 
3595 	memset(pcmd, 0, sizeof(RNID));
3596 	rn = (RNID *) (pcmd);
3597 	rn->Format = format;
3598 	rn->CommonLen = (2 * sizeof(struct lpfc_name));
3599 	memcpy(&rn->portName, &vport->fc_portname, sizeof(struct lpfc_name));
3600 	memcpy(&rn->nodeName, &vport->fc_nodename, sizeof(struct lpfc_name));
3601 	switch (format) {
3602 	case 0:
3603 		rn->SpecificLen = 0;
3604 		break;
3605 	case RNID_TOPOLOGY_DISC:
3606 		rn->SpecificLen = sizeof(RNID_TOP_DISC);
3607 		memcpy(&rn->un.topologyDisc.portName,
3608 		       &vport->fc_portname, sizeof(struct lpfc_name));
3609 		rn->un.topologyDisc.unitType = RNID_HBA;
3610 		rn->un.topologyDisc.physPort = 0;
3611 		rn->un.topologyDisc.attachedNodes = 0;
3612 		break;
3613 	default:
3614 		rn->CommonLen = 0;
3615 		rn->SpecificLen = 0;
3616 		break;
3617 	}
3618 
3619 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_RSP,
3620 		"Issue ACC RNID:  did:x%x flg:x%x",
3621 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
3622 
3623 	phba->fc_stat.elsXmitACC++;
3624 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
3625 	lpfc_nlp_put(ndlp);
3626 	elsiocb->context1 = NULL;  /* Don't need ndlp for cmpl,
3627 				    * it could be freed */
3628 
3629 	rc = lpfc_sli_issue_iocb(phba, pring, elsiocb, 0);
3630 	if (rc == IOCB_ERROR) {
3631 		lpfc_els_free_iocb(phba, elsiocb);
3632 		return 1;
3633 	}
3634 	return 0;
3635 }
3636 
3637 /**
3638  * lpfc_els_disc_adisc: Issue remaining adisc iocbs to npr nodes of a vport.
3639  * @vport: pointer to a host virtual N_Port data structure.
3640  *
3641  * This routine issues Address Discover (ADISC) ELS commands to those
3642  * N_Ports which are in node port recovery state and ADISC has not been issued
3643  * for the @vport. Each time an ELS ADISC IOCB is issued by invoking the
3644  * lpfc_issue_els_adisc() routine, the per @vport number of discover count
3645  * (num_disc_nodes) shall be incremented. If the num_disc_nodes reaches a
3646  * pre-configured threshold (cfg_discovery_threads), the @vport fc_flag will
3647  * be marked with FC_NLP_MORE bit and the process of issuing remaining ADISC
3648  * IOCBs quit for later pick up. On the other hand, after walking through
3649  * all the ndlps with the @vport and there is none ADISC IOCB issued, the
3650  * @vport fc_flag shall be cleared with FC_NLP_MORE bit indicating there is
3651  * no more ADISC need to be sent.
3652  *
3653  * Return code
3654  *    The number of N_Ports with adisc issued.
3655  **/
3656 int
lpfc_els_disc_adisc(struct lpfc_vport * vport)3657 lpfc_els_disc_adisc(struct lpfc_vport *vport)
3658 {
3659 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3660 	struct lpfc_nodelist *ndlp, *next_ndlp;
3661 	int sentadisc = 0;
3662 
3663 	/* go thru NPR nodes and issue any remaining ELS ADISCs */
3664 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3665 		if (!NLP_CHK_NODE_ACT(ndlp))
3666 			continue;
3667 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3668 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3669 		    (ndlp->nlp_flag & NLP_NPR_ADISC) != 0) {
3670 			spin_lock_irq(shost->host_lock);
3671 			ndlp->nlp_flag &= ~NLP_NPR_ADISC;
3672 			spin_unlock_irq(shost->host_lock);
3673 			ndlp->nlp_prev_state = ndlp->nlp_state;
3674 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_ADISC_ISSUE);
3675 			lpfc_issue_els_adisc(vport, ndlp, 0);
3676 			sentadisc++;
3677 			vport->num_disc_nodes++;
3678 			if (vport->num_disc_nodes >=
3679 			    vport->cfg_discovery_threads) {
3680 				spin_lock_irq(shost->host_lock);
3681 				vport->fc_flag |= FC_NLP_MORE;
3682 				spin_unlock_irq(shost->host_lock);
3683 				break;
3684 			}
3685 		}
3686 	}
3687 	if (sentadisc == 0) {
3688 		spin_lock_irq(shost->host_lock);
3689 		vport->fc_flag &= ~FC_NLP_MORE;
3690 		spin_unlock_irq(shost->host_lock);
3691 	}
3692 	return sentadisc;
3693 }
3694 
3695 /**
3696  * lpfc_els_disc_plogi: Issue plogi for all npr nodes of a vport before adisc.
3697  * @vport: pointer to a host virtual N_Port data structure.
3698  *
3699  * This routine issues Port Login (PLOGI) ELS commands to all the N_Ports
3700  * which are in node port recovery state, with a @vport. Each time an ELS
3701  * ADISC PLOGI IOCB is issued by invoking the lpfc_issue_els_plogi() routine,
3702  * the per @vport number of discover count (num_disc_nodes) shall be
3703  * incremented. If the num_disc_nodes reaches a pre-configured threshold
3704  * (cfg_discovery_threads), the @vport fc_flag will be marked with FC_NLP_MORE
3705  * bit set and quit the process of issuing remaining ADISC PLOGIN IOCBs for
3706  * later pick up. On the other hand, after walking through all the ndlps with
3707  * the @vport and there is none ADISC PLOGI IOCB issued, the @vport fc_flag
3708  * shall be cleared with the FC_NLP_MORE bit indicating there is no more ADISC
3709  * PLOGI need to be sent.
3710  *
3711  * Return code
3712  *   The number of N_Ports with plogi issued.
3713  **/
3714 int
lpfc_els_disc_plogi(struct lpfc_vport * vport)3715 lpfc_els_disc_plogi(struct lpfc_vport *vport)
3716 {
3717 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3718 	struct lpfc_nodelist *ndlp, *next_ndlp;
3719 	int sentplogi = 0;
3720 
3721 	/* go thru NPR nodes and issue any remaining ELS PLOGIs */
3722 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
3723 		if (!NLP_CHK_NODE_ACT(ndlp))
3724 			continue;
3725 		if (ndlp->nlp_state == NLP_STE_NPR_NODE &&
3726 		    (ndlp->nlp_flag & NLP_NPR_2B_DISC) != 0 &&
3727 		    (ndlp->nlp_flag & NLP_DELAY_TMO) == 0 &&
3728 		    (ndlp->nlp_flag & NLP_NPR_ADISC) == 0) {
3729 			ndlp->nlp_prev_state = ndlp->nlp_state;
3730 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
3731 			lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
3732 			sentplogi++;
3733 			vport->num_disc_nodes++;
3734 			if (vport->num_disc_nodes >=
3735 			    vport->cfg_discovery_threads) {
3736 				spin_lock_irq(shost->host_lock);
3737 				vport->fc_flag |= FC_NLP_MORE;
3738 				spin_unlock_irq(shost->host_lock);
3739 				break;
3740 			}
3741 		}
3742 	}
3743 	if (sentplogi) {
3744 		lpfc_set_disctmo(vport);
3745 	}
3746 	else {
3747 		spin_lock_irq(shost->host_lock);
3748 		vport->fc_flag &= ~FC_NLP_MORE;
3749 		spin_unlock_irq(shost->host_lock);
3750 	}
3751 	return sentplogi;
3752 }
3753 
3754 /**
3755  * lpfc_els_flush_rscn: Clean up any rscn activities with a vport.
3756  * @vport: pointer to a host virtual N_Port data structure.
3757  *
3758  * This routine cleans up any Registration State Change Notification
3759  * (RSCN) activity with a @vport. Note that the fc_rscn_flush flag of the
3760  * @vport together with the host_lock is used to prevent multiple thread
3761  * trying to access the RSCN array on a same @vport at the same time.
3762  **/
3763 void
lpfc_els_flush_rscn(struct lpfc_vport * vport)3764 lpfc_els_flush_rscn(struct lpfc_vport *vport)
3765 {
3766 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3767 	struct lpfc_hba  *phba = vport->phba;
3768 	int i;
3769 
3770 	spin_lock_irq(shost->host_lock);
3771 	if (vport->fc_rscn_flush) {
3772 		/* Another thread is walking fc_rscn_id_list on this vport */
3773 		spin_unlock_irq(shost->host_lock);
3774 		return;
3775 	}
3776 	/* Indicate we are walking lpfc_els_flush_rscn on this vport */
3777 	vport->fc_rscn_flush = 1;
3778 	spin_unlock_irq(shost->host_lock);
3779 
3780 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3781 		lpfc_in_buf_free(phba, vport->fc_rscn_id_list[i]);
3782 		vport->fc_rscn_id_list[i] = NULL;
3783 	}
3784 	spin_lock_irq(shost->host_lock);
3785 	vport->fc_rscn_id_cnt = 0;
3786 	vport->fc_flag &= ~(FC_RSCN_MODE | FC_RSCN_DISCOVERY);
3787 	spin_unlock_irq(shost->host_lock);
3788 	lpfc_can_disctmo(vport);
3789 	/* Indicate we are done walking this fc_rscn_id_list */
3790 	vport->fc_rscn_flush = 0;
3791 }
3792 
3793 /**
3794  * lpfc_rscn_payload_check: Check whether there is a pending rscn to a did.
3795  * @vport: pointer to a host virtual N_Port data structure.
3796  * @did: remote destination port identifier.
3797  *
3798  * This routine checks whether there is any pending Registration State
3799  * Configuration Notification (RSCN) to a @did on @vport.
3800  *
3801  * Return code
3802  *   None zero - The @did matched with a pending rscn
3803  *   0 - not able to match @did with a pending rscn
3804  **/
3805 int
lpfc_rscn_payload_check(struct lpfc_vport * vport,uint32_t did)3806 lpfc_rscn_payload_check(struct lpfc_vport *vport, uint32_t did)
3807 {
3808 	D_ID ns_did;
3809 	D_ID rscn_did;
3810 	uint32_t *lp;
3811 	uint32_t payload_len, i;
3812 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3813 
3814 	ns_did.un.word = did;
3815 
3816 	/* Never match fabric nodes for RSCNs */
3817 	if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
3818 		return 0;
3819 
3820 	/* If we are doing a FULL RSCN rediscovery, match everything */
3821 	if (vport->fc_flag & FC_RSCN_DISCOVERY)
3822 		return did;
3823 
3824 	spin_lock_irq(shost->host_lock);
3825 	if (vport->fc_rscn_flush) {
3826 		/* Another thread is walking fc_rscn_id_list on this vport */
3827 		spin_unlock_irq(shost->host_lock);
3828 		return 0;
3829 	}
3830 	/* Indicate we are walking fc_rscn_id_list on this vport */
3831 	vport->fc_rscn_flush = 1;
3832 	spin_unlock_irq(shost->host_lock);
3833 	for (i = 0; i < vport->fc_rscn_id_cnt; i++) {
3834 		lp = vport->fc_rscn_id_list[i]->virt;
3835 		payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3836 		payload_len -= sizeof(uint32_t);	/* take off word 0 */
3837 		while (payload_len) {
3838 			rscn_did.un.word = be32_to_cpu(*lp++);
3839 			payload_len -= sizeof(uint32_t);
3840 			switch (rscn_did.un.b.resv & RSCN_ADDRESS_FORMAT_MASK) {
3841 			case RSCN_ADDRESS_FORMAT_PORT:
3842 				if (ns_did.un.word == rscn_did.un.word)
3843 					goto return_did_out;
3844 				break;
3845 			case RSCN_ADDRESS_FORMAT_AREA:
3846 				if ((ns_did.un.b.domain == rscn_did.un.b.domain)
3847 				    && (ns_did.un.b.area == rscn_did.un.b.area))
3848 					goto return_did_out;
3849 				break;
3850 			case RSCN_ADDRESS_FORMAT_DOMAIN:
3851 				if (ns_did.un.b.domain == rscn_did.un.b.domain)
3852 					goto return_did_out;
3853 				break;
3854 			case RSCN_ADDRESS_FORMAT_FABRIC:
3855 				goto return_did_out;
3856 			}
3857 		}
3858 	}
3859 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
3860 	vport->fc_rscn_flush = 0;
3861 	return 0;
3862 return_did_out:
3863 	/* Indicate we are done with walking fc_rscn_id_list on this vport */
3864 	vport->fc_rscn_flush = 0;
3865 	return did;
3866 }
3867 
3868 /**
3869  * lpfc_rscn_recovery_check: Send recovery event to vport nodes matching rscn
3870  * @vport: pointer to a host virtual N_Port data structure.
3871  *
3872  * This routine sends recovery (NLP_EVT_DEVICE_RECOVERY) event to the
3873  * state machine for a @vport's nodes that are with pending RSCN (Registration
3874  * State Change Notification).
3875  *
3876  * Return code
3877  *   0 - Successful (currently alway return 0)
3878  **/
3879 static int
lpfc_rscn_recovery_check(struct lpfc_vport * vport)3880 lpfc_rscn_recovery_check(struct lpfc_vport *vport)
3881 {
3882 	struct lpfc_nodelist *ndlp = NULL;
3883 
3884 	/* Move all affected nodes by pending RSCNs to NPR state. */
3885 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3886 		if (!NLP_CHK_NODE_ACT(ndlp) ||
3887 		    (ndlp->nlp_state == NLP_STE_UNUSED_NODE) ||
3888 		    !lpfc_rscn_payload_check(vport, ndlp->nlp_DID))
3889 			continue;
3890 		lpfc_disc_state_machine(vport, ndlp, NULL,
3891 					NLP_EVT_DEVICE_RECOVERY);
3892 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
3893 	}
3894 	return 0;
3895 }
3896 
3897 /**
3898  * lpfc_send_rscn_event: Send an RSCN event to management application.
3899  * @vport: pointer to a host virtual N_Port data structure.
3900  * @cmdiocb: pointer to lpfc command iocb data structure.
3901  *
3902  * lpfc_send_rscn_event sends an RSCN netlink event to management
3903  * applications.
3904  */
3905 static void
lpfc_send_rscn_event(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb)3906 lpfc_send_rscn_event(struct lpfc_vport *vport,
3907 		struct lpfc_iocbq *cmdiocb)
3908 {
3909 	struct lpfc_dmabuf *pcmd;
3910 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3911 	uint32_t *payload_ptr;
3912 	uint32_t payload_len;
3913 	struct lpfc_rscn_event_header *rscn_event_data;
3914 
3915 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3916 	payload_ptr = (uint32_t *) pcmd->virt;
3917 	payload_len = be32_to_cpu(*payload_ptr & ~ELS_CMD_MASK);
3918 
3919 	rscn_event_data = kmalloc(sizeof(struct lpfc_rscn_event_header) +
3920 		payload_len, GFP_KERNEL);
3921 	if (!rscn_event_data) {
3922 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3923 			"0147 Failed to allocate memory for RSCN event\n");
3924 		return;
3925 	}
3926 	rscn_event_data->event_type = FC_REG_RSCN_EVENT;
3927 	rscn_event_data->payload_length = payload_len;
3928 	memcpy(rscn_event_data->rscn_payload, payload_ptr,
3929 		payload_len);
3930 
3931 	fc_host_post_vendor_event(shost,
3932 		fc_get_event_number(),
3933 		sizeof(struct lpfc_els_event_header) + payload_len,
3934 		(char *)rscn_event_data,
3935 		LPFC_NL_VENDOR_ID);
3936 
3937 	kfree(rscn_event_data);
3938 }
3939 
3940 /**
3941  * lpfc_els_rcv_rscn: Process an unsolicited rscn iocb.
3942  * @vport: pointer to a host virtual N_Port data structure.
3943  * @cmdiocb: pointer to lpfc command iocb data structure.
3944  * @ndlp: pointer to a node-list data structure.
3945  *
3946  * This routine processes an unsolicited RSCN (Registration State Change
3947  * Notification) IOCB. First, the payload of the unsolicited RSCN is walked
3948  * to invoke fc_host_post_event() routine to the FC transport layer. If the
3949  * discover state machine is about to begin discovery, it just accepts the
3950  * RSCN and the discovery process will satisfy the RSCN. If this RSCN only
3951  * contains N_Port IDs for other vports on this HBA, it just accepts the
3952  * RSCN and ignore processing it. If the state machine is in the recovery
3953  * state, the fc_rscn_id_list of this @vport is walked and the
3954  * lpfc_rscn_recovery_check() routine is invoked to send recovery event for
3955  * all nodes that match RSCN payload. Otherwise, the lpfc_els_handle_rscn()
3956  * routine is invoked to handle the RSCN event.
3957  *
3958  * Return code
3959  *   0 - Just sent the acc response
3960  *   1 - Sent the acc response and waited for name server completion
3961  **/
3962 static int
lpfc_els_rcv_rscn(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)3963 lpfc_els_rcv_rscn(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
3964 		  struct lpfc_nodelist *ndlp)
3965 {
3966 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3967 	struct lpfc_hba  *phba = vport->phba;
3968 	struct lpfc_dmabuf *pcmd;
3969 	uint32_t *lp, *datap;
3970 	IOCB_t *icmd;
3971 	uint32_t payload_len, length, nportid, *cmd;
3972 	int rscn_cnt;
3973 	int rscn_id = 0, hba_id = 0;
3974 	int i;
3975 
3976 	icmd = &cmdiocb->iocb;
3977 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
3978 	lp = (uint32_t *) pcmd->virt;
3979 
3980 	payload_len = be32_to_cpu(*lp++ & ~ELS_CMD_MASK);
3981 	payload_len -= sizeof(uint32_t);	/* take off word 0 */
3982 	/* RSCN received */
3983 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
3984 			 "0214 RSCN received Data: x%x x%x x%x x%x\n",
3985 			 vport->fc_flag, payload_len, *lp,
3986 			 vport->fc_rscn_id_cnt);
3987 
3988 	/* Send an RSCN event to the management application */
3989 	lpfc_send_rscn_event(vport, cmdiocb);
3990 
3991 	for (i = 0; i < payload_len/sizeof(uint32_t); i++)
3992 		fc_host_post_event(shost, fc_get_event_number(),
3993 			FCH_EVT_RSCN, lp[i]);
3994 
3995 	/* If we are about to begin discovery, just ACC the RSCN.
3996 	 * Discovery processing will satisfy it.
3997 	 */
3998 	if (vport->port_state <= LPFC_NS_QRY) {
3999 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4000 			"RCV RSCN ignore: did:x%x/ste:x%x flg:x%x",
4001 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4002 
4003 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4004 		return 0;
4005 	}
4006 
4007 	/* If this RSCN just contains NPortIDs for other vports on this HBA,
4008 	 * just ACC and ignore it.
4009 	 */
4010 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
4011 		!(vport->cfg_peer_port_login)) {
4012 		i = payload_len;
4013 		datap = lp;
4014 		while (i > 0) {
4015 			nportid = *datap++;
4016 			nportid = ((be32_to_cpu(nportid)) & Mask_DID);
4017 			i -= sizeof(uint32_t);
4018 			rscn_id++;
4019 			if (lpfc_find_vport_by_did(phba, nportid))
4020 				hba_id++;
4021 		}
4022 		if (rscn_id == hba_id) {
4023 			/* ALL NPortIDs in RSCN are on HBA */
4024 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4025 					 "0219 Ignore RSCN "
4026 					 "Data: x%x x%x x%x x%x\n",
4027 					 vport->fc_flag, payload_len,
4028 					 *lp, vport->fc_rscn_id_cnt);
4029 			lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4030 				"RCV RSCN vport:  did:x%x/ste:x%x flg:x%x",
4031 				ndlp->nlp_DID, vport->port_state,
4032 				ndlp->nlp_flag);
4033 
4034 			lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb,
4035 				ndlp, NULL);
4036 			return 0;
4037 		}
4038 	}
4039 
4040 	spin_lock_irq(shost->host_lock);
4041 	if (vport->fc_rscn_flush) {
4042 		/* Another thread is walking fc_rscn_id_list on this vport */
4043 		spin_unlock_irq(shost->host_lock);
4044 		vport->fc_flag |= FC_RSCN_DISCOVERY;
4045 		/* Send back ACC */
4046 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4047 		return 0;
4048 	}
4049 	/* Indicate we are walking fc_rscn_id_list on this vport */
4050 	vport->fc_rscn_flush = 1;
4051 	spin_unlock_irq(shost->host_lock);
4052 	/* Get the array count after sucessfully have the token */
4053 	rscn_cnt = vport->fc_rscn_id_cnt;
4054 	/* If we are already processing an RSCN, save the received
4055 	 * RSCN payload buffer, cmdiocb->context2 to process later.
4056 	 */
4057 	if (vport->fc_flag & (FC_RSCN_MODE | FC_NDISC_ACTIVE)) {
4058 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4059 			"RCV RSCN defer:  did:x%x/ste:x%x flg:x%x",
4060 			ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4061 
4062 		spin_lock_irq(shost->host_lock);
4063 		vport->fc_flag |= FC_RSCN_DEFERRED;
4064 		if ((rscn_cnt < FC_MAX_HOLD_RSCN) &&
4065 		    !(vport->fc_flag & FC_RSCN_DISCOVERY)) {
4066 			vport->fc_flag |= FC_RSCN_MODE;
4067 			spin_unlock_irq(shost->host_lock);
4068 			if (rscn_cnt) {
4069 				cmd = vport->fc_rscn_id_list[rscn_cnt-1]->virt;
4070 				length = be32_to_cpu(*cmd & ~ELS_CMD_MASK);
4071 			}
4072 			if ((rscn_cnt) &&
4073 			    (payload_len + length <= LPFC_BPL_SIZE)) {
4074 				*cmd &= ELS_CMD_MASK;
4075 				*cmd |= cpu_to_be32(payload_len + length);
4076 				memcpy(((uint8_t *)cmd) + length, lp,
4077 				       payload_len);
4078 			} else {
4079 				vport->fc_rscn_id_list[rscn_cnt] = pcmd;
4080 				vport->fc_rscn_id_cnt++;
4081 				/* If we zero, cmdiocb->context2, the calling
4082 				 * routine will not try to free it.
4083 				 */
4084 				cmdiocb->context2 = NULL;
4085 			}
4086 			/* Deferred RSCN */
4087 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4088 					 "0235 Deferred RSCN "
4089 					 "Data: x%x x%x x%x\n",
4090 					 vport->fc_rscn_id_cnt, vport->fc_flag,
4091 					 vport->port_state);
4092 		} else {
4093 			vport->fc_flag |= FC_RSCN_DISCOVERY;
4094 			spin_unlock_irq(shost->host_lock);
4095 			/* ReDiscovery RSCN */
4096 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4097 					 "0234 ReDiscovery RSCN "
4098 					 "Data: x%x x%x x%x\n",
4099 					 vport->fc_rscn_id_cnt, vport->fc_flag,
4100 					 vport->port_state);
4101 		}
4102 		/* Indicate we are done walking fc_rscn_id_list on this vport */
4103 		vport->fc_rscn_flush = 0;
4104 		/* Send back ACC */
4105 		lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4106 		/* send RECOVERY event for ALL nodes that match RSCN payload */
4107 		lpfc_rscn_recovery_check(vport);
4108 		spin_lock_irq(shost->host_lock);
4109 		vport->fc_flag &= ~FC_RSCN_DEFERRED;
4110 		spin_unlock_irq(shost->host_lock);
4111 		return 0;
4112 	}
4113 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
4114 		"RCV RSCN:        did:x%x/ste:x%x flg:x%x",
4115 		ndlp->nlp_DID, vport->port_state, ndlp->nlp_flag);
4116 
4117 	spin_lock_irq(shost->host_lock);
4118 	vport->fc_flag |= FC_RSCN_MODE;
4119 	spin_unlock_irq(shost->host_lock);
4120 	vport->fc_rscn_id_list[vport->fc_rscn_id_cnt++] = pcmd;
4121 	/* Indicate we are done walking fc_rscn_id_list on this vport */
4122 	vport->fc_rscn_flush = 0;
4123 	/*
4124 	 * If we zero, cmdiocb->context2, the calling routine will
4125 	 * not try to free it.
4126 	 */
4127 	cmdiocb->context2 = NULL;
4128 	lpfc_set_disctmo(vport);
4129 	/* Send back ACC */
4130 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4131 	/* send RECOVERY event for ALL nodes that match RSCN payload */
4132 	lpfc_rscn_recovery_check(vport);
4133 	return lpfc_els_handle_rscn(vport);
4134 }
4135 
4136 /**
4137  * lpfc_els_handle_rscn: Handle rscn for a vport.
4138  * @vport: pointer to a host virtual N_Port data structure.
4139  *
4140  * This routine handles the Registration State Configuration Notification
4141  * (RSCN) for a @vport. If login to NameServer does not exist, a new ndlp shall
4142  * be created and a Port Login (PLOGI) to the NameServer is issued. Otherwise,
4143  * if the ndlp to NameServer exists, a Common Transport (CT) command to the
4144  * NameServer shall be issued. If CT command to the NameServer fails to be
4145  * issued, the lpfc_els_flush_rscn() routine shall be invoked to clean up any
4146  * RSCN activities with the @vport.
4147  *
4148  * Return code
4149  *   0 - Cleaned up rscn on the @vport
4150  *   1 - Wait for plogi to name server before proceed
4151  **/
4152 int
lpfc_els_handle_rscn(struct lpfc_vport * vport)4153 lpfc_els_handle_rscn(struct lpfc_vport *vport)
4154 {
4155 	struct lpfc_nodelist *ndlp;
4156 	struct lpfc_hba *phba = vport->phba;
4157 
4158 	/* Ignore RSCN if the port is being torn down. */
4159 	if (vport->load_flag & FC_UNLOADING) {
4160 		lpfc_els_flush_rscn(vport);
4161 		return 0;
4162 	}
4163 
4164 	/* Start timer for RSCN processing */
4165 	lpfc_set_disctmo(vport);
4166 
4167 	/* RSCN processed */
4168 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4169 			 "0215 RSCN processed Data: x%x x%x x%x x%x\n",
4170 			 vport->fc_flag, 0, vport->fc_rscn_id_cnt,
4171 			 vport->port_state);
4172 
4173 	/* To process RSCN, first compare RSCN data with NameServer */
4174 	vport->fc_ns_retry = 0;
4175 	vport->num_disc_nodes = 0;
4176 
4177 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
4178 	if (ndlp && NLP_CHK_NODE_ACT(ndlp)
4179 	    && ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
4180 		/* Good ndlp, issue CT Request to NameServer */
4181 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, 0) == 0)
4182 			/* Wait for NameServer query cmpl before we can
4183 			   continue */
4184 			return 1;
4185 	} else {
4186 		/* If login to NameServer does not exist, issue one */
4187 		/* Good status, issue PLOGI to NameServer */
4188 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
4189 		if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4190 			/* Wait for NameServer login cmpl before we can
4191 			   continue */
4192 			return 1;
4193 
4194 		if (ndlp) {
4195 			ndlp = lpfc_enable_node(vport, ndlp,
4196 						NLP_STE_PLOGI_ISSUE);
4197 			if (!ndlp) {
4198 				lpfc_els_flush_rscn(vport);
4199 				return 0;
4200 			}
4201 			ndlp->nlp_prev_state = NLP_STE_UNUSED_NODE;
4202 		} else {
4203 			ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
4204 			if (!ndlp) {
4205 				lpfc_els_flush_rscn(vport);
4206 				return 0;
4207 			}
4208 			lpfc_nlp_init(vport, ndlp, NameServer_DID);
4209 			ndlp->nlp_prev_state = ndlp->nlp_state;
4210 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
4211 		}
4212 		ndlp->nlp_type |= NLP_FABRIC;
4213 		lpfc_issue_els_plogi(vport, NameServer_DID, 0);
4214 		/* Wait for NameServer login cmpl before we can
4215 		 * continue
4216 		 */
4217 		return 1;
4218 	}
4219 
4220 	lpfc_els_flush_rscn(vport);
4221 	return 0;
4222 }
4223 
4224 /**
4225  * lpfc_els_rcv_flogi: Process an unsolicited flogi iocb.
4226  * @vport: pointer to a host virtual N_Port data structure.
4227  * @cmdiocb: pointer to lpfc command iocb data structure.
4228  * @ndlp: pointer to a node-list data structure.
4229  *
4230  * This routine processes Fabric Login (FLOGI) IOCB received as an ELS
4231  * unsolicited event. An unsolicited FLOGI can be received in a point-to-
4232  * point topology. As an unsolicited FLOGI should not be received in a loop
4233  * mode, any unsolicited FLOGI received in loop mode shall be ignored. The
4234  * lpfc_check_sparm() routine is invoked to check the parameters in the
4235  * unsolicited FLOGI. If parameters validation failed, the routine
4236  * lpfc_els_rsp_reject() shall be called with reject reason code set to
4237  * LSEXP_SPARM_OPTIONS to reject the FLOGI. Otherwise, the Port WWN in the
4238  * FLOGI shall be compared with the Port WWN of the @vport to determine who
4239  * will initiate PLOGI. The higher lexicographical value party shall has
4240  * higher priority (as the winning port) and will initiate PLOGI and
4241  * communicate Port_IDs (Addresses) for both nodes in PLOGI. The result
4242  * of this will be marked in the @vport fc_flag field with FC_PT2PT_PLOGI
4243  * and then the lpfc_els_rsp_acc() routine is invoked to accept the FLOGI.
4244  *
4245  * Return code
4246  *   0 - Successfully processed the unsolicited flogi
4247  *   1 - Failed to process the unsolicited flogi
4248  **/
4249 static int
lpfc_els_rcv_flogi(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4250 lpfc_els_rcv_flogi(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4251 		   struct lpfc_nodelist *ndlp)
4252 {
4253 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4254 	struct lpfc_hba  *phba = vport->phba;
4255 	struct lpfc_dmabuf *pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4256 	uint32_t *lp = (uint32_t *) pcmd->virt;
4257 	IOCB_t *icmd = &cmdiocb->iocb;
4258 	struct serv_parm *sp;
4259 	LPFC_MBOXQ_t *mbox;
4260 	struct ls_rjt stat;
4261 	uint32_t cmd, did;
4262 	int rc;
4263 
4264 	cmd = *lp++;
4265 	sp = (struct serv_parm *) lp;
4266 
4267 	/* FLOGI received */
4268 
4269 	lpfc_set_disctmo(vport);
4270 
4271 	if (phba->fc_topology == TOPOLOGY_LOOP) {
4272 		/* We should never receive a FLOGI in loop mode, ignore it */
4273 		did = icmd->un.elsreq64.remoteID;
4274 
4275 		/* An FLOGI ELS command <elsCmd> was received from DID <did> in
4276 		   Loop Mode */
4277 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4278 				 "0113 An FLOGI ELS command x%x was "
4279 				 "received from DID x%x in Loop Mode\n",
4280 				 cmd, did);
4281 		return 1;
4282 	}
4283 
4284 	did = Fabric_DID;
4285 
4286 	if ((lpfc_check_sparm(vport, ndlp, sp, CLASS3))) {
4287 		/* For a FLOGI we accept, then if our portname is greater
4288 		 * then the remote portname we initiate Nport login.
4289 		 */
4290 
4291 		rc = memcmp(&vport->fc_portname, &sp->portName,
4292 			    sizeof(struct lpfc_name));
4293 
4294 		if (!rc) {
4295 			mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4296 			if (!mbox)
4297 				return 1;
4298 
4299 			lpfc_linkdown(phba);
4300 			lpfc_init_link(phba, mbox,
4301 				       phba->cfg_topology,
4302 				       phba->cfg_link_speed);
4303 			mbox->mb.un.varInitLnk.lipsr_AL_PA = 0;
4304 			mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4305 			mbox->vport = vport;
4306 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4307 			lpfc_set_loopback_flag(phba);
4308 			if (rc == MBX_NOT_FINISHED) {
4309 				mempool_free(mbox, phba->mbox_mem_pool);
4310 			}
4311 			return 1;
4312 		} else if (rc > 0) {	/* greater than */
4313 			spin_lock_irq(shost->host_lock);
4314 			vport->fc_flag |= FC_PT2PT_PLOGI;
4315 			spin_unlock_irq(shost->host_lock);
4316 		}
4317 		spin_lock_irq(shost->host_lock);
4318 		vport->fc_flag |= FC_PT2PT;
4319 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4320 		spin_unlock_irq(shost->host_lock);
4321 	} else {
4322 		/* Reject this request because invalid parameters */
4323 		stat.un.b.lsRjtRsvd0 = 0;
4324 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4325 		stat.un.b.lsRjtRsnCodeExp = LSEXP_SPARM_OPTIONS;
4326 		stat.un.b.vendorUnique = 0;
4327 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4328 			NULL);
4329 		return 1;
4330 	}
4331 
4332 	/* Send back ACC */
4333 	lpfc_els_rsp_acc(vport, ELS_CMD_PLOGI, cmdiocb, ndlp, NULL);
4334 
4335 	return 0;
4336 }
4337 
4338 /**
4339  * lpfc_els_rcv_rnid: Process an unsolicited rnid iocb.
4340  * @vport: pointer to a host virtual N_Port data structure.
4341  * @cmdiocb: pointer to lpfc command iocb data structure.
4342  * @ndlp: pointer to a node-list data structure.
4343  *
4344  * This routine processes Request Node Identification Data (RNID) IOCB
4345  * received as an ELS unsolicited event. Only when the RNID specified format
4346  * 0x0 or 0xDF (Topology Discovery Specific Node Identification Data)
4347  * present, this routine will invoke the lpfc_els_rsp_rnid_acc() routine to
4348  * Accept (ACC) the RNID ELS command. All the other RNID formats are
4349  * rejected by invoking the lpfc_els_rsp_reject() routine.
4350  *
4351  * Return code
4352  *   0 - Successfully processed rnid iocb (currently always return 0)
4353  **/
4354 static int
lpfc_els_rcv_rnid(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4355 lpfc_els_rcv_rnid(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4356 		  struct lpfc_nodelist *ndlp)
4357 {
4358 	struct lpfc_dmabuf *pcmd;
4359 	uint32_t *lp;
4360 	IOCB_t *icmd;
4361 	RNID *rn;
4362 	struct ls_rjt stat;
4363 	uint32_t cmd, did;
4364 
4365 	icmd = &cmdiocb->iocb;
4366 	did = icmd->un.elsreq64.remoteID;
4367 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4368 	lp = (uint32_t *) pcmd->virt;
4369 
4370 	cmd = *lp++;
4371 	rn = (RNID *) lp;
4372 
4373 	/* RNID received */
4374 
4375 	switch (rn->Format) {
4376 	case 0:
4377 	case RNID_TOPOLOGY_DISC:
4378 		/* Send back ACC */
4379 		lpfc_els_rsp_rnid_acc(vport, rn->Format, cmdiocb, ndlp);
4380 		break;
4381 	default:
4382 		/* Reject this request because format not supported */
4383 		stat.un.b.lsRjtRsvd0 = 0;
4384 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4385 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4386 		stat.un.b.vendorUnique = 0;
4387 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4388 			NULL);
4389 	}
4390 	return 0;
4391 }
4392 
4393 /**
4394  * lpfc_els_rcv_lirr: Process an unsolicited lirr iocb.
4395  * @vport: pointer to a host virtual N_Port data structure.
4396  * @cmdiocb: pointer to lpfc command iocb data structure.
4397  * @ndlp: pointer to a node-list data structure.
4398  *
4399  * This routine processes a Link Incident Report Registration(LIRR) IOCB
4400  * received as an ELS unsolicited event. Currently, this function just invokes
4401  * the lpfc_els_rsp_reject() routine to reject the LIRR IOCB unconditionally.
4402  *
4403  * Return code
4404  *   0 - Successfully processed lirr iocb (currently always return 0)
4405  **/
4406 static int
lpfc_els_rcv_lirr(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4407 lpfc_els_rcv_lirr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4408 		  struct lpfc_nodelist *ndlp)
4409 {
4410 	struct ls_rjt stat;
4411 
4412 	/* For now, unconditionally reject this command */
4413 	stat.un.b.lsRjtRsvd0 = 0;
4414 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4415 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4416 	stat.un.b.vendorUnique = 0;
4417 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4418 	return 0;
4419 }
4420 
4421 /**
4422  * lpfc_els_rsp_rps_acc: Completion callbk func for MBX_READ_LNK_STAT mbox cmd.
4423  * @phba: pointer to lpfc hba data structure.
4424  * @pmb: pointer to the driver internal queue element for mailbox command.
4425  *
4426  * This routine is the completion callback function for the MBX_READ_LNK_STAT
4427  * mailbox command. This callback function is to actually send the Accept
4428  * (ACC) response to a Read Port Status (RPS) unsolicited IOCB event. It
4429  * collects the link statistics from the completion of the MBX_READ_LNK_STAT
4430  * mailbox command, constructs the RPS response with the link statistics
4431  * collected, and then invokes the lpfc_sli_issue_iocb() routine to send ACC
4432  * response to the RPS.
4433  *
4434  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4435  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4436  * will be stored into the context1 field of the IOCB for the completion
4437  * callback function to the RPS Accept Response ELS IOCB command.
4438  *
4439  **/
4440 static void
lpfc_els_rsp_rps_acc(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)4441 lpfc_els_rsp_rps_acc(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4442 {
4443 	struct lpfc_sli *psli = &phba->sli;
4444 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4445 	MAILBOX_t *mb;
4446 	IOCB_t *icmd;
4447 	RPS_RSP *rps_rsp;
4448 	uint8_t *pcmd;
4449 	struct lpfc_iocbq *elsiocb;
4450 	struct lpfc_nodelist *ndlp;
4451 	uint16_t xri, status;
4452 	uint32_t cmdsize;
4453 
4454 	mb = &pmb->mb;
4455 
4456 	ndlp = (struct lpfc_nodelist *) pmb->context2;
4457 	xri = (uint16_t) ((unsigned long)(pmb->context1));
4458 	pmb->context1 = NULL;
4459 	pmb->context2 = NULL;
4460 
4461 	if (mb->mbxStatus) {
4462 		mempool_free(pmb, phba->mbox_mem_pool);
4463 		return;
4464 	}
4465 
4466 	cmdsize = sizeof(RPS_RSP) + sizeof(uint32_t);
4467 	mempool_free(pmb, phba->mbox_mem_pool);
4468 	elsiocb = lpfc_prep_els_iocb(phba->pport, 0, cmdsize,
4469 				     lpfc_max_els_tries, ndlp,
4470 				     ndlp->nlp_DID, ELS_CMD_ACC);
4471 
4472 	/* Decrement the ndlp reference count from previous mbox command */
4473 	lpfc_nlp_put(ndlp);
4474 
4475 	if (!elsiocb)
4476 		return;
4477 
4478 	icmd = &elsiocb->iocb;
4479 	icmd->ulpContext = xri;
4480 
4481 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4482 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4483 	pcmd += sizeof(uint32_t); /* Skip past command */
4484 	rps_rsp = (RPS_RSP *)pcmd;
4485 
4486 	if (phba->fc_topology != TOPOLOGY_LOOP)
4487 		status = 0x10;
4488 	else
4489 		status = 0x8;
4490 	if (phba->pport->fc_flag & FC_FABRIC)
4491 		status |= 0x4;
4492 
4493 	rps_rsp->rsvd1 = 0;
4494 	rps_rsp->portStatus = cpu_to_be16(status);
4495 	rps_rsp->linkFailureCnt = cpu_to_be32(mb->un.varRdLnk.linkFailureCnt);
4496 	rps_rsp->lossSyncCnt = cpu_to_be32(mb->un.varRdLnk.lossSyncCnt);
4497 	rps_rsp->lossSignalCnt = cpu_to_be32(mb->un.varRdLnk.lossSignalCnt);
4498 	rps_rsp->primSeqErrCnt = cpu_to_be32(mb->un.varRdLnk.primSeqErrCnt);
4499 	rps_rsp->invalidXmitWord = cpu_to_be32(mb->un.varRdLnk.invalidXmitWord);
4500 	rps_rsp->crcCnt = cpu_to_be32(mb->un.varRdLnk.crcCnt);
4501 	/* Xmit ELS RPS ACC response tag <ulpIoTag> */
4502 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_ELS,
4503 			 "0118 Xmit ELS RPS ACC response tag x%x xri x%x, "
4504 			 "did x%x, nlp_flag x%x, nlp_state x%x, rpi x%x\n",
4505 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
4506 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4507 			 ndlp->nlp_rpi);
4508 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4509 	phba->fc_stat.elsXmitACC++;
4510 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR)
4511 		lpfc_els_free_iocb(phba, elsiocb);
4512 	return;
4513 }
4514 
4515 /**
4516  * lpfc_els_rcv_rps: Process an unsolicited rps iocb.
4517  * @vport: pointer to a host virtual N_Port data structure.
4518  * @cmdiocb: pointer to lpfc command iocb data structure.
4519  * @ndlp: pointer to a node-list data structure.
4520  *
4521  * This routine processes Read Port Status (RPS) IOCB received as an
4522  * ELS unsolicited event. It first checks the remote port state. If the
4523  * remote port is not in NLP_STE_UNMAPPED_NODE state or NLP_STE_MAPPED_NODE
4524  * state, it invokes the lpfc_els_rsp_reject() routine to send the reject
4525  * response. Otherwise, it issue the MBX_READ_LNK_STAT mailbox command
4526  * for reading the HBA link statistics. It is for the callback function,
4527  * lpfc_els_rsp_rps_acc(), set to the MBX_READ_LNK_STAT mailbox command
4528  * to actually sending out RPS Accept (ACC) response.
4529  *
4530  * Return codes
4531  *   0 - Successfully processed rps iocb (currently always return 0)
4532  **/
4533 static int
lpfc_els_rcv_rps(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4534 lpfc_els_rcv_rps(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4535 		 struct lpfc_nodelist *ndlp)
4536 {
4537 	struct lpfc_hba *phba = vport->phba;
4538 	uint32_t *lp;
4539 	uint8_t flag;
4540 	LPFC_MBOXQ_t *mbox;
4541 	struct lpfc_dmabuf *pcmd;
4542 	RPS *rps;
4543 	struct ls_rjt stat;
4544 
4545 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4546 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))
4547 		/* reject the unsolicited RPS request and done with it */
4548 		goto reject_out;
4549 
4550 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4551 	lp = (uint32_t *) pcmd->virt;
4552 	flag = (be32_to_cpu(*lp++) & 0xf);
4553 	rps = (RPS *) lp;
4554 
4555 	if ((flag == 0) ||
4556 	    ((flag == 1) && (be32_to_cpu(rps->un.portNum) == 0)) ||
4557 	    ((flag == 2) && (memcmp(&rps->un.portName, &vport->fc_portname,
4558 				    sizeof(struct lpfc_name)) == 0))) {
4559 
4560 		printk("Fix me....\n");
4561 		dump_stack();
4562 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_ATOMIC);
4563 		if (mbox) {
4564 			lpfc_read_lnk_stat(phba, mbox);
4565 			mbox->context1 =
4566 			    (void *)((unsigned long) cmdiocb->iocb.ulpContext);
4567 			mbox->context2 = lpfc_nlp_get(ndlp);
4568 			mbox->vport = vport;
4569 			mbox->mbox_cmpl = lpfc_els_rsp_rps_acc;
4570 			if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
4571 				!= MBX_NOT_FINISHED)
4572 				/* Mbox completion will send ELS Response */
4573 				return 0;
4574 			/* Decrement reference count used for the failed mbox
4575 			 * command.
4576 			 */
4577 			lpfc_nlp_put(ndlp);
4578 			mempool_free(mbox, phba->mbox_mem_pool);
4579 		}
4580 	}
4581 
4582 reject_out:
4583 	/* issue rejection response */
4584 	stat.un.b.lsRjtRsvd0 = 0;
4585 	stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4586 	stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4587 	stat.un.b.vendorUnique = 0;
4588 	lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp, NULL);
4589 	return 0;
4590 }
4591 
4592 /**
4593  * lpfc_els_rsp_rpl_acc: Issue an accept rpl els command.
4594  * @vport: pointer to a host virtual N_Port data structure.
4595  * @cmdsize: size of the ELS command.
4596  * @oldiocb: pointer to the original lpfc command iocb data structure.
4597  * @ndlp: pointer to a node-list data structure.
4598  *
4599  * This routine issuees an Accept (ACC) Read Port List (RPL) ELS command.
4600  * It is to be called by the lpfc_els_rcv_rpl() routine to accept the RPL.
4601  *
4602  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
4603  * will be incremented by 1 for holding the ndlp and the reference to ndlp
4604  * will be stored into the context1 field of the IOCB for the completion
4605  * callback function to the RPL Accept Response ELS command.
4606  *
4607  * Return code
4608  *   0 - Successfully issued ACC RPL ELS command
4609  *   1 - Failed to issue ACC RPL ELS command
4610  **/
4611 static int
lpfc_els_rsp_rpl_acc(struct lpfc_vport * vport,uint16_t cmdsize,struct lpfc_iocbq * oldiocb,struct lpfc_nodelist * ndlp)4612 lpfc_els_rsp_rpl_acc(struct lpfc_vport *vport, uint16_t cmdsize,
4613 		     struct lpfc_iocbq *oldiocb, struct lpfc_nodelist *ndlp)
4614 {
4615 	struct lpfc_hba *phba = vport->phba;
4616 	IOCB_t *icmd, *oldcmd;
4617 	RPL_RSP rpl_rsp;
4618 	struct lpfc_iocbq *elsiocb;
4619 	struct lpfc_sli *psli = &phba->sli;
4620 	struct lpfc_sli_ring *pring = &psli->ring[LPFC_ELS_RING];
4621 	uint8_t *pcmd;
4622 
4623 	elsiocb = lpfc_prep_els_iocb(vport, 0, cmdsize, oldiocb->retry, ndlp,
4624 				     ndlp->nlp_DID, ELS_CMD_ACC);
4625 
4626 	if (!elsiocb)
4627 		return 1;
4628 
4629 	icmd = &elsiocb->iocb;
4630 	oldcmd = &oldiocb->iocb;
4631 	icmd->ulpContext = oldcmd->ulpContext;	/* Xri */
4632 
4633 	pcmd = (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
4634 	*((uint32_t *) (pcmd)) = ELS_CMD_ACC;
4635 	pcmd += sizeof(uint16_t);
4636 	*((uint16_t *)(pcmd)) = be16_to_cpu(cmdsize);
4637 	pcmd += sizeof(uint16_t);
4638 
4639 	/* Setup the RPL ACC payload */
4640 	rpl_rsp.listLen = be32_to_cpu(1);
4641 	rpl_rsp.index = 0;
4642 	rpl_rsp.port_num_blk.portNum = 0;
4643 	rpl_rsp.port_num_blk.portID = be32_to_cpu(vport->fc_myDID);
4644 	memcpy(&rpl_rsp.port_num_blk.portName, &vport->fc_portname,
4645 	    sizeof(struct lpfc_name));
4646 	memcpy(pcmd, &rpl_rsp, cmdsize - sizeof(uint32_t));
4647 	/* Xmit ELS RPL ACC response tag <ulpIoTag> */
4648 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4649 			 "0120 Xmit ELS RPL ACC response tag x%x "
4650 			 "xri x%x, did x%x, nlp_flag x%x, nlp_state x%x, "
4651 			 "rpi x%x\n",
4652 			 elsiocb->iotag, elsiocb->iocb.ulpContext,
4653 			 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_state,
4654 			 ndlp->nlp_rpi);
4655 	elsiocb->iocb_cmpl = lpfc_cmpl_els_rsp;
4656 	phba->fc_stat.elsXmitACC++;
4657 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
4658 		lpfc_els_free_iocb(phba, elsiocb);
4659 		return 1;
4660 	}
4661 	return 0;
4662 }
4663 
4664 /**
4665  * lpfc_els_rcv_rpl: Process an unsolicited rpl iocb.
4666  * @vport: pointer to a host virtual N_Port data structure.
4667  * @cmdiocb: pointer to lpfc command iocb data structure.
4668  * @ndlp: pointer to a node-list data structure.
4669  *
4670  * This routine processes Read Port List (RPL) IOCB received as an ELS
4671  * unsolicited event. It first checks the remote port state. If the remote
4672  * port is not in NLP_STE_UNMAPPED_NODE and NLP_STE_MAPPED_NODE states, it
4673  * invokes the lpfc_els_rsp_reject() routine to send reject response.
4674  * Otherwise, this routine then invokes the lpfc_els_rsp_rpl_acc() routine
4675  * to accept the RPL.
4676  *
4677  * Return code
4678  *   0 - Successfully processed rpl iocb (currently always return 0)
4679  **/
4680 static int
lpfc_els_rcv_rpl(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4681 lpfc_els_rcv_rpl(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4682 		 struct lpfc_nodelist *ndlp)
4683 {
4684 	struct lpfc_dmabuf *pcmd;
4685 	uint32_t *lp;
4686 	uint32_t maxsize;
4687 	uint16_t cmdsize;
4688 	RPL *rpl;
4689 	struct ls_rjt stat;
4690 
4691 	if ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
4692 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
4693 		/* issue rejection response */
4694 		stat.un.b.lsRjtRsvd0 = 0;
4695 		stat.un.b.lsRjtRsnCode = LSRJT_UNABLE_TPC;
4696 		stat.un.b.lsRjtRsnCodeExp = LSEXP_CANT_GIVE_DATA;
4697 		stat.un.b.vendorUnique = 0;
4698 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, cmdiocb, ndlp,
4699 			NULL);
4700 		/* rejected the unsolicited RPL request and done with it */
4701 		return 0;
4702 	}
4703 
4704 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4705 	lp = (uint32_t *) pcmd->virt;
4706 	rpl = (RPL *) (lp + 1);
4707 
4708 	maxsize = be32_to_cpu(rpl->maxsize);
4709 
4710 	/* We support only one port */
4711 	if ((rpl->index == 0) &&
4712 	    ((maxsize == 0) ||
4713 	     ((maxsize * sizeof(uint32_t)) >= sizeof(RPL_RSP)))) {
4714 		cmdsize = sizeof(uint32_t) + sizeof(RPL_RSP);
4715 	} else {
4716 		cmdsize = sizeof(uint32_t) + maxsize * sizeof(uint32_t);
4717 	}
4718 	lpfc_els_rsp_rpl_acc(vport, cmdsize, cmdiocb, ndlp);
4719 
4720 	return 0;
4721 }
4722 
4723 /**
4724  * lpfc_els_rcv_farp: Process an unsolicited farp request els command.
4725  * @vport: pointer to a virtual N_Port data structure.
4726  * @cmdiocb: pointer to lpfc command iocb data structure.
4727  * @ndlp: pointer to a node-list data structure.
4728  *
4729  * This routine processes Fibre Channel Address Resolution Protocol
4730  * (FARP) Request IOCB received as an ELS unsolicited event. Currently,
4731  * the lpfc driver only supports matching on WWPN or WWNN for FARP. As such,
4732  * FARP_MATCH_PORT flag and FARP_MATCH_NODE flag are checked against the
4733  * Match Flag in the FARP request IOCB: if FARP_MATCH_PORT flag is set, the
4734  * remote PortName is compared against the FC PortName stored in the @vport
4735  * data structure; if FARP_MATCH_NODE flag is set, the remote NodeName is
4736  * compared against the FC NodeName stored in the @vport data structure.
4737  * If any of these matches and the FARP_REQUEST_FARPR flag is set in the
4738  * FARP request IOCB Response Flag, the lpfc_issue_els_farpr() routine is
4739  * invoked to send out FARP Response to the remote node. Before sending the
4740  * FARP Response, however, the FARP_REQUEST_PLOGI flag is check in the FARP
4741  * request IOCB Response Flag and, if it is set, the lpfc_issue_els_plogi()
4742  * routine is invoked to log into the remote port first.
4743  *
4744  * Return code
4745  *   0 - Either the FARP Match Mode not supported or successfully processed
4746  **/
4747 static int
lpfc_els_rcv_farp(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4748 lpfc_els_rcv_farp(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4749 		  struct lpfc_nodelist *ndlp)
4750 {
4751 	struct lpfc_dmabuf *pcmd;
4752 	uint32_t *lp;
4753 	IOCB_t *icmd;
4754 	FARP *fp;
4755 	uint32_t cmd, cnt, did;
4756 
4757 	icmd = &cmdiocb->iocb;
4758 	did = icmd->un.elsreq64.remoteID;
4759 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4760 	lp = (uint32_t *) pcmd->virt;
4761 
4762 	cmd = *lp++;
4763 	fp = (FARP *) lp;
4764 	/* FARP-REQ received from DID <did> */
4765 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4766 			 "0601 FARP-REQ received from DID x%x\n", did);
4767 	/* We will only support match on WWPN or WWNN */
4768 	if (fp->Mflags & ~(FARP_MATCH_NODE | FARP_MATCH_PORT)) {
4769 		return 0;
4770 	}
4771 
4772 	cnt = 0;
4773 	/* If this FARP command is searching for my portname */
4774 	if (fp->Mflags & FARP_MATCH_PORT) {
4775 		if (memcmp(&fp->RportName, &vport->fc_portname,
4776 			   sizeof(struct lpfc_name)) == 0)
4777 			cnt = 1;
4778 	}
4779 
4780 	/* If this FARP command is searching for my nodename */
4781 	if (fp->Mflags & FARP_MATCH_NODE) {
4782 		if (memcmp(&fp->RnodeName, &vport->fc_nodename,
4783 			   sizeof(struct lpfc_name)) == 0)
4784 			cnt = 1;
4785 	}
4786 
4787 	if (cnt) {
4788 		if ((ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) ||
4789 		   (ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
4790 			/* Log back into the node before sending the FARP. */
4791 			if (fp->Rflags & FARP_REQUEST_PLOGI) {
4792 				ndlp->nlp_prev_state = ndlp->nlp_state;
4793 				lpfc_nlp_set_state(vport, ndlp,
4794 						   NLP_STE_PLOGI_ISSUE);
4795 				lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4796 			}
4797 
4798 			/* Send a FARP response to that node */
4799 			if (fp->Rflags & FARP_REQUEST_FARPR)
4800 				lpfc_issue_els_farpr(vport, did, 0);
4801 		}
4802 	}
4803 	return 0;
4804 }
4805 
4806 /**
4807  * lpfc_els_rcv_farpr: Process an unsolicited farp response iocb.
4808  * @vport: pointer to a host virtual N_Port data structure.
4809  * @cmdiocb: pointer to lpfc command iocb data structure.
4810  * @ndlp: pointer to a node-list data structure.
4811  *
4812  * This routine processes Fibre Channel Address Resolution Protocol
4813  * Response (FARPR) IOCB received as an ELS unsolicited event. It simply
4814  * invokes the lpfc_els_rsp_acc() routine to the remote node to accept
4815  * the FARP response request.
4816  *
4817  * Return code
4818  *   0 - Successfully processed FARPR IOCB (currently always return 0)
4819  **/
4820 static int
lpfc_els_rcv_farpr(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * ndlp)4821 lpfc_els_rcv_farpr(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4822 		   struct lpfc_nodelist  *ndlp)
4823 {
4824 	struct lpfc_dmabuf *pcmd;
4825 	uint32_t *lp;
4826 	IOCB_t *icmd;
4827 	uint32_t cmd, did;
4828 
4829 	icmd = &cmdiocb->iocb;
4830 	did = icmd->un.elsreq64.remoteID;
4831 	pcmd = (struct lpfc_dmabuf *) cmdiocb->context2;
4832 	lp = (uint32_t *) pcmd->virt;
4833 
4834 	cmd = *lp++;
4835 	/* FARP-RSP received from DID <did> */
4836 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
4837 			 "0600 FARP-RSP received from DID x%x\n", did);
4838 	/* ACCEPT the Farp resp request */
4839 	lpfc_els_rsp_acc(vport, ELS_CMD_ACC, cmdiocb, ndlp, NULL);
4840 
4841 	return 0;
4842 }
4843 
4844 /**
4845  * lpfc_els_rcv_fan: Process an unsolicited fan iocb command.
4846  * @vport: pointer to a host virtual N_Port data structure.
4847  * @cmdiocb: pointer to lpfc command iocb data structure.
4848  * @fan_ndlp: pointer to a node-list data structure.
4849  *
4850  * This routine processes a Fabric Address Notification (FAN) IOCB
4851  * command received as an ELS unsolicited event. The FAN ELS command will
4852  * only be processed on a physical port (i.e., the @vport represents the
4853  * physical port). The fabric NodeName and PortName from the FAN IOCB are
4854  * compared against those in the phba data structure. If any of those is
4855  * different, the lpfc_initial_flogi() routine is invoked to initialize
4856  * Fabric Login (FLOGI) to the fabric to start the discover over. Otherwise,
4857  * if both of those are identical, the lpfc_issue_fabric_reglogin() routine
4858  * is invoked to register login to the fabric.
4859  *
4860  * Return code
4861  *   0 - Successfully processed fan iocb (currently always return 0).
4862  **/
4863 static int
lpfc_els_rcv_fan(struct lpfc_vport * vport,struct lpfc_iocbq * cmdiocb,struct lpfc_nodelist * fan_ndlp)4864 lpfc_els_rcv_fan(struct lpfc_vport *vport, struct lpfc_iocbq *cmdiocb,
4865 		 struct lpfc_nodelist *fan_ndlp)
4866 {
4867 	struct lpfc_hba *phba = vport->phba;
4868 	uint32_t *lp;
4869 	FAN *fp;
4870 
4871 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS, "0265 FAN received\n");
4872 	lp = (uint32_t *)((struct lpfc_dmabuf *)cmdiocb->context2)->virt;
4873 	fp = (FAN *) ++lp;
4874 	/* FAN received; Fan does not have a reply sequence */
4875 	if ((vport == phba->pport) &&
4876 	    (vport->port_state == LPFC_LOCAL_CFG_LINK)) {
4877 		if ((memcmp(&phba->fc_fabparam.nodeName, &fp->FnodeName,
4878 			    sizeof(struct lpfc_name))) ||
4879 		    (memcmp(&phba->fc_fabparam.portName, &fp->FportName,
4880 			    sizeof(struct lpfc_name)))) {
4881 			/* This port has switched fabrics. FLOGI is required */
4882 			lpfc_initial_flogi(vport);
4883 		} else {
4884 			/* FAN verified - skip FLOGI */
4885 			vport->fc_myDID = vport->fc_prevDID;
4886 			lpfc_issue_fabric_reglogin(vport);
4887 		}
4888 	}
4889 	return 0;
4890 }
4891 
4892 /**
4893  * lpfc_els_timeout: Handler funciton to the els timer.
4894  * @ptr: holder for the timer function associated data.
4895  *
4896  * This routine is invoked by the ELS timer after timeout. It posts the ELS
4897  * timer timeout event by setting the WORKER_ELS_TMO bit to the work port
4898  * event bitmap and then invokes the lpfc_worker_wake_up() routine to wake
4899  * up the worker thread. It is for the worker thread to invoke the routine
4900  * lpfc_els_timeout_handler() to work on the posted event WORKER_ELS_TMO.
4901  **/
4902 void
lpfc_els_timeout(unsigned long ptr)4903 lpfc_els_timeout(unsigned long ptr)
4904 {
4905 	struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
4906 	struct lpfc_hba   *phba = vport->phba;
4907 	uint32_t tmo_posted;
4908 	unsigned long iflag;
4909 
4910 	spin_lock_irqsave(&vport->work_port_lock, iflag);
4911 	tmo_posted = vport->work_port_events & WORKER_ELS_TMO;
4912 	if (!tmo_posted)
4913 		vport->work_port_events |= WORKER_ELS_TMO;
4914 	spin_unlock_irqrestore(&vport->work_port_lock, iflag);
4915 
4916 	if (!tmo_posted)
4917 		lpfc_worker_wake_up(phba);
4918 	return;
4919 }
4920 
4921 /**
4922  * lpfc_els_timeout_handler: Process an els timeout event.
4923  * @vport: pointer to a virtual N_Port data structure.
4924  *
4925  * This routine is the actual handler function that processes an ELS timeout
4926  * event. It walks the ELS ring to get and abort all the IOCBs (except the
4927  * ABORT/CLOSE/FARP/FARPR/FDISC), which are associated with the @vport by
4928  * invoking the lpfc_sli_issue_abort_iotag() routine.
4929  **/
4930 void
lpfc_els_timeout_handler(struct lpfc_vport * vport)4931 lpfc_els_timeout_handler(struct lpfc_vport *vport)
4932 {
4933 	struct lpfc_hba  *phba = vport->phba;
4934 	struct lpfc_sli_ring *pring;
4935 	struct lpfc_iocbq *tmp_iocb, *piocb;
4936 	IOCB_t *cmd = NULL;
4937 	struct lpfc_dmabuf *pcmd;
4938 	uint32_t els_command = 0;
4939 	uint32_t timeout;
4940 	uint32_t remote_ID = 0xffffffff;
4941 
4942 	spin_lock_irq(&phba->hbalock);
4943 	timeout = (uint32_t)(phba->fc_ratov << 1);
4944 
4945 	pring = &phba->sli.ring[LPFC_ELS_RING];
4946 
4947 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
4948 		cmd = &piocb->iocb;
4949 
4950 		if ((piocb->iocb_flag & LPFC_IO_LIBDFC) != 0 ||
4951 		    piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
4952 		    piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4953 			continue;
4954 
4955 		if (piocb->vport != vport)
4956 			continue;
4957 
4958 		pcmd = (struct lpfc_dmabuf *) piocb->context2;
4959 		if (pcmd)
4960 			els_command = *(uint32_t *) (pcmd->virt);
4961 
4962 		if (els_command == ELS_CMD_FARP ||
4963 		    els_command == ELS_CMD_FARPR ||
4964 		    els_command == ELS_CMD_FDISC)
4965 			continue;
4966 
4967 		if (piocb->drvrTimeout > 0) {
4968 			if (piocb->drvrTimeout >= timeout)
4969 				piocb->drvrTimeout -= timeout;
4970 			else
4971 				piocb->drvrTimeout = 0;
4972 			continue;
4973 		}
4974 
4975 		remote_ID = 0xffffffff;
4976 		if (cmd->ulpCommand != CMD_GEN_REQUEST64_CR)
4977 			remote_ID = cmd->un.elsreq64.remoteID;
4978 		else {
4979 			struct lpfc_nodelist *ndlp;
4980 			ndlp = __lpfc_findnode_rpi(vport, cmd->ulpContext);
4981 			if (ndlp && NLP_CHK_NODE_ACT(ndlp))
4982 				remote_ID = ndlp->nlp_DID;
4983 		}
4984 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4985 				 "0127 ELS timeout Data: x%x x%x x%x "
4986 				 "x%x\n", els_command,
4987 				 remote_ID, cmd->ulpCommand, cmd->ulpIoTag);
4988 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
4989 	}
4990 	spin_unlock_irq(&phba->hbalock);
4991 
4992 	if (phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt)
4993 		mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
4994 }
4995 
4996 /**
4997  * lpfc_els_flush_cmd: Clean up the outstanding els commands to a vport.
4998  * @vport: pointer to a host virtual N_Port data structure.
4999  *
5000  * This routine is used to clean up all the outstanding ELS commands on a
5001  * @vport. It first aborts the @vport by invoking lpfc_fabric_abort_vport()
5002  * routine. After that, it walks the ELS transmit queue to remove all the
5003  * IOCBs with the @vport other than the QUE_RING and ABORT/CLOSE IOCBs. For
5004  * the IOCBs with a non-NULL completion callback function, the callback
5005  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5006  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs with a NULL completion
5007  * callback function, the IOCB will simply be released. Finally, it walks
5008  * the ELS transmit completion queue to issue an abort IOCB to any transmit
5009  * completion queue IOCB that is associated with the @vport and is not
5010  * an IOCB from libdfc (i.e., the management plane IOCBs that are not
5011  * part of the discovery state machine) out to HBA by invoking the
5012  * lpfc_sli_issue_abort_iotag() routine. Note that this function issues the
5013  * abort IOCB to any transmit completion queueed IOCB, it does not guarantee
5014  * the IOCBs are aborted when this function returns.
5015  **/
5016 void
lpfc_els_flush_cmd(struct lpfc_vport * vport)5017 lpfc_els_flush_cmd(struct lpfc_vport *vport)
5018 {
5019 	LIST_HEAD(completions);
5020 	struct lpfc_hba  *phba = vport->phba;
5021 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5022 	struct lpfc_iocbq *tmp_iocb, *piocb;
5023 	IOCB_t *cmd = NULL;
5024 
5025 	lpfc_fabric_abort_vport(vport);
5026 
5027 	spin_lock_irq(&phba->hbalock);
5028 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5029 		cmd = &piocb->iocb;
5030 
5031 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5032 			continue;
5033 		}
5034 
5035 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5036 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5037 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5038 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5039 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
5040 			continue;
5041 
5042 		if (piocb->vport != vport)
5043 			continue;
5044 
5045 		list_move_tail(&piocb->list, &completions);
5046 		pring->txq_cnt--;
5047 	}
5048 
5049 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5050 		if (piocb->iocb_flag & LPFC_IO_LIBDFC) {
5051 			continue;
5052 		}
5053 
5054 		if (piocb->vport != vport)
5055 			continue;
5056 
5057 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5058 	}
5059 	spin_unlock_irq(&phba->hbalock);
5060 
5061 	while (!list_empty(&completions)) {
5062 		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5063 		cmd = &piocb->iocb;
5064 		list_del_init(&piocb->list);
5065 
5066 		if (!piocb->iocb_cmpl)
5067 			lpfc_sli_release_iocbq(phba, piocb);
5068 		else {
5069 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5070 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5071 			(piocb->iocb_cmpl) (phba, piocb, piocb);
5072 		}
5073 	}
5074 
5075 	return;
5076 }
5077 
5078 /**
5079  * lpfc_els_flush_all_cmd: Clean up all the outstanding els commands to a HBA.
5080  * @phba: pointer to lpfc hba data structure.
5081  *
5082  * This routine is used to clean up all the outstanding ELS commands on a
5083  * @phba. It first aborts the @phba by invoking the lpfc_fabric_abort_hba()
5084  * routine. After that, it walks the ELS transmit queue to remove all the
5085  * IOCBs to the @phba other than the QUE_RING and ABORT/CLOSE IOCBs. For
5086  * the IOCBs with the completion callback function associated, the callback
5087  * function will be invoked with the status set to IOSTAT_LOCAL_REJECT and
5088  * un.ulpWord[4] set to IOERR_SLI_ABORTED. For IOCBs without the completion
5089  * callback function associated, the IOCB will simply be released. Finally,
5090  * it walks the ELS transmit completion queue to issue an abort IOCB to any
5091  * transmit completion queue IOCB that is not an IOCB from libdfc (i.e., the
5092  * management plane IOCBs that are not part of the discovery state machine)
5093  * out to HBA by invoking the lpfc_sli_issue_abort_iotag() routine.
5094  **/
5095 void
lpfc_els_flush_all_cmd(struct lpfc_hba * phba)5096 lpfc_els_flush_all_cmd(struct lpfc_hba  *phba)
5097 {
5098 	LIST_HEAD(completions);
5099 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
5100 	struct lpfc_iocbq *tmp_iocb, *piocb;
5101 	IOCB_t *cmd = NULL;
5102 
5103 	lpfc_fabric_abort_hba(phba);
5104 	spin_lock_irq(&phba->hbalock);
5105 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txq, list) {
5106 		cmd = &piocb->iocb;
5107 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5108 			continue;
5109 		/* Do not flush out the QUE_RING and ABORT/CLOSE iocbs */
5110 		if (cmd->ulpCommand == CMD_QUE_RING_BUF_CN ||
5111 		    cmd->ulpCommand == CMD_QUE_RING_BUF64_CN ||
5112 		    cmd->ulpCommand == CMD_CLOSE_XRI_CN ||
5113 		    cmd->ulpCommand == CMD_ABORT_XRI_CN)
5114 			continue;
5115 		list_move_tail(&piocb->list, &completions);
5116 		pring->txq_cnt--;
5117 	}
5118 	list_for_each_entry_safe(piocb, tmp_iocb, &pring->txcmplq, list) {
5119 		if (piocb->iocb_flag & LPFC_IO_LIBDFC)
5120 			continue;
5121 		lpfc_sli_issue_abort_iotag(phba, pring, piocb);
5122 	}
5123 	spin_unlock_irq(&phba->hbalock);
5124 	while (!list_empty(&completions)) {
5125 		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
5126 		cmd = &piocb->iocb;
5127 		list_del_init(&piocb->list);
5128 		if (!piocb->iocb_cmpl)
5129 			lpfc_sli_release_iocbq(phba, piocb);
5130 		else {
5131 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
5132 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
5133 			(piocb->iocb_cmpl) (phba, piocb, piocb);
5134 		}
5135 	}
5136 	return;
5137 }
5138 
5139 /**
5140  * lpfc_send_els_failure_event: Posts an ELS command failure event.
5141  * @phba: Pointer to hba context object.
5142  * @cmdiocbp: Pointer to command iocb which reported error.
5143  * @rspiocbp: Pointer to response iocb which reported error.
5144  *
5145  * This function sends an event when there is an ELS command
5146  * failure.
5147  **/
5148 void
lpfc_send_els_failure_event(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocbp,struct lpfc_iocbq * rspiocbp)5149 lpfc_send_els_failure_event(struct lpfc_hba *phba,
5150 			struct lpfc_iocbq *cmdiocbp,
5151 			struct lpfc_iocbq *rspiocbp)
5152 {
5153 	struct lpfc_vport *vport = cmdiocbp->vport;
5154 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5155 	struct lpfc_lsrjt_event lsrjt_event;
5156 	struct lpfc_fabric_event_header fabric_event;
5157 	struct ls_rjt stat;
5158 	struct lpfc_nodelist *ndlp;
5159 	uint32_t *pcmd;
5160 
5161 	ndlp = cmdiocbp->context1;
5162 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
5163 		return;
5164 
5165 	if (rspiocbp->iocb.ulpStatus == IOSTAT_LS_RJT) {
5166 		lsrjt_event.header.event_type = FC_REG_ELS_EVENT;
5167 		lsrjt_event.header.subcategory = LPFC_EVENT_LSRJT_RCV;
5168 		memcpy(lsrjt_event.header.wwpn, &ndlp->nlp_portname,
5169 			sizeof(struct lpfc_name));
5170 		memcpy(lsrjt_event.header.wwnn, &ndlp->nlp_nodename,
5171 			sizeof(struct lpfc_name));
5172 		pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
5173 			cmdiocbp->context2)->virt);
5174 		lsrjt_event.command = *pcmd;
5175 		stat.un.lsRjtError = be32_to_cpu(rspiocbp->iocb.un.ulpWord[4]);
5176 		lsrjt_event.reason_code = stat.un.b.lsRjtRsnCode;
5177 		lsrjt_event.explanation = stat.un.b.lsRjtRsnCodeExp;
5178 		fc_host_post_vendor_event(shost,
5179 			fc_get_event_number(),
5180 			sizeof(lsrjt_event),
5181 			(char *)&lsrjt_event,
5182 			LPFC_NL_VENDOR_ID);
5183 		return;
5184 	}
5185 	if ((rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY) ||
5186 		(rspiocbp->iocb.ulpStatus == IOSTAT_FABRIC_BSY)) {
5187 		fabric_event.event_type = FC_REG_FABRIC_EVENT;
5188 		if (rspiocbp->iocb.ulpStatus == IOSTAT_NPORT_BSY)
5189 			fabric_event.subcategory = LPFC_EVENT_PORT_BUSY;
5190 		else
5191 			fabric_event.subcategory = LPFC_EVENT_FABRIC_BUSY;
5192 		memcpy(fabric_event.wwpn, &ndlp->nlp_portname,
5193 			sizeof(struct lpfc_name));
5194 		memcpy(fabric_event.wwnn, &ndlp->nlp_nodename,
5195 			sizeof(struct lpfc_name));
5196 		fc_host_post_vendor_event(shost,
5197 			fc_get_event_number(),
5198 			sizeof(fabric_event),
5199 			(char *)&fabric_event,
5200 			LPFC_NL_VENDOR_ID);
5201 		return;
5202 	}
5203 
5204 }
5205 
5206 /**
5207  * lpfc_send_els_event: Posts unsolicited els event.
5208  * @vport: Pointer to vport object.
5209  * @ndlp: Pointer FC node object.
5210  * @cmd: ELS command code.
5211  *
5212  * This function posts an event when there is an incoming
5213  * unsolicited ELS command.
5214  **/
5215 static void
lpfc_send_els_event(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint32_t * payload)5216 lpfc_send_els_event(struct lpfc_vport *vport,
5217 		    struct lpfc_nodelist *ndlp,
5218 		    uint32_t *payload)
5219 {
5220 	struct lpfc_els_event_header *els_data = NULL;
5221 	struct lpfc_logo_event *logo_data = NULL;
5222 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5223 
5224 	if (*payload == ELS_CMD_LOGO) {
5225 		logo_data = kmalloc(sizeof(struct lpfc_logo_event), GFP_KERNEL);
5226 		if (!logo_data) {
5227 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5228 				"0148 Failed to allocate memory "
5229 				"for LOGO event\n");
5230 			return;
5231 		}
5232 		els_data = &logo_data->header;
5233 	} else {
5234 		els_data = kmalloc(sizeof(struct lpfc_els_event_header),
5235 			GFP_KERNEL);
5236 		if (!els_data) {
5237 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5238 				"0149 Failed to allocate memory "
5239 				"for ELS event\n");
5240 			return;
5241 		}
5242 	}
5243 	els_data->event_type = FC_REG_ELS_EVENT;
5244 	switch (*payload) {
5245 	case ELS_CMD_PLOGI:
5246 		els_data->subcategory = LPFC_EVENT_PLOGI_RCV;
5247 		break;
5248 	case ELS_CMD_PRLO:
5249 		els_data->subcategory = LPFC_EVENT_PRLO_RCV;
5250 		break;
5251 	case ELS_CMD_ADISC:
5252 		els_data->subcategory = LPFC_EVENT_ADISC_RCV;
5253 		break;
5254 	case ELS_CMD_LOGO:
5255 		els_data->subcategory = LPFC_EVENT_LOGO_RCV;
5256 		/* Copy the WWPN in the LOGO payload */
5257 		memcpy(logo_data->logo_wwpn, &payload[2],
5258 			sizeof(struct lpfc_name));
5259 		break;
5260 	default:
5261 		kfree(els_data);
5262 		return;
5263 	}
5264 	memcpy(els_data->wwpn, &ndlp->nlp_portname, sizeof(struct lpfc_name));
5265 	memcpy(els_data->wwnn, &ndlp->nlp_nodename, sizeof(struct lpfc_name));
5266 	if (*payload == ELS_CMD_LOGO) {
5267 		fc_host_post_vendor_event(shost,
5268 			fc_get_event_number(),
5269 			sizeof(struct lpfc_logo_event),
5270 			(char *)logo_data,
5271 			LPFC_NL_VENDOR_ID);
5272 		kfree(logo_data);
5273 	} else {
5274 		fc_host_post_vendor_event(shost,
5275 			fc_get_event_number(),
5276 			sizeof(struct lpfc_els_event_header),
5277 			(char *)els_data,
5278 			LPFC_NL_VENDOR_ID);
5279 		kfree(els_data);
5280 	}
5281 
5282 	return;
5283 }
5284 
5285 
5286 /**
5287  * lpfc_els_unsol_buffer: Process an unsolicited event data buffer.
5288  * @phba: pointer to lpfc hba data structure.
5289  * @pring: pointer to a SLI ring.
5290  * @vport: pointer to a host virtual N_Port data structure.
5291  * @elsiocb: pointer to lpfc els command iocb data structure.
5292  *
5293  * This routine is used for processing the IOCB associated with a unsolicited
5294  * event. It first determines whether there is an existing ndlp that matches
5295  * the DID from the unsolicited IOCB. If not, it will create a new one with
5296  * the DID from the unsolicited IOCB. The ELS command from the unsolicited
5297  * IOCB is then used to invoke the proper routine and to set up proper state
5298  * of the discovery state machine.
5299  **/
5300 static void
lpfc_els_unsol_buffer(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_vport * vport,struct lpfc_iocbq * elsiocb)5301 lpfc_els_unsol_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5302 		      struct lpfc_vport *vport, struct lpfc_iocbq *elsiocb)
5303 {
5304 	struct Scsi_Host  *shost;
5305 	struct lpfc_nodelist *ndlp;
5306 	struct ls_rjt stat;
5307 	uint32_t *payload;
5308 	uint32_t cmd, did, newnode, rjt_err = 0;
5309 	IOCB_t *icmd = &elsiocb->iocb;
5310 
5311 	if (!vport || !(elsiocb->context2))
5312 		goto dropit;
5313 
5314 	newnode = 0;
5315 	payload = ((struct lpfc_dmabuf *)elsiocb->context2)->virt;
5316 	cmd = *payload;
5317 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) == 0)
5318 		lpfc_post_buffer(phba, pring, 1);
5319 
5320 	did = icmd->un.rcvels.remoteID;
5321 	if (icmd->ulpStatus) {
5322 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5323 			"RCV Unsol ELS:  status:x%x/x%x did:x%x",
5324 			icmd->ulpStatus, icmd->un.ulpWord[4], did);
5325 		goto dropit;
5326 	}
5327 
5328 	/* Check to see if link went down during discovery */
5329 	if (lpfc_els_chk_latt(vport))
5330 		goto dropit;
5331 
5332 	/* Ignore traffic recevied during vport shutdown. */
5333 	if (vport->load_flag & FC_UNLOADING)
5334 		goto dropit;
5335 
5336 	ndlp = lpfc_findnode_did(vport, did);
5337 	if (!ndlp) {
5338 		/* Cannot find existing Fabric ndlp, so allocate a new one */
5339 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5340 		if (!ndlp)
5341 			goto dropit;
5342 
5343 		lpfc_nlp_init(vport, ndlp, did);
5344 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5345 		newnode = 1;
5346 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5347 			ndlp->nlp_type |= NLP_FABRIC;
5348 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
5349 		ndlp = lpfc_enable_node(vport, ndlp,
5350 					NLP_STE_UNUSED_NODE);
5351 		if (!ndlp)
5352 			goto dropit;
5353 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5354 		newnode = 1;
5355 		if ((did & Fabric_DID_MASK) == Fabric_DID_MASK)
5356 			ndlp->nlp_type |= NLP_FABRIC;
5357 	} else if (ndlp->nlp_state == NLP_STE_UNUSED_NODE) {
5358 		/* This is similar to the new node path */
5359 		ndlp = lpfc_nlp_get(ndlp);
5360 		if (!ndlp)
5361 			goto dropit;
5362 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5363 		newnode = 1;
5364 	}
5365 
5366 	phba->fc_stat.elsRcvFrame++;
5367 
5368 	elsiocb->context1 = lpfc_nlp_get(ndlp);
5369 	elsiocb->vport = vport;
5370 
5371 	if ((cmd & ELS_CMD_MASK) == ELS_CMD_RSCN) {
5372 		cmd &= ELS_CMD_MASK;
5373 	}
5374 	/* ELS command <elsCmd> received from NPORT <did> */
5375 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5376 			 "0112 ELS command x%x received from NPORT x%x "
5377 			 "Data: x%x\n", cmd, did, vport->port_state);
5378 	switch (cmd) {
5379 	case ELS_CMD_PLOGI:
5380 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5381 			"RCV PLOGI:       did:x%x/ste:x%x flg:x%x",
5382 			did, vport->port_state, ndlp->nlp_flag);
5383 
5384 		phba->fc_stat.elsRcvPLOGI++;
5385 		ndlp = lpfc_plogi_confirm_nport(phba, payload, ndlp);
5386 
5387 		lpfc_send_els_event(vport, ndlp, payload);
5388 		if (vport->port_state < LPFC_DISC_AUTH) {
5389 			if (!(phba->pport->fc_flag & FC_PT2PT) ||
5390 				(phba->pport->fc_flag & FC_PT2PT_PLOGI)) {
5391 				rjt_err = LSRJT_UNABLE_TPC;
5392 				break;
5393 			}
5394 			/* We get here, and drop thru, if we are PT2PT with
5395 			 * another NPort and the other side has initiated
5396 			 * the PLOGI before responding to our FLOGI.
5397 			 */
5398 		}
5399 
5400 		shost = lpfc_shost_from_vport(vport);
5401 		spin_lock_irq(shost->host_lock);
5402 		ndlp->nlp_flag &= ~NLP_TARGET_REMOVE;
5403 		spin_unlock_irq(shost->host_lock);
5404 
5405 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
5406 					NLP_EVT_RCV_PLOGI);
5407 
5408 		break;
5409 	case ELS_CMD_FLOGI:
5410 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5411 			"RCV FLOGI:       did:x%x/ste:x%x flg:x%x",
5412 			did, vport->port_state, ndlp->nlp_flag);
5413 
5414 		phba->fc_stat.elsRcvFLOGI++;
5415 		lpfc_els_rcv_flogi(vport, elsiocb, ndlp);
5416 		if (newnode)
5417 			lpfc_nlp_put(ndlp);
5418 		break;
5419 	case ELS_CMD_LOGO:
5420 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5421 			"RCV LOGO:        did:x%x/ste:x%x flg:x%x",
5422 			did, vport->port_state, ndlp->nlp_flag);
5423 
5424 		phba->fc_stat.elsRcvLOGO++;
5425 		lpfc_send_els_event(vport, ndlp, payload);
5426 		if (vport->port_state < LPFC_DISC_AUTH) {
5427 			rjt_err = LSRJT_UNABLE_TPC;
5428 			break;
5429 		}
5430 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_LOGO);
5431 		break;
5432 	case ELS_CMD_PRLO:
5433 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5434 			"RCV PRLO:        did:x%x/ste:x%x flg:x%x",
5435 			did, vport->port_state, ndlp->nlp_flag);
5436 
5437 		phba->fc_stat.elsRcvPRLO++;
5438 		lpfc_send_els_event(vport, ndlp, payload);
5439 		if (vport->port_state < LPFC_DISC_AUTH) {
5440 			rjt_err = LSRJT_UNABLE_TPC;
5441 			break;
5442 		}
5443 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLO);
5444 		break;
5445 	case ELS_CMD_RSCN:
5446 		phba->fc_stat.elsRcvRSCN++;
5447 		lpfc_els_rcv_rscn(vport, elsiocb, ndlp);
5448 		if (newnode)
5449 			lpfc_nlp_put(ndlp);
5450 		break;
5451 	case ELS_CMD_ADISC:
5452 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5453 			"RCV ADISC:       did:x%x/ste:x%x flg:x%x",
5454 			did, vport->port_state, ndlp->nlp_flag);
5455 
5456 		lpfc_send_els_event(vport, ndlp, payload);
5457 		phba->fc_stat.elsRcvADISC++;
5458 		if (vport->port_state < LPFC_DISC_AUTH) {
5459 			rjt_err = LSRJT_UNABLE_TPC;
5460 			break;
5461 		}
5462 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
5463 					NLP_EVT_RCV_ADISC);
5464 		break;
5465 	case ELS_CMD_PDISC:
5466 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5467 			"RCV PDISC:       did:x%x/ste:x%x flg:x%x",
5468 			did, vport->port_state, ndlp->nlp_flag);
5469 
5470 		phba->fc_stat.elsRcvPDISC++;
5471 		if (vport->port_state < LPFC_DISC_AUTH) {
5472 			rjt_err = LSRJT_UNABLE_TPC;
5473 			break;
5474 		}
5475 		lpfc_disc_state_machine(vport, ndlp, elsiocb,
5476 					NLP_EVT_RCV_PDISC);
5477 		break;
5478 	case ELS_CMD_FARPR:
5479 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5480 			"RCV FARPR:       did:x%x/ste:x%x flg:x%x",
5481 			did, vport->port_state, ndlp->nlp_flag);
5482 
5483 		phba->fc_stat.elsRcvFARPR++;
5484 		lpfc_els_rcv_farpr(vport, elsiocb, ndlp);
5485 		break;
5486 	case ELS_CMD_FARP:
5487 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5488 			"RCV FARP:        did:x%x/ste:x%x flg:x%x",
5489 			did, vport->port_state, ndlp->nlp_flag);
5490 
5491 		phba->fc_stat.elsRcvFARP++;
5492 		lpfc_els_rcv_farp(vport, elsiocb, ndlp);
5493 		break;
5494 	case ELS_CMD_FAN:
5495 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5496 			"RCV FAN:         did:x%x/ste:x%x flg:x%x",
5497 			did, vport->port_state, ndlp->nlp_flag);
5498 
5499 		phba->fc_stat.elsRcvFAN++;
5500 		lpfc_els_rcv_fan(vport, elsiocb, ndlp);
5501 		break;
5502 	case ELS_CMD_PRLI:
5503 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5504 			"RCV PRLI:        did:x%x/ste:x%x flg:x%x",
5505 			did, vport->port_state, ndlp->nlp_flag);
5506 
5507 		phba->fc_stat.elsRcvPRLI++;
5508 		if (vport->port_state < LPFC_DISC_AUTH) {
5509 			rjt_err = LSRJT_UNABLE_TPC;
5510 			break;
5511 		}
5512 		lpfc_disc_state_machine(vport, ndlp, elsiocb, NLP_EVT_RCV_PRLI);
5513 		break;
5514 	case ELS_CMD_LIRR:
5515 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5516 			"RCV LIRR:        did:x%x/ste:x%x flg:x%x",
5517 			did, vport->port_state, ndlp->nlp_flag);
5518 
5519 		phba->fc_stat.elsRcvLIRR++;
5520 		lpfc_els_rcv_lirr(vport, elsiocb, ndlp);
5521 		if (newnode)
5522 			lpfc_nlp_put(ndlp);
5523 		break;
5524 	case ELS_CMD_RPS:
5525 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5526 			"RCV RPS:         did:x%x/ste:x%x flg:x%x",
5527 			did, vport->port_state, ndlp->nlp_flag);
5528 
5529 		phba->fc_stat.elsRcvRPS++;
5530 		lpfc_els_rcv_rps(vport, elsiocb, ndlp);
5531 		if (newnode)
5532 			lpfc_nlp_put(ndlp);
5533 		break;
5534 	case ELS_CMD_RPL:
5535 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5536 			"RCV RPL:         did:x%x/ste:x%x flg:x%x",
5537 			did, vport->port_state, ndlp->nlp_flag);
5538 
5539 		phba->fc_stat.elsRcvRPL++;
5540 		lpfc_els_rcv_rpl(vport, elsiocb, ndlp);
5541 		if (newnode)
5542 			lpfc_nlp_put(ndlp);
5543 		break;
5544 	case ELS_CMD_RNID:
5545 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5546 			"RCV RNID:        did:x%x/ste:x%x flg:x%x",
5547 			did, vport->port_state, ndlp->nlp_flag);
5548 
5549 		phba->fc_stat.elsRcvRNID++;
5550 		lpfc_els_rcv_rnid(vport, elsiocb, ndlp);
5551 		if (newnode)
5552 			lpfc_nlp_put(ndlp);
5553 		break;
5554 	default:
5555 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_UNSOL,
5556 			"RCV ELS cmd:     cmd:x%x did:x%x/ste:x%x",
5557 			cmd, did, vport->port_state);
5558 
5559 		/* Unsupported ELS command, reject */
5560 		rjt_err = LSRJT_INVALID_CMD;
5561 
5562 		/* Unknown ELS command <elsCmd> received from NPORT <did> */
5563 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5564 				 "0115 Unknown ELS command x%x "
5565 				 "received from NPORT x%x\n", cmd, did);
5566 		if (newnode)
5567 			lpfc_nlp_put(ndlp);
5568 		break;
5569 	}
5570 
5571 	/* check if need to LS_RJT received ELS cmd */
5572 	if (rjt_err) {
5573 		memset(&stat, 0, sizeof(stat));
5574 		stat.un.b.lsRjtRsnCode = rjt_err;
5575 		stat.un.b.lsRjtRsnCodeExp = LSEXP_NOTHING_MORE;
5576 		lpfc_els_rsp_reject(vport, stat.un.lsRjtError, elsiocb, ndlp,
5577 			NULL);
5578 	}
5579 
5580 	lpfc_nlp_put(elsiocb->context1);
5581 	elsiocb->context1 = NULL;
5582 	return;
5583 
5584 dropit:
5585 	if (vport && !(vport->load_flag & FC_UNLOADING))
5586 		lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5587 			"(%d):0111 Dropping received ELS cmd "
5588 			"Data: x%x x%x x%x\n",
5589 			vport->vpi, icmd->ulpStatus,
5590 			icmd->un.ulpWord[4], icmd->ulpTimeout);
5591 	phba->fc_stat.elsRcvDrop++;
5592 }
5593 
5594 /**
5595  * lpfc_find_vport_by_vpid: Find a vport on a HBA through vport identifier.
5596  * @phba: pointer to lpfc hba data structure.
5597  * @vpi: host virtual N_Port identifier.
5598  *
5599  * This routine finds a vport on a HBA (referred by @phba) through a
5600  * @vpi. The function walks the HBA's vport list and returns the address
5601  * of the vport with the matching @vpi.
5602  *
5603  * Return code
5604  *    NULL - No vport with the matching @vpi found
5605  *    Otherwise - Address to the vport with the matching @vpi.
5606  **/
5607 static struct lpfc_vport *
lpfc_find_vport_by_vpid(struct lpfc_hba * phba,uint16_t vpi)5608 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5609 {
5610 	struct lpfc_vport *vport;
5611 	unsigned long flags;
5612 
5613 	spin_lock_irqsave(&phba->hbalock, flags);
5614 	list_for_each_entry(vport, &phba->port_list, listentry) {
5615 		if (vport->vpi == vpi) {
5616 			spin_unlock_irqrestore(&phba->hbalock, flags);
5617 			return vport;
5618 		}
5619 	}
5620 	spin_unlock_irqrestore(&phba->hbalock, flags);
5621 	return NULL;
5622 }
5623 
5624 /**
5625  * lpfc_els_unsol_event: Process an unsolicited event from an els sli ring.
5626  * @phba: pointer to lpfc hba data structure.
5627  * @pring: pointer to a SLI ring.
5628  * @elsiocb: pointer to lpfc els iocb data structure.
5629  *
5630  * This routine is used to process an unsolicited event received from a SLI
5631  * (Service Level Interface) ring. The actual processing of the data buffer
5632  * associated with the unsolicited event is done by invoking the routine
5633  * lpfc_els_unsol_buffer() after properly set up the iocb buffer from the
5634  * SLI ring on which the unsolicited event was received.
5635  **/
5636 void
lpfc_els_unsol_event(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * elsiocb)5637 lpfc_els_unsol_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5638 		     struct lpfc_iocbq *elsiocb)
5639 {
5640 	struct lpfc_vport *vport = phba->pport;
5641 	IOCB_t *icmd = &elsiocb->iocb;
5642 	dma_addr_t paddr;
5643 	struct lpfc_dmabuf *bdeBuf1 = elsiocb->context2;
5644 	struct lpfc_dmabuf *bdeBuf2 = elsiocb->context3;
5645 
5646 	elsiocb->context1 = NULL;
5647 	elsiocb->context2 = NULL;
5648 	elsiocb->context3 = NULL;
5649 
5650 	if (icmd->ulpStatus == IOSTAT_NEED_BUFFER) {
5651 		lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
5652 	} else if (icmd->ulpStatus == IOSTAT_LOCAL_REJECT &&
5653 	    (icmd->un.ulpWord[4] & 0xff) == IOERR_RCV_BUFFER_WAITING) {
5654 		phba->fc_stat.NoRcvBuf++;
5655 		/* Not enough posted buffers; Try posting more buffers */
5656 		if (!(phba->sli3_options & LPFC_SLI3_HBQ_ENABLED))
5657 			lpfc_post_buffer(phba, pring, 0);
5658 		return;
5659 	}
5660 
5661 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5662 	    (icmd->ulpCommand == CMD_IOCB_RCV_ELS64_CX ||
5663 	     icmd->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
5664 		if (icmd->unsli3.rcvsli3.vpi == 0xffff)
5665 			vport = phba->pport;
5666 		else {
5667 			uint16_t vpi = icmd->unsli3.rcvsli3.vpi;
5668 			vport = lpfc_find_vport_by_vpid(phba, vpi);
5669 		}
5670 	}
5671 	/* If there are no BDEs associated
5672 	 * with this IOCB, there is nothing to do.
5673 	 */
5674 	if (icmd->ulpBdeCount == 0)
5675 		return;
5676 
5677 	/* type of ELS cmd is first 32bit word
5678 	 * in packet
5679 	 */
5680 	if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
5681 		elsiocb->context2 = bdeBuf1;
5682 	} else {
5683 		paddr = getPaddr(icmd->un.cont64[0].addrHigh,
5684 				 icmd->un.cont64[0].addrLow);
5685 		elsiocb->context2 = lpfc_sli_ringpostbuf_get(phba, pring,
5686 							     paddr);
5687 	}
5688 
5689 	lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5690 	/*
5691 	 * The different unsolicited event handlers would tell us
5692 	 * if they are done with "mp" by setting context2 to NULL.
5693 	 */
5694 	if (elsiocb->context2) {
5695 		lpfc_in_buf_free(phba, (struct lpfc_dmabuf *)elsiocb->context2);
5696 		elsiocb->context2 = NULL;
5697 	}
5698 
5699 	/* RCV_ELS64_CX provide for 2 BDEs - process 2nd if included */
5700 	if ((phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) &&
5701 	    icmd->ulpBdeCount == 2) {
5702 		elsiocb->context2 = bdeBuf2;
5703 		lpfc_els_unsol_buffer(phba, pring, vport, elsiocb);
5704 		/* free mp if we are done with it */
5705 		if (elsiocb->context2) {
5706 			lpfc_in_buf_free(phba, elsiocb->context2);
5707 			elsiocb->context2 = NULL;
5708 		}
5709 	}
5710 }
5711 
5712 /**
5713  * lpfc_do_scr_ns_plogi: Issue a plogi to the name server for scr.
5714  * @phba: pointer to lpfc hba data structure.
5715  * @vport: pointer to a virtual N_Port data structure.
5716  *
5717  * This routine issues a Port Login (PLOGI) to the Name Server with
5718  * State Change Request (SCR) for a @vport. This routine will create an
5719  * ndlp for the Name Server associated to the @vport if such node does
5720  * not already exist. The PLOGI to Name Server is issued by invoking the
5721  * lpfc_issue_els_plogi() routine. If Fabric-Device Management Interface
5722  * (FDMI) is configured to the @vport, a FDMI node will be created and
5723  * the PLOGI to FDMI is issued by invoking lpfc_issue_els_plogi() routine.
5724  **/
5725 void
lpfc_do_scr_ns_plogi(struct lpfc_hba * phba,struct lpfc_vport * vport)5726 lpfc_do_scr_ns_plogi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5727 {
5728 	struct lpfc_nodelist *ndlp, *ndlp_fdmi;
5729 
5730 	ndlp = lpfc_findnode_did(vport, NameServer_DID);
5731 	if (!ndlp) {
5732 		ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
5733 		if (!ndlp) {
5734 			if (phba->fc_topology == TOPOLOGY_LOOP) {
5735 				lpfc_disc_start(vport);
5736 				return;
5737 			}
5738 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5739 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5740 					 "0251 NameServer login: no memory\n");
5741 			return;
5742 		}
5743 		lpfc_nlp_init(vport, ndlp, NameServer_DID);
5744 	} else if (!NLP_CHK_NODE_ACT(ndlp)) {
5745 		ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
5746 		if (!ndlp) {
5747 			if (phba->fc_topology == TOPOLOGY_LOOP) {
5748 				lpfc_disc_start(vport);
5749 				return;
5750 			}
5751 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5752 			lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5753 					"0348 NameServer login: node freed\n");
5754 			return;
5755 		}
5756 	}
5757 	ndlp->nlp_type |= NLP_FABRIC;
5758 
5759 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_PLOGI_ISSUE);
5760 
5761 	if (lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0)) {
5762 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5763 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5764 				 "0252 Cannot issue NameServer login\n");
5765 		return;
5766 	}
5767 
5768 	if (vport->cfg_fdmi_on) {
5769 		ndlp_fdmi = mempool_alloc(phba->nlp_mem_pool,
5770 					  GFP_KERNEL);
5771 		if (ndlp_fdmi) {
5772 			lpfc_nlp_init(vport, ndlp_fdmi, FDMI_DID);
5773 			ndlp_fdmi->nlp_type |= NLP_FABRIC;
5774 			lpfc_nlp_set_state(vport, ndlp_fdmi,
5775 				NLP_STE_PLOGI_ISSUE);
5776 			lpfc_issue_els_plogi(vport, ndlp_fdmi->nlp_DID,
5777 					     0);
5778 		}
5779 	}
5780 	return;
5781 }
5782 
5783 /**
5784  * lpfc_cmpl_reg_new_vport: Completion callback function to register new vport.
5785  * @phba: pointer to lpfc hba data structure.
5786  * @pmb: pointer to the driver internal queue element for mailbox command.
5787  *
5788  * This routine is the completion callback function to register new vport
5789  * mailbox command. If the new vport mailbox command completes successfully,
5790  * the fabric registration login shall be performed on physical port (the
5791  * new vport created is actually a physical port, with VPI 0) or the port
5792  * login to Name Server for State Change Request (SCR) will be performed
5793  * on virtual port (real virtual port, with VPI greater than 0).
5794  **/
5795 static void
lpfc_cmpl_reg_new_vport(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)5796 lpfc_cmpl_reg_new_vport(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5797 {
5798 	struct lpfc_vport *vport = pmb->vport;
5799 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5800 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5801 	MAILBOX_t *mb = &pmb->mb;
5802 
5803 	spin_lock_irq(shost->host_lock);
5804 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5805 	spin_unlock_irq(shost->host_lock);
5806 
5807 	if (mb->mbxStatus) {
5808 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5809 				 "0915 Register VPI failed: 0x%x\n",
5810 				 mb->mbxStatus);
5811 
5812 		switch (mb->mbxStatus) {
5813 		case 0x11:	/* unsupported feature */
5814 		case 0x9603:	/* max_vpi exceeded */
5815 		case 0x9602:	/* Link event since CLEAR_LA */
5816 			/* giving up on vport registration */
5817 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5818 			spin_lock_irq(shost->host_lock);
5819 			vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
5820 			spin_unlock_irq(shost->host_lock);
5821 			lpfc_can_disctmo(vport);
5822 			break;
5823 		default:
5824 			/* Try to recover from this error */
5825 			lpfc_mbx_unreg_vpi(vport);
5826 			spin_lock_irq(shost->host_lock);
5827 			vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5828 			spin_unlock_irq(shost->host_lock);
5829 			if (vport->port_type == LPFC_PHYSICAL_PORT)
5830 				lpfc_initial_flogi(vport);
5831 			else
5832 				lpfc_initial_fdisc(vport);
5833 			break;
5834 		}
5835 
5836 	} else {
5837 		if (vport == phba->pport)
5838 			lpfc_issue_fabric_reglogin(vport);
5839 		else
5840 			lpfc_do_scr_ns_plogi(phba, vport);
5841 	}
5842 
5843 	/* Now, we decrement the ndlp reference count held for this
5844 	 * callback function
5845 	 */
5846 	lpfc_nlp_put(ndlp);
5847 
5848 	mempool_free(pmb, phba->mbox_mem_pool);
5849 	return;
5850 }
5851 
5852 /**
5853  * lpfc_register_new_vport: Register a new vport with a HBA.
5854  * @phba: pointer to lpfc hba data structure.
5855  * @vport: pointer to a host virtual N_Port data structure.
5856  * @ndlp: pointer to a node-list data structure.
5857  *
5858  * This routine registers the @vport as a new virtual port with a HBA.
5859  * It is done through a registering vpi mailbox command.
5860  **/
5861 static void
lpfc_register_new_vport(struct lpfc_hba * phba,struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)5862 lpfc_register_new_vport(struct lpfc_hba *phba, struct lpfc_vport *vport,
5863 			struct lpfc_nodelist *ndlp)
5864 {
5865 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5866 	LPFC_MBOXQ_t *mbox;
5867 
5868 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5869 	if (mbox) {
5870 		lpfc_reg_vpi(phba, vport->vpi, vport->fc_myDID, mbox);
5871 		mbox->vport = vport;
5872 		mbox->context2 = lpfc_nlp_get(ndlp);
5873 		mbox->mbox_cmpl = lpfc_cmpl_reg_new_vport;
5874 		if (lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT)
5875 		    == MBX_NOT_FINISHED) {
5876 			/* mailbox command not success, decrement ndlp
5877 			 * reference count for this command
5878 			 */
5879 			lpfc_nlp_put(ndlp);
5880 			mempool_free(mbox, phba->mbox_mem_pool);
5881 
5882 			lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5883 				"0253 Register VPI: Can't send mbox\n");
5884 			goto mbox_err_exit;
5885 		}
5886 	} else {
5887 		lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
5888 				 "0254 Register VPI: no memory\n");
5889 		goto mbox_err_exit;
5890 	}
5891 	return;
5892 
5893 mbox_err_exit:
5894 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5895 	spin_lock_irq(shost->host_lock);
5896 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
5897 	spin_unlock_irq(shost->host_lock);
5898 	return;
5899 }
5900 
5901 /**
5902  * lpfc_cmpl_els_fdisc: Completion function for fdisc iocb command.
5903  * @phba: pointer to lpfc hba data structure.
5904  * @cmdiocb: pointer to lpfc command iocb data structure.
5905  * @rspiocb: pointer to lpfc response iocb data structure.
5906  *
5907  * This routine is the completion callback function to a Fabric Discover
5908  * (FDISC) ELS command. Since all the FDISC ELS commands are issued
5909  * single threaded, each FDISC completion callback function will reset
5910  * the discovery timer for all vports such that the timers will not get
5911  * unnecessary timeout. The function checks the FDISC IOCB status. If error
5912  * detected, the vport will be set to FC_VPORT_FAILED state. Otherwise,the
5913  * vport will set to FC_VPORT_ACTIVE state. It then checks whether the DID
5914  * assigned to the vport has been changed with the completion of the FDISC
5915  * command. If so, both RPI (Remote Port Index) and VPI (Virtual Port Index)
5916  * are unregistered from the HBA, and then the lpfc_register_new_vport()
5917  * routine is invoked to register new vport with the HBA. Otherwise, the
5918  * lpfc_do_scr_ns_plogi() routine is invoked to issue a PLOGI to the Name
5919  * Server for State Change Request (SCR).
5920  **/
5921 static void
lpfc_cmpl_els_fdisc(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)5922 lpfc_cmpl_els_fdisc(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
5923 		    struct lpfc_iocbq *rspiocb)
5924 {
5925 	struct lpfc_vport *vport = cmdiocb->vport;
5926 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
5927 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) cmdiocb->context1;
5928 	struct lpfc_nodelist *np;
5929 	struct lpfc_nodelist *next_np;
5930 	IOCB_t *irsp = &rspiocb->iocb;
5931 	struct lpfc_iocbq *piocb;
5932 
5933 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
5934 			 "0123 FDISC completes. x%x/x%x prevDID: x%x\n",
5935 			 irsp->ulpStatus, irsp->un.ulpWord[4],
5936 			 vport->fc_prevDID);
5937 	/* Since all FDISCs are being single threaded, we
5938 	 * must reset the discovery timer for ALL vports
5939 	 * waiting to send FDISC when one completes.
5940 	 */
5941 	list_for_each_entry(piocb, &phba->fabric_iocb_list, list) {
5942 		lpfc_set_disctmo(piocb->vport);
5943 	}
5944 
5945 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5946 		"FDISC cmpl:      status:x%x/x%x prevdid:x%x",
5947 		irsp->ulpStatus, irsp->un.ulpWord[4], vport->fc_prevDID);
5948 
5949 	if (irsp->ulpStatus) {
5950 		/* Check for retry */
5951 		if (lpfc_els_retry(phba, cmdiocb, rspiocb))
5952 			goto out;
5953 		/* FDISC failed */
5954 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
5955 				 "0126 FDISC failed. (%d/%d)\n",
5956 				 irsp->ulpStatus, irsp->un.ulpWord[4]);
5957 		goto fdisc_failed;
5958 	}
5959 		if (vport->fc_vport->vport_state == FC_VPORT_INITIALIZING)
5960 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
5961 		lpfc_nlp_put(ndlp);
5962 		/* giving up on FDISC. Cancel discovery timer */
5963 		lpfc_can_disctmo(vport);
5964 	spin_lock_irq(shost->host_lock);
5965 	vport->fc_flag |= FC_FABRIC;
5966 	if (vport->phba->fc_topology == TOPOLOGY_LOOP)
5967 		vport->fc_flag |=  FC_PUBLIC_LOOP;
5968 	spin_unlock_irq(shost->host_lock);
5969 
5970 	vport->fc_myDID = irsp->un.ulpWord[4] & Mask_DID;
5971 	lpfc_vport_set_state(vport, FC_VPORT_ACTIVE);
5972 	if ((vport->fc_prevDID != vport->fc_myDID) &&
5973 		!(vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)) {
5974 		/* If our NportID changed, we need to ensure all
5975 		 * remaining NPORTs get unreg_login'ed so we can
5976 		 * issue unreg_vpi.
5977 		 */
5978 		list_for_each_entry_safe(np, next_np,
5979 			&vport->fc_nodes, nlp_listp) {
5980 			if (!NLP_CHK_NODE_ACT(ndlp) ||
5981 			    (np->nlp_state != NLP_STE_NPR_NODE) ||
5982 			    !(np->nlp_flag & NLP_NPR_ADISC))
5983 				continue;
5984 			spin_lock_irq(shost->host_lock);
5985 			np->nlp_flag &= ~NLP_NPR_ADISC;
5986 			spin_unlock_irq(shost->host_lock);
5987 			lpfc_unreg_rpi(vport, np);
5988 		}
5989 		lpfc_mbx_unreg_vpi(vport);
5990 		spin_lock_irq(shost->host_lock);
5991 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
5992 		spin_unlock_irq(shost->host_lock);
5993 	}
5994 
5995 	if (vport->fc_flag & FC_VPORT_NEEDS_REG_VPI)
5996 		lpfc_register_new_vport(phba, vport, ndlp);
5997 	else
5998 		lpfc_do_scr_ns_plogi(phba, vport);
5999 	goto out;
6000 fdisc_failed:
6001 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6002 	/* Cancel discovery timer */
6003 	lpfc_can_disctmo(vport);
6004 	lpfc_nlp_put(ndlp);
6005 out:
6006 	lpfc_els_free_iocb(phba, cmdiocb);
6007 }
6008 
6009 /**
6010  * lpfc_issue_els_fdisc: Issue a fdisc iocb command.
6011  * @vport: pointer to a virtual N_Port data structure.
6012  * @ndlp: pointer to a node-list data structure.
6013  * @retry: number of retries to the command IOCB.
6014  *
6015  * This routine prepares and issues a Fabric Discover (FDISC) IOCB to
6016  * a remote node (@ndlp) off a @vport. It uses the lpfc_issue_fabric_iocb()
6017  * routine to issue the IOCB, which makes sure only one outstanding fabric
6018  * IOCB will be sent off HBA at any given time.
6019  *
6020  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6021  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6022  * will be stored into the context1 field of the IOCB for the completion
6023  * callback function to the FDISC ELS command.
6024  *
6025  * Return code
6026  *   0 - Successfully issued fdisc iocb command
6027  *   1 - Failed to issue fdisc iocb command
6028  **/
6029 static int
lpfc_issue_els_fdisc(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint8_t retry)6030 lpfc_issue_els_fdisc(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
6031 		     uint8_t retry)
6032 {
6033 	struct lpfc_hba *phba = vport->phba;
6034 	IOCB_t *icmd;
6035 	struct lpfc_iocbq *elsiocb;
6036 	struct serv_parm *sp;
6037 	uint8_t *pcmd;
6038 	uint16_t cmdsize;
6039 	int did = ndlp->nlp_DID;
6040 	int rc;
6041 
6042 	cmdsize = (sizeof(uint32_t) + sizeof(struct serv_parm));
6043 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, retry, ndlp, did,
6044 				     ELS_CMD_FDISC);
6045 	if (!elsiocb) {
6046 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6047 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6048 				 "0255 Issue FDISC: no IOCB\n");
6049 		return 1;
6050 	}
6051 
6052 	icmd = &elsiocb->iocb;
6053 	icmd->un.elsreq64.myID = 0;
6054 	icmd->un.elsreq64.fl = 1;
6055 
6056 	/* For FDISC, Let FDISC rsp set the NPortID for this VPI */
6057 	icmd->ulpCt_h = 1;
6058 	icmd->ulpCt_l = 0;
6059 
6060 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6061 	*((uint32_t *) (pcmd)) = ELS_CMD_FDISC;
6062 	pcmd += sizeof(uint32_t); /* CSP Word 1 */
6063 	memcpy(pcmd, &vport->phba->pport->fc_sparam, sizeof(struct serv_parm));
6064 	sp = (struct serv_parm *) pcmd;
6065 	/* Setup CSPs accordingly for Fabric */
6066 	sp->cmn.e_d_tov = 0;
6067 	sp->cmn.w2.r_a_tov = 0;
6068 	sp->cls1.classValid = 0;
6069 	sp->cls2.seqDelivery = 1;
6070 	sp->cls3.seqDelivery = 1;
6071 
6072 	pcmd += sizeof(uint32_t); /* CSP Word 2 */
6073 	pcmd += sizeof(uint32_t); /* CSP Word 3 */
6074 	pcmd += sizeof(uint32_t); /* CSP Word 4 */
6075 	pcmd += sizeof(uint32_t); /* Port Name */
6076 	memcpy(pcmd, &vport->fc_portname, 8);
6077 	pcmd += sizeof(uint32_t); /* Node Name */
6078 	pcmd += sizeof(uint32_t); /* Node Name */
6079 	memcpy(pcmd, &vport->fc_nodename, 8);
6080 
6081 	lpfc_set_disctmo(vport);
6082 
6083 	phba->fc_stat.elsXmitFDISC++;
6084 	elsiocb->iocb_cmpl = lpfc_cmpl_els_fdisc;
6085 
6086 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6087 		"Issue FDISC:     did:x%x",
6088 		did, 0, 0);
6089 
6090 	rc = lpfc_issue_fabric_iocb(phba, elsiocb);
6091 	if (rc == IOCB_ERROR) {
6092 		lpfc_els_free_iocb(phba, elsiocb);
6093 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
6094 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
6095 				 "0256 Issue FDISC: Cannot send IOCB\n");
6096 		return 1;
6097 	}
6098 	lpfc_vport_set_state(vport, FC_VPORT_INITIALIZING);
6099 	vport->port_state = LPFC_FDISC;
6100 	return 0;
6101 }
6102 
6103 /**
6104  * lpfc_cmpl_els_npiv_logo: Completion function with vport logo.
6105  * @phba: pointer to lpfc hba data structure.
6106  * @cmdiocb: pointer to lpfc command iocb data structure.
6107  * @rspiocb: pointer to lpfc response iocb data structure.
6108  *
6109  * This routine is the completion callback function to the issuing of a LOGO
6110  * ELS command off a vport. It frees the command IOCB and then decrement the
6111  * reference count held on ndlp for this completion function, indicating that
6112  * the reference to the ndlp is no long needed. Note that the
6113  * lpfc_els_free_iocb() routine decrements the ndlp reference held for this
6114  * callback function and an additional explicit ndlp reference decrementation
6115  * will trigger the actual release of the ndlp.
6116  **/
6117 static void
lpfc_cmpl_els_npiv_logo(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)6118 lpfc_cmpl_els_npiv_logo(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6119 			struct lpfc_iocbq *rspiocb)
6120 {
6121 	struct lpfc_vport *vport = cmdiocb->vport;
6122 	IOCB_t *irsp;
6123 	struct lpfc_nodelist *ndlp;
6124 	ndlp = (struct lpfc_nodelist *)cmdiocb->context1;
6125 
6126 	irsp = &rspiocb->iocb;
6127 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6128 		"LOGO npiv cmpl:  status:x%x/x%x did:x%x",
6129 		irsp->ulpStatus, irsp->un.ulpWord[4], irsp->un.rcvels.remoteID);
6130 
6131 	lpfc_els_free_iocb(phba, cmdiocb);
6132 	vport->unreg_vpi_cmpl = VPORT_ERROR;
6133 
6134 	/* Trigger the release of the ndlp after logo */
6135 	lpfc_nlp_put(ndlp);
6136 }
6137 
6138 /**
6139  * lpfc_issue_els_npiv_logo: Issue a logo off a vport.
6140  * @vport: pointer to a virtual N_Port data structure.
6141  * @ndlp: pointer to a node-list data structure.
6142  *
6143  * This routine issues a LOGO ELS command to an @ndlp off a @vport.
6144  *
6145  * Note that, in lpfc_prep_els_iocb() routine, the reference count of ndlp
6146  * will be incremented by 1 for holding the ndlp and the reference to ndlp
6147  * will be stored into the context1 field of the IOCB for the completion
6148  * callback function to the LOGO ELS command.
6149  *
6150  * Return codes
6151  *   0 - Successfully issued logo off the @vport
6152  *   1 - Failed to issue logo off the @vport
6153  **/
6154 int
lpfc_issue_els_npiv_logo(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)6155 lpfc_issue_els_npiv_logo(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
6156 {
6157 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6158 	struct lpfc_hba  *phba = vport->phba;
6159 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6160 	IOCB_t *icmd;
6161 	struct lpfc_iocbq *elsiocb;
6162 	uint8_t *pcmd;
6163 	uint16_t cmdsize;
6164 
6165 	cmdsize = 2 * sizeof(uint32_t) + sizeof(struct lpfc_name);
6166 	elsiocb = lpfc_prep_els_iocb(vport, 1, cmdsize, 0, ndlp, ndlp->nlp_DID,
6167 				     ELS_CMD_LOGO);
6168 	if (!elsiocb)
6169 		return 1;
6170 
6171 	icmd = &elsiocb->iocb;
6172 	pcmd = (uint8_t *) (((struct lpfc_dmabuf *) elsiocb->context2)->virt);
6173 	*((uint32_t *) (pcmd)) = ELS_CMD_LOGO;
6174 	pcmd += sizeof(uint32_t);
6175 
6176 	/* Fill in LOGO payload */
6177 	*((uint32_t *) (pcmd)) = be32_to_cpu(vport->fc_myDID);
6178 	pcmd += sizeof(uint32_t);
6179 	memcpy(pcmd, &vport->fc_portname, sizeof(struct lpfc_name));
6180 
6181 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6182 		"Issue LOGO npiv  did:x%x flg:x%x",
6183 		ndlp->nlp_DID, ndlp->nlp_flag, 0);
6184 
6185 	elsiocb->iocb_cmpl = lpfc_cmpl_els_npiv_logo;
6186 	spin_lock_irq(shost->host_lock);
6187 	ndlp->nlp_flag |= NLP_LOGO_SND;
6188 	spin_unlock_irq(shost->host_lock);
6189 	if (lpfc_sli_issue_iocb(phba, pring, elsiocb, 0) == IOCB_ERROR) {
6190 		spin_lock_irq(shost->host_lock);
6191 		ndlp->nlp_flag &= ~NLP_LOGO_SND;
6192 		spin_unlock_irq(shost->host_lock);
6193 		lpfc_els_free_iocb(phba, elsiocb);
6194 		return 1;
6195 	}
6196 	return 0;
6197 }
6198 
6199 /**
6200  * lpfc_fabric_block_timeout: Handler function to the fabric block timer.
6201  * @ptr: holder for the timer function associated data.
6202  *
6203  * This routine is invoked by the fabric iocb block timer after
6204  * timeout. It posts the fabric iocb block timeout event by setting the
6205  * WORKER_FABRIC_BLOCK_TMO bit to work port event bitmap and then invokes
6206  * lpfc_worker_wake_up() routine to wake up the worker thread. It is for
6207  * the worker thread to invoke the lpfc_unblock_fabric_iocbs() on the
6208  * posted event WORKER_FABRIC_BLOCK_TMO.
6209  **/
6210 void
lpfc_fabric_block_timeout(unsigned long ptr)6211 lpfc_fabric_block_timeout(unsigned long ptr)
6212 {
6213 	struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
6214 	unsigned long iflags;
6215 	uint32_t tmo_posted;
6216 
6217 	spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
6218 	tmo_posted = phba->pport->work_port_events & WORKER_FABRIC_BLOCK_TMO;
6219 	if (!tmo_posted)
6220 		phba->pport->work_port_events |= WORKER_FABRIC_BLOCK_TMO;
6221 	spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
6222 
6223 	if (!tmo_posted)
6224 		lpfc_worker_wake_up(phba);
6225 	return;
6226 }
6227 
6228 /**
6229  * lpfc_resume_fabric_iocbs: Issue a fabric iocb from driver internal list.
6230  * @phba: pointer to lpfc hba data structure.
6231  *
6232  * This routine issues one fabric iocb from the driver internal list to
6233  * the HBA. It first checks whether it's ready to issue one fabric iocb to
6234  * the HBA (whether there is no outstanding fabric iocb). If so, it shall
6235  * remove one pending fabric iocb from the driver internal list and invokes
6236  * lpfc_sli_issue_iocb() routine to send the fabric iocb to the HBA.
6237  **/
6238 static void
lpfc_resume_fabric_iocbs(struct lpfc_hba * phba)6239 lpfc_resume_fabric_iocbs(struct lpfc_hba *phba)
6240 {
6241 	struct lpfc_iocbq *iocb;
6242 	unsigned long iflags;
6243 	int ret;
6244 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6245 	IOCB_t *cmd;
6246 
6247 repeat:
6248 	iocb = NULL;
6249 	spin_lock_irqsave(&phba->hbalock, iflags);
6250 	/* Post any pending iocb to the SLI layer */
6251 	if (atomic_read(&phba->fabric_iocb_count) == 0) {
6252 		list_remove_head(&phba->fabric_iocb_list, iocb, typeof(*iocb),
6253 				 list);
6254 		if (iocb)
6255 			/* Increment fabric iocb count to hold the position */
6256 			atomic_inc(&phba->fabric_iocb_count);
6257 	}
6258 	spin_unlock_irqrestore(&phba->hbalock, iflags);
6259 	if (iocb) {
6260 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6261 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6262 		iocb->iocb_flag |= LPFC_IO_FABRIC;
6263 
6264 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6265 			"Fabric sched1:   ste:x%x",
6266 			iocb->vport->port_state, 0, 0);
6267 
6268 		ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6269 
6270 		if (ret == IOCB_ERROR) {
6271 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6272 			iocb->fabric_iocb_cmpl = NULL;
6273 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6274 			cmd = &iocb->iocb;
6275 			cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6276 			cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6277 			iocb->iocb_cmpl(phba, iocb, iocb);
6278 
6279 			atomic_dec(&phba->fabric_iocb_count);
6280 			goto repeat;
6281 		}
6282 	}
6283 
6284 	return;
6285 }
6286 
6287 /**
6288  * lpfc_unblock_fabric_iocbs: Unblock issuing fabric iocb command.
6289  * @phba: pointer to lpfc hba data structure.
6290  *
6291  * This routine unblocks the  issuing fabric iocb command. The function
6292  * will clear the fabric iocb block bit and then invoke the routine
6293  * lpfc_resume_fabric_iocbs() to issue one of the pending fabric iocb
6294  * from the driver internal fabric iocb list.
6295  **/
6296 void
lpfc_unblock_fabric_iocbs(struct lpfc_hba * phba)6297 lpfc_unblock_fabric_iocbs(struct lpfc_hba *phba)
6298 {
6299 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6300 
6301 	lpfc_resume_fabric_iocbs(phba);
6302 	return;
6303 }
6304 
6305 /**
6306  * lpfc_block_fabric_iocbs: Block issuing fabric iocb command.
6307  * @phba: pointer to lpfc hba data structure.
6308  *
6309  * This routine blocks the issuing fabric iocb for a specified amount of
6310  * time (currently 100 ms). This is done by set the fabric iocb block bit
6311  * and set up a timeout timer for 100ms. When the block bit is set, no more
6312  * fabric iocb will be issued out of the HBA.
6313  **/
6314 static void
lpfc_block_fabric_iocbs(struct lpfc_hba * phba)6315 lpfc_block_fabric_iocbs(struct lpfc_hba *phba)
6316 {
6317 	int blocked;
6318 
6319 	blocked = test_and_set_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6320 	/* Start a timer to unblock fabric iocbs after 100ms */
6321 	if (!blocked)
6322 		mod_timer(&phba->fabric_block_timer, jiffies + HZ/10 );
6323 
6324 	return;
6325 }
6326 
6327 /**
6328  * lpfc_cmpl_fabric_iocb: Completion callback function for fabric iocb.
6329  * @phba: pointer to lpfc hba data structure.
6330  * @cmdiocb: pointer to lpfc command iocb data structure.
6331  * @rspiocb: pointer to lpfc response iocb data structure.
6332  *
6333  * This routine is the callback function that is put to the fabric iocb's
6334  * callback function pointer (iocb->iocb_cmpl). The original iocb's callback
6335  * function pointer has been stored in iocb->fabric_iocb_cmpl. This callback
6336  * function first restores and invokes the original iocb's callback function
6337  * and then invokes the lpfc_resume_fabric_iocbs() routine to issue the next
6338  * fabric bound iocb from the driver internal fabric iocb list onto the wire.
6339  **/
6340 static void
lpfc_cmpl_fabric_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * cmdiocb,struct lpfc_iocbq * rspiocb)6341 lpfc_cmpl_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6342 	struct lpfc_iocbq *rspiocb)
6343 {
6344 	struct ls_rjt stat;
6345 
6346 	if ((cmdiocb->iocb_flag & LPFC_IO_FABRIC) != LPFC_IO_FABRIC)
6347 		BUG();
6348 
6349 	switch (rspiocb->iocb.ulpStatus) {
6350 		case IOSTAT_NPORT_RJT:
6351 		case IOSTAT_FABRIC_RJT:
6352 			if (rspiocb->iocb.un.ulpWord[4] & RJT_UNAVAIL_TEMP) {
6353 				lpfc_block_fabric_iocbs(phba);
6354 			}
6355 			break;
6356 
6357 		case IOSTAT_NPORT_BSY:
6358 		case IOSTAT_FABRIC_BSY:
6359 			lpfc_block_fabric_iocbs(phba);
6360 			break;
6361 
6362 		case IOSTAT_LS_RJT:
6363 			stat.un.lsRjtError =
6364 				be32_to_cpu(rspiocb->iocb.un.ulpWord[4]);
6365 			if ((stat.un.b.lsRjtRsnCode == LSRJT_UNABLE_TPC) ||
6366 				(stat.un.b.lsRjtRsnCode == LSRJT_LOGICAL_BSY))
6367 				lpfc_block_fabric_iocbs(phba);
6368 			break;
6369 	}
6370 
6371 	if (atomic_read(&phba->fabric_iocb_count) == 0)
6372 		BUG();
6373 
6374 	cmdiocb->iocb_cmpl = cmdiocb->fabric_iocb_cmpl;
6375 	cmdiocb->fabric_iocb_cmpl = NULL;
6376 	cmdiocb->iocb_flag &= ~LPFC_IO_FABRIC;
6377 	cmdiocb->iocb_cmpl(phba, cmdiocb, rspiocb);
6378 
6379 	atomic_dec(&phba->fabric_iocb_count);
6380 	if (!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags)) {
6381 		/* Post any pending iocbs to HBA */
6382 		lpfc_resume_fabric_iocbs(phba);
6383 	}
6384 }
6385 
6386 /**
6387  * lpfc_issue_fabric_iocb: Issue a fabric iocb command.
6388  * @phba: pointer to lpfc hba data structure.
6389  * @iocb: pointer to lpfc command iocb data structure.
6390  *
6391  * This routine is used as the top-level API for issuing a fabric iocb command
6392  * such as FLOGI and FDISC. To accommodate certain switch fabric, this driver
6393  * function makes sure that only one fabric bound iocb will be outstanding at
6394  * any given time. As such, this function will first check to see whether there
6395  * is already an outstanding fabric iocb on the wire. If so, it will put the
6396  * newly issued iocb onto the driver internal fabric iocb list, waiting to be
6397  * issued later. Otherwise, it will issue the iocb on the wire and update the
6398  * fabric iocb count it indicate that there is one fabric iocb on the wire.
6399  *
6400  * Note, this implementation has a potential sending out fabric IOCBs out of
6401  * order. The problem is caused by the construction of the "ready" boolen does
6402  * not include the condition that the internal fabric IOCB list is empty. As
6403  * such, it is possible a fabric IOCB issued by this routine might be "jump"
6404  * ahead of the fabric IOCBs in the internal list.
6405  *
6406  * Return code
6407  *   IOCB_SUCCESS - either fabric iocb put on the list or issued successfully
6408  *   IOCB_ERROR - failed to issue fabric iocb
6409  **/
6410 static int
lpfc_issue_fabric_iocb(struct lpfc_hba * phba,struct lpfc_iocbq * iocb)6411 lpfc_issue_fabric_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *iocb)
6412 {
6413 	unsigned long iflags;
6414 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6415 	int ready;
6416 	int ret;
6417 
6418 	if (atomic_read(&phba->fabric_iocb_count) > 1)
6419 		BUG();
6420 
6421 	spin_lock_irqsave(&phba->hbalock, iflags);
6422 	ready = atomic_read(&phba->fabric_iocb_count) == 0 &&
6423 		!test_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
6424 
6425 	if (ready)
6426 		/* Increment fabric iocb count to hold the position */
6427 		atomic_inc(&phba->fabric_iocb_count);
6428 	spin_unlock_irqrestore(&phba->hbalock, iflags);
6429 	if (ready) {
6430 		iocb->fabric_iocb_cmpl = iocb->iocb_cmpl;
6431 		iocb->iocb_cmpl = lpfc_cmpl_fabric_iocb;
6432 		iocb->iocb_flag |= LPFC_IO_FABRIC;
6433 
6434 		lpfc_debugfs_disc_trc(iocb->vport, LPFC_DISC_TRC_ELS_CMD,
6435 			"Fabric sched2:   ste:x%x",
6436 			iocb->vport->port_state, 0, 0);
6437 
6438 		ret = lpfc_sli_issue_iocb(phba, pring, iocb, 0);
6439 
6440 		if (ret == IOCB_ERROR) {
6441 			iocb->iocb_cmpl = iocb->fabric_iocb_cmpl;
6442 			iocb->fabric_iocb_cmpl = NULL;
6443 			iocb->iocb_flag &= ~LPFC_IO_FABRIC;
6444 			atomic_dec(&phba->fabric_iocb_count);
6445 		}
6446 	} else {
6447 		spin_lock_irqsave(&phba->hbalock, iflags);
6448 		list_add_tail(&iocb->list, &phba->fabric_iocb_list);
6449 		spin_unlock_irqrestore(&phba->hbalock, iflags);
6450 		ret = IOCB_SUCCESS;
6451 	}
6452 	return ret;
6453 }
6454 
6455 /**
6456  * lpfc_fabric_abort_vport: Abort a vport's iocbs from driver fabric iocb list.
6457  * @vport: pointer to a virtual N_Port data structure.
6458  *
6459  * This routine aborts all the IOCBs associated with a @vport from the
6460  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6461  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6462  * list, removes each IOCB associated with the @vport off the list, set the
6463  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6464  * associated with the IOCB.
6465  **/
lpfc_fabric_abort_vport(struct lpfc_vport * vport)6466 static void lpfc_fabric_abort_vport(struct lpfc_vport *vport)
6467 {
6468 	LIST_HEAD(completions);
6469 	struct lpfc_hba  *phba = vport->phba;
6470 	struct lpfc_iocbq *tmp_iocb, *piocb;
6471 	IOCB_t *cmd;
6472 
6473 	spin_lock_irq(&phba->hbalock);
6474 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6475 				 list) {
6476 
6477 		if (piocb->vport != vport)
6478 			continue;
6479 
6480 		list_move_tail(&piocb->list, &completions);
6481 	}
6482 	spin_unlock_irq(&phba->hbalock);
6483 
6484 	while (!list_empty(&completions)) {
6485 		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6486 		list_del_init(&piocb->list);
6487 
6488 		cmd = &piocb->iocb;
6489 		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6490 		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6491 		(piocb->iocb_cmpl) (phba, piocb, piocb);
6492 	}
6493 }
6494 
6495 /**
6496  * lpfc_fabric_abort_nport: Abort a ndlp's iocbs from driver fabric iocb list.
6497  * @ndlp: pointer to a node-list data structure.
6498  *
6499  * This routine aborts all the IOCBs associated with an @ndlp from the
6500  * driver internal fabric IOCB list. The list contains fabric IOCBs to be
6501  * issued to the ELS IOCB ring. This abort function walks the fabric IOCB
6502  * list, removes each IOCB associated with the @ndlp off the list, set the
6503  * status feild to IOSTAT_LOCAL_REJECT, and invokes the callback function
6504  * associated with the IOCB.
6505  **/
lpfc_fabric_abort_nport(struct lpfc_nodelist * ndlp)6506 void lpfc_fabric_abort_nport(struct lpfc_nodelist *ndlp)
6507 {
6508 	LIST_HEAD(completions);
6509 	struct lpfc_hba  *phba = ndlp->vport->phba;
6510 	struct lpfc_iocbq *tmp_iocb, *piocb;
6511 	struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6512 	IOCB_t *cmd;
6513 
6514 	spin_lock_irq(&phba->hbalock);
6515 	list_for_each_entry_safe(piocb, tmp_iocb, &phba->fabric_iocb_list,
6516 				 list) {
6517 		if ((lpfc_check_sli_ndlp(phba, pring, piocb, ndlp))) {
6518 
6519 			list_move_tail(&piocb->list, &completions);
6520 		}
6521 	}
6522 	spin_unlock_irq(&phba->hbalock);
6523 
6524 	while (!list_empty(&completions)) {
6525 		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6526 		list_del_init(&piocb->list);
6527 
6528 		cmd = &piocb->iocb;
6529 		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6530 		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6531 		(piocb->iocb_cmpl) (phba, piocb, piocb);
6532 	}
6533 }
6534 
6535 /**
6536  * lpfc_fabric_abort_hba: Abort all iocbs on driver fabric iocb list.
6537  * @phba: pointer to lpfc hba data structure.
6538  *
6539  * This routine aborts all the IOCBs currently on the driver internal
6540  * fabric IOCB list. The list contains fabric IOCBs to be issued to the ELS
6541  * IOCB ring. This function takes the entire IOCB list off the fabric IOCB
6542  * list, removes IOCBs off the list, set the status feild to
6543  * IOSTAT_LOCAL_REJECT, and invokes the callback function associated with
6544  * the IOCB.
6545  **/
lpfc_fabric_abort_hba(struct lpfc_hba * phba)6546 void lpfc_fabric_abort_hba(struct lpfc_hba *phba)
6547 {
6548 	LIST_HEAD(completions);
6549 	struct lpfc_iocbq *piocb;
6550 	IOCB_t *cmd;
6551 
6552 	spin_lock_irq(&phba->hbalock);
6553 	list_splice_init(&phba->fabric_iocb_list, &completions);
6554 	spin_unlock_irq(&phba->hbalock);
6555 
6556 	while (!list_empty(&completions)) {
6557 		piocb = list_get_first(&completions, struct lpfc_iocbq, list);
6558 		list_del_init(&piocb->list);
6559 
6560 		cmd = &piocb->iocb;
6561 		cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
6562 		cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
6563 		(piocb->iocb_cmpl) (phba, piocb, piocb);
6564 	}
6565 }
6566