blob: 9df6f0cabab02942c7de5145e906c22675a5e10a [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart0d041212019-01-28 11:14:41 -08004 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
James Smart4ae2ebd2018-06-26 08:24:31 -07005 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
James Smart50611572016-03-31 14:12:34 -07006 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * EMULEX and SLI are trademarks of Emulex. *
James Smartd080abe2017-02-12 13:52:39 -08008 * www.broadcom.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04009 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -050010 * *
11 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040012 * 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. *
dea31012005-04-17 16:05:31 -050022 *******************************************************************/
23
dea31012005-04-17 16:05:31 -050024#include <linux/blkdev.h>
James Smarta93ff372010-10-22 11:06:08 -040025#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050027#include <linux/pci.h>
28#include <linux/kthread.h>
29#include <linux/interrupt.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010030#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050031
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040032#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050033#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi_transport_fc.h>
James Smarta0f2d3e2017-02-12 13:52:31 -080036#include <scsi/fc/fc_fs.h>
37
38#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050039
James Smartda0436e2009-05-22 14:51:39 -040040#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040042#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050043#include "lpfc_disc.h"
44#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040045#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050046#include "lpfc.h"
James Smarta0f2d3e2017-02-12 13:52:31 -080047#include "lpfc_scsi.h"
48#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050049#include "lpfc_logmsg.h"
50#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050051#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050052#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050053
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
James Smart2e0fef82007-06-17 19:56:36 -050071static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010072static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040073static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040074static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050075
James Smartc01f3202006-08-18 17:47:08 -040076void
77lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050078{
James Smartc01f3202006-08-18 17:47:08 -040079 struct lpfc_rport_data *rdata;
80 struct lpfc_nodelist * ndlp;
81 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050082
James Smartc01f3202006-08-18 17:47:08 -040083 rdata = rport->dd_data;
84 ndlp = rdata->pnode;
85
James Smart58da1ff2008-04-07 10:15:56 -040086 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040087 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);
dea31012005-04-17 16:05:31 -050091 return;
92 }
93
James Smarta257bf92009-04-06 18:48:10 -040094 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050095
James Smart858c9f62007-06-17 19:56:39 -050096 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
James Smartc01f3202006-08-18 17:47:08 -0400100 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -0400101 lpfc_sli_abort_iocb(ndlp->vport,
James Smart895427b2017-02-12 13:52:30 -0800102 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart51ef4c22007-08-02 11:10:31 -0400103 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -0400104 }
James Smartc01f3202006-08-18 17:47:08 -0400105}
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;
James Smart2e0fef82007-06-17 19:56:36 -0500115 struct lpfc_vport *vport;
James Smart466e8402015-05-21 13:55:28 -0400116 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500117 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500118 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400119 int put_node;
120 int put_rport;
James Smart894bb172019-08-14 16:56:52 -0700121 unsigned long iflags;
James Smartc01f3202006-08-18 17:47:08 -0400122
123 rdata = rport->dd_data;
124 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400125 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400126 return;
James Smartc01f3202006-08-18 17:47:08 -0400127
James Smart858c9f62007-06-17 19:56:39 -0500128 vport = ndlp->vport;
129 phba = vport->phba;
130
131 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
132 "rport devlosscb: sid:x%x did:x%x flg:x%x",
133 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
134
James Smart34f5ad82012-08-03 12:35:03 -0400135 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -0700136 "3181 dev_loss_callbk x%06x, rport x%px flg x%x\n",
James Smart34f5ad82012-08-03 12:35:03 -0400137 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
138
James Smarta8adb832007-10-27 13:37:53 -0400139 /* Don't defer this if we are in the process of deleting the vport
140 * or unloading the driver. The unload will cleanup the node
141 * appropriately we just need to cleanup the ndlp rport info here.
142 */
143 if (vport->load_flag & FC_UNLOADING) {
144 put_node = rdata->pnode != NULL;
145 put_rport = ndlp->rport != NULL;
146 rdata->pnode = NULL;
147 ndlp->rport = NULL;
148 if (put_node)
149 lpfc_nlp_put(ndlp);
150 if (put_rport)
151 put_device(&rport->dev);
152 return;
153 }
154
155 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
156 return;
157
James Smart466e8402015-05-21 13:55:28 -0400158 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
159 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
160 "6789 rport name %llx != node port name %llx",
161 rport->port_name,
162 wwn_to_u64(ndlp->nlp_portname.u.wwn));
James Smart34f5ad82012-08-03 12:35:03 -0400163
James Smart858c9f62007-06-17 19:56:39 -0500164 evtp = &ndlp->dev_loss_evt;
165
James Smart466e8402015-05-21 13:55:28 -0400166 if (!list_empty(&evtp->evt_listp)) {
167 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
168 "6790 rport name %llx dev_loss_evt pending",
169 rport->port_name);
James Smart858c9f62007-06-17 19:56:39 -0500170 return;
James Smart466e8402015-05-21 13:55:28 -0400171 }
James Smart858c9f62007-06-17 19:56:39 -0500172
James Smart466e8402015-05-21 13:55:28 -0400173 shost = lpfc_shost_from_vport(vport);
James Smart894bb172019-08-14 16:56:52 -0700174 spin_lock_irqsave(shost->host_lock, iflags);
James Smart466e8402015-05-21 13:55:28 -0400175 ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
James Smart894bb172019-08-14 16:56:52 -0700176 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smarta62a4352013-04-17 20:18:19 -0400177
James Smartfa4066b2008-01-11 01:53:27 -0500178 /* We need to hold the node by incrementing the reference
179 * count until this queued work is done
180 */
James Smart466e8402015-05-21 13:55:28 -0400181 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
182
James Smart894bb172019-08-14 16:56:52 -0700183 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart5e9d9b82008-06-14 22:52:53 -0400184 if (evtp->evt_arg1) {
185 evtp->evt = LPFC_EVT_DEV_LOSS;
186 list_add_tail(&evtp->evt_listp, &phba->work_list);
187 lpfc_worker_wake_up(phba);
188 }
James Smart894bb172019-08-14 16:56:52 -0700189 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart858c9f62007-06-17 19:56:39 -0500190
James Smart858c9f62007-06-17 19:56:39 -0500191 return;
192}
193
James Smarta93ff372010-10-22 11:06:08 -0400194/**
195 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
196 * @ndlp: Pointer to remote node object.
197 *
198 * This function is called from the worker thread when devloss timeout timer
199 * expires. For SLI4 host, this routine shall return 1 when at lease one
200 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
201 * routine shall return 0 when there is no remote node is still in use of FCF
202 * when devloss timeout happened to this @ndlp.
203 **/
204static int
James Smart858c9f62007-06-17 19:56:39 -0500205lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
206{
207 struct lpfc_rport_data *rdata;
208 struct fc_rport *rport;
209 struct lpfc_vport *vport;
210 struct lpfc_hba *phba;
James Smart466e8402015-05-21 13:55:28 -0400211 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500212 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400213 int put_node;
James Smart858c9f62007-06-17 19:56:39 -0500214 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400215 int fcf_inuse = 0;
James Smart894bb172019-08-14 16:56:52 -0700216 unsigned long iflags;
James Smart858c9f62007-06-17 19:56:39 -0500217
218 rport = ndlp->rport;
James Smart858c9f62007-06-17 19:56:39 -0500219 vport = ndlp->vport;
James Smart466e8402015-05-21 13:55:28 -0400220 shost = lpfc_shost_from_vport(vport);
221
James Smart894bb172019-08-14 16:56:52 -0700222 spin_lock_irqsave(shost->host_lock, iflags);
James Smart466e8402015-05-21 13:55:28 -0400223 ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
James Smart894bb172019-08-14 16:56:52 -0700224 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart466e8402015-05-21 13:55:28 -0400225
226 if (!rport)
227 return fcf_inuse;
228
229 name = (uint8_t *) &ndlp->nlp_portname;
James Smart858c9f62007-06-17 19:56:39 -0500230 phba = vport->phba;
231
James Smarta93ff372010-10-22 11:06:08 -0400232 if (phba->sli_rev == LPFC_SLI_REV4)
233 fcf_inuse = lpfc_fcf_inuse(phba);
234
James Smart858c9f62007-06-17 19:56:39 -0500235 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
236 "rport devlosstmo:did:x%x type:x%x id:x%x",
237 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
238
James Smart34f5ad82012-08-03 12:35:03 -0400239 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -0700240 "3182 dev_loss_tmo_handler x%06x, rport x%px flg x%x\n",
James Smart34f5ad82012-08-03 12:35:03 -0400241 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
242
James Smart466e8402015-05-21 13:55:28 -0400243 /*
244 * lpfc_nlp_remove if reached with dangling rport drops the
245 * reference. To make sure that does not happen clear rport
246 * pointer in ndlp before lpfc_nlp_put.
247 */
248 rdata = rport->dd_data;
249
James Smarta8adb832007-10-27 13:37:53 -0400250 /* Don't defer this if we are in the process of deleting the vport
251 * or unloading the driver. The unload will cleanup the node
252 * appropriately we just need to cleanup the ndlp rport info here.
253 */
254 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500255 if (ndlp->nlp_sid != NLP_NO_SID) {
256 /* flush the target */
257 lpfc_sli_abort_iocb(vport,
James Smart895427b2017-02-12 13:52:30 -0800258 &phba->sli.sli3_ring[LPFC_FCP_RING],
259 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smart09372822008-01-11 01:52:54 -0500260 }
James Smarta8adb832007-10-27 13:37:53 -0400261 put_node = rdata->pnode != NULL;
James Smarta8adb832007-10-27 13:37:53 -0400262 rdata->pnode = NULL;
263 ndlp->rport = NULL;
264 if (put_node)
265 lpfc_nlp_put(ndlp);
James Smart466e8402015-05-21 13:55:28 -0400266 put_device(&rport->dev);
267
James Smarta93ff372010-10-22 11:06:08 -0400268 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400269 }
270
James Smartd7c255b2008-08-24 21:50:00 -0400271 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
272 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
273 "0284 Devloss timeout Ignored on "
274 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
275 "NPort x%x\n",
276 *name, *(name+1), *(name+2), *(name+3),
277 *(name+4), *(name+5), *(name+6), *(name+7),
278 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400279 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400280 }
James Smart858c9f62007-06-17 19:56:39 -0500281
James Smart466e8402015-05-21 13:55:28 -0400282 put_node = rdata->pnode != NULL;
283 rdata->pnode = NULL;
284 ndlp->rport = NULL;
285 if (put_node)
286 lpfc_nlp_put(ndlp);
287 put_device(&rport->dev);
288
289 if (ndlp->nlp_type & NLP_FABRIC)
James Smarta93ff372010-10-22 11:06:08 -0400290 return fcf_inuse;
James Smart82085712007-04-25 09:52:41 -0400291
dea31012005-04-17 16:05:31 -0500292 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400293 warn_on = 1;
James Smart895427b2017-02-12 13:52:30 -0800294 lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart51ef4c22007-08-02 11:10:31 -0400295 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500296 }
James Smartc01f3202006-08-18 17:47:08 -0400297
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400298 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400299 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
300 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400301 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
302 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400303 *name, *(name+1), *(name+2), *(name+3),
304 *(name+4), *(name+5), *(name+6), *(name+7),
305 ndlp->nlp_DID, ndlp->nlp_flag,
306 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400307 } else {
James Smarte8b62012007-08-02 11:10:09 -0400308 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
309 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400310 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
311 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400312 *name, *(name+1), *(name+2), *(name+3),
313 *(name+4), *(name+5), *(name+6), *(name+7),
314 ndlp->nlp_DID, ndlp->nlp_flag,
315 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400316 }
317
James Smart2ade92a2017-03-04 09:30:38 -0800318 if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400319 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400320 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400321 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
322 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500323 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400324
James Smarta93ff372010-10-22 11:06:08 -0400325 return fcf_inuse;
326}
327
328/**
329 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
330 * @phba: Pointer to hba context object.
331 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
332 * @nlp_did: remote node identifer with devloss timeout.
333 *
334 * This function is called from the worker thread after invoking devloss
335 * timeout handler and releasing the reference count for the ndlp with
336 * which the devloss timeout was handled for SLI4 host. For the devloss
337 * timeout of the last remote node which had been in use of FCF, when this
338 * routine is invoked, it shall be guaranteed that none of the remote are
339 * in-use of FCF. When devloss timeout to the last remote using the FCF,
340 * if the FIP engine is neither in FCF table scan process nor roundrobin
341 * failover process, the in-use FCF shall be unregistered. If the FIP
342 * engine is in FCF discovery process, the devloss timeout state shall
343 * be set for either the FCF table scan process or roundrobin failover
344 * process to unregister the in-use FCF.
345 **/
346static void
347lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
348 uint32_t nlp_did)
349{
350 /* If devloss timeout happened to a remote node when FCF had no
351 * longer been in-use, do nothing.
352 */
353 if (!fcf_inuse)
354 return;
355
356 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
357 spin_lock_irq(&phba->hbalock);
358 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
359 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
360 spin_unlock_irq(&phba->hbalock);
361 return;
362 }
363 phba->hba_flag |= HBA_DEVLOSS_TMO;
364 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
365 "2847 Last remote node (x%x) using "
366 "FCF devloss tmo\n", nlp_did);
367 }
368 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
369 spin_unlock_irq(&phba->hbalock);
370 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
371 "2868 Devloss tmo to FCF rediscovery "
372 "in progress\n");
373 return;
374 }
375 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
376 spin_unlock_irq(&phba->hbalock);
377 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
378 "2869 Devloss tmo to idle FIP engine, "
379 "unreg in-use FCF and rescan.\n");
380 /* Unregister in-use FCF and rescan */
381 lpfc_unregister_fcf_rescan(phba);
382 return;
383 }
384 spin_unlock_irq(&phba->hbalock);
385 if (phba->hba_flag & FCF_TS_INPROG)
386 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
387 "2870 FCF table scan in progress\n");
388 if (phba->hba_flag & FCF_RR_INPROG)
389 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
390 "2871 FLOGI roundrobin FCF failover "
391 "in progress\n");
392 }
James Smart6fb120a2009-05-22 14:52:59 -0400393 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500394}
James Smartc01f3202006-08-18 17:47:08 -0400395
James Smartea2151b2008-09-07 11:52:10 -0400396/**
James Smart3621a712009-04-06 18:47:14 -0400397 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400398 * @phba: Pointer to hba context object.
399 *
400 * This function is called from the functions which need to post
401 * events from interrupt context. This function allocates data
402 * structure required for posting event. It also keeps track of
403 * number of events pending and prevent event storm when there are
404 * too many events.
405 **/
406struct lpfc_fast_path_event *
407lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
408 struct lpfc_fast_path_event *ret;
409
410 /* If there are lot of fast event do not exhaust memory due to this */
411 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
412 return NULL;
413
414 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
415 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400416 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400417 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400418 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
419 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
420 }
James Smartea2151b2008-09-07 11:52:10 -0400421 return ret;
422}
423
424/**
James Smart3621a712009-04-06 18:47:14 -0400425 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400426 * @phba: Pointer to hba context object.
427 * @evt: Event object which need to be freed.
428 *
429 * This function frees the data structure required for posting
430 * events.
431 **/
432void
433lpfc_free_fast_evt(struct lpfc_hba *phba,
434 struct lpfc_fast_path_event *evt) {
435
436 atomic_dec(&phba->fast_event_count);
437 kfree(evt);
438}
439
440/**
James Smart3621a712009-04-06 18:47:14 -0400441 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400442 * @phba: Pointer to hba context object.
443 * @evtp: Event data structure.
444 *
445 * This function is called from worker thread, when the interrupt
446 * context need to post an event. This function posts the event
447 * to fc transport netlink interface.
448 **/
449static void
450lpfc_send_fastpath_evt(struct lpfc_hba *phba,
451 struct lpfc_work_evt *evtp)
452{
453 unsigned long evt_category, evt_sub_category;
454 struct lpfc_fast_path_event *fast_evt_data;
455 char *evt_data;
456 uint32_t evt_data_size;
457 struct Scsi_Host *shost;
458
459 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
460 work_evt);
461
462 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
463 evt_sub_category = (unsigned long) fast_evt_data->un.
464 fabric_evt.subcategory;
465 shost = lpfc_shost_from_vport(fast_evt_data->vport);
466 if (evt_category == FC_REG_FABRIC_EVENT) {
467 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
468 evt_data = (char *) &fast_evt_data->un.read_check_error;
469 evt_data_size = sizeof(fast_evt_data->un.
470 read_check_error);
471 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500472 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400473 evt_data = (char *) &fast_evt_data->un.fabric_evt;
474 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
475 } else {
476 lpfc_free_fast_evt(phba, fast_evt_data);
477 return;
478 }
479 } else if (evt_category == FC_REG_SCSI_EVENT) {
480 switch (evt_sub_category) {
481 case LPFC_EVENT_QFULL:
482 case LPFC_EVENT_DEVBSY:
483 evt_data = (char *) &fast_evt_data->un.scsi_evt;
484 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
485 break;
486 case LPFC_EVENT_CHECK_COND:
487 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
488 evt_data_size = sizeof(fast_evt_data->un.
489 check_cond_evt);
490 break;
491 case LPFC_EVENT_VARQUEDEPTH:
492 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
493 evt_data_size = sizeof(fast_evt_data->un.
494 queue_depth_evt);
495 break;
496 default:
497 lpfc_free_fast_evt(phba, fast_evt_data);
498 return;
499 }
500 } else {
501 lpfc_free_fast_evt(phba, fast_evt_data);
502 return;
503 }
504
James Smart895427b2017-02-12 13:52:30 -0800505 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
506 fc_host_post_vendor_event(shost,
507 fc_get_event_number(),
508 evt_data_size,
509 evt_data,
510 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400511
512 lpfc_free_fast_evt(phba, fast_evt_data);
513 return;
514}
515
dea31012005-04-17 16:05:31 -0500516static void
James Smart2e0fef82007-06-17 19:56:36 -0500517lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500518{
519 struct lpfc_work_evt *evtp = NULL;
520 struct lpfc_nodelist *ndlp;
521 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400522 int fcf_inuse;
523 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500524
James Smart2e0fef82007-06-17 19:56:36 -0500525 spin_lock_irq(&phba->hbalock);
526 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500527 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
528 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500529 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500530 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500531 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500532 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500533 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500534 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500535 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500536 /* decrement the node reference count held
537 * for this queued work
538 */
539 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500540 break;
James Smart858c9f62007-06-17 19:56:39 -0500541 case LPFC_EVT_DEV_LOSS:
542 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400543 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500544 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500545 /* decrement the node reference count held for
546 * this queued work
547 */
James Smarta93ff372010-10-22 11:06:08 -0400548 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500549 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400550 if (phba->sli_rev == LPFC_SLI_REV4)
551 lpfc_sli4_post_dev_loss_tmo_handler(phba,
552 fcf_inuse,
553 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500554 break;
dea31012005-04-17 16:05:31 -0500555 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500556 if (phba->link_state < LPFC_LINK_DOWN)
557 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500558 else
James Smart2e0fef82007-06-17 19:56:36 -0500559 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500560 complete((struct completion *)(evtp->evt_arg2));
561 break;
James Smart46fa3112007-04-25 09:51:45 -0400562 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500563 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400564 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400565 *(int *)(evtp->evt_arg1) = 0;
566 complete((struct completion *)(evtp->evt_arg2));
567 break;
568 case LPFC_EVT_OFFLINE:
569 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500570 lpfc_sli_brdrestart(phba);
571 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400572 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
573 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500574 complete((struct completion *)(evtp->evt_arg2));
575 break;
576 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400577 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500578 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500579 lpfc_sli_brdreset(phba);
580 lpfc_hba_down_post(phba);
581 *(int *)(evtp->evt_arg1) =
582 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400583 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500584 complete((struct completion *)(evtp->evt_arg2));
585 break;
586 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400587 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500588 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500589 = (phba->pport->stopped)
590 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400591 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500592 complete((struct completion *)(evtp->evt_arg2));
593 break;
James Smartea2151b2008-09-07 11:52:10 -0400594 case LPFC_EVT_FASTPATH_MGMT_EVT:
595 lpfc_send_fastpath_evt(phba, evtp);
596 free_evt = 0;
597 break;
James Smart78730cf2010-04-06 15:06:30 -0400598 case LPFC_EVT_RESET_HBA:
599 if (!(phba->pport->load_flag & FC_UNLOADING))
600 lpfc_reset_hba(phba);
601 break;
dea31012005-04-17 16:05:31 -0500602 }
603 if (free_evt)
604 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500605 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500606 }
James Smart2e0fef82007-06-17 19:56:36 -0500607 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500608
609}
610
James Smart311464e2007-08-02 11:10:37 -0400611static void
James Smart2e0fef82007-06-17 19:56:36 -0500612lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500613{
614 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500615 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400616 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400617 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400618 int i;
dea31012005-04-17 16:05:31 -0500619
James Smart2e0fef82007-06-17 19:56:36 -0500620 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500621 ha_copy = phba->work_ha;
622 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500623 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500624
James Smartda0436e2009-05-22 14:51:39 -0400625 /* First, try to post the next mailbox command to SLI4 device */
626 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
627 lpfc_sli4_post_async_mbox(phba);
628
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500629 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400630 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500631 lpfc_handle_eratt(phba);
632
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500633 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500634 lpfc_sli_handle_mb_event(phba);
635
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500636 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500637 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400638
James Smartda0436e2009-05-22 14:51:39 -0400639 /* Process SLI4 events */
640 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500641 if (phba->hba_flag & HBA_RRQ_ACTIVE)
642 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400643 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
644 lpfc_sli4_els_xri_abort_event_proc(phba);
645 if (phba->hba_flag & ASYNC_EVENT)
646 lpfc_sli4_async_event_proc(phba);
647 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
648 spin_lock_irq(&phba->hbalock);
649 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
650 spin_unlock_irq(&phba->hbalock);
651 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
652 }
James Smartecfd03c2010-02-12 14:41:27 -0500653 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
654 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400655 }
656
James Smart549e55c2007-08-02 11:09:51 -0400657 vports = lpfc_create_vport_work_array(phba);
658 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400659 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400660 /*
661 * We could have no vports in array if unloading, so if
662 * this happens then just use the pport
663 */
664 if (vports[i] == NULL && i == 0)
665 vport = phba->pport;
666 else
667 vport = vports[i];
668 if (vport == NULL)
669 break;
James Smart58da1ff2008-04-07 10:15:56 -0400670 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400671 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400672 vport->work_port_events &= ~work_port_events;
673 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400674 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400675 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400676 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400677 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400678 if (work_port_events & WORKER_HB_TMO)
679 lpfc_hb_timeout_handler(phba);
680 if (work_port_events & WORKER_MBOX_TMO)
681 lpfc_mbox_timeout_handler(phba);
682 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
683 lpfc_unblock_fabric_iocbs(phba);
James Smart549e55c2007-08-02 11:09:51 -0400684 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
685 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500686 if (work_port_events & WORKER_DELAYED_DISC_TMO)
687 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500688 }
James Smart09372822008-01-11 01:52:54 -0500689 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500690
James Smart895427b2017-02-12 13:52:30 -0800691 pring = lpfc_phba_elsring(phba);
James Smart858c9f62007-06-17 19:56:39 -0500692 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
693 status >>= (4*LPFC_ELS_RING);
James Smart0c9c6a72017-05-15 15:20:39 -0700694 if (pring && (status & HA_RXMASK ||
695 pring->flag & LPFC_DEFERRED_RING_EVENT ||
696 phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400697 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500698 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart161df4f02018-01-30 15:58:54 -0800699 /* Preserve legacy behavior. */
700 if (!(phba->hba_flag & HBA_SP_QUEUE_EVT))
701 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500702 } else {
James Smartae9e28f2017-05-15 15:20:51 -0700703 if (phba->link_state >= LPFC_LINK_UP ||
704 phba->link_flag & LS_MDS_LOOPBACK) {
James Smart94661502013-03-01 16:37:29 -0500705 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
706 lpfc_sli_handle_slow_ring_event(phba, pring,
707 (status &
708 HA_RXMASK));
709 }
James Smart858c9f62007-06-17 19:56:39 -0500710 }
James Smartdc19e3b2018-05-24 21:08:57 -0700711 if (phba->sli_rev == LPFC_SLI_REV4)
James Smart2a9bf3d2010-06-07 15:24:45 -0400712 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500713 /*
714 * Turn on Ring interrupts
715 */
James Smart3772a992009-05-22 14:50:54 -0400716 if (phba->sli_rev <= LPFC_SLI_REV3) {
717 spin_lock_irq(&phba->hbalock);
718 control = readl(phba->HCregaddr);
719 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
720 lpfc_debugfs_slow_ring_trc(phba,
721 "WRK Enable ring: cntl:x%x hacopy:x%x",
722 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400723
James Smart3772a992009-05-22 14:50:54 -0400724 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
725 writel(control, phba->HCregaddr);
726 readl(phba->HCregaddr); /* flush */
727 } else {
728 lpfc_debugfs_slow_ring_trc(phba,
729 "WRK Ring ok: cntl:x%x hacopy:x%x",
730 control, ha_copy, 0);
731 }
732 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500733 }
734 }
James Smart2e0fef82007-06-17 19:56:36 -0500735 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500736}
737
dea31012005-04-17 16:05:31 -0500738int
739lpfc_do_work(void *p)
740{
741 struct lpfc_hba *phba = p;
742 int rc;
dea31012005-04-17 16:05:31 -0500743
Dongsheng Yang8698a742014-03-11 18:09:12 +0800744 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400745 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400746 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500747
James Smart3a55b532008-12-04 22:38:54 -0500748 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400749 /* wait and check worker queue activities */
750 rc = wait_event_interruptible(phba->work_waitq,
751 (test_and_clear_bit(LPFC_DATA_READY,
752 &phba->data_flags)
753 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500754 /* Signal wakeup shall terminate the worker thread */
755 if (rc) {
756 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
757 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500758 break;
James Smart3a55b532008-12-04 22:38:54 -0500759 }
dea31012005-04-17 16:05:31 -0500760
James Smart5e9d9b82008-06-14 22:52:53 -0400761 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500762 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500763 }
James Smart3a55b532008-12-04 22:38:54 -0500764 phba->worker_thread = NULL;
765 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
766 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500767 return 0;
768}
769
770/*
771 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300772 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500773 * embedding it in the IOCB.
774 */
775int
James Smart2e0fef82007-06-17 19:56:36 -0500776lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500777 uint32_t evt)
778{
779 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500780 unsigned long flags;
dea31012005-04-17 16:05:31 -0500781
782 /*
783 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
784 * be queued to worker thread for processing
785 */
James Smart92d7f7b2007-06-17 19:56:38 -0500786 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500787 if (!evtp)
788 return 0;
789
790 evtp->evt_arg1 = arg1;
791 evtp->evt_arg2 = arg2;
792 evtp->evt = evt;
793
James Smarted957682007-06-17 19:56:37 -0500794 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400795 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500796 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500797
James Smart5e9d9b82008-06-14 22:52:53 -0400798 lpfc_worker_wake_up(phba);
799
dea31012005-04-17 16:05:31 -0500800 return 1;
801}
802
James Smart92d7f7b2007-06-17 19:56:38 -0500803void
804lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
805{
James Smart09372822008-01-11 01:52:54 -0500806 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500807 struct lpfc_hba *phba = vport->phba;
808 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500809
810 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500811 if (!NLP_CHK_NODE_ACT(ndlp))
812 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500813 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
814 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400815 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
816 ((vport->port_type == LPFC_NPIV_PORT) &&
817 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500818 lpfc_unreg_rpi(vport, ndlp);
819
820 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400821 if ((phba->sli_rev < LPFC_SLI_REV4) &&
822 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500823 continue;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -0400824 lpfc_disc_state_machine(vport, ndlp, NULL,
825 remove
826 ? NLP_EVT_DEVICE_RM
827 : NLP_EVT_DEVICE_RECOVERY);
James Smart92d7f7b2007-06-17 19:56:38 -0500828 }
829 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400830 if (phba->sli_rev == LPFC_SLI_REV4)
831 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500832 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500833 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500834 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500835 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500836 }
837}
838
James Smart87af33f2007-10-27 13:37:43 -0400839void
James Smart98c9ea52007-10-27 13:37:33 -0400840lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500841{
James Smart695a8142010-01-26 23:08:03 -0500842 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
843
James Smart45ed1192009-10-02 15:17:02 -0400844 /* Cleanup any outstanding received buffers */
845 lpfc_cleanup_rcv_buffers(vport);
846
James Smart92d7f7b2007-06-17 19:56:38 -0500847 /* Cleanup any outstanding RSCN activity */
848 lpfc_els_flush_rscn(vport);
849
850 /* Cleanup any outstanding ELS commands */
851 lpfc_els_flush_cmd(vport);
852
853 lpfc_cleanup_rpis(vport, 0);
854
James Smart92d7f7b2007-06-17 19:56:38 -0500855 /* Turn off discovery timer if its running */
856 lpfc_can_disctmo(vport);
857}
858
James Smart3772a992009-05-22 14:50:54 -0400859void
James Smart98c9ea52007-10-27 13:37:33 -0400860lpfc_linkdown_port(struct lpfc_vport *vport)
861{
862 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
863
James Smartf6e84792019-01-28 11:14:38 -0800864 if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
James Smarta0f2d3e2017-02-12 13:52:31 -0800865 fc_host_post_event(shost, fc_get_event_number(),
866 FCH_EVT_LINKDOWN, 0);
James Smart98c9ea52007-10-27 13:37:33 -0400867
868 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
869 "Link Down: state:x%x rtry:x%x flg:x%x",
870 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
871
872 lpfc_port_link_failure(vport);
873
James Smart92494142011-02-16 12:39:44 -0500874 /* Stop delayed Nport discovery */
875 spin_lock_irq(shost->host_lock);
876 vport->fc_flag &= ~FC_DISC_DELAYED;
877 spin_unlock_irq(shost->host_lock);
878 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400879}
880
dea31012005-04-17 16:05:31 -0500881int
James Smart685f0bf2007-04-25 09:53:08 -0400882lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500883{
James Smart2e0fef82007-06-17 19:56:36 -0500884 struct lpfc_vport *vport = phba->pport;
885 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400886 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400887 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400888 int i;
dea31012005-04-17 16:05:31 -0500889
James Smart19193ff2019-03-12 16:30:25 -0700890 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500891 return 0;
James Smart19193ff2019-03-12 16:30:25 -0700892
James Smartaacc20e2009-12-21 17:02:51 -0500893 /* Block all SCSI stack I/Os */
894 lpfc_scsi_dev_block(phba);
895
James Smart0a9e9682018-11-29 16:09:36 -0800896 phba->defer_flogi_acc_flag = false;
897
James Smart2e0fef82007-06-17 19:56:36 -0500898 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500899 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500900 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500901 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500902 phba->link_state = LPFC_LINK_DOWN;
James Smart2977a092018-12-13 15:17:52 -0800903 if (phba->sli4_hba.conf_trunk) {
904 phba->trunk_link.link0.state = 0;
905 phba->trunk_link.link1.state = 0;
906 phba->trunk_link.link2.state = 0;
907 phba->trunk_link.link3.state = 0;
James Smart845d0322019-08-14 16:57:01 -0700908 phba->sli4_hba.link_state.logical_speed =
909 LPFC_LINK_SPEED_UNKNOWN;
James Smart2977a092018-12-13 15:17:52 -0800910 }
James Smart72100cc2010-02-12 14:43:01 -0500911 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500912 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500913 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500914 }
James Smart549e55c2007-08-02 11:09:51 -0400915 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -0800916 if (vports != NULL) {
James Smart6fb120a2009-05-22 14:52:59 -0400917 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400918 /* Issue a LINK DOWN event to all nodes */
919 lpfc_linkdown_port(vports[i]);
James Smart895427b2017-02-12 13:52:30 -0800920
921 vports[i]->fc_myDID = 0;
922
James Smartf6e84792019-01-28 11:14:38 -0800923 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
924 (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -0800925 if (phba->nvmet_support)
926 lpfc_nvmet_update_targetport(phba);
927 else
James Smart8c258642017-02-12 13:52:36 -0800928 lpfc_nvme_update_localport(vports[i]);
James Smart8c258642017-02-12 13:52:36 -0800929 }
James Smart549e55c2007-08-02 11:09:51 -0400930 }
James Smart895427b2017-02-12 13:52:30 -0800931 }
James Smart09372822008-01-11 01:52:54 -0500932 lpfc_destroy_vport_work_array(phba, vports);
James Smartc95a3b42019-03-12 16:30:07 -0700933
934 /* Clean up any SLI3 firmware default rpi's */
935 if (phba->sli_rev > LPFC_SLI_REV3)
936 goto skip_unreg_did;
937
James Smart2e0fef82007-06-17 19:56:36 -0500938 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
939 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400940 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500941 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500942 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400943 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500944 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500945 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500946 }
947 }
948
James Smartc95a3b42019-03-12 16:30:07 -0700949 skip_unreg_did:
dea31012005-04-17 16:05:31 -0500950 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500951 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500952 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
953 if (mb) {
dea31012005-04-17 16:05:31 -0500954 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500955 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500956 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400957 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500958 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500959 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500960 }
961 }
James Smart2e0fef82007-06-17 19:56:36 -0500962 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500963 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smartd496b9a2018-10-23 13:41:08 -0700964 phba->pport->rcv_flogi_cnt = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500965 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500966 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500967 return 0;
dea31012005-04-17 16:05:31 -0500968}
969
James Smart92d7f7b2007-06-17 19:56:38 -0500970static void
971lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500972{
James Smart92d7f7b2007-06-17 19:56:38 -0500973 struct lpfc_nodelist *ndlp;
974
975 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarta3da8252018-03-05 12:04:06 -0800976 ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
James Smarte47c9092008-02-08 18:49:26 -0500977 if (!NLP_CHK_NODE_ACT(ndlp))
978 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500979 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
980 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500981 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500982 /* On Linkup its safe to clean up the ndlp
983 * from Fabric connections.
984 */
James Smart92d7f7b2007-06-17 19:56:38 -0500985 if (ndlp->nlp_DID != Fabric_DID)
986 lpfc_unreg_rpi(vport, ndlp);
987 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
988 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500989 /* Fail outstanding IO now since device is
990 * marked for PLOGI.
991 */
James Smart92d7f7b2007-06-17 19:56:38 -0500992 lpfc_unreg_rpi(vport, ndlp);
993 }
994 }
995}
996
997static void
998lpfc_linkup_port(struct lpfc_vport *vport)
999{
1000 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05001001 struct lpfc_hba *phba = vport->phba;
1002
1003 if ((vport->load_flag & FC_UNLOADING) != 0)
1004 return;
1005
James Smart858c9f62007-06-17 19:56:39 -05001006 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
1007 "Link Up: top:x%x speed:x%x flg:x%x",
1008 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
1009
James Smart92d7f7b2007-06-17 19:56:38 -05001010 /* If NPIV is not enabled, only bring the physical port up */
1011 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
1012 (vport != phba->pport))
1013 return;
dea31012005-04-17 16:05:31 -05001014
James Smartf6e84792019-01-28 11:14:38 -08001015 if (vport->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
James Smarta0f2d3e2017-02-12 13:52:31 -08001016 fc_host_post_event(shost, fc_get_event_number(),
1017 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001018
James Smart2e0fef82007-06-17 19:56:36 -05001019 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001020 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1021 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1022 vport->fc_flag |= FC_NDISC_ACTIVE;
1023 vport->fc_ns_retry = 0;
1024 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001025
James Smart92d7f7b2007-06-17 19:56:38 -05001026 if (vport->fc_flag & FC_LBIT)
1027 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001028
James Smart92d7f7b2007-06-17 19:56:38 -05001029}
1030
1031static int
1032lpfc_linkup(struct lpfc_hba *phba)
1033{
James Smart549e55c2007-08-02 11:09:51 -04001034 struct lpfc_vport **vports;
1035 int i;
James Smartd496b9a2018-10-23 13:41:08 -07001036 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
James Smart92d7f7b2007-06-17 19:56:38 -05001037
1038 phba->link_state = LPFC_LINK_UP;
1039
1040 /* Unblock fabric iocbs if they are blocked */
1041 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1042 del_timer_sync(&phba->fabric_block_timer);
1043
James Smart549e55c2007-08-02 11:09:51 -04001044 vports = lpfc_create_vport_work_array(phba);
1045 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001046 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001047 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001048 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001049
James Smartd496b9a2018-10-23 13:41:08 -07001050 /* Clear the pport flogi counter in case the link down was
1051 * absorbed without an ACQE. No lock here - in worker thread
1052 * and discovery is synchronized.
1053 */
1054 spin_lock_irq(shost->host_lock);
1055 phba->pport->rcv_flogi_cnt = 0;
1056 spin_unlock_irq(shost->host_lock);
James Smart0a9e9682018-11-29 16:09:36 -08001057
1058 /* reinitialize initial FLOGI flag */
1059 phba->hba_flag &= ~(HBA_FLOGI_ISSUED);
1060 phba->defer_flogi_acc_flag = false;
1061
dea31012005-04-17 16:05:31 -05001062 return 0;
1063}
1064
1065/*
1066 * This routine handles processing a CLEAR_LA mailbox
1067 * command upon completion. It is setup in the LPFC_MBOXQ
1068 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001069 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001070 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001071static void
James Smart2e0fef82007-06-17 19:56:36 -05001072lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001073{
James Smart2e0fef82007-06-17 19:56:36 -05001074 struct lpfc_vport *vport = pmb->vport;
1075 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1076 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001077 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001078 uint32_t control;
1079
dea31012005-04-17 16:05:31 -05001080 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001081 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1082 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001083
1084 /* Check for error */
1085 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001086 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001087 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1088 "0320 CLEAR_LA mbxStatus error x%x hba "
1089 "state x%x\n",
1090 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001091 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001092 goto out;
1093 }
1094
James Smart92d7f7b2007-06-17 19:56:38 -05001095 if (vport->port_type == LPFC_PHYSICAL_PORT)
1096 phba->link_state = LPFC_HBA_READY;
1097
1098 spin_lock_irq(&phba->hbalock);
1099 psli->sli_flag |= LPFC_PROCESS_LA;
1100 control = readl(phba->HCregaddr);
1101 control |= HC_LAINT_ENA;
1102 writel(control, phba->HCregaddr);
1103 readl(phba->HCregaddr); /* flush */
1104 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001105 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001106 return;
dea31012005-04-17 16:05:31 -05001107
dea31012005-04-17 16:05:31 -05001108out:
1109 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001110 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1111 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001112 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001113
James Smart2e0fef82007-06-17 19:56:36 -05001114 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001115 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001116 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001117
James Smart2e0fef82007-06-17 19:56:36 -05001118 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001119
1120 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001121
1122 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001123 psli->sli_flag |= LPFC_PROCESS_LA;
1124 control = readl(phba->HCregaddr);
1125 control |= HC_LAINT_ENA;
1126 writel(control, phba->HCregaddr);
1127 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001128 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001129
1130 return;
1131}
1132
James Smart2e0fef82007-06-17 19:56:36 -05001133
James Smartd6de08c2015-12-16 18:11:53 -05001134void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001135lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001136{
James Smart2e0fef82007-06-17 19:56:36 -05001137 struct lpfc_vport *vport = pmb->vport;
James Smart44fd7fe2017-08-23 16:55:47 -07001138 uint8_t bbscn = 0;
dea31012005-04-17 16:05:31 -05001139
James Smart04c68492009-05-22 14:52:52 -04001140 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001141 goto out;
dea31012005-04-17 16:05:31 -05001142
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001143 mempool_free(pmb, phba->mbox_mem_pool);
1144
James Smart1b511972011-12-13 13:23:09 -05001145 /* don't perform discovery for SLI4 loopback diagnostic test */
1146 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1147 !(phba->hba_flag & HBA_FCOE_MODE) &&
1148 (phba->link_flag & LS_LOOPBACK_MODE))
1149 return;
1150
James Smart76a95d72010-11-20 23:11:48 -05001151 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001152 vport->fc_flag & FC_PUBLIC_LOOP &&
1153 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001154 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001155 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001156 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1157 */
James Smart2e0fef82007-06-17 19:56:36 -05001158 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001159 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001160 }
dea31012005-04-17 16:05:31 -05001161
James Smart2e0fef82007-06-17 19:56:36 -05001162 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001163 * LPFC_FLOGI while waiting for FLOGI cmpl
1164 */
James Smart44fd7fe2017-08-23 16:55:47 -07001165 if (vport->port_state != LPFC_FLOGI) {
1166 if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
1167 bbscn = bf_get(lpfc_bbscn_def,
1168 &phba->sli4_hba.bbscn_params);
1169 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
1170 vport->fc_sparam.cmn.bbRcvSizeMsb |= (bbscn << 4);
1171 }
James Smart92d7f7b2007-06-17 19:56:38 -05001172 lpfc_initial_flogi(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001173 } else if (vport->fc_flag & FC_PT2PT) {
James Smartd6de08c2015-12-16 18:11:53 -05001174 lpfc_disc_start(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001175 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001176 return;
dea31012005-04-17 16:05:31 -05001177
1178out:
James Smarte8b62012007-08-02 11:10:09 -04001179 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1180 "0306 CONFIG_LINK mbxStatus error x%x "
1181 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001182 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001183 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001184
1185 lpfc_linkdown(phba);
1186
James Smarte8b62012007-08-02 11:10:09 -04001187 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1188 "0200 CONFIG_LINK bad hba state x%x\n",
1189 vport->port_state);
dea31012005-04-17 16:05:31 -05001190
James Smart92d7f7b2007-06-17 19:56:38 -05001191 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001192 return;
1193}
1194
James Smart7d791df2011-07-22 18:37:52 -04001195/**
1196 * lpfc_sli4_clear_fcf_rr_bmask
1197 * @phba pointer to the struct lpfc_hba for this port.
1198 * This fucnction resets the round robin bit mask and clears the
1199 * fcf priority list. The list deletions are done while holding the
1200 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1201 * from the lpfc_fcf_pri record.
1202 **/
1203void
1204lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1205{
1206 struct lpfc_fcf_pri *fcf_pri;
1207 struct lpfc_fcf_pri *next_fcf_pri;
1208 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1209 spin_lock_irq(&phba->hbalock);
1210 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1211 &phba->fcf.fcf_pri_list, list) {
1212 list_del_init(&fcf_pri->list);
1213 fcf_pri->fcf_rec.flag = 0;
1214 }
1215 spin_unlock_irq(&phba->hbalock);
1216}
dea31012005-04-17 16:05:31 -05001217static void
James Smart6fb120a2009-05-22 14:52:59 -04001218lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1219{
1220 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001221
1222 if (mboxq->u.mb.mbxStatus) {
1223 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1224 "2017 REG_FCFI mbxStatus error x%x "
1225 "HBA state x%x\n",
1226 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001227 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001228 }
1229
1230 /* Start FCoE discovery by sending a FLOGI. */
1231 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1232 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001233 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001234 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001235 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001236
James Smart32b97932009-07-19 10:01:21 -04001237 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001238 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1239 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001240 goto fail_out;
1241
1242 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001243 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001244 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001245 phba->hba_flag &= ~FCF_TS_INPROG;
1246 if (vport->port_state != LPFC_FLOGI) {
1247 phba->hba_flag |= FCF_RR_INPROG;
1248 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001249 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001250 goto out;
1251 }
1252 spin_unlock_irq(&phba->hbalock);
1253 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001254
James Smarta93ff372010-10-22 11:06:08 -04001255fail_out:
1256 spin_lock_irq(&phba->hbalock);
1257 phba->hba_flag &= ~FCF_RR_INPROG;
1258 spin_unlock_irq(&phba->hbalock);
1259out:
James Smart6fb120a2009-05-22 14:52:59 -04001260 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001261}
1262
1263/**
1264 * lpfc_fab_name_match - Check if the fcf fabric name match.
1265 * @fab_name: pointer to fabric name.
1266 * @new_fcf_record: pointer to fcf record.
1267 *
1268 * This routine compare the fcf record's fabric name with provided
1269 * fabric name. If the fabric name are identical this function
1270 * returns 1 else return 0.
1271 **/
1272static uint32_t
1273lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1274{
James Smartecfd03c2010-02-12 14:41:27 -05001275 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001276 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001277 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1278 return 0;
1279 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1280 return 0;
1281 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1282 return 0;
1283 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1284 return 0;
1285 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1286 return 0;
1287 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1288 return 0;
1289 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1290 return 0;
1291 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001292}
1293
1294/**
James Smart8fa38512009-07-19 10:01:03 -04001295 * lpfc_sw_name_match - Check if the fcf switch name match.
1296 * @fab_name: pointer to fabric name.
1297 * @new_fcf_record: pointer to fcf record.
1298 *
1299 * This routine compare the fcf record's switch name with provided
1300 * switch name. If the switch name are identical this function
1301 * returns 1 else return 0.
1302 **/
1303static uint32_t
1304lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1305{
James Smartecfd03c2010-02-12 14:41:27 -05001306 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001307 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001308 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1309 return 0;
1310 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1311 return 0;
1312 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1313 return 0;
1314 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1315 return 0;
1316 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1317 return 0;
1318 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1319 return 0;
1320 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1321 return 0;
1322 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001323}
1324
1325/**
James Smart6fb120a2009-05-22 14:52:59 -04001326 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001327 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001328 * @new_fcf_record: pointer to fcf record.
1329 *
1330 * This routine compare the fcf record's mac address with HBA's
1331 * FCF mac address. If the mac addresses are identical this function
1332 * returns 1 else return 0.
1333 **/
1334static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001335lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001336{
James Smartecfd03c2010-02-12 14:41:27 -05001337 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001338 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001339 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1340 return 0;
1341 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1342 return 0;
1343 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1344 return 0;
1345 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1346 return 0;
1347 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1348 return 0;
1349 return 1;
1350}
1351
1352static bool
1353lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1354{
1355 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001356}
1357
1358/**
James Smart7d791df2011-07-22 18:37:52 -04001359 * lpfc_update_fcf_record - Update driver fcf record
1360 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1361 * @phba: pointer to lpfc hba data structure.
1362 * @fcf_index: Index for the lpfc_fcf_record.
1363 * @new_fcf_record: pointer to hba fcf record.
1364 *
1365 * This routine updates the driver FCF priority record from the new HBA FCF
1366 * record. This routine is called with the host lock held.
1367 **/
1368static void
1369__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1370 struct fcf_record *new_fcf_record
1371 )
1372{
1373 struct lpfc_fcf_pri *fcf_pri;
1374
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001375 lockdep_assert_held(&phba->hbalock);
1376
James Smart7d791df2011-07-22 18:37:52 -04001377 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1378 fcf_pri->fcf_rec.fcf_index = fcf_index;
1379 /* FCF record priority */
1380 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1381
1382}
1383
1384/**
James Smart6fb120a2009-05-22 14:52:59 -04001385 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001386 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001387 * @new_fcf_record: pointer to fcf record.
1388 *
1389 * This routine copies the FCF information from the FCF
1390 * record to lpfc_hba data structure.
1391 **/
1392static void
James Smartecfd03c2010-02-12 14:41:27 -05001393lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1394 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001395{
James Smartecfd03c2010-02-12 14:41:27 -05001396 /* Fabric name */
1397 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001398 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001399 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001400 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001401 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001402 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001403 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001404 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001405 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001406 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001407 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001408 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001409 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001410 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001411 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001412 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001413 /* Mac address */
1414 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1415 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1416 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1417 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1418 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1419 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1420 /* FCF record index */
1421 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1422 /* FCF record priority */
1423 fcf_rec->priority = new_fcf_record->fip_priority;
1424 /* Switch name */
1425 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001426 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001427 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001428 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001429 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001430 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001431 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001432 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001433 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001434 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001435 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001436 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001437 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001438 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001439 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001440 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001441}
1442
1443/**
James Smartecfd03c2010-02-12 14:41:27 -05001444 * lpfc_update_fcf_record - Update driver fcf record
1445 * @phba: pointer to lpfc hba data structure.
1446 * @fcf_rec: pointer to driver fcf record.
1447 * @new_fcf_record: pointer to hba fcf record.
1448 * @addr_mode: address mode to be set to the driver fcf record.
1449 * @vlan_id: vlan tag to be set to the driver fcf record.
1450 * @flag: flag bits to be set to the driver fcf record.
1451 *
1452 * This routine updates the driver FCF record from the new HBA FCF record
1453 * together with the address mode, vlan_id, and other informations. This
1454 * routine is called with the host lock held.
1455 **/
1456static void
1457__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1458 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1459 uint16_t vlan_id, uint32_t flag)
1460{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001461 lockdep_assert_held(&phba->hbalock);
1462
James Smartecfd03c2010-02-12 14:41:27 -05001463 /* Copy the fields from the HBA's FCF record */
1464 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1465 /* Update other fields of driver FCF record */
1466 fcf_rec->addr_mode = addr_mode;
1467 fcf_rec->vlan_id = vlan_id;
1468 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001469 __lpfc_update_fcf_record_pri(phba,
1470 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1471 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001472}
1473
1474/**
James Smart6fb120a2009-05-22 14:52:59 -04001475 * lpfc_register_fcf - Register the FCF with hba.
1476 * @phba: pointer to lpfc hba data structure.
1477 *
1478 * This routine issues a register fcfi mailbox command to register
1479 * the fcf with HBA.
1480 **/
1481static void
1482lpfc_register_fcf(struct lpfc_hba *phba)
1483{
1484 LPFC_MBOXQ_t *fcf_mbxq;
1485 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001486
James Smartd439d282010-09-29 11:18:45 -04001487 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001488 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001489 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001490 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001491 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001492 return;
1493 }
1494
1495 /* The FCF is already registered, start discovery */
1496 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001497 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001498 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001499 if (phba->pport->port_state != LPFC_FLOGI &&
1500 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001501 phba->hba_flag |= FCF_RR_INPROG;
1502 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001503 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001504 return;
1505 }
1506 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001507 return;
1508 }
James Smartd439d282010-09-29 11:18:45 -04001509 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001510
James Smarta93ff372010-10-22 11:06:08 -04001511 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001512 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001513 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001514 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001515 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001516 return;
James Smart4d9ab992009-10-02 15:16:39 -04001517 }
James Smart6fb120a2009-05-22 14:52:59 -04001518
1519 lpfc_reg_fcfi(phba, fcf_mbxq);
1520 fcf_mbxq->vport = phba->pport;
1521 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1522 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001523 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001524 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001525 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001526 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001527 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001528 }
James Smart6fb120a2009-05-22 14:52:59 -04001529
1530 return;
1531}
1532
1533/**
1534 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1535 * @phba: pointer to lpfc hba data structure.
1536 * @new_fcf_record: pointer to fcf record.
1537 * @boot_flag: Indicates if this record used by boot bios.
1538 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001539 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001540 *
1541 * This routine compare the fcf record with connect list obtained from the
1542 * config region to decide if this FCF can be used for SAN discovery. It returns
1543 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1544 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1545 * is used by boot bios and addr_mode will indicate the addressing mode to be
1546 * used for this FCF when the function returns.
1547 * If the FCF record need to be used with a particular vlan id, the vlan is
1548 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001549 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001550 **/
1551static int
1552lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1553 struct fcf_record *new_fcf_record,
1554 uint32_t *boot_flag, uint32_t *addr_mode,
1555 uint16_t *vlan_id)
1556{
1557 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001558 int i, j, fcf_vlan_id = 0;
1559
1560 /* Find the lowest VLAN id in the FCF record */
1561 for (i = 0; i < 512; i++) {
1562 if (new_fcf_record->vlan_bitmap[i]) {
1563 fcf_vlan_id = i * 8;
1564 j = 0;
1565 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1566 j++;
1567 fcf_vlan_id++;
1568 }
1569 break;
1570 }
1571 }
James Smart6fb120a2009-05-22 14:52:59 -04001572
James Smart26979ce2012-09-29 11:31:55 -04001573 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001574 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001575 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1576 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001577 return 0;
1578
James Smart45ed1192009-10-02 15:17:02 -04001579 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001580 *boot_flag = 0;
1581 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1582 new_fcf_record);
1583 if (phba->valid_vlan)
1584 *vlan_id = phba->vlan_id;
1585 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001586 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001587 return 1;
1588 }
1589
1590 /*
1591 * If there are no FCF connection table entry, driver connect to all
1592 * FCFs.
1593 */
1594 if (list_empty(&phba->fcf_conn_rec_list)) {
1595 *boot_flag = 0;
1596 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1597 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001598
1599 /*
1600 * When there are no FCF connect entries, use driver's default
1601 * addressing mode - FPMA.
1602 */
1603 if (*addr_mode & LPFC_FCF_FPMA)
1604 *addr_mode = LPFC_FCF_FPMA;
1605
James Smart4d9ab992009-10-02 15:16:39 -04001606 /* If FCF record report a vlan id use that vlan id */
1607 if (fcf_vlan_id)
1608 *vlan_id = fcf_vlan_id;
1609 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001610 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001611 return 1;
1612 }
1613
James Smartecfd03c2010-02-12 14:41:27 -05001614 list_for_each_entry(conn_entry,
1615 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001616 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1617 continue;
1618
1619 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1620 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001621 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001622 continue;
James Smart8fa38512009-07-19 10:01:03 -04001623 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1624 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1625 new_fcf_record))
1626 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001627 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1628 /*
1629 * If the vlan bit map does not have the bit set for the
1630 * vlan id to be used, then it is not a match.
1631 */
1632 if (!(new_fcf_record->vlan_bitmap
1633 [conn_entry->conn_rec.vlan_tag / 8] &
1634 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1635 continue;
1636 }
1637
1638 /*
James Smart0c287582009-06-10 17:22:56 -04001639 * If connection record does not support any addressing mode,
1640 * skip the FCF record.
1641 */
1642 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1643 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1644 continue;
1645
1646 /*
James Smart6fb120a2009-05-22 14:52:59 -04001647 * Check if the connection record specifies a required
1648 * addressing mode.
1649 */
1650 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1651 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1652
1653 /*
1654 * If SPMA required but FCF not support this continue.
1655 */
1656 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1657 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1658 new_fcf_record) & LPFC_FCF_SPMA))
1659 continue;
1660
1661 /*
1662 * If FPMA required but FCF not support this continue.
1663 */
1664 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1665 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1666 new_fcf_record) & LPFC_FCF_FPMA))
1667 continue;
1668 }
1669
1670 /*
1671 * This fcf record matches filtering criteria.
1672 */
1673 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1674 *boot_flag = 1;
1675 else
1676 *boot_flag = 0;
1677
James Smart0c287582009-06-10 17:22:56 -04001678 /*
1679 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001680 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001681 * by FCF, allow fabric to pick the addressing mode.
1682 */
James Smart6fb120a2009-05-22 14:52:59 -04001683 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1684 new_fcf_record);
1685 /*
1686 * If the user specified a required address mode, assign that
1687 * address mode
1688 */
1689 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1690 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1691 *addr_mode = (conn_entry->conn_rec.flags &
1692 FCFCNCT_AM_SPMA) ?
1693 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1694 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001695 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001696 * addr mode only if FCF support the addr_mode.
1697 */
1698 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1699 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1700 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1701 (*addr_mode & LPFC_FCF_SPMA))
1702 *addr_mode = LPFC_FCF_SPMA;
1703 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1704 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1705 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1706 (*addr_mode & LPFC_FCF_FPMA))
1707 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001708
James Smart4d9ab992009-10-02 15:16:39 -04001709 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001710 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1711 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001712 /*
1713 * If no vlan id is specified in connect list, use the vlan id
1714 * in the FCF record
1715 */
1716 else if (fcf_vlan_id)
1717 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001718 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001719 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001720
1721 return 1;
1722 }
1723
1724 return 0;
1725}
1726
1727/**
James Smart32b97932009-07-19 10:01:21 -04001728 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1729 * @phba: pointer to lpfc hba data structure.
1730 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1731 *
1732 * This function check if there is any fcoe event pending while driver
1733 * scan FCF entries. If there is any pending event, it will restart the
1734 * FCF saning and return 1 else return 0.
1735 */
1736int
1737lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1738{
James Smart32b97932009-07-19 10:01:21 -04001739 /*
1740 * If the Link is up and no FCoE events while in the
1741 * FCF discovery, no need to restart FCF discovery.
1742 */
1743 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001744 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001745 return 0;
1746
James Smart0c9ab6f2010-02-26 14:15:57 -05001747 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1748 "2768 Pending link or FCF event during current "
1749 "handling of the previous event: link_state:x%x, "
1750 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1751 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1752 phba->fcoe_eventtag);
1753
James Smart32b97932009-07-19 10:01:21 -04001754 spin_lock_irq(&phba->hbalock);
1755 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1756 spin_unlock_irq(&phba->hbalock);
1757
James Smart0c9ab6f2010-02-26 14:15:57 -05001758 if (phba->link_state >= LPFC_LINK_UP) {
1759 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1760 "2780 Restart FCF table scan due to "
1761 "pending FCF event:evt_tag_at_scan:x%x, "
1762 "evt_tag_current:x%x\n",
1763 phba->fcoe_eventtag_at_fcf_scan,
1764 phba->fcoe_eventtag);
1765 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1766 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001767 /*
James Smarta93ff372010-10-22 11:06:08 -04001768 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001769 * flag
1770 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001771 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1772 "2833 Stop FCF discovery process due to link "
1773 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001774 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001775 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001776 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001777 spin_unlock_irq(&phba->hbalock);
1778 }
James Smart32b97932009-07-19 10:01:21 -04001779
James Smart0c9ab6f2010-02-26 14:15:57 -05001780 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001781 if (unreg_fcf) {
1782 spin_lock_irq(&phba->hbalock);
1783 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1784 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001785 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001786 }
James Smart32b97932009-07-19 10:01:21 -04001787 return 1;
1788}
1789
1790/**
James Smart999d8132010-03-15 11:24:56 -04001791 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1792 * @phba: pointer to lpfc hba data structure.
1793 * @fcf_cnt: number of eligible fcf record seen so far.
1794 *
1795 * This function makes an running random selection decision on FCF record to
1796 * use through a sequence of @fcf_cnt eligible FCF records with equal
1797 * probability. To perform integer manunipulation of random numbers with
1798 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001799 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001800 *
1801 * Returns true when outcome is for the newly read FCF record should be
1802 * chosen; otherwise, return false when outcome is for keeping the previously
1803 * chosen FCF record.
1804 **/
1805static bool
1806lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1807{
1808 uint32_t rand_num;
1809
1810 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001811 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001812
1813 /* Decision with probability 1/fcf_cnt */
1814 if ((fcf_cnt * rand_num) < 0xFFFF)
1815 return true;
1816 else
1817 return false;
1818}
1819
1820/**
James Smart3804dc82010-07-14 15:31:37 -04001821 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001822 * @phba: pointer to lpfc hba data structure.
1823 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001824 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001825 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001826 * This routine parses the non-embedded fcf mailbox command by performing the
1827 * necessarily error checking, non-embedded read FCF record mailbox command
1828 * SGE parsing, and endianness swapping.
1829 *
1830 * Returns the pointer to the new FCF record in the non-embedded mailbox
1831 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001832 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001833static struct fcf_record *
1834lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1835 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001836{
1837 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001838 struct lpfc_mbx_sge sge;
1839 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001840 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001841 union lpfc_sli4_cfg_shdr *shdr;
1842 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001843
James Smart6fb120a2009-05-22 14:52:59 -04001844 /* Get the first SGE entry from the non-embedded DMA memory. This
1845 * routine only uses a single SGE.
1846 */
1847 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001848 if (unlikely(!mboxq->sge_array)) {
1849 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1850 "2524 Failed to get the non-embedded SGE "
1851 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001852 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001853 }
1854 virt_addr = mboxq->sge_array->addr[0];
1855
1856 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001857 lpfc_sli_pcimem_bcopy(shdr, shdr,
1858 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001859 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001860 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001861 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001862 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001863 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1864 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001865 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001866 "2726 READ_FCF_RECORD Indicates empty "
1867 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001868 else
1869 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001870 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001871 "with status x%x add_status x%x, "
1872 "mbx\n", shdr_status, shdr_add_status);
1873 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001874 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001875
1876 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001877 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1878 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1879 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001880 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001881 new_fcf_record = (struct fcf_record *)(virt_addr +
1882 sizeof(struct lpfc_mbx_read_fcf_tbl));
1883 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001884 offsetof(struct fcf_record, vlan_bitmap));
1885 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1886 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001887
James Smart0c9ab6f2010-02-26 14:15:57 -05001888 return new_fcf_record;
1889}
1890
1891/**
1892 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1893 * @phba: pointer to lpfc hba data structure.
1894 * @fcf_record: pointer to the fcf record.
1895 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1896 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1897 *
1898 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1899 * enabled.
1900 **/
1901static void
1902lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1903 struct fcf_record *fcf_record,
1904 uint16_t vlan_id,
1905 uint16_t next_fcf_index)
1906{
1907 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1908 "2764 READ_FCF_RECORD:\n"
1909 "\tFCF_Index : x%x\n"
1910 "\tFCF_Avail : x%x\n"
1911 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001912 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001913 "\tFIP_Priority : x%x\n"
1914 "\tMAC_Provider : x%x\n"
1915 "\tLowest VLANID : x%x\n"
1916 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1917 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1918 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1919 "\tNext_FCF_Index: x%x\n",
1920 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1921 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1922 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001923 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001924 fcf_record->fip_priority,
1925 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1926 vlan_id,
1927 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1928 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1929 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1930 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1931 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1932 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1933 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1934 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1935 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1936 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1937 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1938 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1939 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1940 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1941 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1942 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1943 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1944 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1945 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1946 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1947 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1948 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1949 next_fcf_index);
1950}
1951
1952/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001953 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1954 * @phba: pointer to lpfc hba data structure.
1955 * @fcf_rec: pointer to an existing FCF record.
1956 * @new_fcf_record: pointer to a new FCF record.
1957 * @new_vlan_id: vlan id from the new FCF record.
1958 *
1959 * This function performs matching test of a new FCF record against an existing
1960 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1961 * will not be used as part of the FCF record matching criteria.
1962 *
1963 * Returns true if all the fields matching, otherwise returns false.
1964 */
1965static bool
1966lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1967 struct lpfc_fcf_rec *fcf_rec,
1968 struct fcf_record *new_fcf_record,
1969 uint16_t new_vlan_id)
1970{
1971 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1972 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1973 return false;
1974 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1975 return false;
1976 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1977 return false;
1978 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1979 return false;
James Smart7d791df2011-07-22 18:37:52 -04001980 if (fcf_rec->priority != new_fcf_record->fip_priority)
1981 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001982 return true;
1983}
1984
1985/**
James Smarta93ff372010-10-22 11:06:08 -04001986 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1987 * @vport: Pointer to vport object.
1988 * @fcf_index: index to next fcf.
1989 *
1990 * This function processing the roundrobin fcf failover to next fcf index.
1991 * When this function is invoked, there will be a current fcf registered
1992 * for flogi.
1993 * Return: 0 for continue retrying flogi on currently registered fcf;
1994 * 1 for stop flogi on currently registered fcf;
1995 */
1996int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1997{
1998 struct lpfc_hba *phba = vport->phba;
1999 int rc;
2000
2001 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
2002 spin_lock_irq(&phba->hbalock);
2003 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2004 spin_unlock_irq(&phba->hbalock);
2005 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2006 "2872 Devloss tmo with no eligible "
2007 "FCF, unregister in-use FCF (x%x) "
2008 "and rescan FCF table\n",
2009 phba->fcf.current_rec.fcf_indx);
2010 lpfc_unregister_fcf_rescan(phba);
2011 goto stop_flogi_current_fcf;
2012 }
2013 /* Mark the end to FLOGI roundrobin failover */
2014 phba->hba_flag &= ~FCF_RR_INPROG;
2015 /* Allow action to new fcf asynchronous event */
2016 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
2017 spin_unlock_irq(&phba->hbalock);
2018 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2019 "2865 No FCF available, stop roundrobin FCF "
2020 "failover and change port state:x%x/x%x\n",
2021 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2022 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart036cad12018-10-23 13:41:06 -07002023
2024 if (!phba->fcf.fcf_redisc_attempted) {
2025 lpfc_unregister_fcf(phba);
2026
2027 rc = lpfc_sli4_redisc_fcf_table(phba);
2028 if (!rc) {
2029 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2030 "3195 Rediscover FCF table\n");
2031 phba->fcf.fcf_redisc_attempted = 1;
2032 lpfc_sli4_clear_fcf_rr_bmask(phba);
2033 } else {
2034 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2035 "3196 Rediscover FCF table "
2036 "failed. Status:x%x\n", rc);
2037 }
2038 } else {
2039 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2040 "3197 Already rediscover FCF table "
2041 "attempted. No more retry\n");
2042 }
James Smarta93ff372010-10-22 11:06:08 -04002043 goto stop_flogi_current_fcf;
2044 } else {
2045 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2046 "2794 Try FLOGI roundrobin FCF failover to "
2047 "(x%x)\n", fcf_index);
2048 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2049 if (rc)
2050 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2051 "2761 FLOGI roundrobin FCF failover "
2052 "failed (rc:x%x) to read FCF (x%x)\n",
2053 rc, phba->fcf.current_rec.fcf_indx);
2054 else
2055 goto stop_flogi_current_fcf;
2056 }
2057 return 0;
2058
2059stop_flogi_current_fcf:
2060 lpfc_can_disctmo(vport);
2061 return 1;
2062}
2063
2064/**
James Smart7d791df2011-07-22 18:37:52 -04002065 * lpfc_sli4_fcf_pri_list_del
2066 * @phba: pointer to lpfc hba data structure.
2067 * @fcf_index the index of the fcf record to delete
2068 * This routine checks the on list flag of the fcf_index to be deleted.
2069 * If it is one the list then it is removed from the list, and the flag
2070 * is cleared. This routine grab the hbalock before removing the fcf
2071 * record from the list.
2072 **/
2073static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2074 uint16_t fcf_index)
2075{
2076 struct lpfc_fcf_pri *new_fcf_pri;
2077
2078 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2079 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2080 "3058 deleting idx x%x pri x%x flg x%x\n",
2081 fcf_index, new_fcf_pri->fcf_rec.priority,
2082 new_fcf_pri->fcf_rec.flag);
2083 spin_lock_irq(&phba->hbalock);
2084 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2085 if (phba->fcf.current_rec.priority ==
2086 new_fcf_pri->fcf_rec.priority)
2087 phba->fcf.eligible_fcf_cnt--;
2088 list_del_init(&new_fcf_pri->list);
2089 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2090 }
2091 spin_unlock_irq(&phba->hbalock);
2092}
2093
2094/**
2095 * lpfc_sli4_set_fcf_flogi_fail
2096 * @phba: pointer to lpfc hba data structure.
2097 * @fcf_index the index of the fcf record to update
2098 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2099 * flag so the the round robin slection for the particular priority level
2100 * will try a different fcf record that does not have this bit set.
2101 * If the fcf record is re-read for any reason this flag is cleared brfore
2102 * adding it to the priority list.
2103 **/
2104void
2105lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2106{
2107 struct lpfc_fcf_pri *new_fcf_pri;
2108 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2109 spin_lock_irq(&phba->hbalock);
2110 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2111 spin_unlock_irq(&phba->hbalock);
2112}
2113
2114/**
2115 * lpfc_sli4_fcf_pri_list_add
2116 * @phba: pointer to lpfc hba data structure.
2117 * @fcf_index the index of the fcf record to add
2118 * This routine checks the priority of the fcf_index to be added.
2119 * If it is a lower priority than the current head of the fcf_pri list
2120 * then it is added to the list in the right order.
2121 * If it is the same priority as the current head of the list then it
2122 * is added to the head of the list and its bit in the rr_bmask is set.
2123 * If the fcf_index to be added is of a higher priority than the current
2124 * head of the list then the rr_bmask is cleared, its bit is set in the
2125 * rr_bmask and it is added to the head of the list.
2126 * returns:
2127 * 0=success 1=failure
2128 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002129static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2130 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002131 struct fcf_record *new_fcf_record)
2132{
2133 uint16_t current_fcf_pri;
2134 uint16_t last_index;
2135 struct lpfc_fcf_pri *fcf_pri;
2136 struct lpfc_fcf_pri *next_fcf_pri;
2137 struct lpfc_fcf_pri *new_fcf_pri;
2138 int ret;
2139
2140 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2141 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2142 "3059 adding idx x%x pri x%x flg x%x\n",
2143 fcf_index, new_fcf_record->fip_priority,
2144 new_fcf_pri->fcf_rec.flag);
2145 spin_lock_irq(&phba->hbalock);
2146 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2147 list_del_init(&new_fcf_pri->list);
2148 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2149 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2150 if (list_empty(&phba->fcf.fcf_pri_list)) {
2151 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2152 ret = lpfc_sli4_fcf_rr_index_set(phba,
2153 new_fcf_pri->fcf_rec.fcf_index);
2154 goto out;
2155 }
2156
2157 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2158 LPFC_SLI4_FCF_TBL_INDX_MAX);
2159 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2160 ret = 0; /* Empty rr list */
2161 goto out;
2162 }
2163 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2164 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2165 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2166 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2167 memset(phba->fcf.fcf_rr_bmask, 0,
2168 sizeof(*phba->fcf.fcf_rr_bmask));
2169 /* fcfs_at_this_priority_level = 1; */
2170 phba->fcf.eligible_fcf_cnt = 1;
2171 } else
2172 /* fcfs_at_this_priority_level++; */
2173 phba->fcf.eligible_fcf_cnt++;
2174 ret = lpfc_sli4_fcf_rr_index_set(phba,
2175 new_fcf_pri->fcf_rec.fcf_index);
2176 goto out;
2177 }
2178
2179 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2180 &phba->fcf.fcf_pri_list, list) {
2181 if (new_fcf_pri->fcf_rec.priority <=
2182 fcf_pri->fcf_rec.priority) {
2183 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2184 list_add(&new_fcf_pri->list,
2185 &phba->fcf.fcf_pri_list);
2186 else
2187 list_add(&new_fcf_pri->list,
2188 &((struct lpfc_fcf_pri *)
2189 fcf_pri->list.prev)->list);
2190 ret = 0;
2191 goto out;
2192 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2193 || new_fcf_pri->fcf_rec.priority <
2194 next_fcf_pri->fcf_rec.priority) {
2195 list_add(&new_fcf_pri->list, &fcf_pri->list);
2196 ret = 0;
2197 goto out;
2198 }
2199 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2200 continue;
2201
2202 }
2203 ret = 1;
2204out:
2205 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2206 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2207 spin_unlock_irq(&phba->hbalock);
2208 return ret;
2209}
2210
2211/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2213 * @phba: pointer to lpfc hba data structure.
2214 * @mboxq: pointer to mailbox object.
2215 *
2216 * This function iterates through all the fcf records available in
2217 * HBA and chooses the optimal FCF record for discovery. After finding
2218 * the FCF for discovery it registers the FCF record and kicks start
2219 * discovery.
2220 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2221 * use an FCF record which matches fabric name and mac address of the
2222 * currently used FCF record.
2223 * If the driver supports only one FCF, it will try to use the FCF record
2224 * used by BOOT_BIOS.
2225 */
2226void
2227lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2228{
2229 struct fcf_record *new_fcf_record;
2230 uint32_t boot_flag, addr_mode;
2231 uint16_t fcf_index, next_fcf_index;
2232 struct lpfc_fcf_rec *fcf_rec = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08002233 uint16_t vlan_id = LPFC_FCOE_NULL_VID;
James Smart999d8132010-03-15 11:24:56 -04002234 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002235 int rc;
2236
2237 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002238 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002239 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2240 return;
2241 }
2242
2243 /* Parse the FCF record from the non-embedded mailbox command */
2244 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2245 &next_fcf_index);
2246 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002247 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002248 "2765 Mailbox command READ_FCF_RECORD "
2249 "failed to retrieve a FCF record.\n");
2250 /* Let next new FCF event trigger fast failover */
2251 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002252 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002253 spin_unlock_irq(&phba->hbalock);
2254 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2255 return;
2256 }
2257
2258 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002259 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2260 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002261
2262 /* Log the FCF record information if turned on */
2263 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2264 next_fcf_index);
2265
James Smart6fb120a2009-05-22 14:52:59 -04002266 /*
2267 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002268 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002269 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002270 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002271 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002272 lpfc_sli4_fcf_pri_list_del(phba,
2273 bf_get(lpfc_fcf_record_fcf_index,
2274 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002275 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002276 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002277 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002278 bf_get(lpfc_fcf_record_fcf_index,
2279 new_fcf_record),
2280 bf_get(lpfc_fcf_record_fcf_avail,
2281 new_fcf_record),
2282 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002283 new_fcf_record),
2284 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002285 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002286 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2287 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2288 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002289 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2290 phba->fcf.current_rec.fcf_indx) {
2291 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2292 "2862 FCF (x%x) matches property "
2293 "of in-use FCF (x%x)\n",
2294 bf_get(lpfc_fcf_record_fcf_index,
2295 new_fcf_record),
2296 phba->fcf.current_rec.fcf_indx);
2297 goto read_next_fcf;
2298 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002299 /*
2300 * In case the current in-use FCF record becomes
2301 * invalid/unavailable during FCF discovery that
2302 * was not triggered by fast FCF failover process,
2303 * treat it as fast FCF failover.
2304 */
2305 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2306 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2307 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2308 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002309 "(x%x), enter FCF failover "
2310 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002311 phba->fcf.current_rec.fcf_indx);
2312 spin_lock_irq(&phba->hbalock);
2313 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2314 spin_unlock_irq(&phba->hbalock);
2315 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2316 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2317 LPFC_FCOE_FCF_GET_FIRST);
2318 return;
2319 }
2320 }
James Smart6fb120a2009-05-22 14:52:59 -04002321 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002322 } else {
2323 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002324 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2325 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002326 if (rc)
2327 goto read_next_fcf;
2328 }
2329
James Smart6fb120a2009-05-22 14:52:59 -04002330 /*
2331 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002332 * FCF record for the discovery. The condition that a rescan
2333 * matches the in-use FCF record: fabric name, switch name, mac
2334 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002335 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002336 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002337 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002338 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2339 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002340 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002341 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2342 phba->fcf.current_rec.fcf_indx) {
2343 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2344 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2345 /* Stop FCF redisc wait timer */
2346 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2347 phba);
2348 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2349 /* Fast failover, mark completed */
2350 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2351 spin_unlock_irq(&phba->hbalock);
2352 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2353 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002354 "FCF (x%x), port_state:x%x, "
2355 "fc_flag:x%x\n",
2356 phba->fcf.current_rec.fcf_indx,
2357 phba->pport->port_state,
2358 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002359 goto out;
2360 } else
2361 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2362 "2863 New FCF (x%x) matches "
2363 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002364 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002365 new_fcf_record),
2366 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002367 }
James Smartecfd03c2010-02-12 14:41:27 -05002368 /*
2369 * Read next FCF record from HBA searching for the matching
2370 * with in-use record only if not during the fast failover
2371 * period. In case of fast failover period, it shall try to
2372 * determine whether the FCF record just read should be the
2373 * next candidate.
2374 */
2375 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002376 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002377 goto read_next_fcf;
2378 }
James Smart6fb120a2009-05-22 14:52:59 -04002379 }
James Smartecfd03c2010-02-12 14:41:27 -05002380 /*
2381 * Update on failover FCF record only if it's in FCF fast-failover
2382 * period; otherwise, update on current FCF record.
2383 */
James Smartfc2b9892010-02-26 14:15:29 -05002384 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2385 fcf_rec = &phba->fcf.failover_rec;
2386 else
James Smartecfd03c2010-02-12 14:41:27 -05002387 fcf_rec = &phba->fcf.current_rec;
2388
James Smart6fb120a2009-05-22 14:52:59 -04002389 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2390 /*
James Smartecfd03c2010-02-12 14:41:27 -05002391 * If the driver FCF record does not have boot flag
2392 * set and new hba fcf record has boot flag set, use
2393 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002394 */
James Smartecfd03c2010-02-12 14:41:27 -05002395 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2396 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002397 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2398 "2837 Update current FCF record "
2399 "(x%x) with new FCF record (x%x)\n",
2400 fcf_rec->fcf_indx,
2401 bf_get(lpfc_fcf_record_fcf_index,
2402 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002403 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2404 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002405 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002406 goto read_next_fcf;
2407 }
2408 /*
James Smartecfd03c2010-02-12 14:41:27 -05002409 * If the driver FCF record has boot flag set and the
2410 * new hba FCF record does not have boot flag, read
2411 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002412 */
James Smartecfd03c2010-02-12 14:41:27 -05002413 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002414 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002415 goto read_next_fcf;
2416 }
2417 /*
James Smartecfd03c2010-02-12 14:41:27 -05002418 * If the new hba FCF record has lower priority value
2419 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002420 */
James Smartfc2b9892010-02-26 14:15:29 -05002421 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002422 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002423 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2424 "2838 Update current FCF record "
2425 "(x%x) with new FCF record (x%x)\n",
2426 fcf_rec->fcf_indx,
2427 bf_get(lpfc_fcf_record_fcf_index,
2428 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002429 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2430 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002431 /* Reset running random FCF selection count */
2432 phba->fcf.eligible_fcf_cnt = 1;
2433 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2434 /* Update running random FCF selection count */
2435 phba->fcf.eligible_fcf_cnt++;
2436 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2437 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002438 if (select_new_fcf) {
2439 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2440 "2839 Update current FCF record "
2441 "(x%x) with new FCF record (x%x)\n",
2442 fcf_rec->fcf_indx,
2443 bf_get(lpfc_fcf_record_fcf_index,
2444 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002445 /* Choose the new FCF by random selection */
2446 __lpfc_update_fcf_record(phba, fcf_rec,
2447 new_fcf_record,
2448 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002449 }
James Smart6fb120a2009-05-22 14:52:59 -04002450 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002451 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002452 goto read_next_fcf;
2453 }
2454 /*
James Smartecfd03c2010-02-12 14:41:27 -05002455 * This is the first suitable FCF record, choose this record for
2456 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002457 */
James Smartecfd03c2010-02-12 14:41:27 -05002458 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002459 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002460 "2840 Update initial FCF candidate "
2461 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002462 bf_get(lpfc_fcf_record_fcf_index,
2463 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002464 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2465 addr_mode, vlan_id, (boot_flag ?
2466 BOOT_ENABLE : 0));
2467 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002468 /* Setup initial running random FCF selection count */
2469 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002470 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002471 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002472 goto read_next_fcf;
2473
2474read_next_fcf:
2475 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002476 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2477 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2478 /*
2479 * Case of FCF fast failover scan
2480 */
2481
2482 /*
2483 * It has not found any suitable FCF record, cancel
2484 * FCF scan inprogress, and do nothing
2485 */
2486 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002487 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002488 "2782 No suitable FCF found: "
2489 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002490 phba->fcoe_eventtag_at_fcf_scan,
2491 bf_get(lpfc_fcf_record_fcf_index,
2492 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002493 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002494 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2495 phba->hba_flag &= ~FCF_TS_INPROG;
2496 spin_unlock_irq(&phba->hbalock);
2497 /* Unregister in-use FCF and rescan */
2498 lpfc_printf_log(phba, KERN_INFO,
2499 LOG_FIP,
2500 "2864 On devloss tmo "
2501 "unreg in-use FCF and "
2502 "rescan FCF table\n");
2503 lpfc_unregister_fcf_rescan(phba);
2504 return;
2505 }
2506 /*
2507 * Let next new FCF event trigger fast failover
2508 */
2509 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002510 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002511 return;
2512 }
2513 /*
2514 * It has found a suitable FCF record that is not
2515 * the same as in-use FCF record, unregister the
2516 * in-use FCF record, replace the in-use FCF record
2517 * with the new FCF record, mark FCF fast failover
2518 * completed, and then start register the new FCF
2519 * record.
2520 */
2521
James Smart0c9ab6f2010-02-26 14:15:57 -05002522 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002523 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002524
2525 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002526 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002527 "2842 Replace in-use FCF (x%x) "
2528 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002529 phba->fcf.current_rec.fcf_indx,
2530 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002531 memcpy(&phba->fcf.current_rec,
2532 &phba->fcf.failover_rec,
2533 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002534 /*
2535 * Mark the fast FCF failover rediscovery completed
2536 * and the start of the first round of the roundrobin
2537 * FCF failover.
2538 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002539 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002540 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002541 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002542 /* Register to the new FCF record */
2543 lpfc_register_fcf(phba);
2544 } else {
2545 /*
2546 * In case of transaction period to fast FCF failover,
2547 * do nothing when search to the end of the FCF table.
2548 */
2549 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2550 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2551 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002552
James Smart7d791df2011-07-22 18:37:52 -04002553 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2554 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002555 /*
2556 * In case the current in-use FCF record no
2557 * longer existed during FCF discovery that
2558 * was not triggered by fast FCF failover
2559 * process, treat it as fast FCF failover.
2560 */
2561 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2562 "2841 In-use FCF record (x%x) "
2563 "not reported, entering fast "
2564 "FCF failover mode scanning.\n",
2565 phba->fcf.current_rec.fcf_indx);
2566 spin_lock_irq(&phba->hbalock);
2567 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2568 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002569 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2570 LPFC_FCOE_FCF_GET_FIRST);
2571 return;
2572 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002573 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002574 lpfc_register_fcf(phba);
2575 }
2576 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002577 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002578 return;
2579
2580out:
2581 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2582 lpfc_register_fcf(phba);
2583
2584 return;
2585}
2586
2587/**
James Smarta93ff372010-10-22 11:06:08 -04002588 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002589 * @phba: pointer to lpfc hba data structure.
2590 * @mboxq: pointer to mailbox object.
2591 *
James Smarta93ff372010-10-22 11:06:08 -04002592 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002593 * read FCF record mailbox command from the eligible FCF record bmask for
2594 * performing the failover. If the FCF read back is not valid/available, it
2595 * fails through to retrying FLOGI to the currently registered FCF again.
2596 * Otherwise, if the FCF read back is valid and available, it will set the
2597 * newly read FCF record to the failover FCF record, unregister currently
2598 * registered FCF record, copy the failover FCF record to the current
2599 * FCF record, and then register the current FCF record before proceeding
2600 * to trying FLOGI on the new failover FCF.
2601 */
2602void
2603lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2604{
2605 struct fcf_record *new_fcf_record;
2606 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002607 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002608 uint16_t current_fcf_index;
2609 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002610 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002611
James Smarta93ff372010-10-22 11:06:08 -04002612 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 if (phba->link_state < LPFC_LINK_UP) {
2614 spin_lock_irq(&phba->hbalock);
2615 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002616 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002617 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002618 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002619 }
2620
2621 /* Parse the FCF record from the non-embedded mailbox command */
2622 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2623 &next_fcf_index);
2624 if (!new_fcf_record) {
2625 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2626 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002627 "failed to retrieve a FCF record. "
2628 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2629 phba->fcf.fcf_flag);
2630 lpfc_unregister_fcf_rescan(phba);
2631 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002632 }
2633
2634 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002635 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2636 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002637
2638 /* Log the FCF record information if turned on */
2639 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2640 next_fcf_index);
2641
James Smarta93ff372010-10-22 11:06:08 -04002642 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2643 if (!rc) {
2644 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2645 "2848 Remove ineligible FCF (x%x) from "
2646 "from roundrobin bmask\n", fcf_index);
2647 /* Clear roundrobin bmask bit for ineligible FCF */
2648 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2649 /* Perform next round of roundrobin FCF failover */
2650 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2651 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2652 if (rc)
2653 goto out;
2654 goto error_out;
2655 }
2656
2657 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2658 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2659 "2760 Perform FLOGI roundrobin FCF failover: "
2660 "FCF (x%x) back to FCF (x%x)\n",
2661 phba->fcf.current_rec.fcf_indx, fcf_index);
2662 /* Wait 500 ms before retrying FLOGI to current FCF */
2663 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002664 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002665 goto out;
2666 }
2667
James Smart0c9ab6f2010-02-26 14:15:57 -05002668 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002669 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002670 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2671 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002672 spin_lock_irq(&phba->hbalock);
2673 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2674 new_fcf_record, addr_mode, vlan_id,
2675 (boot_flag ? BOOT_ENABLE : 0));
2676 spin_unlock_irq(&phba->hbalock);
2677
2678 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2679
2680 /* Unregister the current in-use FCF record */
2681 lpfc_unregister_fcf(phba);
2682
2683 /* Replace in-use record with the new record */
2684 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2685 sizeof(struct lpfc_fcf_rec));
2686
2687 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002688 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2689 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002690
James Smarta93ff372010-10-22 11:06:08 -04002691error_out:
2692 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002693out:
2694 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002695}
2696
2697/**
2698 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2699 * @phba: pointer to lpfc hba data structure.
2700 * @mboxq: pointer to mailbox object.
2701 *
2702 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002703 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002704 * failover when a new FCF event happened. If the FCF read back is
2705 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002706 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002707 */
2708void
2709lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2710{
2711 struct fcf_record *new_fcf_record;
2712 uint32_t boot_flag, addr_mode;
2713 uint16_t fcf_index, next_fcf_index;
2714 uint16_t vlan_id;
2715 int rc;
2716
2717 /* If link state is not up, no need to proceed */
2718 if (phba->link_state < LPFC_LINK_UP)
2719 goto out;
2720
2721 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002722 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002723 goto out;
2724
2725 /* Parse the FCF record from the non-embedded mailbox command */
2726 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2727 &next_fcf_index);
2728 if (!new_fcf_record) {
2729 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2730 "2767 Mailbox command READ_FCF_RECORD "
2731 "failed to retrieve a FCF record.\n");
2732 goto out;
2733 }
2734
2735 /* Check the connection list for eligibility */
2736 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2737 &addr_mode, &vlan_id);
2738
2739 /* Log the FCF record information if turned on */
2740 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2741 next_fcf_index);
2742
2743 if (!rc)
2744 goto out;
2745
2746 /* Update the eligible FCF record index bmask */
2747 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002748
2749 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002750
2751out:
2752 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2753}
2754
2755/**
James Smart76a95d72010-11-20 23:11:48 -05002756 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2757 * @phba: pointer to lpfc hba data structure.
2758 * @mboxq: pointer to mailbox data structure.
2759 *
2760 * This function handles completion of init vfi mailbox command.
2761 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002762static void
James Smart76a95d72010-11-20 23:11:48 -05002763lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2764{
2765 struct lpfc_vport *vport = mboxq->vport;
2766
James Smartdf9e1b52011-12-13 13:22:17 -05002767 /*
2768 * VFI not supported on interface type 0, just do the flogi
2769 * Also continue if the VFI is in use - just use the same one.
2770 */
2771 if (mboxq->u.mb.mbxStatus &&
2772 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2773 LPFC_SLI_INTF_IF_TYPE_0) &&
2774 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002775 lpfc_printf_vlog(vport, KERN_ERR,
2776 LOG_MBOX,
2777 "2891 Init VFI mailbox failed 0x%x\n",
2778 mboxq->u.mb.mbxStatus);
2779 mempool_free(mboxq, phba->mbox_mem_pool);
2780 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2781 return;
2782 }
James Smart73d91e52011-10-10 21:32:10 -04002783
James Smart76a95d72010-11-20 23:11:48 -05002784 lpfc_initial_flogi(vport);
2785 mempool_free(mboxq, phba->mbox_mem_pool);
2786 return;
2787}
2788
2789/**
2790 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2791 * @vport: pointer to lpfc_vport data structure.
2792 *
2793 * This function issue a init_vfi mailbox command to initialize the VFI and
2794 * VPI for the physical port.
2795 */
2796void
2797lpfc_issue_init_vfi(struct lpfc_vport *vport)
2798{
2799 LPFC_MBOXQ_t *mboxq;
2800 int rc;
2801 struct lpfc_hba *phba = vport->phba;
2802
2803 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2804 if (!mboxq) {
2805 lpfc_printf_vlog(vport, KERN_ERR,
2806 LOG_MBOX, "2892 Failed to allocate "
2807 "init_vfi mailbox\n");
2808 return;
2809 }
2810 lpfc_init_vfi(mboxq, vport);
2811 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2812 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2813 if (rc == MBX_NOT_FINISHED) {
2814 lpfc_printf_vlog(vport, KERN_ERR,
2815 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2816 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2817 }
2818}
2819
2820/**
James Smart1c6834a2009-07-19 10:01:26 -04002821 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2822 * @phba: pointer to lpfc hba data structure.
2823 * @mboxq: pointer to mailbox data structure.
2824 *
2825 * This function handles completion of init vpi mailbox command.
2826 */
James Smart695a8142010-01-26 23:08:03 -05002827void
James Smart1c6834a2009-07-19 10:01:26 -04002828lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2829{
2830 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002831 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002832 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2833
James Smart1c6834a2009-07-19 10:01:26 -04002834 if (mboxq->u.mb.mbxStatus) {
2835 lpfc_printf_vlog(vport, KERN_ERR,
2836 LOG_MBOX,
2837 "2609 Init VPI mailbox failed 0x%x\n",
2838 mboxq->u.mb.mbxStatus);
2839 mempool_free(mboxq, phba->mbox_mem_pool);
2840 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2841 return;
2842 }
James Smart72100cc2010-02-12 14:43:01 -05002843 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002844 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002845 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002846
James Smart695a8142010-01-26 23:08:03 -05002847 /* If this port is physical port or FDISC is done, do reg_vpi */
2848 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2849 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2850 if (!ndlp)
2851 lpfc_printf_vlog(vport, KERN_ERR,
2852 LOG_DISCOVERY,
2853 "2731 Cannot find fabric "
2854 "controller node\n");
2855 else
2856 lpfc_register_new_vport(phba, vport, ndlp);
2857 mempool_free(mboxq, phba->mbox_mem_pool);
2858 return;
2859 }
2860
James Smart1c6834a2009-07-19 10:01:26 -04002861 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2862 lpfc_initial_fdisc(vport);
2863 else {
2864 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002865 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2866 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002867 }
James Smart695a8142010-01-26 23:08:03 -05002868 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002869 return;
2870}
2871
2872/**
James Smartecfd03c2010-02-12 14:41:27 -05002873 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2874 * @vport: pointer to lpfc_vport data structure.
2875 *
2876 * This function issue a init_vpi mailbox command to initialize
2877 * VPI for the vport.
2878 */
2879void
2880lpfc_issue_init_vpi(struct lpfc_vport *vport)
2881{
2882 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002883 int rc, vpi;
2884
2885 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2886 vpi = lpfc_alloc_vpi(vport->phba);
2887 if (!vpi) {
2888 lpfc_printf_vlog(vport, KERN_ERR,
2889 LOG_MBOX,
2890 "3303 Failed to obtain vport vpi\n");
2891 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2892 return;
2893 }
2894 vport->vpi = vpi;
2895 }
James Smartecfd03c2010-02-12 14:41:27 -05002896
2897 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2898 if (!mboxq) {
2899 lpfc_printf_vlog(vport, KERN_ERR,
2900 LOG_MBOX, "2607 Failed to allocate "
2901 "init_vpi mailbox\n");
2902 return;
2903 }
2904 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2905 mboxq->vport = vport;
2906 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2907 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2908 if (rc == MBX_NOT_FINISHED) {
2909 lpfc_printf_vlog(vport, KERN_ERR,
2910 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2911 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2912 }
2913}
2914
2915/**
James Smart6fb120a2009-05-22 14:52:59 -04002916 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2917 * @phba: pointer to lpfc hba data structure.
2918 *
2919 * This function loops through the list of vports on the @phba and issues an
2920 * FDISC if possible.
2921 */
2922void
2923lpfc_start_fdiscs(struct lpfc_hba *phba)
2924{
2925 struct lpfc_vport **vports;
2926 int i;
2927
2928 vports = lpfc_create_vport_work_array(phba);
2929 if (vports != NULL) {
2930 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2931 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2932 continue;
2933 /* There are no vpi for this vport */
2934 if (vports[i]->vpi > phba->max_vpi) {
2935 lpfc_vport_set_state(vports[i],
2936 FC_VPORT_FAILED);
2937 continue;
2938 }
James Smart76a95d72010-11-20 23:11:48 -05002939 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002940 lpfc_vport_set_state(vports[i],
2941 FC_VPORT_LINKDOWN);
2942 continue;
2943 }
James Smart1c6834a2009-07-19 10:01:26 -04002944 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002945 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002946 continue;
2947 }
James Smart6fb120a2009-05-22 14:52:59 -04002948 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2949 lpfc_initial_fdisc(vports[i]);
2950 else {
2951 lpfc_vport_set_state(vports[i],
2952 FC_VPORT_NO_FABRIC_SUPP);
2953 lpfc_printf_vlog(vports[i], KERN_ERR,
2954 LOG_ELS,
2955 "0259 No NPIV "
2956 "Fabric support\n");
2957 }
2958 }
2959 }
2960 lpfc_destroy_vport_work_array(phba, vports);
2961}
2962
2963void
2964lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2965{
James Smart3e1f0712018-11-29 16:09:29 -08002966 struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
James Smart6fb120a2009-05-22 14:52:59 -04002967 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002968 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002969
James Smartf5eca9b2012-03-01 22:37:54 -05002970 /*
2971 * VFI not supported for interface type 0, so ignore any mailbox
2972 * error (except VFI in use) and continue with the discovery.
2973 */
2974 if (mboxq->u.mb.mbxStatus &&
2975 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2976 LPFC_SLI_INTF_IF_TYPE_0) &&
2977 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002978 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2979 "2018 REG_VFI mbxStatus error x%x "
2980 "HBA state x%x\n",
2981 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002982 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002983 /* FLOGI failed, use loop map to make discovery list */
2984 lpfc_disc_list_loopmap(vport);
2985 /* Start discovery */
2986 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002987 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002988 }
2989 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002990 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002991 }
James Smartae05ebe2013-03-01 16:35:38 -05002992
James Smarte74c03c2013-04-17 20:15:19 -04002993 /* If the VFI is already registered, there is nothing else to do
2994 * Unless this was a VFI update and we are in PT2PT mode, then
2995 * we should drop through to set the port state to ready.
2996 */
James Smartae05ebe2013-03-01 16:35:38 -05002997 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002998 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2999 vport->fc_flag & FC_PT2PT))
3000 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05003001
James Smartc8685952009-11-18 15:39:16 -05003002 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05003003 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003004 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003005 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003006 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04003007 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003008 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04003009
James Smart1b511972011-12-13 13:23:09 -05003010 /* In case SLI4 FC loopback test, we are ready */
3011 if ((phba->sli_rev == LPFC_SLI_REV4) &&
3012 (phba->link_flag & LS_LOOPBACK_MODE)) {
3013 phba->link_state = LPFC_HBA_READY;
3014 goto out_free_mem;
3015 }
3016
James Smarte74c03c2013-04-17 20:15:19 -04003017 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3018 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
3019 "alpacnt:%d LinkState:%x topology:%x\n",
3020 vport->port_state, vport->fc_flag, vport->fc_myDID,
3021 vport->phba->alpa_map[0],
3022 phba->link_state, phba->fc_topology);
3023
James Smart6fb120a2009-05-22 14:52:59 -04003024 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04003025 /*
3026 * For private loop or for NPort pt2pt,
3027 * just start discovery and we are done.
3028 */
3029 if ((vport->fc_flag & FC_PT2PT) ||
3030 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3031 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3032
James Smart76a95d72010-11-20 23:11:48 -05003033 /* Use loop map to make discovery list */
3034 lpfc_disc_list_loopmap(vport);
3035 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04003036 if (vport->fc_flag & FC_PT2PT)
3037 vport->port_state = LPFC_VPORT_READY;
3038 else
3039 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05003040 } else {
3041 lpfc_start_fdiscs(phba);
3042 lpfc_do_scr_ns_plogi(phba, vport);
3043 }
James Smart6fb120a2009-05-22 14:52:59 -04003044 }
3045
James Smart1b511972011-12-13 13:23:09 -05003046out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04003047 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05003048 if (dmabuf) {
3049 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3050 kfree(dmabuf);
3051 }
James Smart6fb120a2009-05-22 14:52:59 -04003052 return;
3053}
3054
3055static void
James Smart2e0fef82007-06-17 19:56:36 -05003056lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003057{
James Smart6fb120a2009-05-22 14:52:59 -04003058 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003059 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart2e0fef82007-06-17 19:56:36 -05003060 struct lpfc_vport *vport = pmb->vport;
James Smartaeb3c812017-04-21 16:05:02 -07003061 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart21bf0b92015-08-31 16:48:21 -04003062 struct serv_parm *sp = &vport->fc_sparam;
3063 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003064
3065 /* Check for error */
3066 if (mb->mbxStatus) {
3067 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003068 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3069 "0319 READ_SPARAM mbxStatus error x%x "
3070 "hba state x%x>\n",
3071 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003072 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003073 goto out;
3074 }
3075
James Smart2e0fef82007-06-17 19:56:36 -05003076 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003077 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003078
3079 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3080 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3081 ed_tov = (ed_tov + 999999) / 1000000;
3082
3083 phba->fc_edtov = ed_tov;
3084 phba->fc_ratov = (2 * ed_tov) / 1000;
3085 if (phba->fc_ratov < FF_DEF_RATOV) {
3086 /* RA_TOV should be atleast 10sec for initial flogi */
3087 phba->fc_ratov = FF_DEF_RATOV;
3088 }
3089
James Smart05580562011-05-24 11:40:48 -04003090 lpfc_update_vport_wwn(vport);
James Smartaeb3c812017-04-21 16:05:02 -07003091 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05003092 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3093 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3094 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3095 }
3096
dea31012005-04-17 16:05:31 -05003097 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3098 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003099 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003100 return;
3101
3102out:
James Smart3e1f0712018-11-29 16:09:29 -08003103 pmb->ctx_buf = NULL;
dea31012005-04-17 16:05:31 -05003104 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3105 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003106 lpfc_issue_clear_la(phba, vport);
3107 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003108 return;
3109}
3110
3111static void
James Smart76a95d72010-11-20 23:11:48 -05003112lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003113{
James Smart92d7f7b2007-06-17 19:56:38 -05003114 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003115 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003116 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003117 int i;
James Smart14691152006-12-02 13:34:28 -05003118 struct lpfc_dmabuf *mp;
3119 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003120 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003121 uint32_t fc_flags = 0;
James Smart894bb172019-08-14 16:56:52 -07003122 unsigned long iflags;
James Smart14691152006-12-02 13:34:28 -05003123
James Smart894bb172019-08-14 16:56:52 -07003124 spin_lock_irqsave(&phba->hbalock, iflags);
James Smarta085e872015-12-16 18:12:02 -05003125 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3126
3127 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3128 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3129 case LPFC_LINK_SPEED_1GHZ:
3130 case LPFC_LINK_SPEED_2GHZ:
3131 case LPFC_LINK_SPEED_4GHZ:
3132 case LPFC_LINK_SPEED_8GHZ:
3133 case LPFC_LINK_SPEED_10GHZ:
3134 case LPFC_LINK_SPEED_16GHZ:
3135 case LPFC_LINK_SPEED_32GHZ:
James Smartfbd8a6b2018-02-22 08:18:45 -08003136 case LPFC_LINK_SPEED_64GHZ:
James Smart1dc5ec22018-10-23 13:41:11 -07003137 case LPFC_LINK_SPEED_128GHZ:
James Smarta085e872015-12-16 18:12:02 -05003138 break;
3139 default:
3140 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3141 break;
3142 }
dea31012005-04-17 16:05:31 -05003143 }
3144
James Smarte74c03c2013-04-17 20:15:19 -04003145 if (phba->fc_topology &&
3146 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3147 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3148 "3314 Toplogy changed was 0x%x is 0x%x\n",
3149 phba->fc_topology,
3150 bf_get(lpfc_mbx_read_top_topology, la));
3151 phba->fc_topology_changed = 1;
3152 }
3153
James Smart76a95d72010-11-20 23:11:48 -05003154 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003155 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003156
James Smartdf9e1b52011-12-13 13:22:17 -05003157 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003158 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003159 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003160
James Smart6a9c52c2009-10-02 15:16:51 -04003161 /* if npiv is enabled and this adapter supports npiv log
3162 * a message that npiv is not supported in this topology
3163 */
3164 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003165 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3166 "1309 Link Up Event npiv not supported in loop "
3167 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003168 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003169 if (bf_get(lpfc_mbx_read_top_il, la))
3170 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003171
James Smart76a95d72010-11-20 23:11:48 -05003172 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3173 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003174
3175 if (i == 0) {
3176 phba->alpa_map[0] = 0;
3177 } else {
James Smarte8b62012007-08-02 11:10:09 -04003178 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003179 int numalpa, j, k;
3180 union {
3181 uint8_t pamap[16];
3182 struct {
3183 uint32_t wd1;
3184 uint32_t wd2;
3185 uint32_t wd3;
3186 uint32_t wd4;
3187 } pa;
3188 } un;
3189 numalpa = phba->alpa_map[0];
3190 j = 0;
3191 while (j < numalpa) {
3192 memset(un.pamap, 0, 16);
3193 for (k = 1; j < numalpa; k++) {
3194 un.pamap[k - 1] =
3195 phba->alpa_map[j + 1];
3196 j++;
3197 if (k == 16)
3198 break;
3199 }
3200 /* Link Up Event ALPA map */
3201 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003202 KERN_WARNING,
3203 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003204 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003205 "ALPA map Data: x%x "
3206 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003207 un.pa.wd1, un.pa.wd2,
3208 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003209 }
3210 }
3211 }
3212 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003213 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003214 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003215 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003216 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3217 }
James Smart2e0fef82007-06-17 19:56:36 -05003218 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003219 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003220 }
James Smart894bb172019-08-14 16:56:52 -07003221 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05003222
James Smart38c20672013-03-01 16:37:44 -05003223 if (fc_flags) {
James Smart894bb172019-08-14 16:56:52 -07003224 spin_lock_irqsave(shost->host_lock, iflags);
James Smart38c20672013-03-01 16:37:44 -05003225 vport->fc_flag |= fc_flags;
James Smart894bb172019-08-14 16:56:52 -07003226 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart38c20672013-03-01 16:37:44 -05003227 }
3228
dea31012005-04-17 16:05:31 -05003229 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003230 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3231 if (!sparam_mbox)
3232 goto out;
3233
3234 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3235 if (rc) {
3236 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3237 goto out;
3238 }
3239 sparam_mbox->vport = vport;
3240 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3241 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3242 if (rc == MBX_NOT_FINISHED) {
James Smart3e1f0712018-11-29 16:09:29 -08003243 mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -05003244 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3245 kfree(mp);
3246 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3247 goto out;
dea31012005-04-17 16:05:31 -05003248 }
3249
James Smart76a95d72010-11-20 23:11:48 -05003250 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003251 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3252 if (!cfglink_mbox)
3253 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003254 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003255 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003256 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003257 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003258 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003259 if (rc == MBX_NOT_FINISHED) {
3260 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3261 goto out;
3262 }
3263 } else {
James Smart32b97932009-07-19 10:01:21 -04003264 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003265 /*
3266 * Add the driver's default FCF record at FCF index 0 now. This
3267 * is phase 1 implementation that support FCF index 0 and driver
3268 * defaults.
3269 */
James Smart45ed1192009-10-02 15:17:02 -04003270 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003271 fcf_record = kzalloc(sizeof(struct fcf_record),
3272 GFP_KERNEL);
3273 if (unlikely(!fcf_record)) {
3274 lpfc_printf_log(phba, KERN_ERR,
3275 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003276 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003277 "fcf record\n");
3278 rc = -ENODEV;
3279 goto out;
3280 }
3281
3282 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3283 LPFC_FCOE_FCF_DEF_INDEX);
3284 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3285 if (unlikely(rc)) {
3286 lpfc_printf_log(phba, KERN_ERR,
3287 LOG_MBOX | LOG_SLI,
3288 "2013 Could not manually add FCF "
3289 "record 0, status %d\n", rc);
3290 rc = -ENODEV;
3291 kfree(fcf_record);
3292 goto out;
3293 }
3294 kfree(fcf_record);
3295 }
3296 /*
3297 * The driver is expected to do FIP/FCF. Call the port
3298 * and get the FCF Table.
3299 */
James Smart894bb172019-08-14 16:56:52 -07003300 spin_lock_irqsave(&phba->hbalock, iflags);
James Smarta93ff372010-10-22 11:06:08 -04003301 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart894bb172019-08-14 16:56:52 -07003302 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart32b97932009-07-19 10:01:21 -04003303 return;
3304 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003305 /* This is the initial FCF discovery scan */
3306 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart894bb172019-08-14 16:56:52 -07003307 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart0c9ab6f2010-02-26 14:15:57 -05003308 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3309 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003310 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3311 LPFC_FCOE_FCF_GET_FIRST);
3312 if (rc) {
James Smart894bb172019-08-14 16:56:52 -07003313 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart0c9ab6f2010-02-26 14:15:57 -05003314 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
James Smart894bb172019-08-14 16:56:52 -07003315 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smart6fb120a2009-05-22 14:52:59 -04003316 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003317 }
James Smart38b92ef2010-08-04 16:11:39 -04003318 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003319 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003320 }
James Smart6fb120a2009-05-22 14:52:59 -04003321
3322 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003323out:
3324 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003325 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
James Smart32350662019-08-14 16:57:06 -07003326 "0263 Discovery Mailbox error: state: 0x%x : x%px x%px\n",
James Smarte8b62012007-08-02 11:10:09 -04003327 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003328 lpfc_issue_clear_la(phba, vport);
3329 return;
dea31012005-04-17 16:05:31 -05003330}
3331
3332static void
James Smart84774a42008-08-24 21:50:06 -04003333lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003334{
dea31012005-04-17 16:05:31 -05003335 uint32_t control;
3336 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003337 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003338 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003339 if (phba->sli_rev <= LPFC_SLI_REV3) {
3340 control = readl(phba->HCregaddr);
3341 control |= HC_LAINT_ENA;
3342 writel(control, phba->HCregaddr);
3343 readl(phba->HCregaddr); /* flush */
3344 }
James Smart2e0fef82007-06-17 19:56:36 -05003345 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003346}
3347
James Smart84774a42008-08-24 21:50:06 -04003348static void
3349lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3350{
3351 lpfc_linkdown(phba);
3352 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003353 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003354 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3355}
3356
3357
dea31012005-04-17 16:05:31 -05003358/*
James Smart76a95d72010-11-20 23:11:48 -05003359 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003360 * command upon completion. It is setup in the LPFC_MBOXQ
3361 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003362 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003363 */
3364void
James Smart76a95d72010-11-20 23:11:48 -05003365lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003366{
James Smart2e0fef82007-06-17 19:56:36 -05003367 struct lpfc_vport *vport = pmb->vport;
3368 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003369 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003370 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003371 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003372 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smartaeb3c812017-04-21 16:05:02 -07003373 uint8_t attn_type;
James Smart894bb172019-08-14 16:56:52 -07003374 unsigned long iflags;
dea31012005-04-17 16:05:31 -05003375
James Smart0d2b6b82008-06-14 22:52:47 -04003376 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003377 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -07003378 if (pring)
3379 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart895427b2017-02-12 13:52:30 -08003380
dea31012005-04-17 16:05:31 -05003381 /* Check for error */
3382 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003383 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003384 "1307 READ_LA mbox error x%x state x%x\n",
3385 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003386 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003387 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003388 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003389 }
3390
James Smart76a95d72010-11-20 23:11:48 -05003391 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
James Smartaeb3c812017-04-21 16:05:02 -07003392 attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
dea31012005-04-17 16:05:31 -05003393
3394 memcpy(&phba->alpa_map[0], mp->virt, 128);
3395
James Smart894bb172019-08-14 16:56:52 -07003396 spin_lock_irqsave(shost->host_lock, iflags);
James Smart76a95d72010-11-20 23:11:48 -05003397 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003398 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003399 else
James Smart2e0fef82007-06-17 19:56:36 -05003400 vport->fc_flag &= ~FC_BYPASSED_MODE;
James Smart894bb172019-08-14 16:56:52 -07003401 spin_unlock_irqrestore(shost->host_lock, iflags);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003402
James Smartbe0c0082013-03-01 16:38:14 -05003403 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003404 phba->fc_stat.LinkMultiEvent++;
James Smartaeb3c812017-04-21 16:05:02 -07003405 if (attn_type == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003406 if (phba->fc_eventTag != 0)
3407 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003408 }
dea31012005-04-17 16:05:31 -05003409
3410 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003411 if (phba->sli_rev < LPFC_SLI_REV4) {
James Smart894bb172019-08-14 16:56:52 -07003412 spin_lock_irqsave(&phba->hbalock, iflags);
James Smartbe0c0082013-03-01 16:38:14 -05003413 if (bf_get(lpfc_mbx_read_top_mm, la))
3414 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3415 else
3416 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
James Smart894bb172019-08-14 16:56:52 -07003417 spin_unlock_irqrestore(&phba->hbalock, iflags);
James Smartbe0c0082013-03-01 16:38:14 -05003418 }
dea31012005-04-17 16:05:31 -05003419
James Smart4d9ab992009-10-02 15:16:39 -04003420 phba->link_events++;
James Smartaeb3c812017-04-21 16:05:02 -07003421 if ((attn_type == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003422 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003423 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003424 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003425 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003426 "1306 Link Up Event in loop back mode "
3427 "x%x received Data: x%x x%x x%x x%x\n",
3428 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003429 bf_get(lpfc_mbx_read_top_alpa_granted,
3430 la),
3431 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003432 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003433 } else {
3434 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003435 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003436 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003437 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003438 bf_get(lpfc_mbx_read_top_alpa_granted,
3439 la),
3440 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003441 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003442 bf_get(lpfc_mbx_read_top_mm, la),
3443 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003444 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003445 }
James Smart92d7f7b2007-06-17 19:56:38 -05003446 lpfc_mbx_process_link_up(phba, la);
James Smartaeb3c812017-04-21 16:05:02 -07003447 } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3448 attn_type == LPFC_ATT_UNEXP_WWPN) {
dea31012005-04-17 16:05:31 -05003449 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003450 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003451 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3452 "1308 Link Down Event in loop back mode "
3453 "x%x received "
3454 "Data: x%x x%x x%x\n",
3455 la->eventTag, phba->fc_eventTag,
3456 phba->pport->port_state, vport->fc_flag);
James Smartaeb3c812017-04-21 16:05:02 -07003457 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3458 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3459 "1313 Link Down UNEXP WWPN Event x%x received "
3460 "Data: x%x x%x x%x x%x x%x\n",
3461 la->eventTag, phba->fc_eventTag,
3462 phba->pport->port_state, vport->fc_flag,
3463 bf_get(lpfc_mbx_read_top_mm, la),
3464 bf_get(lpfc_mbx_read_top_fa, la));
James Smart1b511972011-12-13 13:23:09 -05003465 else
James Smart3163f722008-02-08 18:50:25 -05003466 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003467 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003468 "Data: x%x x%x x%x x%x x%x\n",
3469 la->eventTag, phba->fc_eventTag,
3470 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003471 bf_get(lpfc_mbx_read_top_mm, la),
3472 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003473 lpfc_mbx_issue_link_down(phba);
3474 }
James Smartaeb3c812017-04-21 16:05:02 -07003475 if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3476 attn_type == LPFC_ATT_LINK_UP) {
James Smart84774a42008-08-24 21:50:06 -04003477 if (phba->link_state != LPFC_LINK_DOWN) {
3478 phba->fc_stat.LinkDown++;
3479 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3480 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003481 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003482 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003483 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003484 lpfc_mbx_issue_link_down(phba);
3485 } else
3486 lpfc_enable_la(phba);
3487
3488 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3489 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3490 "Data: x%x x%x x%x\n",
3491 la->eventTag, phba->fc_eventTag,
3492 phba->pport->port_state, vport->fc_flag);
3493 /*
3494 * The cmnd that triggered this will be waiting for this
3495 * signal.
3496 */
3497 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3498 if (phba->wait_4_mlo_maint_flg) {
3499 phba->wait_4_mlo_maint_flg = 0;
3500 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003501 }
James Smart84774a42008-08-24 21:50:06 -04003502 }
3503
James Smartbe0c0082013-03-01 16:38:14 -05003504 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3505 bf_get(lpfc_mbx_read_top_fa, la)) {
3506 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003507 lpfc_issue_clear_la(phba, vport);
3508 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003509 "1311 fa %d\n",
3510 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003511 }
3512
James Smart76a95d72010-11-20 23:11:48 -05003513lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003514 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3515 kfree(mp);
3516 mempool_free(pmb, phba->mbox_mem_pool);
3517 return;
3518}
3519
3520/*
3521 * This routine handles processing a REG_LOGIN mailbox
3522 * command upon completion. It is setup in the LPFC_MBOXQ
3523 * as the completion routine when the command is
3524 * handed off to the SLI layer.
3525 */
3526void
James Smart2e0fef82007-06-17 19:56:36 -05003527lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003528{
James Smart2e0fef82007-06-17 19:56:36 -05003529 struct lpfc_vport *vport = pmb->vport;
James Smart3e1f0712018-11-29 16:09:29 -08003530 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3531 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartffc95492010-06-07 15:23:17 -04003532 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003533
James Smart3e1f0712018-11-29 16:09:29 -08003534 pmb->ctx_buf = NULL;
3535 pmb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05003536
James Smartbe6bb942015-04-07 15:07:22 -04003537 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07003538 "0002 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
James Smartbe6bb942015-04-07 15:07:22 -04003539 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003540 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003541 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003542 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3543 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3544
James Smart40426292010-12-15 17:58:10 -05003545 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3546 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003547 /* We rcvd a rscn after issuing this
3548 * mbox reg login, we may have cycled
3549 * back through the state and be
3550 * back at reg login state so this
3551 * mbox needs to be ignored becase
3552 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003553 * process.
James Smartffc95492010-06-07 15:23:17 -04003554 */
3555 spin_lock_irq(shost->host_lock);
3556 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3557 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003558
3559 /*
3560 * We cannot leave the RPI registered because
3561 * if we go thru discovery again for this ndlp
3562 * a subsequent REG_RPI will fail.
3563 */
3564 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3565 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b72015-12-16 18:11:55 -05003566 }
3567
3568 /* Call state machine */
3569 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003570
dea31012005-04-17 16:05:31 -05003571 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3572 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003573 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003574 /* decrement the node reference count held for this callback
3575 * function.
3576 */
James Smart329f9bc2007-04-25 09:53:01 -04003577 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003578
3579 return;
3580}
3581
James Smart92d7f7b2007-06-17 19:56:38 -05003582static void
3583lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3584{
James Smart04c68492009-05-22 14:52:52 -04003585 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003586 struct lpfc_vport *vport = pmb->vport;
3587 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3588
3589 switch (mb->mbxStatus) {
3590 case 0x0011:
3591 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003592 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3593 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3594 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003595 break;
James Smart78730cf2010-04-06 15:06:30 -04003596 /* If VPI is busy, reset the HBA */
3597 case 0x9700:
3598 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3599 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3600 vport->vpi, mb->mbxStatus);
3601 if (!(phba->pport->load_flag & FC_UNLOADING))
3602 lpfc_workq_post_event(phba, NULL, NULL,
3603 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003604 }
James Smart72100cc2010-02-12 14:43:01 -05003605 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003606 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003607 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003608 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003609 vport->unreg_vpi_cmpl = VPORT_OK;
3610 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003611 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003612 /*
3613 * This shost reference might have been taken at the beginning of
3614 * lpfc_vport_delete()
3615 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003616 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003617 scsi_host_put(shost);
3618}
3619
James Smartd7c255b2008-08-24 21:50:00 -04003620int
James Smart92d7f7b2007-06-17 19:56:38 -05003621lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3622{
3623 struct lpfc_hba *phba = vport->phba;
3624 LPFC_MBOXQ_t *mbox;
3625 int rc;
3626
3627 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3628 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003629 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003630
3631 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3632 mbox->vport = vport;
3633 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003634 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003635 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003636 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3637 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003638 mempool_free(mbox, phba->mbox_mem_pool);
3639 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003640 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003641 }
James Smartd7c255b2008-08-24 21:50:00 -04003642 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003643}
3644
3645static void
3646lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3647{
3648 struct lpfc_vport *vport = pmb->vport;
3649 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003650 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003651
3652 switch (mb->mbxStatus) {
3653 case 0x0011:
3654 case 0x9601:
3655 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003656 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3657 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3658 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003659 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3660 spin_lock_irq(shost->host_lock);
3661 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3662 spin_unlock_irq(shost->host_lock);
3663 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003664
James Smartf6e84792019-01-28 11:14:38 -08003665 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3666 (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003667 if (phba->nvmet_support)
3668 lpfc_nvmet_update_targetport(phba);
3669 else
James Smart8c258642017-02-12 13:52:36 -08003670 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003671 }
James Smart92d7f7b2007-06-17 19:56:38 -05003672 goto out;
3673 }
James Smart92d7f7b2007-06-17 19:56:38 -05003674
James Smart72100cc2010-02-12 14:43:01 -05003675 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003676 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003677 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003678 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003679 vport->num_disc_nodes = 0;
3680 /* go thru NPR list and issue ELS PLOGIs */
3681 if (vport->fc_npr_cnt)
3682 lpfc_els_disc_plogi(vport);
3683
3684 if (!vport->num_disc_nodes) {
3685 spin_lock_irq(shost->host_lock);
3686 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3687 spin_unlock_irq(shost->host_lock);
3688 lpfc_can_disctmo(vport);
3689 }
3690 vport->port_state = LPFC_VPORT_READY;
3691
3692out:
3693 mempool_free(pmb, phba->mbox_mem_pool);
3694 return;
3695}
3696
James Smart21e9a0a2009-05-22 14:53:21 -04003697/**
3698 * lpfc_create_static_vport - Read HBA config region to create static vports.
3699 * @phba: pointer to lpfc hba data structure.
3700 *
3701 * This routine issue a DUMP mailbox command for config region 22 to get
3702 * the list of static vports to be created. The function create vports
3703 * based on the information returned from the HBA.
3704 **/
3705void
3706lpfc_create_static_vport(struct lpfc_hba *phba)
3707{
3708 LPFC_MBOXQ_t *pmb = NULL;
3709 MAILBOX_t *mb;
3710 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003711 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003712 struct fc_vport_identifiers vport_id;
3713 struct fc_vport *new_fc_vport;
3714 struct Scsi_Host *shost;
3715 struct lpfc_vport *vport;
3716 uint16_t offset = 0;
3717 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003718 struct lpfc_dmabuf *mp;
3719 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003720
3721 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3722 if (!pmb) {
3723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3724 "0542 lpfc_create_static_vport failed to"
3725 " allocate mailbox memory\n");
3726 return;
3727 }
James Smartcdcc2342012-08-14 14:26:35 -04003728 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003729 mb = &pmb->u.mb;
3730
3731 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3732 if (!vport_info) {
3733 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3734 "0543 lpfc_create_static_vport failed to"
3735 " allocate vport_info\n");
3736 mempool_free(pmb, phba->mbox_mem_pool);
3737 return;
3738 }
3739
3740 vport_buff = (uint8_t *) vport_info;
3741 do {
James Smartcdcc2342012-08-14 14:26:35 -04003742 /* free dma buffer from previous round */
James Smart3e1f0712018-11-29 16:09:29 -08003743 if (pmb->ctx_buf) {
3744 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smartcdcc2342012-08-14 14:26:35 -04003745 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3746 kfree(mp);
3747 }
James Smart1c6834a2009-07-19 10:01:26 -04003748 if (lpfc_dump_static_vport(phba, pmb, offset))
3749 goto out;
3750
James Smart21e9a0a2009-05-22 14:53:21 -04003751 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003752 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3753 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003754
James Smartcdcc2342012-08-14 14:26:35 -04003755 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003756 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3757 "0544 lpfc_create_static_vport failed to"
3758 " issue dump mailbox command ret 0x%x "
3759 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003760 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003761 goto out;
3762 }
3763
James Smart1c6834a2009-07-19 10:01:26 -04003764 if (phba->sli_rev == LPFC_SLI_REV4) {
3765 byte_count = pmb->u.mqe.un.mb_words[5];
James Smart3e1f0712018-11-29 16:09:29 -08003766 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart1c6834a2009-07-19 10:01:26 -04003767 if (byte_count > sizeof(struct static_vport_info) -
3768 offset)
3769 byte_count = sizeof(struct static_vport_info)
3770 - offset;
3771 memcpy(vport_buff + offset, mp->virt, byte_count);
3772 offset += byte_count;
3773 } else {
3774 if (mb->un.varDmp.word_cnt >
3775 sizeof(struct static_vport_info) - offset)
3776 mb->un.varDmp.word_cnt =
3777 sizeof(struct static_vport_info)
3778 - offset;
3779 byte_count = mb->un.varDmp.word_cnt;
3780 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3781 vport_buff + offset,
3782 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003783
James Smart1c6834a2009-07-19 10:01:26 -04003784 offset += byte_count;
3785 }
James Smart21e9a0a2009-05-22 14:53:21 -04003786
James Smart1c6834a2009-07-19 10:01:26 -04003787 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003788 offset < sizeof(struct static_vport_info));
3789
3790
3791 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3792 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3793 != VPORT_INFO_REV)) {
3794 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3795 "0545 lpfc_create_static_vport bad"
3796 " information header 0x%x 0x%x\n",
3797 le32_to_cpu(vport_info->signature),
3798 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3799
3800 goto out;
3801 }
3802
3803 shost = lpfc_shost_from_vport(phba->pport);
3804
3805 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3806 memset(&vport_id, 0, sizeof(vport_id));
3807 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3808 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3809 if (!vport_id.port_name || !vport_id.node_name)
3810 continue;
3811
3812 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3813 vport_id.vport_type = FC_PORTTYPE_NPIV;
3814 vport_id.disable = false;
3815 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3816
3817 if (!new_fc_vport) {
3818 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3819 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003820 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003821 continue;
3822 }
3823
3824 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3825 vport->vport_flag |= STATIC_VPORT;
3826 }
3827
3828out:
James Smart21e9a0a2009-05-22 14:53:21 -04003829 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003830 if (mbx_wait_rc != MBX_TIMEOUT) {
James Smart3e1f0712018-11-29 16:09:29 -08003831 if (pmb->ctx_buf) {
3832 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart1c6834a2009-07-19 10:01:26 -04003833 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3834 kfree(mp);
3835 }
James Smart21e9a0a2009-05-22 14:53:21 -04003836 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003837 }
James Smart21e9a0a2009-05-22 14:53:21 -04003838
3839 return;
3840}
3841
dea31012005-04-17 16:05:31 -05003842/*
3843 * This routine handles processing a Fabric REG_LOGIN mailbox
3844 * command upon completion. It is setup in the LPFC_MBOXQ
3845 * as the completion routine when the command is
3846 * handed off to the SLI layer.
3847 */
3848void
James Smart2e0fef82007-06-17 19:56:36 -05003849lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003850{
James Smart92d7f7b2007-06-17 19:56:38 -05003851 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003852 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003853 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart92d7f7b2007-06-17 19:56:38 -05003854 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003855 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003856
James Smart3e1f0712018-11-29 16:09:29 -08003857 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3858 pmb->ctx_ndlp = NULL;
3859 pmb->ctx_buf = NULL;
James Smartd439d282010-09-29 11:18:45 -04003860
dea31012005-04-17 16:05:31 -05003861 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003862 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3863 "0258 Register Fabric login error: 0x%x\n",
3864 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003865 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3866 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003867 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003868
James Smart76a95d72010-11-20 23:11:48 -05003869 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003870 /* FLOGI failed, use loop map to make discovery list */
3871 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003872
James Smart92d7f7b2007-06-17 19:56:38 -05003873 /* Start discovery */
3874 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003875 /* Decrement the reference count to ndlp after the
3876 * reference to the ndlp are done.
3877 */
3878 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003879 return;
3880 }
3881
3882 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003883 /* Decrement the reference count to ndlp after the reference
3884 * to the ndlp are done.
3885 */
3886 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003887 return;
3888 }
3889
James Smart6d368e52011-05-24 11:44:12 -04003890 if (phba->sli_rev < LPFC_SLI_REV4)
3891 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003892 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003893 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003894 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003895
James Smart2e0fef82007-06-17 19:56:36 -05003896 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003897 /* when physical port receive logo donot start
3898 * vport discovery */
3899 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3900 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003901 else {
3902 shost = lpfc_shost_from_vport(vport);
3903 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003904 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003905 spin_unlock_irq(shost->host_lock);
3906 }
James Smart92d7f7b2007-06-17 19:56:38 -05003907 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003908 }
3909
3910 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3911 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003912 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003913
3914 /* Drop the reference count from the mbox at the end after
3915 * all the current reference to the ndlp have been done.
3916 */
3917 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003918 return;
3919}
3920
James Smarta0f2d3e2017-02-12 13:52:31 -08003921 /*
3922 * This routine will issue a GID_FT for each FC4 Type supported
3923 * by the driver. ALL GID_FTs must complete before discovery is started.
3924 */
3925int
3926lpfc_issue_gidft(struct lpfc_vport *vport)
3927{
James Smarta0f2d3e2017-02-12 13:52:31 -08003928 /* Good status, issue CT Request to NameServer */
James Smartf6e84792019-01-28 11:14:38 -08003929 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3930 (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
James Smarta0f2d3e2017-02-12 13:52:31 -08003931 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3932 /* Cannot issue NameServer FCP Query, so finish up
3933 * discovery
3934 */
3935 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3936 "0604 %s FC TYPE %x %s\n",
3937 "Failed to issue GID_FT to ",
3938 FC_TYPE_FCP,
3939 "Finishing discovery.");
3940 return 0;
3941 }
3942 vport->gidft_inp++;
3943 }
3944
James Smartf6e84792019-01-28 11:14:38 -08003945 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3946 (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smarta0f2d3e2017-02-12 13:52:31 -08003947 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3948 /* Cannot issue NameServer NVME Query, so finish up
3949 * discovery
3950 */
3951 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3952 "0605 %s FC_TYPE %x %s %d\n",
3953 "Failed to issue GID_FT to ",
3954 FC_TYPE_NVME,
3955 "Finishing discovery: gidftinp ",
3956 vport->gidft_inp);
3957 if (vport->gidft_inp == 0)
3958 return 0;
3959 } else
3960 vport->gidft_inp++;
3961 }
3962 return vport->gidft_inp;
3963}
3964
James Smart7ea92eb2018-10-23 13:41:10 -07003965/**
3966 * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
3967 * @vport: The virtual port for which this call is being executed.
3968 *
3969 * This routine will issue a GID_PT to get a list of all N_Ports
3970 *
3971 * Return value :
3972 * 0 - Failure to issue a GID_PT
3973 * 1 - GID_PT issued
3974 **/
3975int
3976lpfc_issue_gidpt(struct lpfc_vport *vport)
3977{
3978 /* Good status, issue CT Request to NameServer */
3979 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
3980 /* Cannot issue NameServer FCP Query, so finish up
3981 * discovery
3982 */
3983 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3984 "0606 %s Port TYPE %x %s\n",
3985 "Failed to issue GID_PT to ",
3986 GID_PT_N_PORT,
3987 "Finishing discovery.");
3988 return 0;
3989 }
3990 vport->gidft_inp++;
3991 return 1;
3992}
3993
dea31012005-04-17 16:05:31 -05003994/*
3995 * This routine handles processing a NameServer REG_LOGIN mailbox
3996 * command upon completion. It is setup in the LPFC_MBOXQ
3997 * as the completion routine when the command is
3998 * handed off to the SLI layer.
3999 */
4000void
James Smart2e0fef82007-06-17 19:56:36 -05004001lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05004002{
James Smart21e9a0a2009-05-22 14:53:21 -04004003 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08004004 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
4005 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05004006 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05004007
James Smart3e1f0712018-11-29 16:09:29 -08004008 pmb->ctx_buf = NULL;
4009 pmb->ctx_ndlp = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08004010 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04004011
dea31012005-04-17 16:05:31 -05004012 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04004013 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
4014 "0260 Register NameServer error: 0x%x\n",
4015 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08004016
4017out:
James Smartfa4066b2008-01-11 01:53:27 -05004018 /* decrement the node reference count held for this
4019 * callback function.
4020 */
James Smart329f9bc2007-04-25 09:53:01 -04004021 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004022 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4023 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04004024 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04004025
4026 /* If no other thread is using the ndlp, free it */
4027 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05004028
James Smart76a95d72010-11-20 23:11:48 -05004029 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05004030 /*
4031 * RegLogin failed, use loop map to make discovery
4032 * list
4033 */
4034 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004035
James Smart92d7f7b2007-06-17 19:56:38 -05004036 /* Start discovery */
4037 lpfc_disc_start(vport);
4038 return;
4039 }
4040 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05004041 return;
4042 }
4043
James Smart6d368e52011-05-24 11:44:12 -04004044 if (phba->sli_rev < LPFC_SLI_REV4)
4045 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05004046 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05004047 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004048 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004049 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07004050 "0003 rpi:%x DID:%x flg:%x %d map%x x%px\n",
James Smartbe6bb942015-04-07 15:07:22 -04004051 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004052 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004053 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05004054
James Smart2e0fef82007-06-17 19:56:36 -05004055 if (vport->port_state < LPFC_VPORT_READY) {
4056 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05004057 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4058 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4059 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4060 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08004061
James Smartf6e84792019-01-28 11:14:38 -08004062 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4063 (vport->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
James Smarta0f2d3e2017-02-12 13:52:31 -08004064 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4065
James Smartf6e84792019-01-28 11:14:38 -08004066 if ((vport->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4067 (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08004068 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4069 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05004070
4071 /* Issue SCR just before NameServer GID_FT Query */
4072 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05004073 }
4074
James Smart2e0fef82007-06-17 19:56:36 -05004075 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08004076 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05004077 goto out;
dea31012005-04-17 16:05:31 -05004078
James Smarta0f2d3e2017-02-12 13:52:31 -08004079 /*
4080 * At this point in time we may need to wait for multiple
4081 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4082 *
4083 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05004084 * callback function.
4085 */
James Smart329f9bc2007-04-25 09:53:01 -04004086 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004087 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4088 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05004089 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004090
4091 return;
4092}
4093
4094static void
James Smart2e0fef82007-06-17 19:56:36 -05004095lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004096{
James Smart2e0fef82007-06-17 19:56:36 -05004097 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4098 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05004099 struct lpfc_rport_data *rdata;
4100 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05004101 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004102
James Smartf6e84792019-01-28 11:14:38 -08004103 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
James Smart895427b2017-02-12 13:52:30 -08004104 return;
4105
dea31012005-04-17 16:05:31 -05004106 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004107 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4108 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004109 rport_ids.port_id = ndlp->nlp_DID;
4110 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004111
James Smart329f9bc2007-04-25 09:53:01 -04004112 /*
4113 * We leave our node pointer in rport->dd_data when we unregister a
4114 * FCP target port. But fc_remote_port_add zeros the space to which
4115 * rport->dd_data points. So, if we're reusing a previously
4116 * registered port, drop the reference that we took the last time we
4117 * registered the port.
4118 */
James Smart466e8402015-05-21 13:55:28 -04004119 rport = ndlp->rport;
4120 if (rport) {
4121 rdata = rport->dd_data;
4122 /* break the link before dropping the ref */
4123 ndlp->rport = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08004124 if (rdata) {
4125 if (rdata->pnode == ndlp)
4126 lpfc_nlp_put(ndlp);
4127 rdata->pnode = NULL;
4128 }
James Smart466e8402015-05-21 13:55:28 -04004129 /* drop reference for earlier registeration */
4130 put_device(&rport->dev);
4131 }
James Smart858c9f62007-06-17 19:56:39 -05004132
4133 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4134 "rport add: did:x%x flg:x%x type x%x",
4135 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4136
James Smart9589b0622011-04-16 11:03:17 -04004137 /* Don't add the remote port if unloading. */
4138 if (vport->load_flag & FC_UNLOADING)
4139 return;
4140
James Smart2e0fef82007-06-17 19:56:36 -05004141 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004142 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004143 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4144 "Warning: fc_remote_port_add failed\n");
4145 return;
4146 }
4147
4148 /* initialize static port data */
4149 rport->maxframe_size = ndlp->nlp_maxframe;
4150 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004151 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004152 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004153
4154 if (ndlp->nlp_type & NLP_FCP_TARGET)
Hannes Reineckea6a6d052019-04-10 16:16:19 +02004155 rport_ids.roles |= FC_PORT_ROLE_FCP_TARGET;
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004156 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
Hannes Reineckea6a6d052019-04-10 16:16:19 +02004157 rport_ids.roles |= FC_PORT_ROLE_FCP_INITIATOR;
4158 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
4159 rport_ids.roles |= FC_PORT_ROLE_NVME_INITIATOR;
4160 if (ndlp->nlp_type & NLP_NVME_TARGET)
4161 rport_ids.roles |= FC_PORT_ROLE_NVME_TARGET;
4162 if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
4163 rport_ids.roles |= FC_PORT_ROLE_NVME_DISCOVERY;
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004164
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004165 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4166 fc_remote_port_rolechg(rport, rport_ids.roles);
4167
James Smart34f5ad82012-08-03 12:35:03 -04004168 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07004169 "3183 rport register x%06x, rport x%px role x%x\n",
James Smart34f5ad82012-08-03 12:35:03 -04004170 ndlp->nlp_DID, rport, rport_ids.roles);
4171
James Smart071fbd3d2006-04-15 11:53:20 -04004172 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004173 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004174 ndlp->nlp_sid = rport->scsi_target_id;
4175 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004176 return;
4177}
4178
4179static void
James Smart2e0fef82007-06-17 19:56:36 -05004180lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004181{
4182 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004183 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004184
James Smartf6e84792019-01-28 11:14:38 -08004185 if (vport->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
James Smarta0f2d3e2017-02-12 13:52:31 -08004186 return;
James Smartc01f3202006-08-18 17:47:08 -04004187
James Smart2ea259e2017-02-12 13:52:27 -08004188 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004189 "rport delete: did:x%x flg:x%x type x%x",
4190 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4191
James Smart2ea259e2017-02-12 13:52:27 -08004192 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07004193 "3184 rport unregister x%06x, rport x%px\n",
James Smart34f5ad82012-08-03 12:35:03 -04004194 ndlp->nlp_DID, rport);
4195
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004196 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004197
4198 return;
4199}
4200
James Smartde0c5b32007-04-25 09:52:27 -04004201static void
James Smart2e0fef82007-06-17 19:56:36 -05004202lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004203{
James Smart2e0fef82007-06-17 19:56:36 -05004204 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart894bb172019-08-14 16:56:52 -07004205 unsigned long iflags;
James Smart2e0fef82007-06-17 19:56:36 -05004206
James Smart894bb172019-08-14 16:56:52 -07004207 spin_lock_irqsave(shost->host_lock, iflags);
James Smartde0c5b32007-04-25 09:52:27 -04004208 switch (state) {
4209 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004210 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004211 break;
4212 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004213 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004214 break;
4215 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004216 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004217 break;
4218 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004219 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004220 break;
4221 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004222 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004223 break;
4224 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004225 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004226 break;
4227 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004228 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004229 break;
4230 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004231 if (vport->fc_npr_cnt == 0 && count == -1)
4232 vport->fc_npr_cnt = 0;
4233 else
4234 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004235 break;
4236 }
James Smart894bb172019-08-14 16:56:52 -07004237 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smartde0c5b32007-04-25 09:52:27 -04004238}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004239
James Smartde0c5b32007-04-25 09:52:27 -04004240static void
James Smart2e0fef82007-06-17 19:56:36 -05004241lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004242 int old_state, int new_state)
4243{
James Smart2e0fef82007-06-17 19:56:36 -05004244 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4245
James Smartde0c5b32007-04-25 09:52:27 -04004246 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004247 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4248 ndlp->nlp_type |= NLP_FC_NODE;
4249 }
4250 if (new_state == NLP_STE_MAPPED_NODE)
4251 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4252 if (new_state == NLP_STE_NPR_NODE)
4253 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004254
James Smarta0f2d3e2017-02-12 13:52:31 -08004255 /* FCP and NVME Transport interface */
4256 if ((old_state == NLP_STE_MAPPED_NODE ||
4257 old_state == NLP_STE_UNMAPPED_NODE)) {
4258 if (ndlp->rport) {
4259 vport->phba->nport_event_cnt++;
4260 lpfc_unregister_remote_port(ndlp);
4261 }
4262
James Smart1c5b12f2017-04-21 16:05:03 -07004263 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004264 vport->phba->nport_event_cnt++;
James Smart3b5bde692017-11-20 16:00:39 -08004265 if (vport->phba->nvmet_support == 0) {
4266 /* Start devloss if target. */
4267 if (ndlp->nlp_type & NLP_NVME_TARGET)
4268 lpfc_nvme_unregister_port(vport, ndlp);
4269 } else {
James Smart6599e122017-06-01 21:07:00 -07004270 /* NVMET has no upcall. */
4271 lpfc_nlp_put(ndlp);
James Smart3b5bde692017-11-20 16:00:39 -08004272 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004273 }
James Smartde0c5b32007-04-25 09:52:27 -04004274 }
dea31012005-04-17 16:05:31 -05004275
James Smarta0f2d3e2017-02-12 13:52:31 -08004276 /* FCP and NVME Transport interfaces */
4277
James Smartde0c5b32007-04-25 09:52:27 -04004278 if (new_state == NLP_STE_MAPPED_NODE ||
4279 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart01a8aed2018-09-10 10:30:41 -07004280 if (ndlp->nlp_fc4_type ||
James Smart92721c32017-06-01 21:06:59 -07004281 ndlp->nlp_DID == Fabric_DID ||
4282 ndlp->nlp_DID == NameServer_DID ||
4283 ndlp->nlp_DID == FDMI_DID) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004284 vport->phba->nport_event_cnt++;
4285 /*
4286 * Tell the fc transport about the port, if we haven't
4287 * already. If we have, and it's a scsi entity, be
4288 */
4289 lpfc_register_remote_port(vport, ndlp);
4290 }
4291 /* Notify the NVME transport of this new rport. */
James Smart09559e82017-06-15 22:56:46 -07004292 if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4293 ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004294 if (vport->phba->nvmet_support == 0) {
4295 /* Register this rport with the transport.
James Smart3b5bde692017-11-20 16:00:39 -08004296 * Only NVME Target Rports are registered with
4297 * the transport.
James Smarta0f2d3e2017-02-12 13:52:31 -08004298 */
James Smart3b5bde692017-11-20 16:00:39 -08004299 if (ndlp->nlp_type & NLP_NVME_TARGET) {
4300 vport->phba->nport_event_cnt++;
4301 lpfc_nvme_register_port(vport, ndlp);
4302 }
James Smart8c258642017-02-12 13:52:36 -08004303 } else {
4304 /* Just take an NDLP ref count since the
4305 * target does not register rports.
4306 */
4307 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004308 }
4309 }
James Smartde0c5b32007-04-25 09:52:27 -04004310 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004311
James Smartea2151b2008-09-07 11:52:10 -04004312 if ((new_state == NLP_STE_MAPPED_NODE) &&
4313 (vport->stat_data_enabled)) {
4314 /*
4315 * A new target is discovered, if there is no buffer for
4316 * statistical data collection allocate buffer.
4317 */
4318 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4319 sizeof(struct lpfc_scsicmd_bkt),
4320 GFP_KERNEL);
4321
4322 if (!ndlp->lat_data)
4323 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4324 "0286 lpfc_nlp_state_cleanup failed to "
4325 "allocate statistical data buffer DID "
4326 "0x%x\n", ndlp->nlp_DID);
4327 }
James Smart858c9f62007-06-17 19:56:39 -05004328 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004329 * If the node just added to Mapped list was an FCP target,
4330 * but the remote port registration failed or assigned a target
4331 * id outside the presentable range - move the node to the
4332 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004333 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004334 if ((new_state == NLP_STE_MAPPED_NODE) &&
4335 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004336 (!ndlp->rport ||
4337 ndlp->rport->scsi_target_id == -1 ||
4338 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004340 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004341 spin_unlock_irq(shost->host_lock);
4342 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004343 }
James Smartde0c5b32007-04-25 09:52:27 -04004344}
4345
James Smart685f0bf2007-04-25 09:53:08 -04004346static char *
4347lpfc_nlp_state_name(char *buffer, size_t size, int state)
4348{
4349 static char *states[] = {
4350 [NLP_STE_UNUSED_NODE] = "UNUSED",
4351 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4352 [NLP_STE_ADISC_ISSUE] = "ADISC",
4353 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4354 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004355 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004356 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4357 [NLP_STE_MAPPED_NODE] = "MAPPED",
4358 [NLP_STE_NPR_NODE] = "NPR",
4359 };
4360
James Smart311464e2007-08-02 11:10:37 -04004361 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004362 strlcpy(buffer, states[state], size);
4363 else
4364 snprintf(buffer, size, "unknown (%d)", state);
4365 return buffer;
4366}
4367
James Smartde0c5b32007-04-25 09:52:27 -04004368void
James Smart2e0fef82007-06-17 19:56:36 -05004369lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4370 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004371{
James Smart2e0fef82007-06-17 19:56:36 -05004372 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004373 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004374 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004375
James Smarte8b62012007-08-02 11:10:09 -04004376 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4377 "0904 NPort state transition x%06x, %s -> %s\n",
4378 ndlp->nlp_DID,
4379 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4380 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004381
4382 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4383 "node statechg did:x%x old:%d ste:%d",
4384 ndlp->nlp_DID, old_state, state);
4385
James Smartde0c5b32007-04-25 09:52:27 -04004386 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004387 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004388 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004389 if (old_state == NLP_STE_UNMAPPED_NODE) {
4390 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4391 ndlp->nlp_type &= ~NLP_FC_NODE;
4392 }
4393
James Smart685f0bf2007-04-25 09:53:08 -04004394 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004395 spin_lock_irq(shost->host_lock);
4396 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4397 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004398 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004399 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004400
4401 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004402 lpfc_nlp_counters(vport, state, 1);
4403 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004404}
4405
4406void
James Smarte47c9092008-02-08 18:49:26 -05004407lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4408{
4409 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4410
4411 if (list_empty(&ndlp->nlp_listp)) {
4412 spin_lock_irq(shost->host_lock);
4413 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4414 spin_unlock_irq(shost->host_lock);
4415 }
4416}
4417
4418void
James Smart2e0fef82007-06-17 19:56:36 -05004419lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004420{
James Smart2e0fef82007-06-17 19:56:36 -05004421 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4422
James Smart0d2b6b82008-06-14 22:52:47 -04004423 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004424 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004425 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4426 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004427 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004428 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004429 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004430 NLP_STE_UNUSED_NODE);
4431}
4432
Adrian Bunk4d9db012008-02-14 23:24:02 +02004433static void
James Smarte47c9092008-02-08 18:49:26 -05004434lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4435{
James Smart0d2b6b82008-06-14 22:52:47 -04004436 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004437 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4438 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4439 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4440 NLP_STE_UNUSED_NODE);
4441}
James Smart109f6ed2008-12-04 22:39:08 -05004442/**
James Smart3621a712009-04-06 18:47:14 -04004443 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004444 * @vport: Pointer to Virtual Port object.
4445 * @ndlp: Pointer to FC node object.
4446 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004447 *
4448 * This function is always called when node object need to be initialized.
4449 * It initializes all the fields of the node object. Although the reference
4450 * to phba from @ndlp can be obtained indirectly through it's reference to
4451 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4452 * to the life-span of the @ndlp might go beyond the existence of @vport as
4453 * the final release of ndlp is determined by its reference count. And, the
4454 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004455 **/
4456static inline void
4457lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4458 uint32_t did)
4459{
4460 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4461 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
Kees Cookf22eb4d2017-09-06 20:24:26 -07004462 timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
James Smart109f6ed2008-12-04 22:39:08 -05004463 ndlp->nlp_DID = did;
4464 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004465 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004466 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004467 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004468 kref_init(&ndlp->kref);
4469 NLP_INT_NODE_ACT(ndlp);
4470 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004471 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smartdea16bd2018-11-29 16:09:30 -08004472 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
James Smart109f6ed2008-12-04 22:39:08 -05004473}
James Smarte47c9092008-02-08 18:49:26 -05004474
4475struct lpfc_nodelist *
4476lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4477 int state)
4478{
4479 struct lpfc_hba *phba = vport->phba;
James Smartdea16bd2018-11-29 16:09:30 -08004480 uint32_t did, flag;
James Smarte47c9092008-02-08 18:49:26 -05004481 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004482 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smart9d3d3402017-04-21 16:05:00 -07004483 int rpi = LPFC_RPI_ALLOC_ERROR;
James Smartdea16bd2018-11-29 16:09:30 -08004484 uint32_t defer_did = 0;
James Smarte47c9092008-02-08 18:49:26 -05004485
4486 if (!ndlp)
4487 return NULL;
4488
James Smart9d3d3402017-04-21 16:05:00 -07004489 if (phba->sli_rev == LPFC_SLI_REV4) {
James Smartb95b2112019-08-14 16:56:47 -07004490 if (ndlp->nlp_rpi == LPFC_RPI_ALLOC_ERROR)
4491 rpi = lpfc_sli4_alloc_rpi(vport->phba);
4492 else
4493 rpi = ndlp->nlp_rpi;
4494
4495 if (rpi == LPFC_RPI_ALLOC_ERROR) {
4496 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4497 "0359 %s: ndlp:x%px "
4498 "usgmap:x%x refcnt:%d FAILED RPI "
4499 " ALLOC\n",
4500 __func__,
4501 (void *)ndlp, ndlp->nlp_usg_map,
4502 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004503 return NULL;
James Smartb95b2112019-08-14 16:56:47 -07004504 }
James Smart9d3d3402017-04-21 16:05:00 -07004505 }
4506
James Smarte47c9092008-02-08 18:49:26 -05004507 spin_lock_irqsave(&phba->ndlp_lock, flags);
4508 /* The ndlp should not be in memory free mode */
4509 if (NLP_CHK_FREE_REQ(ndlp)) {
4510 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4511 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07004512 "0277 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05004513 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07004514 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004515 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004516 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004517 }
4518 /* The ndlp should not already be in active mode */
4519 if (NLP_CHK_NODE_ACT(ndlp)) {
4520 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4521 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07004522 "0278 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05004523 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07004524 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004525 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004526 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004527 }
4528
James Smartdea16bd2018-11-29 16:09:30 -08004529 /* First preserve the orginal DID, xri_bitmap and some flags */
James Smarte47c9092008-02-08 18:49:26 -05004530 did = ndlp->nlp_DID;
James Smartdea16bd2018-11-29 16:09:30 -08004531 flag = (ndlp->nlp_flag & NLP_UNREG_INP);
4532 if (flag & NLP_UNREG_INP)
4533 defer_did = ndlp->nlp_defer_did;
James Smartcff261f2013-12-17 20:29:47 -05004534 if (phba->sli_rev == LPFC_SLI_REV4)
4535 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004536
James Smartdea16bd2018-11-29 16:09:30 -08004537 /* Zero ndlp except of ndlp linked list pointer */
James Smarte47c9092008-02-08 18:49:26 -05004538 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4539 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smarte47c9092008-02-08 18:49:26 -05004540
James Smartdea16bd2018-11-29 16:09:30 -08004541 /* Next reinitialize and restore saved objects */
4542 lpfc_initialize_node(vport, ndlp, did);
4543 ndlp->nlp_flag |= flag;
4544 if (flag & NLP_UNREG_INP)
4545 ndlp->nlp_defer_did = defer_did;
James Smartcff261f2013-12-17 20:29:47 -05004546 if (phba->sli_rev == LPFC_SLI_REV4)
4547 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4548
James Smarte47c9092008-02-08 18:49:26 -05004549 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004550 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07004551 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04004552 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4553 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
James Smart32350662019-08-14 16:57:06 -07004554 "map:%x x%px\n", ndlp->nlp_rpi, ndlp->nlp_DID,
James Smartbe6bb942015-04-07 15:07:22 -04004555 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004556 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004557 ndlp->nlp_usg_map, ndlp);
4558 }
James Smart725dd392013-10-10 12:23:30 -04004559
James Smarte47c9092008-02-08 18:49:26 -05004560
4561 if (state != NLP_STE_UNUSED_NODE)
4562 lpfc_nlp_set_state(vport, ndlp, state);
James Smartb95b2112019-08-14 16:56:47 -07004563 else
4564 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4565 "0013 rpi:%x DID:%x flg:%x refcnt:%d "
4566 "map:%x x%px STATE=UNUSED\n",
4567 ndlp->nlp_rpi, ndlp->nlp_DID,
4568 ndlp->nlp_flag,
4569 kref_read(&ndlp->kref),
4570 ndlp->nlp_usg_map, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004571
4572 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4573 "node enable: did:x%x",
4574 ndlp->nlp_DID, 0, 0);
4575 return ndlp;
James Smart9d3d3402017-04-21 16:05:00 -07004576
4577free_rpi:
4578 if (phba->sli_rev == LPFC_SLI_REV4)
4579 lpfc_sli4_free_rpi(vport->phba, rpi);
4580 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04004581}
4582
4583void
James Smart2e0fef82007-06-17 19:56:36 -05004584lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004585{
James Smart87af33f2007-10-27 13:37:43 -04004586 /*
James Smartfa4066b2008-01-11 01:53:27 -05004587 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004588 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004589 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4590 * until ALL other outstanding threads have completed. We check
4591 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004592 */
James Smartfa4066b2008-01-11 01:53:27 -05004593 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4594 return;
James Smart51ef4c22007-08-02 11:10:31 -04004595 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004596 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004597 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004598 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004599 }
James Smart02902172015-05-21 13:55:17 -04004600
4601 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004602 return;
dea31012005-04-17 16:05:31 -05004603}
4604
4605/*
4606 * Start / ReStart rescue timer for Discovery / RSCN handling
4607 */
4608void
James Smart2e0fef82007-06-17 19:56:36 -05004609lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004610{
James Smart2e0fef82007-06-17 19:56:36 -05004611 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4612 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004613 uint32_t tmo;
4614
James Smart2e0fef82007-06-17 19:56:36 -05004615 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004616 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004617 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4618 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004619 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004620 * FC spec states we need 3 * ratov for CT requests
4621 */
4622 tmo = ((phba->fc_ratov * 3) + 3);
4623 }
dea31012005-04-17 16:05:31 -05004624
James Smart858c9f62007-06-17 19:56:39 -05004625
4626 if (!timer_pending(&vport->fc_disctmo)) {
4627 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4628 "set disc timer: tmo:x%x state:x%x flg:x%x",
4629 tmo, vport->port_state, vport->fc_flag);
4630 }
4631
James Smart256ec0d2013-04-17 20:14:58 -04004632 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004633 spin_lock_irq(shost->host_lock);
4634 vport->fc_flag |= FC_DISC_TMO;
4635 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004636
4637 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004638 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4639 "0247 Start Discovery Timer state x%x "
4640 "Data: x%x x%lx x%x x%x\n",
4641 vport->port_state, tmo,
4642 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4643 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004644
4645 return;
4646}
4647
4648/*
4649 * Cancel rescue timer for Discovery / RSCN handling
4650 */
4651int
James Smart2e0fef82007-06-17 19:56:36 -05004652lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004653{
James Smart2e0fef82007-06-17 19:56:36 -05004654 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004655 unsigned long iflags;
4656
James Smart858c9f62007-06-17 19:56:39 -05004657 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4658 "can disc timer: state:x%x rtry:x%x flg:x%x",
4659 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4660
dea31012005-04-17 16:05:31 -05004661 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004662 if (vport->fc_flag & FC_DISC_TMO) {
4663 spin_lock_irqsave(shost->host_lock, iflags);
4664 vport->fc_flag &= ~FC_DISC_TMO;
4665 spin_unlock_irqrestore(shost->host_lock, iflags);
4666 del_timer_sync(&vport->fc_disctmo);
4667 spin_lock_irqsave(&vport->work_port_lock, iflags);
4668 vport->work_port_events &= ~WORKER_DISC_TMO;
4669 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004670 }
4671
4672 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004673 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4674 "0248 Cancel Discovery Timer state x%x "
4675 "Data: x%x x%x x%x\n",
4676 vport->port_state, vport->fc_flag,
4677 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004678 return 0;
dea31012005-04-17 16:05:31 -05004679}
4680
4681/*
4682 * Check specified ring for outstanding IOCB on the SLI queue
4683 * Return true if iocb matches the specified nport
4684 */
4685int
James Smart2e0fef82007-06-17 19:56:36 -05004686lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4687 struct lpfc_sli_ring *pring,
4688 struct lpfc_iocbq *iocb,
4689 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004690{
James Smart2e0fef82007-06-17 19:56:36 -05004691 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004692 struct lpfc_vport *vport = ndlp->vport;
4693
4694 if (iocb->vport != vport)
4695 return 0;
4696
dea31012005-04-17 16:05:31 -05004697 if (pring->ringno == LPFC_ELS_RING) {
4698 switch (icmd->ulpCommand) {
4699 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004700 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004701 return 1;
Bart Van Asschecd05c152019-03-28 11:06:18 -07004702 /* fall through */
dea31012005-04-17 16:05:31 -05004703 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004704 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4705 return 1;
Bart Van Asschecd05c152019-03-28 11:06:18 -07004706 /* fall through */
dea31012005-04-17 16:05:31 -05004707 case CMD_XMIT_ELS_RSP64_CX:
4708 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004709 return 1;
Bart Van Asschecd05c152019-03-28 11:06:18 -07004710 /* fall through */
dea31012005-04-17 16:05:31 -05004711 }
James Smart895427b2017-02-12 13:52:30 -08004712 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004713 /* Skip match check if waiting to relogin to FCP target */
4714 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004715 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004716 return 0;
dea31012005-04-17 16:05:31 -05004717 }
4718 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004719 return 1;
dea31012005-04-17 16:05:31 -05004720 }
dea31012005-04-17 16:05:31 -05004721 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004722 return 0;
dea31012005-04-17 16:05:31 -05004723}
4724
James Smart895427b2017-02-12 13:52:30 -08004725static void
4726__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4727 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4728 struct list_head *dequeue_list)
4729{
4730 struct lpfc_iocbq *iocb, *next_iocb;
4731
4732 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4733 /* Check to see if iocb matches the nport */
4734 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4735 /* match, dequeue */
4736 list_move_tail(&iocb->list, dequeue_list);
4737 }
4738}
4739
4740static void
4741lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4742 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4743{
4744 struct lpfc_sli *psli = &phba->sli;
4745 uint32_t i;
4746
4747 spin_lock_irq(&phba->hbalock);
4748 for (i = 0; i < psli->num_rings; i++)
4749 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4750 dequeue_list);
4751 spin_unlock_irq(&phba->hbalock);
4752}
4753
4754static void
4755lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4756 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4757{
4758 struct lpfc_sli_ring *pring;
4759 struct lpfc_queue *qp = NULL;
4760
4761 spin_lock_irq(&phba->hbalock);
4762 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4763 pring = qp->pring;
4764 if (!pring)
4765 continue;
James Smart2ade92a2017-03-04 09:30:38 -08004766 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004767 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
James Smart2ade92a2017-03-04 09:30:38 -08004768 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004769 }
4770 spin_unlock_irq(&phba->hbalock);
4771}
4772
dea31012005-04-17 16:05:31 -05004773/*
4774 * Free resources / clean up outstanding I/Os
4775 * associated with nlp_rpi in the LPFC_NODELIST entry.
4776 */
4777static int
James Smart2e0fef82007-06-17 19:56:36 -05004778lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004779{
James Smart2534ba72007-04-25 09:52:20 -04004780 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004781
James Smart92d7f7b2007-06-17 19:56:38 -05004782 lpfc_fabric_abort_nport(ndlp);
4783
dea31012005-04-17 16:05:31 -05004784 /*
4785 * Everything that matches on txcmplq will be returned
4786 * by firmware with a no rpi error.
4787 */
James Smart40426292010-12-15 17:58:10 -05004788 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004789 if (phba->sli_rev != LPFC_SLI_REV4)
4790 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4791 else
4792 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004793 }
James Smart2534ba72007-04-25 09:52:20 -04004794
James Smarta257bf92009-04-06 18:48:10 -04004795 /* Cancel all the IOCBs from the completions list */
4796 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4797 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004798
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004799 return 0;
dea31012005-04-17 16:05:31 -05004800}
4801
James Smart086a3452012-08-14 14:25:21 -04004802/**
4803 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4804 * @phba: Pointer to HBA context object.
4805 * @pmb: Pointer to mailbox object.
4806 *
4807 * This function will issue an ELS LOGO command after completing
4808 * the UNREG_RPI.
4809 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004810static void
James Smart086a3452012-08-14 14:25:21 -04004811lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4812{
4813 struct lpfc_vport *vport = pmb->vport;
4814 struct lpfc_nodelist *ndlp;
4815
James Smart3e1f0712018-11-29 16:09:29 -08004816 ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
James Smart086a3452012-08-14 14:25:21 -04004817 if (!ndlp)
4818 return;
4819 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004820 mempool_free(pmb, phba->mbox_mem_pool);
James Smartdea16bd2018-11-29 16:09:30 -08004821
4822 /* Check to see if there are any deferred events to process */
4823 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4824 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4825 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4826 "1434 UNREG cmpl deferred logo x%x "
James Smart32350662019-08-14 16:57:06 -07004827 "on NPort x%x Data: x%x x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08004828 ndlp->nlp_rpi, ndlp->nlp_DID,
4829 ndlp->nlp_defer_did, ndlp);
4830
4831 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4832 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4833 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
James Smart00292e02018-12-13 15:17:55 -08004834 } else {
James Smart4f1a2fe2019-08-14 16:56:32 -07004835 if (ndlp->nlp_flag & NLP_RELEASE_RPI) {
4836 lpfc_sli4_free_rpi(vport->phba, ndlp->nlp_rpi);
4837 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
4838 }
James Smart00292e02018-12-13 15:17:55 -08004839 ndlp->nlp_flag &= ~NLP_UNREG_INP;
James Smartdea16bd2018-11-29 16:09:30 -08004840 }
James Smart086a3452012-08-14 14:25:21 -04004841}
4842
dea31012005-04-17 16:05:31 -05004843/*
James Smart97acd002019-09-21 20:58:51 -07004844 * Sets the mailbox completion handler to be used for the
4845 * unreg_rpi command. The handler varies based on the state of
4846 * the port and what will be happening to the rpi next.
4847 */
4848static void
4849lpfc_set_unreg_login_mbx_cmpl(struct lpfc_hba *phba, struct lpfc_vport *vport,
4850 struct lpfc_nodelist *ndlp, LPFC_MBOXQ_t *mbox)
4851{
4852 unsigned long iflags;
4853
4854 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4855 mbox->ctx_ndlp = ndlp;
4856 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4857
4858 } else if (phba->sli_rev == LPFC_SLI_REV4 &&
4859 (!(vport->load_flag & FC_UNLOADING)) &&
4860 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) >=
4861 LPFC_SLI_INTF_IF_TYPE_2) &&
4862 (kref_read(&ndlp->kref) > 0)) {
4863 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
4864 mbox->mbox_cmpl = lpfc_sli4_unreg_rpi_cmpl_clr;
4865 } else {
4866 if (vport->load_flag & FC_UNLOADING) {
4867 if (phba->sli_rev == LPFC_SLI_REV4) {
4868 spin_lock_irqsave(&vport->phba->ndlp_lock,
4869 iflags);
4870 ndlp->nlp_flag |= NLP_RELEASE_RPI;
4871 spin_unlock_irqrestore(&vport->phba->ndlp_lock,
4872 iflags);
4873 }
4874 lpfc_nlp_get(ndlp);
4875 }
4876 mbox->ctx_ndlp = ndlp;
4877 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4878 }
4879}
4880
4881/*
dea31012005-04-17 16:05:31 -05004882 * Free rpi associated with LPFC_NODELIST entry.
4883 * This routine is called from lpfc_freenode(), when we are removing
4884 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4885 * LOGO that completes successfully, and we are waiting to PLOGI back
4886 * to the remote NPort. In addition, it is called after we receive
4887 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4888 * we are waiting to PLOGI back to the remote NPort.
4889 */
4890int
James Smart2e0fef82007-06-17 19:56:36 -05004891lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004892{
James Smart2e0fef82007-06-17 19:56:36 -05004893 struct lpfc_hba *phba = vport->phba;
4894 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004895 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004896 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004897
James Smartf454a9a2013-09-06 12:20:07 -04004898 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4899 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4900 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4901 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4902 "3366 RPI x%x needs to be "
4903 "unregistered nlp_flag x%x "
4904 "did x%x\n",
4905 ndlp->nlp_rpi, ndlp->nlp_flag,
4906 ndlp->nlp_DID);
James Smartdea16bd2018-11-29 16:09:30 -08004907
4908 /* If there is already an UNREG in progress for this ndlp,
4909 * no need to queue up another one.
4910 */
4911 if (ndlp->nlp_flag & NLP_UNREG_INP) {
4912 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4913 "1436 unreg_rpi SKIP UNREG x%x on "
4914 "NPort x%x deferred x%x flg x%x "
James Smart32350662019-08-14 16:57:06 -07004915 "Data: x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08004916 ndlp->nlp_rpi, ndlp->nlp_DID,
4917 ndlp->nlp_defer_did,
4918 ndlp->nlp_flag, ndlp);
4919 goto out;
4920 }
4921
James Smart2e0fef82007-06-17 19:56:36 -05004922 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4923 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004924 /* SLI4 ports require the physical rpi value. */
4925 rpi = ndlp->nlp_rpi;
4926 if (phba->sli_rev == LPFC_SLI_REV4)
4927 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004928
James Smart6d368e52011-05-24 11:44:12 -04004929 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004930 mbox->vport = vport;
James Smart97acd002019-09-21 20:58:51 -07004931 lpfc_set_unreg_login_mbx_cmpl(phba, vport, ndlp, mbox);
4932 if (mbox->mbox_cmpl == lpfc_sli4_unreg_rpi_cmpl_clr)
4933 /*
4934 * accept PLOGIs after unreg_rpi_cmpl
4935 */
4936 acc_plogi = 0;
James Smartdea16bd2018-11-29 16:09:30 -08004937 if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4938 Fabric_DID_MASK) &&
4939 (!(vport->fc_flag & FC_OFFLINE_MODE)))
4940 ndlp->nlp_flag |= NLP_UNREG_INP;
4941
4942 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4943 "1433 unreg_rpi UNREG x%x on "
James Smart32350662019-08-14 16:57:06 -07004944 "NPort x%x deferred flg x%x "
4945 "Data:x%px\n",
James Smartdea16bd2018-11-29 16:09:30 -08004946 ndlp->nlp_rpi, ndlp->nlp_DID,
4947 ndlp->nlp_flag, ndlp);
James Smart086a3452012-08-14 14:25:21 -04004948
James Smart0b727fe2007-10-27 13:37:25 -04004949 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004950 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004951 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004952 acc_plogi = 1;
4953 }
dea31012005-04-17 16:05:31 -05004954 }
dea31012005-04-17 16:05:31 -05004955 lpfc_no_rpi(phba, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08004956out:
James Smart40426292010-12-15 17:58:10 -05004957 if (phba->sli_rev != LPFC_SLI_REV4)
4958 ndlp->nlp_rpi = 0;
4959 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004960 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004961 if (acc_plogi)
4962 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004963 return 1;
4964 }
James Smart7c5e5182015-05-22 10:42:43 -04004965 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004966 return 0;
4967}
4968
James Smartecfd03c2010-02-12 14:41:27 -05004969/**
4970 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4971 * @phba: pointer to lpfc hba data structure.
4972 *
4973 * This routine is invoked to unregister all the currently registered RPIs
4974 * to the HBA.
4975 **/
4976void
4977lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4978{
4979 struct lpfc_vport **vports;
4980 struct lpfc_nodelist *ndlp;
4981 struct Scsi_Host *shost;
4982 int i;
4983
4984 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004985 if (!vports) {
4986 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4987 "2884 Vport array allocation failed \n");
4988 return;
4989 }
James Smartecfd03c2010-02-12 14:41:27 -05004990 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4991 shost = lpfc_shost_from_vport(vports[i]);
4992 spin_lock_irq(shost->host_lock);
4993 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004994 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004995 /* The mempool_alloc might sleep */
4996 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004997 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004998 spin_lock_irq(shost->host_lock);
4999 }
James Smartecfd03c2010-02-12 14:41:27 -05005000 }
5001 spin_unlock_irq(shost->host_lock);
5002 }
5003 lpfc_destroy_vport_work_array(phba, vports);
5004}
5005
James Smart92d7f7b2007-06-17 19:56:38 -05005006void
5007lpfc_unreg_all_rpis(struct lpfc_vport *vport)
5008{
5009 struct lpfc_hba *phba = vport->phba;
5010 LPFC_MBOXQ_t *mbox;
5011 int rc;
5012
James Smart5af5eee2010-10-22 11:06:38 -04005013 if (phba->sli_rev == LPFC_SLI_REV4) {
5014 lpfc_sli4_unreg_all_rpis(vport);
5015 return;
5016 }
5017
James Smart92d7f7b2007-06-17 19:56:38 -05005018 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5019 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04005020 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
5021 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005022 mbox->vport = vport;
5023 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -08005024 mbox->ctx_ndlp = NULL;
James Smart09372822008-01-11 01:52:54 -05005025 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04005026 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05005027 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04005028
5029 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
5030 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
5031 "1836 Could not issue "
5032 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05005033 }
5034}
5035
5036void
5037lpfc_unreg_default_rpis(struct lpfc_vport *vport)
5038{
5039 struct lpfc_hba *phba = vport->phba;
5040 LPFC_MBOXQ_t *mbox;
5041 int rc;
5042
James Smartc95a3b42019-03-12 16:30:07 -07005043 /* Unreg DID is an SLI3 operation. */
5044 if (phba->sli_rev > LPFC_SLI_REV3)
5045 return;
5046
James Smart92d7f7b2007-06-17 19:56:38 -05005047 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5048 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04005049 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
5050 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005051 mbox->vport = vport;
5052 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -08005053 mbox->ctx_ndlp = NULL;
James Smart09372822008-01-11 01:52:54 -05005054 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04005055 if (rc != MBX_TIMEOUT)
5056 mempool_free(mbox, phba->mbox_mem_pool);
5057
5058 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04005059 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
5060 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04005061 "unreg_did (default rpis) status %d\n",
5062 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05005063 }
5064}
5065
dea31012005-04-17 16:05:31 -05005066/*
5067 * Free resources associated with LPFC_NODELIST entry
5068 * so it can be freed.
5069 */
5070static int
James Smart2e0fef82007-06-17 19:56:36 -05005071lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005072{
James Smart2e0fef82007-06-17 19:56:36 -05005073 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5074 struct lpfc_hba *phba = vport->phba;
5075 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05005076 struct lpfc_dmabuf *mp;
James Smart97acd002019-09-21 20:58:51 -07005077 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005078
5079 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04005080 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5081 "0900 Cleanup node for NPort x%x "
5082 "Data: x%x x%x x%x\n",
5083 ndlp->nlp_DID, ndlp->nlp_flag,
5084 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005085 if (NLP_CHK_FREE_REQ(ndlp)) {
5086 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07005087 "0280 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05005088 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07005089 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005090 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005091 lpfc_dequeue_node(vport, ndlp);
5092 } else {
5093 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07005094 "0281 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05005095 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07005096 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005097 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005098 lpfc_disable_node(vport, ndlp);
5099 }
dea31012005-04-17 16:05:31 -05005100
James Smart086a3452012-08-14 14:25:21 -04005101
5102 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5103
dea31012005-04-17 16:05:31 -05005104 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5105 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04005106 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04005107 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart3e1f0712018-11-29 16:09:29 -08005108 (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5109 mb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05005110 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5111 }
5112 }
James Smart33ccf8d2006-08-17 11:57:58 -04005113
James Smart2e0fef82007-06-17 19:56:36 -05005114 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04005115 /* Cleanup REG_LOGIN completions which are not yet processed */
5116 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5117 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04005118 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart3e1f0712018-11-29 16:09:29 -08005119 (ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
James Smart5ac6b302010-10-22 11:05:36 -04005120 continue;
5121
James Smart3e1f0712018-11-29 16:09:29 -08005122 mb->ctx_ndlp = NULL;
James Smart5ac6b302010-10-22 11:05:36 -04005123 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5124 }
5125
dea31012005-04-17 16:05:31 -05005126 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04005127 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04005128 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart3e1f0712018-11-29 16:09:29 -08005129 (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5130 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
dea31012005-04-17 16:05:31 -05005131 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05005132 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05005133 kfree(mp);
5134 }
5135 list_del(&mb->list);
5136 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05005137 /* We shall not invoke the lpfc_nlp_put to decrement
5138 * the ndlp reference count as we are in the process
5139 * of lpfc_nlp_release.
5140 */
dea31012005-04-17 16:05:31 -05005141 }
5142 }
James Smart2e0fef82007-06-17 19:56:36 -05005143 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005144
James Smarte47c9092008-02-08 18:49:26 -05005145 lpfc_els_abort(phba, ndlp);
5146
James Smart2e0fef82007-06-17 19:56:36 -05005147 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04005148 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005149 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005150
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05005151 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05005152 del_timer_sync(&ndlp->nlp_delayfunc);
5153
James Smart0d2b6b82008-06-14 22:52:47 -04005154 list_del_init(&ndlp->els_retry_evt.evt_listp);
5155 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05005156 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart4f1a2fe2019-08-14 16:56:32 -07005157 if (phba->sli_rev == LPFC_SLI_REV4)
5158 ndlp->nlp_flag |= NLP_RELEASE_RPI;
James Smart97acd002019-09-21 20:58:51 -07005159 if (!lpfc_unreg_rpi(vport, ndlp)) {
5160 /* Clean up unregistered and non freed rpis */
5161 if ((ndlp->nlp_flag & NLP_RELEASE_RPI) &&
5162 !(ndlp->nlp_rpi == LPFC_RPI_ALLOC_ERROR)) {
5163 lpfc_sli4_free_rpi(vport->phba,
5164 ndlp->nlp_rpi);
5165 spin_lock_irqsave(&vport->phba->ndlp_lock,
5166 iflags);
5167 ndlp->nlp_flag &= ~NLP_RELEASE_RPI;
5168 ndlp->nlp_rpi = LPFC_RPI_ALLOC_ERROR;
5169 spin_unlock_irqrestore(&vport->phba->ndlp_lock,
5170 iflags);
5171 }
5172 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005173 return 0;
dea31012005-04-17 16:05:31 -05005174}
5175
5176/*
5177 * Check to see if we can free the nlp back to the freelist.
5178 * If we are in the middle of using the nlp in the discovery state
5179 * machine, defer the free till we reach the end of the state machine.
5180 */
James Smart329f9bc2007-04-25 09:53:01 -04005181static void
James Smart2e0fef82007-06-17 19:56:36 -05005182lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005183{
James Smarta8adb832007-10-27 13:37:53 -04005184 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04005185 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04005186 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04005187 LPFC_MBOXQ_t *mbox;
5188 int rc;
dea31012005-04-17 16:05:31 -05005189
James Smart0d2b6b82008-06-14 22:52:47 -04005190 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04005191 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05005192 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
Dick Kennedye8bcf0a2017-09-29 17:34:32 -07005193 !(ndlp->nlp_flag & NLP_RPI_REGISTERED) &&
5194 phba->sli_rev != LPFC_SLI_REV4) {
James Smarta8adb832007-10-27 13:37:53 -04005195 /* For this case we need to cleanup the default rpi
5196 * allocated by the firmware.
5197 */
James Smartbe6bb942015-04-07 15:07:22 -04005198 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07005199 "0005 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
James Smartbe6bb942015-04-07 15:07:22 -04005200 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005201 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005202 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04005203 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
5204 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04005205 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05005206 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04005207 if (rc) {
5208 mempool_free(mbox, phba->mbox_mem_pool);
5209 }
5210 else {
5211 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5212 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5213 mbox->vport = vport;
James Smart3e1f0712018-11-29 16:09:29 -08005214 mbox->ctx_ndlp = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04005215 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5216 if (rc == MBX_NOT_FINISHED) {
5217 mempool_free(mbox, phba->mbox_mem_pool);
5218 }
5219 }
5220 }
5221 }
James Smart2e0fef82007-06-17 19:56:36 -05005222 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04005223
James Smart2e0fef82007-06-17 19:56:36 -05005224 /*
James Smart466e8402015-05-21 13:55:28 -04005225 * ndlp->rport must be set to NULL before it reaches here
5226 * i.e. break rport/node link before doing lpfc_nlp_put for
5227 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05005228 */
James Smart92d7f7b2007-06-17 19:56:38 -05005229 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04005230 /*
5231 * extra lpfc_nlp_put dropped the reference of ndlp
5232 * for registered rport so need to cleanup rport
5233 */
5234 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07005235 "0940 removed node x%px DID x%x "
5236 " rport not null x%px\n",
James Smart466e8402015-05-21 13:55:28 -04005237 ndlp, ndlp->nlp_DID, ndlp->rport);
5238 rport = ndlp->rport;
5239 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04005240 rdata->pnode = NULL;
5241 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04005242 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05005243 }
dea31012005-04-17 16:05:31 -05005244}
5245
5246static int
James Smart2e0fef82007-06-17 19:56:36 -05005247lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5248 uint32_t did)
dea31012005-04-17 16:05:31 -05005249{
James Smart2e0fef82007-06-17 19:56:36 -05005250 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005251
5252 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005253 return 0;
dea31012005-04-17 16:05:31 -05005254
dea31012005-04-17 16:05:31 -05005255 /* First check for Direct match */
5256 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005257 return 1;
dea31012005-04-17 16:05:31 -05005258
5259 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005260 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005261 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005262 return 0;
dea31012005-04-17 16:05:31 -05005263 }
5264
5265 matchdid.un.word = did;
5266 ndlpdid.un.word = ndlp->nlp_DID;
5267 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5268 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5269 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005270 /* This code is supposed to match the ID
5271 * for a private loop device that is
5272 * connect to fl_port. But we need to
5273 * check that the port did not just go
5274 * from pt2pt to fabric or we could end
5275 * up matching ndlp->nlp_DID 000001 to
5276 * fabric DID 0x20101
5277 */
dea31012005-04-17 16:05:31 -05005278 if ((ndlpdid.un.b.domain == 0) &&
5279 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005280 if (ndlpdid.un.b.id &&
5281 vport->phba->fc_topology ==
5282 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005283 return 1;
dea31012005-04-17 16:05:31 -05005284 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005285 return 0;
dea31012005-04-17 16:05:31 -05005286 }
5287
5288 matchdid.un.word = ndlp->nlp_DID;
5289 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5290 (mydid.un.b.area == ndlpdid.un.b.area)) {
5291 if ((matchdid.un.b.domain == 0) &&
5292 (matchdid.un.b.area == 0)) {
5293 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005294 return 1;
dea31012005-04-17 16:05:31 -05005295 }
5296 }
5297 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005298 return 0;
dea31012005-04-17 16:05:31 -05005299}
5300
James Smart685f0bf2007-04-25 09:53:08 -04005301/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005302static struct lpfc_nodelist *
5303__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005304{
James Smart2fb9bd82006-12-02 13:33:57 -05005305 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005306 uint32_t data1;
5307
James Smart2e0fef82007-06-17 19:56:36 -05005308 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5309 if (lpfc_matchdid(vport, ndlp, did)) {
James Smartb95b2112019-08-14 16:56:47 -07005310 data1 = (((uint32_t)ndlp->nlp_state << 24) |
5311 ((uint32_t)ndlp->nlp_xri << 16) |
5312 ((uint32_t)ndlp->nlp_type << 8) |
5313 ((uint32_t)ndlp->nlp_usg_map & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005314 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5315 "0929 FIND node DID "
James Smartb95b2112019-08-14 16:56:47 -07005316 "Data: x%px x%x x%x x%x x%x x%px\n",
James Smarte8b62012007-08-02 11:10:09 -04005317 ndlp, ndlp->nlp_DID,
James Smartb95b2112019-08-14 16:56:47 -07005318 ndlp->nlp_flag, data1, ndlp->nlp_rpi,
James Smartcff261f2013-12-17 20:29:47 -05005319 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005320 return ndlp;
dea31012005-04-17 16:05:31 -05005321 }
5322 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005323
dea31012005-04-17 16:05:31 -05005324 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005325 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5326 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005327 return NULL;
5328}
5329
5330struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005331lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005332{
James Smart2e0fef82007-06-17 19:56:36 -05005333 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005334 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005335 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005336
James Smartfedd3b72011-02-16 12:39:24 -05005337 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005338 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005339 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005340 return ndlp;
5341}
5342
5343struct lpfc_nodelist *
James Smartf60cb932019-05-14 14:58:05 -07005344lpfc_findnode_mapped(struct lpfc_vport *vport)
5345{
5346 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5347 struct lpfc_nodelist *ndlp;
5348 uint32_t data1;
5349 unsigned long iflags;
5350
5351 spin_lock_irqsave(shost->host_lock, iflags);
5352
5353 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5354 if (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
5355 ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
5356 data1 = (((uint32_t)ndlp->nlp_state << 24) |
5357 ((uint32_t)ndlp->nlp_xri << 16) |
5358 ((uint32_t)ndlp->nlp_type << 8) |
5359 ((uint32_t)ndlp->nlp_rpi & 0xff));
5360 spin_unlock_irqrestore(shost->host_lock, iflags);
5361 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5362 "2025 FIND node DID "
James Smart32350662019-08-14 16:57:06 -07005363 "Data: x%px x%x x%x x%x x%px\n",
James Smartf60cb932019-05-14 14:58:05 -07005364 ndlp, ndlp->nlp_DID,
5365 ndlp->nlp_flag, data1,
5366 ndlp->active_rrqs_xri_bitmap);
5367 return ndlp;
5368 }
5369 }
5370 spin_unlock_irqrestore(shost->host_lock, iflags);
5371
5372 /* FIND node did <did> NOT FOUND */
5373 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5374 "2026 FIND mapped did NOT FOUND.\n");
5375 return NULL;
5376}
5377
5378struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005379lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5380{
5381 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5382 struct lpfc_nodelist *ndlp;
5383
5384 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005385 if (!ndlp) {
James Smart1c5b12f2017-04-21 16:05:03 -07005386 if (vport->phba->nvmet_support)
5387 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005388 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5389 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005390 return NULL;
James Smart9d3d3402017-04-21 16:05:00 -07005391 ndlp = lpfc_nlp_init(vport, did);
dea31012005-04-17 16:05:31 -05005392 if (!ndlp)
5393 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005394 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5395 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005396 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005397 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005398 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005399 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005400 if (vport->phba->nvmet_support)
5401 return NULL;
James Smarte47c9092008-02-08 18:49:26 -05005402 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
James Smartb95b2112019-08-14 16:56:47 -07005403 if (!ndlp) {
5404 lpfc_printf_vlog(vport, KERN_WARNING, LOG_SLI,
5405 "0014 Could not enable ndlp\n");
James Smarte47c9092008-02-08 18:49:26 -05005406 return NULL;
James Smartb95b2112019-08-14 16:56:47 -07005407 }
James Smarte47c9092008-02-08 18:49:26 -05005408 spin_lock_irq(shost->host_lock);
5409 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5410 spin_unlock_irq(shost->host_lock);
5411 return ndlp;
dea31012005-04-17 16:05:31 -05005412 }
James Smarte47c9092008-02-08 18:49:26 -05005413
James Smart1c5b12f2017-04-21 16:05:03 -07005414 /* The NVME Target does not want to actively manage an rport.
5415 * The goal is to allow the target to reset its state and clear
5416 * pending IO in preparation for the initiator to recover.
5417 */
James Smart58da1ff2008-04-07 10:15:56 -04005418 if ((vport->fc_flag & FC_RSCN_MODE) &&
5419 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005420 if (lpfc_rscn_payload_check(vport, did)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005421
5422 /* Since this node is marked for discovery,
5423 * delay timeout is not needed.
5424 */
5425 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5426
5427 /* NVME Target mode waits until rport is known to be
5428 * impacted by the RSCN before it transitions. No
5429 * active management - just go to NPR provided the
5430 * node had a valid login.
5431 */
5432 if (vport->phba->nvmet_support)
5433 return ndlp;
5434
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005435 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005436 * we don't need to try to discover it again.
5437 */
5438 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5439 return NULL;
5440
James Smarta257bf92009-04-06 18:48:10 -04005441 spin_lock_irq(shost->host_lock);
5442 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5443 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005444 } else
dea31012005-04-17 16:05:31 -05005445 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005446 } else {
James Smart1c5b12f2017-04-21 16:05:03 -07005447 /* If the initiator received a PLOGI from this NPort or if the
5448 * initiator is already in the process of discovery on it,
5449 * there's no need to try to discover it again.
James Smart87af33f2007-10-27 13:37:43 -04005450 */
James Smart685f0bf2007-04-25 09:53:08 -04005451 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005452 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
James Smart1c5b12f2017-04-21 16:05:03 -07005453 (!vport->phba->nvmet_support &&
5454 ndlp->nlp_flag & NLP_RCV_PLOGI))
dea31012005-04-17 16:05:31 -05005455 return NULL;
James Smart1c5b12f2017-04-21 16:05:03 -07005456
James Smart8c258642017-02-12 13:52:36 -08005457 if (vport->phba->nvmet_support)
5458 return ndlp;
James Smart1c5b12f2017-04-21 16:05:03 -07005459
5460 /* Moving to NPR state clears unsolicited flags and
5461 * allows for rediscovery
5462 */
5463 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5464
James Smart2e0fef82007-06-17 19:56:36 -05005465 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005466 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005467 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005468 }
5469 return ndlp;
5470}
5471
5472/* Build a list of nodes to discover based on the loopmap */
5473void
James Smart2e0fef82007-06-17 19:56:36 -05005474lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005475{
James Smart2e0fef82007-06-17 19:56:36 -05005476 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005477 int j;
5478 uint32_t alpa, index;
5479
James Smart2e0fef82007-06-17 19:56:36 -05005480 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005481 return;
James Smart2e0fef82007-06-17 19:56:36 -05005482
James Smart76a95d72010-11-20 23:11:48 -05005483 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005484 return;
dea31012005-04-17 16:05:31 -05005485
5486 /* Check for loop map present or not */
5487 if (phba->alpa_map[0]) {
5488 for (j = 1; j <= phba->alpa_map[0]; j++) {
5489 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005490 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005491 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005492 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005493 }
5494 } else {
5495 /* No alpamap, so try all alpa's */
5496 for (j = 0; j < FC_MAXLOOP; j++) {
5497 /* If cfg_scan_down is set, start from highest
5498 * ALPA (0xef) to lowest (0x1).
5499 */
James Smart3de2a652007-08-02 11:09:59 -04005500 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005501 index = j;
5502 else
5503 index = FC_MAXLOOP - j - 1;
5504 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005505 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005506 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005507 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005508 }
5509 }
5510 return;
5511}
5512
James Smart895427b2017-02-12 13:52:30 -08005513/* SLI3 only */
dea31012005-04-17 16:05:31 -05005514void
James Smart2e0fef82007-06-17 19:56:36 -05005515lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005516{
dea31012005-04-17 16:05:31 -05005517 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005518 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005519 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5520 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005521 int rc;
5522
James Smart92d7f7b2007-06-17 19:56:38 -05005523 /*
5524 * if it's not a physical port or if we already send
5525 * clear_la then don't send it.
5526 */
5527 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005528 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5529 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005530 return;
5531
James Smart2e0fef82007-06-17 19:56:36 -05005532 /* Link up discovery */
5533 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5534 phba->link_state = LPFC_CLEAR_LA;
5535 lpfc_clear_la(phba, mbox);
5536 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5537 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005538 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005539 if (rc == MBX_NOT_FINISHED) {
5540 mempool_free(mbox, phba->mbox_mem_pool);
5541 lpfc_disc_flush_list(vport);
5542 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5543 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005544 phba->link_state = LPFC_HBA_ERROR;
5545 }
5546 }
5547}
5548
5549/* Reg_vpi to tell firmware to resume normal operations */
5550void
5551lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5552{
5553 LPFC_MBOXQ_t *regvpimbox;
5554
5555 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5556 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005557 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005558 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5559 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005560 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005561 == MBX_NOT_FINISHED) {
5562 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005563 }
5564 }
5565}
5566
5567/* Start Link up / RSCN discovery on NPR nodes */
5568void
5569lpfc_disc_start(struct lpfc_vport *vport)
5570{
5571 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5572 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005573 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005574 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005575
James Smarte74c03c2013-04-17 20:15:19 -04005576 if (!lpfc_is_link_up(phba)) {
5577 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5578 "3315 Link is not up %x\n",
5579 phba->link_state);
dea31012005-04-17 16:05:31 -05005580 return;
James Smarte74c03c2013-04-17 20:15:19 -04005581 }
James Smart2e0fef82007-06-17 19:56:36 -05005582
5583 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005584 clear_la_pending = 1;
5585 else
5586 clear_la_pending = 0;
5587
James Smart2e0fef82007-06-17 19:56:36 -05005588 if (vport->port_state < LPFC_VPORT_READY)
5589 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005590
James Smart2e0fef82007-06-17 19:56:36 -05005591 lpfc_set_disctmo(vport);
5592
James Smart2e0fef82007-06-17 19:56:36 -05005593 vport->fc_prevDID = vport->fc_myDID;
5594 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005595
5596 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005597 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5598 "0202 Start Discovery hba state x%x "
5599 "Data: x%x x%x x%x\n",
5600 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5601 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005602
5603 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005604 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005605
5606 if (num_sent)
5607 return;
5608
James Smart6fa139f2013-04-17 20:20:00 -04005609 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005610 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005611 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005612 !(vport->fc_flag & FC_RSCN_MODE) &&
5613 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005614 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005615 lpfc_issue_reg_vpi(phba, vport);
5616 return;
5617 }
James Smart2e0fef82007-06-17 19:56:36 -05005618
James Smart92d7f7b2007-06-17 19:56:38 -05005619 /*
5620 * For SLI2, we need to set port_state to READY and continue
5621 * discovery.
5622 */
5623 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5624 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005625 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005626
5627 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005628 vport->num_disc_nodes = 0;
5629 /* go thru NPR nodes and issue ELS PLOGIs */
5630 if (vport->fc_npr_cnt)
5631 lpfc_els_disc_plogi(vport);
5632
5633 if (!vport->num_disc_nodes) {
5634 spin_lock_irq(shost->host_lock);
5635 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5636 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005637 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005638 }
5639 }
James Smart92d7f7b2007-06-17 19:56:38 -05005640 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005641 } else {
5642 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005643 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005644
5645 if (num_sent)
5646 return;
5647
James Smart2e0fef82007-06-17 19:56:36 -05005648 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005649 /* Check to see if more RSCNs came in while we
5650 * were processing this one.
5651 */
James Smart2e0fef82007-06-17 19:56:36 -05005652 if ((vport->fc_rscn_id_cnt == 0) &&
5653 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5654 spin_lock_irq(shost->host_lock);
5655 vport->fc_flag &= ~FC_RSCN_MODE;
5656 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005657 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005658 } else
James Smart2e0fef82007-06-17 19:56:36 -05005659 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005660 }
5661 }
5662 return;
5663}
5664
5665/*
5666 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5667 * ring the match the sppecified nodelist.
5668 */
5669static void
James Smart2e0fef82007-06-17 19:56:36 -05005670lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005671{
James Smart2534ba72007-04-25 09:52:20 -04005672 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005673 IOCB_t *icmd;
5674 struct lpfc_iocbq *iocb, *next_iocb;
5675 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005676
James Smart895427b2017-02-12 13:52:30 -08005677 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -07005678 if (unlikely(!pring))
5679 return;
dea31012005-04-17 16:05:31 -05005680
5681 /* Error matching iocb on txq or txcmplq
5682 * First check the txq.
5683 */
James Smart2e0fef82007-06-17 19:56:36 -05005684 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005685 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5686 if (iocb->context1 != ndlp) {
5687 continue;
5688 }
5689 icmd = &iocb->iocb;
5690 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5691 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5692
James Smart2534ba72007-04-25 09:52:20 -04005693 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005694 }
5695 }
5696
5697 /* Next check the txcmplq */
5698 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5699 if (iocb->context1 != ndlp) {
5700 continue;
5701 }
5702 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005703 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5704 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005705 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005706 }
5707 }
James Smart2e0fef82007-06-17 19:56:36 -05005708 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005709
James Smarta257bf92009-04-06 18:48:10 -04005710 /* Cancel all the IOCBs from the completions list */
5711 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5712 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005713}
5714
Adrian Bunka6ababd2007-11-05 18:07:33 +01005715static void
James Smart2e0fef82007-06-17 19:56:36 -05005716lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005717{
5718 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005719 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005720
James Smart2e0fef82007-06-17 19:56:36 -05005721 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5722 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005723 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005724 if (!NLP_CHK_NODE_ACT(ndlp))
5725 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005726 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5727 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5728 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005729 }
dea31012005-04-17 16:05:31 -05005730 }
5731 }
dea31012005-04-17 16:05:31 -05005732}
5733
James Smart92d7f7b2007-06-17 19:56:38 -05005734void
5735lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5736{
5737 lpfc_els_flush_rscn(vport);
5738 lpfc_els_flush_cmd(vport);
5739 lpfc_disc_flush_list(vport);
5740}
5741
dea31012005-04-17 16:05:31 -05005742/*****************************************************************************/
5743/*
5744 * NAME: lpfc_disc_timeout
5745 *
5746 * FUNCTION: Fibre Channel driver discovery timeout routine.
5747 *
5748 * EXECUTION ENVIRONMENT: interrupt only
5749 *
5750 * CALLED FROM:
5751 * Timer function
5752 *
5753 * RETURNS:
5754 * none
5755 */
5756/*****************************************************************************/
5757void
Kees Cookf22eb4d2017-09-06 20:24:26 -07005758lpfc_disc_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05005759{
Kees Cookf22eb4d2017-09-06 20:24:26 -07005760 struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
James Smart2e0fef82007-06-17 19:56:36 -05005761 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005762 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005763 unsigned long flags = 0;
5764
5765 if (unlikely(!phba))
5766 return;
5767
James Smart5e9d9b82008-06-14 22:52:53 -04005768 spin_lock_irqsave(&vport->work_port_lock, flags);
5769 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5770 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005771 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005772 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005773
James Smart5e9d9b82008-06-14 22:52:53 -04005774 if (!tmo_posted)
5775 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005776 return;
5777}
5778
5779static void
James Smart2e0fef82007-06-17 19:56:36 -05005780lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005781{
James Smart2e0fef82007-06-17 19:56:36 -05005782 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5783 struct lpfc_hba *phba = vport->phba;
5784 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005785 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005786 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005787 int rc, clrlaerr = 0;
5788
James Smart2e0fef82007-06-17 19:56:36 -05005789 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005790 return;
5791
James Smart2e0fef82007-06-17 19:56:36 -05005792 spin_lock_irq(shost->host_lock);
5793 vport->fc_flag &= ~FC_DISC_TMO;
5794 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005795
James Smart858c9f62007-06-17 19:56:39 -05005796 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5797 "disc timeout: state:x%x rtry:x%x flg:x%x",
5798 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5799
James Smart2e0fef82007-06-17 19:56:36 -05005800 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005801
5802 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005803 /*
5804 * port_state is identically LPFC_LOCAL_CFG_LINK while
5805 * waiting for FAN timeout
5806 */
James Smarte8b62012007-08-02 11:10:09 -04005807 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5808 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005809
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005810 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005811 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005812 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005813 if (!NLP_CHK_NODE_ACT(ndlp))
5814 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005815 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5816 continue;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005817 if (ndlp->nlp_type & NLP_FABRIC) {
5818 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005819 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005820
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005821 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005822 /* Fail outstanding IO now since device
5823 * is marked for PLOGI.
5824 */
James Smart2e0fef82007-06-17 19:56:36 -05005825 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005826 }
5827 }
James Smart92d7f7b2007-06-17 19:56:38 -05005828 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005829 if (phba->sli_rev <= LPFC_SLI_REV3)
5830 lpfc_initial_flogi(vport);
5831 else
5832 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005833 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005834 }
dea31012005-04-17 16:05:31 -05005835 break;
5836
James Smart92d7f7b2007-06-17 19:56:38 -05005837 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005838 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005839 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005840 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005841 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5842 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005843 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005844
5845 /* Assume no Fabric and go on with discovery.
5846 * Check for outstanding ELS FLOGI to abort.
5847 */
5848
5849 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005850 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005851
5852 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005853 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005854 break;
5855
5856 case LPFC_FABRIC_CFG_LINK:
5857 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5858 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005859 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5860 "0223 Timeout while waiting for "
5861 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005862 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005863 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005864 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005865 lpfc_els_abort(phba, ndlp);
5866
5867 /* ReStart discovery */
5868 goto restart_disc;
dea31012005-04-17 16:05:31 -05005869
5870 case LPFC_NS_QRY:
5871 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005872 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5873 "0224 NameServer Query timeout "
5874 "Data: x%x x%x\n",
5875 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005876
James Smart92d7f7b2007-06-17 19:56:38 -05005877 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5878 /* Try it one more time */
5879 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005880 vport->gidft_inp = 0;
5881 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005882 if (rc == 0)
5883 break;
dea31012005-04-17 16:05:31 -05005884 }
James Smart92d7f7b2007-06-17 19:56:38 -05005885 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005886
James Smart87af33f2007-10-27 13:37:43 -04005887restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005888 /*
5889 * Discovery is over.
5890 * set port_state to PORT_READY if SLI2.
5891 * cmpl_reg_vpi will set port_state to READY for SLI3.
5892 */
James Smart3772a992009-05-22 14:50:54 -04005893 if (phba->sli_rev < LPFC_SLI_REV4) {
5894 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5895 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005896 else {
James Smart3772a992009-05-22 14:50:54 -04005897 lpfc_issue_clear_la(phba, vport);
5898 vport->port_state = LPFC_VPORT_READY;
5899 }
dea31012005-04-17 16:05:31 -05005900 }
5901
5902 /* Setup and issue mailbox INITIALIZE LINK command */
5903 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5904 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005905 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5906 "0206 Device Discovery "
5907 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005908 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005909 break;
5910 }
5911
5912 lpfc_linkdown(phba);
5913 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5914 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005915 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005916 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005917 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005918 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005919 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005920 if (rc == MBX_NOT_FINISHED)
5921 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5922
5923 break;
5924
5925 case LPFC_DISC_AUTH:
5926 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005927 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5928 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005929 lpfc_disc_flush_list(vport);
5930
James Smart92d7f7b2007-06-17 19:56:38 -05005931 /*
5932 * set port_state to PORT_READY if SLI2.
5933 * cmpl_reg_vpi will set port_state to READY for SLI3.
5934 */
James Smart3772a992009-05-22 14:50:54 -04005935 if (phba->sli_rev < LPFC_SLI_REV4) {
5936 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5937 lpfc_issue_reg_vpi(phba, vport);
5938 else { /* NPIV Not enabled */
5939 lpfc_issue_clear_la(phba, vport);
5940 vport->port_state = LPFC_VPORT_READY;
5941 }
dea31012005-04-17 16:05:31 -05005942 }
5943 break;
5944
James Smart2e0fef82007-06-17 19:56:36 -05005945 case LPFC_VPORT_READY:
5946 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005947 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5948 "0231 RSCN timeout Data: x%x "
5949 "x%x\n",
5950 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005951
5952 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005953 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005954
James Smart2e0fef82007-06-17 19:56:36 -05005955 lpfc_els_flush_rscn(vport);
5956 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005957 }
5958 break;
James Smart2e0fef82007-06-17 19:56:36 -05005959
James Smart92d7f7b2007-06-17 19:56:38 -05005960 default:
James Smarte8b62012007-08-02 11:10:09 -04005961 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005962 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005963 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005964 break;
5965 }
5966
5967 switch (phba->link_state) {
5968 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005969 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005970 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5971 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005972 clrlaerr = 1;
5973 break;
5974
James Smart09372822008-01-11 01:52:54 -05005975 case LPFC_LINK_UP:
5976 lpfc_issue_clear_la(phba, vport);
Bart Van Asschecd05c152019-03-28 11:06:18 -07005977 /* fall through */
James Smart2e0fef82007-06-17 19:56:36 -05005978 case LPFC_LINK_UNKNOWN:
5979 case LPFC_WARM_START:
5980 case LPFC_INIT_START:
5981 case LPFC_INIT_MBX_CMDS:
5982 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005983 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005984 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5985 "0230 Unexpected timeout, hba link "
5986 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005987 clrlaerr = 1;
5988 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005989
5990 case LPFC_HBA_READY:
5991 break;
dea31012005-04-17 16:05:31 -05005992 }
5993
5994 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005995 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005996 if (phba->sli_rev != LPFC_SLI_REV4) {
5997 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5998 ~LPFC_STOP_IOCB_EVENT;
5999 psli->sli3_ring[LPFC_FCP_RING].flag &=
6000 ~LPFC_STOP_IOCB_EVENT;
6001 }
James Smart2e0fef82007-06-17 19:56:36 -05006002 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05006003 }
dea31012005-04-17 16:05:31 -05006004 return;
6005}
6006
dea31012005-04-17 16:05:31 -05006007/*
6008 * This routine handles processing a NameServer REG_LOGIN mailbox
6009 * command upon completion. It is setup in the LPFC_MBOXQ
6010 * as the completion routine when the command is
6011 * handed off to the SLI layer.
6012 */
6013void
James Smart2e0fef82007-06-17 19:56:36 -05006014lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05006015{
James Smart04c68492009-05-22 14:52:52 -04006016 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08006017 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
6018 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05006019 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05006020
James Smart3e1f0712018-11-29 16:09:29 -08006021 pmb->ctx_buf = NULL;
6022 pmb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05006023
James Smart6d368e52011-05-24 11:44:12 -04006024 if (phba->sli_rev < LPFC_SLI_REV4)
6025 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05006026 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05006027 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05006028 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04006029 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
James Smart32350662019-08-14 16:57:06 -07006030 "0004 rpi:%x DID:%x flg:%x %d map:%x x%px\n",
James Smartbe6bb942015-04-07 15:07:22 -04006031 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006032 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04006033 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05006034 /*
6035 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05006036 * 0xfffffa (FDMI well known port).
6037 * DHBA -> DPRT -> RHBA -> RPA (physical port)
6038 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05006039 */
James Smart4258e982015-12-16 18:11:58 -05006040 if (vport->port_type == LPFC_PHYSICAL_PORT)
6041 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04006042 else
James Smart4258e982015-12-16 18:11:58 -05006043 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
6044
dea31012005-04-17 16:05:31 -05006045
James Smartfa4066b2008-01-11 01:53:27 -05006046 /* decrement the node reference count held for this callback
6047 * function.
6048 */
James Smart329f9bc2007-04-25 09:53:01 -04006049 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05006050 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6051 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04006052 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05006053
6054 return;
6055}
6056
James Smart685f0bf2007-04-25 09:53:08 -04006057static int
6058lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
6059{
6060 uint16_t *rpi = param;
6061
James Smarteff4a012012-01-18 16:25:25 -05006062 /* check for active node */
6063 if (!NLP_CHK_NODE_ACT(ndlp))
6064 return 0;
6065
James Smart685f0bf2007-04-25 09:53:08 -04006066 return ndlp->nlp_rpi == *rpi;
6067}
6068
6069static int
6070lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
6071{
6072 return memcmp(&ndlp->nlp_portname, param,
6073 sizeof(ndlp->nlp_portname)) == 0;
6074}
6075
Adrian Bunka6ababd2007-11-05 18:07:33 +01006076static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05006077__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05006078{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04006079 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05006080
James Smart2e0fef82007-06-17 19:56:36 -05006081 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04006082 if (filter(ndlp, param)) {
6083 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
Sakari Ailus2d44d162019-09-04 19:04:23 +03006084 "3185 FIND node filter %ps DID "
James Smart32350662019-08-14 16:57:06 -07006085 "ndlp x%px did x%x flg x%x st x%x "
James Smart49382502017-11-20 16:00:38 -08006086 "xri x%x type x%x rpi x%x\n",
James Smart34f5ad82012-08-03 12:35:03 -04006087 filter, ndlp, ndlp->nlp_DID,
James Smart49382502017-11-20 16:00:38 -08006088 ndlp->nlp_flag, ndlp->nlp_state,
6089 ndlp->nlp_xri, ndlp->nlp_type,
6090 ndlp->nlp_rpi);
James Smart685f0bf2007-04-25 09:53:08 -04006091 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04006092 }
James Smart685f0bf2007-04-25 09:53:08 -04006093 }
James Smart34f5ad82012-08-03 12:35:03 -04006094 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
Sakari Ailus2d44d162019-09-04 19:04:23 +03006095 "3186 FIND node filter %ps NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04006096 return NULL;
dea31012005-04-17 16:05:31 -05006097}
6098
James Smart685f0bf2007-04-25 09:53:08 -04006099/*
6100 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05006101 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04006102 */
6103struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05006104__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04006105{
James Smart2e0fef82007-06-17 19:56:36 -05006106 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04006107}
6108
James Smart488d1462006-03-07 15:02:37 -05006109/*
James Smart685f0bf2007-04-25 09:53:08 -04006110 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05006111 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05006112 */
6113struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05006114lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05006115{
James Smart2e0fef82007-06-17 19:56:36 -05006116 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05006117 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05006118
James Smart2e0fef82007-06-17 19:56:36 -05006119 spin_lock_irq(shost->host_lock);
6120 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
6121 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05006122 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05006123}
6124
James Smartcb69f7d2011-12-13 13:21:57 -05006125/*
6126 * This routine looks up the ndlp lists for the given RPI. If the rpi
6127 * is found, the routine returns the node element list pointer else
6128 * return NULL.
6129 */
6130struct lpfc_nodelist *
6131lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
6132{
6133 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6134 struct lpfc_nodelist *ndlp;
Fuqian Huang78d4b132019-08-12 16:31:34 +08006135 unsigned long flags;
James Smartcb69f7d2011-12-13 13:21:57 -05006136
Fuqian Huang78d4b132019-08-12 16:31:34 +08006137 spin_lock_irqsave(shost->host_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006138 ndlp = __lpfc_findnode_rpi(vport, rpi);
Fuqian Huang78d4b132019-08-12 16:31:34 +08006139 spin_unlock_irqrestore(shost->host_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006140 return ndlp;
6141}
6142
6143/**
6144 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6145 * @phba: pointer to lpfc hba data structure.
6146 * @vpi: the physical host virtual N_Port identifier.
6147 *
6148 * This routine finds a vport on a HBA (referred by @phba) through a
6149 * @vpi. The function walks the HBA's vport list and returns the address
6150 * of the vport with the matching @vpi.
6151 *
6152 * Return code
6153 * NULL - No vport with the matching @vpi found
6154 * Otherwise - Address to the vport with the matching @vpi.
6155 **/
6156struct lpfc_vport *
6157lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6158{
6159 struct lpfc_vport *vport;
6160 unsigned long flags;
6161 int i = 0;
6162
6163 /* The physical ports are always vpi 0 - translate is unnecessary. */
6164 if (vpi > 0) {
6165 /*
6166 * Translate the physical vpi to the logical vpi. The
6167 * vport stores the logical vpi.
6168 */
6169 for (i = 0; i < phba->max_vpi; i++) {
6170 if (vpi == phba->vpi_ids[i])
6171 break;
6172 }
6173
6174 if (i >= phba->max_vpi) {
6175 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6176 "2936 Could not find Vport mapped "
6177 "to vpi %d\n", vpi);
6178 return NULL;
6179 }
6180 }
6181
James Smart523128e2018-09-10 10:30:46 -07006182 spin_lock_irqsave(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006183 list_for_each_entry(vport, &phba->port_list, listentry) {
6184 if (vport->vpi == i) {
James Smart523128e2018-09-10 10:30:46 -07006185 spin_unlock_irqrestore(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006186 return vport;
6187 }
6188 }
James Smart523128e2018-09-10 10:30:46 -07006189 spin_unlock_irqrestore(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006190 return NULL;
6191}
6192
James Smart9d3d3402017-04-21 16:05:00 -07006193struct lpfc_nodelist *
6194lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05006195{
James Smart9d3d3402017-04-21 16:05:00 -07006196 struct lpfc_nodelist *ndlp;
6197 int rpi = LPFC_RPI_ALLOC_ERROR;
6198
6199 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6200 rpi = lpfc_sli4_alloc_rpi(vport->phba);
6201 if (rpi == LPFC_RPI_ALLOC_ERROR)
6202 return NULL;
6203 }
6204
6205 ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6206 if (!ndlp) {
6207 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6208 lpfc_sli4_free_rpi(vport->phba, rpi);
6209 return NULL;
6210 }
6211
dea31012005-04-17 16:05:31 -05006212 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05006213
6214 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04006215 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05006216 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07006217 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04006218 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6219 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
James Smart32350662019-08-14 16:57:06 -07006220 "map:%x x%px\n", ndlp->nlp_rpi, ndlp->nlp_DID,
James Smartbe6bb942015-04-07 15:07:22 -04006221 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006222 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04006223 ndlp->nlp_usg_map, ndlp);
6224
James Smartcff261f2013-12-17 20:29:47 -05006225 ndlp->active_rrqs_xri_bitmap =
6226 mempool_alloc(vport->phba->active_rrq_pool,
6227 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04006228 if (ndlp->active_rrqs_xri_bitmap)
6229 memset(ndlp->active_rrqs_xri_bitmap, 0,
6230 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05006231 }
6232
James Smart725dd392013-10-10 12:23:30 -04006233
James Smart858c9f62007-06-17 19:56:39 -05006234
6235 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6236 "node init: did:x%x",
6237 ndlp->nlp_DID, 0, 0);
6238
James Smart9d3d3402017-04-21 16:05:00 -07006239 return ndlp;
dea31012005-04-17 16:05:31 -05006240}
James Smart329f9bc2007-04-25 09:53:01 -04006241
James Smart98c9ea52007-10-27 13:37:33 -04006242/* This routine releases all resources associated with a specifc NPort's ndlp
6243 * and mempool_free's the nodelist.
6244 */
James Smart311464e2007-08-02 11:10:37 -04006245static void
James Smart329f9bc2007-04-25 09:53:01 -04006246lpfc_nlp_release(struct kref *kref)
6247{
James Smarte47c9092008-02-08 18:49:26 -05006248 struct lpfc_hba *phba;
6249 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04006250 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6251 kref);
James Smart858c9f62007-06-17 19:56:39 -05006252
6253 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6254 "node release: did:x%x flg:x%x type:x%x",
6255 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6256
James Smarte47c9092008-02-08 18:49:26 -05006257 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07006258 "0279 %s: ndlp:x%px did %x "
James Smartbe6bb942015-04-07 15:07:22 -04006259 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart32350662019-08-14 16:57:06 -07006260 __func__,
James Smart939723a2012-05-09 21:19:03 -04006261 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006262 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006263
6264 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05006265 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05006266
6267 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04006268 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006269 spin_lock_irqsave(&phba->ndlp_lock, flags);
6270 NLP_CLR_NODE_ACT(ndlp);
6271 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6272
6273 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04006274 if (NLP_CHK_FREE_REQ(ndlp)) {
6275 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05006276 if (phba->sli_rev == LPFC_SLI_REV4)
6277 mempool_free(ndlp->active_rrqs_xri_bitmap,
6278 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04006279 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04006280 }
James Smart329f9bc2007-04-25 09:53:01 -04006281}
6282
James Smart98c9ea52007-10-27 13:37:33 -04006283/* This routine bumps the reference count for a ndlp structure to ensure
6284 * that one discovery thread won't free a ndlp while another discovery thread
6285 * is using it.
6286 */
James Smart329f9bc2007-04-25 09:53:01 -04006287struct lpfc_nodelist *
6288lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6289{
James Smarte47c9092008-02-08 18:49:26 -05006290 struct lpfc_hba *phba;
6291 unsigned long flags;
6292
James Smart98c9ea52007-10-27 13:37:33 -04006293 if (ndlp) {
6294 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6295 "node get: did:x%x flg:x%x refcnt:x%x",
6296 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006297 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006298 /* The check of ndlp usage to prevent incrementing the
6299 * ndlp reference count that is in the process of being
6300 * released.
6301 */
James Smarta257bf92009-04-06 18:48:10 -04006302 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006303 spin_lock_irqsave(&phba->ndlp_lock, flags);
6304 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6305 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6306 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07006307 "0276 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05006308 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07006309 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006310 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006311 return NULL;
6312 } else
6313 kref_get(&ndlp->kref);
6314 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04006315 }
James Smart329f9bc2007-04-25 09:53:01 -04006316 return ndlp;
6317}
6318
James Smart98c9ea52007-10-27 13:37:33 -04006319/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05006320 * count goes to 0, this indicates the the associated nodelist should be
6321 * freed. Returning 1 indicates the ndlp resource has been released; on the
6322 * other hand, returning 0 indicates the ndlp resource has not been released
6323 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006324 */
James Smart329f9bc2007-04-25 09:53:01 -04006325int
6326lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6327{
James Smarte47c9092008-02-08 18:49:26 -05006328 struct lpfc_hba *phba;
6329 unsigned long flags;
6330
6331 if (!ndlp)
6332 return 1;
6333
6334 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
James Smart32350662019-08-14 16:57:06 -07006335 "node put: did:x%x flg:x%x refcnt:x%x",
6336 ndlp->nlp_DID, ndlp->nlp_flag,
6337 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006338 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006339 spin_lock_irqsave(&phba->ndlp_lock, flags);
6340 /* Check the ndlp memory free acknowledge flag to avoid the
6341 * possible race condition that kref_put got invoked again
6342 * after previous one has done ndlp memory free.
6343 */
6344 if (NLP_CHK_FREE_ACK(ndlp)) {
6345 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6346 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07006347 "0274 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05006348 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07006349 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006350 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006351 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006352 }
James Smarte47c9092008-02-08 18:49:26 -05006353 /* Check the ndlp inactivate log flag to avoid the possible
6354 * race condition that kref_put got invoked again after ndlp
6355 * is already in inactivating state.
6356 */
6357 if (NLP_CHK_IACT_REQ(ndlp)) {
6358 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6359 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
James Smart32350662019-08-14 16:57:06 -07006360 "0275 %s: ndlp:x%px "
James Smarte47c9092008-02-08 18:49:26 -05006361 "usgmap:x%x refcnt:%d\n",
James Smart32350662019-08-14 16:57:06 -07006362 __func__, (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006363 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006364 return 1;
6365 }
6366 /* For last put, mark the ndlp usage flags to make sure no
6367 * other kref_get and kref_put on the same ndlp shall get
6368 * in between the process when the final kref_put has been
6369 * invoked on this ndlp.
6370 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006371 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006372 /* Indicate ndlp is put to inactive state. */
6373 NLP_SET_IACT_REQ(ndlp);
6374 /* Acknowledge ndlp memory free has been seen. */
6375 if (NLP_CHK_FREE_REQ(ndlp))
6376 NLP_SET_FREE_ACK(ndlp);
6377 }
6378 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6379 /* Note, the kref_put returns 1 when decrementing a reference
6380 * count that was 1, it invokes the release callback function,
6381 * but it still left the reference count as 1 (not actually
6382 * performs the last decrementation). Otherwise, it actually
6383 * decrements the reference count and returns 0.
6384 */
6385 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006386}
James Smart98c9ea52007-10-27 13:37:33 -04006387
6388/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006389 * by any other discovery thread. This routine returns 1 if the
6390 * ndlp has been freed. A return value of 0 indicates the ndlp is
6391 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006392 */
6393int
6394lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6395{
6396 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6397 "node not used: did:x%x flg:x%x refcnt:x%x",
6398 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006399 kref_read(&ndlp->kref));
6400 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006401 if (lpfc_nlp_put(ndlp))
6402 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006403 return 0;
6404}
James Smart6fb120a2009-05-22 14:52:59 -04006405
6406/**
6407 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6408 * @phba: Pointer to hba context object.
6409 *
6410 * This function iterate through all FC nodes associated
6411 * will all vports to check if there is any node with
6412 * fc_rports associated with it. If there is an fc_rport
6413 * associated with the node, then the node is either in
6414 * discovered state or its devloss_timer is pending.
6415 */
6416static int
6417lpfc_fcf_inuse(struct lpfc_hba *phba)
6418{
6419 struct lpfc_vport **vports;
6420 int i, ret = 0;
6421 struct lpfc_nodelist *ndlp;
6422 struct Scsi_Host *shost;
6423
6424 vports = lpfc_create_vport_work_array(phba);
6425
James Smart63e801c2010-11-20 23:14:19 -05006426 /* If driver cannot allocate memory, indicate fcf is in use */
6427 if (!vports)
6428 return 1;
6429
James Smart6fb120a2009-05-22 14:52:59 -04006430 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6431 shost = lpfc_shost_from_vport(vports[i]);
6432 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006433 /*
6434 * IF the CVL_RCVD bit is not set then we have sent the
6435 * flogi.
6436 * If dev_loss fires while we are waiting we do not want to
6437 * unreg the fcf.
6438 */
6439 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6440 spin_unlock_irq(shost->host_lock);
6441 ret = 1;
6442 goto out;
6443 }
James Smart6fb120a2009-05-22 14:52:59 -04006444 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6445 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6446 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6447 ret = 1;
6448 spin_unlock_irq(shost->host_lock);
6449 goto out;
James Smart80c17842012-03-01 22:35:45 -05006450 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6451 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006452 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006453 "2624 RPI %x DID %x flag %x "
6454 "still logged in\n",
6455 ndlp->nlp_rpi, ndlp->nlp_DID,
6456 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006457 }
6458 }
6459 spin_unlock_irq(shost->host_lock);
6460 }
6461out:
6462 lpfc_destroy_vport_work_array(phba, vports);
6463 return ret;
6464}
6465
6466/**
6467 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6468 * @phba: Pointer to hba context object.
6469 * @mboxq: Pointer to mailbox object.
6470 *
6471 * This function frees memory associated with the mailbox command.
6472 */
James Smart1b511972011-12-13 13:23:09 -05006473void
James Smart6fb120a2009-05-22 14:52:59 -04006474lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6475{
6476 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006477 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006478
6479 if (mboxq->u.mb.mbxStatus) {
6480 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6481 "2555 UNREG_VFI mbxStatus error x%x "
6482 "HBA state x%x\n",
6483 mboxq->u.mb.mbxStatus, vport->port_state);
6484 }
James Smart38b92ef2010-08-04 16:11:39 -04006485 spin_lock_irq(shost->host_lock);
6486 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6487 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006488 mempool_free(mboxq, phba->mbox_mem_pool);
6489 return;
6490}
6491
6492/**
6493 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6494 * @phba: Pointer to hba context object.
6495 * @mboxq: Pointer to mailbox object.
6496 *
6497 * This function frees memory associated with the mailbox command.
6498 */
6499static void
6500lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6501{
6502 struct lpfc_vport *vport = mboxq->vport;
6503
6504 if (mboxq->u.mb.mbxStatus) {
6505 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6506 "2550 UNREG_FCFI mbxStatus error x%x "
6507 "HBA state x%x\n",
6508 mboxq->u.mb.mbxStatus, vport->port_state);
6509 }
6510 mempool_free(mboxq, phba->mbox_mem_pool);
6511 return;
6512}
6513
6514/**
James Smartecfd03c2010-02-12 14:41:27 -05006515 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006516 * @phba: Pointer to hba context object.
6517 *
James Smartecfd03c2010-02-12 14:41:27 -05006518 * This function prepare the HBA for unregistering the currently registered
6519 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6520 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006521 */
James Smartecfd03c2010-02-12 14:41:27 -05006522int
6523lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006524{
James Smart6fb120a2009-05-22 14:52:59 -04006525 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006526 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006527 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006528 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006529
James Smartecfd03c2010-02-12 14:41:27 -05006530 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006531 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006532 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006533
James Smart4d9ab992009-10-02 15:16:39 -04006534 /* At this point, all discovery is aborted */
6535 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006536
6537 /* Unregister VPIs */
6538 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006539 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006540 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006541 /* Stop FLOGI/FDISC retries */
6542 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6543 if (ndlp)
6544 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006545 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006546 if (phba->sli_rev == LPFC_SLI_REV4)
6547 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006548 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006549 shost = lpfc_shost_from_vport(vports[i]);
6550 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006551 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006552 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006553 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006554 }
6555 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006556 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6557 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6558 if (ndlp)
6559 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6560 lpfc_cleanup_pending_mbox(phba->pport);
6561 if (phba->sli_rev == LPFC_SLI_REV4)
6562 lpfc_sli4_unreg_all_rpis(phba->pport);
6563 lpfc_mbx_unreg_vpi(phba->pport);
6564 shost = lpfc_shost_from_vport(phba->pport);
6565 spin_lock_irq(shost->host_lock);
6566 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6567 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6568 spin_unlock_irq(shost->host_lock);
6569 }
James Smart6fb120a2009-05-22 14:52:59 -04006570
James Smart695a8142010-01-26 23:08:03 -05006571 /* Cleanup any outstanding ELS commands */
6572 lpfc_els_flush_all_cmd(phba);
6573
James Smart1b511972011-12-13 13:23:09 -05006574 /* Unregister the physical port VFI */
6575 rc = lpfc_issue_unreg_vfi(phba->pport);
6576 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006577}
6578
6579/**
6580 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6581 * @phba: Pointer to hba context object.
6582 *
6583 * This function issues synchronous unregister FCF mailbox command to HBA to
6584 * unregister the currently registered FCF record. The driver does not reset
6585 * the driver FCF usage state flags.
6586 *
6587 * Return 0 if successfully issued, none-zero otherwise.
6588 */
6589int
6590lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6591{
6592 LPFC_MBOXQ_t *mbox;
6593 int rc;
6594
James Smart6fb120a2009-05-22 14:52:59 -04006595 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6596 if (!mbox) {
6597 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006598 "2551 UNREG_FCFI mbox allocation failed"
6599 "HBA state x%x\n", phba->pport->port_state);
6600 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006601 }
James Smart6fb120a2009-05-22 14:52:59 -04006602 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6603 mbox->vport = phba->pport;
6604 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6605 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6606
6607 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006608 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6609 "2552 Unregister FCFI command failed rc x%x "
6610 "HBA state x%x\n",
6611 rc, phba->pport->port_state);
6612 return -EINVAL;
6613 }
6614 return 0;
6615}
6616
6617/**
6618 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6619 * @phba: Pointer to hba context object.
6620 *
6621 * This function unregisters the currently reigstered FCF. This function
6622 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6623 */
6624void
6625lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6626{
6627 int rc;
6628
6629 /* Preparation for unregistering fcf */
6630 rc = lpfc_unregister_fcf_prep(phba);
6631 if (rc) {
6632 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6633 "2748 Failed to prepare for unregistering "
6634 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006635 return;
6636 }
6637
James Smartecfd03c2010-02-12 14:41:27 -05006638 /* Now, unregister FCF record and reset HBA FCF state */
6639 rc = lpfc_sli4_unregister_fcf(phba);
6640 if (rc)
6641 return;
6642 /* Reset HBA FCF states after successful unregister FCF */
6643 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006644 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006645
6646 /*
6647 * If driver is not unloading, check if there is any other
6648 * FCF record that can be used for discovery.
6649 */
6650 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006651 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006652 return;
6653
James Smart0c9ab6f2010-02-26 14:15:57 -05006654 /* This is considered as the initial FCF discovery scan */
6655 spin_lock_irq(&phba->hbalock);
6656 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6657 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006658
6659 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006660 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006661
James Smart0c9ab6f2010-02-26 14:15:57 -05006662 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006663
James Smart0c9ab6f2010-02-26 14:15:57 -05006664 if (rc) {
6665 spin_lock_irq(&phba->hbalock);
6666 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6667 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006668 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006669 "2553 lpfc_unregister_unused_fcf failed "
6670 "to read FCF record HBA state x%x\n",
6671 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006672 }
James Smartecfd03c2010-02-12 14:41:27 -05006673}
6674
6675/**
6676 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6677 * @phba: Pointer to hba context object.
6678 *
6679 * This function just unregisters the currently reigstered FCF. It does not
6680 * try to find another FCF for discovery.
6681 */
6682void
6683lpfc_unregister_fcf(struct lpfc_hba *phba)
6684{
6685 int rc;
6686
6687 /* Preparation for unregistering fcf */
6688 rc = lpfc_unregister_fcf_prep(phba);
6689 if (rc) {
6690 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6691 "2749 Failed to prepare for unregistering "
6692 "HBA's FCF record: rc=%d\n", rc);
6693 return;
6694 }
6695
6696 /* Now, unregister FCF record and reset HBA FCF state */
6697 rc = lpfc_sli4_unregister_fcf(phba);
6698 if (rc)
6699 return;
6700 /* Set proper HBA FCF states after successful unregister FCF */
6701 spin_lock_irq(&phba->hbalock);
6702 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6703 spin_unlock_irq(&phba->hbalock);
6704}
6705
6706/**
6707 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6708 * @phba: Pointer to hba context object.
6709 *
6710 * This function check if there are any connected remote port for the FCF and
6711 * if all the devices are disconnected, this function unregister FCFI.
6712 * This function also tries to use another FCF for discovery.
6713 */
6714void
6715lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6716{
6717 /*
James Smart3804dc82010-07-14 15:31:37 -04006718 * If HBA is not running in FIP mode, if HBA does not support
6719 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6720 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006721 */
6722 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006723 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006724 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006725 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006726 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006727 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006728 spin_unlock_irq(&phba->hbalock);
6729 return;
6730 }
6731 spin_unlock_irq(&phba->hbalock);
6732
6733 if (lpfc_fcf_inuse(phba))
6734 return;
6735
6736 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006737}
6738
6739/**
6740 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6741 * @phba: Pointer to hba context object.
6742 * @buff: Buffer containing the FCF connection table as in the config
6743 * region.
6744 * This function create driver data structure for the FCF connection
6745 * record table read from config region 23.
6746 */
6747static void
6748lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6749 uint8_t *buff)
6750{
6751 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6752 struct lpfc_fcf_conn_hdr *conn_hdr;
6753 struct lpfc_fcf_conn_rec *conn_rec;
6754 uint32_t record_count;
6755 int i;
6756
6757 /* Free the current connect table */
6758 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006759 &phba->fcf_conn_rec_list, list) {
6760 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006761 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006762 }
James Smart6fb120a2009-05-22 14:52:59 -04006763
6764 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6765 record_count = conn_hdr->length * sizeof(uint32_t)/
6766 sizeof(struct lpfc_fcf_conn_rec);
6767
6768 conn_rec = (struct lpfc_fcf_conn_rec *)
6769 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6770
6771 for (i = 0; i < record_count; i++) {
6772 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6773 continue;
6774 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6775 GFP_KERNEL);
6776 if (!conn_entry) {
6777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6778 "2566 Failed to allocate connection"
6779 " table entry\n");
6780 return;
6781 }
6782
6783 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6784 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006785 list_add_tail(&conn_entry->list,
6786 &phba->fcf_conn_rec_list);
6787 }
James Smartdf0d0852013-05-31 17:05:08 -04006788
6789 if (!list_empty(&phba->fcf_conn_rec_list)) {
6790 i = 0;
6791 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6792 list) {
6793 conn_rec = &conn_entry->conn_rec;
6794 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6795 "3345 FCF connection list rec[%02d]: "
6796 "flags:x%04x, vtag:x%04x, "
6797 "fabric_name:x%02x:%02x:%02x:%02x:"
6798 "%02x:%02x:%02x:%02x, "
6799 "switch_name:x%02x:%02x:%02x:%02x:"
6800 "%02x:%02x:%02x:%02x\n", i++,
6801 conn_rec->flags, conn_rec->vlan_tag,
6802 conn_rec->fabric_name[0],
6803 conn_rec->fabric_name[1],
6804 conn_rec->fabric_name[2],
6805 conn_rec->fabric_name[3],
6806 conn_rec->fabric_name[4],
6807 conn_rec->fabric_name[5],
6808 conn_rec->fabric_name[6],
6809 conn_rec->fabric_name[7],
6810 conn_rec->switch_name[0],
6811 conn_rec->switch_name[1],
6812 conn_rec->switch_name[2],
6813 conn_rec->switch_name[3],
6814 conn_rec->switch_name[4],
6815 conn_rec->switch_name[5],
6816 conn_rec->switch_name[6],
6817 conn_rec->switch_name[7]);
6818 }
6819 }
James Smart6fb120a2009-05-22 14:52:59 -04006820}
6821
6822/**
6823 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6824 * @phba: Pointer to hba context object.
6825 * @buff: Buffer containing the FCoE parameter data structure.
6826 *
6827 * This function update driver data structure with config
6828 * parameters read from config region 23.
6829 */
6830static void
6831lpfc_read_fcoe_param(struct lpfc_hba *phba,
6832 uint8_t *buff)
6833{
6834 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6835 struct lpfc_fcoe_params *fcoe_param;
6836
6837 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6838 buff;
6839 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006840 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006841
6842 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6843 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6844 return;
6845
James Smart6fb120a2009-05-22 14:52:59 -04006846 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6847 phba->valid_vlan = 1;
6848 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6849 0xFFF;
6850 }
6851
6852 phba->fc_map[0] = fcoe_param->fc_map[0];
6853 phba->fc_map[1] = fcoe_param->fc_map[1];
6854 phba->fc_map[2] = fcoe_param->fc_map[2];
6855 return;
6856}
6857
6858/**
6859 * lpfc_get_rec_conf23 - Get a record type in config region data.
6860 * @buff: Buffer containing config region 23 data.
6861 * @size: Size of the data buffer.
6862 * @rec_type: Record type to be searched.
6863 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006864 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006865 * of the record specified by record_type. If record found, this
6866 * function return pointer to the record else return NULL.
6867 */
6868static uint8_t *
6869lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6870{
6871 uint32_t offset = 0, rec_length;
6872
6873 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6874 (size < sizeof(uint32_t)))
6875 return NULL;
6876
6877 rec_length = buff[offset + 1];
6878
6879 /*
6880 * One TLV record has one word header and number of data words
6881 * specified in the rec_length field of the record header.
6882 */
6883 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6884 <= size) {
6885 if (buff[offset] == rec_type)
6886 return &buff[offset];
6887
6888 if (buff[offset] == LPFC_REGION23_LAST_REC)
6889 return NULL;
6890
6891 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6892 rec_length = buff[offset + 1];
6893 }
6894 return NULL;
6895}
6896
6897/**
6898 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6899 * @phba: Pointer to lpfc_hba data structure.
6900 * @buff: Buffer containing config region 23 data.
6901 * @size: Size of the data buffer.
6902 *
Stefan Weileef35c22010-08-06 21:11:15 +02006903 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006904 * populate driver data structure with the parameters.
6905 */
6906void
6907lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6908 uint8_t *buff,
6909 uint32_t size)
6910{
James Smarteb016562014-09-03 12:58:06 -04006911 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006912 uint8_t *rec_ptr;
6913
6914 /*
6915 * If data size is less than 2 words signature and version cannot be
6916 * verified.
6917 */
6918 if (size < 2*sizeof(uint32_t))
6919 return;
6920
6921 /* Check the region signature first */
6922 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6923 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6924 "2567 Config region 23 has bad signature\n");
6925 return;
6926 }
6927
6928 offset += 4;
6929
6930 /* Check the data structure version */
6931 if (buff[offset] != LPFC_REGION23_VERSION) {
6932 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6933 "2568 Config region 23 has bad version\n");
6934 return;
6935 }
6936 offset += 4;
6937
James Smart6fb120a2009-05-22 14:52:59 -04006938 /* Read FCoE param record */
6939 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6940 size - offset, FCOE_PARAM_TYPE);
6941 if (rec_ptr)
6942 lpfc_read_fcoe_param(phba, rec_ptr);
6943
6944 /* Read FCF connection table */
6945 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6946 size - offset, FCOE_CONN_TBL_TYPE);
6947 if (rec_ptr)
6948 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6949
6950}