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