blob: 4cc63139dafa4b8c51780b88e18e1ae52cba6326 [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 Smart128bdda2018-01-30 15:59:03 -08004 * Copyright (C) 2017-2018 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 Smartc01f3202006-08-18 17:47:08 -0400121
122 rdata = rport->dd_data;
123 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400124 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400125 return;
James Smartc01f3202006-08-18 17:47:08 -0400126
James Smart858c9f62007-06-17 19:56:39 -0500127 vport = ndlp->vport;
128 phba = vport->phba;
129
130 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
131 "rport devlosscb: sid:x%x did:x%x flg:x%x",
132 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
133
James Smart34f5ad82012-08-03 12:35:03 -0400134 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
135 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
136 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
137
James Smarta8adb832007-10-27 13:37:53 -0400138 /* Don't defer this if we are in the process of deleting the vport
139 * or unloading the driver. The unload will cleanup the node
140 * appropriately we just need to cleanup the ndlp rport info here.
141 */
142 if (vport->load_flag & FC_UNLOADING) {
143 put_node = rdata->pnode != NULL;
144 put_rport = ndlp->rport != NULL;
145 rdata->pnode = NULL;
146 ndlp->rport = NULL;
147 if (put_node)
148 lpfc_nlp_put(ndlp);
149 if (put_rport)
150 put_device(&rport->dev);
151 return;
152 }
153
154 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
155 return;
156
James Smart466e8402015-05-21 13:55:28 -0400157 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
158 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
159 "6789 rport name %llx != node port name %llx",
160 rport->port_name,
161 wwn_to_u64(ndlp->nlp_portname.u.wwn));
James Smart34f5ad82012-08-03 12:35:03 -0400162
James Smart858c9f62007-06-17 19:56:39 -0500163 evtp = &ndlp->dev_loss_evt;
164
James Smart466e8402015-05-21 13:55:28 -0400165 if (!list_empty(&evtp->evt_listp)) {
166 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
167 "6790 rport name %llx dev_loss_evt pending",
168 rport->port_name);
James Smart858c9f62007-06-17 19:56:39 -0500169 return;
James Smart466e8402015-05-21 13:55:28 -0400170 }
James Smart858c9f62007-06-17 19:56:39 -0500171
James Smart466e8402015-05-21 13:55:28 -0400172 shost = lpfc_shost_from_vport(vport);
173 spin_lock_irq(shost->host_lock);
174 ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
175 spin_unlock_irq(shost->host_lock);
James Smarta62a4352013-04-17 20:18:19 -0400176
James Smartfa4066b2008-01-11 01:53:27 -0500177 /* We need to hold the node by incrementing the reference
178 * count until this queued work is done
179 */
James Smart466e8402015-05-21 13:55:28 -0400180 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
181
182 spin_lock_irq(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -0400183 if (evtp->evt_arg1) {
184 evtp->evt = LPFC_EVT_DEV_LOSS;
185 list_add_tail(&evtp->evt_listp, &phba->work_list);
186 lpfc_worker_wake_up(phba);
187 }
James Smart858c9f62007-06-17 19:56:39 -0500188 spin_unlock_irq(&phba->hbalock);
189
James Smart858c9f62007-06-17 19:56:39 -0500190 return;
191}
192
James Smarta93ff372010-10-22 11:06:08 -0400193/**
194 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
195 * @ndlp: Pointer to remote node object.
196 *
197 * This function is called from the worker thread when devloss timeout timer
198 * expires. For SLI4 host, this routine shall return 1 when at lease one
199 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
200 * routine shall return 0 when there is no remote node is still in use of FCF
201 * when devloss timeout happened to this @ndlp.
202 **/
203static int
James Smart858c9f62007-06-17 19:56:39 -0500204lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
205{
206 struct lpfc_rport_data *rdata;
207 struct fc_rport *rport;
208 struct lpfc_vport *vport;
209 struct lpfc_hba *phba;
James Smart466e8402015-05-21 13:55:28 -0400210 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500211 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400212 int put_node;
James Smart858c9f62007-06-17 19:56:39 -0500213 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400214 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500215
216 rport = ndlp->rport;
James Smart858c9f62007-06-17 19:56:39 -0500217 vport = ndlp->vport;
James Smart466e8402015-05-21 13:55:28 -0400218 shost = lpfc_shost_from_vport(vport);
219
220 spin_lock_irq(shost->host_lock);
221 ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
222 spin_unlock_irq(shost->host_lock);
223
224 if (!rport)
225 return fcf_inuse;
226
227 name = (uint8_t *) &ndlp->nlp_portname;
James Smart858c9f62007-06-17 19:56:39 -0500228 phba = vport->phba;
229
James Smarta93ff372010-10-22 11:06:08 -0400230 if (phba->sli_rev == LPFC_SLI_REV4)
231 fcf_inuse = lpfc_fcf_inuse(phba);
232
James Smart858c9f62007-06-17 19:56:39 -0500233 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
234 "rport devlosstmo:did:x%x type:x%x id:x%x",
235 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
236
James Smart34f5ad82012-08-03 12:35:03 -0400237 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
238 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
239 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
240
James Smart466e8402015-05-21 13:55:28 -0400241 /*
242 * lpfc_nlp_remove if reached with dangling rport drops the
243 * reference. To make sure that does not happen clear rport
244 * pointer in ndlp before lpfc_nlp_put.
245 */
246 rdata = rport->dd_data;
247
James Smarta8adb832007-10-27 13:37:53 -0400248 /* Don't defer this if we are in the process of deleting the vport
249 * or unloading the driver. The unload will cleanup the node
250 * appropriately we just need to cleanup the ndlp rport info here.
251 */
252 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500253 if (ndlp->nlp_sid != NLP_NO_SID) {
254 /* flush the target */
255 lpfc_sli_abort_iocb(vport,
James Smart895427b2017-02-12 13:52:30 -0800256 &phba->sli.sli3_ring[LPFC_FCP_RING],
257 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smart09372822008-01-11 01:52:54 -0500258 }
James Smarta8adb832007-10-27 13:37:53 -0400259 put_node = rdata->pnode != NULL;
James Smarta8adb832007-10-27 13:37:53 -0400260 rdata->pnode = NULL;
261 ndlp->rport = NULL;
262 if (put_node)
263 lpfc_nlp_put(ndlp);
James Smart466e8402015-05-21 13:55:28 -0400264 put_device(&rport->dev);
265
James Smarta93ff372010-10-22 11:06:08 -0400266 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400267 }
268
James Smartd7c255b2008-08-24 21:50:00 -0400269 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
270 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
271 "0284 Devloss timeout Ignored on "
272 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
273 "NPort x%x\n",
274 *name, *(name+1), *(name+2), *(name+3),
275 *(name+4), *(name+5), *(name+6), *(name+7),
276 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400277 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400278 }
James Smart858c9f62007-06-17 19:56:39 -0500279
James Smart466e8402015-05-21 13:55:28 -0400280 put_node = rdata->pnode != NULL;
281 rdata->pnode = NULL;
282 ndlp->rport = NULL;
283 if (put_node)
284 lpfc_nlp_put(ndlp);
285 put_device(&rport->dev);
286
287 if (ndlp->nlp_type & NLP_FABRIC)
James Smarta93ff372010-10-22 11:06:08 -0400288 return fcf_inuse;
James Smart82085712007-04-25 09:52:41 -0400289
dea31012005-04-17 16:05:31 -0500290 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400291 warn_on = 1;
James Smart895427b2017-02-12 13:52:30 -0800292 lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart51ef4c22007-08-02 11:10:31 -0400293 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500294 }
James Smartc01f3202006-08-18 17:47:08 -0400295
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400296 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400297 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
298 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400299 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
300 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400301 *name, *(name+1), *(name+2), *(name+3),
302 *(name+4), *(name+5), *(name+6), *(name+7),
303 ndlp->nlp_DID, ndlp->nlp_flag,
304 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400305 } else {
James Smarte8b62012007-08-02 11:10:09 -0400306 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
307 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400308 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
309 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400310 *name, *(name+1), *(name+2), *(name+3),
311 *(name+4), *(name+5), *(name+6), *(name+7),
312 ndlp->nlp_DID, ndlp->nlp_flag,
313 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400314 }
315
James Smart2ade92a2017-03-04 09:30:38 -0800316 if (!(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400317 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400318 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400319 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
320 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500321 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400322
James Smarta93ff372010-10-22 11:06:08 -0400323 return fcf_inuse;
324}
325
326/**
327 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
328 * @phba: Pointer to hba context object.
329 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
330 * @nlp_did: remote node identifer with devloss timeout.
331 *
332 * This function is called from the worker thread after invoking devloss
333 * timeout handler and releasing the reference count for the ndlp with
334 * which the devloss timeout was handled for SLI4 host. For the devloss
335 * timeout of the last remote node which had been in use of FCF, when this
336 * routine is invoked, it shall be guaranteed that none of the remote are
337 * in-use of FCF. When devloss timeout to the last remote using the FCF,
338 * if the FIP engine is neither in FCF table scan process nor roundrobin
339 * failover process, the in-use FCF shall be unregistered. If the FIP
340 * engine is in FCF discovery process, the devloss timeout state shall
341 * be set for either the FCF table scan process or roundrobin failover
342 * process to unregister the in-use FCF.
343 **/
344static void
345lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
346 uint32_t nlp_did)
347{
348 /* If devloss timeout happened to a remote node when FCF had no
349 * longer been in-use, do nothing.
350 */
351 if (!fcf_inuse)
352 return;
353
354 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
355 spin_lock_irq(&phba->hbalock);
356 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
357 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
358 spin_unlock_irq(&phba->hbalock);
359 return;
360 }
361 phba->hba_flag |= HBA_DEVLOSS_TMO;
362 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
363 "2847 Last remote node (x%x) using "
364 "FCF devloss tmo\n", nlp_did);
365 }
366 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
367 spin_unlock_irq(&phba->hbalock);
368 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
369 "2868 Devloss tmo to FCF rediscovery "
370 "in progress\n");
371 return;
372 }
373 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
374 spin_unlock_irq(&phba->hbalock);
375 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
376 "2869 Devloss tmo to idle FIP engine, "
377 "unreg in-use FCF and rescan.\n");
378 /* Unregister in-use FCF and rescan */
379 lpfc_unregister_fcf_rescan(phba);
380 return;
381 }
382 spin_unlock_irq(&phba->hbalock);
383 if (phba->hba_flag & FCF_TS_INPROG)
384 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
385 "2870 FCF table scan in progress\n");
386 if (phba->hba_flag & FCF_RR_INPROG)
387 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
388 "2871 FLOGI roundrobin FCF failover "
389 "in progress\n");
390 }
James Smart6fb120a2009-05-22 14:52:59 -0400391 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500392}
James Smartc01f3202006-08-18 17:47:08 -0400393
James Smartea2151b2008-09-07 11:52:10 -0400394/**
James Smart3621a712009-04-06 18:47:14 -0400395 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400396 * @phba: Pointer to hba context object.
397 *
398 * This function is called from the functions which need to post
399 * events from interrupt context. This function allocates data
400 * structure required for posting event. It also keeps track of
401 * number of events pending and prevent event storm when there are
402 * too many events.
403 **/
404struct lpfc_fast_path_event *
405lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
406 struct lpfc_fast_path_event *ret;
407
408 /* If there are lot of fast event do not exhaust memory due to this */
409 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
410 return NULL;
411
412 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
413 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400414 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400415 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400416 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
417 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
418 }
James Smartea2151b2008-09-07 11:52:10 -0400419 return ret;
420}
421
422/**
James Smart3621a712009-04-06 18:47:14 -0400423 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400424 * @phba: Pointer to hba context object.
425 * @evt: Event object which need to be freed.
426 *
427 * This function frees the data structure required for posting
428 * events.
429 **/
430void
431lpfc_free_fast_evt(struct lpfc_hba *phba,
432 struct lpfc_fast_path_event *evt) {
433
434 atomic_dec(&phba->fast_event_count);
435 kfree(evt);
436}
437
438/**
James Smart3621a712009-04-06 18:47:14 -0400439 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400440 * @phba: Pointer to hba context object.
441 * @evtp: Event data structure.
442 *
443 * This function is called from worker thread, when the interrupt
444 * context need to post an event. This function posts the event
445 * to fc transport netlink interface.
446 **/
447static void
448lpfc_send_fastpath_evt(struct lpfc_hba *phba,
449 struct lpfc_work_evt *evtp)
450{
451 unsigned long evt_category, evt_sub_category;
452 struct lpfc_fast_path_event *fast_evt_data;
453 char *evt_data;
454 uint32_t evt_data_size;
455 struct Scsi_Host *shost;
456
457 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
458 work_evt);
459
460 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
461 evt_sub_category = (unsigned long) fast_evt_data->un.
462 fabric_evt.subcategory;
463 shost = lpfc_shost_from_vport(fast_evt_data->vport);
464 if (evt_category == FC_REG_FABRIC_EVENT) {
465 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
466 evt_data = (char *) &fast_evt_data->un.read_check_error;
467 evt_data_size = sizeof(fast_evt_data->un.
468 read_check_error);
469 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500470 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400471 evt_data = (char *) &fast_evt_data->un.fabric_evt;
472 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
473 } else {
474 lpfc_free_fast_evt(phba, fast_evt_data);
475 return;
476 }
477 } else if (evt_category == FC_REG_SCSI_EVENT) {
478 switch (evt_sub_category) {
479 case LPFC_EVENT_QFULL:
480 case LPFC_EVENT_DEVBSY:
481 evt_data = (char *) &fast_evt_data->un.scsi_evt;
482 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
483 break;
484 case LPFC_EVENT_CHECK_COND:
485 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
486 evt_data_size = sizeof(fast_evt_data->un.
487 check_cond_evt);
488 break;
489 case LPFC_EVENT_VARQUEDEPTH:
490 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
491 evt_data_size = sizeof(fast_evt_data->un.
492 queue_depth_evt);
493 break;
494 default:
495 lpfc_free_fast_evt(phba, fast_evt_data);
496 return;
497 }
498 } else {
499 lpfc_free_fast_evt(phba, fast_evt_data);
500 return;
501 }
502
James Smart895427b2017-02-12 13:52:30 -0800503 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
504 fc_host_post_vendor_event(shost,
505 fc_get_event_number(),
506 evt_data_size,
507 evt_data,
508 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400509
510 lpfc_free_fast_evt(phba, fast_evt_data);
511 return;
512}
513
dea31012005-04-17 16:05:31 -0500514static void
James Smart2e0fef82007-06-17 19:56:36 -0500515lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500516{
517 struct lpfc_work_evt *evtp = NULL;
518 struct lpfc_nodelist *ndlp;
519 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400520 int fcf_inuse;
521 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500522
James Smart2e0fef82007-06-17 19:56:36 -0500523 spin_lock_irq(&phba->hbalock);
524 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500525 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
526 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500527 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500528 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500529 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500530 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500531 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500532 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500533 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500534 /* decrement the node reference count held
535 * for this queued work
536 */
537 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500538 break;
James Smart858c9f62007-06-17 19:56:39 -0500539 case LPFC_EVT_DEV_LOSS:
540 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400541 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500542 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500543 /* decrement the node reference count held for
544 * this queued work
545 */
James Smarta93ff372010-10-22 11:06:08 -0400546 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500547 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400548 if (phba->sli_rev == LPFC_SLI_REV4)
549 lpfc_sli4_post_dev_loss_tmo_handler(phba,
550 fcf_inuse,
551 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500552 break;
dea31012005-04-17 16:05:31 -0500553 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500554 if (phba->link_state < LPFC_LINK_DOWN)
555 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500556 else
James Smart2e0fef82007-06-17 19:56:36 -0500557 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500558 complete((struct completion *)(evtp->evt_arg2));
559 break;
James Smart46fa3112007-04-25 09:51:45 -0400560 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500561 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400562 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400563 *(int *)(evtp->evt_arg1) = 0;
564 complete((struct completion *)(evtp->evt_arg2));
565 break;
566 case LPFC_EVT_OFFLINE:
567 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500568 lpfc_sli_brdrestart(phba);
569 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400570 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
571 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500572 complete((struct completion *)(evtp->evt_arg2));
573 break;
574 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400575 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500576 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500577 lpfc_sli_brdreset(phba);
578 lpfc_hba_down_post(phba);
579 *(int *)(evtp->evt_arg1) =
580 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400581 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500582 complete((struct completion *)(evtp->evt_arg2));
583 break;
584 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400585 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500586 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500587 = (phba->pport->stopped)
588 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400589 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500590 complete((struct completion *)(evtp->evt_arg2));
591 break;
James Smartea2151b2008-09-07 11:52:10 -0400592 case LPFC_EVT_FASTPATH_MGMT_EVT:
593 lpfc_send_fastpath_evt(phba, evtp);
594 free_evt = 0;
595 break;
James Smart78730cf2010-04-06 15:06:30 -0400596 case LPFC_EVT_RESET_HBA:
597 if (!(phba->pport->load_flag & FC_UNLOADING))
598 lpfc_reset_hba(phba);
599 break;
dea31012005-04-17 16:05:31 -0500600 }
601 if (free_evt)
602 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500603 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500604 }
James Smart2e0fef82007-06-17 19:56:36 -0500605 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500606
607}
608
James Smart311464e2007-08-02 11:10:37 -0400609static void
James Smart2e0fef82007-06-17 19:56:36 -0500610lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500611{
612 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500613 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400614 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400615 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400616 int i;
dea31012005-04-17 16:05:31 -0500617
James Smart2e0fef82007-06-17 19:56:36 -0500618 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500619 ha_copy = phba->work_ha;
620 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500621 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500622
James Smartda0436e2009-05-22 14:51:39 -0400623 /* First, try to post the next mailbox command to SLI4 device */
624 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
625 lpfc_sli4_post_async_mbox(phba);
626
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500627 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400628 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500629 lpfc_handle_eratt(phba);
630
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500631 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500632 lpfc_sli_handle_mb_event(phba);
633
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500634 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500635 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400636
James Smartda0436e2009-05-22 14:51:39 -0400637 /* Process SLI4 events */
638 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500639 if (phba->hba_flag & HBA_RRQ_ACTIVE)
640 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400641 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
642 lpfc_sli4_fcp_xri_abort_event_proc(phba);
643 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
644 lpfc_sli4_els_xri_abort_event_proc(phba);
645 if (phba->hba_flag & ASYNC_EVENT)
646 lpfc_sli4_async_event_proc(phba);
647 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
648 spin_lock_irq(&phba->hbalock);
649 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
650 spin_unlock_irq(&phba->hbalock);
651 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
652 }
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{
James Smarta0f2d3e2017-02-12 13:52:31 -0800862 struct lpfc_hba *phba = vport->phba;
James Smart98c9ea52007-10-27 13:37:33 -0400863 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
864
James Smarta0f2d3e2017-02-12 13:52:31 -0800865 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
866 fc_host_post_event(shost, fc_get_event_number(),
867 FCH_EVT_LINKDOWN, 0);
James Smart98c9ea52007-10-27 13:37:33 -0400868
869 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
870 "Link Down: state:x%x rtry:x%x flg:x%x",
871 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
872
873 lpfc_port_link_failure(vport);
874
James Smart92494142011-02-16 12:39:44 -0500875 /* Stop delayed Nport discovery */
876 spin_lock_irq(shost->host_lock);
877 vport->fc_flag &= ~FC_DISC_DELAYED;
878 spin_unlock_irq(shost->host_lock);
879 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400880}
881
dea31012005-04-17 16:05:31 -0500882int
James Smart685f0bf2007-04-25 09:53:08 -0400883lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500884{
James Smart2e0fef82007-06-17 19:56:36 -0500885 struct lpfc_vport *vport = phba->pport;
886 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400887 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400888 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400889 int i;
dea31012005-04-17 16:05:31 -0500890
James Smart3163f722008-02-08 18:50:25 -0500891 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500892 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500893
894 /* Block all SCSI stack I/Os */
895 lpfc_scsi_dev_block(phba);
896
James Smart2e0fef82007-06-17 19:56:36 -0500897 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500898 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500899 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500900 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500901 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500902 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500903 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500904 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500905 }
James Smart549e55c2007-08-02 11:09:51 -0400906 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -0800907 if (vports != NULL) {
James Smart6fb120a2009-05-22 14:52:59 -0400908 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400909 /* Issue a LINK DOWN event to all nodes */
910 lpfc_linkdown_port(vports[i]);
James Smart895427b2017-02-12 13:52:30 -0800911
912 vports[i]->fc_myDID = 0;
913
James Smart01649562017-02-12 13:52:32 -0800914 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -0800915 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -0800916 if (phba->nvmet_support)
917 lpfc_nvmet_update_targetport(phba);
918 else
James Smart8c258642017-02-12 13:52:36 -0800919 lpfc_nvme_update_localport(vports[i]);
James Smart8c258642017-02-12 13:52:36 -0800920 }
James Smart549e55c2007-08-02 11:09:51 -0400921 }
James Smart895427b2017-02-12 13:52:30 -0800922 }
James Smart09372822008-01-11 01:52:54 -0500923 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500924 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500925 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
926 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400927 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500928 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500929 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400930 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500931 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500932 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500933 }
934 }
935
dea31012005-04-17 16:05:31 -0500936 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500937 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500938 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
939 if (mb) {
dea31012005-04-17 16:05:31 -0500940 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500941 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500942 mb->vport = vport;
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 }
James Smart2e0fef82007-06-17 19:56:36 -0500948 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500949 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smartd496b9a2018-10-23 13:41:08 -0700950 phba->pport->rcv_flogi_cnt = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500951 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500952 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500953 return 0;
dea31012005-04-17 16:05:31 -0500954}
955
James Smart92d7f7b2007-06-17 19:56:38 -0500956static void
957lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500958{
James Smart92d7f7b2007-06-17 19:56:38 -0500959 struct lpfc_nodelist *ndlp;
960
961 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarta3da8252018-03-05 12:04:06 -0800962 ndlp->nlp_fc4_type &= ~(NLP_FC4_FCP | NLP_FC4_NVME);
James Smarte47c9092008-02-08 18:49:26 -0500963 if (!NLP_CHK_NODE_ACT(ndlp))
964 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500965 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
966 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500967 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500968 /* On Linkup its safe to clean up the ndlp
969 * from Fabric connections.
970 */
James Smart92d7f7b2007-06-17 19:56:38 -0500971 if (ndlp->nlp_DID != Fabric_DID)
972 lpfc_unreg_rpi(vport, ndlp);
973 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
974 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500975 /* Fail outstanding IO now since device is
976 * marked for PLOGI.
977 */
James Smart92d7f7b2007-06-17 19:56:38 -0500978 lpfc_unreg_rpi(vport, ndlp);
979 }
980 }
981}
982
983static void
984lpfc_linkup_port(struct lpfc_vport *vport)
985{
986 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500987 struct lpfc_hba *phba = vport->phba;
988
989 if ((vport->load_flag & FC_UNLOADING) != 0)
990 return;
991
James Smart858c9f62007-06-17 19:56:39 -0500992 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
993 "Link Up: top:x%x speed:x%x flg:x%x",
994 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
995
James Smart92d7f7b2007-06-17 19:56:38 -0500996 /* If NPIV is not enabled, only bring the physical port up */
997 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
998 (vport != phba->pport))
999 return;
dea31012005-04-17 16:05:31 -05001000
James Smarta0f2d3e2017-02-12 13:52:31 -08001001 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1002 fc_host_post_event(shost, fc_get_event_number(),
1003 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001004
James Smart2e0fef82007-06-17 19:56:36 -05001005 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001006 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1007 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1008 vport->fc_flag |= FC_NDISC_ACTIVE;
1009 vport->fc_ns_retry = 0;
1010 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001011
James Smart92d7f7b2007-06-17 19:56:38 -05001012 if (vport->fc_flag & FC_LBIT)
1013 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001014
James Smart92d7f7b2007-06-17 19:56:38 -05001015}
1016
1017static int
1018lpfc_linkup(struct lpfc_hba *phba)
1019{
James Smart549e55c2007-08-02 11:09:51 -04001020 struct lpfc_vport **vports;
1021 int i;
James Smartd496b9a2018-10-23 13:41:08 -07001022 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
James Smart92d7f7b2007-06-17 19:56:38 -05001023
1024 phba->link_state = LPFC_LINK_UP;
1025
1026 /* Unblock fabric iocbs if they are blocked */
1027 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1028 del_timer_sync(&phba->fabric_block_timer);
1029
James Smart549e55c2007-08-02 11:09:51 -04001030 vports = lpfc_create_vport_work_array(phba);
1031 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001032 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001033 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001034 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001035
James Smartd496b9a2018-10-23 13:41:08 -07001036 /* Clear the pport flogi counter in case the link down was
1037 * absorbed without an ACQE. No lock here - in worker thread
1038 * and discovery is synchronized.
1039 */
1040 spin_lock_irq(shost->host_lock);
1041 phba->pport->rcv_flogi_cnt = 0;
1042 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001043 return 0;
1044}
1045
1046/*
1047 * This routine handles processing a CLEAR_LA mailbox
1048 * command upon completion. It is setup in the LPFC_MBOXQ
1049 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001050 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001051 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001052static void
James Smart2e0fef82007-06-17 19:56:36 -05001053lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001054{
James Smart2e0fef82007-06-17 19:56:36 -05001055 struct lpfc_vport *vport = pmb->vport;
1056 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1057 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001058 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001059 uint32_t control;
1060
dea31012005-04-17 16:05:31 -05001061 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001062 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1063 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001064
1065 /* Check for error */
1066 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001067 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001068 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1069 "0320 CLEAR_LA mbxStatus error x%x hba "
1070 "state x%x\n",
1071 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001072 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001073 goto out;
1074 }
1075
James Smart92d7f7b2007-06-17 19:56:38 -05001076 if (vport->port_type == LPFC_PHYSICAL_PORT)
1077 phba->link_state = LPFC_HBA_READY;
1078
1079 spin_lock_irq(&phba->hbalock);
1080 psli->sli_flag |= LPFC_PROCESS_LA;
1081 control = readl(phba->HCregaddr);
1082 control |= HC_LAINT_ENA;
1083 writel(control, phba->HCregaddr);
1084 readl(phba->HCregaddr); /* flush */
1085 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001086 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001087 return;
dea31012005-04-17 16:05:31 -05001088
dea31012005-04-17 16:05:31 -05001089out:
1090 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001091 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1092 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001093 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001094
James Smart2e0fef82007-06-17 19:56:36 -05001095 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001096 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001097 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001098
James Smart2e0fef82007-06-17 19:56:36 -05001099 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001100
1101 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001102
1103 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001104 psli->sli_flag |= LPFC_PROCESS_LA;
1105 control = readl(phba->HCregaddr);
1106 control |= HC_LAINT_ENA;
1107 writel(control, phba->HCregaddr);
1108 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001109 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001110
1111 return;
1112}
1113
James Smart2e0fef82007-06-17 19:56:36 -05001114
James Smartd6de08c2015-12-16 18:11:53 -05001115void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001116lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001117{
James Smart2e0fef82007-06-17 19:56:36 -05001118 struct lpfc_vport *vport = pmb->vport;
James Smart44fd7fe2017-08-23 16:55:47 -07001119 uint8_t bbscn = 0;
dea31012005-04-17 16:05:31 -05001120
James Smart04c68492009-05-22 14:52:52 -04001121 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001122 goto out;
dea31012005-04-17 16:05:31 -05001123
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001124 mempool_free(pmb, phba->mbox_mem_pool);
1125
James Smart1b511972011-12-13 13:23:09 -05001126 /* don't perform discovery for SLI4 loopback diagnostic test */
1127 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1128 !(phba->hba_flag & HBA_FCOE_MODE) &&
1129 (phba->link_flag & LS_LOOPBACK_MODE))
1130 return;
1131
James Smart76a95d72010-11-20 23:11:48 -05001132 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001133 vport->fc_flag & FC_PUBLIC_LOOP &&
1134 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001135 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001136 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001137 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1138 */
James Smart2e0fef82007-06-17 19:56:36 -05001139 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001140 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001141 }
dea31012005-04-17 16:05:31 -05001142
James Smart2e0fef82007-06-17 19:56:36 -05001143 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001144 * LPFC_FLOGI while waiting for FLOGI cmpl
1145 */
James Smart44fd7fe2017-08-23 16:55:47 -07001146 if (vport->port_state != LPFC_FLOGI) {
1147 if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
1148 bbscn = bf_get(lpfc_bbscn_def,
1149 &phba->sli4_hba.bbscn_params);
1150 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
1151 vport->fc_sparam.cmn.bbRcvSizeMsb |= (bbscn << 4);
1152 }
James Smart92d7f7b2007-06-17 19:56:38 -05001153 lpfc_initial_flogi(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001154 } else if (vport->fc_flag & FC_PT2PT) {
James Smartd6de08c2015-12-16 18:11:53 -05001155 lpfc_disc_start(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001156 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001157 return;
dea31012005-04-17 16:05:31 -05001158
1159out:
James Smarte8b62012007-08-02 11:10:09 -04001160 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1161 "0306 CONFIG_LINK mbxStatus error x%x "
1162 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001163 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001164 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001165
1166 lpfc_linkdown(phba);
1167
James Smarte8b62012007-08-02 11:10:09 -04001168 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1169 "0200 CONFIG_LINK bad hba state x%x\n",
1170 vport->port_state);
dea31012005-04-17 16:05:31 -05001171
James Smart92d7f7b2007-06-17 19:56:38 -05001172 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001173 return;
1174}
1175
James Smart7d791df2011-07-22 18:37:52 -04001176/**
1177 * lpfc_sli4_clear_fcf_rr_bmask
1178 * @phba pointer to the struct lpfc_hba for this port.
1179 * This fucnction resets the round robin bit mask and clears the
1180 * fcf priority list. The list deletions are done while holding the
1181 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1182 * from the lpfc_fcf_pri record.
1183 **/
1184void
1185lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1186{
1187 struct lpfc_fcf_pri *fcf_pri;
1188 struct lpfc_fcf_pri *next_fcf_pri;
1189 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1190 spin_lock_irq(&phba->hbalock);
1191 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1192 &phba->fcf.fcf_pri_list, list) {
1193 list_del_init(&fcf_pri->list);
1194 fcf_pri->fcf_rec.flag = 0;
1195 }
1196 spin_unlock_irq(&phba->hbalock);
1197}
dea31012005-04-17 16:05:31 -05001198static void
James Smart6fb120a2009-05-22 14:52:59 -04001199lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1200{
1201 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001202
1203 if (mboxq->u.mb.mbxStatus) {
1204 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1205 "2017 REG_FCFI mbxStatus error x%x "
1206 "HBA state x%x\n",
1207 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001208 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001209 }
1210
1211 /* Start FCoE discovery by sending a FLOGI. */
1212 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1213 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001214 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001215 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001216 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001217
James Smart32b97932009-07-19 10:01:21 -04001218 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001219 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1220 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001221 goto fail_out;
1222
1223 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001224 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001225 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001226 phba->hba_flag &= ~FCF_TS_INPROG;
1227 if (vport->port_state != LPFC_FLOGI) {
1228 phba->hba_flag |= FCF_RR_INPROG;
1229 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001230 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001231 goto out;
1232 }
1233 spin_unlock_irq(&phba->hbalock);
1234 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001235
James Smarta93ff372010-10-22 11:06:08 -04001236fail_out:
1237 spin_lock_irq(&phba->hbalock);
1238 phba->hba_flag &= ~FCF_RR_INPROG;
1239 spin_unlock_irq(&phba->hbalock);
1240out:
James Smart6fb120a2009-05-22 14:52:59 -04001241 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001242}
1243
1244/**
1245 * lpfc_fab_name_match - Check if the fcf fabric name match.
1246 * @fab_name: pointer to fabric name.
1247 * @new_fcf_record: pointer to fcf record.
1248 *
1249 * This routine compare the fcf record's fabric name with provided
1250 * fabric name. If the fabric name are identical this function
1251 * returns 1 else return 0.
1252 **/
1253static uint32_t
1254lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1255{
James Smartecfd03c2010-02-12 14:41:27 -05001256 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001257 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001258 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1259 return 0;
1260 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1261 return 0;
1262 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1263 return 0;
1264 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1265 return 0;
1266 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1267 return 0;
1268 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1269 return 0;
1270 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1271 return 0;
1272 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001273}
1274
1275/**
James Smart8fa38512009-07-19 10:01:03 -04001276 * lpfc_sw_name_match - Check if the fcf switch name match.
1277 * @fab_name: pointer to fabric name.
1278 * @new_fcf_record: pointer to fcf record.
1279 *
1280 * This routine compare the fcf record's switch name with provided
1281 * switch name. If the switch name are identical this function
1282 * returns 1 else return 0.
1283 **/
1284static uint32_t
1285lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1286{
James Smartecfd03c2010-02-12 14:41:27 -05001287 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001288 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001289 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1290 return 0;
1291 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1292 return 0;
1293 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1294 return 0;
1295 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1296 return 0;
1297 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1298 return 0;
1299 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1300 return 0;
1301 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1302 return 0;
1303 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001304}
1305
1306/**
James Smart6fb120a2009-05-22 14:52:59 -04001307 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001308 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001309 * @new_fcf_record: pointer to fcf record.
1310 *
1311 * This routine compare the fcf record's mac address with HBA's
1312 * FCF mac address. If the mac addresses are identical this function
1313 * returns 1 else return 0.
1314 **/
1315static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001316lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001317{
James Smartecfd03c2010-02-12 14:41:27 -05001318 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001319 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001320 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1321 return 0;
1322 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1323 return 0;
1324 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1325 return 0;
1326 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1327 return 0;
1328 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1329 return 0;
1330 return 1;
1331}
1332
1333static bool
1334lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1335{
1336 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001337}
1338
1339/**
James Smart7d791df2011-07-22 18:37:52 -04001340 * lpfc_update_fcf_record - Update driver fcf record
1341 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1342 * @phba: pointer to lpfc hba data structure.
1343 * @fcf_index: Index for the lpfc_fcf_record.
1344 * @new_fcf_record: pointer to hba fcf record.
1345 *
1346 * This routine updates the driver FCF priority record from the new HBA FCF
1347 * record. This routine is called with the host lock held.
1348 **/
1349static void
1350__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1351 struct fcf_record *new_fcf_record
1352 )
1353{
1354 struct lpfc_fcf_pri *fcf_pri;
1355
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001356 lockdep_assert_held(&phba->hbalock);
1357
James Smart7d791df2011-07-22 18:37:52 -04001358 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1359 fcf_pri->fcf_rec.fcf_index = fcf_index;
1360 /* FCF record priority */
1361 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1362
1363}
1364
1365/**
James Smart6fb120a2009-05-22 14:52:59 -04001366 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001367 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001368 * @new_fcf_record: pointer to fcf record.
1369 *
1370 * This routine copies the FCF information from the FCF
1371 * record to lpfc_hba data structure.
1372 **/
1373static void
James Smartecfd03c2010-02-12 14:41:27 -05001374lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1375 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001376{
James Smartecfd03c2010-02-12 14:41:27 -05001377 /* Fabric name */
1378 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001379 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001380 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001381 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001382 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001383 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001384 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001385 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001386 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001387 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001388 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001389 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001390 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001391 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001392 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001393 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 /* Mac address */
1395 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1396 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1397 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1398 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1399 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1400 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1401 /* FCF record index */
1402 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1403 /* FCF record priority */
1404 fcf_rec->priority = new_fcf_record->fip_priority;
1405 /* Switch name */
1406 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001407 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001408 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001409 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001410 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001411 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001412 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001413 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001414 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001415 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001416 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001417 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001418 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001419 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001420 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001421 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001422}
1423
1424/**
James Smartecfd03c2010-02-12 14:41:27 -05001425 * lpfc_update_fcf_record - Update driver fcf record
1426 * @phba: pointer to lpfc hba data structure.
1427 * @fcf_rec: pointer to driver fcf record.
1428 * @new_fcf_record: pointer to hba fcf record.
1429 * @addr_mode: address mode to be set to the driver fcf record.
1430 * @vlan_id: vlan tag to be set to the driver fcf record.
1431 * @flag: flag bits to be set to the driver fcf record.
1432 *
1433 * This routine updates the driver FCF record from the new HBA FCF record
1434 * together with the address mode, vlan_id, and other informations. This
1435 * routine is called with the host lock held.
1436 **/
1437static void
1438__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1439 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1440 uint16_t vlan_id, uint32_t flag)
1441{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001442 lockdep_assert_held(&phba->hbalock);
1443
James Smartecfd03c2010-02-12 14:41:27 -05001444 /* Copy the fields from the HBA's FCF record */
1445 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1446 /* Update other fields of driver FCF record */
1447 fcf_rec->addr_mode = addr_mode;
1448 fcf_rec->vlan_id = vlan_id;
1449 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001450 __lpfc_update_fcf_record_pri(phba,
1451 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1452 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001453}
1454
1455/**
James Smart6fb120a2009-05-22 14:52:59 -04001456 * lpfc_register_fcf - Register the FCF with hba.
1457 * @phba: pointer to lpfc hba data structure.
1458 *
1459 * This routine issues a register fcfi mailbox command to register
1460 * the fcf with HBA.
1461 **/
1462static void
1463lpfc_register_fcf(struct lpfc_hba *phba)
1464{
1465 LPFC_MBOXQ_t *fcf_mbxq;
1466 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001467
James Smartd439d282010-09-29 11:18:45 -04001468 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001469 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001470 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001471 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001472 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001473 return;
1474 }
1475
1476 /* The FCF is already registered, start discovery */
1477 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001478 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001479 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001480 if (phba->pport->port_state != LPFC_FLOGI &&
1481 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001482 phba->hba_flag |= FCF_RR_INPROG;
1483 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001484 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001485 return;
1486 }
1487 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001488 return;
1489 }
James Smartd439d282010-09-29 11:18:45 -04001490 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001491
James Smarta93ff372010-10-22 11:06:08 -04001492 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001493 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001494 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001495 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001496 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001497 return;
James Smart4d9ab992009-10-02 15:16:39 -04001498 }
James Smart6fb120a2009-05-22 14:52:59 -04001499
1500 lpfc_reg_fcfi(phba, fcf_mbxq);
1501 fcf_mbxq->vport = phba->pport;
1502 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1503 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001504 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001505 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001506 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001507 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001508 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001509 }
James Smart6fb120a2009-05-22 14:52:59 -04001510
1511 return;
1512}
1513
1514/**
1515 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1516 * @phba: pointer to lpfc hba data structure.
1517 * @new_fcf_record: pointer to fcf record.
1518 * @boot_flag: Indicates if this record used by boot bios.
1519 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001520 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001521 *
1522 * This routine compare the fcf record with connect list obtained from the
1523 * config region to decide if this FCF can be used for SAN discovery. It returns
1524 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1525 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1526 * is used by boot bios and addr_mode will indicate the addressing mode to be
1527 * used for this FCF when the function returns.
1528 * If the FCF record need to be used with a particular vlan id, the vlan is
1529 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001530 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001531 **/
1532static int
1533lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1534 struct fcf_record *new_fcf_record,
1535 uint32_t *boot_flag, uint32_t *addr_mode,
1536 uint16_t *vlan_id)
1537{
1538 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001539 int i, j, fcf_vlan_id = 0;
1540
1541 /* Find the lowest VLAN id in the FCF record */
1542 for (i = 0; i < 512; i++) {
1543 if (new_fcf_record->vlan_bitmap[i]) {
1544 fcf_vlan_id = i * 8;
1545 j = 0;
1546 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1547 j++;
1548 fcf_vlan_id++;
1549 }
1550 break;
1551 }
1552 }
James Smart6fb120a2009-05-22 14:52:59 -04001553
James Smart26979ce2012-09-29 11:31:55 -04001554 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001555 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001556 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1557 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001558 return 0;
1559
James Smart45ed1192009-10-02 15:17:02 -04001560 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001561 *boot_flag = 0;
1562 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1563 new_fcf_record);
1564 if (phba->valid_vlan)
1565 *vlan_id = phba->vlan_id;
1566 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001567 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001568 return 1;
1569 }
1570
1571 /*
1572 * If there are no FCF connection table entry, driver connect to all
1573 * FCFs.
1574 */
1575 if (list_empty(&phba->fcf_conn_rec_list)) {
1576 *boot_flag = 0;
1577 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1578 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001579
1580 /*
1581 * When there are no FCF connect entries, use driver's default
1582 * addressing mode - FPMA.
1583 */
1584 if (*addr_mode & LPFC_FCF_FPMA)
1585 *addr_mode = LPFC_FCF_FPMA;
1586
James Smart4d9ab992009-10-02 15:16:39 -04001587 /* If FCF record report a vlan id use that vlan id */
1588 if (fcf_vlan_id)
1589 *vlan_id = fcf_vlan_id;
1590 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001591 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001592 return 1;
1593 }
1594
James Smartecfd03c2010-02-12 14:41:27 -05001595 list_for_each_entry(conn_entry,
1596 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001597 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1598 continue;
1599
1600 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1601 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001602 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001603 continue;
James Smart8fa38512009-07-19 10:01:03 -04001604 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1605 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1606 new_fcf_record))
1607 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001608 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1609 /*
1610 * If the vlan bit map does not have the bit set for the
1611 * vlan id to be used, then it is not a match.
1612 */
1613 if (!(new_fcf_record->vlan_bitmap
1614 [conn_entry->conn_rec.vlan_tag / 8] &
1615 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1616 continue;
1617 }
1618
1619 /*
James Smart0c287582009-06-10 17:22:56 -04001620 * If connection record does not support any addressing mode,
1621 * skip the FCF record.
1622 */
1623 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1624 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1625 continue;
1626
1627 /*
James Smart6fb120a2009-05-22 14:52:59 -04001628 * Check if the connection record specifies a required
1629 * addressing mode.
1630 */
1631 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1632 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1633
1634 /*
1635 * If SPMA required but FCF not support this continue.
1636 */
1637 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1638 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1639 new_fcf_record) & LPFC_FCF_SPMA))
1640 continue;
1641
1642 /*
1643 * If FPMA required but FCF not support this continue.
1644 */
1645 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1646 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1647 new_fcf_record) & LPFC_FCF_FPMA))
1648 continue;
1649 }
1650
1651 /*
1652 * This fcf record matches filtering criteria.
1653 */
1654 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1655 *boot_flag = 1;
1656 else
1657 *boot_flag = 0;
1658
James Smart0c287582009-06-10 17:22:56 -04001659 /*
1660 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001661 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001662 * by FCF, allow fabric to pick the addressing mode.
1663 */
James Smart6fb120a2009-05-22 14:52:59 -04001664 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1665 new_fcf_record);
1666 /*
1667 * If the user specified a required address mode, assign that
1668 * address mode
1669 */
1670 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1671 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1672 *addr_mode = (conn_entry->conn_rec.flags &
1673 FCFCNCT_AM_SPMA) ?
1674 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1675 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001676 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001677 * addr mode only if FCF support the addr_mode.
1678 */
1679 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1680 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1681 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1682 (*addr_mode & LPFC_FCF_SPMA))
1683 *addr_mode = LPFC_FCF_SPMA;
1684 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1685 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1686 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1687 (*addr_mode & LPFC_FCF_FPMA))
1688 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001689
James Smart4d9ab992009-10-02 15:16:39 -04001690 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001691 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1692 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001693 /*
1694 * If no vlan id is specified in connect list, use the vlan id
1695 * in the FCF record
1696 */
1697 else if (fcf_vlan_id)
1698 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001699 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001700 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001701
1702 return 1;
1703 }
1704
1705 return 0;
1706}
1707
1708/**
James Smart32b97932009-07-19 10:01:21 -04001709 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1710 * @phba: pointer to lpfc hba data structure.
1711 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1712 *
1713 * This function check if there is any fcoe event pending while driver
1714 * scan FCF entries. If there is any pending event, it will restart the
1715 * FCF saning and return 1 else return 0.
1716 */
1717int
1718lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1719{
James Smart32b97932009-07-19 10:01:21 -04001720 /*
1721 * If the Link is up and no FCoE events while in the
1722 * FCF discovery, no need to restart FCF discovery.
1723 */
1724 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001725 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001726 return 0;
1727
James Smart0c9ab6f2010-02-26 14:15:57 -05001728 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1729 "2768 Pending link or FCF event during current "
1730 "handling of the previous event: link_state:x%x, "
1731 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1732 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1733 phba->fcoe_eventtag);
1734
James Smart32b97932009-07-19 10:01:21 -04001735 spin_lock_irq(&phba->hbalock);
1736 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1737 spin_unlock_irq(&phba->hbalock);
1738
James Smart0c9ab6f2010-02-26 14:15:57 -05001739 if (phba->link_state >= LPFC_LINK_UP) {
1740 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1741 "2780 Restart FCF table scan due to "
1742 "pending FCF event:evt_tag_at_scan:x%x, "
1743 "evt_tag_current:x%x\n",
1744 phba->fcoe_eventtag_at_fcf_scan,
1745 phba->fcoe_eventtag);
1746 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1747 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001748 /*
James Smarta93ff372010-10-22 11:06:08 -04001749 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001750 * flag
1751 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001752 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1753 "2833 Stop FCF discovery process due to link "
1754 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001755 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001756 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001757 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001758 spin_unlock_irq(&phba->hbalock);
1759 }
James Smart32b97932009-07-19 10:01:21 -04001760
James Smart0c9ab6f2010-02-26 14:15:57 -05001761 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001762 if (unreg_fcf) {
1763 spin_lock_irq(&phba->hbalock);
1764 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1765 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001766 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001767 }
James Smart32b97932009-07-19 10:01:21 -04001768 return 1;
1769}
1770
1771/**
James Smart999d8132010-03-15 11:24:56 -04001772 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1773 * @phba: pointer to lpfc hba data structure.
1774 * @fcf_cnt: number of eligible fcf record seen so far.
1775 *
1776 * This function makes an running random selection decision on FCF record to
1777 * use through a sequence of @fcf_cnt eligible FCF records with equal
1778 * probability. To perform integer manunipulation of random numbers with
1779 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001780 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001781 *
1782 * Returns true when outcome is for the newly read FCF record should be
1783 * chosen; otherwise, return false when outcome is for keeping the previously
1784 * chosen FCF record.
1785 **/
1786static bool
1787lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1788{
1789 uint32_t rand_num;
1790
1791 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001792 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001793
1794 /* Decision with probability 1/fcf_cnt */
1795 if ((fcf_cnt * rand_num) < 0xFFFF)
1796 return true;
1797 else
1798 return false;
1799}
1800
1801/**
James Smart3804dc82010-07-14 15:31:37 -04001802 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001803 * @phba: pointer to lpfc hba data structure.
1804 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001805 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001806 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001807 * This routine parses the non-embedded fcf mailbox command by performing the
1808 * necessarily error checking, non-embedded read FCF record mailbox command
1809 * SGE parsing, and endianness swapping.
1810 *
1811 * Returns the pointer to the new FCF record in the non-embedded mailbox
1812 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001813 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001814static struct fcf_record *
1815lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1816 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001817{
1818 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001819 struct lpfc_mbx_sge sge;
1820 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001821 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001822 union lpfc_sli4_cfg_shdr *shdr;
1823 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001824
James Smart6fb120a2009-05-22 14:52:59 -04001825 /* Get the first SGE entry from the non-embedded DMA memory. This
1826 * routine only uses a single SGE.
1827 */
1828 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001829 if (unlikely(!mboxq->sge_array)) {
1830 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1831 "2524 Failed to get the non-embedded SGE "
1832 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001833 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001834 }
1835 virt_addr = mboxq->sge_array->addr[0];
1836
1837 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001838 lpfc_sli_pcimem_bcopy(shdr, shdr,
1839 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001840 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001841 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001842 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001843 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001844 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1845 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001846 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001847 "2726 READ_FCF_RECORD Indicates empty "
1848 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001849 else
1850 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001851 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001852 "with status x%x add_status x%x, "
1853 "mbx\n", shdr_status, shdr_add_status);
1854 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001855 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001856
1857 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001858 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1859 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1860 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001861 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001862 new_fcf_record = (struct fcf_record *)(virt_addr +
1863 sizeof(struct lpfc_mbx_read_fcf_tbl));
1864 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001865 offsetof(struct fcf_record, vlan_bitmap));
1866 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1867 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001868
James Smart0c9ab6f2010-02-26 14:15:57 -05001869 return new_fcf_record;
1870}
1871
1872/**
1873 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1874 * @phba: pointer to lpfc hba data structure.
1875 * @fcf_record: pointer to the fcf record.
1876 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1877 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1878 *
1879 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1880 * enabled.
1881 **/
1882static void
1883lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1884 struct fcf_record *fcf_record,
1885 uint16_t vlan_id,
1886 uint16_t next_fcf_index)
1887{
1888 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1889 "2764 READ_FCF_RECORD:\n"
1890 "\tFCF_Index : x%x\n"
1891 "\tFCF_Avail : x%x\n"
1892 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001893 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001894 "\tFIP_Priority : x%x\n"
1895 "\tMAC_Provider : x%x\n"
1896 "\tLowest VLANID : x%x\n"
1897 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1898 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1899 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1900 "\tNext_FCF_Index: x%x\n",
1901 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1902 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1903 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001904 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001905 fcf_record->fip_priority,
1906 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1907 vlan_id,
1908 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1909 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1910 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1911 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1912 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1913 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1914 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1915 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1916 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1917 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1918 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1919 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1920 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1921 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1922 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1923 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1924 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1925 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1926 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1927 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1928 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1929 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1930 next_fcf_index);
1931}
1932
1933/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001934 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1935 * @phba: pointer to lpfc hba data structure.
1936 * @fcf_rec: pointer to an existing FCF record.
1937 * @new_fcf_record: pointer to a new FCF record.
1938 * @new_vlan_id: vlan id from the new FCF record.
1939 *
1940 * This function performs matching test of a new FCF record against an existing
1941 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1942 * will not be used as part of the FCF record matching criteria.
1943 *
1944 * Returns true if all the fields matching, otherwise returns false.
1945 */
1946static bool
1947lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1948 struct lpfc_fcf_rec *fcf_rec,
1949 struct fcf_record *new_fcf_record,
1950 uint16_t new_vlan_id)
1951{
1952 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1953 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1954 return false;
1955 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1956 return false;
1957 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1958 return false;
1959 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1960 return false;
James Smart7d791df2011-07-22 18:37:52 -04001961 if (fcf_rec->priority != new_fcf_record->fip_priority)
1962 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001963 return true;
1964}
1965
1966/**
James Smarta93ff372010-10-22 11:06:08 -04001967 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1968 * @vport: Pointer to vport object.
1969 * @fcf_index: index to next fcf.
1970 *
1971 * This function processing the roundrobin fcf failover to next fcf index.
1972 * When this function is invoked, there will be a current fcf registered
1973 * for flogi.
1974 * Return: 0 for continue retrying flogi on currently registered fcf;
1975 * 1 for stop flogi on currently registered fcf;
1976 */
1977int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1978{
1979 struct lpfc_hba *phba = vport->phba;
1980 int rc;
1981
1982 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1983 spin_lock_irq(&phba->hbalock);
1984 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1985 spin_unlock_irq(&phba->hbalock);
1986 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1987 "2872 Devloss tmo with no eligible "
1988 "FCF, unregister in-use FCF (x%x) "
1989 "and rescan FCF table\n",
1990 phba->fcf.current_rec.fcf_indx);
1991 lpfc_unregister_fcf_rescan(phba);
1992 goto stop_flogi_current_fcf;
1993 }
1994 /* Mark the end to FLOGI roundrobin failover */
1995 phba->hba_flag &= ~FCF_RR_INPROG;
1996 /* Allow action to new fcf asynchronous event */
1997 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1998 spin_unlock_irq(&phba->hbalock);
1999 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2000 "2865 No FCF available, stop roundrobin FCF "
2001 "failover and change port state:x%x/x%x\n",
2002 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
2003 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart036cad12018-10-23 13:41:06 -07002004
2005 if (!phba->fcf.fcf_redisc_attempted) {
2006 lpfc_unregister_fcf(phba);
2007
2008 rc = lpfc_sli4_redisc_fcf_table(phba);
2009 if (!rc) {
2010 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2011 "3195 Rediscover FCF table\n");
2012 phba->fcf.fcf_redisc_attempted = 1;
2013 lpfc_sli4_clear_fcf_rr_bmask(phba);
2014 } else {
2015 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2016 "3196 Rediscover FCF table "
2017 "failed. Status:x%x\n", rc);
2018 }
2019 } else {
2020 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2021 "3197 Already rediscover FCF table "
2022 "attempted. No more retry\n");
2023 }
James Smarta93ff372010-10-22 11:06:08 -04002024 goto stop_flogi_current_fcf;
2025 } else {
2026 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
2027 "2794 Try FLOGI roundrobin FCF failover to "
2028 "(x%x)\n", fcf_index);
2029 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2030 if (rc)
2031 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2032 "2761 FLOGI roundrobin FCF failover "
2033 "failed (rc:x%x) to read FCF (x%x)\n",
2034 rc, phba->fcf.current_rec.fcf_indx);
2035 else
2036 goto stop_flogi_current_fcf;
2037 }
2038 return 0;
2039
2040stop_flogi_current_fcf:
2041 lpfc_can_disctmo(vport);
2042 return 1;
2043}
2044
2045/**
James Smart7d791df2011-07-22 18:37:52 -04002046 * lpfc_sli4_fcf_pri_list_del
2047 * @phba: pointer to lpfc hba data structure.
2048 * @fcf_index the index of the fcf record to delete
2049 * This routine checks the on list flag of the fcf_index to be deleted.
2050 * If it is one the list then it is removed from the list, and the flag
2051 * is cleared. This routine grab the hbalock before removing the fcf
2052 * record from the list.
2053 **/
2054static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2055 uint16_t fcf_index)
2056{
2057 struct lpfc_fcf_pri *new_fcf_pri;
2058
2059 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2060 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2061 "3058 deleting idx x%x pri x%x flg x%x\n",
2062 fcf_index, new_fcf_pri->fcf_rec.priority,
2063 new_fcf_pri->fcf_rec.flag);
2064 spin_lock_irq(&phba->hbalock);
2065 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2066 if (phba->fcf.current_rec.priority ==
2067 new_fcf_pri->fcf_rec.priority)
2068 phba->fcf.eligible_fcf_cnt--;
2069 list_del_init(&new_fcf_pri->list);
2070 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2071 }
2072 spin_unlock_irq(&phba->hbalock);
2073}
2074
2075/**
2076 * lpfc_sli4_set_fcf_flogi_fail
2077 * @phba: pointer to lpfc hba data structure.
2078 * @fcf_index the index of the fcf record to update
2079 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2080 * flag so the the round robin slection for the particular priority level
2081 * will try a different fcf record that does not have this bit set.
2082 * If the fcf record is re-read for any reason this flag is cleared brfore
2083 * adding it to the priority list.
2084 **/
2085void
2086lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2087{
2088 struct lpfc_fcf_pri *new_fcf_pri;
2089 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2090 spin_lock_irq(&phba->hbalock);
2091 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2092 spin_unlock_irq(&phba->hbalock);
2093}
2094
2095/**
2096 * lpfc_sli4_fcf_pri_list_add
2097 * @phba: pointer to lpfc hba data structure.
2098 * @fcf_index the index of the fcf record to add
2099 * This routine checks the priority of the fcf_index to be added.
2100 * If it is a lower priority than the current head of the fcf_pri list
2101 * then it is added to the list in the right order.
2102 * If it is the same priority as the current head of the list then it
2103 * is added to the head of the list and its bit in the rr_bmask is set.
2104 * If the fcf_index to be added is of a higher priority than the current
2105 * head of the list then the rr_bmask is cleared, its bit is set in the
2106 * rr_bmask and it is added to the head of the list.
2107 * returns:
2108 * 0=success 1=failure
2109 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002110static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2111 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002112 struct fcf_record *new_fcf_record)
2113{
2114 uint16_t current_fcf_pri;
2115 uint16_t last_index;
2116 struct lpfc_fcf_pri *fcf_pri;
2117 struct lpfc_fcf_pri *next_fcf_pri;
2118 struct lpfc_fcf_pri *new_fcf_pri;
2119 int ret;
2120
2121 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2122 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2123 "3059 adding idx x%x pri x%x flg x%x\n",
2124 fcf_index, new_fcf_record->fip_priority,
2125 new_fcf_pri->fcf_rec.flag);
2126 spin_lock_irq(&phba->hbalock);
2127 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2128 list_del_init(&new_fcf_pri->list);
2129 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2130 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2131 if (list_empty(&phba->fcf.fcf_pri_list)) {
2132 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2133 ret = lpfc_sli4_fcf_rr_index_set(phba,
2134 new_fcf_pri->fcf_rec.fcf_index);
2135 goto out;
2136 }
2137
2138 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2139 LPFC_SLI4_FCF_TBL_INDX_MAX);
2140 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2141 ret = 0; /* Empty rr list */
2142 goto out;
2143 }
2144 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2145 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2146 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2147 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2148 memset(phba->fcf.fcf_rr_bmask, 0,
2149 sizeof(*phba->fcf.fcf_rr_bmask));
2150 /* fcfs_at_this_priority_level = 1; */
2151 phba->fcf.eligible_fcf_cnt = 1;
2152 } else
2153 /* fcfs_at_this_priority_level++; */
2154 phba->fcf.eligible_fcf_cnt++;
2155 ret = lpfc_sli4_fcf_rr_index_set(phba,
2156 new_fcf_pri->fcf_rec.fcf_index);
2157 goto out;
2158 }
2159
2160 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2161 &phba->fcf.fcf_pri_list, list) {
2162 if (new_fcf_pri->fcf_rec.priority <=
2163 fcf_pri->fcf_rec.priority) {
2164 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2165 list_add(&new_fcf_pri->list,
2166 &phba->fcf.fcf_pri_list);
2167 else
2168 list_add(&new_fcf_pri->list,
2169 &((struct lpfc_fcf_pri *)
2170 fcf_pri->list.prev)->list);
2171 ret = 0;
2172 goto out;
2173 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2174 || new_fcf_pri->fcf_rec.priority <
2175 next_fcf_pri->fcf_rec.priority) {
2176 list_add(&new_fcf_pri->list, &fcf_pri->list);
2177 ret = 0;
2178 goto out;
2179 }
2180 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2181 continue;
2182
2183 }
2184 ret = 1;
2185out:
2186 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2187 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2188 spin_unlock_irq(&phba->hbalock);
2189 return ret;
2190}
2191
2192/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2194 * @phba: pointer to lpfc hba data structure.
2195 * @mboxq: pointer to mailbox object.
2196 *
2197 * This function iterates through all the fcf records available in
2198 * HBA and chooses the optimal FCF record for discovery. After finding
2199 * the FCF for discovery it registers the FCF record and kicks start
2200 * discovery.
2201 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2202 * use an FCF record which matches fabric name and mac address of the
2203 * currently used FCF record.
2204 * If the driver supports only one FCF, it will try to use the FCF record
2205 * used by BOOT_BIOS.
2206 */
2207void
2208lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2209{
2210 struct fcf_record *new_fcf_record;
2211 uint32_t boot_flag, addr_mode;
2212 uint16_t fcf_index, next_fcf_index;
2213 struct lpfc_fcf_rec *fcf_rec = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08002214 uint16_t vlan_id = LPFC_FCOE_NULL_VID;
James Smart999d8132010-03-15 11:24:56 -04002215 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002216 int rc;
2217
2218 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002219 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002220 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2221 return;
2222 }
2223
2224 /* Parse the FCF record from the non-embedded mailbox command */
2225 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2226 &next_fcf_index);
2227 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002228 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002229 "2765 Mailbox command READ_FCF_RECORD "
2230 "failed to retrieve a FCF record.\n");
2231 /* Let next new FCF event trigger fast failover */
2232 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002233 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002234 spin_unlock_irq(&phba->hbalock);
2235 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2236 return;
2237 }
2238
2239 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002240 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2241 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002242
2243 /* Log the FCF record information if turned on */
2244 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2245 next_fcf_index);
2246
James Smart6fb120a2009-05-22 14:52:59 -04002247 /*
2248 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002249 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002250 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002251 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002252 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002253 lpfc_sli4_fcf_pri_list_del(phba,
2254 bf_get(lpfc_fcf_record_fcf_index,
2255 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002256 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002257 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002258 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002259 bf_get(lpfc_fcf_record_fcf_index,
2260 new_fcf_record),
2261 bf_get(lpfc_fcf_record_fcf_avail,
2262 new_fcf_record),
2263 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002264 new_fcf_record),
2265 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002266 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002267 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2268 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2269 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002270 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2271 phba->fcf.current_rec.fcf_indx) {
2272 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2273 "2862 FCF (x%x) matches property "
2274 "of in-use FCF (x%x)\n",
2275 bf_get(lpfc_fcf_record_fcf_index,
2276 new_fcf_record),
2277 phba->fcf.current_rec.fcf_indx);
2278 goto read_next_fcf;
2279 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002280 /*
2281 * In case the current in-use FCF record becomes
2282 * invalid/unavailable during FCF discovery that
2283 * was not triggered by fast FCF failover process,
2284 * treat it as fast FCF failover.
2285 */
2286 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2287 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2288 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2289 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002290 "(x%x), enter FCF failover "
2291 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002292 phba->fcf.current_rec.fcf_indx);
2293 spin_lock_irq(&phba->hbalock);
2294 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2295 spin_unlock_irq(&phba->hbalock);
2296 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2297 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2298 LPFC_FCOE_FCF_GET_FIRST);
2299 return;
2300 }
2301 }
James Smart6fb120a2009-05-22 14:52:59 -04002302 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002303 } else {
2304 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002305 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2306 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002307 if (rc)
2308 goto read_next_fcf;
2309 }
2310
James Smart6fb120a2009-05-22 14:52:59 -04002311 /*
2312 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002313 * FCF record for the discovery. The condition that a rescan
2314 * matches the in-use FCF record: fabric name, switch name, mac
2315 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002316 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002317 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002318 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002319 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2320 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002321 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002322 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2323 phba->fcf.current_rec.fcf_indx) {
2324 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2325 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2326 /* Stop FCF redisc wait timer */
2327 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2328 phba);
2329 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2330 /* Fast failover, mark completed */
2331 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2332 spin_unlock_irq(&phba->hbalock);
2333 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2334 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002335 "FCF (x%x), port_state:x%x, "
2336 "fc_flag:x%x\n",
2337 phba->fcf.current_rec.fcf_indx,
2338 phba->pport->port_state,
2339 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002340 goto out;
2341 } else
2342 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2343 "2863 New FCF (x%x) matches "
2344 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002345 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002346 new_fcf_record),
2347 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002348 }
James Smartecfd03c2010-02-12 14:41:27 -05002349 /*
2350 * Read next FCF record from HBA searching for the matching
2351 * with in-use record only if not during the fast failover
2352 * period. In case of fast failover period, it shall try to
2353 * determine whether the FCF record just read should be the
2354 * next candidate.
2355 */
2356 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002357 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002358 goto read_next_fcf;
2359 }
James Smart6fb120a2009-05-22 14:52:59 -04002360 }
James Smartecfd03c2010-02-12 14:41:27 -05002361 /*
2362 * Update on failover FCF record only if it's in FCF fast-failover
2363 * period; otherwise, update on current FCF record.
2364 */
James Smartfc2b9892010-02-26 14:15:29 -05002365 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2366 fcf_rec = &phba->fcf.failover_rec;
2367 else
James Smartecfd03c2010-02-12 14:41:27 -05002368 fcf_rec = &phba->fcf.current_rec;
2369
James Smart6fb120a2009-05-22 14:52:59 -04002370 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2371 /*
James Smartecfd03c2010-02-12 14:41:27 -05002372 * If the driver FCF record does not have boot flag
2373 * set and new hba fcf record has boot flag set, use
2374 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002375 */
James Smartecfd03c2010-02-12 14:41:27 -05002376 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2377 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002378 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2379 "2837 Update current FCF record "
2380 "(x%x) with new FCF record (x%x)\n",
2381 fcf_rec->fcf_indx,
2382 bf_get(lpfc_fcf_record_fcf_index,
2383 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002384 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2385 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002386 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002387 goto read_next_fcf;
2388 }
2389 /*
James Smartecfd03c2010-02-12 14:41:27 -05002390 * If the driver FCF record has boot flag set and the
2391 * new hba FCF record does not have boot flag, read
2392 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002393 */
James Smartecfd03c2010-02-12 14:41:27 -05002394 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002395 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002396 goto read_next_fcf;
2397 }
2398 /*
James Smartecfd03c2010-02-12 14:41:27 -05002399 * If the new hba FCF record has lower priority value
2400 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002401 */
James Smartfc2b9892010-02-26 14:15:29 -05002402 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002403 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002404 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2405 "2838 Update current FCF record "
2406 "(x%x) with new FCF record (x%x)\n",
2407 fcf_rec->fcf_indx,
2408 bf_get(lpfc_fcf_record_fcf_index,
2409 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002410 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2411 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002412 /* Reset running random FCF selection count */
2413 phba->fcf.eligible_fcf_cnt = 1;
2414 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2415 /* Update running random FCF selection count */
2416 phba->fcf.eligible_fcf_cnt++;
2417 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2418 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002419 if (select_new_fcf) {
2420 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2421 "2839 Update current FCF record "
2422 "(x%x) with new FCF record (x%x)\n",
2423 fcf_rec->fcf_indx,
2424 bf_get(lpfc_fcf_record_fcf_index,
2425 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002426 /* Choose the new FCF by random selection */
2427 __lpfc_update_fcf_record(phba, fcf_rec,
2428 new_fcf_record,
2429 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002430 }
James Smart6fb120a2009-05-22 14:52:59 -04002431 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002432 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002433 goto read_next_fcf;
2434 }
2435 /*
James Smartecfd03c2010-02-12 14:41:27 -05002436 * This is the first suitable FCF record, choose this record for
2437 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002438 */
James Smartecfd03c2010-02-12 14:41:27 -05002439 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002440 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002441 "2840 Update initial FCF candidate "
2442 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002443 bf_get(lpfc_fcf_record_fcf_index,
2444 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002445 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2446 addr_mode, vlan_id, (boot_flag ?
2447 BOOT_ENABLE : 0));
2448 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002449 /* Setup initial running random FCF selection count */
2450 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002451 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002452 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002453 goto read_next_fcf;
2454
2455read_next_fcf:
2456 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002457 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2458 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2459 /*
2460 * Case of FCF fast failover scan
2461 */
2462
2463 /*
2464 * It has not found any suitable FCF record, cancel
2465 * FCF scan inprogress, and do nothing
2466 */
2467 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002468 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002469 "2782 No suitable FCF found: "
2470 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002471 phba->fcoe_eventtag_at_fcf_scan,
2472 bf_get(lpfc_fcf_record_fcf_index,
2473 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002474 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002475 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2476 phba->hba_flag &= ~FCF_TS_INPROG;
2477 spin_unlock_irq(&phba->hbalock);
2478 /* Unregister in-use FCF and rescan */
2479 lpfc_printf_log(phba, KERN_INFO,
2480 LOG_FIP,
2481 "2864 On devloss tmo "
2482 "unreg in-use FCF and "
2483 "rescan FCF table\n");
2484 lpfc_unregister_fcf_rescan(phba);
2485 return;
2486 }
2487 /*
2488 * Let next new FCF event trigger fast failover
2489 */
2490 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002491 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002492 return;
2493 }
2494 /*
2495 * It has found a suitable FCF record that is not
2496 * the same as in-use FCF record, unregister the
2497 * in-use FCF record, replace the in-use FCF record
2498 * with the new FCF record, mark FCF fast failover
2499 * completed, and then start register the new FCF
2500 * record.
2501 */
2502
James Smart0c9ab6f2010-02-26 14:15:57 -05002503 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002504 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002505
2506 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002507 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002508 "2842 Replace in-use FCF (x%x) "
2509 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002510 phba->fcf.current_rec.fcf_indx,
2511 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002512 memcpy(&phba->fcf.current_rec,
2513 &phba->fcf.failover_rec,
2514 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002515 /*
2516 * Mark the fast FCF failover rediscovery completed
2517 * and the start of the first round of the roundrobin
2518 * FCF failover.
2519 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002520 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002521 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002522 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002523 /* Register to the new FCF record */
2524 lpfc_register_fcf(phba);
2525 } else {
2526 /*
2527 * In case of transaction period to fast FCF failover,
2528 * do nothing when search to the end of the FCF table.
2529 */
2530 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2531 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2532 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002533
James Smart7d791df2011-07-22 18:37:52 -04002534 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2535 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002536 /*
2537 * In case the current in-use FCF record no
2538 * longer existed during FCF discovery that
2539 * was not triggered by fast FCF failover
2540 * process, treat it as fast FCF failover.
2541 */
2542 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2543 "2841 In-use FCF record (x%x) "
2544 "not reported, entering fast "
2545 "FCF failover mode scanning.\n",
2546 phba->fcf.current_rec.fcf_indx);
2547 spin_lock_irq(&phba->hbalock);
2548 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2549 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002550 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2551 LPFC_FCOE_FCF_GET_FIRST);
2552 return;
2553 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002554 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002555 lpfc_register_fcf(phba);
2556 }
2557 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002558 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002559 return;
2560
2561out:
2562 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2563 lpfc_register_fcf(phba);
2564
2565 return;
2566}
2567
2568/**
James Smarta93ff372010-10-22 11:06:08 -04002569 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002570 * @phba: pointer to lpfc hba data structure.
2571 * @mboxq: pointer to mailbox object.
2572 *
James Smarta93ff372010-10-22 11:06:08 -04002573 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002574 * read FCF record mailbox command from the eligible FCF record bmask for
2575 * performing the failover. If the FCF read back is not valid/available, it
2576 * fails through to retrying FLOGI to the currently registered FCF again.
2577 * Otherwise, if the FCF read back is valid and available, it will set the
2578 * newly read FCF record to the failover FCF record, unregister currently
2579 * registered FCF record, copy the failover FCF record to the current
2580 * FCF record, and then register the current FCF record before proceeding
2581 * to trying FLOGI on the new failover FCF.
2582 */
2583void
2584lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2585{
2586 struct fcf_record *new_fcf_record;
2587 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002588 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002589 uint16_t current_fcf_index;
2590 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002591 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002592
James Smarta93ff372010-10-22 11:06:08 -04002593 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002594 if (phba->link_state < LPFC_LINK_UP) {
2595 spin_lock_irq(&phba->hbalock);
2596 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002597 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002598 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002599 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002600 }
2601
2602 /* Parse the FCF record from the non-embedded mailbox command */
2603 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2604 &next_fcf_index);
2605 if (!new_fcf_record) {
2606 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2607 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002608 "failed to retrieve a FCF record. "
2609 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2610 phba->fcf.fcf_flag);
2611 lpfc_unregister_fcf_rescan(phba);
2612 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 }
2614
2615 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002616 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2617 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002618
2619 /* Log the FCF record information if turned on */
2620 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2621 next_fcf_index);
2622
James Smarta93ff372010-10-22 11:06:08 -04002623 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2624 if (!rc) {
2625 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2626 "2848 Remove ineligible FCF (x%x) from "
2627 "from roundrobin bmask\n", fcf_index);
2628 /* Clear roundrobin bmask bit for ineligible FCF */
2629 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2630 /* Perform next round of roundrobin FCF failover */
2631 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2632 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2633 if (rc)
2634 goto out;
2635 goto error_out;
2636 }
2637
2638 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2639 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2640 "2760 Perform FLOGI roundrobin FCF failover: "
2641 "FCF (x%x) back to FCF (x%x)\n",
2642 phba->fcf.current_rec.fcf_indx, fcf_index);
2643 /* Wait 500 ms before retrying FLOGI to current FCF */
2644 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002645 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002646 goto out;
2647 }
2648
James Smart0c9ab6f2010-02-26 14:15:57 -05002649 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002650 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002651 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2652 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002653 spin_lock_irq(&phba->hbalock);
2654 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2655 new_fcf_record, addr_mode, vlan_id,
2656 (boot_flag ? BOOT_ENABLE : 0));
2657 spin_unlock_irq(&phba->hbalock);
2658
2659 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2660
2661 /* Unregister the current in-use FCF record */
2662 lpfc_unregister_fcf(phba);
2663
2664 /* Replace in-use record with the new record */
2665 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2666 sizeof(struct lpfc_fcf_rec));
2667
2668 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002669 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2670 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002671
James Smarta93ff372010-10-22 11:06:08 -04002672error_out:
2673 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002674out:
2675 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002676}
2677
2678/**
2679 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2680 * @phba: pointer to lpfc hba data structure.
2681 * @mboxq: pointer to mailbox object.
2682 *
2683 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002684 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002685 * failover when a new FCF event happened. If the FCF read back is
2686 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002687 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002688 */
2689void
2690lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2691{
2692 struct fcf_record *new_fcf_record;
2693 uint32_t boot_flag, addr_mode;
2694 uint16_t fcf_index, next_fcf_index;
2695 uint16_t vlan_id;
2696 int rc;
2697
2698 /* If link state is not up, no need to proceed */
2699 if (phba->link_state < LPFC_LINK_UP)
2700 goto out;
2701
2702 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002703 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002704 goto out;
2705
2706 /* Parse the FCF record from the non-embedded mailbox command */
2707 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2708 &next_fcf_index);
2709 if (!new_fcf_record) {
2710 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2711 "2767 Mailbox command READ_FCF_RECORD "
2712 "failed to retrieve a FCF record.\n");
2713 goto out;
2714 }
2715
2716 /* Check the connection list for eligibility */
2717 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2718 &addr_mode, &vlan_id);
2719
2720 /* Log the FCF record information if turned on */
2721 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2722 next_fcf_index);
2723
2724 if (!rc)
2725 goto out;
2726
2727 /* Update the eligible FCF record index bmask */
2728 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002729
2730 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002731
2732out:
2733 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2734}
2735
2736/**
James Smart76a95d72010-11-20 23:11:48 -05002737 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2738 * @phba: pointer to lpfc hba data structure.
2739 * @mboxq: pointer to mailbox data structure.
2740 *
2741 * This function handles completion of init vfi mailbox command.
2742 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002743static void
James Smart76a95d72010-11-20 23:11:48 -05002744lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2745{
2746 struct lpfc_vport *vport = mboxq->vport;
2747
James Smartdf9e1b52011-12-13 13:22:17 -05002748 /*
2749 * VFI not supported on interface type 0, just do the flogi
2750 * Also continue if the VFI is in use - just use the same one.
2751 */
2752 if (mboxq->u.mb.mbxStatus &&
2753 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2754 LPFC_SLI_INTF_IF_TYPE_0) &&
2755 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002756 lpfc_printf_vlog(vport, KERN_ERR,
2757 LOG_MBOX,
2758 "2891 Init VFI mailbox failed 0x%x\n",
2759 mboxq->u.mb.mbxStatus);
2760 mempool_free(mboxq, phba->mbox_mem_pool);
2761 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2762 return;
2763 }
James Smart73d91e52011-10-10 21:32:10 -04002764
James Smart76a95d72010-11-20 23:11:48 -05002765 lpfc_initial_flogi(vport);
2766 mempool_free(mboxq, phba->mbox_mem_pool);
2767 return;
2768}
2769
2770/**
2771 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2772 * @vport: pointer to lpfc_vport data structure.
2773 *
2774 * This function issue a init_vfi mailbox command to initialize the VFI and
2775 * VPI for the physical port.
2776 */
2777void
2778lpfc_issue_init_vfi(struct lpfc_vport *vport)
2779{
2780 LPFC_MBOXQ_t *mboxq;
2781 int rc;
2782 struct lpfc_hba *phba = vport->phba;
2783
2784 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2785 if (!mboxq) {
2786 lpfc_printf_vlog(vport, KERN_ERR,
2787 LOG_MBOX, "2892 Failed to allocate "
2788 "init_vfi mailbox\n");
2789 return;
2790 }
2791 lpfc_init_vfi(mboxq, vport);
2792 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2793 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2794 if (rc == MBX_NOT_FINISHED) {
2795 lpfc_printf_vlog(vport, KERN_ERR,
2796 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2797 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2798 }
2799}
2800
2801/**
James Smart1c6834a2009-07-19 10:01:26 -04002802 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2803 * @phba: pointer to lpfc hba data structure.
2804 * @mboxq: pointer to mailbox data structure.
2805 *
2806 * This function handles completion of init vpi mailbox command.
2807 */
James Smart695a8142010-01-26 23:08:03 -05002808void
James Smart1c6834a2009-07-19 10:01:26 -04002809lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2810{
2811 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002812 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002813 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2814
James Smart1c6834a2009-07-19 10:01:26 -04002815 if (mboxq->u.mb.mbxStatus) {
2816 lpfc_printf_vlog(vport, KERN_ERR,
2817 LOG_MBOX,
2818 "2609 Init VPI mailbox failed 0x%x\n",
2819 mboxq->u.mb.mbxStatus);
2820 mempool_free(mboxq, phba->mbox_mem_pool);
2821 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2822 return;
2823 }
James Smart72100cc2010-02-12 14:43:01 -05002824 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002825 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002826 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002827
James Smart695a8142010-01-26 23:08:03 -05002828 /* If this port is physical port or FDISC is done, do reg_vpi */
2829 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2830 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2831 if (!ndlp)
2832 lpfc_printf_vlog(vport, KERN_ERR,
2833 LOG_DISCOVERY,
2834 "2731 Cannot find fabric "
2835 "controller node\n");
2836 else
2837 lpfc_register_new_vport(phba, vport, ndlp);
2838 mempool_free(mboxq, phba->mbox_mem_pool);
2839 return;
2840 }
2841
James Smart1c6834a2009-07-19 10:01:26 -04002842 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2843 lpfc_initial_fdisc(vport);
2844 else {
2845 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002846 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2847 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002848 }
James Smart695a8142010-01-26 23:08:03 -05002849 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002850 return;
2851}
2852
2853/**
James Smartecfd03c2010-02-12 14:41:27 -05002854 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2855 * @vport: pointer to lpfc_vport data structure.
2856 *
2857 * This function issue a init_vpi mailbox command to initialize
2858 * VPI for the vport.
2859 */
2860void
2861lpfc_issue_init_vpi(struct lpfc_vport *vport)
2862{
2863 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002864 int rc, vpi;
2865
2866 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2867 vpi = lpfc_alloc_vpi(vport->phba);
2868 if (!vpi) {
2869 lpfc_printf_vlog(vport, KERN_ERR,
2870 LOG_MBOX,
2871 "3303 Failed to obtain vport vpi\n");
2872 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2873 return;
2874 }
2875 vport->vpi = vpi;
2876 }
James Smartecfd03c2010-02-12 14:41:27 -05002877
2878 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2879 if (!mboxq) {
2880 lpfc_printf_vlog(vport, KERN_ERR,
2881 LOG_MBOX, "2607 Failed to allocate "
2882 "init_vpi mailbox\n");
2883 return;
2884 }
2885 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2886 mboxq->vport = vport;
2887 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2888 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2889 if (rc == MBX_NOT_FINISHED) {
2890 lpfc_printf_vlog(vport, KERN_ERR,
2891 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2892 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2893 }
2894}
2895
2896/**
James Smart6fb120a2009-05-22 14:52:59 -04002897 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2898 * @phba: pointer to lpfc hba data structure.
2899 *
2900 * This function loops through the list of vports on the @phba and issues an
2901 * FDISC if possible.
2902 */
2903void
2904lpfc_start_fdiscs(struct lpfc_hba *phba)
2905{
2906 struct lpfc_vport **vports;
2907 int i;
2908
2909 vports = lpfc_create_vport_work_array(phba);
2910 if (vports != NULL) {
2911 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2912 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2913 continue;
2914 /* There are no vpi for this vport */
2915 if (vports[i]->vpi > phba->max_vpi) {
2916 lpfc_vport_set_state(vports[i],
2917 FC_VPORT_FAILED);
2918 continue;
2919 }
James Smart76a95d72010-11-20 23:11:48 -05002920 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002921 lpfc_vport_set_state(vports[i],
2922 FC_VPORT_LINKDOWN);
2923 continue;
2924 }
James Smart1c6834a2009-07-19 10:01:26 -04002925 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002926 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002927 continue;
2928 }
James Smart6fb120a2009-05-22 14:52:59 -04002929 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2930 lpfc_initial_fdisc(vports[i]);
2931 else {
2932 lpfc_vport_set_state(vports[i],
2933 FC_VPORT_NO_FABRIC_SUPP);
2934 lpfc_printf_vlog(vports[i], KERN_ERR,
2935 LOG_ELS,
2936 "0259 No NPIV "
2937 "Fabric support\n");
2938 }
2939 }
2940 }
2941 lpfc_destroy_vport_work_array(phba, vports);
2942}
2943
2944void
2945lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2946{
James Smart3e1f0712018-11-29 16:09:29 -08002947 struct lpfc_dmabuf *dmabuf = mboxq->ctx_buf;
James Smart6fb120a2009-05-22 14:52:59 -04002948 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002949 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002950
James Smartf5eca9b2012-03-01 22:37:54 -05002951 /*
2952 * VFI not supported for interface type 0, so ignore any mailbox
2953 * error (except VFI in use) and continue with the discovery.
2954 */
2955 if (mboxq->u.mb.mbxStatus &&
2956 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2957 LPFC_SLI_INTF_IF_TYPE_0) &&
2958 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002959 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2960 "2018 REG_VFI mbxStatus error x%x "
2961 "HBA state x%x\n",
2962 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002963 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002964 /* FLOGI failed, use loop map to make discovery list */
2965 lpfc_disc_list_loopmap(vport);
2966 /* Start discovery */
2967 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002968 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002969 }
2970 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002971 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002972 }
James Smartae05ebe2013-03-01 16:35:38 -05002973
James Smarte74c03c2013-04-17 20:15:19 -04002974 /* If the VFI is already registered, there is nothing else to do
2975 * Unless this was a VFI update and we are in PT2PT mode, then
2976 * we should drop through to set the port state to ready.
2977 */
James Smartae05ebe2013-03-01 16:35:38 -05002978 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002979 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2980 vport->fc_flag & FC_PT2PT))
2981 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002982
James Smartc8685952009-11-18 15:39:16 -05002983 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002984 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002985 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002986 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002987 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002988 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002989 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002990
James Smart1b511972011-12-13 13:23:09 -05002991 /* In case SLI4 FC loopback test, we are ready */
2992 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2993 (phba->link_flag & LS_LOOPBACK_MODE)) {
2994 phba->link_state = LPFC_HBA_READY;
2995 goto out_free_mem;
2996 }
2997
James Smarte74c03c2013-04-17 20:15:19 -04002998 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2999 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
3000 "alpacnt:%d LinkState:%x topology:%x\n",
3001 vport->port_state, vport->fc_flag, vport->fc_myDID,
3002 vport->phba->alpa_map[0],
3003 phba->link_state, phba->fc_topology);
3004
James Smart6fb120a2009-05-22 14:52:59 -04003005 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04003006 /*
3007 * For private loop or for NPort pt2pt,
3008 * just start discovery and we are done.
3009 */
3010 if ((vport->fc_flag & FC_PT2PT) ||
3011 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
3012 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
3013
James Smart76a95d72010-11-20 23:11:48 -05003014 /* Use loop map to make discovery list */
3015 lpfc_disc_list_loopmap(vport);
3016 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04003017 if (vport->fc_flag & FC_PT2PT)
3018 vport->port_state = LPFC_VPORT_READY;
3019 else
3020 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05003021 } else {
3022 lpfc_start_fdiscs(phba);
3023 lpfc_do_scr_ns_plogi(phba, vport);
3024 }
James Smart6fb120a2009-05-22 14:52:59 -04003025 }
3026
James Smart1b511972011-12-13 13:23:09 -05003027out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04003028 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05003029 if (dmabuf) {
3030 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3031 kfree(dmabuf);
3032 }
James Smart6fb120a2009-05-22 14:52:59 -04003033 return;
3034}
3035
3036static void
James Smart2e0fef82007-06-17 19:56:36 -05003037lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003038{
James Smart6fb120a2009-05-22 14:52:59 -04003039 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003040 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart2e0fef82007-06-17 19:56:36 -05003041 struct lpfc_vport *vport = pmb->vport;
James Smartaeb3c812017-04-21 16:05:02 -07003042 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart21bf0b92015-08-31 16:48:21 -04003043 struct serv_parm *sp = &vport->fc_sparam;
3044 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003045
3046 /* Check for error */
3047 if (mb->mbxStatus) {
3048 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003049 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3050 "0319 READ_SPARAM mbxStatus error x%x "
3051 "hba state x%x>\n",
3052 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003053 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003054 goto out;
3055 }
3056
James Smart2e0fef82007-06-17 19:56:36 -05003057 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003058 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003059
3060 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3061 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3062 ed_tov = (ed_tov + 999999) / 1000000;
3063
3064 phba->fc_edtov = ed_tov;
3065 phba->fc_ratov = (2 * ed_tov) / 1000;
3066 if (phba->fc_ratov < FF_DEF_RATOV) {
3067 /* RA_TOV should be atleast 10sec for initial flogi */
3068 phba->fc_ratov = FF_DEF_RATOV;
3069 }
3070
James Smart05580562011-05-24 11:40:48 -04003071 lpfc_update_vport_wwn(vport);
James Smartaeb3c812017-04-21 16:05:02 -07003072 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05003073 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3074 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3075 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3076 }
3077
dea31012005-04-17 16:05:31 -05003078 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3079 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003080 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003081 return;
3082
3083out:
James Smart3e1f0712018-11-29 16:09:29 -08003084 pmb->ctx_buf = NULL;
dea31012005-04-17 16:05:31 -05003085 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3086 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003087 lpfc_issue_clear_la(phba, vport);
3088 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003089 return;
3090}
3091
3092static void
James Smart76a95d72010-11-20 23:11:48 -05003093lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003094{
James Smart92d7f7b2007-06-17 19:56:38 -05003095 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003096 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003097 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003098 int i;
James Smart14691152006-12-02 13:34:28 -05003099 struct lpfc_dmabuf *mp;
3100 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003101 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003102 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003103
James Smart92d7f7b2007-06-17 19:56:38 -05003104 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003105 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3106
3107 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3108 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3109 case LPFC_LINK_SPEED_1GHZ:
3110 case LPFC_LINK_SPEED_2GHZ:
3111 case LPFC_LINK_SPEED_4GHZ:
3112 case LPFC_LINK_SPEED_8GHZ:
3113 case LPFC_LINK_SPEED_10GHZ:
3114 case LPFC_LINK_SPEED_16GHZ:
3115 case LPFC_LINK_SPEED_32GHZ:
James Smartfbd8a6b2018-02-22 08:18:45 -08003116 case LPFC_LINK_SPEED_64GHZ:
James Smart1dc5ec22018-10-23 13:41:11 -07003117 case LPFC_LINK_SPEED_128GHZ:
James Smarta085e872015-12-16 18:12:02 -05003118 break;
3119 default:
3120 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3121 break;
3122 }
dea31012005-04-17 16:05:31 -05003123 }
3124
James Smarte74c03c2013-04-17 20:15:19 -04003125 if (phba->fc_topology &&
3126 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3127 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3128 "3314 Toplogy changed was 0x%x is 0x%x\n",
3129 phba->fc_topology,
3130 bf_get(lpfc_mbx_read_top_topology, la));
3131 phba->fc_topology_changed = 1;
3132 }
3133
James Smart76a95d72010-11-20 23:11:48 -05003134 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003135 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003136
James Smartdf9e1b52011-12-13 13:22:17 -05003137 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003138 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003139 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003140
James Smart6a9c52c2009-10-02 15:16:51 -04003141 /* if npiv is enabled and this adapter supports npiv log
3142 * a message that npiv is not supported in this topology
3143 */
3144 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003145 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3146 "1309 Link Up Event npiv not supported in loop "
3147 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003148 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003149 if (bf_get(lpfc_mbx_read_top_il, la))
3150 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003151
James Smart76a95d72010-11-20 23:11:48 -05003152 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3153 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003154
3155 if (i == 0) {
3156 phba->alpa_map[0] = 0;
3157 } else {
James Smarte8b62012007-08-02 11:10:09 -04003158 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003159 int numalpa, j, k;
3160 union {
3161 uint8_t pamap[16];
3162 struct {
3163 uint32_t wd1;
3164 uint32_t wd2;
3165 uint32_t wd3;
3166 uint32_t wd4;
3167 } pa;
3168 } un;
3169 numalpa = phba->alpa_map[0];
3170 j = 0;
3171 while (j < numalpa) {
3172 memset(un.pamap, 0, 16);
3173 for (k = 1; j < numalpa; k++) {
3174 un.pamap[k - 1] =
3175 phba->alpa_map[j + 1];
3176 j++;
3177 if (k == 16)
3178 break;
3179 }
3180 /* Link Up Event ALPA map */
3181 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003182 KERN_WARNING,
3183 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003184 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003185 "ALPA map Data: x%x "
3186 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003187 un.pa.wd1, un.pa.wd2,
3188 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003189 }
3190 }
3191 }
3192 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003193 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003194 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003195 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003196 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3197 }
James Smart2e0fef82007-06-17 19:56:36 -05003198 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003199 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003200 }
James Smart92d7f7b2007-06-17 19:56:38 -05003201 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003202
James Smart38c20672013-03-01 16:37:44 -05003203 if (fc_flags) {
3204 spin_lock_irq(shost->host_lock);
3205 vport->fc_flag |= fc_flags;
3206 spin_unlock_irq(shost->host_lock);
3207 }
3208
dea31012005-04-17 16:05:31 -05003209 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003210 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3211 if (!sparam_mbox)
3212 goto out;
3213
3214 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3215 if (rc) {
3216 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3217 goto out;
3218 }
3219 sparam_mbox->vport = vport;
3220 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3221 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3222 if (rc == MBX_NOT_FINISHED) {
James Smart3e1f0712018-11-29 16:09:29 -08003223 mp = (struct lpfc_dmabuf *)sparam_mbox->ctx_buf;
James Smart9f1177a2010-02-26 14:12:57 -05003224 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3225 kfree(mp);
3226 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3227 goto out;
dea31012005-04-17 16:05:31 -05003228 }
3229
James Smart76a95d72010-11-20 23:11:48 -05003230 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003231 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3232 if (!cfglink_mbox)
3233 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003234 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003235 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003236 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003237 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003238 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003239 if (rc == MBX_NOT_FINISHED) {
3240 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3241 goto out;
3242 }
3243 } else {
James Smart32b97932009-07-19 10:01:21 -04003244 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003245 /*
3246 * Add the driver's default FCF record at FCF index 0 now. This
3247 * is phase 1 implementation that support FCF index 0 and driver
3248 * defaults.
3249 */
James Smart45ed1192009-10-02 15:17:02 -04003250 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003251 fcf_record = kzalloc(sizeof(struct fcf_record),
3252 GFP_KERNEL);
3253 if (unlikely(!fcf_record)) {
3254 lpfc_printf_log(phba, KERN_ERR,
3255 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003256 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003257 "fcf record\n");
3258 rc = -ENODEV;
3259 goto out;
3260 }
3261
3262 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3263 LPFC_FCOE_FCF_DEF_INDEX);
3264 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3265 if (unlikely(rc)) {
3266 lpfc_printf_log(phba, KERN_ERR,
3267 LOG_MBOX | LOG_SLI,
3268 "2013 Could not manually add FCF "
3269 "record 0, status %d\n", rc);
3270 rc = -ENODEV;
3271 kfree(fcf_record);
3272 goto out;
3273 }
3274 kfree(fcf_record);
3275 }
3276 /*
3277 * The driver is expected to do FIP/FCF. Call the port
3278 * and get the FCF Table.
3279 */
James Smart32b97932009-07-19 10:01:21 -04003280 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003281 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003282 spin_unlock_irq(&phba->hbalock);
3283 return;
3284 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003285 /* This is the initial FCF discovery scan */
3286 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003287 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003288 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3289 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003290 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3291 LPFC_FCOE_FCF_GET_FIRST);
3292 if (rc) {
3293 spin_lock_irq(&phba->hbalock);
3294 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3295 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003296 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003297 }
James Smart38b92ef2010-08-04 16:11:39 -04003298 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003299 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003300 }
James Smart6fb120a2009-05-22 14:52:59 -04003301
3302 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003303out:
3304 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003305 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3306 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3307 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003308 lpfc_issue_clear_la(phba, vport);
3309 return;
dea31012005-04-17 16:05:31 -05003310}
3311
3312static void
James Smart84774a42008-08-24 21:50:06 -04003313lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003314{
dea31012005-04-17 16:05:31 -05003315 uint32_t control;
3316 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003317 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003318 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003319 if (phba->sli_rev <= LPFC_SLI_REV3) {
3320 control = readl(phba->HCregaddr);
3321 control |= HC_LAINT_ENA;
3322 writel(control, phba->HCregaddr);
3323 readl(phba->HCregaddr); /* flush */
3324 }
James Smart2e0fef82007-06-17 19:56:36 -05003325 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003326}
3327
James Smart84774a42008-08-24 21:50:06 -04003328static void
3329lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3330{
3331 lpfc_linkdown(phba);
3332 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003333 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003334 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3335}
3336
3337
dea31012005-04-17 16:05:31 -05003338/*
James Smart76a95d72010-11-20 23:11:48 -05003339 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003340 * command upon completion. It is setup in the LPFC_MBOXQ
3341 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003342 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003343 */
3344void
James Smart76a95d72010-11-20 23:11:48 -05003345lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003346{
James Smart2e0fef82007-06-17 19:56:36 -05003347 struct lpfc_vport *vport = pmb->vport;
3348 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003349 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003350 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003351 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003352 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smartaeb3c812017-04-21 16:05:02 -07003353 uint8_t attn_type;
dea31012005-04-17 16:05:31 -05003354
James Smart0d2b6b82008-06-14 22:52:47 -04003355 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003356 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -07003357 if (pring)
3358 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart895427b2017-02-12 13:52:30 -08003359
dea31012005-04-17 16:05:31 -05003360 /* Check for error */
3361 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003362 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003363 "1307 READ_LA mbox error x%x state x%x\n",
3364 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003365 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003366 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003367 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003368 }
3369
James Smart76a95d72010-11-20 23:11:48 -05003370 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
James Smartaeb3c812017-04-21 16:05:02 -07003371 attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
dea31012005-04-17 16:05:31 -05003372
3373 memcpy(&phba->alpa_map[0], mp->virt, 128);
3374
James Smart2e0fef82007-06-17 19:56:36 -05003375 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003376 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003377 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003378 else
James Smart2e0fef82007-06-17 19:56:36 -05003379 vport->fc_flag &= ~FC_BYPASSED_MODE;
3380 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003381
James Smartbe0c0082013-03-01 16:38:14 -05003382 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003383 phba->fc_stat.LinkMultiEvent++;
James Smartaeb3c812017-04-21 16:05:02 -07003384 if (attn_type == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003385 if (phba->fc_eventTag != 0)
3386 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003387 }
dea31012005-04-17 16:05:31 -05003388
3389 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003390 if (phba->sli_rev < LPFC_SLI_REV4) {
3391 spin_lock_irq(&phba->hbalock);
3392 if (bf_get(lpfc_mbx_read_top_mm, la))
3393 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3394 else
3395 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3396 spin_unlock_irq(&phba->hbalock);
3397 }
dea31012005-04-17 16:05:31 -05003398
James Smart4d9ab992009-10-02 15:16:39 -04003399 phba->link_events++;
James Smartaeb3c812017-04-21 16:05:02 -07003400 if ((attn_type == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003401 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003402 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003403 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003404 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003405 "1306 Link Up Event in loop back mode "
3406 "x%x received Data: x%x x%x x%x x%x\n",
3407 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003408 bf_get(lpfc_mbx_read_top_alpa_granted,
3409 la),
3410 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003411 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003412 } else {
3413 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003414 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003415 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003416 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003417 bf_get(lpfc_mbx_read_top_alpa_granted,
3418 la),
3419 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003420 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003421 bf_get(lpfc_mbx_read_top_mm, la),
3422 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003423 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003424 }
James Smart92d7f7b2007-06-17 19:56:38 -05003425 lpfc_mbx_process_link_up(phba, la);
James Smartaeb3c812017-04-21 16:05:02 -07003426 } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3427 attn_type == LPFC_ATT_UNEXP_WWPN) {
dea31012005-04-17 16:05:31 -05003428 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003429 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003430 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3431 "1308 Link Down Event in loop back mode "
3432 "x%x received "
3433 "Data: x%x x%x x%x\n",
3434 la->eventTag, phba->fc_eventTag,
3435 phba->pport->port_state, vport->fc_flag);
James Smartaeb3c812017-04-21 16:05:02 -07003436 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3437 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3438 "1313 Link Down UNEXP WWPN Event x%x received "
3439 "Data: x%x x%x x%x x%x x%x\n",
3440 la->eventTag, phba->fc_eventTag,
3441 phba->pport->port_state, vport->fc_flag,
3442 bf_get(lpfc_mbx_read_top_mm, la),
3443 bf_get(lpfc_mbx_read_top_fa, la));
James Smart1b511972011-12-13 13:23:09 -05003444 else
James Smart3163f722008-02-08 18:50:25 -05003445 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003446 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003447 "Data: x%x x%x x%x x%x x%x\n",
3448 la->eventTag, phba->fc_eventTag,
3449 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003450 bf_get(lpfc_mbx_read_top_mm, la),
3451 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003452 lpfc_mbx_issue_link_down(phba);
3453 }
James Smartaeb3c812017-04-21 16:05:02 -07003454 if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3455 attn_type == LPFC_ATT_LINK_UP) {
James Smart84774a42008-08-24 21:50:06 -04003456 if (phba->link_state != LPFC_LINK_DOWN) {
3457 phba->fc_stat.LinkDown++;
3458 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3459 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003460 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003461 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003462 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003463 lpfc_mbx_issue_link_down(phba);
3464 } else
3465 lpfc_enable_la(phba);
3466
3467 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3468 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3469 "Data: x%x x%x x%x\n",
3470 la->eventTag, phba->fc_eventTag,
3471 phba->pport->port_state, vport->fc_flag);
3472 /*
3473 * The cmnd that triggered this will be waiting for this
3474 * signal.
3475 */
3476 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3477 if (phba->wait_4_mlo_maint_flg) {
3478 phba->wait_4_mlo_maint_flg = 0;
3479 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003480 }
James Smart84774a42008-08-24 21:50:06 -04003481 }
3482
James Smartbe0c0082013-03-01 16:38:14 -05003483 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3484 bf_get(lpfc_mbx_read_top_fa, la)) {
3485 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003486 lpfc_issue_clear_la(phba, vport);
3487 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003488 "1311 fa %d\n",
3489 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003490 }
3491
James Smart76a95d72010-11-20 23:11:48 -05003492lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003493 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3494 kfree(mp);
3495 mempool_free(pmb, phba->mbox_mem_pool);
3496 return;
3497}
3498
3499/*
3500 * This routine handles processing a REG_LOGIN mailbox
3501 * command upon completion. It is setup in the LPFC_MBOXQ
3502 * as the completion routine when the command is
3503 * handed off to the SLI layer.
3504 */
3505void
James Smart2e0fef82007-06-17 19:56:36 -05003506lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003507{
James Smart2e0fef82007-06-17 19:56:36 -05003508 struct lpfc_vport *vport = pmb->vport;
James Smart3e1f0712018-11-29 16:09:29 -08003509 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3510 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smartffc95492010-06-07 15:23:17 -04003511 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003512
James Smart3e1f0712018-11-29 16:09:29 -08003513 pmb->ctx_buf = NULL;
3514 pmb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05003515
James Smartbe6bb942015-04-07 15:07:22 -04003516 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3517 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3518 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003519 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003520 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003521 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3522 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3523
James Smart40426292010-12-15 17:58:10 -05003524 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3525 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003526 /* We rcvd a rscn after issuing this
3527 * mbox reg login, we may have cycled
3528 * back through the state and be
3529 * back at reg login state so this
3530 * mbox needs to be ignored becase
3531 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003532 * process.
James Smartffc95492010-06-07 15:23:17 -04003533 */
3534 spin_lock_irq(shost->host_lock);
3535 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3536 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003537
3538 /*
3539 * We cannot leave the RPI registered because
3540 * if we go thru discovery again for this ndlp
3541 * a subsequent REG_RPI will fail.
3542 */
3543 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3544 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003545 }
3546
3547 /* Call state machine */
3548 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003549
dea31012005-04-17 16:05:31 -05003550 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3551 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003552 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003553 /* decrement the node reference count held for this callback
3554 * function.
3555 */
James Smart329f9bc2007-04-25 09:53:01 -04003556 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003557
3558 return;
3559}
3560
James Smart92d7f7b2007-06-17 19:56:38 -05003561static void
3562lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3563{
James Smart04c68492009-05-22 14:52:52 -04003564 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003565 struct lpfc_vport *vport = pmb->vport;
3566 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3567
3568 switch (mb->mbxStatus) {
3569 case 0x0011:
3570 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003571 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3572 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3573 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003574 break;
James Smart78730cf2010-04-06 15:06:30 -04003575 /* If VPI is busy, reset the HBA */
3576 case 0x9700:
3577 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3578 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3579 vport->vpi, mb->mbxStatus);
3580 if (!(phba->pport->load_flag & FC_UNLOADING))
3581 lpfc_workq_post_event(phba, NULL, NULL,
3582 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003583 }
James Smart72100cc2010-02-12 14:43:01 -05003584 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003585 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003586 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003587 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003588 vport->unreg_vpi_cmpl = VPORT_OK;
3589 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003590 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003591 /*
3592 * This shost reference might have been taken at the beginning of
3593 * lpfc_vport_delete()
3594 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003595 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003596 scsi_host_put(shost);
3597}
3598
James Smartd7c255b2008-08-24 21:50:00 -04003599int
James Smart92d7f7b2007-06-17 19:56:38 -05003600lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3601{
3602 struct lpfc_hba *phba = vport->phba;
3603 LPFC_MBOXQ_t *mbox;
3604 int rc;
3605
3606 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3607 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003608 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003609
3610 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3611 mbox->vport = vport;
3612 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003613 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003614 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003615 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3616 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003617 mempool_free(mbox, phba->mbox_mem_pool);
3618 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003619 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003620 }
James Smartd7c255b2008-08-24 21:50:00 -04003621 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003622}
3623
3624static void
3625lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3626{
3627 struct lpfc_vport *vport = pmb->vport;
3628 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003629 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003630
3631 switch (mb->mbxStatus) {
3632 case 0x0011:
3633 case 0x9601:
3634 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003635 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3636 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3637 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003638 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3639 spin_lock_irq(shost->host_lock);
3640 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3641 spin_unlock_irq(shost->host_lock);
3642 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003643
James Smart01649562017-02-12 13:52:32 -08003644 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003645 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003646 if (phba->nvmet_support)
3647 lpfc_nvmet_update_targetport(phba);
3648 else
James Smart8c258642017-02-12 13:52:36 -08003649 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003650 }
James Smart92d7f7b2007-06-17 19:56:38 -05003651 goto out;
3652 }
James Smart92d7f7b2007-06-17 19:56:38 -05003653
James Smart72100cc2010-02-12 14:43:01 -05003654 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003655 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003656 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003657 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003658 vport->num_disc_nodes = 0;
3659 /* go thru NPR list and issue ELS PLOGIs */
3660 if (vport->fc_npr_cnt)
3661 lpfc_els_disc_plogi(vport);
3662
3663 if (!vport->num_disc_nodes) {
3664 spin_lock_irq(shost->host_lock);
3665 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3666 spin_unlock_irq(shost->host_lock);
3667 lpfc_can_disctmo(vport);
3668 }
3669 vport->port_state = LPFC_VPORT_READY;
3670
3671out:
3672 mempool_free(pmb, phba->mbox_mem_pool);
3673 return;
3674}
3675
James Smart21e9a0a2009-05-22 14:53:21 -04003676/**
3677 * lpfc_create_static_vport - Read HBA config region to create static vports.
3678 * @phba: pointer to lpfc hba data structure.
3679 *
3680 * This routine issue a DUMP mailbox command for config region 22 to get
3681 * the list of static vports to be created. The function create vports
3682 * based on the information returned from the HBA.
3683 **/
3684void
3685lpfc_create_static_vport(struct lpfc_hba *phba)
3686{
3687 LPFC_MBOXQ_t *pmb = NULL;
3688 MAILBOX_t *mb;
3689 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003690 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003691 struct fc_vport_identifiers vport_id;
3692 struct fc_vport *new_fc_vport;
3693 struct Scsi_Host *shost;
3694 struct lpfc_vport *vport;
3695 uint16_t offset = 0;
3696 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003697 struct lpfc_dmabuf *mp;
3698 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003699
3700 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3701 if (!pmb) {
3702 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3703 "0542 lpfc_create_static_vport failed to"
3704 " allocate mailbox memory\n");
3705 return;
3706 }
James Smartcdcc2342012-08-14 14:26:35 -04003707 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003708 mb = &pmb->u.mb;
3709
3710 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3711 if (!vport_info) {
3712 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3713 "0543 lpfc_create_static_vport failed to"
3714 " allocate vport_info\n");
3715 mempool_free(pmb, phba->mbox_mem_pool);
3716 return;
3717 }
3718
3719 vport_buff = (uint8_t *) vport_info;
3720 do {
James Smartcdcc2342012-08-14 14:26:35 -04003721 /* free dma buffer from previous round */
James Smart3e1f0712018-11-29 16:09:29 -08003722 if (pmb->ctx_buf) {
3723 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smartcdcc2342012-08-14 14:26:35 -04003724 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3725 kfree(mp);
3726 }
James Smart1c6834a2009-07-19 10:01:26 -04003727 if (lpfc_dump_static_vport(phba, pmb, offset))
3728 goto out;
3729
James Smart21e9a0a2009-05-22 14:53:21 -04003730 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003731 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3732 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003733
James Smartcdcc2342012-08-14 14:26:35 -04003734 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003735 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3736 "0544 lpfc_create_static_vport failed to"
3737 " issue dump mailbox command ret 0x%x "
3738 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003739 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003740 goto out;
3741 }
3742
James Smart1c6834a2009-07-19 10:01:26 -04003743 if (phba->sli_rev == LPFC_SLI_REV4) {
3744 byte_count = pmb->u.mqe.un.mb_words[5];
James Smart3e1f0712018-11-29 16:09:29 -08003745 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart1c6834a2009-07-19 10:01:26 -04003746 if (byte_count > sizeof(struct static_vport_info) -
3747 offset)
3748 byte_count = sizeof(struct static_vport_info)
3749 - offset;
3750 memcpy(vport_buff + offset, mp->virt, byte_count);
3751 offset += byte_count;
3752 } else {
3753 if (mb->un.varDmp.word_cnt >
3754 sizeof(struct static_vport_info) - offset)
3755 mb->un.varDmp.word_cnt =
3756 sizeof(struct static_vport_info)
3757 - offset;
3758 byte_count = mb->un.varDmp.word_cnt;
3759 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3760 vport_buff + offset,
3761 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003762
James Smart1c6834a2009-07-19 10:01:26 -04003763 offset += byte_count;
3764 }
James Smart21e9a0a2009-05-22 14:53:21 -04003765
James Smart1c6834a2009-07-19 10:01:26 -04003766 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003767 offset < sizeof(struct static_vport_info));
3768
3769
3770 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3771 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3772 != VPORT_INFO_REV)) {
3773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3774 "0545 lpfc_create_static_vport bad"
3775 " information header 0x%x 0x%x\n",
3776 le32_to_cpu(vport_info->signature),
3777 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3778
3779 goto out;
3780 }
3781
3782 shost = lpfc_shost_from_vport(phba->pport);
3783
3784 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3785 memset(&vport_id, 0, sizeof(vport_id));
3786 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3787 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3788 if (!vport_id.port_name || !vport_id.node_name)
3789 continue;
3790
3791 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3792 vport_id.vport_type = FC_PORTTYPE_NPIV;
3793 vport_id.disable = false;
3794 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3795
3796 if (!new_fc_vport) {
3797 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3798 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003799 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003800 continue;
3801 }
3802
3803 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3804 vport->vport_flag |= STATIC_VPORT;
3805 }
3806
3807out:
James Smart21e9a0a2009-05-22 14:53:21 -04003808 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003809 if (mbx_wait_rc != MBX_TIMEOUT) {
James Smart3e1f0712018-11-29 16:09:29 -08003810 if (pmb->ctx_buf) {
3811 mp = (struct lpfc_dmabuf *)pmb->ctx_buf;
James Smart1c6834a2009-07-19 10:01:26 -04003812 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3813 kfree(mp);
3814 }
James Smart21e9a0a2009-05-22 14:53:21 -04003815 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003816 }
James Smart21e9a0a2009-05-22 14:53:21 -04003817
3818 return;
3819}
3820
dea31012005-04-17 16:05:31 -05003821/*
3822 * This routine handles processing a Fabric REG_LOGIN mailbox
3823 * command upon completion. It is setup in the LPFC_MBOXQ
3824 * as the completion routine when the command is
3825 * handed off to the SLI layer.
3826 */
3827void
James Smart2e0fef82007-06-17 19:56:36 -05003828lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003829{
James Smart92d7f7b2007-06-17 19:56:38 -05003830 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003831 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003832 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
James Smart92d7f7b2007-06-17 19:56:38 -05003833 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003834 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003835
James Smart3e1f0712018-11-29 16:09:29 -08003836 ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
3837 pmb->ctx_ndlp = NULL;
3838 pmb->ctx_buf = NULL;
James Smartd439d282010-09-29 11:18:45 -04003839
dea31012005-04-17 16:05:31 -05003840 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003841 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3842 "0258 Register Fabric login error: 0x%x\n",
3843 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003844 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3845 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003846 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003847
James Smart76a95d72010-11-20 23:11:48 -05003848 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003849 /* FLOGI failed, use loop map to make discovery list */
3850 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003851
James Smart92d7f7b2007-06-17 19:56:38 -05003852 /* Start discovery */
3853 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003854 /* Decrement the reference count to ndlp after the
3855 * reference to the ndlp are done.
3856 */
3857 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003858 return;
3859 }
3860
3861 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003862 /* Decrement the reference count to ndlp after the reference
3863 * to the ndlp are done.
3864 */
3865 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003866 return;
3867 }
3868
James Smart6d368e52011-05-24 11:44:12 -04003869 if (phba->sli_rev < LPFC_SLI_REV4)
3870 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003871 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003872 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003873 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003874
James Smart2e0fef82007-06-17 19:56:36 -05003875 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003876 /* when physical port receive logo donot start
3877 * vport discovery */
3878 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3879 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003880 else {
3881 shost = lpfc_shost_from_vport(vport);
3882 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003883 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003884 spin_unlock_irq(shost->host_lock);
3885 }
James Smart92d7f7b2007-06-17 19:56:38 -05003886 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003887 }
3888
3889 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3890 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003891 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003892
3893 /* Drop the reference count from the mbox at the end after
3894 * all the current reference to the ndlp have been done.
3895 */
3896 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003897 return;
3898}
3899
James Smarta0f2d3e2017-02-12 13:52:31 -08003900 /*
3901 * This routine will issue a GID_FT for each FC4 Type supported
3902 * by the driver. ALL GID_FTs must complete before discovery is started.
3903 */
3904int
3905lpfc_issue_gidft(struct lpfc_vport *vport)
3906{
3907 struct lpfc_hba *phba = vport->phba;
3908
3909 /* Good status, issue CT Request to NameServer */
3910 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3911 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3912 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3913 /* Cannot issue NameServer FCP Query, so finish up
3914 * discovery
3915 */
3916 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3917 "0604 %s FC TYPE %x %s\n",
3918 "Failed to issue GID_FT to ",
3919 FC_TYPE_FCP,
3920 "Finishing discovery.");
3921 return 0;
3922 }
3923 vport->gidft_inp++;
3924 }
3925
3926 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3927 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3928 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3929 /* Cannot issue NameServer NVME Query, so finish up
3930 * discovery
3931 */
3932 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3933 "0605 %s FC_TYPE %x %s %d\n",
3934 "Failed to issue GID_FT to ",
3935 FC_TYPE_NVME,
3936 "Finishing discovery: gidftinp ",
3937 vport->gidft_inp);
3938 if (vport->gidft_inp == 0)
3939 return 0;
3940 } else
3941 vport->gidft_inp++;
3942 }
3943 return vport->gidft_inp;
3944}
3945
James Smart7ea92eb2018-10-23 13:41:10 -07003946/**
3947 * lpfc_issue_gidpt - issue a GID_PT for all N_Ports
3948 * @vport: The virtual port for which this call is being executed.
3949 *
3950 * This routine will issue a GID_PT to get a list of all N_Ports
3951 *
3952 * Return value :
3953 * 0 - Failure to issue a GID_PT
3954 * 1 - GID_PT issued
3955 **/
3956int
3957lpfc_issue_gidpt(struct lpfc_vport *vport)
3958{
3959 /* Good status, issue CT Request to NameServer */
3960 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_PT, 0, GID_PT_N_PORT)) {
3961 /* Cannot issue NameServer FCP Query, so finish up
3962 * discovery
3963 */
3964 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3965 "0606 %s Port TYPE %x %s\n",
3966 "Failed to issue GID_PT to ",
3967 GID_PT_N_PORT,
3968 "Finishing discovery.");
3969 return 0;
3970 }
3971 vport->gidft_inp++;
3972 return 1;
3973}
3974
dea31012005-04-17 16:05:31 -05003975/*
3976 * This routine handles processing a NameServer REG_LOGIN mailbox
3977 * command upon completion. It is setup in the LPFC_MBOXQ
3978 * as the completion routine when the command is
3979 * handed off to the SLI layer.
3980 */
3981void
James Smart2e0fef82007-06-17 19:56:36 -05003982lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003983{
James Smart21e9a0a2009-05-22 14:53:21 -04003984 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08003985 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
3986 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05003987 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003988
James Smart3e1f0712018-11-29 16:09:29 -08003989 pmb->ctx_buf = NULL;
3990 pmb->ctx_ndlp = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003991 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003992
dea31012005-04-17 16:05:31 -05003993 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003994 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3995 "0260 Register NameServer error: 0x%x\n",
3996 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003997
3998out:
James Smartfa4066b2008-01-11 01:53:27 -05003999 /* decrement the node reference count held for this
4000 * callback function.
4001 */
James Smart329f9bc2007-04-25 09:53:01 -04004002 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004003 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4004 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04004005 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04004006
4007 /* If no other thread is using the ndlp, free it */
4008 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05004009
James Smart76a95d72010-11-20 23:11:48 -05004010 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05004011 /*
4012 * RegLogin failed, use loop map to make discovery
4013 * list
4014 */
4015 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05004016
James Smart92d7f7b2007-06-17 19:56:38 -05004017 /* Start discovery */
4018 lpfc_disc_start(vport);
4019 return;
4020 }
4021 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05004022 return;
4023 }
4024
James Smart6d368e52011-05-24 11:44:12 -04004025 if (phba->sli_rev < LPFC_SLI_REV4)
4026 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05004027 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05004028 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05004029 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004030 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4031 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
4032 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004033 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004034 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05004035
James Smart2e0fef82007-06-17 19:56:36 -05004036 if (vport->port_state < LPFC_VPORT_READY) {
4037 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05004038 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
4039 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
4040 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
4041 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08004042
4043 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4044 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
4045 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
4046
4047 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4048 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08004049 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
4050 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05004051
4052 /* Issue SCR just before NameServer GID_FT Query */
4053 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05004054 }
4055
James Smart2e0fef82007-06-17 19:56:36 -05004056 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08004057 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05004058 goto out;
dea31012005-04-17 16:05:31 -05004059
James Smarta0f2d3e2017-02-12 13:52:31 -08004060 /*
4061 * At this point in time we may need to wait for multiple
4062 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4063 *
4064 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05004065 * callback function.
4066 */
James Smart329f9bc2007-04-25 09:53:01 -04004067 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004068 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4069 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05004070 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004071
4072 return;
4073}
4074
4075static void
James Smart2e0fef82007-06-17 19:56:36 -05004076lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004077{
James Smart2e0fef82007-06-17 19:56:36 -05004078 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4079 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05004080 struct lpfc_rport_data *rdata;
4081 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05004082 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004083
James Smart895427b2017-02-12 13:52:30 -08004084 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4085 return;
4086
dea31012005-04-17 16:05:31 -05004087 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004088 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4089 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004090 rport_ids.port_id = ndlp->nlp_DID;
4091 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004092
James Smart329f9bc2007-04-25 09:53:01 -04004093 /*
4094 * We leave our node pointer in rport->dd_data when we unregister a
4095 * FCP target port. But fc_remote_port_add zeros the space to which
4096 * rport->dd_data points. So, if we're reusing a previously
4097 * registered port, drop the reference that we took the last time we
4098 * registered the port.
4099 */
James Smart466e8402015-05-21 13:55:28 -04004100 rport = ndlp->rport;
4101 if (rport) {
4102 rdata = rport->dd_data;
4103 /* break the link before dropping the ref */
4104 ndlp->rport = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08004105 if (rdata) {
4106 if (rdata->pnode == ndlp)
4107 lpfc_nlp_put(ndlp);
4108 rdata->pnode = NULL;
4109 }
James Smart466e8402015-05-21 13:55:28 -04004110 /* drop reference for earlier registeration */
4111 put_device(&rport->dev);
4112 }
James Smart858c9f62007-06-17 19:56:39 -05004113
4114 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4115 "rport add: did:x%x flg:x%x type x%x",
4116 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4117
James Smart9589b0622011-04-16 11:03:17 -04004118 /* Don't add the remote port if unloading. */
4119 if (vport->load_flag & FC_UNLOADING)
4120 return;
4121
James Smart2e0fef82007-06-17 19:56:36 -05004122 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004123 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004124 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4125 "Warning: fc_remote_port_add failed\n");
4126 return;
4127 }
4128
4129 /* initialize static port data */
4130 rport->maxframe_size = ndlp->nlp_maxframe;
4131 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004132 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004133 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004134
4135 if (ndlp->nlp_type & NLP_FCP_TARGET)
4136 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4137 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4138 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4139
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004140 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4141 fc_remote_port_rolechg(rport, rport_ids.roles);
4142
James Smart34f5ad82012-08-03 12:35:03 -04004143 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4144 "3183 rport register x%06x, rport %p role x%x\n",
4145 ndlp->nlp_DID, rport, rport_ids.roles);
4146
James Smart071fbd3d2006-04-15 11:53:20 -04004147 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004148 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004149 ndlp->nlp_sid = rport->scsi_target_id;
4150 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004151 return;
4152}
4153
4154static void
James Smart2e0fef82007-06-17 19:56:36 -05004155lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004156{
4157 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004158 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004159 struct lpfc_hba *phba = vport->phba;
4160
4161 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4162 return;
James Smartc01f3202006-08-18 17:47:08 -04004163
James Smart2ea259e2017-02-12 13:52:27 -08004164 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004165 "rport delete: did:x%x flg:x%x type x%x",
4166 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4167
James Smart2ea259e2017-02-12 13:52:27 -08004168 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004169 "3184 rport unregister x%06x, rport %p\n",
4170 ndlp->nlp_DID, rport);
4171
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004172 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004173
4174 return;
4175}
4176
James Smartde0c5b32007-04-25 09:52:27 -04004177static void
James Smart2e0fef82007-06-17 19:56:36 -05004178lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004179{
James Smart2e0fef82007-06-17 19:56:36 -05004180 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4181
4182 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004183 switch (state) {
4184 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004185 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004186 break;
4187 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004188 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004189 break;
4190 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004191 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004192 break;
4193 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004194 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004195 break;
4196 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004197 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004198 break;
4199 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004200 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004201 break;
4202 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004203 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004204 break;
4205 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004206 if (vport->fc_npr_cnt == 0 && count == -1)
4207 vport->fc_npr_cnt = 0;
4208 else
4209 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004210 break;
4211 }
James Smart2e0fef82007-06-17 19:56:36 -05004212 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004213}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004214
James Smartde0c5b32007-04-25 09:52:27 -04004215static void
James Smart2e0fef82007-06-17 19:56:36 -05004216lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004217 int old_state, int new_state)
4218{
James Smart2e0fef82007-06-17 19:56:36 -05004219 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4220
James Smartde0c5b32007-04-25 09:52:27 -04004221 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004222 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4223 ndlp->nlp_type |= NLP_FC_NODE;
4224 }
4225 if (new_state == NLP_STE_MAPPED_NODE)
4226 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4227 if (new_state == NLP_STE_NPR_NODE)
4228 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004229
James Smarta0f2d3e2017-02-12 13:52:31 -08004230 /* FCP and NVME Transport interface */
4231 if ((old_state == NLP_STE_MAPPED_NODE ||
4232 old_state == NLP_STE_UNMAPPED_NODE)) {
4233 if (ndlp->rport) {
4234 vport->phba->nport_event_cnt++;
4235 lpfc_unregister_remote_port(ndlp);
4236 }
4237
James Smart1c5b12f2017-04-21 16:05:03 -07004238 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004239 vport->phba->nport_event_cnt++;
James Smart3b5bde692017-11-20 16:00:39 -08004240 if (vport->phba->nvmet_support == 0) {
4241 /* Start devloss if target. */
4242 if (ndlp->nlp_type & NLP_NVME_TARGET)
4243 lpfc_nvme_unregister_port(vport, ndlp);
4244 } else {
James Smart6599e122017-06-01 21:07:00 -07004245 /* NVMET has no upcall. */
4246 lpfc_nlp_put(ndlp);
James Smart3b5bde692017-11-20 16:00:39 -08004247 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004248 }
James Smartde0c5b32007-04-25 09:52:27 -04004249 }
dea31012005-04-17 16:05:31 -05004250
James Smarta0f2d3e2017-02-12 13:52:31 -08004251 /* FCP and NVME Transport interfaces */
4252
James Smartde0c5b32007-04-25 09:52:27 -04004253 if (new_state == NLP_STE_MAPPED_NODE ||
4254 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart01a8aed2018-09-10 10:30:41 -07004255 if (ndlp->nlp_fc4_type ||
James Smart92721c32017-06-01 21:06:59 -07004256 ndlp->nlp_DID == Fabric_DID ||
4257 ndlp->nlp_DID == NameServer_DID ||
4258 ndlp->nlp_DID == FDMI_DID) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004259 vport->phba->nport_event_cnt++;
4260 /*
4261 * Tell the fc transport about the port, if we haven't
4262 * already. If we have, and it's a scsi entity, be
4263 */
4264 lpfc_register_remote_port(vport, ndlp);
4265 }
4266 /* Notify the NVME transport of this new rport. */
James Smart09559e82017-06-15 22:56:46 -07004267 if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4268 ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004269 if (vport->phba->nvmet_support == 0) {
4270 /* Register this rport with the transport.
James Smart3b5bde692017-11-20 16:00:39 -08004271 * Only NVME Target Rports are registered with
4272 * the transport.
James Smarta0f2d3e2017-02-12 13:52:31 -08004273 */
James Smart3b5bde692017-11-20 16:00:39 -08004274 if (ndlp->nlp_type & NLP_NVME_TARGET) {
4275 vport->phba->nport_event_cnt++;
4276 lpfc_nvme_register_port(vport, ndlp);
4277 }
James Smart8c258642017-02-12 13:52:36 -08004278 } else {
4279 /* Just take an NDLP ref count since the
4280 * target does not register rports.
4281 */
4282 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004283 }
4284 }
James Smartde0c5b32007-04-25 09:52:27 -04004285 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004286
James Smartea2151b2008-09-07 11:52:10 -04004287 if ((new_state == NLP_STE_MAPPED_NODE) &&
4288 (vport->stat_data_enabled)) {
4289 /*
4290 * A new target is discovered, if there is no buffer for
4291 * statistical data collection allocate buffer.
4292 */
4293 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4294 sizeof(struct lpfc_scsicmd_bkt),
4295 GFP_KERNEL);
4296
4297 if (!ndlp->lat_data)
4298 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4299 "0286 lpfc_nlp_state_cleanup failed to "
4300 "allocate statistical data buffer DID "
4301 "0x%x\n", ndlp->nlp_DID);
4302 }
James Smart858c9f62007-06-17 19:56:39 -05004303 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004304 * If the node just added to Mapped list was an FCP target,
4305 * but the remote port registration failed or assigned a target
4306 * id outside the presentable range - move the node to the
4307 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004308 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004309 if ((new_state == NLP_STE_MAPPED_NODE) &&
4310 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004311 (!ndlp->rport ||
4312 ndlp->rport->scsi_target_id == -1 ||
4313 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004314 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004315 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004316 spin_unlock_irq(shost->host_lock);
4317 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004318 }
James Smartde0c5b32007-04-25 09:52:27 -04004319}
4320
James Smart685f0bf2007-04-25 09:53:08 -04004321static char *
4322lpfc_nlp_state_name(char *buffer, size_t size, int state)
4323{
4324 static char *states[] = {
4325 [NLP_STE_UNUSED_NODE] = "UNUSED",
4326 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4327 [NLP_STE_ADISC_ISSUE] = "ADISC",
4328 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4329 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004330 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004331 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4332 [NLP_STE_MAPPED_NODE] = "MAPPED",
4333 [NLP_STE_NPR_NODE] = "NPR",
4334 };
4335
James Smart311464e2007-08-02 11:10:37 -04004336 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004337 strlcpy(buffer, states[state], size);
4338 else
4339 snprintf(buffer, size, "unknown (%d)", state);
4340 return buffer;
4341}
4342
James Smartde0c5b32007-04-25 09:52:27 -04004343void
James Smart2e0fef82007-06-17 19:56:36 -05004344lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4345 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004346{
James Smart2e0fef82007-06-17 19:56:36 -05004347 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004348 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004349 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004350
James Smarte8b62012007-08-02 11:10:09 -04004351 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4352 "0904 NPort state transition x%06x, %s -> %s\n",
4353 ndlp->nlp_DID,
4354 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4355 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004356
4357 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4358 "node statechg did:x%x old:%d ste:%d",
4359 ndlp->nlp_DID, old_state, state);
4360
James Smartde0c5b32007-04-25 09:52:27 -04004361 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004362 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004363 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004364 if (old_state == NLP_STE_UNMAPPED_NODE) {
4365 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4366 ndlp->nlp_type &= ~NLP_FC_NODE;
4367 }
4368
James Smart685f0bf2007-04-25 09:53:08 -04004369 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004370 spin_lock_irq(shost->host_lock);
4371 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4372 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004373 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004374 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004375
4376 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004377 lpfc_nlp_counters(vport, state, 1);
4378 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004379}
4380
4381void
James Smarte47c9092008-02-08 18:49:26 -05004382lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4383{
4384 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4385
4386 if (list_empty(&ndlp->nlp_listp)) {
4387 spin_lock_irq(shost->host_lock);
4388 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4389 spin_unlock_irq(shost->host_lock);
4390 }
4391}
4392
4393void
James Smart2e0fef82007-06-17 19:56:36 -05004394lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004395{
James Smart2e0fef82007-06-17 19:56:36 -05004396 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4397
James Smart0d2b6b82008-06-14 22:52:47 -04004398 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004399 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004400 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4401 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004402 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004403 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004404 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004405 NLP_STE_UNUSED_NODE);
4406}
4407
Adrian Bunk4d9db012008-02-14 23:24:02 +02004408static void
James Smarte47c9092008-02-08 18:49:26 -05004409lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4410{
James Smart0d2b6b82008-06-14 22:52:47 -04004411 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004412 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4413 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4414 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4415 NLP_STE_UNUSED_NODE);
4416}
James Smart109f6ed2008-12-04 22:39:08 -05004417/**
James Smart3621a712009-04-06 18:47:14 -04004418 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004419 * @vport: Pointer to Virtual Port object.
4420 * @ndlp: Pointer to FC node object.
4421 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004422 *
4423 * This function is always called when node object need to be initialized.
4424 * It initializes all the fields of the node object. Although the reference
4425 * to phba from @ndlp can be obtained indirectly through it's reference to
4426 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4427 * to the life-span of the @ndlp might go beyond the existence of @vport as
4428 * the final release of ndlp is determined by its reference count. And, the
4429 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004430 **/
4431static inline void
4432lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4433 uint32_t did)
4434{
4435 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4436 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
Kees Cookf22eb4d2017-09-06 20:24:26 -07004437 timer_setup(&ndlp->nlp_delayfunc, lpfc_els_retry_delay, 0);
James Smart109f6ed2008-12-04 22:39:08 -05004438 ndlp->nlp_DID = did;
4439 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004440 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004441 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004442 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004443 kref_init(&ndlp->kref);
4444 NLP_INT_NODE_ACT(ndlp);
4445 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004446 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smartdea16bd2018-11-29 16:09:30 -08004447 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
James Smart109f6ed2008-12-04 22:39:08 -05004448}
James Smarte47c9092008-02-08 18:49:26 -05004449
4450struct lpfc_nodelist *
4451lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4452 int state)
4453{
4454 struct lpfc_hba *phba = vport->phba;
James Smartdea16bd2018-11-29 16:09:30 -08004455 uint32_t did, flag;
James Smarte47c9092008-02-08 18:49:26 -05004456 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004457 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smart9d3d3402017-04-21 16:05:00 -07004458 int rpi = LPFC_RPI_ALLOC_ERROR;
James Smartdea16bd2018-11-29 16:09:30 -08004459 uint32_t defer_did = 0;
James Smarte47c9092008-02-08 18:49:26 -05004460
4461 if (!ndlp)
4462 return NULL;
4463
James Smart9d3d3402017-04-21 16:05:00 -07004464 if (phba->sli_rev == LPFC_SLI_REV4) {
4465 rpi = lpfc_sli4_alloc_rpi(vport->phba);
4466 if (rpi == LPFC_RPI_ALLOC_ERROR)
4467 return NULL;
4468 }
4469
James Smarte47c9092008-02-08 18:49:26 -05004470 spin_lock_irqsave(&phba->ndlp_lock, flags);
4471 /* The ndlp should not be in memory free mode */
4472 if (NLP_CHK_FREE_REQ(ndlp)) {
4473 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4474 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4475 "0277 lpfc_enable_node: ndlp:x%p "
4476 "usgmap:x%x refcnt:%d\n",
4477 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004478 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004479 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004480 }
4481 /* The ndlp should not already be in active mode */
4482 if (NLP_CHK_NODE_ACT(ndlp)) {
4483 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4484 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4485 "0278 lpfc_enable_node: ndlp:x%p "
4486 "usgmap:x%x refcnt:%d\n",
4487 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004488 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004489 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004490 }
4491
James Smartdea16bd2018-11-29 16:09:30 -08004492 /* First preserve the orginal DID, xri_bitmap and some flags */
James Smarte47c9092008-02-08 18:49:26 -05004493 did = ndlp->nlp_DID;
James Smartdea16bd2018-11-29 16:09:30 -08004494 flag = (ndlp->nlp_flag & NLP_UNREG_INP);
4495 if (flag & NLP_UNREG_INP)
4496 defer_did = ndlp->nlp_defer_did;
James Smartcff261f2013-12-17 20:29:47 -05004497 if (phba->sli_rev == LPFC_SLI_REV4)
4498 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004499
James Smartdea16bd2018-11-29 16:09:30 -08004500 /* Zero ndlp except of ndlp linked list pointer */
James Smarte47c9092008-02-08 18:49:26 -05004501 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4502 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smarte47c9092008-02-08 18:49:26 -05004503
James Smartdea16bd2018-11-29 16:09:30 -08004504 /* Next reinitialize and restore saved objects */
4505 lpfc_initialize_node(vport, ndlp, did);
4506 ndlp->nlp_flag |= flag;
4507 if (flag & NLP_UNREG_INP)
4508 ndlp->nlp_defer_did = defer_did;
James Smartcff261f2013-12-17 20:29:47 -05004509 if (phba->sli_rev == LPFC_SLI_REV4)
4510 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4511
James Smarte47c9092008-02-08 18:49:26 -05004512 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004513 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07004514 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04004515 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4516 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4517 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4518 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004519 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004520 ndlp->nlp_usg_map, ndlp);
4521 }
James Smart725dd392013-10-10 12:23:30 -04004522
James Smarte47c9092008-02-08 18:49:26 -05004523
4524 if (state != NLP_STE_UNUSED_NODE)
4525 lpfc_nlp_set_state(vport, ndlp, state);
4526
4527 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4528 "node enable: did:x%x",
4529 ndlp->nlp_DID, 0, 0);
4530 return ndlp;
James Smart9d3d3402017-04-21 16:05:00 -07004531
4532free_rpi:
4533 if (phba->sli_rev == LPFC_SLI_REV4)
4534 lpfc_sli4_free_rpi(vport->phba, rpi);
4535 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04004536}
4537
4538void
James Smart2e0fef82007-06-17 19:56:36 -05004539lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004540{
James Smart87af33f2007-10-27 13:37:43 -04004541 /*
James Smartfa4066b2008-01-11 01:53:27 -05004542 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004543 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004544 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4545 * until ALL other outstanding threads have completed. We check
4546 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004547 */
James Smartfa4066b2008-01-11 01:53:27 -05004548 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4549 return;
James Smart51ef4c22007-08-02 11:10:31 -04004550 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004551 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004552 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004553 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004554 }
James Smart02902172015-05-21 13:55:17 -04004555
4556 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004557 return;
dea31012005-04-17 16:05:31 -05004558}
4559
4560/*
4561 * Start / ReStart rescue timer for Discovery / RSCN handling
4562 */
4563void
James Smart2e0fef82007-06-17 19:56:36 -05004564lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004565{
James Smart2e0fef82007-06-17 19:56:36 -05004566 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4567 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004568 uint32_t tmo;
4569
James Smart2e0fef82007-06-17 19:56:36 -05004570 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004571 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004572 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4573 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004574 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004575 * FC spec states we need 3 * ratov for CT requests
4576 */
4577 tmo = ((phba->fc_ratov * 3) + 3);
4578 }
dea31012005-04-17 16:05:31 -05004579
James Smart858c9f62007-06-17 19:56:39 -05004580
4581 if (!timer_pending(&vport->fc_disctmo)) {
4582 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4583 "set disc timer: tmo:x%x state:x%x flg:x%x",
4584 tmo, vport->port_state, vport->fc_flag);
4585 }
4586
James Smart256ec0d2013-04-17 20:14:58 -04004587 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004588 spin_lock_irq(shost->host_lock);
4589 vport->fc_flag |= FC_DISC_TMO;
4590 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004591
4592 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004593 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4594 "0247 Start Discovery Timer state x%x "
4595 "Data: x%x x%lx x%x x%x\n",
4596 vport->port_state, tmo,
4597 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4598 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004599
4600 return;
4601}
4602
4603/*
4604 * Cancel rescue timer for Discovery / RSCN handling
4605 */
4606int
James Smart2e0fef82007-06-17 19:56:36 -05004607lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004608{
James Smart2e0fef82007-06-17 19:56:36 -05004609 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004610 unsigned long iflags;
4611
James Smart858c9f62007-06-17 19:56:39 -05004612 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4613 "can disc timer: state:x%x rtry:x%x flg:x%x",
4614 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4615
dea31012005-04-17 16:05:31 -05004616 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004617 if (vport->fc_flag & FC_DISC_TMO) {
4618 spin_lock_irqsave(shost->host_lock, iflags);
4619 vport->fc_flag &= ~FC_DISC_TMO;
4620 spin_unlock_irqrestore(shost->host_lock, iflags);
4621 del_timer_sync(&vport->fc_disctmo);
4622 spin_lock_irqsave(&vport->work_port_lock, iflags);
4623 vport->work_port_events &= ~WORKER_DISC_TMO;
4624 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004625 }
4626
4627 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004628 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4629 "0248 Cancel Discovery Timer state x%x "
4630 "Data: x%x x%x x%x\n",
4631 vport->port_state, vport->fc_flag,
4632 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004633 return 0;
dea31012005-04-17 16:05:31 -05004634}
4635
4636/*
4637 * Check specified ring for outstanding IOCB on the SLI queue
4638 * Return true if iocb matches the specified nport
4639 */
4640int
James Smart2e0fef82007-06-17 19:56:36 -05004641lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4642 struct lpfc_sli_ring *pring,
4643 struct lpfc_iocbq *iocb,
4644 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004645{
James Smart2e0fef82007-06-17 19:56:36 -05004646 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004647 struct lpfc_vport *vport = ndlp->vport;
4648
4649 if (iocb->vport != vport)
4650 return 0;
4651
dea31012005-04-17 16:05:31 -05004652 if (pring->ringno == LPFC_ELS_RING) {
4653 switch (icmd->ulpCommand) {
4654 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004655 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004656 return 1;
dea31012005-04-17 16:05:31 -05004657 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004658 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4659 return 1;
dea31012005-04-17 16:05:31 -05004660 case CMD_XMIT_ELS_RSP64_CX:
4661 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004662 return 1;
dea31012005-04-17 16:05:31 -05004663 }
James Smart895427b2017-02-12 13:52:30 -08004664 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004665 /* Skip match check if waiting to relogin to FCP target */
4666 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004667 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004668 return 0;
dea31012005-04-17 16:05:31 -05004669 }
4670 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004671 return 1;
dea31012005-04-17 16:05:31 -05004672 }
dea31012005-04-17 16:05:31 -05004673 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004674 return 0;
dea31012005-04-17 16:05:31 -05004675}
4676
James Smart895427b2017-02-12 13:52:30 -08004677static void
4678__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4679 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4680 struct list_head *dequeue_list)
4681{
4682 struct lpfc_iocbq *iocb, *next_iocb;
4683
4684 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4685 /* Check to see if iocb matches the nport */
4686 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4687 /* match, dequeue */
4688 list_move_tail(&iocb->list, dequeue_list);
4689 }
4690}
4691
4692static void
4693lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4694 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4695{
4696 struct lpfc_sli *psli = &phba->sli;
4697 uint32_t i;
4698
4699 spin_lock_irq(&phba->hbalock);
4700 for (i = 0; i < psli->num_rings; i++)
4701 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4702 dequeue_list);
4703 spin_unlock_irq(&phba->hbalock);
4704}
4705
4706static void
4707lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4708 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4709{
4710 struct lpfc_sli_ring *pring;
4711 struct lpfc_queue *qp = NULL;
4712
4713 spin_lock_irq(&phba->hbalock);
4714 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4715 pring = qp->pring;
4716 if (!pring)
4717 continue;
James Smart2ade92a2017-03-04 09:30:38 -08004718 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004719 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
James Smart2ade92a2017-03-04 09:30:38 -08004720 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004721 }
4722 spin_unlock_irq(&phba->hbalock);
4723}
4724
dea31012005-04-17 16:05:31 -05004725/*
4726 * Free resources / clean up outstanding I/Os
4727 * associated with nlp_rpi in the LPFC_NODELIST entry.
4728 */
4729static int
James Smart2e0fef82007-06-17 19:56:36 -05004730lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004731{
James Smart2534ba72007-04-25 09:52:20 -04004732 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004733
James Smart92d7f7b2007-06-17 19:56:38 -05004734 lpfc_fabric_abort_nport(ndlp);
4735
dea31012005-04-17 16:05:31 -05004736 /*
4737 * Everything that matches on txcmplq will be returned
4738 * by firmware with a no rpi error.
4739 */
James Smart40426292010-12-15 17:58:10 -05004740 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004741 if (phba->sli_rev != LPFC_SLI_REV4)
4742 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4743 else
4744 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004745 }
James Smart2534ba72007-04-25 09:52:20 -04004746
James Smarta257bf92009-04-06 18:48:10 -04004747 /* Cancel all the IOCBs from the completions list */
4748 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4749 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004750
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004751 return 0;
dea31012005-04-17 16:05:31 -05004752}
4753
James Smart086a3452012-08-14 14:25:21 -04004754/**
4755 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4756 * @phba: Pointer to HBA context object.
4757 * @pmb: Pointer to mailbox object.
4758 *
4759 * This function will issue an ELS LOGO command after completing
4760 * the UNREG_RPI.
4761 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004762static void
James Smart086a3452012-08-14 14:25:21 -04004763lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4764{
4765 struct lpfc_vport *vport = pmb->vport;
4766 struct lpfc_nodelist *ndlp;
4767
James Smart3e1f0712018-11-29 16:09:29 -08004768 ndlp = (struct lpfc_nodelist *)(pmb->ctx_ndlp);
James Smart086a3452012-08-14 14:25:21 -04004769 if (!ndlp)
4770 return;
4771 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004772 mempool_free(pmb, phba->mbox_mem_pool);
James Smartdea16bd2018-11-29 16:09:30 -08004773
4774 /* Check to see if there are any deferred events to process */
4775 if ((ndlp->nlp_flag & NLP_UNREG_INP) &&
4776 (ndlp->nlp_defer_did != NLP_EVT_NOTHING_PENDING)) {
4777 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4778 "1434 UNREG cmpl deferred logo x%x "
4779 "on NPort x%x Data: x%x %p\n",
4780 ndlp->nlp_rpi, ndlp->nlp_DID,
4781 ndlp->nlp_defer_did, ndlp);
4782
4783 ndlp->nlp_flag &= ~NLP_UNREG_INP;
4784 ndlp->nlp_defer_did = NLP_EVT_NOTHING_PENDING;
4785 lpfc_issue_els_plogi(vport, ndlp->nlp_DID, 0);
4786 }
James Smart086a3452012-08-14 14:25:21 -04004787}
4788
dea31012005-04-17 16:05:31 -05004789/*
4790 * Free rpi associated with LPFC_NODELIST entry.
4791 * This routine is called from lpfc_freenode(), when we are removing
4792 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4793 * LOGO that completes successfully, and we are waiting to PLOGI back
4794 * to the remote NPort. In addition, it is called after we receive
4795 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4796 * we are waiting to PLOGI back to the remote NPort.
4797 */
4798int
James Smart2e0fef82007-06-17 19:56:36 -05004799lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004800{
James Smart2e0fef82007-06-17 19:56:36 -05004801 struct lpfc_hba *phba = vport->phba;
4802 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004803 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004804 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004805
James Smartf454a9a2013-09-06 12:20:07 -04004806 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4807 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4808 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4809 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4810 "3366 RPI x%x needs to be "
4811 "unregistered nlp_flag x%x "
4812 "did x%x\n",
4813 ndlp->nlp_rpi, ndlp->nlp_flag,
4814 ndlp->nlp_DID);
James Smartdea16bd2018-11-29 16:09:30 -08004815
4816 /* If there is already an UNREG in progress for this ndlp,
4817 * no need to queue up another one.
4818 */
4819 if (ndlp->nlp_flag & NLP_UNREG_INP) {
4820 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4821 "1436 unreg_rpi SKIP UNREG x%x on "
4822 "NPort x%x deferred x%x flg x%x "
4823 "Data: %p\n",
4824 ndlp->nlp_rpi, ndlp->nlp_DID,
4825 ndlp->nlp_defer_did,
4826 ndlp->nlp_flag, ndlp);
4827 goto out;
4828 }
4829
James Smart2e0fef82007-06-17 19:56:36 -05004830 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4831 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004832 /* SLI4 ports require the physical rpi value. */
4833 rpi = ndlp->nlp_rpi;
4834 if (phba->sli_rev == LPFC_SLI_REV4)
4835 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004836
James Smart6d368e52011-05-24 11:44:12 -04004837 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004838 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004839 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
James Smart3e1f0712018-11-29 16:09:29 -08004840 mbox->ctx_ndlp = ndlp;
James Smart086a3452012-08-14 14:25:21 -04004841 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4842 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004843 if (phba->sli_rev == LPFC_SLI_REV4 &&
4844 (!(vport->load_flag & FC_UNLOADING)) &&
4845 (bf_get(lpfc_sli_intf_if_type,
4846 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004847 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004848 (kref_read(&ndlp->kref) > 0)) {
James Smart3e1f0712018-11-29 16:09:29 -08004849 mbox->ctx_ndlp = lpfc_nlp_get(ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004850 mbox->mbox_cmpl =
4851 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004852 /*
4853 * accept PLOGIs after unreg_rpi_cmpl
4854 */
4855 acc_plogi = 0;
James Smartdea16bd2018-11-29 16:09:30 -08004856 } else {
4857 mbox->ctx_ndlp = ndlp;
James Smartbe6bb942015-04-07 15:07:22 -04004858 mbox->mbox_cmpl =
4859 lpfc_sli_def_mbox_cmpl;
James Smartdea16bd2018-11-29 16:09:30 -08004860 }
James Smart086a3452012-08-14 14:25:21 -04004861 }
James Smartdea16bd2018-11-29 16:09:30 -08004862 if (((ndlp->nlp_DID & Fabric_DID_MASK) !=
4863 Fabric_DID_MASK) &&
4864 (!(vport->fc_flag & FC_OFFLINE_MODE)))
4865 ndlp->nlp_flag |= NLP_UNREG_INP;
4866
4867 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4868 "1433 unreg_rpi UNREG x%x on "
4869 "NPort x%x deferred flg x%x Data:%p\n",
4870 ndlp->nlp_rpi, ndlp->nlp_DID,
4871 ndlp->nlp_flag, ndlp);
James Smart086a3452012-08-14 14:25:21 -04004872
James Smart0b727fe2007-10-27 13:37:25 -04004873 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004874 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004875 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004876 acc_plogi = 1;
4877 }
dea31012005-04-17 16:05:31 -05004878 }
dea31012005-04-17 16:05:31 -05004879 lpfc_no_rpi(phba, ndlp);
James Smartdea16bd2018-11-29 16:09:30 -08004880out:
James Smart40426292010-12-15 17:58:10 -05004881 if (phba->sli_rev != LPFC_SLI_REV4)
4882 ndlp->nlp_rpi = 0;
4883 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004884 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004885 if (acc_plogi)
4886 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004887 return 1;
4888 }
James Smart7c5e5182015-05-22 10:42:43 -04004889 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004890 return 0;
4891}
4892
James Smartecfd03c2010-02-12 14:41:27 -05004893/**
4894 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4895 * @phba: pointer to lpfc hba data structure.
4896 *
4897 * This routine is invoked to unregister all the currently registered RPIs
4898 * to the HBA.
4899 **/
4900void
4901lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4902{
4903 struct lpfc_vport **vports;
4904 struct lpfc_nodelist *ndlp;
4905 struct Scsi_Host *shost;
4906 int i;
4907
4908 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004909 if (!vports) {
4910 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4911 "2884 Vport array allocation failed \n");
4912 return;
4913 }
James Smartecfd03c2010-02-12 14:41:27 -05004914 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4915 shost = lpfc_shost_from_vport(vports[i]);
4916 spin_lock_irq(shost->host_lock);
4917 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004918 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004919 /* The mempool_alloc might sleep */
4920 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004921 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004922 spin_lock_irq(shost->host_lock);
4923 }
James Smartecfd03c2010-02-12 14:41:27 -05004924 }
4925 spin_unlock_irq(shost->host_lock);
4926 }
4927 lpfc_destroy_vport_work_array(phba, vports);
4928}
4929
James Smart92d7f7b2007-06-17 19:56:38 -05004930void
4931lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4932{
4933 struct lpfc_hba *phba = vport->phba;
4934 LPFC_MBOXQ_t *mbox;
4935 int rc;
4936
James Smart5af5eee2010-10-22 11:06:38 -04004937 if (phba->sli_rev == LPFC_SLI_REV4) {
4938 lpfc_sli4_unreg_all_rpis(vport);
4939 return;
4940 }
4941
James Smart92d7f7b2007-06-17 19:56:38 -05004942 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4943 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004944 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4945 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004946 mbox->vport = vport;
4947 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -08004948 mbox->ctx_ndlp = NULL;
James Smart09372822008-01-11 01:52:54 -05004949 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004950 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004951 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004952
4953 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4954 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4955 "1836 Could not issue "
4956 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004957 }
4958}
4959
4960void
4961lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4962{
4963 struct lpfc_hba *phba = vport->phba;
4964 LPFC_MBOXQ_t *mbox;
4965 int rc;
4966
4967 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4968 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004969 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4970 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004971 mbox->vport = vport;
4972 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart3e1f0712018-11-29 16:09:29 -08004973 mbox->ctx_ndlp = NULL;
James Smart09372822008-01-11 01:52:54 -05004974 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004975 if (rc != MBX_TIMEOUT)
4976 mempool_free(mbox, phba->mbox_mem_pool);
4977
4978 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004979 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4980 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004981 "unreg_did (default rpis) status %d\n",
4982 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004983 }
4984}
4985
dea31012005-04-17 16:05:31 -05004986/*
4987 * Free resources associated with LPFC_NODELIST entry
4988 * so it can be freed.
4989 */
4990static int
James Smart2e0fef82007-06-17 19:56:36 -05004991lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004992{
James Smart2e0fef82007-06-17 19:56:36 -05004993 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4994 struct lpfc_hba *phba = vport->phba;
4995 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004996 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004997
4998 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004999 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5000 "0900 Cleanup node for NPort x%x "
5001 "Data: x%x x%x x%x\n",
5002 ndlp->nlp_DID, ndlp->nlp_flag,
5003 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005004 if (NLP_CHK_FREE_REQ(ndlp)) {
5005 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5006 "0280 lpfc_cleanup_node: ndlp:x%p "
5007 "usgmap:x%x refcnt:%d\n",
5008 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005009 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005010 lpfc_dequeue_node(vport, ndlp);
5011 } else {
5012 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5013 "0281 lpfc_cleanup_node: ndlp:x%p "
5014 "usgmap:x%x refcnt:%d\n",
5015 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005016 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005017 lpfc_disable_node(vport, ndlp);
5018 }
dea31012005-04-17 16:05:31 -05005019
James Smart086a3452012-08-14 14:25:21 -04005020
5021 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
5022
dea31012005-04-17 16:05:31 -05005023 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
5024 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04005025 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04005026 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart3e1f0712018-11-29 16:09:29 -08005027 (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5028 mb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05005029 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5030 }
5031 }
James Smart33ccf8d2006-08-17 11:57:58 -04005032
James Smart2e0fef82007-06-17 19:56:36 -05005033 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04005034 /* Cleanup REG_LOGIN completions which are not yet processed */
5035 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
5036 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04005037 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart3e1f0712018-11-29 16:09:29 -08005038 (ndlp != (struct lpfc_nodelist *)mb->ctx_ndlp))
James Smart5ac6b302010-10-22 11:05:36 -04005039 continue;
5040
James Smart3e1f0712018-11-29 16:09:29 -08005041 mb->ctx_ndlp = NULL;
James Smart5ac6b302010-10-22 11:05:36 -04005042 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5043 }
5044
dea31012005-04-17 16:05:31 -05005045 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04005046 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04005047 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart3e1f0712018-11-29 16:09:29 -08005048 (ndlp == (struct lpfc_nodelist *)mb->ctx_ndlp)) {
5049 mp = (struct lpfc_dmabuf *)(mb->ctx_buf);
dea31012005-04-17 16:05:31 -05005050 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05005051 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05005052 kfree(mp);
5053 }
5054 list_del(&mb->list);
5055 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05005056 /* We shall not invoke the lpfc_nlp_put to decrement
5057 * the ndlp reference count as we are in the process
5058 * of lpfc_nlp_release.
5059 */
dea31012005-04-17 16:05:31 -05005060 }
5061 }
James Smart2e0fef82007-06-17 19:56:36 -05005062 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005063
James Smarte47c9092008-02-08 18:49:26 -05005064 lpfc_els_abort(phba, ndlp);
5065
James Smart2e0fef82007-06-17 19:56:36 -05005066 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04005067 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05005068 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005069
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05005070 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05005071 del_timer_sync(&ndlp->nlp_delayfunc);
5072
James Smart0d2b6b82008-06-14 22:52:47 -04005073 list_del_init(&ndlp->els_retry_evt.evt_listp);
5074 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05005075 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005076 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05005077
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005078 return 0;
dea31012005-04-17 16:05:31 -05005079}
5080
5081/*
5082 * Check to see if we can free the nlp back to the freelist.
5083 * If we are in the middle of using the nlp in the discovery state
5084 * machine, defer the free till we reach the end of the state machine.
5085 */
James Smart329f9bc2007-04-25 09:53:01 -04005086static void
James Smart2e0fef82007-06-17 19:56:36 -05005087lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005088{
James Smarta8adb832007-10-27 13:37:53 -04005089 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04005090 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04005091 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04005092 LPFC_MBOXQ_t *mbox;
5093 int rc;
dea31012005-04-17 16:05:31 -05005094
James Smart0d2b6b82008-06-14 22:52:47 -04005095 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04005096 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05005097 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
Dick Kennedye8bcf0a2017-09-29 17:34:32 -07005098 !(ndlp->nlp_flag & NLP_RPI_REGISTERED) &&
5099 phba->sli_rev != LPFC_SLI_REV4) {
James Smarta8adb832007-10-27 13:37:53 -04005100 /* For this case we need to cleanup the default rpi
5101 * allocated by the firmware.
5102 */
James Smartbe6bb942015-04-07 15:07:22 -04005103 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5104 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5105 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005106 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005107 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04005108 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
5109 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04005110 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05005111 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04005112 if (rc) {
5113 mempool_free(mbox, phba->mbox_mem_pool);
5114 }
5115 else {
5116 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5117 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5118 mbox->vport = vport;
James Smart3e1f0712018-11-29 16:09:29 -08005119 mbox->ctx_ndlp = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04005120 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5121 if (rc == MBX_NOT_FINISHED) {
5122 mempool_free(mbox, phba->mbox_mem_pool);
5123 }
5124 }
5125 }
5126 }
James Smart2e0fef82007-06-17 19:56:36 -05005127 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04005128
James Smart2e0fef82007-06-17 19:56:36 -05005129 /*
James Smart466e8402015-05-21 13:55:28 -04005130 * ndlp->rport must be set to NULL before it reaches here
5131 * i.e. break rport/node link before doing lpfc_nlp_put for
5132 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05005133 */
James Smart92d7f7b2007-06-17 19:56:38 -05005134 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04005135 /*
5136 * extra lpfc_nlp_put dropped the reference of ndlp
5137 * for registered rport so need to cleanup rport
5138 */
5139 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5140 "0940 removed node x%p DID x%x "
5141 " rport not null %p\n",
5142 ndlp, ndlp->nlp_DID, ndlp->rport);
5143 rport = ndlp->rport;
5144 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04005145 rdata->pnode = NULL;
5146 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04005147 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05005148 }
dea31012005-04-17 16:05:31 -05005149}
5150
5151static int
James Smart2e0fef82007-06-17 19:56:36 -05005152lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5153 uint32_t did)
dea31012005-04-17 16:05:31 -05005154{
James Smart2e0fef82007-06-17 19:56:36 -05005155 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005156
5157 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005158 return 0;
dea31012005-04-17 16:05:31 -05005159
dea31012005-04-17 16:05:31 -05005160 /* First check for Direct match */
5161 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005162 return 1;
dea31012005-04-17 16:05:31 -05005163
5164 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005165 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005166 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005167 return 0;
dea31012005-04-17 16:05:31 -05005168 }
5169
5170 matchdid.un.word = did;
5171 ndlpdid.un.word = ndlp->nlp_DID;
5172 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5173 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5174 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005175 /* This code is supposed to match the ID
5176 * for a private loop device that is
5177 * connect to fl_port. But we need to
5178 * check that the port did not just go
5179 * from pt2pt to fabric or we could end
5180 * up matching ndlp->nlp_DID 000001 to
5181 * fabric DID 0x20101
5182 */
dea31012005-04-17 16:05:31 -05005183 if ((ndlpdid.un.b.domain == 0) &&
5184 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005185 if (ndlpdid.un.b.id &&
5186 vport->phba->fc_topology ==
5187 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005188 return 1;
dea31012005-04-17 16:05:31 -05005189 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005190 return 0;
dea31012005-04-17 16:05:31 -05005191 }
5192
5193 matchdid.un.word = ndlp->nlp_DID;
5194 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5195 (mydid.un.b.area == ndlpdid.un.b.area)) {
5196 if ((matchdid.un.b.domain == 0) &&
5197 (matchdid.un.b.area == 0)) {
5198 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005199 return 1;
dea31012005-04-17 16:05:31 -05005200 }
5201 }
5202 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005203 return 0;
dea31012005-04-17 16:05:31 -05005204}
5205
James Smart685f0bf2007-04-25 09:53:08 -04005206/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005207static struct lpfc_nodelist *
5208__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005209{
James Smart2fb9bd82006-12-02 13:33:57 -05005210 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005211 uint32_t data1;
5212
James Smart2e0fef82007-06-17 19:56:36 -05005213 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5214 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005215 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5216 ((uint32_t) ndlp->nlp_xri << 16) |
5217 ((uint32_t) ndlp->nlp_type << 8) |
5218 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005219 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5220 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005221 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005222 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005223 ndlp->nlp_flag, data1,
5224 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005225 return ndlp;
dea31012005-04-17 16:05:31 -05005226 }
5227 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005228
dea31012005-04-17 16:05:31 -05005229 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005230 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5231 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005232 return NULL;
5233}
5234
5235struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005236lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005237{
James Smart2e0fef82007-06-17 19:56:36 -05005238 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005239 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005240 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005241
James Smartfedd3b72011-02-16 12:39:24 -05005242 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005243 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005244 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005245 return ndlp;
5246}
5247
5248struct lpfc_nodelist *
5249lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5250{
5251 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5252 struct lpfc_nodelist *ndlp;
5253
5254 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005255 if (!ndlp) {
James Smart1c5b12f2017-04-21 16:05:03 -07005256 if (vport->phba->nvmet_support)
5257 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005258 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5259 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005260 return NULL;
James Smart9d3d3402017-04-21 16:05:00 -07005261 ndlp = lpfc_nlp_init(vport, did);
dea31012005-04-17 16:05:31 -05005262 if (!ndlp)
5263 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005264 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5265 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005266 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005267 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005268 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005269 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005270 if (vport->phba->nvmet_support)
5271 return NULL;
James Smarte47c9092008-02-08 18:49:26 -05005272 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5273 if (!ndlp)
5274 return NULL;
5275 spin_lock_irq(shost->host_lock);
5276 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5277 spin_unlock_irq(shost->host_lock);
5278 return ndlp;
dea31012005-04-17 16:05:31 -05005279 }
James Smarte47c9092008-02-08 18:49:26 -05005280
James Smart1c5b12f2017-04-21 16:05:03 -07005281 /* The NVME Target does not want to actively manage an rport.
5282 * The goal is to allow the target to reset its state and clear
5283 * pending IO in preparation for the initiator to recover.
5284 */
James Smart58da1ff2008-04-07 10:15:56 -04005285 if ((vport->fc_flag & FC_RSCN_MODE) &&
5286 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005287 if (lpfc_rscn_payload_check(vport, did)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005288
5289 /* Since this node is marked for discovery,
5290 * delay timeout is not needed.
5291 */
5292 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5293
5294 /* NVME Target mode waits until rport is known to be
5295 * impacted by the RSCN before it transitions. No
5296 * active management - just go to NPR provided the
5297 * node had a valid login.
5298 */
5299 if (vport->phba->nvmet_support)
5300 return ndlp;
5301
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005302 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005303 * we don't need to try to discover it again.
5304 */
5305 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5306 return NULL;
5307
James Smarta257bf92009-04-06 18:48:10 -04005308 spin_lock_irq(shost->host_lock);
5309 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5310 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005311 } else
dea31012005-04-17 16:05:31 -05005312 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005313 } else {
James Smart1c5b12f2017-04-21 16:05:03 -07005314 /* If the initiator received a PLOGI from this NPort or if the
5315 * initiator is already in the process of discovery on it,
5316 * there's no need to try to discover it again.
James Smart87af33f2007-10-27 13:37:43 -04005317 */
James Smart685f0bf2007-04-25 09:53:08 -04005318 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005319 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
James Smart1c5b12f2017-04-21 16:05:03 -07005320 (!vport->phba->nvmet_support &&
5321 ndlp->nlp_flag & NLP_RCV_PLOGI))
dea31012005-04-17 16:05:31 -05005322 return NULL;
James Smart1c5b12f2017-04-21 16:05:03 -07005323
James Smart8c258642017-02-12 13:52:36 -08005324 if (vport->phba->nvmet_support)
5325 return ndlp;
James Smart1c5b12f2017-04-21 16:05:03 -07005326
5327 /* Moving to NPR state clears unsolicited flags and
5328 * allows for rediscovery
5329 */
5330 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5331
James Smart2e0fef82007-06-17 19:56:36 -05005332 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005333 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005334 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005335 }
5336 return ndlp;
5337}
5338
5339/* Build a list of nodes to discover based on the loopmap */
5340void
James Smart2e0fef82007-06-17 19:56:36 -05005341lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005342{
James Smart2e0fef82007-06-17 19:56:36 -05005343 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005344 int j;
5345 uint32_t alpa, index;
5346
James Smart2e0fef82007-06-17 19:56:36 -05005347 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005348 return;
James Smart2e0fef82007-06-17 19:56:36 -05005349
James Smart76a95d72010-11-20 23:11:48 -05005350 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005351 return;
dea31012005-04-17 16:05:31 -05005352
5353 /* Check for loop map present or not */
5354 if (phba->alpa_map[0]) {
5355 for (j = 1; j <= phba->alpa_map[0]; j++) {
5356 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005357 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005358 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005359 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005360 }
5361 } else {
5362 /* No alpamap, so try all alpa's */
5363 for (j = 0; j < FC_MAXLOOP; j++) {
5364 /* If cfg_scan_down is set, start from highest
5365 * ALPA (0xef) to lowest (0x1).
5366 */
James Smart3de2a652007-08-02 11:09:59 -04005367 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005368 index = j;
5369 else
5370 index = FC_MAXLOOP - j - 1;
5371 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005372 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005373 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005374 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005375 }
5376 }
5377 return;
5378}
5379
James Smart895427b2017-02-12 13:52:30 -08005380/* SLI3 only */
dea31012005-04-17 16:05:31 -05005381void
James Smart2e0fef82007-06-17 19:56:36 -05005382lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005383{
dea31012005-04-17 16:05:31 -05005384 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005385 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005386 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5387 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005388 int rc;
5389
James Smart92d7f7b2007-06-17 19:56:38 -05005390 /*
5391 * if it's not a physical port or if we already send
5392 * clear_la then don't send it.
5393 */
5394 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005395 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5396 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005397 return;
5398
James Smart2e0fef82007-06-17 19:56:36 -05005399 /* Link up discovery */
5400 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5401 phba->link_state = LPFC_CLEAR_LA;
5402 lpfc_clear_la(phba, mbox);
5403 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5404 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005405 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005406 if (rc == MBX_NOT_FINISHED) {
5407 mempool_free(mbox, phba->mbox_mem_pool);
5408 lpfc_disc_flush_list(vport);
5409 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5410 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005411 phba->link_state = LPFC_HBA_ERROR;
5412 }
5413 }
5414}
5415
5416/* Reg_vpi to tell firmware to resume normal operations */
5417void
5418lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5419{
5420 LPFC_MBOXQ_t *regvpimbox;
5421
5422 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5423 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005424 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005425 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5426 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005427 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005428 == MBX_NOT_FINISHED) {
5429 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005430 }
5431 }
5432}
5433
5434/* Start Link up / RSCN discovery on NPR nodes */
5435void
5436lpfc_disc_start(struct lpfc_vport *vport)
5437{
5438 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5439 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005440 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005441 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005442
James Smarte74c03c2013-04-17 20:15:19 -04005443 if (!lpfc_is_link_up(phba)) {
5444 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5445 "3315 Link is not up %x\n",
5446 phba->link_state);
dea31012005-04-17 16:05:31 -05005447 return;
James Smarte74c03c2013-04-17 20:15:19 -04005448 }
James Smart2e0fef82007-06-17 19:56:36 -05005449
5450 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005451 clear_la_pending = 1;
5452 else
5453 clear_la_pending = 0;
5454
James Smart2e0fef82007-06-17 19:56:36 -05005455 if (vport->port_state < LPFC_VPORT_READY)
5456 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005457
James Smart2e0fef82007-06-17 19:56:36 -05005458 lpfc_set_disctmo(vport);
5459
James Smart2e0fef82007-06-17 19:56:36 -05005460 vport->fc_prevDID = vport->fc_myDID;
5461 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005462
5463 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005464 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5465 "0202 Start Discovery hba state x%x "
5466 "Data: x%x x%x x%x\n",
5467 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5468 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005469
5470 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005471 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005472
5473 if (num_sent)
5474 return;
5475
James Smart6fa139f2013-04-17 20:20:00 -04005476 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005477 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005478 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005479 !(vport->fc_flag & FC_RSCN_MODE) &&
5480 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005481 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005482 lpfc_issue_reg_vpi(phba, vport);
5483 return;
5484 }
James Smart2e0fef82007-06-17 19:56:36 -05005485
James Smart92d7f7b2007-06-17 19:56:38 -05005486 /*
5487 * For SLI2, we need to set port_state to READY and continue
5488 * discovery.
5489 */
5490 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5491 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005492 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005493
5494 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005495 vport->num_disc_nodes = 0;
5496 /* go thru NPR nodes and issue ELS PLOGIs */
5497 if (vport->fc_npr_cnt)
5498 lpfc_els_disc_plogi(vport);
5499
5500 if (!vport->num_disc_nodes) {
5501 spin_lock_irq(shost->host_lock);
5502 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5503 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005504 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005505 }
5506 }
James Smart92d7f7b2007-06-17 19:56:38 -05005507 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005508 } else {
5509 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005510 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005511
5512 if (num_sent)
5513 return;
5514
James Smart2e0fef82007-06-17 19:56:36 -05005515 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005516 /* Check to see if more RSCNs came in while we
5517 * were processing this one.
5518 */
James Smart2e0fef82007-06-17 19:56:36 -05005519 if ((vport->fc_rscn_id_cnt == 0) &&
5520 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5521 spin_lock_irq(shost->host_lock);
5522 vport->fc_flag &= ~FC_RSCN_MODE;
5523 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005524 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005525 } else
James Smart2e0fef82007-06-17 19:56:36 -05005526 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005527 }
5528 }
5529 return;
5530}
5531
5532/*
5533 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5534 * ring the match the sppecified nodelist.
5535 */
5536static void
James Smart2e0fef82007-06-17 19:56:36 -05005537lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005538{
James Smart2534ba72007-04-25 09:52:20 -04005539 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005540 IOCB_t *icmd;
5541 struct lpfc_iocbq *iocb, *next_iocb;
5542 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005543
James Smart895427b2017-02-12 13:52:30 -08005544 pring = lpfc_phba_elsring(phba);
Dick Kennedy1234a6d2017-09-29 17:34:29 -07005545 if (unlikely(!pring))
5546 return;
dea31012005-04-17 16:05:31 -05005547
5548 /* Error matching iocb on txq or txcmplq
5549 * First check the txq.
5550 */
James Smart2e0fef82007-06-17 19:56:36 -05005551 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005552 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5553 if (iocb->context1 != ndlp) {
5554 continue;
5555 }
5556 icmd = &iocb->iocb;
5557 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5558 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5559
James Smart2534ba72007-04-25 09:52:20 -04005560 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005561 }
5562 }
5563
5564 /* Next check the txcmplq */
5565 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5566 if (iocb->context1 != ndlp) {
5567 continue;
5568 }
5569 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005570 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5571 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005572 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005573 }
5574 }
James Smart2e0fef82007-06-17 19:56:36 -05005575 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005576
James Smarta257bf92009-04-06 18:48:10 -04005577 /* Cancel all the IOCBs from the completions list */
5578 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5579 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005580}
5581
Adrian Bunka6ababd2007-11-05 18:07:33 +01005582static void
James Smart2e0fef82007-06-17 19:56:36 -05005583lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005584{
5585 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005586 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005587
James Smart2e0fef82007-06-17 19:56:36 -05005588 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5589 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005590 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005591 if (!NLP_CHK_NODE_ACT(ndlp))
5592 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005593 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5594 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5595 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005596 }
dea31012005-04-17 16:05:31 -05005597 }
5598 }
dea31012005-04-17 16:05:31 -05005599}
5600
James Smart92d7f7b2007-06-17 19:56:38 -05005601void
5602lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5603{
5604 lpfc_els_flush_rscn(vport);
5605 lpfc_els_flush_cmd(vport);
5606 lpfc_disc_flush_list(vport);
5607}
5608
dea31012005-04-17 16:05:31 -05005609/*****************************************************************************/
5610/*
5611 * NAME: lpfc_disc_timeout
5612 *
5613 * FUNCTION: Fibre Channel driver discovery timeout routine.
5614 *
5615 * EXECUTION ENVIRONMENT: interrupt only
5616 *
5617 * CALLED FROM:
5618 * Timer function
5619 *
5620 * RETURNS:
5621 * none
5622 */
5623/*****************************************************************************/
5624void
Kees Cookf22eb4d2017-09-06 20:24:26 -07005625lpfc_disc_timeout(struct timer_list *t)
dea31012005-04-17 16:05:31 -05005626{
Kees Cookf22eb4d2017-09-06 20:24:26 -07005627 struct lpfc_vport *vport = from_timer(vport, t, fc_disctmo);
James Smart2e0fef82007-06-17 19:56:36 -05005628 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005629 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005630 unsigned long flags = 0;
5631
5632 if (unlikely(!phba))
5633 return;
5634
James Smart5e9d9b82008-06-14 22:52:53 -04005635 spin_lock_irqsave(&vport->work_port_lock, flags);
5636 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5637 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005638 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005639 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005640
James Smart5e9d9b82008-06-14 22:52:53 -04005641 if (!tmo_posted)
5642 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005643 return;
5644}
5645
5646static void
James Smart2e0fef82007-06-17 19:56:36 -05005647lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005648{
James Smart2e0fef82007-06-17 19:56:36 -05005649 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5650 struct lpfc_hba *phba = vport->phba;
5651 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005652 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005653 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005654 int rc, clrlaerr = 0;
5655
James Smart2e0fef82007-06-17 19:56:36 -05005656 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005657 return;
5658
James Smart2e0fef82007-06-17 19:56:36 -05005659 spin_lock_irq(shost->host_lock);
5660 vport->fc_flag &= ~FC_DISC_TMO;
5661 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005662
James Smart858c9f62007-06-17 19:56:39 -05005663 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5664 "disc timeout: state:x%x rtry:x%x flg:x%x",
5665 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5666
James Smart2e0fef82007-06-17 19:56:36 -05005667 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005668
5669 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005670 /*
5671 * port_state is identically LPFC_LOCAL_CFG_LINK while
5672 * waiting for FAN timeout
5673 */
James Smarte8b62012007-08-02 11:10:09 -04005674 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5675 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005676
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005677 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005678 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005679 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005680 if (!NLP_CHK_NODE_ACT(ndlp))
5681 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005682 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5683 continue;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005684 if (ndlp->nlp_type & NLP_FABRIC) {
5685 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005686 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005687
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005688 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005689 /* Fail outstanding IO now since device
5690 * is marked for PLOGI.
5691 */
James Smart2e0fef82007-06-17 19:56:36 -05005692 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005693 }
5694 }
James Smart92d7f7b2007-06-17 19:56:38 -05005695 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005696 if (phba->sli_rev <= LPFC_SLI_REV3)
5697 lpfc_initial_flogi(vport);
5698 else
5699 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005700 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005701 }
dea31012005-04-17 16:05:31 -05005702 break;
5703
James Smart92d7f7b2007-06-17 19:56:38 -05005704 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005705 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005706 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005707 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005708 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5709 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005710 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005711
5712 /* Assume no Fabric and go on with discovery.
5713 * Check for outstanding ELS FLOGI to abort.
5714 */
5715
5716 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005717 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005718
5719 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005720 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005721 break;
5722
5723 case LPFC_FABRIC_CFG_LINK:
5724 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5725 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005726 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5727 "0223 Timeout while waiting for "
5728 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005729 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005730 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005731 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005732 lpfc_els_abort(phba, ndlp);
5733
5734 /* ReStart discovery */
5735 goto restart_disc;
dea31012005-04-17 16:05:31 -05005736
5737 case LPFC_NS_QRY:
5738 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005739 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5740 "0224 NameServer Query timeout "
5741 "Data: x%x x%x\n",
5742 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005743
James Smart92d7f7b2007-06-17 19:56:38 -05005744 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5745 /* Try it one more time */
5746 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005747 vport->gidft_inp = 0;
5748 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005749 if (rc == 0)
5750 break;
dea31012005-04-17 16:05:31 -05005751 }
James Smart92d7f7b2007-06-17 19:56:38 -05005752 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005753
James Smart87af33f2007-10-27 13:37:43 -04005754restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005755 /*
5756 * Discovery is over.
5757 * set port_state to PORT_READY if SLI2.
5758 * cmpl_reg_vpi will set port_state to READY for SLI3.
5759 */
James Smart3772a992009-05-22 14:50:54 -04005760 if (phba->sli_rev < LPFC_SLI_REV4) {
5761 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5762 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005763 else {
James Smart3772a992009-05-22 14:50:54 -04005764 lpfc_issue_clear_la(phba, vport);
5765 vport->port_state = LPFC_VPORT_READY;
5766 }
dea31012005-04-17 16:05:31 -05005767 }
5768
5769 /* Setup and issue mailbox INITIALIZE LINK command */
5770 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5771 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005772 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5773 "0206 Device Discovery "
5774 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005775 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005776 break;
5777 }
5778
5779 lpfc_linkdown(phba);
5780 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5781 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005782 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005783 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005784 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005785 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005786 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005787 if (rc == MBX_NOT_FINISHED)
5788 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5789
5790 break;
5791
5792 case LPFC_DISC_AUTH:
5793 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005794 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5795 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005796 lpfc_disc_flush_list(vport);
5797
James Smart92d7f7b2007-06-17 19:56:38 -05005798 /*
5799 * set port_state to PORT_READY if SLI2.
5800 * cmpl_reg_vpi will set port_state to READY for SLI3.
5801 */
James Smart3772a992009-05-22 14:50:54 -04005802 if (phba->sli_rev < LPFC_SLI_REV4) {
5803 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5804 lpfc_issue_reg_vpi(phba, vport);
5805 else { /* NPIV Not enabled */
5806 lpfc_issue_clear_la(phba, vport);
5807 vport->port_state = LPFC_VPORT_READY;
5808 }
dea31012005-04-17 16:05:31 -05005809 }
5810 break;
5811
James Smart2e0fef82007-06-17 19:56:36 -05005812 case LPFC_VPORT_READY:
5813 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005814 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5815 "0231 RSCN timeout Data: x%x "
5816 "x%x\n",
5817 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005818
5819 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005820 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005821
James Smart2e0fef82007-06-17 19:56:36 -05005822 lpfc_els_flush_rscn(vport);
5823 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005824 }
5825 break;
James Smart2e0fef82007-06-17 19:56:36 -05005826
James Smart92d7f7b2007-06-17 19:56:38 -05005827 default:
James Smarte8b62012007-08-02 11:10:09 -04005828 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005829 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005830 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005831 break;
5832 }
5833
5834 switch (phba->link_state) {
5835 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005836 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005837 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5838 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005839 clrlaerr = 1;
5840 break;
5841
James Smart09372822008-01-11 01:52:54 -05005842 case LPFC_LINK_UP:
5843 lpfc_issue_clear_la(phba, vport);
5844 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005845 case LPFC_LINK_UNKNOWN:
5846 case LPFC_WARM_START:
5847 case LPFC_INIT_START:
5848 case LPFC_INIT_MBX_CMDS:
5849 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005850 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005851 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5852 "0230 Unexpected timeout, hba link "
5853 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005854 clrlaerr = 1;
5855 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005856
5857 case LPFC_HBA_READY:
5858 break;
dea31012005-04-17 16:05:31 -05005859 }
5860
5861 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005862 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005863 if (phba->sli_rev != LPFC_SLI_REV4) {
5864 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5865 ~LPFC_STOP_IOCB_EVENT;
5866 psli->sli3_ring[LPFC_FCP_RING].flag &=
5867 ~LPFC_STOP_IOCB_EVENT;
5868 }
James Smart2e0fef82007-06-17 19:56:36 -05005869 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005870 }
dea31012005-04-17 16:05:31 -05005871 return;
5872}
5873
dea31012005-04-17 16:05:31 -05005874/*
5875 * This routine handles processing a NameServer REG_LOGIN mailbox
5876 * command upon completion. It is setup in the LPFC_MBOXQ
5877 * as the completion routine when the command is
5878 * handed off to the SLI layer.
5879 */
5880void
James Smart2e0fef82007-06-17 19:56:36 -05005881lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005882{
James Smart04c68492009-05-22 14:52:52 -04005883 MAILBOX_t *mb = &pmb->u.mb;
James Smart3e1f0712018-11-29 16:09:29 -08005884 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *)(pmb->ctx_buf);
5885 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *)pmb->ctx_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005886 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005887
James Smart3e1f0712018-11-29 16:09:29 -08005888 pmb->ctx_buf = NULL;
5889 pmb->ctx_ndlp = NULL;
dea31012005-04-17 16:05:31 -05005890
James Smart6d368e52011-05-24 11:44:12 -04005891 if (phba->sli_rev < LPFC_SLI_REV4)
5892 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005893 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005894 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005895 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005896 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5897 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5898 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005899 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005900 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005901 /*
5902 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005903 * 0xfffffa (FDMI well known port).
5904 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5905 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005906 */
James Smart4258e982015-12-16 18:11:58 -05005907 if (vport->port_type == LPFC_PHYSICAL_PORT)
5908 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005909 else
James Smart4258e982015-12-16 18:11:58 -05005910 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5911
dea31012005-04-17 16:05:31 -05005912
James Smartfa4066b2008-01-11 01:53:27 -05005913 /* decrement the node reference count held for this callback
5914 * function.
5915 */
James Smart329f9bc2007-04-25 09:53:01 -04005916 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005917 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5918 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005919 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005920
5921 return;
5922}
5923
James Smart685f0bf2007-04-25 09:53:08 -04005924static int
5925lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5926{
5927 uint16_t *rpi = param;
5928
James Smarteff4a012012-01-18 16:25:25 -05005929 /* check for active node */
5930 if (!NLP_CHK_NODE_ACT(ndlp))
5931 return 0;
5932
James Smart685f0bf2007-04-25 09:53:08 -04005933 return ndlp->nlp_rpi == *rpi;
5934}
5935
5936static int
5937lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5938{
5939 return memcmp(&ndlp->nlp_portname, param,
5940 sizeof(ndlp->nlp_portname)) == 0;
5941}
5942
Adrian Bunka6ababd2007-11-05 18:07:33 +01005943static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005944__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005945{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005946 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005947
James Smart2e0fef82007-06-17 19:56:36 -05005948 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005949 if (filter(ndlp, param)) {
5950 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5951 "3185 FIND node filter %p DID "
James Smart49382502017-11-20 16:00:38 -08005952 "ndlp %p did x%x flg x%x st x%x "
5953 "xri x%x type x%x rpi x%x\n",
James Smart34f5ad82012-08-03 12:35:03 -04005954 filter, ndlp, ndlp->nlp_DID,
James Smart49382502017-11-20 16:00:38 -08005955 ndlp->nlp_flag, ndlp->nlp_state,
5956 ndlp->nlp_xri, ndlp->nlp_type,
5957 ndlp->nlp_rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005958 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005959 }
James Smart685f0bf2007-04-25 09:53:08 -04005960 }
James Smart34f5ad82012-08-03 12:35:03 -04005961 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5962 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005963 return NULL;
dea31012005-04-17 16:05:31 -05005964}
5965
James Smart685f0bf2007-04-25 09:53:08 -04005966/*
5967 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005968 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005969 */
5970struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005971__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005972{
James Smart2e0fef82007-06-17 19:56:36 -05005973 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005974}
5975
James Smart488d1462006-03-07 15:02:37 -05005976/*
James Smart685f0bf2007-04-25 09:53:08 -04005977 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005978 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005979 */
5980struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005981lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005982{
James Smart2e0fef82007-06-17 19:56:36 -05005983 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005984 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005985
James Smart2e0fef82007-06-17 19:56:36 -05005986 spin_lock_irq(shost->host_lock);
5987 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5988 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005989 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005990}
5991
James Smartcb69f7d2011-12-13 13:21:57 -05005992/*
5993 * This routine looks up the ndlp lists for the given RPI. If the rpi
5994 * is found, the routine returns the node element list pointer else
5995 * return NULL.
5996 */
5997struct lpfc_nodelist *
5998lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5999{
6000 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
6001 struct lpfc_nodelist *ndlp;
6002
6003 spin_lock_irq(shost->host_lock);
6004 ndlp = __lpfc_findnode_rpi(vport, rpi);
6005 spin_unlock_irq(shost->host_lock);
6006 return ndlp;
6007}
6008
6009/**
6010 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
6011 * @phba: pointer to lpfc hba data structure.
6012 * @vpi: the physical host virtual N_Port identifier.
6013 *
6014 * This routine finds a vport on a HBA (referred by @phba) through a
6015 * @vpi. The function walks the HBA's vport list and returns the address
6016 * of the vport with the matching @vpi.
6017 *
6018 * Return code
6019 * NULL - No vport with the matching @vpi found
6020 * Otherwise - Address to the vport with the matching @vpi.
6021 **/
6022struct lpfc_vport *
6023lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
6024{
6025 struct lpfc_vport *vport;
6026 unsigned long flags;
6027 int i = 0;
6028
6029 /* The physical ports are always vpi 0 - translate is unnecessary. */
6030 if (vpi > 0) {
6031 /*
6032 * Translate the physical vpi to the logical vpi. The
6033 * vport stores the logical vpi.
6034 */
6035 for (i = 0; i < phba->max_vpi; i++) {
6036 if (vpi == phba->vpi_ids[i])
6037 break;
6038 }
6039
6040 if (i >= phba->max_vpi) {
6041 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
6042 "2936 Could not find Vport mapped "
6043 "to vpi %d\n", vpi);
6044 return NULL;
6045 }
6046 }
6047
James Smart523128e2018-09-10 10:30:46 -07006048 spin_lock_irqsave(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006049 list_for_each_entry(vport, &phba->port_list, listentry) {
6050 if (vport->vpi == i) {
James Smart523128e2018-09-10 10:30:46 -07006051 spin_unlock_irqrestore(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006052 return vport;
6053 }
6054 }
James Smart523128e2018-09-10 10:30:46 -07006055 spin_unlock_irqrestore(&phba->port_list_lock, flags);
James Smartcb69f7d2011-12-13 13:21:57 -05006056 return NULL;
6057}
6058
James Smart9d3d3402017-04-21 16:05:00 -07006059struct lpfc_nodelist *
6060lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05006061{
James Smart9d3d3402017-04-21 16:05:00 -07006062 struct lpfc_nodelist *ndlp;
6063 int rpi = LPFC_RPI_ALLOC_ERROR;
6064
6065 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
6066 rpi = lpfc_sli4_alloc_rpi(vport->phba);
6067 if (rpi == LPFC_RPI_ALLOC_ERROR)
6068 return NULL;
6069 }
6070
6071 ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
6072 if (!ndlp) {
6073 if (vport->phba->sli_rev == LPFC_SLI_REV4)
6074 lpfc_sli4_free_rpi(vport->phba, rpi);
6075 return NULL;
6076 }
6077
dea31012005-04-17 16:05:31 -05006078 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05006079
6080 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04006081 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05006082 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07006083 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04006084 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
6085 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
6086 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
6087 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006088 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04006089 ndlp->nlp_usg_map, ndlp);
6090
James Smartcff261f2013-12-17 20:29:47 -05006091 ndlp->active_rrqs_xri_bitmap =
6092 mempool_alloc(vport->phba->active_rrq_pool,
6093 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04006094 if (ndlp->active_rrqs_xri_bitmap)
6095 memset(ndlp->active_rrqs_xri_bitmap, 0,
6096 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05006097 }
6098
James Smart725dd392013-10-10 12:23:30 -04006099
James Smart858c9f62007-06-17 19:56:39 -05006100
6101 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
6102 "node init: did:x%x",
6103 ndlp->nlp_DID, 0, 0);
6104
James Smart9d3d3402017-04-21 16:05:00 -07006105 return ndlp;
dea31012005-04-17 16:05:31 -05006106}
James Smart329f9bc2007-04-25 09:53:01 -04006107
James Smart98c9ea52007-10-27 13:37:33 -04006108/* This routine releases all resources associated with a specifc NPort's ndlp
6109 * and mempool_free's the nodelist.
6110 */
James Smart311464e2007-08-02 11:10:37 -04006111static void
James Smart329f9bc2007-04-25 09:53:01 -04006112lpfc_nlp_release(struct kref *kref)
6113{
James Smarte47c9092008-02-08 18:49:26 -05006114 struct lpfc_hba *phba;
6115 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04006116 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6117 kref);
James Smart858c9f62007-06-17 19:56:39 -05006118
6119 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6120 "node release: did:x%x flg:x%x type:x%x",
6121 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6122
James Smarte47c9092008-02-08 18:49:26 -05006123 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04006124 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04006125 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04006126 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006127 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006128
6129 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05006130 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05006131
6132 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04006133 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006134 spin_lock_irqsave(&phba->ndlp_lock, flags);
6135 NLP_CLR_NODE_ACT(ndlp);
6136 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05006137 if (phba->sli_rev == LPFC_SLI_REV4)
6138 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006139
6140 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04006141 if (NLP_CHK_FREE_REQ(ndlp)) {
6142 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05006143 if (phba->sli_rev == LPFC_SLI_REV4)
6144 mempool_free(ndlp->active_rrqs_xri_bitmap,
6145 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04006146 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04006147 }
James Smart329f9bc2007-04-25 09:53:01 -04006148}
6149
James Smart98c9ea52007-10-27 13:37:33 -04006150/* This routine bumps the reference count for a ndlp structure to ensure
6151 * that one discovery thread won't free a ndlp while another discovery thread
6152 * is using it.
6153 */
James Smart329f9bc2007-04-25 09:53:01 -04006154struct lpfc_nodelist *
6155lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6156{
James Smarte47c9092008-02-08 18:49:26 -05006157 struct lpfc_hba *phba;
6158 unsigned long flags;
6159
James Smart98c9ea52007-10-27 13:37:33 -04006160 if (ndlp) {
6161 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6162 "node get: did:x%x flg:x%x refcnt:x%x",
6163 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006164 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006165 /* The check of ndlp usage to prevent incrementing the
6166 * ndlp reference count that is in the process of being
6167 * released.
6168 */
James Smarta257bf92009-04-06 18:48:10 -04006169 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006170 spin_lock_irqsave(&phba->ndlp_lock, flags);
6171 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6172 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6173 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6174 "0276 lpfc_nlp_get: ndlp:x%p "
6175 "usgmap:x%x refcnt:%d\n",
6176 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006177 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006178 return NULL;
6179 } else
6180 kref_get(&ndlp->kref);
6181 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04006182 }
James Smart329f9bc2007-04-25 09:53:01 -04006183 return ndlp;
6184}
6185
James Smart98c9ea52007-10-27 13:37:33 -04006186/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05006187 * count goes to 0, this indicates the the associated nodelist should be
6188 * freed. Returning 1 indicates the ndlp resource has been released; on the
6189 * other hand, returning 0 indicates the ndlp resource has not been released
6190 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006191 */
James Smart329f9bc2007-04-25 09:53:01 -04006192int
6193lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6194{
James Smarte47c9092008-02-08 18:49:26 -05006195 struct lpfc_hba *phba;
6196 unsigned long flags;
6197
6198 if (!ndlp)
6199 return 1;
6200
6201 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6202 "node put: did:x%x flg:x%x refcnt:x%x",
6203 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006204 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006205 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006206 spin_lock_irqsave(&phba->ndlp_lock, flags);
6207 /* Check the ndlp memory free acknowledge flag to avoid the
6208 * possible race condition that kref_put got invoked again
6209 * after previous one has done ndlp memory free.
6210 */
6211 if (NLP_CHK_FREE_ACK(ndlp)) {
6212 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6213 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6214 "0274 lpfc_nlp_put: ndlp:x%p "
6215 "usgmap:x%x refcnt:%d\n",
6216 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006217 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006218 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006219 }
James Smarte47c9092008-02-08 18:49:26 -05006220 /* Check the ndlp inactivate log flag to avoid the possible
6221 * race condition that kref_put got invoked again after ndlp
6222 * is already in inactivating state.
6223 */
6224 if (NLP_CHK_IACT_REQ(ndlp)) {
6225 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6226 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6227 "0275 lpfc_nlp_put: ndlp:x%p "
6228 "usgmap:x%x refcnt:%d\n",
6229 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006230 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006231 return 1;
6232 }
6233 /* For last put, mark the ndlp usage flags to make sure no
6234 * other kref_get and kref_put on the same ndlp shall get
6235 * in between the process when the final kref_put has been
6236 * invoked on this ndlp.
6237 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006238 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006239 /* Indicate ndlp is put to inactive state. */
6240 NLP_SET_IACT_REQ(ndlp);
6241 /* Acknowledge ndlp memory free has been seen. */
6242 if (NLP_CHK_FREE_REQ(ndlp))
6243 NLP_SET_FREE_ACK(ndlp);
6244 }
6245 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6246 /* Note, the kref_put returns 1 when decrementing a reference
6247 * count that was 1, it invokes the release callback function,
6248 * but it still left the reference count as 1 (not actually
6249 * performs the last decrementation). Otherwise, it actually
6250 * decrements the reference count and returns 0.
6251 */
6252 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006253}
James Smart98c9ea52007-10-27 13:37:33 -04006254
6255/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006256 * by any other discovery thread. This routine returns 1 if the
6257 * ndlp has been freed. A return value of 0 indicates the ndlp is
6258 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006259 */
6260int
6261lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6262{
6263 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6264 "node not used: did:x%x flg:x%x refcnt:x%x",
6265 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006266 kref_read(&ndlp->kref));
6267 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006268 if (lpfc_nlp_put(ndlp))
6269 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006270 return 0;
6271}
James Smart6fb120a2009-05-22 14:52:59 -04006272
6273/**
6274 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6275 * @phba: Pointer to hba context object.
6276 *
6277 * This function iterate through all FC nodes associated
6278 * will all vports to check if there is any node with
6279 * fc_rports associated with it. If there is an fc_rport
6280 * associated with the node, then the node is either in
6281 * discovered state or its devloss_timer is pending.
6282 */
6283static int
6284lpfc_fcf_inuse(struct lpfc_hba *phba)
6285{
6286 struct lpfc_vport **vports;
6287 int i, ret = 0;
6288 struct lpfc_nodelist *ndlp;
6289 struct Scsi_Host *shost;
6290
6291 vports = lpfc_create_vport_work_array(phba);
6292
James Smart63e801c2010-11-20 23:14:19 -05006293 /* If driver cannot allocate memory, indicate fcf is in use */
6294 if (!vports)
6295 return 1;
6296
James Smart6fb120a2009-05-22 14:52:59 -04006297 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6298 shost = lpfc_shost_from_vport(vports[i]);
6299 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006300 /*
6301 * IF the CVL_RCVD bit is not set then we have sent the
6302 * flogi.
6303 * If dev_loss fires while we are waiting we do not want to
6304 * unreg the fcf.
6305 */
6306 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6307 spin_unlock_irq(shost->host_lock);
6308 ret = 1;
6309 goto out;
6310 }
James Smart6fb120a2009-05-22 14:52:59 -04006311 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6312 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6313 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6314 ret = 1;
6315 spin_unlock_irq(shost->host_lock);
6316 goto out;
James Smart80c17842012-03-01 22:35:45 -05006317 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6318 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006319 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006320 "2624 RPI %x DID %x flag %x "
6321 "still logged in\n",
6322 ndlp->nlp_rpi, ndlp->nlp_DID,
6323 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006324 }
6325 }
6326 spin_unlock_irq(shost->host_lock);
6327 }
6328out:
6329 lpfc_destroy_vport_work_array(phba, vports);
6330 return ret;
6331}
6332
6333/**
6334 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6335 * @phba: Pointer to hba context object.
6336 * @mboxq: Pointer to mailbox object.
6337 *
6338 * This function frees memory associated with the mailbox command.
6339 */
James Smart1b511972011-12-13 13:23:09 -05006340void
James Smart6fb120a2009-05-22 14:52:59 -04006341lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6342{
6343 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006344 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006345
6346 if (mboxq->u.mb.mbxStatus) {
6347 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6348 "2555 UNREG_VFI mbxStatus error x%x "
6349 "HBA state x%x\n",
6350 mboxq->u.mb.mbxStatus, vport->port_state);
6351 }
James Smart38b92ef2010-08-04 16:11:39 -04006352 spin_lock_irq(shost->host_lock);
6353 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6354 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006355 mempool_free(mboxq, phba->mbox_mem_pool);
6356 return;
6357}
6358
6359/**
6360 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6361 * @phba: Pointer to hba context object.
6362 * @mboxq: Pointer to mailbox object.
6363 *
6364 * This function frees memory associated with the mailbox command.
6365 */
6366static void
6367lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6368{
6369 struct lpfc_vport *vport = mboxq->vport;
6370
6371 if (mboxq->u.mb.mbxStatus) {
6372 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6373 "2550 UNREG_FCFI mbxStatus error x%x "
6374 "HBA state x%x\n",
6375 mboxq->u.mb.mbxStatus, vport->port_state);
6376 }
6377 mempool_free(mboxq, phba->mbox_mem_pool);
6378 return;
6379}
6380
6381/**
James Smartecfd03c2010-02-12 14:41:27 -05006382 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006383 * @phba: Pointer to hba context object.
6384 *
James Smartecfd03c2010-02-12 14:41:27 -05006385 * This function prepare the HBA for unregistering the currently registered
6386 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6387 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006388 */
James Smartecfd03c2010-02-12 14:41:27 -05006389int
6390lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006391{
James Smart6fb120a2009-05-22 14:52:59 -04006392 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006393 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006394 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006395 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006396
James Smartecfd03c2010-02-12 14:41:27 -05006397 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006398 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006399 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006400
James Smart4d9ab992009-10-02 15:16:39 -04006401 /* At this point, all discovery is aborted */
6402 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006403
6404 /* Unregister VPIs */
6405 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006406 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006407 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006408 /* Stop FLOGI/FDISC retries */
6409 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6410 if (ndlp)
6411 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006412 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006413 if (phba->sli_rev == LPFC_SLI_REV4)
6414 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006415 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006416 shost = lpfc_shost_from_vport(vports[i]);
6417 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006418 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006419 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006420 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006421 }
6422 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006423 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6424 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6425 if (ndlp)
6426 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6427 lpfc_cleanup_pending_mbox(phba->pport);
6428 if (phba->sli_rev == LPFC_SLI_REV4)
6429 lpfc_sli4_unreg_all_rpis(phba->pport);
6430 lpfc_mbx_unreg_vpi(phba->pport);
6431 shost = lpfc_shost_from_vport(phba->pport);
6432 spin_lock_irq(shost->host_lock);
6433 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6434 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6435 spin_unlock_irq(shost->host_lock);
6436 }
James Smart6fb120a2009-05-22 14:52:59 -04006437
James Smart695a8142010-01-26 23:08:03 -05006438 /* Cleanup any outstanding ELS commands */
6439 lpfc_els_flush_all_cmd(phba);
6440
James Smart1b511972011-12-13 13:23:09 -05006441 /* Unregister the physical port VFI */
6442 rc = lpfc_issue_unreg_vfi(phba->pport);
6443 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006444}
6445
6446/**
6447 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6448 * @phba: Pointer to hba context object.
6449 *
6450 * This function issues synchronous unregister FCF mailbox command to HBA to
6451 * unregister the currently registered FCF record. The driver does not reset
6452 * the driver FCF usage state flags.
6453 *
6454 * Return 0 if successfully issued, none-zero otherwise.
6455 */
6456int
6457lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6458{
6459 LPFC_MBOXQ_t *mbox;
6460 int rc;
6461
James Smart6fb120a2009-05-22 14:52:59 -04006462 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6463 if (!mbox) {
6464 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006465 "2551 UNREG_FCFI mbox allocation failed"
6466 "HBA state x%x\n", phba->pport->port_state);
6467 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006468 }
James Smart6fb120a2009-05-22 14:52:59 -04006469 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6470 mbox->vport = phba->pport;
6471 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6472 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6473
6474 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006475 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6476 "2552 Unregister FCFI command failed rc x%x "
6477 "HBA state x%x\n",
6478 rc, phba->pport->port_state);
6479 return -EINVAL;
6480 }
6481 return 0;
6482}
6483
6484/**
6485 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6486 * @phba: Pointer to hba context object.
6487 *
6488 * This function unregisters the currently reigstered FCF. This function
6489 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6490 */
6491void
6492lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6493{
6494 int rc;
6495
6496 /* Preparation for unregistering fcf */
6497 rc = lpfc_unregister_fcf_prep(phba);
6498 if (rc) {
6499 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6500 "2748 Failed to prepare for unregistering "
6501 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006502 return;
6503 }
6504
James Smartecfd03c2010-02-12 14:41:27 -05006505 /* Now, unregister FCF record and reset HBA FCF state */
6506 rc = lpfc_sli4_unregister_fcf(phba);
6507 if (rc)
6508 return;
6509 /* Reset HBA FCF states after successful unregister FCF */
6510 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006511 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006512
6513 /*
6514 * If driver is not unloading, check if there is any other
6515 * FCF record that can be used for discovery.
6516 */
6517 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006518 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006519 return;
6520
James Smart0c9ab6f2010-02-26 14:15:57 -05006521 /* This is considered as the initial FCF discovery scan */
6522 spin_lock_irq(&phba->hbalock);
6523 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6524 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006525
6526 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006527 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006528
James Smart0c9ab6f2010-02-26 14:15:57 -05006529 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006530
James Smart0c9ab6f2010-02-26 14:15:57 -05006531 if (rc) {
6532 spin_lock_irq(&phba->hbalock);
6533 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6534 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006535 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006536 "2553 lpfc_unregister_unused_fcf failed "
6537 "to read FCF record HBA state x%x\n",
6538 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006539 }
James Smartecfd03c2010-02-12 14:41:27 -05006540}
6541
6542/**
6543 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6544 * @phba: Pointer to hba context object.
6545 *
6546 * This function just unregisters the currently reigstered FCF. It does not
6547 * try to find another FCF for discovery.
6548 */
6549void
6550lpfc_unregister_fcf(struct lpfc_hba *phba)
6551{
6552 int rc;
6553
6554 /* Preparation for unregistering fcf */
6555 rc = lpfc_unregister_fcf_prep(phba);
6556 if (rc) {
6557 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6558 "2749 Failed to prepare for unregistering "
6559 "HBA's FCF record: rc=%d\n", rc);
6560 return;
6561 }
6562
6563 /* Now, unregister FCF record and reset HBA FCF state */
6564 rc = lpfc_sli4_unregister_fcf(phba);
6565 if (rc)
6566 return;
6567 /* Set proper HBA FCF states after successful unregister FCF */
6568 spin_lock_irq(&phba->hbalock);
6569 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6570 spin_unlock_irq(&phba->hbalock);
6571}
6572
6573/**
6574 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6575 * @phba: Pointer to hba context object.
6576 *
6577 * This function check if there are any connected remote port for the FCF and
6578 * if all the devices are disconnected, this function unregister FCFI.
6579 * This function also tries to use another FCF for discovery.
6580 */
6581void
6582lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6583{
6584 /*
James Smart3804dc82010-07-14 15:31:37 -04006585 * If HBA is not running in FIP mode, if HBA does not support
6586 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6587 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006588 */
6589 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006590 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006591 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006592 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006593 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006594 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006595 spin_unlock_irq(&phba->hbalock);
6596 return;
6597 }
6598 spin_unlock_irq(&phba->hbalock);
6599
6600 if (lpfc_fcf_inuse(phba))
6601 return;
6602
6603 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006604}
6605
6606/**
6607 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6608 * @phba: Pointer to hba context object.
6609 * @buff: Buffer containing the FCF connection table as in the config
6610 * region.
6611 * This function create driver data structure for the FCF connection
6612 * record table read from config region 23.
6613 */
6614static void
6615lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6616 uint8_t *buff)
6617{
6618 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6619 struct lpfc_fcf_conn_hdr *conn_hdr;
6620 struct lpfc_fcf_conn_rec *conn_rec;
6621 uint32_t record_count;
6622 int i;
6623
6624 /* Free the current connect table */
6625 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006626 &phba->fcf_conn_rec_list, list) {
6627 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006628 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006629 }
James Smart6fb120a2009-05-22 14:52:59 -04006630
6631 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6632 record_count = conn_hdr->length * sizeof(uint32_t)/
6633 sizeof(struct lpfc_fcf_conn_rec);
6634
6635 conn_rec = (struct lpfc_fcf_conn_rec *)
6636 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6637
6638 for (i = 0; i < record_count; i++) {
6639 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6640 continue;
6641 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6642 GFP_KERNEL);
6643 if (!conn_entry) {
6644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6645 "2566 Failed to allocate connection"
6646 " table entry\n");
6647 return;
6648 }
6649
6650 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6651 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006652 list_add_tail(&conn_entry->list,
6653 &phba->fcf_conn_rec_list);
6654 }
James Smartdf0d0852013-05-31 17:05:08 -04006655
6656 if (!list_empty(&phba->fcf_conn_rec_list)) {
6657 i = 0;
6658 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6659 list) {
6660 conn_rec = &conn_entry->conn_rec;
6661 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6662 "3345 FCF connection list rec[%02d]: "
6663 "flags:x%04x, vtag:x%04x, "
6664 "fabric_name:x%02x:%02x:%02x:%02x:"
6665 "%02x:%02x:%02x:%02x, "
6666 "switch_name:x%02x:%02x:%02x:%02x:"
6667 "%02x:%02x:%02x:%02x\n", i++,
6668 conn_rec->flags, conn_rec->vlan_tag,
6669 conn_rec->fabric_name[0],
6670 conn_rec->fabric_name[1],
6671 conn_rec->fabric_name[2],
6672 conn_rec->fabric_name[3],
6673 conn_rec->fabric_name[4],
6674 conn_rec->fabric_name[5],
6675 conn_rec->fabric_name[6],
6676 conn_rec->fabric_name[7],
6677 conn_rec->switch_name[0],
6678 conn_rec->switch_name[1],
6679 conn_rec->switch_name[2],
6680 conn_rec->switch_name[3],
6681 conn_rec->switch_name[4],
6682 conn_rec->switch_name[5],
6683 conn_rec->switch_name[6],
6684 conn_rec->switch_name[7]);
6685 }
6686 }
James Smart6fb120a2009-05-22 14:52:59 -04006687}
6688
6689/**
6690 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6691 * @phba: Pointer to hba context object.
6692 * @buff: Buffer containing the FCoE parameter data structure.
6693 *
6694 * This function update driver data structure with config
6695 * parameters read from config region 23.
6696 */
6697static void
6698lpfc_read_fcoe_param(struct lpfc_hba *phba,
6699 uint8_t *buff)
6700{
6701 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6702 struct lpfc_fcoe_params *fcoe_param;
6703
6704 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6705 buff;
6706 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006707 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006708
6709 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6710 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6711 return;
6712
James Smart6fb120a2009-05-22 14:52:59 -04006713 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6714 phba->valid_vlan = 1;
6715 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6716 0xFFF;
6717 }
6718
6719 phba->fc_map[0] = fcoe_param->fc_map[0];
6720 phba->fc_map[1] = fcoe_param->fc_map[1];
6721 phba->fc_map[2] = fcoe_param->fc_map[2];
6722 return;
6723}
6724
6725/**
6726 * lpfc_get_rec_conf23 - Get a record type in config region data.
6727 * @buff: Buffer containing config region 23 data.
6728 * @size: Size of the data buffer.
6729 * @rec_type: Record type to be searched.
6730 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006731 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006732 * of the record specified by record_type. If record found, this
6733 * function return pointer to the record else return NULL.
6734 */
6735static uint8_t *
6736lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6737{
6738 uint32_t offset = 0, rec_length;
6739
6740 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6741 (size < sizeof(uint32_t)))
6742 return NULL;
6743
6744 rec_length = buff[offset + 1];
6745
6746 /*
6747 * One TLV record has one word header and number of data words
6748 * specified in the rec_length field of the record header.
6749 */
6750 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6751 <= size) {
6752 if (buff[offset] == rec_type)
6753 return &buff[offset];
6754
6755 if (buff[offset] == LPFC_REGION23_LAST_REC)
6756 return NULL;
6757
6758 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6759 rec_length = buff[offset + 1];
6760 }
6761 return NULL;
6762}
6763
6764/**
6765 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6766 * @phba: Pointer to lpfc_hba data structure.
6767 * @buff: Buffer containing config region 23 data.
6768 * @size: Size of the data buffer.
6769 *
Stefan Weileef35c22010-08-06 21:11:15 +02006770 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006771 * populate driver data structure with the parameters.
6772 */
6773void
6774lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6775 uint8_t *buff,
6776 uint32_t size)
6777{
James Smarteb016562014-09-03 12:58:06 -04006778 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006779 uint8_t *rec_ptr;
6780
6781 /*
6782 * If data size is less than 2 words signature and version cannot be
6783 * verified.
6784 */
6785 if (size < 2*sizeof(uint32_t))
6786 return;
6787
6788 /* Check the region signature first */
6789 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6790 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6791 "2567 Config region 23 has bad signature\n");
6792 return;
6793 }
6794
6795 offset += 4;
6796
6797 /* Check the data structure version */
6798 if (buff[offset] != LPFC_REGION23_VERSION) {
6799 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6800 "2568 Config region 23 has bad version\n");
6801 return;
6802 }
6803 offset += 4;
6804
James Smart6fb120a2009-05-22 14:52:59 -04006805 /* Read FCoE param record */
6806 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6807 size - offset, FCOE_PARAM_TYPE);
6808 if (rec_ptr)
6809 lpfc_read_fcoe_param(phba, rec_ptr);
6810
6811 /* Read FCF connection table */
6812 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6813 size - offset, FCOE_CONN_TBL_TYPE);
6814 if (rec_ptr)
6815 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6816
6817}