xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_hbadisc.c (revision 45230f31)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2017-2023 Broadcom. All Rights Reserved. The term *
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7  * EMULEX and SLI are trademarks of Emulex.                        *
8  * www.broadcom.com                                                *
9  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10  *                                                                 *
11  * This program is free software; you can redistribute it and/or   *
12  * modify it under the terms of version 2 of the GNU General       *
13  * Public License as published by the Free Software Foundation.    *
14  * This program is distributed in the hope that it will be useful. *
15  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20  * more details, a copy of which can be found in the file COPYING  *
21  * included with this package.                                     *
22  *******************************************************************/
23 
24 #include <linux/blkdev.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/pci.h>
28 #include <linux/kthread.h>
29 #include <linux/interrupt.h>
30 #include <linux/lockdep.h>
31 #include <linux/utsname.h>
32 
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
38 
39 #include "lpfc_hw4.h"
40 #include "lpfc_hw.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_sli.h"
44 #include "lpfc_sli4.h"
45 #include "lpfc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_debugfs.h"
52 
53 /* AlpaArray for assignment of scsid for scan-down and bind_method */
54 static uint8_t lpfcAlpaArray[] = {
55 	0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
56 	0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
57 	0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
58 	0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
59 	0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
60 	0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
61 	0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
62 	0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
63 	0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
64 	0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
65 	0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
66 	0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
67 	0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
68 };
69 
70 static void lpfc_disc_timeout_handler(struct lpfc_vport *);
71 static void lpfc_disc_flush_list(struct lpfc_vport *vport);
72 static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
73 static int lpfc_fcf_inuse(struct lpfc_hba *);
74 static void lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *, LPFC_MBOXQ_t *);
75 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba);
76 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba);
77 
78 static int
lpfc_valid_xpt_node(struct lpfc_nodelist * ndlp)79 lpfc_valid_xpt_node(struct lpfc_nodelist *ndlp)
80 {
81 	if (ndlp->nlp_fc4_type ||
82 	    ndlp->nlp_type & NLP_FABRIC)
83 		return 1;
84 	return 0;
85 }
86 /* The source of a terminate rport I/O is either a dev_loss_tmo
87  * event or a call to fc_remove_host.  While the rport should be
88  * valid during these downcalls, the transport can call twice
89  * in a single event.  This routine provides somoe protection
90  * as the NDLP isn't really free, just released to the pool.
91  */
92 static int
lpfc_rport_invalid(struct fc_rport * rport)93 lpfc_rport_invalid(struct fc_rport *rport)
94 {
95 	struct lpfc_rport_data *rdata;
96 	struct lpfc_nodelist *ndlp;
97 
98 	if (!rport) {
99 		pr_err("**** %s: NULL rport, exit.\n", __func__);
100 		return -EINVAL;
101 	}
102 
103 	rdata = rport->dd_data;
104 	if (!rdata) {
105 		pr_err("**** %s: NULL dd_data on rport x%px SID x%x\n",
106 		       __func__, rport, rport->scsi_target_id);
107 		return -EINVAL;
108 	}
109 
110 	ndlp = rdata->pnode;
111 	if (!rdata->pnode) {
112 		pr_info("**** %s: NULL ndlp on rport x%px SID x%x\n",
113 			__func__, rport, rport->scsi_target_id);
114 		return -EINVAL;
115 	}
116 
117 	if (!ndlp->vport) {
118 		pr_err("**** %s: Null vport on ndlp x%px, DID x%x rport x%px "
119 		       "SID x%x\n", __func__, ndlp, ndlp->nlp_DID, rport,
120 		       rport->scsi_target_id);
121 		return -EINVAL;
122 	}
123 	return 0;
124 }
125 
126 void
lpfc_terminate_rport_io(struct fc_rport * rport)127 lpfc_terminate_rport_io(struct fc_rport *rport)
128 {
129 	struct lpfc_rport_data *rdata;
130 	struct lpfc_nodelist *ndlp;
131 	struct lpfc_vport *vport;
132 
133 	if (lpfc_rport_invalid(rport))
134 		return;
135 
136 	rdata = rport->dd_data;
137 	ndlp = rdata->pnode;
138 	vport = ndlp->vport;
139 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
140 			      "rport terminate: sid:x%x did:x%x flg:x%x",
141 			      ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
142 
143 	if (ndlp->nlp_sid != NLP_NO_SID)
144 		lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
145 }
146 
147 /*
148  * This function will be called when dev_loss_tmo fire.
149  */
150 void
lpfc_dev_loss_tmo_callbk(struct fc_rport * rport)151 lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
152 {
153 	struct lpfc_nodelist *ndlp;
154 	struct lpfc_vport *vport;
155 	struct lpfc_hba   *phba;
156 	struct lpfc_work_evt *evtp;
157 	unsigned long iflags;
158 
159 	ndlp = ((struct lpfc_rport_data *)rport->dd_data)->pnode;
160 	if (!ndlp)
161 		return;
162 
163 	vport = ndlp->vport;
164 	phba  = vport->phba;
165 
166 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
167 		"rport devlosscb: sid:x%x did:x%x flg:x%x",
168 		ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
169 
170 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
171 			 "3181 dev_loss_callbk x%06x, rport x%px flg x%x "
172 			 "load_flag x%x refcnt %u state %d xpt x%x\n",
173 			 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag,
174 			 vport->load_flag, kref_read(&ndlp->kref),
175 			 ndlp->nlp_state, ndlp->fc4_xpt_flags);
176 
177 	/* Don't schedule a worker thread event if the vport is going down. */
178 	if (vport->load_flag & FC_UNLOADING) {
179 		spin_lock_irqsave(&ndlp->lock, iflags);
180 		ndlp->rport = NULL;
181 
182 		/* The scsi_transport is done with the rport so lpfc cannot
183 		 * call to unregister. Remove the scsi transport reference
184 		 * and clean up the SCSI transport node details.
185 		 */
186 		if (ndlp->fc4_xpt_flags & (NLP_XPT_REGD | SCSI_XPT_REGD)) {
187 			ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
188 
189 			/* NVME transport-registered rports need the
190 			 * NLP_XPT_REGD flag to complete an unregister.
191 			 */
192 			if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
193 				ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
194 			spin_unlock_irqrestore(&ndlp->lock, iflags);
195 			lpfc_nlp_put(ndlp);
196 			spin_lock_irqsave(&ndlp->lock, iflags);
197 		}
198 
199 		/* Only 1 thread can drop the initial node reference.  If
200 		 * another thread has set NLP_DROPPED, this thread is done.
201 		 */
202 		if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD) &&
203 		    !(ndlp->nlp_flag & NLP_DROPPED)) {
204 			ndlp->nlp_flag |= NLP_DROPPED;
205 			spin_unlock_irqrestore(&ndlp->lock, iflags);
206 			lpfc_nlp_put(ndlp);
207 			return;
208 		}
209 
210 		spin_unlock_irqrestore(&ndlp->lock, iflags);
211 		return;
212 	}
213 
214 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
215 		return;
216 
217 	if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
218 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
219 				 "6789 rport name %llx != node port name %llx",
220 				 rport->port_name,
221 				 wwn_to_u64(ndlp->nlp_portname.u.wwn));
222 
223 	evtp = &ndlp->dev_loss_evt;
224 
225 	if (!list_empty(&evtp->evt_listp)) {
226 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
227 				 "6790 rport name %llx dev_loss_evt pending\n",
228 				 rport->port_name);
229 		return;
230 	}
231 
232 	spin_lock_irqsave(&ndlp->lock, iflags);
233 	ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
234 
235 	/* If there is a PLOGI in progress, and we are in a
236 	 * NLP_NPR_2B_DISC state, don't turn off the flag.
237 	 */
238 	if (ndlp->nlp_state != NLP_STE_PLOGI_ISSUE)
239 		ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
240 
241 	/*
242 	 * The backend does not expect any more calls associated with this
243 	 * rport. Remove the association between rport and ndlp.
244 	 */
245 	ndlp->fc4_xpt_flags &= ~SCSI_XPT_REGD;
246 	((struct lpfc_rport_data *)rport->dd_data)->pnode = NULL;
247 	ndlp->rport = NULL;
248 	spin_unlock_irqrestore(&ndlp->lock, iflags);
249 
250 	if (phba->worker_thread) {
251 		/* We need to hold the node by incrementing the reference
252 		 * count until this queued work is done
253 		 */
254 		evtp->evt_arg1 = lpfc_nlp_get(ndlp);
255 
256 		spin_lock_irqsave(&phba->hbalock, iflags);
257 		if (evtp->evt_arg1) {
258 			evtp->evt = LPFC_EVT_DEV_LOSS;
259 			list_add_tail(&evtp->evt_listp, &phba->work_list);
260 			spin_unlock_irqrestore(&phba->hbalock, iflags);
261 			lpfc_worker_wake_up(phba);
262 			return;
263 		}
264 		spin_unlock_irqrestore(&phba->hbalock, iflags);
265 	} else {
266 		lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
267 				 "3188 worker thread is stopped %s x%06x, "
268 				 " rport x%px flg x%x load_flag x%x refcnt "
269 				 "%d\n", __func__, ndlp->nlp_DID,
270 				 ndlp->rport, ndlp->nlp_flag,
271 				 vport->load_flag, kref_read(&ndlp->kref));
272 		if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD)) {
273 			spin_lock_irqsave(&ndlp->lock, iflags);
274 			/* Node is in dev loss.  No further transaction. */
275 			ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
276 			spin_unlock_irqrestore(&ndlp->lock, iflags);
277 			lpfc_disc_state_machine(vport, ndlp, NULL,
278 						NLP_EVT_DEVICE_RM);
279 		}
280 	}
281 }
282 
283 /**
284  * lpfc_check_inactive_vmid_one - VMID inactivity checker for a vport
285  * @vport: Pointer to vport context object.
286  *
287  * This function checks for idle VMID entries related to a particular vport. If
288  * found unused/idle, free them accordingly.
289  **/
lpfc_check_inactive_vmid_one(struct lpfc_vport * vport)290 static void lpfc_check_inactive_vmid_one(struct lpfc_vport *vport)
291 {
292 	u16 keep;
293 	u32 difftime = 0, r, bucket;
294 	u64 *lta;
295 	int cpu;
296 	struct lpfc_vmid *vmp;
297 
298 	write_lock(&vport->vmid_lock);
299 
300 	if (!vport->cur_vmid_cnt)
301 		goto out;
302 
303 	/* iterate through the table */
304 	hash_for_each(vport->hash_table, bucket, vmp, hnode) {
305 		keep = 0;
306 		if (vmp->flag & LPFC_VMID_REGISTERED) {
307 			/* check if the particular VMID is in use */
308 			/* for all available per cpu variable */
309 			for_each_possible_cpu(cpu) {
310 				/* if last access time is less than timeout */
311 				lta = per_cpu_ptr(vmp->last_io_time, cpu);
312 				if (!lta)
313 					continue;
314 				difftime = (jiffies) - (*lta);
315 				if ((vport->vmid_inactivity_timeout *
316 				     JIFFIES_PER_HR) > difftime) {
317 					keep = 1;
318 					break;
319 				}
320 			}
321 
322 			/* if none of the cpus have been used by the vm, */
323 			/*  remove the entry if already registered */
324 			if (!keep) {
325 				/* mark the entry for deregistration */
326 				vmp->flag = LPFC_VMID_DE_REGISTER;
327 				write_unlock(&vport->vmid_lock);
328 				if (vport->vmid_priority_tagging)
329 					r = lpfc_vmid_uvem(vport, vmp, false);
330 				else
331 					r = lpfc_vmid_cmd(vport,
332 							  SLI_CTAS_DAPP_IDENT,
333 							  vmp);
334 
335 				/* decrement number of active vms and mark */
336 				/* entry in slot as free */
337 				write_lock(&vport->vmid_lock);
338 				if (!r) {
339 					struct lpfc_vmid *ht = vmp;
340 
341 					vport->cur_vmid_cnt--;
342 					ht->flag = LPFC_VMID_SLOT_FREE;
343 					free_percpu(ht->last_io_time);
344 					ht->last_io_time = NULL;
345 					hash_del(&ht->hnode);
346 				}
347 			}
348 		}
349 	}
350  out:
351 	write_unlock(&vport->vmid_lock);
352 }
353 
354 /**
355  * lpfc_check_inactive_vmid - VMID inactivity checker
356  * @phba: Pointer to hba context object.
357  *
358  * This function is called from the worker thread to determine if an entry in
359  * the VMID table can be released since there was no I/O activity seen from that
360  * particular VM for the specified time. When this happens, the entry in the
361  * table is released and also the resources on the switch cleared.
362  **/
363 
lpfc_check_inactive_vmid(struct lpfc_hba * phba)364 static void lpfc_check_inactive_vmid(struct lpfc_hba *phba)
365 {
366 	struct lpfc_vport *vport;
367 	struct lpfc_vport **vports;
368 	int i;
369 
370 	vports = lpfc_create_vport_work_array(phba);
371 	if (!vports)
372 		return;
373 
374 	for (i = 0; i <= phba->max_vports; i++) {
375 		if ((!vports[i]) && (i == 0))
376 			vport = phba->pport;
377 		else
378 			vport = vports[i];
379 		if (!vport)
380 			break;
381 
382 		lpfc_check_inactive_vmid_one(vport);
383 	}
384 	lpfc_destroy_vport_work_array(phba, vports);
385 }
386 
387 /**
388  * lpfc_check_nlp_post_devloss - Check to restore ndlp refcnt after devloss
389  * @vport: Pointer to vport object.
390  * @ndlp: Pointer to remote node object.
391  *
392  * If NLP_IN_RECOV_POST_DEV_LOSS flag was set due to outstanding recovery of
393  * node during dev_loss_tmo processing, then this function restores the nlp_put
394  * kref decrement from lpfc_dev_loss_tmo_handler.
395  **/
396 void
lpfc_check_nlp_post_devloss(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)397 lpfc_check_nlp_post_devloss(struct lpfc_vport *vport,
398 			    struct lpfc_nodelist *ndlp)
399 {
400 	unsigned long iflags;
401 
402 	spin_lock_irqsave(&ndlp->lock, iflags);
403 	if (ndlp->save_flags & NLP_IN_RECOV_POST_DEV_LOSS) {
404 		ndlp->save_flags &= ~NLP_IN_RECOV_POST_DEV_LOSS;
405 		spin_unlock_irqrestore(&ndlp->lock, iflags);
406 		lpfc_nlp_get(ndlp);
407 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY | LOG_NODE,
408 				 "8438 Devloss timeout reversed on DID x%x "
409 				 "refcnt %d ndlp %p flag x%x "
410 				 "port_state = x%x\n",
411 				 ndlp->nlp_DID, kref_read(&ndlp->kref), ndlp,
412 				 ndlp->nlp_flag, vport->port_state);
413 		spin_lock_irqsave(&ndlp->lock, iflags);
414 	}
415 	spin_unlock_irqrestore(&ndlp->lock, iflags);
416 }
417 
418 /**
419  * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
420  * @ndlp: Pointer to remote node object.
421  *
422  * This function is called from the worker thread when devloss timeout timer
423  * expires. For SLI4 host, this routine shall return 1 when at lease one
424  * remote node, including this @ndlp, is still in use of FCF; otherwise, this
425  * routine shall return 0 when there is no remote node is still in use of FCF
426  * when devloss timeout happened to this @ndlp.
427  **/
428 static int
lpfc_dev_loss_tmo_handler(struct lpfc_nodelist * ndlp)429 lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
430 {
431 	struct lpfc_vport *vport;
432 	struct lpfc_hba   *phba;
433 	uint8_t *name;
434 	int warn_on = 0;
435 	int fcf_inuse = 0;
436 	bool recovering = false;
437 	struct fc_vport *fc_vport = NULL;
438 	unsigned long iflags;
439 
440 	vport = ndlp->vport;
441 	name = (uint8_t *)&ndlp->nlp_portname;
442 	phba = vport->phba;
443 
444 	if (phba->sli_rev == LPFC_SLI_REV4)
445 		fcf_inuse = lpfc_fcf_inuse(phba);
446 
447 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
448 			      "rport devlosstmo:did:x%x type:x%x id:x%x",
449 			      ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_sid);
450 
451 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
452 			 "3182 %s x%06x, nflag x%x xflags x%x refcnt %d\n",
453 			 __func__, ndlp->nlp_DID, ndlp->nlp_flag,
454 			 ndlp->fc4_xpt_flags, kref_read(&ndlp->kref));
455 
456 	/* If the driver is recovering the rport, ignore devloss. */
457 	if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
458 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
459 				 "0284 Devloss timeout Ignored on "
460 				 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
461 				 "NPort x%x\n",
462 				 *name, *(name+1), *(name+2), *(name+3),
463 				 *(name+4), *(name+5), *(name+6), *(name+7),
464 				 ndlp->nlp_DID);
465 
466 		spin_lock_irqsave(&ndlp->lock, iflags);
467 		ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
468 		spin_unlock_irqrestore(&ndlp->lock, iflags);
469 		return fcf_inuse;
470 	}
471 
472 	/* Fabric nodes are done. */
473 	if (ndlp->nlp_type & NLP_FABRIC) {
474 		spin_lock_irqsave(&ndlp->lock, iflags);
475 
476 		/* The driver has to account for a race between any fabric
477 		 * node that's in recovery when dev_loss_tmo expires. When this
478 		 * happens, the driver has to allow node recovery.
479 		 */
480 		switch (ndlp->nlp_DID) {
481 		case Fabric_DID:
482 			fc_vport = vport->fc_vport;
483 			if (fc_vport) {
484 				/* NPIV path. */
485 				if (fc_vport->vport_state ==
486 				    FC_VPORT_INITIALIZING)
487 					recovering = true;
488 			} else {
489 				/* Physical port path. */
490 				if (phba->hba_flag & HBA_FLOGI_OUTSTANDING)
491 					recovering = true;
492 			}
493 			break;
494 		case Fabric_Cntl_DID:
495 			if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
496 				recovering = true;
497 			break;
498 		case FDMI_DID:
499 			fallthrough;
500 		case NameServer_DID:
501 			if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
502 			    ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE)
503 				recovering = true;
504 			break;
505 		default:
506 			/* Ensure the nlp_DID at least has the correct prefix.
507 			 * The fabric domain controller's last three nibbles
508 			 * vary so we handle it in the default case.
509 			 */
510 			if (ndlp->nlp_DID & Fabric_DID_MASK) {
511 				if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
512 				    ndlp->nlp_state <= NLP_STE_REG_LOGIN_ISSUE)
513 					recovering = true;
514 			}
515 			break;
516 		}
517 		spin_unlock_irqrestore(&ndlp->lock, iflags);
518 
519 		/* Mark an NLP_IN_RECOV_POST_DEV_LOSS flag to know if reversing
520 		 * the following lpfc_nlp_put is necessary after fabric node is
521 		 * recovered.
522 		 */
523 		if (recovering) {
524 			lpfc_printf_vlog(vport, KERN_INFO,
525 					 LOG_DISCOVERY | LOG_NODE,
526 					 "8436 Devloss timeout marked on "
527 					 "DID x%x refcnt %d ndlp %p "
528 					 "flag x%x port_state = x%x\n",
529 					 ndlp->nlp_DID, kref_read(&ndlp->kref),
530 					 ndlp, ndlp->nlp_flag,
531 					 vport->port_state);
532 			spin_lock_irqsave(&ndlp->lock, iflags);
533 			ndlp->save_flags |= NLP_IN_RECOV_POST_DEV_LOSS;
534 			spin_unlock_irqrestore(&ndlp->lock, iflags);
535 		} else if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE) {
536 			/* Fabric node fully recovered before this dev_loss_tmo
537 			 * queue work is processed.  Thus, ignore the
538 			 * dev_loss_tmo event.
539 			 */
540 			lpfc_printf_vlog(vport, KERN_INFO,
541 					 LOG_DISCOVERY | LOG_NODE,
542 					 "8437 Devloss timeout ignored on "
543 					 "DID x%x refcnt %d ndlp %p "
544 					 "flag x%x port_state = x%x\n",
545 					 ndlp->nlp_DID, kref_read(&ndlp->kref),
546 					 ndlp, ndlp->nlp_flag,
547 					 vport->port_state);
548 			return fcf_inuse;
549 		}
550 
551 		spin_lock_irqsave(&ndlp->lock, iflags);
552 		ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
553 		spin_unlock_irqrestore(&ndlp->lock, iflags);
554 		lpfc_nlp_put(ndlp);
555 		return fcf_inuse;
556 	}
557 
558 	if (ndlp->nlp_sid != NLP_NO_SID) {
559 		warn_on = 1;
560 		lpfc_sli_abort_iocb(vport, ndlp->nlp_sid, 0, LPFC_CTX_TGT);
561 	}
562 
563 	if (warn_on) {
564 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
565 				 "0203 Devloss timeout on "
566 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
567 				 "NPort x%06x Data: x%x x%x x%x refcnt %d\n",
568 				 *name, *(name+1), *(name+2), *(name+3),
569 				 *(name+4), *(name+5), *(name+6), *(name+7),
570 				 ndlp->nlp_DID, ndlp->nlp_flag,
571 				 ndlp->nlp_state, ndlp->nlp_rpi,
572 				 kref_read(&ndlp->kref));
573 	} else {
574 		lpfc_printf_vlog(vport, KERN_INFO, LOG_TRACE_EVENT,
575 				 "0204 Devloss timeout on "
576 				 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
577 				 "NPort x%06x Data: x%x x%x x%x\n",
578 				 *name, *(name+1), *(name+2), *(name+3),
579 				 *(name+4), *(name+5), *(name+6), *(name+7),
580 				 ndlp->nlp_DID, ndlp->nlp_flag,
581 				 ndlp->nlp_state, ndlp->nlp_rpi);
582 	}
583 	spin_lock_irqsave(&ndlp->lock, iflags);
584 	ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
585 	spin_unlock_irqrestore(&ndlp->lock, iflags);
586 
587 	/* If we are devloss, but we are in the process of rediscovering the
588 	 * ndlp, don't issue a NLP_EVT_DEVICE_RM event.
589 	 */
590 	if (ndlp->nlp_state >= NLP_STE_PLOGI_ISSUE &&
591 	    ndlp->nlp_state <= NLP_STE_PRLI_ISSUE) {
592 		return fcf_inuse;
593 	}
594 
595 	if (!(ndlp->fc4_xpt_flags & NVME_XPT_REGD))
596 		lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
597 
598 	return fcf_inuse;
599 }
600 
lpfc_check_vmid_qfpa_issue(struct lpfc_hba * phba)601 static void lpfc_check_vmid_qfpa_issue(struct lpfc_hba *phba)
602 {
603 	struct lpfc_vport *vport;
604 	struct lpfc_vport **vports;
605 	int i;
606 
607 	vports = lpfc_create_vport_work_array(phba);
608 	if (!vports)
609 		return;
610 
611 	for (i = 0; i <= phba->max_vports; i++) {
612 		if ((!vports[i]) && (i == 0))
613 			vport = phba->pport;
614 		else
615 			vport = vports[i];
616 		if (!vport)
617 			break;
618 
619 		if (vport->vmid_flag & LPFC_VMID_ISSUE_QFPA) {
620 			if (!lpfc_issue_els_qfpa(vport))
621 				vport->vmid_flag &= ~LPFC_VMID_ISSUE_QFPA;
622 		}
623 	}
624 	lpfc_destroy_vport_work_array(phba, vports);
625 }
626 
627 /**
628  * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
629  * @phba: Pointer to hba context object.
630  * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
631  * @nlp_did: remote node identifer with devloss timeout.
632  *
633  * This function is called from the worker thread after invoking devloss
634  * timeout handler and releasing the reference count for the ndlp with
635  * which the devloss timeout was handled for SLI4 host. For the devloss
636  * timeout of the last remote node which had been in use of FCF, when this
637  * routine is invoked, it shall be guaranteed that none of the remote are
638  * in-use of FCF. When devloss timeout to the last remote using the FCF,
639  * if the FIP engine is neither in FCF table scan process nor roundrobin
640  * failover process, the in-use FCF shall be unregistered. If the FIP
641  * engine is in FCF discovery process, the devloss timeout state shall
642  * be set for either the FCF table scan process or roundrobin failover
643  * process to unregister the in-use FCF.
644  **/
645 static void
lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba * phba,int fcf_inuse,uint32_t nlp_did)646 lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
647 				    uint32_t nlp_did)
648 {
649 	/* If devloss timeout happened to a remote node when FCF had no
650 	 * longer been in-use, do nothing.
651 	 */
652 	if (!fcf_inuse)
653 		return;
654 
655 	if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
656 		spin_lock_irq(&phba->hbalock);
657 		if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
658 			if (phba->hba_flag & HBA_DEVLOSS_TMO) {
659 				spin_unlock_irq(&phba->hbalock);
660 				return;
661 			}
662 			phba->hba_flag |= HBA_DEVLOSS_TMO;
663 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
664 					"2847 Last remote node (x%x) using "
665 					"FCF devloss tmo\n", nlp_did);
666 		}
667 		if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
668 			spin_unlock_irq(&phba->hbalock);
669 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
670 					"2868 Devloss tmo to FCF rediscovery "
671 					"in progress\n");
672 			return;
673 		}
674 		if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
675 			spin_unlock_irq(&phba->hbalock);
676 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
677 					"2869 Devloss tmo to idle FIP engine, "
678 					"unreg in-use FCF and rescan.\n");
679 			/* Unregister in-use FCF and rescan */
680 			lpfc_unregister_fcf_rescan(phba);
681 			return;
682 		}
683 		spin_unlock_irq(&phba->hbalock);
684 		if (phba->hba_flag & FCF_TS_INPROG)
685 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
686 					"2870 FCF table scan in progress\n");
687 		if (phba->hba_flag & FCF_RR_INPROG)
688 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
689 					"2871 FLOGI roundrobin FCF failover "
690 					"in progress\n");
691 	}
692 	lpfc_unregister_unused_fcf(phba);
693 }
694 
695 /**
696  * lpfc_alloc_fast_evt - Allocates data structure for posting event
697  * @phba: Pointer to hba context object.
698  *
699  * This function is called from the functions which need to post
700  * events from interrupt context. This function allocates data
701  * structure required for posting event. It also keeps track of
702  * number of events pending and prevent event storm when there are
703  * too many events.
704  **/
705 struct lpfc_fast_path_event *
lpfc_alloc_fast_evt(struct lpfc_hba * phba)706 lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
707 	struct lpfc_fast_path_event *ret;
708 
709 	/* If there are lot of fast event do not exhaust memory due to this */
710 	if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
711 		return NULL;
712 
713 	ret = kzalloc(sizeof(struct lpfc_fast_path_event),
714 			GFP_ATOMIC);
715 	if (ret) {
716 		atomic_inc(&phba->fast_event_count);
717 		INIT_LIST_HEAD(&ret->work_evt.evt_listp);
718 		ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
719 	}
720 	return ret;
721 }
722 
723 /**
724  * lpfc_free_fast_evt - Frees event data structure
725  * @phba: Pointer to hba context object.
726  * @evt:  Event object which need to be freed.
727  *
728  * This function frees the data structure required for posting
729  * events.
730  **/
731 void
lpfc_free_fast_evt(struct lpfc_hba * phba,struct lpfc_fast_path_event * evt)732 lpfc_free_fast_evt(struct lpfc_hba *phba,
733 		struct lpfc_fast_path_event *evt) {
734 
735 	atomic_dec(&phba->fast_event_count);
736 	kfree(evt);
737 }
738 
739 /**
740  * lpfc_send_fastpath_evt - Posts events generated from fast path
741  * @phba: Pointer to hba context object.
742  * @evtp: Event data structure.
743  *
744  * This function is called from worker thread, when the interrupt
745  * context need to post an event. This function posts the event
746  * to fc transport netlink interface.
747  **/
748 static void
lpfc_send_fastpath_evt(struct lpfc_hba * phba,struct lpfc_work_evt * evtp)749 lpfc_send_fastpath_evt(struct lpfc_hba *phba,
750 		struct lpfc_work_evt *evtp)
751 {
752 	unsigned long evt_category, evt_sub_category;
753 	struct lpfc_fast_path_event *fast_evt_data;
754 	char *evt_data;
755 	uint32_t evt_data_size;
756 	struct Scsi_Host *shost;
757 
758 	fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
759 		work_evt);
760 
761 	evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
762 	evt_sub_category = (unsigned long) fast_evt_data->un.
763 			fabric_evt.subcategory;
764 	shost = lpfc_shost_from_vport(fast_evt_data->vport);
765 	if (evt_category == FC_REG_FABRIC_EVENT) {
766 		if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
767 			evt_data = (char *) &fast_evt_data->un.read_check_error;
768 			evt_data_size = sizeof(fast_evt_data->un.
769 				read_check_error);
770 		} else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
771 			(evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
772 			evt_data = (char *) &fast_evt_data->un.fabric_evt;
773 			evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
774 		} else {
775 			lpfc_free_fast_evt(phba, fast_evt_data);
776 			return;
777 		}
778 	} else if (evt_category == FC_REG_SCSI_EVENT) {
779 		switch (evt_sub_category) {
780 		case LPFC_EVENT_QFULL:
781 		case LPFC_EVENT_DEVBSY:
782 			evt_data = (char *) &fast_evt_data->un.scsi_evt;
783 			evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
784 			break;
785 		case LPFC_EVENT_CHECK_COND:
786 			evt_data = (char *) &fast_evt_data->un.check_cond_evt;
787 			evt_data_size =  sizeof(fast_evt_data->un.
788 				check_cond_evt);
789 			break;
790 		case LPFC_EVENT_VARQUEDEPTH:
791 			evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
792 			evt_data_size = sizeof(fast_evt_data->un.
793 				queue_depth_evt);
794 			break;
795 		default:
796 			lpfc_free_fast_evt(phba, fast_evt_data);
797 			return;
798 		}
799 	} else {
800 		lpfc_free_fast_evt(phba, fast_evt_data);
801 		return;
802 	}
803 
804 	if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
805 		fc_host_post_vendor_event(shost,
806 			fc_get_event_number(),
807 			evt_data_size,
808 			evt_data,
809 			LPFC_NL_VENDOR_ID);
810 
811 	lpfc_free_fast_evt(phba, fast_evt_data);
812 	return;
813 }
814 
815 static void
lpfc_work_list_done(struct lpfc_hba * phba)816 lpfc_work_list_done(struct lpfc_hba *phba)
817 {
818 	struct lpfc_work_evt  *evtp = NULL;
819 	struct lpfc_nodelist  *ndlp;
820 	int free_evt;
821 	int fcf_inuse;
822 	uint32_t nlp_did;
823 	bool hba_pci_err;
824 
825 	spin_lock_irq(&phba->hbalock);
826 	while (!list_empty(&phba->work_list)) {
827 		list_remove_head((&phba->work_list), evtp, typeof(*evtp),
828 				 evt_listp);
829 		spin_unlock_irq(&phba->hbalock);
830 		hba_pci_err = test_bit(HBA_PCI_ERR, &phba->bit_flags);
831 		free_evt = 1;
832 		switch (evtp->evt) {
833 		case LPFC_EVT_ELS_RETRY:
834 			ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
835 			if (!hba_pci_err) {
836 				lpfc_els_retry_delay_handler(ndlp);
837 				free_evt = 0; /* evt is part of ndlp */
838 			}
839 			/* decrement the node reference count held
840 			 * for this queued work
841 			 */
842 			lpfc_nlp_put(ndlp);
843 			break;
844 		case LPFC_EVT_DEV_LOSS:
845 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
846 			fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
847 			free_evt = 0;
848 			/* decrement the node reference count held for
849 			 * this queued work
850 			 */
851 			nlp_did = ndlp->nlp_DID;
852 			lpfc_nlp_put(ndlp);
853 			if (phba->sli_rev == LPFC_SLI_REV4)
854 				lpfc_sli4_post_dev_loss_tmo_handler(phba,
855 								    fcf_inuse,
856 								    nlp_did);
857 			break;
858 		case LPFC_EVT_RECOVER_PORT:
859 			ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
860 			if (!hba_pci_err) {
861 				lpfc_sli_abts_recover_port(ndlp->vport, ndlp);
862 				free_evt = 0;
863 			}
864 			/* decrement the node reference count held for
865 			 * this queued work
866 			 */
867 			lpfc_nlp_put(ndlp);
868 			break;
869 		case LPFC_EVT_ONLINE:
870 			if (phba->link_state < LPFC_LINK_DOWN)
871 				*(int *) (evtp->evt_arg1) = lpfc_online(phba);
872 			else
873 				*(int *) (evtp->evt_arg1) = 0;
874 			complete((struct completion *)(evtp->evt_arg2));
875 			break;
876 		case LPFC_EVT_OFFLINE_PREP:
877 			if (phba->link_state >= LPFC_LINK_DOWN)
878 				lpfc_offline_prep(phba, LPFC_MBX_WAIT);
879 			*(int *)(evtp->evt_arg1) = 0;
880 			complete((struct completion *)(evtp->evt_arg2));
881 			break;
882 		case LPFC_EVT_OFFLINE:
883 			lpfc_offline(phba);
884 			lpfc_sli_brdrestart(phba);
885 			*(int *)(evtp->evt_arg1) =
886 				lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
887 			lpfc_unblock_mgmt_io(phba);
888 			complete((struct completion *)(evtp->evt_arg2));
889 			break;
890 		case LPFC_EVT_WARM_START:
891 			lpfc_offline(phba);
892 			lpfc_reset_barrier(phba);
893 			lpfc_sli_brdreset(phba);
894 			lpfc_hba_down_post(phba);
895 			*(int *)(evtp->evt_arg1) =
896 				lpfc_sli_brdready(phba, HS_MBRDY);
897 			lpfc_unblock_mgmt_io(phba);
898 			complete((struct completion *)(evtp->evt_arg2));
899 			break;
900 		case LPFC_EVT_KILL:
901 			lpfc_offline(phba);
902 			*(int *)(evtp->evt_arg1)
903 				= (phba->pport->stopped)
904 				        ? 0 : lpfc_sli_brdkill(phba);
905 			lpfc_unblock_mgmt_io(phba);
906 			complete((struct completion *)(evtp->evt_arg2));
907 			break;
908 		case LPFC_EVT_FASTPATH_MGMT_EVT:
909 			lpfc_send_fastpath_evt(phba, evtp);
910 			free_evt = 0;
911 			break;
912 		case LPFC_EVT_RESET_HBA:
913 			if (!(phba->pport->load_flag & FC_UNLOADING))
914 				lpfc_reset_hba(phba);
915 			break;
916 		}
917 		if (free_evt)
918 			kfree(evtp);
919 		spin_lock_irq(&phba->hbalock);
920 	}
921 	spin_unlock_irq(&phba->hbalock);
922 
923 }
924 
925 static void
lpfc_work_done(struct lpfc_hba * phba)926 lpfc_work_done(struct lpfc_hba *phba)
927 {
928 	struct lpfc_sli_ring *pring;
929 	uint32_t ha_copy, status, control, work_port_events;
930 	struct lpfc_vport **vports;
931 	struct lpfc_vport *vport;
932 	int i;
933 	bool hba_pci_err;
934 
935 	hba_pci_err = test_bit(HBA_PCI_ERR, &phba->bit_flags);
936 	spin_lock_irq(&phba->hbalock);
937 	ha_copy = phba->work_ha;
938 	phba->work_ha = 0;
939 	spin_unlock_irq(&phba->hbalock);
940 	if (hba_pci_err)
941 		ha_copy = 0;
942 
943 	/* First, try to post the next mailbox command to SLI4 device */
944 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC && !hba_pci_err)
945 		lpfc_sli4_post_async_mbox(phba);
946 
947 	if (ha_copy & HA_ERATT) {
948 		/* Handle the error attention event */
949 		lpfc_handle_eratt(phba);
950 
951 		if (phba->fw_dump_cmpl) {
952 			complete(phba->fw_dump_cmpl);
953 			phba->fw_dump_cmpl = NULL;
954 		}
955 	}
956 
957 	if (ha_copy & HA_MBATT)
958 		lpfc_sli_handle_mb_event(phba);
959 
960 	if (ha_copy & HA_LATT)
961 		lpfc_handle_latt(phba);
962 
963 	/* Handle VMID Events */
964 	if (lpfc_is_vmid_enabled(phba) && !hba_pci_err) {
965 		if (phba->pport->work_port_events &
966 		    WORKER_CHECK_VMID_ISSUE_QFPA) {
967 			lpfc_check_vmid_qfpa_issue(phba);
968 			phba->pport->work_port_events &=
969 				~WORKER_CHECK_VMID_ISSUE_QFPA;
970 		}
971 		if (phba->pport->work_port_events &
972 		    WORKER_CHECK_INACTIVE_VMID) {
973 			lpfc_check_inactive_vmid(phba);
974 			phba->pport->work_port_events &=
975 			    ~WORKER_CHECK_INACTIVE_VMID;
976 		}
977 	}
978 
979 	/* Process SLI4 events */
980 	if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
981 		if (phba->hba_flag & HBA_RRQ_ACTIVE)
982 			lpfc_handle_rrq_active(phba);
983 		if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
984 			lpfc_sli4_els_xri_abort_event_proc(phba);
985 		if (phba->hba_flag & ASYNC_EVENT)
986 			lpfc_sli4_async_event_proc(phba);
987 		if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
988 			spin_lock_irq(&phba->hbalock);
989 			phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
990 			spin_unlock_irq(&phba->hbalock);
991 			lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
992 		}
993 		if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
994 			lpfc_sli4_fcf_redisc_event_proc(phba);
995 	}
996 
997 	vports = lpfc_create_vport_work_array(phba);
998 	if (vports != NULL)
999 		for (i = 0; i <= phba->max_vports; i++) {
1000 			/*
1001 			 * We could have no vports in array if unloading, so if
1002 			 * this happens then just use the pport
1003 			 */
1004 			if (vports[i] == NULL && i == 0)
1005 				vport = phba->pport;
1006 			else
1007 				vport = vports[i];
1008 			if (vport == NULL)
1009 				break;
1010 			spin_lock_irq(&vport->work_port_lock);
1011 			work_port_events = vport->work_port_events;
1012 			vport->work_port_events &= ~work_port_events;
1013 			spin_unlock_irq(&vport->work_port_lock);
1014 			if (hba_pci_err)
1015 				continue;
1016 			if (work_port_events & WORKER_DISC_TMO)
1017 				lpfc_disc_timeout_handler(vport);
1018 			if (work_port_events & WORKER_ELS_TMO)
1019 				lpfc_els_timeout_handler(vport);
1020 			if (work_port_events & WORKER_HB_TMO)
1021 				lpfc_hb_timeout_handler(phba);
1022 			if (work_port_events & WORKER_MBOX_TMO)
1023 				lpfc_mbox_timeout_handler(phba);
1024 			if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
1025 				lpfc_unblock_fabric_iocbs(phba);
1026 			if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
1027 				lpfc_ramp_down_queue_handler(phba);
1028 			if (work_port_events & WORKER_DELAYED_DISC_TMO)
1029 				lpfc_delayed_disc_timeout_handler(vport);
1030 		}
1031 	lpfc_destroy_vport_work_array(phba, vports);
1032 
1033 	pring = lpfc_phba_elsring(phba);
1034 	status = (ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
1035 	status >>= (4*LPFC_ELS_RING);
1036 	if (pring && (status & HA_RXMASK ||
1037 		      pring->flag & LPFC_DEFERRED_RING_EVENT ||
1038 		      phba->hba_flag & HBA_SP_QUEUE_EVT)) {
1039 		if (pring->flag & LPFC_STOP_IOCB_EVENT) {
1040 			pring->flag |= LPFC_DEFERRED_RING_EVENT;
1041 			/* Preserve legacy behavior. */
1042 			if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
1043 				set_bit(LPFC_DATA_READY, &phba->data_flags);
1044 		} else {
1045 			/* Driver could have abort request completed in queue
1046 			 * when link goes down.  Allow for this transition.
1047 			 */
1048 			if (phba->link_state >= LPFC_LINK_DOWN ||
1049 			    phba->link_flag & LS_MDS_LOOPBACK) {
1050 				pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
1051 				lpfc_sli_handle_slow_ring_event(phba, pring,
1052 								(status &
1053 								HA_RXMASK));
1054 			}
1055 		}
1056 		if (phba->sli_rev == LPFC_SLI_REV4)
1057 			lpfc_drain_txq(phba);
1058 		/*
1059 		 * Turn on Ring interrupts
1060 		 */
1061 		if (phba->sli_rev <= LPFC_SLI_REV3) {
1062 			spin_lock_irq(&phba->hbalock);
1063 			control = readl(phba->HCregaddr);
1064 			if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
1065 				lpfc_debugfs_slow_ring_trc(phba,
1066 					"WRK Enable ring: cntl:x%x hacopy:x%x",
1067 					control, ha_copy, 0);
1068 
1069 				control |= (HC_R0INT_ENA << LPFC_ELS_RING);
1070 				writel(control, phba->HCregaddr);
1071 				readl(phba->HCregaddr); /* flush */
1072 			} else {
1073 				lpfc_debugfs_slow_ring_trc(phba,
1074 					"WRK Ring ok:     cntl:x%x hacopy:x%x",
1075 					control, ha_copy, 0);
1076 			}
1077 			spin_unlock_irq(&phba->hbalock);
1078 		}
1079 	}
1080 	lpfc_work_list_done(phba);
1081 }
1082 
1083 int
lpfc_do_work(void * p)1084 lpfc_do_work(void *p)
1085 {
1086 	struct lpfc_hba *phba = p;
1087 	int rc;
1088 
1089 	set_user_nice(current, MIN_NICE);
1090 	current->flags |= PF_NOFREEZE;
1091 	phba->data_flags = 0;
1092 
1093 	while (!kthread_should_stop()) {
1094 		/* wait and check worker queue activities */
1095 		rc = wait_event_interruptible(phba->work_waitq,
1096 					(test_and_clear_bit(LPFC_DATA_READY,
1097 							    &phba->data_flags)
1098 					 || kthread_should_stop()));
1099 		/* Signal wakeup shall terminate the worker thread */
1100 		if (rc) {
1101 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1102 					"0433 Wakeup on signal: rc=x%x\n", rc);
1103 			break;
1104 		}
1105 
1106 		/* Attend pending lpfc data processing */
1107 		lpfc_work_done(phba);
1108 	}
1109 	phba->worker_thread = NULL;
1110 	lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
1111 			"0432 Worker thread stopped.\n");
1112 	return 0;
1113 }
1114 
1115 /*
1116  * This is only called to handle FC worker events. Since this a rare
1117  * occurrence, we allocate a struct lpfc_work_evt structure here instead of
1118  * embedding it in the IOCB.
1119  */
1120 int
lpfc_workq_post_event(struct lpfc_hba * phba,void * arg1,void * arg2,uint32_t evt)1121 lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
1122 		      uint32_t evt)
1123 {
1124 	struct lpfc_work_evt  *evtp;
1125 	unsigned long flags;
1126 
1127 	/*
1128 	 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
1129 	 * be queued to worker thread for processing
1130 	 */
1131 	evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
1132 	if (!evtp)
1133 		return 0;
1134 
1135 	evtp->evt_arg1  = arg1;
1136 	evtp->evt_arg2  = arg2;
1137 	evtp->evt       = evt;
1138 
1139 	spin_lock_irqsave(&phba->hbalock, flags);
1140 	list_add_tail(&evtp->evt_listp, &phba->work_list);
1141 	spin_unlock_irqrestore(&phba->hbalock, flags);
1142 
1143 	lpfc_worker_wake_up(phba);
1144 
1145 	return 1;
1146 }
1147 
1148 void
lpfc_cleanup_rpis(struct lpfc_vport * vport,int remove)1149 lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
1150 {
1151 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1152 	struct lpfc_hba  *phba = vport->phba;
1153 	struct lpfc_nodelist *ndlp, *next_ndlp;
1154 
1155 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
1156 		if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
1157 		    ((vport->port_type == LPFC_NPIV_PORT) &&
1158 		     ((ndlp->nlp_DID == NameServer_DID) ||
1159 		      (ndlp->nlp_DID == FDMI_DID) ||
1160 		      (ndlp->nlp_DID == Fabric_Cntl_DID))))
1161 			lpfc_unreg_rpi(vport, ndlp);
1162 
1163 		/* Leave Fabric nodes alone on link down */
1164 		if ((phba->sli_rev < LPFC_SLI_REV4) &&
1165 		    (!remove && ndlp->nlp_type & NLP_FABRIC))
1166 			continue;
1167 
1168 		/* Notify transport of connectivity loss to trigger cleanup. */
1169 		if (phba->nvmet_support &&
1170 		    ndlp->nlp_state == NLP_STE_UNMAPPED_NODE)
1171 			lpfc_nvmet_invalidate_host(phba, ndlp);
1172 
1173 		lpfc_disc_state_machine(vport, ndlp, NULL,
1174 					remove
1175 					? NLP_EVT_DEVICE_RM
1176 					: NLP_EVT_DEVICE_RECOVERY);
1177 	}
1178 	if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
1179 		if (phba->sli_rev == LPFC_SLI_REV4)
1180 			lpfc_sli4_unreg_all_rpis(vport);
1181 		lpfc_mbx_unreg_vpi(vport);
1182 		spin_lock_irq(shost->host_lock);
1183 		vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
1184 		spin_unlock_irq(shost->host_lock);
1185 	}
1186 }
1187 
1188 void
lpfc_port_link_failure(struct lpfc_vport * vport)1189 lpfc_port_link_failure(struct lpfc_vport *vport)
1190 {
1191 	lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
1192 
1193 	/* Cleanup any outstanding received buffers */
1194 	lpfc_cleanup_rcv_buffers(vport);
1195 
1196 	/* Cleanup any outstanding RSCN activity */
1197 	lpfc_els_flush_rscn(vport);
1198 
1199 	/* Cleanup any outstanding ELS commands */
1200 	lpfc_els_flush_cmd(vport);
1201 
1202 	lpfc_cleanup_rpis(vport, 0);
1203 
1204 	/* Turn off discovery timer if its running */
1205 	lpfc_can_disctmo(vport);
1206 }
1207 
1208 void
lpfc_linkdown_port(struct lpfc_vport * vport)1209 lpfc_linkdown_port(struct lpfc_vport *vport)
1210 {
1211 	struct lpfc_hba *phba = vport->phba;
1212 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1213 
1214 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1215 		fc_host_post_event(shost, fc_get_event_number(),
1216 				   FCH_EVT_LINKDOWN, 0);
1217 
1218 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1219 		"Link Down:       state:x%x rtry:x%x flg:x%x",
1220 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
1221 
1222 	lpfc_port_link_failure(vport);
1223 
1224 	/* Stop delayed Nport discovery */
1225 	spin_lock_irq(shost->host_lock);
1226 	vport->fc_flag &= ~FC_DISC_DELAYED;
1227 	spin_unlock_irq(shost->host_lock);
1228 	del_timer_sync(&vport->delayed_disc_tmo);
1229 
1230 	if (phba->sli_rev == LPFC_SLI_REV4 &&
1231 	    vport->port_type == LPFC_PHYSICAL_PORT &&
1232 	    phba->sli4_hba.fawwpn_flag & LPFC_FAWWPN_CONFIG) {
1233 		/* Assume success on link up */
1234 		phba->sli4_hba.fawwpn_flag |= LPFC_FAWWPN_FABRIC;
1235 	}
1236 }
1237 
1238 int
lpfc_linkdown(struct lpfc_hba * phba)1239 lpfc_linkdown(struct lpfc_hba *phba)
1240 {
1241 	struct lpfc_vport *vport = phba->pport;
1242 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1243 	struct lpfc_vport **vports;
1244 	LPFC_MBOXQ_t          *mb;
1245 	int i;
1246 	int offline;
1247 
1248 	if (phba->link_state == LPFC_LINK_DOWN)
1249 		return 0;
1250 
1251 	/* Block all SCSI stack I/Os */
1252 	lpfc_scsi_dev_block(phba);
1253 	offline = pci_channel_offline(phba->pcidev);
1254 
1255 	phba->defer_flogi_acc_flag = false;
1256 
1257 	/* Clear external loopback plug detected flag */
1258 	phba->link_flag &= ~LS_EXTERNAL_LOOPBACK;
1259 
1260 	spin_lock_irq(&phba->hbalock);
1261 	phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1262 	spin_unlock_irq(&phba->hbalock);
1263 	if (phba->link_state > LPFC_LINK_DOWN) {
1264 		phba->link_state = LPFC_LINK_DOWN;
1265 		if (phba->sli4_hba.conf_trunk) {
1266 			phba->trunk_link.link0.state = 0;
1267 			phba->trunk_link.link1.state = 0;
1268 			phba->trunk_link.link2.state = 0;
1269 			phba->trunk_link.link3.state = 0;
1270 			phba->trunk_link.phy_lnk_speed =
1271 						LPFC_LINK_SPEED_UNKNOWN;
1272 			phba->sli4_hba.link_state.logical_speed =
1273 						LPFC_LINK_SPEED_UNKNOWN;
1274 		}
1275 		spin_lock_irq(shost->host_lock);
1276 		phba->pport->fc_flag &= ~FC_LBIT;
1277 		spin_unlock_irq(shost->host_lock);
1278 	}
1279 	vports = lpfc_create_vport_work_array(phba);
1280 	if (vports != NULL) {
1281 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
1282 			/* Issue a LINK DOWN event to all nodes */
1283 			lpfc_linkdown_port(vports[i]);
1284 
1285 			vports[i]->fc_myDID = 0;
1286 
1287 			if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
1288 			    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
1289 				if (phba->nvmet_support)
1290 					lpfc_nvmet_update_targetport(phba);
1291 				else
1292 					lpfc_nvme_update_localport(vports[i]);
1293 			}
1294 		}
1295 	}
1296 	lpfc_destroy_vport_work_array(phba, vports);
1297 
1298 	/* Clean up any SLI3 firmware default rpi's */
1299 	if (phba->sli_rev > LPFC_SLI_REV3 || offline)
1300 		goto skip_unreg_did;
1301 
1302 	mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1303 	if (mb) {
1304 		lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
1305 		mb->vport = vport;
1306 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1307 		if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1308 		    == MBX_NOT_FINISHED) {
1309 			mempool_free(mb, phba->mbox_mem_pool);
1310 		}
1311 	}
1312 
1313  skip_unreg_did:
1314 	/* Setup myDID for link up if we are in pt2pt mode */
1315 	if (phba->pport->fc_flag & FC_PT2PT) {
1316 		mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1317 		if (mb) {
1318 			lpfc_config_link(phba, mb);
1319 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1320 			mb->vport = vport;
1321 			if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
1322 			    == MBX_NOT_FINISHED) {
1323 				mempool_free(mb, phba->mbox_mem_pool);
1324 			}
1325 		}
1326 		spin_lock_irq(shost->host_lock);
1327 		phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
1328 		phba->pport->rcv_flogi_cnt = 0;
1329 		spin_unlock_irq(shost->host_lock);
1330 	}
1331 	return 0;
1332 }
1333 
1334 static void
lpfc_linkup_cleanup_nodes(struct lpfc_vport * vport)1335 lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
1336 {
1337 	struct lpfc_nodelist *ndlp;
1338 
1339 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1340 		ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
1341 
1342 		if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
1343 			continue;
1344 		if (ndlp->nlp_type & NLP_FABRIC) {
1345 			/* On Linkup its safe to clean up the ndlp
1346 			 * from Fabric connections.
1347 			 */
1348 			if (ndlp->nlp_DID != Fabric_DID)
1349 				lpfc_unreg_rpi(vport, ndlp);
1350 			lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
1351 		} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
1352 			/* Fail outstanding IO now since device is
1353 			 * marked for PLOGI.
1354 			 */
1355 			lpfc_unreg_rpi(vport, ndlp);
1356 		}
1357 	}
1358 }
1359 
1360 static void
lpfc_linkup_port(struct lpfc_vport * vport)1361 lpfc_linkup_port(struct lpfc_vport *vport)
1362 {
1363 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1364 	struct lpfc_hba  *phba = vport->phba;
1365 
1366 	if ((vport->load_flag & FC_UNLOADING) != 0)
1367 		return;
1368 
1369 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1370 		"Link Up:         top:x%x speed:x%x flg:x%x",
1371 		phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1372 
1373 	/* If NPIV is not enabled, only bring the physical port up */
1374 	if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1375 		(vport != phba->pport))
1376 		return;
1377 
1378 	if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1379 		fc_host_post_event(shost, fc_get_event_number(),
1380 				   FCH_EVT_LINKUP, 0);
1381 
1382 	spin_lock_irq(shost->host_lock);
1383 	if (phba->defer_flogi_acc_flag)
1384 		vport->fc_flag &= ~(FC_ABORT_DISCOVERY | FC_RSCN_MODE |
1385 				    FC_NLP_MORE | FC_RSCN_DISCOVERY);
1386 	else
1387 		vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI |
1388 				    FC_ABORT_DISCOVERY | FC_RSCN_MODE |
1389 				    FC_NLP_MORE | FC_RSCN_DISCOVERY);
1390 	vport->fc_flag |= FC_NDISC_ACTIVE;
1391 	vport->fc_ns_retry = 0;
1392 	spin_unlock_irq(shost->host_lock);
1393 	lpfc_setup_fdmi_mask(vport);
1394 
1395 	lpfc_linkup_cleanup_nodes(vport);
1396 }
1397 
1398 static int
lpfc_linkup(struct lpfc_hba * phba)1399 lpfc_linkup(struct lpfc_hba *phba)
1400 {
1401 	struct lpfc_vport **vports;
1402 	int i;
1403 	struct Scsi_Host  *shost = lpfc_shost_from_vport(phba->pport);
1404 
1405 	phba->link_state = LPFC_LINK_UP;
1406 
1407 	/* Unblock fabric iocbs if they are blocked */
1408 	clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1409 	del_timer_sync(&phba->fabric_block_timer);
1410 
1411 	vports = lpfc_create_vport_work_array(phba);
1412 	if (vports != NULL)
1413 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1414 			lpfc_linkup_port(vports[i]);
1415 	lpfc_destroy_vport_work_array(phba, vports);
1416 
1417 	/* Clear the pport flogi counter in case the link down was
1418 	 * absorbed without an ACQE. No lock here - in worker thread
1419 	 * and discovery is synchronized.
1420 	 */
1421 	spin_lock_irq(shost->host_lock);
1422 	phba->pport->rcv_flogi_cnt = 0;
1423 	spin_unlock_irq(shost->host_lock);
1424 
1425 	/* reinitialize initial HBA flag */
1426 	phba->hba_flag &= ~(HBA_FLOGI_ISSUED | HBA_RHBA_CMPL);
1427 
1428 	return 0;
1429 }
1430 
1431 /*
1432  * This routine handles processing a CLEAR_LA mailbox
1433  * command upon completion. It is setup in the LPFC_MBOXQ
1434  * as the completion routine when the command is
1435  * handed off to the SLI layer. SLI3 only.
1436  */
1437 static void
lpfc_mbx_cmpl_clear_la(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)1438 lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1439 {
1440 	struct lpfc_vport *vport = pmb->vport;
1441 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
1442 	struct lpfc_sli   *psli = &phba->sli;
1443 	MAILBOX_t *mb = &pmb->u.mb;
1444 	uint32_t control;
1445 
1446 	/* Since we don't do discovery right now, turn these off here */
1447 	psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1448 	psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1449 
1450 	/* Check for error */
1451 	if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
1452 		/* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
1453 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1454 				 "0320 CLEAR_LA mbxStatus error x%x hba "
1455 				 "state x%x\n",
1456 				 mb->mbxStatus, vport->port_state);
1457 		phba->link_state = LPFC_HBA_ERROR;
1458 		goto out;
1459 	}
1460 
1461 	if (vport->port_type == LPFC_PHYSICAL_PORT)
1462 		phba->link_state = LPFC_HBA_READY;
1463 
1464 	spin_lock_irq(&phba->hbalock);
1465 	psli->sli_flag |= LPFC_PROCESS_LA;
1466 	control = readl(phba->HCregaddr);
1467 	control |= HC_LAINT_ENA;
1468 	writel(control, phba->HCregaddr);
1469 	readl(phba->HCregaddr); /* flush */
1470 	spin_unlock_irq(&phba->hbalock);
1471 	mempool_free(pmb, phba->mbox_mem_pool);
1472 	return;
1473 
1474 out:
1475 	/* Device Discovery completes */
1476 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1477 			 "0225 Device Discovery completes\n");
1478 	mempool_free(pmb, phba->mbox_mem_pool);
1479 
1480 	spin_lock_irq(shost->host_lock);
1481 	vport->fc_flag &= ~FC_ABORT_DISCOVERY;
1482 	spin_unlock_irq(shost->host_lock);
1483 
1484 	lpfc_can_disctmo(vport);
1485 
1486 	/* turn on Link Attention interrupts */
1487 
1488 	spin_lock_irq(&phba->hbalock);
1489 	psli->sli_flag |= LPFC_PROCESS_LA;
1490 	control = readl(phba->HCregaddr);
1491 	control |= HC_LAINT_ENA;
1492 	writel(control, phba->HCregaddr);
1493 	readl(phba->HCregaddr); /* flush */
1494 	spin_unlock_irq(&phba->hbalock);
1495 
1496 	return;
1497 }
1498 
1499 void
lpfc_mbx_cmpl_local_config_link(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)1500 lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1501 {
1502 	struct lpfc_vport *vport = pmb->vport;
1503 	LPFC_MBOXQ_t *sparam_mb;
1504 	u16 status = pmb->u.mb.mbxStatus;
1505 	int rc;
1506 
1507 	mempool_free(pmb, phba->mbox_mem_pool);
1508 
1509 	if (status)
1510 		goto out;
1511 
1512 	/* don't perform discovery for SLI4 loopback diagnostic test */
1513 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
1514 	    !(phba->hba_flag & HBA_FCOE_MODE) &&
1515 	    (phba->link_flag & LS_LOOPBACK_MODE))
1516 		return;
1517 
1518 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
1519 	    vport->fc_flag & FC_PUBLIC_LOOP &&
1520 	    !(vport->fc_flag & FC_LBIT)) {
1521 			/* Need to wait for FAN - use discovery timer
1522 			 * for timeout.  port_state is identically
1523 			 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1524 			 */
1525 			lpfc_set_disctmo(vport);
1526 			return;
1527 	}
1528 
1529 	/* Start discovery by sending a FLOGI. port_state is identically
1530 	 * LPFC_FLOGI while waiting for FLOGI cmpl.
1531 	 */
1532 	if (vport->port_state != LPFC_FLOGI) {
1533 		/* Issue MBX_READ_SPARAM to update CSPs before FLOGI if
1534 		 * bb-credit recovery is in place.
1535 		 */
1536 		if (phba->bbcredit_support && phba->cfg_enable_bbcr &&
1537 		    !(phba->link_flag & LS_LOOPBACK_MODE)) {
1538 			sparam_mb = mempool_alloc(phba->mbox_mem_pool,
1539 						  GFP_KERNEL);
1540 			if (!sparam_mb)
1541 				goto sparam_out;
1542 
1543 			rc = lpfc_read_sparam(phba, sparam_mb, 0);
1544 			if (rc) {
1545 				mempool_free(sparam_mb, phba->mbox_mem_pool);
1546 				goto sparam_out;
1547 			}
1548 			sparam_mb->vport = vport;
1549 			sparam_mb->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
1550 			rc = lpfc_sli_issue_mbox(phba, sparam_mb, MBX_NOWAIT);
1551 			if (rc == MBX_NOT_FINISHED) {
1552 				lpfc_mbox_rsrc_cleanup(phba, sparam_mb,
1553 						       MBOX_THD_UNLOCKED);
1554 				goto sparam_out;
1555 			}
1556 
1557 			phba->hba_flag |= HBA_DEFER_FLOGI;
1558 		}  else {
1559 			lpfc_initial_flogi(vport);
1560 		}
1561 	} else {
1562 		if (vport->fc_flag & FC_PT2PT)
1563 			lpfc_disc_start(vport);
1564 	}
1565 	return;
1566 
1567 out:
1568 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1569 			 "0306 CONFIG_LINK mbxStatus error x%x HBA state x%x\n",
1570 			 status, vport->port_state);
1571 
1572 sparam_out:
1573 	lpfc_linkdown(phba);
1574 
1575 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1576 			 "0200 CONFIG_LINK bad hba state x%x\n",
1577 			 vport->port_state);
1578 
1579 	lpfc_issue_clear_la(phba, vport);
1580 	return;
1581 }
1582 
1583 /**
1584  * lpfc_sli4_clear_fcf_rr_bmask
1585  * @phba: pointer to the struct lpfc_hba for this port.
1586  * This fucnction resets the round robin bit mask and clears the
1587  * fcf priority list. The list deletions are done while holding the
1588  * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1589  * from the lpfc_fcf_pri record.
1590  **/
1591 void
lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba * phba)1592 lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1593 {
1594 	struct lpfc_fcf_pri *fcf_pri;
1595 	struct lpfc_fcf_pri *next_fcf_pri;
1596 	memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1597 	spin_lock_irq(&phba->hbalock);
1598 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1599 				&phba->fcf.fcf_pri_list, list) {
1600 		list_del_init(&fcf_pri->list);
1601 		fcf_pri->fcf_rec.flag = 0;
1602 	}
1603 	spin_unlock_irq(&phba->hbalock);
1604 }
1605 static void
lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)1606 lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1607 {
1608 	struct lpfc_vport *vport = mboxq->vport;
1609 
1610 	if (mboxq->u.mb.mbxStatus) {
1611 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1612 				 "2017 REG_FCFI mbxStatus error x%x "
1613 				 "HBA state x%x\n", mboxq->u.mb.mbxStatus,
1614 				 vport->port_state);
1615 		goto fail_out;
1616 	}
1617 
1618 	/* Start FCoE discovery by sending a FLOGI. */
1619 	phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1620 	/* Set the FCFI registered flag */
1621 	spin_lock_irq(&phba->hbalock);
1622 	phba->fcf.fcf_flag |= FCF_REGISTERED;
1623 	spin_unlock_irq(&phba->hbalock);
1624 
1625 	/* If there is a pending FCoE event, restart FCF table scan. */
1626 	if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1627 		lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
1628 		goto fail_out;
1629 
1630 	/* Mark successful completion of FCF table scan */
1631 	spin_lock_irq(&phba->hbalock);
1632 	phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1633 	phba->hba_flag &= ~FCF_TS_INPROG;
1634 	if (vport->port_state != LPFC_FLOGI) {
1635 		phba->hba_flag |= FCF_RR_INPROG;
1636 		spin_unlock_irq(&phba->hbalock);
1637 		lpfc_issue_init_vfi(vport);
1638 		goto out;
1639 	}
1640 	spin_unlock_irq(&phba->hbalock);
1641 	goto out;
1642 
1643 fail_out:
1644 	spin_lock_irq(&phba->hbalock);
1645 	phba->hba_flag &= ~FCF_RR_INPROG;
1646 	spin_unlock_irq(&phba->hbalock);
1647 out:
1648 	mempool_free(mboxq, phba->mbox_mem_pool);
1649 }
1650 
1651 /**
1652  * lpfc_fab_name_match - Check if the fcf fabric name match.
1653  * @fab_name: pointer to fabric name.
1654  * @new_fcf_record: pointer to fcf record.
1655  *
1656  * This routine compare the fcf record's fabric name with provided
1657  * fabric name. If the fabric name are identical this function
1658  * returns 1 else return 0.
1659  **/
1660 static uint32_t
lpfc_fab_name_match(uint8_t * fab_name,struct fcf_record * new_fcf_record)1661 lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1662 {
1663 	if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
1664 		return 0;
1665 	if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1666 		return 0;
1667 	if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1668 		return 0;
1669 	if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1670 		return 0;
1671 	if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1672 		return 0;
1673 	if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1674 		return 0;
1675 	if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1676 		return 0;
1677 	if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1678 		return 0;
1679 	return 1;
1680 }
1681 
1682 /**
1683  * lpfc_sw_name_match - Check if the fcf switch name match.
1684  * @sw_name: pointer to switch name.
1685  * @new_fcf_record: pointer to fcf record.
1686  *
1687  * This routine compare the fcf record's switch name with provided
1688  * switch name. If the switch name are identical this function
1689  * returns 1 else return 0.
1690  **/
1691 static uint32_t
lpfc_sw_name_match(uint8_t * sw_name,struct fcf_record * new_fcf_record)1692 lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1693 {
1694 	if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
1695 		return 0;
1696 	if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1697 		return 0;
1698 	if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1699 		return 0;
1700 	if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1701 		return 0;
1702 	if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1703 		return 0;
1704 	if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1705 		return 0;
1706 	if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1707 		return 0;
1708 	if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1709 		return 0;
1710 	return 1;
1711 }
1712 
1713 /**
1714  * lpfc_mac_addr_match - Check if the fcf mac address match.
1715  * @mac_addr: pointer to mac address.
1716  * @new_fcf_record: pointer to fcf record.
1717  *
1718  * This routine compare the fcf record's mac address with HBA's
1719  * FCF mac address. If the mac addresses are identical this function
1720  * returns 1 else return 0.
1721  **/
1722 static uint32_t
lpfc_mac_addr_match(uint8_t * mac_addr,struct fcf_record * new_fcf_record)1723 lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
1724 {
1725 	if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
1726 		return 0;
1727 	if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1728 		return 0;
1729 	if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1730 		return 0;
1731 	if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1732 		return 0;
1733 	if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1734 		return 0;
1735 	if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1736 		return 0;
1737 	return 1;
1738 }
1739 
1740 static bool
lpfc_vlan_id_match(uint16_t curr_vlan_id,uint16_t new_vlan_id)1741 lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1742 {
1743 	return (curr_vlan_id == new_vlan_id);
1744 }
1745 
1746 /**
1747  * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1748  * @phba: pointer to lpfc hba data structure.
1749  * @fcf_index: Index for the lpfc_fcf_record.
1750  * @new_fcf_record: pointer to hba fcf record.
1751  *
1752  * This routine updates the driver FCF priority record from the new HBA FCF
1753  * record. The hbalock is asserted held in the code path calling this
1754  * routine.
1755  **/
1756 static void
__lpfc_update_fcf_record_pri(struct lpfc_hba * phba,uint16_t fcf_index,struct fcf_record * new_fcf_record)1757 __lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1758 				 struct fcf_record *new_fcf_record
1759 				 )
1760 {
1761 	struct lpfc_fcf_pri *fcf_pri;
1762 
1763 	fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1764 	fcf_pri->fcf_rec.fcf_index = fcf_index;
1765 	/* FCF record priority */
1766 	fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1767 
1768 }
1769 
1770 /**
1771  * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
1772  * @fcf_rec: pointer to driver fcf record.
1773  * @new_fcf_record: pointer to fcf record.
1774  *
1775  * This routine copies the FCF information from the FCF
1776  * record to lpfc_hba data structure.
1777  **/
1778 static void
lpfc_copy_fcf_record(struct lpfc_fcf_rec * fcf_rec,struct fcf_record * new_fcf_record)1779 lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1780 		     struct fcf_record *new_fcf_record)
1781 {
1782 	/* Fabric name */
1783 	fcf_rec->fabric_name[0] =
1784 		bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
1785 	fcf_rec->fabric_name[1] =
1786 		bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
1787 	fcf_rec->fabric_name[2] =
1788 		bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
1789 	fcf_rec->fabric_name[3] =
1790 		bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
1791 	fcf_rec->fabric_name[4] =
1792 		bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
1793 	fcf_rec->fabric_name[5] =
1794 		bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
1795 	fcf_rec->fabric_name[6] =
1796 		bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
1797 	fcf_rec->fabric_name[7] =
1798 		bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
1799 	/* Mac address */
1800 	fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1801 	fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1802 	fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1803 	fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1804 	fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1805 	fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1806 	/* FCF record index */
1807 	fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1808 	/* FCF record priority */
1809 	fcf_rec->priority = new_fcf_record->fip_priority;
1810 	/* Switch name */
1811 	fcf_rec->switch_name[0] =
1812 		bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
1813 	fcf_rec->switch_name[1] =
1814 		bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
1815 	fcf_rec->switch_name[2] =
1816 		bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
1817 	fcf_rec->switch_name[3] =
1818 		bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
1819 	fcf_rec->switch_name[4] =
1820 		bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
1821 	fcf_rec->switch_name[5] =
1822 		bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
1823 	fcf_rec->switch_name[6] =
1824 		bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
1825 	fcf_rec->switch_name[7] =
1826 		bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
1827 }
1828 
1829 /**
1830  * __lpfc_update_fcf_record - Update driver fcf record
1831  * @phba: pointer to lpfc hba data structure.
1832  * @fcf_rec: pointer to driver fcf record.
1833  * @new_fcf_record: pointer to hba fcf record.
1834  * @addr_mode: address mode to be set to the driver fcf record.
1835  * @vlan_id: vlan tag to be set to the driver fcf record.
1836  * @flag: flag bits to be set to the driver fcf record.
1837  *
1838  * This routine updates the driver FCF record from the new HBA FCF record
1839  * together with the address mode, vlan_id, and other informations. This
1840  * routine is called with the hbalock held.
1841  **/
1842 static void
__lpfc_update_fcf_record(struct lpfc_hba * phba,struct lpfc_fcf_rec * fcf_rec,struct fcf_record * new_fcf_record,uint32_t addr_mode,uint16_t vlan_id,uint32_t flag)1843 __lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1844 		       struct fcf_record *new_fcf_record, uint32_t addr_mode,
1845 		       uint16_t vlan_id, uint32_t flag)
1846 {
1847 	lockdep_assert_held(&phba->hbalock);
1848 
1849 	/* Copy the fields from the HBA's FCF record */
1850 	lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1851 	/* Update other fields of driver FCF record */
1852 	fcf_rec->addr_mode = addr_mode;
1853 	fcf_rec->vlan_id = vlan_id;
1854 	fcf_rec->flag |= (flag | RECORD_VALID);
1855 	__lpfc_update_fcf_record_pri(phba,
1856 		bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1857 				 new_fcf_record);
1858 }
1859 
1860 /**
1861  * lpfc_register_fcf - Register the FCF with hba.
1862  * @phba: pointer to lpfc hba data structure.
1863  *
1864  * This routine issues a register fcfi mailbox command to register
1865  * the fcf with HBA.
1866  **/
1867 static void
lpfc_register_fcf(struct lpfc_hba * phba)1868 lpfc_register_fcf(struct lpfc_hba *phba)
1869 {
1870 	LPFC_MBOXQ_t *fcf_mbxq;
1871 	int rc;
1872 
1873 	spin_lock_irq(&phba->hbalock);
1874 	/* If the FCF is not available do nothing. */
1875 	if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
1876 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1877 		spin_unlock_irq(&phba->hbalock);
1878 		return;
1879 	}
1880 
1881 	/* The FCF is already registered, start discovery */
1882 	if (phba->fcf.fcf_flag & FCF_REGISTERED) {
1883 		phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
1884 		phba->hba_flag &= ~FCF_TS_INPROG;
1885 		if (phba->pport->port_state != LPFC_FLOGI &&
1886 		    phba->pport->fc_flag & FC_FABRIC) {
1887 			phba->hba_flag |= FCF_RR_INPROG;
1888 			spin_unlock_irq(&phba->hbalock);
1889 			lpfc_initial_flogi(phba->pport);
1890 			return;
1891 		}
1892 		spin_unlock_irq(&phba->hbalock);
1893 		return;
1894 	}
1895 	spin_unlock_irq(&phba->hbalock);
1896 
1897 	fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1898 	if (!fcf_mbxq) {
1899 		spin_lock_irq(&phba->hbalock);
1900 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1901 		spin_unlock_irq(&phba->hbalock);
1902 		return;
1903 	}
1904 
1905 	lpfc_reg_fcfi(phba, fcf_mbxq);
1906 	fcf_mbxq->vport = phba->pport;
1907 	fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1908 	rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
1909 	if (rc == MBX_NOT_FINISHED) {
1910 		spin_lock_irq(&phba->hbalock);
1911 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
1912 		spin_unlock_irq(&phba->hbalock);
1913 		mempool_free(fcf_mbxq, phba->mbox_mem_pool);
1914 	}
1915 
1916 	return;
1917 }
1918 
1919 /**
1920  * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1921  * @phba: pointer to lpfc hba data structure.
1922  * @new_fcf_record: pointer to fcf record.
1923  * @boot_flag: Indicates if this record used by boot bios.
1924  * @addr_mode: The address mode to be used by this FCF
1925  * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
1926  *
1927  * This routine compare the fcf record with connect list obtained from the
1928  * config region to decide if this FCF can be used for SAN discovery. It returns
1929  * 1 if this record can be used for SAN discovery else return zero. If this FCF
1930  * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1931  * is used by boot bios and addr_mode will indicate the addressing mode to be
1932  * used for this FCF when the function returns.
1933  * If the FCF record need to be used with a particular vlan id, the vlan is
1934  * set in the vlan_id on return of the function. If not VLAN tagging need to
1935  * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
1936  **/
1937 static int
lpfc_match_fcf_conn_list(struct lpfc_hba * phba,struct fcf_record * new_fcf_record,uint32_t * boot_flag,uint32_t * addr_mode,uint16_t * vlan_id)1938 lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1939 			struct fcf_record *new_fcf_record,
1940 			uint32_t *boot_flag, uint32_t *addr_mode,
1941 			uint16_t *vlan_id)
1942 {
1943 	struct lpfc_fcf_conn_entry *conn_entry;
1944 	int i, j, fcf_vlan_id = 0;
1945 
1946 	/* Find the lowest VLAN id in the FCF record */
1947 	for (i = 0; i < 512; i++) {
1948 		if (new_fcf_record->vlan_bitmap[i]) {
1949 			fcf_vlan_id = i * 8;
1950 			j = 0;
1951 			while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1952 				j++;
1953 				fcf_vlan_id++;
1954 			}
1955 			break;
1956 		}
1957 	}
1958 
1959 	/* FCF not valid/available or solicitation in progress */
1960 	if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
1961 	    !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1962 	    bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
1963 		return 0;
1964 
1965 	if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
1966 		*boot_flag = 0;
1967 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1968 				new_fcf_record);
1969 		if (phba->valid_vlan)
1970 			*vlan_id = phba->vlan_id;
1971 		else
1972 			*vlan_id = LPFC_FCOE_NULL_VID;
1973 		return 1;
1974 	}
1975 
1976 	/*
1977 	 * If there are no FCF connection table entry, driver connect to all
1978 	 * FCFs.
1979 	 */
1980 	if (list_empty(&phba->fcf_conn_rec_list)) {
1981 		*boot_flag = 0;
1982 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1983 			new_fcf_record);
1984 
1985 		/*
1986 		 * When there are no FCF connect entries, use driver's default
1987 		 * addressing mode - FPMA.
1988 		 */
1989 		if (*addr_mode & LPFC_FCF_FPMA)
1990 			*addr_mode = LPFC_FCF_FPMA;
1991 
1992 		/* If FCF record report a vlan id use that vlan id */
1993 		if (fcf_vlan_id)
1994 			*vlan_id = fcf_vlan_id;
1995 		else
1996 			*vlan_id = LPFC_FCOE_NULL_VID;
1997 		return 1;
1998 	}
1999 
2000 	list_for_each_entry(conn_entry,
2001 			    &phba->fcf_conn_rec_list, list) {
2002 		if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
2003 			continue;
2004 
2005 		if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
2006 			!lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
2007 					     new_fcf_record))
2008 			continue;
2009 		if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
2010 			!lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
2011 					    new_fcf_record))
2012 			continue;
2013 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
2014 			/*
2015 			 * If the vlan bit map does not have the bit set for the
2016 			 * vlan id to be used, then it is not a match.
2017 			 */
2018 			if (!(new_fcf_record->vlan_bitmap
2019 				[conn_entry->conn_rec.vlan_tag / 8] &
2020 				(1 << (conn_entry->conn_rec.vlan_tag % 8))))
2021 				continue;
2022 		}
2023 
2024 		/*
2025 		 * If connection record does not support any addressing mode,
2026 		 * skip the FCF record.
2027 		 */
2028 		if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
2029 			& (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
2030 			continue;
2031 
2032 		/*
2033 		 * Check if the connection record specifies a required
2034 		 * addressing mode.
2035 		 */
2036 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2037 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
2038 
2039 			/*
2040 			 * If SPMA required but FCF not support this continue.
2041 			 */
2042 			if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2043 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
2044 					new_fcf_record) & LPFC_FCF_SPMA))
2045 				continue;
2046 
2047 			/*
2048 			 * If FPMA required but FCF not support this continue.
2049 			 */
2050 			if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2051 				!(bf_get(lpfc_fcf_record_mac_addr_prov,
2052 				new_fcf_record) & LPFC_FCF_FPMA))
2053 				continue;
2054 		}
2055 
2056 		/*
2057 		 * This fcf record matches filtering criteria.
2058 		 */
2059 		if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
2060 			*boot_flag = 1;
2061 		else
2062 			*boot_flag = 0;
2063 
2064 		/*
2065 		 * If user did not specify any addressing mode, or if the
2066 		 * preferred addressing mode specified by user is not supported
2067 		 * by FCF, allow fabric to pick the addressing mode.
2068 		 */
2069 		*addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
2070 				new_fcf_record);
2071 		/*
2072 		 * If the user specified a required address mode, assign that
2073 		 * address mode
2074 		 */
2075 		if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2076 			(!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
2077 			*addr_mode = (conn_entry->conn_rec.flags &
2078 				FCFCNCT_AM_SPMA) ?
2079 				LPFC_FCF_SPMA : LPFC_FCF_FPMA;
2080 		/*
2081 		 * If the user specified a preferred address mode, use the
2082 		 * addr mode only if FCF support the addr_mode.
2083 		 */
2084 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2085 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
2086 			(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2087 			(*addr_mode & LPFC_FCF_SPMA))
2088 				*addr_mode = LPFC_FCF_SPMA;
2089 		else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
2090 			(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
2091 			!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
2092 			(*addr_mode & LPFC_FCF_FPMA))
2093 				*addr_mode = LPFC_FCF_FPMA;
2094 
2095 		/* If matching connect list has a vlan id, use it */
2096 		if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
2097 			*vlan_id = conn_entry->conn_rec.vlan_tag;
2098 		/*
2099 		 * If no vlan id is specified in connect list, use the vlan id
2100 		 * in the FCF record
2101 		 */
2102 		else if (fcf_vlan_id)
2103 			*vlan_id = fcf_vlan_id;
2104 		else
2105 			*vlan_id = LPFC_FCOE_NULL_VID;
2106 
2107 		return 1;
2108 	}
2109 
2110 	return 0;
2111 }
2112 
2113 /**
2114  * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
2115  * @phba: pointer to lpfc hba data structure.
2116  * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
2117  *
2118  * This function check if there is any fcoe event pending while driver
2119  * scan FCF entries. If there is any pending event, it will restart the
2120  * FCF saning and return 1 else return 0.
2121  */
2122 int
lpfc_check_pending_fcoe_event(struct lpfc_hba * phba,uint8_t unreg_fcf)2123 lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
2124 {
2125 	/*
2126 	 * If the Link is up and no FCoE events while in the
2127 	 * FCF discovery, no need to restart FCF discovery.
2128 	 */
2129 	if ((phba->link_state  >= LPFC_LINK_UP) &&
2130 	    (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
2131 		return 0;
2132 
2133 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2134 			"2768 Pending link or FCF event during current "
2135 			"handling of the previous event: link_state:x%x, "
2136 			"evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
2137 			phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
2138 			phba->fcoe_eventtag);
2139 
2140 	spin_lock_irq(&phba->hbalock);
2141 	phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
2142 	spin_unlock_irq(&phba->hbalock);
2143 
2144 	if (phba->link_state >= LPFC_LINK_UP) {
2145 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2146 				"2780 Restart FCF table scan due to "
2147 				"pending FCF event:evt_tag_at_scan:x%x, "
2148 				"evt_tag_current:x%x\n",
2149 				phba->fcoe_eventtag_at_fcf_scan,
2150 				phba->fcoe_eventtag);
2151 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
2152 	} else {
2153 		/*
2154 		 * Do not continue FCF discovery and clear FCF_TS_INPROG
2155 		 * flag
2156 		 */
2157 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
2158 				"2833 Stop FCF discovery process due to link "
2159 				"state change (x%x)\n", phba->link_state);
2160 		spin_lock_irq(&phba->hbalock);
2161 		phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
2162 		phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
2163 		spin_unlock_irq(&phba->hbalock);
2164 	}
2165 
2166 	/* Unregister the currently registered FCF if required */
2167 	if (unreg_fcf) {
2168 		spin_lock_irq(&phba->hbalock);
2169 		phba->fcf.fcf_flag &= ~FCF_REGISTERED;
2170 		spin_unlock_irq(&phba->hbalock);
2171 		lpfc_sli4_unregister_fcf(phba);
2172 	}
2173 	return 1;
2174 }
2175 
2176 /**
2177  * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
2178  * @phba: pointer to lpfc hba data structure.
2179  * @fcf_cnt: number of eligible fcf record seen so far.
2180  *
2181  * This function makes an running random selection decision on FCF record to
2182  * use through a sequence of @fcf_cnt eligible FCF records with equal
2183  * probability. To perform integer manunipulation of random numbers with
2184  * size unit32_t, a 16-bit random number returned from get_random_u16() is
2185  * taken as the random random number generated.
2186  *
2187  * Returns true when outcome is for the newly read FCF record should be
2188  * chosen; otherwise, return false when outcome is for keeping the previously
2189  * chosen FCF record.
2190  **/
2191 static bool
lpfc_sli4_new_fcf_random_select(struct lpfc_hba * phba,uint32_t fcf_cnt)2192 lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
2193 {
2194 	uint32_t rand_num;
2195 
2196 	/* Get 16-bit uniform random number */
2197 	rand_num = get_random_u16();
2198 
2199 	/* Decision with probability 1/fcf_cnt */
2200 	if ((fcf_cnt * rand_num) < 0xFFFF)
2201 		return true;
2202 	else
2203 		return false;
2204 }
2205 
2206 /**
2207  * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
2208  * @phba: pointer to lpfc hba data structure.
2209  * @mboxq: pointer to mailbox object.
2210  * @next_fcf_index: pointer to holder of next fcf index.
2211  *
2212  * This routine parses the non-embedded fcf mailbox command by performing the
2213  * necessarily error checking, non-embedded read FCF record mailbox command
2214  * SGE parsing, and endianness swapping.
2215  *
2216  * Returns the pointer to the new FCF record in the non-embedded mailbox
2217  * command DMA memory if successfully, other NULL.
2218  */
2219 static struct fcf_record *
lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq,uint16_t * next_fcf_index)2220 lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
2221 			     uint16_t *next_fcf_index)
2222 {
2223 	void *virt_addr;
2224 	struct lpfc_mbx_sge sge;
2225 	struct lpfc_mbx_read_fcf_tbl *read_fcf;
2226 	uint32_t shdr_status, shdr_add_status, if_type;
2227 	union lpfc_sli4_cfg_shdr *shdr;
2228 	struct fcf_record *new_fcf_record;
2229 
2230 	/* Get the first SGE entry from the non-embedded DMA memory. This
2231 	 * routine only uses a single SGE.
2232 	 */
2233 	lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
2234 	if (unlikely(!mboxq->sge_array)) {
2235 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2236 				"2524 Failed to get the non-embedded SGE "
2237 				"virtual address\n");
2238 		return NULL;
2239 	}
2240 	virt_addr = mboxq->sge_array->addr[0];
2241 
2242 	shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
2243 	lpfc_sli_pcimem_bcopy(shdr, shdr,
2244 			      sizeof(union lpfc_sli4_cfg_shdr));
2245 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2246 	if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
2247 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2248 	if (shdr_status || shdr_add_status) {
2249 		if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
2250 					if_type == LPFC_SLI_INTF_IF_TYPE_2)
2251 			lpfc_printf_log(phba, KERN_ERR,
2252 					LOG_TRACE_EVENT,
2253 					"2726 READ_FCF_RECORD Indicates empty "
2254 					"FCF table.\n");
2255 		else
2256 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2257 					"2521 READ_FCF_RECORD mailbox failed "
2258 					"with status x%x add_status x%x, "
2259 					"mbx\n", shdr_status, shdr_add_status);
2260 		return NULL;
2261 	}
2262 
2263 	/* Interpreting the returned information of the FCF record */
2264 	read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
2265 	lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
2266 			      sizeof(struct lpfc_mbx_read_fcf_tbl));
2267 	*next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
2268 	new_fcf_record = (struct fcf_record *)(virt_addr +
2269 			  sizeof(struct lpfc_mbx_read_fcf_tbl));
2270 	lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
2271 				offsetof(struct fcf_record, vlan_bitmap));
2272 	new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
2273 	new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
2274 
2275 	return new_fcf_record;
2276 }
2277 
2278 /**
2279  * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
2280  * @phba: pointer to lpfc hba data structure.
2281  * @fcf_record: pointer to the fcf record.
2282  * @vlan_id: the lowest vlan identifier associated to this fcf record.
2283  * @next_fcf_index: the index to the next fcf record in hba's fcf table.
2284  *
2285  * This routine logs the detailed FCF record if the LOG_FIP loggin is
2286  * enabled.
2287  **/
2288 static void
lpfc_sli4_log_fcf_record_info(struct lpfc_hba * phba,struct fcf_record * fcf_record,uint16_t vlan_id,uint16_t next_fcf_index)2289 lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
2290 			      struct fcf_record *fcf_record,
2291 			      uint16_t vlan_id,
2292 			      uint16_t next_fcf_index)
2293 {
2294 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2295 			"2764 READ_FCF_RECORD:\n"
2296 			"\tFCF_Index     : x%x\n"
2297 			"\tFCF_Avail     : x%x\n"
2298 			"\tFCF_Valid     : x%x\n"
2299 			"\tFCF_SOL       : x%x\n"
2300 			"\tFIP_Priority  : x%x\n"
2301 			"\tMAC_Provider  : x%x\n"
2302 			"\tLowest VLANID : x%x\n"
2303 			"\tFCF_MAC Addr  : x%x:%x:%x:%x:%x:%x\n"
2304 			"\tFabric_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2305 			"\tSwitch_Name   : x%x:%x:%x:%x:%x:%x:%x:%x\n"
2306 			"\tNext_FCF_Index: x%x\n",
2307 			bf_get(lpfc_fcf_record_fcf_index, fcf_record),
2308 			bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
2309 			bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
2310 			bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
2311 			fcf_record->fip_priority,
2312 			bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
2313 			vlan_id,
2314 			bf_get(lpfc_fcf_record_mac_0, fcf_record),
2315 			bf_get(lpfc_fcf_record_mac_1, fcf_record),
2316 			bf_get(lpfc_fcf_record_mac_2, fcf_record),
2317 			bf_get(lpfc_fcf_record_mac_3, fcf_record),
2318 			bf_get(lpfc_fcf_record_mac_4, fcf_record),
2319 			bf_get(lpfc_fcf_record_mac_5, fcf_record),
2320 			bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
2321 			bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
2322 			bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
2323 			bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
2324 			bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
2325 			bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
2326 			bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
2327 			bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
2328 			bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
2329 			bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
2330 			bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
2331 			bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
2332 			bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
2333 			bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
2334 			bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
2335 			bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
2336 			next_fcf_index);
2337 }
2338 
2339 /**
2340  * lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
2341  * @phba: pointer to lpfc hba data structure.
2342  * @fcf_rec: pointer to an existing FCF record.
2343  * @new_fcf_record: pointer to a new FCF record.
2344  * @new_vlan_id: vlan id from the new FCF record.
2345  *
2346  * This function performs matching test of a new FCF record against an existing
2347  * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
2348  * will not be used as part of the FCF record matching criteria.
2349  *
2350  * Returns true if all the fields matching, otherwise returns false.
2351  */
2352 static bool
lpfc_sli4_fcf_record_match(struct lpfc_hba * phba,struct lpfc_fcf_rec * fcf_rec,struct fcf_record * new_fcf_record,uint16_t new_vlan_id)2353 lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
2354 			   struct lpfc_fcf_rec *fcf_rec,
2355 			   struct fcf_record *new_fcf_record,
2356 			   uint16_t new_vlan_id)
2357 {
2358 	if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
2359 		if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
2360 			return false;
2361 	if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
2362 		return false;
2363 	if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
2364 		return false;
2365 	if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
2366 		return false;
2367 	if (fcf_rec->priority != new_fcf_record->fip_priority)
2368 		return false;
2369 	return true;
2370 }
2371 
2372 /**
2373  * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
2374  * @vport: Pointer to vport object.
2375  * @fcf_index: index to next fcf.
2376  *
2377  * This function processing the roundrobin fcf failover to next fcf index.
2378  * When this function is invoked, there will be a current fcf registered
2379  * for flogi.
2380  * Return: 0 for continue retrying flogi on currently registered fcf;
2381  *         1 for stop flogi on currently registered fcf;
2382  */
lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport * vport,uint16_t fcf_index)2383 int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
2384 {
2385 	struct lpfc_hba *phba = vport->phba;
2386 	int rc;
2387 
2388 	if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2389 		spin_lock_irq(&phba->hbalock);
2390 		if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2391 			spin_unlock_irq(&phba->hbalock);
2392 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2393 					"2872 Devloss tmo with no eligible "
2394 					"FCF, unregister in-use FCF (x%x) "
2395 					"and rescan FCF table\n",
2396 					phba->fcf.current_rec.fcf_indx);
2397 			lpfc_unregister_fcf_rescan(phba);
2398 			goto stop_flogi_current_fcf;
2399 		}
2400 		/* Mark the end to FLOGI roundrobin failover */
2401 		phba->hba_flag &= ~FCF_RR_INPROG;
2402 		/* Allow action to new fcf asynchronous event */
2403 		phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2404 		spin_unlock_irq(&phba->hbalock);
2405 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2406 				"2865 No FCF available, stop roundrobin FCF "
2407 				"failover and change port state:x%x/x%x\n",
2408 				phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2409 		phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2410 
2411 		if (!phba->fcf.fcf_redisc_attempted) {
2412 			lpfc_unregister_fcf(phba);
2413 
2414 			rc = lpfc_sli4_redisc_fcf_table(phba);
2415 			if (!rc) {
2416 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2417 						"3195 Rediscover FCF table\n");
2418 				phba->fcf.fcf_redisc_attempted = 1;
2419 				lpfc_sli4_clear_fcf_rr_bmask(phba);
2420 			} else {
2421 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2422 						"3196 Rediscover FCF table "
2423 						"failed. Status:x%x\n", rc);
2424 			}
2425 		} else {
2426 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2427 					"3197 Already rediscover FCF table "
2428 					"attempted. No more retry\n");
2429 		}
2430 		goto stop_flogi_current_fcf;
2431 	} else {
2432 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2433 				"2794 Try FLOGI roundrobin FCF failover to "
2434 				"(x%x)\n", fcf_index);
2435 		rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2436 		if (rc)
2437 			lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2438 					"2761 FLOGI roundrobin FCF failover "
2439 					"failed (rc:x%x) to read FCF (x%x)\n",
2440 					rc, phba->fcf.current_rec.fcf_indx);
2441 		else
2442 			goto stop_flogi_current_fcf;
2443 	}
2444 	return 0;
2445 
2446 stop_flogi_current_fcf:
2447 	lpfc_can_disctmo(vport);
2448 	return 1;
2449 }
2450 
2451 /**
2452  * lpfc_sli4_fcf_pri_list_del
2453  * @phba: pointer to lpfc hba data structure.
2454  * @fcf_index: the index of the fcf record to delete
2455  * This routine checks the on list flag of the fcf_index to be deleted.
2456  * If it is one the list then it is removed from the list, and the flag
2457  * is cleared. This routine grab the hbalock before removing the fcf
2458  * record from the list.
2459  **/
lpfc_sli4_fcf_pri_list_del(struct lpfc_hba * phba,uint16_t fcf_index)2460 static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2461 			uint16_t fcf_index)
2462 {
2463 	struct lpfc_fcf_pri *new_fcf_pri;
2464 
2465 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2466 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2467 		"3058 deleting idx x%x pri x%x flg x%x\n",
2468 		fcf_index, new_fcf_pri->fcf_rec.priority,
2469 		 new_fcf_pri->fcf_rec.flag);
2470 	spin_lock_irq(&phba->hbalock);
2471 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2472 		if (phba->fcf.current_rec.priority ==
2473 				new_fcf_pri->fcf_rec.priority)
2474 			phba->fcf.eligible_fcf_cnt--;
2475 		list_del_init(&new_fcf_pri->list);
2476 		new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2477 	}
2478 	spin_unlock_irq(&phba->hbalock);
2479 }
2480 
2481 /**
2482  * lpfc_sli4_set_fcf_flogi_fail
2483  * @phba: pointer to lpfc hba data structure.
2484  * @fcf_index: the index of the fcf record to update
2485  * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2486  * flag so the round robin selection for the particular priority level
2487  * will try a different fcf record that does not have this bit set.
2488  * If the fcf record is re-read for any reason this flag is cleared brfore
2489  * adding it to the priority list.
2490  **/
2491 void
lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba * phba,uint16_t fcf_index)2492 lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2493 {
2494 	struct lpfc_fcf_pri *new_fcf_pri;
2495 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2496 	spin_lock_irq(&phba->hbalock);
2497 	new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2498 	spin_unlock_irq(&phba->hbalock);
2499 }
2500 
2501 /**
2502  * lpfc_sli4_fcf_pri_list_add
2503  * @phba: pointer to lpfc hba data structure.
2504  * @fcf_index: the index of the fcf record to add
2505  * @new_fcf_record: pointer to a new FCF record.
2506  * This routine checks the priority of the fcf_index to be added.
2507  * If it is a lower priority than the current head of the fcf_pri list
2508  * then it is added to the list in the right order.
2509  * If it is the same priority as the current head of the list then it
2510  * is added to the head of the list and its bit in the rr_bmask is set.
2511  * If the fcf_index to be added is of a higher priority than the current
2512  * head of the list then the rr_bmask is cleared, its bit is set in the
2513  * rr_bmask and it is added to the head of the list.
2514  * returns:
2515  * 0=success 1=failure
2516  **/
lpfc_sli4_fcf_pri_list_add(struct lpfc_hba * phba,uint16_t fcf_index,struct fcf_record * new_fcf_record)2517 static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2518 	uint16_t fcf_index,
2519 	struct fcf_record *new_fcf_record)
2520 {
2521 	uint16_t current_fcf_pri;
2522 	uint16_t last_index;
2523 	struct lpfc_fcf_pri *fcf_pri;
2524 	struct lpfc_fcf_pri *next_fcf_pri;
2525 	struct lpfc_fcf_pri *new_fcf_pri;
2526 	int ret;
2527 
2528 	new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2529 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2530 		"3059 adding idx x%x pri x%x flg x%x\n",
2531 		fcf_index, new_fcf_record->fip_priority,
2532 		 new_fcf_pri->fcf_rec.flag);
2533 	spin_lock_irq(&phba->hbalock);
2534 	if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2535 		list_del_init(&new_fcf_pri->list);
2536 	new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2537 	new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2538 	if (list_empty(&phba->fcf.fcf_pri_list)) {
2539 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2540 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2541 				new_fcf_pri->fcf_rec.fcf_index);
2542 		goto out;
2543 	}
2544 
2545 	last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2546 				LPFC_SLI4_FCF_TBL_INDX_MAX);
2547 	if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2548 		ret = 0; /* Empty rr list */
2549 		goto out;
2550 	}
2551 	current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2552 	if (new_fcf_pri->fcf_rec.priority <=  current_fcf_pri) {
2553 		list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2554 		if (new_fcf_pri->fcf_rec.priority <  current_fcf_pri) {
2555 			memset(phba->fcf.fcf_rr_bmask, 0,
2556 				sizeof(*phba->fcf.fcf_rr_bmask));
2557 			/* fcfs_at_this_priority_level = 1; */
2558 			phba->fcf.eligible_fcf_cnt = 1;
2559 		} else
2560 			/* fcfs_at_this_priority_level++; */
2561 			phba->fcf.eligible_fcf_cnt++;
2562 		ret = lpfc_sli4_fcf_rr_index_set(phba,
2563 				new_fcf_pri->fcf_rec.fcf_index);
2564 		goto out;
2565 	}
2566 
2567 	list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2568 				&phba->fcf.fcf_pri_list, list) {
2569 		if (new_fcf_pri->fcf_rec.priority <=
2570 				fcf_pri->fcf_rec.priority) {
2571 			if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2572 				list_add(&new_fcf_pri->list,
2573 						&phba->fcf.fcf_pri_list);
2574 			else
2575 				list_add(&new_fcf_pri->list,
2576 					 &((struct lpfc_fcf_pri *)
2577 					fcf_pri->list.prev)->list);
2578 			ret = 0;
2579 			goto out;
2580 		} else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2581 			|| new_fcf_pri->fcf_rec.priority <
2582 				next_fcf_pri->fcf_rec.priority) {
2583 			list_add(&new_fcf_pri->list, &fcf_pri->list);
2584 			ret = 0;
2585 			goto out;
2586 		}
2587 		if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2588 			continue;
2589 
2590 	}
2591 	ret = 1;
2592 out:
2593 	/* we use = instead of |= to clear the FLOGI_FAILED flag. */
2594 	new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2595 	spin_unlock_irq(&phba->hbalock);
2596 	return ret;
2597 }
2598 
2599 /**
2600  * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2601  * @phba: pointer to lpfc hba data structure.
2602  * @mboxq: pointer to mailbox object.
2603  *
2604  * This function iterates through all the fcf records available in
2605  * HBA and chooses the optimal FCF record for discovery. After finding
2606  * the FCF for discovery it registers the FCF record and kicks start
2607  * discovery.
2608  * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2609  * use an FCF record which matches fabric name and mac address of the
2610  * currently used FCF record.
2611  * If the driver supports only one FCF, it will try to use the FCF record
2612  * used by BOOT_BIOS.
2613  */
2614 void
lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)2615 lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2616 {
2617 	struct fcf_record *new_fcf_record;
2618 	uint32_t boot_flag, addr_mode;
2619 	uint16_t fcf_index, next_fcf_index;
2620 	struct lpfc_fcf_rec *fcf_rec = NULL;
2621 	uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2622 	bool select_new_fcf;
2623 	int rc;
2624 
2625 	/* If there is pending FCoE event restart FCF table scan */
2626 	if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
2627 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2628 		return;
2629 	}
2630 
2631 	/* Parse the FCF record from the non-embedded mailbox command */
2632 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2633 						      &next_fcf_index);
2634 	if (!new_fcf_record) {
2635 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2636 				"2765 Mailbox command READ_FCF_RECORD "
2637 				"failed to retrieve a FCF record.\n");
2638 		/* Let next new FCF event trigger fast failover */
2639 		spin_lock_irq(&phba->hbalock);
2640 		phba->hba_flag &= ~FCF_TS_INPROG;
2641 		spin_unlock_irq(&phba->hbalock);
2642 		lpfc_sli4_mbox_cmd_free(phba, mboxq);
2643 		return;
2644 	}
2645 
2646 	/* Check the FCF record against the connection list */
2647 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2648 				      &addr_mode, &vlan_id);
2649 
2650 	/* Log the FCF record information if turned on */
2651 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2652 				      next_fcf_index);
2653 
2654 	/*
2655 	 * If the fcf record does not match with connect list entries
2656 	 * read the next entry; otherwise, this is an eligible FCF
2657 	 * record for roundrobin FCF failover.
2658 	 */
2659 	if (!rc) {
2660 		lpfc_sli4_fcf_pri_list_del(phba,
2661 					bf_get(lpfc_fcf_record_fcf_index,
2662 					       new_fcf_record));
2663 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2664 				"2781 FCF (x%x) failed connection "
2665 				"list check: (x%x/x%x/%x)\n",
2666 				bf_get(lpfc_fcf_record_fcf_index,
2667 				       new_fcf_record),
2668 				bf_get(lpfc_fcf_record_fcf_avail,
2669 				       new_fcf_record),
2670 				bf_get(lpfc_fcf_record_fcf_valid,
2671 				       new_fcf_record),
2672 				bf_get(lpfc_fcf_record_fcf_sol,
2673 				       new_fcf_record));
2674 		if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2675 		    lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2676 		    new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
2677 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2678 			    phba->fcf.current_rec.fcf_indx) {
2679 				lpfc_printf_log(phba, KERN_ERR,
2680 						LOG_TRACE_EVENT,
2681 					"2862 FCF (x%x) matches property "
2682 					"of in-use FCF (x%x)\n",
2683 					bf_get(lpfc_fcf_record_fcf_index,
2684 					       new_fcf_record),
2685 					phba->fcf.current_rec.fcf_indx);
2686 				goto read_next_fcf;
2687 			}
2688 			/*
2689 			 * In case the current in-use FCF record becomes
2690 			 * invalid/unavailable during FCF discovery that
2691 			 * was not triggered by fast FCF failover process,
2692 			 * treat it as fast FCF failover.
2693 			 */
2694 			if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2695 			    !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2696 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2697 						"2835 Invalid in-use FCF "
2698 						"(x%x), enter FCF failover "
2699 						"table scan.\n",
2700 						phba->fcf.current_rec.fcf_indx);
2701 				spin_lock_irq(&phba->hbalock);
2702 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2703 				spin_unlock_irq(&phba->hbalock);
2704 				lpfc_sli4_mbox_cmd_free(phba, mboxq);
2705 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2706 						LPFC_FCOE_FCF_GET_FIRST);
2707 				return;
2708 			}
2709 		}
2710 		goto read_next_fcf;
2711 	} else {
2712 		fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2713 		rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2714 							new_fcf_record);
2715 		if (rc)
2716 			goto read_next_fcf;
2717 	}
2718 
2719 	/*
2720 	 * If this is not the first FCF discovery of the HBA, use last
2721 	 * FCF record for the discovery. The condition that a rescan
2722 	 * matches the in-use FCF record: fabric name, switch name, mac
2723 	 * address, and vlan_id.
2724 	 */
2725 	spin_lock_irq(&phba->hbalock);
2726 	if (phba->fcf.fcf_flag & FCF_IN_USE) {
2727 		if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2728 			lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2729 		    new_fcf_record, vlan_id)) {
2730 			if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2731 			    phba->fcf.current_rec.fcf_indx) {
2732 				phba->fcf.fcf_flag |= FCF_AVAILABLE;
2733 				if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2734 					/* Stop FCF redisc wait timer */
2735 					__lpfc_sli4_stop_fcf_redisc_wait_timer(
2736 									phba);
2737 				else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2738 					/* Fast failover, mark completed */
2739 					phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2740 				spin_unlock_irq(&phba->hbalock);
2741 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2742 						"2836 New FCF matches in-use "
2743 						"FCF (x%x), port_state:x%x, "
2744 						"fc_flag:x%x\n",
2745 						phba->fcf.current_rec.fcf_indx,
2746 						phba->pport->port_state,
2747 						phba->pport->fc_flag);
2748 				goto out;
2749 			} else
2750 				lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2751 					"2863 New FCF (x%x) matches "
2752 					"property of in-use FCF (x%x)\n",
2753 					bf_get(lpfc_fcf_record_fcf_index,
2754 					       new_fcf_record),
2755 					phba->fcf.current_rec.fcf_indx);
2756 		}
2757 		/*
2758 		 * Read next FCF record from HBA searching for the matching
2759 		 * with in-use record only if not during the fast failover
2760 		 * period. In case of fast failover period, it shall try to
2761 		 * determine whether the FCF record just read should be the
2762 		 * next candidate.
2763 		 */
2764 		if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2765 			spin_unlock_irq(&phba->hbalock);
2766 			goto read_next_fcf;
2767 		}
2768 	}
2769 	/*
2770 	 * Update on failover FCF record only if it's in FCF fast-failover
2771 	 * period; otherwise, update on current FCF record.
2772 	 */
2773 	if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2774 		fcf_rec = &phba->fcf.failover_rec;
2775 	else
2776 		fcf_rec = &phba->fcf.current_rec;
2777 
2778 	if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2779 		/*
2780 		 * If the driver FCF record does not have boot flag
2781 		 * set and new hba fcf record has boot flag set, use
2782 		 * the new hba fcf record.
2783 		 */
2784 		if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2785 			/* Choose this FCF record */
2786 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2787 					"2837 Update current FCF record "
2788 					"(x%x) with new FCF record (x%x)\n",
2789 					fcf_rec->fcf_indx,
2790 					bf_get(lpfc_fcf_record_fcf_index,
2791 					new_fcf_record));
2792 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2793 					addr_mode, vlan_id, BOOT_ENABLE);
2794 			spin_unlock_irq(&phba->hbalock);
2795 			goto read_next_fcf;
2796 		}
2797 		/*
2798 		 * If the driver FCF record has boot flag set and the
2799 		 * new hba FCF record does not have boot flag, read
2800 		 * the next FCF record.
2801 		 */
2802 		if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
2803 			spin_unlock_irq(&phba->hbalock);
2804 			goto read_next_fcf;
2805 		}
2806 		/*
2807 		 * If the new hba FCF record has lower priority value
2808 		 * than the driver FCF record, use the new record.
2809 		 */
2810 		if (new_fcf_record->fip_priority < fcf_rec->priority) {
2811 			/* Choose the new FCF record with lower priority */
2812 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2813 					"2838 Update current FCF record "
2814 					"(x%x) with new FCF record (x%x)\n",
2815 					fcf_rec->fcf_indx,
2816 					bf_get(lpfc_fcf_record_fcf_index,
2817 					       new_fcf_record));
2818 			__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2819 					addr_mode, vlan_id, 0);
2820 			/* Reset running random FCF selection count */
2821 			phba->fcf.eligible_fcf_cnt = 1;
2822 		} else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2823 			/* Update running random FCF selection count */
2824 			phba->fcf.eligible_fcf_cnt++;
2825 			select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2826 						phba->fcf.eligible_fcf_cnt);
2827 			if (select_new_fcf) {
2828 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2829 					"2839 Update current FCF record "
2830 					"(x%x) with new FCF record (x%x)\n",
2831 					fcf_rec->fcf_indx,
2832 					bf_get(lpfc_fcf_record_fcf_index,
2833 					       new_fcf_record));
2834 				/* Choose the new FCF by random selection */
2835 				__lpfc_update_fcf_record(phba, fcf_rec,
2836 							 new_fcf_record,
2837 							 addr_mode, vlan_id, 0);
2838 			}
2839 		}
2840 		spin_unlock_irq(&phba->hbalock);
2841 		goto read_next_fcf;
2842 	}
2843 	/*
2844 	 * This is the first suitable FCF record, choose this record for
2845 	 * initial best-fit FCF.
2846 	 */
2847 	if (fcf_rec) {
2848 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2849 				"2840 Update initial FCF candidate "
2850 				"with FCF (x%x)\n",
2851 				bf_get(lpfc_fcf_record_fcf_index,
2852 				       new_fcf_record));
2853 		__lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2854 					 addr_mode, vlan_id, (boot_flag ?
2855 					 BOOT_ENABLE : 0));
2856 		phba->fcf.fcf_flag |= FCF_AVAILABLE;
2857 		/* Setup initial running random FCF selection count */
2858 		phba->fcf.eligible_fcf_cnt = 1;
2859 	}
2860 	spin_unlock_irq(&phba->hbalock);
2861 	goto read_next_fcf;
2862 
2863 read_next_fcf:
2864 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2865 	if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2866 		if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2867 			/*
2868 			 * Case of FCF fast failover scan
2869 			 */
2870 
2871 			/*
2872 			 * It has not found any suitable FCF record, cancel
2873 			 * FCF scan inprogress, and do nothing
2874 			 */
2875 			if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
2876 				lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2877 					       "2782 No suitable FCF found: "
2878 					       "(x%x/x%x)\n",
2879 					       phba->fcoe_eventtag_at_fcf_scan,
2880 					       bf_get(lpfc_fcf_record_fcf_index,
2881 						      new_fcf_record));
2882 				spin_lock_irq(&phba->hbalock);
2883 				if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2884 					phba->hba_flag &= ~FCF_TS_INPROG;
2885 					spin_unlock_irq(&phba->hbalock);
2886 					/* Unregister in-use FCF and rescan */
2887 					lpfc_printf_log(phba, KERN_INFO,
2888 							LOG_FIP,
2889 							"2864 On devloss tmo "
2890 							"unreg in-use FCF and "
2891 							"rescan FCF table\n");
2892 					lpfc_unregister_fcf_rescan(phba);
2893 					return;
2894 				}
2895 				/*
2896 				 * Let next new FCF event trigger fast failover
2897 				 */
2898 				phba->hba_flag &= ~FCF_TS_INPROG;
2899 				spin_unlock_irq(&phba->hbalock);
2900 				return;
2901 			}
2902 			/*
2903 			 * It has found a suitable FCF record that is not
2904 			 * the same as in-use FCF record, unregister the
2905 			 * in-use FCF record, replace the in-use FCF record
2906 			 * with the new FCF record, mark FCF fast failover
2907 			 * completed, and then start register the new FCF
2908 			 * record.
2909 			 */
2910 
2911 			/* Unregister the current in-use FCF record */
2912 			lpfc_unregister_fcf(phba);
2913 
2914 			/* Replace in-use record with the new record */
2915 			lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2916 					"2842 Replace in-use FCF (x%x) "
2917 					"with failover FCF (x%x)\n",
2918 					phba->fcf.current_rec.fcf_indx,
2919 					phba->fcf.failover_rec.fcf_indx);
2920 			memcpy(&phba->fcf.current_rec,
2921 			       &phba->fcf.failover_rec,
2922 			       sizeof(struct lpfc_fcf_rec));
2923 			/*
2924 			 * Mark the fast FCF failover rediscovery completed
2925 			 * and the start of the first round of the roundrobin
2926 			 * FCF failover.
2927 			 */
2928 			spin_lock_irq(&phba->hbalock);
2929 			phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2930 			spin_unlock_irq(&phba->hbalock);
2931 			/* Register to the new FCF record */
2932 			lpfc_register_fcf(phba);
2933 		} else {
2934 			/*
2935 			 * In case of transaction period to fast FCF failover,
2936 			 * do nothing when search to the end of the FCF table.
2937 			 */
2938 			if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2939 			    (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2940 				return;
2941 
2942 			if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2943 				phba->fcf.fcf_flag & FCF_IN_USE) {
2944 				/*
2945 				 * In case the current in-use FCF record no
2946 				 * longer existed during FCF discovery that
2947 				 * was not triggered by fast FCF failover
2948 				 * process, treat it as fast FCF failover.
2949 				 */
2950 				lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2951 						"2841 In-use FCF record (x%x) "
2952 						"not reported, entering fast "
2953 						"FCF failover mode scanning.\n",
2954 						phba->fcf.current_rec.fcf_indx);
2955 				spin_lock_irq(&phba->hbalock);
2956 				phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2957 				spin_unlock_irq(&phba->hbalock);
2958 				lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2959 						LPFC_FCOE_FCF_GET_FIRST);
2960 				return;
2961 			}
2962 			/* Register to the new FCF record */
2963 			lpfc_register_fcf(phba);
2964 		}
2965 	} else
2966 		lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
2967 	return;
2968 
2969 out:
2970 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
2971 	lpfc_register_fcf(phba);
2972 
2973 	return;
2974 }
2975 
2976 /**
2977  * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
2978  * @phba: pointer to lpfc hba data structure.
2979  * @mboxq: pointer to mailbox object.
2980  *
2981  * This is the callback function for FLOGI failure roundrobin FCF failover
2982  * read FCF record mailbox command from the eligible FCF record bmask for
2983  * performing the failover. If the FCF read back is not valid/available, it
2984  * fails through to retrying FLOGI to the currently registered FCF again.
2985  * Otherwise, if the FCF read back is valid and available, it will set the
2986  * newly read FCF record to the failover FCF record, unregister currently
2987  * registered FCF record, copy the failover FCF record to the current
2988  * FCF record, and then register the current FCF record before proceeding
2989  * to trying FLOGI on the new failover FCF.
2990  */
2991 void
lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)2992 lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2993 {
2994 	struct fcf_record *new_fcf_record;
2995 	uint32_t boot_flag, addr_mode;
2996 	uint16_t next_fcf_index, fcf_index;
2997 	uint16_t current_fcf_index;
2998 	uint16_t vlan_id = LPFC_FCOE_NULL_VID;
2999 	int rc;
3000 
3001 	/* If link state is not up, stop the roundrobin failover process */
3002 	if (phba->link_state < LPFC_LINK_UP) {
3003 		spin_lock_irq(&phba->hbalock);
3004 		phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
3005 		phba->hba_flag &= ~FCF_RR_INPROG;
3006 		spin_unlock_irq(&phba->hbalock);
3007 		goto out;
3008 	}
3009 
3010 	/* Parse the FCF record from the non-embedded mailbox command */
3011 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
3012 						      &next_fcf_index);
3013 	if (!new_fcf_record) {
3014 		lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
3015 				"2766 Mailbox command READ_FCF_RECORD "
3016 				"failed to retrieve a FCF record. "
3017 				"hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
3018 				phba->fcf.fcf_flag);
3019 		lpfc_unregister_fcf_rescan(phba);
3020 		goto out;
3021 	}
3022 
3023 	/* Get the needed parameters from FCF record */
3024 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
3025 				      &addr_mode, &vlan_id);
3026 
3027 	/* Log the FCF record information if turned on */
3028 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
3029 				      next_fcf_index);
3030 
3031 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
3032 	if (!rc) {
3033 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3034 				"2848 Remove ineligible FCF (x%x) from "
3035 				"from roundrobin bmask\n", fcf_index);
3036 		/* Clear roundrobin bmask bit for ineligible FCF */
3037 		lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
3038 		/* Perform next round of roundrobin FCF failover */
3039 		fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
3040 		rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
3041 		if (rc)
3042 			goto out;
3043 		goto error_out;
3044 	}
3045 
3046 	if (fcf_index == phba->fcf.current_rec.fcf_indx) {
3047 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3048 				"2760 Perform FLOGI roundrobin FCF failover: "
3049 				"FCF (x%x) back to FCF (x%x)\n",
3050 				phba->fcf.current_rec.fcf_indx, fcf_index);
3051 		/* Wait 500 ms before retrying FLOGI to current FCF */
3052 		msleep(500);
3053 		lpfc_issue_init_vfi(phba->pport);
3054 		goto out;
3055 	}
3056 
3057 	/* Upload new FCF record to the failover FCF record */
3058 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3059 			"2834 Update current FCF (x%x) with new FCF (x%x)\n",
3060 			phba->fcf.failover_rec.fcf_indx, fcf_index);
3061 	spin_lock_irq(&phba->hbalock);
3062 	__lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
3063 				 new_fcf_record, addr_mode, vlan_id,
3064 				 (boot_flag ? BOOT_ENABLE : 0));
3065 	spin_unlock_irq(&phba->hbalock);
3066 
3067 	current_fcf_index = phba->fcf.current_rec.fcf_indx;
3068 
3069 	/* Unregister the current in-use FCF record */
3070 	lpfc_unregister_fcf(phba);
3071 
3072 	/* Replace in-use record with the new record */
3073 	memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
3074 	       sizeof(struct lpfc_fcf_rec));
3075 
3076 	lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3077 			"2783 Perform FLOGI roundrobin FCF failover: FCF "
3078 			"(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
3079 
3080 error_out:
3081 	lpfc_register_fcf(phba);
3082 out:
3083 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
3084 }
3085 
3086 /**
3087  * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
3088  * @phba: pointer to lpfc hba data structure.
3089  * @mboxq: pointer to mailbox object.
3090  *
3091  * This is the callback function of read FCF record mailbox command for
3092  * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
3093  * failover when a new FCF event happened. If the FCF read back is
3094  * valid/available and it passes the connection list check, it updates
3095  * the bmask for the eligible FCF record for roundrobin failover.
3096  */
3097 void
lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)3098 lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3099 {
3100 	struct fcf_record *new_fcf_record;
3101 	uint32_t boot_flag, addr_mode;
3102 	uint16_t fcf_index, next_fcf_index;
3103 	uint16_t vlan_id =  LPFC_FCOE_NULL_VID;
3104 	int rc;
3105 
3106 	/* If link state is not up, no need to proceed */
3107 	if (phba->link_state < LPFC_LINK_UP)
3108 		goto out;
3109 
3110 	/* If FCF discovery period is over, no need to proceed */
3111 	if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
3112 		goto out;
3113 
3114 	/* Parse the FCF record from the non-embedded mailbox command */
3115 	new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
3116 						      &next_fcf_index);
3117 	if (!new_fcf_record) {
3118 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3119 				"2767 Mailbox command READ_FCF_RECORD "
3120 				"failed to retrieve a FCF record.\n");
3121 		goto out;
3122 	}
3123 
3124 	/* Check the connection list for eligibility */
3125 	rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
3126 				      &addr_mode, &vlan_id);
3127 
3128 	/* Log the FCF record information if turned on */
3129 	lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
3130 				      next_fcf_index);
3131 
3132 	if (!rc)
3133 		goto out;
3134 
3135 	/* Update the eligible FCF record index bmask */
3136 	fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
3137 
3138 	rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
3139 
3140 out:
3141 	lpfc_sli4_mbox_cmd_free(phba, mboxq);
3142 }
3143 
3144 /**
3145  * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
3146  * @phba: pointer to lpfc hba data structure.
3147  * @mboxq: pointer to mailbox data structure.
3148  *
3149  * This function handles completion of init vfi mailbox command.
3150  */
3151 static void
lpfc_init_vfi_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)3152 lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3153 {
3154 	struct lpfc_vport *vport = mboxq->vport;
3155 
3156 	/*
3157 	 * VFI not supported on interface type 0, just do the flogi
3158 	 * Also continue if the VFI is in use - just use the same one.
3159 	 */
3160 	if (mboxq->u.mb.mbxStatus &&
3161 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3162 			LPFC_SLI_INTF_IF_TYPE_0) &&
3163 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3164 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3165 				 "2891 Init VFI mailbox failed 0x%x\n",
3166 				 mboxq->u.mb.mbxStatus);
3167 		mempool_free(mboxq, phba->mbox_mem_pool);
3168 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3169 		return;
3170 	}
3171 
3172 	lpfc_initial_flogi(vport);
3173 	mempool_free(mboxq, phba->mbox_mem_pool);
3174 	return;
3175 }
3176 
3177 /**
3178  * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
3179  * @vport: pointer to lpfc_vport data structure.
3180  *
3181  * This function issue a init_vfi mailbox command to initialize the VFI and
3182  * VPI for the physical port.
3183  */
3184 void
lpfc_issue_init_vfi(struct lpfc_vport * vport)3185 lpfc_issue_init_vfi(struct lpfc_vport *vport)
3186 {
3187 	LPFC_MBOXQ_t *mboxq;
3188 	int rc;
3189 	struct lpfc_hba *phba = vport->phba;
3190 
3191 	mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3192 	if (!mboxq) {
3193 		lpfc_printf_vlog(vport, KERN_ERR,
3194 			LOG_TRACE_EVENT, "2892 Failed to allocate "
3195 			"init_vfi mailbox\n");
3196 		return;
3197 	}
3198 	lpfc_init_vfi(mboxq, vport);
3199 	mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
3200 	rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
3201 	if (rc == MBX_NOT_FINISHED) {
3202 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3203 				 "2893 Failed to issue init_vfi mailbox\n");
3204 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
3205 	}
3206 }
3207 
3208 /**
3209  * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
3210  * @phba: pointer to lpfc hba data structure.
3211  * @mboxq: pointer to mailbox data structure.
3212  *
3213  * This function handles completion of init vpi mailbox command.
3214  */
3215 void
lpfc_init_vpi_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)3216 lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3217 {
3218 	struct lpfc_vport *vport = mboxq->vport;
3219 	struct lpfc_nodelist *ndlp;
3220 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3221 
3222 	if (mboxq->u.mb.mbxStatus) {
3223 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3224 				 "2609 Init VPI mailbox failed 0x%x\n",
3225 				 mboxq->u.mb.mbxStatus);
3226 		mempool_free(mboxq, phba->mbox_mem_pool);
3227 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3228 		return;
3229 	}
3230 	spin_lock_irq(shost->host_lock);
3231 	vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3232 	spin_unlock_irq(shost->host_lock);
3233 
3234 	/* If this port is physical port or FDISC is done, do reg_vpi */
3235 	if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
3236 			ndlp = lpfc_findnode_did(vport, Fabric_DID);
3237 			if (!ndlp)
3238 				lpfc_printf_vlog(vport, KERN_ERR,
3239 					LOG_TRACE_EVENT,
3240 					"2731 Cannot find fabric "
3241 					"controller node\n");
3242 			else
3243 				lpfc_register_new_vport(phba, vport, ndlp);
3244 			mempool_free(mboxq, phba->mbox_mem_pool);
3245 			return;
3246 	}
3247 
3248 	if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3249 		lpfc_initial_fdisc(vport);
3250 	else {
3251 		lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
3252 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3253 				 "2606 No NPIV Fabric support\n");
3254 	}
3255 	mempool_free(mboxq, phba->mbox_mem_pool);
3256 	return;
3257 }
3258 
3259 /**
3260  * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
3261  * @vport: pointer to lpfc_vport data structure.
3262  *
3263  * This function issue a init_vpi mailbox command to initialize
3264  * VPI for the vport.
3265  */
3266 void
lpfc_issue_init_vpi(struct lpfc_vport * vport)3267 lpfc_issue_init_vpi(struct lpfc_vport *vport)
3268 {
3269 	LPFC_MBOXQ_t *mboxq;
3270 	int rc, vpi;
3271 
3272 	if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
3273 		vpi = lpfc_alloc_vpi(vport->phba);
3274 		if (!vpi) {
3275 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3276 					 "3303 Failed to obtain vport vpi\n");
3277 			lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3278 			return;
3279 		}
3280 		vport->vpi = vpi;
3281 	}
3282 
3283 	mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
3284 	if (!mboxq) {
3285 		lpfc_printf_vlog(vport, KERN_ERR,
3286 			LOG_TRACE_EVENT, "2607 Failed to allocate "
3287 			"init_vpi mailbox\n");
3288 		return;
3289 	}
3290 	lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
3291 	mboxq->vport = vport;
3292 	mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
3293 	rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
3294 	if (rc == MBX_NOT_FINISHED) {
3295 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3296 				 "2608 Failed to issue init_vpi mailbox\n");
3297 		mempool_free(mboxq, vport->phba->mbox_mem_pool);
3298 	}
3299 }
3300 
3301 /**
3302  * lpfc_start_fdiscs - send fdiscs for each vports on this port.
3303  * @phba: pointer to lpfc hba data structure.
3304  *
3305  * This function loops through the list of vports on the @phba and issues an
3306  * FDISC if possible.
3307  */
3308 void
lpfc_start_fdiscs(struct lpfc_hba * phba)3309 lpfc_start_fdiscs(struct lpfc_hba *phba)
3310 {
3311 	struct lpfc_vport **vports;
3312 	int i;
3313 
3314 	vports = lpfc_create_vport_work_array(phba);
3315 	if (vports != NULL) {
3316 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3317 			if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
3318 				continue;
3319 			/* There are no vpi for this vport */
3320 			if (vports[i]->vpi > phba->max_vpi) {
3321 				lpfc_vport_set_state(vports[i],
3322 						     FC_VPORT_FAILED);
3323 				continue;
3324 			}
3325 			if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3326 				lpfc_vport_set_state(vports[i],
3327 						     FC_VPORT_LINKDOWN);
3328 				continue;
3329 			}
3330 			if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
3331 				lpfc_issue_init_vpi(vports[i]);
3332 				continue;
3333 			}
3334 			if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
3335 				lpfc_initial_fdisc(vports[i]);
3336 			else {
3337 				lpfc_vport_set_state(vports[i],
3338 						     FC_VPORT_NO_FABRIC_SUPP);
3339 				lpfc_printf_vlog(vports[i], KERN_ERR,
3340 						 LOG_TRACE_EVENT,
3341 						 "0259 No NPIV "
3342 						 "Fabric support\n");
3343 			}
3344 		}
3345 	}
3346 	lpfc_destroy_vport_work_array(phba, vports);
3347 }
3348 
3349 void
lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)3350 lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
3351 {
3352 	struct lpfc_vport *vport = mboxq->vport;
3353 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3354 
3355 	/*
3356 	 * VFI not supported for interface type 0, so ignore any mailbox
3357 	 * error (except VFI in use) and continue with the discovery.
3358 	 */
3359 	if (mboxq->u.mb.mbxStatus &&
3360 	    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
3361 			LPFC_SLI_INTF_IF_TYPE_0) &&
3362 	    mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
3363 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3364 				 "2018 REG_VFI mbxStatus error x%x "
3365 				 "HBA state x%x\n",
3366 				 mboxq->u.mb.mbxStatus, vport->port_state);
3367 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3368 			/* FLOGI failed, use loop map to make discovery list */
3369 			lpfc_disc_list_loopmap(vport);
3370 			/* Start discovery */
3371 			lpfc_disc_start(vport);
3372 			goto out_free_mem;
3373 		}
3374 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3375 		goto out_free_mem;
3376 	}
3377 
3378 	/* If the VFI is already registered, there is nothing else to do
3379 	 * Unless this was a VFI update and we are in PT2PT mode, then
3380 	 * we should drop through to set the port state to ready.
3381 	 */
3382 	if (vport->fc_flag & FC_VFI_REGISTERED)
3383 		if (!(phba->sli_rev == LPFC_SLI_REV4 &&
3384 		      vport->fc_flag & FC_PT2PT))
3385 			goto out_free_mem;
3386 
3387 	/* The VPI is implicitly registered when the VFI is registered */
3388 	spin_lock_irq(shost->host_lock);
3389 	vport->vpi_state |= LPFC_VPI_REGISTERED;
3390 	vport->fc_flag |= FC_VFI_REGISTERED;
3391 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
3392 	vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
3393 	spin_unlock_irq(shost->host_lock);
3394 
3395 	/* In case SLI4 FC loopback test, we are ready */
3396 	if ((phba->sli_rev == LPFC_SLI_REV4) &&
3397 	    (phba->link_flag & LS_LOOPBACK_MODE)) {
3398 		phba->link_state = LPFC_HBA_READY;
3399 		goto out_free_mem;
3400 	}
3401 
3402 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3403 			 "3313 cmpl reg vfi  port_state:%x fc_flag:%x myDid:%x "
3404 			 "alpacnt:%d LinkState:%x topology:%x\n",
3405 			 vport->port_state, vport->fc_flag, vport->fc_myDID,
3406 			 vport->phba->alpa_map[0],
3407 			 phba->link_state, phba->fc_topology);
3408 
3409 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
3410 		/*
3411 		 * For private loop or for NPort pt2pt,
3412 		 * just start discovery and we are done.
3413 		 */
3414 		if ((vport->fc_flag & FC_PT2PT) ||
3415 		    ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3416 		    !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3417 
3418 			/* Use loop map to make discovery list */
3419 			lpfc_disc_list_loopmap(vport);
3420 			/* Start discovery */
3421 			if (vport->fc_flag & FC_PT2PT)
3422 				vport->port_state = LPFC_VPORT_READY;
3423 			else
3424 				lpfc_disc_start(vport);
3425 		} else {
3426 			lpfc_start_fdiscs(phba);
3427 			lpfc_do_scr_ns_plogi(phba, vport);
3428 		}
3429 	}
3430 
3431 out_free_mem:
3432 	lpfc_mbox_rsrc_cleanup(phba, mboxq, MBOX_THD_UNLOCKED);
3433 }
3434 
3435 static void
lpfc_mbx_cmpl_read_sparam(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)3436 lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3437 {
3438 	MAILBOX_t *mb = &pmb->u.mb;
3439 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3440 	struct lpfc_vport  *vport = pmb->vport;
3441 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3442 	struct serv_parm *sp = &vport->fc_sparam;
3443 	uint32_t ed_tov;
3444 
3445 	/* Check for error */
3446 	if (mb->mbxStatus) {
3447 		/* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
3448 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3449 				 "0319 READ_SPARAM mbxStatus error x%x "
3450 				 "hba state x%x>\n",
3451 				 mb->mbxStatus, vport->port_state);
3452 		lpfc_linkdown(phba);
3453 		goto out;
3454 	}
3455 
3456 	memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
3457 	       sizeof (struct serv_parm));
3458 
3459 	ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3460 	if (sp->cmn.edtovResolution)	/* E_D_TOV ticks are in nanoseconds */
3461 		ed_tov = (ed_tov + 999999) / 1000000;
3462 
3463 	phba->fc_edtov = ed_tov;
3464 	phba->fc_ratov = (2 * ed_tov) / 1000;
3465 	if (phba->fc_ratov < FF_DEF_RATOV) {
3466 		/* RA_TOV should be atleast 10sec for initial flogi */
3467 		phba->fc_ratov = FF_DEF_RATOV;
3468 	}
3469 
3470 	lpfc_update_vport_wwn(vport);
3471 	fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
3472 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
3473 		memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3474 		memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3475 	}
3476 
3477 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3478 
3479 	/* Check if sending the FLOGI is being deferred to after we get
3480 	 * up to date CSPs from MBX_READ_SPARAM.
3481 	 */
3482 	if (phba->hba_flag & HBA_DEFER_FLOGI) {
3483 		lpfc_initial_flogi(vport);
3484 		phba->hba_flag &= ~HBA_DEFER_FLOGI;
3485 	}
3486 	return;
3487 
3488 out:
3489 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3490 	lpfc_issue_clear_la(phba, vport);
3491 }
3492 
3493 static void
lpfc_mbx_process_link_up(struct lpfc_hba * phba,struct lpfc_mbx_read_top * la)3494 lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
3495 {
3496 	struct lpfc_vport *vport = phba->pport;
3497 	LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
3498 	struct Scsi_Host *shost;
3499 	int i;
3500 	int rc;
3501 	struct fcf_record *fcf_record;
3502 	uint32_t fc_flags = 0;
3503 	unsigned long iflags;
3504 
3505 	spin_lock_irqsave(&phba->hbalock, iflags);
3506 	phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3507 
3508 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3509 		switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3510 		case LPFC_LINK_SPEED_1GHZ:
3511 		case LPFC_LINK_SPEED_2GHZ:
3512 		case LPFC_LINK_SPEED_4GHZ:
3513 		case LPFC_LINK_SPEED_8GHZ:
3514 		case LPFC_LINK_SPEED_10GHZ:
3515 		case LPFC_LINK_SPEED_16GHZ:
3516 		case LPFC_LINK_SPEED_32GHZ:
3517 		case LPFC_LINK_SPEED_64GHZ:
3518 		case LPFC_LINK_SPEED_128GHZ:
3519 		case LPFC_LINK_SPEED_256GHZ:
3520 			break;
3521 		default:
3522 			phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3523 			break;
3524 		}
3525 	}
3526 
3527 	if (phba->fc_topology &&
3528 	    phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3529 		lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3530 				"3314 Toplogy changed was 0x%x is 0x%x\n",
3531 				phba->fc_topology,
3532 				bf_get(lpfc_mbx_read_top_topology, la));
3533 		phba->fc_topology_changed = 1;
3534 	}
3535 
3536 	phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
3537 	phba->link_flag &= ~(LS_NPIV_FAB_SUPPORTED | LS_CT_VEN_RPA);
3538 
3539 	shost = lpfc_shost_from_vport(vport);
3540 	if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
3541 		phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
3542 
3543 		/* if npiv is enabled and this adapter supports npiv log
3544 		 * a message that npiv is not supported in this topology
3545 		 */
3546 		if (phba->cfg_enable_npiv && phba->max_vpi)
3547 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3548 				"1309 Link Up Event npiv not supported in loop "
3549 				"topology\n");
3550 				/* Get Loop Map information */
3551 		if (bf_get(lpfc_mbx_read_top_il, la))
3552 			fc_flags |= FC_LBIT;
3553 
3554 		vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3555 		i = la->lilpBde64.tus.f.bdeSize;
3556 
3557 		if (i == 0) {
3558 			phba->alpa_map[0] = 0;
3559 		} else {
3560 			if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
3561 				int numalpa, j, k;
3562 				union {
3563 					uint8_t pamap[16];
3564 					struct {
3565 						uint32_t wd1;
3566 						uint32_t wd2;
3567 						uint32_t wd3;
3568 						uint32_t wd4;
3569 					} pa;
3570 				} un;
3571 				numalpa = phba->alpa_map[0];
3572 				j = 0;
3573 				while (j < numalpa) {
3574 					memset(un.pamap, 0, 16);
3575 					for (k = 1; j < numalpa; k++) {
3576 						un.pamap[k - 1] =
3577 							phba->alpa_map[j + 1];
3578 						j++;
3579 						if (k == 16)
3580 							break;
3581 					}
3582 					/* Link Up Event ALPA map */
3583 					lpfc_printf_log(phba,
3584 							KERN_WARNING,
3585 							LOG_LINK_EVENT,
3586 							"1304 Link Up Event "
3587 							"ALPA map Data: x%x "
3588 							"x%x x%x x%x\n",
3589 							un.pa.wd1, un.pa.wd2,
3590 							un.pa.wd3, un.pa.wd4);
3591 				}
3592 			}
3593 		}
3594 	} else {
3595 		if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
3596 			if (phba->max_vpi && phba->cfg_enable_npiv &&
3597 			   (phba->sli_rev >= LPFC_SLI_REV3))
3598 				phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3599 		}
3600 		vport->fc_myDID = phba->fc_pref_DID;
3601 		fc_flags |= FC_LBIT;
3602 	}
3603 	spin_unlock_irqrestore(&phba->hbalock, iflags);
3604 
3605 	if (fc_flags) {
3606 		spin_lock_irqsave(shost->host_lock, iflags);
3607 		vport->fc_flag |= fc_flags;
3608 		spin_unlock_irqrestore(shost->host_lock, iflags);
3609 	}
3610 
3611 	lpfc_linkup(phba);
3612 	sparam_mbox = NULL;
3613 
3614 	sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3615 	if (!sparam_mbox)
3616 		goto out;
3617 
3618 	rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3619 	if (rc) {
3620 		mempool_free(sparam_mbox, phba->mbox_mem_pool);
3621 		goto out;
3622 	}
3623 	sparam_mbox->vport = vport;
3624 	sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3625 	rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3626 	if (rc == MBX_NOT_FINISHED) {
3627 		lpfc_mbox_rsrc_cleanup(phba, sparam_mbox, MBOX_THD_UNLOCKED);
3628 		goto out;
3629 	}
3630 
3631 	if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3632 		cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3633 		if (!cfglink_mbox)
3634 			goto out;
3635 		vport->port_state = LPFC_LOCAL_CFG_LINK;
3636 		lpfc_config_link(phba, cfglink_mbox);
3637 		cfglink_mbox->vport = vport;
3638 		cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
3639 		rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
3640 		if (rc == MBX_NOT_FINISHED) {
3641 			mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3642 			goto out;
3643 		}
3644 	} else {
3645 		vport->port_state = LPFC_VPORT_UNKNOWN;
3646 		/*
3647 		 * Add the driver's default FCF record at FCF index 0 now. This
3648 		 * is phase 1 implementation that support FCF index 0 and driver
3649 		 * defaults.
3650 		 */
3651 		if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
3652 			fcf_record = kzalloc(sizeof(struct fcf_record),
3653 					GFP_KERNEL);
3654 			if (unlikely(!fcf_record)) {
3655 				lpfc_printf_log(phba, KERN_ERR,
3656 					LOG_TRACE_EVENT,
3657 					"2554 Could not allocate memory for "
3658 					"fcf record\n");
3659 				rc = -ENODEV;
3660 				goto out;
3661 			}
3662 
3663 			lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3664 						LPFC_FCOE_FCF_DEF_INDEX);
3665 			rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3666 			if (unlikely(rc)) {
3667 				lpfc_printf_log(phba, KERN_ERR,
3668 					LOG_TRACE_EVENT,
3669 					"2013 Could not manually add FCF "
3670 					"record 0, status %d\n", rc);
3671 				rc = -ENODEV;
3672 				kfree(fcf_record);
3673 				goto out;
3674 			}
3675 			kfree(fcf_record);
3676 		}
3677 		/*
3678 		 * The driver is expected to do FIP/FCF. Call the port
3679 		 * and get the FCF Table.
3680 		 */
3681 		spin_lock_irqsave(&phba->hbalock, iflags);
3682 		if (phba->hba_flag & FCF_TS_INPROG) {
3683 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3684 			return;
3685 		}
3686 		/* This is the initial FCF discovery scan */
3687 		phba->fcf.fcf_flag |= FCF_INIT_DISC;
3688 		spin_unlock_irqrestore(&phba->hbalock, iflags);
3689 		lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3690 				"2778 Start FCF table scan at linkup\n");
3691 		rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3692 						     LPFC_FCOE_FCF_GET_FIRST);
3693 		if (rc) {
3694 			spin_lock_irqsave(&phba->hbalock, iflags);
3695 			phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3696 			spin_unlock_irqrestore(&phba->hbalock, iflags);
3697 			goto out;
3698 		}
3699 		/* Reset FCF roundrobin bmask for new discovery */
3700 		lpfc_sli4_clear_fcf_rr_bmask(phba);
3701 	}
3702 
3703 	/* Prepare for LINK up registrations */
3704 	memset(phba->os_host_name, 0, sizeof(phba->os_host_name));
3705 	scnprintf(phba->os_host_name, sizeof(phba->os_host_name), "%s",
3706 		  init_utsname()->nodename);
3707 	return;
3708 out:
3709 	lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3710 	lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3711 			 "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
3712 			 vport->port_state, sparam_mbox, cfglink_mbox);
3713 	lpfc_issue_clear_la(phba, vport);
3714 	return;
3715 }
3716 
3717 static void
lpfc_enable_la(struct lpfc_hba * phba)3718 lpfc_enable_la(struct lpfc_hba *phba)
3719 {
3720 	uint32_t control;
3721 	struct lpfc_sli *psli = &phba->sli;
3722 	spin_lock_irq(&phba->hbalock);
3723 	psli->sli_flag |= LPFC_PROCESS_LA;
3724 	if (phba->sli_rev <= LPFC_SLI_REV3) {
3725 		control = readl(phba->HCregaddr);
3726 		control |= HC_LAINT_ENA;
3727 		writel(control, phba->HCregaddr);
3728 		readl(phba->HCregaddr); /* flush */
3729 	}
3730 	spin_unlock_irq(&phba->hbalock);
3731 }
3732 
3733 static void
lpfc_mbx_issue_link_down(struct lpfc_hba * phba)3734 lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3735 {
3736 	lpfc_linkdown(phba);
3737 	lpfc_enable_la(phba);
3738 	lpfc_unregister_unused_fcf(phba);
3739 	/* turn on Link Attention interrupts - no CLEAR_LA needed */
3740 }
3741 
3742 
3743 /*
3744  * This routine handles processing a READ_TOPOLOGY mailbox
3745  * command upon completion. It is setup in the LPFC_MBOXQ
3746  * as the completion routine when the command is
3747  * handed off to the SLI layer. SLI4 only.
3748  */
3749 void
lpfc_mbx_cmpl_read_topology(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)3750 lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3751 {
3752 	struct lpfc_vport *vport = pmb->vport;
3753 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3754 	struct lpfc_mbx_read_top *la;
3755 	struct lpfc_sli_ring *pring;
3756 	MAILBOX_t *mb = &pmb->u.mb;
3757 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3758 	uint8_t attn_type;
3759 	unsigned long iflags;
3760 
3761 	/* Unblock ELS traffic */
3762 	pring = lpfc_phba_elsring(phba);
3763 	if (pring)
3764 		pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3765 
3766 	/* Check for error */
3767 	if (mb->mbxStatus) {
3768 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3769 				"1307 READ_LA mbox error x%x state x%x\n",
3770 				mb->mbxStatus, vport->port_state);
3771 		lpfc_mbx_issue_link_down(phba);
3772 		phba->link_state = LPFC_HBA_ERROR;
3773 		goto lpfc_mbx_cmpl_read_topology_free_mbuf;
3774 	}
3775 
3776 	la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3777 	attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
3778 
3779 	memcpy(&phba->alpa_map[0], mp->virt, 128);
3780 
3781 	spin_lock_irqsave(shost->host_lock, iflags);
3782 	if (bf_get(lpfc_mbx_read_top_pb, la))
3783 		vport->fc_flag |= FC_BYPASSED_MODE;
3784 	else
3785 		vport->fc_flag &= ~FC_BYPASSED_MODE;
3786 	spin_unlock_irqrestore(shost->host_lock, iflags);
3787 
3788 	if (phba->fc_eventTag <= la->eventTag) {
3789 		phba->fc_stat.LinkMultiEvent++;
3790 		if (attn_type == LPFC_ATT_LINK_UP)
3791 			if (phba->fc_eventTag != 0)
3792 				lpfc_linkdown(phba);
3793 	}
3794 
3795 	phba->fc_eventTag = la->eventTag;
3796 	phba->link_events++;
3797 	if (attn_type == LPFC_ATT_LINK_UP) {
3798 		phba->fc_stat.LinkUp++;
3799 		if (phba->link_flag & LS_LOOPBACK_MODE) {
3800 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3801 					"1306 Link Up Event in loop back mode "
3802 					"x%x received Data: x%x x%x x%x x%x\n",
3803 					la->eventTag, phba->fc_eventTag,
3804 					bf_get(lpfc_mbx_read_top_alpa_granted,
3805 					       la),
3806 					bf_get(lpfc_mbx_read_top_link_spd, la),
3807 					phba->alpa_map[0]);
3808 		} else {
3809 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3810 					"1303 Link Up Event x%x received "
3811 					"Data: x%x x%x x%x x%x x%x\n",
3812 					la->eventTag, phba->fc_eventTag,
3813 					bf_get(lpfc_mbx_read_top_alpa_granted,
3814 					       la),
3815 					bf_get(lpfc_mbx_read_top_link_spd, la),
3816 					phba->alpa_map[0],
3817 					bf_get(lpfc_mbx_read_top_fa, la));
3818 		}
3819 		lpfc_mbx_process_link_up(phba, la);
3820 
3821 		if (phba->cmf_active_mode != LPFC_CFG_OFF)
3822 			lpfc_cmf_signal_init(phba);
3823 
3824 		if (phba->lmt & LMT_64Gb)
3825 			lpfc_read_lds_params(phba);
3826 
3827 	} else if (attn_type == LPFC_ATT_LINK_DOWN ||
3828 		   attn_type == LPFC_ATT_UNEXP_WWPN) {
3829 		phba->fc_stat.LinkDown++;
3830 		if (phba->link_flag & LS_LOOPBACK_MODE)
3831 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3832 				"1308 Link Down Event in loop back mode "
3833 				"x%x received "
3834 				"Data: x%x x%x x%x\n",
3835 				la->eventTag, phba->fc_eventTag,
3836 				phba->pport->port_state, vport->fc_flag);
3837 		else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3838 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3839 				"1313 Link Down Unexpected FA WWPN Event x%x "
3840 				"received Data: x%x x%x x%x x%x\n",
3841 				la->eventTag, phba->fc_eventTag,
3842 				phba->pport->port_state, vport->fc_flag,
3843 				bf_get(lpfc_mbx_read_top_fa, la));
3844 		else
3845 			lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3846 				"1305 Link Down Event x%x received "
3847 				"Data: x%x x%x x%x x%x\n",
3848 				la->eventTag, phba->fc_eventTag,
3849 				phba->pport->port_state, vport->fc_flag,
3850 				bf_get(lpfc_mbx_read_top_fa, la));
3851 		lpfc_mbx_issue_link_down(phba);
3852 	}
3853 
3854 	if ((phba->sli_rev < LPFC_SLI_REV4) &&
3855 	    bf_get(lpfc_mbx_read_top_fa, la))
3856 		lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
3857 				"1311 fa %d\n",
3858 				bf_get(lpfc_mbx_read_top_fa, la));
3859 
3860 lpfc_mbx_cmpl_read_topology_free_mbuf:
3861 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3862 }
3863 
3864 /*
3865  * This routine handles processing a REG_LOGIN mailbox
3866  * command upon completion. It is setup in the LPFC_MBOXQ
3867  * as the completion routine when the command is
3868  * handed off to the SLI layer.
3869  */
3870 void
lpfc_mbx_cmpl_reg_login(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)3871 lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3872 {
3873 	struct lpfc_vport  *vport = pmb->vport;
3874 	struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
3875 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3876 
3877 	/* The driver calls the state machine with the pmb pointer
3878 	 * but wants to make sure a stale ctx_buf isn't acted on.
3879 	 * The ctx_buf is restored later and cleaned up.
3880 	 */
3881 	pmb->ctx_buf = NULL;
3882 	pmb->ctx_ndlp = NULL;
3883 
3884 	lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI | LOG_NODE | LOG_DISCOVERY,
3885 			 "0002 rpi:%x DID:%x flg:%x %d x%px\n",
3886 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
3887 			 kref_read(&ndlp->kref),
3888 			 ndlp);
3889 	if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3890 		ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3891 
3892 	if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3893 	    ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
3894 		/* We rcvd a rscn after issuing this
3895 		 * mbox reg login, we may have cycled
3896 		 * back through the state and be
3897 		 * back at reg login state so this
3898 		 * mbox needs to be ignored becase
3899 		 * there is another reg login in
3900 		 * process.
3901 		 */
3902 		spin_lock_irq(&ndlp->lock);
3903 		ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3904 		spin_unlock_irq(&ndlp->lock);
3905 
3906 		/*
3907 		 * We cannot leave the RPI registered because
3908 		 * if we go thru discovery again for this ndlp
3909 		 * a subsequent REG_RPI will fail.
3910 		 */
3911 		ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3912 		lpfc_unreg_rpi(vport, ndlp);
3913 	}
3914 
3915 	/* Call state machine */
3916 	lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
3917 	pmb->ctx_buf = mp;
3918 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
3919 
3920 	/* decrement the node reference count held for this callback
3921 	 * function.
3922 	 */
3923 	lpfc_nlp_put(ndlp);
3924 
3925 	return;
3926 }
3927 
3928 static void
lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)3929 lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3930 {
3931 	MAILBOX_t *mb = &pmb->u.mb;
3932 	struct lpfc_vport *vport = pmb->vport;
3933 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3934 
3935 	switch (mb->mbxStatus) {
3936 	case 0x0011:
3937 	case 0x0020:
3938 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3939 				 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3940 				 mb->mbxStatus);
3941 		break;
3942 	/* If VPI is busy, reset the HBA */
3943 	case 0x9700:
3944 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3945 			"2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3946 			vport->vpi, mb->mbxStatus);
3947 		if (!(phba->pport->load_flag & FC_UNLOADING))
3948 			lpfc_workq_post_event(phba, NULL, NULL,
3949 				LPFC_EVT_RESET_HBA);
3950 	}
3951 	spin_lock_irq(shost->host_lock);
3952 	vport->vpi_state &= ~LPFC_VPI_REGISTERED;
3953 	vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
3954 	spin_unlock_irq(shost->host_lock);
3955 	mempool_free(pmb, phba->mbox_mem_pool);
3956 	lpfc_cleanup_vports_rrqs(vport, NULL);
3957 	/*
3958 	 * This shost reference might have been taken at the beginning of
3959 	 * lpfc_vport_delete()
3960 	 */
3961 	if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
3962 		scsi_host_put(shost);
3963 }
3964 
3965 int
lpfc_mbx_unreg_vpi(struct lpfc_vport * vport)3966 lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3967 {
3968 	struct lpfc_hba  *phba = vport->phba;
3969 	LPFC_MBOXQ_t *mbox;
3970 	int rc;
3971 
3972 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3973 	if (!mbox)
3974 		return 1;
3975 
3976 	lpfc_unreg_vpi(phba, vport->vpi, mbox);
3977 	mbox->vport = vport;
3978 	mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
3979 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
3980 	if (rc == MBX_NOT_FINISHED) {
3981 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3982 				 "1800 Could not issue unreg_vpi\n");
3983 		mempool_free(mbox, phba->mbox_mem_pool);
3984 		return rc;
3985 	}
3986 	return 0;
3987 }
3988 
3989 static void
lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)3990 lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3991 {
3992 	struct lpfc_vport *vport = pmb->vport;
3993 	struct Scsi_Host  *shost = lpfc_shost_from_vport(vport);
3994 	MAILBOX_t *mb = &pmb->u.mb;
3995 
3996 	switch (mb->mbxStatus) {
3997 	case 0x0011:
3998 	case 0x9601:
3999 	case 0x9602:
4000 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4001 				 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
4002 				 mb->mbxStatus);
4003 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4004 		spin_lock_irq(shost->host_lock);
4005 		vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
4006 		spin_unlock_irq(shost->host_lock);
4007 		vport->fc_myDID = 0;
4008 
4009 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4010 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
4011 			if (phba->nvmet_support)
4012 				lpfc_nvmet_update_targetport(phba);
4013 			else
4014 				lpfc_nvme_update_localport(vport);
4015 		}
4016 		goto out;
4017 	}
4018 
4019 	spin_lock_irq(shost->host_lock);
4020 	vport->vpi_state |= LPFC_VPI_REGISTERED;
4021 	vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
4022 	spin_unlock_irq(shost->host_lock);
4023 	vport->num_disc_nodes = 0;
4024 	/* go thru NPR list and issue ELS PLOGIs */
4025 	if (vport->fc_npr_cnt)
4026 		lpfc_els_disc_plogi(vport);
4027 
4028 	if (!vport->num_disc_nodes) {
4029 		spin_lock_irq(shost->host_lock);
4030 		vport->fc_flag &= ~FC_NDISC_ACTIVE;
4031 		spin_unlock_irq(shost->host_lock);
4032 		lpfc_can_disctmo(vport);
4033 	}
4034 	vport->port_state = LPFC_VPORT_READY;
4035 
4036 out:
4037 	mempool_free(pmb, phba->mbox_mem_pool);
4038 	return;
4039 }
4040 
4041 /**
4042  * lpfc_create_static_vport - Read HBA config region to create static vports.
4043  * @phba: pointer to lpfc hba data structure.
4044  *
4045  * This routine issue a DUMP mailbox command for config region 22 to get
4046  * the list of static vports to be created. The function create vports
4047  * based on the information returned from the HBA.
4048  **/
4049 void
lpfc_create_static_vport(struct lpfc_hba * phba)4050 lpfc_create_static_vport(struct lpfc_hba *phba)
4051 {
4052 	LPFC_MBOXQ_t *pmb = NULL;
4053 	MAILBOX_t *mb;
4054 	struct static_vport_info *vport_info;
4055 	int mbx_wait_rc = 0, i;
4056 	struct fc_vport_identifiers vport_id;
4057 	struct fc_vport *new_fc_vport;
4058 	struct Scsi_Host *shost;
4059 	struct lpfc_vport *vport;
4060 	uint16_t offset = 0;
4061 	uint8_t *vport_buff;
4062 	struct lpfc_dmabuf *mp;
4063 	uint32_t byte_count = 0;
4064 
4065 	pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4066 	if (!pmb) {
4067 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4068 				"0542 lpfc_create_static_vport failed to"
4069 				" allocate mailbox memory\n");
4070 		return;
4071 	}
4072 	memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
4073 	mb = &pmb->u.mb;
4074 
4075 	vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
4076 	if (!vport_info) {
4077 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4078 				"0543 lpfc_create_static_vport failed to"
4079 				" allocate vport_info\n");
4080 		mempool_free(pmb, phba->mbox_mem_pool);
4081 		return;
4082 	}
4083 
4084 	vport_buff = (uint8_t *) vport_info;
4085 	do {
4086 		/* While loop iteration forces a free dma buffer from
4087 		 * the previous loop because the mbox is reused and
4088 		 * the dump routine is a single-use construct.
4089 		 */
4090 		if (pmb->ctx_buf) {
4091 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4092 			lpfc_mbuf_free(phba, mp->virt, mp->phys);
4093 			kfree(mp);
4094 			pmb->ctx_buf = NULL;
4095 		}
4096 		if (lpfc_dump_static_vport(phba, pmb, offset))
4097 			goto out;
4098 
4099 		pmb->vport = phba->pport;
4100 		mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
4101 							LPFC_MBOX_TMO);
4102 
4103 		if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
4104 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4105 				"0544 lpfc_create_static_vport failed to"
4106 				" issue dump mailbox command ret 0x%x "
4107 				"status 0x%x\n",
4108 				mbx_wait_rc, mb->mbxStatus);
4109 			goto out;
4110 		}
4111 
4112 		if (phba->sli_rev == LPFC_SLI_REV4) {
4113 			byte_count = pmb->u.mqe.un.mb_words[5];
4114 			mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
4115 			if (byte_count > sizeof(struct static_vport_info) -
4116 					offset)
4117 				byte_count = sizeof(struct static_vport_info)
4118 					- offset;
4119 			memcpy(vport_buff + offset, mp->virt, byte_count);
4120 			offset += byte_count;
4121 		} else {
4122 			if (mb->un.varDmp.word_cnt >
4123 				sizeof(struct static_vport_info) - offset)
4124 				mb->un.varDmp.word_cnt =
4125 					sizeof(struct static_vport_info)
4126 						- offset;
4127 			byte_count = mb->un.varDmp.word_cnt;
4128 			lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
4129 				vport_buff + offset,
4130 				byte_count);
4131 
4132 			offset += byte_count;
4133 		}
4134 
4135 	} while (byte_count &&
4136 		offset < sizeof(struct static_vport_info));
4137 
4138 
4139 	if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
4140 		((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
4141 			!= VPORT_INFO_REV)) {
4142 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
4143 				"0545 lpfc_create_static_vport bad"
4144 				" information header 0x%x 0x%x\n",
4145 				le32_to_cpu(vport_info->signature),
4146 				le32_to_cpu(vport_info->rev) &
4147 				VPORT_INFO_REV_MASK);
4148 
4149 		goto out;
4150 	}
4151 
4152 	shost = lpfc_shost_from_vport(phba->pport);
4153 
4154 	for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
4155 		memset(&vport_id, 0, sizeof(vport_id));
4156 		vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
4157 		vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
4158 		if (!vport_id.port_name || !vport_id.node_name)
4159 			continue;
4160 
4161 		vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
4162 		vport_id.vport_type = FC_PORTTYPE_NPIV;
4163 		vport_id.disable = false;
4164 		new_fc_vport = fc_vport_create(shost, 0, &vport_id);
4165 
4166 		if (!new_fc_vport) {
4167 			lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4168 				"0546 lpfc_create_static_vport failed to"
4169 				" create vport\n");
4170 			continue;
4171 		}
4172 
4173 		vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
4174 		vport->vport_flag |= STATIC_VPORT;
4175 	}
4176 
4177 out:
4178 	kfree(vport_info);
4179 	if (mbx_wait_rc != MBX_TIMEOUT)
4180 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4181 }
4182 
4183 /*
4184  * This routine handles processing a Fabric REG_LOGIN mailbox
4185  * command upon completion. It is setup in the LPFC_MBOXQ
4186  * as the completion routine when the command is
4187  * handed off to the SLI layer.
4188  */
4189 void
lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)4190 lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4191 {
4192 	struct lpfc_vport *vport = pmb->vport;
4193 	MAILBOX_t *mb = &pmb->u.mb;
4194 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4195 	struct Scsi_Host *shost;
4196 
4197 	pmb->ctx_ndlp = NULL;
4198 
4199 	if (mb->mbxStatus) {
4200 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4201 				 "0258 Register Fabric login error: 0x%x\n",
4202 				 mb->mbxStatus);
4203 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4204 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4205 			/* FLOGI failed, use loop map to make discovery list */
4206 			lpfc_disc_list_loopmap(vport);
4207 
4208 			/* Start discovery */
4209 			lpfc_disc_start(vport);
4210 			/* Decrement the reference count to ndlp after the
4211 			 * reference to the ndlp are done.
4212 			 */
4213 			lpfc_nlp_put(ndlp);
4214 			return;
4215 		}
4216 
4217 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4218 		/* Decrement the reference count to ndlp after the reference
4219 		 * to the ndlp are done.
4220 		 */
4221 		lpfc_nlp_put(ndlp);
4222 		return;
4223 	}
4224 
4225 	if (phba->sli_rev < LPFC_SLI_REV4)
4226 		ndlp->nlp_rpi = mb->un.varWords[0];
4227 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4228 	ndlp->nlp_type |= NLP_FABRIC;
4229 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4230 
4231 	if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
4232 		/* when physical port receive logo donot start
4233 		 * vport discovery */
4234 		if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
4235 			lpfc_start_fdiscs(phba);
4236 		else {
4237 			shost = lpfc_shost_from_vport(vport);
4238 			spin_lock_irq(shost->host_lock);
4239 			vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
4240 			spin_unlock_irq(shost->host_lock);
4241 		}
4242 		lpfc_do_scr_ns_plogi(phba, vport);
4243 	}
4244 
4245 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4246 
4247 	/* Drop the reference count from the mbox at the end after
4248 	 * all the current reference to the ndlp have been done.
4249 	 */
4250 	lpfc_nlp_put(ndlp);
4251 	return;
4252 }
4253 
4254  /*
4255   * This routine will issue a GID_FT for each FC4 Type supported
4256   * by the driver. ALL GID_FTs must complete before discovery is started.
4257   */
4258 int
lpfc_issue_gidft(struct lpfc_vport * vport)4259 lpfc_issue_gidft(struct lpfc_vport *vport)
4260 {
4261 	/* Good status, issue CT Request to NameServer */
4262 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4263 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
4264 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
4265 			/* Cannot issue NameServer FCP Query, so finish up
4266 			 * discovery
4267 			 */
4268 			lpfc_printf_vlog(vport, KERN_ERR,
4269 					 LOG_TRACE_EVENT,
4270 					 "0604 %s FC TYPE %x %s\n",
4271 					 "Failed to issue GID_FT to ",
4272 					 FC_TYPE_FCP,
4273 					 "Finishing discovery.");
4274 			return 0;
4275 		}
4276 		vport->gidft_inp++;
4277 	}
4278 
4279 	if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4280 	    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
4281 		if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
4282 			/* Cannot issue NameServer NVME Query, so finish up
4283 			 * discovery
4284 			 */
4285 			lpfc_printf_vlog(vport, KERN_ERR,
4286 					 LOG_TRACE_EVENT,
4287 					 "0605 %s FC_TYPE %x %s %d\n",
4288 					 "Failed to issue GID_FT to ",
4289 					 FC_TYPE_NVME,
4290 					 "Finishing discovery: gidftinp ",
4291 					 vport->gidft_inp);
4292 			if (vport->gidft_inp == 0)
4293 				return 0;
4294 		} else
4295 			vport->gidft_inp++;
4296 	}
4297 	return vport->gidft_inp;
4298 }
4299 
4300 /**
4301  * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
4302  * @vport: The virtual port for which this call is being executed.
4303  *
4304  * This routine will issue a GID_PT to get a list of all N_Ports
4305  *
4306  * Return value :
4307  *   0 - Failure to issue a GID_PT
4308  *   1 - GID_PT issued
4309  **/
4310 int
lpfc_issue_gidpt(struct lpfc_vport * vport)4311 lpfc_issue_gidpt(struct lpfc_vport *vport)
4312 {
4313 	/* Good status, issue CT Request to NameServer */
4314 	if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
4315 		/* Cannot issue NameServer FCP Query, so finish up
4316 		 * discovery
4317 		 */
4318 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4319 				 "0606 %s Port TYPE %x %s\n",
4320 				 "Failed to issue GID_PT to ",
4321 				 GID_PT_N_PORT,
4322 				 "Finishing discovery.");
4323 		return 0;
4324 	}
4325 	vport->gidft_inp++;
4326 	return 1;
4327 }
4328 
4329 /*
4330  * This routine handles processing a NameServer REG_LOGIN mailbox
4331  * command upon completion. It is setup in the LPFC_MBOXQ
4332  * as the completion routine when the command is
4333  * handed off to the SLI layer.
4334  */
4335 void
lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)4336 lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4337 {
4338 	MAILBOX_t *mb = &pmb->u.mb;
4339 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4340 	struct lpfc_vport *vport = pmb->vport;
4341 	int rc;
4342 
4343 	pmb->ctx_ndlp = NULL;
4344 	vport->gidft_inp = 0;
4345 
4346 	if (mb->mbxStatus) {
4347 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4348 				 "0260 Register NameServer error: 0x%x\n",
4349 				 mb->mbxStatus);
4350 
4351 out:
4352 		/* decrement the node reference count held for this
4353 		 * callback function.
4354 		 */
4355 		lpfc_nlp_put(ndlp);
4356 		lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4357 
4358 		/* If the node is not registered with the scsi or nvme
4359 		 * transport, remove the fabric node.  The failed reg_login
4360 		 * is terminal and forces the removal of the last node
4361 		 * reference.
4362 		 */
4363 		if (!(ndlp->fc4_xpt_flags & (SCSI_XPT_REGD | NVME_XPT_REGD))) {
4364 			spin_lock_irq(&ndlp->lock);
4365 			ndlp->nlp_flag &= ~NLP_NPR_2B_DISC;
4366 			spin_unlock_irq(&ndlp->lock);
4367 			lpfc_nlp_put(ndlp);
4368 		}
4369 
4370 		if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
4371 			/*
4372 			 * RegLogin failed, use loop map to make discovery
4373 			 * list
4374 			 */
4375 			lpfc_disc_list_loopmap(vport);
4376 
4377 			/* Start discovery */
4378 			lpfc_disc_start(vport);
4379 			return;
4380 		}
4381 		lpfc_vport_set_state(vport, FC_VPORT_FAILED);
4382 		return;
4383 	}
4384 
4385 	if (phba->sli_rev < LPFC_SLI_REV4)
4386 		ndlp->nlp_rpi = mb->un.varWords[0];
4387 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4388 	ndlp->nlp_type |= NLP_FABRIC;
4389 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4390 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
4391 			 "0003 rpi:%x DID:%x flg:%x %d x%px\n",
4392 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
4393 			 kref_read(&ndlp->kref),
4394 			 ndlp);
4395 
4396 	if (vport->port_state < LPFC_VPORT_READY) {
4397 		/* Link up discovery requires Fabric registration. */
4398 		lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4399 		lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4400 		lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4401 		lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
4402 
4403 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4404 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4405 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4406 
4407 		if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4408 		    (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
4409 			lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4410 				    FC_TYPE_NVME);
4411 
4412 		/* Issue SCR just before NameServer GID_FT Query */
4413 		lpfc_issue_els_scr(vport, 0);
4414 
4415 		/* Link was bounced or a Fabric LOGO occurred.  Start EDC
4416 		 * with initial FW values provided the congestion mode is
4417 		 * not off.  Note that signals may or may not be supported
4418 		 * by the adapter but FPIN is provided by default for 1
4419 		 * or both missing signals support.
4420 		 */
4421 		if (phba->cmf_active_mode != LPFC_CFG_OFF) {
4422 			phba->cgn_reg_fpin = phba->cgn_init_reg_fpin;
4423 			phba->cgn_reg_signal = phba->cgn_init_reg_signal;
4424 			rc = lpfc_issue_els_edc(vport, 0);
4425 			lpfc_printf_log(phba, KERN_INFO,
4426 					LOG_INIT | LOG_ELS | LOG_DISCOVERY,
4427 					"4220 Issue EDC status x%x Data x%x\n",
4428 					rc, phba->cgn_init_reg_signal);
4429 		} else if (phba->lmt & LMT_64Gb) {
4430 			/* may send link fault capability descriptor */
4431 			lpfc_issue_els_edc(vport, 0);
4432 		} else {
4433 			lpfc_issue_els_rdf(vport, 0);
4434 		}
4435 	}
4436 
4437 	vport->fc_ns_retry = 0;
4438 	if (lpfc_issue_gidft(vport) == 0)
4439 		goto out;
4440 
4441 	/*
4442 	 * At this point in time we may need to wait for multiple
4443 	 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4444 	 *
4445 	 * decrement the node reference count held for this
4446 	 * callback function.
4447 	 */
4448 	lpfc_nlp_put(ndlp);
4449 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4450 	return;
4451 }
4452 
4453 /*
4454  * This routine handles processing a Fabric Controller REG_LOGIN mailbox
4455  * command upon completion. It is setup in the LPFC_MBOXQ
4456  * as the completion routine when the command is handed off to the SLI layer.
4457  */
4458 void
lpfc_mbx_cmpl_fc_reg_login(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)4459 lpfc_mbx_cmpl_fc_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4460 {
4461 	struct lpfc_vport *vport = pmb->vport;
4462 	MAILBOX_t *mb = &pmb->u.mb;
4463 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
4464 
4465 	pmb->ctx_ndlp = NULL;
4466 	if (mb->mbxStatus) {
4467 		lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4468 				 "0933 %s: Register FC login error: 0x%x\n",
4469 				 __func__, mb->mbxStatus);
4470 		goto out;
4471 	}
4472 
4473 	lpfc_check_nlp_post_devloss(vport, ndlp);
4474 
4475 	if (phba->sli_rev < LPFC_SLI_REV4)
4476 		ndlp->nlp_rpi = mb->un.varWords[0];
4477 
4478 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4479 			 "0934 %s: Complete FC x%x RegLogin rpi x%x ste x%x\n",
4480 			 __func__, ndlp->nlp_DID, ndlp->nlp_rpi,
4481 			 ndlp->nlp_state);
4482 
4483 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
4484 	ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
4485 	ndlp->nlp_type |= NLP_FABRIC;
4486 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4487 
4488  out:
4489 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
4490 
4491 	/* Drop the reference count from the mbox at the end after
4492 	 * all the current reference to the ndlp have been done.
4493 	 */
4494 	lpfc_nlp_put(ndlp);
4495 }
4496 
4497 static void
lpfc_register_remote_port(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4498 lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4499 {
4500 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4501 	struct fc_rport  *rport;
4502 	struct lpfc_rport_data *rdata;
4503 	struct fc_rport_identifiers rport_ids;
4504 	struct lpfc_hba  *phba = vport->phba;
4505 	unsigned long flags;
4506 
4507 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4508 		return;
4509 
4510 	/* Remote port has reappeared. Re-register w/ FC transport */
4511 	rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4512 	rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
4513 	rport_ids.port_id = ndlp->nlp_DID;
4514 	rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
4515 
4516 
4517 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4518 			      "rport add:       did:x%x flg:x%x type x%x",
4519 			      ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4520 
4521 	/* Don't add the remote port if unloading. */
4522 	if (vport->load_flag & FC_UNLOADING)
4523 		return;
4524 
4525 	ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
4526 	if (!rport) {
4527 		dev_printk(KERN_WARNING, &phba->pcidev->dev,
4528 			   "Warning: fc_remote_port_add failed\n");
4529 		return;
4530 	}
4531 
4532 	/* Successful port add.  Complete initializing node data */
4533 	rport->maxframe_size = ndlp->nlp_maxframe;
4534 	rport->supported_classes = ndlp->nlp_class_sup;
4535 	rdata = rport->dd_data;
4536 	rdata->pnode = lpfc_nlp_get(ndlp);
4537 	if (!rdata->pnode) {
4538 		dev_warn(&phba->pcidev->dev,
4539 			 "Warning - node ref failed. Unreg rport\n");
4540 		fc_remote_port_delete(rport);
4541 		ndlp->rport = NULL;
4542 		return;
4543 	}
4544 
4545 	spin_lock_irqsave(&ndlp->lock, flags);
4546 	ndlp->fc4_xpt_flags |= SCSI_XPT_REGD;
4547 	spin_unlock_irqrestore(&ndlp->lock, flags);
4548 
4549 	if (ndlp->nlp_type & NLP_FCP_TARGET)
4550 		rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
4551 	if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4552 		rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4553 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4554 		rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4555 	if (ndlp->nlp_type & NLP_NVME_TARGET)
4556 		rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4557 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4558 		rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
4559 
4560 	if (rport_ids.roles !=  FC_RPORT_ROLE_UNKNOWN)
4561 		fc_remote_port_rolechg(rport, rport_ids.roles);
4562 
4563 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4564 			 "3183 %s rport x%px DID x%x, role x%x refcnt %d\n",
4565 			 __func__, rport, rport->port_id, rport->roles,
4566 			 kref_read(&ndlp->kref));
4567 
4568 	if ((rport->scsi_target_id != -1) &&
4569 	    (rport->scsi_target_id < LPFC_MAX_TARGET)) {
4570 		ndlp->nlp_sid = rport->scsi_target_id;
4571 	}
4572 
4573 	return;
4574 }
4575 
4576 static void
lpfc_unregister_remote_port(struct lpfc_nodelist * ndlp)4577 lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
4578 {
4579 	struct fc_rport *rport = ndlp->rport;
4580 	struct lpfc_vport *vport = ndlp->vport;
4581 
4582 	if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4583 		return;
4584 
4585 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4586 		"rport delete:    did:x%x flg:x%x type x%x",
4587 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4588 
4589 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4590 			 "3184 rport unregister x%06x, rport x%px "
4591 			 "xptflg x%x refcnt %d\n",
4592 			 ndlp->nlp_DID, rport, ndlp->fc4_xpt_flags,
4593 			 kref_read(&ndlp->kref));
4594 
4595 	fc_remote_port_delete(rport);
4596 	lpfc_nlp_put(ndlp);
4597 }
4598 
4599 static void
lpfc_nlp_counters(struct lpfc_vport * vport,int state,int count)4600 lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
4601 {
4602 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4603 	unsigned long iflags;
4604 
4605 	spin_lock_irqsave(shost->host_lock, iflags);
4606 	switch (state) {
4607 	case NLP_STE_UNUSED_NODE:
4608 		vport->fc_unused_cnt += count;
4609 		break;
4610 	case NLP_STE_PLOGI_ISSUE:
4611 		vport->fc_plogi_cnt += count;
4612 		break;
4613 	case NLP_STE_ADISC_ISSUE:
4614 		vport->fc_adisc_cnt += count;
4615 		break;
4616 	case NLP_STE_REG_LOGIN_ISSUE:
4617 		vport->fc_reglogin_cnt += count;
4618 		break;
4619 	case NLP_STE_PRLI_ISSUE:
4620 		vport->fc_prli_cnt += count;
4621 		break;
4622 	case NLP_STE_UNMAPPED_NODE:
4623 		vport->fc_unmap_cnt += count;
4624 		break;
4625 	case NLP_STE_MAPPED_NODE:
4626 		vport->fc_map_cnt += count;
4627 		break;
4628 	case NLP_STE_NPR_NODE:
4629 		if (vport->fc_npr_cnt == 0 && count == -1)
4630 			vport->fc_npr_cnt = 0;
4631 		else
4632 			vport->fc_npr_cnt += count;
4633 		break;
4634 	}
4635 	spin_unlock_irqrestore(shost->host_lock, iflags);
4636 }
4637 
4638 /* Register a node with backend if not already done */
4639 void
lpfc_nlp_reg_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4640 lpfc_nlp_reg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4641 {
4642 	unsigned long iflags;
4643 
4644 	lpfc_check_nlp_post_devloss(vport, ndlp);
4645 
4646 	spin_lock_irqsave(&ndlp->lock, iflags);
4647 	if (ndlp->fc4_xpt_flags & NLP_XPT_REGD) {
4648 		/* Already registered with backend, trigger rescan */
4649 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4650 
4651 		if (ndlp->fc4_xpt_flags & NVME_XPT_REGD &&
4652 		    ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_DISCOVERY)) {
4653 			lpfc_nvme_rescan_port(vport, ndlp);
4654 		}
4655 		return;
4656 	}
4657 
4658 	ndlp->fc4_xpt_flags |= NLP_XPT_REGD;
4659 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4660 
4661 	if (lpfc_valid_xpt_node(ndlp)) {
4662 		vport->phba->nport_event_cnt++;
4663 		/*
4664 		 * Tell the fc transport about the port, if we haven't
4665 		 * already. If we have, and it's a scsi entity, be
4666 		 */
4667 		lpfc_register_remote_port(vport, ndlp);
4668 	}
4669 
4670 	/* We are done if we do not have any NVME remote node */
4671 	if (!(ndlp->nlp_fc4_type & NLP_FC4_NVME))
4672 		return;
4673 
4674 	/* Notify the NVME transport of this new rport. */
4675 	if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4676 			ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4677 		if (vport->phba->nvmet_support == 0) {
4678 			/* Register this rport with the transport.
4679 			 * Only NVME Target Rports are registered with
4680 			 * the transport.
4681 			 */
4682 			if (ndlp->nlp_type & NLP_NVME_TARGET) {
4683 				vport->phba->nport_event_cnt++;
4684 				lpfc_nvme_register_port(vport, ndlp);
4685 			}
4686 		} else {
4687 			/* Just take an NDLP ref count since the
4688 			 * target does not register rports.
4689 			 */
4690 			lpfc_nlp_get(ndlp);
4691 		}
4692 	}
4693 }
4694 
4695 /* Unregister a node with backend if not already done */
4696 void
lpfc_nlp_unreg_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4697 lpfc_nlp_unreg_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4698 {
4699 	unsigned long iflags;
4700 
4701 	spin_lock_irqsave(&ndlp->lock, iflags);
4702 	if (!(ndlp->fc4_xpt_flags & NLP_XPT_REGD)) {
4703 		spin_unlock_irqrestore(&ndlp->lock, iflags);
4704 		lpfc_printf_vlog(vport, KERN_INFO,
4705 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
4706 				 "0999 %s Not regd: ndlp x%px rport x%px DID "
4707 				 "x%x FLG x%x XPT x%x\n",
4708 				  __func__, ndlp, ndlp->rport, ndlp->nlp_DID,
4709 				  ndlp->nlp_flag, ndlp->fc4_xpt_flags);
4710 		return;
4711 	}
4712 
4713 	ndlp->fc4_xpt_flags &= ~NLP_XPT_REGD;
4714 	spin_unlock_irqrestore(&ndlp->lock, iflags);
4715 
4716 	if (ndlp->rport &&
4717 	    ndlp->fc4_xpt_flags & SCSI_XPT_REGD) {
4718 		vport->phba->nport_event_cnt++;
4719 		lpfc_unregister_remote_port(ndlp);
4720 	} else if (!ndlp->rport) {
4721 		lpfc_printf_vlog(vport, KERN_INFO,
4722 				 LOG_ELS | LOG_NODE | LOG_DISCOVERY,
4723 				 "1999 %s NDLP in devloss x%px DID x%x FLG x%x"
4724 				 " XPT x%x refcnt %u\n",
4725 				 __func__, ndlp, ndlp->nlp_DID, ndlp->nlp_flag,
4726 				 ndlp->fc4_xpt_flags,
4727 				 kref_read(&ndlp->kref));
4728 	}
4729 
4730 	if (ndlp->fc4_xpt_flags & NVME_XPT_REGD) {
4731 		vport->phba->nport_event_cnt++;
4732 		if (vport->phba->nvmet_support == 0) {
4733 			/* Start devloss if target. */
4734 			if (ndlp->nlp_type & NLP_NVME_TARGET)
4735 				lpfc_nvme_unregister_port(vport, ndlp);
4736 		} else {
4737 			/* NVMET has no upcall. */
4738 			lpfc_nlp_put(ndlp);
4739 		}
4740 	}
4741 
4742 }
4743 
4744 /*
4745  * Adisc state change handling
4746  */
4747 static void
lpfc_handle_adisc_state(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,int new_state)4748 lpfc_handle_adisc_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4749 		int new_state)
4750 {
4751 	switch (new_state) {
4752 	/*
4753 	 * Any state to ADISC_ISSUE
4754 	 * Do nothing, adisc cmpl handling will trigger state changes
4755 	 */
4756 	case NLP_STE_ADISC_ISSUE:
4757 		break;
4758 
4759 	/*
4760 	 * ADISC_ISSUE to mapped states
4761 	 * Trigger a registration with backend, it will be nop if
4762 	 * already registered
4763 	 */
4764 	case NLP_STE_UNMAPPED_NODE:
4765 		ndlp->nlp_type |= NLP_FC_NODE;
4766 		fallthrough;
4767 	case NLP_STE_MAPPED_NODE:
4768 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4769 		lpfc_nlp_reg_node(vport, ndlp);
4770 		break;
4771 
4772 	/*
4773 	 * ADISC_ISSUE to non-mapped states
4774 	 * We are moving from ADISC_ISSUE to a non-mapped state because
4775 	 * ADISC failed, we would have skipped unregistering with
4776 	 * backend, attempt it now
4777 	 */
4778 	case NLP_STE_NPR_NODE:
4779 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4780 		fallthrough;
4781 	default:
4782 		lpfc_nlp_unreg_node(vport, ndlp);
4783 		break;
4784 	}
4785 
4786 }
4787 
4788 static void
lpfc_nlp_state_cleanup(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,int old_state,int new_state)4789 lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4790 		       int old_state, int new_state)
4791 {
4792 	/* Trap ADISC changes here */
4793 	if (new_state == NLP_STE_ADISC_ISSUE ||
4794 	    old_state == NLP_STE_ADISC_ISSUE) {
4795 		lpfc_handle_adisc_state(vport, ndlp, new_state);
4796 		return;
4797 	}
4798 
4799 	if (new_state == NLP_STE_UNMAPPED_NODE) {
4800 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4801 		ndlp->nlp_type |= NLP_FC_NODE;
4802 	}
4803 	if (new_state == NLP_STE_MAPPED_NODE)
4804 		ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4805 	if (new_state == NLP_STE_NPR_NODE)
4806 		ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
4807 
4808 	/* Reg/Unreg for FCP and NVME Transport interface */
4809 	if ((old_state == NLP_STE_MAPPED_NODE ||
4810 	     old_state == NLP_STE_UNMAPPED_NODE)) {
4811 		/* For nodes marked for ADISC, Handle unreg in ADISC cmpl
4812 		 * if linkup. In linkdown do unreg_node
4813 		 */
4814 		if (!(ndlp->nlp_flag & NLP_NPR_ADISC) ||
4815 		    !lpfc_is_link_up(vport->phba))
4816 			lpfc_nlp_unreg_node(vport, ndlp);
4817 	}
4818 
4819 	if (new_state ==  NLP_STE_MAPPED_NODE ||
4820 	    new_state == NLP_STE_UNMAPPED_NODE)
4821 		lpfc_nlp_reg_node(vport, ndlp);
4822 
4823 	/*
4824 	 * If the node just added to Mapped list was an FCP target,
4825 	 * but the remote port registration failed or assigned a target
4826 	 * id outside the presentable range - move the node to the
4827 	 * Unmapped List.
4828 	 */
4829 	if ((new_state == NLP_STE_MAPPED_NODE) &&
4830 	    (ndlp->nlp_type & NLP_FCP_TARGET) &&
4831 	    (!ndlp->rport ||
4832 	     ndlp->rport->scsi_target_id == -1 ||
4833 	     ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
4834 		spin_lock_irq(&ndlp->lock);
4835 		ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
4836 		spin_unlock_irq(&ndlp->lock);
4837 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
4838 	}
4839 }
4840 
4841 static char *
lpfc_nlp_state_name(char * buffer,size_t size,int state)4842 lpfc_nlp_state_name(char *buffer, size_t size, int state)
4843 {
4844 	static char *states[] = {
4845 		[NLP_STE_UNUSED_NODE] = "UNUSED",
4846 		[NLP_STE_PLOGI_ISSUE] = "PLOGI",
4847 		[NLP_STE_ADISC_ISSUE] = "ADISC",
4848 		[NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4849 		[NLP_STE_PRLI_ISSUE] = "PRLI",
4850 		[NLP_STE_LOGO_ISSUE] = "LOGO",
4851 		[NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4852 		[NLP_STE_MAPPED_NODE] = "MAPPED",
4853 		[NLP_STE_NPR_NODE] = "NPR",
4854 	};
4855 
4856 	if (state < NLP_STE_MAX_STATE && states[state])
4857 		strscpy(buffer, states[state], size);
4858 	else
4859 		snprintf(buffer, size, "unknown (%d)", state);
4860 	return buffer;
4861 }
4862 
4863 void
lpfc_nlp_set_state(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,int state)4864 lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4865 		   int state)
4866 {
4867 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4868 	int  old_state = ndlp->nlp_state;
4869 	int node_dropped = ndlp->nlp_flag & NLP_DROPPED;
4870 	char name1[16], name2[16];
4871 
4872 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4873 			 "0904 NPort state transition x%06x, %s -> %s\n",
4874 			 ndlp->nlp_DID,
4875 			 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4876 			 lpfc_nlp_state_name(name2, sizeof(name2), state));
4877 
4878 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4879 		"node statechg    did:x%x old:%d ste:%d",
4880 		ndlp->nlp_DID, old_state, state);
4881 
4882 	if (node_dropped && old_state == NLP_STE_UNUSED_NODE &&
4883 	    state != NLP_STE_UNUSED_NODE) {
4884 		ndlp->nlp_flag &= ~NLP_DROPPED;
4885 		lpfc_nlp_get(ndlp);
4886 	}
4887 
4888 	if (old_state == NLP_STE_NPR_NODE &&
4889 	    state != NLP_STE_NPR_NODE)
4890 		lpfc_cancel_retry_delay_tmo(vport, ndlp);
4891 	if (old_state == NLP_STE_UNMAPPED_NODE) {
4892 		ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4893 		ndlp->nlp_type &= ~NLP_FC_NODE;
4894 	}
4895 
4896 	if (list_empty(&ndlp->nlp_listp)) {
4897 		spin_lock_irq(shost->host_lock);
4898 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4899 		spin_unlock_irq(shost->host_lock);
4900 	} else if (old_state)
4901 		lpfc_nlp_counters(vport, old_state, -1);
4902 
4903 	ndlp->nlp_state = state;
4904 	lpfc_nlp_counters(vport, state, 1);
4905 	lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
4906 }
4907 
4908 void
lpfc_enqueue_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4909 lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4910 {
4911 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4912 
4913 	if (list_empty(&ndlp->nlp_listp)) {
4914 		spin_lock_irq(shost->host_lock);
4915 		list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4916 		spin_unlock_irq(shost->host_lock);
4917 	}
4918 }
4919 
4920 void
lpfc_dequeue_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4921 lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4922 {
4923 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4924 
4925 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
4926 	if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4927 		lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4928 	spin_lock_irq(shost->host_lock);
4929 	list_del_init(&ndlp->nlp_listp);
4930 	spin_unlock_irq(shost->host_lock);
4931 	lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4932 				NLP_STE_UNUSED_NODE);
4933 }
4934 
4935 /**
4936  * lpfc_initialize_node - Initialize all fields of node object
4937  * @vport: Pointer to Virtual Port object.
4938  * @ndlp: Pointer to FC node object.
4939  * @did: FC_ID of the node.
4940  *
4941  * This function is always called when node object need to be initialized.
4942  * It initializes all the fields of the node object. Although the reference
4943  * to phba from @ndlp can be obtained indirectly through it's reference to
4944  * @vport, a direct reference to phba is taken here by @ndlp. This is due
4945  * to the life-span of the @ndlp might go beyond the existence of @vport as
4946  * the final release of ndlp is determined by its reference count. And, the
4947  * operation on @ndlp needs the reference to phba.
4948  **/
4949 static inline void
lpfc_initialize_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint32_t did)4950 lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4951 	uint32_t did)
4952 {
4953 	INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4954 	INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4955 	timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
4956 	INIT_LIST_HEAD(&ndlp->recovery_evt.evt_listp);
4957 
4958 	ndlp->nlp_DID = did;
4959 	ndlp->vport = vport;
4960 	ndlp->phba = vport->phba;
4961 	ndlp->nlp_sid = NLP_NO_SID;
4962 	ndlp->nlp_fc4_type = NLP_FC4_NONE;
4963 	kref_init(&ndlp->kref);
4964 	atomic_set(&ndlp->cmd_pending, 0);
4965 	ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
4966 	ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4967 }
4968 
4969 void
lpfc_drop_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)4970 lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4971 {
4972 	/*
4973 	 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
4974 	 * be used when lpfc wants to remove the "last" lpfc_nlp_put() to
4975 	 * release the ndlp from the vport when conditions are correct.
4976 	 */
4977 	if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4978 		return;
4979 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
4980 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4981 		lpfc_cleanup_vports_rrqs(vport, ndlp);
4982 		lpfc_unreg_rpi(vport, ndlp);
4983 	}
4984 
4985 	/* NLP_DROPPED means another thread already removed the initial
4986 	 * reference from lpfc_nlp_init.  If set, don't drop it again and
4987 	 * introduce an imbalance.
4988 	 */
4989 	spin_lock_irq(&ndlp->lock);
4990 	if (!(ndlp->nlp_flag & NLP_DROPPED)) {
4991 		ndlp->nlp_flag |= NLP_DROPPED;
4992 		spin_unlock_irq(&ndlp->lock);
4993 		lpfc_nlp_put(ndlp);
4994 		return;
4995 	}
4996 	spin_unlock_irq(&ndlp->lock);
4997 }
4998 
4999 /*
5000  * Start / ReStart rescue timer for Discovery / RSCN handling
5001  */
5002 void
lpfc_set_disctmo(struct lpfc_vport * vport)5003 lpfc_set_disctmo(struct lpfc_vport *vport)
5004 {
5005 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5006 	struct lpfc_hba  *phba = vport->phba;
5007 	uint32_t tmo;
5008 
5009 	if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
5010 		/* For FAN, timeout should be greater than edtov */
5011 		tmo = (((phba->fc_edtov + 999) / 1000) + 1);
5012 	} else {
5013 		/* Normal discovery timeout should be > than ELS/CT timeout
5014 		 * FC spec states we need 3 * ratov for CT requests
5015 		 */
5016 		tmo = ((phba->fc_ratov * 3) + 3);
5017 	}
5018 
5019 
5020 	if (!timer_pending(&vport->fc_disctmo)) {
5021 		lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5022 			"set disc timer:  tmo:x%x state:x%x flg:x%x",
5023 			tmo, vport->port_state, vport->fc_flag);
5024 	}
5025 
5026 	mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
5027 	spin_lock_irq(shost->host_lock);
5028 	vport->fc_flag |= FC_DISC_TMO;
5029 	spin_unlock_irq(shost->host_lock);
5030 
5031 	/* Start Discovery Timer state <hba_state> */
5032 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5033 			 "0247 Start Discovery Timer state x%x "
5034 			 "Data: x%x x%lx x%x x%x\n",
5035 			 vport->port_state, tmo,
5036 			 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
5037 			 vport->fc_adisc_cnt);
5038 
5039 	return;
5040 }
5041 
5042 /*
5043  * Cancel rescue timer for Discovery / RSCN handling
5044  */
5045 int
lpfc_can_disctmo(struct lpfc_vport * vport)5046 lpfc_can_disctmo(struct lpfc_vport *vport)
5047 {
5048 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5049 	unsigned long iflags;
5050 
5051 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5052 		"can disc timer:  state:x%x rtry:x%x flg:x%x",
5053 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5054 
5055 	/* Turn off discovery timer if its running */
5056 	if (vport->fc_flag & FC_DISC_TMO ||
5057 	    timer_pending(&vport->fc_disctmo)) {
5058 		spin_lock_irqsave(shost->host_lock, iflags);
5059 		vport->fc_flag &= ~FC_DISC_TMO;
5060 		spin_unlock_irqrestore(shost->host_lock, iflags);
5061 		del_timer_sync(&vport->fc_disctmo);
5062 		spin_lock_irqsave(&vport->work_port_lock, iflags);
5063 		vport->work_port_events &= ~WORKER_DISC_TMO;
5064 		spin_unlock_irqrestore(&vport->work_port_lock, iflags);
5065 	}
5066 
5067 	/* Cancel Discovery Timer state <hba_state> */
5068 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5069 			 "0248 Cancel Discovery Timer state x%x "
5070 			 "Data: x%x x%x x%x\n",
5071 			 vport->port_state, vport->fc_flag,
5072 			 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
5073 	return 0;
5074 }
5075 
5076 /*
5077  * Check specified ring for outstanding IOCB on the SLI queue
5078  * Return true if iocb matches the specified nport
5079  */
5080 int
lpfc_check_sli_ndlp(struct lpfc_hba * phba,struct lpfc_sli_ring * pring,struct lpfc_iocbq * iocb,struct lpfc_nodelist * ndlp)5081 lpfc_check_sli_ndlp(struct lpfc_hba *phba,
5082 		    struct lpfc_sli_ring *pring,
5083 		    struct lpfc_iocbq *iocb,
5084 		    struct lpfc_nodelist *ndlp)
5085 {
5086 	struct lpfc_vport *vport = ndlp->vport;
5087 	u8 ulp_command;
5088 	u16 ulp_context;
5089 	u32 remote_id;
5090 
5091 	if (iocb->vport != vport)
5092 		return 0;
5093 
5094 	ulp_command = get_job_cmnd(phba, iocb);
5095 	ulp_context = get_job_ulpcontext(phba, iocb);
5096 	remote_id = get_job_els_rsp64_did(phba, iocb);
5097 
5098 	if (pring->ringno == LPFC_ELS_RING) {
5099 		switch (ulp_command) {
5100 		case CMD_GEN_REQUEST64_CR:
5101 			if (iocb->ndlp == ndlp)
5102 				return 1;
5103 			fallthrough;
5104 		case CMD_ELS_REQUEST64_CR:
5105 			if (remote_id == ndlp->nlp_DID)
5106 				return 1;
5107 			fallthrough;
5108 		case CMD_XMIT_ELS_RSP64_CX:
5109 			if (iocb->ndlp == ndlp)
5110 				return 1;
5111 		}
5112 	} else if (pring->ringno == LPFC_FCP_RING) {
5113 		/* Skip match check if waiting to relogin to FCP target */
5114 		if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
5115 		    (ndlp->nlp_flag & NLP_DELAY_TMO)) {
5116 			return 0;
5117 		}
5118 		if (ulp_context == ndlp->nlp_rpi)
5119 			return 1;
5120 	}
5121 	return 0;
5122 }
5123 
5124 static void
__lpfc_dequeue_nport_iocbs(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct lpfc_sli_ring * pring,struct list_head * dequeue_list)5125 __lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
5126 		struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
5127 		struct list_head *dequeue_list)
5128 {
5129 	struct lpfc_iocbq *iocb, *next_iocb;
5130 
5131 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5132 		/* Check to see if iocb matches the nport */
5133 		if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
5134 			/* match, dequeue */
5135 			list_move_tail(&iocb->list, dequeue_list);
5136 	}
5137 }
5138 
5139 static void
lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct list_head * dequeue_list)5140 lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
5141 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5142 {
5143 	struct lpfc_sli *psli = &phba->sli;
5144 	uint32_t i;
5145 
5146 	spin_lock_irq(&phba->hbalock);
5147 	for (i = 0; i < psli->num_rings; i++)
5148 		__lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
5149 						dequeue_list);
5150 	spin_unlock_irq(&phba->hbalock);
5151 }
5152 
5153 static void
lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp,struct list_head * dequeue_list)5154 lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
5155 		struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
5156 {
5157 	struct lpfc_sli_ring *pring;
5158 	struct lpfc_queue *qp = NULL;
5159 
5160 	spin_lock_irq(&phba->hbalock);
5161 	list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
5162 		pring = qp->pring;
5163 		if (!pring)
5164 			continue;
5165 		spin_lock(&pring->ring_lock);
5166 		__lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
5167 		spin_unlock(&pring->ring_lock);
5168 	}
5169 	spin_unlock_irq(&phba->hbalock);
5170 }
5171 
5172 /*
5173  * Free resources / clean up outstanding I/Os
5174  * associated with nlp_rpi in the LPFC_NODELIST entry.
5175  */
5176 static int
lpfc_no_rpi(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)5177 lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
5178 {
5179 	LIST_HEAD(completions);
5180 
5181 	lpfc_fabric_abort_nport(ndlp);
5182 
5183 	/*
5184 	 * Everything that matches on txcmplq will be returned
5185 	 * by firmware with a no rpi error.
5186 	 */
5187 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5188 		if (phba->sli_rev != LPFC_SLI_REV4)
5189 			lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
5190 		else
5191 			lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
5192 	}
5193 
5194 	/* Cancel all the IOCBs from the completions list */
5195 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5196 			      IOERR_SLI_ABORTED);
5197 
5198 	return 0;
5199 }
5200 
5201 /**
5202  * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
5203  * @phba: Pointer to HBA context object.
5204  * @pmb: Pointer to mailbox object.
5205  *
5206  * This function will issue an ELS LOGO command after completing
5207  * the UNREG_RPI.
5208  **/
5209 static void
lpfc_nlp_logo_unreg(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)5210 lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
5211 {
5212 	struct lpfc_vport  *vport = pmb->vport;
5213 	struct lpfc_nodelist *ndlp;
5214 
5215 	ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
5216 	if (!ndlp)
5217 		return;
5218 	lpfc_issue_els_logo(vport, ndlp, 0);
5219 
5220 	/* Check to see if there are any deferred events to process */
5221 	if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
5222 	    (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
5223 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5224 				 "1434 UNREG cmpl deferred logo x%x "
5225 				 "on NPort x%x Data: x%x x%px\n",
5226 				 ndlp->nlp_rpi, ndlp->nlp_DID,
5227 				 ndlp->nlp_defer_did, ndlp);
5228 
5229 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5230 		ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
5231 		lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
5232 	} else {
5233 		/* NLP_RELEASE_RPI is only set for SLI4 ports. */
5234 		if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
5235 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
5236 			spin_lock_irq(&ndlp->lock);
5237 			ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5238 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5239 			spin_unlock_irq(&ndlp->lock);
5240 		}
5241 		spin_lock_irq(&ndlp->lock);
5242 		ndlp->nlp_flag &= ~NLP_UNREG_INP;
5243 		spin_unlock_irq(&ndlp->lock);
5244 	}
5245 
5246 	/* The node has an outstanding reference for the unreg. Now
5247 	 * that the LOGO action and cleanup are finished, release
5248 	 * resources.
5249 	 */
5250 	lpfc_nlp_put(ndlp);
5251 	mempool_free(pmb, phba->mbox_mem_pool);
5252 }
5253 
5254 /*
5255  * Sets the mailbox completion handler to be used for the
5256  * unreg_rpi command. The handler varies based on the state of
5257  * the port and what will be happening to the rpi next.
5258  */
5259 static void
lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba * phba,struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,LPFC_MBOXQ_t * mbox)5260 lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
5261 	struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
5262 {
5263 	unsigned long iflags;
5264 
5265 	/* Driver always gets a reference on the mailbox job
5266 	 * in support of async jobs.
5267 	 */
5268 	mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
5269 	if (!mbox->ctx_ndlp)
5270 		return;
5271 
5272 	if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
5273 		mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
5274 
5275 	} else if (phba->sli_rev == LPFC_SLI_REV4 &&
5276 		   (!(vport->load_flag & FC_UNLOADING)) &&
5277 		    (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
5278 				      LPFC_SLI_INTF_IF_TYPE_2) &&
5279 		    (kref_read(&ndlp->kref) > 0)) {
5280 		mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
5281 	} else {
5282 		if (vport->load_flag & FC_UNLOADING) {
5283 			if (phba->sli_rev == LPFC_SLI_REV4) {
5284 				spin_lock_irqsave(&ndlp->lock, iflags);
5285 				ndlp->nlp_flag |= NLP_RELEASE_RPI;
5286 				spin_unlock_irqrestore(&ndlp->lock, iflags);
5287 			}
5288 		}
5289 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5290 	}
5291 }
5292 
5293 /*
5294  * Free rpi associated with LPFC_NODELIST entry.
5295  * This routine is called from lpfc_freenode(), when we are removing
5296  * a LPFC_NODELIST entry. It is also called if the driver initiates a
5297  * LOGO that completes successfully, and we are waiting to PLOGI back
5298  * to the remote NPort. In addition, it is called after we receive
5299  * and unsolicated ELS cmd, send back a rsp, the rsp completes and
5300  * we are waiting to PLOGI back to the remote NPort.
5301  */
5302 int
lpfc_unreg_rpi(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)5303 lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5304 {
5305 	struct lpfc_hba *phba = vport->phba;
5306 	LPFC_MBOXQ_t    *mbox;
5307 	int rc, acc_plogi = 1;
5308 	uint16_t rpi;
5309 
5310 	if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
5311 	    ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
5312 		if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
5313 			lpfc_printf_vlog(vport, KERN_INFO,
5314 					 LOG_NODE | LOG_DISCOVERY,
5315 					 "3366 RPI x%x needs to be "
5316 					 "unregistered nlp_flag x%x "
5317 					 "did x%x\n",
5318 					 ndlp->nlp_rpi, ndlp->nlp_flag,
5319 					 ndlp->nlp_DID);
5320 
5321 		/* If there is already an UNREG in progress for this ndlp,
5322 		 * no need to queue up another one.
5323 		 */
5324 		if (ndlp->nlp_flag & NLP_UNREG_INP) {
5325 			lpfc_printf_vlog(vport, KERN_INFO,
5326 					 LOG_NODE | LOG_DISCOVERY,
5327 					 "1436 unreg_rpi SKIP UNREG x%x on "
5328 					 "NPort x%x deferred x%x  flg x%x "
5329 					 "Data: x%px\n",
5330 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5331 					 ndlp->nlp_defer_did,
5332 					 ndlp->nlp_flag, ndlp);
5333 			goto out;
5334 		}
5335 
5336 		mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5337 		if (mbox) {
5338 			/* SLI4 ports require the physical rpi value. */
5339 			rpi = ndlp->nlp_rpi;
5340 			if (phba->sli_rev == LPFC_SLI_REV4)
5341 				rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
5342 
5343 			lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
5344 			mbox->vport = vport;
5345 			lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
5346 			if (!mbox->ctx_ndlp) {
5347 				mempool_free(mbox, phba->mbox_mem_pool);
5348 				return 1;
5349 			}
5350 
5351 			if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
5352 				/*
5353 				 * accept PLOGIs after unreg_rpi_cmpl
5354 				 */
5355 				acc_plogi = 0;
5356 			if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
5357 			    Fabric_DID_MASK) &&
5358 			    (!(vport->fc_flag & FC_OFFLINE_MODE)))
5359 				ndlp->nlp_flag |= NLP_UNREG_INP;
5360 
5361 			lpfc_printf_vlog(vport, KERN_INFO,
5362 					 LOG_NODE | LOG_DISCOVERY,
5363 					 "1433 unreg_rpi UNREG x%x on "
5364 					 "NPort x%x deferred flg x%x "
5365 					 "Data:x%px\n",
5366 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5367 					 ndlp->nlp_flag, ndlp);
5368 
5369 			rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5370 			if (rc == MBX_NOT_FINISHED) {
5371 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5372 				mempool_free(mbox, phba->mbox_mem_pool);
5373 				acc_plogi = 1;
5374 				lpfc_nlp_put(ndlp);
5375 			}
5376 		} else {
5377 			lpfc_printf_vlog(vport, KERN_INFO,
5378 					 LOG_NODE | LOG_DISCOVERY,
5379 					 "1444 Failed to allocate mempool "
5380 					 "unreg_rpi UNREG x%x, "
5381 					 "DID x%x, flag x%x, "
5382 					 "ndlp x%px\n",
5383 					 ndlp->nlp_rpi, ndlp->nlp_DID,
5384 					 ndlp->nlp_flag, ndlp);
5385 
5386 			/* Because mempool_alloc failed, we
5387 			 * will issue a LOGO here and keep the rpi alive if
5388 			 * not unloading.
5389 			 */
5390 			if (!(vport->load_flag & FC_UNLOADING)) {
5391 				ndlp->nlp_flag &= ~NLP_UNREG_INP;
5392 				lpfc_issue_els_logo(vport, ndlp, 0);
5393 				ndlp->nlp_prev_state = ndlp->nlp_state;
5394 				lpfc_nlp_set_state(vport, ndlp,
5395 						   NLP_STE_NPR_NODE);
5396 			}
5397 
5398 			return 1;
5399 		}
5400 		lpfc_no_rpi(phba, ndlp);
5401 out:
5402 		if (phba->sli_rev != LPFC_SLI_REV4)
5403 			ndlp->nlp_rpi = 0;
5404 		ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
5405 		ndlp->nlp_flag &= ~NLP_NPR_ADISC;
5406 		if (acc_plogi)
5407 			ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5408 		return 1;
5409 	}
5410 	ndlp->nlp_flag &= ~NLP_LOGO_ACC;
5411 	return 0;
5412 }
5413 
5414 /**
5415  * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
5416  * @phba: pointer to lpfc hba data structure.
5417  *
5418  * This routine is invoked to unregister all the currently registered RPIs
5419  * to the HBA.
5420  **/
5421 void
lpfc_unreg_hba_rpis(struct lpfc_hba * phba)5422 lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
5423 {
5424 	struct lpfc_vport **vports;
5425 	struct lpfc_nodelist *ndlp;
5426 	struct Scsi_Host *shost;
5427 	int i;
5428 
5429 	vports = lpfc_create_vport_work_array(phba);
5430 	if (!vports) {
5431 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5432 				"2884 Vport array allocation failed \n");
5433 		return;
5434 	}
5435 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
5436 		shost = lpfc_shost_from_vport(vports[i]);
5437 		spin_lock_irq(shost->host_lock);
5438 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
5439 			if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
5440 				/* The mempool_alloc might sleep */
5441 				spin_unlock_irq(shost->host_lock);
5442 				lpfc_unreg_rpi(vports[i], ndlp);
5443 				spin_lock_irq(shost->host_lock);
5444 			}
5445 		}
5446 		spin_unlock_irq(shost->host_lock);
5447 	}
5448 	lpfc_destroy_vport_work_array(phba, vports);
5449 }
5450 
5451 void
lpfc_unreg_all_rpis(struct lpfc_vport * vport)5452 lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5453 {
5454 	struct lpfc_hba  *phba  = vport->phba;
5455 	LPFC_MBOXQ_t     *mbox;
5456 	int rc;
5457 
5458 	if (phba->sli_rev == LPFC_SLI_REV4) {
5459 		lpfc_sli4_unreg_all_rpis(vport);
5460 		return;
5461 	}
5462 
5463 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5464 	if (mbox) {
5465 		lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5466 				 mbox);
5467 		mbox->vport = vport;
5468 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5469 		mbox->ctx_ndlp = NULL;
5470 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5471 		if (rc != MBX_TIMEOUT)
5472 			mempool_free(mbox, phba->mbox_mem_pool);
5473 
5474 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5475 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5476 					 "1836 Could not issue "
5477 					 "unreg_login(all_rpis) status %d\n",
5478 					 rc);
5479 	}
5480 }
5481 
5482 void
lpfc_unreg_default_rpis(struct lpfc_vport * vport)5483 lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5484 {
5485 	struct lpfc_hba  *phba  = vport->phba;
5486 	LPFC_MBOXQ_t     *mbox;
5487 	int rc;
5488 
5489 	/* Unreg DID is an SLI3 operation. */
5490 	if (phba->sli_rev > LPFC_SLI_REV3)
5491 		return;
5492 
5493 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5494 	if (mbox) {
5495 		lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5496 			       mbox);
5497 		mbox->vport = vport;
5498 		mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5499 		mbox->ctx_ndlp = NULL;
5500 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
5501 		if (rc != MBX_TIMEOUT)
5502 			mempool_free(mbox, phba->mbox_mem_pool);
5503 
5504 		if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5505 			lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5506 					 "1815 Could not issue "
5507 					 "unreg_did (default rpis) status %d\n",
5508 					 rc);
5509 	}
5510 }
5511 
5512 /*
5513  * Free resources associated with LPFC_NODELIST entry
5514  * so it can be freed.
5515  */
5516 static int
lpfc_cleanup_node(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp)5517 lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
5518 {
5519 	struct lpfc_hba  *phba = vport->phba;
5520 	LPFC_MBOXQ_t *mb, *nextmb;
5521 
5522 	/* Cleanup node for NPort <nlp_DID> */
5523 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5524 			 "0900 Cleanup node for NPort x%x "
5525 			 "Data: x%x x%x x%x\n",
5526 			 ndlp->nlp_DID, ndlp->nlp_flag,
5527 			 ndlp->nlp_state, ndlp->nlp_rpi);
5528 	lpfc_dequeue_node(vport, ndlp);
5529 
5530 	/* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5531 
5532 	/* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5533 	if ((mb = phba->sli.mbox_active)) {
5534 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5535 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5536 		   (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5537 			mb->ctx_ndlp = NULL;
5538 			mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5539 		}
5540 	}
5541 
5542 	spin_lock_irq(&phba->hbalock);
5543 	/* Cleanup REG_LOGIN completions which are not yet processed */
5544 	list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5545 		if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
5546 			(mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
5547 			(ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
5548 			continue;
5549 
5550 		mb->ctx_ndlp = NULL;
5551 		mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5552 	}
5553 
5554 	list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
5555 		if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
5556 		   !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
5557 		    (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5558 			list_del(&mb->list);
5559 			lpfc_mbox_rsrc_cleanup(phba, mb, MBOX_THD_LOCKED);
5560 
5561 			/* Don't invoke lpfc_nlp_put. The driver is in
5562 			 * lpfc_nlp_release context.
5563 			 */
5564 		}
5565 	}
5566 	spin_unlock_irq(&phba->hbalock);
5567 
5568 	lpfc_els_abort(phba, ndlp);
5569 
5570 	spin_lock_irq(&ndlp->lock);
5571 	ndlp->nlp_flag &= ~NLP_DELAY_TMO;
5572 	spin_unlock_irq(&ndlp->lock);
5573 
5574 	ndlp->nlp_last_elscmd = 0;
5575 	del_timer_sync(&ndlp->nlp_delayfunc);
5576 
5577 	list_del_init(&ndlp->els_retry_evt.evt_listp);
5578 	list_del_init(&ndlp->dev_loss_evt.evt_listp);
5579 	list_del_init(&ndlp->recovery_evt.evt_listp);
5580 	lpfc_cleanup_vports_rrqs(vport, ndlp);
5581 
5582 	if (phba->sli_rev == LPFC_SLI_REV4)
5583 		ndlp->nlp_flag |= NLP_RELEASE_RPI;
5584 
5585 	return 0;
5586 }
5587 
5588 static int
lpfc_matchdid(struct lpfc_vport * vport,struct lpfc_nodelist * ndlp,uint32_t did)5589 lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5590 	      uint32_t did)
5591 {
5592 	D_ID mydid, ndlpdid, matchdid;
5593 
5594 	if (did == Bcast_DID)
5595 		return 0;
5596 
5597 	/* First check for Direct match */
5598 	if (ndlp->nlp_DID == did)
5599 		return 1;
5600 
5601 	/* Next check for area/domain identically equals 0 match */
5602 	mydid.un.word = vport->fc_myDID;
5603 	if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
5604 		return 0;
5605 	}
5606 
5607 	matchdid.un.word = did;
5608 	ndlpdid.un.word = ndlp->nlp_DID;
5609 	if (matchdid.un.b.id == ndlpdid.un.b.id) {
5610 		if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5611 		    (mydid.un.b.area == matchdid.un.b.area)) {
5612 			/* This code is supposed to match the ID
5613 			 * for a private loop device that is
5614 			 * connect to fl_port. But we need to
5615 			 * check that the port did not just go
5616 			 * from pt2pt to fabric or we could end
5617 			 * up matching ndlp->nlp_DID 000001 to
5618 			 * fabric DID 0x20101
5619 			 */
5620 			if ((ndlpdid.un.b.domain == 0) &&
5621 			    (ndlpdid.un.b.area == 0)) {
5622 				if (ndlpdid.un.b.id &&
5623 				    vport->phba->fc_topology ==
5624 				    LPFC_TOPOLOGY_LOOP)
5625 					return 1;
5626 			}
5627 			return 0;
5628 		}
5629 
5630 		matchdid.un.word = ndlp->nlp_DID;
5631 		if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5632 		    (mydid.un.b.area == ndlpdid.un.b.area)) {
5633 			if ((matchdid.un.b.domain == 0) &&
5634 			    (matchdid.un.b.area == 0)) {
5635 				if (matchdid.un.b.id)
5636 					return 1;
5637 			}
5638 		}
5639 	}
5640 	return 0;
5641 }
5642 
5643 /* Search for a nodelist entry */
5644 static struct lpfc_nodelist *
__lpfc_findnode_did(struct lpfc_vport * vport,uint32_t did)5645 __lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5646 {
5647 	struct lpfc_nodelist *ndlp;
5648 	uint32_t data1;
5649 
5650 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5651 		if (lpfc_matchdid(vport, ndlp, did)) {
5652 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5653 				 ((uint32_t)ndlp->nlp_xri << 16) |
5654 				 ((uint32_t)ndlp->nlp_type << 8)
5655 				 );
5656 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5657 					 "0929 FIND node DID "
5658 					 "Data: x%px x%x x%x x%x x%x x%px\n",
5659 					 ndlp, ndlp->nlp_DID,
5660 					 ndlp->nlp_flag, data1, ndlp->nlp_rpi,
5661 					 ndlp->active_rrqs_xri_bitmap);
5662 			return ndlp;
5663 		}
5664 	}
5665 
5666 	/* FIND node did <did> NOT FOUND */
5667 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5668 			 "0932 FIND node did x%x NOT FOUND.\n", did);
5669 	return NULL;
5670 }
5671 
5672 struct lpfc_nodelist *
lpfc_findnode_did(struct lpfc_vport * vport,uint32_t did)5673 lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
5674 {
5675 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5676 	struct lpfc_nodelist *ndlp;
5677 	unsigned long iflags;
5678 
5679 	spin_lock_irqsave(shost->host_lock, iflags);
5680 	ndlp = __lpfc_findnode_did(vport, did);
5681 	spin_unlock_irqrestore(shost->host_lock, iflags);
5682 	return ndlp;
5683 }
5684 
5685 struct lpfc_nodelist *
lpfc_findnode_mapped(struct lpfc_vport * vport)5686 lpfc_findnode_mapped(struct lpfc_vport *vport)
5687 {
5688 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5689 	struct lpfc_nodelist *ndlp;
5690 	uint32_t data1;
5691 	unsigned long iflags;
5692 
5693 	spin_lock_irqsave(shost->host_lock, iflags);
5694 
5695 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5696 		if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5697 		    ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5698 			data1 = (((uint32_t)ndlp->nlp_state << 24) |
5699 				 ((uint32_t)ndlp->nlp_xri << 16) |
5700 				 ((uint32_t)ndlp->nlp_type << 8) |
5701 				 ((uint32_t)ndlp->nlp_rpi & 0xff));
5702 			spin_unlock_irqrestore(shost->host_lock, iflags);
5703 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5704 					 "2025 FIND node DID "
5705 					 "Data: x%px x%x x%x x%x x%px\n",
5706 					 ndlp, ndlp->nlp_DID,
5707 					 ndlp->nlp_flag, data1,
5708 					 ndlp->active_rrqs_xri_bitmap);
5709 			return ndlp;
5710 		}
5711 	}
5712 	spin_unlock_irqrestore(shost->host_lock, iflags);
5713 
5714 	/* FIND node did <did> NOT FOUND */
5715 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5716 			 "2026 FIND mapped did NOT FOUND.\n");
5717 	return NULL;
5718 }
5719 
5720 struct lpfc_nodelist *
lpfc_setup_disc_node(struct lpfc_vport * vport,uint32_t did)5721 lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5722 {
5723 	struct lpfc_nodelist *ndlp;
5724 
5725 	ndlp = lpfc_findnode_did(vport, did);
5726 	if (!ndlp) {
5727 		if (vport->phba->nvmet_support)
5728 			return NULL;
5729 		if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5730 		    lpfc_rscn_payload_check(vport, did) == 0)
5731 			return NULL;
5732 		ndlp = lpfc_nlp_init(vport, did);
5733 		if (!ndlp)
5734 			return NULL;
5735 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5736 
5737 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5738 				 "6453 Setup New Node 2B_DISC x%x "
5739 				 "Data:x%x x%x x%x\n",
5740 				 ndlp->nlp_DID, ndlp->nlp_flag,
5741 				 ndlp->nlp_state, vport->fc_flag);
5742 
5743 		spin_lock_irq(&ndlp->lock);
5744 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5745 		spin_unlock_irq(&ndlp->lock);
5746 		return ndlp;
5747 	}
5748 
5749 	/* The NVME Target does not want to actively manage an rport.
5750 	 * The goal is to allow the target to reset its state and clear
5751 	 * pending IO in preparation for the initiator to recover.
5752 	 */
5753 	if ((vport->fc_flag & FC_RSCN_MODE) &&
5754 	    !(vport->fc_flag & FC_NDISC_ACTIVE)) {
5755 		if (lpfc_rscn_payload_check(vport, did)) {
5756 
5757 			/* Since this node is marked for discovery,
5758 			 * delay timeout is not needed.
5759 			 */
5760 			lpfc_cancel_retry_delay_tmo(vport, ndlp);
5761 
5762 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5763 					 "6455 Setup RSCN Node 2B_DISC x%x "
5764 					 "Data:x%x x%x x%x\n",
5765 					 ndlp->nlp_DID, ndlp->nlp_flag,
5766 					 ndlp->nlp_state, vport->fc_flag);
5767 
5768 			/* NVME Target mode waits until rport is known to be
5769 			 * impacted by the RSCN before it transitions.  No
5770 			 * active management - just go to NPR provided the
5771 			 * node had a valid login.
5772 			 */
5773 			if (vport->phba->nvmet_support)
5774 				return ndlp;
5775 
5776 			/* If we've already received a PLOGI from this NPort
5777 			 * we don't need to try to discover it again.
5778 			 */
5779 			if (ndlp->nlp_flag & NLP_RCV_PLOGI &&
5780 			    !(ndlp->nlp_type &
5781 			     (NLP_FCP_TARGET | NLP_NVME_TARGET)))
5782 				return NULL;
5783 
5784 			if (ndlp->nlp_state > NLP_STE_UNUSED_NODE &&
5785 			    ndlp->nlp_state <= NLP_STE_PRLI_ISSUE) {
5786 				lpfc_disc_state_machine(vport, ndlp, NULL,
5787 							NLP_EVT_DEVICE_RECOVERY);
5788 			}
5789 
5790 			spin_lock_irq(&ndlp->lock);
5791 			ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5792 			spin_unlock_irq(&ndlp->lock);
5793 		} else {
5794 			lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5795 					 "6456 Skip Setup RSCN Node x%x "
5796 					 "Data:x%x x%x x%x\n",
5797 					 ndlp->nlp_DID, ndlp->nlp_flag,
5798 					 ndlp->nlp_state, vport->fc_flag);
5799 			ndlp = NULL;
5800 		}
5801 	} else {
5802 		lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5803 				 "6457 Setup Active Node 2B_DISC x%x "
5804 				 "Data:x%x x%x x%x\n",
5805 				 ndlp->nlp_DID, ndlp->nlp_flag,
5806 				 ndlp->nlp_state, vport->fc_flag);
5807 
5808 		/* If the initiator received a PLOGI from this NPort or if the
5809 		 * initiator is already in the process of discovery on it,
5810 		 * there's no need to try to discover it again.
5811 		 */
5812 		if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
5813 		    ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5814 		    (!vport->phba->nvmet_support &&
5815 		     ndlp->nlp_flag & NLP_RCV_PLOGI))
5816 			return NULL;
5817 
5818 		if (vport->phba->nvmet_support)
5819 			return ndlp;
5820 
5821 		/* Moving to NPR state clears unsolicited flags and
5822 		 * allows for rediscovery
5823 		 */
5824 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5825 
5826 		spin_lock_irq(&ndlp->lock);
5827 		ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5828 		spin_unlock_irq(&ndlp->lock);
5829 	}
5830 	return ndlp;
5831 }
5832 
5833 /* Build a list of nodes to discover based on the loopmap */
5834 void
lpfc_disc_list_loopmap(struct lpfc_vport * vport)5835 lpfc_disc_list_loopmap(struct lpfc_vport *vport)
5836 {
5837 	struct lpfc_hba  *phba = vport->phba;
5838 	int j;
5839 	uint32_t alpa, index;
5840 
5841 	if (!lpfc_is_link_up(phba))
5842 		return;
5843 
5844 	if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
5845 		return;
5846 
5847 	/* Check for loop map present or not */
5848 	if (phba->alpa_map[0]) {
5849 		for (j = 1; j <= phba->alpa_map[0]; j++) {
5850 			alpa = phba->alpa_map[j];
5851 			if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
5852 				continue;
5853 			lpfc_setup_disc_node(vport, alpa);
5854 		}
5855 	} else {
5856 		/* No alpamap, so try all alpa's */
5857 		for (j = 0; j < FC_MAXLOOP; j++) {
5858 			/* If cfg_scan_down is set, start from highest
5859 			 * ALPA (0xef) to lowest (0x1).
5860 			 */
5861 			if (vport->cfg_scan_down)
5862 				index = j;
5863 			else
5864 				index = FC_MAXLOOP - j - 1;
5865 			alpa = lpfcAlpaArray[index];
5866 			if ((vport->fc_myDID & 0xff) == alpa)
5867 				continue;
5868 			lpfc_setup_disc_node(vport, alpa);
5869 		}
5870 	}
5871 	return;
5872 }
5873 
5874 /* SLI3 only */
5875 void
lpfc_issue_clear_la(struct lpfc_hba * phba,struct lpfc_vport * vport)5876 lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
5877 {
5878 	LPFC_MBOXQ_t *mbox;
5879 	struct lpfc_sli *psli = &phba->sli;
5880 	struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5881 	struct lpfc_sli_ring *fcp_ring   = &psli->sli3_ring[LPFC_FCP_RING];
5882 	int  rc;
5883 
5884 	/*
5885 	 * if it's not a physical port or if we already send
5886 	 * clear_la then don't send it.
5887 	 */
5888 	if ((phba->link_state >= LPFC_CLEAR_LA) ||
5889 	    (vport->port_type != LPFC_PHYSICAL_PORT) ||
5890 		(phba->sli_rev == LPFC_SLI_REV4))
5891 		return;
5892 
5893 			/* Link up discovery */
5894 	if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5895 		phba->link_state = LPFC_CLEAR_LA;
5896 		lpfc_clear_la(phba, mbox);
5897 		mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5898 		mbox->vport = vport;
5899 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5900 		if (rc == MBX_NOT_FINISHED) {
5901 			mempool_free(mbox, phba->mbox_mem_pool);
5902 			lpfc_disc_flush_list(vport);
5903 			extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5904 			fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5905 			phba->link_state = LPFC_HBA_ERROR;
5906 		}
5907 	}
5908 }
5909 
5910 /* Reg_vpi to tell firmware to resume normal operations */
5911 void
lpfc_issue_reg_vpi(struct lpfc_hba * phba,struct lpfc_vport * vport)5912 lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5913 {
5914 	LPFC_MBOXQ_t *regvpimbox;
5915 
5916 	regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5917 	if (regvpimbox) {
5918 		lpfc_reg_vpi(vport, regvpimbox);
5919 		regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5920 		regvpimbox->vport = vport;
5921 		if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
5922 					== MBX_NOT_FINISHED) {
5923 			mempool_free(regvpimbox, phba->mbox_mem_pool);
5924 		}
5925 	}
5926 }
5927 
5928 /* Start Link up / RSCN discovery on NPR nodes */
5929 void
lpfc_disc_start(struct lpfc_vport * vport)5930 lpfc_disc_start(struct lpfc_vport *vport)
5931 {
5932 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5933 	struct lpfc_hba  *phba = vport->phba;
5934 	uint32_t num_sent;
5935 	uint32_t clear_la_pending;
5936 
5937 	if (!lpfc_is_link_up(phba)) {
5938 		lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5939 				 "3315 Link is not up %x\n",
5940 				 phba->link_state);
5941 		return;
5942 	}
5943 
5944 	if (phba->link_state == LPFC_CLEAR_LA)
5945 		clear_la_pending = 1;
5946 	else
5947 		clear_la_pending = 0;
5948 
5949 	if (vport->port_state < LPFC_VPORT_READY)
5950 		vport->port_state = LPFC_DISC_AUTH;
5951 
5952 	lpfc_set_disctmo(vport);
5953 
5954 	vport->fc_prevDID = vport->fc_myDID;
5955 	vport->num_disc_nodes = 0;
5956 
5957 	/* Start Discovery state <hba_state> */
5958 	lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5959 			 "0202 Start Discovery port state x%x "
5960 			 "flg x%x Data: x%x x%x x%x\n",
5961 			 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5962 			 vport->fc_adisc_cnt, vport->fc_npr_cnt);
5963 
5964 	/* First do ADISCs - if any */
5965 	num_sent = lpfc_els_disc_adisc(vport);
5966 
5967 	if (num_sent)
5968 		return;
5969 
5970 	/* Register the VPI for SLI3, NPIV only. */
5971 	if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
5972 	    !(vport->fc_flag & FC_PT2PT) &&
5973 	    !(vport->fc_flag & FC_RSCN_MODE) &&
5974 	    (phba->sli_rev < LPFC_SLI_REV4)) {
5975 		lpfc_issue_clear_la(phba, vport);
5976 		lpfc_issue_reg_vpi(phba, vport);
5977 		return;
5978 	}
5979 
5980 	/*
5981 	 * For SLI2, we need to set port_state to READY and continue
5982 	 * discovery.
5983 	 */
5984 	if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5985 		/* If we get here, there is nothing to ADISC */
5986 		lpfc_issue_clear_la(phba, vport);
5987 
5988 		if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
5989 			vport->num_disc_nodes = 0;
5990 			/* go thru NPR nodes and issue ELS PLOGIs */
5991 			if (vport->fc_npr_cnt)
5992 				lpfc_els_disc_plogi(vport);
5993 
5994 			if (!vport->num_disc_nodes) {
5995 				spin_lock_irq(shost->host_lock);
5996 				vport->fc_flag &= ~FC_NDISC_ACTIVE;
5997 				spin_unlock_irq(shost->host_lock);
5998 				lpfc_can_disctmo(vport);
5999 			}
6000 		}
6001 		vport->port_state = LPFC_VPORT_READY;
6002 	} else {
6003 		/* Next do PLOGIs - if any */
6004 		num_sent = lpfc_els_disc_plogi(vport);
6005 
6006 		if (num_sent)
6007 			return;
6008 
6009 		if (vport->fc_flag & FC_RSCN_MODE) {
6010 			/* Check to see if more RSCNs came in while we
6011 			 * were processing this one.
6012 			 */
6013 			if ((vport->fc_rscn_id_cnt == 0) &&
6014 			    (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
6015 				spin_lock_irq(shost->host_lock);
6016 				vport->fc_flag &= ~FC_RSCN_MODE;
6017 				spin_unlock_irq(shost->host_lock);
6018 				lpfc_can_disctmo(vport);
6019 			} else
6020 				lpfc_els_handle_rscn(vport);
6021 		}
6022 	}
6023 	return;
6024 }
6025 
6026 /*
6027  *  Ignore completion for all IOCBs on tx and txcmpl queue for ELS
6028  *  ring the match the sppecified nodelist.
6029  */
6030 static void
lpfc_free_tx(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)6031 lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
6032 {
6033 	LIST_HEAD(completions);
6034 	struct lpfc_iocbq    *iocb, *next_iocb;
6035 	struct lpfc_sli_ring *pring;
6036 	u32 ulp_command;
6037 
6038 	pring = lpfc_phba_elsring(phba);
6039 	if (unlikely(!pring))
6040 		return;
6041 
6042 	/* Error matching iocb on txq or txcmplq
6043 	 * First check the txq.
6044 	 */
6045 	spin_lock_irq(&phba->hbalock);
6046 	list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6047 		if (iocb->ndlp != ndlp)
6048 			continue;
6049 
6050 		ulp_command = get_job_cmnd(phba, iocb);
6051 
6052 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6053 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6054 
6055 			list_move_tail(&iocb->list, &completions);
6056 		}
6057 	}
6058 
6059 	/* Next check the txcmplq */
6060 	list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
6061 		if (iocb->ndlp != ndlp)
6062 			continue;
6063 
6064 		ulp_command = get_job_cmnd(phba, iocb);
6065 
6066 		if (ulp_command == CMD_ELS_REQUEST64_CR ||
6067 		    ulp_command == CMD_XMIT_ELS_RSP64_CX) {
6068 			lpfc_sli_issue_abort_iotag(phba, pring, iocb, NULL);
6069 		}
6070 	}
6071 	spin_unlock_irq(&phba->hbalock);
6072 
6073 	/* Make sure HBA is alive */
6074 	lpfc_issue_hb_tmo(phba);
6075 
6076 	/* Cancel all the IOCBs from the completions list */
6077 	lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6078 			      IOERR_SLI_ABORTED);
6079 }
6080 
6081 static void
lpfc_disc_flush_list(struct lpfc_vport * vport)6082 lpfc_disc_flush_list(struct lpfc_vport *vport)
6083 {
6084 	struct lpfc_nodelist *ndlp, *next_ndlp;
6085 	struct lpfc_hba *phba = vport->phba;
6086 
6087 	if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
6088 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6089 					 nlp_listp) {
6090 			if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
6091 			    ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
6092 				lpfc_free_tx(phba, ndlp);
6093 			}
6094 		}
6095 	}
6096 }
6097 
6098 /*
6099  * lpfc_notify_xport_npr - notifies xport of node disappearance
6100  * @vport: Pointer to Virtual Port object.
6101  *
6102  * Transitions all ndlps to NPR state.  When lpfc_nlp_set_state
6103  * calls lpfc_nlp_state_cleanup, the ndlp->rport is unregistered
6104  * and transport notified that the node is gone.
6105  * Return Code:
6106  *	none
6107  */
6108 static void
lpfc_notify_xport_npr(struct lpfc_vport * vport)6109 lpfc_notify_xport_npr(struct lpfc_vport *vport)
6110 {
6111 	struct lpfc_nodelist *ndlp, *next_ndlp;
6112 
6113 	list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6114 				 nlp_listp) {
6115 		lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
6116 	}
6117 }
6118 void
lpfc_cleanup_discovery_resources(struct lpfc_vport * vport)6119 lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
6120 {
6121 	lpfc_els_flush_rscn(vport);
6122 	lpfc_els_flush_cmd(vport);
6123 	lpfc_disc_flush_list(vport);
6124 	if (pci_channel_offline(vport->phba->pcidev))
6125 		lpfc_notify_xport_npr(vport);
6126 }
6127 
6128 /*****************************************************************************/
6129 /*
6130  * NAME:     lpfc_disc_timeout
6131  *
6132  * FUNCTION: Fibre Channel driver discovery timeout routine.
6133  *
6134  * EXECUTION ENVIRONMENT: interrupt only
6135  *
6136  * CALLED FROM:
6137  *      Timer function
6138  *
6139  * RETURNS:
6140  *      none
6141  */
6142 /*****************************************************************************/
6143 void
lpfc_disc_timeout(struct timer_list * t)6144 lpfc_disc_timeout(struct timer_list *t)
6145 {
6146 	struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
6147 	struct lpfc_hba   *phba = vport->phba;
6148 	uint32_t tmo_posted;
6149 	unsigned long flags = 0;
6150 
6151 	if (unlikely(!phba))
6152 		return;
6153 
6154 	spin_lock_irqsave(&vport->work_port_lock, flags);
6155 	tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
6156 	if (!tmo_posted)
6157 		vport->work_port_events |= WORKER_DISC_TMO;
6158 	spin_unlock_irqrestore(&vport->work_port_lock, flags);
6159 
6160 	if (!tmo_posted)
6161 		lpfc_worker_wake_up(phba);
6162 	return;
6163 }
6164 
6165 static void
lpfc_disc_timeout_handler(struct lpfc_vport * vport)6166 lpfc_disc_timeout_handler(struct lpfc_vport *vport)
6167 {
6168 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6169 	struct lpfc_hba  *phba = vport->phba;
6170 	struct lpfc_sli  *psli = &phba->sli;
6171 	struct lpfc_nodelist *ndlp, *next_ndlp;
6172 	LPFC_MBOXQ_t *initlinkmbox;
6173 	int rc, clrlaerr = 0;
6174 
6175 	if (!(vport->fc_flag & FC_DISC_TMO))
6176 		return;
6177 
6178 	spin_lock_irq(shost->host_lock);
6179 	vport->fc_flag &= ~FC_DISC_TMO;
6180 	spin_unlock_irq(shost->host_lock);
6181 
6182 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
6183 		"disc timeout:    state:x%x rtry:x%x flg:x%x",
6184 		vport->port_state, vport->fc_ns_retry, vport->fc_flag);
6185 
6186 	switch (vport->port_state) {
6187 
6188 	case LPFC_LOCAL_CFG_LINK:
6189 		/*
6190 		 * port_state is identically  LPFC_LOCAL_CFG_LINK while
6191 		 * waiting for FAN timeout
6192 		 */
6193 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
6194 				 "0221 FAN timeout\n");
6195 
6196 		/* Start discovery by sending FLOGI, clean up old rpis */
6197 		list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
6198 					 nlp_listp) {
6199 			if (ndlp->nlp_state != NLP_STE_NPR_NODE)
6200 				continue;
6201 			if (ndlp->nlp_type & NLP_FABRIC) {
6202 				/* Clean up the ndlp on Fabric connections */
6203 				lpfc_drop_node(vport, ndlp);
6204 
6205 			} else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
6206 				/* Fail outstanding IO now since device
6207 				 * is marked for PLOGI.
6208 				 */
6209 				lpfc_unreg_rpi(vport, ndlp);
6210 			}
6211 		}
6212 		if (vport->port_state != LPFC_FLOGI) {
6213 			if (phba->sli_rev <= LPFC_SLI_REV3)
6214 				lpfc_initial_flogi(vport);
6215 			else
6216 				lpfc_issue_init_vfi(vport);
6217 			return;
6218 		}
6219 		break;
6220 
6221 	case LPFC_FDISC:
6222 	case LPFC_FLOGI:
6223 	/* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
6224 		/* Initial FLOGI timeout */
6225 		lpfc_printf_vlog(vport, KERN_ERR,
6226 				 LOG_TRACE_EVENT,
6227 				 "0222 Initial %s timeout\n",
6228 				 vport->vpi ? "FDISC" : "FLOGI");
6229 
6230 		/* Assume no Fabric and go on with discovery.
6231 		 * Check for outstanding ELS FLOGI to abort.
6232 		 */
6233 
6234 		/* FLOGI failed, so just use loop map to make discovery list */
6235 		lpfc_disc_list_loopmap(vport);
6236 
6237 		/* Start discovery */
6238 		lpfc_disc_start(vport);
6239 		break;
6240 
6241 	case LPFC_FABRIC_CFG_LINK:
6242 	/* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
6243 	   NameServer login */
6244 		lpfc_printf_vlog(vport, KERN_ERR,
6245 				 LOG_TRACE_EVENT,
6246 				 "0223 Timeout while waiting for "
6247 				 "NameServer login\n");
6248 		/* Next look for NameServer ndlp */
6249 		ndlp = lpfc_findnode_did(vport, NameServer_DID);
6250 		if (ndlp)
6251 			lpfc_els_abort(phba, ndlp);
6252 
6253 		/* ReStart discovery */
6254 		goto restart_disc;
6255 
6256 	case LPFC_NS_QRY:
6257 	/* Check for wait for NameServer Rsp timeout */
6258 		lpfc_printf_vlog(vport, KERN_ERR,
6259 				 LOG_TRACE_EVENT,
6260 				 "0224 NameServer Query timeout "
6261 				 "Data: x%x x%x\n",
6262 				 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
6263 
6264 		if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
6265 			/* Try it one more time */
6266 			vport->fc_ns_retry++;
6267 			vport->gidft_inp = 0;
6268 			rc = lpfc_issue_gidft(vport);
6269 			if (rc == 0)
6270 				break;
6271 		}
6272 		vport->fc_ns_retry = 0;
6273 
6274 restart_disc:
6275 		/*
6276 		 * Discovery is over.
6277 		 * set port_state to PORT_READY if SLI2.
6278 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6279 		 */
6280 		if (phba->sli_rev < LPFC_SLI_REV4) {
6281 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6282 				lpfc_issue_reg_vpi(phba, vport);
6283 			else  {
6284 				lpfc_issue_clear_la(phba, vport);
6285 				vport->port_state = LPFC_VPORT_READY;
6286 			}
6287 		}
6288 
6289 		/* Setup and issue mailbox INITIALIZE LINK command */
6290 		initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6291 		if (!initlinkmbox) {
6292 			lpfc_printf_vlog(vport, KERN_ERR,
6293 					 LOG_TRACE_EVENT,
6294 					 "0206 Device Discovery "
6295 					 "completion error\n");
6296 			phba->link_state = LPFC_HBA_ERROR;
6297 			break;
6298 		}
6299 
6300 		lpfc_linkdown(phba);
6301 		lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
6302 			       phba->cfg_link_speed);
6303 		initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
6304 		initlinkmbox->vport = vport;
6305 		initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
6306 		rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
6307 		lpfc_set_loopback_flag(phba);
6308 		if (rc == MBX_NOT_FINISHED)
6309 			mempool_free(initlinkmbox, phba->mbox_mem_pool);
6310 
6311 		break;
6312 
6313 	case LPFC_DISC_AUTH:
6314 	/* Node Authentication timeout */
6315 		lpfc_printf_vlog(vport, KERN_ERR,
6316 				 LOG_TRACE_EVENT,
6317 				 "0227 Node Authentication timeout\n");
6318 		lpfc_disc_flush_list(vport);
6319 
6320 		/*
6321 		 * set port_state to PORT_READY if SLI2.
6322 		 * cmpl_reg_vpi will set port_state to READY for SLI3.
6323 		 */
6324 		if (phba->sli_rev < LPFC_SLI_REV4) {
6325 			if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
6326 				lpfc_issue_reg_vpi(phba, vport);
6327 			else  {	/* NPIV Not enabled */
6328 				lpfc_issue_clear_la(phba, vport);
6329 				vport->port_state = LPFC_VPORT_READY;
6330 			}
6331 		}
6332 		break;
6333 
6334 	case LPFC_VPORT_READY:
6335 		if (vport->fc_flag & FC_RSCN_MODE) {
6336 			lpfc_printf_vlog(vport, KERN_ERR,
6337 					 LOG_TRACE_EVENT,
6338 					 "0231 RSCN timeout Data: x%x "
6339 					 "x%x x%x x%x\n",
6340 					 vport->fc_ns_retry, LPFC_MAX_NS_RETRY,
6341 					 vport->port_state, vport->gidft_inp);
6342 
6343 			/* Cleanup any outstanding ELS commands */
6344 			lpfc_els_flush_cmd(vport);
6345 
6346 			lpfc_els_flush_rscn(vport);
6347 			lpfc_disc_flush_list(vport);
6348 		}
6349 		break;
6350 
6351 	default:
6352 		lpfc_printf_vlog(vport, KERN_ERR,
6353 				 LOG_TRACE_EVENT,
6354 				 "0273 Unexpected discovery timeout, "
6355 				 "vport State x%x\n", vport->port_state);
6356 		break;
6357 	}
6358 
6359 	switch (phba->link_state) {
6360 	case LPFC_CLEAR_LA:
6361 				/* CLEAR LA timeout */
6362 		lpfc_printf_vlog(vport, KERN_ERR,
6363 				 LOG_TRACE_EVENT,
6364 				 "0228 CLEAR LA timeout\n");
6365 		clrlaerr = 1;
6366 		break;
6367 
6368 	case LPFC_LINK_UP:
6369 		lpfc_issue_clear_la(phba, vport);
6370 		fallthrough;
6371 	case LPFC_LINK_UNKNOWN:
6372 	case LPFC_WARM_START:
6373 	case LPFC_INIT_START:
6374 	case LPFC_INIT_MBX_CMDS:
6375 	case LPFC_LINK_DOWN:
6376 	case LPFC_HBA_ERROR:
6377 		lpfc_printf_vlog(vport, KERN_ERR,
6378 				 LOG_TRACE_EVENT,
6379 				 "0230 Unexpected timeout, hba link "
6380 				 "state x%x\n", phba->link_state);
6381 		clrlaerr = 1;
6382 		break;
6383 
6384 	case LPFC_HBA_READY:
6385 		break;
6386 	}
6387 
6388 	if (clrlaerr) {
6389 		lpfc_disc_flush_list(vport);
6390 		if (phba->sli_rev != LPFC_SLI_REV4) {
6391 			psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
6392 				~LPFC_STOP_IOCB_EVENT;
6393 			psli->sli3_ring[LPFC_FCP_RING].flag &=
6394 				~LPFC_STOP_IOCB_EVENT;
6395 		}
6396 		vport->port_state = LPFC_VPORT_READY;
6397 	}
6398 	return;
6399 }
6400 
6401 /*
6402  * This routine handles processing a NameServer REG_LOGIN mailbox
6403  * command upon completion. It is setup in the LPFC_MBOXQ
6404  * as the completion routine when the command is
6405  * handed off to the SLI layer.
6406  */
6407 void
lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba * phba,LPFC_MBOXQ_t * pmb)6408 lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
6409 {
6410 	MAILBOX_t *mb = &pmb->u.mb;
6411 	struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
6412 	struct lpfc_vport    *vport = pmb->vport;
6413 
6414 	pmb->ctx_ndlp = NULL;
6415 
6416 	if (phba->sli_rev < LPFC_SLI_REV4)
6417 		ndlp->nlp_rpi = mb->un.varWords[0];
6418 	ndlp->nlp_flag |= NLP_RPI_REGISTERED;
6419 	ndlp->nlp_type |= NLP_FABRIC;
6420 	lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
6421 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6422 			 "0004 rpi:%x DID:%x flg:%x %d x%px\n",
6423 			 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
6424 			 kref_read(&ndlp->kref),
6425 			 ndlp);
6426 	/*
6427 	 * Start issuing Fabric-Device Management Interface (FDMI) command to
6428 	 * 0xfffffa (FDMI well known port).
6429 	 * DHBA -> DPRT -> RHBA -> RPA  (physical port)
6430 	 * DPRT -> RPRT (vports)
6431 	 */
6432 	if (vport->port_type == LPFC_PHYSICAL_PORT) {
6433 		phba->link_flag &= ~LS_CT_VEN_RPA; /* For extra Vendor RPA */
6434 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
6435 	} else {
6436 		lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6437 	}
6438 
6439 
6440 	/* decrement the node reference count held for this callback
6441 	 * function.
6442 	 */
6443 	lpfc_nlp_put(ndlp);
6444 	lpfc_mbox_rsrc_cleanup(phba, pmb, MBOX_THD_UNLOCKED);
6445 	return;
6446 }
6447 
6448 static int
lpfc_filter_by_rpi(struct lpfc_nodelist * ndlp,void * param)6449 lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6450 {
6451 	uint16_t *rpi = param;
6452 
6453 	return ndlp->nlp_rpi == *rpi;
6454 }
6455 
6456 static int
lpfc_filter_by_wwpn(struct lpfc_nodelist * ndlp,void * param)6457 lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6458 {
6459 	return memcmp(&ndlp->nlp_portname, param,
6460 		      sizeof(ndlp->nlp_portname)) == 0;
6461 }
6462 
6463 static struct lpfc_nodelist *
__lpfc_find_node(struct lpfc_vport * vport,node_filter filter,void * param)6464 __lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
6465 {
6466 	struct lpfc_nodelist *ndlp;
6467 
6468 	list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
6469 		if (filter(ndlp, param)) {
6470 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6471 					 "3185 FIND node filter %ps DID "
6472 					 "ndlp x%px did x%x flg x%x st x%x "
6473 					 "xri x%x type x%x rpi x%x\n",
6474 					 filter, ndlp, ndlp->nlp_DID,
6475 					 ndlp->nlp_flag, ndlp->nlp_state,
6476 					 ndlp->nlp_xri, ndlp->nlp_type,
6477 					 ndlp->nlp_rpi);
6478 			return ndlp;
6479 		}
6480 	}
6481 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6482 			 "3186 FIND node filter %ps NOT FOUND.\n", filter);
6483 	return NULL;
6484 }
6485 
6486 /*
6487  * This routine looks up the ndlp lists for the given RPI. If rpi found it
6488  * returns the node list element pointer else return NULL.
6489  */
6490 struct lpfc_nodelist *
__lpfc_findnode_rpi(struct lpfc_vport * vport,uint16_t rpi)6491 __lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6492 {
6493 	return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
6494 }
6495 
6496 /*
6497  * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
6498  * returns the node element list pointer else return NULL.
6499  */
6500 struct lpfc_nodelist *
lpfc_findnode_wwpn(struct lpfc_vport * vport,struct lpfc_name * wwpn)6501 lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
6502 {
6503 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6504 	struct lpfc_nodelist *ndlp;
6505 
6506 	spin_lock_irq(shost->host_lock);
6507 	ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6508 	spin_unlock_irq(shost->host_lock);
6509 	return ndlp;
6510 }
6511 
6512 /*
6513  * This routine looks up the ndlp lists for the given RPI. If the rpi
6514  * is found, the routine returns the node element list pointer else
6515  * return NULL.
6516  */
6517 struct lpfc_nodelist *
lpfc_findnode_rpi(struct lpfc_vport * vport,uint16_t rpi)6518 lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6519 {
6520 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6521 	struct lpfc_nodelist *ndlp;
6522 	unsigned long flags;
6523 
6524 	spin_lock_irqsave(shost->host_lock, flags);
6525 	ndlp = __lpfc_findnode_rpi(vport, rpi);
6526 	spin_unlock_irqrestore(shost->host_lock, flags);
6527 	return ndlp;
6528 }
6529 
6530 /**
6531  * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6532  * @phba: pointer to lpfc hba data structure.
6533  * @vpi: the physical host virtual N_Port identifier.
6534  *
6535  * This routine finds a vport on a HBA (referred by @phba) through a
6536  * @vpi. The function walks the HBA's vport list and returns the address
6537  * of the vport with the matching @vpi.
6538  *
6539  * Return code
6540  *    NULL - No vport with the matching @vpi found
6541  *    Otherwise - Address to the vport with the matching @vpi.
6542  **/
6543 struct lpfc_vport *
lpfc_find_vport_by_vpid(struct lpfc_hba * phba,uint16_t vpi)6544 lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6545 {
6546 	struct lpfc_vport *vport;
6547 	unsigned long flags;
6548 	int i = 0;
6549 
6550 	/* The physical ports are always vpi 0 - translate is unnecessary. */
6551 	if (vpi > 0) {
6552 		/*
6553 		 * Translate the physical vpi to the logical vpi.  The
6554 		 * vport stores the logical vpi.
6555 		 */
6556 		for (i = 0; i <= phba->max_vpi; i++) {
6557 			if (vpi == phba->vpi_ids[i])
6558 				break;
6559 		}
6560 
6561 		if (i > phba->max_vpi) {
6562 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6563 					"2936 Could not find Vport mapped "
6564 					"to vpi %d\n", vpi);
6565 			return NULL;
6566 		}
6567 	}
6568 
6569 	spin_lock_irqsave(&phba->port_list_lock, flags);
6570 	list_for_each_entry(vport, &phba->port_list, listentry) {
6571 		if (vport->vpi == i) {
6572 			spin_unlock_irqrestore(&phba->port_list_lock, flags);
6573 			return vport;
6574 		}
6575 	}
6576 	spin_unlock_irqrestore(&phba->port_list_lock, flags);
6577 	return NULL;
6578 }
6579 
6580 struct lpfc_nodelist *
lpfc_nlp_init(struct lpfc_vport * vport,uint32_t did)6581 lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
6582 {
6583 	struct lpfc_nodelist *ndlp;
6584 	int rpi = LPFC_RPI_ALLOC_ERROR;
6585 
6586 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6587 		rpi = lpfc_sli4_alloc_rpi(vport->phba);
6588 		if (rpi == LPFC_RPI_ALLOC_ERROR)
6589 			return NULL;
6590 	}
6591 
6592 	ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6593 	if (!ndlp) {
6594 		if (vport->phba->sli_rev == LPFC_SLI_REV4)
6595 			lpfc_sli4_free_rpi(vport->phba, rpi);
6596 		return NULL;
6597 	}
6598 
6599 	memset(ndlp, 0, sizeof (struct lpfc_nodelist));
6600 
6601 	spin_lock_init(&ndlp->lock);
6602 
6603 	lpfc_initialize_node(vport, ndlp, did);
6604 	INIT_LIST_HEAD(&ndlp->nlp_listp);
6605 	if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6606 		ndlp->nlp_rpi = rpi;
6607 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_DISCOVERY,
6608 				 "0007 Init New ndlp x%px, rpi:x%x DID:%x "
6609 				 "flg:x%x refcnt:%d\n",
6610 				 ndlp, ndlp->nlp_rpi, ndlp->nlp_DID,
6611 				 ndlp->nlp_flag, kref_read(&ndlp->kref));
6612 
6613 		ndlp->active_rrqs_xri_bitmap =
6614 				mempool_alloc(vport->phba->active_rrq_pool,
6615 					      GFP_KERNEL);
6616 		if (ndlp->active_rrqs_xri_bitmap)
6617 			memset(ndlp->active_rrqs_xri_bitmap, 0,
6618 			       ndlp->phba->cfg_rrq_xri_bitmap_sz);
6619 	}
6620 
6621 
6622 
6623 	lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6624 		"node init:       did:x%x",
6625 		ndlp->nlp_DID, 0, 0);
6626 
6627 	return ndlp;
6628 }
6629 
6630 /* This routine releases all resources associated with a specifc NPort's ndlp
6631  * and mempool_free's the nodelist.
6632  */
6633 static void
lpfc_nlp_release(struct kref * kref)6634 lpfc_nlp_release(struct kref *kref)
6635 {
6636 	struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6637 						  kref);
6638 	struct lpfc_vport *vport = ndlp->vport;
6639 
6640 	lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6641 		"node release:    did:x%x flg:x%x type:x%x",
6642 		ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6643 
6644 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6645 			 "0279 %s: ndlp: x%px did %x refcnt:%d rpi:%x\n",
6646 			 __func__, ndlp, ndlp->nlp_DID,
6647 			 kref_read(&ndlp->kref), ndlp->nlp_rpi);
6648 
6649 	/* remove ndlp from action. */
6650 	lpfc_cancel_retry_delay_tmo(vport, ndlp);
6651 	lpfc_cleanup_node(vport, ndlp);
6652 
6653 	/* Not all ELS transactions have registered the RPI with the port.
6654 	 * In these cases the rpi usage is temporary and the node is
6655 	 * released when the WQE is completed.  Catch this case to free the
6656 	 * RPI to the pool.  Because this node is in the release path, a lock
6657 	 * is unnecessary.  All references are gone and the node has been
6658 	 * dequeued.
6659 	 */
6660 	if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
6661 		if (ndlp->nlp_rpi != LPFC_RPI_ALLOC_ERROR &&
6662 		    !(ndlp->nlp_flag & (NLP_RPI_REGISTERED | NLP_UNREG_INP))) {
6663 			lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
6664 			ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
6665 		}
6666 	}
6667 
6668 	/* The node is not freed back to memory, it is released to a pool so
6669 	 * the node fields need to be cleaned up.
6670 	 */
6671 	ndlp->vport = NULL;
6672 	ndlp->nlp_state = NLP_STE_FREED_NODE;
6673 	ndlp->nlp_flag = 0;
6674 	ndlp->fc4_xpt_flags = 0;
6675 
6676 	/* free ndlp memory for final ndlp release */
6677 	if (ndlp->phba->sli_rev == LPFC_SLI_REV4)
6678 		mempool_free(ndlp->active_rrqs_xri_bitmap,
6679 				ndlp->phba->active_rrq_pool);
6680 	mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
6681 }
6682 
6683 /* This routine bumps the reference count for a ndlp structure to ensure
6684  * that one discovery thread won't free a ndlp while another discovery thread
6685  * is using it.
6686  */
6687 struct lpfc_nodelist *
lpfc_nlp_get(struct lpfc_nodelist * ndlp)6688 lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6689 {
6690 	unsigned long flags;
6691 
6692 	if (ndlp) {
6693 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6694 			"node get:        did:x%x flg:x%x refcnt:x%x",
6695 			ndlp->nlp_DID, ndlp->nlp_flag,
6696 			kref_read(&ndlp->kref));
6697 
6698 		/* The check of ndlp usage to prevent incrementing the
6699 		 * ndlp reference count that is in the process of being
6700 		 * released.
6701 		 */
6702 		spin_lock_irqsave(&ndlp->lock, flags);
6703 		if (!kref_get_unless_zero(&ndlp->kref)) {
6704 			spin_unlock_irqrestore(&ndlp->lock, flags);
6705 			lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6706 				"0276 %s: ndlp:x%px refcnt:%d\n",
6707 				__func__, (void *)ndlp, kref_read(&ndlp->kref));
6708 			return NULL;
6709 		}
6710 		spin_unlock_irqrestore(&ndlp->lock, flags);
6711 	} else {
6712 		WARN_ONCE(!ndlp, "**** %s, get ref on NULL ndlp!", __func__);
6713 	}
6714 
6715 	return ndlp;
6716 }
6717 
6718 /* This routine decrements the reference count for a ndlp structure. If the
6719  * count goes to 0, this indicates the associated nodelist should be freed.
6720  */
6721 int
lpfc_nlp_put(struct lpfc_nodelist * ndlp)6722 lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6723 {
6724 	if (ndlp) {
6725 		lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6726 				"node put:        did:x%x flg:x%x refcnt:x%x",
6727 				ndlp->nlp_DID, ndlp->nlp_flag,
6728 				kref_read(&ndlp->kref));
6729 	} else {
6730 		WARN_ONCE(!ndlp, "**** %s, put ref on NULL ndlp!", __func__);
6731 	}
6732 
6733 	return ndlp ? kref_put(&ndlp->kref, lpfc_nlp_release) : 0;
6734 }
6735 
6736 /**
6737  * lpfc_fcf_inuse - Check if FCF can be unregistered.
6738  * @phba: Pointer to hba context object.
6739  *
6740  * This function iterate through all FC nodes associated
6741  * will all vports to check if there is any node with
6742  * fc_rports associated with it. If there is an fc_rport
6743  * associated with the node, then the node is either in
6744  * discovered state or its devloss_timer is pending.
6745  */
6746 static int
lpfc_fcf_inuse(struct lpfc_hba * phba)6747 lpfc_fcf_inuse(struct lpfc_hba *phba)
6748 {
6749 	struct lpfc_vport **vports;
6750 	int i, ret = 0;
6751 	struct lpfc_nodelist *ndlp;
6752 	struct Scsi_Host  *shost;
6753 
6754 	vports = lpfc_create_vport_work_array(phba);
6755 
6756 	/* If driver cannot allocate memory, indicate fcf is in use */
6757 	if (!vports)
6758 		return 1;
6759 
6760 	for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6761 		shost = lpfc_shost_from_vport(vports[i]);
6762 		spin_lock_irq(shost->host_lock);
6763 		/*
6764 		 * IF the CVL_RCVD bit is not set then we have sent the
6765 		 * flogi.
6766 		 * If dev_loss fires while we are waiting we do not want to
6767 		 * unreg the fcf.
6768 		 */
6769 		if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6770 			spin_unlock_irq(shost->host_lock);
6771 			ret =  1;
6772 			goto out;
6773 		}
6774 		list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6775 			if (ndlp->rport &&
6776 			  (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6777 				ret = 1;
6778 				spin_unlock_irq(shost->host_lock);
6779 				goto out;
6780 			} else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6781 				ret = 1;
6782 				lpfc_printf_log(phba, KERN_INFO,
6783 						LOG_NODE | LOG_DISCOVERY,
6784 						"2624 RPI %x DID %x flag %x "
6785 						"still logged in\n",
6786 						ndlp->nlp_rpi, ndlp->nlp_DID,
6787 						ndlp->nlp_flag);
6788 			}
6789 		}
6790 		spin_unlock_irq(shost->host_lock);
6791 	}
6792 out:
6793 	lpfc_destroy_vport_work_array(phba, vports);
6794 	return ret;
6795 }
6796 
6797 /**
6798  * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6799  * @phba: Pointer to hba context object.
6800  * @mboxq: Pointer to mailbox object.
6801  *
6802  * This function frees memory associated with the mailbox command.
6803  */
6804 void
lpfc_unregister_vfi_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)6805 lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6806 {
6807 	struct lpfc_vport *vport = mboxq->vport;
6808 	struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6809 
6810 	if (mboxq->u.mb.mbxStatus) {
6811 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6812 				"2555 UNREG_VFI mbxStatus error x%x "
6813 				"HBA state x%x\n",
6814 				mboxq->u.mb.mbxStatus, vport->port_state);
6815 	}
6816 	spin_lock_irq(shost->host_lock);
6817 	phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6818 	spin_unlock_irq(shost->host_lock);
6819 	mempool_free(mboxq, phba->mbox_mem_pool);
6820 	return;
6821 }
6822 
6823 /**
6824  * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6825  * @phba: Pointer to hba context object.
6826  * @mboxq: Pointer to mailbox object.
6827  *
6828  * This function frees memory associated with the mailbox command.
6829  */
6830 static void
lpfc_unregister_fcfi_cmpl(struct lpfc_hba * phba,LPFC_MBOXQ_t * mboxq)6831 lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6832 {
6833 	struct lpfc_vport *vport = mboxq->vport;
6834 
6835 	if (mboxq->u.mb.mbxStatus) {
6836 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6837 				"2550 UNREG_FCFI mbxStatus error x%x "
6838 				"HBA state x%x\n",
6839 				mboxq->u.mb.mbxStatus, vport->port_state);
6840 	}
6841 	mempool_free(mboxq, phba->mbox_mem_pool);
6842 	return;
6843 }
6844 
6845 /**
6846  * lpfc_unregister_fcf_prep - Unregister fcf record preparation
6847  * @phba: Pointer to hba context object.
6848  *
6849  * This function prepare the HBA for unregistering the currently registered
6850  * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6851  * VFIs.
6852  */
6853 int
lpfc_unregister_fcf_prep(struct lpfc_hba * phba)6854 lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
6855 {
6856 	struct lpfc_vport **vports;
6857 	struct lpfc_nodelist *ndlp;
6858 	struct Scsi_Host *shost;
6859 	int i = 0, rc;
6860 
6861 	/* Unregister RPIs */
6862 	if (lpfc_fcf_inuse(phba))
6863 		lpfc_unreg_hba_rpis(phba);
6864 
6865 	/* At this point, all discovery is aborted */
6866 	phba->pport->port_state = LPFC_VPORT_UNKNOWN;
6867 
6868 	/* Unregister VPIs */
6869 	vports = lpfc_create_vport_work_array(phba);
6870 	if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
6871 		for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6872 			/* Stop FLOGI/FDISC retries */
6873 			ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6874 			if (ndlp)
6875 				lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
6876 			lpfc_cleanup_pending_mbox(vports[i]);
6877 			if (phba->sli_rev == LPFC_SLI_REV4)
6878 				lpfc_sli4_unreg_all_rpis(vports[i]);
6879 			lpfc_mbx_unreg_vpi(vports[i]);
6880 			shost = lpfc_shost_from_vport(vports[i]);
6881 			spin_lock_irq(shost->host_lock);
6882 			vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6883 			vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
6884 			spin_unlock_irq(shost->host_lock);
6885 		}
6886 	lpfc_destroy_vport_work_array(phba, vports);
6887 	if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6888 		ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6889 		if (ndlp)
6890 			lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6891 		lpfc_cleanup_pending_mbox(phba->pport);
6892 		if (phba->sli_rev == LPFC_SLI_REV4)
6893 			lpfc_sli4_unreg_all_rpis(phba->pport);
6894 		lpfc_mbx_unreg_vpi(phba->pport);
6895 		shost = lpfc_shost_from_vport(phba->pport);
6896 		spin_lock_irq(shost->host_lock);
6897 		phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6898 		phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6899 		spin_unlock_irq(shost->host_lock);
6900 	}
6901 
6902 	/* Cleanup any outstanding ELS commands */
6903 	lpfc_els_flush_all_cmd(phba);
6904 
6905 	/* Unregister the physical port VFI */
6906 	rc = lpfc_issue_unreg_vfi(phba->pport);
6907 	return rc;
6908 }
6909 
6910 /**
6911  * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6912  * @phba: Pointer to hba context object.
6913  *
6914  * This function issues synchronous unregister FCF mailbox command to HBA to
6915  * unregister the currently registered FCF record. The driver does not reset
6916  * the driver FCF usage state flags.
6917  *
6918  * Return 0 if successfully issued, none-zero otherwise.
6919  */
6920 int
lpfc_sli4_unregister_fcf(struct lpfc_hba * phba)6921 lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6922 {
6923 	LPFC_MBOXQ_t *mbox;
6924 	int rc;
6925 
6926 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6927 	if (!mbox) {
6928 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6929 				"2551 UNREG_FCFI mbox allocation failed"
6930 				"HBA state x%x\n", phba->pport->port_state);
6931 		return -ENOMEM;
6932 	}
6933 	lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6934 	mbox->vport = phba->pport;
6935 	mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6936 	rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6937 
6938 	if (rc == MBX_NOT_FINISHED) {
6939 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6940 				"2552 Unregister FCFI command failed rc x%x "
6941 				"HBA state x%x\n",
6942 				rc, phba->pport->port_state);
6943 		return -EINVAL;
6944 	}
6945 	return 0;
6946 }
6947 
6948 /**
6949  * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6950  * @phba: Pointer to hba context object.
6951  *
6952  * This function unregisters the currently reigstered FCF. This function
6953  * also tries to find another FCF for discovery by rescan the HBA FCF table.
6954  */
6955 void
lpfc_unregister_fcf_rescan(struct lpfc_hba * phba)6956 lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6957 {
6958 	int rc;
6959 
6960 	/* Preparation for unregistering fcf */
6961 	rc = lpfc_unregister_fcf_prep(phba);
6962 	if (rc) {
6963 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
6964 				"2748 Failed to prepare for unregistering "
6965 				"HBA's FCF record: rc=%d\n", rc);
6966 		return;
6967 	}
6968 
6969 	/* Now, unregister FCF record and reset HBA FCF state */
6970 	rc = lpfc_sli4_unregister_fcf(phba);
6971 	if (rc)
6972 		return;
6973 	/* Reset HBA FCF states after successful unregister FCF */
6974 	spin_lock_irq(&phba->hbalock);
6975 	phba->fcf.fcf_flag = 0;
6976 	spin_unlock_irq(&phba->hbalock);
6977 	phba->fcf.current_rec.flag = 0;
6978 
6979 	/*
6980 	 * If driver is not unloading, check if there is any other
6981 	 * FCF record that can be used for discovery.
6982 	 */
6983 	if ((phba->pport->load_flag & FC_UNLOADING) ||
6984 	    (phba->link_state < LPFC_LINK_UP))
6985 		return;
6986 
6987 	/* This is considered as the initial FCF discovery scan */
6988 	spin_lock_irq(&phba->hbalock);
6989 	phba->fcf.fcf_flag |= FCF_INIT_DISC;
6990 	spin_unlock_irq(&phba->hbalock);
6991 
6992 	/* Reset FCF roundrobin bmask for new discovery */
6993 	lpfc_sli4_clear_fcf_rr_bmask(phba);
6994 
6995 	rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
6996 
6997 	if (rc) {
6998 		spin_lock_irq(&phba->hbalock);
6999 		phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
7000 		spin_unlock_irq(&phba->hbalock);
7001 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7002 				"2553 lpfc_unregister_unused_fcf failed "
7003 				"to read FCF record HBA state x%x\n",
7004 				phba->pport->port_state);
7005 	}
7006 }
7007 
7008 /**
7009  * lpfc_unregister_fcf - Unregister the currently registered fcf record
7010  * @phba: Pointer to hba context object.
7011  *
7012  * This function just unregisters the currently reigstered FCF. It does not
7013  * try to find another FCF for discovery.
7014  */
7015 void
lpfc_unregister_fcf(struct lpfc_hba * phba)7016 lpfc_unregister_fcf(struct lpfc_hba *phba)
7017 {
7018 	int rc;
7019 
7020 	/* Preparation for unregistering fcf */
7021 	rc = lpfc_unregister_fcf_prep(phba);
7022 	if (rc) {
7023 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7024 				"2749 Failed to prepare for unregistering "
7025 				"HBA's FCF record: rc=%d\n", rc);
7026 		return;
7027 	}
7028 
7029 	/* Now, unregister FCF record and reset HBA FCF state */
7030 	rc = lpfc_sli4_unregister_fcf(phba);
7031 	if (rc)
7032 		return;
7033 	/* Set proper HBA FCF states after successful unregister FCF */
7034 	spin_lock_irq(&phba->hbalock);
7035 	phba->fcf.fcf_flag &= ~FCF_REGISTERED;
7036 	spin_unlock_irq(&phba->hbalock);
7037 }
7038 
7039 /**
7040  * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
7041  * @phba: Pointer to hba context object.
7042  *
7043  * This function check if there are any connected remote port for the FCF and
7044  * if all the devices are disconnected, this function unregister FCFI.
7045  * This function also tries to use another FCF for discovery.
7046  */
7047 void
lpfc_unregister_unused_fcf(struct lpfc_hba * phba)7048 lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
7049 {
7050 	/*
7051 	 * If HBA is not running in FIP mode, if HBA does not support
7052 	 * FCoE, if FCF discovery is ongoing, or if FCF has not been
7053 	 * registered, do nothing.
7054 	 */
7055 	spin_lock_irq(&phba->hbalock);
7056 	if (!(phba->hba_flag & HBA_FCOE_MODE) ||
7057 	    !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
7058 	    !(phba->hba_flag & HBA_FIP_SUPPORT) ||
7059 	    (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
7060 	    (phba->pport->port_state == LPFC_FLOGI)) {
7061 		spin_unlock_irq(&phba->hbalock);
7062 		return;
7063 	}
7064 	spin_unlock_irq(&phba->hbalock);
7065 
7066 	if (lpfc_fcf_inuse(phba))
7067 		return;
7068 
7069 	lpfc_unregister_fcf_rescan(phba);
7070 }
7071 
7072 /**
7073  * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
7074  * @phba: Pointer to hba context object.
7075  * @buff: Buffer containing the FCF connection table as in the config
7076  *         region.
7077  * This function create driver data structure for the FCF connection
7078  * record table read from config region 23.
7079  */
7080 static void
lpfc_read_fcf_conn_tbl(struct lpfc_hba * phba,uint8_t * buff)7081 lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
7082 	uint8_t *buff)
7083 {
7084 	struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
7085 	struct lpfc_fcf_conn_hdr *conn_hdr;
7086 	struct lpfc_fcf_conn_rec *conn_rec;
7087 	uint32_t record_count;
7088 	int i;
7089 
7090 	/* Free the current connect table */
7091 	list_for_each_entry_safe(conn_entry, next_conn_entry,
7092 		&phba->fcf_conn_rec_list, list) {
7093 		list_del_init(&conn_entry->list);
7094 		kfree(conn_entry);
7095 	}
7096 
7097 	conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
7098 	record_count = conn_hdr->length * sizeof(uint32_t)/
7099 		sizeof(struct lpfc_fcf_conn_rec);
7100 
7101 	conn_rec = (struct lpfc_fcf_conn_rec *)
7102 		(buff + sizeof(struct lpfc_fcf_conn_hdr));
7103 
7104 	for (i = 0; i < record_count; i++) {
7105 		if (!(conn_rec[i].flags & FCFCNCT_VALID))
7106 			continue;
7107 		conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
7108 			GFP_KERNEL);
7109 		if (!conn_entry) {
7110 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7111 					"2566 Failed to allocate connection"
7112 					" table entry\n");
7113 			return;
7114 		}
7115 
7116 		memcpy(&conn_entry->conn_rec, &conn_rec[i],
7117 			sizeof(struct lpfc_fcf_conn_rec));
7118 		list_add_tail(&conn_entry->list,
7119 			&phba->fcf_conn_rec_list);
7120 	}
7121 
7122 	if (!list_empty(&phba->fcf_conn_rec_list)) {
7123 		i = 0;
7124 		list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
7125 				    list) {
7126 			conn_rec = &conn_entry->conn_rec;
7127 			lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7128 					"3345 FCF connection list rec[%02d]: "
7129 					"flags:x%04x, vtag:x%04x, "
7130 					"fabric_name:x%02x:%02x:%02x:%02x:"
7131 					"%02x:%02x:%02x:%02x, "
7132 					"switch_name:x%02x:%02x:%02x:%02x:"
7133 					"%02x:%02x:%02x:%02x\n", i++,
7134 					conn_rec->flags, conn_rec->vlan_tag,
7135 					conn_rec->fabric_name[0],
7136 					conn_rec->fabric_name[1],
7137 					conn_rec->fabric_name[2],
7138 					conn_rec->fabric_name[3],
7139 					conn_rec->fabric_name[4],
7140 					conn_rec->fabric_name[5],
7141 					conn_rec->fabric_name[6],
7142 					conn_rec->fabric_name[7],
7143 					conn_rec->switch_name[0],
7144 					conn_rec->switch_name[1],
7145 					conn_rec->switch_name[2],
7146 					conn_rec->switch_name[3],
7147 					conn_rec->switch_name[4],
7148 					conn_rec->switch_name[5],
7149 					conn_rec->switch_name[6],
7150 					conn_rec->switch_name[7]);
7151 		}
7152 	}
7153 }
7154 
7155 /**
7156  * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
7157  * @phba: Pointer to hba context object.
7158  * @buff: Buffer containing the FCoE parameter data structure.
7159  *
7160  *  This function update driver data structure with config
7161  *  parameters read from config region 23.
7162  */
7163 static void
lpfc_read_fcoe_param(struct lpfc_hba * phba,uint8_t * buff)7164 lpfc_read_fcoe_param(struct lpfc_hba *phba,
7165 			uint8_t *buff)
7166 {
7167 	struct lpfc_fip_param_hdr *fcoe_param_hdr;
7168 	struct lpfc_fcoe_params *fcoe_param;
7169 
7170 	fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
7171 		buff;
7172 	fcoe_param = (struct lpfc_fcoe_params *)
7173 		(buff + sizeof(struct lpfc_fip_param_hdr));
7174 
7175 	if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
7176 		(fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
7177 		return;
7178 
7179 	if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
7180 		phba->valid_vlan = 1;
7181 		phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
7182 			0xFFF;
7183 	}
7184 
7185 	phba->fc_map[0] = fcoe_param->fc_map[0];
7186 	phba->fc_map[1] = fcoe_param->fc_map[1];
7187 	phba->fc_map[2] = fcoe_param->fc_map[2];
7188 	return;
7189 }
7190 
7191 /**
7192  * lpfc_get_rec_conf23 - Get a record type in config region data.
7193  * @buff: Buffer containing config region 23 data.
7194  * @size: Size of the data buffer.
7195  * @rec_type: Record type to be searched.
7196  *
7197  * This function searches config region data to find the beginning
7198  * of the record specified by record_type. If record found, this
7199  * function return pointer to the record else return NULL.
7200  */
7201 static uint8_t *
lpfc_get_rec_conf23(uint8_t * buff,uint32_t size,uint8_t rec_type)7202 lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
7203 {
7204 	uint32_t offset = 0, rec_length;
7205 
7206 	if ((buff[0] == LPFC_REGION23_LAST_REC) ||
7207 		(size < sizeof(uint32_t)))
7208 		return NULL;
7209 
7210 	rec_length = buff[offset + 1];
7211 
7212 	/*
7213 	 * One TLV record has one word header and number of data words
7214 	 * specified in the rec_length field of the record header.
7215 	 */
7216 	while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
7217 		<= size) {
7218 		if (buff[offset] == rec_type)
7219 			return &buff[offset];
7220 
7221 		if (buff[offset] == LPFC_REGION23_LAST_REC)
7222 			return NULL;
7223 
7224 		offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
7225 		rec_length = buff[offset + 1];
7226 	}
7227 	return NULL;
7228 }
7229 
7230 /**
7231  * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
7232  * @phba: Pointer to lpfc_hba data structure.
7233  * @buff: Buffer containing config region 23 data.
7234  * @size: Size of the data buffer.
7235  *
7236  * This function parses the FCoE config parameters in config region 23 and
7237  * populate driver data structure with the parameters.
7238  */
7239 void
lpfc_parse_fcoe_conf(struct lpfc_hba * phba,uint8_t * buff,uint32_t size)7240 lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
7241 		uint8_t *buff,
7242 		uint32_t size)
7243 {
7244 	uint32_t offset = 0;
7245 	uint8_t *rec_ptr;
7246 
7247 	/*
7248 	 * If data size is less than 2 words signature and version cannot be
7249 	 * verified.
7250 	 */
7251 	if (size < 2*sizeof(uint32_t))
7252 		return;
7253 
7254 	/* Check the region signature first */
7255 	if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
7256 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7257 			"2567 Config region 23 has bad signature\n");
7258 		return;
7259 	}
7260 
7261 	offset += 4;
7262 
7263 	/* Check the data structure version */
7264 	if (buff[offset] != LPFC_REGION23_VERSION) {
7265 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
7266 				"2568 Config region 23 has bad version\n");
7267 		return;
7268 	}
7269 	offset += 4;
7270 
7271 	/* Read FCoE param record */
7272 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7273 			size - offset, FCOE_PARAM_TYPE);
7274 	if (rec_ptr)
7275 		lpfc_read_fcoe_param(phba, rec_ptr);
7276 
7277 	/* Read FCF connection table */
7278 	rec_ptr = lpfc_get_rec_conf23(&buff[offset],
7279 		size - offset, FCOE_CONN_TBL_TYPE);
7280 	if (rec_ptr)
7281 		lpfc_read_fcf_conn_tbl(phba, rec_ptr);
7282 
7283 }
7284 
7285 /*
7286  * lpfc_error_lost_link - IO failure from link event or FW reset check.
7287  *
7288  * @vport: Pointer to lpfc_vport data structure.
7289  * @ulp_status: IO completion status.
7290  * @ulp_word4: Reason code for the ulp_status.
7291  *
7292  * This function evaluates the ulp_status and ulp_word4 values
7293  * for specific error values that indicate an internal link fault
7294  * or fw reset event for the completing IO.  Callers require this
7295  * common data to decide next steps on the IO.
7296  *
7297  * Return:
7298  * false - No link or reset error occurred.
7299  * true - A link or reset error occurred.
7300  */
7301 bool
lpfc_error_lost_link(struct lpfc_vport * vport,u32 ulp_status,u32 ulp_word4)7302 lpfc_error_lost_link(struct lpfc_vport *vport, u32 ulp_status, u32 ulp_word4)
7303 {
7304 	/* Mask off the extra port data to get just the reason code. */
7305 	u32 rsn_code = IOERR_PARAM_MASK & ulp_word4;
7306 
7307 	if (ulp_status == IOSTAT_LOCAL_REJECT &&
7308 	    (rsn_code == IOERR_SLI_ABORTED ||
7309 	     rsn_code == IOERR_LINK_DOWN ||
7310 	     rsn_code == IOERR_SLI_DOWN)) {
7311 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI | LOG_ELS,
7312 				 "0408 Report link error true: <x%x:x%x>\n",
7313 				 ulp_status, ulp_word4);
7314 		return true;
7315 	}
7316 
7317 	return false;
7318 }
7319