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