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