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