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