blob: db2d0e692ddf97b467cbe9d3e0222a1f388d5ca0 [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 Smartd080abe2017-02-12 13:52:39 -08004 * Copyright (C) 2017 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Limited 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);
James Smart318083a2017-03-04 09:30:30 -0800643 if (phba->hba_flag & NVME_XRI_ABORT_EVENT)
644 lpfc_sli4_nvme_xri_abort_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400645 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
646 lpfc_sli4_els_xri_abort_event_proc(phba);
647 if (phba->hba_flag & ASYNC_EVENT)
648 lpfc_sli4_async_event_proc(phba);
649 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
650 spin_lock_irq(&phba->hbalock);
651 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
652 spin_unlock_irq(&phba->hbalock);
653 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
654 }
James Smartecfd03c2010-02-12 14:41:27 -0500655 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
656 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400657 }
658
James Smart549e55c2007-08-02 11:09:51 -0400659 vports = lpfc_create_vport_work_array(phba);
660 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400661 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400662 /*
663 * We could have no vports in array if unloading, so if
664 * this happens then just use the pport
665 */
666 if (vports[i] == NULL && i == 0)
667 vport = phba->pport;
668 else
669 vport = vports[i];
670 if (vport == NULL)
671 break;
James Smart58da1ff2008-04-07 10:15:56 -0400672 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400673 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400674 vport->work_port_events &= ~work_port_events;
675 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400676 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400677 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400678 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400679 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400680 if (work_port_events & WORKER_HB_TMO)
681 lpfc_hb_timeout_handler(phba);
682 if (work_port_events & WORKER_MBOX_TMO)
683 lpfc_mbox_timeout_handler(phba);
684 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
685 lpfc_unblock_fabric_iocbs(phba);
James Smart549e55c2007-08-02 11:09:51 -0400686 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
687 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500688 if (work_port_events & WORKER_DELAYED_DISC_TMO)
689 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500690 }
James Smart09372822008-01-11 01:52:54 -0500691 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500692
James Smart895427b2017-02-12 13:52:30 -0800693 pring = lpfc_phba_elsring(phba);
James Smart858c9f62007-06-17 19:56:39 -0500694 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
695 status >>= (4*LPFC_ELS_RING);
James Smart0c9c6a72017-05-15 15:20:39 -0700696 if (pring && (status & HA_RXMASK ||
697 pring->flag & LPFC_DEFERRED_RING_EVENT ||
698 phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400699 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500700 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400701 /* Set the lpfc data pending flag */
702 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500703 } else {
James Smartae9e28f2017-05-15 15:20:51 -0700704 if (phba->link_state >= LPFC_LINK_UP ||
705 phba->link_flag & LS_MDS_LOOPBACK) {
James Smart94661502013-03-01 16:37:29 -0500706 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
707 lpfc_sli_handle_slow_ring_event(phba, pring,
708 (status &
709 HA_RXMASK));
710 }
James Smart858c9f62007-06-17 19:56:39 -0500711 }
Sebastian Herbsztb1039182015-07-22 10:53:22 +0200712 if ((phba->sli_rev == LPFC_SLI_REV4) &&
James Smart0e9bb8d2013-03-01 16:35:12 -0500713 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400714 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500715 /*
716 * Turn on Ring interrupts
717 */
James Smart3772a992009-05-22 14:50:54 -0400718 if (phba->sli_rev <= LPFC_SLI_REV3) {
719 spin_lock_irq(&phba->hbalock);
720 control = readl(phba->HCregaddr);
721 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
722 lpfc_debugfs_slow_ring_trc(phba,
723 "WRK Enable ring: cntl:x%x hacopy:x%x",
724 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400725
James Smart3772a992009-05-22 14:50:54 -0400726 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
727 writel(control, phba->HCregaddr);
728 readl(phba->HCregaddr); /* flush */
729 } else {
730 lpfc_debugfs_slow_ring_trc(phba,
731 "WRK Ring ok: cntl:x%x hacopy:x%x",
732 control, ha_copy, 0);
733 }
734 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500735 }
736 }
James Smart2e0fef82007-06-17 19:56:36 -0500737 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500738}
739
dea31012005-04-17 16:05:31 -0500740int
741lpfc_do_work(void *p)
742{
743 struct lpfc_hba *phba = p;
744 int rc;
dea31012005-04-17 16:05:31 -0500745
Dongsheng Yang8698a742014-03-11 18:09:12 +0800746 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400747 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400748 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500749
James Smart3a55b532008-12-04 22:38:54 -0500750 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400751 /* wait and check worker queue activities */
752 rc = wait_event_interruptible(phba->work_waitq,
753 (test_and_clear_bit(LPFC_DATA_READY,
754 &phba->data_flags)
755 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500756 /* Signal wakeup shall terminate the worker thread */
757 if (rc) {
758 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
759 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500760 break;
James Smart3a55b532008-12-04 22:38:54 -0500761 }
dea31012005-04-17 16:05:31 -0500762
James Smart5e9d9b82008-06-14 22:52:53 -0400763 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500764 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500765 }
James Smart3a55b532008-12-04 22:38:54 -0500766 phba->worker_thread = NULL;
767 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
768 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500769 return 0;
770}
771
772/*
773 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300774 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500775 * embedding it in the IOCB.
776 */
777int
James Smart2e0fef82007-06-17 19:56:36 -0500778lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500779 uint32_t evt)
780{
781 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500782 unsigned long flags;
dea31012005-04-17 16:05:31 -0500783
784 /*
785 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
786 * be queued to worker thread for processing
787 */
James Smart92d7f7b2007-06-17 19:56:38 -0500788 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500789 if (!evtp)
790 return 0;
791
792 evtp->evt_arg1 = arg1;
793 evtp->evt_arg2 = arg2;
794 evtp->evt = evt;
795
James Smarted957682007-06-17 19:56:37 -0500796 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400797 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500798 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500799
James Smart5e9d9b82008-06-14 22:52:53 -0400800 lpfc_worker_wake_up(phba);
801
dea31012005-04-17 16:05:31 -0500802 return 1;
803}
804
James Smart92d7f7b2007-06-17 19:56:38 -0500805void
806lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
807{
James Smart09372822008-01-11 01:52:54 -0500808 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500809 struct lpfc_hba *phba = vport->phba;
810 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500811
812 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500813 if (!NLP_CHK_NODE_ACT(ndlp))
814 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500815 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
816 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400817 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
818 ((vport->port_type == LPFC_NPIV_PORT) &&
819 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500820 lpfc_unreg_rpi(vport, ndlp);
821
822 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400823 if ((phba->sli_rev < LPFC_SLI_REV4) &&
824 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500825 continue;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -0400826 lpfc_disc_state_machine(vport, ndlp, NULL,
827 remove
828 ? NLP_EVT_DEVICE_RM
829 : NLP_EVT_DEVICE_RECOVERY);
James Smart92d7f7b2007-06-17 19:56:38 -0500830 }
831 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400832 if (phba->sli_rev == LPFC_SLI_REV4)
833 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500834 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500835 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500836 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500837 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500838 }
839}
840
James Smart87af33f2007-10-27 13:37:43 -0400841void
James Smart98c9ea52007-10-27 13:37:33 -0400842lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500843{
James Smart695a8142010-01-26 23:08:03 -0500844 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
845
James Smart45ed1192009-10-02 15:17:02 -0400846 /* Cleanup any outstanding received buffers */
847 lpfc_cleanup_rcv_buffers(vport);
848
James Smart92d7f7b2007-06-17 19:56:38 -0500849 /* Cleanup any outstanding RSCN activity */
850 lpfc_els_flush_rscn(vport);
851
852 /* Cleanup any outstanding ELS commands */
853 lpfc_els_flush_cmd(vport);
854
855 lpfc_cleanup_rpis(vport, 0);
856
James Smart92d7f7b2007-06-17 19:56:38 -0500857 /* Turn off discovery timer if its running */
858 lpfc_can_disctmo(vport);
859}
860
James Smart3772a992009-05-22 14:50:54 -0400861void
James Smart98c9ea52007-10-27 13:37:33 -0400862lpfc_linkdown_port(struct lpfc_vport *vport)
863{
James Smarta0f2d3e2017-02-12 13:52:31 -0800864 struct lpfc_hba *phba = vport->phba;
James Smart98c9ea52007-10-27 13:37:33 -0400865 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
866
James Smarta0f2d3e2017-02-12 13:52:31 -0800867 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
868 fc_host_post_event(shost, fc_get_event_number(),
869 FCH_EVT_LINKDOWN, 0);
James Smart98c9ea52007-10-27 13:37:33 -0400870
871 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
872 "Link Down: state:x%x rtry:x%x flg:x%x",
873 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
874
875 lpfc_port_link_failure(vport);
876
James Smart92494142011-02-16 12:39:44 -0500877 /* Stop delayed Nport discovery */
878 spin_lock_irq(shost->host_lock);
879 vport->fc_flag &= ~FC_DISC_DELAYED;
880 spin_unlock_irq(shost->host_lock);
881 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400882}
883
dea31012005-04-17 16:05:31 -0500884int
James Smart685f0bf2007-04-25 09:53:08 -0400885lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500886{
James Smart2e0fef82007-06-17 19:56:36 -0500887 struct lpfc_vport *vport = phba->pport;
888 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400889 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400890 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400891 int i;
dea31012005-04-17 16:05:31 -0500892
James Smart3163f722008-02-08 18:50:25 -0500893 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500894 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500895
896 /* Block all SCSI stack I/Os */
897 lpfc_scsi_dev_block(phba);
898
James Smart2e0fef82007-06-17 19:56:36 -0500899 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500900 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500901 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500902 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500903 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500904 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500905 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500906 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500907 }
James Smart549e55c2007-08-02 11:09:51 -0400908 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -0800909 if (vports != NULL) {
James Smart6fb120a2009-05-22 14:52:59 -0400910 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400911 /* Issue a LINK DOWN event to all nodes */
912 lpfc_linkdown_port(vports[i]);
James Smart895427b2017-02-12 13:52:30 -0800913
914 vports[i]->fc_myDID = 0;
915
James Smart01649562017-02-12 13:52:32 -0800916 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -0800917 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -0800918 if (phba->nvmet_support)
919 lpfc_nvmet_update_targetport(phba);
920 else
James Smart8c258642017-02-12 13:52:36 -0800921 lpfc_nvme_update_localport(vports[i]);
James Smart8c258642017-02-12 13:52:36 -0800922 }
James Smart549e55c2007-08-02 11:09:51 -0400923 }
James Smart895427b2017-02-12 13:52:30 -0800924 }
James Smart09372822008-01-11 01:52:54 -0500925 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500926 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500927 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
928 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400929 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500930 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500931 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400932 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500933 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500934 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500935 }
936 }
937
dea31012005-04-17 16:05:31 -0500938 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500939 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500940 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
941 if (mb) {
dea31012005-04-17 16:05:31 -0500942 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500943 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500944 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400945 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500946 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500947 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500948 }
949 }
James Smart2e0fef82007-06-17 19:56:36 -0500950 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500951 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500952 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500953 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500954 return 0;
dea31012005-04-17 16:05:31 -0500955}
956
James Smart92d7f7b2007-06-17 19:56:38 -0500957static void
958lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500959{
James Smart92d7f7b2007-06-17 19:56:38 -0500960 struct lpfc_nodelist *ndlp;
961
962 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
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 Smart92d7f7b2007-06-17 19:56:38 -05001022
1023 phba->link_state = LPFC_LINK_UP;
1024
1025 /* Unblock fabric iocbs if they are blocked */
1026 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1027 del_timer_sync(&phba->fabric_block_timer);
1028
James Smart549e55c2007-08-02 11:09:51 -04001029 vports = lpfc_create_vport_work_array(phba);
1030 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001031 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001032 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001033 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001034
1035 return 0;
1036}
1037
1038/*
1039 * This routine handles processing a CLEAR_LA mailbox
1040 * command upon completion. It is setup in the LPFC_MBOXQ
1041 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001042 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001043 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001044static void
James Smart2e0fef82007-06-17 19:56:36 -05001045lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001046{
James Smart2e0fef82007-06-17 19:56:36 -05001047 struct lpfc_vport *vport = pmb->vport;
1048 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1049 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001050 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001051 uint32_t control;
1052
dea31012005-04-17 16:05:31 -05001053 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001054 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1055 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001056
1057 /* Check for error */
1058 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001059 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001060 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1061 "0320 CLEAR_LA mbxStatus error x%x hba "
1062 "state x%x\n",
1063 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001064 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001065 goto out;
1066 }
1067
James Smart92d7f7b2007-06-17 19:56:38 -05001068 if (vport->port_type == LPFC_PHYSICAL_PORT)
1069 phba->link_state = LPFC_HBA_READY;
1070
1071 spin_lock_irq(&phba->hbalock);
1072 psli->sli_flag |= LPFC_PROCESS_LA;
1073 control = readl(phba->HCregaddr);
1074 control |= HC_LAINT_ENA;
1075 writel(control, phba->HCregaddr);
1076 readl(phba->HCregaddr); /* flush */
1077 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001078 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001079 return;
dea31012005-04-17 16:05:31 -05001080
dea31012005-04-17 16:05:31 -05001081out:
1082 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001083 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1084 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001085 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001086
James Smart2e0fef82007-06-17 19:56:36 -05001087 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001088 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001089 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001090
James Smart2e0fef82007-06-17 19:56:36 -05001091 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001092
1093 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001094
1095 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001096 psli->sli_flag |= LPFC_PROCESS_LA;
1097 control = readl(phba->HCregaddr);
1098 control |= HC_LAINT_ENA;
1099 writel(control, phba->HCregaddr);
1100 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001101 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001102
1103 return;
1104}
1105
James Smart2e0fef82007-06-17 19:56:36 -05001106
James Smartd6de08c2015-12-16 18:11:53 -05001107void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001108lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001109{
James Smart2e0fef82007-06-17 19:56:36 -05001110 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001111
James Smart04c68492009-05-22 14:52:52 -04001112 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001113 goto out;
dea31012005-04-17 16:05:31 -05001114
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001115 mempool_free(pmb, phba->mbox_mem_pool);
1116
James Smart1b511972011-12-13 13:23:09 -05001117 /* don't perform discovery for SLI4 loopback diagnostic test */
1118 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1119 !(phba->hba_flag & HBA_FCOE_MODE) &&
1120 (phba->link_flag & LS_LOOPBACK_MODE))
1121 return;
1122
James Smart76a95d72010-11-20 23:11:48 -05001123 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001124 vport->fc_flag & FC_PUBLIC_LOOP &&
1125 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001126 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001127 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001128 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1129 */
James Smart2e0fef82007-06-17 19:56:36 -05001130 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001131 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001132 }
dea31012005-04-17 16:05:31 -05001133
James Smart2e0fef82007-06-17 19:56:36 -05001134 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001135 * LPFC_FLOGI while waiting for FLOGI cmpl
1136 */
James Smartd6de08c2015-12-16 18:11:53 -05001137 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001138 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001139 else if (vport->fc_flag & FC_PT2PT)
1140 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001141 return;
dea31012005-04-17 16:05:31 -05001142
1143out:
James Smarte8b62012007-08-02 11:10:09 -04001144 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1145 "0306 CONFIG_LINK mbxStatus error x%x "
1146 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001147 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001148 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001149
1150 lpfc_linkdown(phba);
1151
James Smarte8b62012007-08-02 11:10:09 -04001152 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1153 "0200 CONFIG_LINK bad hba state x%x\n",
1154 vport->port_state);
dea31012005-04-17 16:05:31 -05001155
James Smart92d7f7b2007-06-17 19:56:38 -05001156 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001157 return;
1158}
1159
James Smart7d791df2011-07-22 18:37:52 -04001160/**
1161 * lpfc_sli4_clear_fcf_rr_bmask
1162 * @phba pointer to the struct lpfc_hba for this port.
1163 * This fucnction resets the round robin bit mask and clears the
1164 * fcf priority list. The list deletions are done while holding the
1165 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1166 * from the lpfc_fcf_pri record.
1167 **/
1168void
1169lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1170{
1171 struct lpfc_fcf_pri *fcf_pri;
1172 struct lpfc_fcf_pri *next_fcf_pri;
1173 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1174 spin_lock_irq(&phba->hbalock);
1175 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1176 &phba->fcf.fcf_pri_list, list) {
1177 list_del_init(&fcf_pri->list);
1178 fcf_pri->fcf_rec.flag = 0;
1179 }
1180 spin_unlock_irq(&phba->hbalock);
1181}
dea31012005-04-17 16:05:31 -05001182static void
James Smart6fb120a2009-05-22 14:52:59 -04001183lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1184{
1185 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001186
1187 if (mboxq->u.mb.mbxStatus) {
1188 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1189 "2017 REG_FCFI mbxStatus error x%x "
1190 "HBA state x%x\n",
1191 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001192 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001193 }
1194
1195 /* Start FCoE discovery by sending a FLOGI. */
1196 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1197 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001198 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001199 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001200 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001201
James Smart32b97932009-07-19 10:01:21 -04001202 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001203 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1204 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001205 goto fail_out;
1206
1207 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001208 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001209 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001210 phba->hba_flag &= ~FCF_TS_INPROG;
1211 if (vport->port_state != LPFC_FLOGI) {
1212 phba->hba_flag |= FCF_RR_INPROG;
1213 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001214 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001215 goto out;
1216 }
1217 spin_unlock_irq(&phba->hbalock);
1218 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001219
James Smarta93ff372010-10-22 11:06:08 -04001220fail_out:
1221 spin_lock_irq(&phba->hbalock);
1222 phba->hba_flag &= ~FCF_RR_INPROG;
1223 spin_unlock_irq(&phba->hbalock);
1224out:
James Smart6fb120a2009-05-22 14:52:59 -04001225 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001226}
1227
1228/**
1229 * lpfc_fab_name_match - Check if the fcf fabric name match.
1230 * @fab_name: pointer to fabric name.
1231 * @new_fcf_record: pointer to fcf record.
1232 *
1233 * This routine compare the fcf record's fabric name with provided
1234 * fabric name. If the fabric name are identical this function
1235 * returns 1 else return 0.
1236 **/
1237static uint32_t
1238lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1239{
James Smartecfd03c2010-02-12 14:41:27 -05001240 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001241 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001242 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1243 return 0;
1244 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1245 return 0;
1246 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1247 return 0;
1248 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1249 return 0;
1250 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1251 return 0;
1252 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1253 return 0;
1254 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1255 return 0;
1256 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001257}
1258
1259/**
James Smart8fa38512009-07-19 10:01:03 -04001260 * lpfc_sw_name_match - Check if the fcf switch name match.
1261 * @fab_name: pointer to fabric name.
1262 * @new_fcf_record: pointer to fcf record.
1263 *
1264 * This routine compare the fcf record's switch name with provided
1265 * switch name. If the switch name are identical this function
1266 * returns 1 else return 0.
1267 **/
1268static uint32_t
1269lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1270{
James Smartecfd03c2010-02-12 14:41:27 -05001271 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001272 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001273 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1274 return 0;
1275 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1276 return 0;
1277 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1278 return 0;
1279 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1280 return 0;
1281 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1282 return 0;
1283 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1284 return 0;
1285 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1286 return 0;
1287 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001288}
1289
1290/**
James Smart6fb120a2009-05-22 14:52:59 -04001291 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001292 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001293 * @new_fcf_record: pointer to fcf record.
1294 *
1295 * This routine compare the fcf record's mac address with HBA's
1296 * FCF mac address. If the mac addresses are identical this function
1297 * returns 1 else return 0.
1298 **/
1299static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001300lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001301{
James Smartecfd03c2010-02-12 14:41:27 -05001302 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001303 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001304 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1305 return 0;
1306 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1307 return 0;
1308 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1309 return 0;
1310 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1311 return 0;
1312 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1313 return 0;
1314 return 1;
1315}
1316
1317static bool
1318lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1319{
1320 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001321}
1322
1323/**
James Smart7d791df2011-07-22 18:37:52 -04001324 * lpfc_update_fcf_record - Update driver fcf record
1325 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1326 * @phba: pointer to lpfc hba data structure.
1327 * @fcf_index: Index for the lpfc_fcf_record.
1328 * @new_fcf_record: pointer to hba fcf record.
1329 *
1330 * This routine updates the driver FCF priority record from the new HBA FCF
1331 * record. This routine is called with the host lock held.
1332 **/
1333static void
1334__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1335 struct fcf_record *new_fcf_record
1336 )
1337{
1338 struct lpfc_fcf_pri *fcf_pri;
1339
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001340 lockdep_assert_held(&phba->hbalock);
1341
James Smart7d791df2011-07-22 18:37:52 -04001342 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1343 fcf_pri->fcf_rec.fcf_index = fcf_index;
1344 /* FCF record priority */
1345 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1346
1347}
1348
1349/**
James Smart6fb120a2009-05-22 14:52:59 -04001350 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001351 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001352 * @new_fcf_record: pointer to fcf record.
1353 *
1354 * This routine copies the FCF information from the FCF
1355 * record to lpfc_hba data structure.
1356 **/
1357static void
James Smartecfd03c2010-02-12 14:41:27 -05001358lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1359 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001360{
James Smartecfd03c2010-02-12 14:41:27 -05001361 /* Fabric name */
1362 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001363 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001364 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001365 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001367 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001369 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001370 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001371 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001373 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001375 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001376 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001377 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001378 /* Mac address */
1379 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1380 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1381 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1382 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1383 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1384 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1385 /* FCF record index */
1386 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1387 /* FCF record priority */
1388 fcf_rec->priority = new_fcf_record->fip_priority;
1389 /* Switch name */
1390 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001391 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001392 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001393 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001395 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001396 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001397 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001398 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001399 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001400 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001401 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001402 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001403 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001404 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001405 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001406}
1407
1408/**
James Smartecfd03c2010-02-12 14:41:27 -05001409 * lpfc_update_fcf_record - Update driver fcf record
1410 * @phba: pointer to lpfc hba data structure.
1411 * @fcf_rec: pointer to driver fcf record.
1412 * @new_fcf_record: pointer to hba fcf record.
1413 * @addr_mode: address mode to be set to the driver fcf record.
1414 * @vlan_id: vlan tag to be set to the driver fcf record.
1415 * @flag: flag bits to be set to the driver fcf record.
1416 *
1417 * This routine updates the driver FCF record from the new HBA FCF record
1418 * together with the address mode, vlan_id, and other informations. This
1419 * routine is called with the host lock held.
1420 **/
1421static void
1422__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1423 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1424 uint16_t vlan_id, uint32_t flag)
1425{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001426 lockdep_assert_held(&phba->hbalock);
1427
James Smartecfd03c2010-02-12 14:41:27 -05001428 /* Copy the fields from the HBA's FCF record */
1429 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1430 /* Update other fields of driver FCF record */
1431 fcf_rec->addr_mode = addr_mode;
1432 fcf_rec->vlan_id = vlan_id;
1433 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001434 __lpfc_update_fcf_record_pri(phba,
1435 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1436 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001437}
1438
1439/**
James Smart6fb120a2009-05-22 14:52:59 -04001440 * lpfc_register_fcf - Register the FCF with hba.
1441 * @phba: pointer to lpfc hba data structure.
1442 *
1443 * This routine issues a register fcfi mailbox command to register
1444 * the fcf with HBA.
1445 **/
1446static void
1447lpfc_register_fcf(struct lpfc_hba *phba)
1448{
1449 LPFC_MBOXQ_t *fcf_mbxq;
1450 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001451
James Smartd439d282010-09-29 11:18:45 -04001452 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001453 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001454 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001455 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001456 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001457 return;
1458 }
1459
1460 /* The FCF is already registered, start discovery */
1461 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001462 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001463 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001464 if (phba->pport->port_state != LPFC_FLOGI &&
1465 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001466 phba->hba_flag |= FCF_RR_INPROG;
1467 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001468 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001469 return;
1470 }
1471 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001472 return;
1473 }
James Smartd439d282010-09-29 11:18:45 -04001474 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001475
James Smarta93ff372010-10-22 11:06:08 -04001476 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001477 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001478 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001479 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001480 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001481 return;
James Smart4d9ab992009-10-02 15:16:39 -04001482 }
James Smart6fb120a2009-05-22 14:52:59 -04001483
1484 lpfc_reg_fcfi(phba, fcf_mbxq);
1485 fcf_mbxq->vport = phba->pport;
1486 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1487 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001488 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001489 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001490 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001491 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001492 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001493 }
James Smart6fb120a2009-05-22 14:52:59 -04001494
1495 return;
1496}
1497
1498/**
1499 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1500 * @phba: pointer to lpfc hba data structure.
1501 * @new_fcf_record: pointer to fcf record.
1502 * @boot_flag: Indicates if this record used by boot bios.
1503 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001504 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001505 *
1506 * This routine compare the fcf record with connect list obtained from the
1507 * config region to decide if this FCF can be used for SAN discovery. It returns
1508 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1509 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1510 * is used by boot bios and addr_mode will indicate the addressing mode to be
1511 * used for this FCF when the function returns.
1512 * If the FCF record need to be used with a particular vlan id, the vlan is
1513 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001514 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001515 **/
1516static int
1517lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1518 struct fcf_record *new_fcf_record,
1519 uint32_t *boot_flag, uint32_t *addr_mode,
1520 uint16_t *vlan_id)
1521{
1522 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001523 int i, j, fcf_vlan_id = 0;
1524
1525 /* Find the lowest VLAN id in the FCF record */
1526 for (i = 0; i < 512; i++) {
1527 if (new_fcf_record->vlan_bitmap[i]) {
1528 fcf_vlan_id = i * 8;
1529 j = 0;
1530 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1531 j++;
1532 fcf_vlan_id++;
1533 }
1534 break;
1535 }
1536 }
James Smart6fb120a2009-05-22 14:52:59 -04001537
James Smart26979ce2012-09-29 11:31:55 -04001538 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001539 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001540 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1541 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001542 return 0;
1543
James Smart45ed1192009-10-02 15:17:02 -04001544 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001545 *boot_flag = 0;
1546 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1547 new_fcf_record);
1548 if (phba->valid_vlan)
1549 *vlan_id = phba->vlan_id;
1550 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001551 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001552 return 1;
1553 }
1554
1555 /*
1556 * If there are no FCF connection table entry, driver connect to all
1557 * FCFs.
1558 */
1559 if (list_empty(&phba->fcf_conn_rec_list)) {
1560 *boot_flag = 0;
1561 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1562 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001563
1564 /*
1565 * When there are no FCF connect entries, use driver's default
1566 * addressing mode - FPMA.
1567 */
1568 if (*addr_mode & LPFC_FCF_FPMA)
1569 *addr_mode = LPFC_FCF_FPMA;
1570
James Smart4d9ab992009-10-02 15:16:39 -04001571 /* If FCF record report a vlan id use that vlan id */
1572 if (fcf_vlan_id)
1573 *vlan_id = fcf_vlan_id;
1574 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001575 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001576 return 1;
1577 }
1578
James Smartecfd03c2010-02-12 14:41:27 -05001579 list_for_each_entry(conn_entry,
1580 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001581 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1582 continue;
1583
1584 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1585 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001586 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001587 continue;
James Smart8fa38512009-07-19 10:01:03 -04001588 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1589 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1590 new_fcf_record))
1591 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001592 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1593 /*
1594 * If the vlan bit map does not have the bit set for the
1595 * vlan id to be used, then it is not a match.
1596 */
1597 if (!(new_fcf_record->vlan_bitmap
1598 [conn_entry->conn_rec.vlan_tag / 8] &
1599 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1600 continue;
1601 }
1602
1603 /*
James Smart0c287582009-06-10 17:22:56 -04001604 * If connection record does not support any addressing mode,
1605 * skip the FCF record.
1606 */
1607 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1608 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1609 continue;
1610
1611 /*
James Smart6fb120a2009-05-22 14:52:59 -04001612 * Check if the connection record specifies a required
1613 * addressing mode.
1614 */
1615 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1616 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1617
1618 /*
1619 * If SPMA required but FCF not support this continue.
1620 */
1621 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1622 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1623 new_fcf_record) & LPFC_FCF_SPMA))
1624 continue;
1625
1626 /*
1627 * If FPMA required but FCF not support this continue.
1628 */
1629 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1630 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1631 new_fcf_record) & LPFC_FCF_FPMA))
1632 continue;
1633 }
1634
1635 /*
1636 * This fcf record matches filtering criteria.
1637 */
1638 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1639 *boot_flag = 1;
1640 else
1641 *boot_flag = 0;
1642
James Smart0c287582009-06-10 17:22:56 -04001643 /*
1644 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001645 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001646 * by FCF, allow fabric to pick the addressing mode.
1647 */
James Smart6fb120a2009-05-22 14:52:59 -04001648 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1649 new_fcf_record);
1650 /*
1651 * If the user specified a required address mode, assign that
1652 * address mode
1653 */
1654 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1655 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1656 *addr_mode = (conn_entry->conn_rec.flags &
1657 FCFCNCT_AM_SPMA) ?
1658 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1659 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001660 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001661 * addr mode only if FCF support the addr_mode.
1662 */
1663 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1664 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1665 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1666 (*addr_mode & LPFC_FCF_SPMA))
1667 *addr_mode = LPFC_FCF_SPMA;
1668 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1669 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1670 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1671 (*addr_mode & LPFC_FCF_FPMA))
1672 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001673
James Smart4d9ab992009-10-02 15:16:39 -04001674 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001675 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1676 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001677 /*
1678 * If no vlan id is specified in connect list, use the vlan id
1679 * in the FCF record
1680 */
1681 else if (fcf_vlan_id)
1682 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001683 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001684 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001685
1686 return 1;
1687 }
1688
1689 return 0;
1690}
1691
1692/**
James Smart32b97932009-07-19 10:01:21 -04001693 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1694 * @phba: pointer to lpfc hba data structure.
1695 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1696 *
1697 * This function check if there is any fcoe event pending while driver
1698 * scan FCF entries. If there is any pending event, it will restart the
1699 * FCF saning and return 1 else return 0.
1700 */
1701int
1702lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1703{
James Smart32b97932009-07-19 10:01:21 -04001704 /*
1705 * If the Link is up and no FCoE events while in the
1706 * FCF discovery, no need to restart FCF discovery.
1707 */
1708 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001709 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001710 return 0;
1711
James Smart0c9ab6f2010-02-26 14:15:57 -05001712 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1713 "2768 Pending link or FCF event during current "
1714 "handling of the previous event: link_state:x%x, "
1715 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1716 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1717 phba->fcoe_eventtag);
1718
James Smart32b97932009-07-19 10:01:21 -04001719 spin_lock_irq(&phba->hbalock);
1720 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1721 spin_unlock_irq(&phba->hbalock);
1722
James Smart0c9ab6f2010-02-26 14:15:57 -05001723 if (phba->link_state >= LPFC_LINK_UP) {
1724 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1725 "2780 Restart FCF table scan due to "
1726 "pending FCF event:evt_tag_at_scan:x%x, "
1727 "evt_tag_current:x%x\n",
1728 phba->fcoe_eventtag_at_fcf_scan,
1729 phba->fcoe_eventtag);
1730 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1731 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001732 /*
James Smarta93ff372010-10-22 11:06:08 -04001733 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001734 * flag
1735 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001736 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1737 "2833 Stop FCF discovery process due to link "
1738 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001739 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001740 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001741 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001742 spin_unlock_irq(&phba->hbalock);
1743 }
James Smart32b97932009-07-19 10:01:21 -04001744
James Smart0c9ab6f2010-02-26 14:15:57 -05001745 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001746 if (unreg_fcf) {
1747 spin_lock_irq(&phba->hbalock);
1748 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1749 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001750 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001751 }
James Smart32b97932009-07-19 10:01:21 -04001752 return 1;
1753}
1754
1755/**
James Smart999d8132010-03-15 11:24:56 -04001756 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1757 * @phba: pointer to lpfc hba data structure.
1758 * @fcf_cnt: number of eligible fcf record seen so far.
1759 *
1760 * This function makes an running random selection decision on FCF record to
1761 * use through a sequence of @fcf_cnt eligible FCF records with equal
1762 * probability. To perform integer manunipulation of random numbers with
1763 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001764 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001765 *
1766 * Returns true when outcome is for the newly read FCF record should be
1767 * chosen; otherwise, return false when outcome is for keeping the previously
1768 * chosen FCF record.
1769 **/
1770static bool
1771lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1772{
1773 uint32_t rand_num;
1774
1775 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001776 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001777
1778 /* Decision with probability 1/fcf_cnt */
1779 if ((fcf_cnt * rand_num) < 0xFFFF)
1780 return true;
1781 else
1782 return false;
1783}
1784
1785/**
James Smart3804dc82010-07-14 15:31:37 -04001786 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001787 * @phba: pointer to lpfc hba data structure.
1788 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001789 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001790 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001791 * This routine parses the non-embedded fcf mailbox command by performing the
1792 * necessarily error checking, non-embedded read FCF record mailbox command
1793 * SGE parsing, and endianness swapping.
1794 *
1795 * Returns the pointer to the new FCF record in the non-embedded mailbox
1796 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001797 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001798static struct fcf_record *
1799lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1800 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001801{
1802 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001803 struct lpfc_mbx_sge sge;
1804 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001805 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001806 union lpfc_sli4_cfg_shdr *shdr;
1807 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001808
James Smart6fb120a2009-05-22 14:52:59 -04001809 /* Get the first SGE entry from the non-embedded DMA memory. This
1810 * routine only uses a single SGE.
1811 */
1812 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001813 if (unlikely(!mboxq->sge_array)) {
1814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1815 "2524 Failed to get the non-embedded SGE "
1816 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001817 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001818 }
1819 virt_addr = mboxq->sge_array->addr[0];
1820
1821 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001822 lpfc_sli_pcimem_bcopy(shdr, shdr,
1823 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001824 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001825 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001826 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001827 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001828 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1829 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001830 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001831 "2726 READ_FCF_RECORD Indicates empty "
1832 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001833 else
1834 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001835 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001836 "with status x%x add_status x%x, "
1837 "mbx\n", shdr_status, shdr_add_status);
1838 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001839 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001840
1841 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001842 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1843 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1844 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001845 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001846 new_fcf_record = (struct fcf_record *)(virt_addr +
1847 sizeof(struct lpfc_mbx_read_fcf_tbl));
1848 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001849 offsetof(struct fcf_record, vlan_bitmap));
1850 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1851 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001852
James Smart0c9ab6f2010-02-26 14:15:57 -05001853 return new_fcf_record;
1854}
1855
1856/**
1857 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1858 * @phba: pointer to lpfc hba data structure.
1859 * @fcf_record: pointer to the fcf record.
1860 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1861 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1862 *
1863 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1864 * enabled.
1865 **/
1866static void
1867lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1868 struct fcf_record *fcf_record,
1869 uint16_t vlan_id,
1870 uint16_t next_fcf_index)
1871{
1872 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1873 "2764 READ_FCF_RECORD:\n"
1874 "\tFCF_Index : x%x\n"
1875 "\tFCF_Avail : x%x\n"
1876 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001877 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001878 "\tFIP_Priority : x%x\n"
1879 "\tMAC_Provider : x%x\n"
1880 "\tLowest VLANID : x%x\n"
1881 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1882 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1883 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1884 "\tNext_FCF_Index: x%x\n",
1885 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1886 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1887 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001888 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001889 fcf_record->fip_priority,
1890 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1891 vlan_id,
1892 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1893 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1894 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1895 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1896 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1897 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1901 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1902 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1903 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1904 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1905 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1909 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1910 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1911 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1912 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1913 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1914 next_fcf_index);
1915}
1916
1917/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001918 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1919 * @phba: pointer to lpfc hba data structure.
1920 * @fcf_rec: pointer to an existing FCF record.
1921 * @new_fcf_record: pointer to a new FCF record.
1922 * @new_vlan_id: vlan id from the new FCF record.
1923 *
1924 * This function performs matching test of a new FCF record against an existing
1925 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1926 * will not be used as part of the FCF record matching criteria.
1927 *
1928 * Returns true if all the fields matching, otherwise returns false.
1929 */
1930static bool
1931lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1932 struct lpfc_fcf_rec *fcf_rec,
1933 struct fcf_record *new_fcf_record,
1934 uint16_t new_vlan_id)
1935{
1936 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1937 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1938 return false;
1939 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1940 return false;
1941 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1942 return false;
1943 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1944 return false;
James Smart7d791df2011-07-22 18:37:52 -04001945 if (fcf_rec->priority != new_fcf_record->fip_priority)
1946 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001947 return true;
1948}
1949
1950/**
James Smarta93ff372010-10-22 11:06:08 -04001951 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1952 * @vport: Pointer to vport object.
1953 * @fcf_index: index to next fcf.
1954 *
1955 * This function processing the roundrobin fcf failover to next fcf index.
1956 * When this function is invoked, there will be a current fcf registered
1957 * for flogi.
1958 * Return: 0 for continue retrying flogi on currently registered fcf;
1959 * 1 for stop flogi on currently registered fcf;
1960 */
1961int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1962{
1963 struct lpfc_hba *phba = vport->phba;
1964 int rc;
1965
1966 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1967 spin_lock_irq(&phba->hbalock);
1968 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1969 spin_unlock_irq(&phba->hbalock);
1970 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1971 "2872 Devloss tmo with no eligible "
1972 "FCF, unregister in-use FCF (x%x) "
1973 "and rescan FCF table\n",
1974 phba->fcf.current_rec.fcf_indx);
1975 lpfc_unregister_fcf_rescan(phba);
1976 goto stop_flogi_current_fcf;
1977 }
1978 /* Mark the end to FLOGI roundrobin failover */
1979 phba->hba_flag &= ~FCF_RR_INPROG;
1980 /* Allow action to new fcf asynchronous event */
1981 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1982 spin_unlock_irq(&phba->hbalock);
1983 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1984 "2865 No FCF available, stop roundrobin FCF "
1985 "failover and change port state:x%x/x%x\n",
1986 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1987 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1988 goto stop_flogi_current_fcf;
1989 } else {
1990 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1991 "2794 Try FLOGI roundrobin FCF failover to "
1992 "(x%x)\n", fcf_index);
1993 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1994 if (rc)
1995 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1996 "2761 FLOGI roundrobin FCF failover "
1997 "failed (rc:x%x) to read FCF (x%x)\n",
1998 rc, phba->fcf.current_rec.fcf_indx);
1999 else
2000 goto stop_flogi_current_fcf;
2001 }
2002 return 0;
2003
2004stop_flogi_current_fcf:
2005 lpfc_can_disctmo(vport);
2006 return 1;
2007}
2008
2009/**
James Smart7d791df2011-07-22 18:37:52 -04002010 * lpfc_sli4_fcf_pri_list_del
2011 * @phba: pointer to lpfc hba data structure.
2012 * @fcf_index the index of the fcf record to delete
2013 * This routine checks the on list flag of the fcf_index to be deleted.
2014 * If it is one the list then it is removed from the list, and the flag
2015 * is cleared. This routine grab the hbalock before removing the fcf
2016 * record from the list.
2017 **/
2018static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2019 uint16_t fcf_index)
2020{
2021 struct lpfc_fcf_pri *new_fcf_pri;
2022
2023 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2024 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2025 "3058 deleting idx x%x pri x%x flg x%x\n",
2026 fcf_index, new_fcf_pri->fcf_rec.priority,
2027 new_fcf_pri->fcf_rec.flag);
2028 spin_lock_irq(&phba->hbalock);
2029 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2030 if (phba->fcf.current_rec.priority ==
2031 new_fcf_pri->fcf_rec.priority)
2032 phba->fcf.eligible_fcf_cnt--;
2033 list_del_init(&new_fcf_pri->list);
2034 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2035 }
2036 spin_unlock_irq(&phba->hbalock);
2037}
2038
2039/**
2040 * lpfc_sli4_set_fcf_flogi_fail
2041 * @phba: pointer to lpfc hba data structure.
2042 * @fcf_index the index of the fcf record to update
2043 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2044 * flag so the the round robin slection for the particular priority level
2045 * will try a different fcf record that does not have this bit set.
2046 * If the fcf record is re-read for any reason this flag is cleared brfore
2047 * adding it to the priority list.
2048 **/
2049void
2050lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2051{
2052 struct lpfc_fcf_pri *new_fcf_pri;
2053 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2054 spin_lock_irq(&phba->hbalock);
2055 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2056 spin_unlock_irq(&phba->hbalock);
2057}
2058
2059/**
2060 * lpfc_sli4_fcf_pri_list_add
2061 * @phba: pointer to lpfc hba data structure.
2062 * @fcf_index the index of the fcf record to add
2063 * This routine checks the priority of the fcf_index to be added.
2064 * If it is a lower priority than the current head of the fcf_pri list
2065 * then it is added to the list in the right order.
2066 * If it is the same priority as the current head of the list then it
2067 * is added to the head of the list and its bit in the rr_bmask is set.
2068 * If the fcf_index to be added is of a higher priority than the current
2069 * head of the list then the rr_bmask is cleared, its bit is set in the
2070 * rr_bmask and it is added to the head of the list.
2071 * returns:
2072 * 0=success 1=failure
2073 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002074static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2075 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002076 struct fcf_record *new_fcf_record)
2077{
2078 uint16_t current_fcf_pri;
2079 uint16_t last_index;
2080 struct lpfc_fcf_pri *fcf_pri;
2081 struct lpfc_fcf_pri *next_fcf_pri;
2082 struct lpfc_fcf_pri *new_fcf_pri;
2083 int ret;
2084
2085 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2086 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2087 "3059 adding idx x%x pri x%x flg x%x\n",
2088 fcf_index, new_fcf_record->fip_priority,
2089 new_fcf_pri->fcf_rec.flag);
2090 spin_lock_irq(&phba->hbalock);
2091 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2092 list_del_init(&new_fcf_pri->list);
2093 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2094 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2095 if (list_empty(&phba->fcf.fcf_pri_list)) {
2096 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2097 ret = lpfc_sli4_fcf_rr_index_set(phba,
2098 new_fcf_pri->fcf_rec.fcf_index);
2099 goto out;
2100 }
2101
2102 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2103 LPFC_SLI4_FCF_TBL_INDX_MAX);
2104 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2105 ret = 0; /* Empty rr list */
2106 goto out;
2107 }
2108 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2109 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2110 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2111 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2112 memset(phba->fcf.fcf_rr_bmask, 0,
2113 sizeof(*phba->fcf.fcf_rr_bmask));
2114 /* fcfs_at_this_priority_level = 1; */
2115 phba->fcf.eligible_fcf_cnt = 1;
2116 } else
2117 /* fcfs_at_this_priority_level++; */
2118 phba->fcf.eligible_fcf_cnt++;
2119 ret = lpfc_sli4_fcf_rr_index_set(phba,
2120 new_fcf_pri->fcf_rec.fcf_index);
2121 goto out;
2122 }
2123
2124 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2125 &phba->fcf.fcf_pri_list, list) {
2126 if (new_fcf_pri->fcf_rec.priority <=
2127 fcf_pri->fcf_rec.priority) {
2128 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2129 list_add(&new_fcf_pri->list,
2130 &phba->fcf.fcf_pri_list);
2131 else
2132 list_add(&new_fcf_pri->list,
2133 &((struct lpfc_fcf_pri *)
2134 fcf_pri->list.prev)->list);
2135 ret = 0;
2136 goto out;
2137 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2138 || new_fcf_pri->fcf_rec.priority <
2139 next_fcf_pri->fcf_rec.priority) {
2140 list_add(&new_fcf_pri->list, &fcf_pri->list);
2141 ret = 0;
2142 goto out;
2143 }
2144 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2145 continue;
2146
2147 }
2148 ret = 1;
2149out:
2150 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2151 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2152 spin_unlock_irq(&phba->hbalock);
2153 return ret;
2154}
2155
2156/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002157 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2158 * @phba: pointer to lpfc hba data structure.
2159 * @mboxq: pointer to mailbox object.
2160 *
2161 * This function iterates through all the fcf records available in
2162 * HBA and chooses the optimal FCF record for discovery. After finding
2163 * the FCF for discovery it registers the FCF record and kicks start
2164 * discovery.
2165 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2166 * use an FCF record which matches fabric name and mac address of the
2167 * currently used FCF record.
2168 * If the driver supports only one FCF, it will try to use the FCF record
2169 * used by BOOT_BIOS.
2170 */
2171void
2172lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2173{
2174 struct fcf_record *new_fcf_record;
2175 uint32_t boot_flag, addr_mode;
2176 uint16_t fcf_index, next_fcf_index;
2177 struct lpfc_fcf_rec *fcf_rec = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08002178 uint16_t vlan_id = LPFC_FCOE_NULL_VID;
James Smart999d8132010-03-15 11:24:56 -04002179 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002180 int rc;
2181
2182 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002183 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002184 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2185 return;
2186 }
2187
2188 /* Parse the FCF record from the non-embedded mailbox command */
2189 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2190 &next_fcf_index);
2191 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002192 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002193 "2765 Mailbox command READ_FCF_RECORD "
2194 "failed to retrieve a FCF record.\n");
2195 /* Let next new FCF event trigger fast failover */
2196 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002197 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002198 spin_unlock_irq(&phba->hbalock);
2199 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2200 return;
2201 }
2202
2203 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002204 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2205 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002206
2207 /* Log the FCF record information if turned on */
2208 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2209 next_fcf_index);
2210
James Smart6fb120a2009-05-22 14:52:59 -04002211 /*
2212 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002213 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002214 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002215 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002216 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002217 lpfc_sli4_fcf_pri_list_del(phba,
2218 bf_get(lpfc_fcf_record_fcf_index,
2219 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002220 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002221 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002222 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002223 bf_get(lpfc_fcf_record_fcf_index,
2224 new_fcf_record),
2225 bf_get(lpfc_fcf_record_fcf_avail,
2226 new_fcf_record),
2227 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002228 new_fcf_record),
2229 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002230 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002231 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2232 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2233 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002234 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2235 phba->fcf.current_rec.fcf_indx) {
2236 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2237 "2862 FCF (x%x) matches property "
2238 "of in-use FCF (x%x)\n",
2239 bf_get(lpfc_fcf_record_fcf_index,
2240 new_fcf_record),
2241 phba->fcf.current_rec.fcf_indx);
2242 goto read_next_fcf;
2243 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002244 /*
2245 * In case the current in-use FCF record becomes
2246 * invalid/unavailable during FCF discovery that
2247 * was not triggered by fast FCF failover process,
2248 * treat it as fast FCF failover.
2249 */
2250 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2251 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2252 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2253 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002254 "(x%x), enter FCF failover "
2255 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002256 phba->fcf.current_rec.fcf_indx);
2257 spin_lock_irq(&phba->hbalock);
2258 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2259 spin_unlock_irq(&phba->hbalock);
2260 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2261 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2262 LPFC_FCOE_FCF_GET_FIRST);
2263 return;
2264 }
2265 }
James Smart6fb120a2009-05-22 14:52:59 -04002266 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002267 } else {
2268 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002269 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2270 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002271 if (rc)
2272 goto read_next_fcf;
2273 }
2274
James Smart6fb120a2009-05-22 14:52:59 -04002275 /*
2276 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002277 * FCF record for the discovery. The condition that a rescan
2278 * matches the in-use FCF record: fabric name, switch name, mac
2279 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002280 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002281 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002282 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002283 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2284 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002285 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002286 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2287 phba->fcf.current_rec.fcf_indx) {
2288 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2289 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2290 /* Stop FCF redisc wait timer */
2291 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2292 phba);
2293 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2294 /* Fast failover, mark completed */
2295 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2296 spin_unlock_irq(&phba->hbalock);
2297 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2298 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002299 "FCF (x%x), port_state:x%x, "
2300 "fc_flag:x%x\n",
2301 phba->fcf.current_rec.fcf_indx,
2302 phba->pport->port_state,
2303 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002304 goto out;
2305 } else
2306 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2307 "2863 New FCF (x%x) matches "
2308 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002309 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002310 new_fcf_record),
2311 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002312 }
James Smartecfd03c2010-02-12 14:41:27 -05002313 /*
2314 * Read next FCF record from HBA searching for the matching
2315 * with in-use record only if not during the fast failover
2316 * period. In case of fast failover period, it shall try to
2317 * determine whether the FCF record just read should be the
2318 * next candidate.
2319 */
2320 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002321 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002322 goto read_next_fcf;
2323 }
James Smart6fb120a2009-05-22 14:52:59 -04002324 }
James Smartecfd03c2010-02-12 14:41:27 -05002325 /*
2326 * Update on failover FCF record only if it's in FCF fast-failover
2327 * period; otherwise, update on current FCF record.
2328 */
James Smartfc2b9892010-02-26 14:15:29 -05002329 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2330 fcf_rec = &phba->fcf.failover_rec;
2331 else
James Smartecfd03c2010-02-12 14:41:27 -05002332 fcf_rec = &phba->fcf.current_rec;
2333
James Smart6fb120a2009-05-22 14:52:59 -04002334 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2335 /*
James Smartecfd03c2010-02-12 14:41:27 -05002336 * If the driver FCF record does not have boot flag
2337 * set and new hba fcf record has boot flag set, use
2338 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002339 */
James Smartecfd03c2010-02-12 14:41:27 -05002340 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2341 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002342 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2343 "2837 Update current FCF record "
2344 "(x%x) with new FCF record (x%x)\n",
2345 fcf_rec->fcf_indx,
2346 bf_get(lpfc_fcf_record_fcf_index,
2347 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002348 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2349 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002350 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002351 goto read_next_fcf;
2352 }
2353 /*
James Smartecfd03c2010-02-12 14:41:27 -05002354 * If the driver FCF record has boot flag set and the
2355 * new hba FCF record does not have boot flag, read
2356 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002357 */
James Smartecfd03c2010-02-12 14:41:27 -05002358 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002359 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002360 goto read_next_fcf;
2361 }
2362 /*
James Smartecfd03c2010-02-12 14:41:27 -05002363 * If the new hba FCF record has lower priority value
2364 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002365 */
James Smartfc2b9892010-02-26 14:15:29 -05002366 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002367 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002368 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2369 "2838 Update current FCF record "
2370 "(x%x) with new FCF record (x%x)\n",
2371 fcf_rec->fcf_indx,
2372 bf_get(lpfc_fcf_record_fcf_index,
2373 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002374 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2375 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002376 /* Reset running random FCF selection count */
2377 phba->fcf.eligible_fcf_cnt = 1;
2378 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2379 /* Update running random FCF selection count */
2380 phba->fcf.eligible_fcf_cnt++;
2381 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2382 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002383 if (select_new_fcf) {
2384 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2385 "2839 Update current FCF record "
2386 "(x%x) with new FCF record (x%x)\n",
2387 fcf_rec->fcf_indx,
2388 bf_get(lpfc_fcf_record_fcf_index,
2389 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002390 /* Choose the new FCF by random selection */
2391 __lpfc_update_fcf_record(phba, fcf_rec,
2392 new_fcf_record,
2393 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002394 }
James Smart6fb120a2009-05-22 14:52:59 -04002395 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002396 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002397 goto read_next_fcf;
2398 }
2399 /*
James Smartecfd03c2010-02-12 14:41:27 -05002400 * This is the first suitable FCF record, choose this record for
2401 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002402 */
James Smartecfd03c2010-02-12 14:41:27 -05002403 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002404 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002405 "2840 Update initial FCF candidate "
2406 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002407 bf_get(lpfc_fcf_record_fcf_index,
2408 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002409 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2410 addr_mode, vlan_id, (boot_flag ?
2411 BOOT_ENABLE : 0));
2412 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002413 /* Setup initial running random FCF selection count */
2414 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002415 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002416 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002417 goto read_next_fcf;
2418
2419read_next_fcf:
2420 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002421 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2422 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2423 /*
2424 * Case of FCF fast failover scan
2425 */
2426
2427 /*
2428 * It has not found any suitable FCF record, cancel
2429 * FCF scan inprogress, and do nothing
2430 */
2431 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002432 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002433 "2782 No suitable FCF found: "
2434 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002435 phba->fcoe_eventtag_at_fcf_scan,
2436 bf_get(lpfc_fcf_record_fcf_index,
2437 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002438 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002439 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2440 phba->hba_flag &= ~FCF_TS_INPROG;
2441 spin_unlock_irq(&phba->hbalock);
2442 /* Unregister in-use FCF and rescan */
2443 lpfc_printf_log(phba, KERN_INFO,
2444 LOG_FIP,
2445 "2864 On devloss tmo "
2446 "unreg in-use FCF and "
2447 "rescan FCF table\n");
2448 lpfc_unregister_fcf_rescan(phba);
2449 return;
2450 }
2451 /*
2452 * Let next new FCF event trigger fast failover
2453 */
2454 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002455 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002456 return;
2457 }
2458 /*
2459 * It has found a suitable FCF record that is not
2460 * the same as in-use FCF record, unregister the
2461 * in-use FCF record, replace the in-use FCF record
2462 * with the new FCF record, mark FCF fast failover
2463 * completed, and then start register the new FCF
2464 * record.
2465 */
2466
James Smart0c9ab6f2010-02-26 14:15:57 -05002467 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002468 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002469
2470 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002471 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002472 "2842 Replace in-use FCF (x%x) "
2473 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002474 phba->fcf.current_rec.fcf_indx,
2475 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002476 memcpy(&phba->fcf.current_rec,
2477 &phba->fcf.failover_rec,
2478 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002479 /*
2480 * Mark the fast FCF failover rediscovery completed
2481 * and the start of the first round of the roundrobin
2482 * FCF failover.
2483 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002484 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002485 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002486 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002487 /* Register to the new FCF record */
2488 lpfc_register_fcf(phba);
2489 } else {
2490 /*
2491 * In case of transaction period to fast FCF failover,
2492 * do nothing when search to the end of the FCF table.
2493 */
2494 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2495 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2496 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002497
James Smart7d791df2011-07-22 18:37:52 -04002498 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2499 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002500 /*
2501 * In case the current in-use FCF record no
2502 * longer existed during FCF discovery that
2503 * was not triggered by fast FCF failover
2504 * process, treat it as fast FCF failover.
2505 */
2506 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2507 "2841 In-use FCF record (x%x) "
2508 "not reported, entering fast "
2509 "FCF failover mode scanning.\n",
2510 phba->fcf.current_rec.fcf_indx);
2511 spin_lock_irq(&phba->hbalock);
2512 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2513 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002514 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2515 LPFC_FCOE_FCF_GET_FIRST);
2516 return;
2517 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002518 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002519 lpfc_register_fcf(phba);
2520 }
2521 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002522 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002523 return;
2524
2525out:
2526 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2527 lpfc_register_fcf(phba);
2528
2529 return;
2530}
2531
2532/**
James Smarta93ff372010-10-22 11:06:08 -04002533 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002534 * @phba: pointer to lpfc hba data structure.
2535 * @mboxq: pointer to mailbox object.
2536 *
James Smarta93ff372010-10-22 11:06:08 -04002537 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002538 * read FCF record mailbox command from the eligible FCF record bmask for
2539 * performing the failover. If the FCF read back is not valid/available, it
2540 * fails through to retrying FLOGI to the currently registered FCF again.
2541 * Otherwise, if the FCF read back is valid and available, it will set the
2542 * newly read FCF record to the failover FCF record, unregister currently
2543 * registered FCF record, copy the failover FCF record to the current
2544 * FCF record, and then register the current FCF record before proceeding
2545 * to trying FLOGI on the new failover FCF.
2546 */
2547void
2548lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2549{
2550 struct fcf_record *new_fcf_record;
2551 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002552 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002553 uint16_t current_fcf_index;
2554 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002555 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002556
James Smarta93ff372010-10-22 11:06:08 -04002557 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002558 if (phba->link_state < LPFC_LINK_UP) {
2559 spin_lock_irq(&phba->hbalock);
2560 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002561 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002562 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002563 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002564 }
2565
2566 /* Parse the FCF record from the non-embedded mailbox command */
2567 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2568 &next_fcf_index);
2569 if (!new_fcf_record) {
2570 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2571 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002572 "failed to retrieve a FCF record. "
2573 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2574 phba->fcf.fcf_flag);
2575 lpfc_unregister_fcf_rescan(phba);
2576 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002577 }
2578
2579 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002580 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2581 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002582
2583 /* Log the FCF record information if turned on */
2584 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2585 next_fcf_index);
2586
James Smarta93ff372010-10-22 11:06:08 -04002587 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2588 if (!rc) {
2589 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2590 "2848 Remove ineligible FCF (x%x) from "
2591 "from roundrobin bmask\n", fcf_index);
2592 /* Clear roundrobin bmask bit for ineligible FCF */
2593 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2594 /* Perform next round of roundrobin FCF failover */
2595 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2596 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2597 if (rc)
2598 goto out;
2599 goto error_out;
2600 }
2601
2602 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2603 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2604 "2760 Perform FLOGI roundrobin FCF failover: "
2605 "FCF (x%x) back to FCF (x%x)\n",
2606 phba->fcf.current_rec.fcf_indx, fcf_index);
2607 /* Wait 500 ms before retrying FLOGI to current FCF */
2608 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002609 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002610 goto out;
2611 }
2612
James Smart0c9ab6f2010-02-26 14:15:57 -05002613 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002614 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002615 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2616 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002617 spin_lock_irq(&phba->hbalock);
2618 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2619 new_fcf_record, addr_mode, vlan_id,
2620 (boot_flag ? BOOT_ENABLE : 0));
2621 spin_unlock_irq(&phba->hbalock);
2622
2623 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2624
2625 /* Unregister the current in-use FCF record */
2626 lpfc_unregister_fcf(phba);
2627
2628 /* Replace in-use record with the new record */
2629 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2630 sizeof(struct lpfc_fcf_rec));
2631
2632 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002633 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2634 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002635
James Smarta93ff372010-10-22 11:06:08 -04002636error_out:
2637 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002638out:
2639 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002640}
2641
2642/**
2643 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2644 * @phba: pointer to lpfc hba data structure.
2645 * @mboxq: pointer to mailbox object.
2646 *
2647 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002648 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002649 * failover when a new FCF event happened. If the FCF read back is
2650 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002651 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002652 */
2653void
2654lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2655{
2656 struct fcf_record *new_fcf_record;
2657 uint32_t boot_flag, addr_mode;
2658 uint16_t fcf_index, next_fcf_index;
2659 uint16_t vlan_id;
2660 int rc;
2661
2662 /* If link state is not up, no need to proceed */
2663 if (phba->link_state < LPFC_LINK_UP)
2664 goto out;
2665
2666 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002667 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002668 goto out;
2669
2670 /* Parse the FCF record from the non-embedded mailbox command */
2671 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2672 &next_fcf_index);
2673 if (!new_fcf_record) {
2674 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2675 "2767 Mailbox command READ_FCF_RECORD "
2676 "failed to retrieve a FCF record.\n");
2677 goto out;
2678 }
2679
2680 /* Check the connection list for eligibility */
2681 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2682 &addr_mode, &vlan_id);
2683
2684 /* Log the FCF record information if turned on */
2685 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2686 next_fcf_index);
2687
2688 if (!rc)
2689 goto out;
2690
2691 /* Update the eligible FCF record index bmask */
2692 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002693
2694 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002695
2696out:
2697 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2698}
2699
2700/**
James Smart76a95d72010-11-20 23:11:48 -05002701 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2702 * @phba: pointer to lpfc hba data structure.
2703 * @mboxq: pointer to mailbox data structure.
2704 *
2705 * This function handles completion of init vfi mailbox command.
2706 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002707static void
James Smart76a95d72010-11-20 23:11:48 -05002708lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2709{
2710 struct lpfc_vport *vport = mboxq->vport;
2711
James Smartdf9e1b52011-12-13 13:22:17 -05002712 /*
2713 * VFI not supported on interface type 0, just do the flogi
2714 * Also continue if the VFI is in use - just use the same one.
2715 */
2716 if (mboxq->u.mb.mbxStatus &&
2717 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2718 LPFC_SLI_INTF_IF_TYPE_0) &&
2719 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002720 lpfc_printf_vlog(vport, KERN_ERR,
2721 LOG_MBOX,
2722 "2891 Init VFI mailbox failed 0x%x\n",
2723 mboxq->u.mb.mbxStatus);
2724 mempool_free(mboxq, phba->mbox_mem_pool);
2725 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2726 return;
2727 }
James Smart73d91e52011-10-10 21:32:10 -04002728
James Smart76a95d72010-11-20 23:11:48 -05002729 lpfc_initial_flogi(vport);
2730 mempool_free(mboxq, phba->mbox_mem_pool);
2731 return;
2732}
2733
2734/**
2735 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2736 * @vport: pointer to lpfc_vport data structure.
2737 *
2738 * This function issue a init_vfi mailbox command to initialize the VFI and
2739 * VPI for the physical port.
2740 */
2741void
2742lpfc_issue_init_vfi(struct lpfc_vport *vport)
2743{
2744 LPFC_MBOXQ_t *mboxq;
2745 int rc;
2746 struct lpfc_hba *phba = vport->phba;
2747
2748 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2749 if (!mboxq) {
2750 lpfc_printf_vlog(vport, KERN_ERR,
2751 LOG_MBOX, "2892 Failed to allocate "
2752 "init_vfi mailbox\n");
2753 return;
2754 }
2755 lpfc_init_vfi(mboxq, vport);
2756 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2757 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2758 if (rc == MBX_NOT_FINISHED) {
2759 lpfc_printf_vlog(vport, KERN_ERR,
2760 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2761 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2762 }
2763}
2764
2765/**
James Smart1c6834a2009-07-19 10:01:26 -04002766 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2767 * @phba: pointer to lpfc hba data structure.
2768 * @mboxq: pointer to mailbox data structure.
2769 *
2770 * This function handles completion of init vpi mailbox command.
2771 */
James Smart695a8142010-01-26 23:08:03 -05002772void
James Smart1c6834a2009-07-19 10:01:26 -04002773lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2774{
2775 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002776 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002777 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2778
James Smart1c6834a2009-07-19 10:01:26 -04002779 if (mboxq->u.mb.mbxStatus) {
2780 lpfc_printf_vlog(vport, KERN_ERR,
2781 LOG_MBOX,
2782 "2609 Init VPI mailbox failed 0x%x\n",
2783 mboxq->u.mb.mbxStatus);
2784 mempool_free(mboxq, phba->mbox_mem_pool);
2785 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2786 return;
2787 }
James Smart72100cc2010-02-12 14:43:01 -05002788 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002789 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002790 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002791
James Smart695a8142010-01-26 23:08:03 -05002792 /* If this port is physical port or FDISC is done, do reg_vpi */
2793 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2794 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2795 if (!ndlp)
2796 lpfc_printf_vlog(vport, KERN_ERR,
2797 LOG_DISCOVERY,
2798 "2731 Cannot find fabric "
2799 "controller node\n");
2800 else
2801 lpfc_register_new_vport(phba, vport, ndlp);
2802 mempool_free(mboxq, phba->mbox_mem_pool);
2803 return;
2804 }
2805
James Smart1c6834a2009-07-19 10:01:26 -04002806 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2807 lpfc_initial_fdisc(vport);
2808 else {
2809 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002810 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2811 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002812 }
James Smart695a8142010-01-26 23:08:03 -05002813 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002814 return;
2815}
2816
2817/**
James Smartecfd03c2010-02-12 14:41:27 -05002818 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2819 * @vport: pointer to lpfc_vport data structure.
2820 *
2821 * This function issue a init_vpi mailbox command to initialize
2822 * VPI for the vport.
2823 */
2824void
2825lpfc_issue_init_vpi(struct lpfc_vport *vport)
2826{
2827 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002828 int rc, vpi;
2829
2830 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2831 vpi = lpfc_alloc_vpi(vport->phba);
2832 if (!vpi) {
2833 lpfc_printf_vlog(vport, KERN_ERR,
2834 LOG_MBOX,
2835 "3303 Failed to obtain vport vpi\n");
2836 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2837 return;
2838 }
2839 vport->vpi = vpi;
2840 }
James Smartecfd03c2010-02-12 14:41:27 -05002841
2842 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2843 if (!mboxq) {
2844 lpfc_printf_vlog(vport, KERN_ERR,
2845 LOG_MBOX, "2607 Failed to allocate "
2846 "init_vpi mailbox\n");
2847 return;
2848 }
2849 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2850 mboxq->vport = vport;
2851 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2852 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2853 if (rc == MBX_NOT_FINISHED) {
2854 lpfc_printf_vlog(vport, KERN_ERR,
2855 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2856 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2857 }
2858}
2859
2860/**
James Smart6fb120a2009-05-22 14:52:59 -04002861 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2862 * @phba: pointer to lpfc hba data structure.
2863 *
2864 * This function loops through the list of vports on the @phba and issues an
2865 * FDISC if possible.
2866 */
2867void
2868lpfc_start_fdiscs(struct lpfc_hba *phba)
2869{
2870 struct lpfc_vport **vports;
2871 int i;
2872
2873 vports = lpfc_create_vport_work_array(phba);
2874 if (vports != NULL) {
2875 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2876 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2877 continue;
2878 /* There are no vpi for this vport */
2879 if (vports[i]->vpi > phba->max_vpi) {
2880 lpfc_vport_set_state(vports[i],
2881 FC_VPORT_FAILED);
2882 continue;
2883 }
James Smart76a95d72010-11-20 23:11:48 -05002884 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002885 lpfc_vport_set_state(vports[i],
2886 FC_VPORT_LINKDOWN);
2887 continue;
2888 }
James Smart1c6834a2009-07-19 10:01:26 -04002889 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002890 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002891 continue;
2892 }
James Smart6fb120a2009-05-22 14:52:59 -04002893 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2894 lpfc_initial_fdisc(vports[i]);
2895 else {
2896 lpfc_vport_set_state(vports[i],
2897 FC_VPORT_NO_FABRIC_SUPP);
2898 lpfc_printf_vlog(vports[i], KERN_ERR,
2899 LOG_ELS,
2900 "0259 No NPIV "
2901 "Fabric support\n");
2902 }
2903 }
2904 }
2905 lpfc_destroy_vport_work_array(phba, vports);
2906}
2907
2908void
2909lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2910{
2911 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2912 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002913 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002914
James Smartf5eca9b2012-03-01 22:37:54 -05002915 /*
2916 * VFI not supported for interface type 0, so ignore any mailbox
2917 * error (except VFI in use) and continue with the discovery.
2918 */
2919 if (mboxq->u.mb.mbxStatus &&
2920 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2921 LPFC_SLI_INTF_IF_TYPE_0) &&
2922 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002923 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2924 "2018 REG_VFI mbxStatus error x%x "
2925 "HBA state x%x\n",
2926 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002927 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002928 /* FLOGI failed, use loop map to make discovery list */
2929 lpfc_disc_list_loopmap(vport);
2930 /* Start discovery */
2931 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002932 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002933 }
2934 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002935 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002936 }
James Smartae05ebe2013-03-01 16:35:38 -05002937
James Smarte74c03c2013-04-17 20:15:19 -04002938 /* If the VFI is already registered, there is nothing else to do
2939 * Unless this was a VFI update and we are in PT2PT mode, then
2940 * we should drop through to set the port state to ready.
2941 */
James Smartae05ebe2013-03-01 16:35:38 -05002942 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002943 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2944 vport->fc_flag & FC_PT2PT))
2945 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002946
James Smartc8685952009-11-18 15:39:16 -05002947 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002948 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002949 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002950 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002951 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002952 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002953 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002954
James Smart1b511972011-12-13 13:23:09 -05002955 /* In case SLI4 FC loopback test, we are ready */
2956 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2957 (phba->link_flag & LS_LOOPBACK_MODE)) {
2958 phba->link_state = LPFC_HBA_READY;
2959 goto out_free_mem;
2960 }
2961
James Smarte74c03c2013-04-17 20:15:19 -04002962 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2963 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2964 "alpacnt:%d LinkState:%x topology:%x\n",
2965 vport->port_state, vport->fc_flag, vport->fc_myDID,
2966 vport->phba->alpa_map[0],
2967 phba->link_state, phba->fc_topology);
2968
James Smart6fb120a2009-05-22 14:52:59 -04002969 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002970 /*
2971 * For private loop or for NPort pt2pt,
2972 * just start discovery and we are done.
2973 */
2974 if ((vport->fc_flag & FC_PT2PT) ||
2975 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2976 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2977
James Smart76a95d72010-11-20 23:11:48 -05002978 /* Use loop map to make discovery list */
2979 lpfc_disc_list_loopmap(vport);
2980 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002981 if (vport->fc_flag & FC_PT2PT)
2982 vport->port_state = LPFC_VPORT_READY;
2983 else
2984 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002985 } else {
2986 lpfc_start_fdiscs(phba);
2987 lpfc_do_scr_ns_plogi(phba, vport);
2988 }
James Smart6fb120a2009-05-22 14:52:59 -04002989 }
2990
James Smart1b511972011-12-13 13:23:09 -05002991out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002992 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002993 if (dmabuf) {
2994 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2995 kfree(dmabuf);
2996 }
James Smart6fb120a2009-05-22 14:52:59 -04002997 return;
2998}
2999
3000static void
James Smart2e0fef82007-06-17 19:56:36 -05003001lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003002{
James Smart6fb120a2009-05-22 14:52:59 -04003003 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003004 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003005 struct lpfc_vport *vport = pmb->vport;
James Smartaeb3c812017-04-21 16:05:02 -07003006 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart21bf0b92015-08-31 16:48:21 -04003007 struct serv_parm *sp = &vport->fc_sparam;
3008 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003009
3010 /* Check for error */
3011 if (mb->mbxStatus) {
3012 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003013 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3014 "0319 READ_SPARAM mbxStatus error x%x "
3015 "hba state x%x>\n",
3016 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003017 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003018 goto out;
3019 }
3020
James Smart2e0fef82007-06-17 19:56:36 -05003021 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003022 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003023
3024 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3025 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3026 ed_tov = (ed_tov + 999999) / 1000000;
3027
3028 phba->fc_edtov = ed_tov;
3029 phba->fc_ratov = (2 * ed_tov) / 1000;
3030 if (phba->fc_ratov < FF_DEF_RATOV) {
3031 /* RA_TOV should be atleast 10sec for initial flogi */
3032 phba->fc_ratov = FF_DEF_RATOV;
3033 }
3034
James Smart05580562011-05-24 11:40:48 -04003035 lpfc_update_vport_wwn(vport);
James Smartaeb3c812017-04-21 16:05:02 -07003036 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05003037 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3038 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3039 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3040 }
3041
dea31012005-04-17 16:05:31 -05003042 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3043 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003044 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003045 return;
3046
3047out:
3048 pmb->context1 = NULL;
3049 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3050 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003051 lpfc_issue_clear_la(phba, vport);
3052 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003053 return;
3054}
3055
3056static void
James Smart76a95d72010-11-20 23:11:48 -05003057lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003058{
James Smart92d7f7b2007-06-17 19:56:38 -05003059 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003060 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003061 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003062 int i;
James Smart14691152006-12-02 13:34:28 -05003063 struct lpfc_dmabuf *mp;
3064 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003065 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003066 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003067
James Smart92d7f7b2007-06-17 19:56:38 -05003068 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003069 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3070
3071 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3072 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3073 case LPFC_LINK_SPEED_1GHZ:
3074 case LPFC_LINK_SPEED_2GHZ:
3075 case LPFC_LINK_SPEED_4GHZ:
3076 case LPFC_LINK_SPEED_8GHZ:
3077 case LPFC_LINK_SPEED_10GHZ:
3078 case LPFC_LINK_SPEED_16GHZ:
3079 case LPFC_LINK_SPEED_32GHZ:
3080 break;
3081 default:
3082 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3083 break;
3084 }
dea31012005-04-17 16:05:31 -05003085 }
3086
James Smarte74c03c2013-04-17 20:15:19 -04003087 if (phba->fc_topology &&
3088 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3089 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3090 "3314 Toplogy changed was 0x%x is 0x%x\n",
3091 phba->fc_topology,
3092 bf_get(lpfc_mbx_read_top_topology, la));
3093 phba->fc_topology_changed = 1;
3094 }
3095
James Smart76a95d72010-11-20 23:11:48 -05003096 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003097 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003098
James Smartdf9e1b52011-12-13 13:22:17 -05003099 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003100 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003101 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003102
James Smart6a9c52c2009-10-02 15:16:51 -04003103 /* if npiv is enabled and this adapter supports npiv log
3104 * a message that npiv is not supported in this topology
3105 */
3106 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003107 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3108 "1309 Link Up Event npiv not supported in loop "
3109 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003110 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003111 if (bf_get(lpfc_mbx_read_top_il, la))
3112 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003113
James Smart76a95d72010-11-20 23:11:48 -05003114 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3115 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003116
3117 if (i == 0) {
3118 phba->alpa_map[0] = 0;
3119 } else {
James Smarte8b62012007-08-02 11:10:09 -04003120 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003121 int numalpa, j, k;
3122 union {
3123 uint8_t pamap[16];
3124 struct {
3125 uint32_t wd1;
3126 uint32_t wd2;
3127 uint32_t wd3;
3128 uint32_t wd4;
3129 } pa;
3130 } un;
3131 numalpa = phba->alpa_map[0];
3132 j = 0;
3133 while (j < numalpa) {
3134 memset(un.pamap, 0, 16);
3135 for (k = 1; j < numalpa; k++) {
3136 un.pamap[k - 1] =
3137 phba->alpa_map[j + 1];
3138 j++;
3139 if (k == 16)
3140 break;
3141 }
3142 /* Link Up Event ALPA map */
3143 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003144 KERN_WARNING,
3145 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003146 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003147 "ALPA map Data: x%x "
3148 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003149 un.pa.wd1, un.pa.wd2,
3150 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003151 }
3152 }
3153 }
3154 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003155 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003156 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003157 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003158 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3159 }
James Smart2e0fef82007-06-17 19:56:36 -05003160 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003161 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003162 }
James Smart92d7f7b2007-06-17 19:56:38 -05003163 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003164
James Smart38c20672013-03-01 16:37:44 -05003165 if (fc_flags) {
3166 spin_lock_irq(shost->host_lock);
3167 vport->fc_flag |= fc_flags;
3168 spin_unlock_irq(shost->host_lock);
3169 }
3170
dea31012005-04-17 16:05:31 -05003171 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003172 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3173 if (!sparam_mbox)
3174 goto out;
3175
3176 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3177 if (rc) {
3178 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3179 goto out;
3180 }
3181 sparam_mbox->vport = vport;
3182 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3183 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3184 if (rc == MBX_NOT_FINISHED) {
3185 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3186 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3187 kfree(mp);
3188 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3189 goto out;
dea31012005-04-17 16:05:31 -05003190 }
3191
James Smart76a95d72010-11-20 23:11:48 -05003192 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003193 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3194 if (!cfglink_mbox)
3195 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003196 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003197 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003198 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003199 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003200 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003201 if (rc == MBX_NOT_FINISHED) {
3202 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3203 goto out;
3204 }
3205 } else {
James Smart32b97932009-07-19 10:01:21 -04003206 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003207 /*
3208 * Add the driver's default FCF record at FCF index 0 now. This
3209 * is phase 1 implementation that support FCF index 0 and driver
3210 * defaults.
3211 */
James Smart45ed1192009-10-02 15:17:02 -04003212 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003213 fcf_record = kzalloc(sizeof(struct fcf_record),
3214 GFP_KERNEL);
3215 if (unlikely(!fcf_record)) {
3216 lpfc_printf_log(phba, KERN_ERR,
3217 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003218 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003219 "fcf record\n");
3220 rc = -ENODEV;
3221 goto out;
3222 }
3223
3224 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3225 LPFC_FCOE_FCF_DEF_INDEX);
3226 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3227 if (unlikely(rc)) {
3228 lpfc_printf_log(phba, KERN_ERR,
3229 LOG_MBOX | LOG_SLI,
3230 "2013 Could not manually add FCF "
3231 "record 0, status %d\n", rc);
3232 rc = -ENODEV;
3233 kfree(fcf_record);
3234 goto out;
3235 }
3236 kfree(fcf_record);
3237 }
3238 /*
3239 * The driver is expected to do FIP/FCF. Call the port
3240 * and get the FCF Table.
3241 */
James Smart32b97932009-07-19 10:01:21 -04003242 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003243 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003244 spin_unlock_irq(&phba->hbalock);
3245 return;
3246 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003247 /* This is the initial FCF discovery scan */
3248 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003249 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003250 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3251 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003252 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3253 LPFC_FCOE_FCF_GET_FIRST);
3254 if (rc) {
3255 spin_lock_irq(&phba->hbalock);
3256 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3257 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003258 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003259 }
James Smart38b92ef2010-08-04 16:11:39 -04003260 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003261 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003262 }
James Smart6fb120a2009-05-22 14:52:59 -04003263
3264 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003265out:
3266 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003267 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3268 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3269 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003270 lpfc_issue_clear_la(phba, vport);
3271 return;
dea31012005-04-17 16:05:31 -05003272}
3273
3274static void
James Smart84774a42008-08-24 21:50:06 -04003275lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003276{
dea31012005-04-17 16:05:31 -05003277 uint32_t control;
3278 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003279 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003280 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003281 if (phba->sli_rev <= LPFC_SLI_REV3) {
3282 control = readl(phba->HCregaddr);
3283 control |= HC_LAINT_ENA;
3284 writel(control, phba->HCregaddr);
3285 readl(phba->HCregaddr); /* flush */
3286 }
James Smart2e0fef82007-06-17 19:56:36 -05003287 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003288}
3289
James Smart84774a42008-08-24 21:50:06 -04003290static void
3291lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3292{
3293 lpfc_linkdown(phba);
3294 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003295 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003296 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3297}
3298
3299
dea31012005-04-17 16:05:31 -05003300/*
James Smart76a95d72010-11-20 23:11:48 -05003301 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003302 * command upon completion. It is setup in the LPFC_MBOXQ
3303 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003304 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003305 */
3306void
James Smart76a95d72010-11-20 23:11:48 -05003307lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003308{
James Smart2e0fef82007-06-17 19:56:36 -05003309 struct lpfc_vport *vport = pmb->vport;
3310 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003311 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003312 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003313 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003314 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smartaeb3c812017-04-21 16:05:02 -07003315 uint8_t attn_type;
dea31012005-04-17 16:05:31 -05003316
James Smart0d2b6b82008-06-14 22:52:47 -04003317 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003318 pring = lpfc_phba_elsring(phba);
3319 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3320
dea31012005-04-17 16:05:31 -05003321 /* Check for error */
3322 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003323 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003324 "1307 READ_LA mbox error x%x state x%x\n",
3325 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003326 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003327 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003328 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003329 }
3330
James Smart76a95d72010-11-20 23:11:48 -05003331 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
James Smartaeb3c812017-04-21 16:05:02 -07003332 attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
dea31012005-04-17 16:05:31 -05003333
3334 memcpy(&phba->alpa_map[0], mp->virt, 128);
3335
James Smart2e0fef82007-06-17 19:56:36 -05003336 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003337 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003338 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003339 else
James Smart2e0fef82007-06-17 19:56:36 -05003340 vport->fc_flag &= ~FC_BYPASSED_MODE;
3341 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05003342
James Smartbe0c0082013-03-01 16:38:14 -05003343 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003344 phba->fc_stat.LinkMultiEvent++;
James Smartaeb3c812017-04-21 16:05:02 -07003345 if (attn_type == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003346 if (phba->fc_eventTag != 0)
3347 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003348 }
dea31012005-04-17 16:05:31 -05003349
3350 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003351 if (phba->sli_rev < LPFC_SLI_REV4) {
3352 spin_lock_irq(&phba->hbalock);
3353 if (bf_get(lpfc_mbx_read_top_mm, la))
3354 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3355 else
3356 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3357 spin_unlock_irq(&phba->hbalock);
3358 }
dea31012005-04-17 16:05:31 -05003359
James Smart4d9ab992009-10-02 15:16:39 -04003360 phba->link_events++;
James Smartaeb3c812017-04-21 16:05:02 -07003361 if ((attn_type == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003362 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003363 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003364 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003365 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003366 "1306 Link Up Event in loop back mode "
3367 "x%x received Data: x%x x%x x%x x%x\n",
3368 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003369 bf_get(lpfc_mbx_read_top_alpa_granted,
3370 la),
3371 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003372 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003373 } else {
3374 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003375 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003376 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003377 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003378 bf_get(lpfc_mbx_read_top_alpa_granted,
3379 la),
3380 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003381 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003382 bf_get(lpfc_mbx_read_top_mm, la),
3383 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003384 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003385 }
James Smart92d7f7b2007-06-17 19:56:38 -05003386 lpfc_mbx_process_link_up(phba, la);
James Smartaeb3c812017-04-21 16:05:02 -07003387 } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3388 attn_type == LPFC_ATT_UNEXP_WWPN) {
dea31012005-04-17 16:05:31 -05003389 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003390 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003391 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3392 "1308 Link Down Event in loop back mode "
3393 "x%x received "
3394 "Data: x%x x%x x%x\n",
3395 la->eventTag, phba->fc_eventTag,
3396 phba->pport->port_state, vport->fc_flag);
James Smartaeb3c812017-04-21 16:05:02 -07003397 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3398 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3399 "1313 Link Down UNEXP WWPN Event x%x received "
3400 "Data: x%x x%x x%x x%x x%x\n",
3401 la->eventTag, phba->fc_eventTag,
3402 phba->pport->port_state, vport->fc_flag,
3403 bf_get(lpfc_mbx_read_top_mm, la),
3404 bf_get(lpfc_mbx_read_top_fa, la));
James Smart1b511972011-12-13 13:23:09 -05003405 else
James Smart3163f722008-02-08 18:50:25 -05003406 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003407 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003408 "Data: x%x x%x x%x x%x x%x\n",
3409 la->eventTag, phba->fc_eventTag,
3410 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003411 bf_get(lpfc_mbx_read_top_mm, la),
3412 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003413 lpfc_mbx_issue_link_down(phba);
3414 }
James Smartaeb3c812017-04-21 16:05:02 -07003415 if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3416 attn_type == LPFC_ATT_LINK_UP) {
James Smart84774a42008-08-24 21:50:06 -04003417 if (phba->link_state != LPFC_LINK_DOWN) {
3418 phba->fc_stat.LinkDown++;
3419 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3420 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003421 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003422 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003423 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003424 lpfc_mbx_issue_link_down(phba);
3425 } else
3426 lpfc_enable_la(phba);
3427
3428 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3429 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3430 "Data: x%x x%x x%x\n",
3431 la->eventTag, phba->fc_eventTag,
3432 phba->pport->port_state, vport->fc_flag);
3433 /*
3434 * The cmnd that triggered this will be waiting for this
3435 * signal.
3436 */
3437 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3438 if (phba->wait_4_mlo_maint_flg) {
3439 phba->wait_4_mlo_maint_flg = 0;
3440 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003441 }
James Smart84774a42008-08-24 21:50:06 -04003442 }
3443
James Smartbe0c0082013-03-01 16:38:14 -05003444 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3445 bf_get(lpfc_mbx_read_top_fa, la)) {
3446 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003447 lpfc_issue_clear_la(phba, vport);
3448 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003449 "1311 fa %d\n",
3450 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003451 }
3452
James Smart76a95d72010-11-20 23:11:48 -05003453lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003454 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3455 kfree(mp);
3456 mempool_free(pmb, phba->mbox_mem_pool);
3457 return;
3458}
3459
3460/*
3461 * This routine handles processing a REG_LOGIN mailbox
3462 * command upon completion. It is setup in the LPFC_MBOXQ
3463 * as the completion routine when the command is
3464 * handed off to the SLI layer.
3465 */
3466void
James Smart2e0fef82007-06-17 19:56:36 -05003467lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003468{
James Smart2e0fef82007-06-17 19:56:36 -05003469 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003470 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003471 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003472 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003473
dea31012005-04-17 16:05:31 -05003474 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003475 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003476
James Smartbe6bb942015-04-07 15:07:22 -04003477 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3478 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3479 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003480 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003481 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003482 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3483 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3484
James Smart40426292010-12-15 17:58:10 -05003485 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3486 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003487 /* We rcvd a rscn after issuing this
3488 * mbox reg login, we may have cycled
3489 * back through the state and be
3490 * back at reg login state so this
3491 * mbox needs to be ignored becase
3492 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003493 * process.
James Smartffc95492010-06-07 15:23:17 -04003494 */
3495 spin_lock_irq(shost->host_lock);
3496 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3497 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003498
3499 /*
3500 * We cannot leave the RPI registered because
3501 * if we go thru discovery again for this ndlp
3502 * a subsequent REG_RPI will fail.
3503 */
3504 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3505 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003506 }
3507
3508 /* Call state machine */
3509 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003510
dea31012005-04-17 16:05:31 -05003511 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3512 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003513 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003514 /* decrement the node reference count held for this callback
3515 * function.
3516 */
James Smart329f9bc2007-04-25 09:53:01 -04003517 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003518
3519 return;
3520}
3521
James Smart92d7f7b2007-06-17 19:56:38 -05003522static void
3523lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3524{
James Smart04c68492009-05-22 14:52:52 -04003525 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003526 struct lpfc_vport *vport = pmb->vport;
3527 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3528
3529 switch (mb->mbxStatus) {
3530 case 0x0011:
3531 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003532 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3533 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3534 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003535 break;
James Smart78730cf2010-04-06 15:06:30 -04003536 /* If VPI is busy, reset the HBA */
3537 case 0x9700:
3538 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3539 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3540 vport->vpi, mb->mbxStatus);
3541 if (!(phba->pport->load_flag & FC_UNLOADING))
3542 lpfc_workq_post_event(phba, NULL, NULL,
3543 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003544 }
James Smart72100cc2010-02-12 14:43:01 -05003545 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003546 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003547 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003548 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003549 vport->unreg_vpi_cmpl = VPORT_OK;
3550 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003551 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003552 /*
3553 * This shost reference might have been taken at the beginning of
3554 * lpfc_vport_delete()
3555 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003556 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003557 scsi_host_put(shost);
3558}
3559
James Smartd7c255b2008-08-24 21:50:00 -04003560int
James Smart92d7f7b2007-06-17 19:56:38 -05003561lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3562{
3563 struct lpfc_hba *phba = vport->phba;
3564 LPFC_MBOXQ_t *mbox;
3565 int rc;
3566
3567 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3568 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003569 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003570
3571 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3572 mbox->vport = vport;
3573 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003574 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003575 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003576 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3577 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003578 mempool_free(mbox, phba->mbox_mem_pool);
3579 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003580 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003581 }
James Smartd7c255b2008-08-24 21:50:00 -04003582 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003583}
3584
3585static void
3586lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3587{
3588 struct lpfc_vport *vport = pmb->vport;
3589 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003590 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003591
3592 switch (mb->mbxStatus) {
3593 case 0x0011:
3594 case 0x9601:
3595 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003596 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3597 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3598 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003599 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3600 spin_lock_irq(shost->host_lock);
3601 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3602 spin_unlock_irq(shost->host_lock);
3603 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003604
James Smart01649562017-02-12 13:52:32 -08003605 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003606 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003607 if (phba->nvmet_support)
3608 lpfc_nvmet_update_targetport(phba);
3609 else
James Smart8c258642017-02-12 13:52:36 -08003610 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003611 }
James Smart92d7f7b2007-06-17 19:56:38 -05003612 goto out;
3613 }
James Smart92d7f7b2007-06-17 19:56:38 -05003614
James Smart72100cc2010-02-12 14:43:01 -05003615 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003616 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003617 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003618 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003619 vport->num_disc_nodes = 0;
3620 /* go thru NPR list and issue ELS PLOGIs */
3621 if (vport->fc_npr_cnt)
3622 lpfc_els_disc_plogi(vport);
3623
3624 if (!vport->num_disc_nodes) {
3625 spin_lock_irq(shost->host_lock);
3626 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3627 spin_unlock_irq(shost->host_lock);
3628 lpfc_can_disctmo(vport);
3629 }
3630 vport->port_state = LPFC_VPORT_READY;
3631
3632out:
3633 mempool_free(pmb, phba->mbox_mem_pool);
3634 return;
3635}
3636
James Smart21e9a0a2009-05-22 14:53:21 -04003637/**
3638 * lpfc_create_static_vport - Read HBA config region to create static vports.
3639 * @phba: pointer to lpfc hba data structure.
3640 *
3641 * This routine issue a DUMP mailbox command for config region 22 to get
3642 * the list of static vports to be created. The function create vports
3643 * based on the information returned from the HBA.
3644 **/
3645void
3646lpfc_create_static_vport(struct lpfc_hba *phba)
3647{
3648 LPFC_MBOXQ_t *pmb = NULL;
3649 MAILBOX_t *mb;
3650 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003651 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003652 struct fc_vport_identifiers vport_id;
3653 struct fc_vport *new_fc_vport;
3654 struct Scsi_Host *shost;
3655 struct lpfc_vport *vport;
3656 uint16_t offset = 0;
3657 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003658 struct lpfc_dmabuf *mp;
3659 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003660
3661 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3662 if (!pmb) {
3663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3664 "0542 lpfc_create_static_vport failed to"
3665 " allocate mailbox memory\n");
3666 return;
3667 }
James Smartcdcc2342012-08-14 14:26:35 -04003668 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003669 mb = &pmb->u.mb;
3670
3671 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3672 if (!vport_info) {
3673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3674 "0543 lpfc_create_static_vport failed to"
3675 " allocate vport_info\n");
3676 mempool_free(pmb, phba->mbox_mem_pool);
3677 return;
3678 }
3679
3680 vport_buff = (uint8_t *) vport_info;
3681 do {
James Smartcdcc2342012-08-14 14:26:35 -04003682 /* free dma buffer from previous round */
3683 if (pmb->context1) {
3684 mp = (struct lpfc_dmabuf *)pmb->context1;
3685 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3686 kfree(mp);
3687 }
James Smart1c6834a2009-07-19 10:01:26 -04003688 if (lpfc_dump_static_vport(phba, pmb, offset))
3689 goto out;
3690
James Smart21e9a0a2009-05-22 14:53:21 -04003691 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003692 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3693 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003694
James Smartcdcc2342012-08-14 14:26:35 -04003695 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003696 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3697 "0544 lpfc_create_static_vport failed to"
3698 " issue dump mailbox command ret 0x%x "
3699 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003700 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003701 goto out;
3702 }
3703
James Smart1c6834a2009-07-19 10:01:26 -04003704 if (phba->sli_rev == LPFC_SLI_REV4) {
3705 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003706 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003707 if (byte_count > sizeof(struct static_vport_info) -
3708 offset)
3709 byte_count = sizeof(struct static_vport_info)
3710 - offset;
3711 memcpy(vport_buff + offset, mp->virt, byte_count);
3712 offset += byte_count;
3713 } else {
3714 if (mb->un.varDmp.word_cnt >
3715 sizeof(struct static_vport_info) - offset)
3716 mb->un.varDmp.word_cnt =
3717 sizeof(struct static_vport_info)
3718 - offset;
3719 byte_count = mb->un.varDmp.word_cnt;
3720 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3721 vport_buff + offset,
3722 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003723
James Smart1c6834a2009-07-19 10:01:26 -04003724 offset += byte_count;
3725 }
James Smart21e9a0a2009-05-22 14:53:21 -04003726
James Smart1c6834a2009-07-19 10:01:26 -04003727 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003728 offset < sizeof(struct static_vport_info));
3729
3730
3731 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3732 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3733 != VPORT_INFO_REV)) {
3734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3735 "0545 lpfc_create_static_vport bad"
3736 " information header 0x%x 0x%x\n",
3737 le32_to_cpu(vport_info->signature),
3738 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3739
3740 goto out;
3741 }
3742
3743 shost = lpfc_shost_from_vport(phba->pport);
3744
3745 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3746 memset(&vport_id, 0, sizeof(vport_id));
3747 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3748 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3749 if (!vport_id.port_name || !vport_id.node_name)
3750 continue;
3751
3752 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3753 vport_id.vport_type = FC_PORTTYPE_NPIV;
3754 vport_id.disable = false;
3755 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3756
3757 if (!new_fc_vport) {
3758 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3759 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003760 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003761 continue;
3762 }
3763
3764 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3765 vport->vport_flag |= STATIC_VPORT;
3766 }
3767
3768out:
James Smart21e9a0a2009-05-22 14:53:21 -04003769 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003770 if (mbx_wait_rc != MBX_TIMEOUT) {
3771 if (pmb->context1) {
3772 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003773 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3774 kfree(mp);
3775 }
James Smart21e9a0a2009-05-22 14:53:21 -04003776 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003777 }
James Smart21e9a0a2009-05-22 14:53:21 -04003778
3779 return;
3780}
3781
dea31012005-04-17 16:05:31 -05003782/*
3783 * This routine handles processing a Fabric REG_LOGIN mailbox
3784 * command upon completion. It is setup in the LPFC_MBOXQ
3785 * as the completion routine when the command is
3786 * handed off to the SLI layer.
3787 */
3788void
James Smart2e0fef82007-06-17 19:56:36 -05003789lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003790{
James Smart92d7f7b2007-06-17 19:56:38 -05003791 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003792 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003793 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003794 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003795 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003796
James Smart549e55c2007-08-02 11:09:51 -04003797 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003798 pmb->context1 = NULL;
3799 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003800
dea31012005-04-17 16:05:31 -05003801 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003802 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3803 "0258 Register Fabric login error: 0x%x\n",
3804 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003805 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3806 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003807 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003808
James Smart76a95d72010-11-20 23:11:48 -05003809 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003810 /* FLOGI failed, use loop map to make discovery list */
3811 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003812
James Smart92d7f7b2007-06-17 19:56:38 -05003813 /* Start discovery */
3814 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003815 /* Decrement the reference count to ndlp after the
3816 * reference to the ndlp are done.
3817 */
3818 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003819 return;
3820 }
3821
3822 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003823 /* Decrement the reference count to ndlp after the reference
3824 * to the ndlp are done.
3825 */
3826 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003827 return;
3828 }
3829
James Smart6d368e52011-05-24 11:44:12 -04003830 if (phba->sli_rev < LPFC_SLI_REV4)
3831 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003832 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003833 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003834 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003835
James Smart2e0fef82007-06-17 19:56:36 -05003836 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003837 /* when physical port receive logo donot start
3838 * vport discovery */
3839 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3840 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003841 else {
3842 shost = lpfc_shost_from_vport(vport);
3843 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003844 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003845 spin_unlock_irq(shost->host_lock);
3846 }
James Smart92d7f7b2007-06-17 19:56:38 -05003847 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003848 }
3849
3850 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3851 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003852 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003853
3854 /* Drop the reference count from the mbox at the end after
3855 * all the current reference to the ndlp have been done.
3856 */
3857 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003858 return;
3859}
3860
James Smarta0f2d3e2017-02-12 13:52:31 -08003861 /*
3862 * This routine will issue a GID_FT for each FC4 Type supported
3863 * by the driver. ALL GID_FTs must complete before discovery is started.
3864 */
3865int
3866lpfc_issue_gidft(struct lpfc_vport *vport)
3867{
3868 struct lpfc_hba *phba = vport->phba;
3869
3870 /* Good status, issue CT Request to NameServer */
3871 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3872 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3873 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3874 /* Cannot issue NameServer FCP Query, so finish up
3875 * discovery
3876 */
3877 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3878 "0604 %s FC TYPE %x %s\n",
3879 "Failed to issue GID_FT to ",
3880 FC_TYPE_FCP,
3881 "Finishing discovery.");
3882 return 0;
3883 }
3884 vport->gidft_inp++;
3885 }
3886
3887 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3888 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3889 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3890 /* Cannot issue NameServer NVME Query, so finish up
3891 * discovery
3892 */
3893 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3894 "0605 %s FC_TYPE %x %s %d\n",
3895 "Failed to issue GID_FT to ",
3896 FC_TYPE_NVME,
3897 "Finishing discovery: gidftinp ",
3898 vport->gidft_inp);
3899 if (vport->gidft_inp == 0)
3900 return 0;
3901 } else
3902 vport->gidft_inp++;
3903 }
3904 return vport->gidft_inp;
3905}
3906
dea31012005-04-17 16:05:31 -05003907/*
3908 * This routine handles processing a NameServer REG_LOGIN mailbox
3909 * command upon completion. It is setup in the LPFC_MBOXQ
3910 * as the completion routine when the command is
3911 * handed off to the SLI layer.
3912 */
3913void
James Smart2e0fef82007-06-17 19:56:36 -05003914lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003915{
James Smart21e9a0a2009-05-22 14:53:21 -04003916 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003917 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3918 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3919 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003920
James Smartd439d282010-09-29 11:18:45 -04003921 pmb->context1 = NULL;
3922 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003923 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003924
dea31012005-04-17 16:05:31 -05003925 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003926 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3927 "0260 Register NameServer error: 0x%x\n",
3928 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003929
3930out:
James Smartfa4066b2008-01-11 01:53:27 -05003931 /* decrement the node reference count held for this
3932 * callback function.
3933 */
James Smart329f9bc2007-04-25 09:53:01 -04003934 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003935 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3936 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003937 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003938
3939 /* If no other thread is using the ndlp, free it */
3940 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003941
James Smart76a95d72010-11-20 23:11:48 -05003942 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003943 /*
3944 * RegLogin failed, use loop map to make discovery
3945 * list
3946 */
3947 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003948
James Smart92d7f7b2007-06-17 19:56:38 -05003949 /* Start discovery */
3950 lpfc_disc_start(vport);
3951 return;
3952 }
3953 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003954 return;
3955 }
3956
James Smart6d368e52011-05-24 11:44:12 -04003957 if (phba->sli_rev < LPFC_SLI_REV4)
3958 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003959 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003960 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003961 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003962 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3963 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3964 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003965 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003966 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003967
James Smart2e0fef82007-06-17 19:56:36 -05003968 if (vport->port_state < LPFC_VPORT_READY) {
3969 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003970 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3971 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3972 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3973 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003974
3975 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3976 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3977 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3978
3979 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3980 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003981 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3982 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003983
3984 /* Issue SCR just before NameServer GID_FT Query */
3985 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003986 }
3987
James Smart2e0fef82007-06-17 19:56:36 -05003988 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003989 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003990 goto out;
dea31012005-04-17 16:05:31 -05003991
James Smarta0f2d3e2017-02-12 13:52:31 -08003992 /*
3993 * At this point in time we may need to wait for multiple
3994 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3995 *
3996 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003997 * callback function.
3998 */
James Smart329f9bc2007-04-25 09:53:01 -04003999 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004000 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4001 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05004002 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004003
4004 return;
4005}
4006
4007static void
James Smart2e0fef82007-06-17 19:56:36 -05004008lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004009{
James Smart2e0fef82007-06-17 19:56:36 -05004010 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4011 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05004012 struct lpfc_rport_data *rdata;
4013 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05004014 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004015
James Smart895427b2017-02-12 13:52:30 -08004016 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4017 return;
4018
dea31012005-04-17 16:05:31 -05004019 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004020 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4021 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004022 rport_ids.port_id = ndlp->nlp_DID;
4023 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004024
James Smart329f9bc2007-04-25 09:53:01 -04004025 /*
4026 * We leave our node pointer in rport->dd_data when we unregister a
4027 * FCP target port. But fc_remote_port_add zeros the space to which
4028 * rport->dd_data points. So, if we're reusing a previously
4029 * registered port, drop the reference that we took the last time we
4030 * registered the port.
4031 */
James Smart466e8402015-05-21 13:55:28 -04004032 rport = ndlp->rport;
4033 if (rport) {
4034 rdata = rport->dd_data;
4035 /* break the link before dropping the ref */
4036 ndlp->rport = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08004037 if (rdata) {
4038 if (rdata->pnode == ndlp)
4039 lpfc_nlp_put(ndlp);
4040 rdata->pnode = NULL;
4041 }
James Smart466e8402015-05-21 13:55:28 -04004042 /* drop reference for earlier registeration */
4043 put_device(&rport->dev);
4044 }
James Smart858c9f62007-06-17 19:56:39 -05004045
4046 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4047 "rport add: did:x%x flg:x%x type x%x",
4048 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4049
James Smart9589b0622011-04-16 11:03:17 -04004050 /* Don't add the remote port if unloading. */
4051 if (vport->load_flag & FC_UNLOADING)
4052 return;
4053
James Smart2e0fef82007-06-17 19:56:36 -05004054 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004055 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004056 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4057 "Warning: fc_remote_port_add failed\n");
4058 return;
4059 }
4060
4061 /* initialize static port data */
4062 rport->maxframe_size = ndlp->nlp_maxframe;
4063 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004064 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004065 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004066
4067 if (ndlp->nlp_type & NLP_FCP_TARGET)
4068 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4069 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4070 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4071
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004072 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4073 fc_remote_port_rolechg(rport, rport_ids.roles);
4074
James Smart34f5ad82012-08-03 12:35:03 -04004075 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4076 "3183 rport register x%06x, rport %p role x%x\n",
4077 ndlp->nlp_DID, rport, rport_ids.roles);
4078
James Smart071fbd3d2006-04-15 11:53:20 -04004079 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004080 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004081 ndlp->nlp_sid = rport->scsi_target_id;
4082 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004083 return;
4084}
4085
4086static void
James Smart2e0fef82007-06-17 19:56:36 -05004087lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004088{
4089 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004090 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004091 struct lpfc_hba *phba = vport->phba;
4092
4093 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4094 return;
James Smartc01f3202006-08-18 17:47:08 -04004095
James Smart2ea259e2017-02-12 13:52:27 -08004096 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004097 "rport delete: did:x%x flg:x%x type x%x",
4098 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4099
James Smart2ea259e2017-02-12 13:52:27 -08004100 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004101 "3184 rport unregister x%06x, rport %p\n",
4102 ndlp->nlp_DID, rport);
4103
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004104 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004105
4106 return;
4107}
4108
James Smartde0c5b32007-04-25 09:52:27 -04004109static void
James Smart2e0fef82007-06-17 19:56:36 -05004110lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004111{
James Smart2e0fef82007-06-17 19:56:36 -05004112 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4113
4114 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004115 switch (state) {
4116 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004117 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004118 break;
4119 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004120 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004121 break;
4122 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004123 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004124 break;
4125 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004126 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004127 break;
4128 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004129 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004130 break;
4131 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004132 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004133 break;
4134 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004135 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004136 break;
4137 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004138 if (vport->fc_npr_cnt == 0 && count == -1)
4139 vport->fc_npr_cnt = 0;
4140 else
4141 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004142 break;
4143 }
James Smart2e0fef82007-06-17 19:56:36 -05004144 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004145}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004146
James Smartde0c5b32007-04-25 09:52:27 -04004147static void
James Smart2e0fef82007-06-17 19:56:36 -05004148lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004149 int old_state, int new_state)
4150{
James Smart2e0fef82007-06-17 19:56:36 -05004151 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4152
James Smartde0c5b32007-04-25 09:52:27 -04004153 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004154 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4155 ndlp->nlp_type |= NLP_FC_NODE;
4156 }
4157 if (new_state == NLP_STE_MAPPED_NODE)
4158 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4159 if (new_state == NLP_STE_NPR_NODE)
4160 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004161
James Smarta0f2d3e2017-02-12 13:52:31 -08004162 /* FCP and NVME Transport interface */
4163 if ((old_state == NLP_STE_MAPPED_NODE ||
4164 old_state == NLP_STE_UNMAPPED_NODE)) {
4165 if (ndlp->rport) {
4166 vport->phba->nport_event_cnt++;
4167 lpfc_unregister_remote_port(ndlp);
4168 }
4169
James Smart1c5b12f2017-04-21 16:05:03 -07004170 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004171 vport->phba->nport_event_cnt++;
James Smart1c5b12f2017-04-21 16:05:03 -07004172 if (vport->phba->nvmet_support == 0)
James Smart6599e122017-06-01 21:07:00 -07004173 /* Start devloss */
James Smart1c5b12f2017-04-21 16:05:03 -07004174 lpfc_nvme_unregister_port(vport, ndlp);
James Smart6599e122017-06-01 21:07:00 -07004175 else
4176 /* NVMET has no upcall. */
4177 lpfc_nlp_put(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004178 }
James Smartde0c5b32007-04-25 09:52:27 -04004179 }
dea31012005-04-17 16:05:31 -05004180
James Smarta0f2d3e2017-02-12 13:52:31 -08004181 /* FCP and NVME Transport interfaces */
4182
James Smartde0c5b32007-04-25 09:52:27 -04004183 if (new_state == NLP_STE_MAPPED_NODE ||
4184 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart92721c32017-06-01 21:06:59 -07004185 if (ndlp->nlp_fc4_type & NLP_FC4_FCP ||
4186 ndlp->nlp_DID == Fabric_DID ||
4187 ndlp->nlp_DID == NameServer_DID ||
4188 ndlp->nlp_DID == FDMI_DID) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004189 vport->phba->nport_event_cnt++;
4190 /*
4191 * Tell the fc transport about the port, if we haven't
4192 * already. If we have, and it's a scsi entity, be
4193 */
4194 lpfc_register_remote_port(vport, ndlp);
4195 }
4196 /* Notify the NVME transport of this new rport. */
4197 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4198 if (vport->phba->nvmet_support == 0) {
4199 /* Register this rport with the transport.
4200 * Initiators take the NDLP ref count in
4201 * the register.
4202 */
4203 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004204 lpfc_nvme_register_port(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08004205 } else {
4206 /* Just take an NDLP ref count since the
4207 * target does not register rports.
4208 */
4209 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004210 }
4211 }
James Smartde0c5b32007-04-25 09:52:27 -04004212 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004213
James Smartea2151b2008-09-07 11:52:10 -04004214 if ((new_state == NLP_STE_MAPPED_NODE) &&
4215 (vport->stat_data_enabled)) {
4216 /*
4217 * A new target is discovered, if there is no buffer for
4218 * statistical data collection allocate buffer.
4219 */
4220 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4221 sizeof(struct lpfc_scsicmd_bkt),
4222 GFP_KERNEL);
4223
4224 if (!ndlp->lat_data)
4225 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4226 "0286 lpfc_nlp_state_cleanup failed to "
4227 "allocate statistical data buffer DID "
4228 "0x%x\n", ndlp->nlp_DID);
4229 }
James Smart858c9f62007-06-17 19:56:39 -05004230 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004231 * If the node just added to Mapped list was an FCP target,
4232 * but the remote port registration failed or assigned a target
4233 * id outside the presentable range - move the node to the
4234 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004235 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004236 if ((new_state == NLP_STE_MAPPED_NODE) &&
4237 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004238 (!ndlp->rport ||
4239 ndlp->rport->scsi_target_id == -1 ||
4240 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004241 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004242 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004243 spin_unlock_irq(shost->host_lock);
4244 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004245 }
James Smartde0c5b32007-04-25 09:52:27 -04004246}
4247
James Smart685f0bf2007-04-25 09:53:08 -04004248static char *
4249lpfc_nlp_state_name(char *buffer, size_t size, int state)
4250{
4251 static char *states[] = {
4252 [NLP_STE_UNUSED_NODE] = "UNUSED",
4253 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4254 [NLP_STE_ADISC_ISSUE] = "ADISC",
4255 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4256 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004257 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004258 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4259 [NLP_STE_MAPPED_NODE] = "MAPPED",
4260 [NLP_STE_NPR_NODE] = "NPR",
4261 };
4262
James Smart311464e2007-08-02 11:10:37 -04004263 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004264 strlcpy(buffer, states[state], size);
4265 else
4266 snprintf(buffer, size, "unknown (%d)", state);
4267 return buffer;
4268}
4269
James Smartde0c5b32007-04-25 09:52:27 -04004270void
James Smart2e0fef82007-06-17 19:56:36 -05004271lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4272 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004273{
James Smart2e0fef82007-06-17 19:56:36 -05004274 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004275 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004276 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004277
James Smarte8b62012007-08-02 11:10:09 -04004278 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4279 "0904 NPort state transition x%06x, %s -> %s\n",
4280 ndlp->nlp_DID,
4281 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4282 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004283
4284 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4285 "node statechg did:x%x old:%d ste:%d",
4286 ndlp->nlp_DID, old_state, state);
4287
James Smartde0c5b32007-04-25 09:52:27 -04004288 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004289 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004290 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004291 if (old_state == NLP_STE_UNMAPPED_NODE) {
4292 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4293 ndlp->nlp_type &= ~NLP_FC_NODE;
4294 }
4295
James Smart685f0bf2007-04-25 09:53:08 -04004296 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004297 spin_lock_irq(shost->host_lock);
4298 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4299 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004300 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004301 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004302
4303 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004304 lpfc_nlp_counters(vport, state, 1);
4305 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004306}
4307
4308void
James Smarte47c9092008-02-08 18:49:26 -05004309lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4310{
4311 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4312
4313 if (list_empty(&ndlp->nlp_listp)) {
4314 spin_lock_irq(shost->host_lock);
4315 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4316 spin_unlock_irq(shost->host_lock);
4317 }
4318}
4319
4320void
James Smart2e0fef82007-06-17 19:56:36 -05004321lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004322{
James Smart2e0fef82007-06-17 19:56:36 -05004323 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4324
James Smart0d2b6b82008-06-14 22:52:47 -04004325 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004326 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004327 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4328 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004329 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004330 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004331 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004332 NLP_STE_UNUSED_NODE);
4333}
4334
Adrian Bunk4d9db012008-02-14 23:24:02 +02004335static void
James Smarte47c9092008-02-08 18:49:26 -05004336lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4337{
James Smart0d2b6b82008-06-14 22:52:47 -04004338 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004339 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4340 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4341 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4342 NLP_STE_UNUSED_NODE);
4343}
James Smart109f6ed2008-12-04 22:39:08 -05004344/**
James Smart3621a712009-04-06 18:47:14 -04004345 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004346 * @vport: Pointer to Virtual Port object.
4347 * @ndlp: Pointer to FC node object.
4348 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004349 *
4350 * This function is always called when node object need to be initialized.
4351 * It initializes all the fields of the node object. Although the reference
4352 * to phba from @ndlp can be obtained indirectly through it's reference to
4353 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4354 * to the life-span of the @ndlp might go beyond the existence of @vport as
4355 * the final release of ndlp is determined by its reference count. And, the
4356 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004357 **/
4358static inline void
4359lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4360 uint32_t did)
4361{
4362 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4363 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
Tomas Jasek33cc5592017-03-03 13:45:48 +01004364 setup_timer(&ndlp->nlp_delayfunc, lpfc_els_retry_delay,
4365 (unsigned long)ndlp);
James Smart109f6ed2008-12-04 22:39:08 -05004366 ndlp->nlp_DID = did;
4367 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004368 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004369 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004370 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004371 kref_init(&ndlp->kref);
4372 NLP_INT_NODE_ACT(ndlp);
4373 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004374 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004375}
James Smarte47c9092008-02-08 18:49:26 -05004376
4377struct lpfc_nodelist *
4378lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4379 int state)
4380{
4381 struct lpfc_hba *phba = vport->phba;
4382 uint32_t did;
4383 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004384 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smart9d3d3402017-04-21 16:05:00 -07004385 int rpi = LPFC_RPI_ALLOC_ERROR;
James Smarte47c9092008-02-08 18:49:26 -05004386
4387 if (!ndlp)
4388 return NULL;
4389
James Smart9d3d3402017-04-21 16:05:00 -07004390 if (phba->sli_rev == LPFC_SLI_REV4) {
4391 rpi = lpfc_sli4_alloc_rpi(vport->phba);
4392 if (rpi == LPFC_RPI_ALLOC_ERROR)
4393 return NULL;
4394 }
4395
James Smarte47c9092008-02-08 18:49:26 -05004396 spin_lock_irqsave(&phba->ndlp_lock, flags);
4397 /* The ndlp should not be in memory free mode */
4398 if (NLP_CHK_FREE_REQ(ndlp)) {
4399 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4400 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4401 "0277 lpfc_enable_node: ndlp:x%p "
4402 "usgmap:x%x refcnt:%d\n",
4403 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004404 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004405 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004406 }
4407 /* The ndlp should not already be in active mode */
4408 if (NLP_CHK_NODE_ACT(ndlp)) {
4409 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4410 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4411 "0278 lpfc_enable_node: ndlp:x%p "
4412 "usgmap:x%x refcnt:%d\n",
4413 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004414 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004415 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004416 }
4417
4418 /* Keep the original DID */
4419 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004420 if (phba->sli_rev == LPFC_SLI_REV4)
4421 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004422
4423 /* re-initialize ndlp except of ndlp linked list pointer */
4424 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4425 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004426 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004427
James Smartcff261f2013-12-17 20:29:47 -05004428 if (phba->sli_rev == LPFC_SLI_REV4)
4429 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4430
James Smarte47c9092008-02-08 18:49:26 -05004431 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004432 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07004433 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04004434 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4435 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4436 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4437 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004438 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004439 ndlp->nlp_usg_map, ndlp);
4440 }
James Smart725dd392013-10-10 12:23:30 -04004441
James Smarte47c9092008-02-08 18:49:26 -05004442
4443 if (state != NLP_STE_UNUSED_NODE)
4444 lpfc_nlp_set_state(vport, ndlp, state);
4445
4446 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4447 "node enable: did:x%x",
4448 ndlp->nlp_DID, 0, 0);
4449 return ndlp;
James Smart9d3d3402017-04-21 16:05:00 -07004450
4451free_rpi:
4452 if (phba->sli_rev == LPFC_SLI_REV4)
4453 lpfc_sli4_free_rpi(vport->phba, rpi);
4454 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04004455}
4456
4457void
James Smart2e0fef82007-06-17 19:56:36 -05004458lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004459{
James Smart87af33f2007-10-27 13:37:43 -04004460 /*
James Smartfa4066b2008-01-11 01:53:27 -05004461 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004462 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004463 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4464 * until ALL other outstanding threads have completed. We check
4465 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004466 */
James Smartfa4066b2008-01-11 01:53:27 -05004467 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4468 return;
James Smart51ef4c22007-08-02 11:10:31 -04004469 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004470 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004471 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004472 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004473 }
James Smart02902172015-05-21 13:55:17 -04004474
4475 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004476 return;
dea31012005-04-17 16:05:31 -05004477}
4478
4479/*
4480 * Start / ReStart rescue timer for Discovery / RSCN handling
4481 */
4482void
James Smart2e0fef82007-06-17 19:56:36 -05004483lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004484{
James Smart2e0fef82007-06-17 19:56:36 -05004485 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4486 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004487 uint32_t tmo;
4488
James Smart2e0fef82007-06-17 19:56:36 -05004489 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004490 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004491 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4492 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004493 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05004494 * FC spec states we need 3 * ratov for CT requests
4495 */
4496 tmo = ((phba->fc_ratov * 3) + 3);
4497 }
dea31012005-04-17 16:05:31 -05004498
James Smart858c9f62007-06-17 19:56:39 -05004499
4500 if (!timer_pending(&vport->fc_disctmo)) {
4501 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4502 "set disc timer: tmo:x%x state:x%x flg:x%x",
4503 tmo, vport->port_state, vport->fc_flag);
4504 }
4505
James Smart256ec0d2013-04-17 20:14:58 -04004506 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004507 spin_lock_irq(shost->host_lock);
4508 vport->fc_flag |= FC_DISC_TMO;
4509 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004510
4511 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004512 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4513 "0247 Start Discovery Timer state x%x "
4514 "Data: x%x x%lx x%x x%x\n",
4515 vport->port_state, tmo,
4516 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4517 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004518
4519 return;
4520}
4521
4522/*
4523 * Cancel rescue timer for Discovery / RSCN handling
4524 */
4525int
James Smart2e0fef82007-06-17 19:56:36 -05004526lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004527{
James Smart2e0fef82007-06-17 19:56:36 -05004528 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004529 unsigned long iflags;
4530
James Smart858c9f62007-06-17 19:56:39 -05004531 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4532 "can disc timer: state:x%x rtry:x%x flg:x%x",
4533 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4534
dea31012005-04-17 16:05:31 -05004535 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004536 if (vport->fc_flag & FC_DISC_TMO) {
4537 spin_lock_irqsave(shost->host_lock, iflags);
4538 vport->fc_flag &= ~FC_DISC_TMO;
4539 spin_unlock_irqrestore(shost->host_lock, iflags);
4540 del_timer_sync(&vport->fc_disctmo);
4541 spin_lock_irqsave(&vport->work_port_lock, iflags);
4542 vport->work_port_events &= ~WORKER_DISC_TMO;
4543 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004544 }
4545
4546 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004547 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4548 "0248 Cancel Discovery Timer state x%x "
4549 "Data: x%x x%x x%x\n",
4550 vport->port_state, vport->fc_flag,
4551 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004552 return 0;
dea31012005-04-17 16:05:31 -05004553}
4554
4555/*
4556 * Check specified ring for outstanding IOCB on the SLI queue
4557 * Return true if iocb matches the specified nport
4558 */
4559int
James Smart2e0fef82007-06-17 19:56:36 -05004560lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4561 struct lpfc_sli_ring *pring,
4562 struct lpfc_iocbq *iocb,
4563 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004564{
James Smart2e0fef82007-06-17 19:56:36 -05004565 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004566 struct lpfc_vport *vport = ndlp->vport;
4567
4568 if (iocb->vport != vport)
4569 return 0;
4570
dea31012005-04-17 16:05:31 -05004571 if (pring->ringno == LPFC_ELS_RING) {
4572 switch (icmd->ulpCommand) {
4573 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004574 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004575 return 1;
dea31012005-04-17 16:05:31 -05004576 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004577 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4578 return 1;
dea31012005-04-17 16:05:31 -05004579 case CMD_XMIT_ELS_RSP64_CX:
4580 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004581 return 1;
dea31012005-04-17 16:05:31 -05004582 }
James Smart895427b2017-02-12 13:52:30 -08004583 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004584 /* Skip match check if waiting to relogin to FCP target */
4585 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004586 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004587 return 0;
dea31012005-04-17 16:05:31 -05004588 }
4589 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004590 return 1;
dea31012005-04-17 16:05:31 -05004591 }
dea31012005-04-17 16:05:31 -05004592 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004593 return 0;
dea31012005-04-17 16:05:31 -05004594}
4595
James Smart895427b2017-02-12 13:52:30 -08004596static void
4597__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4598 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4599 struct list_head *dequeue_list)
4600{
4601 struct lpfc_iocbq *iocb, *next_iocb;
4602
4603 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4604 /* Check to see if iocb matches the nport */
4605 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4606 /* match, dequeue */
4607 list_move_tail(&iocb->list, dequeue_list);
4608 }
4609}
4610
4611static void
4612lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4613 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4614{
4615 struct lpfc_sli *psli = &phba->sli;
4616 uint32_t i;
4617
4618 spin_lock_irq(&phba->hbalock);
4619 for (i = 0; i < psli->num_rings; i++)
4620 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4621 dequeue_list);
4622 spin_unlock_irq(&phba->hbalock);
4623}
4624
4625static void
4626lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4627 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4628{
4629 struct lpfc_sli_ring *pring;
4630 struct lpfc_queue *qp = NULL;
4631
4632 spin_lock_irq(&phba->hbalock);
4633 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4634 pring = qp->pring;
4635 if (!pring)
4636 continue;
James Smart2ade92a2017-03-04 09:30:38 -08004637 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004638 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
James Smart2ade92a2017-03-04 09:30:38 -08004639 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004640 }
4641 spin_unlock_irq(&phba->hbalock);
4642}
4643
dea31012005-04-17 16:05:31 -05004644/*
4645 * Free resources / clean up outstanding I/Os
4646 * associated with nlp_rpi in the LPFC_NODELIST entry.
4647 */
4648static int
James Smart2e0fef82007-06-17 19:56:36 -05004649lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004650{
James Smart2534ba72007-04-25 09:52:20 -04004651 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004652
James Smart92d7f7b2007-06-17 19:56:38 -05004653 lpfc_fabric_abort_nport(ndlp);
4654
dea31012005-04-17 16:05:31 -05004655 /*
4656 * Everything that matches on txcmplq will be returned
4657 * by firmware with a no rpi error.
4658 */
James Smart40426292010-12-15 17:58:10 -05004659 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004660 if (phba->sli_rev != LPFC_SLI_REV4)
4661 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4662 else
4663 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004664 }
James Smart2534ba72007-04-25 09:52:20 -04004665
James Smarta257bf92009-04-06 18:48:10 -04004666 /* Cancel all the IOCBs from the completions list */
4667 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4668 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004669
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004670 return 0;
dea31012005-04-17 16:05:31 -05004671}
4672
James Smart086a3452012-08-14 14:25:21 -04004673/**
4674 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4675 * @phba: Pointer to HBA context object.
4676 * @pmb: Pointer to mailbox object.
4677 *
4678 * This function will issue an ELS LOGO command after completing
4679 * the UNREG_RPI.
4680 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004681static void
James Smart086a3452012-08-14 14:25:21 -04004682lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4683{
4684 struct lpfc_vport *vport = pmb->vport;
4685 struct lpfc_nodelist *ndlp;
4686
4687 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4688 if (!ndlp)
4689 return;
4690 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004691 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004692}
4693
dea31012005-04-17 16:05:31 -05004694/*
4695 * Free rpi associated with LPFC_NODELIST entry.
4696 * This routine is called from lpfc_freenode(), when we are removing
4697 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4698 * LOGO that completes successfully, and we are waiting to PLOGI back
4699 * to the remote NPort. In addition, it is called after we receive
4700 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4701 * we are waiting to PLOGI back to the remote NPort.
4702 */
4703int
James Smart2e0fef82007-06-17 19:56:36 -05004704lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004705{
James Smart2e0fef82007-06-17 19:56:36 -05004706 struct lpfc_hba *phba = vport->phba;
4707 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004708 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004709 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004710
James Smartf454a9a2013-09-06 12:20:07 -04004711 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4712 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4713 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4714 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4715 "3366 RPI x%x needs to be "
4716 "unregistered nlp_flag x%x "
4717 "did x%x\n",
4718 ndlp->nlp_rpi, ndlp->nlp_flag,
4719 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004720 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4721 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004722 /* SLI4 ports require the physical rpi value. */
4723 rpi = ndlp->nlp_rpi;
4724 if (phba->sli_rev == LPFC_SLI_REV4)
4725 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004726
James Smart6d368e52011-05-24 11:44:12 -04004727 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004728 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004729 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4730 mbox->context1 = ndlp;
4731 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4732 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004733 if (phba->sli_rev == LPFC_SLI_REV4 &&
4734 (!(vport->load_flag & FC_UNLOADING)) &&
4735 (bf_get(lpfc_sli_intf_if_type,
4736 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004737 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004738 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004739 mbox->context1 = lpfc_nlp_get(ndlp);
4740 mbox->mbox_cmpl =
4741 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004742 /*
4743 * accept PLOGIs after unreg_rpi_cmpl
4744 */
4745 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004746 } else
4747 mbox->mbox_cmpl =
4748 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004749 }
4750
James Smart0b727fe2007-10-27 13:37:25 -04004751 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004752 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004753 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004754 acc_plogi = 1;
4755 }
dea31012005-04-17 16:05:31 -05004756 }
dea31012005-04-17 16:05:31 -05004757 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004758
James Smart40426292010-12-15 17:58:10 -05004759 if (phba->sli_rev != LPFC_SLI_REV4)
4760 ndlp->nlp_rpi = 0;
4761 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004762 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004763 if (acc_plogi)
4764 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004765 return 1;
4766 }
James Smart7c5e5182015-05-22 10:42:43 -04004767 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004768 return 0;
4769}
4770
James Smartecfd03c2010-02-12 14:41:27 -05004771/**
4772 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4773 * @phba: pointer to lpfc hba data structure.
4774 *
4775 * This routine is invoked to unregister all the currently registered RPIs
4776 * to the HBA.
4777 **/
4778void
4779lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4780{
4781 struct lpfc_vport **vports;
4782 struct lpfc_nodelist *ndlp;
4783 struct Scsi_Host *shost;
4784 int i;
4785
4786 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004787 if (!vports) {
4788 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4789 "2884 Vport array allocation failed \n");
4790 return;
4791 }
James Smartecfd03c2010-02-12 14:41:27 -05004792 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4793 shost = lpfc_shost_from_vport(vports[i]);
4794 spin_lock_irq(shost->host_lock);
4795 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004796 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004797 /* The mempool_alloc might sleep */
4798 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004799 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004800 spin_lock_irq(shost->host_lock);
4801 }
James Smartecfd03c2010-02-12 14:41:27 -05004802 }
4803 spin_unlock_irq(shost->host_lock);
4804 }
4805 lpfc_destroy_vport_work_array(phba, vports);
4806}
4807
James Smart92d7f7b2007-06-17 19:56:38 -05004808void
4809lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4810{
4811 struct lpfc_hba *phba = vport->phba;
4812 LPFC_MBOXQ_t *mbox;
4813 int rc;
4814
James Smart5af5eee2010-10-22 11:06:38 -04004815 if (phba->sli_rev == LPFC_SLI_REV4) {
4816 lpfc_sli4_unreg_all_rpis(vport);
4817 return;
4818 }
4819
James Smart92d7f7b2007-06-17 19:56:38 -05004820 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4821 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004822 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4823 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004824 mbox->vport = vport;
4825 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004826 mbox->context1 = NULL;
4827 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004828 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004829 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004830
4831 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4832 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4833 "1836 Could not issue "
4834 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004835 }
4836}
4837
4838void
4839lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4840{
4841 struct lpfc_hba *phba = vport->phba;
4842 LPFC_MBOXQ_t *mbox;
4843 int rc;
4844
4845 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4846 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004847 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4848 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004849 mbox->vport = vport;
4850 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004851 mbox->context1 = NULL;
4852 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004853 if (rc != MBX_TIMEOUT)
4854 mempool_free(mbox, phba->mbox_mem_pool);
4855
4856 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004857 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4858 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004859 "unreg_did (default rpis) status %d\n",
4860 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004861 }
4862}
4863
dea31012005-04-17 16:05:31 -05004864/*
4865 * Free resources associated with LPFC_NODELIST entry
4866 * so it can be freed.
4867 */
4868static int
James Smart2e0fef82007-06-17 19:56:36 -05004869lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004870{
James Smart2e0fef82007-06-17 19:56:36 -05004871 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4872 struct lpfc_hba *phba = vport->phba;
4873 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004874 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004875
4876 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004877 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4878 "0900 Cleanup node for NPort x%x "
4879 "Data: x%x x%x x%x\n",
4880 ndlp->nlp_DID, ndlp->nlp_flag,
4881 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004882 if (NLP_CHK_FREE_REQ(ndlp)) {
4883 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4884 "0280 lpfc_cleanup_node: ndlp:x%p "
4885 "usgmap:x%x refcnt:%d\n",
4886 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004887 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004888 lpfc_dequeue_node(vport, ndlp);
4889 } else {
4890 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4891 "0281 lpfc_cleanup_node: ndlp:x%p "
4892 "usgmap:x%x refcnt:%d\n",
4893 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004894 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004895 lpfc_disable_node(vport, ndlp);
4896 }
dea31012005-04-17 16:05:31 -05004897
James Smart086a3452012-08-14 14:25:21 -04004898
4899 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4900
dea31012005-04-17 16:05:31 -05004901 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4902 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004903 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004904 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004905 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4906 mb->context2 = NULL;
4907 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4908 }
4909 }
James Smart33ccf8d2006-08-17 11:57:58 -04004910
James Smart2e0fef82007-06-17 19:56:36 -05004911 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004912 /* Cleanup REG_LOGIN completions which are not yet processed */
4913 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4914 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004915 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004916 (ndlp != (struct lpfc_nodelist *) mb->context2))
4917 continue;
4918
4919 mb->context2 = NULL;
4920 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4921 }
4922
dea31012005-04-17 16:05:31 -05004923 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004924 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004925 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004926 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004927 mp = (struct lpfc_dmabuf *) (mb->context1);
4928 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004929 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004930 kfree(mp);
4931 }
4932 list_del(&mb->list);
4933 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004934 /* We shall not invoke the lpfc_nlp_put to decrement
4935 * the ndlp reference count as we are in the process
4936 * of lpfc_nlp_release.
4937 */
dea31012005-04-17 16:05:31 -05004938 }
4939 }
James Smart2e0fef82007-06-17 19:56:36 -05004940 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004941
James Smarte47c9092008-02-08 18:49:26 -05004942 lpfc_els_abort(phba, ndlp);
4943
James Smart2e0fef82007-06-17 19:56:36 -05004944 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004945 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004946 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004947
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004948 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004949 del_timer_sync(&ndlp->nlp_delayfunc);
4950
James Smart0d2b6b82008-06-14 22:52:47 -04004951 list_del_init(&ndlp->els_retry_evt.evt_listp);
4952 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004953 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004954 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004955
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004956 return 0;
dea31012005-04-17 16:05:31 -05004957}
4958
4959/*
4960 * Check to see if we can free the nlp back to the freelist.
4961 * If we are in the middle of using the nlp in the discovery state
4962 * machine, defer the free till we reach the end of the state machine.
4963 */
James Smart329f9bc2007-04-25 09:53:01 -04004964static void
James Smart2e0fef82007-06-17 19:56:36 -05004965lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004966{
James Smarta8adb832007-10-27 13:37:53 -04004967 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004968 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004969 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004970 LPFC_MBOXQ_t *mbox;
4971 int rc;
dea31012005-04-17 16:05:31 -05004972
James Smart0d2b6b82008-06-14 22:52:47 -04004973 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004974 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004975 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4976 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004977 /* For this case we need to cleanup the default rpi
4978 * allocated by the firmware.
4979 */
James Smartbe6bb942015-04-07 15:07:22 -04004980 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4981 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4982 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004983 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004984 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004985 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4986 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004987 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004988 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004989 if (rc) {
4990 mempool_free(mbox, phba->mbox_mem_pool);
4991 }
4992 else {
4993 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4994 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4995 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004996 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004997 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4998 if (rc == MBX_NOT_FINISHED) {
4999 mempool_free(mbox, phba->mbox_mem_pool);
5000 }
5001 }
5002 }
5003 }
James Smart2e0fef82007-06-17 19:56:36 -05005004 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04005005
James Smart2e0fef82007-06-17 19:56:36 -05005006 /*
James Smart466e8402015-05-21 13:55:28 -04005007 * ndlp->rport must be set to NULL before it reaches here
5008 * i.e. break rport/node link before doing lpfc_nlp_put for
5009 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05005010 */
James Smart92d7f7b2007-06-17 19:56:38 -05005011 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04005012 /*
5013 * extra lpfc_nlp_put dropped the reference of ndlp
5014 * for registered rport so need to cleanup rport
5015 */
5016 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5017 "0940 removed node x%p DID x%x "
5018 " rport not null %p\n",
5019 ndlp, ndlp->nlp_DID, ndlp->rport);
5020 rport = ndlp->rport;
5021 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04005022 rdata->pnode = NULL;
5023 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04005024 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05005025 }
dea31012005-04-17 16:05:31 -05005026}
5027
5028static int
James Smart2e0fef82007-06-17 19:56:36 -05005029lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5030 uint32_t did)
dea31012005-04-17 16:05:31 -05005031{
James Smart2e0fef82007-06-17 19:56:36 -05005032 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005033
5034 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005035 return 0;
dea31012005-04-17 16:05:31 -05005036
dea31012005-04-17 16:05:31 -05005037 /* First check for Direct match */
5038 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005039 return 1;
dea31012005-04-17 16:05:31 -05005040
5041 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005042 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005043 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005044 return 0;
dea31012005-04-17 16:05:31 -05005045 }
5046
5047 matchdid.un.word = did;
5048 ndlpdid.un.word = ndlp->nlp_DID;
5049 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5050 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5051 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005052 /* This code is supposed to match the ID
5053 * for a private loop device that is
5054 * connect to fl_port. But we need to
5055 * check that the port did not just go
5056 * from pt2pt to fabric or we could end
5057 * up matching ndlp->nlp_DID 000001 to
5058 * fabric DID 0x20101
5059 */
dea31012005-04-17 16:05:31 -05005060 if ((ndlpdid.un.b.domain == 0) &&
5061 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005062 if (ndlpdid.un.b.id &&
5063 vport->phba->fc_topology ==
5064 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005065 return 1;
dea31012005-04-17 16:05:31 -05005066 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005067 return 0;
dea31012005-04-17 16:05:31 -05005068 }
5069
5070 matchdid.un.word = ndlp->nlp_DID;
5071 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5072 (mydid.un.b.area == ndlpdid.un.b.area)) {
5073 if ((matchdid.un.b.domain == 0) &&
5074 (matchdid.un.b.area == 0)) {
5075 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005076 return 1;
dea31012005-04-17 16:05:31 -05005077 }
5078 }
5079 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005080 return 0;
dea31012005-04-17 16:05:31 -05005081}
5082
James Smart685f0bf2007-04-25 09:53:08 -04005083/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005084static struct lpfc_nodelist *
5085__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005086{
James Smart2fb9bd82006-12-02 13:33:57 -05005087 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005088 uint32_t data1;
5089
James Smart2e0fef82007-06-17 19:56:36 -05005090 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5091 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005092 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5093 ((uint32_t) ndlp->nlp_xri << 16) |
5094 ((uint32_t) ndlp->nlp_type << 8) |
5095 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005096 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5097 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005098 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005099 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005100 ndlp->nlp_flag, data1,
5101 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005102 return ndlp;
dea31012005-04-17 16:05:31 -05005103 }
5104 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005105
dea31012005-04-17 16:05:31 -05005106 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005107 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5108 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005109 return NULL;
5110}
5111
5112struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005113lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005114{
James Smart2e0fef82007-06-17 19:56:36 -05005115 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005116 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005117 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005118
James Smartfedd3b72011-02-16 12:39:24 -05005119 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005120 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005121 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005122 return ndlp;
5123}
5124
5125struct lpfc_nodelist *
5126lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5127{
5128 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5129 struct lpfc_nodelist *ndlp;
5130
5131 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005132 if (!ndlp) {
James Smart1c5b12f2017-04-21 16:05:03 -07005133 if (vport->phba->nvmet_support)
5134 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005135 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5136 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005137 return NULL;
James Smart9d3d3402017-04-21 16:05:00 -07005138 ndlp = lpfc_nlp_init(vport, did);
dea31012005-04-17 16:05:31 -05005139 if (!ndlp)
5140 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005141 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5142 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005143 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005144 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005145 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005146 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005147 if (vport->phba->nvmet_support)
5148 return NULL;
James Smarte47c9092008-02-08 18:49:26 -05005149 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5150 if (!ndlp)
5151 return NULL;
5152 spin_lock_irq(shost->host_lock);
5153 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5154 spin_unlock_irq(shost->host_lock);
5155 return ndlp;
dea31012005-04-17 16:05:31 -05005156 }
James Smarte47c9092008-02-08 18:49:26 -05005157
James Smart1c5b12f2017-04-21 16:05:03 -07005158 /* The NVME Target does not want to actively manage an rport.
5159 * The goal is to allow the target to reset its state and clear
5160 * pending IO in preparation for the initiator to recover.
5161 */
James Smart58da1ff2008-04-07 10:15:56 -04005162 if ((vport->fc_flag & FC_RSCN_MODE) &&
5163 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005164 if (lpfc_rscn_payload_check(vport, did)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005165
5166 /* Since this node is marked for discovery,
5167 * delay timeout is not needed.
5168 */
5169 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5170
5171 /* NVME Target mode waits until rport is known to be
5172 * impacted by the RSCN before it transitions. No
5173 * active management - just go to NPR provided the
5174 * node had a valid login.
5175 */
5176 if (vport->phba->nvmet_support)
5177 return ndlp;
5178
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005179 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005180 * we don't need to try to discover it again.
5181 */
5182 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5183 return NULL;
5184
James Smarta257bf92009-04-06 18:48:10 -04005185 spin_lock_irq(shost->host_lock);
5186 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5187 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005188 } else
dea31012005-04-17 16:05:31 -05005189 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005190 } else {
James Smart1c5b12f2017-04-21 16:05:03 -07005191 /* If the initiator received a PLOGI from this NPort or if the
5192 * initiator is already in the process of discovery on it,
5193 * there's no need to try to discover it again.
James Smart87af33f2007-10-27 13:37:43 -04005194 */
James Smart685f0bf2007-04-25 09:53:08 -04005195 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005196 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
James Smart1c5b12f2017-04-21 16:05:03 -07005197 (!vport->phba->nvmet_support &&
5198 ndlp->nlp_flag & NLP_RCV_PLOGI))
dea31012005-04-17 16:05:31 -05005199 return NULL;
James Smart1c5b12f2017-04-21 16:05:03 -07005200
James Smart8c258642017-02-12 13:52:36 -08005201 if (vport->phba->nvmet_support)
5202 return ndlp;
James Smart1c5b12f2017-04-21 16:05:03 -07005203
5204 /* Moving to NPR state clears unsolicited flags and
5205 * allows for rediscovery
5206 */
5207 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5208
James Smart2e0fef82007-06-17 19:56:36 -05005209 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005210 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005211 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005212 }
5213 return ndlp;
5214}
5215
5216/* Build a list of nodes to discover based on the loopmap */
5217void
James Smart2e0fef82007-06-17 19:56:36 -05005218lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005219{
James Smart2e0fef82007-06-17 19:56:36 -05005220 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005221 int j;
5222 uint32_t alpa, index;
5223
James Smart2e0fef82007-06-17 19:56:36 -05005224 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005225 return;
James Smart2e0fef82007-06-17 19:56:36 -05005226
James Smart76a95d72010-11-20 23:11:48 -05005227 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005228 return;
dea31012005-04-17 16:05:31 -05005229
5230 /* Check for loop map present or not */
5231 if (phba->alpa_map[0]) {
5232 for (j = 1; j <= phba->alpa_map[0]; j++) {
5233 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005234 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005235 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005236 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005237 }
5238 } else {
5239 /* No alpamap, so try all alpa's */
5240 for (j = 0; j < FC_MAXLOOP; j++) {
5241 /* If cfg_scan_down is set, start from highest
5242 * ALPA (0xef) to lowest (0x1).
5243 */
James Smart3de2a652007-08-02 11:09:59 -04005244 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005245 index = j;
5246 else
5247 index = FC_MAXLOOP - j - 1;
5248 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005249 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005250 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005251 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005252 }
5253 }
5254 return;
5255}
5256
James Smart895427b2017-02-12 13:52:30 -08005257/* SLI3 only */
dea31012005-04-17 16:05:31 -05005258void
James Smart2e0fef82007-06-17 19:56:36 -05005259lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005260{
dea31012005-04-17 16:05:31 -05005261 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005262 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005263 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5264 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005265 int rc;
5266
James Smart92d7f7b2007-06-17 19:56:38 -05005267 /*
5268 * if it's not a physical port or if we already send
5269 * clear_la then don't send it.
5270 */
5271 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005272 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5273 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005274 return;
5275
James Smart2e0fef82007-06-17 19:56:36 -05005276 /* Link up discovery */
5277 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5278 phba->link_state = LPFC_CLEAR_LA;
5279 lpfc_clear_la(phba, mbox);
5280 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5281 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005282 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005283 if (rc == MBX_NOT_FINISHED) {
5284 mempool_free(mbox, phba->mbox_mem_pool);
5285 lpfc_disc_flush_list(vport);
5286 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5287 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005288 phba->link_state = LPFC_HBA_ERROR;
5289 }
5290 }
5291}
5292
5293/* Reg_vpi to tell firmware to resume normal operations */
5294void
5295lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5296{
5297 LPFC_MBOXQ_t *regvpimbox;
5298
5299 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5300 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005301 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005302 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5303 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005304 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005305 == MBX_NOT_FINISHED) {
5306 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005307 }
5308 }
5309}
5310
5311/* Start Link up / RSCN discovery on NPR nodes */
5312void
5313lpfc_disc_start(struct lpfc_vport *vport)
5314{
5315 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5316 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005317 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005318 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005319
James Smarte74c03c2013-04-17 20:15:19 -04005320 if (!lpfc_is_link_up(phba)) {
5321 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5322 "3315 Link is not up %x\n",
5323 phba->link_state);
dea31012005-04-17 16:05:31 -05005324 return;
James Smarte74c03c2013-04-17 20:15:19 -04005325 }
James Smart2e0fef82007-06-17 19:56:36 -05005326
5327 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005328 clear_la_pending = 1;
5329 else
5330 clear_la_pending = 0;
5331
James Smart2e0fef82007-06-17 19:56:36 -05005332 if (vport->port_state < LPFC_VPORT_READY)
5333 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005334
James Smart2e0fef82007-06-17 19:56:36 -05005335 lpfc_set_disctmo(vport);
5336
James Smart2e0fef82007-06-17 19:56:36 -05005337 vport->fc_prevDID = vport->fc_myDID;
5338 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005339
5340 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005341 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5342 "0202 Start Discovery hba state x%x "
5343 "Data: x%x x%x x%x\n",
5344 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5345 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005346
5347 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005348 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005349
5350 if (num_sent)
5351 return;
5352
James Smart6fa139f2013-04-17 20:20:00 -04005353 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005354 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005355 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005356 !(vport->fc_flag & FC_RSCN_MODE) &&
5357 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005358 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005359 lpfc_issue_reg_vpi(phba, vport);
5360 return;
5361 }
James Smart2e0fef82007-06-17 19:56:36 -05005362
James Smart92d7f7b2007-06-17 19:56:38 -05005363 /*
5364 * For SLI2, we need to set port_state to READY and continue
5365 * discovery.
5366 */
5367 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5368 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005369 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005370
5371 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005372 vport->num_disc_nodes = 0;
5373 /* go thru NPR nodes and issue ELS PLOGIs */
5374 if (vport->fc_npr_cnt)
5375 lpfc_els_disc_plogi(vport);
5376
5377 if (!vport->num_disc_nodes) {
5378 spin_lock_irq(shost->host_lock);
5379 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5380 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005381 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005382 }
5383 }
James Smart92d7f7b2007-06-17 19:56:38 -05005384 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005385 } else {
5386 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005387 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005388
5389 if (num_sent)
5390 return;
5391
James Smart2e0fef82007-06-17 19:56:36 -05005392 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005393 /* Check to see if more RSCNs came in while we
5394 * were processing this one.
5395 */
James Smart2e0fef82007-06-17 19:56:36 -05005396 if ((vport->fc_rscn_id_cnt == 0) &&
5397 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5398 spin_lock_irq(shost->host_lock);
5399 vport->fc_flag &= ~FC_RSCN_MODE;
5400 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005401 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005402 } else
James Smart2e0fef82007-06-17 19:56:36 -05005403 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005404 }
5405 }
5406 return;
5407}
5408
5409/*
5410 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5411 * ring the match the sppecified nodelist.
5412 */
5413static void
James Smart2e0fef82007-06-17 19:56:36 -05005414lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005415{
James Smart2534ba72007-04-25 09:52:20 -04005416 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005417 struct lpfc_sli *psli;
5418 IOCB_t *icmd;
5419 struct lpfc_iocbq *iocb, *next_iocb;
5420 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005421
5422 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005423 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005424
5425 /* Error matching iocb on txq or txcmplq
5426 * First check the txq.
5427 */
James Smart2e0fef82007-06-17 19:56:36 -05005428 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005429 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5430 if (iocb->context1 != ndlp) {
5431 continue;
5432 }
5433 icmd = &iocb->iocb;
5434 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5435 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5436
James Smart2534ba72007-04-25 09:52:20 -04005437 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005438 }
5439 }
5440
5441 /* Next check the txcmplq */
5442 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5443 if (iocb->context1 != ndlp) {
5444 continue;
5445 }
5446 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005447 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5448 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005449 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005450 }
5451 }
James Smart2e0fef82007-06-17 19:56:36 -05005452 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005453
James Smarta257bf92009-04-06 18:48:10 -04005454 /* Cancel all the IOCBs from the completions list */
5455 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5456 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005457}
5458
Adrian Bunka6ababd2007-11-05 18:07:33 +01005459static void
James Smart2e0fef82007-06-17 19:56:36 -05005460lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005461{
5462 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005463 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005464
James Smart2e0fef82007-06-17 19:56:36 -05005465 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5466 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005467 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005468 if (!NLP_CHK_NODE_ACT(ndlp))
5469 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005470 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5471 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5472 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005473 }
dea31012005-04-17 16:05:31 -05005474 }
5475 }
dea31012005-04-17 16:05:31 -05005476}
5477
James Smart92d7f7b2007-06-17 19:56:38 -05005478void
5479lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5480{
5481 lpfc_els_flush_rscn(vport);
5482 lpfc_els_flush_cmd(vport);
5483 lpfc_disc_flush_list(vport);
5484}
5485
dea31012005-04-17 16:05:31 -05005486/*****************************************************************************/
5487/*
5488 * NAME: lpfc_disc_timeout
5489 *
5490 * FUNCTION: Fibre Channel driver discovery timeout routine.
5491 *
5492 * EXECUTION ENVIRONMENT: interrupt only
5493 *
5494 * CALLED FROM:
5495 * Timer function
5496 *
5497 * RETURNS:
5498 * none
5499 */
5500/*****************************************************************************/
5501void
5502lpfc_disc_timeout(unsigned long ptr)
5503{
James Smart2e0fef82007-06-17 19:56:36 -05005504 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5505 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005506 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005507 unsigned long flags = 0;
5508
5509 if (unlikely(!phba))
5510 return;
5511
James Smart5e9d9b82008-06-14 22:52:53 -04005512 spin_lock_irqsave(&vport->work_port_lock, flags);
5513 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5514 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005515 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005516 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005517
James Smart5e9d9b82008-06-14 22:52:53 -04005518 if (!tmo_posted)
5519 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005520 return;
5521}
5522
5523static void
James Smart2e0fef82007-06-17 19:56:36 -05005524lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005525{
James Smart2e0fef82007-06-17 19:56:36 -05005526 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5527 struct lpfc_hba *phba = vport->phba;
5528 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005529 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005530 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005531 int rc, clrlaerr = 0;
5532
James Smart2e0fef82007-06-17 19:56:36 -05005533 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005534 return;
5535
James Smart2e0fef82007-06-17 19:56:36 -05005536 spin_lock_irq(shost->host_lock);
5537 vport->fc_flag &= ~FC_DISC_TMO;
5538 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005539
James Smart858c9f62007-06-17 19:56:39 -05005540 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5541 "disc timeout: state:x%x rtry:x%x flg:x%x",
5542 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5543
James Smart2e0fef82007-06-17 19:56:36 -05005544 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005545
5546 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005547 /*
5548 * port_state is identically LPFC_LOCAL_CFG_LINK while
5549 * waiting for FAN timeout
5550 */
James Smarte8b62012007-08-02 11:10:09 -04005551 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5552 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005553
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005554 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005555 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005556 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005557 if (!NLP_CHK_NODE_ACT(ndlp))
5558 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005559 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5560 continue;
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005561 if (ndlp->nlp_type & NLP_FABRIC) {
5562 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005563 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005564
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005565 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005566 /* Fail outstanding IO now since device
5567 * is marked for PLOGI.
5568 */
James Smart2e0fef82007-06-17 19:56:36 -05005569 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f8735b2006-02-28 19:25:23 -05005570 }
5571 }
James Smart92d7f7b2007-06-17 19:56:38 -05005572 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005573 if (phba->sli_rev <= LPFC_SLI_REV3)
5574 lpfc_initial_flogi(vport);
5575 else
5576 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005577 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005578 }
dea31012005-04-17 16:05:31 -05005579 break;
5580
James Smart92d7f7b2007-06-17 19:56:38 -05005581 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005582 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005583 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005584 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005585 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5586 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005587 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005588
5589 /* Assume no Fabric and go on with discovery.
5590 * Check for outstanding ELS FLOGI to abort.
5591 */
5592
5593 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005594 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005595
5596 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005597 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005598 break;
5599
5600 case LPFC_FABRIC_CFG_LINK:
5601 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5602 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005603 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5604 "0223 Timeout while waiting for "
5605 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005606 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005607 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005608 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005609 lpfc_els_abort(phba, ndlp);
5610
5611 /* ReStart discovery */
5612 goto restart_disc;
dea31012005-04-17 16:05:31 -05005613
5614 case LPFC_NS_QRY:
5615 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005616 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5617 "0224 NameServer Query timeout "
5618 "Data: x%x x%x\n",
5619 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005620
James Smart92d7f7b2007-06-17 19:56:38 -05005621 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5622 /* Try it one more time */
5623 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005624 vport->gidft_inp = 0;
5625 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005626 if (rc == 0)
5627 break;
dea31012005-04-17 16:05:31 -05005628 }
James Smart92d7f7b2007-06-17 19:56:38 -05005629 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005630
James Smart87af33f2007-10-27 13:37:43 -04005631restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005632 /*
5633 * Discovery is over.
5634 * set port_state to PORT_READY if SLI2.
5635 * cmpl_reg_vpi will set port_state to READY for SLI3.
5636 */
James Smart3772a992009-05-22 14:50:54 -04005637 if (phba->sli_rev < LPFC_SLI_REV4) {
5638 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5639 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005640 else {
James Smart3772a992009-05-22 14:50:54 -04005641 lpfc_issue_clear_la(phba, vport);
5642 vport->port_state = LPFC_VPORT_READY;
5643 }
dea31012005-04-17 16:05:31 -05005644 }
5645
5646 /* Setup and issue mailbox INITIALIZE LINK command */
5647 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5648 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005649 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5650 "0206 Device Discovery "
5651 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005652 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005653 break;
5654 }
5655
5656 lpfc_linkdown(phba);
5657 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5658 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005659 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005660 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005661 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005662 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005663 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005664 if (rc == MBX_NOT_FINISHED)
5665 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5666
5667 break;
5668
5669 case LPFC_DISC_AUTH:
5670 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005671 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5672 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005673 lpfc_disc_flush_list(vport);
5674
James Smart92d7f7b2007-06-17 19:56:38 -05005675 /*
5676 * set port_state to PORT_READY if SLI2.
5677 * cmpl_reg_vpi will set port_state to READY for SLI3.
5678 */
James Smart3772a992009-05-22 14:50:54 -04005679 if (phba->sli_rev < LPFC_SLI_REV4) {
5680 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5681 lpfc_issue_reg_vpi(phba, vport);
5682 else { /* NPIV Not enabled */
5683 lpfc_issue_clear_la(phba, vport);
5684 vport->port_state = LPFC_VPORT_READY;
5685 }
dea31012005-04-17 16:05:31 -05005686 }
5687 break;
5688
James Smart2e0fef82007-06-17 19:56:36 -05005689 case LPFC_VPORT_READY:
5690 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005691 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5692 "0231 RSCN timeout Data: x%x "
5693 "x%x\n",
5694 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005695
5696 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005697 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005698
James Smart2e0fef82007-06-17 19:56:36 -05005699 lpfc_els_flush_rscn(vport);
5700 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005701 }
5702 break;
James Smart2e0fef82007-06-17 19:56:36 -05005703
James Smart92d7f7b2007-06-17 19:56:38 -05005704 default:
James Smarte8b62012007-08-02 11:10:09 -04005705 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005706 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005707 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005708 break;
5709 }
5710
5711 switch (phba->link_state) {
5712 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005713 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005714 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5715 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005716 clrlaerr = 1;
5717 break;
5718
James Smart09372822008-01-11 01:52:54 -05005719 case LPFC_LINK_UP:
5720 lpfc_issue_clear_la(phba, vport);
5721 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005722 case LPFC_LINK_UNKNOWN:
5723 case LPFC_WARM_START:
5724 case LPFC_INIT_START:
5725 case LPFC_INIT_MBX_CMDS:
5726 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005727 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005728 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5729 "0230 Unexpected timeout, hba link "
5730 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005731 clrlaerr = 1;
5732 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005733
5734 case LPFC_HBA_READY:
5735 break;
dea31012005-04-17 16:05:31 -05005736 }
5737
5738 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005739 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005740 if (phba->sli_rev != LPFC_SLI_REV4) {
5741 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5742 ~LPFC_STOP_IOCB_EVENT;
5743 psli->sli3_ring[LPFC_FCP_RING].flag &=
5744 ~LPFC_STOP_IOCB_EVENT;
5745 }
James Smart2e0fef82007-06-17 19:56:36 -05005746 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005747 }
dea31012005-04-17 16:05:31 -05005748 return;
5749}
5750
dea31012005-04-17 16:05:31 -05005751/*
5752 * This routine handles processing a NameServer REG_LOGIN mailbox
5753 * command upon completion. It is setup in the LPFC_MBOXQ
5754 * as the completion routine when the command is
5755 * handed off to the SLI layer.
5756 */
5757void
James Smart2e0fef82007-06-17 19:56:36 -05005758lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005759{
James Smart04c68492009-05-22 14:52:52 -04005760 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005761 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5762 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5763 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005764
5765 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005766 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005767
James Smart6d368e52011-05-24 11:44:12 -04005768 if (phba->sli_rev < LPFC_SLI_REV4)
5769 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005770 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005771 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005772 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005773 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5774 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5775 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005776 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005777 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005778 /*
5779 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005780 * 0xfffffa (FDMI well known port).
5781 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5782 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005783 */
James Smart4258e982015-12-16 18:11:58 -05005784 if (vport->port_type == LPFC_PHYSICAL_PORT)
5785 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005786 else
James Smart4258e982015-12-16 18:11:58 -05005787 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5788
dea31012005-04-17 16:05:31 -05005789
James Smartfa4066b2008-01-11 01:53:27 -05005790 /* decrement the node reference count held for this callback
5791 * function.
5792 */
James Smart329f9bc2007-04-25 09:53:01 -04005793 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005794 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5795 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005796 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005797
5798 return;
5799}
5800
James Smart685f0bf2007-04-25 09:53:08 -04005801static int
5802lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5803{
5804 uint16_t *rpi = param;
5805
James Smarteff4a012012-01-18 16:25:25 -05005806 /* check for active node */
5807 if (!NLP_CHK_NODE_ACT(ndlp))
5808 return 0;
5809
James Smart685f0bf2007-04-25 09:53:08 -04005810 return ndlp->nlp_rpi == *rpi;
5811}
5812
5813static int
5814lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5815{
5816 return memcmp(&ndlp->nlp_portname, param,
5817 sizeof(ndlp->nlp_portname)) == 0;
5818}
5819
Adrian Bunka6ababd2007-11-05 18:07:33 +01005820static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005821__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005822{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005823 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005824
James Smart2e0fef82007-06-17 19:56:36 -05005825 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005826 if (filter(ndlp, param)) {
5827 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5828 "3185 FIND node filter %p DID "
5829 "Data: x%p x%x x%x\n",
5830 filter, ndlp, ndlp->nlp_DID,
5831 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005832 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005833 }
James Smart685f0bf2007-04-25 09:53:08 -04005834 }
James Smart34f5ad82012-08-03 12:35:03 -04005835 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5836 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005837 return NULL;
dea31012005-04-17 16:05:31 -05005838}
5839
James Smart685f0bf2007-04-25 09:53:08 -04005840/*
5841 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005842 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005843 */
5844struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005845__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005846{
James Smart2e0fef82007-06-17 19:56:36 -05005847 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005848}
5849
James Smart488d1462006-03-07 15:02:37 -05005850/*
James Smart685f0bf2007-04-25 09:53:08 -04005851 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005852 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005853 */
5854struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005855lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005856{
James Smart2e0fef82007-06-17 19:56:36 -05005857 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005858 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005859
James Smart2e0fef82007-06-17 19:56:36 -05005860 spin_lock_irq(shost->host_lock);
5861 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5862 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005863 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005864}
5865
James Smartcb69f7d2011-12-13 13:21:57 -05005866/*
5867 * This routine looks up the ndlp lists for the given RPI. If the rpi
5868 * is found, the routine returns the node element list pointer else
5869 * return NULL.
5870 */
5871struct lpfc_nodelist *
5872lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5873{
5874 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5875 struct lpfc_nodelist *ndlp;
5876
5877 spin_lock_irq(shost->host_lock);
5878 ndlp = __lpfc_findnode_rpi(vport, rpi);
5879 spin_unlock_irq(shost->host_lock);
5880 return ndlp;
5881}
5882
5883/**
5884 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5885 * @phba: pointer to lpfc hba data structure.
5886 * @vpi: the physical host virtual N_Port identifier.
5887 *
5888 * This routine finds a vport on a HBA (referred by @phba) through a
5889 * @vpi. The function walks the HBA's vport list and returns the address
5890 * of the vport with the matching @vpi.
5891 *
5892 * Return code
5893 * NULL - No vport with the matching @vpi found
5894 * Otherwise - Address to the vport with the matching @vpi.
5895 **/
5896struct lpfc_vport *
5897lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5898{
5899 struct lpfc_vport *vport;
5900 unsigned long flags;
5901 int i = 0;
5902
5903 /* The physical ports are always vpi 0 - translate is unnecessary. */
5904 if (vpi > 0) {
5905 /*
5906 * Translate the physical vpi to the logical vpi. The
5907 * vport stores the logical vpi.
5908 */
5909 for (i = 0; i < phba->max_vpi; i++) {
5910 if (vpi == phba->vpi_ids[i])
5911 break;
5912 }
5913
5914 if (i >= phba->max_vpi) {
5915 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5916 "2936 Could not find Vport mapped "
5917 "to vpi %d\n", vpi);
5918 return NULL;
5919 }
5920 }
5921
5922 spin_lock_irqsave(&phba->hbalock, flags);
5923 list_for_each_entry(vport, &phba->port_list, listentry) {
5924 if (vport->vpi == i) {
5925 spin_unlock_irqrestore(&phba->hbalock, flags);
5926 return vport;
5927 }
5928 }
5929 spin_unlock_irqrestore(&phba->hbalock, flags);
5930 return NULL;
5931}
5932
James Smart9d3d3402017-04-21 16:05:00 -07005933struct lpfc_nodelist *
5934lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005935{
James Smart9d3d3402017-04-21 16:05:00 -07005936 struct lpfc_nodelist *ndlp;
5937 int rpi = LPFC_RPI_ALLOC_ERROR;
5938
5939 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
5940 rpi = lpfc_sli4_alloc_rpi(vport->phba);
5941 if (rpi == LPFC_RPI_ALLOC_ERROR)
5942 return NULL;
5943 }
5944
5945 ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
5946 if (!ndlp) {
5947 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5948 lpfc_sli4_free_rpi(vport->phba, rpi);
5949 return NULL;
5950 }
5951
dea31012005-04-17 16:05:31 -05005952 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005953
5954 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005955 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005956 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07005957 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04005958 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5959 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5960 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5961 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005962 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005963 ndlp->nlp_usg_map, ndlp);
5964
James Smartcff261f2013-12-17 20:29:47 -05005965 ndlp->active_rrqs_xri_bitmap =
5966 mempool_alloc(vport->phba->active_rrq_pool,
5967 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005968 if (ndlp->active_rrqs_xri_bitmap)
5969 memset(ndlp->active_rrqs_xri_bitmap, 0,
5970 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005971 }
5972
James Smart725dd392013-10-10 12:23:30 -04005973
James Smart858c9f62007-06-17 19:56:39 -05005974
5975 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5976 "node init: did:x%x",
5977 ndlp->nlp_DID, 0, 0);
5978
James Smart9d3d3402017-04-21 16:05:00 -07005979 return ndlp;
dea31012005-04-17 16:05:31 -05005980}
James Smart329f9bc2007-04-25 09:53:01 -04005981
James Smart98c9ea52007-10-27 13:37:33 -04005982/* This routine releases all resources associated with a specifc NPort's ndlp
5983 * and mempool_free's the nodelist.
5984 */
James Smart311464e2007-08-02 11:10:37 -04005985static void
James Smart329f9bc2007-04-25 09:53:01 -04005986lpfc_nlp_release(struct kref *kref)
5987{
James Smarte47c9092008-02-08 18:49:26 -05005988 struct lpfc_hba *phba;
5989 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005990 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5991 kref);
James Smart858c9f62007-06-17 19:56:39 -05005992
5993 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5994 "node release: did:x%x flg:x%x type:x%x",
5995 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5996
James Smarte47c9092008-02-08 18:49:26 -05005997 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005998 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005999 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04006000 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006001 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006002
6003 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05006004 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05006005
6006 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04006007 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006008 spin_lock_irqsave(&phba->ndlp_lock, flags);
6009 NLP_CLR_NODE_ACT(ndlp);
6010 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05006011 if (phba->sli_rev == LPFC_SLI_REV4)
6012 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006013
6014 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04006015 if (NLP_CHK_FREE_REQ(ndlp)) {
6016 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05006017 if (phba->sli_rev == LPFC_SLI_REV4)
6018 mempool_free(ndlp->active_rrqs_xri_bitmap,
6019 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04006020 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04006021 }
James Smart329f9bc2007-04-25 09:53:01 -04006022}
6023
James Smart98c9ea52007-10-27 13:37:33 -04006024/* This routine bumps the reference count for a ndlp structure to ensure
6025 * that one discovery thread won't free a ndlp while another discovery thread
6026 * is using it.
6027 */
James Smart329f9bc2007-04-25 09:53:01 -04006028struct lpfc_nodelist *
6029lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6030{
James Smarte47c9092008-02-08 18:49:26 -05006031 struct lpfc_hba *phba;
6032 unsigned long flags;
6033
James Smart98c9ea52007-10-27 13:37:33 -04006034 if (ndlp) {
6035 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6036 "node get: did:x%x flg:x%x refcnt:x%x",
6037 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006038 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006039 /* The check of ndlp usage to prevent incrementing the
6040 * ndlp reference count that is in the process of being
6041 * released.
6042 */
James Smarta257bf92009-04-06 18:48:10 -04006043 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006044 spin_lock_irqsave(&phba->ndlp_lock, flags);
6045 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6046 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6047 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6048 "0276 lpfc_nlp_get: ndlp:x%p "
6049 "usgmap:x%x refcnt:%d\n",
6050 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006051 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006052 return NULL;
6053 } else
6054 kref_get(&ndlp->kref);
6055 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04006056 }
James Smart329f9bc2007-04-25 09:53:01 -04006057 return ndlp;
6058}
6059
James Smart98c9ea52007-10-27 13:37:33 -04006060/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05006061 * count goes to 0, this indicates the the associated nodelist should be
6062 * freed. Returning 1 indicates the ndlp resource has been released; on the
6063 * other hand, returning 0 indicates the ndlp resource has not been released
6064 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006065 */
James Smart329f9bc2007-04-25 09:53:01 -04006066int
6067lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6068{
James Smarte47c9092008-02-08 18:49:26 -05006069 struct lpfc_hba *phba;
6070 unsigned long flags;
6071
6072 if (!ndlp)
6073 return 1;
6074
6075 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6076 "node put: did:x%x flg:x%x refcnt:x%x",
6077 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006078 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006079 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006080 spin_lock_irqsave(&phba->ndlp_lock, flags);
6081 /* Check the ndlp memory free acknowledge flag to avoid the
6082 * possible race condition that kref_put got invoked again
6083 * after previous one has done ndlp memory free.
6084 */
6085 if (NLP_CHK_FREE_ACK(ndlp)) {
6086 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6087 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6088 "0274 lpfc_nlp_put: ndlp:x%p "
6089 "usgmap:x%x refcnt:%d\n",
6090 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006091 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006092 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006093 }
James Smarte47c9092008-02-08 18:49:26 -05006094 /* Check the ndlp inactivate log flag to avoid the possible
6095 * race condition that kref_put got invoked again after ndlp
6096 * is already in inactivating state.
6097 */
6098 if (NLP_CHK_IACT_REQ(ndlp)) {
6099 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6100 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6101 "0275 lpfc_nlp_put: ndlp:x%p "
6102 "usgmap:x%x refcnt:%d\n",
6103 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006104 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006105 return 1;
6106 }
6107 /* For last put, mark the ndlp usage flags to make sure no
6108 * other kref_get and kref_put on the same ndlp shall get
6109 * in between the process when the final kref_put has been
6110 * invoked on this ndlp.
6111 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006112 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006113 /* Indicate ndlp is put to inactive state. */
6114 NLP_SET_IACT_REQ(ndlp);
6115 /* Acknowledge ndlp memory free has been seen. */
6116 if (NLP_CHK_FREE_REQ(ndlp))
6117 NLP_SET_FREE_ACK(ndlp);
6118 }
6119 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6120 /* Note, the kref_put returns 1 when decrementing a reference
6121 * count that was 1, it invokes the release callback function,
6122 * but it still left the reference count as 1 (not actually
6123 * performs the last decrementation). Otherwise, it actually
6124 * decrements the reference count and returns 0.
6125 */
6126 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006127}
James Smart98c9ea52007-10-27 13:37:33 -04006128
6129/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006130 * by any other discovery thread. This routine returns 1 if the
6131 * ndlp has been freed. A return value of 0 indicates the ndlp is
6132 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006133 */
6134int
6135lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6136{
6137 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6138 "node not used: did:x%x flg:x%x refcnt:x%x",
6139 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006140 kref_read(&ndlp->kref));
6141 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006142 if (lpfc_nlp_put(ndlp))
6143 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006144 return 0;
6145}
James Smart6fb120a2009-05-22 14:52:59 -04006146
6147/**
6148 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6149 * @phba: Pointer to hba context object.
6150 *
6151 * This function iterate through all FC nodes associated
6152 * will all vports to check if there is any node with
6153 * fc_rports associated with it. If there is an fc_rport
6154 * associated with the node, then the node is either in
6155 * discovered state or its devloss_timer is pending.
6156 */
6157static int
6158lpfc_fcf_inuse(struct lpfc_hba *phba)
6159{
6160 struct lpfc_vport **vports;
6161 int i, ret = 0;
6162 struct lpfc_nodelist *ndlp;
6163 struct Scsi_Host *shost;
6164
6165 vports = lpfc_create_vport_work_array(phba);
6166
James Smart63e801c2010-11-20 23:14:19 -05006167 /* If driver cannot allocate memory, indicate fcf is in use */
6168 if (!vports)
6169 return 1;
6170
James Smart6fb120a2009-05-22 14:52:59 -04006171 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6172 shost = lpfc_shost_from_vport(vports[i]);
6173 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006174 /*
6175 * IF the CVL_RCVD bit is not set then we have sent the
6176 * flogi.
6177 * If dev_loss fires while we are waiting we do not want to
6178 * unreg the fcf.
6179 */
6180 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6181 spin_unlock_irq(shost->host_lock);
6182 ret = 1;
6183 goto out;
6184 }
James Smart6fb120a2009-05-22 14:52:59 -04006185 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6186 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6187 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6188 ret = 1;
6189 spin_unlock_irq(shost->host_lock);
6190 goto out;
James Smart80c17842012-03-01 22:35:45 -05006191 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6192 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006193 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006194 "2624 RPI %x DID %x flag %x "
6195 "still logged in\n",
6196 ndlp->nlp_rpi, ndlp->nlp_DID,
6197 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006198 }
6199 }
6200 spin_unlock_irq(shost->host_lock);
6201 }
6202out:
6203 lpfc_destroy_vport_work_array(phba, vports);
6204 return ret;
6205}
6206
6207/**
6208 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6209 * @phba: Pointer to hba context object.
6210 * @mboxq: Pointer to mailbox object.
6211 *
6212 * This function frees memory associated with the mailbox command.
6213 */
James Smart1b511972011-12-13 13:23:09 -05006214void
James Smart6fb120a2009-05-22 14:52:59 -04006215lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6216{
6217 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006218 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006219
6220 if (mboxq->u.mb.mbxStatus) {
6221 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6222 "2555 UNREG_VFI mbxStatus error x%x "
6223 "HBA state x%x\n",
6224 mboxq->u.mb.mbxStatus, vport->port_state);
6225 }
James Smart38b92ef2010-08-04 16:11:39 -04006226 spin_lock_irq(shost->host_lock);
6227 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6228 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006229 mempool_free(mboxq, phba->mbox_mem_pool);
6230 return;
6231}
6232
6233/**
6234 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6235 * @phba: Pointer to hba context object.
6236 * @mboxq: Pointer to mailbox object.
6237 *
6238 * This function frees memory associated with the mailbox command.
6239 */
6240static void
6241lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6242{
6243 struct lpfc_vport *vport = mboxq->vport;
6244
6245 if (mboxq->u.mb.mbxStatus) {
6246 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6247 "2550 UNREG_FCFI mbxStatus error x%x "
6248 "HBA state x%x\n",
6249 mboxq->u.mb.mbxStatus, vport->port_state);
6250 }
6251 mempool_free(mboxq, phba->mbox_mem_pool);
6252 return;
6253}
6254
6255/**
James Smartecfd03c2010-02-12 14:41:27 -05006256 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006257 * @phba: Pointer to hba context object.
6258 *
James Smartecfd03c2010-02-12 14:41:27 -05006259 * This function prepare the HBA for unregistering the currently registered
6260 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6261 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006262 */
James Smartecfd03c2010-02-12 14:41:27 -05006263int
6264lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006265{
James Smart6fb120a2009-05-22 14:52:59 -04006266 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006267 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006268 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006269 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006270
James Smartecfd03c2010-02-12 14:41:27 -05006271 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006272 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006273 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006274
James Smart4d9ab992009-10-02 15:16:39 -04006275 /* At this point, all discovery is aborted */
6276 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006277
6278 /* Unregister VPIs */
6279 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006280 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006281 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006282 /* Stop FLOGI/FDISC retries */
6283 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6284 if (ndlp)
6285 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006286 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006287 if (phba->sli_rev == LPFC_SLI_REV4)
6288 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006289 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006290 shost = lpfc_shost_from_vport(vports[i]);
6291 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006292 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006293 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006294 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006295 }
6296 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006297 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6298 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6299 if (ndlp)
6300 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6301 lpfc_cleanup_pending_mbox(phba->pport);
6302 if (phba->sli_rev == LPFC_SLI_REV4)
6303 lpfc_sli4_unreg_all_rpis(phba->pport);
6304 lpfc_mbx_unreg_vpi(phba->pport);
6305 shost = lpfc_shost_from_vport(phba->pport);
6306 spin_lock_irq(shost->host_lock);
6307 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6308 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6309 spin_unlock_irq(shost->host_lock);
6310 }
James Smart6fb120a2009-05-22 14:52:59 -04006311
James Smart695a8142010-01-26 23:08:03 -05006312 /* Cleanup any outstanding ELS commands */
6313 lpfc_els_flush_all_cmd(phba);
6314
James Smart1b511972011-12-13 13:23:09 -05006315 /* Unregister the physical port VFI */
6316 rc = lpfc_issue_unreg_vfi(phba->pport);
6317 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006318}
6319
6320/**
6321 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6322 * @phba: Pointer to hba context object.
6323 *
6324 * This function issues synchronous unregister FCF mailbox command to HBA to
6325 * unregister the currently registered FCF record. The driver does not reset
6326 * the driver FCF usage state flags.
6327 *
6328 * Return 0 if successfully issued, none-zero otherwise.
6329 */
6330int
6331lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6332{
6333 LPFC_MBOXQ_t *mbox;
6334 int rc;
6335
James Smart6fb120a2009-05-22 14:52:59 -04006336 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6337 if (!mbox) {
6338 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006339 "2551 UNREG_FCFI mbox allocation failed"
6340 "HBA state x%x\n", phba->pport->port_state);
6341 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006342 }
James Smart6fb120a2009-05-22 14:52:59 -04006343 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6344 mbox->vport = phba->pport;
6345 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6346 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6347
6348 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6350 "2552 Unregister FCFI command failed rc x%x "
6351 "HBA state x%x\n",
6352 rc, phba->pport->port_state);
6353 return -EINVAL;
6354 }
6355 return 0;
6356}
6357
6358/**
6359 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6360 * @phba: Pointer to hba context object.
6361 *
6362 * This function unregisters the currently reigstered FCF. This function
6363 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6364 */
6365void
6366lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6367{
6368 int rc;
6369
6370 /* Preparation for unregistering fcf */
6371 rc = lpfc_unregister_fcf_prep(phba);
6372 if (rc) {
6373 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6374 "2748 Failed to prepare for unregistering "
6375 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006376 return;
6377 }
6378
James Smartecfd03c2010-02-12 14:41:27 -05006379 /* Now, unregister FCF record and reset HBA FCF state */
6380 rc = lpfc_sli4_unregister_fcf(phba);
6381 if (rc)
6382 return;
6383 /* Reset HBA FCF states after successful unregister FCF */
6384 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006385 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006386
6387 /*
6388 * If driver is not unloading, check if there is any other
6389 * FCF record that can be used for discovery.
6390 */
6391 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006392 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006393 return;
6394
James Smart0c9ab6f2010-02-26 14:15:57 -05006395 /* This is considered as the initial FCF discovery scan */
6396 spin_lock_irq(&phba->hbalock);
6397 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6398 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006399
6400 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006401 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006402
James Smart0c9ab6f2010-02-26 14:15:57 -05006403 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006404
James Smart0c9ab6f2010-02-26 14:15:57 -05006405 if (rc) {
6406 spin_lock_irq(&phba->hbalock);
6407 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6408 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006409 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006410 "2553 lpfc_unregister_unused_fcf failed "
6411 "to read FCF record HBA state x%x\n",
6412 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006413 }
James Smartecfd03c2010-02-12 14:41:27 -05006414}
6415
6416/**
6417 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6418 * @phba: Pointer to hba context object.
6419 *
6420 * This function just unregisters the currently reigstered FCF. It does not
6421 * try to find another FCF for discovery.
6422 */
6423void
6424lpfc_unregister_fcf(struct lpfc_hba *phba)
6425{
6426 int rc;
6427
6428 /* Preparation for unregistering fcf */
6429 rc = lpfc_unregister_fcf_prep(phba);
6430 if (rc) {
6431 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6432 "2749 Failed to prepare for unregistering "
6433 "HBA's FCF record: rc=%d\n", rc);
6434 return;
6435 }
6436
6437 /* Now, unregister FCF record and reset HBA FCF state */
6438 rc = lpfc_sli4_unregister_fcf(phba);
6439 if (rc)
6440 return;
6441 /* Set proper HBA FCF states after successful unregister FCF */
6442 spin_lock_irq(&phba->hbalock);
6443 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6444 spin_unlock_irq(&phba->hbalock);
6445}
6446
6447/**
6448 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6449 * @phba: Pointer to hba context object.
6450 *
6451 * This function check if there are any connected remote port for the FCF and
6452 * if all the devices are disconnected, this function unregister FCFI.
6453 * This function also tries to use another FCF for discovery.
6454 */
6455void
6456lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6457{
6458 /*
James Smart3804dc82010-07-14 15:31:37 -04006459 * If HBA is not running in FIP mode, if HBA does not support
6460 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6461 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006462 */
6463 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006464 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006465 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006466 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006467 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006468 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006469 spin_unlock_irq(&phba->hbalock);
6470 return;
6471 }
6472 spin_unlock_irq(&phba->hbalock);
6473
6474 if (lpfc_fcf_inuse(phba))
6475 return;
6476
6477 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006478}
6479
6480/**
6481 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6482 * @phba: Pointer to hba context object.
6483 * @buff: Buffer containing the FCF connection table as in the config
6484 * region.
6485 * This function create driver data structure for the FCF connection
6486 * record table read from config region 23.
6487 */
6488static void
6489lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6490 uint8_t *buff)
6491{
6492 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6493 struct lpfc_fcf_conn_hdr *conn_hdr;
6494 struct lpfc_fcf_conn_rec *conn_rec;
6495 uint32_t record_count;
6496 int i;
6497
6498 /* Free the current connect table */
6499 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006500 &phba->fcf_conn_rec_list, list) {
6501 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006502 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006503 }
James Smart6fb120a2009-05-22 14:52:59 -04006504
6505 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6506 record_count = conn_hdr->length * sizeof(uint32_t)/
6507 sizeof(struct lpfc_fcf_conn_rec);
6508
6509 conn_rec = (struct lpfc_fcf_conn_rec *)
6510 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6511
6512 for (i = 0; i < record_count; i++) {
6513 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6514 continue;
6515 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6516 GFP_KERNEL);
6517 if (!conn_entry) {
6518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6519 "2566 Failed to allocate connection"
6520 " table entry\n");
6521 return;
6522 }
6523
6524 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6525 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006526 list_add_tail(&conn_entry->list,
6527 &phba->fcf_conn_rec_list);
6528 }
James Smartdf0d0852013-05-31 17:05:08 -04006529
6530 if (!list_empty(&phba->fcf_conn_rec_list)) {
6531 i = 0;
6532 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6533 list) {
6534 conn_rec = &conn_entry->conn_rec;
6535 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6536 "3345 FCF connection list rec[%02d]: "
6537 "flags:x%04x, vtag:x%04x, "
6538 "fabric_name:x%02x:%02x:%02x:%02x:"
6539 "%02x:%02x:%02x:%02x, "
6540 "switch_name:x%02x:%02x:%02x:%02x:"
6541 "%02x:%02x:%02x:%02x\n", i++,
6542 conn_rec->flags, conn_rec->vlan_tag,
6543 conn_rec->fabric_name[0],
6544 conn_rec->fabric_name[1],
6545 conn_rec->fabric_name[2],
6546 conn_rec->fabric_name[3],
6547 conn_rec->fabric_name[4],
6548 conn_rec->fabric_name[5],
6549 conn_rec->fabric_name[6],
6550 conn_rec->fabric_name[7],
6551 conn_rec->switch_name[0],
6552 conn_rec->switch_name[1],
6553 conn_rec->switch_name[2],
6554 conn_rec->switch_name[3],
6555 conn_rec->switch_name[4],
6556 conn_rec->switch_name[5],
6557 conn_rec->switch_name[6],
6558 conn_rec->switch_name[7]);
6559 }
6560 }
James Smart6fb120a2009-05-22 14:52:59 -04006561}
6562
6563/**
6564 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6565 * @phba: Pointer to hba context object.
6566 * @buff: Buffer containing the FCoE parameter data structure.
6567 *
6568 * This function update driver data structure with config
6569 * parameters read from config region 23.
6570 */
6571static void
6572lpfc_read_fcoe_param(struct lpfc_hba *phba,
6573 uint8_t *buff)
6574{
6575 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6576 struct lpfc_fcoe_params *fcoe_param;
6577
6578 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6579 buff;
6580 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006581 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006582
6583 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6584 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6585 return;
6586
James Smart6fb120a2009-05-22 14:52:59 -04006587 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6588 phba->valid_vlan = 1;
6589 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6590 0xFFF;
6591 }
6592
6593 phba->fc_map[0] = fcoe_param->fc_map[0];
6594 phba->fc_map[1] = fcoe_param->fc_map[1];
6595 phba->fc_map[2] = fcoe_param->fc_map[2];
6596 return;
6597}
6598
6599/**
6600 * lpfc_get_rec_conf23 - Get a record type in config region data.
6601 * @buff: Buffer containing config region 23 data.
6602 * @size: Size of the data buffer.
6603 * @rec_type: Record type to be searched.
6604 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006605 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006606 * of the record specified by record_type. If record found, this
6607 * function return pointer to the record else return NULL.
6608 */
6609static uint8_t *
6610lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6611{
6612 uint32_t offset = 0, rec_length;
6613
6614 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6615 (size < sizeof(uint32_t)))
6616 return NULL;
6617
6618 rec_length = buff[offset + 1];
6619
6620 /*
6621 * One TLV record has one word header and number of data words
6622 * specified in the rec_length field of the record header.
6623 */
6624 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6625 <= size) {
6626 if (buff[offset] == rec_type)
6627 return &buff[offset];
6628
6629 if (buff[offset] == LPFC_REGION23_LAST_REC)
6630 return NULL;
6631
6632 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6633 rec_length = buff[offset + 1];
6634 }
6635 return NULL;
6636}
6637
6638/**
6639 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6640 * @phba: Pointer to lpfc_hba data structure.
6641 * @buff: Buffer containing config region 23 data.
6642 * @size: Size of the data buffer.
6643 *
Stefan Weileef35c22010-08-06 21:11:15 +02006644 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006645 * populate driver data structure with the parameters.
6646 */
6647void
6648lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6649 uint8_t *buff,
6650 uint32_t size)
6651{
James Smarteb016562014-09-03 12:58:06 -04006652 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006653 uint8_t *rec_ptr;
6654
6655 /*
6656 * If data size is less than 2 words signature and version cannot be
6657 * verified.
6658 */
6659 if (size < 2*sizeof(uint32_t))
6660 return;
6661
6662 /* Check the region signature first */
6663 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6665 "2567 Config region 23 has bad signature\n");
6666 return;
6667 }
6668
6669 offset += 4;
6670
6671 /* Check the data structure version */
6672 if (buff[offset] != LPFC_REGION23_VERSION) {
6673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6674 "2568 Config region 23 has bad version\n");
6675 return;
6676 }
6677 offset += 4;
6678
James Smart6fb120a2009-05-22 14:52:59 -04006679 /* Read FCoE param record */
6680 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6681 size - offset, FCOE_PARAM_TYPE);
6682 if (rec_ptr)
6683 lpfc_read_fcoe_param(phba, rec_ptr);
6684
6685 /* Read FCF connection table */
6686 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6687 size - offset, FCOE_CONN_TBL_TYPE);
6688 if (rec_ptr)
6689 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6690
6691}