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