blob: d313dde76963c2e6d341a8d9f85c9642bfe5c16d [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 Smart4d9ab992009-10-02 15:16:39 -0400696 if ((status & HA_RXMASK) ||
697 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400698 (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 Smart94661502013-03-01 16:37:29 -0500704 if (phba->link_state >= LPFC_LINK_UP) {
705 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
706 lpfc_sli_handle_slow_ring_event(phba, pring,
707 (status &
708 HA_RXMASK));
709 }
James Smart858c9f62007-06-17 19:56:39 -0500710 }
Sebastian Herbsztb1039182015-07-22 10:53:22 +0200711 if ((phba->sli_rev == LPFC_SLI_REV4) &&
James Smart0e9bb8d2013-03-01 16:35:12 -0500712 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400713 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500714 /*
715 * Turn on Ring interrupts
716 */
James Smart3772a992009-05-22 14:50:54 -0400717 if (phba->sli_rev <= LPFC_SLI_REV3) {
718 spin_lock_irq(&phba->hbalock);
719 control = readl(phba->HCregaddr);
720 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
721 lpfc_debugfs_slow_ring_trc(phba,
722 "WRK Enable ring: cntl:x%x hacopy:x%x",
723 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400724
James Smart3772a992009-05-22 14:50:54 -0400725 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
726 writel(control, phba->HCregaddr);
727 readl(phba->HCregaddr); /* flush */
728 } else {
729 lpfc_debugfs_slow_ring_trc(phba,
730 "WRK Ring ok: cntl:x%x hacopy:x%x",
731 control, ha_copy, 0);
732 }
733 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500734 }
735 }
James Smart2e0fef82007-06-17 19:56:36 -0500736 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500737}
738
dea31012005-04-17 16:05:31 -0500739int
740lpfc_do_work(void *p)
741{
742 struct lpfc_hba *phba = p;
743 int rc;
dea31012005-04-17 16:05:31 -0500744
Dongsheng Yang8698a742014-03-11 18:09:12 +0800745 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400746 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400747 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500748
James Smart3a55b532008-12-04 22:38:54 -0500749 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400750 /* wait and check worker queue activities */
751 rc = wait_event_interruptible(phba->work_waitq,
752 (test_and_clear_bit(LPFC_DATA_READY,
753 &phba->data_flags)
754 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500755 /* Signal wakeup shall terminate the worker thread */
756 if (rc) {
757 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
758 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500759 break;
James Smart3a55b532008-12-04 22:38:54 -0500760 }
dea31012005-04-17 16:05:31 -0500761
James Smart5e9d9b82008-06-14 22:52:53 -0400762 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500763 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500764 }
James Smart3a55b532008-12-04 22:38:54 -0500765 phba->worker_thread = NULL;
766 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
767 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500768 return 0;
769}
770
771/*
772 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300773 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500774 * embedding it in the IOCB.
775 */
776int
James Smart2e0fef82007-06-17 19:56:36 -0500777lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500778 uint32_t evt)
779{
780 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500781 unsigned long flags;
dea31012005-04-17 16:05:31 -0500782
783 /*
784 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
785 * be queued to worker thread for processing
786 */
James Smart92d7f7b2007-06-17 19:56:38 -0500787 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500788 if (!evtp)
789 return 0;
790
791 evtp->evt_arg1 = arg1;
792 evtp->evt_arg2 = arg2;
793 evtp->evt = evt;
794
James Smarted957682007-06-17 19:56:37 -0500795 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400796 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500797 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500798
James Smart5e9d9b82008-06-14 22:52:53 -0400799 lpfc_worker_wake_up(phba);
800
dea31012005-04-17 16:05:31 -0500801 return 1;
802}
803
James Smart92d7f7b2007-06-17 19:56:38 -0500804void
805lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
806{
James Smart09372822008-01-11 01:52:54 -0500807 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500808 struct lpfc_hba *phba = vport->phba;
809 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500810
811 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500812 if (!NLP_CHK_NODE_ACT(ndlp))
813 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500814 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
815 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400816 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
817 ((vport->port_type == LPFC_NPIV_PORT) &&
818 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500819 lpfc_unreg_rpi(vport, ndlp);
820
821 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400822 if ((phba->sli_rev < LPFC_SLI_REV4) &&
823 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500824 continue;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -0400825 lpfc_disc_state_machine(vport, ndlp, NULL,
826 remove
827 ? NLP_EVT_DEVICE_RM
828 : NLP_EVT_DEVICE_RECOVERY);
James Smart92d7f7b2007-06-17 19:56:38 -0500829 }
830 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400831 if (phba->sli_rev == LPFC_SLI_REV4)
832 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500833 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500834 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500835 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500836 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500837 }
838}
839
James Smart87af33f2007-10-27 13:37:43 -0400840void
James Smart98c9ea52007-10-27 13:37:33 -0400841lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500842{
James Smart695a8142010-01-26 23:08:03 -0500843 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
844
James Smart45ed1192009-10-02 15:17:02 -0400845 /* Cleanup any outstanding received buffers */
846 lpfc_cleanup_rcv_buffers(vport);
847
James Smart92d7f7b2007-06-17 19:56:38 -0500848 /* Cleanup any outstanding RSCN activity */
849 lpfc_els_flush_rscn(vport);
850
851 /* Cleanup any outstanding ELS commands */
852 lpfc_els_flush_cmd(vport);
853
854 lpfc_cleanup_rpis(vport, 0);
855
James Smart92d7f7b2007-06-17 19:56:38 -0500856 /* Turn off discovery timer if its running */
857 lpfc_can_disctmo(vport);
858}
859
James Smart3772a992009-05-22 14:50:54 -0400860void
James Smart98c9ea52007-10-27 13:37:33 -0400861lpfc_linkdown_port(struct lpfc_vport *vport)
862{
James Smarta0f2d3e2017-02-12 13:52:31 -0800863 struct lpfc_hba *phba = vport->phba;
James Smart98c9ea52007-10-27 13:37:33 -0400864 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
865
James Smarta0f2d3e2017-02-12 13:52:31 -0800866 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
867 fc_host_post_event(shost, fc_get_event_number(),
868 FCH_EVT_LINKDOWN, 0);
James Smart98c9ea52007-10-27 13:37:33 -0400869
870 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
871 "Link Down: state:x%x rtry:x%x flg:x%x",
872 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
873
874 lpfc_port_link_failure(vport);
875
James Smart92494142011-02-16 12:39:44 -0500876 /* Stop delayed Nport discovery */
877 spin_lock_irq(shost->host_lock);
878 vport->fc_flag &= ~FC_DISC_DELAYED;
879 spin_unlock_irq(shost->host_lock);
880 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400881}
882
dea31012005-04-17 16:05:31 -0500883int
James Smart685f0bf2007-04-25 09:53:08 -0400884lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500885{
James Smart2e0fef82007-06-17 19:56:36 -0500886 struct lpfc_vport *vport = phba->pport;
887 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400888 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400889 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400890 int i;
dea31012005-04-17 16:05:31 -0500891
James Smart3163f722008-02-08 18:50:25 -0500892 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500893 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500894
895 /* Block all SCSI stack I/Os */
896 lpfc_scsi_dev_block(phba);
897
James Smart2e0fef82007-06-17 19:56:36 -0500898 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500899 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500900 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500901 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500902 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500903 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500904 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500905 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500906 }
James Smart549e55c2007-08-02 11:09:51 -0400907 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -0800908 if (vports != NULL) {
James Smart6fb120a2009-05-22 14:52:59 -0400909 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400910 /* Issue a LINK DOWN event to all nodes */
911 lpfc_linkdown_port(vports[i]);
James Smart895427b2017-02-12 13:52:30 -0800912
913 vports[i]->fc_myDID = 0;
914
James Smart01649562017-02-12 13:52:32 -0800915 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -0800916 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -0800917 if (phba->nvmet_support)
918 lpfc_nvmet_update_targetport(phba);
919 else
James Smart8c258642017-02-12 13:52:36 -0800920 lpfc_nvme_update_localport(vports[i]);
James Smart8c258642017-02-12 13:52:36 -0800921 }
James Smart549e55c2007-08-02 11:09:51 -0400922 }
James Smart895427b2017-02-12 13:52:30 -0800923 }
James Smart09372822008-01-11 01:52:54 -0500924 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500925 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500926 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
927 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400928 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500929 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500930 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400931 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500932 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500933 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500934 }
935 }
936
dea31012005-04-17 16:05:31 -0500937 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500938 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500939 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
940 if (mb) {
dea31012005-04-17 16:05:31 -0500941 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500942 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500943 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400944 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500945 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500946 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500947 }
948 }
James Smart2e0fef82007-06-17 19:56:36 -0500949 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500950 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500951 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500952 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500953 return 0;
dea31012005-04-17 16:05:31 -0500954}
955
James Smart92d7f7b2007-06-17 19:56:38 -0500956static void
957lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500958{
James Smart92d7f7b2007-06-17 19:56:38 -0500959 struct lpfc_nodelist *ndlp;
960
961 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500962 if (!NLP_CHK_NODE_ACT(ndlp))
963 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500964 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
965 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500966 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500967 /* On Linkup its safe to clean up the ndlp
968 * from Fabric connections.
969 */
James Smart92d7f7b2007-06-17 19:56:38 -0500970 if (ndlp->nlp_DID != Fabric_DID)
971 lpfc_unreg_rpi(vport, ndlp);
972 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
973 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500974 /* Fail outstanding IO now since device is
975 * marked for PLOGI.
976 */
James Smart92d7f7b2007-06-17 19:56:38 -0500977 lpfc_unreg_rpi(vport, ndlp);
978 }
979 }
980}
981
982static void
983lpfc_linkup_port(struct lpfc_vport *vport)
984{
985 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500986 struct lpfc_hba *phba = vport->phba;
987
988 if ((vport->load_flag & FC_UNLOADING) != 0)
989 return;
990
James Smart858c9f62007-06-17 19:56:39 -0500991 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
992 "Link Up: top:x%x speed:x%x flg:x%x",
993 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
994
James Smart92d7f7b2007-06-17 19:56:38 -0500995 /* If NPIV is not enabled, only bring the physical port up */
996 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
997 (vport != phba->pport))
998 return;
dea31012005-04-17 16:05:31 -0500999
James Smarta0f2d3e2017-02-12 13:52:31 -08001000 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
1001 fc_host_post_event(shost, fc_get_event_number(),
1002 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -04001003
James Smart2e0fef82007-06-17 19:56:36 -05001004 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -05001005 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
1006 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
1007 vport->fc_flag |= FC_NDISC_ACTIVE;
1008 vport->fc_ns_retry = 0;
1009 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001010
James Smart92d7f7b2007-06-17 19:56:38 -05001011 if (vport->fc_flag & FC_LBIT)
1012 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001013
James Smart92d7f7b2007-06-17 19:56:38 -05001014}
1015
1016static int
1017lpfc_linkup(struct lpfc_hba *phba)
1018{
James Smart549e55c2007-08-02 11:09:51 -04001019 struct lpfc_vport **vports;
1020 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001021
1022 phba->link_state = LPFC_LINK_UP;
1023
1024 /* Unblock fabric iocbs if they are blocked */
1025 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1026 del_timer_sync(&phba->fabric_block_timer);
1027
James Smart549e55c2007-08-02 11:09:51 -04001028 vports = lpfc_create_vport_work_array(phba);
1029 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001030 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001031 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001032 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001033
1034 return 0;
1035}
1036
1037/*
1038 * This routine handles processing a CLEAR_LA mailbox
1039 * command upon completion. It is setup in the LPFC_MBOXQ
1040 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001041 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001042 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001043static void
James Smart2e0fef82007-06-17 19:56:36 -05001044lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001045{
James Smart2e0fef82007-06-17 19:56:36 -05001046 struct lpfc_vport *vport = pmb->vport;
1047 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1048 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001049 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001050 uint32_t control;
1051
dea31012005-04-17 16:05:31 -05001052 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001053 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1054 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001055
1056 /* Check for error */
1057 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001058 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001059 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1060 "0320 CLEAR_LA mbxStatus error x%x hba "
1061 "state x%x\n",
1062 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001063 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001064 goto out;
1065 }
1066
James Smart92d7f7b2007-06-17 19:56:38 -05001067 if (vport->port_type == LPFC_PHYSICAL_PORT)
1068 phba->link_state = LPFC_HBA_READY;
1069
1070 spin_lock_irq(&phba->hbalock);
1071 psli->sli_flag |= LPFC_PROCESS_LA;
1072 control = readl(phba->HCregaddr);
1073 control |= HC_LAINT_ENA;
1074 writel(control, phba->HCregaddr);
1075 readl(phba->HCregaddr); /* flush */
1076 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001077 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001078 return;
dea31012005-04-17 16:05:31 -05001079
dea31012005-04-17 16:05:31 -05001080out:
1081 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001082 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1083 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001084 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001085
James Smart2e0fef82007-06-17 19:56:36 -05001086 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001087 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001088 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001089
James Smart2e0fef82007-06-17 19:56:36 -05001090 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001091
1092 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001093
1094 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001095 psli->sli_flag |= LPFC_PROCESS_LA;
1096 control = readl(phba->HCregaddr);
1097 control |= HC_LAINT_ENA;
1098 writel(control, phba->HCregaddr);
1099 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001100 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001101
1102 return;
1103}
1104
James Smart2e0fef82007-06-17 19:56:36 -05001105
James Smartd6de08c2015-12-16 18:11:53 -05001106void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001107lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001108{
James Smart2e0fef82007-06-17 19:56:36 -05001109 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001110
James Smart04c68492009-05-22 14:52:52 -04001111 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001112 goto out;
dea31012005-04-17 16:05:31 -05001113
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001114 mempool_free(pmb, phba->mbox_mem_pool);
1115
James Smart1b511972011-12-13 13:23:09 -05001116 /* don't perform discovery for SLI4 loopback diagnostic test */
1117 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1118 !(phba->hba_flag & HBA_FCOE_MODE) &&
1119 (phba->link_flag & LS_LOOPBACK_MODE))
1120 return;
1121
James Smart76a95d72010-11-20 23:11:48 -05001122 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001123 vport->fc_flag & FC_PUBLIC_LOOP &&
1124 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001125 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001126 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001127 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1128 */
James Smart2e0fef82007-06-17 19:56:36 -05001129 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001130 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001131 }
dea31012005-04-17 16:05:31 -05001132
James Smart2e0fef82007-06-17 19:56:36 -05001133 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001134 * LPFC_FLOGI while waiting for FLOGI cmpl
1135 */
James Smartd6de08c2015-12-16 18:11:53 -05001136 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001137 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001138 else if (vport->fc_flag & FC_PT2PT)
1139 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001140 return;
dea31012005-04-17 16:05:31 -05001141
1142out:
James Smarte8b62012007-08-02 11:10:09 -04001143 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1144 "0306 CONFIG_LINK mbxStatus error x%x "
1145 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001146 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001147 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001148
1149 lpfc_linkdown(phba);
1150
James Smarte8b62012007-08-02 11:10:09 -04001151 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1152 "0200 CONFIG_LINK bad hba state x%x\n",
1153 vport->port_state);
dea31012005-04-17 16:05:31 -05001154
James Smart92d7f7b2007-06-17 19:56:38 -05001155 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001156 return;
1157}
1158
James Smart7d791df2011-07-22 18:37:52 -04001159/**
1160 * lpfc_sli4_clear_fcf_rr_bmask
1161 * @phba pointer to the struct lpfc_hba for this port.
1162 * This fucnction resets the round robin bit mask and clears the
1163 * fcf priority list. The list deletions are done while holding the
1164 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1165 * from the lpfc_fcf_pri record.
1166 **/
1167void
1168lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1169{
1170 struct lpfc_fcf_pri *fcf_pri;
1171 struct lpfc_fcf_pri *next_fcf_pri;
1172 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1173 spin_lock_irq(&phba->hbalock);
1174 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1175 &phba->fcf.fcf_pri_list, list) {
1176 list_del_init(&fcf_pri->list);
1177 fcf_pri->fcf_rec.flag = 0;
1178 }
1179 spin_unlock_irq(&phba->hbalock);
1180}
dea31012005-04-17 16:05:31 -05001181static void
James Smart6fb120a2009-05-22 14:52:59 -04001182lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1183{
1184 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001185
1186 if (mboxq->u.mb.mbxStatus) {
1187 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1188 "2017 REG_FCFI mbxStatus error x%x "
1189 "HBA state x%x\n",
1190 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001191 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001192 }
1193
1194 /* Start FCoE discovery by sending a FLOGI. */
1195 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1196 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001197 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001198 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001199 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001200
James Smart32b97932009-07-19 10:01:21 -04001201 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001202 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1203 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001204 goto fail_out;
1205
1206 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001207 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001208 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001209 phba->hba_flag &= ~FCF_TS_INPROG;
1210 if (vport->port_state != LPFC_FLOGI) {
1211 phba->hba_flag |= FCF_RR_INPROG;
1212 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001213 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001214 goto out;
1215 }
1216 spin_unlock_irq(&phba->hbalock);
1217 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001218
James Smarta93ff372010-10-22 11:06:08 -04001219fail_out:
1220 spin_lock_irq(&phba->hbalock);
1221 phba->hba_flag &= ~FCF_RR_INPROG;
1222 spin_unlock_irq(&phba->hbalock);
1223out:
James Smart6fb120a2009-05-22 14:52:59 -04001224 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001225}
1226
1227/**
1228 * lpfc_fab_name_match - Check if the fcf fabric name match.
1229 * @fab_name: pointer to fabric name.
1230 * @new_fcf_record: pointer to fcf record.
1231 *
1232 * This routine compare the fcf record's fabric name with provided
1233 * fabric name. If the fabric name are identical this function
1234 * returns 1 else return 0.
1235 **/
1236static uint32_t
1237lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1238{
James Smartecfd03c2010-02-12 14:41:27 -05001239 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001240 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001241 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1242 return 0;
1243 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1244 return 0;
1245 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1246 return 0;
1247 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1248 return 0;
1249 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1250 return 0;
1251 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1252 return 0;
1253 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1254 return 0;
1255 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001256}
1257
1258/**
James Smart8fa38512009-07-19 10:01:03 -04001259 * lpfc_sw_name_match - Check if the fcf switch name match.
1260 * @fab_name: pointer to fabric name.
1261 * @new_fcf_record: pointer to fcf record.
1262 *
1263 * This routine compare the fcf record's switch name with provided
1264 * switch name. If the switch name are identical this function
1265 * returns 1 else return 0.
1266 **/
1267static uint32_t
1268lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1269{
James Smartecfd03c2010-02-12 14:41:27 -05001270 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001271 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001272 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1273 return 0;
1274 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1275 return 0;
1276 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1277 return 0;
1278 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1279 return 0;
1280 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1281 return 0;
1282 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1283 return 0;
1284 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1285 return 0;
1286 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001287}
1288
1289/**
James Smart6fb120a2009-05-22 14:52:59 -04001290 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001291 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001292 * @new_fcf_record: pointer to fcf record.
1293 *
1294 * This routine compare the fcf record's mac address with HBA's
1295 * FCF mac address. If the mac addresses are identical this function
1296 * returns 1 else return 0.
1297 **/
1298static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001299lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001300{
James Smartecfd03c2010-02-12 14:41:27 -05001301 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001302 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001303 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1304 return 0;
1305 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1306 return 0;
1307 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1308 return 0;
1309 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1310 return 0;
1311 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1312 return 0;
1313 return 1;
1314}
1315
1316static bool
1317lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1318{
1319 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001320}
1321
1322/**
James Smart7d791df2011-07-22 18:37:52 -04001323 * lpfc_update_fcf_record - Update driver fcf record
1324 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1325 * @phba: pointer to lpfc hba data structure.
1326 * @fcf_index: Index for the lpfc_fcf_record.
1327 * @new_fcf_record: pointer to hba fcf record.
1328 *
1329 * This routine updates the driver FCF priority record from the new HBA FCF
1330 * record. This routine is called with the host lock held.
1331 **/
1332static void
1333__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1334 struct fcf_record *new_fcf_record
1335 )
1336{
1337 struct lpfc_fcf_pri *fcf_pri;
1338
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001339 lockdep_assert_held(&phba->hbalock);
1340
James Smart7d791df2011-07-22 18:37:52 -04001341 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1342 fcf_pri->fcf_rec.fcf_index = fcf_index;
1343 /* FCF record priority */
1344 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1345
1346}
1347
1348/**
James Smart6fb120a2009-05-22 14:52:59 -04001349 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001350 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001351 * @new_fcf_record: pointer to fcf record.
1352 *
1353 * This routine copies the FCF information from the FCF
1354 * record to lpfc_hba data structure.
1355 **/
1356static void
James Smartecfd03c2010-02-12 14:41:27 -05001357lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1358 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001359{
James Smartecfd03c2010-02-12 14:41:27 -05001360 /* Fabric name */
1361 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001362 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001363 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001364 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001365 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001366 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001367 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001368 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001369 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001370 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001371 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001372 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001373 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001374 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001375 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001376 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001377 /* Mac address */
1378 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1379 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1380 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1381 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1382 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1383 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1384 /* FCF record index */
1385 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1386 /* FCF record priority */
1387 fcf_rec->priority = new_fcf_record->fip_priority;
1388 /* Switch name */
1389 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001390 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001391 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001392 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001393 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001394 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001395 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001396 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001397 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001398 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001399 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001400 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001401 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001402 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001403 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001404 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001405}
1406
1407/**
James Smartecfd03c2010-02-12 14:41:27 -05001408 * lpfc_update_fcf_record - Update driver fcf record
1409 * @phba: pointer to lpfc hba data structure.
1410 * @fcf_rec: pointer to driver fcf record.
1411 * @new_fcf_record: pointer to hba fcf record.
1412 * @addr_mode: address mode to be set to the driver fcf record.
1413 * @vlan_id: vlan tag to be set to the driver fcf record.
1414 * @flag: flag bits to be set to the driver fcf record.
1415 *
1416 * This routine updates the driver FCF record from the new HBA FCF record
1417 * together with the address mode, vlan_id, and other informations. This
1418 * routine is called with the host lock held.
1419 **/
1420static void
1421__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1422 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1423 uint16_t vlan_id, uint32_t flag)
1424{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001425 lockdep_assert_held(&phba->hbalock);
1426
James Smartecfd03c2010-02-12 14:41:27 -05001427 /* Copy the fields from the HBA's FCF record */
1428 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1429 /* Update other fields of driver FCF record */
1430 fcf_rec->addr_mode = addr_mode;
1431 fcf_rec->vlan_id = vlan_id;
1432 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001433 __lpfc_update_fcf_record_pri(phba,
1434 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1435 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001436}
1437
1438/**
James Smart6fb120a2009-05-22 14:52:59 -04001439 * lpfc_register_fcf - Register the FCF with hba.
1440 * @phba: pointer to lpfc hba data structure.
1441 *
1442 * This routine issues a register fcfi mailbox command to register
1443 * the fcf with HBA.
1444 **/
1445static void
1446lpfc_register_fcf(struct lpfc_hba *phba)
1447{
1448 LPFC_MBOXQ_t *fcf_mbxq;
1449 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001450
James Smartd439d282010-09-29 11:18:45 -04001451 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001452 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001453 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001454 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001455 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001456 return;
1457 }
1458
1459 /* The FCF is already registered, start discovery */
1460 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001461 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001462 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001463 if (phba->pport->port_state != LPFC_FLOGI &&
1464 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001465 phba->hba_flag |= FCF_RR_INPROG;
1466 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001467 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001468 return;
1469 }
1470 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001471 return;
1472 }
James Smartd439d282010-09-29 11:18:45 -04001473 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001474
James Smarta93ff372010-10-22 11:06:08 -04001475 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001476 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001477 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001478 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001479 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001480 return;
James Smart4d9ab992009-10-02 15:16:39 -04001481 }
James Smart6fb120a2009-05-22 14:52:59 -04001482
1483 lpfc_reg_fcfi(phba, fcf_mbxq);
1484 fcf_mbxq->vport = phba->pport;
1485 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1486 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001487 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001488 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001489 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001490 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001491 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001492 }
James Smart6fb120a2009-05-22 14:52:59 -04001493
1494 return;
1495}
1496
1497/**
1498 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1499 * @phba: pointer to lpfc hba data structure.
1500 * @new_fcf_record: pointer to fcf record.
1501 * @boot_flag: Indicates if this record used by boot bios.
1502 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001503 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001504 *
1505 * This routine compare the fcf record with connect list obtained from the
1506 * config region to decide if this FCF can be used for SAN discovery. It returns
1507 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1508 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1509 * is used by boot bios and addr_mode will indicate the addressing mode to be
1510 * used for this FCF when the function returns.
1511 * If the FCF record need to be used with a particular vlan id, the vlan is
1512 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001513 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001514 **/
1515static int
1516lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1517 struct fcf_record *new_fcf_record,
1518 uint32_t *boot_flag, uint32_t *addr_mode,
1519 uint16_t *vlan_id)
1520{
1521 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001522 int i, j, fcf_vlan_id = 0;
1523
1524 /* Find the lowest VLAN id in the FCF record */
1525 for (i = 0; i < 512; i++) {
1526 if (new_fcf_record->vlan_bitmap[i]) {
1527 fcf_vlan_id = i * 8;
1528 j = 0;
1529 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1530 j++;
1531 fcf_vlan_id++;
1532 }
1533 break;
1534 }
1535 }
James Smart6fb120a2009-05-22 14:52:59 -04001536
James Smart26979ce2012-09-29 11:31:55 -04001537 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001538 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001539 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1540 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001541 return 0;
1542
James Smart45ed1192009-10-02 15:17:02 -04001543 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001544 *boot_flag = 0;
1545 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1546 new_fcf_record);
1547 if (phba->valid_vlan)
1548 *vlan_id = phba->vlan_id;
1549 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001550 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001551 return 1;
1552 }
1553
1554 /*
1555 * If there are no FCF connection table entry, driver connect to all
1556 * FCFs.
1557 */
1558 if (list_empty(&phba->fcf_conn_rec_list)) {
1559 *boot_flag = 0;
1560 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1561 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001562
1563 /*
1564 * When there are no FCF connect entries, use driver's default
1565 * addressing mode - FPMA.
1566 */
1567 if (*addr_mode & LPFC_FCF_FPMA)
1568 *addr_mode = LPFC_FCF_FPMA;
1569
James Smart4d9ab992009-10-02 15:16:39 -04001570 /* If FCF record report a vlan id use that vlan id */
1571 if (fcf_vlan_id)
1572 *vlan_id = fcf_vlan_id;
1573 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001574 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001575 return 1;
1576 }
1577
James Smartecfd03c2010-02-12 14:41:27 -05001578 list_for_each_entry(conn_entry,
1579 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001580 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1581 continue;
1582
1583 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1584 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001585 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001586 continue;
James Smart8fa38512009-07-19 10:01:03 -04001587 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1588 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1589 new_fcf_record))
1590 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001591 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1592 /*
1593 * If the vlan bit map does not have the bit set for the
1594 * vlan id to be used, then it is not a match.
1595 */
1596 if (!(new_fcf_record->vlan_bitmap
1597 [conn_entry->conn_rec.vlan_tag / 8] &
1598 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1599 continue;
1600 }
1601
1602 /*
James Smart0c287582009-06-10 17:22:56 -04001603 * If connection record does not support any addressing mode,
1604 * skip the FCF record.
1605 */
1606 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1607 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1608 continue;
1609
1610 /*
James Smart6fb120a2009-05-22 14:52:59 -04001611 * Check if the connection record specifies a required
1612 * addressing mode.
1613 */
1614 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1615 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1616
1617 /*
1618 * If SPMA required but FCF not support this continue.
1619 */
1620 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1621 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1622 new_fcf_record) & LPFC_FCF_SPMA))
1623 continue;
1624
1625 /*
1626 * If FPMA required but FCF not support this continue.
1627 */
1628 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1629 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1630 new_fcf_record) & LPFC_FCF_FPMA))
1631 continue;
1632 }
1633
1634 /*
1635 * This fcf record matches filtering criteria.
1636 */
1637 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1638 *boot_flag = 1;
1639 else
1640 *boot_flag = 0;
1641
James Smart0c287582009-06-10 17:22:56 -04001642 /*
1643 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001644 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001645 * by FCF, allow fabric to pick the addressing mode.
1646 */
James Smart6fb120a2009-05-22 14:52:59 -04001647 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1648 new_fcf_record);
1649 /*
1650 * If the user specified a required address mode, assign that
1651 * address mode
1652 */
1653 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1654 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1655 *addr_mode = (conn_entry->conn_rec.flags &
1656 FCFCNCT_AM_SPMA) ?
1657 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1658 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001659 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001660 * addr mode only if FCF support the addr_mode.
1661 */
1662 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1663 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1664 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1665 (*addr_mode & LPFC_FCF_SPMA))
1666 *addr_mode = LPFC_FCF_SPMA;
1667 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1668 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1669 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1670 (*addr_mode & LPFC_FCF_FPMA))
1671 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001672
James Smart4d9ab992009-10-02 15:16:39 -04001673 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001674 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1675 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001676 /*
1677 * If no vlan id is specified in connect list, use the vlan id
1678 * in the FCF record
1679 */
1680 else if (fcf_vlan_id)
1681 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001682 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001683 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001684
1685 return 1;
1686 }
1687
1688 return 0;
1689}
1690
1691/**
James Smart32b97932009-07-19 10:01:21 -04001692 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1693 * @phba: pointer to lpfc hba data structure.
1694 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1695 *
1696 * This function check if there is any fcoe event pending while driver
1697 * scan FCF entries. If there is any pending event, it will restart the
1698 * FCF saning and return 1 else return 0.
1699 */
1700int
1701lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1702{
James Smart32b97932009-07-19 10:01:21 -04001703 /*
1704 * If the Link is up and no FCoE events while in the
1705 * FCF discovery, no need to restart FCF discovery.
1706 */
1707 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001708 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001709 return 0;
1710
James Smart0c9ab6f2010-02-26 14:15:57 -05001711 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1712 "2768 Pending link or FCF event during current "
1713 "handling of the previous event: link_state:x%x, "
1714 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1715 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1716 phba->fcoe_eventtag);
1717
James Smart32b97932009-07-19 10:01:21 -04001718 spin_lock_irq(&phba->hbalock);
1719 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1720 spin_unlock_irq(&phba->hbalock);
1721
James Smart0c9ab6f2010-02-26 14:15:57 -05001722 if (phba->link_state >= LPFC_LINK_UP) {
1723 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1724 "2780 Restart FCF table scan due to "
1725 "pending FCF event:evt_tag_at_scan:x%x, "
1726 "evt_tag_current:x%x\n",
1727 phba->fcoe_eventtag_at_fcf_scan,
1728 phba->fcoe_eventtag);
1729 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1730 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001731 /*
James Smarta93ff372010-10-22 11:06:08 -04001732 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001733 * flag
1734 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001735 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1736 "2833 Stop FCF discovery process due to link "
1737 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001738 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001739 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001740 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001741 spin_unlock_irq(&phba->hbalock);
1742 }
James Smart32b97932009-07-19 10:01:21 -04001743
James Smart0c9ab6f2010-02-26 14:15:57 -05001744 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001745 if (unreg_fcf) {
1746 spin_lock_irq(&phba->hbalock);
1747 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1748 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001749 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001750 }
James Smart32b97932009-07-19 10:01:21 -04001751 return 1;
1752}
1753
1754/**
James Smart999d8132010-03-15 11:24:56 -04001755 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1756 * @phba: pointer to lpfc hba data structure.
1757 * @fcf_cnt: number of eligible fcf record seen so far.
1758 *
1759 * This function makes an running random selection decision on FCF record to
1760 * use through a sequence of @fcf_cnt eligible FCF records with equal
1761 * probability. To perform integer manunipulation of random numbers with
1762 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001763 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001764 *
1765 * Returns true when outcome is for the newly read FCF record should be
1766 * chosen; otherwise, return false when outcome is for keeping the previously
1767 * chosen FCF record.
1768 **/
1769static bool
1770lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1771{
1772 uint32_t rand_num;
1773
1774 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001775 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001776
1777 /* Decision with probability 1/fcf_cnt */
1778 if ((fcf_cnt * rand_num) < 0xFFFF)
1779 return true;
1780 else
1781 return false;
1782}
1783
1784/**
James Smart3804dc82010-07-14 15:31:37 -04001785 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001786 * @phba: pointer to lpfc hba data structure.
1787 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001788 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001789 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001790 * This routine parses the non-embedded fcf mailbox command by performing the
1791 * necessarily error checking, non-embedded read FCF record mailbox command
1792 * SGE parsing, and endianness swapping.
1793 *
1794 * Returns the pointer to the new FCF record in the non-embedded mailbox
1795 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001796 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001797static struct fcf_record *
1798lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1799 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001800{
1801 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001802 struct lpfc_mbx_sge sge;
1803 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001804 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001805 union lpfc_sli4_cfg_shdr *shdr;
1806 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001807
James Smart6fb120a2009-05-22 14:52:59 -04001808 /* Get the first SGE entry from the non-embedded DMA memory. This
1809 * routine only uses a single SGE.
1810 */
1811 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001812 if (unlikely(!mboxq->sge_array)) {
1813 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1814 "2524 Failed to get the non-embedded SGE "
1815 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001816 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001817 }
1818 virt_addr = mboxq->sge_array->addr[0];
1819
1820 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001821 lpfc_sli_pcimem_bcopy(shdr, shdr,
1822 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001823 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001824 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001825 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001826 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001827 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1828 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001829 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001830 "2726 READ_FCF_RECORD Indicates empty "
1831 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001832 else
1833 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001834 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001835 "with status x%x add_status x%x, "
1836 "mbx\n", shdr_status, shdr_add_status);
1837 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001838 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001839
1840 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001841 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1842 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1843 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001844 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001845 new_fcf_record = (struct fcf_record *)(virt_addr +
1846 sizeof(struct lpfc_mbx_read_fcf_tbl));
1847 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001848 offsetof(struct fcf_record, vlan_bitmap));
1849 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1850 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001851
James Smart0c9ab6f2010-02-26 14:15:57 -05001852 return new_fcf_record;
1853}
1854
1855/**
1856 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1857 * @phba: pointer to lpfc hba data structure.
1858 * @fcf_record: pointer to the fcf record.
1859 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1860 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1861 *
1862 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1863 * enabled.
1864 **/
1865static void
1866lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1867 struct fcf_record *fcf_record,
1868 uint16_t vlan_id,
1869 uint16_t next_fcf_index)
1870{
1871 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1872 "2764 READ_FCF_RECORD:\n"
1873 "\tFCF_Index : x%x\n"
1874 "\tFCF_Avail : x%x\n"
1875 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001876 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001877 "\tFIP_Priority : x%x\n"
1878 "\tMAC_Provider : x%x\n"
1879 "\tLowest VLANID : x%x\n"
1880 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1881 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1882 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1883 "\tNext_FCF_Index: x%x\n",
1884 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1885 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1886 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001887 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001888 fcf_record->fip_priority,
1889 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1890 vlan_id,
1891 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1892 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1893 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1894 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1895 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1896 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1897 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1898 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1899 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1900 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1901 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1902 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1903 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1904 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1905 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1906 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1907 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1908 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1909 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1910 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1911 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1912 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1913 next_fcf_index);
1914}
1915
1916/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001917 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1918 * @phba: pointer to lpfc hba data structure.
1919 * @fcf_rec: pointer to an existing FCF record.
1920 * @new_fcf_record: pointer to a new FCF record.
1921 * @new_vlan_id: vlan id from the new FCF record.
1922 *
1923 * This function performs matching test of a new FCF record against an existing
1924 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1925 * will not be used as part of the FCF record matching criteria.
1926 *
1927 * Returns true if all the fields matching, otherwise returns false.
1928 */
1929static bool
1930lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1931 struct lpfc_fcf_rec *fcf_rec,
1932 struct fcf_record *new_fcf_record,
1933 uint16_t new_vlan_id)
1934{
1935 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1936 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1937 return false;
1938 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1939 return false;
1940 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1941 return false;
1942 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1943 return false;
James Smart7d791df2011-07-22 18:37:52 -04001944 if (fcf_rec->priority != new_fcf_record->fip_priority)
1945 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001946 return true;
1947}
1948
1949/**
James Smarta93ff372010-10-22 11:06:08 -04001950 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1951 * @vport: Pointer to vport object.
1952 * @fcf_index: index to next fcf.
1953 *
1954 * This function processing the roundrobin fcf failover to next fcf index.
1955 * When this function is invoked, there will be a current fcf registered
1956 * for flogi.
1957 * Return: 0 for continue retrying flogi on currently registered fcf;
1958 * 1 for stop flogi on currently registered fcf;
1959 */
1960int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1961{
1962 struct lpfc_hba *phba = vport->phba;
1963 int rc;
1964
1965 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1966 spin_lock_irq(&phba->hbalock);
1967 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1968 spin_unlock_irq(&phba->hbalock);
1969 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1970 "2872 Devloss tmo with no eligible "
1971 "FCF, unregister in-use FCF (x%x) "
1972 "and rescan FCF table\n",
1973 phba->fcf.current_rec.fcf_indx);
1974 lpfc_unregister_fcf_rescan(phba);
1975 goto stop_flogi_current_fcf;
1976 }
1977 /* Mark the end to FLOGI roundrobin failover */
1978 phba->hba_flag &= ~FCF_RR_INPROG;
1979 /* Allow action to new fcf asynchronous event */
1980 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1981 spin_unlock_irq(&phba->hbalock);
1982 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1983 "2865 No FCF available, stop roundrobin FCF "
1984 "failover and change port state:x%x/x%x\n",
1985 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1986 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1987 goto stop_flogi_current_fcf;
1988 } else {
1989 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1990 "2794 Try FLOGI roundrobin FCF failover to "
1991 "(x%x)\n", fcf_index);
1992 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1993 if (rc)
1994 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1995 "2761 FLOGI roundrobin FCF failover "
1996 "failed (rc:x%x) to read FCF (x%x)\n",
1997 rc, phba->fcf.current_rec.fcf_indx);
1998 else
1999 goto stop_flogi_current_fcf;
2000 }
2001 return 0;
2002
2003stop_flogi_current_fcf:
2004 lpfc_can_disctmo(vport);
2005 return 1;
2006}
2007
2008/**
James Smart7d791df2011-07-22 18:37:52 -04002009 * lpfc_sli4_fcf_pri_list_del
2010 * @phba: pointer to lpfc hba data structure.
2011 * @fcf_index the index of the fcf record to delete
2012 * This routine checks the on list flag of the fcf_index to be deleted.
2013 * If it is one the list then it is removed from the list, and the flag
2014 * is cleared. This routine grab the hbalock before removing the fcf
2015 * record from the list.
2016 **/
2017static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2018 uint16_t fcf_index)
2019{
2020 struct lpfc_fcf_pri *new_fcf_pri;
2021
2022 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2023 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2024 "3058 deleting idx x%x pri x%x flg x%x\n",
2025 fcf_index, new_fcf_pri->fcf_rec.priority,
2026 new_fcf_pri->fcf_rec.flag);
2027 spin_lock_irq(&phba->hbalock);
2028 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2029 if (phba->fcf.current_rec.priority ==
2030 new_fcf_pri->fcf_rec.priority)
2031 phba->fcf.eligible_fcf_cnt--;
2032 list_del_init(&new_fcf_pri->list);
2033 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2034 }
2035 spin_unlock_irq(&phba->hbalock);
2036}
2037
2038/**
2039 * lpfc_sli4_set_fcf_flogi_fail
2040 * @phba: pointer to lpfc hba data structure.
2041 * @fcf_index the index of the fcf record to update
2042 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2043 * flag so the the round robin slection for the particular priority level
2044 * will try a different fcf record that does not have this bit set.
2045 * If the fcf record is re-read for any reason this flag is cleared brfore
2046 * adding it to the priority list.
2047 **/
2048void
2049lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2050{
2051 struct lpfc_fcf_pri *new_fcf_pri;
2052 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2053 spin_lock_irq(&phba->hbalock);
2054 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2055 spin_unlock_irq(&phba->hbalock);
2056}
2057
2058/**
2059 * lpfc_sli4_fcf_pri_list_add
2060 * @phba: pointer to lpfc hba data structure.
2061 * @fcf_index the index of the fcf record to add
2062 * This routine checks the priority of the fcf_index to be added.
2063 * If it is a lower priority than the current head of the fcf_pri list
2064 * then it is added to the list in the right order.
2065 * If it is the same priority as the current head of the list then it
2066 * is added to the head of the list and its bit in the rr_bmask is set.
2067 * If the fcf_index to be added is of a higher priority than the current
2068 * head of the list then the rr_bmask is cleared, its bit is set in the
2069 * rr_bmask and it is added to the head of the list.
2070 * returns:
2071 * 0=success 1=failure
2072 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002073static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2074 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002075 struct fcf_record *new_fcf_record)
2076{
2077 uint16_t current_fcf_pri;
2078 uint16_t last_index;
2079 struct lpfc_fcf_pri *fcf_pri;
2080 struct lpfc_fcf_pri *next_fcf_pri;
2081 struct lpfc_fcf_pri *new_fcf_pri;
2082 int ret;
2083
2084 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2085 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2086 "3059 adding idx x%x pri x%x flg x%x\n",
2087 fcf_index, new_fcf_record->fip_priority,
2088 new_fcf_pri->fcf_rec.flag);
2089 spin_lock_irq(&phba->hbalock);
2090 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2091 list_del_init(&new_fcf_pri->list);
2092 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2093 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2094 if (list_empty(&phba->fcf.fcf_pri_list)) {
2095 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2096 ret = lpfc_sli4_fcf_rr_index_set(phba,
2097 new_fcf_pri->fcf_rec.fcf_index);
2098 goto out;
2099 }
2100
2101 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2102 LPFC_SLI4_FCF_TBL_INDX_MAX);
2103 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2104 ret = 0; /* Empty rr list */
2105 goto out;
2106 }
2107 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2108 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2109 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2110 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2111 memset(phba->fcf.fcf_rr_bmask, 0,
2112 sizeof(*phba->fcf.fcf_rr_bmask));
2113 /* fcfs_at_this_priority_level = 1; */
2114 phba->fcf.eligible_fcf_cnt = 1;
2115 } else
2116 /* fcfs_at_this_priority_level++; */
2117 phba->fcf.eligible_fcf_cnt++;
2118 ret = lpfc_sli4_fcf_rr_index_set(phba,
2119 new_fcf_pri->fcf_rec.fcf_index);
2120 goto out;
2121 }
2122
2123 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2124 &phba->fcf.fcf_pri_list, list) {
2125 if (new_fcf_pri->fcf_rec.priority <=
2126 fcf_pri->fcf_rec.priority) {
2127 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2128 list_add(&new_fcf_pri->list,
2129 &phba->fcf.fcf_pri_list);
2130 else
2131 list_add(&new_fcf_pri->list,
2132 &((struct lpfc_fcf_pri *)
2133 fcf_pri->list.prev)->list);
2134 ret = 0;
2135 goto out;
2136 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2137 || new_fcf_pri->fcf_rec.priority <
2138 next_fcf_pri->fcf_rec.priority) {
2139 list_add(&new_fcf_pri->list, &fcf_pri->list);
2140 ret = 0;
2141 goto out;
2142 }
2143 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2144 continue;
2145
2146 }
2147 ret = 1;
2148out:
2149 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2150 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2151 spin_unlock_irq(&phba->hbalock);
2152 return ret;
2153}
2154
2155/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002156 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2157 * @phba: pointer to lpfc hba data structure.
2158 * @mboxq: pointer to mailbox object.
2159 *
2160 * This function iterates through all the fcf records available in
2161 * HBA and chooses the optimal FCF record for discovery. After finding
2162 * the FCF for discovery it registers the FCF record and kicks start
2163 * discovery.
2164 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2165 * use an FCF record which matches fabric name and mac address of the
2166 * currently used FCF record.
2167 * If the driver supports only one FCF, it will try to use the FCF record
2168 * used by BOOT_BIOS.
2169 */
2170void
2171lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2172{
2173 struct fcf_record *new_fcf_record;
2174 uint32_t boot_flag, addr_mode;
2175 uint16_t fcf_index, next_fcf_index;
2176 struct lpfc_fcf_rec *fcf_rec = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08002177 uint16_t vlan_id = LPFC_FCOE_NULL_VID;
James Smart999d8132010-03-15 11:24:56 -04002178 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002179 int rc;
2180
2181 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002182 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002183 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2184 return;
2185 }
2186
2187 /* Parse the FCF record from the non-embedded mailbox command */
2188 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2189 &next_fcf_index);
2190 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002191 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002192 "2765 Mailbox command READ_FCF_RECORD "
2193 "failed to retrieve a FCF record.\n");
2194 /* Let next new FCF event trigger fast failover */
2195 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002196 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002197 spin_unlock_irq(&phba->hbalock);
2198 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2199 return;
2200 }
2201
2202 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002203 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2204 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002205
2206 /* Log the FCF record information if turned on */
2207 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2208 next_fcf_index);
2209
James Smart6fb120a2009-05-22 14:52:59 -04002210 /*
2211 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002212 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002213 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002214 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002215 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002216 lpfc_sli4_fcf_pri_list_del(phba,
2217 bf_get(lpfc_fcf_record_fcf_index,
2218 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002219 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002220 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002221 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002222 bf_get(lpfc_fcf_record_fcf_index,
2223 new_fcf_record),
2224 bf_get(lpfc_fcf_record_fcf_avail,
2225 new_fcf_record),
2226 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002227 new_fcf_record),
2228 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002229 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002230 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2231 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2232 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002233 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2234 phba->fcf.current_rec.fcf_indx) {
2235 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2236 "2862 FCF (x%x) matches property "
2237 "of in-use FCF (x%x)\n",
2238 bf_get(lpfc_fcf_record_fcf_index,
2239 new_fcf_record),
2240 phba->fcf.current_rec.fcf_indx);
2241 goto read_next_fcf;
2242 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002243 /*
2244 * In case the current in-use FCF record becomes
2245 * invalid/unavailable during FCF discovery that
2246 * was not triggered by fast FCF failover process,
2247 * treat it as fast FCF failover.
2248 */
2249 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2250 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2251 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2252 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002253 "(x%x), enter FCF failover "
2254 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002255 phba->fcf.current_rec.fcf_indx);
2256 spin_lock_irq(&phba->hbalock);
2257 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2258 spin_unlock_irq(&phba->hbalock);
2259 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2260 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2261 LPFC_FCOE_FCF_GET_FIRST);
2262 return;
2263 }
2264 }
James Smart6fb120a2009-05-22 14:52:59 -04002265 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002266 } else {
2267 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002268 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2269 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002270 if (rc)
2271 goto read_next_fcf;
2272 }
2273
James Smart6fb120a2009-05-22 14:52:59 -04002274 /*
2275 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002276 * FCF record for the discovery. The condition that a rescan
2277 * matches the in-use FCF record: fabric name, switch name, mac
2278 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002279 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002280 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002281 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002282 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2283 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002284 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002285 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2286 phba->fcf.current_rec.fcf_indx) {
2287 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2288 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2289 /* Stop FCF redisc wait timer */
2290 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2291 phba);
2292 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2293 /* Fast failover, mark completed */
2294 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2295 spin_unlock_irq(&phba->hbalock);
2296 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2297 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002298 "FCF (x%x), port_state:x%x, "
2299 "fc_flag:x%x\n",
2300 phba->fcf.current_rec.fcf_indx,
2301 phba->pport->port_state,
2302 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002303 goto out;
2304 } else
2305 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2306 "2863 New FCF (x%x) matches "
2307 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002308 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002309 new_fcf_record),
2310 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002311 }
James Smartecfd03c2010-02-12 14:41:27 -05002312 /*
2313 * Read next FCF record from HBA searching for the matching
2314 * with in-use record only if not during the fast failover
2315 * period. In case of fast failover period, it shall try to
2316 * determine whether the FCF record just read should be the
2317 * next candidate.
2318 */
2319 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002320 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002321 goto read_next_fcf;
2322 }
James Smart6fb120a2009-05-22 14:52:59 -04002323 }
James Smartecfd03c2010-02-12 14:41:27 -05002324 /*
2325 * Update on failover FCF record only if it's in FCF fast-failover
2326 * period; otherwise, update on current FCF record.
2327 */
James Smartfc2b9892010-02-26 14:15:29 -05002328 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2329 fcf_rec = &phba->fcf.failover_rec;
2330 else
James Smartecfd03c2010-02-12 14:41:27 -05002331 fcf_rec = &phba->fcf.current_rec;
2332
James Smart6fb120a2009-05-22 14:52:59 -04002333 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2334 /*
James Smartecfd03c2010-02-12 14:41:27 -05002335 * If the driver FCF record does not have boot flag
2336 * set and new hba fcf record has boot flag set, use
2337 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002338 */
James Smartecfd03c2010-02-12 14:41:27 -05002339 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2340 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002341 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2342 "2837 Update current FCF record "
2343 "(x%x) with new FCF record (x%x)\n",
2344 fcf_rec->fcf_indx,
2345 bf_get(lpfc_fcf_record_fcf_index,
2346 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002347 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2348 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002349 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002350 goto read_next_fcf;
2351 }
2352 /*
James Smartecfd03c2010-02-12 14:41:27 -05002353 * If the driver FCF record has boot flag set and the
2354 * new hba FCF record does not have boot flag, read
2355 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002356 */
James Smartecfd03c2010-02-12 14:41:27 -05002357 if (!boot_flag && (fcf_rec->flag & 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 new hba FCF record has lower priority value
2363 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002364 */
James Smartfc2b9892010-02-26 14:15:29 -05002365 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002366 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002367 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2368 "2838 Update current FCF record "
2369 "(x%x) with new FCF record (x%x)\n",
2370 fcf_rec->fcf_indx,
2371 bf_get(lpfc_fcf_record_fcf_index,
2372 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002373 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2374 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002375 /* Reset running random FCF selection count */
2376 phba->fcf.eligible_fcf_cnt = 1;
2377 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2378 /* Update running random FCF selection count */
2379 phba->fcf.eligible_fcf_cnt++;
2380 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2381 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002382 if (select_new_fcf) {
2383 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2384 "2839 Update current FCF record "
2385 "(x%x) with new FCF record (x%x)\n",
2386 fcf_rec->fcf_indx,
2387 bf_get(lpfc_fcf_record_fcf_index,
2388 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002389 /* Choose the new FCF by random selection */
2390 __lpfc_update_fcf_record(phba, fcf_rec,
2391 new_fcf_record,
2392 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002393 }
James Smart6fb120a2009-05-22 14:52:59 -04002394 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002395 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002396 goto read_next_fcf;
2397 }
2398 /*
James Smartecfd03c2010-02-12 14:41:27 -05002399 * This is the first suitable FCF record, choose this record for
2400 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002401 */
James Smartecfd03c2010-02-12 14:41:27 -05002402 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002403 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002404 "2840 Update initial FCF candidate "
2405 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002406 bf_get(lpfc_fcf_record_fcf_index,
2407 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002408 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2409 addr_mode, vlan_id, (boot_flag ?
2410 BOOT_ENABLE : 0));
2411 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002412 /* Setup initial running random FCF selection count */
2413 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002414 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002415 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002416 goto read_next_fcf;
2417
2418read_next_fcf:
2419 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002420 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2421 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2422 /*
2423 * Case of FCF fast failover scan
2424 */
2425
2426 /*
2427 * It has not found any suitable FCF record, cancel
2428 * FCF scan inprogress, and do nothing
2429 */
2430 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002431 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002432 "2782 No suitable FCF found: "
2433 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002434 phba->fcoe_eventtag_at_fcf_scan,
2435 bf_get(lpfc_fcf_record_fcf_index,
2436 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002437 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002438 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2439 phba->hba_flag &= ~FCF_TS_INPROG;
2440 spin_unlock_irq(&phba->hbalock);
2441 /* Unregister in-use FCF and rescan */
2442 lpfc_printf_log(phba, KERN_INFO,
2443 LOG_FIP,
2444 "2864 On devloss tmo "
2445 "unreg in-use FCF and "
2446 "rescan FCF table\n");
2447 lpfc_unregister_fcf_rescan(phba);
2448 return;
2449 }
2450 /*
2451 * Let next new FCF event trigger fast failover
2452 */
2453 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002454 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002455 return;
2456 }
2457 /*
2458 * It has found a suitable FCF record that is not
2459 * the same as in-use FCF record, unregister the
2460 * in-use FCF record, replace the in-use FCF record
2461 * with the new FCF record, mark FCF fast failover
2462 * completed, and then start register the new FCF
2463 * record.
2464 */
2465
James Smart0c9ab6f2010-02-26 14:15:57 -05002466 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002467 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002468
2469 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002470 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002471 "2842 Replace in-use FCF (x%x) "
2472 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002473 phba->fcf.current_rec.fcf_indx,
2474 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002475 memcpy(&phba->fcf.current_rec,
2476 &phba->fcf.failover_rec,
2477 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002478 /*
2479 * Mark the fast FCF failover rediscovery completed
2480 * and the start of the first round of the roundrobin
2481 * FCF failover.
2482 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002483 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002484 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002485 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002486 /* Register to the new FCF record */
2487 lpfc_register_fcf(phba);
2488 } else {
2489 /*
2490 * In case of transaction period to fast FCF failover,
2491 * do nothing when search to the end of the FCF table.
2492 */
2493 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2494 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2495 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002496
James Smart7d791df2011-07-22 18:37:52 -04002497 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2498 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002499 /*
2500 * In case the current in-use FCF record no
2501 * longer existed during FCF discovery that
2502 * was not triggered by fast FCF failover
2503 * process, treat it as fast FCF failover.
2504 */
2505 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2506 "2841 In-use FCF record (x%x) "
2507 "not reported, entering fast "
2508 "FCF failover mode scanning.\n",
2509 phba->fcf.current_rec.fcf_indx);
2510 spin_lock_irq(&phba->hbalock);
2511 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2512 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002513 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2514 LPFC_FCOE_FCF_GET_FIRST);
2515 return;
2516 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002517 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002518 lpfc_register_fcf(phba);
2519 }
2520 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002521 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002522 return;
2523
2524out:
2525 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2526 lpfc_register_fcf(phba);
2527
2528 return;
2529}
2530
2531/**
James Smarta93ff372010-10-22 11:06:08 -04002532 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002533 * @phba: pointer to lpfc hba data structure.
2534 * @mboxq: pointer to mailbox object.
2535 *
James Smarta93ff372010-10-22 11:06:08 -04002536 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002537 * read FCF record mailbox command from the eligible FCF record bmask for
2538 * performing the failover. If the FCF read back is not valid/available, it
2539 * fails through to retrying FLOGI to the currently registered FCF again.
2540 * Otherwise, if the FCF read back is valid and available, it will set the
2541 * newly read FCF record to the failover FCF record, unregister currently
2542 * registered FCF record, copy the failover FCF record to the current
2543 * FCF record, and then register the current FCF record before proceeding
2544 * to trying FLOGI on the new failover FCF.
2545 */
2546void
2547lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2548{
2549 struct fcf_record *new_fcf_record;
2550 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002551 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002552 uint16_t current_fcf_index;
2553 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002554 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002555
James Smarta93ff372010-10-22 11:06:08 -04002556 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002557 if (phba->link_state < LPFC_LINK_UP) {
2558 spin_lock_irq(&phba->hbalock);
2559 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002560 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002561 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002562 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002563 }
2564
2565 /* Parse the FCF record from the non-embedded mailbox command */
2566 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2567 &next_fcf_index);
2568 if (!new_fcf_record) {
2569 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2570 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002571 "failed to retrieve a FCF record. "
2572 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2573 phba->fcf.fcf_flag);
2574 lpfc_unregister_fcf_rescan(phba);
2575 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002576 }
2577
2578 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002579 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2580 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002581
2582 /* Log the FCF record information if turned on */
2583 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2584 next_fcf_index);
2585
James Smarta93ff372010-10-22 11:06:08 -04002586 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2587 if (!rc) {
2588 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2589 "2848 Remove ineligible FCF (x%x) from "
2590 "from roundrobin bmask\n", fcf_index);
2591 /* Clear roundrobin bmask bit for ineligible FCF */
2592 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2593 /* Perform next round of roundrobin FCF failover */
2594 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2595 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2596 if (rc)
2597 goto out;
2598 goto error_out;
2599 }
2600
2601 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2602 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2603 "2760 Perform FLOGI roundrobin FCF failover: "
2604 "FCF (x%x) back to FCF (x%x)\n",
2605 phba->fcf.current_rec.fcf_indx, fcf_index);
2606 /* Wait 500 ms before retrying FLOGI to current FCF */
2607 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002608 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002609 goto out;
2610 }
2611
James Smart0c9ab6f2010-02-26 14:15:57 -05002612 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002613 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002614 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2615 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002616 spin_lock_irq(&phba->hbalock);
2617 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2618 new_fcf_record, addr_mode, vlan_id,
2619 (boot_flag ? BOOT_ENABLE : 0));
2620 spin_unlock_irq(&phba->hbalock);
2621
2622 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2623
2624 /* Unregister the current in-use FCF record */
2625 lpfc_unregister_fcf(phba);
2626
2627 /* Replace in-use record with the new record */
2628 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2629 sizeof(struct lpfc_fcf_rec));
2630
2631 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002632 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2633 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002634
James Smarta93ff372010-10-22 11:06:08 -04002635error_out:
2636 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002637out:
2638 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002639}
2640
2641/**
2642 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2643 * @phba: pointer to lpfc hba data structure.
2644 * @mboxq: pointer to mailbox object.
2645 *
2646 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002647 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002648 * failover when a new FCF event happened. If the FCF read back is
2649 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002650 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002651 */
2652void
2653lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2654{
2655 struct fcf_record *new_fcf_record;
2656 uint32_t boot_flag, addr_mode;
2657 uint16_t fcf_index, next_fcf_index;
2658 uint16_t vlan_id;
2659 int rc;
2660
2661 /* If link state is not up, no need to proceed */
2662 if (phba->link_state < LPFC_LINK_UP)
2663 goto out;
2664
2665 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002666 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002667 goto out;
2668
2669 /* Parse the FCF record from the non-embedded mailbox command */
2670 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2671 &next_fcf_index);
2672 if (!new_fcf_record) {
2673 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2674 "2767 Mailbox command READ_FCF_RECORD "
2675 "failed to retrieve a FCF record.\n");
2676 goto out;
2677 }
2678
2679 /* Check the connection list for eligibility */
2680 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2681 &addr_mode, &vlan_id);
2682
2683 /* Log the FCF record information if turned on */
2684 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2685 next_fcf_index);
2686
2687 if (!rc)
2688 goto out;
2689
2690 /* Update the eligible FCF record index bmask */
2691 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002692
2693 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002694
2695out:
2696 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2697}
2698
2699/**
James Smart76a95d72010-11-20 23:11:48 -05002700 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2701 * @phba: pointer to lpfc hba data structure.
2702 * @mboxq: pointer to mailbox data structure.
2703 *
2704 * This function handles completion of init vfi mailbox command.
2705 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002706static void
James Smart76a95d72010-11-20 23:11:48 -05002707lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2708{
2709 struct lpfc_vport *vport = mboxq->vport;
2710
James Smartdf9e1b52011-12-13 13:22:17 -05002711 /*
2712 * VFI not supported on interface type 0, just do the flogi
2713 * Also continue if the VFI is in use - just use the same one.
2714 */
2715 if (mboxq->u.mb.mbxStatus &&
2716 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2717 LPFC_SLI_INTF_IF_TYPE_0) &&
2718 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002719 lpfc_printf_vlog(vport, KERN_ERR,
2720 LOG_MBOX,
2721 "2891 Init VFI mailbox failed 0x%x\n",
2722 mboxq->u.mb.mbxStatus);
2723 mempool_free(mboxq, phba->mbox_mem_pool);
2724 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2725 return;
2726 }
James Smart73d91e52011-10-10 21:32:10 -04002727
James Smart76a95d72010-11-20 23:11:48 -05002728 lpfc_initial_flogi(vport);
2729 mempool_free(mboxq, phba->mbox_mem_pool);
2730 return;
2731}
2732
2733/**
2734 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2735 * @vport: pointer to lpfc_vport data structure.
2736 *
2737 * This function issue a init_vfi mailbox command to initialize the VFI and
2738 * VPI for the physical port.
2739 */
2740void
2741lpfc_issue_init_vfi(struct lpfc_vport *vport)
2742{
2743 LPFC_MBOXQ_t *mboxq;
2744 int rc;
2745 struct lpfc_hba *phba = vport->phba;
2746
2747 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2748 if (!mboxq) {
2749 lpfc_printf_vlog(vport, KERN_ERR,
2750 LOG_MBOX, "2892 Failed to allocate "
2751 "init_vfi mailbox\n");
2752 return;
2753 }
2754 lpfc_init_vfi(mboxq, vport);
2755 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2756 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2757 if (rc == MBX_NOT_FINISHED) {
2758 lpfc_printf_vlog(vport, KERN_ERR,
2759 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2760 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2761 }
2762}
2763
2764/**
James Smart1c6834a2009-07-19 10:01:26 -04002765 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2766 * @phba: pointer to lpfc hba data structure.
2767 * @mboxq: pointer to mailbox data structure.
2768 *
2769 * This function handles completion of init vpi mailbox command.
2770 */
James Smart695a8142010-01-26 23:08:03 -05002771void
James Smart1c6834a2009-07-19 10:01:26 -04002772lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2773{
2774 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002775 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002776 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2777
James Smart1c6834a2009-07-19 10:01:26 -04002778 if (mboxq->u.mb.mbxStatus) {
2779 lpfc_printf_vlog(vport, KERN_ERR,
2780 LOG_MBOX,
2781 "2609 Init VPI mailbox failed 0x%x\n",
2782 mboxq->u.mb.mbxStatus);
2783 mempool_free(mboxq, phba->mbox_mem_pool);
2784 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2785 return;
2786 }
James Smart72100cc2010-02-12 14:43:01 -05002787 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002788 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002789 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002790
James Smart695a8142010-01-26 23:08:03 -05002791 /* If this port is physical port or FDISC is done, do reg_vpi */
2792 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2793 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2794 if (!ndlp)
2795 lpfc_printf_vlog(vport, KERN_ERR,
2796 LOG_DISCOVERY,
2797 "2731 Cannot find fabric "
2798 "controller node\n");
2799 else
2800 lpfc_register_new_vport(phba, vport, ndlp);
2801 mempool_free(mboxq, phba->mbox_mem_pool);
2802 return;
2803 }
2804
James Smart1c6834a2009-07-19 10:01:26 -04002805 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2806 lpfc_initial_fdisc(vport);
2807 else {
2808 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002809 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2810 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002811 }
James Smart695a8142010-01-26 23:08:03 -05002812 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002813 return;
2814}
2815
2816/**
James Smartecfd03c2010-02-12 14:41:27 -05002817 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2818 * @vport: pointer to lpfc_vport data structure.
2819 *
2820 * This function issue a init_vpi mailbox command to initialize
2821 * VPI for the vport.
2822 */
2823void
2824lpfc_issue_init_vpi(struct lpfc_vport *vport)
2825{
2826 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002827 int rc, vpi;
2828
2829 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2830 vpi = lpfc_alloc_vpi(vport->phba);
2831 if (!vpi) {
2832 lpfc_printf_vlog(vport, KERN_ERR,
2833 LOG_MBOX,
2834 "3303 Failed to obtain vport vpi\n");
2835 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2836 return;
2837 }
2838 vport->vpi = vpi;
2839 }
James Smartecfd03c2010-02-12 14:41:27 -05002840
2841 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2842 if (!mboxq) {
2843 lpfc_printf_vlog(vport, KERN_ERR,
2844 LOG_MBOX, "2607 Failed to allocate "
2845 "init_vpi mailbox\n");
2846 return;
2847 }
2848 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2849 mboxq->vport = vport;
2850 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2851 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2852 if (rc == MBX_NOT_FINISHED) {
2853 lpfc_printf_vlog(vport, KERN_ERR,
2854 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2855 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2856 }
2857}
2858
2859/**
James Smart6fb120a2009-05-22 14:52:59 -04002860 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2861 * @phba: pointer to lpfc hba data structure.
2862 *
2863 * This function loops through the list of vports on the @phba and issues an
2864 * FDISC if possible.
2865 */
2866void
2867lpfc_start_fdiscs(struct lpfc_hba *phba)
2868{
2869 struct lpfc_vport **vports;
2870 int i;
2871
2872 vports = lpfc_create_vport_work_array(phba);
2873 if (vports != NULL) {
2874 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2875 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2876 continue;
2877 /* There are no vpi for this vport */
2878 if (vports[i]->vpi > phba->max_vpi) {
2879 lpfc_vport_set_state(vports[i],
2880 FC_VPORT_FAILED);
2881 continue;
2882 }
James Smart76a95d72010-11-20 23:11:48 -05002883 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002884 lpfc_vport_set_state(vports[i],
2885 FC_VPORT_LINKDOWN);
2886 continue;
2887 }
James Smart1c6834a2009-07-19 10:01:26 -04002888 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002889 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002890 continue;
2891 }
James Smart6fb120a2009-05-22 14:52:59 -04002892 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2893 lpfc_initial_fdisc(vports[i]);
2894 else {
2895 lpfc_vport_set_state(vports[i],
2896 FC_VPORT_NO_FABRIC_SUPP);
2897 lpfc_printf_vlog(vports[i], KERN_ERR,
2898 LOG_ELS,
2899 "0259 No NPIV "
2900 "Fabric support\n");
2901 }
2902 }
2903 }
2904 lpfc_destroy_vport_work_array(phba, vports);
2905}
2906
2907void
2908lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2909{
2910 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2911 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002912 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002913
James Smartf5eca9b2012-03-01 22:37:54 -05002914 /*
2915 * VFI not supported for interface type 0, so ignore any mailbox
2916 * error (except VFI in use) and continue with the discovery.
2917 */
2918 if (mboxq->u.mb.mbxStatus &&
2919 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2920 LPFC_SLI_INTF_IF_TYPE_0) &&
2921 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002922 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2923 "2018 REG_VFI mbxStatus error x%x "
2924 "HBA state x%x\n",
2925 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002926 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002927 /* FLOGI failed, use loop map to make discovery list */
2928 lpfc_disc_list_loopmap(vport);
2929 /* Start discovery */
2930 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002931 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002932 }
2933 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002934 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002935 }
James Smartae05ebe2013-03-01 16:35:38 -05002936
James Smarte74c03c2013-04-17 20:15:19 -04002937 /* If the VFI is already registered, there is nothing else to do
2938 * Unless this was a VFI update and we are in PT2PT mode, then
2939 * we should drop through to set the port state to ready.
2940 */
James Smartae05ebe2013-03-01 16:35:38 -05002941 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002942 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2943 vport->fc_flag & FC_PT2PT))
2944 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002945
James Smartc8685952009-11-18 15:39:16 -05002946 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002947 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002948 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002949 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002950 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002951 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002952 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002953
James Smart1b511972011-12-13 13:23:09 -05002954 /* In case SLI4 FC loopback test, we are ready */
2955 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2956 (phba->link_flag & LS_LOOPBACK_MODE)) {
2957 phba->link_state = LPFC_HBA_READY;
2958 goto out_free_mem;
2959 }
2960
James Smarte74c03c2013-04-17 20:15:19 -04002961 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2962 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2963 "alpacnt:%d LinkState:%x topology:%x\n",
2964 vport->port_state, vport->fc_flag, vport->fc_myDID,
2965 vport->phba->alpa_map[0],
2966 phba->link_state, phba->fc_topology);
2967
James Smart6fb120a2009-05-22 14:52:59 -04002968 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002969 /*
2970 * For private loop or for NPort pt2pt,
2971 * just start discovery and we are done.
2972 */
2973 if ((vport->fc_flag & FC_PT2PT) ||
2974 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2975 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2976
James Smart76a95d72010-11-20 23:11:48 -05002977 /* Use loop map to make discovery list */
2978 lpfc_disc_list_loopmap(vport);
2979 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002980 if (vport->fc_flag & FC_PT2PT)
2981 vport->port_state = LPFC_VPORT_READY;
2982 else
2983 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002984 } else {
2985 lpfc_start_fdiscs(phba);
2986 lpfc_do_scr_ns_plogi(phba, vport);
2987 }
James Smart6fb120a2009-05-22 14:52:59 -04002988 }
2989
James Smart1b511972011-12-13 13:23:09 -05002990out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002991 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002992 if (dmabuf) {
2993 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2994 kfree(dmabuf);
2995 }
James Smart6fb120a2009-05-22 14:52:59 -04002996 return;
2997}
2998
2999static void
James Smart2e0fef82007-06-17 19:56:36 -05003000lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003001{
James Smart6fb120a2009-05-22 14:52:59 -04003002 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003003 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05003004 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04003005 struct serv_parm *sp = &vport->fc_sparam;
3006 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05003007
3008 /* Check for error */
3009 if (mb->mbxStatus) {
3010 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003011 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3012 "0319 READ_SPARAM mbxStatus error x%x "
3013 "hba state x%x>\n",
3014 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003015 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003016 goto out;
3017 }
3018
James Smart2e0fef82007-06-17 19:56:36 -05003019 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003020 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003021
3022 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3023 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3024 ed_tov = (ed_tov + 999999) / 1000000;
3025
3026 phba->fc_edtov = ed_tov;
3027 phba->fc_ratov = (2 * ed_tov) / 1000;
3028 if (phba->fc_ratov < FF_DEF_RATOV) {
3029 /* RA_TOV should be atleast 10sec for initial flogi */
3030 phba->fc_ratov = FF_DEF_RATOV;
3031 }
3032
James Smart05580562011-05-24 11:40:48 -04003033 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003034 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3035 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3036 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3037 }
3038
dea31012005-04-17 16:05:31 -05003039 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3040 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003041 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003042 return;
3043
3044out:
3045 pmb->context1 = NULL;
3046 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3047 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003048 lpfc_issue_clear_la(phba, vport);
3049 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003050 return;
3051}
3052
3053static void
James Smart76a95d72010-11-20 23:11:48 -05003054lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003055{
James Smart92d7f7b2007-06-17 19:56:38 -05003056 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003057 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003058 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003059 int i;
James Smart14691152006-12-02 13:34:28 -05003060 struct lpfc_dmabuf *mp;
3061 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003062 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003063 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003064
James Smart92d7f7b2007-06-17 19:56:38 -05003065 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003066 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3067
3068 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3069 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3070 case LPFC_LINK_SPEED_1GHZ:
3071 case LPFC_LINK_SPEED_2GHZ:
3072 case LPFC_LINK_SPEED_4GHZ:
3073 case LPFC_LINK_SPEED_8GHZ:
3074 case LPFC_LINK_SPEED_10GHZ:
3075 case LPFC_LINK_SPEED_16GHZ:
3076 case LPFC_LINK_SPEED_32GHZ:
3077 break;
3078 default:
3079 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3080 break;
3081 }
dea31012005-04-17 16:05:31 -05003082 }
3083
James Smarte74c03c2013-04-17 20:15:19 -04003084 if (phba->fc_topology &&
3085 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3086 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3087 "3314 Toplogy changed was 0x%x is 0x%x\n",
3088 phba->fc_topology,
3089 bf_get(lpfc_mbx_read_top_topology, la));
3090 phba->fc_topology_changed = 1;
3091 }
3092
James Smart76a95d72010-11-20 23:11:48 -05003093 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003094 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003095
James Smartdf9e1b52011-12-13 13:22:17 -05003096 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003097 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003098 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003099
James Smart6a9c52c2009-10-02 15:16:51 -04003100 /* if npiv is enabled and this adapter supports npiv log
3101 * a message that npiv is not supported in this topology
3102 */
3103 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003104 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3105 "1309 Link Up Event npiv not supported in loop "
3106 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003107 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003108 if (bf_get(lpfc_mbx_read_top_il, la))
3109 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003110
James Smart76a95d72010-11-20 23:11:48 -05003111 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3112 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003113
3114 if (i == 0) {
3115 phba->alpa_map[0] = 0;
3116 } else {
James Smarte8b62012007-08-02 11:10:09 -04003117 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003118 int numalpa, j, k;
3119 union {
3120 uint8_t pamap[16];
3121 struct {
3122 uint32_t wd1;
3123 uint32_t wd2;
3124 uint32_t wd3;
3125 uint32_t wd4;
3126 } pa;
3127 } un;
3128 numalpa = phba->alpa_map[0];
3129 j = 0;
3130 while (j < numalpa) {
3131 memset(un.pamap, 0, 16);
3132 for (k = 1; j < numalpa; k++) {
3133 un.pamap[k - 1] =
3134 phba->alpa_map[j + 1];
3135 j++;
3136 if (k == 16)
3137 break;
3138 }
3139 /* Link Up Event ALPA map */
3140 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003141 KERN_WARNING,
3142 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003143 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003144 "ALPA map Data: x%x "
3145 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003146 un.pa.wd1, un.pa.wd2,
3147 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003148 }
3149 }
3150 }
3151 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003152 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003153 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003154 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003155 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3156 }
James Smart2e0fef82007-06-17 19:56:36 -05003157 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003158 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003159 }
James Smart92d7f7b2007-06-17 19:56:38 -05003160 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003161
James Smart38c20672013-03-01 16:37:44 -05003162 if (fc_flags) {
3163 spin_lock_irq(shost->host_lock);
3164 vport->fc_flag |= fc_flags;
3165 spin_unlock_irq(shost->host_lock);
3166 }
3167
dea31012005-04-17 16:05:31 -05003168 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003169 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3170 if (!sparam_mbox)
3171 goto out;
3172
3173 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3174 if (rc) {
3175 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3176 goto out;
3177 }
3178 sparam_mbox->vport = vport;
3179 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3180 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3181 if (rc == MBX_NOT_FINISHED) {
3182 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3183 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3184 kfree(mp);
3185 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3186 goto out;
dea31012005-04-17 16:05:31 -05003187 }
3188
James Smart76a95d72010-11-20 23:11:48 -05003189 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003190 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3191 if (!cfglink_mbox)
3192 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003193 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003194 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003195 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003196 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003197 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003198 if (rc == MBX_NOT_FINISHED) {
3199 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3200 goto out;
3201 }
3202 } else {
James Smart32b97932009-07-19 10:01:21 -04003203 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003204 /*
3205 * Add the driver's default FCF record at FCF index 0 now. This
3206 * is phase 1 implementation that support FCF index 0 and driver
3207 * defaults.
3208 */
James Smart45ed1192009-10-02 15:17:02 -04003209 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003210 fcf_record = kzalloc(sizeof(struct fcf_record),
3211 GFP_KERNEL);
3212 if (unlikely(!fcf_record)) {
3213 lpfc_printf_log(phba, KERN_ERR,
3214 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003215 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003216 "fcf record\n");
3217 rc = -ENODEV;
3218 goto out;
3219 }
3220
3221 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3222 LPFC_FCOE_FCF_DEF_INDEX);
3223 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3224 if (unlikely(rc)) {
3225 lpfc_printf_log(phba, KERN_ERR,
3226 LOG_MBOX | LOG_SLI,
3227 "2013 Could not manually add FCF "
3228 "record 0, status %d\n", rc);
3229 rc = -ENODEV;
3230 kfree(fcf_record);
3231 goto out;
3232 }
3233 kfree(fcf_record);
3234 }
3235 /*
3236 * The driver is expected to do FIP/FCF. Call the port
3237 * and get the FCF Table.
3238 */
James Smart32b97932009-07-19 10:01:21 -04003239 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003240 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003241 spin_unlock_irq(&phba->hbalock);
3242 return;
3243 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003244 /* This is the initial FCF discovery scan */
3245 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003246 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003247 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3248 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003249 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3250 LPFC_FCOE_FCF_GET_FIRST);
3251 if (rc) {
3252 spin_lock_irq(&phba->hbalock);
3253 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3254 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003255 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003256 }
James Smart38b92ef2010-08-04 16:11:39 -04003257 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003258 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003259 }
James Smart6fb120a2009-05-22 14:52:59 -04003260
3261 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003262out:
3263 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003264 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3265 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3266 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003267 lpfc_issue_clear_la(phba, vport);
3268 return;
dea31012005-04-17 16:05:31 -05003269}
3270
3271static void
James Smart84774a42008-08-24 21:50:06 -04003272lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003273{
dea31012005-04-17 16:05:31 -05003274 uint32_t control;
3275 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003276 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003277 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003278 if (phba->sli_rev <= LPFC_SLI_REV3) {
3279 control = readl(phba->HCregaddr);
3280 control |= HC_LAINT_ENA;
3281 writel(control, phba->HCregaddr);
3282 readl(phba->HCregaddr); /* flush */
3283 }
James Smart2e0fef82007-06-17 19:56:36 -05003284 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003285}
3286
James Smart84774a42008-08-24 21:50:06 -04003287static void
3288lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3289{
3290 lpfc_linkdown(phba);
3291 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003292 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003293 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3294}
3295
3296
dea31012005-04-17 16:05:31 -05003297/*
James Smart76a95d72010-11-20 23:11:48 -05003298 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003299 * command upon completion. It is setup in the LPFC_MBOXQ
3300 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003301 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003302 */
3303void
James Smart76a95d72010-11-20 23:11:48 -05003304lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003305{
James Smart2e0fef82007-06-17 19:56:36 -05003306 struct lpfc_vport *vport = pmb->vport;
3307 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003308 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003309 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003310 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003311 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3312
James Smart0d2b6b82008-06-14 22:52:47 -04003313 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003314 pring = lpfc_phba_elsring(phba);
3315 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3316
dea31012005-04-17 16:05:31 -05003317 /* Check for error */
3318 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003319 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003320 "1307 READ_LA mbox error x%x state x%x\n",
3321 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003322 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003323 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003324 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003325 }
3326
James Smart76a95d72010-11-20 23:11:48 -05003327 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003328
3329 memcpy(&phba->alpa_map[0], mp->virt, 128);
3330
James Smart2e0fef82007-06-17 19:56:36 -05003331 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003332 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003333 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003334 else
James Smart2e0fef82007-06-17 19:56:36 -05003335 vport->fc_flag &= ~FC_BYPASSED_MODE;
3336 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003337
James Smartbe0c0082013-03-01 16:38:14 -05003338 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003339 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003340 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003341 if (phba->fc_eventTag != 0)
3342 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003343 }
dea31012005-04-17 16:05:31 -05003344
3345 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003346 if (phba->sli_rev < LPFC_SLI_REV4) {
3347 spin_lock_irq(&phba->hbalock);
3348 if (bf_get(lpfc_mbx_read_top_mm, la))
3349 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3350 else
3351 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3352 spin_unlock_irq(&phba->hbalock);
3353 }
dea31012005-04-17 16:05:31 -05003354
James Smart4d9ab992009-10-02 15:16:39 -04003355 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003356 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003357 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003358 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003359 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003360 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003361 "1306 Link Up Event in loop back mode "
3362 "x%x received Data: x%x x%x x%x x%x\n",
3363 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003364 bf_get(lpfc_mbx_read_top_alpa_granted,
3365 la),
3366 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003367 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003368 } else {
3369 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003370 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003371 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003372 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003373 bf_get(lpfc_mbx_read_top_alpa_granted,
3374 la),
3375 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smart84774a42008-08-24 21:50:06 -04003376 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003377 bf_get(lpfc_mbx_read_top_mm, la),
3378 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003379 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003380 }
James Smart92d7f7b2007-06-17 19:56:38 -05003381 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003382 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3383 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003384 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003385 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003386 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3387 "1308 Link Down Event in loop back mode "
3388 "x%x received "
3389 "Data: x%x x%x x%x\n",
3390 la->eventTag, phba->fc_eventTag,
3391 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003392 else
James Smart3163f722008-02-08 18:50:25 -05003393 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003394 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003395 "Data: x%x x%x x%x x%x x%x\n",
3396 la->eventTag, phba->fc_eventTag,
3397 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003398 bf_get(lpfc_mbx_read_top_mm, la),
3399 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003400 lpfc_mbx_issue_link_down(phba);
3401 }
James Smartbe0c0082013-03-01 16:38:14 -05003402 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3403 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003404 if (phba->link_state != LPFC_LINK_DOWN) {
3405 phba->fc_stat.LinkDown++;
3406 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3407 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003408 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003409 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003410 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003411 lpfc_mbx_issue_link_down(phba);
3412 } else
3413 lpfc_enable_la(phba);
3414
3415 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3416 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3417 "Data: x%x x%x x%x\n",
3418 la->eventTag, phba->fc_eventTag,
3419 phba->pport->port_state, vport->fc_flag);
3420 /*
3421 * The cmnd that triggered this will be waiting for this
3422 * signal.
3423 */
3424 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3425 if (phba->wait_4_mlo_maint_flg) {
3426 phba->wait_4_mlo_maint_flg = 0;
3427 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003428 }
James Smart84774a42008-08-24 21:50:06 -04003429 }
3430
James Smartbe0c0082013-03-01 16:38:14 -05003431 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3432 bf_get(lpfc_mbx_read_top_fa, la)) {
3433 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003434 lpfc_issue_clear_la(phba, vport);
3435 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003436 "1311 fa %d\n",
3437 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003438 }
3439
James Smart76a95d72010-11-20 23:11:48 -05003440lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003441 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3442 kfree(mp);
3443 mempool_free(pmb, phba->mbox_mem_pool);
3444 return;
3445}
3446
3447/*
3448 * This routine handles processing a REG_LOGIN mailbox
3449 * command upon completion. It is setup in the LPFC_MBOXQ
3450 * as the completion routine when the command is
3451 * handed off to the SLI layer.
3452 */
3453void
James Smart2e0fef82007-06-17 19:56:36 -05003454lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003455{
James Smart2e0fef82007-06-17 19:56:36 -05003456 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003457 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003458 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003459 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003460
dea31012005-04-17 16:05:31 -05003461 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003462 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003463
James Smartbe6bb942015-04-07 15:07:22 -04003464 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3465 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3466 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003467 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003468 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003469 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3470 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3471
James Smart40426292010-12-15 17:58:10 -05003472 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3473 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003474 /* We rcvd a rscn after issuing this
3475 * mbox reg login, we may have cycled
3476 * back through the state and be
3477 * back at reg login state so this
3478 * mbox needs to be ignored becase
3479 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003480 * process.
James Smartffc95492010-06-07 15:23:17 -04003481 */
3482 spin_lock_irq(shost->host_lock);
3483 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3484 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003485
3486 /*
3487 * We cannot leave the RPI registered because
3488 * if we go thru discovery again for this ndlp
3489 * a subsequent REG_RPI will fail.
3490 */
3491 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3492 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003493 }
3494
3495 /* Call state machine */
3496 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003497
dea31012005-04-17 16:05:31 -05003498 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3499 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003500 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003501 /* decrement the node reference count held for this callback
3502 * function.
3503 */
James Smart329f9bc2007-04-25 09:53:01 -04003504 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003505
3506 return;
3507}
3508
James Smart92d7f7b2007-06-17 19:56:38 -05003509static void
3510lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3511{
James Smart04c68492009-05-22 14:52:52 -04003512 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003513 struct lpfc_vport *vport = pmb->vport;
3514 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3515
3516 switch (mb->mbxStatus) {
3517 case 0x0011:
3518 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003519 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3520 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3521 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003522 break;
James Smart78730cf2010-04-06 15:06:30 -04003523 /* If VPI is busy, reset the HBA */
3524 case 0x9700:
3525 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3526 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3527 vport->vpi, mb->mbxStatus);
3528 if (!(phba->pport->load_flag & FC_UNLOADING))
3529 lpfc_workq_post_event(phba, NULL, NULL,
3530 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003531 }
James Smart72100cc2010-02-12 14:43:01 -05003532 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003533 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003534 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003535 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003536 vport->unreg_vpi_cmpl = VPORT_OK;
3537 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003538 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003539 /*
3540 * This shost reference might have been taken at the beginning of
3541 * lpfc_vport_delete()
3542 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003543 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003544 scsi_host_put(shost);
3545}
3546
James Smartd7c255b2008-08-24 21:50:00 -04003547int
James Smart92d7f7b2007-06-17 19:56:38 -05003548lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3549{
3550 struct lpfc_hba *phba = vport->phba;
3551 LPFC_MBOXQ_t *mbox;
3552 int rc;
3553
3554 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3555 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003556 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003557
3558 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3559 mbox->vport = vport;
3560 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003561 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003562 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003563 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3564 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003565 mempool_free(mbox, phba->mbox_mem_pool);
3566 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003567 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003568 }
James Smartd7c255b2008-08-24 21:50:00 -04003569 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003570}
3571
3572static void
3573lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3574{
3575 struct lpfc_vport *vport = pmb->vport;
3576 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003577 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003578
3579 switch (mb->mbxStatus) {
3580 case 0x0011:
3581 case 0x9601:
3582 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003583 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3584 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3585 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003586 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3587 spin_lock_irq(shost->host_lock);
3588 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3589 spin_unlock_irq(shost->host_lock);
3590 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003591
James Smart01649562017-02-12 13:52:32 -08003592 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
James Smart8c258642017-02-12 13:52:36 -08003593 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
James Smartd613b6a2017-02-12 13:52:37 -08003594 if (phba->nvmet_support)
3595 lpfc_nvmet_update_targetport(phba);
3596 else
James Smart8c258642017-02-12 13:52:36 -08003597 lpfc_nvme_update_localport(vport);
James Smart8c258642017-02-12 13:52:36 -08003598 }
James Smart92d7f7b2007-06-17 19:56:38 -05003599 goto out;
3600 }
James Smart92d7f7b2007-06-17 19:56:38 -05003601
James Smart72100cc2010-02-12 14:43:01 -05003602 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003603 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003604 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003605 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003606 vport->num_disc_nodes = 0;
3607 /* go thru NPR list and issue ELS PLOGIs */
3608 if (vport->fc_npr_cnt)
3609 lpfc_els_disc_plogi(vport);
3610
3611 if (!vport->num_disc_nodes) {
3612 spin_lock_irq(shost->host_lock);
3613 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3614 spin_unlock_irq(shost->host_lock);
3615 lpfc_can_disctmo(vport);
3616 }
3617 vport->port_state = LPFC_VPORT_READY;
3618
3619out:
3620 mempool_free(pmb, phba->mbox_mem_pool);
3621 return;
3622}
3623
James Smart21e9a0a2009-05-22 14:53:21 -04003624/**
3625 * lpfc_create_static_vport - Read HBA config region to create static vports.
3626 * @phba: pointer to lpfc hba data structure.
3627 *
3628 * This routine issue a DUMP mailbox command for config region 22 to get
3629 * the list of static vports to be created. The function create vports
3630 * based on the information returned from the HBA.
3631 **/
3632void
3633lpfc_create_static_vport(struct lpfc_hba *phba)
3634{
3635 LPFC_MBOXQ_t *pmb = NULL;
3636 MAILBOX_t *mb;
3637 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003638 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003639 struct fc_vport_identifiers vport_id;
3640 struct fc_vport *new_fc_vport;
3641 struct Scsi_Host *shost;
3642 struct lpfc_vport *vport;
3643 uint16_t offset = 0;
3644 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003645 struct lpfc_dmabuf *mp;
3646 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003647
3648 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3649 if (!pmb) {
3650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3651 "0542 lpfc_create_static_vport failed to"
3652 " allocate mailbox memory\n");
3653 return;
3654 }
James Smartcdcc2342012-08-14 14:26:35 -04003655 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003656 mb = &pmb->u.mb;
3657
3658 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3659 if (!vport_info) {
3660 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3661 "0543 lpfc_create_static_vport failed to"
3662 " allocate vport_info\n");
3663 mempool_free(pmb, phba->mbox_mem_pool);
3664 return;
3665 }
3666
3667 vport_buff = (uint8_t *) vport_info;
3668 do {
James Smartcdcc2342012-08-14 14:26:35 -04003669 /* free dma buffer from previous round */
3670 if (pmb->context1) {
3671 mp = (struct lpfc_dmabuf *)pmb->context1;
3672 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3673 kfree(mp);
3674 }
James Smart1c6834a2009-07-19 10:01:26 -04003675 if (lpfc_dump_static_vport(phba, pmb, offset))
3676 goto out;
3677
James Smart21e9a0a2009-05-22 14:53:21 -04003678 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003679 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3680 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003681
James Smartcdcc2342012-08-14 14:26:35 -04003682 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003683 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3684 "0544 lpfc_create_static_vport failed to"
3685 " issue dump mailbox command ret 0x%x "
3686 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003687 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003688 goto out;
3689 }
3690
James Smart1c6834a2009-07-19 10:01:26 -04003691 if (phba->sli_rev == LPFC_SLI_REV4) {
3692 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003693 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003694 if (byte_count > sizeof(struct static_vport_info) -
3695 offset)
3696 byte_count = sizeof(struct static_vport_info)
3697 - offset;
3698 memcpy(vport_buff + offset, mp->virt, byte_count);
3699 offset += byte_count;
3700 } else {
3701 if (mb->un.varDmp.word_cnt >
3702 sizeof(struct static_vport_info) - offset)
3703 mb->un.varDmp.word_cnt =
3704 sizeof(struct static_vport_info)
3705 - offset;
3706 byte_count = mb->un.varDmp.word_cnt;
3707 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3708 vport_buff + offset,
3709 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003710
James Smart1c6834a2009-07-19 10:01:26 -04003711 offset += byte_count;
3712 }
James Smart21e9a0a2009-05-22 14:53:21 -04003713
James Smart1c6834a2009-07-19 10:01:26 -04003714 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003715 offset < sizeof(struct static_vport_info));
3716
3717
3718 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3719 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3720 != VPORT_INFO_REV)) {
3721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3722 "0545 lpfc_create_static_vport bad"
3723 " information header 0x%x 0x%x\n",
3724 le32_to_cpu(vport_info->signature),
3725 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3726
3727 goto out;
3728 }
3729
3730 shost = lpfc_shost_from_vport(phba->pport);
3731
3732 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3733 memset(&vport_id, 0, sizeof(vport_id));
3734 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3735 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3736 if (!vport_id.port_name || !vport_id.node_name)
3737 continue;
3738
3739 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3740 vport_id.vport_type = FC_PORTTYPE_NPIV;
3741 vport_id.disable = false;
3742 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3743
3744 if (!new_fc_vport) {
3745 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3746 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003747 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003748 continue;
3749 }
3750
3751 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3752 vport->vport_flag |= STATIC_VPORT;
3753 }
3754
3755out:
James Smart21e9a0a2009-05-22 14:53:21 -04003756 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003757 if (mbx_wait_rc != MBX_TIMEOUT) {
3758 if (pmb->context1) {
3759 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003760 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3761 kfree(mp);
3762 }
James Smart21e9a0a2009-05-22 14:53:21 -04003763 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003764 }
James Smart21e9a0a2009-05-22 14:53:21 -04003765
3766 return;
3767}
3768
dea31012005-04-17 16:05:31 -05003769/*
3770 * This routine handles processing a Fabric REG_LOGIN mailbox
3771 * command upon completion. It is setup in the LPFC_MBOXQ
3772 * as the completion routine when the command is
3773 * handed off to the SLI layer.
3774 */
3775void
James Smart2e0fef82007-06-17 19:56:36 -05003776lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003777{
James Smart92d7f7b2007-06-17 19:56:38 -05003778 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003779 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003780 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003781 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003782 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003783
James Smart549e55c2007-08-02 11:09:51 -04003784 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003785 pmb->context1 = NULL;
3786 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003787
dea31012005-04-17 16:05:31 -05003788 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003789 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3790 "0258 Register Fabric login error: 0x%x\n",
3791 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003792 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3793 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003794 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003795
James Smart76a95d72010-11-20 23:11:48 -05003796 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003797 /* FLOGI failed, use loop map to make discovery list */
3798 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003799
James Smart92d7f7b2007-06-17 19:56:38 -05003800 /* Start discovery */
3801 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003802 /* Decrement the reference count to ndlp after the
3803 * reference to the ndlp are done.
3804 */
3805 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003806 return;
3807 }
3808
3809 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003810 /* Decrement the reference count to ndlp after the reference
3811 * to the ndlp are done.
3812 */
3813 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003814 return;
3815 }
3816
James Smart6d368e52011-05-24 11:44:12 -04003817 if (phba->sli_rev < LPFC_SLI_REV4)
3818 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003819 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003820 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003821 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003822
James Smart2e0fef82007-06-17 19:56:36 -05003823 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003824 /* when physical port receive logo donot start
3825 * vport discovery */
3826 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3827 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003828 else {
3829 shost = lpfc_shost_from_vport(vport);
3830 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003831 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003832 spin_unlock_irq(shost->host_lock);
3833 }
James Smart92d7f7b2007-06-17 19:56:38 -05003834 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003835 }
3836
3837 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3838 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003839 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003840
3841 /* Drop the reference count from the mbox at the end after
3842 * all the current reference to the ndlp have been done.
3843 */
3844 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003845 return;
3846}
3847
James Smarta0f2d3e2017-02-12 13:52:31 -08003848 /*
3849 * This routine will issue a GID_FT for each FC4 Type supported
3850 * by the driver. ALL GID_FTs must complete before discovery is started.
3851 */
3852int
3853lpfc_issue_gidft(struct lpfc_vport *vport)
3854{
3855 struct lpfc_hba *phba = vport->phba;
3856
3857 /* Good status, issue CT Request to NameServer */
3858 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3859 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3860 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3861 /* Cannot issue NameServer FCP Query, so finish up
3862 * discovery
3863 */
3864 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3865 "0604 %s FC TYPE %x %s\n",
3866 "Failed to issue GID_FT to ",
3867 FC_TYPE_FCP,
3868 "Finishing discovery.");
3869 return 0;
3870 }
3871 vport->gidft_inp++;
3872 }
3873
3874 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3875 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3876 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3877 /* Cannot issue NameServer NVME Query, so finish up
3878 * discovery
3879 */
3880 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3881 "0605 %s FC_TYPE %x %s %d\n",
3882 "Failed to issue GID_FT to ",
3883 FC_TYPE_NVME,
3884 "Finishing discovery: gidftinp ",
3885 vport->gidft_inp);
3886 if (vport->gidft_inp == 0)
3887 return 0;
3888 } else
3889 vport->gidft_inp++;
3890 }
3891 return vport->gidft_inp;
3892}
3893
dea31012005-04-17 16:05:31 -05003894/*
3895 * This routine handles processing a NameServer REG_LOGIN mailbox
3896 * command upon completion. It is setup in the LPFC_MBOXQ
3897 * as the completion routine when the command is
3898 * handed off to the SLI layer.
3899 */
3900void
James Smart2e0fef82007-06-17 19:56:36 -05003901lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003902{
James Smart21e9a0a2009-05-22 14:53:21 -04003903 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003904 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3905 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3906 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003907
James Smartd439d282010-09-29 11:18:45 -04003908 pmb->context1 = NULL;
3909 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003910 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003911
dea31012005-04-17 16:05:31 -05003912 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003913 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3914 "0260 Register NameServer error: 0x%x\n",
3915 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003916
3917out:
James Smartfa4066b2008-01-11 01:53:27 -05003918 /* decrement the node reference count held for this
3919 * callback function.
3920 */
James Smart329f9bc2007-04-25 09:53:01 -04003921 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003922 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3923 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003924 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003925
3926 /* If no other thread is using the ndlp, free it */
3927 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003928
James Smart76a95d72010-11-20 23:11:48 -05003929 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003930 /*
3931 * RegLogin failed, use loop map to make discovery
3932 * list
3933 */
3934 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003935
James Smart92d7f7b2007-06-17 19:56:38 -05003936 /* Start discovery */
3937 lpfc_disc_start(vport);
3938 return;
3939 }
3940 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003941 return;
3942 }
3943
James Smart6d368e52011-05-24 11:44:12 -04003944 if (phba->sli_rev < LPFC_SLI_REV4)
3945 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003946 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003947 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003948 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003949 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3950 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3951 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003952 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003953 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003954
James Smart2e0fef82007-06-17 19:56:36 -05003955 if (vport->port_state < LPFC_VPORT_READY) {
3956 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003957 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3958 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3959 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3960 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003961
3962 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3963 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3964 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3965
3966 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3967 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
James Smart01649562017-02-12 13:52:32 -08003968 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0,
3969 FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003970
3971 /* Issue SCR just before NameServer GID_FT Query */
3972 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003973 }
3974
James Smart2e0fef82007-06-17 19:56:36 -05003975 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003976 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003977 goto out;
dea31012005-04-17 16:05:31 -05003978
James Smarta0f2d3e2017-02-12 13:52:31 -08003979 /*
3980 * At this point in time we may need to wait for multiple
3981 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3982 *
3983 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003984 * callback function.
3985 */
James Smart329f9bc2007-04-25 09:53:01 -04003986 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003987 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3988 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003989 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003990
3991 return;
3992}
3993
3994static void
James Smart2e0fef82007-06-17 19:56:36 -05003995lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003996{
James Smart2e0fef82007-06-17 19:56:36 -05003997 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3998 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003999 struct lpfc_rport_data *rdata;
4000 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05004001 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004002
James Smart895427b2017-02-12 13:52:30 -08004003 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4004 return;
4005
dea31012005-04-17 16:05:31 -05004006 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07004007 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
4008 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05004009 rport_ids.port_id = ndlp->nlp_DID;
4010 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05004011
James Smart329f9bc2007-04-25 09:53:01 -04004012 /*
4013 * We leave our node pointer in rport->dd_data when we unregister a
4014 * FCP target port. But fc_remote_port_add zeros the space to which
4015 * rport->dd_data points. So, if we're reusing a previously
4016 * registered port, drop the reference that we took the last time we
4017 * registered the port.
4018 */
James Smart466e8402015-05-21 13:55:28 -04004019 rport = ndlp->rport;
4020 if (rport) {
4021 rdata = rport->dd_data;
4022 /* break the link before dropping the ref */
4023 ndlp->rport = NULL;
James Smart2ade92a2017-03-04 09:30:38 -08004024 if (rdata) {
4025 if (rdata->pnode == ndlp)
4026 lpfc_nlp_put(ndlp);
4027 rdata->pnode = NULL;
4028 }
James Smart466e8402015-05-21 13:55:28 -04004029 /* drop reference for earlier registeration */
4030 put_device(&rport->dev);
4031 }
James Smart858c9f62007-06-17 19:56:39 -05004032
4033 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4034 "rport add: did:x%x flg:x%x type x%x",
4035 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4036
James Smart9589b0622011-04-16 11:03:17 -04004037 /* Don't add the remote port if unloading. */
4038 if (vport->load_flag & FC_UNLOADING)
4039 return;
4040
James Smart2e0fef82007-06-17 19:56:36 -05004041 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004042 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004043 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4044 "Warning: fc_remote_port_add failed\n");
4045 return;
4046 }
4047
4048 /* initialize static port data */
4049 rport->maxframe_size = ndlp->nlp_maxframe;
4050 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004051 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004052 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004053
4054 if (ndlp->nlp_type & NLP_FCP_TARGET)
4055 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4056 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4057 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4058
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004059 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4060 fc_remote_port_rolechg(rport, rport_ids.roles);
4061
James Smart34f5ad82012-08-03 12:35:03 -04004062 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4063 "3183 rport register x%06x, rport %p role x%x\n",
4064 ndlp->nlp_DID, rport, rport_ids.roles);
4065
James Smart071fbd3d2006-04-15 11:53:20 -04004066 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004067 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004068 ndlp->nlp_sid = rport->scsi_target_id;
4069 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004070 return;
4071}
4072
4073static void
James Smart2e0fef82007-06-17 19:56:36 -05004074lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004075{
4076 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004077 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004078 struct lpfc_hba *phba = vport->phba;
4079
4080 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4081 return;
James Smartc01f3202006-08-18 17:47:08 -04004082
James Smart2ea259e2017-02-12 13:52:27 -08004083 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004084 "rport delete: did:x%x flg:x%x type x%x",
4085 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4086
James Smart2ea259e2017-02-12 13:52:27 -08004087 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004088 "3184 rport unregister x%06x, rport %p\n",
4089 ndlp->nlp_DID, rport);
4090
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004091 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004092
4093 return;
4094}
4095
James Smartde0c5b32007-04-25 09:52:27 -04004096static void
James Smart2e0fef82007-06-17 19:56:36 -05004097lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004098{
James Smart2e0fef82007-06-17 19:56:36 -05004099 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4100
4101 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004102 switch (state) {
4103 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004104 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004105 break;
4106 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004107 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004108 break;
4109 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004110 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004111 break;
4112 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004113 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004114 break;
4115 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004116 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004117 break;
4118 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004119 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004120 break;
4121 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004122 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004123 break;
4124 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004125 if (vport->fc_npr_cnt == 0 && count == -1)
4126 vport->fc_npr_cnt = 0;
4127 else
4128 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004129 break;
4130 }
James Smart2e0fef82007-06-17 19:56:36 -05004131 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004132}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004133
James Smartde0c5b32007-04-25 09:52:27 -04004134static void
James Smart2e0fef82007-06-17 19:56:36 -05004135lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004136 int old_state, int new_state)
4137{
James Smart2e0fef82007-06-17 19:56:36 -05004138 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smarta0f2d3e2017-02-12 13:52:31 -08004139 struct lpfc_hba *phba = vport->phba;
James Smart2e0fef82007-06-17 19:56:36 -05004140
James Smartde0c5b32007-04-25 09:52:27 -04004141 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004142 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4143 ndlp->nlp_type |= NLP_FC_NODE;
4144 }
4145 if (new_state == NLP_STE_MAPPED_NODE)
4146 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4147 if (new_state == NLP_STE_NPR_NODE)
4148 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004149
James Smarta0f2d3e2017-02-12 13:52:31 -08004150 /* FCP and NVME Transport interface */
4151 if ((old_state == NLP_STE_MAPPED_NODE ||
4152 old_state == NLP_STE_UNMAPPED_NODE)) {
4153 if (ndlp->rport) {
4154 vport->phba->nport_event_cnt++;
4155 lpfc_unregister_remote_port(ndlp);
4156 }
4157
4158 /* Notify the NVME transport of this rport's loss */
4159 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4160 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
4161 (vport->phba->nvmet_support == 0) &&
4162 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4163 (ndlp->nlp_DID == Fabric_DID))) {
4164 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004165 lpfc_nvme_unregister_port(vport, ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004166 }
James Smartde0c5b32007-04-25 09:52:27 -04004167 }
dea31012005-04-17 16:05:31 -05004168
James Smarta0f2d3e2017-02-12 13:52:31 -08004169 /* FCP and NVME Transport interfaces */
4170
James Smartde0c5b32007-04-25 09:52:27 -04004171 if (new_state == NLP_STE_MAPPED_NODE ||
4172 new_state == NLP_STE_UNMAPPED_NODE) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004173 if ((ndlp->nlp_fc4_type & NLP_FC4_FCP) ||
4174 (ndlp->nlp_DID == Fabric_DID)) {
4175 vport->phba->nport_event_cnt++;
4176 /*
4177 * Tell the fc transport about the port, if we haven't
4178 * already. If we have, and it's a scsi entity, be
4179 */
4180 lpfc_register_remote_port(vport, ndlp);
4181 }
4182 /* Notify the NVME transport of this new rport. */
4183 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4184 if (vport->phba->nvmet_support == 0) {
4185 /* Register this rport with the transport.
4186 * Initiators take the NDLP ref count in
4187 * the register.
4188 */
4189 vport->phba->nport_event_cnt++;
James Smart01649562017-02-12 13:52:32 -08004190 lpfc_nvme_register_port(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08004191 } else {
4192 /* Just take an NDLP ref count since the
4193 * target does not register rports.
4194 */
4195 lpfc_nlp_get(ndlp);
James Smarta0f2d3e2017-02-12 13:52:31 -08004196 }
4197 }
James Smartde0c5b32007-04-25 09:52:27 -04004198 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004199
James Smartea2151b2008-09-07 11:52:10 -04004200 if ((new_state == NLP_STE_MAPPED_NODE) &&
4201 (vport->stat_data_enabled)) {
4202 /*
4203 * A new target is discovered, if there is no buffer for
4204 * statistical data collection allocate buffer.
4205 */
4206 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4207 sizeof(struct lpfc_scsicmd_bkt),
4208 GFP_KERNEL);
4209
4210 if (!ndlp->lat_data)
4211 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4212 "0286 lpfc_nlp_state_cleanup failed to "
4213 "allocate statistical data buffer DID "
4214 "0x%x\n", ndlp->nlp_DID);
4215 }
James Smart858c9f62007-06-17 19:56:39 -05004216 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004217 * If the node just added to Mapped list was an FCP target,
4218 * but the remote port registration failed or assigned a target
4219 * id outside the presentable range - move the node to the
4220 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004221 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004222 if ((new_state == NLP_STE_MAPPED_NODE) &&
4223 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004224 (!ndlp->rport ||
4225 ndlp->rport->scsi_target_id == -1 ||
4226 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004227 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004228 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004229 spin_unlock_irq(shost->host_lock);
4230 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004231 }
James Smartde0c5b32007-04-25 09:52:27 -04004232}
4233
James Smart685f0bf2007-04-25 09:53:08 -04004234static char *
4235lpfc_nlp_state_name(char *buffer, size_t size, int state)
4236{
4237 static char *states[] = {
4238 [NLP_STE_UNUSED_NODE] = "UNUSED",
4239 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4240 [NLP_STE_ADISC_ISSUE] = "ADISC",
4241 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4242 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004243 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004244 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4245 [NLP_STE_MAPPED_NODE] = "MAPPED",
4246 [NLP_STE_NPR_NODE] = "NPR",
4247 };
4248
James Smart311464e2007-08-02 11:10:37 -04004249 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004250 strlcpy(buffer, states[state], size);
4251 else
4252 snprintf(buffer, size, "unknown (%d)", state);
4253 return buffer;
4254}
4255
James Smartde0c5b32007-04-25 09:52:27 -04004256void
James Smart2e0fef82007-06-17 19:56:36 -05004257lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4258 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004259{
James Smart2e0fef82007-06-17 19:56:36 -05004260 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004261 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004262 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004263
James Smarte8b62012007-08-02 11:10:09 -04004264 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4265 "0904 NPort state transition x%06x, %s -> %s\n",
4266 ndlp->nlp_DID,
4267 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4268 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004269
4270 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4271 "node statechg did:x%x old:%d ste:%d",
4272 ndlp->nlp_DID, old_state, state);
4273
James Smartde0c5b32007-04-25 09:52:27 -04004274 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004275 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004276 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004277 if (old_state == NLP_STE_UNMAPPED_NODE) {
4278 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4279 ndlp->nlp_type &= ~NLP_FC_NODE;
4280 }
4281
James Smart685f0bf2007-04-25 09:53:08 -04004282 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004283 spin_lock_irq(shost->host_lock);
4284 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4285 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004286 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004287 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004288
4289 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004290 lpfc_nlp_counters(vport, state, 1);
4291 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004292}
4293
4294void
James Smarte47c9092008-02-08 18:49:26 -05004295lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4296{
4297 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4298
4299 if (list_empty(&ndlp->nlp_listp)) {
4300 spin_lock_irq(shost->host_lock);
4301 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4302 spin_unlock_irq(shost->host_lock);
4303 }
4304}
4305
4306void
James Smart2e0fef82007-06-17 19:56:36 -05004307lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004308{
James Smart2e0fef82007-06-17 19:56:36 -05004309 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4310
James Smart0d2b6b82008-06-14 22:52:47 -04004311 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004312 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004313 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4314 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004315 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004316 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004317 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004318 NLP_STE_UNUSED_NODE);
4319}
4320
Adrian Bunk4d9db012008-02-14 23:24:02 +02004321static void
James Smarte47c9092008-02-08 18:49:26 -05004322lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4323{
James Smart0d2b6b82008-06-14 22:52:47 -04004324 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004325 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4326 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4327 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4328 NLP_STE_UNUSED_NODE);
4329}
James Smart109f6ed2008-12-04 22:39:08 -05004330/**
James Smart3621a712009-04-06 18:47:14 -04004331 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004332 * @vport: Pointer to Virtual Port object.
4333 * @ndlp: Pointer to FC node object.
4334 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004335 *
4336 * This function is always called when node object need to be initialized.
4337 * It initializes all the fields of the node object. Although the reference
4338 * to phba from @ndlp can be obtained indirectly through it's reference to
4339 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4340 * to the life-span of the @ndlp might go beyond the existence of @vport as
4341 * the final release of ndlp is determined by its reference count. And, the
4342 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004343 **/
4344static inline void
4345lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4346 uint32_t did)
4347{
4348 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4349 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
Tomas Jasek33cc5592017-03-03 13:45:48 +01004350 setup_timer(&ndlp->nlp_delayfunc, lpfc_els_retry_delay,
4351 (unsigned long)ndlp);
James Smart109f6ed2008-12-04 22:39:08 -05004352 ndlp->nlp_DID = did;
4353 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004354 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004355 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004356 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004357 kref_init(&ndlp->kref);
4358 NLP_INT_NODE_ACT(ndlp);
4359 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004360 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004361}
James Smarte47c9092008-02-08 18:49:26 -05004362
4363struct lpfc_nodelist *
4364lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4365 int state)
4366{
4367 struct lpfc_hba *phba = vport->phba;
4368 uint32_t did;
4369 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004370 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smart9d3d3402017-04-21 16:05:00 -07004371 int rpi = LPFC_RPI_ALLOC_ERROR;
James Smarte47c9092008-02-08 18:49:26 -05004372
4373 if (!ndlp)
4374 return NULL;
4375
James Smart9d3d3402017-04-21 16:05:00 -07004376 if (phba->sli_rev == LPFC_SLI_REV4) {
4377 rpi = lpfc_sli4_alloc_rpi(vport->phba);
4378 if (rpi == LPFC_RPI_ALLOC_ERROR)
4379 return NULL;
4380 }
4381
James Smarte47c9092008-02-08 18:49:26 -05004382 spin_lock_irqsave(&phba->ndlp_lock, flags);
4383 /* The ndlp should not be in memory free mode */
4384 if (NLP_CHK_FREE_REQ(ndlp)) {
4385 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4386 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4387 "0277 lpfc_enable_node: ndlp:x%p "
4388 "usgmap:x%x refcnt:%d\n",
4389 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004390 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004391 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004392 }
4393 /* The ndlp should not already be in active mode */
4394 if (NLP_CHK_NODE_ACT(ndlp)) {
4395 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4396 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4397 "0278 lpfc_enable_node: ndlp:x%p "
4398 "usgmap:x%x refcnt:%d\n",
4399 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004400 kref_read(&ndlp->kref));
James Smart9d3d3402017-04-21 16:05:00 -07004401 goto free_rpi;
James Smarte47c9092008-02-08 18:49:26 -05004402 }
4403
4404 /* Keep the original DID */
4405 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004406 if (phba->sli_rev == LPFC_SLI_REV4)
4407 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004408
4409 /* re-initialize ndlp except of ndlp linked list pointer */
4410 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4411 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004412 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004413
James Smartcff261f2013-12-17 20:29:47 -05004414 if (phba->sli_rev == LPFC_SLI_REV4)
4415 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4416
James Smarte47c9092008-02-08 18:49:26 -05004417 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004418 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07004419 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04004420 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4421 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4422 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4423 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004424 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004425 ndlp->nlp_usg_map, ndlp);
4426 }
James Smart725dd392013-10-10 12:23:30 -04004427
James Smarte47c9092008-02-08 18:49:26 -05004428
4429 if (state != NLP_STE_UNUSED_NODE)
4430 lpfc_nlp_set_state(vport, ndlp, state);
4431
4432 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4433 "node enable: did:x%x",
4434 ndlp->nlp_DID, 0, 0);
4435 return ndlp;
James Smart9d3d3402017-04-21 16:05:00 -07004436
4437free_rpi:
4438 if (phba->sli_rev == LPFC_SLI_REV4)
4439 lpfc_sli4_free_rpi(vport->phba, rpi);
4440 return NULL;
James Smartde0c5b32007-04-25 09:52:27 -04004441}
4442
4443void
James Smart2e0fef82007-06-17 19:56:36 -05004444lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004445{
James Smart87af33f2007-10-27 13:37:43 -04004446 /*
James Smartfa4066b2008-01-11 01:53:27 -05004447 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004448 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004449 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4450 * until ALL other outstanding threads have completed. We check
4451 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004452 */
James Smartfa4066b2008-01-11 01:53:27 -05004453 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4454 return;
James Smart51ef4c22007-08-02 11:10:31 -04004455 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004456 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004457 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004458 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004459 }
James Smart02902172015-05-21 13:55:17 -04004460
4461 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004462 return;
dea31012005-04-17 16:05:31 -05004463}
4464
4465/*
4466 * Start / ReStart rescue timer for Discovery / RSCN handling
4467 */
4468void
James Smart2e0fef82007-06-17 19:56:36 -05004469lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004470{
James Smart2e0fef82007-06-17 19:56:36 -05004471 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4472 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004473 uint32_t tmo;
4474
James Smart2e0fef82007-06-17 19:56:36 -05004475 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004476 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004477 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4478 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004479 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004480 * FC spec states we need 3 * ratov for CT requests
4481 */
4482 tmo = ((phba->fc_ratov * 3) + 3);
4483 }
dea31012005-04-17 16:05:31 -05004484
James Smart858c9f62007-06-17 19:56:39 -05004485
4486 if (!timer_pending(&vport->fc_disctmo)) {
4487 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4488 "set disc timer: tmo:x%x state:x%x flg:x%x",
4489 tmo, vport->port_state, vport->fc_flag);
4490 }
4491
James Smart256ec0d2013-04-17 20:14:58 -04004492 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004493 spin_lock_irq(shost->host_lock);
4494 vport->fc_flag |= FC_DISC_TMO;
4495 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004496
4497 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004498 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4499 "0247 Start Discovery Timer state x%x "
4500 "Data: x%x x%lx x%x x%x\n",
4501 vport->port_state, tmo,
4502 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4503 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004504
4505 return;
4506}
4507
4508/*
4509 * Cancel rescue timer for Discovery / RSCN handling
4510 */
4511int
James Smart2e0fef82007-06-17 19:56:36 -05004512lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004513{
James Smart2e0fef82007-06-17 19:56:36 -05004514 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004515 unsigned long iflags;
4516
James Smart858c9f62007-06-17 19:56:39 -05004517 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4518 "can disc timer: state:x%x rtry:x%x flg:x%x",
4519 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4520
dea31012005-04-17 16:05:31 -05004521 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004522 if (vport->fc_flag & FC_DISC_TMO) {
4523 spin_lock_irqsave(shost->host_lock, iflags);
4524 vport->fc_flag &= ~FC_DISC_TMO;
4525 spin_unlock_irqrestore(shost->host_lock, iflags);
4526 del_timer_sync(&vport->fc_disctmo);
4527 spin_lock_irqsave(&vport->work_port_lock, iflags);
4528 vport->work_port_events &= ~WORKER_DISC_TMO;
4529 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004530 }
4531
4532 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004533 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4534 "0248 Cancel Discovery Timer state x%x "
4535 "Data: x%x x%x x%x\n",
4536 vport->port_state, vport->fc_flag,
4537 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004538 return 0;
dea31012005-04-17 16:05:31 -05004539}
4540
4541/*
4542 * Check specified ring for outstanding IOCB on the SLI queue
4543 * Return true if iocb matches the specified nport
4544 */
4545int
James Smart2e0fef82007-06-17 19:56:36 -05004546lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4547 struct lpfc_sli_ring *pring,
4548 struct lpfc_iocbq *iocb,
4549 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004550{
James Smart2e0fef82007-06-17 19:56:36 -05004551 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004552 struct lpfc_vport *vport = ndlp->vport;
4553
4554 if (iocb->vport != vport)
4555 return 0;
4556
dea31012005-04-17 16:05:31 -05004557 if (pring->ringno == LPFC_ELS_RING) {
4558 switch (icmd->ulpCommand) {
4559 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004560 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004561 return 1;
dea31012005-04-17 16:05:31 -05004562 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004563 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4564 return 1;
dea31012005-04-17 16:05:31 -05004565 case CMD_XMIT_ELS_RSP64_CX:
4566 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004567 return 1;
dea31012005-04-17 16:05:31 -05004568 }
James Smart895427b2017-02-12 13:52:30 -08004569 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004570 /* Skip match check if waiting to relogin to FCP target */
4571 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004572 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004573 return 0;
dea31012005-04-17 16:05:31 -05004574 }
4575 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004576 return 1;
dea31012005-04-17 16:05:31 -05004577 }
dea31012005-04-17 16:05:31 -05004578 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004579 return 0;
dea31012005-04-17 16:05:31 -05004580}
4581
James Smart895427b2017-02-12 13:52:30 -08004582static void
4583__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4584 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4585 struct list_head *dequeue_list)
4586{
4587 struct lpfc_iocbq *iocb, *next_iocb;
4588
4589 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4590 /* Check to see if iocb matches the nport */
4591 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4592 /* match, dequeue */
4593 list_move_tail(&iocb->list, dequeue_list);
4594 }
4595}
4596
4597static void
4598lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4599 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4600{
4601 struct lpfc_sli *psli = &phba->sli;
4602 uint32_t i;
4603
4604 spin_lock_irq(&phba->hbalock);
4605 for (i = 0; i < psli->num_rings; i++)
4606 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4607 dequeue_list);
4608 spin_unlock_irq(&phba->hbalock);
4609}
4610
4611static void
4612lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4613 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4614{
4615 struct lpfc_sli_ring *pring;
4616 struct lpfc_queue *qp = NULL;
4617
4618 spin_lock_irq(&phba->hbalock);
4619 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4620 pring = qp->pring;
4621 if (!pring)
4622 continue;
James Smart2ade92a2017-03-04 09:30:38 -08004623 spin_lock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004624 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
James Smart2ade92a2017-03-04 09:30:38 -08004625 spin_unlock(&pring->ring_lock);
James Smart895427b2017-02-12 13:52:30 -08004626 }
4627 spin_unlock_irq(&phba->hbalock);
4628}
4629
dea31012005-04-17 16:05:31 -05004630/*
4631 * Free resources / clean up outstanding I/Os
4632 * associated with nlp_rpi in the LPFC_NODELIST entry.
4633 */
4634static int
James Smart2e0fef82007-06-17 19:56:36 -05004635lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004636{
James Smart2534ba72007-04-25 09:52:20 -04004637 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004638
James Smart92d7f7b2007-06-17 19:56:38 -05004639 lpfc_fabric_abort_nport(ndlp);
4640
dea31012005-04-17 16:05:31 -05004641 /*
4642 * Everything that matches on txcmplq will be returned
4643 * by firmware with a no rpi error.
4644 */
James Smart40426292010-12-15 17:58:10 -05004645 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004646 if (phba->sli_rev != LPFC_SLI_REV4)
4647 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4648 else
4649 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004650 }
James Smart2534ba72007-04-25 09:52:20 -04004651
James Smarta257bf92009-04-06 18:48:10 -04004652 /* Cancel all the IOCBs from the completions list */
4653 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4654 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004655
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004656 return 0;
dea31012005-04-17 16:05:31 -05004657}
4658
James Smart086a3452012-08-14 14:25:21 -04004659/**
4660 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4661 * @phba: Pointer to HBA context object.
4662 * @pmb: Pointer to mailbox object.
4663 *
4664 * This function will issue an ELS LOGO command after completing
4665 * the UNREG_RPI.
4666 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004667static void
James Smart086a3452012-08-14 14:25:21 -04004668lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4669{
4670 struct lpfc_vport *vport = pmb->vport;
4671 struct lpfc_nodelist *ndlp;
4672
4673 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4674 if (!ndlp)
4675 return;
4676 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004677 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004678}
4679
dea31012005-04-17 16:05:31 -05004680/*
4681 * Free rpi associated with LPFC_NODELIST entry.
4682 * This routine is called from lpfc_freenode(), when we are removing
4683 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4684 * LOGO that completes successfully, and we are waiting to PLOGI back
4685 * to the remote NPort. In addition, it is called after we receive
4686 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4687 * we are waiting to PLOGI back to the remote NPort.
4688 */
4689int
James Smart2e0fef82007-06-17 19:56:36 -05004690lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004691{
James Smart2e0fef82007-06-17 19:56:36 -05004692 struct lpfc_hba *phba = vport->phba;
4693 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004694 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004695 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004696
James Smartf454a9a2013-09-06 12:20:07 -04004697 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4698 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4699 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4700 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4701 "3366 RPI x%x needs to be "
4702 "unregistered nlp_flag x%x "
4703 "did x%x\n",
4704 ndlp->nlp_rpi, ndlp->nlp_flag,
4705 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004706 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4707 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004708 /* SLI4 ports require the physical rpi value. */
4709 rpi = ndlp->nlp_rpi;
4710 if (phba->sli_rev == LPFC_SLI_REV4)
4711 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004712
James Smart6d368e52011-05-24 11:44:12 -04004713 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004714 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004715 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4716 mbox->context1 = ndlp;
4717 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4718 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004719 if (phba->sli_rev == LPFC_SLI_REV4 &&
4720 (!(vport->load_flag & FC_UNLOADING)) &&
4721 (bf_get(lpfc_sli_intf_if_type,
4722 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004723 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004724 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004725 mbox->context1 = lpfc_nlp_get(ndlp);
4726 mbox->mbox_cmpl =
4727 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004728 /*
4729 * accept PLOGIs after unreg_rpi_cmpl
4730 */
4731 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004732 } else
4733 mbox->mbox_cmpl =
4734 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004735 }
4736
James Smart0b727fe2007-10-27 13:37:25 -04004737 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004738 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004739 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004740 acc_plogi = 1;
4741 }
dea31012005-04-17 16:05:31 -05004742 }
dea31012005-04-17 16:05:31 -05004743 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004744
James Smart40426292010-12-15 17:58:10 -05004745 if (phba->sli_rev != LPFC_SLI_REV4)
4746 ndlp->nlp_rpi = 0;
4747 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004748 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004749 if (acc_plogi)
4750 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004751 return 1;
4752 }
James Smart7c5e5182015-05-22 10:42:43 -04004753 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004754 return 0;
4755}
4756
James Smartecfd03c2010-02-12 14:41:27 -05004757/**
4758 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4759 * @phba: pointer to lpfc hba data structure.
4760 *
4761 * This routine is invoked to unregister all the currently registered RPIs
4762 * to the HBA.
4763 **/
4764void
4765lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4766{
4767 struct lpfc_vport **vports;
4768 struct lpfc_nodelist *ndlp;
4769 struct Scsi_Host *shost;
4770 int i;
4771
4772 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004773 if (!vports) {
4774 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4775 "2884 Vport array allocation failed \n");
4776 return;
4777 }
James Smartecfd03c2010-02-12 14:41:27 -05004778 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4779 shost = lpfc_shost_from_vport(vports[i]);
4780 spin_lock_irq(shost->host_lock);
4781 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004782 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004783 /* The mempool_alloc might sleep */
4784 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004785 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004786 spin_lock_irq(shost->host_lock);
4787 }
James Smartecfd03c2010-02-12 14:41:27 -05004788 }
4789 spin_unlock_irq(shost->host_lock);
4790 }
4791 lpfc_destroy_vport_work_array(phba, vports);
4792}
4793
James Smart92d7f7b2007-06-17 19:56:38 -05004794void
4795lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4796{
4797 struct lpfc_hba *phba = vport->phba;
4798 LPFC_MBOXQ_t *mbox;
4799 int rc;
4800
James Smart5af5eee2010-10-22 11:06:38 -04004801 if (phba->sli_rev == LPFC_SLI_REV4) {
4802 lpfc_sli4_unreg_all_rpis(vport);
4803 return;
4804 }
4805
James Smart92d7f7b2007-06-17 19:56:38 -05004806 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4807 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004808 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4809 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004810 mbox->vport = vport;
4811 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004812 mbox->context1 = NULL;
4813 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004814 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004815 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004816
4817 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4818 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4819 "1836 Could not issue "
4820 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004821 }
4822}
4823
4824void
4825lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4826{
4827 struct lpfc_hba *phba = vport->phba;
4828 LPFC_MBOXQ_t *mbox;
4829 int rc;
4830
4831 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4832 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004833 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4834 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004835 mbox->vport = vport;
4836 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004837 mbox->context1 = NULL;
4838 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004839 if (rc != MBX_TIMEOUT)
4840 mempool_free(mbox, phba->mbox_mem_pool);
4841
4842 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004843 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4844 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004845 "unreg_did (default rpis) status %d\n",
4846 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004847 }
4848}
4849
dea31012005-04-17 16:05:31 -05004850/*
4851 * Free resources associated with LPFC_NODELIST entry
4852 * so it can be freed.
4853 */
4854static int
James Smart2e0fef82007-06-17 19:56:36 -05004855lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004856{
James Smart2e0fef82007-06-17 19:56:36 -05004857 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4858 struct lpfc_hba *phba = vport->phba;
4859 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004860 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004861
4862 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004863 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4864 "0900 Cleanup node for NPort x%x "
4865 "Data: x%x x%x x%x\n",
4866 ndlp->nlp_DID, ndlp->nlp_flag,
4867 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004868 if (NLP_CHK_FREE_REQ(ndlp)) {
4869 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4870 "0280 lpfc_cleanup_node: ndlp:x%p "
4871 "usgmap:x%x refcnt:%d\n",
4872 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004873 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004874 lpfc_dequeue_node(vport, ndlp);
4875 } else {
4876 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4877 "0281 lpfc_cleanup_node: ndlp:x%p "
4878 "usgmap:x%x refcnt:%d\n",
4879 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004880 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004881 lpfc_disable_node(vport, ndlp);
4882 }
dea31012005-04-17 16:05:31 -05004883
James Smart086a3452012-08-14 14:25:21 -04004884
4885 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4886
dea31012005-04-17 16:05:31 -05004887 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4888 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004889 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004890 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004891 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4892 mb->context2 = NULL;
4893 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4894 }
4895 }
James Smart33ccf8d2006-08-17 11:57:58 -04004896
James Smart2e0fef82007-06-17 19:56:36 -05004897 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004898 /* Cleanup REG_LOGIN completions which are not yet processed */
4899 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4900 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004901 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004902 (ndlp != (struct lpfc_nodelist *) mb->context2))
4903 continue;
4904
4905 mb->context2 = NULL;
4906 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4907 }
4908
dea31012005-04-17 16:05:31 -05004909 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004910 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004911 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004912 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004913 mp = (struct lpfc_dmabuf *) (mb->context1);
4914 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004915 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004916 kfree(mp);
4917 }
4918 list_del(&mb->list);
4919 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004920 /* We shall not invoke the lpfc_nlp_put to decrement
4921 * the ndlp reference count as we are in the process
4922 * of lpfc_nlp_release.
4923 */
dea31012005-04-17 16:05:31 -05004924 }
4925 }
James Smart2e0fef82007-06-17 19:56:36 -05004926 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004927
James Smarte47c9092008-02-08 18:49:26 -05004928 lpfc_els_abort(phba, ndlp);
4929
James Smart2e0fef82007-06-17 19:56:36 -05004930 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004931 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004932 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004933
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004934 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004935 del_timer_sync(&ndlp->nlp_delayfunc);
4936
James Smart0d2b6b82008-06-14 22:52:47 -04004937 list_del_init(&ndlp->els_retry_evt.evt_listp);
4938 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004939 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004940 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004941
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004942 return 0;
dea31012005-04-17 16:05:31 -05004943}
4944
4945/*
4946 * Check to see if we can free the nlp back to the freelist.
4947 * If we are in the middle of using the nlp in the discovery state
4948 * machine, defer the free till we reach the end of the state machine.
4949 */
James Smart329f9bc2007-04-25 09:53:01 -04004950static void
James Smart2e0fef82007-06-17 19:56:36 -05004951lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004952{
James Smarta8adb832007-10-27 13:37:53 -04004953 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004954 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004955 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004956 LPFC_MBOXQ_t *mbox;
4957 int rc;
dea31012005-04-17 16:05:31 -05004958
James Smart0d2b6b82008-06-14 22:52:47 -04004959 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004960 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004961 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4962 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004963 /* For this case we need to cleanup the default rpi
4964 * allocated by the firmware.
4965 */
James Smartbe6bb942015-04-07 15:07:22 -04004966 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4967 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4968 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004969 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004970 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004971 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4972 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004973 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004974 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004975 if (rc) {
4976 mempool_free(mbox, phba->mbox_mem_pool);
4977 }
4978 else {
4979 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4980 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4981 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004982 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004983 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4984 if (rc == MBX_NOT_FINISHED) {
4985 mempool_free(mbox, phba->mbox_mem_pool);
4986 }
4987 }
4988 }
4989 }
James Smart2e0fef82007-06-17 19:56:36 -05004990 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004991
James Smart2e0fef82007-06-17 19:56:36 -05004992 /*
James Smart466e8402015-05-21 13:55:28 -04004993 * ndlp->rport must be set to NULL before it reaches here
4994 * i.e. break rport/node link before doing lpfc_nlp_put for
4995 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004996 */
James Smart92d7f7b2007-06-17 19:56:38 -05004997 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004998 /*
4999 * extra lpfc_nlp_put dropped the reference of ndlp
5000 * for registered rport so need to cleanup rport
5001 */
5002 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
5003 "0940 removed node x%p DID x%x "
5004 " rport not null %p\n",
5005 ndlp, ndlp->nlp_DID, ndlp->rport);
5006 rport = ndlp->rport;
5007 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04005008 rdata->pnode = NULL;
5009 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04005010 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05005011 }
dea31012005-04-17 16:05:31 -05005012}
5013
5014static int
James Smart2e0fef82007-06-17 19:56:36 -05005015lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5016 uint32_t did)
dea31012005-04-17 16:05:31 -05005017{
James Smart2e0fef82007-06-17 19:56:36 -05005018 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05005019
5020 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005021 return 0;
dea31012005-04-17 16:05:31 -05005022
dea31012005-04-17 16:05:31 -05005023 /* First check for Direct match */
5024 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005025 return 1;
dea31012005-04-17 16:05:31 -05005026
5027 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05005028 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05005029 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005030 return 0;
dea31012005-04-17 16:05:31 -05005031 }
5032
5033 matchdid.un.word = did;
5034 ndlpdid.un.word = ndlp->nlp_DID;
5035 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5036 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5037 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005038 /* This code is supposed to match the ID
5039 * for a private loop device that is
5040 * connect to fl_port. But we need to
5041 * check that the port did not just go
5042 * from pt2pt to fabric or we could end
5043 * up matching ndlp->nlp_DID 000001 to
5044 * fabric DID 0x20101
5045 */
dea31012005-04-17 16:05:31 -05005046 if ((ndlpdid.un.b.domain == 0) &&
5047 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005048 if (ndlpdid.un.b.id &&
5049 vport->phba->fc_topology ==
5050 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005051 return 1;
dea31012005-04-17 16:05:31 -05005052 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005053 return 0;
dea31012005-04-17 16:05:31 -05005054 }
5055
5056 matchdid.un.word = ndlp->nlp_DID;
5057 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5058 (mydid.un.b.area == ndlpdid.un.b.area)) {
5059 if ((matchdid.un.b.domain == 0) &&
5060 (matchdid.un.b.area == 0)) {
5061 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005062 return 1;
dea31012005-04-17 16:05:31 -05005063 }
5064 }
5065 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005066 return 0;
dea31012005-04-17 16:05:31 -05005067}
5068
James Smart685f0bf2007-04-25 09:53:08 -04005069/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005070static struct lpfc_nodelist *
5071__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005072{
James Smart2fb9bd82006-12-02 13:33:57 -05005073 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005074 uint32_t data1;
5075
James Smart2e0fef82007-06-17 19:56:36 -05005076 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5077 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005078 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5079 ((uint32_t) ndlp->nlp_xri << 16) |
5080 ((uint32_t) ndlp->nlp_type << 8) |
5081 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005082 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5083 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005084 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005085 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005086 ndlp->nlp_flag, data1,
5087 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005088 return ndlp;
dea31012005-04-17 16:05:31 -05005089 }
5090 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005091
dea31012005-04-17 16:05:31 -05005092 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005093 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5094 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005095 return NULL;
5096}
5097
5098struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005099lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005100{
James Smart2e0fef82007-06-17 19:56:36 -05005101 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005102 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005103 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005104
James Smartfedd3b72011-02-16 12:39:24 -05005105 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005106 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005107 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005108 return ndlp;
5109}
5110
5111struct lpfc_nodelist *
5112lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5113{
5114 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5115 struct lpfc_nodelist *ndlp;
5116
5117 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005118 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05005119 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5120 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005121 return NULL;
James Smart9d3d3402017-04-21 16:05:00 -07005122 ndlp = lpfc_nlp_init(vport, did);
dea31012005-04-17 16:05:31 -05005123 if (!ndlp)
5124 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005125 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005126 if (vport->phba->nvmet_support)
5127 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005128 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005129 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005130 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005131 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005132 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5133 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5134 if (!ndlp)
5135 return NULL;
James Smart8c258642017-02-12 13:52:36 -08005136 if (vport->phba->nvmet_support)
5137 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005138 spin_lock_irq(shost->host_lock);
5139 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5140 spin_unlock_irq(shost->host_lock);
5141 return ndlp;
dea31012005-04-17 16:05:31 -05005142 }
James Smarte47c9092008-02-08 18:49:26 -05005143
James Smart58da1ff2008-04-07 10:15:56 -04005144 if ((vport->fc_flag & FC_RSCN_MODE) &&
5145 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005146 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005147 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005148 * we don't need to try to discover it again.
5149 */
5150 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5151 return NULL;
5152
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005153 /* Since this node is marked for discovery,
5154 * delay timeout is not needed.
5155 */
James Smart0d2b6b82008-06-14 22:52:47 -04005156 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart8c258642017-02-12 13:52:36 -08005157 if (vport->phba->nvmet_support)
5158 return ndlp;
James Smarta257bf92009-04-06 18:48:10 -04005159 spin_lock_irq(shost->host_lock);
5160 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5161 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005162 } else
dea31012005-04-17 16:05:31 -05005163 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005164 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005165 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04005166 * or we are already in the process of discovery on it,
5167 * we don't need to try to discover it again.
5168 */
James Smart685f0bf2007-04-25 09:53:08 -04005169 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005170 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5171 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05005172 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005173 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
James Smart8c258642017-02-12 13:52:36 -08005174 if (vport->phba->nvmet_support)
5175 return ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005176 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005177 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005178 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005179 }
5180 return ndlp;
5181}
5182
5183/* Build a list of nodes to discover based on the loopmap */
5184void
James Smart2e0fef82007-06-17 19:56:36 -05005185lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005186{
James Smart2e0fef82007-06-17 19:56:36 -05005187 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005188 int j;
5189 uint32_t alpa, index;
5190
James Smart2e0fef82007-06-17 19:56:36 -05005191 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005192 return;
James Smart2e0fef82007-06-17 19:56:36 -05005193
James Smart76a95d72010-11-20 23:11:48 -05005194 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005195 return;
dea31012005-04-17 16:05:31 -05005196
5197 /* Check for loop map present or not */
5198 if (phba->alpa_map[0]) {
5199 for (j = 1; j <= phba->alpa_map[0]; j++) {
5200 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005201 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005202 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005203 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005204 }
5205 } else {
5206 /* No alpamap, so try all alpa's */
5207 for (j = 0; j < FC_MAXLOOP; j++) {
5208 /* If cfg_scan_down is set, start from highest
5209 * ALPA (0xef) to lowest (0x1).
5210 */
James Smart3de2a652007-08-02 11:09:59 -04005211 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005212 index = j;
5213 else
5214 index = FC_MAXLOOP - j - 1;
5215 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005216 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005217 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005218 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005219 }
5220 }
5221 return;
5222}
5223
James Smart895427b2017-02-12 13:52:30 -08005224/* SLI3 only */
dea31012005-04-17 16:05:31 -05005225void
James Smart2e0fef82007-06-17 19:56:36 -05005226lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005227{
dea31012005-04-17 16:05:31 -05005228 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005229 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005230 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5231 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005232 int rc;
5233
James Smart92d7f7b2007-06-17 19:56:38 -05005234 /*
5235 * if it's not a physical port or if we already send
5236 * clear_la then don't send it.
5237 */
5238 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005239 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5240 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005241 return;
5242
James Smart2e0fef82007-06-17 19:56:36 -05005243 /* Link up discovery */
5244 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5245 phba->link_state = LPFC_CLEAR_LA;
5246 lpfc_clear_la(phba, mbox);
5247 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5248 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005249 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005250 if (rc == MBX_NOT_FINISHED) {
5251 mempool_free(mbox, phba->mbox_mem_pool);
5252 lpfc_disc_flush_list(vport);
5253 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5254 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005255 phba->link_state = LPFC_HBA_ERROR;
5256 }
5257 }
5258}
5259
5260/* Reg_vpi to tell firmware to resume normal operations */
5261void
5262lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5263{
5264 LPFC_MBOXQ_t *regvpimbox;
5265
5266 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5267 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005268 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005269 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5270 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005271 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005272 == MBX_NOT_FINISHED) {
5273 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005274 }
5275 }
5276}
5277
5278/* Start Link up / RSCN discovery on NPR nodes */
5279void
5280lpfc_disc_start(struct lpfc_vport *vport)
5281{
5282 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5283 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005284 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005285 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005286
James Smarte74c03c2013-04-17 20:15:19 -04005287 if (!lpfc_is_link_up(phba)) {
5288 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5289 "3315 Link is not up %x\n",
5290 phba->link_state);
dea31012005-04-17 16:05:31 -05005291 return;
James Smarte74c03c2013-04-17 20:15:19 -04005292 }
James Smart2e0fef82007-06-17 19:56:36 -05005293
5294 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005295 clear_la_pending = 1;
5296 else
5297 clear_la_pending = 0;
5298
James Smart2e0fef82007-06-17 19:56:36 -05005299 if (vport->port_state < LPFC_VPORT_READY)
5300 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005301
James Smart2e0fef82007-06-17 19:56:36 -05005302 lpfc_set_disctmo(vport);
5303
James Smart2e0fef82007-06-17 19:56:36 -05005304 vport->fc_prevDID = vport->fc_myDID;
5305 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005306
5307 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005308 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5309 "0202 Start Discovery hba state x%x "
5310 "Data: x%x x%x x%x\n",
5311 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5312 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005313
5314 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005315 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005316
5317 if (num_sent)
5318 return;
5319
James Smart6fa139f2013-04-17 20:20:00 -04005320 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005321 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005322 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005323 !(vport->fc_flag & FC_RSCN_MODE) &&
5324 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005325 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005326 lpfc_issue_reg_vpi(phba, vport);
5327 return;
5328 }
James Smart2e0fef82007-06-17 19:56:36 -05005329
James Smart92d7f7b2007-06-17 19:56:38 -05005330 /*
5331 * For SLI2, we need to set port_state to READY and continue
5332 * discovery.
5333 */
5334 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5335 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005336 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005337
5338 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005339 vport->num_disc_nodes = 0;
5340 /* go thru NPR nodes and issue ELS PLOGIs */
5341 if (vport->fc_npr_cnt)
5342 lpfc_els_disc_plogi(vport);
5343
5344 if (!vport->num_disc_nodes) {
5345 spin_lock_irq(shost->host_lock);
5346 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5347 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005348 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005349 }
5350 }
James Smart92d7f7b2007-06-17 19:56:38 -05005351 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005352 } else {
5353 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005354 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005355
5356 if (num_sent)
5357 return;
5358
James Smart2e0fef82007-06-17 19:56:36 -05005359 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005360 /* Check to see if more RSCNs came in while we
5361 * were processing this one.
5362 */
James Smart2e0fef82007-06-17 19:56:36 -05005363 if ((vport->fc_rscn_id_cnt == 0) &&
5364 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5365 spin_lock_irq(shost->host_lock);
5366 vport->fc_flag &= ~FC_RSCN_MODE;
5367 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005368 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005369 } else
James Smart2e0fef82007-06-17 19:56:36 -05005370 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005371 }
5372 }
5373 return;
5374}
5375
5376/*
5377 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5378 * ring the match the sppecified nodelist.
5379 */
5380static void
James Smart2e0fef82007-06-17 19:56:36 -05005381lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005382{
James Smart2534ba72007-04-25 09:52:20 -04005383 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005384 struct lpfc_sli *psli;
5385 IOCB_t *icmd;
5386 struct lpfc_iocbq *iocb, *next_iocb;
5387 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005388
5389 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005390 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005391
5392 /* Error matching iocb on txq or txcmplq
5393 * First check the txq.
5394 */
James Smart2e0fef82007-06-17 19:56:36 -05005395 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005396 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5397 if (iocb->context1 != ndlp) {
5398 continue;
5399 }
5400 icmd = &iocb->iocb;
5401 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5402 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5403
James Smart2534ba72007-04-25 09:52:20 -04005404 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005405 }
5406 }
5407
5408 /* Next check the txcmplq */
5409 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5410 if (iocb->context1 != ndlp) {
5411 continue;
5412 }
5413 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005414 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5415 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005416 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005417 }
5418 }
James Smart2e0fef82007-06-17 19:56:36 -05005419 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005420
James Smarta257bf92009-04-06 18:48:10 -04005421 /* Cancel all the IOCBs from the completions list */
5422 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5423 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005424}
5425
Adrian Bunka6ababd2007-11-05 18:07:33 +01005426static void
James Smart2e0fef82007-06-17 19:56:36 -05005427lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005428{
5429 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005430 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005431
James Smart2e0fef82007-06-17 19:56:36 -05005432 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5433 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005434 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005435 if (!NLP_CHK_NODE_ACT(ndlp))
5436 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005437 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5438 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5439 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005440 }
dea31012005-04-17 16:05:31 -05005441 }
5442 }
dea31012005-04-17 16:05:31 -05005443}
5444
James Smart92d7f7b2007-06-17 19:56:38 -05005445void
5446lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5447{
5448 lpfc_els_flush_rscn(vport);
5449 lpfc_els_flush_cmd(vport);
5450 lpfc_disc_flush_list(vport);
5451}
5452
dea31012005-04-17 16:05:31 -05005453/*****************************************************************************/
5454/*
5455 * NAME: lpfc_disc_timeout
5456 *
5457 * FUNCTION: Fibre Channel driver discovery timeout routine.
5458 *
5459 * EXECUTION ENVIRONMENT: interrupt only
5460 *
5461 * CALLED FROM:
5462 * Timer function
5463 *
5464 * RETURNS:
5465 * none
5466 */
5467/*****************************************************************************/
5468void
5469lpfc_disc_timeout(unsigned long ptr)
5470{
James Smart2e0fef82007-06-17 19:56:36 -05005471 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5472 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005473 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005474 unsigned long flags = 0;
5475
5476 if (unlikely(!phba))
5477 return;
5478
James Smart5e9d9b82008-06-14 22:52:53 -04005479 spin_lock_irqsave(&vport->work_port_lock, flags);
5480 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5481 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005482 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005483 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005484
James Smart5e9d9b82008-06-14 22:52:53 -04005485 if (!tmo_posted)
5486 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005487 return;
5488}
5489
5490static void
James Smart2e0fef82007-06-17 19:56:36 -05005491lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005492{
James Smart2e0fef82007-06-17 19:56:36 -05005493 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5494 struct lpfc_hba *phba = vport->phba;
5495 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005496 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005497 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005498 int rc, clrlaerr = 0;
5499
James Smart2e0fef82007-06-17 19:56:36 -05005500 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005501 return;
5502
James Smart2e0fef82007-06-17 19:56:36 -05005503 spin_lock_irq(shost->host_lock);
5504 vport->fc_flag &= ~FC_DISC_TMO;
5505 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005506
James Smart858c9f62007-06-17 19:56:39 -05005507 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5508 "disc timeout: state:x%x rtry:x%x flg:x%x",
5509 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5510
James Smart2e0fef82007-06-17 19:56:36 -05005511 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005512
5513 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005514 /*
5515 * port_state is identically LPFC_LOCAL_CFG_LINK while
5516 * waiting for FAN timeout
5517 */
James Smarte8b62012007-08-02 11:10:09 -04005518 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5519 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005520
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005521 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005522 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005523 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005524 if (!NLP_CHK_NODE_ACT(ndlp))
5525 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005526 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5527 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005528 if (ndlp->nlp_type & NLP_FABRIC) {
5529 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005530 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005531
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005532 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005533 /* Fail outstanding IO now since device
5534 * is marked for PLOGI.
5535 */
James Smart2e0fef82007-06-17 19:56:36 -05005536 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005537 }
5538 }
James Smart92d7f7b2007-06-17 19:56:38 -05005539 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005540 if (phba->sli_rev <= LPFC_SLI_REV3)
5541 lpfc_initial_flogi(vport);
5542 else
5543 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005544 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005545 }
dea31012005-04-17 16:05:31 -05005546 break;
5547
James Smart92d7f7b2007-06-17 19:56:38 -05005548 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005549 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005550 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005551 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005552 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5553 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005554 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005555
5556 /* Assume no Fabric and go on with discovery.
5557 * Check for outstanding ELS FLOGI to abort.
5558 */
5559
5560 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005561 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005562
5563 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005564 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005565 break;
5566
5567 case LPFC_FABRIC_CFG_LINK:
5568 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5569 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005570 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5571 "0223 Timeout while waiting for "
5572 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005573 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005574 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005575 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005576 lpfc_els_abort(phba, ndlp);
5577
5578 /* ReStart discovery */
5579 goto restart_disc;
dea31012005-04-17 16:05:31 -05005580
5581 case LPFC_NS_QRY:
5582 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005583 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5584 "0224 NameServer Query timeout "
5585 "Data: x%x x%x\n",
5586 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005587
James Smart92d7f7b2007-06-17 19:56:38 -05005588 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5589 /* Try it one more time */
5590 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005591 vport->gidft_inp = 0;
5592 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005593 if (rc == 0)
5594 break;
dea31012005-04-17 16:05:31 -05005595 }
James Smart92d7f7b2007-06-17 19:56:38 -05005596 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005597
James Smart87af33f2007-10-27 13:37:43 -04005598restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005599 /*
5600 * Discovery is over.
5601 * set port_state to PORT_READY if SLI2.
5602 * cmpl_reg_vpi will set port_state to READY for SLI3.
5603 */
James Smart3772a992009-05-22 14:50:54 -04005604 if (phba->sli_rev < LPFC_SLI_REV4) {
5605 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5606 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005607 else {
James Smart3772a992009-05-22 14:50:54 -04005608 lpfc_issue_clear_la(phba, vport);
5609 vport->port_state = LPFC_VPORT_READY;
5610 }
dea31012005-04-17 16:05:31 -05005611 }
5612
5613 /* Setup and issue mailbox INITIALIZE LINK command */
5614 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5615 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005616 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5617 "0206 Device Discovery "
5618 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005619 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005620 break;
5621 }
5622
5623 lpfc_linkdown(phba);
5624 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5625 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005626 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005627 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005628 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005629 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005630 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005631 if (rc == MBX_NOT_FINISHED)
5632 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5633
5634 break;
5635
5636 case LPFC_DISC_AUTH:
5637 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005638 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5639 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005640 lpfc_disc_flush_list(vport);
5641
James Smart92d7f7b2007-06-17 19:56:38 -05005642 /*
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);
5649 else { /* NPIV Not enabled */
5650 lpfc_issue_clear_la(phba, vport);
5651 vport->port_state = LPFC_VPORT_READY;
5652 }
dea31012005-04-17 16:05:31 -05005653 }
5654 break;
5655
James Smart2e0fef82007-06-17 19:56:36 -05005656 case LPFC_VPORT_READY:
5657 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005658 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5659 "0231 RSCN timeout Data: x%x "
5660 "x%x\n",
5661 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005662
5663 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005664 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005665
James Smart2e0fef82007-06-17 19:56:36 -05005666 lpfc_els_flush_rscn(vport);
5667 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005668 }
5669 break;
James Smart2e0fef82007-06-17 19:56:36 -05005670
James Smart92d7f7b2007-06-17 19:56:38 -05005671 default:
James Smarte8b62012007-08-02 11:10:09 -04005672 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005673 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005674 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005675 break;
5676 }
5677
5678 switch (phba->link_state) {
5679 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005680 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005681 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5682 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005683 clrlaerr = 1;
5684 break;
5685
James Smart09372822008-01-11 01:52:54 -05005686 case LPFC_LINK_UP:
5687 lpfc_issue_clear_la(phba, vport);
5688 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005689 case LPFC_LINK_UNKNOWN:
5690 case LPFC_WARM_START:
5691 case LPFC_INIT_START:
5692 case LPFC_INIT_MBX_CMDS:
5693 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005694 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005695 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5696 "0230 Unexpected timeout, hba link "
5697 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005698 clrlaerr = 1;
5699 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005700
5701 case LPFC_HBA_READY:
5702 break;
dea31012005-04-17 16:05:31 -05005703 }
5704
5705 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005706 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005707 if (phba->sli_rev != LPFC_SLI_REV4) {
5708 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5709 ~LPFC_STOP_IOCB_EVENT;
5710 psli->sli3_ring[LPFC_FCP_RING].flag &=
5711 ~LPFC_STOP_IOCB_EVENT;
5712 }
James Smart2e0fef82007-06-17 19:56:36 -05005713 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005714 }
dea31012005-04-17 16:05:31 -05005715 return;
5716}
5717
dea31012005-04-17 16:05:31 -05005718/*
5719 * This routine handles processing a NameServer REG_LOGIN mailbox
5720 * command upon completion. It is setup in the LPFC_MBOXQ
5721 * as the completion routine when the command is
5722 * handed off to the SLI layer.
5723 */
5724void
James Smart2e0fef82007-06-17 19:56:36 -05005725lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005726{
James Smart04c68492009-05-22 14:52:52 -04005727 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005728 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5729 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5730 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005731
5732 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005733 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005734
James Smart6d368e52011-05-24 11:44:12 -04005735 if (phba->sli_rev < LPFC_SLI_REV4)
5736 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005737 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005738 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005739 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005740 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5741 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5742 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005743 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005744 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005745 /*
5746 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005747 * 0xfffffa (FDMI well known port).
5748 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5749 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005750 */
James Smart4258e982015-12-16 18:11:58 -05005751 if (vport->port_type == LPFC_PHYSICAL_PORT)
5752 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005753 else
James Smart4258e982015-12-16 18:11:58 -05005754 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5755
dea31012005-04-17 16:05:31 -05005756
James Smartfa4066b2008-01-11 01:53:27 -05005757 /* decrement the node reference count held for this callback
5758 * function.
5759 */
James Smart329f9bc2007-04-25 09:53:01 -04005760 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005761 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5762 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005763 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005764
5765 return;
5766}
5767
James Smart685f0bf2007-04-25 09:53:08 -04005768static int
5769lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5770{
5771 uint16_t *rpi = param;
5772
James Smarteff4a012012-01-18 16:25:25 -05005773 /* check for active node */
5774 if (!NLP_CHK_NODE_ACT(ndlp))
5775 return 0;
5776
James Smart685f0bf2007-04-25 09:53:08 -04005777 return ndlp->nlp_rpi == *rpi;
5778}
5779
5780static int
5781lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5782{
5783 return memcmp(&ndlp->nlp_portname, param,
5784 sizeof(ndlp->nlp_portname)) == 0;
5785}
5786
Adrian Bunka6ababd2007-11-05 18:07:33 +01005787static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005788__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005789{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005790 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005791
James Smart2e0fef82007-06-17 19:56:36 -05005792 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005793 if (filter(ndlp, param)) {
5794 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5795 "3185 FIND node filter %p DID "
5796 "Data: x%p x%x x%x\n",
5797 filter, ndlp, ndlp->nlp_DID,
5798 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005799 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005800 }
James Smart685f0bf2007-04-25 09:53:08 -04005801 }
James Smart34f5ad82012-08-03 12:35:03 -04005802 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5803 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005804 return NULL;
dea31012005-04-17 16:05:31 -05005805}
5806
James Smart685f0bf2007-04-25 09:53:08 -04005807/*
5808 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005809 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005810 */
5811struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005812__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005813{
James Smart2e0fef82007-06-17 19:56:36 -05005814 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005815}
5816
James Smart488d1462006-03-07 15:02:37 -05005817/*
James Smart685f0bf2007-04-25 09:53:08 -04005818 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005819 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005820 */
5821struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005822lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005823{
James Smart2e0fef82007-06-17 19:56:36 -05005824 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005825 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005826
James Smart2e0fef82007-06-17 19:56:36 -05005827 spin_lock_irq(shost->host_lock);
5828 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5829 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005830 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005831}
5832
James Smartcb69f7d2011-12-13 13:21:57 -05005833/*
5834 * This routine looks up the ndlp lists for the given RPI. If the rpi
5835 * is found, the routine returns the node element list pointer else
5836 * return NULL.
5837 */
5838struct lpfc_nodelist *
5839lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5840{
5841 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5842 struct lpfc_nodelist *ndlp;
5843
5844 spin_lock_irq(shost->host_lock);
5845 ndlp = __lpfc_findnode_rpi(vport, rpi);
5846 spin_unlock_irq(shost->host_lock);
5847 return ndlp;
5848}
5849
5850/**
5851 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5852 * @phba: pointer to lpfc hba data structure.
5853 * @vpi: the physical host virtual N_Port identifier.
5854 *
5855 * This routine finds a vport on a HBA (referred by @phba) through a
5856 * @vpi. The function walks the HBA's vport list and returns the address
5857 * of the vport with the matching @vpi.
5858 *
5859 * Return code
5860 * NULL - No vport with the matching @vpi found
5861 * Otherwise - Address to the vport with the matching @vpi.
5862 **/
5863struct lpfc_vport *
5864lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5865{
5866 struct lpfc_vport *vport;
5867 unsigned long flags;
5868 int i = 0;
5869
5870 /* The physical ports are always vpi 0 - translate is unnecessary. */
5871 if (vpi > 0) {
5872 /*
5873 * Translate the physical vpi to the logical vpi. The
5874 * vport stores the logical vpi.
5875 */
5876 for (i = 0; i < phba->max_vpi; i++) {
5877 if (vpi == phba->vpi_ids[i])
5878 break;
5879 }
5880
5881 if (i >= phba->max_vpi) {
5882 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5883 "2936 Could not find Vport mapped "
5884 "to vpi %d\n", vpi);
5885 return NULL;
5886 }
5887 }
5888
5889 spin_lock_irqsave(&phba->hbalock, flags);
5890 list_for_each_entry(vport, &phba->port_list, listentry) {
5891 if (vport->vpi == i) {
5892 spin_unlock_irqrestore(&phba->hbalock, flags);
5893 return vport;
5894 }
5895 }
5896 spin_unlock_irqrestore(&phba->hbalock, flags);
5897 return NULL;
5898}
5899
James Smart9d3d3402017-04-21 16:05:00 -07005900struct lpfc_nodelist *
5901lpfc_nlp_init(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005902{
James Smart9d3d3402017-04-21 16:05:00 -07005903 struct lpfc_nodelist *ndlp;
5904 int rpi = LPFC_RPI_ALLOC_ERROR;
5905
5906 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
5907 rpi = lpfc_sli4_alloc_rpi(vport->phba);
5908 if (rpi == LPFC_RPI_ALLOC_ERROR)
5909 return NULL;
5910 }
5911
5912 ndlp = mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
5913 if (!ndlp) {
5914 if (vport->phba->sli_rev == LPFC_SLI_REV4)
5915 lpfc_sli4_free_rpi(vport->phba, rpi);
5916 return NULL;
5917 }
5918
dea31012005-04-17 16:05:31 -05005919 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005920
5921 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005922 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005923 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart9d3d3402017-04-21 16:05:00 -07005924 ndlp->nlp_rpi = rpi;
James Smartbe6bb942015-04-07 15:07:22 -04005925 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5926 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5927 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5928 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005929 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005930 ndlp->nlp_usg_map, ndlp);
5931
James Smartcff261f2013-12-17 20:29:47 -05005932 ndlp->active_rrqs_xri_bitmap =
5933 mempool_alloc(vport->phba->active_rrq_pool,
5934 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005935 if (ndlp->active_rrqs_xri_bitmap)
5936 memset(ndlp->active_rrqs_xri_bitmap, 0,
5937 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005938 }
5939
James Smart725dd392013-10-10 12:23:30 -04005940
James Smart858c9f62007-06-17 19:56:39 -05005941
5942 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5943 "node init: did:x%x",
5944 ndlp->nlp_DID, 0, 0);
5945
James Smart9d3d3402017-04-21 16:05:00 -07005946 return ndlp;
dea31012005-04-17 16:05:31 -05005947}
James Smart329f9bc2007-04-25 09:53:01 -04005948
James Smart98c9ea52007-10-27 13:37:33 -04005949/* This routine releases all resources associated with a specifc NPort's ndlp
5950 * and mempool_free's the nodelist.
5951 */
James Smart311464e2007-08-02 11:10:37 -04005952static void
James Smart329f9bc2007-04-25 09:53:01 -04005953lpfc_nlp_release(struct kref *kref)
5954{
James Smarte47c9092008-02-08 18:49:26 -05005955 struct lpfc_hba *phba;
5956 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005957 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5958 kref);
James Smart858c9f62007-06-17 19:56:39 -05005959
5960 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5961 "node release: did:x%x flg:x%x type:x%x",
5962 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5963
James Smarte47c9092008-02-08 18:49:26 -05005964 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005965 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005966 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005967 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005968 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005969
5970 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005971 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005972
5973 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005974 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005975 spin_lock_irqsave(&phba->ndlp_lock, flags);
5976 NLP_CLR_NODE_ACT(ndlp);
5977 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005978 if (phba->sli_rev == LPFC_SLI_REV4)
5979 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005980
5981 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005982 if (NLP_CHK_FREE_REQ(ndlp)) {
5983 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005984 if (phba->sli_rev == LPFC_SLI_REV4)
5985 mempool_free(ndlp->active_rrqs_xri_bitmap,
5986 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005987 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005988 }
James Smart329f9bc2007-04-25 09:53:01 -04005989}
5990
James Smart98c9ea52007-10-27 13:37:33 -04005991/* This routine bumps the reference count for a ndlp structure to ensure
5992 * that one discovery thread won't free a ndlp while another discovery thread
5993 * is using it.
5994 */
James Smart329f9bc2007-04-25 09:53:01 -04005995struct lpfc_nodelist *
5996lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5997{
James Smarte47c9092008-02-08 18:49:26 -05005998 struct lpfc_hba *phba;
5999 unsigned long flags;
6000
James Smart98c9ea52007-10-27 13:37:33 -04006001 if (ndlp) {
6002 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6003 "node get: did:x%x flg:x%x refcnt:x%x",
6004 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006005 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006006 /* The check of ndlp usage to prevent incrementing the
6007 * ndlp reference count that is in the process of being
6008 * released.
6009 */
James Smarta257bf92009-04-06 18:48:10 -04006010 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006011 spin_lock_irqsave(&phba->ndlp_lock, flags);
6012 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
6013 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6014 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6015 "0276 lpfc_nlp_get: ndlp:x%p "
6016 "usgmap:x%x refcnt:%d\n",
6017 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006018 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006019 return NULL;
6020 } else
6021 kref_get(&ndlp->kref);
6022 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04006023 }
James Smart329f9bc2007-04-25 09:53:01 -04006024 return ndlp;
6025}
6026
James Smart98c9ea52007-10-27 13:37:33 -04006027/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05006028 * count goes to 0, this indicates the the associated nodelist should be
6029 * freed. Returning 1 indicates the ndlp resource has been released; on the
6030 * other hand, returning 0 indicates the ndlp resource has not been released
6031 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04006032 */
James Smart329f9bc2007-04-25 09:53:01 -04006033int
6034lpfc_nlp_put(struct lpfc_nodelist *ndlp)
6035{
James Smarte47c9092008-02-08 18:49:26 -05006036 struct lpfc_hba *phba;
6037 unsigned long flags;
6038
6039 if (!ndlp)
6040 return 1;
6041
6042 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6043 "node put: did:x%x flg:x%x refcnt:x%x",
6044 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006045 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04006046 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05006047 spin_lock_irqsave(&phba->ndlp_lock, flags);
6048 /* Check the ndlp memory free acknowledge flag to avoid the
6049 * possible race condition that kref_put got invoked again
6050 * after previous one has done ndlp memory free.
6051 */
6052 if (NLP_CHK_FREE_ACK(ndlp)) {
6053 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6054 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6055 "0274 lpfc_nlp_put: ndlp:x%p "
6056 "usgmap:x%x refcnt:%d\n",
6057 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006058 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006059 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006060 }
James Smarte47c9092008-02-08 18:49:26 -05006061 /* Check the ndlp inactivate log flag to avoid the possible
6062 * race condition that kref_put got invoked again after ndlp
6063 * is already in inactivating state.
6064 */
6065 if (NLP_CHK_IACT_REQ(ndlp)) {
6066 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6067 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6068 "0275 lpfc_nlp_put: ndlp:x%p "
6069 "usgmap:x%x refcnt:%d\n",
6070 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006071 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006072 return 1;
6073 }
6074 /* For last put, mark the ndlp usage flags to make sure no
6075 * other kref_get and kref_put on the same ndlp shall get
6076 * in between the process when the final kref_put has been
6077 * invoked on this ndlp.
6078 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006079 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006080 /* Indicate ndlp is put to inactive state. */
6081 NLP_SET_IACT_REQ(ndlp);
6082 /* Acknowledge ndlp memory free has been seen. */
6083 if (NLP_CHK_FREE_REQ(ndlp))
6084 NLP_SET_FREE_ACK(ndlp);
6085 }
6086 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6087 /* Note, the kref_put returns 1 when decrementing a reference
6088 * count that was 1, it invokes the release callback function,
6089 * but it still left the reference count as 1 (not actually
6090 * performs the last decrementation). Otherwise, it actually
6091 * decrements the reference count and returns 0.
6092 */
6093 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006094}
James Smart98c9ea52007-10-27 13:37:33 -04006095
6096/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006097 * by any other discovery thread. This routine returns 1 if the
6098 * ndlp has been freed. A return value of 0 indicates the ndlp is
6099 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006100 */
6101int
6102lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6103{
6104 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6105 "node not used: did:x%x flg:x%x refcnt:x%x",
6106 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006107 kref_read(&ndlp->kref));
6108 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006109 if (lpfc_nlp_put(ndlp))
6110 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006111 return 0;
6112}
James Smart6fb120a2009-05-22 14:52:59 -04006113
6114/**
6115 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6116 * @phba: Pointer to hba context object.
6117 *
6118 * This function iterate through all FC nodes associated
6119 * will all vports to check if there is any node with
6120 * fc_rports associated with it. If there is an fc_rport
6121 * associated with the node, then the node is either in
6122 * discovered state or its devloss_timer is pending.
6123 */
6124static int
6125lpfc_fcf_inuse(struct lpfc_hba *phba)
6126{
6127 struct lpfc_vport **vports;
6128 int i, ret = 0;
6129 struct lpfc_nodelist *ndlp;
6130 struct Scsi_Host *shost;
6131
6132 vports = lpfc_create_vport_work_array(phba);
6133
James Smart63e801c2010-11-20 23:14:19 -05006134 /* If driver cannot allocate memory, indicate fcf is in use */
6135 if (!vports)
6136 return 1;
6137
James Smart6fb120a2009-05-22 14:52:59 -04006138 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6139 shost = lpfc_shost_from_vport(vports[i]);
6140 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006141 /*
6142 * IF the CVL_RCVD bit is not set then we have sent the
6143 * flogi.
6144 * If dev_loss fires while we are waiting we do not want to
6145 * unreg the fcf.
6146 */
6147 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6148 spin_unlock_irq(shost->host_lock);
6149 ret = 1;
6150 goto out;
6151 }
James Smart6fb120a2009-05-22 14:52:59 -04006152 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6153 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6154 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6155 ret = 1;
6156 spin_unlock_irq(shost->host_lock);
6157 goto out;
James Smart80c17842012-03-01 22:35:45 -05006158 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6159 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006160 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006161 "2624 RPI %x DID %x flag %x "
6162 "still logged in\n",
6163 ndlp->nlp_rpi, ndlp->nlp_DID,
6164 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006165 }
6166 }
6167 spin_unlock_irq(shost->host_lock);
6168 }
6169out:
6170 lpfc_destroy_vport_work_array(phba, vports);
6171 return ret;
6172}
6173
6174/**
6175 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6176 * @phba: Pointer to hba context object.
6177 * @mboxq: Pointer to mailbox object.
6178 *
6179 * This function frees memory associated with the mailbox command.
6180 */
James Smart1b511972011-12-13 13:23:09 -05006181void
James Smart6fb120a2009-05-22 14:52:59 -04006182lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6183{
6184 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006185 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006186
6187 if (mboxq->u.mb.mbxStatus) {
6188 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6189 "2555 UNREG_VFI mbxStatus error x%x "
6190 "HBA state x%x\n",
6191 mboxq->u.mb.mbxStatus, vport->port_state);
6192 }
James Smart38b92ef2010-08-04 16:11:39 -04006193 spin_lock_irq(shost->host_lock);
6194 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6195 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006196 mempool_free(mboxq, phba->mbox_mem_pool);
6197 return;
6198}
6199
6200/**
6201 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6202 * @phba: Pointer to hba context object.
6203 * @mboxq: Pointer to mailbox object.
6204 *
6205 * This function frees memory associated with the mailbox command.
6206 */
6207static void
6208lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6209{
6210 struct lpfc_vport *vport = mboxq->vport;
6211
6212 if (mboxq->u.mb.mbxStatus) {
6213 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6214 "2550 UNREG_FCFI mbxStatus error x%x "
6215 "HBA state x%x\n",
6216 mboxq->u.mb.mbxStatus, vport->port_state);
6217 }
6218 mempool_free(mboxq, phba->mbox_mem_pool);
6219 return;
6220}
6221
6222/**
James Smartecfd03c2010-02-12 14:41:27 -05006223 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006224 * @phba: Pointer to hba context object.
6225 *
James Smartecfd03c2010-02-12 14:41:27 -05006226 * This function prepare the HBA for unregistering the currently registered
6227 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6228 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006229 */
James Smartecfd03c2010-02-12 14:41:27 -05006230int
6231lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006232{
James Smart6fb120a2009-05-22 14:52:59 -04006233 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006234 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006235 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006236 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006237
James Smartecfd03c2010-02-12 14:41:27 -05006238 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006239 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006240 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006241
James Smart4d9ab992009-10-02 15:16:39 -04006242 /* At this point, all discovery is aborted */
6243 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006244
6245 /* Unregister VPIs */
6246 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006247 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006248 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006249 /* Stop FLOGI/FDISC retries */
6250 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6251 if (ndlp)
6252 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006253 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006254 if (phba->sli_rev == LPFC_SLI_REV4)
6255 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006256 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006257 shost = lpfc_shost_from_vport(vports[i]);
6258 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006259 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006260 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006261 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006262 }
6263 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006264 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6265 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6266 if (ndlp)
6267 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6268 lpfc_cleanup_pending_mbox(phba->pport);
6269 if (phba->sli_rev == LPFC_SLI_REV4)
6270 lpfc_sli4_unreg_all_rpis(phba->pport);
6271 lpfc_mbx_unreg_vpi(phba->pport);
6272 shost = lpfc_shost_from_vport(phba->pport);
6273 spin_lock_irq(shost->host_lock);
6274 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6275 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6276 spin_unlock_irq(shost->host_lock);
6277 }
James Smart6fb120a2009-05-22 14:52:59 -04006278
James Smart695a8142010-01-26 23:08:03 -05006279 /* Cleanup any outstanding ELS commands */
6280 lpfc_els_flush_all_cmd(phba);
6281
James Smart1b511972011-12-13 13:23:09 -05006282 /* Unregister the physical port VFI */
6283 rc = lpfc_issue_unreg_vfi(phba->pport);
6284 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006285}
6286
6287/**
6288 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6289 * @phba: Pointer to hba context object.
6290 *
6291 * This function issues synchronous unregister FCF mailbox command to HBA to
6292 * unregister the currently registered FCF record. The driver does not reset
6293 * the driver FCF usage state flags.
6294 *
6295 * Return 0 if successfully issued, none-zero otherwise.
6296 */
6297int
6298lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6299{
6300 LPFC_MBOXQ_t *mbox;
6301 int rc;
6302
James Smart6fb120a2009-05-22 14:52:59 -04006303 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6304 if (!mbox) {
6305 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006306 "2551 UNREG_FCFI mbox allocation failed"
6307 "HBA state x%x\n", phba->pport->port_state);
6308 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006309 }
James Smart6fb120a2009-05-22 14:52:59 -04006310 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6311 mbox->vport = phba->pport;
6312 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6313 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6314
6315 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006316 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6317 "2552 Unregister FCFI command failed rc x%x "
6318 "HBA state x%x\n",
6319 rc, phba->pport->port_state);
6320 return -EINVAL;
6321 }
6322 return 0;
6323}
6324
6325/**
6326 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6327 * @phba: Pointer to hba context object.
6328 *
6329 * This function unregisters the currently reigstered FCF. This function
6330 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6331 */
6332void
6333lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6334{
6335 int rc;
6336
6337 /* Preparation for unregistering fcf */
6338 rc = lpfc_unregister_fcf_prep(phba);
6339 if (rc) {
6340 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6341 "2748 Failed to prepare for unregistering "
6342 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006343 return;
6344 }
6345
James Smartecfd03c2010-02-12 14:41:27 -05006346 /* Now, unregister FCF record and reset HBA FCF state */
6347 rc = lpfc_sli4_unregister_fcf(phba);
6348 if (rc)
6349 return;
6350 /* Reset HBA FCF states after successful unregister FCF */
6351 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006352 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006353
6354 /*
6355 * If driver is not unloading, check if there is any other
6356 * FCF record that can be used for discovery.
6357 */
6358 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006359 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006360 return;
6361
James Smart0c9ab6f2010-02-26 14:15:57 -05006362 /* This is considered as the initial FCF discovery scan */
6363 spin_lock_irq(&phba->hbalock);
6364 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6365 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006366
6367 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006368 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006369
James Smart0c9ab6f2010-02-26 14:15:57 -05006370 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006371
James Smart0c9ab6f2010-02-26 14:15:57 -05006372 if (rc) {
6373 spin_lock_irq(&phba->hbalock);
6374 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6375 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006376 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006377 "2553 lpfc_unregister_unused_fcf failed "
6378 "to read FCF record HBA state x%x\n",
6379 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006380 }
James Smartecfd03c2010-02-12 14:41:27 -05006381}
6382
6383/**
6384 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6385 * @phba: Pointer to hba context object.
6386 *
6387 * This function just unregisters the currently reigstered FCF. It does not
6388 * try to find another FCF for discovery.
6389 */
6390void
6391lpfc_unregister_fcf(struct lpfc_hba *phba)
6392{
6393 int rc;
6394
6395 /* Preparation for unregistering fcf */
6396 rc = lpfc_unregister_fcf_prep(phba);
6397 if (rc) {
6398 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6399 "2749 Failed to prepare for unregistering "
6400 "HBA's FCF record: rc=%d\n", rc);
6401 return;
6402 }
6403
6404 /* Now, unregister FCF record and reset HBA FCF state */
6405 rc = lpfc_sli4_unregister_fcf(phba);
6406 if (rc)
6407 return;
6408 /* Set proper HBA FCF states after successful unregister FCF */
6409 spin_lock_irq(&phba->hbalock);
6410 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6411 spin_unlock_irq(&phba->hbalock);
6412}
6413
6414/**
6415 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6416 * @phba: Pointer to hba context object.
6417 *
6418 * This function check if there are any connected remote port for the FCF and
6419 * if all the devices are disconnected, this function unregister FCFI.
6420 * This function also tries to use another FCF for discovery.
6421 */
6422void
6423lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6424{
6425 /*
James Smart3804dc82010-07-14 15:31:37 -04006426 * If HBA is not running in FIP mode, if HBA does not support
6427 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6428 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006429 */
6430 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006431 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006432 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006433 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006434 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006435 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006436 spin_unlock_irq(&phba->hbalock);
6437 return;
6438 }
6439 spin_unlock_irq(&phba->hbalock);
6440
6441 if (lpfc_fcf_inuse(phba))
6442 return;
6443
6444 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006445}
6446
6447/**
6448 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6449 * @phba: Pointer to hba context object.
6450 * @buff: Buffer containing the FCF connection table as in the config
6451 * region.
6452 * This function create driver data structure for the FCF connection
6453 * record table read from config region 23.
6454 */
6455static void
6456lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6457 uint8_t *buff)
6458{
6459 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6460 struct lpfc_fcf_conn_hdr *conn_hdr;
6461 struct lpfc_fcf_conn_rec *conn_rec;
6462 uint32_t record_count;
6463 int i;
6464
6465 /* Free the current connect table */
6466 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006467 &phba->fcf_conn_rec_list, list) {
6468 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006469 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006470 }
James Smart6fb120a2009-05-22 14:52:59 -04006471
6472 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6473 record_count = conn_hdr->length * sizeof(uint32_t)/
6474 sizeof(struct lpfc_fcf_conn_rec);
6475
6476 conn_rec = (struct lpfc_fcf_conn_rec *)
6477 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6478
6479 for (i = 0; i < record_count; i++) {
6480 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6481 continue;
6482 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6483 GFP_KERNEL);
6484 if (!conn_entry) {
6485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6486 "2566 Failed to allocate connection"
6487 " table entry\n");
6488 return;
6489 }
6490
6491 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6492 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006493 list_add_tail(&conn_entry->list,
6494 &phba->fcf_conn_rec_list);
6495 }
James Smartdf0d0852013-05-31 17:05:08 -04006496
6497 if (!list_empty(&phba->fcf_conn_rec_list)) {
6498 i = 0;
6499 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6500 list) {
6501 conn_rec = &conn_entry->conn_rec;
6502 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6503 "3345 FCF connection list rec[%02d]: "
6504 "flags:x%04x, vtag:x%04x, "
6505 "fabric_name:x%02x:%02x:%02x:%02x:"
6506 "%02x:%02x:%02x:%02x, "
6507 "switch_name:x%02x:%02x:%02x:%02x:"
6508 "%02x:%02x:%02x:%02x\n", i++,
6509 conn_rec->flags, conn_rec->vlan_tag,
6510 conn_rec->fabric_name[0],
6511 conn_rec->fabric_name[1],
6512 conn_rec->fabric_name[2],
6513 conn_rec->fabric_name[3],
6514 conn_rec->fabric_name[4],
6515 conn_rec->fabric_name[5],
6516 conn_rec->fabric_name[6],
6517 conn_rec->fabric_name[7],
6518 conn_rec->switch_name[0],
6519 conn_rec->switch_name[1],
6520 conn_rec->switch_name[2],
6521 conn_rec->switch_name[3],
6522 conn_rec->switch_name[4],
6523 conn_rec->switch_name[5],
6524 conn_rec->switch_name[6],
6525 conn_rec->switch_name[7]);
6526 }
6527 }
James Smart6fb120a2009-05-22 14:52:59 -04006528}
6529
6530/**
6531 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6532 * @phba: Pointer to hba context object.
6533 * @buff: Buffer containing the FCoE parameter data structure.
6534 *
6535 * This function update driver data structure with config
6536 * parameters read from config region 23.
6537 */
6538static void
6539lpfc_read_fcoe_param(struct lpfc_hba *phba,
6540 uint8_t *buff)
6541{
6542 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6543 struct lpfc_fcoe_params *fcoe_param;
6544
6545 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6546 buff;
6547 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006548 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006549
6550 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6551 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6552 return;
6553
James Smart6fb120a2009-05-22 14:52:59 -04006554 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6555 phba->valid_vlan = 1;
6556 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6557 0xFFF;
6558 }
6559
6560 phba->fc_map[0] = fcoe_param->fc_map[0];
6561 phba->fc_map[1] = fcoe_param->fc_map[1];
6562 phba->fc_map[2] = fcoe_param->fc_map[2];
6563 return;
6564}
6565
6566/**
6567 * lpfc_get_rec_conf23 - Get a record type in config region data.
6568 * @buff: Buffer containing config region 23 data.
6569 * @size: Size of the data buffer.
6570 * @rec_type: Record type to be searched.
6571 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006572 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006573 * of the record specified by record_type. If record found, this
6574 * function return pointer to the record else return NULL.
6575 */
6576static uint8_t *
6577lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6578{
6579 uint32_t offset = 0, rec_length;
6580
6581 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6582 (size < sizeof(uint32_t)))
6583 return NULL;
6584
6585 rec_length = buff[offset + 1];
6586
6587 /*
6588 * One TLV record has one word header and number of data words
6589 * specified in the rec_length field of the record header.
6590 */
6591 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6592 <= size) {
6593 if (buff[offset] == rec_type)
6594 return &buff[offset];
6595
6596 if (buff[offset] == LPFC_REGION23_LAST_REC)
6597 return NULL;
6598
6599 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6600 rec_length = buff[offset + 1];
6601 }
6602 return NULL;
6603}
6604
6605/**
6606 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6607 * @phba: Pointer to lpfc_hba data structure.
6608 * @buff: Buffer containing config region 23 data.
6609 * @size: Size of the data buffer.
6610 *
Stefan Weileef35c22010-08-06 21:11:15 +02006611 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006612 * populate driver data structure with the parameters.
6613 */
6614void
6615lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6616 uint8_t *buff,
6617 uint32_t size)
6618{
James Smarteb016562014-09-03 12:58:06 -04006619 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006620 uint8_t *rec_ptr;
6621
6622 /*
6623 * If data size is less than 2 words signature and version cannot be
6624 * verified.
6625 */
6626 if (size < 2*sizeof(uint32_t))
6627 return;
6628
6629 /* Check the region signature first */
6630 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6631 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6632 "2567 Config region 23 has bad signature\n");
6633 return;
6634 }
6635
6636 offset += 4;
6637
6638 /* Check the data structure version */
6639 if (buff[offset] != LPFC_REGION23_VERSION) {
6640 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6641 "2568 Config region 23 has bad version\n");
6642 return;
6643 }
6644 offset += 4;
6645
James Smart6fb120a2009-05-22 14:52:59 -04006646 /* Read FCoE param record */
6647 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6648 size - offset, FCOE_PARAM_TYPE);
6649 if (rec_ptr)
6650 lpfc_read_fcoe_param(phba, rec_ptr);
6651
6652 /* Read FCF connection table */
6653 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6654 size - offset, FCOE_CONN_TBL_TYPE);
6655 if (rec_ptr)
6656 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6657
6658}