blob: 20808349a80e9908d6d535b5107aa50882c0f553 [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;
James Smart44fd7fe2017-08-23 16:55:47 -07001111 uint8_t bbscn = 0;
dea31012005-04-17 16:05:31 -05001112
James Smart04c68492009-05-22 14:52:52 -04001113 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001114 goto out;
dea31012005-04-17 16:05:31 -05001115
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001116 mempool_free(pmb, phba->mbox_mem_pool);
1117
James Smart1b511972011-12-13 13:23:09 -05001118 /* don't perform discovery for SLI4 loopback diagnostic test */
1119 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1120 !(phba->hba_flag & HBA_FCOE_MODE) &&
1121 (phba->link_flag & LS_LOOPBACK_MODE))
1122 return;
1123
James Smart76a95d72010-11-20 23:11:48 -05001124 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001125 vport->fc_flag & FC_PUBLIC_LOOP &&
1126 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001127 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001128 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001129 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1130 */
James Smart2e0fef82007-06-17 19:56:36 -05001131 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001132 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001133 }
dea31012005-04-17 16:05:31 -05001134
James Smart2e0fef82007-06-17 19:56:36 -05001135 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001136 * LPFC_FLOGI while waiting for FLOGI cmpl
1137 */
James Smart44fd7fe2017-08-23 16:55:47 -07001138 if (vport->port_state != LPFC_FLOGI) {
1139 if (phba->bbcredit_support && phba->cfg_enable_bbcr) {
1140 bbscn = bf_get(lpfc_bbscn_def,
1141 &phba->sli4_hba.bbscn_params);
1142 vport->fc_sparam.cmn.bbRcvSizeMsb &= 0xf;
1143 vport->fc_sparam.cmn.bbRcvSizeMsb |= (bbscn << 4);
1144 }
James Smart92d7f7b2007-06-17 19:56:38 -05001145 lpfc_initial_flogi(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001146 } else if (vport->fc_flag & FC_PT2PT) {
James Smartd6de08c2015-12-16 18:11:53 -05001147 lpfc_disc_start(vport);
James Smart44fd7fe2017-08-23 16:55:47 -07001148 }
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001149 return;
dea31012005-04-17 16:05:31 -05001150
1151out:
James Smarte8b62012007-08-02 11:10:09 -04001152 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1153 "0306 CONFIG_LINK mbxStatus error x%x "
1154 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001155 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001156 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001157
1158 lpfc_linkdown(phba);
1159
James Smarte8b62012007-08-02 11:10:09 -04001160 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1161 "0200 CONFIG_LINK bad hba state x%x\n",
1162 vport->port_state);
dea31012005-04-17 16:05:31 -05001163
James Smart92d7f7b2007-06-17 19:56:38 -05001164 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001165 return;
1166}
1167
James Smart7d791df2011-07-22 18:37:52 -04001168/**
1169 * lpfc_sli4_clear_fcf_rr_bmask
1170 * @phba pointer to the struct lpfc_hba for this port.
1171 * This fucnction resets the round robin bit mask and clears the
1172 * fcf priority list. The list deletions are done while holding the
1173 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1174 * from the lpfc_fcf_pri record.
1175 **/
1176void
1177lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1178{
1179 struct lpfc_fcf_pri *fcf_pri;
1180 struct lpfc_fcf_pri *next_fcf_pri;
1181 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1182 spin_lock_irq(&phba->hbalock);
1183 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1184 &phba->fcf.fcf_pri_list, list) {
1185 list_del_init(&fcf_pri->list);
1186 fcf_pri->fcf_rec.flag = 0;
1187 }
1188 spin_unlock_irq(&phba->hbalock);
1189}
dea31012005-04-17 16:05:31 -05001190static void
James Smart6fb120a2009-05-22 14:52:59 -04001191lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1192{
1193 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001194
1195 if (mboxq->u.mb.mbxStatus) {
1196 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1197 "2017 REG_FCFI mbxStatus error x%x "
1198 "HBA state x%x\n",
1199 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001200 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001201 }
1202
1203 /* Start FCoE discovery by sending a FLOGI. */
1204 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1205 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001206 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001207 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001208 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001209
James Smart32b97932009-07-19 10:01:21 -04001210 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001211 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1212 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001213 goto fail_out;
1214
1215 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001216 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001217 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001218 phba->hba_flag &= ~FCF_TS_INPROG;
1219 if (vport->port_state != LPFC_FLOGI) {
1220 phba->hba_flag |= FCF_RR_INPROG;
1221 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001222 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001223 goto out;
1224 }
1225 spin_unlock_irq(&phba->hbalock);
1226 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001227
James Smarta93ff372010-10-22 11:06:08 -04001228fail_out:
1229 spin_lock_irq(&phba->hbalock);
1230 phba->hba_flag &= ~FCF_RR_INPROG;
1231 spin_unlock_irq(&phba->hbalock);
1232out:
James Smart6fb120a2009-05-22 14:52:59 -04001233 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001234}
1235
1236/**
1237 * lpfc_fab_name_match - Check if the fcf fabric name match.
1238 * @fab_name: pointer to fabric name.
1239 * @new_fcf_record: pointer to fcf record.
1240 *
1241 * This routine compare the fcf record's fabric name with provided
1242 * fabric name. If the fabric name are identical this function
1243 * returns 1 else return 0.
1244 **/
1245static uint32_t
1246lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1247{
James Smartecfd03c2010-02-12 14:41:27 -05001248 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001249 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001250 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1251 return 0;
1252 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1253 return 0;
1254 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1255 return 0;
1256 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1257 return 0;
1258 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1259 return 0;
1260 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1261 return 0;
1262 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1263 return 0;
1264 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001265}
1266
1267/**
James Smart8fa38512009-07-19 10:01:03 -04001268 * lpfc_sw_name_match - Check if the fcf switch name match.
1269 * @fab_name: pointer to fabric name.
1270 * @new_fcf_record: pointer to fcf record.
1271 *
1272 * This routine compare the fcf record's switch name with provided
1273 * switch name. If the switch name are identical this function
1274 * returns 1 else return 0.
1275 **/
1276static uint32_t
1277lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1278{
James Smartecfd03c2010-02-12 14:41:27 -05001279 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001280 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001281 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1282 return 0;
1283 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1284 return 0;
1285 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1286 return 0;
1287 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1288 return 0;
1289 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1290 return 0;
1291 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1292 return 0;
1293 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1294 return 0;
1295 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001296}
1297
1298/**
James Smart6fb120a2009-05-22 14:52:59 -04001299 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001300 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001301 * @new_fcf_record: pointer to fcf record.
1302 *
1303 * This routine compare the fcf record's mac address with HBA's
1304 * FCF mac address. If the mac addresses are identical this function
1305 * returns 1 else return 0.
1306 **/
1307static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001308lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001309{
James Smartecfd03c2010-02-12 14:41:27 -05001310 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001311 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001312 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1313 return 0;
1314 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1315 return 0;
1316 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1317 return 0;
1318 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1319 return 0;
1320 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1321 return 0;
1322 return 1;
1323}
1324
1325static bool
1326lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1327{
1328 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001329}
1330
1331/**
James Smart7d791df2011-07-22 18:37:52 -04001332 * lpfc_update_fcf_record - Update driver fcf record
1333 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1334 * @phba: pointer to lpfc hba data structure.
1335 * @fcf_index: Index for the lpfc_fcf_record.
1336 * @new_fcf_record: pointer to hba fcf record.
1337 *
1338 * This routine updates the driver FCF priority record from the new HBA FCF
1339 * record. This routine is called with the host lock held.
1340 **/
1341static void
1342__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1343 struct fcf_record *new_fcf_record
1344 )
1345{
1346 struct lpfc_fcf_pri *fcf_pri;
1347
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001348 lockdep_assert_held(&phba->hbalock);
1349
James Smart7d791df2011-07-22 18:37:52 -04001350 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1351 fcf_pri->fcf_rec.fcf_index = fcf_index;
1352 /* FCF record priority */
1353 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1354
1355}
1356
1357/**
James Smart6fb120a2009-05-22 14:52:59 -04001358 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001359 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001360 * @new_fcf_record: pointer to fcf record.
1361 *
1362 * This routine copies the FCF information from the FCF
1363 * record to lpfc_hba data structure.
1364 **/
1365static void
James Smartecfd03c2010-02-12 14:41:27 -05001366lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1367 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001368{
James Smartecfd03c2010-02-12 14:41:27 -05001369 /* Fabric name */
1370 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001371 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001372 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001373 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001374 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001375 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001376 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001377 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001378 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001379 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001380 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001381 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001382 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001383 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001384 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001385 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001386 /* Mac address */
1387 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1388 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1389 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1390 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1391 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1392 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1393 /* FCF record index */
1394 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1395 /* FCF record priority */
1396 fcf_rec->priority = new_fcf_record->fip_priority;
1397 /* Switch name */
1398 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001399 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001400 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001401 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001402 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001403 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001404 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001405 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001406 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001407 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001408 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001409 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001410 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001411 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001412 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001413 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001414}
1415
1416/**
James Smartecfd03c2010-02-12 14:41:27 -05001417 * lpfc_update_fcf_record - Update driver fcf record
1418 * @phba: pointer to lpfc hba data structure.
1419 * @fcf_rec: pointer to driver fcf record.
1420 * @new_fcf_record: pointer to hba fcf record.
1421 * @addr_mode: address mode to be set to the driver fcf record.
1422 * @vlan_id: vlan tag to be set to the driver fcf record.
1423 * @flag: flag bits to be set to the driver fcf record.
1424 *
1425 * This routine updates the driver FCF record from the new HBA FCF record
1426 * together with the address mode, vlan_id, and other informations. This
1427 * routine is called with the host lock held.
1428 **/
1429static void
1430__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1431 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1432 uint16_t vlan_id, uint32_t flag)
1433{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001434 lockdep_assert_held(&phba->hbalock);
1435
James Smartecfd03c2010-02-12 14:41:27 -05001436 /* Copy the fields from the HBA's FCF record */
1437 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1438 /* Update other fields of driver FCF record */
1439 fcf_rec->addr_mode = addr_mode;
1440 fcf_rec->vlan_id = vlan_id;
1441 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001442 __lpfc_update_fcf_record_pri(phba,
1443 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1444 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001445}
1446
1447/**
James Smart6fb120a2009-05-22 14:52:59 -04001448 * lpfc_register_fcf - Register the FCF with hba.
1449 * @phba: pointer to lpfc hba data structure.
1450 *
1451 * This routine issues a register fcfi mailbox command to register
1452 * the fcf with HBA.
1453 **/
1454static void
1455lpfc_register_fcf(struct lpfc_hba *phba)
1456{
1457 LPFC_MBOXQ_t *fcf_mbxq;
1458 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001459
James Smartd439d282010-09-29 11:18:45 -04001460 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001461 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001462 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001463 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001464 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001465 return;
1466 }
1467
1468 /* The FCF is already registered, start discovery */
1469 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001470 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001471 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001472 if (phba->pport->port_state != LPFC_FLOGI &&
1473 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001474 phba->hba_flag |= FCF_RR_INPROG;
1475 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001476 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001477 return;
1478 }
1479 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001480 return;
1481 }
James Smartd439d282010-09-29 11:18:45 -04001482 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001483
James Smarta93ff372010-10-22 11:06:08 -04001484 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001485 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001486 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001487 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001488 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001489 return;
James Smart4d9ab992009-10-02 15:16:39 -04001490 }
James Smart6fb120a2009-05-22 14:52:59 -04001491
1492 lpfc_reg_fcfi(phba, fcf_mbxq);
1493 fcf_mbxq->vport = phba->pport;
1494 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1495 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001496 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001497 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001498 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001499 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001500 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001501 }
James Smart6fb120a2009-05-22 14:52:59 -04001502
1503 return;
1504}
1505
1506/**
1507 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1508 * @phba: pointer to lpfc hba data structure.
1509 * @new_fcf_record: pointer to fcf record.
1510 * @boot_flag: Indicates if this record used by boot bios.
1511 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001512 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001513 *
1514 * This routine compare the fcf record with connect list obtained from the
1515 * config region to decide if this FCF can be used for SAN discovery. It returns
1516 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1517 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1518 * is used by boot bios and addr_mode will indicate the addressing mode to be
1519 * used for this FCF when the function returns.
1520 * If the FCF record need to be used with a particular vlan id, the vlan is
1521 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001522 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001523 **/
1524static int
1525lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1526 struct fcf_record *new_fcf_record,
1527 uint32_t *boot_flag, uint32_t *addr_mode,
1528 uint16_t *vlan_id)
1529{
1530 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001531 int i, j, fcf_vlan_id = 0;
1532
1533 /* Find the lowest VLAN id in the FCF record */
1534 for (i = 0; i < 512; i++) {
1535 if (new_fcf_record->vlan_bitmap[i]) {
1536 fcf_vlan_id = i * 8;
1537 j = 0;
1538 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1539 j++;
1540 fcf_vlan_id++;
1541 }
1542 break;
1543 }
1544 }
James Smart6fb120a2009-05-22 14:52:59 -04001545
James Smart26979ce2012-09-29 11:31:55 -04001546 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001547 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001548 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1549 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001550 return 0;
1551
James Smart45ed1192009-10-02 15:17:02 -04001552 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001553 *boot_flag = 0;
1554 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1555 new_fcf_record);
1556 if (phba->valid_vlan)
1557 *vlan_id = phba->vlan_id;
1558 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001559 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001560 return 1;
1561 }
1562
1563 /*
1564 * If there are no FCF connection table entry, driver connect to all
1565 * FCFs.
1566 */
1567 if (list_empty(&phba->fcf_conn_rec_list)) {
1568 *boot_flag = 0;
1569 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1570 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001571
1572 /*
1573 * When there are no FCF connect entries, use driver's default
1574 * addressing mode - FPMA.
1575 */
1576 if (*addr_mode & LPFC_FCF_FPMA)
1577 *addr_mode = LPFC_FCF_FPMA;
1578
James Smart4d9ab992009-10-02 15:16:39 -04001579 /* If FCF record report a vlan id use that vlan id */
1580 if (fcf_vlan_id)
1581 *vlan_id = fcf_vlan_id;
1582 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001583 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001584 return 1;
1585 }
1586
James Smartecfd03c2010-02-12 14:41:27 -05001587 list_for_each_entry(conn_entry,
1588 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001589 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1590 continue;
1591
1592 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1593 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001594 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001595 continue;
James Smart8fa38512009-07-19 10:01:03 -04001596 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1597 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1598 new_fcf_record))
1599 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001600 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1601 /*
1602 * If the vlan bit map does not have the bit set for the
1603 * vlan id to be used, then it is not a match.
1604 */
1605 if (!(new_fcf_record->vlan_bitmap
1606 [conn_entry->conn_rec.vlan_tag / 8] &
1607 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1608 continue;
1609 }
1610
1611 /*
James Smart0c287582009-06-10 17:22:56 -04001612 * If connection record does not support any addressing mode,
1613 * skip the FCF record.
1614 */
1615 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1616 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1617 continue;
1618
1619 /*
James Smart6fb120a2009-05-22 14:52:59 -04001620 * Check if the connection record specifies a required
1621 * addressing mode.
1622 */
1623 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1624 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1625
1626 /*
1627 * If SPMA 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_SPMA))
1632 continue;
1633
1634 /*
1635 * If FPMA required but FCF not support this continue.
1636 */
1637 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1638 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1639 new_fcf_record) & LPFC_FCF_FPMA))
1640 continue;
1641 }
1642
1643 /*
1644 * This fcf record matches filtering criteria.
1645 */
1646 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1647 *boot_flag = 1;
1648 else
1649 *boot_flag = 0;
1650
James Smart0c287582009-06-10 17:22:56 -04001651 /*
1652 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001653 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001654 * by FCF, allow fabric to pick the addressing mode.
1655 */
James Smart6fb120a2009-05-22 14:52:59 -04001656 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1657 new_fcf_record);
1658 /*
1659 * If the user specified a required address mode, assign that
1660 * address mode
1661 */
1662 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1663 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1664 *addr_mode = (conn_entry->conn_rec.flags &
1665 FCFCNCT_AM_SPMA) ?
1666 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1667 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001668 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001669 * addr mode only if FCF support the addr_mode.
1670 */
1671 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1672 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1673 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1674 (*addr_mode & LPFC_FCF_SPMA))
1675 *addr_mode = LPFC_FCF_SPMA;
1676 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1677 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1678 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1679 (*addr_mode & LPFC_FCF_FPMA))
1680 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001681
James Smart4d9ab992009-10-02 15:16:39 -04001682 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001683 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1684 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001685 /*
1686 * If no vlan id is specified in connect list, use the vlan id
1687 * in the FCF record
1688 */
1689 else if (fcf_vlan_id)
1690 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001691 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001692 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001693
1694 return 1;
1695 }
1696
1697 return 0;
1698}
1699
1700/**
James Smart32b97932009-07-19 10:01:21 -04001701 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1702 * @phba: pointer to lpfc hba data structure.
1703 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1704 *
1705 * This function check if there is any fcoe event pending while driver
1706 * scan FCF entries. If there is any pending event, it will restart the
1707 * FCF saning and return 1 else return 0.
1708 */
1709int
1710lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1711{
James Smart32b97932009-07-19 10:01:21 -04001712 /*
1713 * If the Link is up and no FCoE events while in the
1714 * FCF discovery, no need to restart FCF discovery.
1715 */
1716 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001717 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001718 return 0;
1719
James Smart0c9ab6f2010-02-26 14:15:57 -05001720 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1721 "2768 Pending link or FCF event during current "
1722 "handling of the previous event: link_state:x%x, "
1723 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1724 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1725 phba->fcoe_eventtag);
1726
James Smart32b97932009-07-19 10:01:21 -04001727 spin_lock_irq(&phba->hbalock);
1728 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1729 spin_unlock_irq(&phba->hbalock);
1730
James Smart0c9ab6f2010-02-26 14:15:57 -05001731 if (phba->link_state >= LPFC_LINK_UP) {
1732 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1733 "2780 Restart FCF table scan due to "
1734 "pending FCF event:evt_tag_at_scan:x%x, "
1735 "evt_tag_current:x%x\n",
1736 phba->fcoe_eventtag_at_fcf_scan,
1737 phba->fcoe_eventtag);
1738 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1739 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001740 /*
James Smarta93ff372010-10-22 11:06:08 -04001741 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001742 * flag
1743 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001744 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1745 "2833 Stop FCF discovery process due to link "
1746 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001747 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001748 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001749 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001750 spin_unlock_irq(&phba->hbalock);
1751 }
James Smart32b97932009-07-19 10:01:21 -04001752
James Smart0c9ab6f2010-02-26 14:15:57 -05001753 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001754 if (unreg_fcf) {
1755 spin_lock_irq(&phba->hbalock);
1756 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1757 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001758 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001759 }
James Smart32b97932009-07-19 10:01:21 -04001760 return 1;
1761}
1762
1763/**
James Smart999d8132010-03-15 11:24:56 -04001764 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1765 * @phba: pointer to lpfc hba data structure.
1766 * @fcf_cnt: number of eligible fcf record seen so far.
1767 *
1768 * This function makes an running random selection decision on FCF record to
1769 * use through a sequence of @fcf_cnt eligible FCF records with equal
1770 * probability. To perform integer manunipulation of random numbers with
1771 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001772 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001773 *
1774 * Returns true when outcome is for the newly read FCF record should be
1775 * chosen; otherwise, return false when outcome is for keeping the previously
1776 * chosen FCF record.
1777 **/
1778static bool
1779lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1780{
1781 uint32_t rand_num;
1782
1783 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001784 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001785
1786 /* Decision with probability 1/fcf_cnt */
1787 if ((fcf_cnt * rand_num) < 0xFFFF)
1788 return true;
1789 else
1790 return false;
1791}
1792
1793/**
James Smart3804dc82010-07-14 15:31:37 -04001794 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001795 * @phba: pointer to lpfc hba data structure.
1796 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001797 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001798 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001799 * This routine parses the non-embedded fcf mailbox command by performing the
1800 * necessarily error checking, non-embedded read FCF record mailbox command
1801 * SGE parsing, and endianness swapping.
1802 *
1803 * Returns the pointer to the new FCF record in the non-embedded mailbox
1804 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001805 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001806static struct fcf_record *
1807lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1808 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001809{
1810 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001811 struct lpfc_mbx_sge sge;
1812 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001813 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001814 union lpfc_sli4_cfg_shdr *shdr;
1815 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001816
James Smart6fb120a2009-05-22 14:52:59 -04001817 /* Get the first SGE entry from the non-embedded DMA memory. This
1818 * routine only uses a single SGE.
1819 */
1820 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001821 if (unlikely(!mboxq->sge_array)) {
1822 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1823 "2524 Failed to get the non-embedded SGE "
1824 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001825 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001826 }
1827 virt_addr = mboxq->sge_array->addr[0];
1828
1829 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001830 lpfc_sli_pcimem_bcopy(shdr, shdr,
1831 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001832 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001833 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001834 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001835 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001836 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1837 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001838 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001839 "2726 READ_FCF_RECORD Indicates empty "
1840 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001841 else
1842 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001843 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001844 "with status x%x add_status x%x, "
1845 "mbx\n", shdr_status, shdr_add_status);
1846 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001847 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001848
1849 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001850 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1851 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1852 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001853 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001854 new_fcf_record = (struct fcf_record *)(virt_addr +
1855 sizeof(struct lpfc_mbx_read_fcf_tbl));
1856 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001857 offsetof(struct fcf_record, vlan_bitmap));
1858 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1859 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001860
James Smart0c9ab6f2010-02-26 14:15:57 -05001861 return new_fcf_record;
1862}
1863
1864/**
1865 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1866 * @phba: pointer to lpfc hba data structure.
1867 * @fcf_record: pointer to the fcf record.
1868 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1869 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1870 *
1871 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1872 * enabled.
1873 **/
1874static void
1875lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1876 struct fcf_record *fcf_record,
1877 uint16_t vlan_id,
1878 uint16_t next_fcf_index)
1879{
1880 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1881 "2764 READ_FCF_RECORD:\n"
1882 "\tFCF_Index : x%x\n"
1883 "\tFCF_Avail : x%x\n"
1884 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001885 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001886 "\tFIP_Priority : x%x\n"
1887 "\tMAC_Provider : x%x\n"
1888 "\tLowest VLANID : x%x\n"
1889 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1890 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1891 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1892 "\tNext_FCF_Index: x%x\n",
1893 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1894 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1895 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001896 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001897 fcf_record->fip_priority,
1898 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1899 vlan_id,
1900 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1901 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1902 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1903 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1904 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1905 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1906 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1907 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1908 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1909 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1910 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1911 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1912 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1913 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1914 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1915 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1916 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1917 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1918 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1919 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1920 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1921 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1922 next_fcf_index);
1923}
1924
1925/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001926 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1927 * @phba: pointer to lpfc hba data structure.
1928 * @fcf_rec: pointer to an existing FCF record.
1929 * @new_fcf_record: pointer to a new FCF record.
1930 * @new_vlan_id: vlan id from the new FCF record.
1931 *
1932 * This function performs matching test of a new FCF record against an existing
1933 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1934 * will not be used as part of the FCF record matching criteria.
1935 *
1936 * Returns true if all the fields matching, otherwise returns false.
1937 */
1938static bool
1939lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1940 struct lpfc_fcf_rec *fcf_rec,
1941 struct fcf_record *new_fcf_record,
1942 uint16_t new_vlan_id)
1943{
1944 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1945 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1946 return false;
1947 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1948 return false;
1949 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1950 return false;
1951 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1952 return false;
James Smart7d791df2011-07-22 18:37:52 -04001953 if (fcf_rec->priority != new_fcf_record->fip_priority)
1954 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001955 return true;
1956}
1957
1958/**
James Smarta93ff372010-10-22 11:06:08 -04001959 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1960 * @vport: Pointer to vport object.
1961 * @fcf_index: index to next fcf.
1962 *
1963 * This function processing the roundrobin fcf failover to next fcf index.
1964 * When this function is invoked, there will be a current fcf registered
1965 * for flogi.
1966 * Return: 0 for continue retrying flogi on currently registered fcf;
1967 * 1 for stop flogi on currently registered fcf;
1968 */
1969int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1970{
1971 struct lpfc_hba *phba = vport->phba;
1972 int rc;
1973
1974 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1975 spin_lock_irq(&phba->hbalock);
1976 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1977 spin_unlock_irq(&phba->hbalock);
1978 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1979 "2872 Devloss tmo with no eligible "
1980 "FCF, unregister in-use FCF (x%x) "
1981 "and rescan FCF table\n",
1982 phba->fcf.current_rec.fcf_indx);
1983 lpfc_unregister_fcf_rescan(phba);
1984 goto stop_flogi_current_fcf;
1985 }
1986 /* Mark the end to FLOGI roundrobin failover */
1987 phba->hba_flag &= ~FCF_RR_INPROG;
1988 /* Allow action to new fcf asynchronous event */
1989 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1990 spin_unlock_irq(&phba->hbalock);
1991 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1992 "2865 No FCF available, stop roundrobin FCF "
1993 "failover and change port state:x%x/x%x\n",
1994 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1995 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1996 goto stop_flogi_current_fcf;
1997 } else {
1998 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1999 "2794 Try FLOGI roundrobin FCF failover to "
2000 "(x%x)\n", fcf_index);
2001 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
2002 if (rc)
2003 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
2004 "2761 FLOGI roundrobin FCF failover "
2005 "failed (rc:x%x) to read FCF (x%x)\n",
2006 rc, phba->fcf.current_rec.fcf_indx);
2007 else
2008 goto stop_flogi_current_fcf;
2009 }
2010 return 0;
2011
2012stop_flogi_current_fcf:
2013 lpfc_can_disctmo(vport);
2014 return 1;
2015}
2016
2017/**
James Smart7d791df2011-07-22 18:37:52 -04002018 * lpfc_sli4_fcf_pri_list_del
2019 * @phba: pointer to lpfc hba data structure.
2020 * @fcf_index the index of the fcf record to delete
2021 * This routine checks the on list flag of the fcf_index to be deleted.
2022 * If it is one the list then it is removed from the list, and the flag
2023 * is cleared. This routine grab the hbalock before removing the fcf
2024 * record from the list.
2025 **/
2026static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2027 uint16_t fcf_index)
2028{
2029 struct lpfc_fcf_pri *new_fcf_pri;
2030
2031 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2032 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2033 "3058 deleting idx x%x pri x%x flg x%x\n",
2034 fcf_index, new_fcf_pri->fcf_rec.priority,
2035 new_fcf_pri->fcf_rec.flag);
2036 spin_lock_irq(&phba->hbalock);
2037 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2038 if (phba->fcf.current_rec.priority ==
2039 new_fcf_pri->fcf_rec.priority)
2040 phba->fcf.eligible_fcf_cnt--;
2041 list_del_init(&new_fcf_pri->list);
2042 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2043 }
2044 spin_unlock_irq(&phba->hbalock);
2045}
2046
2047/**
2048 * lpfc_sli4_set_fcf_flogi_fail
2049 * @phba: pointer to lpfc hba data structure.
2050 * @fcf_index the index of the fcf record to update
2051 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2052 * flag so the the round robin slection for the particular priority level
2053 * will try a different fcf record that does not have this bit set.
2054 * If the fcf record is re-read for any reason this flag is cleared brfore
2055 * adding it to the priority list.
2056 **/
2057void
2058lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2059{
2060 struct lpfc_fcf_pri *new_fcf_pri;
2061 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2062 spin_lock_irq(&phba->hbalock);
2063 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2064 spin_unlock_irq(&phba->hbalock);
2065}
2066
2067/**
2068 * lpfc_sli4_fcf_pri_list_add
2069 * @phba: pointer to lpfc hba data structure.
2070 * @fcf_index the index of the fcf record to add
2071 * This routine checks the priority of the fcf_index to be added.
2072 * If it is a lower priority than the current head of the fcf_pri list
2073 * then it is added to the list in the right order.
2074 * If it is the same priority as the current head of the list then it
2075 * is added to the head of the list and its bit in the rr_bmask is set.
2076 * If the fcf_index to be added is of a higher priority than the current
2077 * head of the list then the rr_bmask is cleared, its bit is set in the
2078 * rr_bmask and it is added to the head of the list.
2079 * returns:
2080 * 0=success 1=failure
2081 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002082static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2083 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002084 struct fcf_record *new_fcf_record)
2085{
2086 uint16_t current_fcf_pri;
2087 uint16_t last_index;
2088 struct lpfc_fcf_pri *fcf_pri;
2089 struct lpfc_fcf_pri *next_fcf_pri;
2090 struct lpfc_fcf_pri *new_fcf_pri;
2091 int ret;
2092
2093 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2094 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2095 "3059 adding idx x%x pri x%x flg x%x\n",
2096 fcf_index, new_fcf_record->fip_priority,
2097 new_fcf_pri->fcf_rec.flag);
2098 spin_lock_irq(&phba->hbalock);
2099 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2100 list_del_init(&new_fcf_pri->list);
2101 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2102 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2103 if (list_empty(&phba->fcf.fcf_pri_list)) {
2104 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2105 ret = lpfc_sli4_fcf_rr_index_set(phba,
2106 new_fcf_pri->fcf_rec.fcf_index);
2107 goto out;
2108 }
2109
2110 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2111 LPFC_SLI4_FCF_TBL_INDX_MAX);
2112 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2113 ret = 0; /* Empty rr list */
2114 goto out;
2115 }
2116 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2117 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2118 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2119 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2120 memset(phba->fcf.fcf_rr_bmask, 0,
2121 sizeof(*phba->fcf.fcf_rr_bmask));
2122 /* fcfs_at_this_priority_level = 1; */
2123 phba->fcf.eligible_fcf_cnt = 1;
2124 } else
2125 /* fcfs_at_this_priority_level++; */
2126 phba->fcf.eligible_fcf_cnt++;
2127 ret = lpfc_sli4_fcf_rr_index_set(phba,
2128 new_fcf_pri->fcf_rec.fcf_index);
2129 goto out;
2130 }
2131
2132 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2133 &phba->fcf.fcf_pri_list, list) {
2134 if (new_fcf_pri->fcf_rec.priority <=
2135 fcf_pri->fcf_rec.priority) {
2136 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2137 list_add(&new_fcf_pri->list,
2138 &phba->fcf.fcf_pri_list);
2139 else
2140 list_add(&new_fcf_pri->list,
2141 &((struct lpfc_fcf_pri *)
2142 fcf_pri->list.prev)->list);
2143 ret = 0;
2144 goto out;
2145 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2146 || new_fcf_pri->fcf_rec.priority <
2147 next_fcf_pri->fcf_rec.priority) {
2148 list_add(&new_fcf_pri->list, &fcf_pri->list);
2149 ret = 0;
2150 goto out;
2151 }
2152 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2153 continue;
2154
2155 }
2156 ret = 1;
2157out:
2158 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2159 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2160 spin_unlock_irq(&phba->hbalock);
2161 return ret;
2162}
2163
2164/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002165 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2166 * @phba: pointer to lpfc hba data structure.
2167 * @mboxq: pointer to mailbox object.
2168 *
2169 * This function iterates through all the fcf records available in
2170 * HBA and chooses the optimal FCF record for discovery. After finding
2171 * the FCF for discovery it registers the FCF record and kicks start
2172 * discovery.
2173 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2174 * use an FCF record which matches fabric name and mac address of the
2175 * currently used FCF record.
2176 * If the driver supports only one FCF, it will try to use the FCF record
2177 * used by BOOT_BIOS.
2178 */
2179void
2180lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2181{
2182 struct fcf_record *new_fcf_record;
2183 uint32_t boot_flag, addr_mode;
2184 uint16_t fcf_index, next_fcf_index;
2185 struct lpfc_fcf_rec *fcf_rec = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08002186 uint16_t vlan_id = LPFC_FCOE_NULL_VID;
James Smart999d8132010-03-15 11:24:56 -04002187 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 int rc;
2189
2190 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002191 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002192 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2193 return;
2194 }
2195
2196 /* Parse the FCF record from the non-embedded mailbox command */
2197 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2198 &next_fcf_index);
2199 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002200 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002201 "2765 Mailbox command READ_FCF_RECORD "
2202 "failed to retrieve a FCF record.\n");
2203 /* Let next new FCF event trigger fast failover */
2204 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002205 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002206 spin_unlock_irq(&phba->hbalock);
2207 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2208 return;
2209 }
2210
2211 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002212 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2213 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002214
2215 /* Log the FCF record information if turned on */
2216 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2217 next_fcf_index);
2218
James Smart6fb120a2009-05-22 14:52:59 -04002219 /*
2220 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002221 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002222 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002223 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002224 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002225 lpfc_sli4_fcf_pri_list_del(phba,
2226 bf_get(lpfc_fcf_record_fcf_index,
2227 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002228 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002229 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002230 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002231 bf_get(lpfc_fcf_record_fcf_index,
2232 new_fcf_record),
2233 bf_get(lpfc_fcf_record_fcf_avail,
2234 new_fcf_record),
2235 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002236 new_fcf_record),
2237 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002238 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002239 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2240 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2241 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002242 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2243 phba->fcf.current_rec.fcf_indx) {
2244 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2245 "2862 FCF (x%x) matches property "
2246 "of in-use FCF (x%x)\n",
2247 bf_get(lpfc_fcf_record_fcf_index,
2248 new_fcf_record),
2249 phba->fcf.current_rec.fcf_indx);
2250 goto read_next_fcf;
2251 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002252 /*
2253 * In case the current in-use FCF record becomes
2254 * invalid/unavailable during FCF discovery that
2255 * was not triggered by fast FCF failover process,
2256 * treat it as fast FCF failover.
2257 */
2258 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2259 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2260 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2261 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002262 "(x%x), enter FCF failover "
2263 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002264 phba->fcf.current_rec.fcf_indx);
2265 spin_lock_irq(&phba->hbalock);
2266 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2267 spin_unlock_irq(&phba->hbalock);
2268 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2269 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2270 LPFC_FCOE_FCF_GET_FIRST);
2271 return;
2272 }
2273 }
James Smart6fb120a2009-05-22 14:52:59 -04002274 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002275 } else {
2276 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002277 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2278 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002279 if (rc)
2280 goto read_next_fcf;
2281 }
2282
James Smart6fb120a2009-05-22 14:52:59 -04002283 /*
2284 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002285 * FCF record for the discovery. The condition that a rescan
2286 * matches the in-use FCF record: fabric name, switch name, mac
2287 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002288 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002289 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002290 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002291 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2292 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002293 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002294 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2295 phba->fcf.current_rec.fcf_indx) {
2296 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2297 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2298 /* Stop FCF redisc wait timer */
2299 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2300 phba);
2301 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2302 /* Fast failover, mark completed */
2303 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2304 spin_unlock_irq(&phba->hbalock);
2305 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2306 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002307 "FCF (x%x), port_state:x%x, "
2308 "fc_flag:x%x\n",
2309 phba->fcf.current_rec.fcf_indx,
2310 phba->pport->port_state,
2311 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002312 goto out;
2313 } else
2314 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2315 "2863 New FCF (x%x) matches "
2316 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002317 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002318 new_fcf_record),
2319 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002320 }
James Smartecfd03c2010-02-12 14:41:27 -05002321 /*
2322 * Read next FCF record from HBA searching for the matching
2323 * with in-use record only if not during the fast failover
2324 * period. In case of fast failover period, it shall try to
2325 * determine whether the FCF record just read should be the
2326 * next candidate.
2327 */
2328 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002329 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002330 goto read_next_fcf;
2331 }
James Smart6fb120a2009-05-22 14:52:59 -04002332 }
James Smartecfd03c2010-02-12 14:41:27 -05002333 /*
2334 * Update on failover FCF record only if it's in FCF fast-failover
2335 * period; otherwise, update on current FCF record.
2336 */
James Smartfc2b9892010-02-26 14:15:29 -05002337 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2338 fcf_rec = &phba->fcf.failover_rec;
2339 else
James Smartecfd03c2010-02-12 14:41:27 -05002340 fcf_rec = &phba->fcf.current_rec;
2341
James Smart6fb120a2009-05-22 14:52:59 -04002342 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2343 /*
James Smartecfd03c2010-02-12 14:41:27 -05002344 * If the driver FCF record does not have boot flag
2345 * set and new hba fcf record has boot flag set, use
2346 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002347 */
James Smartecfd03c2010-02-12 14:41:27 -05002348 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2349 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002350 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2351 "2837 Update current FCF record "
2352 "(x%x) with new FCF record (x%x)\n",
2353 fcf_rec->fcf_indx,
2354 bf_get(lpfc_fcf_record_fcf_index,
2355 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002356 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2357 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002358 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002359 goto read_next_fcf;
2360 }
2361 /*
James Smartecfd03c2010-02-12 14:41:27 -05002362 * If the driver FCF record has boot flag set and the
2363 * new hba FCF record does not have boot flag, read
2364 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002365 */
James Smartecfd03c2010-02-12 14:41:27 -05002366 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002367 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002368 goto read_next_fcf;
2369 }
2370 /*
James Smartecfd03c2010-02-12 14:41:27 -05002371 * If the new hba FCF record has lower priority value
2372 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002373 */
James Smartfc2b9892010-02-26 14:15:29 -05002374 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002375 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002376 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2377 "2838 Update current FCF record "
2378 "(x%x) with new FCF record (x%x)\n",
2379 fcf_rec->fcf_indx,
2380 bf_get(lpfc_fcf_record_fcf_index,
2381 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002382 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2383 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002384 /* Reset running random FCF selection count */
2385 phba->fcf.eligible_fcf_cnt = 1;
2386 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2387 /* Update running random FCF selection count */
2388 phba->fcf.eligible_fcf_cnt++;
2389 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2390 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002391 if (select_new_fcf) {
2392 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2393 "2839 Update current FCF record "
2394 "(x%x) with new FCF record (x%x)\n",
2395 fcf_rec->fcf_indx,
2396 bf_get(lpfc_fcf_record_fcf_index,
2397 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002398 /* Choose the new FCF by random selection */
2399 __lpfc_update_fcf_record(phba, fcf_rec,
2400 new_fcf_record,
2401 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002402 }
James Smart6fb120a2009-05-22 14:52:59 -04002403 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002404 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002405 goto read_next_fcf;
2406 }
2407 /*
James Smartecfd03c2010-02-12 14:41:27 -05002408 * This is the first suitable FCF record, choose this record for
2409 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002410 */
James Smartecfd03c2010-02-12 14:41:27 -05002411 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002412 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002413 "2840 Update initial FCF candidate "
2414 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002415 bf_get(lpfc_fcf_record_fcf_index,
2416 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002417 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2418 addr_mode, vlan_id, (boot_flag ?
2419 BOOT_ENABLE : 0));
2420 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002421 /* Setup initial running random FCF selection count */
2422 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002423 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002424 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002425 goto read_next_fcf;
2426
2427read_next_fcf:
2428 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002429 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2430 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2431 /*
2432 * Case of FCF fast failover scan
2433 */
2434
2435 /*
2436 * It has not found any suitable FCF record, cancel
2437 * FCF scan inprogress, and do nothing
2438 */
2439 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002440 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002441 "2782 No suitable FCF found: "
2442 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002443 phba->fcoe_eventtag_at_fcf_scan,
2444 bf_get(lpfc_fcf_record_fcf_index,
2445 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002446 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002447 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2448 phba->hba_flag &= ~FCF_TS_INPROG;
2449 spin_unlock_irq(&phba->hbalock);
2450 /* Unregister in-use FCF and rescan */
2451 lpfc_printf_log(phba, KERN_INFO,
2452 LOG_FIP,
2453 "2864 On devloss tmo "
2454 "unreg in-use FCF and "
2455 "rescan FCF table\n");
2456 lpfc_unregister_fcf_rescan(phba);
2457 return;
2458 }
2459 /*
2460 * Let next new FCF event trigger fast failover
2461 */
2462 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002463 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002464 return;
2465 }
2466 /*
2467 * It has found a suitable FCF record that is not
2468 * the same as in-use FCF record, unregister the
2469 * in-use FCF record, replace the in-use FCF record
2470 * with the new FCF record, mark FCF fast failover
2471 * completed, and then start register the new FCF
2472 * record.
2473 */
2474
James Smart0c9ab6f2010-02-26 14:15:57 -05002475 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002476 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002477
2478 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002479 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002480 "2842 Replace in-use FCF (x%x) "
2481 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002482 phba->fcf.current_rec.fcf_indx,
2483 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002484 memcpy(&phba->fcf.current_rec,
2485 &phba->fcf.failover_rec,
2486 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002487 /*
2488 * Mark the fast FCF failover rediscovery completed
2489 * and the start of the first round of the roundrobin
2490 * FCF failover.
2491 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002492 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002493 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002494 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002495 /* Register to the new FCF record */
2496 lpfc_register_fcf(phba);
2497 } else {
2498 /*
2499 * In case of transaction period to fast FCF failover,
2500 * do nothing when search to the end of the FCF table.
2501 */
2502 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2503 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2504 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002505
James Smart7d791df2011-07-22 18:37:52 -04002506 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2507 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002508 /*
2509 * In case the current in-use FCF record no
2510 * longer existed during FCF discovery that
2511 * was not triggered by fast FCF failover
2512 * process, treat it as fast FCF failover.
2513 */
2514 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2515 "2841 In-use FCF record (x%x) "
2516 "not reported, entering fast "
2517 "FCF failover mode scanning.\n",
2518 phba->fcf.current_rec.fcf_indx);
2519 spin_lock_irq(&phba->hbalock);
2520 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2521 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002522 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2523 LPFC_FCOE_FCF_GET_FIRST);
2524 return;
2525 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002526 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002527 lpfc_register_fcf(phba);
2528 }
2529 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002530 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002531 return;
2532
2533out:
2534 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2535 lpfc_register_fcf(phba);
2536
2537 return;
2538}
2539
2540/**
James Smarta93ff372010-10-22 11:06:08 -04002541 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002542 * @phba: pointer to lpfc hba data structure.
2543 * @mboxq: pointer to mailbox object.
2544 *
James Smarta93ff372010-10-22 11:06:08 -04002545 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002546 * read FCF record mailbox command from the eligible FCF record bmask for
2547 * performing the failover. If the FCF read back is not valid/available, it
2548 * fails through to retrying FLOGI to the currently registered FCF again.
2549 * Otherwise, if the FCF read back is valid and available, it will set the
2550 * newly read FCF record to the failover FCF record, unregister currently
2551 * registered FCF record, copy the failover FCF record to the current
2552 * FCF record, and then register the current FCF record before proceeding
2553 * to trying FLOGI on the new failover FCF.
2554 */
2555void
2556lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2557{
2558 struct fcf_record *new_fcf_record;
2559 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002560 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002561 uint16_t current_fcf_index;
2562 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002563 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002564
James Smarta93ff372010-10-22 11:06:08 -04002565 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002566 if (phba->link_state < LPFC_LINK_UP) {
2567 spin_lock_irq(&phba->hbalock);
2568 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002569 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002570 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002571 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002572 }
2573
2574 /* Parse the FCF record from the non-embedded mailbox command */
2575 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2576 &next_fcf_index);
2577 if (!new_fcf_record) {
2578 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2579 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002580 "failed to retrieve a FCF record. "
2581 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2582 phba->fcf.fcf_flag);
2583 lpfc_unregister_fcf_rescan(phba);
2584 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002585 }
2586
2587 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002588 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2589 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002590
2591 /* Log the FCF record information if turned on */
2592 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2593 next_fcf_index);
2594
James Smarta93ff372010-10-22 11:06:08 -04002595 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2596 if (!rc) {
2597 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2598 "2848 Remove ineligible FCF (x%x) from "
2599 "from roundrobin bmask\n", fcf_index);
2600 /* Clear roundrobin bmask bit for ineligible FCF */
2601 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2602 /* Perform next round of roundrobin FCF failover */
2603 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2604 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2605 if (rc)
2606 goto out;
2607 goto error_out;
2608 }
2609
2610 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2611 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2612 "2760 Perform FLOGI roundrobin FCF failover: "
2613 "FCF (x%x) back to FCF (x%x)\n",
2614 phba->fcf.current_rec.fcf_indx, fcf_index);
2615 /* Wait 500 ms before retrying FLOGI to current FCF */
2616 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002617 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002618 goto out;
2619 }
2620
James Smart0c9ab6f2010-02-26 14:15:57 -05002621 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002622 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002623 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2624 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002625 spin_lock_irq(&phba->hbalock);
2626 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2627 new_fcf_record, addr_mode, vlan_id,
2628 (boot_flag ? BOOT_ENABLE : 0));
2629 spin_unlock_irq(&phba->hbalock);
2630
2631 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2632
2633 /* Unregister the current in-use FCF record */
2634 lpfc_unregister_fcf(phba);
2635
2636 /* Replace in-use record with the new record */
2637 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2638 sizeof(struct lpfc_fcf_rec));
2639
2640 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002641 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2642 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002643
James Smarta93ff372010-10-22 11:06:08 -04002644error_out:
2645 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002646out:
2647 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002648}
2649
2650/**
2651 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2652 * @phba: pointer to lpfc hba data structure.
2653 * @mboxq: pointer to mailbox object.
2654 *
2655 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002656 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002657 * failover when a new FCF event happened. If the FCF read back is
2658 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002659 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002660 */
2661void
2662lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2663{
2664 struct fcf_record *new_fcf_record;
2665 uint32_t boot_flag, addr_mode;
2666 uint16_t fcf_index, next_fcf_index;
2667 uint16_t vlan_id;
2668 int rc;
2669
2670 /* If link state is not up, no need to proceed */
2671 if (phba->link_state < LPFC_LINK_UP)
2672 goto out;
2673
2674 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002675 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002676 goto out;
2677
2678 /* Parse the FCF record from the non-embedded mailbox command */
2679 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2680 &next_fcf_index);
2681 if (!new_fcf_record) {
2682 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2683 "2767 Mailbox command READ_FCF_RECORD "
2684 "failed to retrieve a FCF record.\n");
2685 goto out;
2686 }
2687
2688 /* Check the connection list for eligibility */
2689 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2690 &addr_mode, &vlan_id);
2691
2692 /* Log the FCF record information if turned on */
2693 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2694 next_fcf_index);
2695
2696 if (!rc)
2697 goto out;
2698
2699 /* Update the eligible FCF record index bmask */
2700 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002701
2702 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002703
2704out:
2705 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2706}
2707
2708/**
James Smart76a95d72010-11-20 23:11:48 -05002709 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2710 * @phba: pointer to lpfc hba data structure.
2711 * @mboxq: pointer to mailbox data structure.
2712 *
2713 * This function handles completion of init vfi mailbox command.
2714 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002715static void
James Smart76a95d72010-11-20 23:11:48 -05002716lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2717{
2718 struct lpfc_vport *vport = mboxq->vport;
2719
James Smartdf9e1b52011-12-13 13:22:17 -05002720 /*
2721 * VFI not supported on interface type 0, just do the flogi
2722 * Also continue if the VFI is in use - just use the same one.
2723 */
2724 if (mboxq->u.mb.mbxStatus &&
2725 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2726 LPFC_SLI_INTF_IF_TYPE_0) &&
2727 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002728 lpfc_printf_vlog(vport, KERN_ERR,
2729 LOG_MBOX,
2730 "2891 Init VFI mailbox failed 0x%x\n",
2731 mboxq->u.mb.mbxStatus);
2732 mempool_free(mboxq, phba->mbox_mem_pool);
2733 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2734 return;
2735 }
James Smart73d91e52011-10-10 21:32:10 -04002736
James Smart76a95d72010-11-20 23:11:48 -05002737 lpfc_initial_flogi(vport);
2738 mempool_free(mboxq, phba->mbox_mem_pool);
2739 return;
2740}
2741
2742/**
2743 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2744 * @vport: pointer to lpfc_vport data structure.
2745 *
2746 * This function issue a init_vfi mailbox command to initialize the VFI and
2747 * VPI for the physical port.
2748 */
2749void
2750lpfc_issue_init_vfi(struct lpfc_vport *vport)
2751{
2752 LPFC_MBOXQ_t *mboxq;
2753 int rc;
2754 struct lpfc_hba *phba = vport->phba;
2755
2756 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2757 if (!mboxq) {
2758 lpfc_printf_vlog(vport, KERN_ERR,
2759 LOG_MBOX, "2892 Failed to allocate "
2760 "init_vfi mailbox\n");
2761 return;
2762 }
2763 lpfc_init_vfi(mboxq, vport);
2764 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2765 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2766 if (rc == MBX_NOT_FINISHED) {
2767 lpfc_printf_vlog(vport, KERN_ERR,
2768 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2769 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2770 }
2771}
2772
2773/**
James Smart1c6834a2009-07-19 10:01:26 -04002774 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2775 * @phba: pointer to lpfc hba data structure.
2776 * @mboxq: pointer to mailbox data structure.
2777 *
2778 * This function handles completion of init vpi mailbox command.
2779 */
James Smart695a8142010-01-26 23:08:03 -05002780void
James Smart1c6834a2009-07-19 10:01:26 -04002781lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2782{
2783 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002784 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002785 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2786
James Smart1c6834a2009-07-19 10:01:26 -04002787 if (mboxq->u.mb.mbxStatus) {
2788 lpfc_printf_vlog(vport, KERN_ERR,
2789 LOG_MBOX,
2790 "2609 Init VPI mailbox failed 0x%x\n",
2791 mboxq->u.mb.mbxStatus);
2792 mempool_free(mboxq, phba->mbox_mem_pool);
2793 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2794 return;
2795 }
James Smart72100cc2010-02-12 14:43:01 -05002796 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002797 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002798 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002799
James Smart695a8142010-01-26 23:08:03 -05002800 /* If this port is physical port or FDISC is done, do reg_vpi */
2801 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2802 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2803 if (!ndlp)
2804 lpfc_printf_vlog(vport, KERN_ERR,
2805 LOG_DISCOVERY,
2806 "2731 Cannot find fabric "
2807 "controller node\n");
2808 else
2809 lpfc_register_new_vport(phba, vport, ndlp);
2810 mempool_free(mboxq, phba->mbox_mem_pool);
2811 return;
2812 }
2813
James Smart1c6834a2009-07-19 10:01:26 -04002814 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2815 lpfc_initial_fdisc(vport);
2816 else {
2817 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002818 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2819 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002820 }
James Smart695a8142010-01-26 23:08:03 -05002821 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002822 return;
2823}
2824
2825/**
James Smartecfd03c2010-02-12 14:41:27 -05002826 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2827 * @vport: pointer to lpfc_vport data structure.
2828 *
2829 * This function issue a init_vpi mailbox command to initialize
2830 * VPI for the vport.
2831 */
2832void
2833lpfc_issue_init_vpi(struct lpfc_vport *vport)
2834{
2835 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002836 int rc, vpi;
2837
2838 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2839 vpi = lpfc_alloc_vpi(vport->phba);
2840 if (!vpi) {
2841 lpfc_printf_vlog(vport, KERN_ERR,
2842 LOG_MBOX,
2843 "3303 Failed to obtain vport vpi\n");
2844 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2845 return;
2846 }
2847 vport->vpi = vpi;
2848 }
James Smartecfd03c2010-02-12 14:41:27 -05002849
2850 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2851 if (!mboxq) {
2852 lpfc_printf_vlog(vport, KERN_ERR,
2853 LOG_MBOX, "2607 Failed to allocate "
2854 "init_vpi mailbox\n");
2855 return;
2856 }
2857 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2858 mboxq->vport = vport;
2859 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2860 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2861 if (rc == MBX_NOT_FINISHED) {
2862 lpfc_printf_vlog(vport, KERN_ERR,
2863 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2864 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2865 }
2866}
2867
2868/**
James Smart6fb120a2009-05-22 14:52:59 -04002869 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2870 * @phba: pointer to lpfc hba data structure.
2871 *
2872 * This function loops through the list of vports on the @phba and issues an
2873 * FDISC if possible.
2874 */
2875void
2876lpfc_start_fdiscs(struct lpfc_hba *phba)
2877{
2878 struct lpfc_vport **vports;
2879 int i;
2880
2881 vports = lpfc_create_vport_work_array(phba);
2882 if (vports != NULL) {
2883 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2884 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2885 continue;
2886 /* There are no vpi for this vport */
2887 if (vports[i]->vpi > phba->max_vpi) {
2888 lpfc_vport_set_state(vports[i],
2889 FC_VPORT_FAILED);
2890 continue;
2891 }
James Smart76a95d72010-11-20 23:11:48 -05002892 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002893 lpfc_vport_set_state(vports[i],
2894 FC_VPORT_LINKDOWN);
2895 continue;
2896 }
James Smart1c6834a2009-07-19 10:01:26 -04002897 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002898 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002899 continue;
2900 }
James Smart6fb120a2009-05-22 14:52:59 -04002901 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2902 lpfc_initial_fdisc(vports[i]);
2903 else {
2904 lpfc_vport_set_state(vports[i],
2905 FC_VPORT_NO_FABRIC_SUPP);
2906 lpfc_printf_vlog(vports[i], KERN_ERR,
2907 LOG_ELS,
2908 "0259 No NPIV "
2909 "Fabric support\n");
2910 }
2911 }
2912 }
2913 lpfc_destroy_vport_work_array(phba, vports);
2914}
2915
2916void
2917lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2918{
2919 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2920 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002921 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002922
James Smartf5eca9b2012-03-01 22:37:54 -05002923 /*
2924 * VFI not supported for interface type 0, so ignore any mailbox
2925 * error (except VFI in use) and continue with the discovery.
2926 */
2927 if (mboxq->u.mb.mbxStatus &&
2928 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2929 LPFC_SLI_INTF_IF_TYPE_0) &&
2930 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002931 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2932 "2018 REG_VFI mbxStatus error x%x "
2933 "HBA state x%x\n",
2934 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002935 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002936 /* FLOGI failed, use loop map to make discovery list */
2937 lpfc_disc_list_loopmap(vport);
2938 /* Start discovery */
2939 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002940 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002941 }
2942 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002943 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002944 }
James Smartae05ebe2013-03-01 16:35:38 -05002945
James Smarte74c03c2013-04-17 20:15:19 -04002946 /* If the VFI is already registered, there is nothing else to do
2947 * Unless this was a VFI update and we are in PT2PT mode, then
2948 * we should drop through to set the port state to ready.
2949 */
James Smartae05ebe2013-03-01 16:35:38 -05002950 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002951 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2952 vport->fc_flag & FC_PT2PT))
2953 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002954
James Smartc8685952009-11-18 15:39:16 -05002955 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002956 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002957 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002958 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002959 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002960 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002961 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002962
James Smart1b511972011-12-13 13:23:09 -05002963 /* In case SLI4 FC loopback test, we are ready */
2964 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2965 (phba->link_flag & LS_LOOPBACK_MODE)) {
2966 phba->link_state = LPFC_HBA_READY;
2967 goto out_free_mem;
2968 }
2969
James Smarte74c03c2013-04-17 20:15:19 -04002970 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2971 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2972 "alpacnt:%d LinkState:%x topology:%x\n",
2973 vport->port_state, vport->fc_flag, vport->fc_myDID,
2974 vport->phba->alpa_map[0],
2975 phba->link_state, phba->fc_topology);
2976
James Smart6fb120a2009-05-22 14:52:59 -04002977 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002978 /*
2979 * For private loop or for NPort pt2pt,
2980 * just start discovery and we are done.
2981 */
2982 if ((vport->fc_flag & FC_PT2PT) ||
2983 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2984 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2985
James Smart76a95d72010-11-20 23:11:48 -05002986 /* Use loop map to make discovery list */
2987 lpfc_disc_list_loopmap(vport);
2988 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002989 if (vport->fc_flag & FC_PT2PT)
2990 vport->port_state = LPFC_VPORT_READY;
2991 else
2992 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002993 } else {
2994 lpfc_start_fdiscs(phba);
2995 lpfc_do_scr_ns_plogi(phba, vport);
2996 }
James Smart6fb120a2009-05-22 14:52:59 -04002997 }
2998
James Smart1b511972011-12-13 13:23:09 -05002999out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04003000 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05003001 if (dmabuf) {
3002 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
3003 kfree(dmabuf);
3004 }
James Smart6fb120a2009-05-22 14:52:59 -04003005 return;
3006}
3007
3008static void
James Smart2e0fef82007-06-17 19:56:36 -05003009lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003010{
James Smart6fb120a2009-05-22 14:52:59 -04003011 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003012 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003013 struct lpfc_vport *vport = pmb->vport;
James Smartaeb3c812017-04-21 16:05:02 -07003014 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart21bf0b92015-08-31 16:48:21 -04003015 struct serv_parm *sp = &vport->fc_sparam;
3016 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003017
3018 /* Check for error */
3019 if (mb->mbxStatus) {
3020 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003021 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3022 "0319 READ_SPARAM mbxStatus error x%x "
3023 "hba state x%x>\n",
3024 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003025 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003026 goto out;
3027 }
3028
James Smart2e0fef82007-06-17 19:56:36 -05003029 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003030 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003031
3032 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3033 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3034 ed_tov = (ed_tov + 999999) / 1000000;
3035
3036 phba->fc_edtov = ed_tov;
3037 phba->fc_ratov = (2 * ed_tov) / 1000;
3038 if (phba->fc_ratov < FF_DEF_RATOV) {
3039 /* RA_TOV should be atleast 10sec for initial flogi */
3040 phba->fc_ratov = FF_DEF_RATOV;
3041 }
3042
James Smart05580562011-05-24 11:40:48 -04003043 lpfc_update_vport_wwn(vport);
James Smartaeb3c812017-04-21 16:05:02 -07003044 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
James Smart92d7f7b2007-06-17 19:56:38 -05003045 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3046 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3047 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3048 }
3049
dea31012005-04-17 16:05:31 -05003050 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3051 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003052 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003053 return;
3054
3055out:
3056 pmb->context1 = NULL;
3057 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3058 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003059 lpfc_issue_clear_la(phba, vport);
3060 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003061 return;
3062}
3063
3064static void
James Smart76a95d72010-11-20 23:11:48 -05003065lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003066{
James Smart92d7f7b2007-06-17 19:56:38 -05003067 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003068 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003069 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003070 int i;
James Smart14691152006-12-02 13:34:28 -05003071 struct lpfc_dmabuf *mp;
3072 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003073 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003074 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003075
James Smart92d7f7b2007-06-17 19:56:38 -05003076 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003077 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3078
3079 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3080 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3081 case LPFC_LINK_SPEED_1GHZ:
3082 case LPFC_LINK_SPEED_2GHZ:
3083 case LPFC_LINK_SPEED_4GHZ:
3084 case LPFC_LINK_SPEED_8GHZ:
3085 case LPFC_LINK_SPEED_10GHZ:
3086 case LPFC_LINK_SPEED_16GHZ:
3087 case LPFC_LINK_SPEED_32GHZ:
3088 break;
3089 default:
3090 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3091 break;
3092 }
dea31012005-04-17 16:05:31 -05003093 }
3094
James Smarte74c03c2013-04-17 20:15:19 -04003095 if (phba->fc_topology &&
3096 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3097 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3098 "3314 Toplogy changed was 0x%x is 0x%x\n",
3099 phba->fc_topology,
3100 bf_get(lpfc_mbx_read_top_topology, la));
3101 phba->fc_topology_changed = 1;
3102 }
3103
James Smart76a95d72010-11-20 23:11:48 -05003104 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003105 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003106
James Smartdf9e1b52011-12-13 13:22:17 -05003107 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003108 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003109 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003110
James Smart6a9c52c2009-10-02 15:16:51 -04003111 /* if npiv is enabled and this adapter supports npiv log
3112 * a message that npiv is not supported in this topology
3113 */
3114 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003115 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3116 "1309 Link Up Event npiv not supported in loop "
3117 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003118 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003119 if (bf_get(lpfc_mbx_read_top_il, la))
3120 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003121
James Smart76a95d72010-11-20 23:11:48 -05003122 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3123 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003124
3125 if (i == 0) {
3126 phba->alpa_map[0] = 0;
3127 } else {
James Smarte8b62012007-08-02 11:10:09 -04003128 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003129 int numalpa, j, k;
3130 union {
3131 uint8_t pamap[16];
3132 struct {
3133 uint32_t wd1;
3134 uint32_t wd2;
3135 uint32_t wd3;
3136 uint32_t wd4;
3137 } pa;
3138 } un;
3139 numalpa = phba->alpa_map[0];
3140 j = 0;
3141 while (j < numalpa) {
3142 memset(un.pamap, 0, 16);
3143 for (k = 1; j < numalpa; k++) {
3144 un.pamap[k - 1] =
3145 phba->alpa_map[j + 1];
3146 j++;
3147 if (k == 16)
3148 break;
3149 }
3150 /* Link Up Event ALPA map */
3151 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003152 KERN_WARNING,
3153 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003154 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003155 "ALPA map Data: x%x "
3156 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003157 un.pa.wd1, un.pa.wd2,
3158 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003159 }
3160 }
3161 }
3162 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003163 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003164 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003165 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003166 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3167 }
James Smart2e0fef82007-06-17 19:56:36 -05003168 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003169 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003170 }
James Smart92d7f7b2007-06-17 19:56:38 -05003171 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003172
James Smart38c20672013-03-01 16:37:44 -05003173 if (fc_flags) {
3174 spin_lock_irq(shost->host_lock);
3175 vport->fc_flag |= fc_flags;
3176 spin_unlock_irq(shost->host_lock);
3177 }
3178
dea31012005-04-17 16:05:31 -05003179 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003180 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3181 if (!sparam_mbox)
3182 goto out;
3183
3184 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3185 if (rc) {
3186 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3187 goto out;
3188 }
3189 sparam_mbox->vport = vport;
3190 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3191 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3192 if (rc == MBX_NOT_FINISHED) {
3193 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3194 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3195 kfree(mp);
3196 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3197 goto out;
dea31012005-04-17 16:05:31 -05003198 }
3199
James Smart76a95d72010-11-20 23:11:48 -05003200 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003201 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3202 if (!cfglink_mbox)
3203 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003204 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003205 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003206 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003207 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003208 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003209 if (rc == MBX_NOT_FINISHED) {
3210 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3211 goto out;
3212 }
3213 } else {
James Smart32b97932009-07-19 10:01:21 -04003214 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003215 /*
3216 * Add the driver's default FCF record at FCF index 0 now. This
3217 * is phase 1 implementation that support FCF index 0 and driver
3218 * defaults.
3219 */
James Smart45ed1192009-10-02 15:17:02 -04003220 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003221 fcf_record = kzalloc(sizeof(struct fcf_record),
3222 GFP_KERNEL);
3223 if (unlikely(!fcf_record)) {
3224 lpfc_printf_log(phba, KERN_ERR,
3225 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003226 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003227 "fcf record\n");
3228 rc = -ENODEV;
3229 goto out;
3230 }
3231
3232 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3233 LPFC_FCOE_FCF_DEF_INDEX);
3234 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3235 if (unlikely(rc)) {
3236 lpfc_printf_log(phba, KERN_ERR,
3237 LOG_MBOX | LOG_SLI,
3238 "2013 Could not manually add FCF "
3239 "record 0, status %d\n", rc);
3240 rc = -ENODEV;
3241 kfree(fcf_record);
3242 goto out;
3243 }
3244 kfree(fcf_record);
3245 }
3246 /*
3247 * The driver is expected to do FIP/FCF. Call the port
3248 * and get the FCF Table.
3249 */
James Smart32b97932009-07-19 10:01:21 -04003250 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003251 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003252 spin_unlock_irq(&phba->hbalock);
3253 return;
3254 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003255 /* This is the initial FCF discovery scan */
3256 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003257 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003258 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3259 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003260 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3261 LPFC_FCOE_FCF_GET_FIRST);
3262 if (rc) {
3263 spin_lock_irq(&phba->hbalock);
3264 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3265 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003266 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003267 }
James Smart38b92ef2010-08-04 16:11:39 -04003268 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003269 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003270 }
James Smart6fb120a2009-05-22 14:52:59 -04003271
3272 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003273out:
3274 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003275 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3276 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3277 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003278 lpfc_issue_clear_la(phba, vport);
3279 return;
dea31012005-04-17 16:05:31 -05003280}
3281
3282static void
James Smart84774a42008-08-24 21:50:06 -04003283lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003284{
dea31012005-04-17 16:05:31 -05003285 uint32_t control;
3286 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003287 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003288 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003289 if (phba->sli_rev <= LPFC_SLI_REV3) {
3290 control = readl(phba->HCregaddr);
3291 control |= HC_LAINT_ENA;
3292 writel(control, phba->HCregaddr);
3293 readl(phba->HCregaddr); /* flush */
3294 }
James Smart2e0fef82007-06-17 19:56:36 -05003295 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003296}
3297
James Smart84774a42008-08-24 21:50:06 -04003298static void
3299lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3300{
3301 lpfc_linkdown(phba);
3302 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003303 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003304 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3305}
3306
3307
dea31012005-04-17 16:05:31 -05003308/*
James Smart76a95d72010-11-20 23:11:48 -05003309 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003310 * command upon completion. It is setup in the LPFC_MBOXQ
3311 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003312 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003313 */
3314void
James Smart76a95d72010-11-20 23:11:48 -05003315lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003316{
James Smart2e0fef82007-06-17 19:56:36 -05003317 struct lpfc_vport *vport = pmb->vport;
3318 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003319 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003320 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003321 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003322 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smartaeb3c812017-04-21 16:05:02 -07003323 uint8_t attn_type;
dea31012005-04-17 16:05:31 -05003324
James Smart0d2b6b82008-06-14 22:52:47 -04003325 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003326 pring = lpfc_phba_elsring(phba);
3327 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3328
dea31012005-04-17 16:05:31 -05003329 /* Check for error */
3330 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003331 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003332 "1307 READ_LA mbox error x%x state x%x\n",
3333 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003334 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003335 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003336 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003337 }
3338
James Smart76a95d72010-11-20 23:11:48 -05003339 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
James Smartaeb3c812017-04-21 16:05:02 -07003340 attn_type = bf_get(lpfc_mbx_read_top_att_type, la);
dea31012005-04-17 16:05:31 -05003341
3342 memcpy(&phba->alpa_map[0], mp->virt, 128);
3343
James Smart2e0fef82007-06-17 19:56:36 -05003344 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003345 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003346 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003347 else
James Smart2e0fef82007-06-17 19:56:36 -05003348 vport->fc_flag &= ~FC_BYPASSED_MODE;
3349 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003350
James Smartbe0c0082013-03-01 16:38:14 -05003351 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003352 phba->fc_stat.LinkMultiEvent++;
James Smartaeb3c812017-04-21 16:05:02 -07003353 if (attn_type == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003354 if (phba->fc_eventTag != 0)
3355 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003356 }
dea31012005-04-17 16:05:31 -05003357
3358 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003359 if (phba->sli_rev < LPFC_SLI_REV4) {
3360 spin_lock_irq(&phba->hbalock);
3361 if (bf_get(lpfc_mbx_read_top_mm, la))
3362 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3363 else
3364 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3365 spin_unlock_irq(&phba->hbalock);
3366 }
dea31012005-04-17 16:05:31 -05003367
James Smart4d9ab992009-10-02 15:16:39 -04003368 phba->link_events++;
James Smartaeb3c812017-04-21 16:05:02 -07003369 if ((attn_type == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003370 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003371 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003372 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003373 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003374 "1306 Link Up Event in loop back mode "
3375 "x%x received Data: x%x x%x x%x x%x\n",
3376 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003377 bf_get(lpfc_mbx_read_top_alpa_granted,
3378 la),
3379 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003380 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003381 } else {
3382 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003383 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003384 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003385 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003386 bf_get(lpfc_mbx_read_top_alpa_granted,
3387 la),
3388 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003389 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003390 bf_get(lpfc_mbx_read_top_mm, la),
3391 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003392 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003393 }
James Smart92d7f7b2007-06-17 19:56:38 -05003394 lpfc_mbx_process_link_up(phba, la);
James Smartaeb3c812017-04-21 16:05:02 -07003395 } else if (attn_type == LPFC_ATT_LINK_DOWN ||
3396 attn_type == LPFC_ATT_UNEXP_WWPN) {
dea31012005-04-17 16:05:31 -05003397 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003398 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003399 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3400 "1308 Link Down Event in loop back mode "
3401 "x%x received "
3402 "Data: x%x x%x x%x\n",
3403 la->eventTag, phba->fc_eventTag,
3404 phba->pport->port_state, vport->fc_flag);
James Smartaeb3c812017-04-21 16:05:02 -07003405 else if (attn_type == LPFC_ATT_UNEXP_WWPN)
3406 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3407 "1313 Link Down UNEXP WWPN Event x%x received "
3408 "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,
3411 bf_get(lpfc_mbx_read_top_mm, la),
3412 bf_get(lpfc_mbx_read_top_fa, la));
James Smart1b511972011-12-13 13:23:09 -05003413 else
James Smart3163f722008-02-08 18:50:25 -05003414 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003415 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003416 "Data: x%x x%x x%x x%x x%x\n",
3417 la->eventTag, phba->fc_eventTag,
3418 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003419 bf_get(lpfc_mbx_read_top_mm, la),
3420 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003421 lpfc_mbx_issue_link_down(phba);
3422 }
James Smartaeb3c812017-04-21 16:05:02 -07003423 if (phba->sli.sli_flag & LPFC_MENLO_MAINT &&
3424 attn_type == LPFC_ATT_LINK_UP) {
James Smart84774a42008-08-24 21:50:06 -04003425 if (phba->link_state != LPFC_LINK_DOWN) {
3426 phba->fc_stat.LinkDown++;
3427 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3428 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003429 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003430 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003431 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003432 lpfc_mbx_issue_link_down(phba);
3433 } else
3434 lpfc_enable_la(phba);
3435
3436 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3437 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3438 "Data: x%x x%x x%x\n",
3439 la->eventTag, phba->fc_eventTag,
3440 phba->pport->port_state, vport->fc_flag);
3441 /*
3442 * The cmnd that triggered this will be waiting for this
3443 * signal.
3444 */
3445 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3446 if (phba->wait_4_mlo_maint_flg) {
3447 phba->wait_4_mlo_maint_flg = 0;
3448 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003449 }
James Smart84774a42008-08-24 21:50:06 -04003450 }
3451
James Smartbe0c0082013-03-01 16:38:14 -05003452 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3453 bf_get(lpfc_mbx_read_top_fa, la)) {
3454 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003455 lpfc_issue_clear_la(phba, vport);
3456 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003457 "1311 fa %d\n",
3458 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003459 }
3460
James Smart76a95d72010-11-20 23:11:48 -05003461lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003462 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3463 kfree(mp);
3464 mempool_free(pmb, phba->mbox_mem_pool);
3465 return;
3466}
3467
3468/*
3469 * This routine handles processing a REG_LOGIN mailbox
3470 * command upon completion. It is setup in the LPFC_MBOXQ
3471 * as the completion routine when the command is
3472 * handed off to the SLI layer.
3473 */
3474void
James Smart2e0fef82007-06-17 19:56:36 -05003475lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003476{
James Smart2e0fef82007-06-17 19:56:36 -05003477 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003478 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003479 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003480 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003481
dea31012005-04-17 16:05:31 -05003482 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003483 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003484
James Smartbe6bb942015-04-07 15:07:22 -04003485 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3486 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3487 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003488 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003489 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003490 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3491 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3492
James Smart40426292010-12-15 17:58:10 -05003493 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3494 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003495 /* We rcvd a rscn after issuing this
3496 * mbox reg login, we may have cycled
3497 * back through the state and be
3498 * back at reg login state so this
3499 * mbox needs to be ignored becase
3500 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003501 * process.
James Smartffc95492010-06-07 15:23:17 -04003502 */
3503 spin_lock_irq(shost->host_lock);
3504 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3505 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003506
3507 /*
3508 * We cannot leave the RPI registered because
3509 * if we go thru discovery again for this ndlp
3510 * a subsequent REG_RPI will fail.
3511 */
3512 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3513 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003514 }
3515
3516 /* Call state machine */
3517 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003518
dea31012005-04-17 16:05:31 -05003519 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3520 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003521 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003522 /* decrement the node reference count held for this callback
3523 * function.
3524 */
James Smart329f9bc2007-04-25 09:53:01 -04003525 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003526
3527 return;
3528}
3529
James Smart92d7f7b2007-06-17 19:56:38 -05003530static void
3531lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3532{
James Smart04c68492009-05-22 14:52:52 -04003533 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003534 struct lpfc_vport *vport = pmb->vport;
3535 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3536
3537 switch (mb->mbxStatus) {
3538 case 0x0011:
3539 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003540 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3541 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3542 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003543 break;
James Smart78730cf2010-04-06 15:06:30 -04003544 /* If VPI is busy, reset the HBA */
3545 case 0x9700:
3546 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3547 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3548 vport->vpi, mb->mbxStatus);
3549 if (!(phba->pport->load_flag & FC_UNLOADING))
3550 lpfc_workq_post_event(phba, NULL, NULL,
3551 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003552 }
James Smart72100cc2010-02-12 14:43:01 -05003553 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003554 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003555 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003556 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003557 vport->unreg_vpi_cmpl = VPORT_OK;
3558 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003559 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003560 /*
3561 * This shost reference might have been taken at the beginning of
3562 * lpfc_vport_delete()
3563 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003564 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003565 scsi_host_put(shost);
3566}
3567
James Smartd7c255b2008-08-24 21:50:00 -04003568int
James Smart92d7f7b2007-06-17 19:56:38 -05003569lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3570{
3571 struct lpfc_hba *phba = vport->phba;
3572 LPFC_MBOXQ_t *mbox;
3573 int rc;
3574
3575 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3576 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003577 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003578
3579 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3580 mbox->vport = vport;
3581 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003582 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003583 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003584 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3585 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003586 mempool_free(mbox, phba->mbox_mem_pool);
3587 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003588 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003589 }
James Smartd7c255b2008-08-24 21:50:00 -04003590 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003591}
3592
3593static void
3594lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3595{
3596 struct lpfc_vport *vport = pmb->vport;
3597 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003598 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003599
3600 switch (mb->mbxStatus) {
3601 case 0x0011:
3602 case 0x9601:
3603 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003604 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3605 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3606 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003607 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3608 spin_lock_irq(shost->host_lock);
3609 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3610 spin_unlock_irq(shost->host_lock);
3611 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003612
James Smart01649562017-02-12 13:52:32 -08003613 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003614 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003615 if (phba->nvmet_support)
3616 lpfc_nvmet_update_targetport(phba);
3617 else
James Smart8c258642017-02-12 13:52:36 -08003618 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003619 }
James Smart92d7f7b2007-06-17 19:56:38 -05003620 goto out;
3621 }
James Smart92d7f7b2007-06-17 19:56:38 -05003622
James Smart72100cc2010-02-12 14:43:01 -05003623 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003624 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003625 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003626 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003627 vport->num_disc_nodes = 0;
3628 /* go thru NPR list and issue ELS PLOGIs */
3629 if (vport->fc_npr_cnt)
3630 lpfc_els_disc_plogi(vport);
3631
3632 if (!vport->num_disc_nodes) {
3633 spin_lock_irq(shost->host_lock);
3634 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3635 spin_unlock_irq(shost->host_lock);
3636 lpfc_can_disctmo(vport);
3637 }
3638 vport->port_state = LPFC_VPORT_READY;
3639
3640out:
3641 mempool_free(pmb, phba->mbox_mem_pool);
3642 return;
3643}
3644
James Smart21e9a0a2009-05-22 14:53:21 -04003645/**
3646 * lpfc_create_static_vport - Read HBA config region to create static vports.
3647 * @phba: pointer to lpfc hba data structure.
3648 *
3649 * This routine issue a DUMP mailbox command for config region 22 to get
3650 * the list of static vports to be created. The function create vports
3651 * based on the information returned from the HBA.
3652 **/
3653void
3654lpfc_create_static_vport(struct lpfc_hba *phba)
3655{
3656 LPFC_MBOXQ_t *pmb = NULL;
3657 MAILBOX_t *mb;
3658 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003659 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003660 struct fc_vport_identifiers vport_id;
3661 struct fc_vport *new_fc_vport;
3662 struct Scsi_Host *shost;
3663 struct lpfc_vport *vport;
3664 uint16_t offset = 0;
3665 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003666 struct lpfc_dmabuf *mp;
3667 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003668
3669 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3670 if (!pmb) {
3671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3672 "0542 lpfc_create_static_vport failed to"
3673 " allocate mailbox memory\n");
3674 return;
3675 }
James Smartcdcc2342012-08-14 14:26:35 -04003676 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003677 mb = &pmb->u.mb;
3678
3679 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3680 if (!vport_info) {
3681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3682 "0543 lpfc_create_static_vport failed to"
3683 " allocate vport_info\n");
3684 mempool_free(pmb, phba->mbox_mem_pool);
3685 return;
3686 }
3687
3688 vport_buff = (uint8_t *) vport_info;
3689 do {
James Smartcdcc2342012-08-14 14:26:35 -04003690 /* free dma buffer from previous round */
3691 if (pmb->context1) {
3692 mp = (struct lpfc_dmabuf *)pmb->context1;
3693 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3694 kfree(mp);
3695 }
James Smart1c6834a2009-07-19 10:01:26 -04003696 if (lpfc_dump_static_vport(phba, pmb, offset))
3697 goto out;
3698
James Smart21e9a0a2009-05-22 14:53:21 -04003699 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003700 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3701 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003702
James Smartcdcc2342012-08-14 14:26:35 -04003703 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003704 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3705 "0544 lpfc_create_static_vport failed to"
3706 " issue dump mailbox command ret 0x%x "
3707 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003708 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003709 goto out;
3710 }
3711
James Smart1c6834a2009-07-19 10:01:26 -04003712 if (phba->sli_rev == LPFC_SLI_REV4) {
3713 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003714 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003715 if (byte_count > sizeof(struct static_vport_info) -
3716 offset)
3717 byte_count = sizeof(struct static_vport_info)
3718 - offset;
3719 memcpy(vport_buff + offset, mp->virt, byte_count);
3720 offset += byte_count;
3721 } else {
3722 if (mb->un.varDmp.word_cnt >
3723 sizeof(struct static_vport_info) - offset)
3724 mb->un.varDmp.word_cnt =
3725 sizeof(struct static_vport_info)
3726 - offset;
3727 byte_count = mb->un.varDmp.word_cnt;
3728 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3729 vport_buff + offset,
3730 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003731
James Smart1c6834a2009-07-19 10:01:26 -04003732 offset += byte_count;
3733 }
James Smart21e9a0a2009-05-22 14:53:21 -04003734
James Smart1c6834a2009-07-19 10:01:26 -04003735 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003736 offset < sizeof(struct static_vport_info));
3737
3738
3739 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3740 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3741 != VPORT_INFO_REV)) {
3742 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3743 "0545 lpfc_create_static_vport bad"
3744 " information header 0x%x 0x%x\n",
3745 le32_to_cpu(vport_info->signature),
3746 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3747
3748 goto out;
3749 }
3750
3751 shost = lpfc_shost_from_vport(phba->pport);
3752
3753 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3754 memset(&vport_id, 0, sizeof(vport_id));
3755 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3756 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3757 if (!vport_id.port_name || !vport_id.node_name)
3758 continue;
3759
3760 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3761 vport_id.vport_type = FC_PORTTYPE_NPIV;
3762 vport_id.disable = false;
3763 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3764
3765 if (!new_fc_vport) {
3766 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3767 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003768 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003769 continue;
3770 }
3771
3772 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3773 vport->vport_flag |= STATIC_VPORT;
3774 }
3775
3776out:
James Smart21e9a0a2009-05-22 14:53:21 -04003777 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003778 if (mbx_wait_rc != MBX_TIMEOUT) {
3779 if (pmb->context1) {
3780 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003781 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3782 kfree(mp);
3783 }
James Smart21e9a0a2009-05-22 14:53:21 -04003784 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003785 }
James Smart21e9a0a2009-05-22 14:53:21 -04003786
3787 return;
3788}
3789
dea31012005-04-17 16:05:31 -05003790/*
3791 * This routine handles processing a Fabric REG_LOGIN mailbox
3792 * command upon completion. It is setup in the LPFC_MBOXQ
3793 * as the completion routine when the command is
3794 * handed off to the SLI layer.
3795 */
3796void
James Smart2e0fef82007-06-17 19:56:36 -05003797lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003798{
James Smart92d7f7b2007-06-17 19:56:38 -05003799 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003800 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003801 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003802 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003803 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003804
James Smart549e55c2007-08-02 11:09:51 -04003805 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003806 pmb->context1 = NULL;
3807 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003808
dea31012005-04-17 16:05:31 -05003809 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003810 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3811 "0258 Register Fabric login error: 0x%x\n",
3812 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003813 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3814 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003815 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003816
James Smart76a95d72010-11-20 23:11:48 -05003817 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003818 /* FLOGI failed, use loop map to make discovery list */
3819 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003820
James Smart92d7f7b2007-06-17 19:56:38 -05003821 /* Start discovery */
3822 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003823 /* Decrement the reference count to ndlp after the
3824 * reference to the ndlp are done.
3825 */
3826 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003827 return;
3828 }
3829
3830 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003831 /* Decrement the reference count to ndlp after the reference
3832 * to the ndlp are done.
3833 */
3834 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003835 return;
3836 }
3837
James Smart6d368e52011-05-24 11:44:12 -04003838 if (phba->sli_rev < LPFC_SLI_REV4)
3839 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003840 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003841 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003842 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003843
James Smart2e0fef82007-06-17 19:56:36 -05003844 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003845 /* when physical port receive logo donot start
3846 * vport discovery */
3847 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3848 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003849 else {
3850 shost = lpfc_shost_from_vport(vport);
3851 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003852 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003853 spin_unlock_irq(shost->host_lock);
3854 }
James Smart92d7f7b2007-06-17 19:56:38 -05003855 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003856 }
3857
3858 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3859 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003860 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003861
3862 /* Drop the reference count from the mbox at the end after
3863 * all the current reference to the ndlp have been done.
3864 */
3865 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003866 return;
3867}
3868
James Smarta0f2d3e2017-02-12 13:52:31 -08003869 /*
3870 * This routine will issue a GID_FT for each FC4 Type supported
3871 * by the driver. ALL GID_FTs must complete before discovery is started.
3872 */
3873int
3874lpfc_issue_gidft(struct lpfc_vport *vport)
3875{
3876 struct lpfc_hba *phba = vport->phba;
3877
3878 /* Good status, issue CT Request to NameServer */
3879 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3880 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3881 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3882 /* Cannot issue NameServer FCP Query, so finish up
3883 * discovery
3884 */
3885 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3886 "0604 %s FC TYPE %x %s\n",
3887 "Failed to issue GID_FT to ",
3888 FC_TYPE_FCP,
3889 "Finishing discovery.");
3890 return 0;
3891 }
3892 vport->gidft_inp++;
3893 }
3894
3895 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3896 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3897 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3898 /* Cannot issue NameServer NVME Query, so finish up
3899 * discovery
3900 */
3901 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3902 "0605 %s FC_TYPE %x %s %d\n",
3903 "Failed to issue GID_FT to ",
3904 FC_TYPE_NVME,
3905 "Finishing discovery: gidftinp ",
3906 vport->gidft_inp);
3907 if (vport->gidft_inp == 0)
3908 return 0;
3909 } else
3910 vport->gidft_inp++;
3911 }
3912 return vport->gidft_inp;
3913}
3914
dea31012005-04-17 16:05:31 -05003915/*
3916 * This routine handles processing a NameServer REG_LOGIN mailbox
3917 * command upon completion. It is setup in the LPFC_MBOXQ
3918 * as the completion routine when the command is
3919 * handed off to the SLI layer.
3920 */
3921void
James Smart2e0fef82007-06-17 19:56:36 -05003922lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003923{
James Smart21e9a0a2009-05-22 14:53:21 -04003924 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003925 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3926 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3927 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003928
James Smartd439d282010-09-29 11:18:45 -04003929 pmb->context1 = NULL;
3930 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003931 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003932
dea31012005-04-17 16:05:31 -05003933 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003934 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3935 "0260 Register NameServer error: 0x%x\n",
3936 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003937
3938out:
James Smartfa4066b2008-01-11 01:53:27 -05003939 /* decrement the node reference count held for this
3940 * callback function.
3941 */
James Smart329f9bc2007-04-25 09:53:01 -04003942 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003943 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3944 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003945 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003946
3947 /* If no other thread is using the ndlp, free it */
3948 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003949
James Smart76a95d72010-11-20 23:11:48 -05003950 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003951 /*
3952 * RegLogin failed, use loop map to make discovery
3953 * list
3954 */
3955 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003956
James Smart92d7f7b2007-06-17 19:56:38 -05003957 /* Start discovery */
3958 lpfc_disc_start(vport);
3959 return;
3960 }
3961 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003962 return;
3963 }
3964
James Smart6d368e52011-05-24 11:44:12 -04003965 if (phba->sli_rev < LPFC_SLI_REV4)
3966 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003967 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003968 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003969 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003970 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3971 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3972 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003973 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003974 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003975
James Smart2e0fef82007-06-17 19:56:36 -05003976 if (vport->port_state < LPFC_VPORT_READY) {
3977 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003978 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3979 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3980 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3981 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003982
3983 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3984 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3985 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3986
3987 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3988 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003989 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3990 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003991
3992 /* Issue SCR just before NameServer GID_FT Query */
3993 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003994 }
3995
James Smart2e0fef82007-06-17 19:56:36 -05003996 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003997 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003998 goto out;
dea31012005-04-17 16:05:31 -05003999
James Smarta0f2d3e2017-02-12 13:52:31 -08004000 /*
4001 * At this point in time we may need to wait for multiple
4002 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
4003 *
4004 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05004005 * callback function.
4006 */
James Smart329f9bc2007-04-25 09:53:01 -04004007 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05004008 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4009 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05004010 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05004011
4012 return;
4013}
4014
4015static void
James Smart2e0fef82007-06-17 19:56:36 -05004016lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004017{
James Smart2e0fef82007-06-17 19:56:36 -05004018 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4019 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05004020 struct lpfc_rport_data *rdata;
4021 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05004022 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004023
James Smart895427b2017-02-12 13:52:30 -08004024 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4025 return;
4026
dea31012005-04-17 16:05:31 -05004027 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004028 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4029 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004030 rport_ids.port_id = ndlp->nlp_DID;
4031 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004032
James Smart329f9bc2007-04-25 09:53:01 -04004033 /*
4034 * We leave our node pointer in rport->dd_data when we unregister a
4035 * FCP target port. But fc_remote_port_add zeros the space to which
4036 * rport->dd_data points. So, if we're reusing a previously
4037 * registered port, drop the reference that we took the last time we
4038 * registered the port.
4039 */
James Smart466e8402015-05-21 13:55:28 -04004040 rport = ndlp->rport;
4041 if (rport) {
4042 rdata = rport->dd_data;
4043 /* break the link before dropping the ref */
4044 ndlp->rport = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08004045 if (rdata) {
4046 if (rdata->pnode == ndlp)
4047 lpfc_nlp_put(ndlp);
4048 rdata->pnode = NULL;
4049 }
James Smart466e8402015-05-21 13:55:28 -04004050 /* drop reference for earlier registeration */
4051 put_device(&rport->dev);
4052 }
James Smart858c9f62007-06-17 19:56:39 -05004053
4054 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4055 "rport add: did:x%x flg:x%x type x%x",
4056 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4057
James Smart9589b0622011-04-16 11:03:17 -04004058 /* Don't add the remote port if unloading. */
4059 if (vport->load_flag & FC_UNLOADING)
4060 return;
4061
James Smart2e0fef82007-06-17 19:56:36 -05004062 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004063 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004064 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4065 "Warning: fc_remote_port_add failed\n");
4066 return;
4067 }
4068
4069 /* initialize static port data */
4070 rport->maxframe_size = ndlp->nlp_maxframe;
4071 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004072 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004073 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004074
4075 if (ndlp->nlp_type & NLP_FCP_TARGET)
4076 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4077 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4078 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4079
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004080 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4081 fc_remote_port_rolechg(rport, rport_ids.roles);
4082
James Smart34f5ad82012-08-03 12:35:03 -04004083 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4084 "3183 rport register x%06x, rport %p role x%x\n",
4085 ndlp->nlp_DID, rport, rport_ids.roles);
4086
James Smart071fbd3d2006-04-15 11:53:20 -04004087 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004088 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004089 ndlp->nlp_sid = rport->scsi_target_id;
4090 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004091 return;
4092}
4093
4094static void
James Smart2e0fef82007-06-17 19:56:36 -05004095lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004096{
4097 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004098 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004099 struct lpfc_hba *phba = vport->phba;
4100
4101 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4102 return;
James Smartc01f3202006-08-18 17:47:08 -04004103
James Smart2ea259e2017-02-12 13:52:27 -08004104 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004105 "rport delete: did:x%x flg:x%x type x%x",
4106 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4107
James Smart2ea259e2017-02-12 13:52:27 -08004108 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004109 "3184 rport unregister x%06x, rport %p\n",
4110 ndlp->nlp_DID, rport);
4111
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004112 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004113
4114 return;
4115}
4116
James Smartde0c5b32007-04-25 09:52:27 -04004117static void
James Smart2e0fef82007-06-17 19:56:36 -05004118lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004119{
James Smart2e0fef82007-06-17 19:56:36 -05004120 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4121
4122 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004123 switch (state) {
4124 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004125 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004126 break;
4127 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004128 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004129 break;
4130 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004131 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004132 break;
4133 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004134 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004135 break;
4136 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004137 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004138 break;
4139 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004140 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004141 break;
4142 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004143 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004144 break;
4145 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004146 if (vport->fc_npr_cnt == 0 && count == -1)
4147 vport->fc_npr_cnt = 0;
4148 else
4149 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004150 break;
4151 }
James Smart2e0fef82007-06-17 19:56:36 -05004152 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004153}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004154
James Smartde0c5b32007-04-25 09:52:27 -04004155static void
James Smart2e0fef82007-06-17 19:56:36 -05004156lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004157 int old_state, int new_state)
4158{
James Smart2e0fef82007-06-17 19:56:36 -05004159 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4160
James Smartde0c5b32007-04-25 09:52:27 -04004161 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004162 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4163 ndlp->nlp_type |= NLP_FC_NODE;
4164 }
4165 if (new_state == NLP_STE_MAPPED_NODE)
4166 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4167 if (new_state == NLP_STE_NPR_NODE)
4168 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004169
James Smarta0f2d3e2017-02-12 13:52:31 -08004170 /* FCP and NVME Transport interface */
4171 if ((old_state == NLP_STE_MAPPED_NODE ||
4172 old_state == NLP_STE_UNMAPPED_NODE)) {
4173 if (ndlp->rport) {
4174 vport->phba->nport_event_cnt++;
4175 lpfc_unregister_remote_port(ndlp);
4176 }
4177
James Smart1c5b12f2017-04-21 16:05:03 -07004178 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004179 vport->phba->nport_event_cnt++;
James Smart1c5b12f2017-04-21 16:05:03 -07004180 if (vport->phba->nvmet_support == 0)
James Smart6599e122017-06-01 21:07:00 -07004181 /* Start devloss */
James Smart1c5b12f2017-04-21 16:05:03 -07004182 lpfc_nvme_unregister_port(vport, ndlp);
James Smart6599e122017-06-01 21:07:00 -07004183 else
4184 /* NVMET has no upcall. */
4185 lpfc_nlp_put(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004186 }
James Smartde0c5b32007-04-25 09:52:27 -04004187 }
dea31012005-04-17 16:05:31 -05004188
James Smarta0f2d3e2017-02-12 13:52:31 -08004189 /* FCP and NVME Transport interfaces */
4190
James Smartde0c5b32007-04-25 09:52:27 -04004191 if (new_state == NLP_STE_MAPPED_NODE ||
4192 new_state == NLP_STE_UNMAPPED_NODE) {
James Smart92721c32017-06-01 21:06:59 -07004193 if (ndlp->nlp_fc4_type & NLP_FC4_FCP ||
4194 ndlp->nlp_DID == Fabric_DID ||
4195 ndlp->nlp_DID == NameServer_DID ||
4196 ndlp->nlp_DID == FDMI_DID) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004197 vport->phba->nport_event_cnt++;
4198 /*
4199 * Tell the fc transport about the port, if we haven't
4200 * already. If we have, and it's a scsi entity, be
4201 */
4202 lpfc_register_remote_port(vport, ndlp);
4203 }
4204 /* Notify the NVME transport of this new rport. */
James Smart09559e82017-06-15 22:56:46 -07004205 if (vport->phba->sli_rev >= LPFC_SLI_REV4 &&
4206 ndlp->nlp_fc4_type & NLP_FC4_NVME) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004207 if (vport->phba->nvmet_support == 0) {
4208 /* Register this rport with the transport.
4209 * Initiators take the NDLP ref count in
4210 * the register.
4211 */
4212 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004213 lpfc_nvme_register_port(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08004214 } else {
4215 /* Just take an NDLP ref count since the
4216 * target does not register rports.
4217 */
4218 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004219 }
4220 }
James Smartde0c5b32007-04-25 09:52:27 -04004221 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004222
James Smartea2151b2008-09-07 11:52:10 -04004223 if ((new_state == NLP_STE_MAPPED_NODE) &&
4224 (vport->stat_data_enabled)) {
4225 /*
4226 * A new target is discovered, if there is no buffer for
4227 * statistical data collection allocate buffer.
4228 */
4229 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4230 sizeof(struct lpfc_scsicmd_bkt),
4231 GFP_KERNEL);
4232
4233 if (!ndlp->lat_data)
4234 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4235 "0286 lpfc_nlp_state_cleanup failed to "
4236 "allocate statistical data buffer DID "
4237 "0x%x\n", ndlp->nlp_DID);
4238 }
James Smart858c9f62007-06-17 19:56:39 -05004239 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004240 * If the node just added to Mapped list was an FCP target,
4241 * but the remote port registration failed or assigned a target
4242 * id outside the presentable range - move the node to the
4243 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004244 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004245 if ((new_state == NLP_STE_MAPPED_NODE) &&
4246 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004247 (!ndlp->rport ||
4248 ndlp->rport->scsi_target_id == -1 ||
4249 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004250 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004251 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004252 spin_unlock_irq(shost->host_lock);
4253 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004254 }
James Smartde0c5b32007-04-25 09:52:27 -04004255}
4256
James Smart685f0bf2007-04-25 09:53:08 -04004257static char *
4258lpfc_nlp_state_name(char *buffer, size_t size, int state)
4259{
4260 static char *states[] = {
4261 [NLP_STE_UNUSED_NODE] = "UNUSED",
4262 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4263 [NLP_STE_ADISC_ISSUE] = "ADISC",
4264 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4265 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004266 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004267 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4268 [NLP_STE_MAPPED_NODE] = "MAPPED",
4269 [NLP_STE_NPR_NODE] = "NPR",
4270 };
4271
James Smart311464e2007-08-02 11:10:37 -04004272 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004273 strlcpy(buffer, states[state], size);
4274 else
4275 snprintf(buffer, size, "unknown (%d)", state);
4276 return buffer;
4277}
4278
James Smartde0c5b32007-04-25 09:52:27 -04004279void
James Smart2e0fef82007-06-17 19:56:36 -05004280lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4281 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004282{
James Smart2e0fef82007-06-17 19:56:36 -05004283 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004284 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004285 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004286
James Smarte8b62012007-08-02 11:10:09 -04004287 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4288 "0904 NPort state transition x%06x, %s -> %s\n",
4289 ndlp->nlp_DID,
4290 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4291 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004292
4293 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4294 "node statechg did:x%x old:%d ste:%d",
4295 ndlp->nlp_DID, old_state, state);
4296
James Smartde0c5b32007-04-25 09:52:27 -04004297 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004298 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004299 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004300 if (old_state == NLP_STE_UNMAPPED_NODE) {
4301 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4302 ndlp->nlp_type &= ~NLP_FC_NODE;
4303 }
4304
James Smart685f0bf2007-04-25 09:53:08 -04004305 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004306 spin_lock_irq(shost->host_lock);
4307 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4308 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004309 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004310 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004311
4312 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004313 lpfc_nlp_counters(vport, state, 1);
4314 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004315}
4316
4317void
James Smarte47c9092008-02-08 18:49:26 -05004318lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4319{
4320 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4321
4322 if (list_empty(&ndlp->nlp_listp)) {
4323 spin_lock_irq(shost->host_lock);
4324 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4325 spin_unlock_irq(shost->host_lock);
4326 }
4327}
4328
4329void
James Smart2e0fef82007-06-17 19:56:36 -05004330lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004331{
James Smart2e0fef82007-06-17 19:56:36 -05004332 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4333
James Smart0d2b6b82008-06-14 22:52:47 -04004334 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004335 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004336 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4337 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004338 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004339 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004340 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004341 NLP_STE_UNUSED_NODE);
4342}
4343
Adrian Bunk4d9db012008-02-14 23:24:02 +02004344static void
James Smarte47c9092008-02-08 18:49:26 -05004345lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4346{
James Smart0d2b6b82008-06-14 22:52:47 -04004347 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004348 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4349 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4350 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4351 NLP_STE_UNUSED_NODE);
4352}
James Smart109f6ed2008-12-04 22:39:08 -05004353/**
James Smart3621a712009-04-06 18:47:14 -04004354 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004355 * @vport: Pointer to Virtual Port object.
4356 * @ndlp: Pointer to FC node object.
4357 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004358 *
4359 * This function is always called when node object need to be initialized.
4360 * It initializes all the fields of the node object. Although the reference
4361 * to phba from @ndlp can be obtained indirectly through it's reference to
4362 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4363 * to the life-span of the @ndlp might go beyond the existence of @vport as
4364 * the final release of ndlp is determined by its reference count. And, the
4365 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004366 **/
4367static inline void
4368lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4369 uint32_t did)
4370{
4371 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4372 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
Tomas Jasek33cc5592017-03-03 13:45:48 +01004373 setup_timer(&ndlp->nlp_delayfunc, lpfc_els_retry_delay,
4374 (unsigned long)ndlp);
James Smart109f6ed2008-12-04 22:39:08 -05004375 ndlp->nlp_DID = did;
4376 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004377 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004378 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004379 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004380 kref_init(&ndlp->kref);
4381 NLP_INT_NODE_ACT(ndlp);
4382 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004383 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004384}
James Smarte47c9092008-02-08 18:49:26 -05004385
4386struct lpfc_nodelist *
4387lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4388 int state)
4389{
4390 struct lpfc_hba *phba = vport->phba;
4391 uint32_t did;
4392 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004393 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smart9d3d3402017-04-21 16:05:00 -07004394 int rpi = LPFC_RPI_ALLOC_ERROR;
James Smarte47c9092008-02-08 18:49:26 -05004395
4396 if (!ndlp)
4397 return NULL;
4398
James Smart9d3d3402017-04-21 16:05:00 -07004399 if (phba->sli_rev == LPFC_SLI_REV4) {
4400 rpi = lpfc_sli4_alloc_rpi(vport->phba);
4401 if (rpi == LPFC_RPI_ALLOC_ERROR)
4402 return NULL;
4403 }
4404
James Smarte47c9092008-02-08 18:49:26 -05004405 spin_lock_irqsave(&phba->ndlp_lock, flags);
4406 /* The ndlp should not be in memory free mode */
4407 if (NLP_CHK_FREE_REQ(ndlp)) {
4408 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4409 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4410 "0277 lpfc_enable_node: ndlp:x%p "
4411 "usgmap:x%x refcnt:%d\n",
4412 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004413 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004414 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004415 }
4416 /* The ndlp should not already be in active mode */
4417 if (NLP_CHK_NODE_ACT(ndlp)) {
4418 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4419 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4420 "0278 lpfc_enable_node: ndlp:x%p "
4421 "usgmap:x%x refcnt:%d\n",
4422 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004423 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004424 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004425 }
4426
4427 /* Keep the original DID */
4428 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004429 if (phba->sli_rev == LPFC_SLI_REV4)
4430 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004431
4432 /* re-initialize ndlp except of ndlp linked list pointer */
4433 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4434 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004435 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004436
James Smartcff261f2013-12-17 20:29:47 -05004437 if (phba->sli_rev == LPFC_SLI_REV4)
4438 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4439
James Smarte47c9092008-02-08 18:49:26 -05004440 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004441 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07004442 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04004443 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4444 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4445 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4446 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004447 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004448 ndlp->nlp_usg_map, ndlp);
4449 }
James Smart725dd392013-10-10 12:23:30 -04004450
James Smarte47c9092008-02-08 18:49:26 -05004451
4452 if (state != NLP_STE_UNUSED_NODE)
4453 lpfc_nlp_set_state(vport, ndlp, state);
4454
4455 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4456 "node enable: did:x%x",
4457 ndlp->nlp_DID, 0, 0);
4458 return ndlp;
James Smart9d3d3402017-04-21 16:05:00 -07004459
4460free_rpi:
4461 if (phba->sli_rev == LPFC_SLI_REV4)
4462 lpfc_sli4_free_rpi(vport->phba, rpi);
4463 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04004464}
4465
4466void
James Smart2e0fef82007-06-17 19:56:36 -05004467lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004468{
James Smart87af33f2007-10-27 13:37:43 -04004469 /*
James Smartfa4066b2008-01-11 01:53:27 -05004470 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004471 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004472 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4473 * until ALL other outstanding threads have completed. We check
4474 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004475 */
James Smartfa4066b2008-01-11 01:53:27 -05004476 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4477 return;
James Smart51ef4c22007-08-02 11:10:31 -04004478 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004479 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004480 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004481 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004482 }
James Smart02902172015-05-21 13:55:17 -04004483
4484 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004485 return;
dea31012005-04-17 16:05:31 -05004486}
4487
4488/*
4489 * Start / ReStart rescue timer for Discovery / RSCN handling
4490 */
4491void
James Smart2e0fef82007-06-17 19:56:36 -05004492lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004493{
James Smart2e0fef82007-06-17 19:56:36 -05004494 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4495 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004496 uint32_t tmo;
4497
James Smart2e0fef82007-06-17 19:56:36 -05004498 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004499 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004500 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4501 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004502 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004503 * FC spec states we need 3 * ratov for CT requests
4504 */
4505 tmo = ((phba->fc_ratov * 3) + 3);
4506 }
dea31012005-04-17 16:05:31 -05004507
James Smart858c9f62007-06-17 19:56:39 -05004508
4509 if (!timer_pending(&vport->fc_disctmo)) {
4510 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4511 "set disc timer: tmo:x%x state:x%x flg:x%x",
4512 tmo, vport->port_state, vport->fc_flag);
4513 }
4514
James Smart256ec0d2013-04-17 20:14:58 -04004515 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004516 spin_lock_irq(shost->host_lock);
4517 vport->fc_flag |= FC_DISC_TMO;
4518 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004519
4520 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004521 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4522 "0247 Start Discovery Timer state x%x "
4523 "Data: x%x x%lx x%x x%x\n",
4524 vport->port_state, tmo,
4525 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4526 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004527
4528 return;
4529}
4530
4531/*
4532 * Cancel rescue timer for Discovery / RSCN handling
4533 */
4534int
James Smart2e0fef82007-06-17 19:56:36 -05004535lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004536{
James Smart2e0fef82007-06-17 19:56:36 -05004537 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004538 unsigned long iflags;
4539
James Smart858c9f62007-06-17 19:56:39 -05004540 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4541 "can disc timer: state:x%x rtry:x%x flg:x%x",
4542 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4543
dea31012005-04-17 16:05:31 -05004544 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004545 if (vport->fc_flag & FC_DISC_TMO) {
4546 spin_lock_irqsave(shost->host_lock, iflags);
4547 vport->fc_flag &= ~FC_DISC_TMO;
4548 spin_unlock_irqrestore(shost->host_lock, iflags);
4549 del_timer_sync(&vport->fc_disctmo);
4550 spin_lock_irqsave(&vport->work_port_lock, iflags);
4551 vport->work_port_events &= ~WORKER_DISC_TMO;
4552 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004553 }
4554
4555 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004556 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4557 "0248 Cancel Discovery Timer state x%x "
4558 "Data: x%x x%x x%x\n",
4559 vport->port_state, vport->fc_flag,
4560 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004561 return 0;
dea31012005-04-17 16:05:31 -05004562}
4563
4564/*
4565 * Check specified ring for outstanding IOCB on the SLI queue
4566 * Return true if iocb matches the specified nport
4567 */
4568int
James Smart2e0fef82007-06-17 19:56:36 -05004569lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4570 struct lpfc_sli_ring *pring,
4571 struct lpfc_iocbq *iocb,
4572 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004573{
James Smart2e0fef82007-06-17 19:56:36 -05004574 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004575 struct lpfc_vport *vport = ndlp->vport;
4576
4577 if (iocb->vport != vport)
4578 return 0;
4579
dea31012005-04-17 16:05:31 -05004580 if (pring->ringno == LPFC_ELS_RING) {
4581 switch (icmd->ulpCommand) {
4582 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004583 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004584 return 1;
dea31012005-04-17 16:05:31 -05004585 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004586 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4587 return 1;
dea31012005-04-17 16:05:31 -05004588 case CMD_XMIT_ELS_RSP64_CX:
4589 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004590 return 1;
dea31012005-04-17 16:05:31 -05004591 }
James Smart895427b2017-02-12 13:52:30 -08004592 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004593 /* Skip match check if waiting to relogin to FCP target */
4594 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004595 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004596 return 0;
dea31012005-04-17 16:05:31 -05004597 }
4598 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004599 return 1;
dea31012005-04-17 16:05:31 -05004600 }
dea31012005-04-17 16:05:31 -05004601 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004602 return 0;
dea31012005-04-17 16:05:31 -05004603}
4604
James Smart895427b2017-02-12 13:52:30 -08004605static void
4606__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4607 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4608 struct list_head *dequeue_list)
4609{
4610 struct lpfc_iocbq *iocb, *next_iocb;
4611
4612 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4613 /* Check to see if iocb matches the nport */
4614 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4615 /* match, dequeue */
4616 list_move_tail(&iocb->list, dequeue_list);
4617 }
4618}
4619
4620static void
4621lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4622 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4623{
4624 struct lpfc_sli *psli = &phba->sli;
4625 uint32_t i;
4626
4627 spin_lock_irq(&phba->hbalock);
4628 for (i = 0; i < psli->num_rings; i++)
4629 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4630 dequeue_list);
4631 spin_unlock_irq(&phba->hbalock);
4632}
4633
4634static void
4635lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4636 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4637{
4638 struct lpfc_sli_ring *pring;
4639 struct lpfc_queue *qp = NULL;
4640
4641 spin_lock_irq(&phba->hbalock);
4642 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4643 pring = qp->pring;
4644 if (!pring)
4645 continue;
James Smart2ade92a2017-03-04 09:30:38 -08004646 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004647 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
James Smart2ade92a2017-03-04 09:30:38 -08004648 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004649 }
4650 spin_unlock_irq(&phba->hbalock);
4651}
4652
dea31012005-04-17 16:05:31 -05004653/*
4654 * Free resources / clean up outstanding I/Os
4655 * associated with nlp_rpi in the LPFC_NODELIST entry.
4656 */
4657static int
James Smart2e0fef82007-06-17 19:56:36 -05004658lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004659{
James Smart2534ba72007-04-25 09:52:20 -04004660 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004661
James Smart92d7f7b2007-06-17 19:56:38 -05004662 lpfc_fabric_abort_nport(ndlp);
4663
dea31012005-04-17 16:05:31 -05004664 /*
4665 * Everything that matches on txcmplq will be returned
4666 * by firmware with a no rpi error.
4667 */
James Smart40426292010-12-15 17:58:10 -05004668 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004669 if (phba->sli_rev != LPFC_SLI_REV4)
4670 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4671 else
4672 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004673 }
James Smart2534ba72007-04-25 09:52:20 -04004674
James Smarta257bf92009-04-06 18:48:10 -04004675 /* Cancel all the IOCBs from the completions list */
4676 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4677 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004678
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004679 return 0;
dea31012005-04-17 16:05:31 -05004680}
4681
James Smart086a3452012-08-14 14:25:21 -04004682/**
4683 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4684 * @phba: Pointer to HBA context object.
4685 * @pmb: Pointer to mailbox object.
4686 *
4687 * This function will issue an ELS LOGO command after completing
4688 * the UNREG_RPI.
4689 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004690static void
James Smart086a3452012-08-14 14:25:21 -04004691lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4692{
4693 struct lpfc_vport *vport = pmb->vport;
4694 struct lpfc_nodelist *ndlp;
4695
4696 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4697 if (!ndlp)
4698 return;
4699 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004700 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004701}
4702
dea31012005-04-17 16:05:31 -05004703/*
4704 * Free rpi associated with LPFC_NODELIST entry.
4705 * This routine is called from lpfc_freenode(), when we are removing
4706 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4707 * LOGO that completes successfully, and we are waiting to PLOGI back
4708 * to the remote NPort. In addition, it is called after we receive
4709 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4710 * we are waiting to PLOGI back to the remote NPort.
4711 */
4712int
James Smart2e0fef82007-06-17 19:56:36 -05004713lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004714{
James Smart2e0fef82007-06-17 19:56:36 -05004715 struct lpfc_hba *phba = vport->phba;
4716 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004717 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004718 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004719
James Smartf454a9a2013-09-06 12:20:07 -04004720 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4721 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4722 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4723 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4724 "3366 RPI x%x needs to be "
4725 "unregistered nlp_flag x%x "
4726 "did x%x\n",
4727 ndlp->nlp_rpi, ndlp->nlp_flag,
4728 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004729 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4730 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004731 /* SLI4 ports require the physical rpi value. */
4732 rpi = ndlp->nlp_rpi;
4733 if (phba->sli_rev == LPFC_SLI_REV4)
4734 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004735
James Smart6d368e52011-05-24 11:44:12 -04004736 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004737 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004738 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4739 mbox->context1 = ndlp;
4740 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4741 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004742 if (phba->sli_rev == LPFC_SLI_REV4 &&
4743 (!(vport->load_flag & FC_UNLOADING)) &&
4744 (bf_get(lpfc_sli_intf_if_type,
4745 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004746 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004747 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004748 mbox->context1 = lpfc_nlp_get(ndlp);
4749 mbox->mbox_cmpl =
4750 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004751 /*
4752 * accept PLOGIs after unreg_rpi_cmpl
4753 */
4754 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004755 } else
4756 mbox->mbox_cmpl =
4757 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004758 }
4759
James Smart0b727fe2007-10-27 13:37:25 -04004760 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004761 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004762 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004763 acc_plogi = 1;
4764 }
dea31012005-04-17 16:05:31 -05004765 }
dea31012005-04-17 16:05:31 -05004766 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004767
James Smart40426292010-12-15 17:58:10 -05004768 if (phba->sli_rev != LPFC_SLI_REV4)
4769 ndlp->nlp_rpi = 0;
4770 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004771 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004772 if (acc_plogi)
4773 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004774 return 1;
4775 }
James Smart7c5e5182015-05-22 10:42:43 -04004776 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004777 return 0;
4778}
4779
James Smartecfd03c2010-02-12 14:41:27 -05004780/**
4781 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4782 * @phba: pointer to lpfc hba data structure.
4783 *
4784 * This routine is invoked to unregister all the currently registered RPIs
4785 * to the HBA.
4786 **/
4787void
4788lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4789{
4790 struct lpfc_vport **vports;
4791 struct lpfc_nodelist *ndlp;
4792 struct Scsi_Host *shost;
4793 int i;
4794
4795 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004796 if (!vports) {
4797 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4798 "2884 Vport array allocation failed \n");
4799 return;
4800 }
James Smartecfd03c2010-02-12 14:41:27 -05004801 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4802 shost = lpfc_shost_from_vport(vports[i]);
4803 spin_lock_irq(shost->host_lock);
4804 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004805 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004806 /* The mempool_alloc might sleep */
4807 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004808 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004809 spin_lock_irq(shost->host_lock);
4810 }
James Smartecfd03c2010-02-12 14:41:27 -05004811 }
4812 spin_unlock_irq(shost->host_lock);
4813 }
4814 lpfc_destroy_vport_work_array(phba, vports);
4815}
4816
James Smart92d7f7b2007-06-17 19:56:38 -05004817void
4818lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4819{
4820 struct lpfc_hba *phba = vport->phba;
4821 LPFC_MBOXQ_t *mbox;
4822 int rc;
4823
James Smart5af5eee2010-10-22 11:06:38 -04004824 if (phba->sli_rev == LPFC_SLI_REV4) {
4825 lpfc_sli4_unreg_all_rpis(vport);
4826 return;
4827 }
4828
James Smart92d7f7b2007-06-17 19:56:38 -05004829 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4830 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004831 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4832 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004833 mbox->vport = vport;
4834 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004835 mbox->context1 = NULL;
4836 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004837 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004838 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004839
4840 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4841 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4842 "1836 Could not issue "
4843 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004844 }
4845}
4846
4847void
4848lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4849{
4850 struct lpfc_hba *phba = vport->phba;
4851 LPFC_MBOXQ_t *mbox;
4852 int rc;
4853
4854 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4855 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004856 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4857 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004858 mbox->vport = vport;
4859 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004860 mbox->context1 = NULL;
4861 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004862 if (rc != MBX_TIMEOUT)
4863 mempool_free(mbox, phba->mbox_mem_pool);
4864
4865 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004866 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4867 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004868 "unreg_did (default rpis) status %d\n",
4869 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004870 }
4871}
4872
dea31012005-04-17 16:05:31 -05004873/*
4874 * Free resources associated with LPFC_NODELIST entry
4875 * so it can be freed.
4876 */
4877static int
James Smart2e0fef82007-06-17 19:56:36 -05004878lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004879{
James Smart2e0fef82007-06-17 19:56:36 -05004880 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4881 struct lpfc_hba *phba = vport->phba;
4882 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004883 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004884
4885 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004886 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4887 "0900 Cleanup node for NPort x%x "
4888 "Data: x%x x%x x%x\n",
4889 ndlp->nlp_DID, ndlp->nlp_flag,
4890 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004891 if (NLP_CHK_FREE_REQ(ndlp)) {
4892 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4893 "0280 lpfc_cleanup_node: ndlp:x%p "
4894 "usgmap:x%x refcnt:%d\n",
4895 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004896 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004897 lpfc_dequeue_node(vport, ndlp);
4898 } else {
4899 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4900 "0281 lpfc_cleanup_node: ndlp:x%p "
4901 "usgmap:x%x refcnt:%d\n",
4902 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004903 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004904 lpfc_disable_node(vport, ndlp);
4905 }
dea31012005-04-17 16:05:31 -05004906
James Smart086a3452012-08-14 14:25:21 -04004907
4908 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4909
dea31012005-04-17 16:05:31 -05004910 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4911 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004912 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004913 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004914 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4915 mb->context2 = NULL;
4916 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4917 }
4918 }
James Smart33ccf8d2006-08-17 11:57:58 -04004919
James Smart2e0fef82007-06-17 19:56:36 -05004920 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004921 /* Cleanup REG_LOGIN completions which are not yet processed */
4922 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4923 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004924 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004925 (ndlp != (struct lpfc_nodelist *) mb->context2))
4926 continue;
4927
4928 mb->context2 = NULL;
4929 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4930 }
4931
dea31012005-04-17 16:05:31 -05004932 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004933 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004934 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004935 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004936 mp = (struct lpfc_dmabuf *) (mb->context1);
4937 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004938 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004939 kfree(mp);
4940 }
4941 list_del(&mb->list);
4942 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004943 /* We shall not invoke the lpfc_nlp_put to decrement
4944 * the ndlp reference count as we are in the process
4945 * of lpfc_nlp_release.
4946 */
dea31012005-04-17 16:05:31 -05004947 }
4948 }
James Smart2e0fef82007-06-17 19:56:36 -05004949 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004950
James Smarte47c9092008-02-08 18:49:26 -05004951 lpfc_els_abort(phba, ndlp);
4952
James Smart2e0fef82007-06-17 19:56:36 -05004953 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004954 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004955 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004956
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004957 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004958 del_timer_sync(&ndlp->nlp_delayfunc);
4959
James Smart0d2b6b82008-06-14 22:52:47 -04004960 list_del_init(&ndlp->els_retry_evt.evt_listp);
4961 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004962 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004963 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004964
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004965 return 0;
dea31012005-04-17 16:05:31 -05004966}
4967
4968/*
4969 * Check to see if we can free the nlp back to the freelist.
4970 * If we are in the middle of using the nlp in the discovery state
4971 * machine, defer the free till we reach the end of the state machine.
4972 */
James Smart329f9bc2007-04-25 09:53:01 -04004973static void
James Smart2e0fef82007-06-17 19:56:36 -05004974lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004975{
James Smarta8adb832007-10-27 13:37:53 -04004976 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004977 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004978 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004979 LPFC_MBOXQ_t *mbox;
4980 int rc;
dea31012005-04-17 16:05:31 -05004981
James Smart0d2b6b82008-06-14 22:52:47 -04004982 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004983 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004984 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4985 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004986 /* For this case we need to cleanup the default rpi
4987 * allocated by the firmware.
4988 */
James Smartbe6bb942015-04-07 15:07:22 -04004989 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4990 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4991 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004992 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004993 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004994 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4995 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004996 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004997 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004998 if (rc) {
4999 mempool_free(mbox, phba->mbox_mem_pool);
5000 }
5001 else {
5002 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
5003 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
5004 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04005005 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04005006 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
5007 if (rc == MBX_NOT_FINISHED) {
5008 mempool_free(mbox, phba->mbox_mem_pool);
5009 }
5010 }
5011 }
5012 }
James Smart2e0fef82007-06-17 19:56:36 -05005013 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04005014
James Smart2e0fef82007-06-17 19:56:36 -05005015 /*
James Smart466e8402015-05-21 13:55:28 -04005016 * ndlp->rport must be set to NULL before it reaches here
5017 * i.e. break rport/node link before doing lpfc_nlp_put for
5018 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05005019 */
James Smart92d7f7b2007-06-17 19:56:38 -05005020 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04005021 /*
5022 * extra lpfc_nlp_put dropped the reference of ndlp
5023 * for registered rport so need to cleanup rport
5024 */
5025 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5026 "0940 removed node x%p DID x%x "
5027 " rport not null %p\n",
5028 ndlp, ndlp->nlp_DID, ndlp->rport);
5029 rport = ndlp->rport;
5030 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04005031 rdata->pnode = NULL;
5032 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04005033 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05005034 }
dea31012005-04-17 16:05:31 -05005035}
5036
5037static int
James Smart2e0fef82007-06-17 19:56:36 -05005038lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5039 uint32_t did)
dea31012005-04-17 16:05:31 -05005040{
James Smart2e0fef82007-06-17 19:56:36 -05005041 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005042
5043 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005044 return 0;
dea31012005-04-17 16:05:31 -05005045
dea31012005-04-17 16:05:31 -05005046 /* First check for Direct match */
5047 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005048 return 1;
dea31012005-04-17 16:05:31 -05005049
5050 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005051 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005052 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005053 return 0;
dea31012005-04-17 16:05:31 -05005054 }
5055
5056 matchdid.un.word = did;
5057 ndlpdid.un.word = ndlp->nlp_DID;
5058 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5059 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5060 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005061 /* This code is supposed to match the ID
5062 * for a private loop device that is
5063 * connect to fl_port. But we need to
5064 * check that the port did not just go
5065 * from pt2pt to fabric or we could end
5066 * up matching ndlp->nlp_DID 000001 to
5067 * fabric DID 0x20101
5068 */
dea31012005-04-17 16:05:31 -05005069 if ((ndlpdid.un.b.domain == 0) &&
5070 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005071 if (ndlpdid.un.b.id &&
5072 vport->phba->fc_topology ==
5073 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005074 return 1;
dea31012005-04-17 16:05:31 -05005075 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005076 return 0;
dea31012005-04-17 16:05:31 -05005077 }
5078
5079 matchdid.un.word = ndlp->nlp_DID;
5080 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5081 (mydid.un.b.area == ndlpdid.un.b.area)) {
5082 if ((matchdid.un.b.domain == 0) &&
5083 (matchdid.un.b.area == 0)) {
5084 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005085 return 1;
dea31012005-04-17 16:05:31 -05005086 }
5087 }
5088 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005089 return 0;
dea31012005-04-17 16:05:31 -05005090}
5091
James Smart685f0bf2007-04-25 09:53:08 -04005092/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005093static struct lpfc_nodelist *
5094__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005095{
James Smart2fb9bd82006-12-02 13:33:57 -05005096 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005097 uint32_t data1;
5098
James Smart2e0fef82007-06-17 19:56:36 -05005099 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5100 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005101 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5102 ((uint32_t) ndlp->nlp_xri << 16) |
5103 ((uint32_t) ndlp->nlp_type << 8) |
5104 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005105 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5106 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005107 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005108 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005109 ndlp->nlp_flag, data1,
5110 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005111 return ndlp;
dea31012005-04-17 16:05:31 -05005112 }
5113 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005114
dea31012005-04-17 16:05:31 -05005115 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005116 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5117 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005118 return NULL;
5119}
5120
5121struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005122lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005123{
James Smart2e0fef82007-06-17 19:56:36 -05005124 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005125 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005126 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005127
James Smartfedd3b72011-02-16 12:39:24 -05005128 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005129 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005130 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005131 return ndlp;
5132}
5133
5134struct lpfc_nodelist *
5135lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5136{
5137 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5138 struct lpfc_nodelist *ndlp;
5139
5140 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005141 if (!ndlp) {
James Smart1c5b12f2017-04-21 16:05:03 -07005142 if (vport->phba->nvmet_support)
5143 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005144 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5145 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005146 return NULL;
James Smart9d3d3402017-04-21 16:05:00 -07005147 ndlp = lpfc_nlp_init(vport, did);
dea31012005-04-17 16:05:31 -05005148 if (!ndlp)
5149 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005150 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5151 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005152 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005153 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005154 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005155 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005156 if (vport->phba->nvmet_support)
5157 return NULL;
James Smarte47c9092008-02-08 18:49:26 -05005158 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5159 if (!ndlp)
5160 return NULL;
5161 spin_lock_irq(shost->host_lock);
5162 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5163 spin_unlock_irq(shost->host_lock);
5164 return ndlp;
dea31012005-04-17 16:05:31 -05005165 }
James Smarte47c9092008-02-08 18:49:26 -05005166
James Smart1c5b12f2017-04-21 16:05:03 -07005167 /* The NVME Target does not want to actively manage an rport.
5168 * The goal is to allow the target to reset its state and clear
5169 * pending IO in preparation for the initiator to recover.
5170 */
James Smart58da1ff2008-04-07 10:15:56 -04005171 if ((vport->fc_flag & FC_RSCN_MODE) &&
5172 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005173 if (lpfc_rscn_payload_check(vport, did)) {
James Smart1c5b12f2017-04-21 16:05:03 -07005174
5175 /* Since this node is marked for discovery,
5176 * delay timeout is not needed.
5177 */
5178 lpfc_cancel_retry_delay_tmo(vport, ndlp);
5179
5180 /* NVME Target mode waits until rport is known to be
5181 * impacted by the RSCN before it transitions. No
5182 * active management - just go to NPR provided the
5183 * node had a valid login.
5184 */
5185 if (vport->phba->nvmet_support)
5186 return ndlp;
5187
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005188 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005189 * we don't need to try to discover it again.
5190 */
5191 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5192 return NULL;
5193
James Smarta257bf92009-04-06 18:48:10 -04005194 spin_lock_irq(shost->host_lock);
5195 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5196 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005197 } else
dea31012005-04-17 16:05:31 -05005198 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005199 } else {
James Smart1c5b12f2017-04-21 16:05:03 -07005200 /* If the initiator received a PLOGI from this NPort or if the
5201 * initiator is already in the process of discovery on it,
5202 * there's no need to try to discover it again.
James Smart87af33f2007-10-27 13:37:43 -04005203 */
James Smart685f0bf2007-04-25 09:53:08 -04005204 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005205 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
James Smart1c5b12f2017-04-21 16:05:03 -07005206 (!vport->phba->nvmet_support &&
5207 ndlp->nlp_flag & NLP_RCV_PLOGI))
dea31012005-04-17 16:05:31 -05005208 return NULL;
James Smart1c5b12f2017-04-21 16:05:03 -07005209
James Smart8c258642017-02-12 13:52:36 -08005210 if (vport->phba->nvmet_support)
5211 return ndlp;
James Smart1c5b12f2017-04-21 16:05:03 -07005212
5213 /* Moving to NPR state clears unsolicited flags and
5214 * allows for rediscovery
5215 */
5216 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5217
James Smart2e0fef82007-06-17 19:56:36 -05005218 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005219 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005220 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005221 }
5222 return ndlp;
5223}
5224
5225/* Build a list of nodes to discover based on the loopmap */
5226void
James Smart2e0fef82007-06-17 19:56:36 -05005227lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005228{
James Smart2e0fef82007-06-17 19:56:36 -05005229 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005230 int j;
5231 uint32_t alpa, index;
5232
James Smart2e0fef82007-06-17 19:56:36 -05005233 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005234 return;
James Smart2e0fef82007-06-17 19:56:36 -05005235
James Smart76a95d72010-11-20 23:11:48 -05005236 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005237 return;
dea31012005-04-17 16:05:31 -05005238
5239 /* Check for loop map present or not */
5240 if (phba->alpa_map[0]) {
5241 for (j = 1; j <= phba->alpa_map[0]; j++) {
5242 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005243 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005244 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005245 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005246 }
5247 } else {
5248 /* No alpamap, so try all alpa's */
5249 for (j = 0; j < FC_MAXLOOP; j++) {
5250 /* If cfg_scan_down is set, start from highest
5251 * ALPA (0xef) to lowest (0x1).
5252 */
James Smart3de2a652007-08-02 11:09:59 -04005253 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005254 index = j;
5255 else
5256 index = FC_MAXLOOP - j - 1;
5257 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005258 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005259 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005260 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005261 }
5262 }
5263 return;
5264}
5265
James Smart895427b2017-02-12 13:52:30 -08005266/* SLI3 only */
dea31012005-04-17 16:05:31 -05005267void
James Smart2e0fef82007-06-17 19:56:36 -05005268lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005269{
dea31012005-04-17 16:05:31 -05005270 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005271 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005272 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5273 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005274 int rc;
5275
James Smart92d7f7b2007-06-17 19:56:38 -05005276 /*
5277 * if it's not a physical port or if we already send
5278 * clear_la then don't send it.
5279 */
5280 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005281 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5282 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005283 return;
5284
James Smart2e0fef82007-06-17 19:56:36 -05005285 /* Link up discovery */
5286 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5287 phba->link_state = LPFC_CLEAR_LA;
5288 lpfc_clear_la(phba, mbox);
5289 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5290 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005291 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005292 if (rc == MBX_NOT_FINISHED) {
5293 mempool_free(mbox, phba->mbox_mem_pool);
5294 lpfc_disc_flush_list(vport);
5295 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5296 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005297 phba->link_state = LPFC_HBA_ERROR;
5298 }
5299 }
5300}
5301
5302/* Reg_vpi to tell firmware to resume normal operations */
5303void
5304lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5305{
5306 LPFC_MBOXQ_t *regvpimbox;
5307
5308 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5309 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005310 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005311 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5312 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005313 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005314 == MBX_NOT_FINISHED) {
5315 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005316 }
5317 }
5318}
5319
5320/* Start Link up / RSCN discovery on NPR nodes */
5321void
5322lpfc_disc_start(struct lpfc_vport *vport)
5323{
5324 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5325 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005326 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005327 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005328
James Smarte74c03c2013-04-17 20:15:19 -04005329 if (!lpfc_is_link_up(phba)) {
5330 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5331 "3315 Link is not up %x\n",
5332 phba->link_state);
dea31012005-04-17 16:05:31 -05005333 return;
James Smarte74c03c2013-04-17 20:15:19 -04005334 }
James Smart2e0fef82007-06-17 19:56:36 -05005335
5336 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005337 clear_la_pending = 1;
5338 else
5339 clear_la_pending = 0;
5340
James Smart2e0fef82007-06-17 19:56:36 -05005341 if (vport->port_state < LPFC_VPORT_READY)
5342 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005343
James Smart2e0fef82007-06-17 19:56:36 -05005344 lpfc_set_disctmo(vport);
5345
James Smart2e0fef82007-06-17 19:56:36 -05005346 vport->fc_prevDID = vport->fc_myDID;
5347 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005348
5349 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005350 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5351 "0202 Start Discovery hba state x%x "
5352 "Data: x%x x%x x%x\n",
5353 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5354 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005355
5356 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005357 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005358
5359 if (num_sent)
5360 return;
5361
James Smart6fa139f2013-04-17 20:20:00 -04005362 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005363 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005364 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005365 !(vport->fc_flag & FC_RSCN_MODE) &&
5366 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005367 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005368 lpfc_issue_reg_vpi(phba, vport);
5369 return;
5370 }
James Smart2e0fef82007-06-17 19:56:36 -05005371
James Smart92d7f7b2007-06-17 19:56:38 -05005372 /*
5373 * For SLI2, we need to set port_state to READY and continue
5374 * discovery.
5375 */
5376 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5377 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005378 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005379
5380 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005381 vport->num_disc_nodes = 0;
5382 /* go thru NPR nodes and issue ELS PLOGIs */
5383 if (vport->fc_npr_cnt)
5384 lpfc_els_disc_plogi(vport);
5385
5386 if (!vport->num_disc_nodes) {
5387 spin_lock_irq(shost->host_lock);
5388 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5389 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005390 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005391 }
5392 }
James Smart92d7f7b2007-06-17 19:56:38 -05005393 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005394 } else {
5395 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005396 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005397
5398 if (num_sent)
5399 return;
5400
James Smart2e0fef82007-06-17 19:56:36 -05005401 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005402 /* Check to see if more RSCNs came in while we
5403 * were processing this one.
5404 */
James Smart2e0fef82007-06-17 19:56:36 -05005405 if ((vport->fc_rscn_id_cnt == 0) &&
5406 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5407 spin_lock_irq(shost->host_lock);
5408 vport->fc_flag &= ~FC_RSCN_MODE;
5409 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005410 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005411 } else
James Smart2e0fef82007-06-17 19:56:36 -05005412 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005413 }
5414 }
5415 return;
5416}
5417
5418/*
5419 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5420 * ring the match the sppecified nodelist.
5421 */
5422static void
James Smart2e0fef82007-06-17 19:56:36 -05005423lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005424{
James Smart2534ba72007-04-25 09:52:20 -04005425 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005426 struct lpfc_sli *psli;
5427 IOCB_t *icmd;
5428 struct lpfc_iocbq *iocb, *next_iocb;
5429 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005430
5431 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005432 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005433
5434 /* Error matching iocb on txq or txcmplq
5435 * First check the txq.
5436 */
James Smart2e0fef82007-06-17 19:56:36 -05005437 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005438 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5439 if (iocb->context1 != ndlp) {
5440 continue;
5441 }
5442 icmd = &iocb->iocb;
5443 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5444 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5445
James Smart2534ba72007-04-25 09:52:20 -04005446 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005447 }
5448 }
5449
5450 /* Next check the txcmplq */
5451 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5452 if (iocb->context1 != ndlp) {
5453 continue;
5454 }
5455 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005456 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5457 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005458 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005459 }
5460 }
James Smart2e0fef82007-06-17 19:56:36 -05005461 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005462
James Smarta257bf92009-04-06 18:48:10 -04005463 /* Cancel all the IOCBs from the completions list */
5464 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5465 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005466}
5467
Adrian Bunka6ababd2007-11-05 18:07:33 +01005468static void
James Smart2e0fef82007-06-17 19:56:36 -05005469lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005470{
5471 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005472 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005473
James Smart2e0fef82007-06-17 19:56:36 -05005474 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5475 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005476 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005477 if (!NLP_CHK_NODE_ACT(ndlp))
5478 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005479 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5480 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5481 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005482 }
dea31012005-04-17 16:05:31 -05005483 }
5484 }
dea31012005-04-17 16:05:31 -05005485}
5486
James Smart92d7f7b2007-06-17 19:56:38 -05005487void
5488lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5489{
5490 lpfc_els_flush_rscn(vport);
5491 lpfc_els_flush_cmd(vport);
5492 lpfc_disc_flush_list(vport);
5493}
5494
dea31012005-04-17 16:05:31 -05005495/*****************************************************************************/
5496/*
5497 * NAME: lpfc_disc_timeout
5498 *
5499 * FUNCTION: Fibre Channel driver discovery timeout routine.
5500 *
5501 * EXECUTION ENVIRONMENT: interrupt only
5502 *
5503 * CALLED FROM:
5504 * Timer function
5505 *
5506 * RETURNS:
5507 * none
5508 */
5509/*****************************************************************************/
5510void
5511lpfc_disc_timeout(unsigned long ptr)
5512{
James Smart2e0fef82007-06-17 19:56:36 -05005513 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5514 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005515 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005516 unsigned long flags = 0;
5517
5518 if (unlikely(!phba))
5519 return;
5520
James Smart5e9d9b82008-06-14 22:52:53 -04005521 spin_lock_irqsave(&vport->work_port_lock, flags);
5522 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5523 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005524 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005525 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005526
James Smart5e9d9b82008-06-14 22:52:53 -04005527 if (!tmo_posted)
5528 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005529 return;
5530}
5531
5532static void
James Smart2e0fef82007-06-17 19:56:36 -05005533lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005534{
James Smart2e0fef82007-06-17 19:56:36 -05005535 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5536 struct lpfc_hba *phba = vport->phba;
5537 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005538 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005539 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005540 int rc, clrlaerr = 0;
5541
James Smart2e0fef82007-06-17 19:56:36 -05005542 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005543 return;
5544
James Smart2e0fef82007-06-17 19:56:36 -05005545 spin_lock_irq(shost->host_lock);
5546 vport->fc_flag &= ~FC_DISC_TMO;
5547 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005548
James Smart858c9f62007-06-17 19:56:39 -05005549 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5550 "disc timeout: state:x%x rtry:x%x flg:x%x",
5551 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5552
James Smart2e0fef82007-06-17 19:56:36 -05005553 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005554
5555 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005556 /*
5557 * port_state is identically LPFC_LOCAL_CFG_LINK while
5558 * waiting for FAN timeout
5559 */
James Smarte8b62012007-08-02 11:10:09 -04005560 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5561 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005562
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005563 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005564 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005565 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005566 if (!NLP_CHK_NODE_ACT(ndlp))
5567 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005568 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5569 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005570 if (ndlp->nlp_type & NLP_FABRIC) {
5571 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005572 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005573
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005574 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005575 /* Fail outstanding IO now since device
5576 * is marked for PLOGI.
5577 */
James Smart2e0fef82007-06-17 19:56:36 -05005578 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005579 }
5580 }
James Smart92d7f7b2007-06-17 19:56:38 -05005581 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005582 if (phba->sli_rev <= LPFC_SLI_REV3)
5583 lpfc_initial_flogi(vport);
5584 else
5585 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005586 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005587 }
dea31012005-04-17 16:05:31 -05005588 break;
5589
James Smart92d7f7b2007-06-17 19:56:38 -05005590 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005591 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005592 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005593 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005594 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5595 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005596 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005597
5598 /* Assume no Fabric and go on with discovery.
5599 * Check for outstanding ELS FLOGI to abort.
5600 */
5601
5602 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005603 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005604
5605 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005606 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005607 break;
5608
5609 case LPFC_FABRIC_CFG_LINK:
5610 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5611 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005612 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5613 "0223 Timeout while waiting for "
5614 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005615 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005616 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005617 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005618 lpfc_els_abort(phba, ndlp);
5619
5620 /* ReStart discovery */
5621 goto restart_disc;
dea31012005-04-17 16:05:31 -05005622
5623 case LPFC_NS_QRY:
5624 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005625 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5626 "0224 NameServer Query timeout "
5627 "Data: x%x x%x\n",
5628 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005629
James Smart92d7f7b2007-06-17 19:56:38 -05005630 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5631 /* Try it one more time */
5632 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005633 vport->gidft_inp = 0;
5634 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005635 if (rc == 0)
5636 break;
dea31012005-04-17 16:05:31 -05005637 }
James Smart92d7f7b2007-06-17 19:56:38 -05005638 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005639
James Smart87af33f2007-10-27 13:37:43 -04005640restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005641 /*
5642 * Discovery is over.
5643 * set port_state to PORT_READY if SLI2.
5644 * cmpl_reg_vpi will set port_state to READY for SLI3.
5645 */
James Smart3772a992009-05-22 14:50:54 -04005646 if (phba->sli_rev < LPFC_SLI_REV4) {
5647 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5648 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005649 else {
James Smart3772a992009-05-22 14:50:54 -04005650 lpfc_issue_clear_la(phba, vport);
5651 vport->port_state = LPFC_VPORT_READY;
5652 }
dea31012005-04-17 16:05:31 -05005653 }
5654
5655 /* Setup and issue mailbox INITIALIZE LINK command */
5656 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5657 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005658 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5659 "0206 Device Discovery "
5660 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005661 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005662 break;
5663 }
5664
5665 lpfc_linkdown(phba);
5666 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5667 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005668 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005669 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005670 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005671 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005672 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005673 if (rc == MBX_NOT_FINISHED)
5674 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5675
5676 break;
5677
5678 case LPFC_DISC_AUTH:
5679 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005680 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5681 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005682 lpfc_disc_flush_list(vport);
5683
James Smart92d7f7b2007-06-17 19:56:38 -05005684 /*
5685 * set port_state to PORT_READY if SLI2.
5686 * cmpl_reg_vpi will set port_state to READY for SLI3.
5687 */
James Smart3772a992009-05-22 14:50:54 -04005688 if (phba->sli_rev < LPFC_SLI_REV4) {
5689 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5690 lpfc_issue_reg_vpi(phba, vport);
5691 else { /* NPIV Not enabled */
5692 lpfc_issue_clear_la(phba, vport);
5693 vport->port_state = LPFC_VPORT_READY;
5694 }
dea31012005-04-17 16:05:31 -05005695 }
5696 break;
5697
James Smart2e0fef82007-06-17 19:56:36 -05005698 case LPFC_VPORT_READY:
5699 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005700 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5701 "0231 RSCN timeout Data: x%x "
5702 "x%x\n",
5703 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005704
5705 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005706 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005707
James Smart2e0fef82007-06-17 19:56:36 -05005708 lpfc_els_flush_rscn(vport);
5709 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005710 }
5711 break;
James Smart2e0fef82007-06-17 19:56:36 -05005712
James Smart92d7f7b2007-06-17 19:56:38 -05005713 default:
James Smarte8b62012007-08-02 11:10:09 -04005714 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005715 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005716 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005717 break;
5718 }
5719
5720 switch (phba->link_state) {
5721 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005722 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005723 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5724 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005725 clrlaerr = 1;
5726 break;
5727
James Smart09372822008-01-11 01:52:54 -05005728 case LPFC_LINK_UP:
5729 lpfc_issue_clear_la(phba, vport);
5730 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005731 case LPFC_LINK_UNKNOWN:
5732 case LPFC_WARM_START:
5733 case LPFC_INIT_START:
5734 case LPFC_INIT_MBX_CMDS:
5735 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005736 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005737 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5738 "0230 Unexpected timeout, hba link "
5739 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005740 clrlaerr = 1;
5741 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005742
5743 case LPFC_HBA_READY:
5744 break;
dea31012005-04-17 16:05:31 -05005745 }
5746
5747 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005748 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005749 if (phba->sli_rev != LPFC_SLI_REV4) {
5750 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5751 ~LPFC_STOP_IOCB_EVENT;
5752 psli->sli3_ring[LPFC_FCP_RING].flag &=
5753 ~LPFC_STOP_IOCB_EVENT;
5754 }
James Smart2e0fef82007-06-17 19:56:36 -05005755 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005756 }
dea31012005-04-17 16:05:31 -05005757 return;
5758}
5759
dea31012005-04-17 16:05:31 -05005760/*
5761 * This routine handles processing a NameServer REG_LOGIN mailbox
5762 * command upon completion. It is setup in the LPFC_MBOXQ
5763 * as the completion routine when the command is
5764 * handed off to the SLI layer.
5765 */
5766void
James Smart2e0fef82007-06-17 19:56:36 -05005767lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005768{
James Smart04c68492009-05-22 14:52:52 -04005769 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005770 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5771 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5772 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005773
5774 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005775 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005776
James Smart6d368e52011-05-24 11:44:12 -04005777 if (phba->sli_rev < LPFC_SLI_REV4)
5778 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005779 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005780 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005781 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005782 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5783 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5784 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005785 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005786 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005787 /*
5788 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005789 * 0xfffffa (FDMI well known port).
5790 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5791 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005792 */
James Smart4258e982015-12-16 18:11:58 -05005793 if (vport->port_type == LPFC_PHYSICAL_PORT)
5794 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005795 else
James Smart4258e982015-12-16 18:11:58 -05005796 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5797
dea31012005-04-17 16:05:31 -05005798
James Smartfa4066b2008-01-11 01:53:27 -05005799 /* decrement the node reference count held for this callback
5800 * function.
5801 */
James Smart329f9bc2007-04-25 09:53:01 -04005802 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005803 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5804 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005805 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005806
5807 return;
5808}
5809
James Smart685f0bf2007-04-25 09:53:08 -04005810static int
5811lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5812{
5813 uint16_t *rpi = param;
5814
James Smarteff4a012012-01-18 16:25:25 -05005815 /* check for active node */
5816 if (!NLP_CHK_NODE_ACT(ndlp))
5817 return 0;
5818
James Smart685f0bf2007-04-25 09:53:08 -04005819 return ndlp->nlp_rpi == *rpi;
5820}
5821
5822static int
5823lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5824{
5825 return memcmp(&ndlp->nlp_portname, param,
5826 sizeof(ndlp->nlp_portname)) == 0;
5827}
5828
Adrian Bunka6ababd2007-11-05 18:07:33 +01005829static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005830__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005831{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005832 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005833
James Smart2e0fef82007-06-17 19:56:36 -05005834 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005835 if (filter(ndlp, param)) {
5836 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5837 "3185 FIND node filter %p DID "
5838 "Data: x%p x%x x%x\n",
5839 filter, ndlp, ndlp->nlp_DID,
5840 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005841 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005842 }
James Smart685f0bf2007-04-25 09:53:08 -04005843 }
James Smart34f5ad82012-08-03 12:35:03 -04005844 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5845 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005846 return NULL;
dea31012005-04-17 16:05:31 -05005847}
5848
James Smart685f0bf2007-04-25 09:53:08 -04005849/*
5850 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005851 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005852 */
5853struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005854__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005855{
James Smart2e0fef82007-06-17 19:56:36 -05005856 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005857}
5858
James Smart488d1462006-03-07 15:02:37 -05005859/*
James Smart685f0bf2007-04-25 09:53:08 -04005860 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005861 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005862 */
5863struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005864lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005865{
James Smart2e0fef82007-06-17 19:56:36 -05005866 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005867 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005868
James Smart2e0fef82007-06-17 19:56:36 -05005869 spin_lock_irq(shost->host_lock);
5870 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5871 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005872 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005873}
5874
James Smartcb69f7d2011-12-13 13:21:57 -05005875/*
5876 * This routine looks up the ndlp lists for the given RPI. If the rpi
5877 * is found, the routine returns the node element list pointer else
5878 * return NULL.
5879 */
5880struct lpfc_nodelist *
5881lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5882{
5883 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5884 struct lpfc_nodelist *ndlp;
5885
5886 spin_lock_irq(shost->host_lock);
5887 ndlp = __lpfc_findnode_rpi(vport, rpi);
5888 spin_unlock_irq(shost->host_lock);
5889 return ndlp;
5890}
5891
5892/**
5893 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5894 * @phba: pointer to lpfc hba data structure.
5895 * @vpi: the physical host virtual N_Port identifier.
5896 *
5897 * This routine finds a vport on a HBA (referred by @phba) through a
5898 * @vpi. The function walks the HBA's vport list and returns the address
5899 * of the vport with the matching @vpi.
5900 *
5901 * Return code
5902 * NULL - No vport with the matching @vpi found
5903 * Otherwise - Address to the vport with the matching @vpi.
5904 **/
5905struct lpfc_vport *
5906lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5907{
5908 struct lpfc_vport *vport;
5909 unsigned long flags;
5910 int i = 0;
5911
5912 /* The physical ports are always vpi 0 - translate is unnecessary. */
5913 if (vpi > 0) {
5914 /*
5915 * Translate the physical vpi to the logical vpi. The
5916 * vport stores the logical vpi.
5917 */
5918 for (i = 0; i < phba->max_vpi; i++) {
5919 if (vpi == phba->vpi_ids[i])
5920 break;
5921 }
5922
5923 if (i >= phba->max_vpi) {
5924 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5925 "2936 Could not find Vport mapped "
5926 "to vpi %d\n", vpi);
5927 return NULL;
5928 }
5929 }
5930
5931 spin_lock_irqsave(&phba->hbalock, flags);
5932 list_for_each_entry(vport, &phba->port_list, listentry) {
5933 if (vport->vpi == i) {
5934 spin_unlock_irqrestore(&phba->hbalock, flags);
5935 return vport;
5936 }
5937 }
5938 spin_unlock_irqrestore(&phba->hbalock, flags);
5939 return NULL;
5940}
5941
James Smart9d3d3402017-04-21 16:05:00 -07005942struct lpfc_nodelist *
5943lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005944{
James Smart9d3d3402017-04-21 16:05:00 -07005945 struct lpfc_nodelist *ndlp;
5946 int rpi = LPFC_RPI_ALLOC_ERROR;
5947
5948 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
5949 rpi = lpfc_sli4_alloc_rpi(vport->phba);
5950 if (rpi == LPFC_RPI_ALLOC_ERROR)
5951 return NULL;
5952 }
5953
5954 ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
5955 if (!ndlp) {
5956 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5957 lpfc_sli4_free_rpi(vport->phba, rpi);
5958 return NULL;
5959 }
5960
dea31012005-04-17 16:05:31 -05005961 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005962
5963 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005964 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005965 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07005966 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04005967 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5968 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5969 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5970 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005971 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005972 ndlp->nlp_usg_map, ndlp);
5973
James Smartcff261f2013-12-17 20:29:47 -05005974 ndlp->active_rrqs_xri_bitmap =
5975 mempool_alloc(vport->phba->active_rrq_pool,
5976 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005977 if (ndlp->active_rrqs_xri_bitmap)
5978 memset(ndlp->active_rrqs_xri_bitmap, 0,
5979 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005980 }
5981
James Smart725dd392013-10-10 12:23:30 -04005982
James Smart858c9f62007-06-17 19:56:39 -05005983
5984 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5985 "node init: did:x%x",
5986 ndlp->nlp_DID, 0, 0);
5987
James Smart9d3d3402017-04-21 16:05:00 -07005988 return ndlp;
dea31012005-04-17 16:05:31 -05005989}
James Smart329f9bc2007-04-25 09:53:01 -04005990
James Smart98c9ea52007-10-27 13:37:33 -04005991/* This routine releases all resources associated with a specifc NPort's ndlp
5992 * and mempool_free's the nodelist.
5993 */
James Smart311464e2007-08-02 11:10:37 -04005994static void
James Smart329f9bc2007-04-25 09:53:01 -04005995lpfc_nlp_release(struct kref *kref)
5996{
James Smarte47c9092008-02-08 18:49:26 -05005997 struct lpfc_hba *phba;
5998 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005999 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
6000 kref);
James Smart858c9f62007-06-17 19:56:39 -05006001
6002 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6003 "node release: did:x%x flg:x%x type:x%x",
6004 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
6005
James Smarte47c9092008-02-08 18:49:26 -05006006 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04006007 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04006008 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04006009 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006010 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006011
6012 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05006013 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05006014
6015 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04006016 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006017 spin_lock_irqsave(&phba->ndlp_lock, flags);
6018 NLP_CLR_NODE_ACT(ndlp);
6019 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05006020 if (phba->sli_rev == LPFC_SLI_REV4)
6021 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05006022
6023 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04006024 if (NLP_CHK_FREE_REQ(ndlp)) {
6025 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05006026 if (phba->sli_rev == LPFC_SLI_REV4)
6027 mempool_free(ndlp->active_rrqs_xri_bitmap,
6028 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04006029 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04006030 }
James Smart329f9bc2007-04-25 09:53:01 -04006031}
6032
James Smart98c9ea52007-10-27 13:37:33 -04006033/* This routine bumps the reference count for a ndlp structure to ensure
6034 * that one discovery thread won't free a ndlp while another discovery thread
6035 * is using it.
6036 */
James Smart329f9bc2007-04-25 09:53:01 -04006037struct lpfc_nodelist *
6038lpfc_nlp_get(struct lpfc_nodelist *ndlp)
6039{
James Smarte47c9092008-02-08 18:49:26 -05006040 struct lpfc_hba *phba;
6041 unsigned long flags;
6042
James Smart98c9ea52007-10-27 13:37:33 -04006043 if (ndlp) {
6044 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6045 "node get: did:x%x flg:x%x refcnt:x%x",
6046 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006047 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006048 /* The check of ndlp usage to prevent incrementing the
6049 * ndlp reference count that is in the process of being
6050 * released.
6051 */
James Smarta257bf92009-04-06 18:48:10 -04006052 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006053 spin_lock_irqsave(&phba->ndlp_lock, flags);
6054 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6055 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6056 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6057 "0276 lpfc_nlp_get: ndlp:x%p "
6058 "usgmap:x%x refcnt:%d\n",
6059 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006060 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006061 return NULL;
6062 } else
6063 kref_get(&ndlp->kref);
6064 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04006065 }
James Smart329f9bc2007-04-25 09:53:01 -04006066 return ndlp;
6067}
6068
James Smart98c9ea52007-10-27 13:37:33 -04006069/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05006070 * count goes to 0, this indicates the the associated nodelist should be
6071 * freed. Returning 1 indicates the ndlp resource has been released; on the
6072 * other hand, returning 0 indicates the ndlp resource has not been released
6073 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006074 */
James Smart329f9bc2007-04-25 09:53:01 -04006075int
6076lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6077{
James Smarte47c9092008-02-08 18:49:26 -05006078 struct lpfc_hba *phba;
6079 unsigned long flags;
6080
6081 if (!ndlp)
6082 return 1;
6083
6084 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6085 "node put: did:x%x flg:x%x refcnt:x%x",
6086 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006087 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006088 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006089 spin_lock_irqsave(&phba->ndlp_lock, flags);
6090 /* Check the ndlp memory free acknowledge flag to avoid the
6091 * possible race condition that kref_put got invoked again
6092 * after previous one has done ndlp memory free.
6093 */
6094 if (NLP_CHK_FREE_ACK(ndlp)) {
6095 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6096 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6097 "0274 lpfc_nlp_put: ndlp:x%p "
6098 "usgmap:x%x refcnt:%d\n",
6099 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006100 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006101 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006102 }
James Smarte47c9092008-02-08 18:49:26 -05006103 /* Check the ndlp inactivate log flag to avoid the possible
6104 * race condition that kref_put got invoked again after ndlp
6105 * is already in inactivating state.
6106 */
6107 if (NLP_CHK_IACT_REQ(ndlp)) {
6108 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6109 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6110 "0275 lpfc_nlp_put: ndlp:x%p "
6111 "usgmap:x%x refcnt:%d\n",
6112 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006113 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006114 return 1;
6115 }
6116 /* For last put, mark the ndlp usage flags to make sure no
6117 * other kref_get and kref_put on the same ndlp shall get
6118 * in between the process when the final kref_put has been
6119 * invoked on this ndlp.
6120 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006121 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006122 /* Indicate ndlp is put to inactive state. */
6123 NLP_SET_IACT_REQ(ndlp);
6124 /* Acknowledge ndlp memory free has been seen. */
6125 if (NLP_CHK_FREE_REQ(ndlp))
6126 NLP_SET_FREE_ACK(ndlp);
6127 }
6128 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6129 /* Note, the kref_put returns 1 when decrementing a reference
6130 * count that was 1, it invokes the release callback function,
6131 * but it still left the reference count as 1 (not actually
6132 * performs the last decrementation). Otherwise, it actually
6133 * decrements the reference count and returns 0.
6134 */
6135 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006136}
James Smart98c9ea52007-10-27 13:37:33 -04006137
6138/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006139 * by any other discovery thread. This routine returns 1 if the
6140 * ndlp has been freed. A return value of 0 indicates the ndlp is
6141 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006142 */
6143int
6144lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6145{
6146 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6147 "node not used: did:x%x flg:x%x refcnt:x%x",
6148 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006149 kref_read(&ndlp->kref));
6150 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006151 if (lpfc_nlp_put(ndlp))
6152 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006153 return 0;
6154}
James Smart6fb120a2009-05-22 14:52:59 -04006155
6156/**
6157 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6158 * @phba: Pointer to hba context object.
6159 *
6160 * This function iterate through all FC nodes associated
6161 * will all vports to check if there is any node with
6162 * fc_rports associated with it. If there is an fc_rport
6163 * associated with the node, then the node is either in
6164 * discovered state or its devloss_timer is pending.
6165 */
6166static int
6167lpfc_fcf_inuse(struct lpfc_hba *phba)
6168{
6169 struct lpfc_vport **vports;
6170 int i, ret = 0;
6171 struct lpfc_nodelist *ndlp;
6172 struct Scsi_Host *shost;
6173
6174 vports = lpfc_create_vport_work_array(phba);
6175
James Smart63e801c2010-11-20 23:14:19 -05006176 /* If driver cannot allocate memory, indicate fcf is in use */
6177 if (!vports)
6178 return 1;
6179
James Smart6fb120a2009-05-22 14:52:59 -04006180 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6181 shost = lpfc_shost_from_vport(vports[i]);
6182 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006183 /*
6184 * IF the CVL_RCVD bit is not set then we have sent the
6185 * flogi.
6186 * If dev_loss fires while we are waiting we do not want to
6187 * unreg the fcf.
6188 */
6189 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6190 spin_unlock_irq(shost->host_lock);
6191 ret = 1;
6192 goto out;
6193 }
James Smart6fb120a2009-05-22 14:52:59 -04006194 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6195 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6196 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6197 ret = 1;
6198 spin_unlock_irq(shost->host_lock);
6199 goto out;
James Smart80c17842012-03-01 22:35:45 -05006200 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6201 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006202 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006203 "2624 RPI %x DID %x flag %x "
6204 "still logged in\n",
6205 ndlp->nlp_rpi, ndlp->nlp_DID,
6206 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006207 }
6208 }
6209 spin_unlock_irq(shost->host_lock);
6210 }
6211out:
6212 lpfc_destroy_vport_work_array(phba, vports);
6213 return ret;
6214}
6215
6216/**
6217 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6218 * @phba: Pointer to hba context object.
6219 * @mboxq: Pointer to mailbox object.
6220 *
6221 * This function frees memory associated with the mailbox command.
6222 */
James Smart1b511972011-12-13 13:23:09 -05006223void
James Smart6fb120a2009-05-22 14:52:59 -04006224lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6225{
6226 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006227 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006228
6229 if (mboxq->u.mb.mbxStatus) {
6230 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6231 "2555 UNREG_VFI mbxStatus error x%x "
6232 "HBA state x%x\n",
6233 mboxq->u.mb.mbxStatus, vport->port_state);
6234 }
James Smart38b92ef2010-08-04 16:11:39 -04006235 spin_lock_irq(shost->host_lock);
6236 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6237 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006238 mempool_free(mboxq, phba->mbox_mem_pool);
6239 return;
6240}
6241
6242/**
6243 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6244 * @phba: Pointer to hba context object.
6245 * @mboxq: Pointer to mailbox object.
6246 *
6247 * This function frees memory associated with the mailbox command.
6248 */
6249static void
6250lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6251{
6252 struct lpfc_vport *vport = mboxq->vport;
6253
6254 if (mboxq->u.mb.mbxStatus) {
6255 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6256 "2550 UNREG_FCFI mbxStatus error x%x "
6257 "HBA state x%x\n",
6258 mboxq->u.mb.mbxStatus, vport->port_state);
6259 }
6260 mempool_free(mboxq, phba->mbox_mem_pool);
6261 return;
6262}
6263
6264/**
James Smartecfd03c2010-02-12 14:41:27 -05006265 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006266 * @phba: Pointer to hba context object.
6267 *
James Smartecfd03c2010-02-12 14:41:27 -05006268 * This function prepare the HBA for unregistering the currently registered
6269 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6270 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006271 */
James Smartecfd03c2010-02-12 14:41:27 -05006272int
6273lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006274{
James Smart6fb120a2009-05-22 14:52:59 -04006275 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006276 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006277 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006278 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006279
James Smartecfd03c2010-02-12 14:41:27 -05006280 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006281 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006282 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006283
James Smart4d9ab992009-10-02 15:16:39 -04006284 /* At this point, all discovery is aborted */
6285 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006286
6287 /* Unregister VPIs */
6288 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006289 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006290 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006291 /* Stop FLOGI/FDISC retries */
6292 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6293 if (ndlp)
6294 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006295 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006296 if (phba->sli_rev == LPFC_SLI_REV4)
6297 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006298 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006299 shost = lpfc_shost_from_vport(vports[i]);
6300 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006301 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006302 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006303 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006304 }
6305 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006306 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6307 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6308 if (ndlp)
6309 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6310 lpfc_cleanup_pending_mbox(phba->pport);
6311 if (phba->sli_rev == LPFC_SLI_REV4)
6312 lpfc_sli4_unreg_all_rpis(phba->pport);
6313 lpfc_mbx_unreg_vpi(phba->pport);
6314 shost = lpfc_shost_from_vport(phba->pport);
6315 spin_lock_irq(shost->host_lock);
6316 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6317 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6318 spin_unlock_irq(shost->host_lock);
6319 }
James Smart6fb120a2009-05-22 14:52:59 -04006320
James Smart695a8142010-01-26 23:08:03 -05006321 /* Cleanup any outstanding ELS commands */
6322 lpfc_els_flush_all_cmd(phba);
6323
James Smart1b511972011-12-13 13:23:09 -05006324 /* Unregister the physical port VFI */
6325 rc = lpfc_issue_unreg_vfi(phba->pport);
6326 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006327}
6328
6329/**
6330 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6331 * @phba: Pointer to hba context object.
6332 *
6333 * This function issues synchronous unregister FCF mailbox command to HBA to
6334 * unregister the currently registered FCF record. The driver does not reset
6335 * the driver FCF usage state flags.
6336 *
6337 * Return 0 if successfully issued, none-zero otherwise.
6338 */
6339int
6340lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6341{
6342 LPFC_MBOXQ_t *mbox;
6343 int rc;
6344
James Smart6fb120a2009-05-22 14:52:59 -04006345 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6346 if (!mbox) {
6347 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006348 "2551 UNREG_FCFI mbox allocation failed"
6349 "HBA state x%x\n", phba->pport->port_state);
6350 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006351 }
James Smart6fb120a2009-05-22 14:52:59 -04006352 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6353 mbox->vport = phba->pport;
6354 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6355 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6356
6357 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6359 "2552 Unregister FCFI command failed rc x%x "
6360 "HBA state x%x\n",
6361 rc, phba->pport->port_state);
6362 return -EINVAL;
6363 }
6364 return 0;
6365}
6366
6367/**
6368 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6369 * @phba: Pointer to hba context object.
6370 *
6371 * This function unregisters the currently reigstered FCF. This function
6372 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6373 */
6374void
6375lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6376{
6377 int rc;
6378
6379 /* Preparation for unregistering fcf */
6380 rc = lpfc_unregister_fcf_prep(phba);
6381 if (rc) {
6382 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6383 "2748 Failed to prepare for unregistering "
6384 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006385 return;
6386 }
6387
James Smartecfd03c2010-02-12 14:41:27 -05006388 /* Now, unregister FCF record and reset HBA FCF state */
6389 rc = lpfc_sli4_unregister_fcf(phba);
6390 if (rc)
6391 return;
6392 /* Reset HBA FCF states after successful unregister FCF */
6393 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006394 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006395
6396 /*
6397 * If driver is not unloading, check if there is any other
6398 * FCF record that can be used for discovery.
6399 */
6400 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006401 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006402 return;
6403
James Smart0c9ab6f2010-02-26 14:15:57 -05006404 /* This is considered as the initial FCF discovery scan */
6405 spin_lock_irq(&phba->hbalock);
6406 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6407 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006408
6409 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006410 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006411
James Smart0c9ab6f2010-02-26 14:15:57 -05006412 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006413
James Smart0c9ab6f2010-02-26 14:15:57 -05006414 if (rc) {
6415 spin_lock_irq(&phba->hbalock);
6416 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6417 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006418 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006419 "2553 lpfc_unregister_unused_fcf failed "
6420 "to read FCF record HBA state x%x\n",
6421 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006422 }
James Smartecfd03c2010-02-12 14:41:27 -05006423}
6424
6425/**
6426 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6427 * @phba: Pointer to hba context object.
6428 *
6429 * This function just unregisters the currently reigstered FCF. It does not
6430 * try to find another FCF for discovery.
6431 */
6432void
6433lpfc_unregister_fcf(struct lpfc_hba *phba)
6434{
6435 int rc;
6436
6437 /* Preparation for unregistering fcf */
6438 rc = lpfc_unregister_fcf_prep(phba);
6439 if (rc) {
6440 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6441 "2749 Failed to prepare for unregistering "
6442 "HBA's FCF record: rc=%d\n", rc);
6443 return;
6444 }
6445
6446 /* Now, unregister FCF record and reset HBA FCF state */
6447 rc = lpfc_sli4_unregister_fcf(phba);
6448 if (rc)
6449 return;
6450 /* Set proper HBA FCF states after successful unregister FCF */
6451 spin_lock_irq(&phba->hbalock);
6452 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6453 spin_unlock_irq(&phba->hbalock);
6454}
6455
6456/**
6457 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6458 * @phba: Pointer to hba context object.
6459 *
6460 * This function check if there are any connected remote port for the FCF and
6461 * if all the devices are disconnected, this function unregister FCFI.
6462 * This function also tries to use another FCF for discovery.
6463 */
6464void
6465lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6466{
6467 /*
James Smart3804dc82010-07-14 15:31:37 -04006468 * If HBA is not running in FIP mode, if HBA does not support
6469 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6470 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006471 */
6472 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006473 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006474 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006475 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006476 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006477 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006478 spin_unlock_irq(&phba->hbalock);
6479 return;
6480 }
6481 spin_unlock_irq(&phba->hbalock);
6482
6483 if (lpfc_fcf_inuse(phba))
6484 return;
6485
6486 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006487}
6488
6489/**
6490 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6491 * @phba: Pointer to hba context object.
6492 * @buff: Buffer containing the FCF connection table as in the config
6493 * region.
6494 * This function create driver data structure for the FCF connection
6495 * record table read from config region 23.
6496 */
6497static void
6498lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6499 uint8_t *buff)
6500{
6501 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6502 struct lpfc_fcf_conn_hdr *conn_hdr;
6503 struct lpfc_fcf_conn_rec *conn_rec;
6504 uint32_t record_count;
6505 int i;
6506
6507 /* Free the current connect table */
6508 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006509 &phba->fcf_conn_rec_list, list) {
6510 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006511 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006512 }
James Smart6fb120a2009-05-22 14:52:59 -04006513
6514 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6515 record_count = conn_hdr->length * sizeof(uint32_t)/
6516 sizeof(struct lpfc_fcf_conn_rec);
6517
6518 conn_rec = (struct lpfc_fcf_conn_rec *)
6519 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6520
6521 for (i = 0; i < record_count; i++) {
6522 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6523 continue;
6524 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6525 GFP_KERNEL);
6526 if (!conn_entry) {
6527 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6528 "2566 Failed to allocate connection"
6529 " table entry\n");
6530 return;
6531 }
6532
6533 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6534 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006535 list_add_tail(&conn_entry->list,
6536 &phba->fcf_conn_rec_list);
6537 }
James Smartdf0d0852013-05-31 17:05:08 -04006538
6539 if (!list_empty(&phba->fcf_conn_rec_list)) {
6540 i = 0;
6541 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6542 list) {
6543 conn_rec = &conn_entry->conn_rec;
6544 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6545 "3345 FCF connection list rec[%02d]: "
6546 "flags:x%04x, vtag:x%04x, "
6547 "fabric_name:x%02x:%02x:%02x:%02x:"
6548 "%02x:%02x:%02x:%02x, "
6549 "switch_name:x%02x:%02x:%02x:%02x:"
6550 "%02x:%02x:%02x:%02x\n", i++,
6551 conn_rec->flags, conn_rec->vlan_tag,
6552 conn_rec->fabric_name[0],
6553 conn_rec->fabric_name[1],
6554 conn_rec->fabric_name[2],
6555 conn_rec->fabric_name[3],
6556 conn_rec->fabric_name[4],
6557 conn_rec->fabric_name[5],
6558 conn_rec->fabric_name[6],
6559 conn_rec->fabric_name[7],
6560 conn_rec->switch_name[0],
6561 conn_rec->switch_name[1],
6562 conn_rec->switch_name[2],
6563 conn_rec->switch_name[3],
6564 conn_rec->switch_name[4],
6565 conn_rec->switch_name[5],
6566 conn_rec->switch_name[6],
6567 conn_rec->switch_name[7]);
6568 }
6569 }
James Smart6fb120a2009-05-22 14:52:59 -04006570}
6571
6572/**
6573 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6574 * @phba: Pointer to hba context object.
6575 * @buff: Buffer containing the FCoE parameter data structure.
6576 *
6577 * This function update driver data structure with config
6578 * parameters read from config region 23.
6579 */
6580static void
6581lpfc_read_fcoe_param(struct lpfc_hba *phba,
6582 uint8_t *buff)
6583{
6584 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6585 struct lpfc_fcoe_params *fcoe_param;
6586
6587 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6588 buff;
6589 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006590 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006591
6592 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6593 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6594 return;
6595
James Smart6fb120a2009-05-22 14:52:59 -04006596 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6597 phba->valid_vlan = 1;
6598 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6599 0xFFF;
6600 }
6601
6602 phba->fc_map[0] = fcoe_param->fc_map[0];
6603 phba->fc_map[1] = fcoe_param->fc_map[1];
6604 phba->fc_map[2] = fcoe_param->fc_map[2];
6605 return;
6606}
6607
6608/**
6609 * lpfc_get_rec_conf23 - Get a record type in config region data.
6610 * @buff: Buffer containing config region 23 data.
6611 * @size: Size of the data buffer.
6612 * @rec_type: Record type to be searched.
6613 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006614 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006615 * of the record specified by record_type. If record found, this
6616 * function return pointer to the record else return NULL.
6617 */
6618static uint8_t *
6619lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6620{
6621 uint32_t offset = 0, rec_length;
6622
6623 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6624 (size < sizeof(uint32_t)))
6625 return NULL;
6626
6627 rec_length = buff[offset + 1];
6628
6629 /*
6630 * One TLV record has one word header and number of data words
6631 * specified in the rec_length field of the record header.
6632 */
6633 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6634 <= size) {
6635 if (buff[offset] == rec_type)
6636 return &buff[offset];
6637
6638 if (buff[offset] == LPFC_REGION23_LAST_REC)
6639 return NULL;
6640
6641 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6642 rec_length = buff[offset + 1];
6643 }
6644 return NULL;
6645}
6646
6647/**
6648 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6649 * @phba: Pointer to lpfc_hba data structure.
6650 * @buff: Buffer containing config region 23 data.
6651 * @size: Size of the data buffer.
6652 *
Stefan Weileef35c22010-08-06 21:11:15 +02006653 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006654 * populate driver data structure with the parameters.
6655 */
6656void
6657lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6658 uint8_t *buff,
6659 uint32_t size)
6660{
James Smarteb016562014-09-03 12:58:06 -04006661 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006662 uint8_t *rec_ptr;
6663
6664 /*
6665 * If data size is less than 2 words signature and version cannot be
6666 * verified.
6667 */
6668 if (size < 2*sizeof(uint32_t))
6669 return;
6670
6671 /* Check the region signature first */
6672 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6673 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6674 "2567 Config region 23 has bad signature\n");
6675 return;
6676 }
6677
6678 offset += 4;
6679
6680 /* Check the data structure version */
6681 if (buff[offset] != LPFC_REGION23_VERSION) {
6682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6683 "2568 Config region 23 has bad version\n");
6684 return;
6685 }
6686 offset += 4;
6687
James Smart6fb120a2009-05-22 14:52:59 -04006688 /* Read FCoE param record */
6689 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6690 size - offset, FCOE_PARAM_TYPE);
6691 if (rec_ptr)
6692 lpfc_read_fcoe_param(phba, rec_ptr);
6693
6694 /* Read FCF connection table */
6695 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6696 size - offset, FCOE_CONN_TBL_TYPE);
6697 if (rec_ptr)
6698 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6699
6700}