blob: 6e64c8e8e44f5d90306c6f9fbdc854cfbcac734c [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 Smart50611572016-03-31 14:12:34 -07004 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
James Smarta93ff372010-10-22 11:06:08 -040023#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
dea31012005-04-17 16:05:31 -050025#include <linux/pci.h>
26#include <linux/kthread.h>
27#include <linux/interrupt.h>
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +010028#include <linux/lockdep.h>
dea31012005-04-17 16:05:31 -050029
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040030#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050031#include <scsi/scsi_device.h>
32#include <scsi/scsi_host.h>
33#include <scsi/scsi_transport_fc.h>
James Smarta0f2d3e2017-02-12 13:52:31 -080034#include <scsi/fc/fc_fs.h>
35
36#include <linux/nvme-fc-driver.h>
dea31012005-04-17 16:05:31 -050037
James Smartda0436e2009-05-22 14:51:39 -040038#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_hw.h"
James Smartea2151b2008-09-07 11:52:10 -040040#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050041#include "lpfc_disc.h"
42#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040043#include "lpfc_sli4.h"
dea31012005-04-17 16:05:31 -050044#include "lpfc.h"
James Smarta0f2d3e2017-02-12 13:52:31 -080045#include "lpfc_scsi.h"
46#include "lpfc_nvme.h"
dea31012005-04-17 16:05:31 -050047#include "lpfc_logmsg.h"
48#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050049#include "lpfc_vport.h"
James Smart858c9f62007-06-17 19:56:39 -050050#include "lpfc_debugfs.h"
dea31012005-04-17 16:05:31 -050051
52/* AlpaArray for assignment of scsid for scan-down and bind_method */
53static uint8_t lpfcAlpaArray[] = {
54 0xEF, 0xE8, 0xE4, 0xE2, 0xE1, 0xE0, 0xDC, 0xDA, 0xD9, 0xD6,
55 0xD5, 0xD4, 0xD3, 0xD2, 0xD1, 0xCE, 0xCD, 0xCC, 0xCB, 0xCA,
56 0xC9, 0xC7, 0xC6, 0xC5, 0xC3, 0xBC, 0xBA, 0xB9, 0xB6, 0xB5,
57 0xB4, 0xB3, 0xB2, 0xB1, 0xAE, 0xAD, 0xAC, 0xAB, 0xAA, 0xA9,
58 0xA7, 0xA6, 0xA5, 0xA3, 0x9F, 0x9E, 0x9D, 0x9B, 0x98, 0x97,
59 0x90, 0x8F, 0x88, 0x84, 0x82, 0x81, 0x80, 0x7C, 0x7A, 0x79,
60 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x6E, 0x6D, 0x6C, 0x6B,
61 0x6A, 0x69, 0x67, 0x66, 0x65, 0x63, 0x5C, 0x5A, 0x59, 0x56,
62 0x55, 0x54, 0x53, 0x52, 0x51, 0x4E, 0x4D, 0x4C, 0x4B, 0x4A,
63 0x49, 0x47, 0x46, 0x45, 0x43, 0x3C, 0x3A, 0x39, 0x36, 0x35,
64 0x34, 0x33, 0x32, 0x31, 0x2E, 0x2D, 0x2C, 0x2B, 0x2A, 0x29,
65 0x27, 0x26, 0x25, 0x23, 0x1F, 0x1E, 0x1D, 0x1B, 0x18, 0x17,
66 0x10, 0x0F, 0x08, 0x04, 0x02, 0x01
67};
68
James Smart2e0fef82007-06-17 19:56:36 -050069static void lpfc_disc_timeout_handler(struct lpfc_vport *);
Adrian Bunka6ababd2007-11-05 18:07:33 +010070static void lpfc_disc_flush_list(struct lpfc_vport *vport);
James Smart32b97932009-07-19 10:01:21 -040071static void lpfc_unregister_fcfi_cmpl(struct lpfc_hba *, LPFC_MBOXQ_t *);
James Smarta93ff372010-10-22 11:06:08 -040072static int lpfc_fcf_inuse(struct lpfc_hba *);
dea31012005-04-17 16:05:31 -050073
James Smartc01f3202006-08-18 17:47:08 -040074void
75lpfc_terminate_rport_io(struct fc_rport *rport)
dea31012005-04-17 16:05:31 -050076{
James Smartc01f3202006-08-18 17:47:08 -040077 struct lpfc_rport_data *rdata;
78 struct lpfc_nodelist * ndlp;
79 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -050080
James Smartc01f3202006-08-18 17:47:08 -040081 rdata = rport->dd_data;
82 ndlp = rdata->pnode;
83
James Smart58da1ff2008-04-07 10:15:56 -040084 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James Smartc01f3202006-08-18 17:47:08 -040085 if (rport->roles & FC_RPORT_ROLE_FCP_TARGET)
86 printk(KERN_ERR "Cannot find remote node"
87 " to terminate I/O Data x%x\n",
88 rport->port_id);
dea31012005-04-17 16:05:31 -050089 return;
90 }
91
James Smarta257bf92009-04-06 18:48:10 -040092 phba = ndlp->phba;
James Smart1a169682006-03-07 15:04:06 -050093
James Smart858c9f62007-06-17 19:56:39 -050094 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_RPORT,
95 "rport terminate: sid:x%x did:x%x flg:x%x",
96 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
97
James Smartc01f3202006-08-18 17:47:08 -040098 if (ndlp->nlp_sid != NLP_NO_SID) {
James Smart51ef4c22007-08-02 11:10:31 -040099 lpfc_sli_abort_iocb(ndlp->vport,
James Smart895427b2017-02-12 13:52:30 -0800100 &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart51ef4c22007-08-02 11:10:31 -0400101 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smartc01f3202006-08-18 17:47:08 -0400102 }
James Smartc01f3202006-08-18 17:47:08 -0400103}
104
105/*
106 * This function will be called when dev_loss_tmo fire.
107 */
108void
109lpfc_dev_loss_tmo_callbk(struct fc_rport *rport)
110{
111 struct lpfc_rport_data *rdata;
112 struct lpfc_nodelist * ndlp;
James Smart2e0fef82007-06-17 19:56:36 -0500113 struct lpfc_vport *vport;
James Smart466e8402015-05-21 13:55:28 -0400114 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500115 struct lpfc_hba *phba;
James Smart858c9f62007-06-17 19:56:39 -0500116 struct lpfc_work_evt *evtp;
James Smarta8adb832007-10-27 13:37:53 -0400117 int put_node;
118 int put_rport;
James Smartc01f3202006-08-18 17:47:08 -0400119
120 rdata = rport->dd_data;
121 ndlp = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -0400122 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
James Smartc01f3202006-08-18 17:47:08 -0400123 return;
James Smartc01f3202006-08-18 17:47:08 -0400124
James Smart858c9f62007-06-17 19:56:39 -0500125 vport = ndlp->vport;
126 phba = vport->phba;
127
128 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
129 "rport devlosscb: sid:x%x did:x%x flg:x%x",
130 ndlp->nlp_sid, ndlp->nlp_DID, ndlp->nlp_flag);
131
James Smart34f5ad82012-08-03 12:35:03 -0400132 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
133 "3181 dev_loss_callbk x%06x, rport %p flg x%x\n",
134 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
135
James Smarta8adb832007-10-27 13:37:53 -0400136 /* Don't defer this if we are in the process of deleting the vport
137 * or unloading the driver. The unload will cleanup the node
138 * appropriately we just need to cleanup the ndlp rport info here.
139 */
140 if (vport->load_flag & FC_UNLOADING) {
141 put_node = rdata->pnode != NULL;
142 put_rport = ndlp->rport != NULL;
143 rdata->pnode = NULL;
144 ndlp->rport = NULL;
145 if (put_node)
146 lpfc_nlp_put(ndlp);
147 if (put_rport)
148 put_device(&rport->dev);
149 return;
150 }
151
152 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE)
153 return;
154
James Smart466e8402015-05-21 13:55:28 -0400155 if (rport->port_name != wwn_to_u64(ndlp->nlp_portname.u.wwn))
156 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
157 "6789 rport name %llx != node port name %llx",
158 rport->port_name,
159 wwn_to_u64(ndlp->nlp_portname.u.wwn));
James Smart34f5ad82012-08-03 12:35:03 -0400160
James Smart858c9f62007-06-17 19:56:39 -0500161 evtp = &ndlp->dev_loss_evt;
162
James Smart466e8402015-05-21 13:55:28 -0400163 if (!list_empty(&evtp->evt_listp)) {
164 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
165 "6790 rport name %llx dev_loss_evt pending",
166 rport->port_name);
James Smart858c9f62007-06-17 19:56:39 -0500167 return;
James Smart466e8402015-05-21 13:55:28 -0400168 }
James Smart858c9f62007-06-17 19:56:39 -0500169
James Smart466e8402015-05-21 13:55:28 -0400170 shost = lpfc_shost_from_vport(vport);
171 spin_lock_irq(shost->host_lock);
172 ndlp->nlp_flag |= NLP_IN_DEV_LOSS;
173 spin_unlock_irq(shost->host_lock);
James Smarta62a4352013-04-17 20:18:19 -0400174
James Smartfa4066b2008-01-11 01:53:27 -0500175 /* We need to hold the node by incrementing the reference
176 * count until this queued work is done
177 */
James Smart466e8402015-05-21 13:55:28 -0400178 evtp->evt_arg1 = lpfc_nlp_get(ndlp);
179
180 spin_lock_irq(&phba->hbalock);
James Smart5e9d9b82008-06-14 22:52:53 -0400181 if (evtp->evt_arg1) {
182 evtp->evt = LPFC_EVT_DEV_LOSS;
183 list_add_tail(&evtp->evt_listp, &phba->work_list);
184 lpfc_worker_wake_up(phba);
185 }
James Smart858c9f62007-06-17 19:56:39 -0500186 spin_unlock_irq(&phba->hbalock);
187
James Smart858c9f62007-06-17 19:56:39 -0500188 return;
189}
190
James Smarta93ff372010-10-22 11:06:08 -0400191/**
192 * lpfc_dev_loss_tmo_handler - Remote node devloss timeout handler
193 * @ndlp: Pointer to remote node object.
194 *
195 * This function is called from the worker thread when devloss timeout timer
196 * expires. For SLI4 host, this routine shall return 1 when at lease one
197 * remote node, including this @ndlp, is still in use of FCF; otherwise, this
198 * routine shall return 0 when there is no remote node is still in use of FCF
199 * when devloss timeout happened to this @ndlp.
200 **/
201static int
James Smart858c9f62007-06-17 19:56:39 -0500202lpfc_dev_loss_tmo_handler(struct lpfc_nodelist *ndlp)
203{
204 struct lpfc_rport_data *rdata;
205 struct fc_rport *rport;
206 struct lpfc_vport *vport;
207 struct lpfc_hba *phba;
James Smart466e8402015-05-21 13:55:28 -0400208 struct Scsi_Host *shost;
James Smart858c9f62007-06-17 19:56:39 -0500209 uint8_t *name;
James Smart87af33f2007-10-27 13:37:43 -0400210 int put_node;
James Smart858c9f62007-06-17 19:56:39 -0500211 int warn_on = 0;
James Smarta93ff372010-10-22 11:06:08 -0400212 int fcf_inuse = 0;
James Smart858c9f62007-06-17 19:56:39 -0500213
214 rport = ndlp->rport;
James Smart858c9f62007-06-17 19:56:39 -0500215 vport = ndlp->vport;
James Smart466e8402015-05-21 13:55:28 -0400216 shost = lpfc_shost_from_vport(vport);
217
218 spin_lock_irq(shost->host_lock);
219 ndlp->nlp_flag &= ~NLP_IN_DEV_LOSS;
220 spin_unlock_irq(shost->host_lock);
221
222 if (!rport)
223 return fcf_inuse;
224
225 name = (uint8_t *) &ndlp->nlp_portname;
James Smart858c9f62007-06-17 19:56:39 -0500226 phba = vport->phba;
227
James Smarta93ff372010-10-22 11:06:08 -0400228 if (phba->sli_rev == LPFC_SLI_REV4)
229 fcf_inuse = lpfc_fcf_inuse(phba);
230
James Smart858c9f62007-06-17 19:56:39 -0500231 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
232 "rport devlosstmo:did:x%x type:x%x id:x%x",
233 ndlp->nlp_DID, ndlp->nlp_type, rport->scsi_target_id);
234
James Smart34f5ad82012-08-03 12:35:03 -0400235 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
236 "3182 dev_loss_tmo_handler x%06x, rport %p flg x%x\n",
237 ndlp->nlp_DID, ndlp->rport, ndlp->nlp_flag);
238
James Smart466e8402015-05-21 13:55:28 -0400239 /*
240 * lpfc_nlp_remove if reached with dangling rport drops the
241 * reference. To make sure that does not happen clear rport
242 * pointer in ndlp before lpfc_nlp_put.
243 */
244 rdata = rport->dd_data;
245
James Smarta8adb832007-10-27 13:37:53 -0400246 /* Don't defer this if we are in the process of deleting the vport
247 * or unloading the driver. The unload will cleanup the node
248 * appropriately we just need to cleanup the ndlp rport info here.
249 */
250 if (vport->load_flag & FC_UNLOADING) {
James Smart09372822008-01-11 01:52:54 -0500251 if (ndlp->nlp_sid != NLP_NO_SID) {
252 /* flush the target */
253 lpfc_sli_abort_iocb(vport,
James Smart895427b2017-02-12 13:52:30 -0800254 &phba->sli.sli3_ring[LPFC_FCP_RING],
255 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
James Smart09372822008-01-11 01:52:54 -0500256 }
James Smarta8adb832007-10-27 13:37:53 -0400257 put_node = rdata->pnode != NULL;
James Smarta8adb832007-10-27 13:37:53 -0400258 rdata->pnode = NULL;
259 ndlp->rport = NULL;
260 if (put_node)
261 lpfc_nlp_put(ndlp);
James Smart466e8402015-05-21 13:55:28 -0400262 put_device(&rport->dev);
263
James Smarta93ff372010-10-22 11:06:08 -0400264 return fcf_inuse;
James Smarta8adb832007-10-27 13:37:53 -0400265 }
266
James Smartd7c255b2008-08-24 21:50:00 -0400267 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
268 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
269 "0284 Devloss timeout Ignored on "
270 "WWPN %x:%x:%x:%x:%x:%x:%x:%x "
271 "NPort x%x\n",
272 *name, *(name+1), *(name+2), *(name+3),
273 *(name+4), *(name+5), *(name+6), *(name+7),
274 ndlp->nlp_DID);
James Smarta93ff372010-10-22 11:06:08 -0400275 return fcf_inuse;
James Smartd7c255b2008-08-24 21:50:00 -0400276 }
James Smart858c9f62007-06-17 19:56:39 -0500277
James Smart466e8402015-05-21 13:55:28 -0400278 put_node = rdata->pnode != NULL;
279 rdata->pnode = NULL;
280 ndlp->rport = NULL;
281 if (put_node)
282 lpfc_nlp_put(ndlp);
283 put_device(&rport->dev);
284
285 if (ndlp->nlp_type & NLP_FABRIC)
James Smarta93ff372010-10-22 11:06:08 -0400286 return fcf_inuse;
James Smart82085712007-04-25 09:52:41 -0400287
dea31012005-04-17 16:05:31 -0500288 if (ndlp->nlp_sid != NLP_NO_SID) {
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400289 warn_on = 1;
James Smart895427b2017-02-12 13:52:30 -0800290 lpfc_sli_abort_iocb(vport, &phba->sli.sli3_ring[LPFC_FCP_RING],
James Smart51ef4c22007-08-02 11:10:31 -0400291 ndlp->nlp_sid, 0, LPFC_CTX_TGT);
dea31012005-04-17 16:05:31 -0500292 }
James Smartc01f3202006-08-18 17:47:08 -0400293
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400294 if (warn_on) {
James Smarte8b62012007-08-02 11:10:09 -0400295 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
296 "0203 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400297 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
298 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400299 *name, *(name+1), *(name+2), *(name+3),
300 *(name+4), *(name+5), *(name+6), *(name+7),
301 ndlp->nlp_DID, ndlp->nlp_flag,
302 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400303 } else {
James Smarte8b62012007-08-02 11:10:09 -0400304 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
305 "0204 Devloss timeout on "
James Smart58da1ff2008-04-07 10:15:56 -0400306 "WWPN %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x "
307 "NPort x%06x Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -0400308 *name, *(name+1), *(name+2), *(name+3),
309 *(name+4), *(name+5), *(name+6), *(name+7),
310 ndlp->nlp_DID, ndlp->nlp_flag,
311 ndlp->nlp_state, ndlp->nlp_rpi);
James.Smart@Emulex.Com6e8215e2005-06-25 10:34:04 -0400312 }
313
James Smart2e0fef82007-06-17 19:56:36 -0500314 if (!(vport->load_flag & FC_UNLOADING) &&
James Smart1dcb58e2007-04-25 09:51:30 -0400315 !(ndlp->nlp_flag & NLP_DELAY_TMO) &&
James Smart82085712007-04-25 09:52:41 -0400316 !(ndlp->nlp_flag & NLP_NPR_2B_DISC) &&
James Smartffc95492010-06-07 15:23:17 -0400317 (ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
James Smart589a52d2010-07-14 15:30:54 -0400318 (ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) &&
319 (ndlp->nlp_state != NLP_STE_PRLI_ISSUE))
James Smart2e0fef82007-06-17 19:56:36 -0500320 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
James Smart6fb120a2009-05-22 14:52:59 -0400321
James Smarta93ff372010-10-22 11:06:08 -0400322 return fcf_inuse;
323}
324
325/**
326 * lpfc_sli4_post_dev_loss_tmo_handler - SLI4 post devloss timeout handler
327 * @phba: Pointer to hba context object.
328 * @fcf_inuse: SLI4 FCF in-use state reported from devloss timeout handler.
329 * @nlp_did: remote node identifer with devloss timeout.
330 *
331 * This function is called from the worker thread after invoking devloss
332 * timeout handler and releasing the reference count for the ndlp with
333 * which the devloss timeout was handled for SLI4 host. For the devloss
334 * timeout of the last remote node which had been in use of FCF, when this
335 * routine is invoked, it shall be guaranteed that none of the remote are
336 * in-use of FCF. When devloss timeout to the last remote using the FCF,
337 * if the FIP engine is neither in FCF table scan process nor roundrobin
338 * failover process, the in-use FCF shall be unregistered. If the FIP
339 * engine is in FCF discovery process, the devloss timeout state shall
340 * be set for either the FCF table scan process or roundrobin failover
341 * process to unregister the in-use FCF.
342 **/
343static void
344lpfc_sli4_post_dev_loss_tmo_handler(struct lpfc_hba *phba, int fcf_inuse,
345 uint32_t nlp_did)
346{
347 /* If devloss timeout happened to a remote node when FCF had no
348 * longer been in-use, do nothing.
349 */
350 if (!fcf_inuse)
351 return;
352
353 if ((phba->hba_flag & HBA_FIP_SUPPORT) && !lpfc_fcf_inuse(phba)) {
354 spin_lock_irq(&phba->hbalock);
355 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
356 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
357 spin_unlock_irq(&phba->hbalock);
358 return;
359 }
360 phba->hba_flag |= HBA_DEVLOSS_TMO;
361 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
362 "2847 Last remote node (x%x) using "
363 "FCF devloss tmo\n", nlp_did);
364 }
365 if (phba->fcf.fcf_flag & FCF_REDISC_PROG) {
366 spin_unlock_irq(&phba->hbalock);
367 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
368 "2868 Devloss tmo to FCF rediscovery "
369 "in progress\n");
370 return;
371 }
372 if (!(phba->hba_flag & (FCF_TS_INPROG | FCF_RR_INPROG))) {
373 spin_unlock_irq(&phba->hbalock);
374 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
375 "2869 Devloss tmo to idle FIP engine, "
376 "unreg in-use FCF and rescan.\n");
377 /* Unregister in-use FCF and rescan */
378 lpfc_unregister_fcf_rescan(phba);
379 return;
380 }
381 spin_unlock_irq(&phba->hbalock);
382 if (phba->hba_flag & FCF_TS_INPROG)
383 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
384 "2870 FCF table scan in progress\n");
385 if (phba->hba_flag & FCF_RR_INPROG)
386 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
387 "2871 FLOGI roundrobin FCF failover "
388 "in progress\n");
389 }
James Smart6fb120a2009-05-22 14:52:59 -0400390 lpfc_unregister_unused_fcf(phba);
James Smart92d7f7b2007-06-17 19:56:38 -0500391}
James Smartc01f3202006-08-18 17:47:08 -0400392
James Smartea2151b2008-09-07 11:52:10 -0400393/**
James Smart3621a712009-04-06 18:47:14 -0400394 * lpfc_alloc_fast_evt - Allocates data structure for posting event
James Smartea2151b2008-09-07 11:52:10 -0400395 * @phba: Pointer to hba context object.
396 *
397 * This function is called from the functions which need to post
398 * events from interrupt context. This function allocates data
399 * structure required for posting event. It also keeps track of
400 * number of events pending and prevent event storm when there are
401 * too many events.
402 **/
403struct lpfc_fast_path_event *
404lpfc_alloc_fast_evt(struct lpfc_hba *phba) {
405 struct lpfc_fast_path_event *ret;
406
407 /* If there are lot of fast event do not exhaust memory due to this */
408 if (atomic_read(&phba->fast_event_count) > LPFC_MAX_EVT_COUNT)
409 return NULL;
410
411 ret = kzalloc(sizeof(struct lpfc_fast_path_event),
412 GFP_ATOMIC);
James Smart6fb120a2009-05-22 14:52:59 -0400413 if (ret) {
James Smartea2151b2008-09-07 11:52:10 -0400414 atomic_inc(&phba->fast_event_count);
James Smart6fb120a2009-05-22 14:52:59 -0400415 INIT_LIST_HEAD(&ret->work_evt.evt_listp);
416 ret->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
417 }
James Smartea2151b2008-09-07 11:52:10 -0400418 return ret;
419}
420
421/**
James Smart3621a712009-04-06 18:47:14 -0400422 * lpfc_free_fast_evt - Frees event data structure
James Smartea2151b2008-09-07 11:52:10 -0400423 * @phba: Pointer to hba context object.
424 * @evt: Event object which need to be freed.
425 *
426 * This function frees the data structure required for posting
427 * events.
428 **/
429void
430lpfc_free_fast_evt(struct lpfc_hba *phba,
431 struct lpfc_fast_path_event *evt) {
432
433 atomic_dec(&phba->fast_event_count);
434 kfree(evt);
435}
436
437/**
James Smart3621a712009-04-06 18:47:14 -0400438 * lpfc_send_fastpath_evt - Posts events generated from fast path
James Smartea2151b2008-09-07 11:52:10 -0400439 * @phba: Pointer to hba context object.
440 * @evtp: Event data structure.
441 *
442 * This function is called from worker thread, when the interrupt
443 * context need to post an event. This function posts the event
444 * to fc transport netlink interface.
445 **/
446static void
447lpfc_send_fastpath_evt(struct lpfc_hba *phba,
448 struct lpfc_work_evt *evtp)
449{
450 unsigned long evt_category, evt_sub_category;
451 struct lpfc_fast_path_event *fast_evt_data;
452 char *evt_data;
453 uint32_t evt_data_size;
454 struct Scsi_Host *shost;
455
456 fast_evt_data = container_of(evtp, struct lpfc_fast_path_event,
457 work_evt);
458
459 evt_category = (unsigned long) fast_evt_data->un.fabric_evt.event_type;
460 evt_sub_category = (unsigned long) fast_evt_data->un.
461 fabric_evt.subcategory;
462 shost = lpfc_shost_from_vport(fast_evt_data->vport);
463 if (evt_category == FC_REG_FABRIC_EVENT) {
464 if (evt_sub_category == LPFC_EVENT_FCPRDCHKERR) {
465 evt_data = (char *) &fast_evt_data->un.read_check_error;
466 evt_data_size = sizeof(fast_evt_data->un.
467 read_check_error);
468 } else if ((evt_sub_category == LPFC_EVENT_FABRIC_BUSY) ||
James Smarteaf15d52008-12-04 22:39:29 -0500469 (evt_sub_category == LPFC_EVENT_PORT_BUSY)) {
James Smartea2151b2008-09-07 11:52:10 -0400470 evt_data = (char *) &fast_evt_data->un.fabric_evt;
471 evt_data_size = sizeof(fast_evt_data->un.fabric_evt);
472 } else {
473 lpfc_free_fast_evt(phba, fast_evt_data);
474 return;
475 }
476 } else if (evt_category == FC_REG_SCSI_EVENT) {
477 switch (evt_sub_category) {
478 case LPFC_EVENT_QFULL:
479 case LPFC_EVENT_DEVBSY:
480 evt_data = (char *) &fast_evt_data->un.scsi_evt;
481 evt_data_size = sizeof(fast_evt_data->un.scsi_evt);
482 break;
483 case LPFC_EVENT_CHECK_COND:
484 evt_data = (char *) &fast_evt_data->un.check_cond_evt;
485 evt_data_size = sizeof(fast_evt_data->un.
486 check_cond_evt);
487 break;
488 case LPFC_EVENT_VARQUEDEPTH:
489 evt_data = (char *) &fast_evt_data->un.queue_depth_evt;
490 evt_data_size = sizeof(fast_evt_data->un.
491 queue_depth_evt);
492 break;
493 default:
494 lpfc_free_fast_evt(phba, fast_evt_data);
495 return;
496 }
497 } else {
498 lpfc_free_fast_evt(phba, fast_evt_data);
499 return;
500 }
501
James Smart895427b2017-02-12 13:52:30 -0800502 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
503 fc_host_post_vendor_event(shost,
504 fc_get_event_number(),
505 evt_data_size,
506 evt_data,
507 LPFC_NL_VENDOR_ID);
James Smartea2151b2008-09-07 11:52:10 -0400508
509 lpfc_free_fast_evt(phba, fast_evt_data);
510 return;
511}
512
dea31012005-04-17 16:05:31 -0500513static void
James Smart2e0fef82007-06-17 19:56:36 -0500514lpfc_work_list_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500515{
516 struct lpfc_work_evt *evtp = NULL;
517 struct lpfc_nodelist *ndlp;
518 int free_evt;
James Smarta93ff372010-10-22 11:06:08 -0400519 int fcf_inuse;
520 uint32_t nlp_did;
dea31012005-04-17 16:05:31 -0500521
James Smart2e0fef82007-06-17 19:56:36 -0500522 spin_lock_irq(&phba->hbalock);
523 while (!list_empty(&phba->work_list)) {
dea31012005-04-17 16:05:31 -0500524 list_remove_head((&phba->work_list), evtp, typeof(*evtp),
525 evt_listp);
James Smart2e0fef82007-06-17 19:56:36 -0500526 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500527 free_evt = 1;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500528 switch (evtp->evt) {
dea31012005-04-17 16:05:31 -0500529 case LPFC_EVT_ELS_RETRY:
James Smart2e0fef82007-06-17 19:56:36 -0500530 ndlp = (struct lpfc_nodelist *) (evtp->evt_arg1);
dea31012005-04-17 16:05:31 -0500531 lpfc_els_retry_delay_handler(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -0500532 free_evt = 0; /* evt is part of ndlp */
James Smartfa4066b2008-01-11 01:53:27 -0500533 /* decrement the node reference count held
534 * for this queued work
535 */
536 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -0500537 break;
James Smart858c9f62007-06-17 19:56:39 -0500538 case LPFC_EVT_DEV_LOSS:
539 ndlp = (struct lpfc_nodelist *)(evtp->evt_arg1);
James Smarta93ff372010-10-22 11:06:08 -0400540 fcf_inuse = lpfc_dev_loss_tmo_handler(ndlp);
James Smart858c9f62007-06-17 19:56:39 -0500541 free_evt = 0;
James Smartfa4066b2008-01-11 01:53:27 -0500542 /* decrement the node reference count held for
543 * this queued work
544 */
James Smarta93ff372010-10-22 11:06:08 -0400545 nlp_did = ndlp->nlp_DID;
James Smart858c9f62007-06-17 19:56:39 -0500546 lpfc_nlp_put(ndlp);
James Smarta93ff372010-10-22 11:06:08 -0400547 if (phba->sli_rev == LPFC_SLI_REV4)
548 lpfc_sli4_post_dev_loss_tmo_handler(phba,
549 fcf_inuse,
550 nlp_did);
James Smart858c9f62007-06-17 19:56:39 -0500551 break;
dea31012005-04-17 16:05:31 -0500552 case LPFC_EVT_ONLINE:
James Smart2e0fef82007-06-17 19:56:36 -0500553 if (phba->link_state < LPFC_LINK_DOWN)
554 *(int *) (evtp->evt_arg1) = lpfc_online(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500555 else
James Smart2e0fef82007-06-17 19:56:36 -0500556 *(int *) (evtp->evt_arg1) = 0;
dea31012005-04-17 16:05:31 -0500557 complete((struct completion *)(evtp->evt_arg2));
558 break;
James Smart46fa3112007-04-25 09:51:45 -0400559 case LPFC_EVT_OFFLINE_PREP:
James Smart2e0fef82007-06-17 19:56:36 -0500560 if (phba->link_state >= LPFC_LINK_DOWN)
James Smart618a5232012-06-12 13:54:36 -0400561 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
James Smart46fa3112007-04-25 09:51:45 -0400562 *(int *)(evtp->evt_arg1) = 0;
563 complete((struct completion *)(evtp->evt_arg2));
564 break;
565 case LPFC_EVT_OFFLINE:
566 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500567 lpfc_sli_brdrestart(phba);
568 *(int *)(evtp->evt_arg1) =
James Smart46fa3112007-04-25 09:51:45 -0400569 lpfc_sli_brdready(phba, HS_FFRDY | HS_MBRDY);
570 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500571 complete((struct completion *)(evtp->evt_arg2));
572 break;
573 case LPFC_EVT_WARM_START:
James Smart46fa3112007-04-25 09:51:45 -0400574 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500575 lpfc_reset_barrier(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500576 lpfc_sli_brdreset(phba);
577 lpfc_hba_down_post(phba);
578 *(int *)(evtp->evt_arg1) =
579 lpfc_sli_brdready(phba, HS_MBRDY);
James Smart46fa3112007-04-25 09:51:45 -0400580 lpfc_unblock_mgmt_io(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500581 complete((struct completion *)(evtp->evt_arg2));
582 break;
583 case LPFC_EVT_KILL:
James Smart46fa3112007-04-25 09:51:45 -0400584 lpfc_offline(phba);
James Smart92908312006-03-07 15:04:13 -0500585 *(int *)(evtp->evt_arg1)
James Smart2e0fef82007-06-17 19:56:36 -0500586 = (phba->pport->stopped)
587 ? 0 : lpfc_sli_brdkill(phba);
James Smart46fa3112007-04-25 09:51:45 -0400588 lpfc_unblock_mgmt_io(phba);
dea31012005-04-17 16:05:31 -0500589 complete((struct completion *)(evtp->evt_arg2));
590 break;
James Smartea2151b2008-09-07 11:52:10 -0400591 case LPFC_EVT_FASTPATH_MGMT_EVT:
592 lpfc_send_fastpath_evt(phba, evtp);
593 free_evt = 0;
594 break;
James Smart78730cf2010-04-06 15:06:30 -0400595 case LPFC_EVT_RESET_HBA:
596 if (!(phba->pport->load_flag & FC_UNLOADING))
597 lpfc_reset_hba(phba);
598 break;
dea31012005-04-17 16:05:31 -0500599 }
600 if (free_evt)
601 kfree(evtp);
James Smart2e0fef82007-06-17 19:56:36 -0500602 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500603 }
James Smart2e0fef82007-06-17 19:56:36 -0500604 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500605
606}
607
James Smart311464e2007-08-02 11:10:37 -0400608static void
James Smart2e0fef82007-06-17 19:56:36 -0500609lpfc_work_done(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500610{
611 struct lpfc_sli_ring *pring;
James Smart858c9f62007-06-17 19:56:39 -0500612 uint32_t ha_copy, status, control, work_port_events;
James Smart549e55c2007-08-02 11:09:51 -0400613 struct lpfc_vport **vports;
James Smart51ef4c22007-08-02 11:10:31 -0400614 struct lpfc_vport *vport;
James Smart549e55c2007-08-02 11:09:51 -0400615 int i;
dea31012005-04-17 16:05:31 -0500616
James Smart2e0fef82007-06-17 19:56:36 -0500617 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500618 ha_copy = phba->work_ha;
619 phba->work_ha = 0;
James Smart2e0fef82007-06-17 19:56:36 -0500620 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500621
James Smartda0436e2009-05-22 14:51:39 -0400622 /* First, try to post the next mailbox command to SLI4 device */
623 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC)
624 lpfc_sli4_post_async_mbox(phba);
625
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500626 if (ha_copy & HA_ERATT)
James Smart93996272008-08-24 21:50:30 -0400627 /* Handle the error attention event */
dea31012005-04-17 16:05:31 -0500628 lpfc_handle_eratt(phba);
629
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500630 if (ha_copy & HA_MBATT)
dea31012005-04-17 16:05:31 -0500631 lpfc_sli_handle_mb_event(phba);
632
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500633 if (ha_copy & HA_LATT)
dea31012005-04-17 16:05:31 -0500634 lpfc_handle_latt(phba);
James Smart93996272008-08-24 21:50:30 -0400635
James Smartda0436e2009-05-22 14:51:39 -0400636 /* Process SLI4 events */
637 if (phba->pci_dev_grp == LPFC_PCI_DEV_OC) {
James Smart19ca7602010-11-20 23:11:55 -0500638 if (phba->hba_flag & HBA_RRQ_ACTIVE)
639 lpfc_handle_rrq_active(phba);
James Smartda0436e2009-05-22 14:51:39 -0400640 if (phba->hba_flag & FCP_XRI_ABORT_EVENT)
641 lpfc_sli4_fcp_xri_abort_event_proc(phba);
642 if (phba->hba_flag & ELS_XRI_ABORT_EVENT)
643 lpfc_sli4_els_xri_abort_event_proc(phba);
644 if (phba->hba_flag & ASYNC_EVENT)
645 lpfc_sli4_async_event_proc(phba);
646 if (phba->hba_flag & HBA_POST_RECEIVE_BUFFER) {
647 spin_lock_irq(&phba->hbalock);
648 phba->hba_flag &= ~HBA_POST_RECEIVE_BUFFER;
649 spin_unlock_irq(&phba->hbalock);
650 lpfc_sli_hbqbuf_add_hbqs(phba, LPFC_ELS_HBQ);
651 }
James Smartecfd03c2010-02-12 14:41:27 -0500652 if (phba->fcf.fcf_flag & FCF_REDISC_EVT)
653 lpfc_sli4_fcf_redisc_event_proc(phba);
James Smartda0436e2009-05-22 14:51:39 -0400654 }
655
James Smart549e55c2007-08-02 11:09:51 -0400656 vports = lpfc_create_vport_work_array(phba);
657 if (vports != NULL)
James Smartda0436e2009-05-22 14:51:39 -0400658 for (i = 0; i <= phba->max_vports; i++) {
James Smart51ef4c22007-08-02 11:10:31 -0400659 /*
660 * We could have no vports in array if unloading, so if
661 * this happens then just use the pport
662 */
663 if (vports[i] == NULL && i == 0)
664 vport = phba->pport;
665 else
666 vport = vports[i];
667 if (vport == NULL)
668 break;
James Smart58da1ff2008-04-07 10:15:56 -0400669 spin_lock_irq(&vport->work_port_lock);
James Smart51ef4c22007-08-02 11:10:31 -0400670 work_port_events = vport->work_port_events;
James Smart58da1ff2008-04-07 10:15:56 -0400671 vport->work_port_events &= ~work_port_events;
672 spin_unlock_irq(&vport->work_port_lock);
James Smart549e55c2007-08-02 11:09:51 -0400673 if (work_port_events & WORKER_DISC_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400674 lpfc_disc_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400675 if (work_port_events & WORKER_ELS_TMO)
James Smart51ef4c22007-08-02 11:10:31 -0400676 lpfc_els_timeout_handler(vport);
James Smart549e55c2007-08-02 11:09:51 -0400677 if (work_port_events & WORKER_HB_TMO)
678 lpfc_hb_timeout_handler(phba);
679 if (work_port_events & WORKER_MBOX_TMO)
680 lpfc_mbox_timeout_handler(phba);
681 if (work_port_events & WORKER_FABRIC_BLOCK_TMO)
682 lpfc_unblock_fabric_iocbs(phba);
James Smart549e55c2007-08-02 11:09:51 -0400683 if (work_port_events & WORKER_RAMP_DOWN_QUEUE)
684 lpfc_ramp_down_queue_handler(phba);
James Smart92494142011-02-16 12:39:44 -0500685 if (work_port_events & WORKER_DELAYED_DISC_TMO)
686 lpfc_delayed_disc_timeout_handler(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500687 }
James Smart09372822008-01-11 01:52:54 -0500688 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500689
James Smart895427b2017-02-12 13:52:30 -0800690 pring = lpfc_phba_elsring(phba);
James Smart858c9f62007-06-17 19:56:39 -0500691 status = (ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
692 status >>= (4*LPFC_ELS_RING);
James Smart4d9ab992009-10-02 15:16:39 -0400693 if ((status & HA_RXMASK) ||
694 (pring->flag & LPFC_DEFERRED_RING_EVENT) ||
James Smart45ed1192009-10-02 15:17:02 -0400695 (phba->hba_flag & HBA_SP_QUEUE_EVT)) {
James Smart0b727fe2007-10-27 13:37:25 -0400696 if (pring->flag & LPFC_STOP_IOCB_EVENT) {
James Smart858c9f62007-06-17 19:56:39 -0500697 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -0400698 /* Set the lpfc data pending flag */
699 set_bit(LPFC_DATA_READY, &phba->data_flags);
James Smart858c9f62007-06-17 19:56:39 -0500700 } else {
James Smart94661502013-03-01 16:37:29 -0500701 if (phba->link_state >= LPFC_LINK_UP) {
702 pring->flag &= ~LPFC_DEFERRED_RING_EVENT;
703 lpfc_sli_handle_slow_ring_event(phba, pring,
704 (status &
705 HA_RXMASK));
706 }
James Smart858c9f62007-06-17 19:56:39 -0500707 }
Sebastian Herbsztb1039182015-07-22 10:53:22 +0200708 if ((phba->sli_rev == LPFC_SLI_REV4) &&
James Smart0e9bb8d2013-03-01 16:35:12 -0500709 (!list_empty(&pring->txq)))
James Smart2a9bf3d2010-06-07 15:24:45 -0400710 lpfc_drain_txq(phba);
James Smart858c9f62007-06-17 19:56:39 -0500711 /*
712 * Turn on Ring interrupts
713 */
James Smart3772a992009-05-22 14:50:54 -0400714 if (phba->sli_rev <= LPFC_SLI_REV3) {
715 spin_lock_irq(&phba->hbalock);
716 control = readl(phba->HCregaddr);
717 if (!(control & (HC_R0INT_ENA << LPFC_ELS_RING))) {
718 lpfc_debugfs_slow_ring_trc(phba,
719 "WRK Enable ring: cntl:x%x hacopy:x%x",
720 control, ha_copy, 0);
James Smarta58cbd52007-08-02 11:09:43 -0400721
James Smart3772a992009-05-22 14:50:54 -0400722 control |= (HC_R0INT_ENA << LPFC_ELS_RING);
723 writel(control, phba->HCregaddr);
724 readl(phba->HCregaddr); /* flush */
725 } else {
726 lpfc_debugfs_slow_ring_trc(phba,
727 "WRK Ring ok: cntl:x%x hacopy:x%x",
728 control, ha_copy, 0);
729 }
730 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -0500731 }
732 }
James Smart2e0fef82007-06-17 19:56:36 -0500733 lpfc_work_list_done(phba);
dea31012005-04-17 16:05:31 -0500734}
735
dea31012005-04-17 16:05:31 -0500736int
737lpfc_do_work(void *p)
738{
739 struct lpfc_hba *phba = p;
740 int rc;
dea31012005-04-17 16:05:31 -0500741
Dongsheng Yang8698a742014-03-11 18:09:12 +0800742 set_user_nice(current, MIN_NICE);
James Smart043c9562012-05-09 21:17:43 -0400743 current->flags |= PF_NOFREEZE;
James Smart5e9d9b82008-06-14 22:52:53 -0400744 phba->data_flags = 0;
dea31012005-04-17 16:05:31 -0500745
James Smart3a55b532008-12-04 22:38:54 -0500746 while (!kthread_should_stop()) {
James Smart5e9d9b82008-06-14 22:52:53 -0400747 /* wait and check worker queue activities */
748 rc = wait_event_interruptible(phba->work_waitq,
749 (test_and_clear_bit(LPFC_DATA_READY,
750 &phba->data_flags)
751 || kthread_should_stop()));
James Smart3a55b532008-12-04 22:38:54 -0500752 /* Signal wakeup shall terminate the worker thread */
753 if (rc) {
754 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
755 "0433 Wakeup on signal: rc=x%x\n", rc);
dea31012005-04-17 16:05:31 -0500756 break;
James Smart3a55b532008-12-04 22:38:54 -0500757 }
dea31012005-04-17 16:05:31 -0500758
James Smart5e9d9b82008-06-14 22:52:53 -0400759 /* Attend pending lpfc data processing */
dea31012005-04-17 16:05:31 -0500760 lpfc_work_done(phba);
dea31012005-04-17 16:05:31 -0500761 }
James Smart3a55b532008-12-04 22:38:54 -0500762 phba->worker_thread = NULL;
763 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
764 "0432 Worker thread stopped.\n");
dea31012005-04-17 16:05:31 -0500765 return 0;
766}
767
768/*
769 * This is only called to handle FC worker events. Since this a rare
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300770 * occurrence, we allocate a struct lpfc_work_evt structure here instead of
dea31012005-04-17 16:05:31 -0500771 * embedding it in the IOCB.
772 */
773int
James Smart2e0fef82007-06-17 19:56:36 -0500774lpfc_workq_post_event(struct lpfc_hba *phba, void *arg1, void *arg2,
dea31012005-04-17 16:05:31 -0500775 uint32_t evt)
776{
777 struct lpfc_work_evt *evtp;
James Smarted957682007-06-17 19:56:37 -0500778 unsigned long flags;
dea31012005-04-17 16:05:31 -0500779
780 /*
781 * All Mailbox completions and LPFC_ELS_RING rcv ring IOCB events will
782 * be queued to worker thread for processing
783 */
James Smart92d7f7b2007-06-17 19:56:38 -0500784 evtp = kmalloc(sizeof(struct lpfc_work_evt), GFP_ATOMIC);
dea31012005-04-17 16:05:31 -0500785 if (!evtp)
786 return 0;
787
788 evtp->evt_arg1 = arg1;
789 evtp->evt_arg2 = arg2;
790 evtp->evt = evt;
791
James Smarted957682007-06-17 19:56:37 -0500792 spin_lock_irqsave(&phba->hbalock, flags);
James Smart071fbd3d2006-04-15 11:53:20 -0400793 list_add_tail(&evtp->evt_listp, &phba->work_list);
James Smarted957682007-06-17 19:56:37 -0500794 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -0500795
James Smart5e9d9b82008-06-14 22:52:53 -0400796 lpfc_worker_wake_up(phba);
797
dea31012005-04-17 16:05:31 -0500798 return 1;
799}
800
James Smart92d7f7b2007-06-17 19:56:38 -0500801void
802lpfc_cleanup_rpis(struct lpfc_vport *vport, int remove)
803{
James Smart09372822008-01-11 01:52:54 -0500804 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500805 struct lpfc_hba *phba = vport->phba;
806 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -0500807
808 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500809 if (!NLP_CHK_NODE_ACT(ndlp))
810 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500811 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
812 continue;
James Smart98c9ea52007-10-27 13:37:33 -0400813 if ((phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) ||
814 ((vport->port_type == LPFC_NPIV_PORT) &&
815 (ndlp->nlp_DID == NameServer_DID)))
James Smart92d7f7b2007-06-17 19:56:38 -0500816 lpfc_unreg_rpi(vport, ndlp);
817
818 /* Leave Fabric nodes alone on link down */
James Smart4d9ab992009-10-02 15:16:39 -0400819 if ((phba->sli_rev < LPFC_SLI_REV4) &&
820 (!remove && ndlp->nlp_type & NLP_FABRIC))
James Smart92d7f7b2007-06-17 19:56:38 -0500821 continue;
Sebastian Herbsztdb6f1c22015-08-31 16:48:14 -0400822 lpfc_disc_state_machine(vport, ndlp, NULL,
823 remove
824 ? NLP_EVT_DEVICE_RM
825 : NLP_EVT_DEVICE_RECOVERY);
James Smart92d7f7b2007-06-17 19:56:38 -0500826 }
827 if (phba->sli3_options & LPFC_SLI3_VPORT_TEARDOWN) {
James Smart5af5eee2010-10-22 11:06:38 -0400828 if (phba->sli_rev == LPFC_SLI_REV4)
829 lpfc_sli4_unreg_all_rpis(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500830 lpfc_mbx_unreg_vpi(vport);
James Smart09372822008-01-11 01:52:54 -0500831 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500832 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart09372822008-01-11 01:52:54 -0500833 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500834 }
835}
836
James Smart87af33f2007-10-27 13:37:43 -0400837void
James Smart98c9ea52007-10-27 13:37:33 -0400838lpfc_port_link_failure(struct lpfc_vport *vport)
James Smart92d7f7b2007-06-17 19:56:38 -0500839{
James Smart695a8142010-01-26 23:08:03 -0500840 lpfc_vport_set_state(vport, FC_VPORT_LINKDOWN);
841
James Smart45ed1192009-10-02 15:17:02 -0400842 /* Cleanup any outstanding received buffers */
843 lpfc_cleanup_rcv_buffers(vport);
844
James Smart92d7f7b2007-06-17 19:56:38 -0500845 /* Cleanup any outstanding RSCN activity */
846 lpfc_els_flush_rscn(vport);
847
848 /* Cleanup any outstanding ELS commands */
849 lpfc_els_flush_cmd(vport);
850
851 lpfc_cleanup_rpis(vport, 0);
852
James Smart92d7f7b2007-06-17 19:56:38 -0500853 /* Turn off discovery timer if its running */
854 lpfc_can_disctmo(vport);
855}
856
James Smart3772a992009-05-22 14:50:54 -0400857void
James Smart98c9ea52007-10-27 13:37:33 -0400858lpfc_linkdown_port(struct lpfc_vport *vport)
859{
James Smarta0f2d3e2017-02-12 13:52:31 -0800860 struct lpfc_hba *phba = vport->phba;
James Smart98c9ea52007-10-27 13:37:33 -0400861 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
862
James Smarta0f2d3e2017-02-12 13:52:31 -0800863 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
864 fc_host_post_event(shost, fc_get_event_number(),
865 FCH_EVT_LINKDOWN, 0);
James Smart98c9ea52007-10-27 13:37:33 -0400866
867 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
868 "Link Down: state:x%x rtry:x%x flg:x%x",
869 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
870
871 lpfc_port_link_failure(vport);
872
James Smart92494142011-02-16 12:39:44 -0500873 /* Stop delayed Nport discovery */
874 spin_lock_irq(shost->host_lock);
875 vport->fc_flag &= ~FC_DISC_DELAYED;
876 spin_unlock_irq(shost->host_lock);
877 del_timer_sync(&vport->delayed_disc_tmo);
James Smart98c9ea52007-10-27 13:37:33 -0400878}
879
dea31012005-04-17 16:05:31 -0500880int
James Smart685f0bf2007-04-25 09:53:08 -0400881lpfc_linkdown(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500882{
James Smart2e0fef82007-06-17 19:56:36 -0500883 struct lpfc_vport *vport = phba->pport;
884 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart549e55c2007-08-02 11:09:51 -0400885 struct lpfc_vport **vports;
James Smart685f0bf2007-04-25 09:53:08 -0400886 LPFC_MBOXQ_t *mb;
James Smart549e55c2007-08-02 11:09:51 -0400887 int i;
dea31012005-04-17 16:05:31 -0500888
James Smart3163f722008-02-08 18:50:25 -0500889 if (phba->link_state == LPFC_LINK_DOWN)
James Smart2e0fef82007-06-17 19:56:36 -0500890 return 0;
James Smartaacc20e2009-12-21 17:02:51 -0500891
892 /* Block all SCSI stack I/Os */
893 lpfc_scsi_dev_block(phba);
894
James Smart2e0fef82007-06-17 19:56:36 -0500895 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -0500896 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
James Smart72100cc2010-02-12 14:43:01 -0500897 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -0500898 if (phba->link_state > LPFC_LINK_DOWN) {
James Smart2e0fef82007-06-17 19:56:36 -0500899 phba->link_state = LPFC_LINK_DOWN;
James Smart72100cc2010-02-12 14:43:01 -0500900 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500901 phba->pport->fc_flag &= ~FC_LBIT;
James Smart72100cc2010-02-12 14:43:01 -0500902 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500903 }
James Smart549e55c2007-08-02 11:09:51 -0400904 vports = lpfc_create_vport_work_array(phba);
James Smart895427b2017-02-12 13:52:30 -0800905 if (vports != NULL) {
James Smart6fb120a2009-05-22 14:52:59 -0400906 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400907 /* Issue a LINK DOWN event to all nodes */
908 lpfc_linkdown_port(vports[i]);
James Smart895427b2017-02-12 13:52:30 -0800909
910 vports[i]->fc_myDID = 0;
911
912 /* todo: init: revise localport nvme attributes */
James Smart549e55c2007-08-02 11:09:51 -0400913 }
James Smart895427b2017-02-12 13:52:30 -0800914 }
James Smart09372822008-01-11 01:52:54 -0500915 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -0500916 /* Clean up any firmware default rpi's */
James Smart2e0fef82007-06-17 19:56:36 -0500917 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
918 if (mb) {
James Smart6d368e52011-05-24 11:44:12 -0400919 lpfc_unreg_did(phba, 0xffff, LPFC_UNREG_ALL_DFLT_RPIS, mb);
James Smarted957682007-06-17 19:56:37 -0500920 mb->vport = vport;
James Smart2e0fef82007-06-17 19:56:36 -0500921 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -0400922 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500923 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500924 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500925 }
926 }
927
dea31012005-04-17 16:05:31 -0500928 /* Setup myDID for link up if we are in pt2pt mode */
James Smart92d7f7b2007-06-17 19:56:38 -0500929 if (phba->pport->fc_flag & FC_PT2PT) {
James Smart2e0fef82007-06-17 19:56:36 -0500930 mb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
931 if (mb) {
dea31012005-04-17 16:05:31 -0500932 lpfc_config_link(phba, mb);
James Smart92d7f7b2007-06-17 19:56:38 -0500933 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smarted957682007-06-17 19:56:37 -0500934 mb->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -0400935 if (lpfc_sli_issue_mbox(phba, mb, MBX_NOWAIT)
dea31012005-04-17 16:05:31 -0500936 == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -0500937 mempool_free(mb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500938 }
939 }
James Smart2e0fef82007-06-17 19:56:36 -0500940 spin_lock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -0500941 phba->pport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI);
James Smart2e0fef82007-06-17 19:56:36 -0500942 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500943 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -0500944 return 0;
dea31012005-04-17 16:05:31 -0500945}
946
James Smart92d7f7b2007-06-17 19:56:38 -0500947static void
948lpfc_linkup_cleanup_nodes(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500949{
James Smart92d7f7b2007-06-17 19:56:38 -0500950 struct lpfc_nodelist *ndlp;
951
952 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -0500953 if (!NLP_CHK_NODE_ACT(ndlp))
954 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500955 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
956 continue;
James Smart92d7f7b2007-06-17 19:56:38 -0500957 if (ndlp->nlp_type & NLP_FABRIC) {
James Smarte47c9092008-02-08 18:49:26 -0500958 /* On Linkup its safe to clean up the ndlp
959 * from Fabric connections.
960 */
James Smart92d7f7b2007-06-17 19:56:38 -0500961 if (ndlp->nlp_DID != Fabric_DID)
962 lpfc_unreg_rpi(vport, ndlp);
963 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
964 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
James Smarte47c9092008-02-08 18:49:26 -0500965 /* Fail outstanding IO now since device is
966 * marked for PLOGI.
967 */
James Smart92d7f7b2007-06-17 19:56:38 -0500968 lpfc_unreg_rpi(vport, ndlp);
969 }
970 }
971}
972
973static void
974lpfc_linkup_port(struct lpfc_vport *vport)
975{
976 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart92d7f7b2007-06-17 19:56:38 -0500977 struct lpfc_hba *phba = vport->phba;
978
979 if ((vport->load_flag & FC_UNLOADING) != 0)
980 return;
981
James Smart858c9f62007-06-17 19:56:39 -0500982 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
983 "Link Up: top:x%x speed:x%x flg:x%x",
984 phba->fc_topology, phba->fc_linkspeed, phba->link_flag);
985
James Smart92d7f7b2007-06-17 19:56:38 -0500986 /* If NPIV is not enabled, only bring the physical port up */
987 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
988 (vport != phba->pport))
989 return;
dea31012005-04-17 16:05:31 -0500990
James Smarta0f2d3e2017-02-12 13:52:31 -0800991 if (phba->cfg_enable_fc4_type != LPFC_ENABLE_NVME)
992 fc_host_post_event(shost, fc_get_event_number(),
993 FCH_EVT_LINKUP, 0);
James Smartd2873e42006-08-18 17:46:43 -0400994
James Smart2e0fef82007-06-17 19:56:36 -0500995 spin_lock_irq(shost->host_lock);
James Smart2e0fef82007-06-17 19:56:36 -0500996 vport->fc_flag &= ~(FC_PT2PT | FC_PT2PT_PLOGI | FC_ABORT_DISCOVERY |
997 FC_RSCN_MODE | FC_NLP_MORE | FC_RSCN_DISCOVERY);
998 vport->fc_flag |= FC_NDISC_ACTIVE;
999 vport->fc_ns_retry = 0;
1000 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001001
James Smart92d7f7b2007-06-17 19:56:38 -05001002 if (vport->fc_flag & FC_LBIT)
1003 lpfc_linkup_cleanup_nodes(vport);
dea31012005-04-17 16:05:31 -05001004
James Smart92d7f7b2007-06-17 19:56:38 -05001005}
1006
1007static int
1008lpfc_linkup(struct lpfc_hba *phba)
1009{
James Smart549e55c2007-08-02 11:09:51 -04001010 struct lpfc_vport **vports;
1011 int i;
James Smart92d7f7b2007-06-17 19:56:38 -05001012
1013 phba->link_state = LPFC_LINK_UP;
1014
1015 /* Unblock fabric iocbs if they are blocked */
1016 clear_bit(FABRIC_COMANDS_BLOCKED, &phba->bit_flags);
1017 del_timer_sync(&phba->fabric_block_timer);
1018
James Smart549e55c2007-08-02 11:09:51 -04001019 vports = lpfc_create_vport_work_array(phba);
1020 if (vports != NULL)
James Smart6fb120a2009-05-22 14:52:59 -04001021 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
James Smart549e55c2007-08-02 11:09:51 -04001022 lpfc_linkup_port(vports[i]);
James Smart09372822008-01-11 01:52:54 -05001023 lpfc_destroy_vport_work_array(phba, vports);
dea31012005-04-17 16:05:31 -05001024
1025 return 0;
1026}
1027
1028/*
1029 * This routine handles processing a CLEAR_LA mailbox
1030 * command upon completion. It is setup in the LPFC_MBOXQ
1031 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08001032 * handed off to the SLI layer. SLI3 only.
dea31012005-04-17 16:05:31 -05001033 */
Adrian Bunka6ababd2007-11-05 18:07:33 +01001034static void
James Smart2e0fef82007-06-17 19:56:36 -05001035lpfc_mbx_cmpl_clear_la(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001036{
James Smart2e0fef82007-06-17 19:56:36 -05001037 struct lpfc_vport *vport = pmb->vport;
1038 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
1039 struct lpfc_sli *psli = &phba->sli;
James Smart04c68492009-05-22 14:52:52 -04001040 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001041 uint32_t control;
1042
dea31012005-04-17 16:05:31 -05001043 /* Since we don't do discovery right now, turn these off here */
James Smart895427b2017-02-12 13:52:30 -08001044 psli->sli3_ring[LPFC_EXTRA_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1045 psli->sli3_ring[LPFC_FCP_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -05001046
1047 /* Check for error */
1048 if ((mb->mbxStatus) && (mb->mbxStatus != 0x1601)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001049 /* CLEAR_LA mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04001050 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1051 "0320 CLEAR_LA mbxStatus error x%x hba "
1052 "state x%x\n",
1053 mb->mbxStatus, vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05001054 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001055 goto out;
1056 }
1057
James Smart92d7f7b2007-06-17 19:56:38 -05001058 if (vport->port_type == LPFC_PHYSICAL_PORT)
1059 phba->link_state = LPFC_HBA_READY;
1060
1061 spin_lock_irq(&phba->hbalock);
1062 psli->sli_flag |= LPFC_PROCESS_LA;
1063 control = readl(phba->HCregaddr);
1064 control |= HC_LAINT_ENA;
1065 writel(control, phba->HCregaddr);
1066 readl(phba->HCregaddr); /* flush */
1067 spin_unlock_irq(&phba->hbalock);
James Smart1b32f6a2008-02-08 18:49:39 -05001068 mempool_free(pmb, phba->mbox_mem_pool);
James Smart92d7f7b2007-06-17 19:56:38 -05001069 return;
dea31012005-04-17 16:05:31 -05001070
dea31012005-04-17 16:05:31 -05001071out:
1072 /* Device Discovery completes */
James Smarte8b62012007-08-02 11:10:09 -04001073 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
1074 "0225 Device Discovery completes\n");
James Smart2e0fef82007-06-17 19:56:36 -05001075 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001076
James Smart2e0fef82007-06-17 19:56:36 -05001077 spin_lock_irq(shost->host_lock);
James Smart58da1ff2008-04-07 10:15:56 -04001078 vport->fc_flag &= ~FC_ABORT_DISCOVERY;
James Smart2e0fef82007-06-17 19:56:36 -05001079 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001080
James Smart2e0fef82007-06-17 19:56:36 -05001081 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05001082
1083 /* turn on Link Attention interrupts */
James Smart2e0fef82007-06-17 19:56:36 -05001084
1085 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001086 psli->sli_flag |= LPFC_PROCESS_LA;
1087 control = readl(phba->HCregaddr);
1088 control |= HC_LAINT_ENA;
1089 writel(control, phba->HCregaddr);
1090 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05001091 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001092
1093 return;
1094}
1095
James Smart2e0fef82007-06-17 19:56:36 -05001096
James Smartd6de08c2015-12-16 18:11:53 -05001097void
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001098lpfc_mbx_cmpl_local_config_link(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001099{
James Smart2e0fef82007-06-17 19:56:36 -05001100 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05001101
James Smart04c68492009-05-22 14:52:52 -04001102 if (pmb->u.mb.mbxStatus)
dea31012005-04-17 16:05:31 -05001103 goto out;
dea31012005-04-17 16:05:31 -05001104
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001105 mempool_free(pmb, phba->mbox_mem_pool);
1106
James Smart1b511972011-12-13 13:23:09 -05001107 /* don't perform discovery for SLI4 loopback diagnostic test */
1108 if ((phba->sli_rev == LPFC_SLI_REV4) &&
1109 !(phba->hba_flag & HBA_FCOE_MODE) &&
1110 (phba->link_flag & LS_LOOPBACK_MODE))
1111 return;
1112
James Smart76a95d72010-11-20 23:11:48 -05001113 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP &&
James Smart2e0fef82007-06-17 19:56:36 -05001114 vport->fc_flag & FC_PUBLIC_LOOP &&
1115 !(vport->fc_flag & FC_LBIT)) {
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001116 /* Need to wait for FAN - use discovery timer
James Smart2e0fef82007-06-17 19:56:36 -05001117 * for timeout. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001118 * LPFC_LOCAL_CFG_LINK while waiting for FAN
1119 */
James Smart2e0fef82007-06-17 19:56:36 -05001120 lpfc_set_disctmo(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001121 return;
James Smart92d7f7b2007-06-17 19:56:38 -05001122 }
dea31012005-04-17 16:05:31 -05001123
James Smart2e0fef82007-06-17 19:56:36 -05001124 /* Start discovery by sending a FLOGI. port_state is identically
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001125 * LPFC_FLOGI while waiting for FLOGI cmpl
1126 */
James Smartd6de08c2015-12-16 18:11:53 -05001127 if (vport->port_state != LPFC_FLOGI)
James Smart92d7f7b2007-06-17 19:56:38 -05001128 lpfc_initial_flogi(vport);
James Smartd6de08c2015-12-16 18:11:53 -05001129 else if (vport->fc_flag & FC_PT2PT)
1130 lpfc_disc_start(vport);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001131 return;
dea31012005-04-17 16:05:31 -05001132
1133out:
James Smarte8b62012007-08-02 11:10:09 -04001134 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1135 "0306 CONFIG_LINK mbxStatus error x%x "
1136 "HBA state x%x\n",
James Smart04c68492009-05-22 14:52:52 -04001137 pmb->u.mb.mbxStatus, vport->port_state);
James Smart92d7f7b2007-06-17 19:56:38 -05001138 mempool_free(pmb, phba->mbox_mem_pool);
Jamie Wellnitz25594c62006-02-28 19:25:34 -05001139
1140 lpfc_linkdown(phba);
1141
James Smarte8b62012007-08-02 11:10:09 -04001142 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
1143 "0200 CONFIG_LINK bad hba state x%x\n",
1144 vport->port_state);
dea31012005-04-17 16:05:31 -05001145
James Smart92d7f7b2007-06-17 19:56:38 -05001146 lpfc_issue_clear_la(phba, vport);
dea31012005-04-17 16:05:31 -05001147 return;
1148}
1149
James Smart7d791df2011-07-22 18:37:52 -04001150/**
1151 * lpfc_sli4_clear_fcf_rr_bmask
1152 * @phba pointer to the struct lpfc_hba for this port.
1153 * This fucnction resets the round robin bit mask and clears the
1154 * fcf priority list. The list deletions are done while holding the
1155 * hbalock. The ON_LIST flag and the FLOGI_FAILED flags are cleared
1156 * from the lpfc_fcf_pri record.
1157 **/
1158void
1159lpfc_sli4_clear_fcf_rr_bmask(struct lpfc_hba *phba)
1160{
1161 struct lpfc_fcf_pri *fcf_pri;
1162 struct lpfc_fcf_pri *next_fcf_pri;
1163 memset(phba->fcf.fcf_rr_bmask, 0, sizeof(*phba->fcf.fcf_rr_bmask));
1164 spin_lock_irq(&phba->hbalock);
1165 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
1166 &phba->fcf.fcf_pri_list, list) {
1167 list_del_init(&fcf_pri->list);
1168 fcf_pri->fcf_rec.flag = 0;
1169 }
1170 spin_unlock_irq(&phba->hbalock);
1171}
dea31012005-04-17 16:05:31 -05001172static void
James Smart6fb120a2009-05-22 14:52:59 -04001173lpfc_mbx_cmpl_reg_fcfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
1174{
1175 struct lpfc_vport *vport = mboxq->vport;
James Smart6fb120a2009-05-22 14:52:59 -04001176
1177 if (mboxq->u.mb.mbxStatus) {
1178 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
1179 "2017 REG_FCFI mbxStatus error x%x "
1180 "HBA state x%x\n",
1181 mboxq->u.mb.mbxStatus, vport->port_state);
James Smarta93ff372010-10-22 11:06:08 -04001182 goto fail_out;
James Smart6fb120a2009-05-22 14:52:59 -04001183 }
1184
1185 /* Start FCoE discovery by sending a FLOGI. */
1186 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi, &mboxq->u.mqe.un.reg_fcfi);
1187 /* Set the FCFI registered flag */
James Smartd439d282010-09-29 11:18:45 -04001188 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001189 phba->fcf.fcf_flag |= FCF_REGISTERED;
James Smartd439d282010-09-29 11:18:45 -04001190 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001191
James Smart32b97932009-07-19 10:01:21 -04001192 /* If there is a pending FCoE event, restart FCF table scan. */
James Smart7d791df2011-07-22 18:37:52 -04001193 if ((!(phba->hba_flag & FCF_RR_INPROG)) &&
1194 lpfc_check_pending_fcoe_event(phba, LPFC_UNREG_FCF))
James Smarta93ff372010-10-22 11:06:08 -04001195 goto fail_out;
1196
1197 /* Mark successful completion of FCF table scan */
James Smartd439d282010-09-29 11:18:45 -04001198 spin_lock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05001199 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001200 phba->hba_flag &= ~FCF_TS_INPROG;
1201 if (vport->port_state != LPFC_FLOGI) {
1202 phba->hba_flag |= FCF_RR_INPROG;
1203 spin_unlock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05001204 lpfc_issue_init_vfi(vport);
James Smarta93ff372010-10-22 11:06:08 -04001205 goto out;
1206 }
1207 spin_unlock_irq(&phba->hbalock);
1208 goto out;
James Smart6fb120a2009-05-22 14:52:59 -04001209
James Smarta93ff372010-10-22 11:06:08 -04001210fail_out:
1211 spin_lock_irq(&phba->hbalock);
1212 phba->hba_flag &= ~FCF_RR_INPROG;
1213 spin_unlock_irq(&phba->hbalock);
1214out:
James Smart6fb120a2009-05-22 14:52:59 -04001215 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart6fb120a2009-05-22 14:52:59 -04001216}
1217
1218/**
1219 * lpfc_fab_name_match - Check if the fcf fabric name match.
1220 * @fab_name: pointer to fabric name.
1221 * @new_fcf_record: pointer to fcf record.
1222 *
1223 * This routine compare the fcf record's fabric name with provided
1224 * fabric name. If the fabric name are identical this function
1225 * returns 1 else return 0.
1226 **/
1227static uint32_t
1228lpfc_fab_name_match(uint8_t *fab_name, struct fcf_record *new_fcf_record)
1229{
James Smartecfd03c2010-02-12 14:41:27 -05001230 if (fab_name[0] != bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001231 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001232 if (fab_name[1] != bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record))
1233 return 0;
1234 if (fab_name[2] != bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record))
1235 return 0;
1236 if (fab_name[3] != bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record))
1237 return 0;
1238 if (fab_name[4] != bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record))
1239 return 0;
1240 if (fab_name[5] != bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record))
1241 return 0;
1242 if (fab_name[6] != bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record))
1243 return 0;
1244 if (fab_name[7] != bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record))
1245 return 0;
1246 return 1;
James Smart6fb120a2009-05-22 14:52:59 -04001247}
1248
1249/**
James Smart8fa38512009-07-19 10:01:03 -04001250 * lpfc_sw_name_match - Check if the fcf switch name match.
1251 * @fab_name: pointer to fabric name.
1252 * @new_fcf_record: pointer to fcf record.
1253 *
1254 * This routine compare the fcf record's switch name with provided
1255 * switch name. If the switch name are identical this function
1256 * returns 1 else return 0.
1257 **/
1258static uint32_t
1259lpfc_sw_name_match(uint8_t *sw_name, struct fcf_record *new_fcf_record)
1260{
James Smartecfd03c2010-02-12 14:41:27 -05001261 if (sw_name[0] != bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record))
James Smart8fa38512009-07-19 10:01:03 -04001262 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001263 if (sw_name[1] != bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record))
1264 return 0;
1265 if (sw_name[2] != bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record))
1266 return 0;
1267 if (sw_name[3] != bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record))
1268 return 0;
1269 if (sw_name[4] != bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record))
1270 return 0;
1271 if (sw_name[5] != bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record))
1272 return 0;
1273 if (sw_name[6] != bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record))
1274 return 0;
1275 if (sw_name[7] != bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record))
1276 return 0;
1277 return 1;
James Smart8fa38512009-07-19 10:01:03 -04001278}
1279
1280/**
James Smart6fb120a2009-05-22 14:52:59 -04001281 * lpfc_mac_addr_match - Check if the fcf mac address match.
James Smartecfd03c2010-02-12 14:41:27 -05001282 * @mac_addr: pointer to mac address.
James Smart6fb120a2009-05-22 14:52:59 -04001283 * @new_fcf_record: pointer to fcf record.
1284 *
1285 * This routine compare the fcf record's mac address with HBA's
1286 * FCF mac address. If the mac addresses are identical this function
1287 * returns 1 else return 0.
1288 **/
1289static uint32_t
James Smartecfd03c2010-02-12 14:41:27 -05001290lpfc_mac_addr_match(uint8_t *mac_addr, struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001291{
James Smartecfd03c2010-02-12 14:41:27 -05001292 if (mac_addr[0] != bf_get(lpfc_fcf_record_mac_0, new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001293 return 0;
James Smartecfd03c2010-02-12 14:41:27 -05001294 if (mac_addr[1] != bf_get(lpfc_fcf_record_mac_1, new_fcf_record))
1295 return 0;
1296 if (mac_addr[2] != bf_get(lpfc_fcf_record_mac_2, new_fcf_record))
1297 return 0;
1298 if (mac_addr[3] != bf_get(lpfc_fcf_record_mac_3, new_fcf_record))
1299 return 0;
1300 if (mac_addr[4] != bf_get(lpfc_fcf_record_mac_4, new_fcf_record))
1301 return 0;
1302 if (mac_addr[5] != bf_get(lpfc_fcf_record_mac_5, new_fcf_record))
1303 return 0;
1304 return 1;
1305}
1306
1307static bool
1308lpfc_vlan_id_match(uint16_t curr_vlan_id, uint16_t new_vlan_id)
1309{
1310 return (curr_vlan_id == new_vlan_id);
James Smart6fb120a2009-05-22 14:52:59 -04001311}
1312
1313/**
James Smart7d791df2011-07-22 18:37:52 -04001314 * lpfc_update_fcf_record - Update driver fcf record
1315 * __lpfc_update_fcf_record_pri - update the lpfc_fcf_pri record.
1316 * @phba: pointer to lpfc hba data structure.
1317 * @fcf_index: Index for the lpfc_fcf_record.
1318 * @new_fcf_record: pointer to hba fcf record.
1319 *
1320 * This routine updates the driver FCF priority record from the new HBA FCF
1321 * record. This routine is called with the host lock held.
1322 **/
1323static void
1324__lpfc_update_fcf_record_pri(struct lpfc_hba *phba, uint16_t fcf_index,
1325 struct fcf_record *new_fcf_record
1326 )
1327{
1328 struct lpfc_fcf_pri *fcf_pri;
1329
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001330 lockdep_assert_held(&phba->hbalock);
1331
James Smart7d791df2011-07-22 18:37:52 -04001332 fcf_pri = &phba->fcf.fcf_pri[fcf_index];
1333 fcf_pri->fcf_rec.fcf_index = fcf_index;
1334 /* FCF record priority */
1335 fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
1336
1337}
1338
1339/**
James Smart6fb120a2009-05-22 14:52:59 -04001340 * lpfc_copy_fcf_record - Copy fcf information to lpfc_hba.
James Smartecfd03c2010-02-12 14:41:27 -05001341 * @fcf: pointer to driver fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04001342 * @new_fcf_record: pointer to fcf record.
1343 *
1344 * This routine copies the FCF information from the FCF
1345 * record to lpfc_hba data structure.
1346 **/
1347static void
James Smartecfd03c2010-02-12 14:41:27 -05001348lpfc_copy_fcf_record(struct lpfc_fcf_rec *fcf_rec,
1349 struct fcf_record *new_fcf_record)
James Smart6fb120a2009-05-22 14:52:59 -04001350{
James Smartecfd03c2010-02-12 14:41:27 -05001351 /* Fabric name */
1352 fcf_rec->fabric_name[0] =
James Smart6fb120a2009-05-22 14:52:59 -04001353 bf_get(lpfc_fcf_record_fab_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001354 fcf_rec->fabric_name[1] =
James Smart6fb120a2009-05-22 14:52:59 -04001355 bf_get(lpfc_fcf_record_fab_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001356 fcf_rec->fabric_name[2] =
James Smart6fb120a2009-05-22 14:52:59 -04001357 bf_get(lpfc_fcf_record_fab_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001358 fcf_rec->fabric_name[3] =
James Smart6fb120a2009-05-22 14:52:59 -04001359 bf_get(lpfc_fcf_record_fab_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001360 fcf_rec->fabric_name[4] =
James Smart6fb120a2009-05-22 14:52:59 -04001361 bf_get(lpfc_fcf_record_fab_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001362 fcf_rec->fabric_name[5] =
James Smart6fb120a2009-05-22 14:52:59 -04001363 bf_get(lpfc_fcf_record_fab_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001364 fcf_rec->fabric_name[6] =
James Smart6fb120a2009-05-22 14:52:59 -04001365 bf_get(lpfc_fcf_record_fab_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001366 fcf_rec->fabric_name[7] =
James Smart6fb120a2009-05-22 14:52:59 -04001367 bf_get(lpfc_fcf_record_fab_name_7, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001368 /* Mac address */
1369 fcf_rec->mac_addr[0] = bf_get(lpfc_fcf_record_mac_0, new_fcf_record);
1370 fcf_rec->mac_addr[1] = bf_get(lpfc_fcf_record_mac_1, new_fcf_record);
1371 fcf_rec->mac_addr[2] = bf_get(lpfc_fcf_record_mac_2, new_fcf_record);
1372 fcf_rec->mac_addr[3] = bf_get(lpfc_fcf_record_mac_3, new_fcf_record);
1373 fcf_rec->mac_addr[4] = bf_get(lpfc_fcf_record_mac_4, new_fcf_record);
1374 fcf_rec->mac_addr[5] = bf_get(lpfc_fcf_record_mac_5, new_fcf_record);
1375 /* FCF record index */
1376 fcf_rec->fcf_indx = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
1377 /* FCF record priority */
1378 fcf_rec->priority = new_fcf_record->fip_priority;
1379 /* Switch name */
1380 fcf_rec->switch_name[0] =
James Smart8fa38512009-07-19 10:01:03 -04001381 bf_get(lpfc_fcf_record_switch_name_0, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001382 fcf_rec->switch_name[1] =
James Smart8fa38512009-07-19 10:01:03 -04001383 bf_get(lpfc_fcf_record_switch_name_1, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001384 fcf_rec->switch_name[2] =
James Smart8fa38512009-07-19 10:01:03 -04001385 bf_get(lpfc_fcf_record_switch_name_2, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001386 fcf_rec->switch_name[3] =
James Smart8fa38512009-07-19 10:01:03 -04001387 bf_get(lpfc_fcf_record_switch_name_3, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001388 fcf_rec->switch_name[4] =
James Smart8fa38512009-07-19 10:01:03 -04001389 bf_get(lpfc_fcf_record_switch_name_4, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001390 fcf_rec->switch_name[5] =
James Smart8fa38512009-07-19 10:01:03 -04001391 bf_get(lpfc_fcf_record_switch_name_5, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001392 fcf_rec->switch_name[6] =
James Smart8fa38512009-07-19 10:01:03 -04001393 bf_get(lpfc_fcf_record_switch_name_6, new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001394 fcf_rec->switch_name[7] =
James Smart8fa38512009-07-19 10:01:03 -04001395 bf_get(lpfc_fcf_record_switch_name_7, new_fcf_record);
James Smart6fb120a2009-05-22 14:52:59 -04001396}
1397
1398/**
James Smartecfd03c2010-02-12 14:41:27 -05001399 * lpfc_update_fcf_record - Update driver fcf record
1400 * @phba: pointer to lpfc hba data structure.
1401 * @fcf_rec: pointer to driver fcf record.
1402 * @new_fcf_record: pointer to hba fcf record.
1403 * @addr_mode: address mode to be set to the driver fcf record.
1404 * @vlan_id: vlan tag to be set to the driver fcf record.
1405 * @flag: flag bits to be set to the driver fcf record.
1406 *
1407 * This routine updates the driver FCF record from the new HBA FCF record
1408 * together with the address mode, vlan_id, and other informations. This
1409 * routine is called with the host lock held.
1410 **/
1411static void
1412__lpfc_update_fcf_record(struct lpfc_hba *phba, struct lpfc_fcf_rec *fcf_rec,
1413 struct fcf_record *new_fcf_record, uint32_t addr_mode,
1414 uint16_t vlan_id, uint32_t flag)
1415{
Johannes Thumshirn1c2ba472016-01-20 16:22:22 +01001416 lockdep_assert_held(&phba->hbalock);
1417
James Smartecfd03c2010-02-12 14:41:27 -05001418 /* Copy the fields from the HBA's FCF record */
1419 lpfc_copy_fcf_record(fcf_rec, new_fcf_record);
1420 /* Update other fields of driver FCF record */
1421 fcf_rec->addr_mode = addr_mode;
1422 fcf_rec->vlan_id = vlan_id;
1423 fcf_rec->flag |= (flag | RECORD_VALID);
James Smart7d791df2011-07-22 18:37:52 -04001424 __lpfc_update_fcf_record_pri(phba,
1425 bf_get(lpfc_fcf_record_fcf_index, new_fcf_record),
1426 new_fcf_record);
James Smartecfd03c2010-02-12 14:41:27 -05001427}
1428
1429/**
James Smart6fb120a2009-05-22 14:52:59 -04001430 * lpfc_register_fcf - Register the FCF with hba.
1431 * @phba: pointer to lpfc hba data structure.
1432 *
1433 * This routine issues a register fcfi mailbox command to register
1434 * the fcf with HBA.
1435 **/
1436static void
1437lpfc_register_fcf(struct lpfc_hba *phba)
1438{
1439 LPFC_MBOXQ_t *fcf_mbxq;
1440 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04001441
James Smartd439d282010-09-29 11:18:45 -04001442 spin_lock_irq(&phba->hbalock);
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001443 /* If the FCF is not available do nothing. */
James Smart6fb120a2009-05-22 14:52:59 -04001444 if (!(phba->fcf.fcf_flag & FCF_AVAILABLE)) {
James Smarta93ff372010-10-22 11:06:08 -04001445 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001446 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001447 return;
1448 }
1449
1450 /* The FCF is already registered, start discovery */
1451 if (phba->fcf.fcf_flag & FCF_REGISTERED) {
James Smartecfd03c2010-02-12 14:41:27 -05001452 phba->fcf.fcf_flag |= (FCF_SCAN_DONE | FCF_IN_USE);
James Smarta93ff372010-10-22 11:06:08 -04001453 phba->hba_flag &= ~FCF_TS_INPROG;
James Smarte74c03c2013-04-17 20:15:19 -04001454 if (phba->pport->port_state != LPFC_FLOGI &&
1455 phba->pport->fc_flag & FC_FABRIC) {
James Smarta93ff372010-10-22 11:06:08 -04001456 phba->hba_flag |= FCF_RR_INPROG;
1457 spin_unlock_irq(&phba->hbalock);
James Smartbf086112011-08-21 21:48:13 -04001458 lpfc_initial_flogi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04001459 return;
1460 }
1461 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001462 return;
1463 }
James Smartd439d282010-09-29 11:18:45 -04001464 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001465
James Smarta93ff372010-10-22 11:06:08 -04001466 fcf_mbxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
James Smart4d9ab992009-10-02 15:16:39 -04001467 if (!fcf_mbxq) {
James Smartd439d282010-09-29 11:18:45 -04001468 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001469 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001470 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001471 return;
James Smart4d9ab992009-10-02 15:16:39 -04001472 }
James Smart6fb120a2009-05-22 14:52:59 -04001473
1474 lpfc_reg_fcfi(phba, fcf_mbxq);
1475 fcf_mbxq->vport = phba->pport;
1476 fcf_mbxq->mbox_cmpl = lpfc_mbx_cmpl_reg_fcfi;
1477 rc = lpfc_sli_issue_mbox(phba, fcf_mbxq, MBX_NOWAIT);
James Smart4d9ab992009-10-02 15:16:39 -04001478 if (rc == MBX_NOT_FINISHED) {
James Smartd439d282010-09-29 11:18:45 -04001479 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001480 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smartd439d282010-09-29 11:18:45 -04001481 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04001482 mempool_free(fcf_mbxq, phba->mbox_mem_pool);
James Smart4d9ab992009-10-02 15:16:39 -04001483 }
James Smart6fb120a2009-05-22 14:52:59 -04001484
1485 return;
1486}
1487
1488/**
1489 * lpfc_match_fcf_conn_list - Check if the FCF record can be used for discovery.
1490 * @phba: pointer to lpfc hba data structure.
1491 * @new_fcf_record: pointer to fcf record.
1492 * @boot_flag: Indicates if this record used by boot bios.
1493 * @addr_mode: The address mode to be used by this FCF
James Smartecfd03c2010-02-12 14:41:27 -05001494 * @vlan_id: The vlan id to be used as vlan tagging by this FCF.
James Smart6fb120a2009-05-22 14:52:59 -04001495 *
1496 * This routine compare the fcf record with connect list obtained from the
1497 * config region to decide if this FCF can be used for SAN discovery. It returns
1498 * 1 if this record can be used for SAN discovery else return zero. If this FCF
1499 * record can be used for SAN discovery, the boot_flag will indicate if this FCF
1500 * is used by boot bios and addr_mode will indicate the addressing mode to be
1501 * used for this FCF when the function returns.
1502 * If the FCF record need to be used with a particular vlan id, the vlan is
1503 * set in the vlan_id on return of the function. If not VLAN tagging need to
James Smartdbb6b3a2010-06-08 18:31:37 -04001504 * be used with the FCF vlan_id will be set to LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001505 **/
1506static int
1507lpfc_match_fcf_conn_list(struct lpfc_hba *phba,
1508 struct fcf_record *new_fcf_record,
1509 uint32_t *boot_flag, uint32_t *addr_mode,
1510 uint16_t *vlan_id)
1511{
1512 struct lpfc_fcf_conn_entry *conn_entry;
James Smart4d9ab992009-10-02 15:16:39 -04001513 int i, j, fcf_vlan_id = 0;
1514
1515 /* Find the lowest VLAN id in the FCF record */
1516 for (i = 0; i < 512; i++) {
1517 if (new_fcf_record->vlan_bitmap[i]) {
1518 fcf_vlan_id = i * 8;
1519 j = 0;
1520 while (!((new_fcf_record->vlan_bitmap[i] >> j) & 1)) {
1521 j++;
1522 fcf_vlan_id++;
1523 }
1524 break;
1525 }
1526 }
James Smart6fb120a2009-05-22 14:52:59 -04001527
James Smart26979ce2012-09-29 11:31:55 -04001528 /* FCF not valid/available or solicitation in progress */
James Smart0c287582009-06-10 17:22:56 -04001529 if (!bf_get(lpfc_fcf_record_fcf_avail, new_fcf_record) ||
James Smart26979ce2012-09-29 11:31:55 -04001530 !bf_get(lpfc_fcf_record_fcf_valid, new_fcf_record) ||
1531 bf_get(lpfc_fcf_record_fcf_sol, new_fcf_record))
James Smart0c287582009-06-10 17:22:56 -04001532 return 0;
1533
James Smart45ed1192009-10-02 15:17:02 -04001534 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04001535 *boot_flag = 0;
1536 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1537 new_fcf_record);
1538 if (phba->valid_vlan)
1539 *vlan_id = phba->vlan_id;
1540 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001541 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001542 return 1;
1543 }
1544
1545 /*
1546 * If there are no FCF connection table entry, driver connect to all
1547 * FCFs.
1548 */
1549 if (list_empty(&phba->fcf_conn_rec_list)) {
1550 *boot_flag = 0;
1551 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1552 new_fcf_record);
James Smart0c287582009-06-10 17:22:56 -04001553
1554 /*
1555 * When there are no FCF connect entries, use driver's default
1556 * addressing mode - FPMA.
1557 */
1558 if (*addr_mode & LPFC_FCF_FPMA)
1559 *addr_mode = LPFC_FCF_FPMA;
1560
James Smart4d9ab992009-10-02 15:16:39 -04001561 /* If FCF record report a vlan id use that vlan id */
1562 if (fcf_vlan_id)
1563 *vlan_id = fcf_vlan_id;
1564 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001565 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001566 return 1;
1567 }
1568
James Smartecfd03c2010-02-12 14:41:27 -05001569 list_for_each_entry(conn_entry,
1570 &phba->fcf_conn_rec_list, list) {
James Smart6fb120a2009-05-22 14:52:59 -04001571 if (!(conn_entry->conn_rec.flags & FCFCNCT_VALID))
1572 continue;
1573
1574 if ((conn_entry->conn_rec.flags & FCFCNCT_FBNM_VALID) &&
1575 !lpfc_fab_name_match(conn_entry->conn_rec.fabric_name,
James Smart8fa38512009-07-19 10:01:03 -04001576 new_fcf_record))
James Smart6fb120a2009-05-22 14:52:59 -04001577 continue;
James Smart8fa38512009-07-19 10:01:03 -04001578 if ((conn_entry->conn_rec.flags & FCFCNCT_SWNM_VALID) &&
1579 !lpfc_sw_name_match(conn_entry->conn_rec.switch_name,
1580 new_fcf_record))
1581 continue;
James Smart6fb120a2009-05-22 14:52:59 -04001582 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID) {
1583 /*
1584 * If the vlan bit map does not have the bit set for the
1585 * vlan id to be used, then it is not a match.
1586 */
1587 if (!(new_fcf_record->vlan_bitmap
1588 [conn_entry->conn_rec.vlan_tag / 8] &
1589 (1 << (conn_entry->conn_rec.vlan_tag % 8))))
1590 continue;
1591 }
1592
1593 /*
James Smart0c287582009-06-10 17:22:56 -04001594 * If connection record does not support any addressing mode,
1595 * skip the FCF record.
1596 */
1597 if (!(bf_get(lpfc_fcf_record_mac_addr_prov, new_fcf_record)
1598 & (LPFC_FCF_FPMA | LPFC_FCF_SPMA)))
1599 continue;
1600
1601 /*
James Smart6fb120a2009-05-22 14:52:59 -04001602 * Check if the connection record specifies a required
1603 * addressing mode.
1604 */
1605 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1606 !(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)) {
1607
1608 /*
1609 * If SPMA required but FCF not support this continue.
1610 */
1611 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1612 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1613 new_fcf_record) & LPFC_FCF_SPMA))
1614 continue;
1615
1616 /*
1617 * If FPMA required but FCF not support this continue.
1618 */
1619 if (!(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1620 !(bf_get(lpfc_fcf_record_mac_addr_prov,
1621 new_fcf_record) & LPFC_FCF_FPMA))
1622 continue;
1623 }
1624
1625 /*
1626 * This fcf record matches filtering criteria.
1627 */
1628 if (conn_entry->conn_rec.flags & FCFCNCT_BOOT)
1629 *boot_flag = 1;
1630 else
1631 *boot_flag = 0;
1632
James Smart0c287582009-06-10 17:22:56 -04001633 /*
1634 * If user did not specify any addressing mode, or if the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001635 * preferred addressing mode specified by user is not supported
James Smart0c287582009-06-10 17:22:56 -04001636 * by FCF, allow fabric to pick the addressing mode.
1637 */
James Smart6fb120a2009-05-22 14:52:59 -04001638 *addr_mode = bf_get(lpfc_fcf_record_mac_addr_prov,
1639 new_fcf_record);
1640 /*
1641 * If the user specified a required address mode, assign that
1642 * address mode
1643 */
1644 if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1645 (!(conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED)))
1646 *addr_mode = (conn_entry->conn_rec.flags &
1647 FCFCNCT_AM_SPMA) ?
1648 LPFC_FCF_SPMA : LPFC_FCF_FPMA;
1649 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001650 * If the user specified a preferred address mode, use the
James Smart6fb120a2009-05-22 14:52:59 -04001651 * addr mode only if FCF support the addr_mode.
1652 */
1653 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1654 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1655 (conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1656 (*addr_mode & LPFC_FCF_SPMA))
1657 *addr_mode = LPFC_FCF_SPMA;
1658 else if ((conn_entry->conn_rec.flags & FCFCNCT_AM_VALID) &&
1659 (conn_entry->conn_rec.flags & FCFCNCT_AM_PREFERRED) &&
1660 !(conn_entry->conn_rec.flags & FCFCNCT_AM_SPMA) &&
1661 (*addr_mode & LPFC_FCF_FPMA))
1662 *addr_mode = LPFC_FCF_FPMA;
James Smart6fb120a2009-05-22 14:52:59 -04001663
James Smart4d9ab992009-10-02 15:16:39 -04001664 /* If matching connect list has a vlan id, use it */
James Smart6fb120a2009-05-22 14:52:59 -04001665 if (conn_entry->conn_rec.flags & FCFCNCT_VLAN_VALID)
1666 *vlan_id = conn_entry->conn_rec.vlan_tag;
James Smart4d9ab992009-10-02 15:16:39 -04001667 /*
1668 * If no vlan id is specified in connect list, use the vlan id
1669 * in the FCF record
1670 */
1671 else if (fcf_vlan_id)
1672 *vlan_id = fcf_vlan_id;
James Smart6fb120a2009-05-22 14:52:59 -04001673 else
James Smartdbb6b3a2010-06-08 18:31:37 -04001674 *vlan_id = LPFC_FCOE_NULL_VID;
James Smart6fb120a2009-05-22 14:52:59 -04001675
1676 return 1;
1677 }
1678
1679 return 0;
1680}
1681
1682/**
James Smart32b97932009-07-19 10:01:21 -04001683 * lpfc_check_pending_fcoe_event - Check if there is pending fcoe event.
1684 * @phba: pointer to lpfc hba data structure.
1685 * @unreg_fcf: Unregister FCF if FCF table need to be re-scaned.
1686 *
1687 * This function check if there is any fcoe event pending while driver
1688 * scan FCF entries. If there is any pending event, it will restart the
1689 * FCF saning and return 1 else return 0.
1690 */
1691int
1692lpfc_check_pending_fcoe_event(struct lpfc_hba *phba, uint8_t unreg_fcf)
1693{
James Smart32b97932009-07-19 10:01:21 -04001694 /*
1695 * If the Link is up and no FCoE events while in the
1696 * FCF discovery, no need to restart FCF discovery.
1697 */
1698 if ((phba->link_state >= LPFC_LINK_UP) &&
James Smarta93ff372010-10-22 11:06:08 -04001699 (phba->fcoe_eventtag == phba->fcoe_eventtag_at_fcf_scan))
James Smart32b97932009-07-19 10:01:21 -04001700 return 0;
1701
James Smart0c9ab6f2010-02-26 14:15:57 -05001702 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1703 "2768 Pending link or FCF event during current "
1704 "handling of the previous event: link_state:x%x, "
1705 "evt_tag_at_scan:x%x, evt_tag_current:x%x\n",
1706 phba->link_state, phba->fcoe_eventtag_at_fcf_scan,
1707 phba->fcoe_eventtag);
1708
James Smart32b97932009-07-19 10:01:21 -04001709 spin_lock_irq(&phba->hbalock);
1710 phba->fcf.fcf_flag &= ~FCF_AVAILABLE;
1711 spin_unlock_irq(&phba->hbalock);
1712
James Smart0c9ab6f2010-02-26 14:15:57 -05001713 if (phba->link_state >= LPFC_LINK_UP) {
1714 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1715 "2780 Restart FCF table scan due to "
1716 "pending FCF event:evt_tag_at_scan:x%x, "
1717 "evt_tag_current:x%x\n",
1718 phba->fcoe_eventtag_at_fcf_scan,
1719 phba->fcoe_eventtag);
1720 lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
1721 } else {
James Smart4d9ab992009-10-02 15:16:39 -04001722 /*
James Smarta93ff372010-10-22 11:06:08 -04001723 * Do not continue FCF discovery and clear FCF_TS_INPROG
James Smart4d9ab992009-10-02 15:16:39 -04001724 * flag
1725 */
James Smartdbb6b3a2010-06-08 18:31:37 -04001726 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
1727 "2833 Stop FCF discovery process due to link "
1728 "state change (x%x)\n", phba->link_state);
James Smart1c6f4ef52009-11-18 15:40:49 -05001729 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04001730 phba->hba_flag &= ~(FCF_TS_INPROG | FCF_RR_INPROG);
James Smart0c9ab6f2010-02-26 14:15:57 -05001731 phba->fcf.fcf_flag &= ~(FCF_REDISC_FOV | FCF_DISCOVERY);
James Smart1c6f4ef52009-11-18 15:40:49 -05001732 spin_unlock_irq(&phba->hbalock);
1733 }
James Smart32b97932009-07-19 10:01:21 -04001734
James Smart0c9ab6f2010-02-26 14:15:57 -05001735 /* Unregister the currently registered FCF if required */
James Smart32b97932009-07-19 10:01:21 -04001736 if (unreg_fcf) {
1737 spin_lock_irq(&phba->hbalock);
1738 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
1739 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05001740 lpfc_sli4_unregister_fcf(phba);
James Smart32b97932009-07-19 10:01:21 -04001741 }
James Smart32b97932009-07-19 10:01:21 -04001742 return 1;
1743}
1744
1745/**
James Smart999d8132010-03-15 11:24:56 -04001746 * lpfc_sli4_new_fcf_random_select - Randomly select an eligible new fcf record
1747 * @phba: pointer to lpfc hba data structure.
1748 * @fcf_cnt: number of eligible fcf record seen so far.
1749 *
1750 * This function makes an running random selection decision on FCF record to
1751 * use through a sequence of @fcf_cnt eligible FCF records with equal
1752 * probability. To perform integer manunipulation of random numbers with
1753 * size unit32_t, the lower 16 bits of the 32-bit random number returned
Akinobu Mita3b60a642013-04-29 16:21:35 -07001754 * from prandom_u32() are taken as the random random number generated.
James Smart999d8132010-03-15 11:24:56 -04001755 *
1756 * Returns true when outcome is for the newly read FCF record should be
1757 * chosen; otherwise, return false when outcome is for keeping the previously
1758 * chosen FCF record.
1759 **/
1760static bool
1761lpfc_sli4_new_fcf_random_select(struct lpfc_hba *phba, uint32_t fcf_cnt)
1762{
1763 uint32_t rand_num;
1764
1765 /* Get 16-bit uniform random number */
Akinobu Mita3b60a642013-04-29 16:21:35 -07001766 rand_num = 0xFFFF & prandom_u32();
James Smart999d8132010-03-15 11:24:56 -04001767
1768 /* Decision with probability 1/fcf_cnt */
1769 if ((fcf_cnt * rand_num) < 0xFFFF)
1770 return true;
1771 else
1772 return false;
1773}
1774
1775/**
James Smart3804dc82010-07-14 15:31:37 -04001776 * lpfc_sli4_fcf_rec_mbox_parse - Parse read_fcf mbox command.
James Smart6fb120a2009-05-22 14:52:59 -04001777 * @phba: pointer to lpfc hba data structure.
1778 * @mboxq: pointer to mailbox object.
James Smart0c9ab6f2010-02-26 14:15:57 -05001779 * @next_fcf_index: pointer to holder of next fcf index.
James Smart6fb120a2009-05-22 14:52:59 -04001780 *
James Smart0c9ab6f2010-02-26 14:15:57 -05001781 * This routine parses the non-embedded fcf mailbox command by performing the
1782 * necessarily error checking, non-embedded read FCF record mailbox command
1783 * SGE parsing, and endianness swapping.
1784 *
1785 * Returns the pointer to the new FCF record in the non-embedded mailbox
1786 * command DMA memory if successfully, other NULL.
James Smart6fb120a2009-05-22 14:52:59 -04001787 */
James Smart0c9ab6f2010-02-26 14:15:57 -05001788static struct fcf_record *
1789lpfc_sli4_fcf_rec_mbox_parse(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
1790 uint16_t *next_fcf_index)
James Smart6fb120a2009-05-22 14:52:59 -04001791{
1792 void *virt_addr;
James Smart6fb120a2009-05-22 14:52:59 -04001793 struct lpfc_mbx_sge sge;
1794 struct lpfc_mbx_read_fcf_tbl *read_fcf;
James Smartf818ea72015-05-21 13:55:29 -04001795 uint32_t shdr_status, shdr_add_status, if_type;
James Smart6fb120a2009-05-22 14:52:59 -04001796 union lpfc_sli4_cfg_shdr *shdr;
1797 struct fcf_record *new_fcf_record;
James Smart32b97932009-07-19 10:01:21 -04001798
James Smart6fb120a2009-05-22 14:52:59 -04001799 /* Get the first SGE entry from the non-embedded DMA memory. This
1800 * routine only uses a single SGE.
1801 */
1802 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
James Smart6fb120a2009-05-22 14:52:59 -04001803 if (unlikely(!mboxq->sge_array)) {
1804 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1805 "2524 Failed to get the non-embedded SGE "
1806 "virtual address\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001807 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001808 }
1809 virt_addr = mboxq->sge_array->addr[0];
1810
1811 shdr = (union lpfc_sli4_cfg_shdr *)virt_addr;
James Smart25626692013-03-01 16:36:54 -05001812 lpfc_sli_pcimem_bcopy(shdr, shdr,
1813 sizeof(union lpfc_sli4_cfg_shdr));
James Smart6fb120a2009-05-22 14:52:59 -04001814 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
James Smartf818ea72015-05-21 13:55:29 -04001815 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
James Smart0c9ab6f2010-02-26 14:15:57 -05001816 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
James Smart6fb120a2009-05-22 14:52:59 -04001817 if (shdr_status || shdr_add_status) {
James Smartf818ea72015-05-21 13:55:29 -04001818 if (shdr_status == STATUS_FCF_TABLE_EMPTY ||
1819 if_type == LPFC_SLI_INTF_IF_TYPE_2)
James Smart0c9ab6f2010-02-26 14:15:57 -05001820 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001821 "2726 READ_FCF_RECORD Indicates empty "
1822 "FCF table.\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05001823 else
1824 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smartdef9c7a2009-12-21 17:02:28 -05001825 "2521 READ_FCF_RECORD mailbox failed "
James Smart0c9ab6f2010-02-26 14:15:57 -05001826 "with status x%x add_status x%x, "
1827 "mbx\n", shdr_status, shdr_add_status);
1828 return NULL;
James Smart6fb120a2009-05-22 14:52:59 -04001829 }
James Smart0c9ab6f2010-02-26 14:15:57 -05001830
1831 /* Interpreting the returned information of the FCF record */
James Smart6fb120a2009-05-22 14:52:59 -04001832 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
1833 lpfc_sli_pcimem_bcopy(read_fcf, read_fcf,
1834 sizeof(struct lpfc_mbx_read_fcf_tbl));
James Smart0c9ab6f2010-02-26 14:15:57 -05001835 *next_fcf_index = bf_get(lpfc_mbx_read_fcf_tbl_nxt_vindx, read_fcf);
James Smart6fb120a2009-05-22 14:52:59 -04001836 new_fcf_record = (struct fcf_record *)(virt_addr +
1837 sizeof(struct lpfc_mbx_read_fcf_tbl));
1838 lpfc_sli_pcimem_bcopy(new_fcf_record, new_fcf_record,
James Smartcb5172e2010-03-15 11:25:07 -04001839 offsetof(struct fcf_record, vlan_bitmap));
1840 new_fcf_record->word137 = le32_to_cpu(new_fcf_record->word137);
1841 new_fcf_record->word138 = le32_to_cpu(new_fcf_record->word138);
James Smart6fb120a2009-05-22 14:52:59 -04001842
James Smart0c9ab6f2010-02-26 14:15:57 -05001843 return new_fcf_record;
1844}
1845
1846/**
1847 * lpfc_sli4_log_fcf_record_info - Log the information of a fcf record
1848 * @phba: pointer to lpfc hba data structure.
1849 * @fcf_record: pointer to the fcf record.
1850 * @vlan_id: the lowest vlan identifier associated to this fcf record.
1851 * @next_fcf_index: the index to the next fcf record in hba's fcf table.
1852 *
1853 * This routine logs the detailed FCF record if the LOG_FIP loggin is
1854 * enabled.
1855 **/
1856static void
1857lpfc_sli4_log_fcf_record_info(struct lpfc_hba *phba,
1858 struct fcf_record *fcf_record,
1859 uint16_t vlan_id,
1860 uint16_t next_fcf_index)
1861{
1862 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1863 "2764 READ_FCF_RECORD:\n"
1864 "\tFCF_Index : x%x\n"
1865 "\tFCF_Avail : x%x\n"
1866 "\tFCF_Valid : x%x\n"
James Smart26979ce2012-09-29 11:31:55 -04001867 "\tFCF_SOL : x%x\n"
James Smart0c9ab6f2010-02-26 14:15:57 -05001868 "\tFIP_Priority : x%x\n"
1869 "\tMAC_Provider : x%x\n"
1870 "\tLowest VLANID : x%x\n"
1871 "\tFCF_MAC Addr : x%x:%x:%x:%x:%x:%x\n"
1872 "\tFabric_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1873 "\tSwitch_Name : x%x:%x:%x:%x:%x:%x:%x:%x\n"
1874 "\tNext_FCF_Index: x%x\n",
1875 bf_get(lpfc_fcf_record_fcf_index, fcf_record),
1876 bf_get(lpfc_fcf_record_fcf_avail, fcf_record),
1877 bf_get(lpfc_fcf_record_fcf_valid, fcf_record),
James Smart26979ce2012-09-29 11:31:55 -04001878 bf_get(lpfc_fcf_record_fcf_sol, fcf_record),
James Smart0c9ab6f2010-02-26 14:15:57 -05001879 fcf_record->fip_priority,
1880 bf_get(lpfc_fcf_record_mac_addr_prov, fcf_record),
1881 vlan_id,
1882 bf_get(lpfc_fcf_record_mac_0, fcf_record),
1883 bf_get(lpfc_fcf_record_mac_1, fcf_record),
1884 bf_get(lpfc_fcf_record_mac_2, fcf_record),
1885 bf_get(lpfc_fcf_record_mac_3, fcf_record),
1886 bf_get(lpfc_fcf_record_mac_4, fcf_record),
1887 bf_get(lpfc_fcf_record_mac_5, fcf_record),
1888 bf_get(lpfc_fcf_record_fab_name_0, fcf_record),
1889 bf_get(lpfc_fcf_record_fab_name_1, fcf_record),
1890 bf_get(lpfc_fcf_record_fab_name_2, fcf_record),
1891 bf_get(lpfc_fcf_record_fab_name_3, fcf_record),
1892 bf_get(lpfc_fcf_record_fab_name_4, fcf_record),
1893 bf_get(lpfc_fcf_record_fab_name_5, fcf_record),
1894 bf_get(lpfc_fcf_record_fab_name_6, fcf_record),
1895 bf_get(lpfc_fcf_record_fab_name_7, fcf_record),
1896 bf_get(lpfc_fcf_record_switch_name_0, fcf_record),
1897 bf_get(lpfc_fcf_record_switch_name_1, fcf_record),
1898 bf_get(lpfc_fcf_record_switch_name_2, fcf_record),
1899 bf_get(lpfc_fcf_record_switch_name_3, fcf_record),
1900 bf_get(lpfc_fcf_record_switch_name_4, fcf_record),
1901 bf_get(lpfc_fcf_record_switch_name_5, fcf_record),
1902 bf_get(lpfc_fcf_record_switch_name_6, fcf_record),
1903 bf_get(lpfc_fcf_record_switch_name_7, fcf_record),
1904 next_fcf_index);
1905}
1906
1907/**
James Smartdbb6b3a2010-06-08 18:31:37 -04001908 lpfc_sli4_fcf_record_match - testing new FCF record for matching existing FCF
1909 * @phba: pointer to lpfc hba data structure.
1910 * @fcf_rec: pointer to an existing FCF record.
1911 * @new_fcf_record: pointer to a new FCF record.
1912 * @new_vlan_id: vlan id from the new FCF record.
1913 *
1914 * This function performs matching test of a new FCF record against an existing
1915 * FCF record. If the new_vlan_id passed in is LPFC_FCOE_IGNORE_VID, vlan id
1916 * will not be used as part of the FCF record matching criteria.
1917 *
1918 * Returns true if all the fields matching, otherwise returns false.
1919 */
1920static bool
1921lpfc_sli4_fcf_record_match(struct lpfc_hba *phba,
1922 struct lpfc_fcf_rec *fcf_rec,
1923 struct fcf_record *new_fcf_record,
1924 uint16_t new_vlan_id)
1925{
1926 if (new_vlan_id != LPFC_FCOE_IGNORE_VID)
1927 if (!lpfc_vlan_id_match(fcf_rec->vlan_id, new_vlan_id))
1928 return false;
1929 if (!lpfc_mac_addr_match(fcf_rec->mac_addr, new_fcf_record))
1930 return false;
1931 if (!lpfc_sw_name_match(fcf_rec->switch_name, new_fcf_record))
1932 return false;
1933 if (!lpfc_fab_name_match(fcf_rec->fabric_name, new_fcf_record))
1934 return false;
James Smart7d791df2011-07-22 18:37:52 -04001935 if (fcf_rec->priority != new_fcf_record->fip_priority)
1936 return false;
James Smartdbb6b3a2010-06-08 18:31:37 -04001937 return true;
1938}
1939
1940/**
James Smarta93ff372010-10-22 11:06:08 -04001941 * lpfc_sli4_fcf_rr_next_proc - processing next roundrobin fcf
1942 * @vport: Pointer to vport object.
1943 * @fcf_index: index to next fcf.
1944 *
1945 * This function processing the roundrobin fcf failover to next fcf index.
1946 * When this function is invoked, there will be a current fcf registered
1947 * for flogi.
1948 * Return: 0 for continue retrying flogi on currently registered fcf;
1949 * 1 for stop flogi on currently registered fcf;
1950 */
1951int lpfc_sli4_fcf_rr_next_proc(struct lpfc_vport *vport, uint16_t fcf_index)
1952{
1953 struct lpfc_hba *phba = vport->phba;
1954 int rc;
1955
1956 if (fcf_index == LPFC_FCOE_FCF_NEXT_NONE) {
1957 spin_lock_irq(&phba->hbalock);
1958 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
1959 spin_unlock_irq(&phba->hbalock);
1960 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1961 "2872 Devloss tmo with no eligible "
1962 "FCF, unregister in-use FCF (x%x) "
1963 "and rescan FCF table\n",
1964 phba->fcf.current_rec.fcf_indx);
1965 lpfc_unregister_fcf_rescan(phba);
1966 goto stop_flogi_current_fcf;
1967 }
1968 /* Mark the end to FLOGI roundrobin failover */
1969 phba->hba_flag &= ~FCF_RR_INPROG;
1970 /* Allow action to new fcf asynchronous event */
1971 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
1972 spin_unlock_irq(&phba->hbalock);
1973 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
1974 "2865 No FCF available, stop roundrobin FCF "
1975 "failover and change port state:x%x/x%x\n",
1976 phba->pport->port_state, LPFC_VPORT_UNKNOWN);
1977 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1978 goto stop_flogi_current_fcf;
1979 } else {
1980 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_ELS,
1981 "2794 Try FLOGI roundrobin FCF failover to "
1982 "(x%x)\n", fcf_index);
1983 rc = lpfc_sli4_fcf_rr_read_fcf_rec(phba, fcf_index);
1984 if (rc)
1985 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP | LOG_ELS,
1986 "2761 FLOGI roundrobin FCF failover "
1987 "failed (rc:x%x) to read FCF (x%x)\n",
1988 rc, phba->fcf.current_rec.fcf_indx);
1989 else
1990 goto stop_flogi_current_fcf;
1991 }
1992 return 0;
1993
1994stop_flogi_current_fcf:
1995 lpfc_can_disctmo(vport);
1996 return 1;
1997}
1998
1999/**
James Smart7d791df2011-07-22 18:37:52 -04002000 * lpfc_sli4_fcf_pri_list_del
2001 * @phba: pointer to lpfc hba data structure.
2002 * @fcf_index the index of the fcf record to delete
2003 * This routine checks the on list flag of the fcf_index to be deleted.
2004 * If it is one the list then it is removed from the list, and the flag
2005 * is cleared. This routine grab the hbalock before removing the fcf
2006 * record from the list.
2007 **/
2008static void lpfc_sli4_fcf_pri_list_del(struct lpfc_hba *phba,
2009 uint16_t fcf_index)
2010{
2011 struct lpfc_fcf_pri *new_fcf_pri;
2012
2013 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2014 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2015 "3058 deleting idx x%x pri x%x flg x%x\n",
2016 fcf_index, new_fcf_pri->fcf_rec.priority,
2017 new_fcf_pri->fcf_rec.flag);
2018 spin_lock_irq(&phba->hbalock);
2019 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST) {
2020 if (phba->fcf.current_rec.priority ==
2021 new_fcf_pri->fcf_rec.priority)
2022 phba->fcf.eligible_fcf_cnt--;
2023 list_del_init(&new_fcf_pri->list);
2024 new_fcf_pri->fcf_rec.flag &= ~LPFC_FCF_ON_PRI_LIST;
2025 }
2026 spin_unlock_irq(&phba->hbalock);
2027}
2028
2029/**
2030 * lpfc_sli4_set_fcf_flogi_fail
2031 * @phba: pointer to lpfc hba data structure.
2032 * @fcf_index the index of the fcf record to update
2033 * This routine acquires the hbalock and then set the LPFC_FCF_FLOGI_FAILED
2034 * flag so the the round robin slection for the particular priority level
2035 * will try a different fcf record that does not have this bit set.
2036 * If the fcf record is re-read for any reason this flag is cleared brfore
2037 * adding it to the priority list.
2038 **/
2039void
2040lpfc_sli4_set_fcf_flogi_fail(struct lpfc_hba *phba, uint16_t fcf_index)
2041{
2042 struct lpfc_fcf_pri *new_fcf_pri;
2043 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2044 spin_lock_irq(&phba->hbalock);
2045 new_fcf_pri->fcf_rec.flag |= LPFC_FCF_FLOGI_FAILED;
2046 spin_unlock_irq(&phba->hbalock);
2047}
2048
2049/**
2050 * lpfc_sli4_fcf_pri_list_add
2051 * @phba: pointer to lpfc hba data structure.
2052 * @fcf_index the index of the fcf record to add
2053 * This routine checks the priority of the fcf_index to be added.
2054 * If it is a lower priority than the current head of the fcf_pri list
2055 * then it is added to the list in the right order.
2056 * If it is the same priority as the current head of the list then it
2057 * is added to the head of the list and its bit in the rr_bmask is set.
2058 * If the fcf_index to be added is of a higher priority than the current
2059 * head of the list then the rr_bmask is cleared, its bit is set in the
2060 * rr_bmask and it is added to the head of the list.
2061 * returns:
2062 * 0=success 1=failure
2063 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04002064static int lpfc_sli4_fcf_pri_list_add(struct lpfc_hba *phba,
2065 uint16_t fcf_index,
James Smart7d791df2011-07-22 18:37:52 -04002066 struct fcf_record *new_fcf_record)
2067{
2068 uint16_t current_fcf_pri;
2069 uint16_t last_index;
2070 struct lpfc_fcf_pri *fcf_pri;
2071 struct lpfc_fcf_pri *next_fcf_pri;
2072 struct lpfc_fcf_pri *new_fcf_pri;
2073 int ret;
2074
2075 new_fcf_pri = &phba->fcf.fcf_pri[fcf_index];
2076 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2077 "3059 adding idx x%x pri x%x flg x%x\n",
2078 fcf_index, new_fcf_record->fip_priority,
2079 new_fcf_pri->fcf_rec.flag);
2080 spin_lock_irq(&phba->hbalock);
2081 if (new_fcf_pri->fcf_rec.flag & LPFC_FCF_ON_PRI_LIST)
2082 list_del_init(&new_fcf_pri->list);
2083 new_fcf_pri->fcf_rec.fcf_index = fcf_index;
2084 new_fcf_pri->fcf_rec.priority = new_fcf_record->fip_priority;
2085 if (list_empty(&phba->fcf.fcf_pri_list)) {
2086 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2087 ret = lpfc_sli4_fcf_rr_index_set(phba,
2088 new_fcf_pri->fcf_rec.fcf_index);
2089 goto out;
2090 }
2091
2092 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
2093 LPFC_SLI4_FCF_TBL_INDX_MAX);
2094 if (last_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
2095 ret = 0; /* Empty rr list */
2096 goto out;
2097 }
2098 current_fcf_pri = phba->fcf.fcf_pri[last_index].fcf_rec.priority;
2099 if (new_fcf_pri->fcf_rec.priority <= current_fcf_pri) {
2100 list_add(&new_fcf_pri->list, &phba->fcf.fcf_pri_list);
2101 if (new_fcf_pri->fcf_rec.priority < current_fcf_pri) {
2102 memset(phba->fcf.fcf_rr_bmask, 0,
2103 sizeof(*phba->fcf.fcf_rr_bmask));
2104 /* fcfs_at_this_priority_level = 1; */
2105 phba->fcf.eligible_fcf_cnt = 1;
2106 } else
2107 /* fcfs_at_this_priority_level++; */
2108 phba->fcf.eligible_fcf_cnt++;
2109 ret = lpfc_sli4_fcf_rr_index_set(phba,
2110 new_fcf_pri->fcf_rec.fcf_index);
2111 goto out;
2112 }
2113
2114 list_for_each_entry_safe(fcf_pri, next_fcf_pri,
2115 &phba->fcf.fcf_pri_list, list) {
2116 if (new_fcf_pri->fcf_rec.priority <=
2117 fcf_pri->fcf_rec.priority) {
2118 if (fcf_pri->list.prev == &phba->fcf.fcf_pri_list)
2119 list_add(&new_fcf_pri->list,
2120 &phba->fcf.fcf_pri_list);
2121 else
2122 list_add(&new_fcf_pri->list,
2123 &((struct lpfc_fcf_pri *)
2124 fcf_pri->list.prev)->list);
2125 ret = 0;
2126 goto out;
2127 } else if (fcf_pri->list.next == &phba->fcf.fcf_pri_list
2128 || new_fcf_pri->fcf_rec.priority <
2129 next_fcf_pri->fcf_rec.priority) {
2130 list_add(&new_fcf_pri->list, &fcf_pri->list);
2131 ret = 0;
2132 goto out;
2133 }
2134 if (new_fcf_pri->fcf_rec.priority > fcf_pri->fcf_rec.priority)
2135 continue;
2136
2137 }
2138 ret = 1;
2139out:
2140 /* we use = instead of |= to clear the FLOGI_FAILED flag. */
2141 new_fcf_pri->fcf_rec.flag = LPFC_FCF_ON_PRI_LIST;
2142 spin_unlock_irq(&phba->hbalock);
2143 return ret;
2144}
2145
2146/**
James Smart0c9ab6f2010-02-26 14:15:57 -05002147 * lpfc_mbx_cmpl_fcf_scan_read_fcf_rec - fcf scan read_fcf mbox cmpl handler.
2148 * @phba: pointer to lpfc hba data structure.
2149 * @mboxq: pointer to mailbox object.
2150 *
2151 * This function iterates through all the fcf records available in
2152 * HBA and chooses the optimal FCF record for discovery. After finding
2153 * the FCF for discovery it registers the FCF record and kicks start
2154 * discovery.
2155 * If FCF_IN_USE flag is set in currently used FCF, the routine tries to
2156 * use an FCF record which matches fabric name and mac address of the
2157 * currently used FCF record.
2158 * If the driver supports only one FCF, it will try to use the FCF record
2159 * used by BOOT_BIOS.
2160 */
2161void
2162lpfc_mbx_cmpl_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2163{
2164 struct fcf_record *new_fcf_record;
2165 uint32_t boot_flag, addr_mode;
2166 uint16_t fcf_index, next_fcf_index;
2167 struct lpfc_fcf_rec *fcf_rec = NULL;
2168 uint16_t vlan_id;
James Smart999d8132010-03-15 11:24:56 -04002169 bool select_new_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002170 int rc;
2171
2172 /* If there is pending FCoE event restart FCF table scan */
James Smarta93ff372010-10-22 11:06:08 -04002173 if (lpfc_check_pending_fcoe_event(phba, LPFC_SKIP_UNREG_FCF)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002174 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2175 return;
2176 }
2177
2178 /* Parse the FCF record from the non-embedded mailbox command */
2179 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2180 &next_fcf_index);
2181 if (!new_fcf_record) {
James Smarta93ff372010-10-22 11:06:08 -04002182 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
James Smart0c9ab6f2010-02-26 14:15:57 -05002183 "2765 Mailbox command READ_FCF_RECORD "
2184 "failed to retrieve a FCF record.\n");
2185 /* Let next new FCF event trigger fast failover */
2186 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002187 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002188 spin_unlock_irq(&phba->hbalock);
2189 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2190 return;
2191 }
2192
2193 /* Check the FCF record against the connection list */
James Smartecfd03c2010-02-12 14:41:27 -05002194 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2195 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002196
2197 /* Log the FCF record information if turned on */
2198 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2199 next_fcf_index);
2200
James Smart6fb120a2009-05-22 14:52:59 -04002201 /*
2202 * If the fcf record does not match with connect list entries
James Smart0c9ab6f2010-02-26 14:15:57 -05002203 * read the next entry; otherwise, this is an eligible FCF
James Smarta93ff372010-10-22 11:06:08 -04002204 * record for roundrobin FCF failover.
James Smart6fb120a2009-05-22 14:52:59 -04002205 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002206 if (!rc) {
James Smart7d791df2011-07-22 18:37:52 -04002207 lpfc_sli4_fcf_pri_list_del(phba,
2208 bf_get(lpfc_fcf_record_fcf_index,
2209 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002210 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002211 "2781 FCF (x%x) failed connection "
James Smart26979ce2012-09-29 11:31:55 -04002212 "list check: (x%x/x%x/%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002213 bf_get(lpfc_fcf_record_fcf_index,
2214 new_fcf_record),
2215 bf_get(lpfc_fcf_record_fcf_avail,
2216 new_fcf_record),
2217 bf_get(lpfc_fcf_record_fcf_valid,
James Smart26979ce2012-09-29 11:31:55 -04002218 new_fcf_record),
2219 bf_get(lpfc_fcf_record_fcf_sol,
James Smart0c9ab6f2010-02-26 14:15:57 -05002220 new_fcf_record));
James Smartdbb6b3a2010-06-08 18:31:37 -04002221 if ((phba->fcf.fcf_flag & FCF_IN_USE) &&
2222 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
2223 new_fcf_record, LPFC_FCOE_IGNORE_VID)) {
James Smart5ac6b302010-10-22 11:05:36 -04002224 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) !=
2225 phba->fcf.current_rec.fcf_indx) {
2226 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2227 "2862 FCF (x%x) matches property "
2228 "of in-use FCF (x%x)\n",
2229 bf_get(lpfc_fcf_record_fcf_index,
2230 new_fcf_record),
2231 phba->fcf.current_rec.fcf_indx);
2232 goto read_next_fcf;
2233 }
James Smartdbb6b3a2010-06-08 18:31:37 -04002234 /*
2235 * In case the current in-use FCF record becomes
2236 * invalid/unavailable during FCF discovery that
2237 * was not triggered by fast FCF failover process,
2238 * treat it as fast FCF failover.
2239 */
2240 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND) &&
2241 !(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
2242 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2243 "2835 Invalid in-use FCF "
James Smarta93ff372010-10-22 11:06:08 -04002244 "(x%x), enter FCF failover "
2245 "table scan.\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002246 phba->fcf.current_rec.fcf_indx);
2247 spin_lock_irq(&phba->hbalock);
2248 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2249 spin_unlock_irq(&phba->hbalock);
2250 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2251 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2252 LPFC_FCOE_FCF_GET_FIRST);
2253 return;
2254 }
2255 }
James Smart6fb120a2009-05-22 14:52:59 -04002256 goto read_next_fcf;
James Smart0c9ab6f2010-02-26 14:15:57 -05002257 } else {
2258 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002259 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index,
2260 new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002261 if (rc)
2262 goto read_next_fcf;
2263 }
2264
James Smart6fb120a2009-05-22 14:52:59 -04002265 /*
2266 * If this is not the first FCF discovery of the HBA, use last
James Smartecfd03c2010-02-12 14:41:27 -05002267 * FCF record for the discovery. The condition that a rescan
2268 * matches the in-use FCF record: fabric name, switch name, mac
2269 * address, and vlan_id.
James Smart6fb120a2009-05-22 14:52:59 -04002270 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002271 spin_lock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002272 if (phba->fcf.fcf_flag & FCF_IN_USE) {
James Smart7d791df2011-07-22 18:37:52 -04002273 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2274 lpfc_sli4_fcf_record_match(phba, &phba->fcf.current_rec,
James Smartdbb6b3a2010-06-08 18:31:37 -04002275 new_fcf_record, vlan_id)) {
James Smart5ac6b302010-10-22 11:05:36 -04002276 if (bf_get(lpfc_fcf_record_fcf_index, new_fcf_record) ==
2277 phba->fcf.current_rec.fcf_indx) {
2278 phba->fcf.fcf_flag |= FCF_AVAILABLE;
2279 if (phba->fcf.fcf_flag & FCF_REDISC_PEND)
2280 /* Stop FCF redisc wait timer */
2281 __lpfc_sli4_stop_fcf_redisc_wait_timer(
2282 phba);
2283 else if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2284 /* Fast failover, mark completed */
2285 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
2286 spin_unlock_irq(&phba->hbalock);
2287 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2288 "2836 New FCF matches in-use "
James Smart88f43a02013-04-17 20:19:44 -04002289 "FCF (x%x), port_state:x%x, "
2290 "fc_flag:x%x\n",
2291 phba->fcf.current_rec.fcf_indx,
2292 phba->pport->port_state,
2293 phba->pport->fc_flag);
James Smart5ac6b302010-10-22 11:05:36 -04002294 goto out;
2295 } else
2296 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
2297 "2863 New FCF (x%x) matches "
2298 "property of in-use FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002299 bf_get(lpfc_fcf_record_fcf_index,
James Smart5ac6b302010-10-22 11:05:36 -04002300 new_fcf_record),
2301 phba->fcf.current_rec.fcf_indx);
James Smart6fb120a2009-05-22 14:52:59 -04002302 }
James Smartecfd03c2010-02-12 14:41:27 -05002303 /*
2304 * Read next FCF record from HBA searching for the matching
2305 * with in-use record only if not during the fast failover
2306 * period. In case of fast failover period, it shall try to
2307 * determine whether the FCF record just read should be the
2308 * next candidate.
2309 */
2310 if (!(phba->fcf.fcf_flag & FCF_REDISC_FOV)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002311 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002312 goto read_next_fcf;
2313 }
James Smart6fb120a2009-05-22 14:52:59 -04002314 }
James Smartecfd03c2010-02-12 14:41:27 -05002315 /*
2316 * Update on failover FCF record only if it's in FCF fast-failover
2317 * period; otherwise, update on current FCF record.
2318 */
James Smartfc2b9892010-02-26 14:15:29 -05002319 if (phba->fcf.fcf_flag & FCF_REDISC_FOV)
2320 fcf_rec = &phba->fcf.failover_rec;
2321 else
James Smartecfd03c2010-02-12 14:41:27 -05002322 fcf_rec = &phba->fcf.current_rec;
2323
James Smart6fb120a2009-05-22 14:52:59 -04002324 if (phba->fcf.fcf_flag & FCF_AVAILABLE) {
2325 /*
James Smartecfd03c2010-02-12 14:41:27 -05002326 * If the driver FCF record does not have boot flag
2327 * set and new hba fcf record has boot flag set, use
2328 * the new hba fcf record.
James Smart6fb120a2009-05-22 14:52:59 -04002329 */
James Smartecfd03c2010-02-12 14:41:27 -05002330 if (boot_flag && !(fcf_rec->flag & BOOT_ENABLE)) {
2331 /* Choose this FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002332 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2333 "2837 Update current FCF record "
2334 "(x%x) with new FCF record (x%x)\n",
2335 fcf_rec->fcf_indx,
2336 bf_get(lpfc_fcf_record_fcf_index,
2337 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002338 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2339 addr_mode, vlan_id, BOOT_ENABLE);
James Smart0c9ab6f2010-02-26 14:15:57 -05002340 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002341 goto read_next_fcf;
2342 }
2343 /*
James Smartecfd03c2010-02-12 14:41:27 -05002344 * If the driver FCF record has boot flag set and the
2345 * new hba FCF record does not have boot flag, read
2346 * the next FCF record.
James Smart6fb120a2009-05-22 14:52:59 -04002347 */
James Smartecfd03c2010-02-12 14:41:27 -05002348 if (!boot_flag && (fcf_rec->flag & BOOT_ENABLE)) {
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 new hba FCF record has lower priority value
2354 * than the driver FCF record, use the new record.
James Smart6fb120a2009-05-22 14:52:59 -04002355 */
James Smartfc2b9892010-02-26 14:15:29 -05002356 if (new_fcf_record->fip_priority < fcf_rec->priority) {
James Smart999d8132010-03-15 11:24:56 -04002357 /* Choose the new FCF record with lower priority */
James Smartdbb6b3a2010-06-08 18:31:37 -04002358 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2359 "2838 Update current FCF record "
2360 "(x%x) with new FCF record (x%x)\n",
2361 fcf_rec->fcf_indx,
2362 bf_get(lpfc_fcf_record_fcf_index,
2363 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002364 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2365 addr_mode, vlan_id, 0);
James Smart999d8132010-03-15 11:24:56 -04002366 /* Reset running random FCF selection count */
2367 phba->fcf.eligible_fcf_cnt = 1;
2368 } else if (new_fcf_record->fip_priority == fcf_rec->priority) {
2369 /* Update running random FCF selection count */
2370 phba->fcf.eligible_fcf_cnt++;
2371 select_new_fcf = lpfc_sli4_new_fcf_random_select(phba,
2372 phba->fcf.eligible_fcf_cnt);
James Smartdbb6b3a2010-06-08 18:31:37 -04002373 if (select_new_fcf) {
2374 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2375 "2839 Update current FCF record "
2376 "(x%x) with new FCF record (x%x)\n",
2377 fcf_rec->fcf_indx,
2378 bf_get(lpfc_fcf_record_fcf_index,
2379 new_fcf_record));
James Smart999d8132010-03-15 11:24:56 -04002380 /* Choose the new FCF by random selection */
2381 __lpfc_update_fcf_record(phba, fcf_rec,
2382 new_fcf_record,
2383 addr_mode, vlan_id, 0);
James Smartdbb6b3a2010-06-08 18:31:37 -04002384 }
James Smart6fb120a2009-05-22 14:52:59 -04002385 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002386 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002387 goto read_next_fcf;
2388 }
2389 /*
James Smartecfd03c2010-02-12 14:41:27 -05002390 * This is the first suitable FCF record, choose this record for
2391 * initial best-fit FCF.
James Smart6fb120a2009-05-22 14:52:59 -04002392 */
James Smartecfd03c2010-02-12 14:41:27 -05002393 if (fcf_rec) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002394 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002395 "2840 Update initial FCF candidate "
2396 "with FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002397 bf_get(lpfc_fcf_record_fcf_index,
2398 new_fcf_record));
James Smartecfd03c2010-02-12 14:41:27 -05002399 __lpfc_update_fcf_record(phba, fcf_rec, new_fcf_record,
2400 addr_mode, vlan_id, (boot_flag ?
2401 BOOT_ENABLE : 0));
2402 phba->fcf.fcf_flag |= FCF_AVAILABLE;
James Smart999d8132010-03-15 11:24:56 -04002403 /* Setup initial running random FCF selection count */
2404 phba->fcf.eligible_fcf_cnt = 1;
James Smart6fb120a2009-05-22 14:52:59 -04002405 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002406 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04002407 goto read_next_fcf;
2408
2409read_next_fcf:
2410 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smartecfd03c2010-02-12 14:41:27 -05002411 if (next_fcf_index == LPFC_FCOE_FCF_NEXT_NONE || next_fcf_index == 0) {
2412 if (phba->fcf.fcf_flag & FCF_REDISC_FOV) {
2413 /*
2414 * Case of FCF fast failover scan
2415 */
2416
2417 /*
2418 * It has not found any suitable FCF record, cancel
2419 * FCF scan inprogress, and do nothing
2420 */
2421 if (!(phba->fcf.failover_rec.flag & RECORD_VALID)) {
James Smart0c9ab6f2010-02-26 14:15:57 -05002422 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002423 "2782 No suitable FCF found: "
2424 "(x%x/x%x)\n",
James Smart0c9ab6f2010-02-26 14:15:57 -05002425 phba->fcoe_eventtag_at_fcf_scan,
2426 bf_get(lpfc_fcf_record_fcf_index,
2427 new_fcf_record));
James Smart0c9ab6f2010-02-26 14:15:57 -05002428 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002429 if (phba->hba_flag & HBA_DEVLOSS_TMO) {
2430 phba->hba_flag &= ~FCF_TS_INPROG;
2431 spin_unlock_irq(&phba->hbalock);
2432 /* Unregister in-use FCF and rescan */
2433 lpfc_printf_log(phba, KERN_INFO,
2434 LOG_FIP,
2435 "2864 On devloss tmo "
2436 "unreg in-use FCF and "
2437 "rescan FCF table\n");
2438 lpfc_unregister_fcf_rescan(phba);
2439 return;
2440 }
2441 /*
2442 * Let next new FCF event trigger fast failover
2443 */
2444 phba->hba_flag &= ~FCF_TS_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002445 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002446 return;
2447 }
2448 /*
2449 * It has found a suitable FCF record that is not
2450 * the same as in-use FCF record, unregister the
2451 * in-use FCF record, replace the in-use FCF record
2452 * with the new FCF record, mark FCF fast failover
2453 * completed, and then start register the new FCF
2454 * record.
2455 */
2456
James Smart0c9ab6f2010-02-26 14:15:57 -05002457 /* Unregister the current in-use FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002458 lpfc_unregister_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002459
2460 /* Replace in-use record with the new record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002461 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002462 "2842 Replace in-use FCF (x%x) "
2463 "with failover FCF (x%x)\n",
James Smartdbb6b3a2010-06-08 18:31:37 -04002464 phba->fcf.current_rec.fcf_indx,
2465 phba->fcf.failover_rec.fcf_indx);
James Smartecfd03c2010-02-12 14:41:27 -05002466 memcpy(&phba->fcf.current_rec,
2467 &phba->fcf.failover_rec,
2468 sizeof(struct lpfc_fcf_rec));
James Smart3804dc82010-07-14 15:31:37 -04002469 /*
2470 * Mark the fast FCF failover rediscovery completed
2471 * and the start of the first round of the roundrobin
2472 * FCF failover.
2473 */
James Smart0c9ab6f2010-02-26 14:15:57 -05002474 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002475 phba->fcf.fcf_flag &= ~FCF_REDISC_FOV;
James Smart0c9ab6f2010-02-26 14:15:57 -05002476 spin_unlock_irq(&phba->hbalock);
James Smartecfd03c2010-02-12 14:41:27 -05002477 /* Register to the new FCF record */
2478 lpfc_register_fcf(phba);
2479 } else {
2480 /*
2481 * In case of transaction period to fast FCF failover,
2482 * do nothing when search to the end of the FCF table.
2483 */
2484 if ((phba->fcf.fcf_flag & FCF_REDISC_EVT) ||
2485 (phba->fcf.fcf_flag & FCF_REDISC_PEND))
2486 return;
James Smartdbb6b3a2010-06-08 18:31:37 -04002487
James Smart7d791df2011-07-22 18:37:52 -04002488 if (phba->cfg_fcf_failover_policy == LPFC_FCF_FOV &&
2489 phba->fcf.fcf_flag & FCF_IN_USE) {
James Smartdbb6b3a2010-06-08 18:31:37 -04002490 /*
2491 * In case the current in-use FCF record no
2492 * longer existed during FCF discovery that
2493 * was not triggered by fast FCF failover
2494 * process, treat it as fast FCF failover.
2495 */
2496 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2497 "2841 In-use FCF record (x%x) "
2498 "not reported, entering fast "
2499 "FCF failover mode scanning.\n",
2500 phba->fcf.current_rec.fcf_indx);
2501 spin_lock_irq(&phba->hbalock);
2502 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
2503 spin_unlock_irq(&phba->hbalock);
James Smartdbb6b3a2010-06-08 18:31:37 -04002504 lpfc_sli4_fcf_scan_read_fcf_rec(phba,
2505 LPFC_FCOE_FCF_GET_FIRST);
2506 return;
2507 }
James Smart0c9ab6f2010-02-26 14:15:57 -05002508 /* Register to the new FCF record */
James Smartecfd03c2010-02-12 14:41:27 -05002509 lpfc_register_fcf(phba);
2510 }
2511 } else
James Smart0c9ab6f2010-02-26 14:15:57 -05002512 lpfc_sli4_fcf_scan_read_fcf_rec(phba, next_fcf_index);
James Smart6fb120a2009-05-22 14:52:59 -04002513 return;
2514
2515out:
2516 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2517 lpfc_register_fcf(phba);
2518
2519 return;
2520}
2521
2522/**
James Smarta93ff372010-10-22 11:06:08 -04002523 * lpfc_mbx_cmpl_fcf_rr_read_fcf_rec - fcf roundrobin read_fcf mbox cmpl hdler
James Smart0c9ab6f2010-02-26 14:15:57 -05002524 * @phba: pointer to lpfc hba data structure.
2525 * @mboxq: pointer to mailbox object.
2526 *
James Smarta93ff372010-10-22 11:06:08 -04002527 * This is the callback function for FLOGI failure roundrobin FCF failover
James Smart0c9ab6f2010-02-26 14:15:57 -05002528 * read FCF record mailbox command from the eligible FCF record bmask for
2529 * performing the failover. If the FCF read back is not valid/available, it
2530 * fails through to retrying FLOGI to the currently registered FCF again.
2531 * Otherwise, if the FCF read back is valid and available, it will set the
2532 * newly read FCF record to the failover FCF record, unregister currently
2533 * registered FCF record, copy the failover FCF record to the current
2534 * FCF record, and then register the current FCF record before proceeding
2535 * to trying FLOGI on the new failover FCF.
2536 */
2537void
2538lpfc_mbx_cmpl_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2539{
2540 struct fcf_record *new_fcf_record;
2541 uint32_t boot_flag, addr_mode;
James Smarta93ff372010-10-22 11:06:08 -04002542 uint16_t next_fcf_index, fcf_index;
James Smart0c9ab6f2010-02-26 14:15:57 -05002543 uint16_t current_fcf_index;
2544 uint16_t vlan_id;
James Smarta93ff372010-10-22 11:06:08 -04002545 int rc;
James Smart0c9ab6f2010-02-26 14:15:57 -05002546
James Smarta93ff372010-10-22 11:06:08 -04002547 /* If link state is not up, stop the roundrobin failover process */
James Smart0c9ab6f2010-02-26 14:15:57 -05002548 if (phba->link_state < LPFC_LINK_UP) {
2549 spin_lock_irq(&phba->hbalock);
2550 phba->fcf.fcf_flag &= ~FCF_DISCOVERY;
James Smarta93ff372010-10-22 11:06:08 -04002551 phba->hba_flag &= ~FCF_RR_INPROG;
James Smart0c9ab6f2010-02-26 14:15:57 -05002552 spin_unlock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04002553 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002554 }
2555
2556 /* Parse the FCF record from the non-embedded mailbox command */
2557 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2558 &next_fcf_index);
2559 if (!new_fcf_record) {
2560 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
2561 "2766 Mailbox command READ_FCF_RECORD "
James Smart646a2dd2013-12-17 20:29:22 -05002562 "failed to retrieve a FCF record. "
2563 "hba_flg x%x fcf_flg x%x\n", phba->hba_flag,
2564 phba->fcf.fcf_flag);
2565 lpfc_unregister_fcf_rescan(phba);
2566 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05002567 }
2568
2569 /* Get the needed parameters from FCF record */
James Smarta93ff372010-10-22 11:06:08 -04002570 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2571 &addr_mode, &vlan_id);
James Smart0c9ab6f2010-02-26 14:15:57 -05002572
2573 /* Log the FCF record information if turned on */
2574 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2575 next_fcf_index);
2576
James Smarta93ff372010-10-22 11:06:08 -04002577 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
2578 if (!rc) {
2579 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2580 "2848 Remove ineligible FCF (x%x) from "
2581 "from roundrobin bmask\n", fcf_index);
2582 /* Clear roundrobin bmask bit for ineligible FCF */
2583 lpfc_sli4_fcf_rr_index_clear(phba, fcf_index);
2584 /* Perform next round of roundrobin FCF failover */
2585 fcf_index = lpfc_sli4_fcf_rr_next_index_get(phba);
2586 rc = lpfc_sli4_fcf_rr_next_proc(phba->pport, fcf_index);
2587 if (rc)
2588 goto out;
2589 goto error_out;
2590 }
2591
2592 if (fcf_index == phba->fcf.current_rec.fcf_indx) {
2593 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2594 "2760 Perform FLOGI roundrobin FCF failover: "
2595 "FCF (x%x) back to FCF (x%x)\n",
2596 phba->fcf.current_rec.fcf_indx, fcf_index);
2597 /* Wait 500 ms before retrying FLOGI to current FCF */
2598 msleep(500);
James Smart76a95d72010-11-20 23:11:48 -05002599 lpfc_issue_init_vfi(phba->pport);
James Smarta93ff372010-10-22 11:06:08 -04002600 goto out;
2601 }
2602
James Smart0c9ab6f2010-02-26 14:15:57 -05002603 /* Upload new FCF record to the failover FCF record */
James Smartdbb6b3a2010-06-08 18:31:37 -04002604 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002605 "2834 Update current FCF (x%x) with new FCF (x%x)\n",
2606 phba->fcf.failover_rec.fcf_indx, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002607 spin_lock_irq(&phba->hbalock);
2608 __lpfc_update_fcf_record(phba, &phba->fcf.failover_rec,
2609 new_fcf_record, addr_mode, vlan_id,
2610 (boot_flag ? BOOT_ENABLE : 0));
2611 spin_unlock_irq(&phba->hbalock);
2612
2613 current_fcf_index = phba->fcf.current_rec.fcf_indx;
2614
2615 /* Unregister the current in-use FCF record */
2616 lpfc_unregister_fcf(phba);
2617
2618 /* Replace in-use record with the new record */
2619 memcpy(&phba->fcf.current_rec, &phba->fcf.failover_rec,
2620 sizeof(struct lpfc_fcf_rec));
2621
2622 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
James Smarta93ff372010-10-22 11:06:08 -04002623 "2783 Perform FLOGI roundrobin FCF failover: FCF "
2624 "(x%x) to FCF (x%x)\n", current_fcf_index, fcf_index);
James Smart0c9ab6f2010-02-26 14:15:57 -05002625
James Smarta93ff372010-10-22 11:06:08 -04002626error_out:
2627 lpfc_register_fcf(phba);
James Smart0c9ab6f2010-02-26 14:15:57 -05002628out:
2629 lpfc_sli4_mbox_cmd_free(phba, mboxq);
James Smart0c9ab6f2010-02-26 14:15:57 -05002630}
2631
2632/**
2633 * lpfc_mbx_cmpl_read_fcf_rec - read fcf completion handler.
2634 * @phba: pointer to lpfc hba data structure.
2635 * @mboxq: pointer to mailbox object.
2636 *
2637 * This is the callback function of read FCF record mailbox command for
James Smarta93ff372010-10-22 11:06:08 -04002638 * updating the eligible FCF bmask for FLOGI failure roundrobin FCF
James Smart0c9ab6f2010-02-26 14:15:57 -05002639 * failover when a new FCF event happened. If the FCF read back is
2640 * valid/available and it passes the connection list check, it updates
James Smarta93ff372010-10-22 11:06:08 -04002641 * the bmask for the eligible FCF record for roundrobin failover.
James Smart0c9ab6f2010-02-26 14:15:57 -05002642 */
2643void
2644lpfc_mbx_cmpl_read_fcf_rec(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2645{
2646 struct fcf_record *new_fcf_record;
2647 uint32_t boot_flag, addr_mode;
2648 uint16_t fcf_index, next_fcf_index;
2649 uint16_t vlan_id;
2650 int rc;
2651
2652 /* If link state is not up, no need to proceed */
2653 if (phba->link_state < LPFC_LINK_UP)
2654 goto out;
2655
2656 /* If FCF discovery period is over, no need to proceed */
James Smart3804dc82010-07-14 15:31:37 -04002657 if (!(phba->fcf.fcf_flag & FCF_DISCOVERY))
James Smart0c9ab6f2010-02-26 14:15:57 -05002658 goto out;
2659
2660 /* Parse the FCF record from the non-embedded mailbox command */
2661 new_fcf_record = lpfc_sli4_fcf_rec_mbox_parse(phba, mboxq,
2662 &next_fcf_index);
2663 if (!new_fcf_record) {
2664 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
2665 "2767 Mailbox command READ_FCF_RECORD "
2666 "failed to retrieve a FCF record.\n");
2667 goto out;
2668 }
2669
2670 /* Check the connection list for eligibility */
2671 rc = lpfc_match_fcf_conn_list(phba, new_fcf_record, &boot_flag,
2672 &addr_mode, &vlan_id);
2673
2674 /* Log the FCF record information if turned on */
2675 lpfc_sli4_log_fcf_record_info(phba, new_fcf_record, vlan_id,
2676 next_fcf_index);
2677
2678 if (!rc)
2679 goto out;
2680
2681 /* Update the eligible FCF record index bmask */
2682 fcf_index = bf_get(lpfc_fcf_record_fcf_index, new_fcf_record);
James Smart7d791df2011-07-22 18:37:52 -04002683
2684 rc = lpfc_sli4_fcf_pri_list_add(phba, fcf_index, new_fcf_record);
James Smart0c9ab6f2010-02-26 14:15:57 -05002685
2686out:
2687 lpfc_sli4_mbox_cmd_free(phba, mboxq);
2688}
2689
2690/**
James Smart76a95d72010-11-20 23:11:48 -05002691 * lpfc_init_vfi_cmpl - Completion handler for init_vfi mbox command.
2692 * @phba: pointer to lpfc hba data structure.
2693 * @mboxq: pointer to mailbox data structure.
2694 *
2695 * This function handles completion of init vfi mailbox command.
2696 */
Rashika Kheriab86a6752014-09-03 12:55:17 -04002697static void
James Smart76a95d72010-11-20 23:11:48 -05002698lpfc_init_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2699{
2700 struct lpfc_vport *vport = mboxq->vport;
2701
James Smartdf9e1b52011-12-13 13:22:17 -05002702 /*
2703 * VFI not supported on interface type 0, just do the flogi
2704 * Also continue if the VFI is in use - just use the same one.
2705 */
2706 if (mboxq->u.mb.mbxStatus &&
2707 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2708 LPFC_SLI_INTF_IF_TYPE_0) &&
2709 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart76a95d72010-11-20 23:11:48 -05002710 lpfc_printf_vlog(vport, KERN_ERR,
2711 LOG_MBOX,
2712 "2891 Init VFI mailbox failed 0x%x\n",
2713 mboxq->u.mb.mbxStatus);
2714 mempool_free(mboxq, phba->mbox_mem_pool);
2715 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2716 return;
2717 }
James Smart73d91e52011-10-10 21:32:10 -04002718
James Smart76a95d72010-11-20 23:11:48 -05002719 lpfc_initial_flogi(vport);
2720 mempool_free(mboxq, phba->mbox_mem_pool);
2721 return;
2722}
2723
2724/**
2725 * lpfc_issue_init_vfi - Issue init_vfi mailbox command.
2726 * @vport: pointer to lpfc_vport data structure.
2727 *
2728 * This function issue a init_vfi mailbox command to initialize the VFI and
2729 * VPI for the physical port.
2730 */
2731void
2732lpfc_issue_init_vfi(struct lpfc_vport *vport)
2733{
2734 LPFC_MBOXQ_t *mboxq;
2735 int rc;
2736 struct lpfc_hba *phba = vport->phba;
2737
2738 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2739 if (!mboxq) {
2740 lpfc_printf_vlog(vport, KERN_ERR,
2741 LOG_MBOX, "2892 Failed to allocate "
2742 "init_vfi mailbox\n");
2743 return;
2744 }
2745 lpfc_init_vfi(mboxq, vport);
2746 mboxq->mbox_cmpl = lpfc_init_vfi_cmpl;
2747 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
2748 if (rc == MBX_NOT_FINISHED) {
2749 lpfc_printf_vlog(vport, KERN_ERR,
2750 LOG_MBOX, "2893 Failed to issue init_vfi mailbox\n");
2751 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2752 }
2753}
2754
2755/**
James Smart1c6834a2009-07-19 10:01:26 -04002756 * lpfc_init_vpi_cmpl - Completion handler for init_vpi mbox command.
2757 * @phba: pointer to lpfc hba data structure.
2758 * @mboxq: pointer to mailbox data structure.
2759 *
2760 * This function handles completion of init vpi mailbox command.
2761 */
James Smart695a8142010-01-26 23:08:03 -05002762void
James Smart1c6834a2009-07-19 10:01:26 -04002763lpfc_init_vpi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2764{
2765 struct lpfc_vport *vport = mboxq->vport;
James Smart695a8142010-01-26 23:08:03 -05002766 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05002767 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2768
James Smart1c6834a2009-07-19 10:01:26 -04002769 if (mboxq->u.mb.mbxStatus) {
2770 lpfc_printf_vlog(vport, KERN_ERR,
2771 LOG_MBOX,
2772 "2609 Init VPI mailbox failed 0x%x\n",
2773 mboxq->u.mb.mbxStatus);
2774 mempool_free(mboxq, phba->mbox_mem_pool);
2775 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2776 return;
2777 }
James Smart72100cc2010-02-12 14:43:01 -05002778 spin_lock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002779 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002780 spin_unlock_irq(shost->host_lock);
James Smart1c6834a2009-07-19 10:01:26 -04002781
James Smart695a8142010-01-26 23:08:03 -05002782 /* If this port is physical port or FDISC is done, do reg_vpi */
2783 if ((phba->pport == vport) || (vport->port_state == LPFC_FDISC)) {
2784 ndlp = lpfc_findnode_did(vport, Fabric_DID);
2785 if (!ndlp)
2786 lpfc_printf_vlog(vport, KERN_ERR,
2787 LOG_DISCOVERY,
2788 "2731 Cannot find fabric "
2789 "controller node\n");
2790 else
2791 lpfc_register_new_vport(phba, vport, ndlp);
2792 mempool_free(mboxq, phba->mbox_mem_pool);
2793 return;
2794 }
2795
James Smart1c6834a2009-07-19 10:01:26 -04002796 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2797 lpfc_initial_fdisc(vport);
2798 else {
2799 lpfc_vport_set_state(vport, FC_VPORT_NO_FABRIC_SUPP);
James Smart6a9c52c2009-10-02 15:16:51 -04002800 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
2801 "2606 No NPIV Fabric support\n");
James Smart1c6834a2009-07-19 10:01:26 -04002802 }
James Smart695a8142010-01-26 23:08:03 -05002803 mempool_free(mboxq, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04002804 return;
2805}
2806
2807/**
James Smartecfd03c2010-02-12 14:41:27 -05002808 * lpfc_issue_init_vpi - Issue init_vpi mailbox command.
2809 * @vport: pointer to lpfc_vport data structure.
2810 *
2811 * This function issue a init_vpi mailbox command to initialize
2812 * VPI for the vport.
2813 */
2814void
2815lpfc_issue_init_vpi(struct lpfc_vport *vport)
2816{
2817 LPFC_MBOXQ_t *mboxq;
James Smart16a3a202013-04-17 20:14:38 -04002818 int rc, vpi;
2819
2820 if ((vport->port_type != LPFC_PHYSICAL_PORT) && (!vport->vpi)) {
2821 vpi = lpfc_alloc_vpi(vport->phba);
2822 if (!vpi) {
2823 lpfc_printf_vlog(vport, KERN_ERR,
2824 LOG_MBOX,
2825 "3303 Failed to obtain vport vpi\n");
2826 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
2827 return;
2828 }
2829 vport->vpi = vpi;
2830 }
James Smartecfd03c2010-02-12 14:41:27 -05002831
2832 mboxq = mempool_alloc(vport->phba->mbox_mem_pool, GFP_KERNEL);
2833 if (!mboxq) {
2834 lpfc_printf_vlog(vport, KERN_ERR,
2835 LOG_MBOX, "2607 Failed to allocate "
2836 "init_vpi mailbox\n");
2837 return;
2838 }
2839 lpfc_init_vpi(vport->phba, mboxq, vport->vpi);
2840 mboxq->vport = vport;
2841 mboxq->mbox_cmpl = lpfc_init_vpi_cmpl;
2842 rc = lpfc_sli_issue_mbox(vport->phba, mboxq, MBX_NOWAIT);
2843 if (rc == MBX_NOT_FINISHED) {
2844 lpfc_printf_vlog(vport, KERN_ERR,
2845 LOG_MBOX, "2608 Failed to issue init_vpi mailbox\n");
2846 mempool_free(mboxq, vport->phba->mbox_mem_pool);
2847 }
2848}
2849
2850/**
James Smart6fb120a2009-05-22 14:52:59 -04002851 * lpfc_start_fdiscs - send fdiscs for each vports on this port.
2852 * @phba: pointer to lpfc hba data structure.
2853 *
2854 * This function loops through the list of vports on the @phba and issues an
2855 * FDISC if possible.
2856 */
2857void
2858lpfc_start_fdiscs(struct lpfc_hba *phba)
2859{
2860 struct lpfc_vport **vports;
2861 int i;
2862
2863 vports = lpfc_create_vport_work_array(phba);
2864 if (vports != NULL) {
2865 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2866 if (vports[i]->port_type == LPFC_PHYSICAL_PORT)
2867 continue;
2868 /* There are no vpi for this vport */
2869 if (vports[i]->vpi > phba->max_vpi) {
2870 lpfc_vport_set_state(vports[i],
2871 FC_VPORT_FAILED);
2872 continue;
2873 }
James Smart76a95d72010-11-20 23:11:48 -05002874 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002875 lpfc_vport_set_state(vports[i],
2876 FC_VPORT_LINKDOWN);
2877 continue;
2878 }
James Smart1c6834a2009-07-19 10:01:26 -04002879 if (vports[i]->fc_flag & FC_VPORT_NEEDS_INIT_VPI) {
James Smartecfd03c2010-02-12 14:41:27 -05002880 lpfc_issue_init_vpi(vports[i]);
James Smart1c6834a2009-07-19 10:01:26 -04002881 continue;
2882 }
James Smart6fb120a2009-05-22 14:52:59 -04002883 if (phba->link_flag & LS_NPIV_FAB_SUPPORTED)
2884 lpfc_initial_fdisc(vports[i]);
2885 else {
2886 lpfc_vport_set_state(vports[i],
2887 FC_VPORT_NO_FABRIC_SUPP);
2888 lpfc_printf_vlog(vports[i], KERN_ERR,
2889 LOG_ELS,
2890 "0259 No NPIV "
2891 "Fabric support\n");
2892 }
2893 }
2894 }
2895 lpfc_destroy_vport_work_array(phba, vports);
2896}
2897
2898void
2899lpfc_mbx_cmpl_reg_vfi(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
2900{
2901 struct lpfc_dmabuf *dmabuf = mboxq->context1;
2902 struct lpfc_vport *vport = mboxq->vport;
James Smart72100cc2010-02-12 14:43:01 -05002903 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04002904
James Smartf5eca9b2012-03-01 22:37:54 -05002905 /*
2906 * VFI not supported for interface type 0, so ignore any mailbox
2907 * error (except VFI in use) and continue with the discovery.
2908 */
2909 if (mboxq->u.mb.mbxStatus &&
2910 (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
2911 LPFC_SLI_INTF_IF_TYPE_0) &&
2912 mboxq->u.mb.mbxStatus != MBX_VFI_IN_USE) {
James Smart6fb120a2009-05-22 14:52:59 -04002913 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
2914 "2018 REG_VFI mbxStatus error x%x "
2915 "HBA state x%x\n",
2916 mboxq->u.mb.mbxStatus, vport->port_state);
James Smart76a95d72010-11-20 23:11:48 -05002917 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart6fb120a2009-05-22 14:52:59 -04002918 /* FLOGI failed, use loop map to make discovery list */
2919 lpfc_disc_list_loopmap(vport);
2920 /* Start discovery */
2921 lpfc_disc_start(vport);
James Smart1b511972011-12-13 13:23:09 -05002922 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002923 }
2924 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smart1b511972011-12-13 13:23:09 -05002925 goto out_free_mem;
James Smart6fb120a2009-05-22 14:52:59 -04002926 }
James Smartae05ebe2013-03-01 16:35:38 -05002927
James Smarte74c03c2013-04-17 20:15:19 -04002928 /* If the VFI is already registered, there is nothing else to do
2929 * Unless this was a VFI update and we are in PT2PT mode, then
2930 * we should drop through to set the port state to ready.
2931 */
James Smartae05ebe2013-03-01 16:35:38 -05002932 if (vport->fc_flag & FC_VFI_REGISTERED)
James Smarte74c03c2013-04-17 20:15:19 -04002933 if (!(phba->sli_rev == LPFC_SLI_REV4 &&
2934 vport->fc_flag & FC_PT2PT))
2935 goto out_free_mem;
James Smartae05ebe2013-03-01 16:35:38 -05002936
James Smartc8685952009-11-18 15:39:16 -05002937 /* The VPI is implicitly registered when the VFI is registered */
James Smart72100cc2010-02-12 14:43:01 -05002938 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05002939 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002940 vport->fc_flag |= FC_VFI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05002941 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart5248a742011-07-22 18:37:06 -04002942 vport->fc_flag &= ~FC_VPORT_NEEDS_INIT_VPI;
James Smart72100cc2010-02-12 14:43:01 -05002943 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04002944
James Smart1b511972011-12-13 13:23:09 -05002945 /* In case SLI4 FC loopback test, we are ready */
2946 if ((phba->sli_rev == LPFC_SLI_REV4) &&
2947 (phba->link_flag & LS_LOOPBACK_MODE)) {
2948 phba->link_state = LPFC_HBA_READY;
2949 goto out_free_mem;
2950 }
2951
James Smarte74c03c2013-04-17 20:15:19 -04002952 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2953 "3313 cmpl reg vfi port_state:%x fc_flag:%x myDid:%x "
2954 "alpacnt:%d LinkState:%x topology:%x\n",
2955 vport->port_state, vport->fc_flag, vport->fc_myDID,
2956 vport->phba->alpa_map[0],
2957 phba->link_state, phba->fc_topology);
2958
James Smart6fb120a2009-05-22 14:52:59 -04002959 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart939723a2012-05-09 21:19:03 -04002960 /*
2961 * For private loop or for NPort pt2pt,
2962 * just start discovery and we are done.
2963 */
2964 if ((vport->fc_flag & FC_PT2PT) ||
2965 ((phba->fc_topology == LPFC_TOPOLOGY_LOOP) &&
2966 !(vport->fc_flag & FC_PUBLIC_LOOP))) {
2967
James Smart76a95d72010-11-20 23:11:48 -05002968 /* Use loop map to make discovery list */
2969 lpfc_disc_list_loopmap(vport);
2970 /* Start discovery */
James Smarte74c03c2013-04-17 20:15:19 -04002971 if (vport->fc_flag & FC_PT2PT)
2972 vport->port_state = LPFC_VPORT_READY;
2973 else
2974 lpfc_disc_start(vport);
James Smart76a95d72010-11-20 23:11:48 -05002975 } else {
2976 lpfc_start_fdiscs(phba);
2977 lpfc_do_scr_ns_plogi(phba, vport);
2978 }
James Smart6fb120a2009-05-22 14:52:59 -04002979 }
2980
James Smart1b511972011-12-13 13:23:09 -05002981out_free_mem:
James Smart6fb120a2009-05-22 14:52:59 -04002982 mempool_free(mboxq, phba->mbox_mem_pool);
James Smartd6de08c2015-12-16 18:11:53 -05002983 if (dmabuf) {
2984 lpfc_mbuf_free(phba, dmabuf->virt, dmabuf->phys);
2985 kfree(dmabuf);
2986 }
James Smart6fb120a2009-05-22 14:52:59 -04002987 return;
2988}
2989
2990static void
James Smart2e0fef82007-06-17 19:56:36 -05002991lpfc_mbx_cmpl_read_sparam(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05002992{
James Smart6fb120a2009-05-22 14:52:59 -04002993 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05002994 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) pmb->context1;
James Smart2e0fef82007-06-17 19:56:36 -05002995 struct lpfc_vport *vport = pmb->vport;
James Smart21bf0b92015-08-31 16:48:21 -04002996 struct serv_parm *sp = &vport->fc_sparam;
2997 uint32_t ed_tov;
dea31012005-04-17 16:05:31 -05002998
2999 /* Check for error */
3000 if (mb->mbxStatus) {
3001 /* READ_SPARAM mbox error <mbxStatus> state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04003002 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3003 "0319 READ_SPARAM mbxStatus error x%x "
3004 "hba state x%x>\n",
3005 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003006 lpfc_linkdown(phba);
dea31012005-04-17 16:05:31 -05003007 goto out;
3008 }
3009
James Smart2e0fef82007-06-17 19:56:36 -05003010 memcpy((uint8_t *) &vport->fc_sparam, (uint8_t *) mp->virt,
dea31012005-04-17 16:05:31 -05003011 sizeof (struct serv_parm));
James Smart21bf0b92015-08-31 16:48:21 -04003012
3013 ed_tov = be32_to_cpu(sp->cmn.e_d_tov);
3014 if (sp->cmn.edtovResolution) /* E_D_TOV ticks are in nanoseconds */
3015 ed_tov = (ed_tov + 999999) / 1000000;
3016
3017 phba->fc_edtov = ed_tov;
3018 phba->fc_ratov = (2 * ed_tov) / 1000;
3019 if (phba->fc_ratov < FF_DEF_RATOV) {
3020 /* RA_TOV should be atleast 10sec for initial flogi */
3021 phba->fc_ratov = FF_DEF_RATOV;
3022 }
3023
James Smart05580562011-05-24 11:40:48 -04003024 lpfc_update_vport_wwn(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05003025 if (vport->port_type == LPFC_PHYSICAL_PORT) {
3026 memcpy(&phba->wwnn, &vport->fc_nodename, sizeof(phba->wwnn));
3027 memcpy(&phba->wwpn, &vport->fc_portname, sizeof(phba->wwnn));
3028 }
3029
dea31012005-04-17 16:05:31 -05003030 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3031 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003032 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003033 return;
3034
3035out:
3036 pmb->context1 = NULL;
3037 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3038 kfree(mp);
James Smart92d7f7b2007-06-17 19:56:38 -05003039 lpfc_issue_clear_la(phba, vport);
3040 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003041 return;
3042}
3043
3044static void
James Smart76a95d72010-11-20 23:11:48 -05003045lpfc_mbx_process_link_up(struct lpfc_hba *phba, struct lpfc_mbx_read_top *la)
dea31012005-04-17 16:05:31 -05003046{
James Smart92d7f7b2007-06-17 19:56:38 -05003047 struct lpfc_vport *vport = phba->pport;
James Smart6fb120a2009-05-22 14:52:59 -04003048 LPFC_MBOXQ_t *sparam_mbox, *cfglink_mbox = NULL;
James Smartdf9e1b52011-12-13 13:22:17 -05003049 struct Scsi_Host *shost;
James Smart2e0fef82007-06-17 19:56:36 -05003050 int i;
James Smart14691152006-12-02 13:34:28 -05003051 struct lpfc_dmabuf *mp;
3052 int rc;
James Smart6fb120a2009-05-22 14:52:59 -04003053 struct fcf_record *fcf_record;
James Smart38c20672013-03-01 16:37:44 -05003054 uint32_t fc_flags = 0;
James Smart14691152006-12-02 13:34:28 -05003055
James Smart92d7f7b2007-06-17 19:56:38 -05003056 spin_lock_irq(&phba->hbalock);
James Smarta085e872015-12-16 18:12:02 -05003057 phba->fc_linkspeed = bf_get(lpfc_mbx_read_top_link_spd, la);
3058
3059 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3060 switch (bf_get(lpfc_mbx_read_top_link_spd, la)) {
3061 case LPFC_LINK_SPEED_1GHZ:
3062 case LPFC_LINK_SPEED_2GHZ:
3063 case LPFC_LINK_SPEED_4GHZ:
3064 case LPFC_LINK_SPEED_8GHZ:
3065 case LPFC_LINK_SPEED_10GHZ:
3066 case LPFC_LINK_SPEED_16GHZ:
3067 case LPFC_LINK_SPEED_32GHZ:
3068 break;
3069 default:
3070 phba->fc_linkspeed = LPFC_LINK_SPEED_UNKNOWN;
3071 break;
3072 }
dea31012005-04-17 16:05:31 -05003073 }
3074
James Smarte74c03c2013-04-17 20:15:19 -04003075 if (phba->fc_topology &&
3076 phba->fc_topology != bf_get(lpfc_mbx_read_top_topology, la)) {
3077 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3078 "3314 Toplogy changed was 0x%x is 0x%x\n",
3079 phba->fc_topology,
3080 bf_get(lpfc_mbx_read_top_topology, la));
3081 phba->fc_topology_changed = 1;
3082 }
3083
James Smart76a95d72010-11-20 23:11:48 -05003084 phba->fc_topology = bf_get(lpfc_mbx_read_top_topology, la);
James Smart92d7f7b2007-06-17 19:56:38 -05003085 phba->link_flag &= ~LS_NPIV_FAB_SUPPORTED;
dea31012005-04-17 16:05:31 -05003086
James Smartdf9e1b52011-12-13 13:22:17 -05003087 shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003088 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003089 phba->sli3_options &= ~LPFC_SLI3_NPIV_ENABLED;
dea31012005-04-17 16:05:31 -05003090
James Smart6a9c52c2009-10-02 15:16:51 -04003091 /* if npiv is enabled and this adapter supports npiv log
3092 * a message that npiv is not supported in this topology
3093 */
3094 if (phba->cfg_enable_npiv && phba->max_vpi)
James Smart495a7142008-06-14 22:52:59 -04003095 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3096 "1309 Link Up Event npiv not supported in loop "
3097 "topology\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003098 /* Get Loop Map information */
James Smart38c20672013-03-01 16:37:44 -05003099 if (bf_get(lpfc_mbx_read_top_il, la))
3100 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003101
James Smart76a95d72010-11-20 23:11:48 -05003102 vport->fc_myDID = bf_get(lpfc_mbx_read_top_alpa_granted, la);
3103 i = la->lilpBde64.tus.f.bdeSize;
dea31012005-04-17 16:05:31 -05003104
3105 if (i == 0) {
3106 phba->alpa_map[0] = 0;
3107 } else {
James Smarte8b62012007-08-02 11:10:09 -04003108 if (vport->cfg_log_verbose & LOG_LINK_EVENT) {
dea31012005-04-17 16:05:31 -05003109 int numalpa, j, k;
3110 union {
3111 uint8_t pamap[16];
3112 struct {
3113 uint32_t wd1;
3114 uint32_t wd2;
3115 uint32_t wd3;
3116 uint32_t wd4;
3117 } pa;
3118 } un;
3119 numalpa = phba->alpa_map[0];
3120 j = 0;
3121 while (j < numalpa) {
3122 memset(un.pamap, 0, 16);
3123 for (k = 1; j < numalpa; k++) {
3124 un.pamap[k - 1] =
3125 phba->alpa_map[j + 1];
3126 j++;
3127 if (k == 16)
3128 break;
3129 }
3130 /* Link Up Event ALPA map */
3131 lpfc_printf_log(phba,
James Smart92d7f7b2007-06-17 19:56:38 -05003132 KERN_WARNING,
3133 LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003134 "1304 Link Up Event "
James Smart92d7f7b2007-06-17 19:56:38 -05003135 "ALPA map Data: x%x "
3136 "x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05003137 un.pa.wd1, un.pa.wd2,
3138 un.pa.wd3, un.pa.wd4);
dea31012005-04-17 16:05:31 -05003139 }
3140 }
3141 }
3142 } else {
James Smart92d7f7b2007-06-17 19:56:38 -05003143 if (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)) {
James Smart78b2d852007-08-02 11:10:21 -04003144 if (phba->max_vpi && phba->cfg_enable_npiv &&
James Smartdf9e1b52011-12-13 13:22:17 -05003145 (phba->sli_rev >= LPFC_SLI_REV3))
James Smart92d7f7b2007-06-17 19:56:38 -05003146 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3147 }
James Smart2e0fef82007-06-17 19:56:36 -05003148 vport->fc_myDID = phba->fc_pref_DID;
James Smart38c20672013-03-01 16:37:44 -05003149 fc_flags |= FC_LBIT;
dea31012005-04-17 16:05:31 -05003150 }
James Smart92d7f7b2007-06-17 19:56:38 -05003151 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003152
James Smart38c20672013-03-01 16:37:44 -05003153 if (fc_flags) {
3154 spin_lock_irq(shost->host_lock);
3155 vport->fc_flag |= fc_flags;
3156 spin_unlock_irq(shost->host_lock);
3157 }
3158
dea31012005-04-17 16:05:31 -05003159 lpfc_linkup(phba);
James Smart9f1177a2010-02-26 14:12:57 -05003160 sparam_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3161 if (!sparam_mbox)
3162 goto out;
3163
3164 rc = lpfc_read_sparam(phba, sparam_mbox, 0);
3165 if (rc) {
3166 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3167 goto out;
3168 }
3169 sparam_mbox->vport = vport;
3170 sparam_mbox->mbox_cmpl = lpfc_mbx_cmpl_read_sparam;
3171 rc = lpfc_sli_issue_mbox(phba, sparam_mbox, MBX_NOWAIT);
3172 if (rc == MBX_NOT_FINISHED) {
3173 mp = (struct lpfc_dmabuf *) sparam_mbox->context1;
3174 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3175 kfree(mp);
3176 mempool_free(sparam_mbox, phba->mbox_mem_pool);
3177 goto out;
dea31012005-04-17 16:05:31 -05003178 }
3179
James Smart76a95d72010-11-20 23:11:48 -05003180 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
James Smart6fb120a2009-05-22 14:52:59 -04003181 cfglink_mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3182 if (!cfglink_mbox)
3183 goto out;
James Smart2e0fef82007-06-17 19:56:36 -05003184 vport->port_state = LPFC_LOCAL_CFG_LINK;
dea31012005-04-17 16:05:31 -05003185 lpfc_config_link(phba, cfglink_mbox);
James Smart2e0fef82007-06-17 19:56:36 -05003186 cfglink_mbox->vport = vport;
Jamie Wellnitz25594c62006-02-28 19:25:34 -05003187 cfglink_mbox->mbox_cmpl = lpfc_mbx_cmpl_local_config_link;
James Smart0b727fe2007-10-27 13:37:25 -04003188 rc = lpfc_sli_issue_mbox(phba, cfglink_mbox, MBX_NOWAIT);
James Smart6fb120a2009-05-22 14:52:59 -04003189 if (rc == MBX_NOT_FINISHED) {
3190 mempool_free(cfglink_mbox, phba->mbox_mem_pool);
3191 goto out;
3192 }
3193 } else {
James Smart32b97932009-07-19 10:01:21 -04003194 vport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04003195 /*
3196 * Add the driver's default FCF record at FCF index 0 now. This
3197 * is phase 1 implementation that support FCF index 0 and driver
3198 * defaults.
3199 */
James Smart45ed1192009-10-02 15:17:02 -04003200 if (!(phba->hba_flag & HBA_FIP_SUPPORT)) {
James Smart6fb120a2009-05-22 14:52:59 -04003201 fcf_record = kzalloc(sizeof(struct fcf_record),
3202 GFP_KERNEL);
3203 if (unlikely(!fcf_record)) {
3204 lpfc_printf_log(phba, KERN_ERR,
3205 LOG_MBOX | LOG_SLI,
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003206 "2554 Could not allocate memory for "
James Smart6fb120a2009-05-22 14:52:59 -04003207 "fcf record\n");
3208 rc = -ENODEV;
3209 goto out;
3210 }
3211
3212 lpfc_sli4_build_dflt_fcf_record(phba, fcf_record,
3213 LPFC_FCOE_FCF_DEF_INDEX);
3214 rc = lpfc_sli4_add_fcf_record(phba, fcf_record);
3215 if (unlikely(rc)) {
3216 lpfc_printf_log(phba, KERN_ERR,
3217 LOG_MBOX | LOG_SLI,
3218 "2013 Could not manually add FCF "
3219 "record 0, status %d\n", rc);
3220 rc = -ENODEV;
3221 kfree(fcf_record);
3222 goto out;
3223 }
3224 kfree(fcf_record);
3225 }
3226 /*
3227 * The driver is expected to do FIP/FCF. Call the port
3228 * and get the FCF Table.
3229 */
James Smart32b97932009-07-19 10:01:21 -04003230 spin_lock_irq(&phba->hbalock);
James Smarta93ff372010-10-22 11:06:08 -04003231 if (phba->hba_flag & FCF_TS_INPROG) {
James Smart32b97932009-07-19 10:01:21 -04003232 spin_unlock_irq(&phba->hbalock);
3233 return;
3234 }
James Smart0c9ab6f2010-02-26 14:15:57 -05003235 /* This is the initial FCF discovery scan */
3236 phba->fcf.fcf_flag |= FCF_INIT_DISC;
James Smart32b97932009-07-19 10:01:21 -04003237 spin_unlock_irq(&phba->hbalock);
James Smart0c9ab6f2010-02-26 14:15:57 -05003238 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3239 "2778 Start FCF table scan at linkup\n");
James Smart0c9ab6f2010-02-26 14:15:57 -05003240 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3241 LPFC_FCOE_FCF_GET_FIRST);
3242 if (rc) {
3243 spin_lock_irq(&phba->hbalock);
3244 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
3245 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04003246 goto out;
James Smart0c9ab6f2010-02-26 14:15:57 -05003247 }
James Smart38b92ef2010-08-04 16:11:39 -04003248 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04003249 lpfc_sli4_clear_fcf_rr_bmask(phba);
dea31012005-04-17 16:05:31 -05003250 }
James Smart6fb120a2009-05-22 14:52:59 -04003251
3252 return;
James Smart92d7f7b2007-06-17 19:56:38 -05003253out:
3254 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte8b62012007-08-02 11:10:09 -04003255 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3256 "0263 Discovery Mailbox error: state: 0x%x : %p %p\n",
3257 vport->port_state, sparam_mbox, cfglink_mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05003258 lpfc_issue_clear_la(phba, vport);
3259 return;
dea31012005-04-17 16:05:31 -05003260}
3261
3262static void
James Smart84774a42008-08-24 21:50:06 -04003263lpfc_enable_la(struct lpfc_hba *phba)
James Smart2e0fef82007-06-17 19:56:36 -05003264{
dea31012005-04-17 16:05:31 -05003265 uint32_t control;
3266 struct lpfc_sli *psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05003267 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003268 psli->sli_flag |= LPFC_PROCESS_LA;
James Smart3772a992009-05-22 14:50:54 -04003269 if (phba->sli_rev <= LPFC_SLI_REV3) {
3270 control = readl(phba->HCregaddr);
3271 control |= HC_LAINT_ENA;
3272 writel(control, phba->HCregaddr);
3273 readl(phba->HCregaddr); /* flush */
3274 }
James Smart2e0fef82007-06-17 19:56:36 -05003275 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003276}
3277
James Smart84774a42008-08-24 21:50:06 -04003278static void
3279lpfc_mbx_issue_link_down(struct lpfc_hba *phba)
3280{
3281 lpfc_linkdown(phba);
3282 lpfc_enable_la(phba);
James Smart6fb120a2009-05-22 14:52:59 -04003283 lpfc_unregister_unused_fcf(phba);
James Smart84774a42008-08-24 21:50:06 -04003284 /* turn on Link Attention interrupts - no CLEAR_LA needed */
3285}
3286
3287
dea31012005-04-17 16:05:31 -05003288/*
James Smart76a95d72010-11-20 23:11:48 -05003289 * This routine handles processing a READ_TOPOLOGY mailbox
dea31012005-04-17 16:05:31 -05003290 * command upon completion. It is setup in the LPFC_MBOXQ
3291 * as the completion routine when the command is
James Smart895427b2017-02-12 13:52:30 -08003292 * handed off to the SLI layer. SLI4 only.
dea31012005-04-17 16:05:31 -05003293 */
3294void
James Smart76a95d72010-11-20 23:11:48 -05003295lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003296{
James Smart2e0fef82007-06-17 19:56:36 -05003297 struct lpfc_vport *vport = pmb->vport;
3298 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart76a95d72010-11-20 23:11:48 -05003299 struct lpfc_mbx_read_top *la;
James Smart895427b2017-02-12 13:52:30 -08003300 struct lpfc_sli_ring *pring;
James Smart04c68492009-05-22 14:52:52 -04003301 MAILBOX_t *mb = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05003302 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3303
James Smart0d2b6b82008-06-14 22:52:47 -04003304 /* Unblock ELS traffic */
James Smart895427b2017-02-12 13:52:30 -08003305 pring = lpfc_phba_elsring(phba);
3306 pring->flag &= ~LPFC_STOP_IOCB_EVENT;
3307
dea31012005-04-17 16:05:31 -05003308 /* Check for error */
3309 if (mb->mbxStatus) {
James Smarted957682007-06-17 19:56:37 -05003310 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003311 "1307 READ_LA mbox error x%x state x%x\n",
3312 mb->mbxStatus, vport->port_state);
dea31012005-04-17 16:05:31 -05003313 lpfc_mbx_issue_link_down(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003314 phba->link_state = LPFC_HBA_ERROR;
James Smart76a95d72010-11-20 23:11:48 -05003315 goto lpfc_mbx_cmpl_read_topology_free_mbuf;
dea31012005-04-17 16:05:31 -05003316 }
3317
James Smart76a95d72010-11-20 23:11:48 -05003318 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
dea31012005-04-17 16:05:31 -05003319
3320 memcpy(&phba->alpa_map[0], mp->virt, 128);
3321
James Smart2e0fef82007-06-17 19:56:36 -05003322 spin_lock_irq(shost->host_lock);
James Smart76a95d72010-11-20 23:11:48 -05003323 if (bf_get(lpfc_mbx_read_top_pb, la))
James Smart2e0fef82007-06-17 19:56:36 -05003324 vport->fc_flag |= FC_BYPASSED_MODE;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003325 else
James Smart2e0fef82007-06-17 19:56:36 -05003326 vport->fc_flag &= ~FC_BYPASSED_MODE;
3327 spin_unlock_irq(shost->host_lock);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05003328
James Smartbe0c0082013-03-01 16:38:14 -05003329 if (phba->fc_eventTag <= la->eventTag) {
dea31012005-04-17 16:05:31 -05003330 phba->fc_stat.LinkMultiEvent++;
James Smart76a95d72010-11-20 23:11:48 -05003331 if (bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP)
dea31012005-04-17 16:05:31 -05003332 if (phba->fc_eventTag != 0)
3333 lpfc_linkdown(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05003334 }
dea31012005-04-17 16:05:31 -05003335
3336 phba->fc_eventTag = la->eventTag;
James Smartbe0c0082013-03-01 16:38:14 -05003337 if (phba->sli_rev < LPFC_SLI_REV4) {
3338 spin_lock_irq(&phba->hbalock);
3339 if (bf_get(lpfc_mbx_read_top_mm, la))
3340 phba->sli.sli_flag |= LPFC_MENLO_MAINT;
3341 else
3342 phba->sli.sli_flag &= ~LPFC_MENLO_MAINT;
3343 spin_unlock_irq(&phba->hbalock);
3344 }
dea31012005-04-17 16:05:31 -05003345
James Smart4d9ab992009-10-02 15:16:39 -04003346 phba->link_events++;
James Smart76a95d72010-11-20 23:11:48 -05003347 if ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP) &&
James Smartbe0c0082013-03-01 16:38:14 -05003348 !(phba->sli.sli_flag & LPFC_MENLO_MAINT)) {
dea31012005-04-17 16:05:31 -05003349 phba->fc_stat.LinkUp++;
James Smart2e0fef82007-06-17 19:56:36 -05003350 if (phba->link_flag & LS_LOOPBACK_MODE) {
James Smart3163f722008-02-08 18:50:25 -05003351 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003352 "1306 Link Up Event in loop back mode "
3353 "x%x received Data: x%x x%x x%x x%x\n",
3354 la->eventTag, phba->fc_eventTag,
James Smart76a95d72010-11-20 23:11:48 -05003355 bf_get(lpfc_mbx_read_top_alpa_granted,
3356 la),
3357 bf_get(lpfc_mbx_read_top_link_spd, la),
James Smarte8b62012007-08-02 11:10:09 -04003358 phba->alpa_map[0]);
James Smart5b8bd0c2007-04-25 09:52:49 -04003359 } else {
3360 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003361 "1303 Link Up Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003362 "Data: x%x x%x x%x x%x x%x x%x %d\n",
James Smarte8b62012007-08-02 11:10:09 -04003363 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 Smart84774a42008-08-24 21:50:06 -04003367 phba->alpa_map[0],
James Smart76a95d72010-11-20 23:11:48 -05003368 bf_get(lpfc_mbx_read_top_mm, la),
3369 bf_get(lpfc_mbx_read_top_fa, la),
James Smart84774a42008-08-24 21:50:06 -04003370 phba->wait_4_mlo_maint_flg);
James Smart5b8bd0c2007-04-25 09:52:49 -04003371 }
James Smart92d7f7b2007-06-17 19:56:38 -05003372 lpfc_mbx_process_link_up(phba, la);
James Smart76a95d72010-11-20 23:11:48 -05003373 } else if (bf_get(lpfc_mbx_read_top_att_type, la) ==
3374 LPFC_ATT_LINK_DOWN) {
dea31012005-04-17 16:05:31 -05003375 phba->fc_stat.LinkDown++;
James Smart1b511972011-12-13 13:23:09 -05003376 if (phba->link_flag & LS_LOOPBACK_MODE)
James Smart3163f722008-02-08 18:50:25 -05003377 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3378 "1308 Link Down Event in loop back mode "
3379 "x%x received "
3380 "Data: x%x x%x x%x\n",
3381 la->eventTag, phba->fc_eventTag,
3382 phba->pport->port_state, vport->fc_flag);
James Smart1b511972011-12-13 13:23:09 -05003383 else
James Smart3163f722008-02-08 18:50:25 -05003384 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
James Smarte8b62012007-08-02 11:10:09 -04003385 "1305 Link Down Event x%x received "
James Smart84774a42008-08-24 21:50:06 -04003386 "Data: x%x x%x x%x x%x x%x\n",
3387 la->eventTag, phba->fc_eventTag,
3388 phba->pport->port_state, vport->fc_flag,
James Smart76a95d72010-11-20 23:11:48 -05003389 bf_get(lpfc_mbx_read_top_mm, la),
3390 bf_get(lpfc_mbx_read_top_fa, la));
James Smart84774a42008-08-24 21:50:06 -04003391 lpfc_mbx_issue_link_down(phba);
3392 }
James Smartbe0c0082013-03-01 16:38:14 -05003393 if ((phba->sli.sli_flag & LPFC_MENLO_MAINT) &&
3394 ((bf_get(lpfc_mbx_read_top_att_type, la) == LPFC_ATT_LINK_UP))) {
James Smart84774a42008-08-24 21:50:06 -04003395 if (phba->link_state != LPFC_LINK_DOWN) {
3396 phba->fc_stat.LinkDown++;
3397 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3398 "1312 Link Down Event x%x received "
dea31012005-04-17 16:05:31 -05003399 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003400 la->eventTag, phba->fc_eventTag,
James Smart2e0fef82007-06-17 19:56:36 -05003401 phba->pport->port_state, vport->fc_flag);
James Smart84774a42008-08-24 21:50:06 -04003402 lpfc_mbx_issue_link_down(phba);
3403 } else
3404 lpfc_enable_la(phba);
3405
3406 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
3407 "1310 Menlo Maint Mode Link up Event x%x rcvd "
3408 "Data: x%x x%x x%x\n",
3409 la->eventTag, phba->fc_eventTag,
3410 phba->pport->port_state, vport->fc_flag);
3411 /*
3412 * The cmnd that triggered this will be waiting for this
3413 * signal.
3414 */
3415 /* WAKEUP for MENLO_SET_MODE or MENLO_RESET command. */
3416 if (phba->wait_4_mlo_maint_flg) {
3417 phba->wait_4_mlo_maint_flg = 0;
3418 wake_up_interruptible(&phba->wait_4_mlo_m_q);
James Smart3163f722008-02-08 18:50:25 -05003419 }
James Smart84774a42008-08-24 21:50:06 -04003420 }
3421
James Smartbe0c0082013-03-01 16:38:14 -05003422 if ((phba->sli_rev < LPFC_SLI_REV4) &&
3423 bf_get(lpfc_mbx_read_top_fa, la)) {
3424 if (phba->sli.sli_flag & LPFC_MENLO_MAINT)
James Smart84774a42008-08-24 21:50:06 -04003425 lpfc_issue_clear_la(phba, vport);
3426 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
James Smart76a95d72010-11-20 23:11:48 -05003427 "1311 fa %d\n",
3428 bf_get(lpfc_mbx_read_top_fa, la));
dea31012005-04-17 16:05:31 -05003429 }
3430
James Smart76a95d72010-11-20 23:11:48 -05003431lpfc_mbx_cmpl_read_topology_free_mbuf:
dea31012005-04-17 16:05:31 -05003432 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3433 kfree(mp);
3434 mempool_free(pmb, phba->mbox_mem_pool);
3435 return;
3436}
3437
3438/*
3439 * This routine handles processing a REG_LOGIN mailbox
3440 * command upon completion. It is setup in the LPFC_MBOXQ
3441 * as the completion routine when the command is
3442 * handed off to the SLI layer.
3443 */
3444void
James Smart2e0fef82007-06-17 19:56:36 -05003445lpfc_mbx_cmpl_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003446{
James Smart2e0fef82007-06-17 19:56:36 -05003447 struct lpfc_vport *vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05003448 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart2e0fef82007-06-17 19:56:36 -05003449 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smartffc95492010-06-07 15:23:17 -04003450 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05003451
dea31012005-04-17 16:05:31 -05003452 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003453 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05003454
James Smartbe6bb942015-04-07 15:07:22 -04003455 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3456 "0002 rpi:%x DID:%x flg:%x %d map:%x %p\n",
3457 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003458 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003459 ndlp->nlp_usg_map, ndlp);
James Smartffc95492010-06-07 15:23:17 -04003460 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
3461 ndlp->nlp_flag &= ~NLP_REG_LOGIN_SEND;
3462
James Smart40426292010-12-15 17:58:10 -05003463 if (ndlp->nlp_flag & NLP_IGNR_REG_CMPL ||
3464 ndlp->nlp_state != NLP_STE_REG_LOGIN_ISSUE) {
James Smartffc95492010-06-07 15:23:17 -04003465 /* We rcvd a rscn after issuing this
3466 * mbox reg login, we may have cycled
3467 * back through the state and be
3468 * back at reg login state so this
3469 * mbox needs to be ignored becase
3470 * there is another reg login in
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003471 * process.
James Smartffc95492010-06-07 15:23:17 -04003472 */
3473 spin_lock_irq(shost->host_lock);
3474 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
3475 spin_unlock_irq(shost->host_lock);
James Smart895427b2017-02-12 13:52:30 -08003476
3477 /*
3478 * We cannot leave the RPI registered because
3479 * if we go thru discovery again for this ndlp
3480 * a subsequent REG_RPI will fail.
3481 */
3482 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
3483 lpfc_unreg_rpi(vport, ndlp);
James Smart4b7789b2015-12-16 18:11:55 -05003484 }
3485
3486 /* Call state machine */
3487 lpfc_disc_state_machine(vport, ndlp, pmb, NLP_EVT_CMPL_REG_LOGIN);
James Smartffc95492010-06-07 15:23:17 -04003488
dea31012005-04-17 16:05:31 -05003489 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3490 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003491 mempool_free(pmb, phba->mbox_mem_pool);
James Smartfa4066b2008-01-11 01:53:27 -05003492 /* decrement the node reference count held for this callback
3493 * function.
3494 */
James Smart329f9bc2007-04-25 09:53:01 -04003495 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003496
3497 return;
3498}
3499
James Smart92d7f7b2007-06-17 19:56:38 -05003500static void
3501lpfc_mbx_cmpl_unreg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3502{
James Smart04c68492009-05-22 14:52:52 -04003503 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003504 struct lpfc_vport *vport = pmb->vport;
3505 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3506
3507 switch (mb->mbxStatus) {
3508 case 0x0011:
3509 case 0x0020:
James Smarte8b62012007-08-02 11:10:09 -04003510 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3511 "0911 cmpl_unreg_vpi, mb status = 0x%x\n",
3512 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003513 break;
James Smart78730cf2010-04-06 15:06:30 -04003514 /* If VPI is busy, reset the HBA */
3515 case 0x9700:
3516 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
3517 "2798 Unreg_vpi failed vpi 0x%x, mb status = 0x%x\n",
3518 vport->vpi, mb->mbxStatus);
3519 if (!(phba->pport->load_flag & FC_UNLOADING))
3520 lpfc_workq_post_event(phba, NULL, NULL,
3521 LPFC_EVT_RESET_HBA);
James Smart92d7f7b2007-06-17 19:56:38 -05003522 }
James Smart72100cc2010-02-12 14:43:01 -05003523 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003524 vport->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart19878072009-12-21 17:02:00 -05003525 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003526 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003527 vport->unreg_vpi_cmpl = VPORT_OK;
3528 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1151e3e2011-02-16 12:39:35 -05003529 lpfc_cleanup_vports_rrqs(vport, NULL);
James Smart92d7f7b2007-06-17 19:56:38 -05003530 /*
3531 * This shost reference might have been taken at the beginning of
3532 * lpfc_vport_delete()
3533 */
James Smart1c6f4ef52009-11-18 15:40:49 -05003534 if ((vport->load_flag & FC_UNLOADING) && (vport != phba->pport))
James Smart92d7f7b2007-06-17 19:56:38 -05003535 scsi_host_put(shost);
3536}
3537
James Smartd7c255b2008-08-24 21:50:00 -04003538int
James Smart92d7f7b2007-06-17 19:56:38 -05003539lpfc_mbx_unreg_vpi(struct lpfc_vport *vport)
3540{
3541 struct lpfc_hba *phba = vport->phba;
3542 LPFC_MBOXQ_t *mbox;
3543 int rc;
3544
3545 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3546 if (!mbox)
James Smartd7c255b2008-08-24 21:50:00 -04003547 return 1;
James Smart92d7f7b2007-06-17 19:56:38 -05003548
3549 lpfc_unreg_vpi(phba, vport->vpi, mbox);
3550 mbox->vport = vport;
3551 mbox->mbox_cmpl = lpfc_mbx_cmpl_unreg_vpi;
James Smart0b727fe2007-10-27 13:37:25 -04003552 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart92d7f7b2007-06-17 19:56:38 -05003553 if (rc == MBX_NOT_FINISHED) {
James Smarte8b62012007-08-02 11:10:09 -04003554 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
3555 "1800 Could not issue unreg_vpi\n");
James Smart92d7f7b2007-06-17 19:56:38 -05003556 mempool_free(mbox, phba->mbox_mem_pool);
3557 vport->unreg_vpi_cmpl = VPORT_ERROR;
James Smartd7c255b2008-08-24 21:50:00 -04003558 return rc;
James Smart92d7f7b2007-06-17 19:56:38 -05003559 }
James Smartd7c255b2008-08-24 21:50:00 -04003560 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05003561}
3562
3563static void
3564lpfc_mbx_cmpl_reg_vpi(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
3565{
3566 struct lpfc_vport *vport = pmb->vport;
3567 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart04c68492009-05-22 14:52:52 -04003568 MAILBOX_t *mb = &pmb->u.mb;
James Smart92d7f7b2007-06-17 19:56:38 -05003569
3570 switch (mb->mbxStatus) {
3571 case 0x0011:
3572 case 0x9601:
3573 case 0x9602:
James Smarte8b62012007-08-02 11:10:09 -04003574 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
3575 "0912 cmpl_reg_vpi, mb status = 0x%x\n",
3576 mb->mbxStatus);
James Smart92d7f7b2007-06-17 19:56:38 -05003577 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
3578 spin_lock_irq(shost->host_lock);
3579 vport->fc_flag &= ~(FC_FABRIC | FC_PUBLIC_LOOP);
3580 spin_unlock_irq(shost->host_lock);
3581 vport->fc_myDID = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003582
3583 /* todo: init: revise localport nvme attributes */
James Smart92d7f7b2007-06-17 19:56:38 -05003584 goto out;
3585 }
James Smart92d7f7b2007-06-17 19:56:38 -05003586
James Smart72100cc2010-02-12 14:43:01 -05003587 spin_lock_irq(shost->host_lock);
James Smartc8685952009-11-18 15:39:16 -05003588 vport->vpi_state |= LPFC_VPI_REGISTERED;
James Smart695a8142010-01-26 23:08:03 -05003589 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
James Smart72100cc2010-02-12 14:43:01 -05003590 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05003591 vport->num_disc_nodes = 0;
3592 /* go thru NPR list and issue ELS PLOGIs */
3593 if (vport->fc_npr_cnt)
3594 lpfc_els_disc_plogi(vport);
3595
3596 if (!vport->num_disc_nodes) {
3597 spin_lock_irq(shost->host_lock);
3598 vport->fc_flag &= ~FC_NDISC_ACTIVE;
3599 spin_unlock_irq(shost->host_lock);
3600 lpfc_can_disctmo(vport);
3601 }
3602 vport->port_state = LPFC_VPORT_READY;
3603
3604out:
3605 mempool_free(pmb, phba->mbox_mem_pool);
3606 return;
3607}
3608
James Smart21e9a0a2009-05-22 14:53:21 -04003609/**
3610 * lpfc_create_static_vport - Read HBA config region to create static vports.
3611 * @phba: pointer to lpfc hba data structure.
3612 *
3613 * This routine issue a DUMP mailbox command for config region 22 to get
3614 * the list of static vports to be created. The function create vports
3615 * based on the information returned from the HBA.
3616 **/
3617void
3618lpfc_create_static_vport(struct lpfc_hba *phba)
3619{
3620 LPFC_MBOXQ_t *pmb = NULL;
3621 MAILBOX_t *mb;
3622 struct static_vport_info *vport_info;
James Smartcdcc2342012-08-14 14:26:35 -04003623 int mbx_wait_rc = 0, i;
James Smart21e9a0a2009-05-22 14:53:21 -04003624 struct fc_vport_identifiers vport_id;
3625 struct fc_vport *new_fc_vport;
3626 struct Scsi_Host *shost;
3627 struct lpfc_vport *vport;
3628 uint16_t offset = 0;
3629 uint8_t *vport_buff;
James Smart1c6834a2009-07-19 10:01:26 -04003630 struct lpfc_dmabuf *mp;
3631 uint32_t byte_count = 0;
James Smart21e9a0a2009-05-22 14:53:21 -04003632
3633 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3634 if (!pmb) {
3635 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3636 "0542 lpfc_create_static_vport failed to"
3637 " allocate mailbox memory\n");
3638 return;
3639 }
James Smartcdcc2342012-08-14 14:26:35 -04003640 memset(pmb, 0, sizeof(LPFC_MBOXQ_t));
James Smart21e9a0a2009-05-22 14:53:21 -04003641 mb = &pmb->u.mb;
3642
3643 vport_info = kzalloc(sizeof(struct static_vport_info), GFP_KERNEL);
3644 if (!vport_info) {
3645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3646 "0543 lpfc_create_static_vport failed to"
3647 " allocate vport_info\n");
3648 mempool_free(pmb, phba->mbox_mem_pool);
3649 return;
3650 }
3651
3652 vport_buff = (uint8_t *) vport_info;
3653 do {
James Smartcdcc2342012-08-14 14:26:35 -04003654 /* free dma buffer from previous round */
3655 if (pmb->context1) {
3656 mp = (struct lpfc_dmabuf *)pmb->context1;
3657 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3658 kfree(mp);
3659 }
James Smart1c6834a2009-07-19 10:01:26 -04003660 if (lpfc_dump_static_vport(phba, pmb, offset))
3661 goto out;
3662
James Smart21e9a0a2009-05-22 14:53:21 -04003663 pmb->vport = phba->pport;
James Smartcdcc2342012-08-14 14:26:35 -04003664 mbx_wait_rc = lpfc_sli_issue_mbox_wait(phba, pmb,
3665 LPFC_MBOX_TMO);
James Smart21e9a0a2009-05-22 14:53:21 -04003666
James Smartcdcc2342012-08-14 14:26:35 -04003667 if ((mbx_wait_rc != MBX_SUCCESS) || mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003668 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3669 "0544 lpfc_create_static_vport failed to"
3670 " issue dump mailbox command ret 0x%x "
3671 "status 0x%x\n",
James Smartcdcc2342012-08-14 14:26:35 -04003672 mbx_wait_rc, mb->mbxStatus);
James Smart21e9a0a2009-05-22 14:53:21 -04003673 goto out;
3674 }
3675
James Smart1c6834a2009-07-19 10:01:26 -04003676 if (phba->sli_rev == LPFC_SLI_REV4) {
3677 byte_count = pmb->u.mqe.un.mb_words[5];
James Smartcdcc2342012-08-14 14:26:35 -04003678 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003679 if (byte_count > sizeof(struct static_vport_info) -
3680 offset)
3681 byte_count = sizeof(struct static_vport_info)
3682 - offset;
3683 memcpy(vport_buff + offset, mp->virt, byte_count);
3684 offset += byte_count;
3685 } else {
3686 if (mb->un.varDmp.word_cnt >
3687 sizeof(struct static_vport_info) - offset)
3688 mb->un.varDmp.word_cnt =
3689 sizeof(struct static_vport_info)
3690 - offset;
3691 byte_count = mb->un.varDmp.word_cnt;
3692 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
3693 vport_buff + offset,
3694 byte_count);
James Smart21e9a0a2009-05-22 14:53:21 -04003695
James Smart1c6834a2009-07-19 10:01:26 -04003696 offset += byte_count;
3697 }
James Smart21e9a0a2009-05-22 14:53:21 -04003698
James Smart1c6834a2009-07-19 10:01:26 -04003699 } while (byte_count &&
James Smart21e9a0a2009-05-22 14:53:21 -04003700 offset < sizeof(struct static_vport_info));
3701
3702
3703 if ((le32_to_cpu(vport_info->signature) != VPORT_INFO_SIG) ||
3704 ((le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK)
3705 != VPORT_INFO_REV)) {
3706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3707 "0545 lpfc_create_static_vport bad"
3708 " information header 0x%x 0x%x\n",
3709 le32_to_cpu(vport_info->signature),
3710 le32_to_cpu(vport_info->rev) & VPORT_INFO_REV_MASK);
3711
3712 goto out;
3713 }
3714
3715 shost = lpfc_shost_from_vport(phba->pport);
3716
3717 for (i = 0; i < MAX_STATIC_VPORT_COUNT; i++) {
3718 memset(&vport_id, 0, sizeof(vport_id));
3719 vport_id.port_name = wwn_to_u64(vport_info->vport_list[i].wwpn);
3720 vport_id.node_name = wwn_to_u64(vport_info->vport_list[i].wwnn);
3721 if (!vport_id.port_name || !vport_id.node_name)
3722 continue;
3723
3724 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
3725 vport_id.vport_type = FC_PORTTYPE_NPIV;
3726 vport_id.disable = false;
3727 new_fc_vport = fc_vport_create(shost, 0, &vport_id);
3728
3729 if (!new_fc_vport) {
3730 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
3731 "0546 lpfc_create_static_vport failed to"
James Smarte4e74272009-07-19 10:01:38 -04003732 " create vport\n");
James Smart21e9a0a2009-05-22 14:53:21 -04003733 continue;
3734 }
3735
3736 vport = *(struct lpfc_vport **)new_fc_vport->dd_data;
3737 vport->vport_flag |= STATIC_VPORT;
3738 }
3739
3740out:
James Smart21e9a0a2009-05-22 14:53:21 -04003741 kfree(vport_info);
James Smartcdcc2342012-08-14 14:26:35 -04003742 if (mbx_wait_rc != MBX_TIMEOUT) {
3743 if (pmb->context1) {
3744 mp = (struct lpfc_dmabuf *)pmb->context1;
James Smart1c6834a2009-07-19 10:01:26 -04003745 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3746 kfree(mp);
3747 }
James Smart21e9a0a2009-05-22 14:53:21 -04003748 mempool_free(pmb, phba->mbox_mem_pool);
James Smart1c6834a2009-07-19 10:01:26 -04003749 }
James Smart21e9a0a2009-05-22 14:53:21 -04003750
3751 return;
3752}
3753
dea31012005-04-17 16:05:31 -05003754/*
3755 * This routine handles processing a Fabric REG_LOGIN mailbox
3756 * command upon completion. It is setup in the LPFC_MBOXQ
3757 * as the completion routine when the command is
3758 * handed off to the SLI layer.
3759 */
3760void
James Smart2e0fef82007-06-17 19:56:36 -05003761lpfc_mbx_cmpl_fabric_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003762{
James Smart92d7f7b2007-06-17 19:56:38 -05003763 struct lpfc_vport *vport = pmb->vport;
James Smart21e9a0a2009-05-22 14:53:21 -04003764 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003765 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart92d7f7b2007-06-17 19:56:38 -05003766 struct lpfc_nodelist *ndlp;
James Smartdf9e1b52011-12-13 13:22:17 -05003767 struct Scsi_Host *shost;
dea31012005-04-17 16:05:31 -05003768
James Smart549e55c2007-08-02 11:09:51 -04003769 ndlp = (struct lpfc_nodelist *) pmb->context2;
James Smart329f9bc2007-04-25 09:53:01 -04003770 pmb->context1 = NULL;
3771 pmb->context2 = NULL;
James Smartd439d282010-09-29 11:18:45 -04003772
dea31012005-04-17 16:05:31 -05003773 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003774 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX,
3775 "0258 Register Fabric login error: 0x%x\n",
3776 mb->mbxStatus);
dea31012005-04-17 16:05:31 -05003777 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3778 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003779 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003780
James Smart76a95d72010-11-20 23:11:48 -05003781 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003782 /* FLOGI failed, use loop map to make discovery list */
3783 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003784
James Smart92d7f7b2007-06-17 19:56:38 -05003785 /* Start discovery */
3786 lpfc_disc_start(vport);
James Smarte47c9092008-02-08 18:49:26 -05003787 /* Decrement the reference count to ndlp after the
3788 * reference to the ndlp are done.
3789 */
3790 lpfc_nlp_put(ndlp);
James Smart92d7f7b2007-06-17 19:56:38 -05003791 return;
3792 }
3793
3794 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
James Smarte47c9092008-02-08 18:49:26 -05003795 /* Decrement the reference count to ndlp after the reference
3796 * to the ndlp are done.
3797 */
3798 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003799 return;
3800 }
3801
James Smart6d368e52011-05-24 11:44:12 -04003802 if (phba->sli_rev < LPFC_SLI_REV4)
3803 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003804 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003805 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003806 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05003807
James Smart2e0fef82007-06-17 19:56:36 -05003808 if (vport->port_state == LPFC_FABRIC_CFG_LINK) {
James Smart4b40c592010-03-15 11:25:44 -04003809 /* when physical port receive logo donot start
3810 * vport discovery */
3811 if (!(vport->fc_flag & FC_LOGO_RCVD_DID_CHNG))
3812 lpfc_start_fdiscs(phba);
James Smartdf9e1b52011-12-13 13:22:17 -05003813 else {
3814 shost = lpfc_shost_from_vport(vport);
3815 spin_lock_irq(shost->host_lock);
James Smart4b40c592010-03-15 11:25:44 -04003816 vport->fc_flag &= ~FC_LOGO_RCVD_DID_CHNG ;
James Smartdf9e1b52011-12-13 13:22:17 -05003817 spin_unlock_irq(shost->host_lock);
3818 }
James Smart92d7f7b2007-06-17 19:56:38 -05003819 lpfc_do_scr_ns_plogi(phba, vport);
dea31012005-04-17 16:05:31 -05003820 }
3821
3822 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3823 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04003824 mempool_free(pmb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05003825
3826 /* Drop the reference count from the mbox at the end after
3827 * all the current reference to the ndlp have been done.
3828 */
3829 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003830 return;
3831}
3832
James Smarta0f2d3e2017-02-12 13:52:31 -08003833 /*
3834 * This routine will issue a GID_FT for each FC4 Type supported
3835 * by the driver. ALL GID_FTs must complete before discovery is started.
3836 */
3837int
3838lpfc_issue_gidft(struct lpfc_vport *vport)
3839{
3840 struct lpfc_hba *phba = vport->phba;
3841
3842 /* Good status, issue CT Request to NameServer */
3843 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3844 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP)) {
3845 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_FCP)) {
3846 /* Cannot issue NameServer FCP Query, so finish up
3847 * discovery
3848 */
3849 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3850 "0604 %s FC TYPE %x %s\n",
3851 "Failed to issue GID_FT to ",
3852 FC_TYPE_FCP,
3853 "Finishing discovery.");
3854 return 0;
3855 }
3856 vport->gidft_inp++;
3857 }
3858
3859 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3860 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) {
3861 if (lpfc_ns_cmd(vport, SLI_CTNS_GID_FT, 0, SLI_CTPT_NVME)) {
3862 /* Cannot issue NameServer NVME Query, so finish up
3863 * discovery
3864 */
3865 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
3866 "0605 %s FC_TYPE %x %s %d\n",
3867 "Failed to issue GID_FT to ",
3868 FC_TYPE_NVME,
3869 "Finishing discovery: gidftinp ",
3870 vport->gidft_inp);
3871 if (vport->gidft_inp == 0)
3872 return 0;
3873 } else
3874 vport->gidft_inp++;
3875 }
3876 return vport->gidft_inp;
3877}
3878
dea31012005-04-17 16:05:31 -05003879/*
3880 * This routine handles processing a NameServer REG_LOGIN mailbox
3881 * command upon completion. It is setup in the LPFC_MBOXQ
3882 * as the completion routine when the command is
3883 * handed off to the SLI layer.
3884 */
3885void
James Smart2e0fef82007-06-17 19:56:36 -05003886lpfc_mbx_cmpl_ns_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05003887{
James Smart21e9a0a2009-05-22 14:53:21 -04003888 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05003889 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
3890 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
3891 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05003892
James Smartd439d282010-09-29 11:18:45 -04003893 pmb->context1 = NULL;
3894 pmb->context2 = NULL;
James Smarta0f2d3e2017-02-12 13:52:31 -08003895 vport->gidft_inp = 0;
James Smartd439d282010-09-29 11:18:45 -04003896
dea31012005-04-17 16:05:31 -05003897 if (mb->mbxStatus) {
James Smart21e9a0a2009-05-22 14:53:21 -04003898 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
3899 "0260 Register NameServer error: 0x%x\n",
3900 mb->mbxStatus);
James Smarta0f2d3e2017-02-12 13:52:31 -08003901
3902out:
James Smartfa4066b2008-01-11 01:53:27 -05003903 /* decrement the node reference count held for this
3904 * callback function.
3905 */
James Smart329f9bc2007-04-25 09:53:01 -04003906 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003907 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3908 kfree(mp);
James Smartde0c5b32007-04-25 09:52:27 -04003909 mempool_free(pmb, phba->mbox_mem_pool);
James Smart87af33f2007-10-27 13:37:43 -04003910
3911 /* If no other thread is using the ndlp, free it */
3912 lpfc_nlp_not_used(ndlp);
dea31012005-04-17 16:05:31 -05003913
James Smart76a95d72010-11-20 23:11:48 -05003914 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
James Smart92d7f7b2007-06-17 19:56:38 -05003915 /*
3916 * RegLogin failed, use loop map to make discovery
3917 * list
3918 */
3919 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05003920
James Smart92d7f7b2007-06-17 19:56:38 -05003921 /* Start discovery */
3922 lpfc_disc_start(vport);
3923 return;
3924 }
3925 lpfc_vport_set_state(vport, FC_VPORT_FAILED);
dea31012005-04-17 16:05:31 -05003926 return;
3927 }
3928
James Smart6d368e52011-05-24 11:44:12 -04003929 if (phba->sli_rev < LPFC_SLI_REV4)
3930 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05003931 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05003932 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05003933 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04003934 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3935 "0003 rpi:%x DID:%x flg:%x %d map%x %p\n",
3936 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01003937 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04003938 ndlp->nlp_usg_map, ndlp);
dea31012005-04-17 16:05:31 -05003939
James Smart2e0fef82007-06-17 19:56:36 -05003940 if (vport->port_state < LPFC_VPORT_READY) {
3941 /* Link up discovery requires Fabric registration. */
James Smart92d7f7b2007-06-17 19:56:38 -05003942 lpfc_ns_cmd(vport, SLI_CTNS_RNN_ID, 0, 0);
3943 lpfc_ns_cmd(vport, SLI_CTNS_RSNN_NN, 0, 0);
3944 lpfc_ns_cmd(vport, SLI_CTNS_RSPN_ID, 0, 0);
3945 lpfc_ns_cmd(vport, SLI_CTNS_RFT_ID, 0, 0);
James Smarta0f2d3e2017-02-12 13:52:31 -08003946
3947 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3948 (phba->cfg_enable_fc4_type == LPFC_ENABLE_FCP))
3949 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_FCP);
3950
3951 if ((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
3952 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME))
3953 lpfc_ns_cmd(vport, SLI_CTNS_RFF_ID, 0, FC_TYPE_NVME);
James Smart92d7f7b2007-06-17 19:56:38 -05003954
3955 /* Issue SCR just before NameServer GID_FT Query */
3956 lpfc_issue_els_scr(vport, SCR_DID, 0);
dea31012005-04-17 16:05:31 -05003957 }
3958
James Smart2e0fef82007-06-17 19:56:36 -05003959 vport->fc_ns_retry = 0;
James Smarta0f2d3e2017-02-12 13:52:31 -08003960 if (lpfc_issue_gidft(vport) == 0)
James Smart92d7f7b2007-06-17 19:56:38 -05003961 goto out;
dea31012005-04-17 16:05:31 -05003962
James Smarta0f2d3e2017-02-12 13:52:31 -08003963 /*
3964 * At this point in time we may need to wait for multiple
3965 * SLI_CTNS_GID_FT CT commands to complete before we start discovery.
3966 *
3967 * decrement the node reference count held for this
James Smartfa4066b2008-01-11 01:53:27 -05003968 * callback function.
3969 */
James Smart329f9bc2007-04-25 09:53:01 -04003970 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05003971 lpfc_mbuf_free(phba, mp->virt, mp->phys);
3972 kfree(mp);
James Smart2e0fef82007-06-17 19:56:36 -05003973 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05003974
3975 return;
3976}
3977
3978static void
James Smart2e0fef82007-06-17 19:56:36 -05003979lpfc_register_remote_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05003980{
James Smart2e0fef82007-06-17 19:56:36 -05003981 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
3982 struct fc_rport *rport;
dea31012005-04-17 16:05:31 -05003983 struct lpfc_rport_data *rdata;
3984 struct fc_rport_identifiers rport_ids;
James Smart2e0fef82007-06-17 19:56:36 -05003985 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05003986
James Smart895427b2017-02-12 13:52:30 -08003987 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
3988 return;
3989
dea31012005-04-17 16:05:31 -05003990 /* Remote port has reappeared. Re-register w/ FC transport */
Andrew Morton68ce1eb2005-09-21 09:46:54 -07003991 rport_ids.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
3992 rport_ids.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
dea31012005-04-17 16:05:31 -05003993 rport_ids.port_id = ndlp->nlp_DID;
3994 rport_ids.roles = FC_RPORT_ROLE_UNKNOWN;
dea31012005-04-17 16:05:31 -05003995
James Smart329f9bc2007-04-25 09:53:01 -04003996 /*
3997 * We leave our node pointer in rport->dd_data when we unregister a
3998 * FCP target port. But fc_remote_port_add zeros the space to which
3999 * rport->dd_data points. So, if we're reusing a previously
4000 * registered port, drop the reference that we took the last time we
4001 * registered the port.
4002 */
James Smart466e8402015-05-21 13:55:28 -04004003 rport = ndlp->rport;
4004 if (rport) {
4005 rdata = rport->dd_data;
4006 /* break the link before dropping the ref */
4007 ndlp->rport = NULL;
4008 if (rdata && rdata->pnode == ndlp)
4009 lpfc_nlp_put(ndlp);
4010 rdata->pnode = NULL;
4011 /* drop reference for earlier registeration */
4012 put_device(&rport->dev);
4013 }
James Smart858c9f62007-06-17 19:56:39 -05004014
4015 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
4016 "rport add: did:x%x flg:x%x type x%x",
4017 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4018
James Smart9589b0622011-04-16 11:03:17 -04004019 /* Don't add the remote port if unloading. */
4020 if (vport->load_flag & FC_UNLOADING)
4021 return;
4022
James Smart2e0fef82007-06-17 19:56:36 -05004023 ndlp->rport = rport = fc_remote_port_add(shost, 0, &rport_ids);
James Smart329f9bc2007-04-25 09:53:01 -04004024 if (!rport || !get_device(&rport->dev)) {
dea31012005-04-17 16:05:31 -05004025 dev_printk(KERN_WARNING, &phba->pcidev->dev,
4026 "Warning: fc_remote_port_add failed\n");
4027 return;
4028 }
4029
4030 /* initialize static port data */
4031 rport->maxframe_size = ndlp->nlp_maxframe;
4032 rport->supported_classes = ndlp->nlp_class_sup;
dea31012005-04-17 16:05:31 -05004033 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004034 rdata->pnode = lpfc_nlp_get(ndlp);
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004035
4036 if (ndlp->nlp_type & NLP_FCP_TARGET)
4037 rport_ids.roles |= FC_RPORT_ROLE_FCP_TARGET;
4038 if (ndlp->nlp_type & NLP_FCP_INITIATOR)
4039 rport_ids.roles |= FC_RPORT_ROLE_FCP_INITIATOR;
4040
James.Smart@Emulex.Com23dc04f2005-11-28 11:41:44 -05004041 if (rport_ids.roles != FC_RPORT_ROLE_UNKNOWN)
4042 fc_remote_port_rolechg(rport, rport_ids.roles);
4043
James Smart34f5ad82012-08-03 12:35:03 -04004044 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
4045 "3183 rport register x%06x, rport %p role x%x\n",
4046 ndlp->nlp_DID, rport, rport_ids.roles);
4047
James Smart071fbd3d2006-04-15 11:53:20 -04004048 if ((rport->scsi_target_id != -1) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004049 (rport->scsi_target_id < LPFC_MAX_TARGET)) {
James Smart071fbd3d2006-04-15 11:53:20 -04004050 ndlp->nlp_sid = rport->scsi_target_id;
4051 }
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004052 return;
4053}
4054
4055static void
James Smart2e0fef82007-06-17 19:56:36 -05004056lpfc_unregister_remote_port(struct lpfc_nodelist *ndlp)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004057{
4058 struct fc_rport *rport = ndlp->rport;
James Smart2ea259e2017-02-12 13:52:27 -08004059 struct lpfc_vport *vport = ndlp->vport;
James Smarta0f2d3e2017-02-12 13:52:31 -08004060 struct lpfc_hba *phba = vport->phba;
4061
4062 if (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)
4063 return;
James Smartc01f3202006-08-18 17:47:08 -04004064
James Smart2ea259e2017-02-12 13:52:27 -08004065 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_RPORT,
James Smart858c9f62007-06-17 19:56:39 -05004066 "rport delete: did:x%x flg:x%x type x%x",
4067 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
4068
James Smart2ea259e2017-02-12 13:52:27 -08004069 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
James Smart34f5ad82012-08-03 12:35:03 -04004070 "3184 rport unregister x%06x, rport %p\n",
4071 ndlp->nlp_DID, rport);
4072
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04004073 fc_remote_port_delete(rport);
dea31012005-04-17 16:05:31 -05004074
4075 return;
4076}
4077
James Smartde0c5b32007-04-25 09:52:27 -04004078static void
James Smart2e0fef82007-06-17 19:56:36 -05004079lpfc_nlp_counters(struct lpfc_vport *vport, int state, int count)
James Smartde0c5b32007-04-25 09:52:27 -04004080{
James Smart2e0fef82007-06-17 19:56:36 -05004081 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4082
4083 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004084 switch (state) {
4085 case NLP_STE_UNUSED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004086 vport->fc_unused_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004087 break;
4088 case NLP_STE_PLOGI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004089 vport->fc_plogi_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004090 break;
4091 case NLP_STE_ADISC_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004092 vport->fc_adisc_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004093 break;
4094 case NLP_STE_REG_LOGIN_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004095 vport->fc_reglogin_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004096 break;
4097 case NLP_STE_PRLI_ISSUE:
James Smart2e0fef82007-06-17 19:56:36 -05004098 vport->fc_prli_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004099 break;
4100 case NLP_STE_UNMAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004101 vport->fc_unmap_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004102 break;
4103 case NLP_STE_MAPPED_NODE:
James Smart2e0fef82007-06-17 19:56:36 -05004104 vport->fc_map_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004105 break;
4106 case NLP_STE_NPR_NODE:
James Smart646a2dd2013-12-17 20:29:22 -05004107 if (vport->fc_npr_cnt == 0 && count == -1)
4108 vport->fc_npr_cnt = 0;
4109 else
4110 vport->fc_npr_cnt += count;
James Smartde0c5b32007-04-25 09:52:27 -04004111 break;
4112 }
James Smart2e0fef82007-06-17 19:56:36 -05004113 spin_unlock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004114}
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05004115
James Smartde0c5b32007-04-25 09:52:27 -04004116static void
James Smart2e0fef82007-06-17 19:56:36 -05004117lpfc_nlp_state_cleanup(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
James Smartde0c5b32007-04-25 09:52:27 -04004118 int old_state, int new_state)
4119{
James Smart2e0fef82007-06-17 19:56:36 -05004120 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smarta0f2d3e2017-02-12 13:52:31 -08004121 struct lpfc_hba *phba = vport->phba;
James Smart2e0fef82007-06-17 19:56:36 -05004122
James Smartde0c5b32007-04-25 09:52:27 -04004123 if (new_state == NLP_STE_UNMAPPED_NODE) {
James Smartde0c5b32007-04-25 09:52:27 -04004124 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4125 ndlp->nlp_type |= NLP_FC_NODE;
4126 }
4127 if (new_state == NLP_STE_MAPPED_NODE)
4128 ndlp->nlp_flag &= ~NLP_NODEV_REMOVE;
4129 if (new_state == NLP_STE_NPR_NODE)
4130 ndlp->nlp_flag &= ~NLP_RCV_PLOGI;
dea31012005-04-17 16:05:31 -05004131
James Smarta0f2d3e2017-02-12 13:52:31 -08004132 /* FCP and NVME Transport interface */
4133 if ((old_state == NLP_STE_MAPPED_NODE ||
4134 old_state == NLP_STE_UNMAPPED_NODE)) {
4135 if (ndlp->rport) {
4136 vport->phba->nport_event_cnt++;
4137 lpfc_unregister_remote_port(ndlp);
4138 }
4139
4140 /* Notify the NVME transport of this rport's loss */
4141 if (((phba->cfg_enable_fc4_type == LPFC_ENABLE_BOTH) ||
4142 (phba->cfg_enable_fc4_type == LPFC_ENABLE_NVME)) &&
4143 (vport->phba->nvmet_support == 0) &&
4144 ((ndlp->nlp_fc4_type & NLP_FC4_NVME) ||
4145 (ndlp->nlp_DID == Fabric_DID))) {
4146 vport->phba->nport_event_cnt++;
4147 /* todo: init: unregister rport from nvme */
4148 }
James Smartde0c5b32007-04-25 09:52:27 -04004149 }
dea31012005-04-17 16:05:31 -05004150
James Smarta0f2d3e2017-02-12 13:52:31 -08004151 /* FCP and NVME Transport interfaces */
4152
James Smartde0c5b32007-04-25 09:52:27 -04004153 if (new_state == NLP_STE_MAPPED_NODE ||
4154 new_state == NLP_STE_UNMAPPED_NODE) {
James Smarta0f2d3e2017-02-12 13:52:31 -08004155 if ((ndlp->nlp_fc4_type & NLP_FC4_FCP) ||
4156 (ndlp->nlp_DID == Fabric_DID)) {
4157 vport->phba->nport_event_cnt++;
4158 /*
4159 * Tell the fc transport about the port, if we haven't
4160 * already. If we have, and it's a scsi entity, be
4161 */
4162 lpfc_register_remote_port(vport, ndlp);
4163 }
4164 /* Notify the NVME transport of this new rport. */
4165 if (ndlp->nlp_fc4_type & NLP_FC4_NVME) {
4166 if (vport->phba->nvmet_support == 0) {
4167 /* Register this rport with the transport.
4168 * Initiators take the NDLP ref count in
4169 * the register.
4170 */
4171 vport->phba->nport_event_cnt++;
4172 /* todo: init: register rport with nvme */
4173 }
4174 }
James Smartde0c5b32007-04-25 09:52:27 -04004175 }
James Smarta0f2d3e2017-02-12 13:52:31 -08004176
James Smartea2151b2008-09-07 11:52:10 -04004177 if ((new_state == NLP_STE_MAPPED_NODE) &&
4178 (vport->stat_data_enabled)) {
4179 /*
4180 * A new target is discovered, if there is no buffer for
4181 * statistical data collection allocate buffer.
4182 */
4183 ndlp->lat_data = kcalloc(LPFC_MAX_BUCKET_COUNT,
4184 sizeof(struct lpfc_scsicmd_bkt),
4185 GFP_KERNEL);
4186
4187 if (!ndlp->lat_data)
4188 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE,
4189 "0286 lpfc_nlp_state_cleanup failed to "
4190 "allocate statistical data buffer DID "
4191 "0x%x\n", ndlp->nlp_DID);
4192 }
James Smart858c9f62007-06-17 19:56:39 -05004193 /*
James Smarta0f2d3e2017-02-12 13:52:31 -08004194 * If the node just added to Mapped list was an FCP target,
4195 * but the remote port registration failed or assigned a target
4196 * id outside the presentable range - move the node to the
4197 * Unmapped List.
James Smart858c9f62007-06-17 19:56:39 -05004198 */
James Smarta0f2d3e2017-02-12 13:52:31 -08004199 if ((new_state == NLP_STE_MAPPED_NODE) &&
4200 (ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smartde0c5b32007-04-25 09:52:27 -04004201 (!ndlp->rport ||
4202 ndlp->rport->scsi_target_id == -1 ||
4203 ndlp->rport->scsi_target_id >= LPFC_MAX_TARGET)) {
James Smart2e0fef82007-06-17 19:56:36 -05004204 spin_lock_irq(shost->host_lock);
James Smartde0c5b32007-04-25 09:52:27 -04004205 ndlp->nlp_flag |= NLP_TGT_NO_SCSIID;
James Smart2e0fef82007-06-17 19:56:36 -05004206 spin_unlock_irq(shost->host_lock);
4207 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
dea31012005-04-17 16:05:31 -05004208 }
James Smartde0c5b32007-04-25 09:52:27 -04004209}
4210
James Smart685f0bf2007-04-25 09:53:08 -04004211static char *
4212lpfc_nlp_state_name(char *buffer, size_t size, int state)
4213{
4214 static char *states[] = {
4215 [NLP_STE_UNUSED_NODE] = "UNUSED",
4216 [NLP_STE_PLOGI_ISSUE] = "PLOGI",
4217 [NLP_STE_ADISC_ISSUE] = "ADISC",
4218 [NLP_STE_REG_LOGIN_ISSUE] = "REGLOGIN",
4219 [NLP_STE_PRLI_ISSUE] = "PRLI",
James Smart086a3452012-08-14 14:25:21 -04004220 [NLP_STE_LOGO_ISSUE] = "LOGO",
James Smart685f0bf2007-04-25 09:53:08 -04004221 [NLP_STE_UNMAPPED_NODE] = "UNMAPPED",
4222 [NLP_STE_MAPPED_NODE] = "MAPPED",
4223 [NLP_STE_NPR_NODE] = "NPR",
4224 };
4225
James Smart311464e2007-08-02 11:10:37 -04004226 if (state < NLP_STE_MAX_STATE && states[state])
James Smart685f0bf2007-04-25 09:53:08 -04004227 strlcpy(buffer, states[state], size);
4228 else
4229 snprintf(buffer, size, "unknown (%d)", state);
4230 return buffer;
4231}
4232
James Smartde0c5b32007-04-25 09:52:27 -04004233void
James Smart2e0fef82007-06-17 19:56:36 -05004234lpfc_nlp_set_state(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4235 int state)
James Smartde0c5b32007-04-25 09:52:27 -04004236{
James Smart2e0fef82007-06-17 19:56:36 -05004237 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smartde0c5b32007-04-25 09:52:27 -04004238 int old_state = ndlp->nlp_state;
James Smart685f0bf2007-04-25 09:53:08 -04004239 char name1[16], name2[16];
James Smartde0c5b32007-04-25 09:52:27 -04004240
James Smarte8b62012007-08-02 11:10:09 -04004241 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4242 "0904 NPort state transition x%06x, %s -> %s\n",
4243 ndlp->nlp_DID,
4244 lpfc_nlp_state_name(name1, sizeof(name1), old_state),
4245 lpfc_nlp_state_name(name2, sizeof(name2), state));
James Smart858c9f62007-06-17 19:56:39 -05004246
4247 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4248 "node statechg did:x%x old:%d ste:%d",
4249 ndlp->nlp_DID, old_state, state);
4250
James Smartde0c5b32007-04-25 09:52:27 -04004251 if (old_state == NLP_STE_NPR_NODE &&
James Smartde0c5b32007-04-25 09:52:27 -04004252 state != NLP_STE_NPR_NODE)
James Smart2e0fef82007-06-17 19:56:36 -05004253 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004254 if (old_state == NLP_STE_UNMAPPED_NODE) {
4255 ndlp->nlp_flag &= ~NLP_TGT_NO_SCSIID;
4256 ndlp->nlp_type &= ~NLP_FC_NODE;
4257 }
4258
James Smart685f0bf2007-04-25 09:53:08 -04004259 if (list_empty(&ndlp->nlp_listp)) {
James Smart2e0fef82007-06-17 19:56:36 -05004260 spin_lock_irq(shost->host_lock);
4261 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4262 spin_unlock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004263 } else if (old_state)
James Smart2e0fef82007-06-17 19:56:36 -05004264 lpfc_nlp_counters(vport, old_state, -1);
James Smartde0c5b32007-04-25 09:52:27 -04004265
4266 ndlp->nlp_state = state;
James Smart2e0fef82007-06-17 19:56:36 -05004267 lpfc_nlp_counters(vport, state, 1);
4268 lpfc_nlp_state_cleanup(vport, ndlp, old_state, state);
James Smartde0c5b32007-04-25 09:52:27 -04004269}
4270
4271void
James Smarte47c9092008-02-08 18:49:26 -05004272lpfc_enqueue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4273{
4274 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4275
4276 if (list_empty(&ndlp->nlp_listp)) {
4277 spin_lock_irq(shost->host_lock);
4278 list_add_tail(&ndlp->nlp_listp, &vport->fc_nodes);
4279 spin_unlock_irq(shost->host_lock);
4280 }
4281}
4282
4283void
James Smart2e0fef82007-06-17 19:56:36 -05004284lpfc_dequeue_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004285{
James Smart2e0fef82007-06-17 19:56:36 -05004286 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4287
James Smart0d2b6b82008-06-14 22:52:47 -04004288 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smartde0c5b32007-04-25 09:52:27 -04004289 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
James Smart2e0fef82007-06-17 19:56:36 -05004290 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4291 spin_lock_irq(shost->host_lock);
James Smart685f0bf2007-04-25 09:53:08 -04004292 list_del_init(&ndlp->nlp_listp);
James Smart2e0fef82007-06-17 19:56:36 -05004293 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05004294 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
James Smarte47c9092008-02-08 18:49:26 -05004295 NLP_STE_UNUSED_NODE);
4296}
4297
Adrian Bunk4d9db012008-02-14 23:24:02 +02004298static void
James Smarte47c9092008-02-08 18:49:26 -05004299lpfc_disable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
4300{
James Smart0d2b6b82008-06-14 22:52:47 -04004301 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05004302 if (ndlp->nlp_state && !list_empty(&ndlp->nlp_listp))
4303 lpfc_nlp_counters(vport, ndlp->nlp_state, -1);
4304 lpfc_nlp_state_cleanup(vport, ndlp, ndlp->nlp_state,
4305 NLP_STE_UNUSED_NODE);
4306}
James Smart109f6ed2008-12-04 22:39:08 -05004307/**
James Smart3621a712009-04-06 18:47:14 -04004308 * lpfc_initialize_node - Initialize all fields of node object
James Smart109f6ed2008-12-04 22:39:08 -05004309 * @vport: Pointer to Virtual Port object.
4310 * @ndlp: Pointer to FC node object.
4311 * @did: FC_ID of the node.
James Smarta257bf92009-04-06 18:48:10 -04004312 *
4313 * This function is always called when node object need to be initialized.
4314 * It initializes all the fields of the node object. Although the reference
4315 * to phba from @ndlp can be obtained indirectly through it's reference to
4316 * @vport, a direct reference to phba is taken here by @ndlp. This is due
4317 * to the life-span of the @ndlp might go beyond the existence of @vport as
4318 * the final release of ndlp is determined by its reference count. And, the
4319 * operation on @ndlp needs the reference to phba.
James Smart109f6ed2008-12-04 22:39:08 -05004320 **/
4321static inline void
4322lpfc_initialize_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4323 uint32_t did)
4324{
4325 INIT_LIST_HEAD(&ndlp->els_retry_evt.evt_listp);
4326 INIT_LIST_HEAD(&ndlp->dev_loss_evt.evt_listp);
4327 init_timer(&ndlp->nlp_delayfunc);
4328 ndlp->nlp_delayfunc.function = lpfc_els_retry_delay;
4329 ndlp->nlp_delayfunc.data = (unsigned long)ndlp;
4330 ndlp->nlp_DID = did;
4331 ndlp->vport = vport;
James Smarta257bf92009-04-06 18:48:10 -04004332 ndlp->phba = vport->phba;
James Smart109f6ed2008-12-04 22:39:08 -05004333 ndlp->nlp_sid = NLP_NO_SID;
James Smarta0f2d3e2017-02-12 13:52:31 -08004334 ndlp->nlp_fc4_type = NLP_FC4_NONE;
James Smart109f6ed2008-12-04 22:39:08 -05004335 kref_init(&ndlp->kref);
4336 NLP_INT_NODE_ACT(ndlp);
4337 atomic_set(&ndlp->cmd_pending, 0);
James Smart7dc517d2010-07-14 15:32:10 -04004338 ndlp->cmd_qdepth = vport->cfg_tgt_queue_depth;
James Smart109f6ed2008-12-04 22:39:08 -05004339}
James Smarte47c9092008-02-08 18:49:26 -05004340
4341struct lpfc_nodelist *
4342lpfc_enable_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4343 int state)
4344{
4345 struct lpfc_hba *phba = vport->phba;
4346 uint32_t did;
4347 unsigned long flags;
James Smartcff261f2013-12-17 20:29:47 -05004348 unsigned long *active_rrqs_xri_bitmap = NULL;
James Smarte47c9092008-02-08 18:49:26 -05004349
4350 if (!ndlp)
4351 return NULL;
4352
4353 spin_lock_irqsave(&phba->ndlp_lock, flags);
4354 /* The ndlp should not be in memory free mode */
4355 if (NLP_CHK_FREE_REQ(ndlp)) {
4356 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4357 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4358 "0277 lpfc_enable_node: ndlp:x%p "
4359 "usgmap:x%x refcnt:%d\n",
4360 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004361 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004362 return NULL;
4363 }
4364 /* The ndlp should not already be in active mode */
4365 if (NLP_CHK_NODE_ACT(ndlp)) {
4366 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
4367 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4368 "0278 lpfc_enable_node: ndlp:x%p "
4369 "usgmap:x%x refcnt:%d\n",
4370 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004371 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004372 return NULL;
4373 }
4374
4375 /* Keep the original DID */
4376 did = ndlp->nlp_DID;
James Smartcff261f2013-12-17 20:29:47 -05004377 if (phba->sli_rev == LPFC_SLI_REV4)
4378 active_rrqs_xri_bitmap = ndlp->active_rrqs_xri_bitmap;
James Smarte47c9092008-02-08 18:49:26 -05004379
4380 /* re-initialize ndlp except of ndlp linked list pointer */
4381 memset((((char *)ndlp) + sizeof (struct list_head)), 0,
4382 sizeof (struct lpfc_nodelist) - sizeof (struct list_head));
James Smart109f6ed2008-12-04 22:39:08 -05004383 lpfc_initialize_node(vport, ndlp, did);
James Smarte47c9092008-02-08 18:49:26 -05004384
James Smartcff261f2013-12-17 20:29:47 -05004385 if (phba->sli_rev == LPFC_SLI_REV4)
4386 ndlp->active_rrqs_xri_bitmap = active_rrqs_xri_bitmap;
4387
James Smarte47c9092008-02-08 18:49:26 -05004388 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smartbe6bb942015-04-07 15:07:22 -04004389 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04004390 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04004391 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4392 "0008 rpi:%x DID:%x flg:%x refcnt:%d "
4393 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
4394 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004395 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004396 ndlp->nlp_usg_map, ndlp);
4397 }
James Smart725dd392013-10-10 12:23:30 -04004398
James Smarte47c9092008-02-08 18:49:26 -05004399
4400 if (state != NLP_STE_UNUSED_NODE)
4401 lpfc_nlp_set_state(vport, ndlp, state);
4402
4403 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
4404 "node enable: did:x%x",
4405 ndlp->nlp_DID, 0, 0);
4406 return ndlp;
James Smartde0c5b32007-04-25 09:52:27 -04004407}
4408
4409void
James Smart2e0fef82007-06-17 19:56:36 -05004410lpfc_drop_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
James Smartde0c5b32007-04-25 09:52:27 -04004411{
James Smart87af33f2007-10-27 13:37:43 -04004412 /*
James Smartfa4066b2008-01-11 01:53:27 -05004413 * Use of lpfc_drop_node and UNUSED list: lpfc_drop_node should
James Smart87af33f2007-10-27 13:37:43 -04004414 * be used if we wish to issue the "last" lpfc_nlp_put() to remove
James Smartfa4066b2008-01-11 01:53:27 -05004415 * the ndlp from the vport. The ndlp marked as UNUSED on the list
4416 * until ALL other outstanding threads have completed. We check
4417 * that the ndlp not already in the UNUSED state before we proceed.
James Smart87af33f2007-10-27 13:37:43 -04004418 */
James Smartfa4066b2008-01-11 01:53:27 -05004419 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
4420 return;
James Smart51ef4c22007-08-02 11:10:31 -04004421 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNUSED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04004422 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart1151e3e2011-02-16 12:39:35 -05004423 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004424 lpfc_unreg_rpi(vport, ndlp);
James Smartbe6bb942015-04-07 15:07:22 -04004425 }
James Smart02902172015-05-21 13:55:17 -04004426
4427 lpfc_nlp_put(ndlp);
James Smart98c9ea52007-10-27 13:37:33 -04004428 return;
dea31012005-04-17 16:05:31 -05004429}
4430
4431/*
4432 * Start / ReStart rescue timer for Discovery / RSCN handling
4433 */
4434void
James Smart2e0fef82007-06-17 19:56:36 -05004435lpfc_set_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004436{
James Smart2e0fef82007-06-17 19:56:36 -05004437 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4438 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05004439 uint32_t tmo;
4440
James Smart2e0fef82007-06-17 19:56:36 -05004441 if (vport->port_state == LPFC_LOCAL_CFG_LINK) {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004442 /* For FAN, timeout should be greater than edtov */
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004443 tmo = (((phba->fc_edtov + 999) / 1000) + 1);
4444 } else {
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004445 /* Normal discovery timeout should be > than ELS/CT timeout
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05004446 * FC spec states we need 3 * ratov for CT requests
4447 */
4448 tmo = ((phba->fc_ratov * 3) + 3);
4449 }
dea31012005-04-17 16:05:31 -05004450
James Smart858c9f62007-06-17 19:56:39 -05004451
4452 if (!timer_pending(&vport->fc_disctmo)) {
4453 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4454 "set disc timer: tmo:x%x state:x%x flg:x%x",
4455 tmo, vport->port_state, vport->fc_flag);
4456 }
4457
James Smart256ec0d2013-04-17 20:14:58 -04004458 mod_timer(&vport->fc_disctmo, jiffies + msecs_to_jiffies(1000 * tmo));
James Smart2e0fef82007-06-17 19:56:36 -05004459 spin_lock_irq(shost->host_lock);
4460 vport->fc_flag |= FC_DISC_TMO;
4461 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004462
4463 /* Start Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004464 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4465 "0247 Start Discovery Timer state x%x "
4466 "Data: x%x x%lx x%x x%x\n",
4467 vport->port_state, tmo,
4468 (unsigned long)&vport->fc_disctmo, vport->fc_plogi_cnt,
4469 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05004470
4471 return;
4472}
4473
4474/*
4475 * Cancel rescue timer for Discovery / RSCN handling
4476 */
4477int
James Smart2e0fef82007-06-17 19:56:36 -05004478lpfc_can_disctmo(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05004479{
James Smart2e0fef82007-06-17 19:56:36 -05004480 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart2e0fef82007-06-17 19:56:36 -05004481 unsigned long iflags;
4482
James Smart858c9f62007-06-17 19:56:39 -05004483 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
4484 "can disc timer: state:x%x rtry:x%x flg:x%x",
4485 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
4486
dea31012005-04-17 16:05:31 -05004487 /* Turn off discovery timer if its running */
James Smart2e0fef82007-06-17 19:56:36 -05004488 if (vport->fc_flag & FC_DISC_TMO) {
4489 spin_lock_irqsave(shost->host_lock, iflags);
4490 vport->fc_flag &= ~FC_DISC_TMO;
4491 spin_unlock_irqrestore(shost->host_lock, iflags);
4492 del_timer_sync(&vport->fc_disctmo);
4493 spin_lock_irqsave(&vport->work_port_lock, iflags);
4494 vport->work_port_events &= ~WORKER_DISC_TMO;
4495 spin_unlock_irqrestore(&vport->work_port_lock, iflags);
dea31012005-04-17 16:05:31 -05004496 }
4497
4498 /* Cancel Discovery Timer state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04004499 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
4500 "0248 Cancel Discovery Timer state x%x "
4501 "Data: x%x x%x x%x\n",
4502 vport->port_state, vport->fc_flag,
4503 vport->fc_plogi_cnt, vport->fc_adisc_cnt);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004504 return 0;
dea31012005-04-17 16:05:31 -05004505}
4506
4507/*
4508 * Check specified ring for outstanding IOCB on the SLI queue
4509 * Return true if iocb matches the specified nport
4510 */
4511int
James Smart2e0fef82007-06-17 19:56:36 -05004512lpfc_check_sli_ndlp(struct lpfc_hba *phba,
4513 struct lpfc_sli_ring *pring,
4514 struct lpfc_iocbq *iocb,
4515 struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004516{
James Smart2e0fef82007-06-17 19:56:36 -05004517 IOCB_t *icmd = &iocb->iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05004518 struct lpfc_vport *vport = ndlp->vport;
4519
4520 if (iocb->vport != vport)
4521 return 0;
4522
dea31012005-04-17 16:05:31 -05004523 if (pring->ringno == LPFC_ELS_RING) {
4524 switch (icmd->ulpCommand) {
4525 case CMD_GEN_REQUEST64_CR:
James Smart21e9a0a2009-05-22 14:53:21 -04004526 if (iocb->context_un.ndlp == ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004527 return 1;
dea31012005-04-17 16:05:31 -05004528 case CMD_ELS_REQUEST64_CR:
James Smart10d4e952006-04-15 11:53:15 -04004529 if (icmd->un.elsreq64.remoteID == ndlp->nlp_DID)
4530 return 1;
dea31012005-04-17 16:05:31 -05004531 case CMD_XMIT_ELS_RSP64_CX:
4532 if (iocb->context1 == (uint8_t *) ndlp)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004533 return 1;
dea31012005-04-17 16:05:31 -05004534 }
James Smart895427b2017-02-12 13:52:30 -08004535 } else if (pring->ringno == LPFC_FCP_RING) {
dea31012005-04-17 16:05:31 -05004536 /* Skip match check if waiting to relogin to FCP target */
4537 if ((ndlp->nlp_type & NLP_FCP_TARGET) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004538 (ndlp->nlp_flag & NLP_DELAY_TMO)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004539 return 0;
dea31012005-04-17 16:05:31 -05004540 }
4541 if (icmd->ulpContext == (volatile ushort)ndlp->nlp_rpi) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004542 return 1;
dea31012005-04-17 16:05:31 -05004543 }
dea31012005-04-17 16:05:31 -05004544 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004545 return 0;
dea31012005-04-17 16:05:31 -05004546}
4547
James Smart895427b2017-02-12 13:52:30 -08004548static void
4549__lpfc_dequeue_nport_iocbs(struct lpfc_hba *phba,
4550 struct lpfc_nodelist *ndlp, struct lpfc_sli_ring *pring,
4551 struct list_head *dequeue_list)
4552{
4553 struct lpfc_iocbq *iocb, *next_iocb;
4554
4555 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4556 /* Check to see if iocb matches the nport */
4557 if (lpfc_check_sli_ndlp(phba, pring, iocb, ndlp))
4558 /* match, dequeue */
4559 list_move_tail(&iocb->list, dequeue_list);
4560 }
4561}
4562
4563static void
4564lpfc_sli3_dequeue_nport_iocbs(struct lpfc_hba *phba,
4565 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4566{
4567 struct lpfc_sli *psli = &phba->sli;
4568 uint32_t i;
4569
4570 spin_lock_irq(&phba->hbalock);
4571 for (i = 0; i < psli->num_rings; i++)
4572 __lpfc_dequeue_nport_iocbs(phba, ndlp, &psli->sli3_ring[i],
4573 dequeue_list);
4574 spin_unlock_irq(&phba->hbalock);
4575}
4576
4577static void
4578lpfc_sli4_dequeue_nport_iocbs(struct lpfc_hba *phba,
4579 struct lpfc_nodelist *ndlp, struct list_head *dequeue_list)
4580{
4581 struct lpfc_sli_ring *pring;
4582 struct lpfc_queue *qp = NULL;
4583
4584 spin_lock_irq(&phba->hbalock);
4585 list_for_each_entry(qp, &phba->sli4_hba.lpfc_wq_list, wq_list) {
4586 pring = qp->pring;
4587 if (!pring)
4588 continue;
4589 spin_lock_irq(&pring->ring_lock);
4590 __lpfc_dequeue_nport_iocbs(phba, ndlp, pring, dequeue_list);
4591 spin_unlock_irq(&pring->ring_lock);
4592 }
4593 spin_unlock_irq(&phba->hbalock);
4594}
4595
dea31012005-04-17 16:05:31 -05004596/*
4597 * Free resources / clean up outstanding I/Os
4598 * associated with nlp_rpi in the LPFC_NODELIST entry.
4599 */
4600static int
James Smart2e0fef82007-06-17 19:56:36 -05004601lpfc_no_rpi(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004602{
James Smart2534ba72007-04-25 09:52:20 -04004603 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05004604
James Smart92d7f7b2007-06-17 19:56:38 -05004605 lpfc_fabric_abort_nport(ndlp);
4606
dea31012005-04-17 16:05:31 -05004607 /*
4608 * Everything that matches on txcmplq will be returned
4609 * by firmware with a no rpi error.
4610 */
James Smart40426292010-12-15 17:58:10 -05004611 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart895427b2017-02-12 13:52:30 -08004612 if (phba->sli_rev != LPFC_SLI_REV4)
4613 lpfc_sli3_dequeue_nport_iocbs(phba, ndlp, &completions);
4614 else
4615 lpfc_sli4_dequeue_nport_iocbs(phba, ndlp, &completions);
dea31012005-04-17 16:05:31 -05004616 }
James Smart2534ba72007-04-25 09:52:20 -04004617
James Smarta257bf92009-04-06 18:48:10 -04004618 /* Cancel all the IOCBs from the completions list */
4619 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
4620 IOERR_SLI_ABORTED);
James Smart2534ba72007-04-25 09:52:20 -04004621
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004622 return 0;
dea31012005-04-17 16:05:31 -05004623}
4624
James Smart086a3452012-08-14 14:25:21 -04004625/**
4626 * lpfc_nlp_logo_unreg - Unreg mailbox completion handler before LOGO
4627 * @phba: Pointer to HBA context object.
4628 * @pmb: Pointer to mailbox object.
4629 *
4630 * This function will issue an ELS LOGO command after completing
4631 * the UNREG_RPI.
4632 **/
Rashika Kheriab86a6752014-09-03 12:55:17 -04004633static void
James Smart086a3452012-08-14 14:25:21 -04004634lpfc_nlp_logo_unreg(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
4635{
4636 struct lpfc_vport *vport = pmb->vport;
4637 struct lpfc_nodelist *ndlp;
4638
4639 ndlp = (struct lpfc_nodelist *)(pmb->context1);
4640 if (!ndlp)
4641 return;
4642 lpfc_issue_els_logo(vport, ndlp, 0);
James Smart4aa74c32013-09-06 12:19:06 -04004643 mempool_free(pmb, phba->mbox_mem_pool);
James Smart086a3452012-08-14 14:25:21 -04004644}
4645
dea31012005-04-17 16:05:31 -05004646/*
4647 * Free rpi associated with LPFC_NODELIST entry.
4648 * This routine is called from lpfc_freenode(), when we are removing
4649 * a LPFC_NODELIST entry. It is also called if the driver initiates a
4650 * LOGO that completes successfully, and we are waiting to PLOGI back
4651 * to the remote NPort. In addition, it is called after we receive
4652 * and unsolicated ELS cmd, send back a rsp, the rsp completes and
4653 * we are waiting to PLOGI back to the remote NPort.
4654 */
4655int
James Smart2e0fef82007-06-17 19:56:36 -05004656lpfc_unreg_rpi(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004657{
James Smart2e0fef82007-06-17 19:56:36 -05004658 struct lpfc_hba *phba = vport->phba;
4659 LPFC_MBOXQ_t *mbox;
James Smart7c5e5182015-05-22 10:42:43 -04004660 int rc, acc_plogi = 1;
James Smart6d368e52011-05-24 11:44:12 -04004661 uint16_t rpi;
dea31012005-04-17 16:05:31 -05004662
James Smartf454a9a2013-09-06 12:20:07 -04004663 if (ndlp->nlp_flag & NLP_RPI_REGISTERED ||
4664 ndlp->nlp_flag & NLP_REG_LOGIN_SEND) {
4665 if (ndlp->nlp_flag & NLP_REG_LOGIN_SEND)
4666 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4667 "3366 RPI x%x needs to be "
4668 "unregistered nlp_flag x%x "
4669 "did x%x\n",
4670 ndlp->nlp_rpi, ndlp->nlp_flag,
4671 ndlp->nlp_DID);
James Smart2e0fef82007-06-17 19:56:36 -05004672 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4673 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004674 /* SLI4 ports require the physical rpi value. */
4675 rpi = ndlp->nlp_rpi;
4676 if (phba->sli_rev == LPFC_SLI_REV4)
4677 rpi = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
James Smart086a3452012-08-14 14:25:21 -04004678
James Smart6d368e52011-05-24 11:44:12 -04004679 lpfc_unreg_login(phba, vport->vpi, rpi, mbox);
James Smarted957682007-06-17 19:56:37 -05004680 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004681 if (ndlp->nlp_flag & NLP_ISSUE_LOGO) {
4682 mbox->context1 = ndlp;
4683 mbox->mbox_cmpl = lpfc_nlp_logo_unreg;
4684 } else {
James Smartbe6bb942015-04-07 15:07:22 -04004685 if (phba->sli_rev == LPFC_SLI_REV4 &&
4686 (!(vport->load_flag & FC_UNLOADING)) &&
4687 (bf_get(lpfc_sli_intf_if_type,
4688 &phba->sli4_hba.sli_intf) ==
James Smarta6517db2016-03-31 14:12:29 -07004689 LPFC_SLI_INTF_IF_TYPE_2) &&
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004690 (kref_read(&ndlp->kref) > 0)) {
James Smartbe6bb942015-04-07 15:07:22 -04004691 mbox->context1 = lpfc_nlp_get(ndlp);
4692 mbox->mbox_cmpl =
4693 lpfc_sli4_unreg_rpi_cmpl_clr;
James Smart7c5e5182015-05-22 10:42:43 -04004694 /*
4695 * accept PLOGIs after unreg_rpi_cmpl
4696 */
4697 acc_plogi = 0;
James Smartbe6bb942015-04-07 15:07:22 -04004698 } else
4699 mbox->mbox_cmpl =
4700 lpfc_sli_def_mbox_cmpl;
James Smart086a3452012-08-14 14:25:21 -04004701 }
4702
James Smart0b727fe2007-10-27 13:37:25 -04004703 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart7c5e5182015-05-22 10:42:43 -04004704 if (rc == MBX_NOT_FINISHED) {
James Smart2e0fef82007-06-17 19:56:36 -05004705 mempool_free(mbox, phba->mbox_mem_pool);
James Smart7c5e5182015-05-22 10:42:43 -04004706 acc_plogi = 1;
4707 }
dea31012005-04-17 16:05:31 -05004708 }
dea31012005-04-17 16:05:31 -05004709 lpfc_no_rpi(phba, ndlp);
James Smartd7c47992010-06-08 18:31:54 -04004710
James Smart40426292010-12-15 17:58:10 -05004711 if (phba->sli_rev != LPFC_SLI_REV4)
4712 ndlp->nlp_rpi = 0;
4713 ndlp->nlp_flag &= ~NLP_RPI_REGISTERED;
James Smart0c287582009-06-10 17:22:56 -04004714 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
James Smart7c5e5182015-05-22 10:42:43 -04004715 if (acc_plogi)
4716 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004717 return 1;
4718 }
James Smart7c5e5182015-05-22 10:42:43 -04004719 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
dea31012005-04-17 16:05:31 -05004720 return 0;
4721}
4722
James Smartecfd03c2010-02-12 14:41:27 -05004723/**
4724 * lpfc_unreg_hba_rpis - Unregister rpis registered to the hba.
4725 * @phba: pointer to lpfc hba data structure.
4726 *
4727 * This routine is invoked to unregister all the currently registered RPIs
4728 * to the HBA.
4729 **/
4730void
4731lpfc_unreg_hba_rpis(struct lpfc_hba *phba)
4732{
4733 struct lpfc_vport **vports;
4734 struct lpfc_nodelist *ndlp;
4735 struct Scsi_Host *shost;
4736 int i;
4737
4738 vports = lpfc_create_vport_work_array(phba);
James Smart63e801c2010-11-20 23:14:19 -05004739 if (!vports) {
4740 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
4741 "2884 Vport array allocation failed \n");
4742 return;
4743 }
James Smartecfd03c2010-02-12 14:41:27 -05004744 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
4745 shost = lpfc_shost_from_vport(vports[i]);
4746 spin_lock_irq(shost->host_lock);
4747 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
James Smart40426292010-12-15 17:58:10 -05004748 if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
James Smart0c9ab6f2010-02-26 14:15:57 -05004749 /* The mempool_alloc might sleep */
4750 spin_unlock_irq(shost->host_lock);
James Smartecfd03c2010-02-12 14:41:27 -05004751 lpfc_unreg_rpi(vports[i], ndlp);
James Smart0c9ab6f2010-02-26 14:15:57 -05004752 spin_lock_irq(shost->host_lock);
4753 }
James Smartecfd03c2010-02-12 14:41:27 -05004754 }
4755 spin_unlock_irq(shost->host_lock);
4756 }
4757 lpfc_destroy_vport_work_array(phba, vports);
4758}
4759
James Smart92d7f7b2007-06-17 19:56:38 -05004760void
4761lpfc_unreg_all_rpis(struct lpfc_vport *vport)
4762{
4763 struct lpfc_hba *phba = vport->phba;
4764 LPFC_MBOXQ_t *mbox;
4765 int rc;
4766
James Smart5af5eee2010-10-22 11:06:38 -04004767 if (phba->sli_rev == LPFC_SLI_REV4) {
4768 lpfc_sli4_unreg_all_rpis(vport);
4769 return;
4770 }
4771
James Smart92d7f7b2007-06-17 19:56:38 -05004772 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4773 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004774 lpfc_unreg_login(phba, vport->vpi, LPFC_UNREG_ALL_RPIS_VPORT,
4775 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004776 mbox->vport = vport;
4777 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004778 mbox->context1 = NULL;
4779 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004780 if (rc != MBX_TIMEOUT)
James Smart92d7f7b2007-06-17 19:56:38 -05004781 mempool_free(mbox, phba->mbox_mem_pool);
James Smarta257bf92009-04-06 18:48:10 -04004782
4783 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
4784 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4785 "1836 Could not issue "
4786 "unreg_login(all_rpis) status %d\n", rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004787 }
4788}
4789
4790void
4791lpfc_unreg_default_rpis(struct lpfc_vport *vport)
4792{
4793 struct lpfc_hba *phba = vport->phba;
4794 LPFC_MBOXQ_t *mbox;
4795 int rc;
4796
4797 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4798 if (mbox) {
James Smart6d368e52011-05-24 11:44:12 -04004799 lpfc_unreg_did(phba, vport->vpi, LPFC_UNREG_ALL_DFLT_RPIS,
4800 mbox);
James Smart92d7f7b2007-06-17 19:56:38 -05004801 mbox->vport = vport;
4802 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart09372822008-01-11 01:52:54 -05004803 mbox->context1 = NULL;
4804 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
James Smarta257bf92009-04-06 18:48:10 -04004805 if (rc != MBX_TIMEOUT)
4806 mempool_free(mbox, phba->mbox_mem_pool);
4807
4808 if ((rc == MBX_TIMEOUT) || (rc == MBX_NOT_FINISHED))
James Smarte8b62012007-08-02 11:10:09 -04004809 lpfc_printf_vlog(vport, KERN_ERR, LOG_MBOX | LOG_VPORT,
4810 "1815 Could not issue "
James Smarta257bf92009-04-06 18:48:10 -04004811 "unreg_did (default rpis) status %d\n",
4812 rc);
James Smart92d7f7b2007-06-17 19:56:38 -05004813 }
4814}
4815
dea31012005-04-17 16:05:31 -05004816/*
4817 * Free resources associated with LPFC_NODELIST entry
4818 * so it can be freed.
4819 */
4820static int
James Smart2e0fef82007-06-17 19:56:36 -05004821lpfc_cleanup_node(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004822{
James Smart2e0fef82007-06-17 19:56:36 -05004823 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
4824 struct lpfc_hba *phba = vport->phba;
4825 LPFC_MBOXQ_t *mb, *nextmb;
dea31012005-04-17 16:05:31 -05004826 struct lpfc_dmabuf *mp;
dea31012005-04-17 16:05:31 -05004827
4828 /* Cleanup node for NPort <nlp_DID> */
James Smarte8b62012007-08-02 11:10:09 -04004829 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4830 "0900 Cleanup node for NPort x%x "
4831 "Data: x%x x%x x%x\n",
4832 ndlp->nlp_DID, ndlp->nlp_flag,
4833 ndlp->nlp_state, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05004834 if (NLP_CHK_FREE_REQ(ndlp)) {
4835 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4836 "0280 lpfc_cleanup_node: ndlp:x%p "
4837 "usgmap:x%x refcnt:%d\n",
4838 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004839 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004840 lpfc_dequeue_node(vport, ndlp);
4841 } else {
4842 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4843 "0281 lpfc_cleanup_node: ndlp:x%p "
4844 "usgmap:x%x refcnt:%d\n",
4845 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004846 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05004847 lpfc_disable_node(vport, ndlp);
4848 }
dea31012005-04-17 16:05:31 -05004849
James Smart086a3452012-08-14 14:25:21 -04004850
4851 /* Don't need to clean up REG_LOGIN64 cmds for Default RPI cleanup */
4852
dea31012005-04-17 16:05:31 -05004853 /* cleanup any ndlp on mbox q waiting for reglogin cmpl */
4854 if ((mb = phba->sli.mbox_active)) {
James Smart04c68492009-05-22 14:52:52 -04004855 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004856 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
dea31012005-04-17 16:05:31 -05004857 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
4858 mb->context2 = NULL;
4859 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4860 }
4861 }
James Smart33ccf8d2006-08-17 11:57:58 -04004862
James Smart2e0fef82007-06-17 19:56:36 -05004863 spin_lock_irq(&phba->hbalock);
James Smart5ac6b302010-10-22 11:05:36 -04004864 /* Cleanup REG_LOGIN completions which are not yet processed */
4865 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
4866 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) ||
James Smart086a3452012-08-14 14:25:21 -04004867 (mb->mbox_flag & LPFC_MBX_IMED_UNREG) ||
James Smart5ac6b302010-10-22 11:05:36 -04004868 (ndlp != (struct lpfc_nodelist *) mb->context2))
4869 continue;
4870
4871 mb->context2 = NULL;
4872 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4873 }
4874
dea31012005-04-17 16:05:31 -05004875 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
James Smart04c68492009-05-22 14:52:52 -04004876 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) &&
James Smart086a3452012-08-14 14:25:21 -04004877 !(mb->mbox_flag & LPFC_MBX_IMED_UNREG) &&
James Smart92d7f7b2007-06-17 19:56:38 -05004878 (ndlp == (struct lpfc_nodelist *) mb->context2)) {
dea31012005-04-17 16:05:31 -05004879 mp = (struct lpfc_dmabuf *) (mb->context1);
4880 if (mp) {
James Smart2e0fef82007-06-17 19:56:36 -05004881 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -05004882 kfree(mp);
4883 }
4884 list_del(&mb->list);
4885 mempool_free(mb, phba->mbox_mem_pool);
James Smarte47c9092008-02-08 18:49:26 -05004886 /* We shall not invoke the lpfc_nlp_put to decrement
4887 * the ndlp reference count as we are in the process
4888 * of lpfc_nlp_release.
4889 */
dea31012005-04-17 16:05:31 -05004890 }
4891 }
James Smart2e0fef82007-06-17 19:56:36 -05004892 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004893
James Smarte47c9092008-02-08 18:49:26 -05004894 lpfc_els_abort(phba, ndlp);
4895
James Smart2e0fef82007-06-17 19:56:36 -05004896 spin_lock_irq(shost->host_lock);
James Smartc01f3202006-08-18 17:47:08 -04004897 ndlp->nlp_flag &= ~NLP_DELAY_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004898 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05004899
Jamie Wellnitz5024ab12006-02-28 19:25:28 -05004900 ndlp->nlp_last_elscmd = 0;
dea31012005-04-17 16:05:31 -05004901 del_timer_sync(&ndlp->nlp_delayfunc);
4902
James Smart0d2b6b82008-06-14 22:52:47 -04004903 list_del_init(&ndlp->els_retry_evt.evt_listp);
4904 list_del_init(&ndlp->dev_loss_evt.evt_listp);
James Smart1151e3e2011-02-16 12:39:35 -05004905 lpfc_cleanup_vports_rrqs(vport, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05004906 lpfc_unreg_rpi(vport, ndlp);
dea31012005-04-17 16:05:31 -05004907
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004908 return 0;
dea31012005-04-17 16:05:31 -05004909}
4910
4911/*
4912 * Check to see if we can free the nlp back to the freelist.
4913 * If we are in the middle of using the nlp in the discovery state
4914 * machine, defer the free till we reach the end of the state machine.
4915 */
James Smart329f9bc2007-04-25 09:53:01 -04004916static void
James Smart2e0fef82007-06-17 19:56:36 -05004917lpfc_nlp_remove(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05004918{
James Smarta8adb832007-10-27 13:37:53 -04004919 struct lpfc_hba *phba = vport->phba;
James Smart1dcb58e2007-04-25 09:51:30 -04004920 struct lpfc_rport_data *rdata;
James Smart466e8402015-05-21 13:55:28 -04004921 struct fc_rport *rport;
James Smarta8adb832007-10-27 13:37:53 -04004922 LPFC_MBOXQ_t *mbox;
4923 int rc;
dea31012005-04-17 16:05:31 -05004924
James Smart0d2b6b82008-06-14 22:52:47 -04004925 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smart21e9a0a2009-05-22 14:53:21 -04004926 if ((ndlp->nlp_flag & NLP_DEFER_RM) &&
James Smart40426292010-12-15 17:58:10 -05004927 !(ndlp->nlp_flag & NLP_REG_LOGIN_SEND) &&
4928 !(ndlp->nlp_flag & NLP_RPI_REGISTERED)) {
James Smarta8adb832007-10-27 13:37:53 -04004929 /* For this case we need to cleanup the default rpi
4930 * allocated by the firmware.
4931 */
James Smartbe6bb942015-04-07 15:07:22 -04004932 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
4933 "0005 rpi:%x DID:%x flg:%x %d map:%x %p\n",
4934 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01004935 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04004936 ndlp->nlp_usg_map, ndlp);
James Smarta8adb832007-10-27 13:37:53 -04004937 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL))
4938 != NULL) {
James Smart21e9a0a2009-05-22 14:53:21 -04004939 rc = lpfc_reg_rpi(phba, vport->vpi, ndlp->nlp_DID,
James Smart40426292010-12-15 17:58:10 -05004940 (uint8_t *) &vport->fc_sparam, mbox, ndlp->nlp_rpi);
James Smarta8adb832007-10-27 13:37:53 -04004941 if (rc) {
4942 mempool_free(mbox, phba->mbox_mem_pool);
4943 }
4944 else {
4945 mbox->mbox_flag |= LPFC_MBX_IMED_UNREG;
4946 mbox->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4947 mbox->vport = vport;
James Smart086a3452012-08-14 14:25:21 -04004948 mbox->context2 = ndlp;
James Smarta8adb832007-10-27 13:37:53 -04004949 rc =lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
4950 if (rc == MBX_NOT_FINISHED) {
4951 mempool_free(mbox, phba->mbox_mem_pool);
4952 }
4953 }
4954 }
4955 }
James Smart2e0fef82007-06-17 19:56:36 -05004956 lpfc_cleanup_node(vport, ndlp);
James Smart1dcb58e2007-04-25 09:51:30 -04004957
James Smart2e0fef82007-06-17 19:56:36 -05004958 /*
James Smart466e8402015-05-21 13:55:28 -04004959 * ndlp->rport must be set to NULL before it reaches here
4960 * i.e. break rport/node link before doing lpfc_nlp_put for
4961 * registered rport and then drop the reference of rport.
James Smart2e0fef82007-06-17 19:56:36 -05004962 */
James Smart92d7f7b2007-06-17 19:56:38 -05004963 if (ndlp->rport) {
James Smart466e8402015-05-21 13:55:28 -04004964 /*
4965 * extra lpfc_nlp_put dropped the reference of ndlp
4966 * for registered rport so need to cleanup rport
4967 */
4968 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NODE,
4969 "0940 removed node x%p DID x%x "
4970 " rport not null %p\n",
4971 ndlp, ndlp->nlp_DID, ndlp->rport);
4972 rport = ndlp->rport;
4973 rdata = rport->dd_data;
James Smart329f9bc2007-04-25 09:53:01 -04004974 rdata->pnode = NULL;
4975 ndlp->rport = NULL;
James Smart466e8402015-05-21 13:55:28 -04004976 put_device(&rport->dev);
dea31012005-04-17 16:05:31 -05004977 }
dea31012005-04-17 16:05:31 -05004978}
4979
4980static int
James Smart2e0fef82007-06-17 19:56:36 -05004981lpfc_matchdid(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
4982 uint32_t did)
dea31012005-04-17 16:05:31 -05004983{
James Smart2e0fef82007-06-17 19:56:36 -05004984 D_ID mydid, ndlpdid, matchdid;
dea31012005-04-17 16:05:31 -05004985
4986 if (did == Bcast_DID)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004987 return 0;
dea31012005-04-17 16:05:31 -05004988
dea31012005-04-17 16:05:31 -05004989 /* First check for Direct match */
4990 if (ndlp->nlp_DID == did)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004991 return 1;
dea31012005-04-17 16:05:31 -05004992
4993 /* Next check for area/domain identically equals 0 match */
James Smart2e0fef82007-06-17 19:56:36 -05004994 mydid.un.word = vport->fc_myDID;
dea31012005-04-17 16:05:31 -05004995 if ((mydid.un.b.domain == 0) && (mydid.un.b.area == 0)) {
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05004996 return 0;
dea31012005-04-17 16:05:31 -05004997 }
4998
4999 matchdid.un.word = did;
5000 ndlpdid.un.word = ndlp->nlp_DID;
5001 if (matchdid.un.b.id == ndlpdid.un.b.id) {
5002 if ((mydid.un.b.domain == matchdid.un.b.domain) &&
5003 (mydid.un.b.area == matchdid.un.b.area)) {
James Smart6b337e02015-05-21 13:55:23 -04005004 /* This code is supposed to match the ID
5005 * for a private loop device that is
5006 * connect to fl_port. But we need to
5007 * check that the port did not just go
5008 * from pt2pt to fabric or we could end
5009 * up matching ndlp->nlp_DID 000001 to
5010 * fabric DID 0x20101
5011 */
dea31012005-04-17 16:05:31 -05005012 if ((ndlpdid.un.b.domain == 0) &&
5013 (ndlpdid.un.b.area == 0)) {
James Smart6b337e02015-05-21 13:55:23 -04005014 if (ndlpdid.un.b.id &&
5015 vport->phba->fc_topology ==
5016 LPFC_TOPOLOGY_LOOP)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005017 return 1;
dea31012005-04-17 16:05:31 -05005018 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005019 return 0;
dea31012005-04-17 16:05:31 -05005020 }
5021
5022 matchdid.un.word = ndlp->nlp_DID;
5023 if ((mydid.un.b.domain == ndlpdid.un.b.domain) &&
5024 (mydid.un.b.area == ndlpdid.un.b.area)) {
5025 if ((matchdid.un.b.domain == 0) &&
5026 (matchdid.un.b.area == 0)) {
5027 if (matchdid.un.b.id)
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005028 return 1;
dea31012005-04-17 16:05:31 -05005029 }
5030 }
5031 }
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005032 return 0;
dea31012005-04-17 16:05:31 -05005033}
5034
James Smart685f0bf2007-04-25 09:53:08 -04005035/* Search for a nodelist entry */
James Smart2e0fef82007-06-17 19:56:36 -05005036static struct lpfc_nodelist *
5037__lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005038{
James Smart2fb9bd82006-12-02 13:33:57 -05005039 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005040 uint32_t data1;
5041
James Smart2e0fef82007-06-17 19:56:36 -05005042 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5043 if (lpfc_matchdid(vport, ndlp, did)) {
James Smart685f0bf2007-04-25 09:53:08 -04005044 data1 = (((uint32_t) ndlp->nlp_state << 24) |
5045 ((uint32_t) ndlp->nlp_xri << 16) |
5046 ((uint32_t) ndlp->nlp_type << 8) |
5047 ((uint32_t) ndlp->nlp_rpi & 0xff));
James Smarte8b62012007-08-02 11:10:09 -04005048 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5049 "0929 FIND node DID "
James Smartcff261f2013-12-17 20:29:47 -05005050 "Data: x%p x%x x%x x%x %p\n",
James Smarte8b62012007-08-02 11:10:09 -04005051 ndlp, ndlp->nlp_DID,
James Smartcff261f2013-12-17 20:29:47 -05005052 ndlp->nlp_flag, data1,
5053 ndlp->active_rrqs_xri_bitmap);
James Smart685f0bf2007-04-25 09:53:08 -04005054 return ndlp;
dea31012005-04-17 16:05:31 -05005055 }
5056 }
Jamie Wellnitz66a9ed62006-02-28 22:33:10 -05005057
dea31012005-04-17 16:05:31 -05005058 /* FIND node did <did> NOT FOUND */
James Smarte8b62012007-08-02 11:10:09 -04005059 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5060 "0932 FIND node did x%x NOT FOUND.\n", did);
dea31012005-04-17 16:05:31 -05005061 return NULL;
5062}
5063
5064struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005065lpfc_findnode_did(struct lpfc_vport *vport, uint32_t did)
dea31012005-04-17 16:05:31 -05005066{
James Smart2e0fef82007-06-17 19:56:36 -05005067 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
dea31012005-04-17 16:05:31 -05005068 struct lpfc_nodelist *ndlp;
James Smartfedd3b72011-02-16 12:39:24 -05005069 unsigned long iflags;
dea31012005-04-17 16:05:31 -05005070
James Smartfedd3b72011-02-16 12:39:24 -05005071 spin_lock_irqsave(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005072 ndlp = __lpfc_findnode_did(vport, did);
James Smartfedd3b72011-02-16 12:39:24 -05005073 spin_unlock_irqrestore(shost->host_lock, iflags);
James Smart2e0fef82007-06-17 19:56:36 -05005074 return ndlp;
5075}
5076
5077struct lpfc_nodelist *
5078lpfc_setup_disc_node(struct lpfc_vport *vport, uint32_t did)
5079{
5080 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5081 struct lpfc_nodelist *ndlp;
5082
5083 ndlp = lpfc_findnode_did(vport, did);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005084 if (!ndlp) {
James Smart2e0fef82007-06-17 19:56:36 -05005085 if ((vport->fc_flag & FC_RSCN_MODE) != 0 &&
5086 lpfc_rscn_payload_check(vport, did) == 0)
dea31012005-04-17 16:05:31 -05005087 return NULL;
5088 ndlp = (struct lpfc_nodelist *)
James Smart2e0fef82007-06-17 19:56:36 -05005089 mempool_alloc(vport->phba->nlp_mem_pool, GFP_KERNEL);
dea31012005-04-17 16:05:31 -05005090 if (!ndlp)
5091 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005092 lpfc_nlp_init(vport, ndlp, did);
5093 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5094 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005095 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005096 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005097 return ndlp;
James Smarte47c9092008-02-08 18:49:26 -05005098 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
5099 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_NPR_NODE);
5100 if (!ndlp)
5101 return NULL;
5102 spin_lock_irq(shost->host_lock);
5103 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5104 spin_unlock_irq(shost->host_lock);
5105 return ndlp;
dea31012005-04-17 16:05:31 -05005106 }
James Smarte47c9092008-02-08 18:49:26 -05005107
James Smart58da1ff2008-04-07 10:15:56 -04005108 if ((vport->fc_flag & FC_RSCN_MODE) &&
5109 !(vport->fc_flag & FC_NDISC_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05005110 if (lpfc_rscn_payload_check(vport, did)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005111 /* If we've already received a PLOGI from this NPort
James Smart87af33f2007-10-27 13:37:43 -04005112 * we don't need to try to discover it again.
5113 */
5114 if (ndlp->nlp_flag & NLP_RCV_PLOGI)
5115 return NULL;
5116
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005117 /* Since this node is marked for discovery,
5118 * delay timeout is not needed.
5119 */
James Smart0d2b6b82008-06-14 22:52:47 -04005120 lpfc_cancel_retry_delay_tmo(vport, ndlp);
James Smarta257bf92009-04-06 18:48:10 -04005121 spin_lock_irq(shost->host_lock);
5122 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
5123 spin_unlock_irq(shost->host_lock);
James Smart071fbd3d2006-04-15 11:53:20 -04005124 } else
dea31012005-04-17 16:05:31 -05005125 ndlp = NULL;
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005126 } else {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005127 /* If we've already received a PLOGI from this NPort,
James Smart87af33f2007-10-27 13:37:43 -04005128 * or we are already in the process of discovery on it,
5129 * we don't need to try to discover it again.
5130 */
James Smart685f0bf2007-04-25 09:53:08 -04005131 if (ndlp->nlp_state == NLP_STE_ADISC_ISSUE ||
James Smart87af33f2007-10-27 13:37:43 -04005132 ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5133 ndlp->nlp_flag & NLP_RCV_PLOGI)
dea31012005-04-17 16:05:31 -05005134 return NULL;
James Smart2e0fef82007-06-17 19:56:36 -05005135 lpfc_nlp_set_state(vport, ndlp, NLP_STE_NPR_NODE);
5136 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005137 ndlp->nlp_flag |= NLP_NPR_2B_DISC;
James Smart2e0fef82007-06-17 19:56:36 -05005138 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005139 }
5140 return ndlp;
5141}
5142
5143/* Build a list of nodes to discover based on the loopmap */
5144void
James Smart2e0fef82007-06-17 19:56:36 -05005145lpfc_disc_list_loopmap(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005146{
James Smart2e0fef82007-06-17 19:56:36 -05005147 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005148 int j;
5149 uint32_t alpa, index;
5150
James Smart2e0fef82007-06-17 19:56:36 -05005151 if (!lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05005152 return;
James Smart2e0fef82007-06-17 19:56:36 -05005153
James Smart76a95d72010-11-20 23:11:48 -05005154 if (phba->fc_topology != LPFC_TOPOLOGY_LOOP)
dea31012005-04-17 16:05:31 -05005155 return;
dea31012005-04-17 16:05:31 -05005156
5157 /* Check for loop map present or not */
5158 if (phba->alpa_map[0]) {
5159 for (j = 1; j <= phba->alpa_map[0]; j++) {
5160 alpa = phba->alpa_map[j];
James Smart2e0fef82007-06-17 19:56:36 -05005161 if (((vport->fc_myDID & 0xff) == alpa) || (alpa == 0))
dea31012005-04-17 16:05:31 -05005162 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005163 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005164 }
5165 } else {
5166 /* No alpamap, so try all alpa's */
5167 for (j = 0; j < FC_MAXLOOP; j++) {
5168 /* If cfg_scan_down is set, start from highest
5169 * ALPA (0xef) to lowest (0x1).
5170 */
James Smart3de2a652007-08-02 11:09:59 -04005171 if (vport->cfg_scan_down)
dea31012005-04-17 16:05:31 -05005172 index = j;
5173 else
5174 index = FC_MAXLOOP - j - 1;
5175 alpa = lpfcAlpaArray[index];
James Smart2e0fef82007-06-17 19:56:36 -05005176 if ((vport->fc_myDID & 0xff) == alpa)
dea31012005-04-17 16:05:31 -05005177 continue;
James Smart2e0fef82007-06-17 19:56:36 -05005178 lpfc_setup_disc_node(vport, alpa);
dea31012005-04-17 16:05:31 -05005179 }
5180 }
5181 return;
5182}
5183
James Smart895427b2017-02-12 13:52:30 -08005184/* SLI3 only */
dea31012005-04-17 16:05:31 -05005185void
James Smart2e0fef82007-06-17 19:56:36 -05005186lpfc_issue_clear_la(struct lpfc_hba *phba, struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005187{
dea31012005-04-17 16:05:31 -05005188 LPFC_MBOXQ_t *mbox;
James Smart2e0fef82007-06-17 19:56:36 -05005189 struct lpfc_sli *psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005190 struct lpfc_sli_ring *extra_ring = &psli->sli3_ring[LPFC_EXTRA_RING];
5191 struct lpfc_sli_ring *fcp_ring = &psli->sli3_ring[LPFC_FCP_RING];
James Smart2e0fef82007-06-17 19:56:36 -05005192 int rc;
5193
James Smart92d7f7b2007-06-17 19:56:38 -05005194 /*
5195 * if it's not a physical port or if we already send
5196 * clear_la then don't send it.
5197 */
5198 if ((phba->link_state >= LPFC_CLEAR_LA) ||
James Smartda0436e2009-05-22 14:51:39 -04005199 (vport->port_type != LPFC_PHYSICAL_PORT) ||
5200 (phba->sli_rev == LPFC_SLI_REV4))
James Smart92d7f7b2007-06-17 19:56:38 -05005201 return;
5202
James Smart2e0fef82007-06-17 19:56:36 -05005203 /* Link up discovery */
5204 if ((mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL)) != NULL) {
5205 phba->link_state = LPFC_CLEAR_LA;
5206 lpfc_clear_la(phba, mbox);
5207 mbox->mbox_cmpl = lpfc_mbx_cmpl_clear_la;
5208 mbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005209 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
James Smart2e0fef82007-06-17 19:56:36 -05005210 if (rc == MBX_NOT_FINISHED) {
5211 mempool_free(mbox, phba->mbox_mem_pool);
5212 lpfc_disc_flush_list(vport);
5213 extra_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
5214 fcp_ring->flag &= ~LPFC_STOP_IOCB_EVENT;
James Smart92d7f7b2007-06-17 19:56:38 -05005215 phba->link_state = LPFC_HBA_ERROR;
5216 }
5217 }
5218}
5219
5220/* Reg_vpi to tell firmware to resume normal operations */
5221void
5222lpfc_issue_reg_vpi(struct lpfc_hba *phba, struct lpfc_vport *vport)
5223{
5224 LPFC_MBOXQ_t *regvpimbox;
5225
5226 regvpimbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5227 if (regvpimbox) {
James Smartda0436e2009-05-22 14:51:39 -04005228 lpfc_reg_vpi(vport, regvpimbox);
James Smart92d7f7b2007-06-17 19:56:38 -05005229 regvpimbox->mbox_cmpl = lpfc_mbx_cmpl_reg_vpi;
5230 regvpimbox->vport = vport;
James Smart0b727fe2007-10-27 13:37:25 -04005231 if (lpfc_sli_issue_mbox(phba, regvpimbox, MBX_NOWAIT)
James Smart92d7f7b2007-06-17 19:56:38 -05005232 == MBX_NOT_FINISHED) {
5233 mempool_free(regvpimbox, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05005234 }
5235 }
5236}
5237
5238/* Start Link up / RSCN discovery on NPR nodes */
5239void
5240lpfc_disc_start(struct lpfc_vport *vport)
5241{
5242 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5243 struct lpfc_hba *phba = vport->phba;
James Smart685f0bf2007-04-25 09:53:08 -04005244 uint32_t num_sent;
dea31012005-04-17 16:05:31 -05005245 uint32_t clear_la_pending;
dea31012005-04-17 16:05:31 -05005246
James Smarte74c03c2013-04-17 20:15:19 -04005247 if (!lpfc_is_link_up(phba)) {
5248 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5249 "3315 Link is not up %x\n",
5250 phba->link_state);
dea31012005-04-17 16:05:31 -05005251 return;
James Smarte74c03c2013-04-17 20:15:19 -04005252 }
James Smart2e0fef82007-06-17 19:56:36 -05005253
5254 if (phba->link_state == LPFC_CLEAR_LA)
dea31012005-04-17 16:05:31 -05005255 clear_la_pending = 1;
5256 else
5257 clear_la_pending = 0;
5258
James Smart2e0fef82007-06-17 19:56:36 -05005259 if (vport->port_state < LPFC_VPORT_READY)
5260 vport->port_state = LPFC_DISC_AUTH;
dea31012005-04-17 16:05:31 -05005261
James Smart2e0fef82007-06-17 19:56:36 -05005262 lpfc_set_disctmo(vport);
5263
James Smart2e0fef82007-06-17 19:56:36 -05005264 vport->fc_prevDID = vport->fc_myDID;
5265 vport->num_disc_nodes = 0;
dea31012005-04-17 16:05:31 -05005266
5267 /* Start Discovery state <hba_state> */
James Smarte8b62012007-08-02 11:10:09 -04005268 lpfc_printf_vlog(vport, KERN_INFO, LOG_DISCOVERY,
5269 "0202 Start Discovery hba state x%x "
5270 "Data: x%x x%x x%x\n",
5271 vport->port_state, vport->fc_flag, vport->fc_plogi_cnt,
5272 vport->fc_adisc_cnt);
dea31012005-04-17 16:05:31 -05005273
5274 /* First do ADISCs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005275 num_sent = lpfc_els_disc_adisc(vport);
dea31012005-04-17 16:05:31 -05005276
5277 if (num_sent)
5278 return;
5279
James Smart6fa139f2013-04-17 20:20:00 -04005280 /* Register the VPI for SLI3, NPIV only. */
James Smart92d7f7b2007-06-17 19:56:38 -05005281 if ((phba->sli3_options & LPFC_SLI3_NPIV_ENABLED) &&
James Smart1b32f6a2008-02-08 18:49:39 -05005282 !(vport->fc_flag & FC_PT2PT) &&
James Smartda0436e2009-05-22 14:51:39 -04005283 !(vport->fc_flag & FC_RSCN_MODE) &&
5284 (phba->sli_rev < LPFC_SLI_REV4)) {
James Smart85c0f172015-04-07 15:07:12 -04005285 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005286 lpfc_issue_reg_vpi(phba, vport);
5287 return;
5288 }
James Smart2e0fef82007-06-17 19:56:36 -05005289
James Smart92d7f7b2007-06-17 19:56:38 -05005290 /*
5291 * For SLI2, we need to set port_state to READY and continue
5292 * discovery.
5293 */
5294 if (vport->port_state < LPFC_VPORT_READY && !clear_la_pending) {
5295 /* If we get here, there is nothing to ADISC */
James Smart85c0f172015-04-07 15:07:12 -04005296 lpfc_issue_clear_la(phba, vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005297
5298 if (!(vport->fc_flag & FC_ABORT_DISCOVERY)) {
James Smart2e0fef82007-06-17 19:56:36 -05005299 vport->num_disc_nodes = 0;
5300 /* go thru NPR nodes and issue ELS PLOGIs */
5301 if (vport->fc_npr_cnt)
5302 lpfc_els_disc_plogi(vport);
5303
5304 if (!vport->num_disc_nodes) {
5305 spin_lock_irq(shost->host_lock);
5306 vport->fc_flag &= ~FC_NDISC_ACTIVE;
5307 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005308 lpfc_can_disctmo(vport);
dea31012005-04-17 16:05:31 -05005309 }
5310 }
James Smart92d7f7b2007-06-17 19:56:38 -05005311 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005312 } else {
5313 /* Next do PLOGIs - if any */
James Smart2e0fef82007-06-17 19:56:36 -05005314 num_sent = lpfc_els_disc_plogi(vport);
dea31012005-04-17 16:05:31 -05005315
5316 if (num_sent)
5317 return;
5318
James Smart2e0fef82007-06-17 19:56:36 -05005319 if (vport->fc_flag & FC_RSCN_MODE) {
dea31012005-04-17 16:05:31 -05005320 /* Check to see if more RSCNs came in while we
5321 * were processing this one.
5322 */
James Smart2e0fef82007-06-17 19:56:36 -05005323 if ((vport->fc_rscn_id_cnt == 0) &&
5324 (!(vport->fc_flag & FC_RSCN_DISCOVERY))) {
5325 spin_lock_irq(shost->host_lock);
5326 vport->fc_flag &= ~FC_RSCN_MODE;
5327 spin_unlock_irq(shost->host_lock);
James Smart92d7f7b2007-06-17 19:56:38 -05005328 lpfc_can_disctmo(vport);
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005329 } else
James Smart2e0fef82007-06-17 19:56:36 -05005330 lpfc_els_handle_rscn(vport);
dea31012005-04-17 16:05:31 -05005331 }
5332 }
5333 return;
5334}
5335
5336/*
5337 * Ignore completion for all IOCBs on tx and txcmpl queue for ELS
5338 * ring the match the sppecified nodelist.
5339 */
5340static void
James Smart2e0fef82007-06-17 19:56:36 -05005341lpfc_free_tx(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
dea31012005-04-17 16:05:31 -05005342{
James Smart2534ba72007-04-25 09:52:20 -04005343 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05005344 struct lpfc_sli *psli;
5345 IOCB_t *icmd;
5346 struct lpfc_iocbq *iocb, *next_iocb;
5347 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05005348
5349 psli = &phba->sli;
James Smart895427b2017-02-12 13:52:30 -08005350 pring = lpfc_phba_elsring(phba);
dea31012005-04-17 16:05:31 -05005351
5352 /* Error matching iocb on txq or txcmplq
5353 * First check the txq.
5354 */
James Smart2e0fef82007-06-17 19:56:36 -05005355 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05005356 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
5357 if (iocb->context1 != ndlp) {
5358 continue;
5359 }
5360 icmd = &iocb->iocb;
5361 if ((icmd->ulpCommand == CMD_ELS_REQUEST64_CR) ||
5362 (icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX)) {
5363
James Smart2534ba72007-04-25 09:52:20 -04005364 list_move_tail(&iocb->list, &completions);
dea31012005-04-17 16:05:31 -05005365 }
5366 }
5367
5368 /* Next check the txcmplq */
5369 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
5370 if (iocb->context1 != ndlp) {
5371 continue;
5372 }
5373 icmd = &iocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05005374 if (icmd->ulpCommand == CMD_ELS_REQUEST64_CR ||
5375 icmd->ulpCommand == CMD_XMIT_ELS_RSP64_CX) {
James Smart2534ba72007-04-25 09:52:20 -04005376 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea31012005-04-17 16:05:31 -05005377 }
5378 }
James Smart2e0fef82007-06-17 19:56:36 -05005379 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04005380
James Smarta257bf92009-04-06 18:48:10 -04005381 /* Cancel all the IOCBs from the completions list */
5382 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
5383 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05005384}
5385
Adrian Bunka6ababd2007-11-05 18:07:33 +01005386static void
James Smart2e0fef82007-06-17 19:56:36 -05005387lpfc_disc_flush_list(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005388{
5389 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart2e0fef82007-06-17 19:56:36 -05005390 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05005391
James Smart2e0fef82007-06-17 19:56:36 -05005392 if (vport->fc_plogi_cnt || vport->fc_adisc_cnt) {
5393 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005394 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005395 if (!NLP_CHK_NODE_ACT(ndlp))
5396 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005397 if (ndlp->nlp_state == NLP_STE_PLOGI_ISSUE ||
5398 ndlp->nlp_state == NLP_STE_ADISC_ISSUE) {
5399 lpfc_free_tx(phba, ndlp);
James Smart685f0bf2007-04-25 09:53:08 -04005400 }
dea31012005-04-17 16:05:31 -05005401 }
5402 }
dea31012005-04-17 16:05:31 -05005403}
5404
James Smart92d7f7b2007-06-17 19:56:38 -05005405void
5406lpfc_cleanup_discovery_resources(struct lpfc_vport *vport)
5407{
5408 lpfc_els_flush_rscn(vport);
5409 lpfc_els_flush_cmd(vport);
5410 lpfc_disc_flush_list(vport);
5411}
5412
dea31012005-04-17 16:05:31 -05005413/*****************************************************************************/
5414/*
5415 * NAME: lpfc_disc_timeout
5416 *
5417 * FUNCTION: Fibre Channel driver discovery timeout routine.
5418 *
5419 * EXECUTION ENVIRONMENT: interrupt only
5420 *
5421 * CALLED FROM:
5422 * Timer function
5423 *
5424 * RETURNS:
5425 * none
5426 */
5427/*****************************************************************************/
5428void
5429lpfc_disc_timeout(unsigned long ptr)
5430{
James Smart2e0fef82007-06-17 19:56:36 -05005431 struct lpfc_vport *vport = (struct lpfc_vport *) ptr;
5432 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -04005433 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05005434 unsigned long flags = 0;
5435
5436 if (unlikely(!phba))
5437 return;
5438
James Smart5e9d9b82008-06-14 22:52:53 -04005439 spin_lock_irqsave(&vport->work_port_lock, flags);
5440 tmo_posted = vport->work_port_events & WORKER_DISC_TMO;
5441 if (!tmo_posted)
James Smart2e0fef82007-06-17 19:56:36 -05005442 vport->work_port_events |= WORKER_DISC_TMO;
James Smart5e9d9b82008-06-14 22:52:53 -04005443 spin_unlock_irqrestore(&vport->work_port_lock, flags);
James Smart2e0fef82007-06-17 19:56:36 -05005444
James Smart5e9d9b82008-06-14 22:52:53 -04005445 if (!tmo_posted)
5446 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05005447 return;
5448}
5449
5450static void
James Smart2e0fef82007-06-17 19:56:36 -05005451lpfc_disc_timeout_handler(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -05005452{
James Smart2e0fef82007-06-17 19:56:36 -05005453 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5454 struct lpfc_hba *phba = vport->phba;
5455 struct lpfc_sli *psli = &phba->sli;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005456 struct lpfc_nodelist *ndlp, *next_ndlp;
James Smart92d7f7b2007-06-17 19:56:38 -05005457 LPFC_MBOXQ_t *initlinkmbox;
dea31012005-04-17 16:05:31 -05005458 int rc, clrlaerr = 0;
5459
James Smart2e0fef82007-06-17 19:56:36 -05005460 if (!(vport->fc_flag & FC_DISC_TMO))
dea31012005-04-17 16:05:31 -05005461 return;
5462
James Smart2e0fef82007-06-17 19:56:36 -05005463 spin_lock_irq(shost->host_lock);
5464 vport->fc_flag &= ~FC_DISC_TMO;
5465 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05005466
James Smart858c9f62007-06-17 19:56:39 -05005467 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_ELS_CMD,
5468 "disc timeout: state:x%x rtry:x%x flg:x%x",
5469 vport->port_state, vport->fc_ns_retry, vport->fc_flag);
5470
James Smart2e0fef82007-06-17 19:56:36 -05005471 switch (vport->port_state) {
dea31012005-04-17 16:05:31 -05005472
5473 case LPFC_LOCAL_CFG_LINK:
James Smarta0f2d3e2017-02-12 13:52:31 -08005474 /*
5475 * port_state is identically LPFC_LOCAL_CFG_LINK while
5476 * waiting for FAN timeout
5477 */
James Smarte8b62012007-08-02 11:10:09 -04005478 lpfc_printf_vlog(vport, KERN_WARNING, LOG_DISCOVERY,
5479 "0221 FAN timeout\n");
James Smarta0f2d3e2017-02-12 13:52:31 -08005480
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005481 /* Start discovery by sending FLOGI, clean up old rpis */
James Smart2e0fef82007-06-17 19:56:36 -05005482 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes,
James Smart685f0bf2007-04-25 09:53:08 -04005483 nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05005484 if (!NLP_CHK_NODE_ACT(ndlp))
5485 continue;
James Smart685f0bf2007-04-25 09:53:08 -04005486 if (ndlp->nlp_state != NLP_STE_NPR_NODE)
5487 continue;
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005488 if (ndlp->nlp_type & NLP_FABRIC) {
5489 /* Clean up the ndlp on Fabric connections */
James Smart2e0fef82007-06-17 19:56:36 -05005490 lpfc_drop_node(vport, ndlp);
James Smart87af33f2007-10-27 13:37:43 -04005491
Jamie Wellnitz2fe165b2006-02-28 19:25:31 -05005492 } else if (!(ndlp->nlp_flag & NLP_NPR_ADISC)) {
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005493 /* Fail outstanding IO now since device
5494 * is marked for PLOGI.
5495 */
James Smart2e0fef82007-06-17 19:56:36 -05005496 lpfc_unreg_rpi(vport, ndlp);
Jamie Wellnitzc9f87352006-02-28 19:25:23 -05005497 }
5498 }
James Smart92d7f7b2007-06-17 19:56:38 -05005499 if (vport->port_state != LPFC_FLOGI) {
James Smart76a95d72010-11-20 23:11:48 -05005500 if (phba->sli_rev <= LPFC_SLI_REV3)
5501 lpfc_initial_flogi(vport);
5502 else
5503 lpfc_issue_init_vfi(vport);
James Smart0ff10d42008-01-11 01:52:36 -05005504 return;
James Smart92d7f7b2007-06-17 19:56:38 -05005505 }
dea31012005-04-17 16:05:31 -05005506 break;
5507
James Smart92d7f7b2007-06-17 19:56:38 -05005508 case LPFC_FDISC:
dea31012005-04-17 16:05:31 -05005509 case LPFC_FLOGI:
James Smart2e0fef82007-06-17 19:56:36 -05005510 /* port_state is identically LPFC_FLOGI while waiting for FLOGI cmpl */
dea31012005-04-17 16:05:31 -05005511 /* Initial FLOGI timeout */
James Smarte8b62012007-08-02 11:10:09 -04005512 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5513 "0222 Initial %s timeout\n",
James Smart87af33f2007-10-27 13:37:43 -04005514 vport->vpi ? "FDISC" : "FLOGI");
dea31012005-04-17 16:05:31 -05005515
5516 /* Assume no Fabric and go on with discovery.
5517 * Check for outstanding ELS FLOGI to abort.
5518 */
5519
5520 /* FLOGI failed, so just use loop map to make discovery list */
James Smart2e0fef82007-06-17 19:56:36 -05005521 lpfc_disc_list_loopmap(vport);
dea31012005-04-17 16:05:31 -05005522
5523 /* Start discovery */
James Smart2e0fef82007-06-17 19:56:36 -05005524 lpfc_disc_start(vport);
dea31012005-04-17 16:05:31 -05005525 break;
5526
5527 case LPFC_FABRIC_CFG_LINK:
5528 /* hba_state is identically LPFC_FABRIC_CFG_LINK while waiting for
5529 NameServer login */
James Smarte8b62012007-08-02 11:10:09 -04005530 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5531 "0223 Timeout while waiting for "
5532 "NameServer login\n");
dea31012005-04-17 16:05:31 -05005533 /* Next look for NameServer ndlp */
James Smart2e0fef82007-06-17 19:56:36 -05005534 ndlp = lpfc_findnode_did(vport, NameServer_DID);
James Smarte47c9092008-02-08 18:49:26 -05005535 if (ndlp && NLP_CHK_NODE_ACT(ndlp))
James Smart87af33f2007-10-27 13:37:43 -04005536 lpfc_els_abort(phba, ndlp);
5537
5538 /* ReStart discovery */
5539 goto restart_disc;
dea31012005-04-17 16:05:31 -05005540
5541 case LPFC_NS_QRY:
5542 /* Check for wait for NameServer Rsp timeout */
James Smarte8b62012007-08-02 11:10:09 -04005543 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5544 "0224 NameServer Query timeout "
5545 "Data: x%x x%x\n",
5546 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005547
James Smart92d7f7b2007-06-17 19:56:38 -05005548 if (vport->fc_ns_retry < LPFC_MAX_NS_RETRY) {
5549 /* Try it one more time */
5550 vport->fc_ns_retry++;
James Smarta0f2d3e2017-02-12 13:52:31 -08005551 vport->gidft_inp = 0;
5552 rc = lpfc_issue_gidft(vport);
James Smart92d7f7b2007-06-17 19:56:38 -05005553 if (rc == 0)
5554 break;
dea31012005-04-17 16:05:31 -05005555 }
James Smart92d7f7b2007-06-17 19:56:38 -05005556 vport->fc_ns_retry = 0;
dea31012005-04-17 16:05:31 -05005557
James Smart87af33f2007-10-27 13:37:43 -04005558restart_disc:
James Smart92d7f7b2007-06-17 19:56:38 -05005559 /*
5560 * Discovery is over.
5561 * set port_state to PORT_READY if SLI2.
5562 * cmpl_reg_vpi will set port_state to READY for SLI3.
5563 */
James Smart3772a992009-05-22 14:50:54 -04005564 if (phba->sli_rev < LPFC_SLI_REV4) {
5565 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5566 lpfc_issue_reg_vpi(phba, vport);
James Smart6d368e52011-05-24 11:44:12 -04005567 else {
James Smart3772a992009-05-22 14:50:54 -04005568 lpfc_issue_clear_la(phba, vport);
5569 vport->port_state = LPFC_VPORT_READY;
5570 }
dea31012005-04-17 16:05:31 -05005571 }
5572
5573 /* Setup and issue mailbox INITIALIZE LINK command */
5574 initlinkmbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5575 if (!initlinkmbox) {
James Smarte8b62012007-08-02 11:10:09 -04005576 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5577 "0206 Device Discovery "
5578 "completion error\n");
James Smart2e0fef82007-06-17 19:56:36 -05005579 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05005580 break;
5581 }
5582
5583 lpfc_linkdown(phba);
5584 lpfc_init_link(phba, initlinkmbox, phba->cfg_topology,
5585 phba->cfg_link_speed);
James Smart04c68492009-05-22 14:52:52 -04005586 initlinkmbox->u.mb.un.varInitLnk.lipsr_AL_PA = 0;
James Smarted957682007-06-17 19:56:37 -05005587 initlinkmbox->vport = vport;
James Smart92d7f7b2007-06-17 19:56:38 -05005588 initlinkmbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart0b727fe2007-10-27 13:37:25 -04005589 rc = lpfc_sli_issue_mbox(phba, initlinkmbox, MBX_NOWAIT);
James Smart5b8bd0c2007-04-25 09:52:49 -04005590 lpfc_set_loopback_flag(phba);
dea31012005-04-17 16:05:31 -05005591 if (rc == MBX_NOT_FINISHED)
5592 mempool_free(initlinkmbox, phba->mbox_mem_pool);
5593
5594 break;
5595
5596 case LPFC_DISC_AUTH:
5597 /* Node Authentication timeout */
James Smarte8b62012007-08-02 11:10:09 -04005598 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5599 "0227 Node Authentication timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005600 lpfc_disc_flush_list(vport);
5601
James Smart92d7f7b2007-06-17 19:56:38 -05005602 /*
5603 * set port_state to PORT_READY if SLI2.
5604 * cmpl_reg_vpi will set port_state to READY for SLI3.
5605 */
James Smart3772a992009-05-22 14:50:54 -04005606 if (phba->sli_rev < LPFC_SLI_REV4) {
5607 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
5608 lpfc_issue_reg_vpi(phba, vport);
5609 else { /* NPIV Not enabled */
5610 lpfc_issue_clear_la(phba, vport);
5611 vport->port_state = LPFC_VPORT_READY;
5612 }
dea31012005-04-17 16:05:31 -05005613 }
5614 break;
5615
James Smart2e0fef82007-06-17 19:56:36 -05005616 case LPFC_VPORT_READY:
5617 if (vport->fc_flag & FC_RSCN_MODE) {
James Smarte8b62012007-08-02 11:10:09 -04005618 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5619 "0231 RSCN timeout Data: x%x "
5620 "x%x\n",
5621 vport->fc_ns_retry, LPFC_MAX_NS_RETRY);
dea31012005-04-17 16:05:31 -05005622
5623 /* Cleanup any outstanding ELS commands */
James Smart2e0fef82007-06-17 19:56:36 -05005624 lpfc_els_flush_cmd(vport);
dea31012005-04-17 16:05:31 -05005625
James Smart2e0fef82007-06-17 19:56:36 -05005626 lpfc_els_flush_rscn(vport);
5627 lpfc_disc_flush_list(vport);
dea31012005-04-17 16:05:31 -05005628 }
5629 break;
James Smart2e0fef82007-06-17 19:56:36 -05005630
James Smart92d7f7b2007-06-17 19:56:38 -05005631 default:
James Smarte8b62012007-08-02 11:10:09 -04005632 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
James Smartd7c255b2008-08-24 21:50:00 -04005633 "0273 Unexpected discovery timeout, "
James Smarte8b62012007-08-02 11:10:09 -04005634 "vport State x%x\n", vport->port_state);
James Smart2e0fef82007-06-17 19:56:36 -05005635 break;
5636 }
5637
5638 switch (phba->link_state) {
5639 case LPFC_CLEAR_LA:
James Smart92d7f7b2007-06-17 19:56:38 -05005640 /* CLEAR LA timeout */
James Smarte8b62012007-08-02 11:10:09 -04005641 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5642 "0228 CLEAR LA timeout\n");
James Smart2e0fef82007-06-17 19:56:36 -05005643 clrlaerr = 1;
5644 break;
5645
James Smart09372822008-01-11 01:52:54 -05005646 case LPFC_LINK_UP:
5647 lpfc_issue_clear_la(phba, vport);
5648 /* Drop thru */
James Smart2e0fef82007-06-17 19:56:36 -05005649 case LPFC_LINK_UNKNOWN:
5650 case LPFC_WARM_START:
5651 case LPFC_INIT_START:
5652 case LPFC_INIT_MBX_CMDS:
5653 case LPFC_LINK_DOWN:
James Smart2e0fef82007-06-17 19:56:36 -05005654 case LPFC_HBA_ERROR:
James Smarte8b62012007-08-02 11:10:09 -04005655 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
5656 "0230 Unexpected timeout, hba link "
5657 "state x%x\n", phba->link_state);
James Smart2e0fef82007-06-17 19:56:36 -05005658 clrlaerr = 1;
5659 break;
James Smart92d7f7b2007-06-17 19:56:38 -05005660
5661 case LPFC_HBA_READY:
5662 break;
dea31012005-04-17 16:05:31 -05005663 }
5664
5665 if (clrlaerr) {
James Smart2e0fef82007-06-17 19:56:36 -05005666 lpfc_disc_flush_list(vport);
James Smart895427b2017-02-12 13:52:30 -08005667 if (phba->sli_rev != LPFC_SLI_REV4) {
5668 psli->sli3_ring[(LPFC_EXTRA_RING)].flag &=
5669 ~LPFC_STOP_IOCB_EVENT;
5670 psli->sli3_ring[LPFC_FCP_RING].flag &=
5671 ~LPFC_STOP_IOCB_EVENT;
5672 }
James Smart2e0fef82007-06-17 19:56:36 -05005673 vport->port_state = LPFC_VPORT_READY;
dea31012005-04-17 16:05:31 -05005674 }
dea31012005-04-17 16:05:31 -05005675 return;
5676}
5677
dea31012005-04-17 16:05:31 -05005678/*
5679 * This routine handles processing a NameServer REG_LOGIN mailbox
5680 * command upon completion. It is setup in the LPFC_MBOXQ
5681 * as the completion routine when the command is
5682 * handed off to the SLI layer.
5683 */
5684void
James Smart2e0fef82007-06-17 19:56:36 -05005685lpfc_mbx_cmpl_fdmi_reg_login(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05005686{
James Smart04c68492009-05-22 14:52:52 -04005687 MAILBOX_t *mb = &pmb->u.mb;
James Smart2e0fef82007-06-17 19:56:36 -05005688 struct lpfc_dmabuf *mp = (struct lpfc_dmabuf *) (pmb->context1);
5689 struct lpfc_nodelist *ndlp = (struct lpfc_nodelist *) pmb->context2;
5690 struct lpfc_vport *vport = pmb->vport;
dea31012005-04-17 16:05:31 -05005691
5692 pmb->context1 = NULL;
James Smartd439d282010-09-29 11:18:45 -04005693 pmb->context2 = NULL;
dea31012005-04-17 16:05:31 -05005694
James Smart6d368e52011-05-24 11:44:12 -04005695 if (phba->sli_rev < LPFC_SLI_REV4)
5696 ndlp->nlp_rpi = mb->un.varWords[0];
James Smart40426292010-12-15 17:58:10 -05005697 ndlp->nlp_flag |= NLP_RPI_REGISTERED;
dea31012005-04-17 16:05:31 -05005698 ndlp->nlp_type |= NLP_FABRIC;
James Smart2e0fef82007-06-17 19:56:36 -05005699 lpfc_nlp_set_state(vport, ndlp, NLP_STE_UNMAPPED_NODE);
James Smartbe6bb942015-04-07 15:07:22 -04005700 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
5701 "0004 rpi:%x DID:%x flg:%x %d map:%x %p\n",
5702 ndlp->nlp_rpi, ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005703 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005704 ndlp->nlp_usg_map, ndlp);
James Smart2e0fef82007-06-17 19:56:36 -05005705 /*
5706 * Start issuing Fabric-Device Management Interface (FDMI) command to
James Smart4258e982015-12-16 18:11:58 -05005707 * 0xfffffa (FDMI well known port).
5708 * DHBA -> DPRT -> RHBA -> RPA (physical port)
5709 * DPRT -> RPRT (vports)
dea31012005-04-17 16:05:31 -05005710 */
James Smart4258e982015-12-16 18:11:58 -05005711 if (vport->port_type == LPFC_PHYSICAL_PORT)
5712 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DHBA, 0);
James Smart76b2c342015-04-07 15:07:19 -04005713 else
James Smart4258e982015-12-16 18:11:58 -05005714 lpfc_fdmi_cmd(vport, ndlp, SLI_MGMT_DPRT, 0);
5715
dea31012005-04-17 16:05:31 -05005716
James Smartfa4066b2008-01-11 01:53:27 -05005717 /* decrement the node reference count held for this callback
5718 * function.
5719 */
James Smart329f9bc2007-04-25 09:53:01 -04005720 lpfc_nlp_put(ndlp);
dea31012005-04-17 16:05:31 -05005721 lpfc_mbuf_free(phba, mp->virt, mp->phys);
5722 kfree(mp);
James Smart329f9bc2007-04-25 09:53:01 -04005723 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05005724
5725 return;
5726}
5727
James Smart685f0bf2007-04-25 09:53:08 -04005728static int
5729lpfc_filter_by_rpi(struct lpfc_nodelist *ndlp, void *param)
5730{
5731 uint16_t *rpi = param;
5732
James Smarteff4a012012-01-18 16:25:25 -05005733 /* check for active node */
5734 if (!NLP_CHK_NODE_ACT(ndlp))
5735 return 0;
5736
James Smart685f0bf2007-04-25 09:53:08 -04005737 return ndlp->nlp_rpi == *rpi;
5738}
5739
5740static int
5741lpfc_filter_by_wwpn(struct lpfc_nodelist *ndlp, void *param)
5742{
5743 return memcmp(&ndlp->nlp_portname, param,
5744 sizeof(ndlp->nlp_portname)) == 0;
5745}
5746
Adrian Bunka6ababd2007-11-05 18:07:33 +01005747static struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005748__lpfc_find_node(struct lpfc_vport *vport, node_filter filter, void *param)
dea31012005-04-17 16:05:31 -05005749{
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005750 struct lpfc_nodelist *ndlp;
dea31012005-04-17 16:05:31 -05005751
James Smart2e0fef82007-06-17 19:56:36 -05005752 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart34f5ad82012-08-03 12:35:03 -04005753 if (filter(ndlp, param)) {
5754 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5755 "3185 FIND node filter %p DID "
5756 "Data: x%p x%x x%x\n",
5757 filter, ndlp, ndlp->nlp_DID,
5758 ndlp->nlp_flag);
James Smart685f0bf2007-04-25 09:53:08 -04005759 return ndlp;
James Smart34f5ad82012-08-03 12:35:03 -04005760 }
James Smart685f0bf2007-04-25 09:53:08 -04005761 }
James Smart34f5ad82012-08-03 12:35:03 -04005762 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5763 "3186 FIND node filter %p NOT FOUND.\n", filter);
James.Smart@Emulex.Com21568f52005-10-28 20:29:36 -04005764 return NULL;
dea31012005-04-17 16:05:31 -05005765}
5766
James Smart685f0bf2007-04-25 09:53:08 -04005767/*
5768 * This routine looks up the ndlp lists for the given RPI. If rpi found it
James Smart2e0fef82007-06-17 19:56:36 -05005769 * returns the node list element pointer else return NULL.
James Smart685f0bf2007-04-25 09:53:08 -04005770 */
5771struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005772__lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
James Smart685f0bf2007-04-25 09:53:08 -04005773{
James Smart2e0fef82007-06-17 19:56:36 -05005774 return __lpfc_find_node(vport, lpfc_filter_by_rpi, &rpi);
James Smart685f0bf2007-04-25 09:53:08 -04005775}
5776
James Smart488d1462006-03-07 15:02:37 -05005777/*
James Smart685f0bf2007-04-25 09:53:08 -04005778 * This routine looks up the ndlp lists for the given WWPN. If WWPN found it
James Smart2e0fef82007-06-17 19:56:36 -05005779 * returns the node element list pointer else return NULL.
James Smart488d1462006-03-07 15:02:37 -05005780 */
5781struct lpfc_nodelist *
James Smart2e0fef82007-06-17 19:56:36 -05005782lpfc_findnode_wwpn(struct lpfc_vport *vport, struct lpfc_name *wwpn)
James Smart488d1462006-03-07 15:02:37 -05005783{
James Smart2e0fef82007-06-17 19:56:36 -05005784 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart488d1462006-03-07 15:02:37 -05005785 struct lpfc_nodelist *ndlp;
James Smart488d1462006-03-07 15:02:37 -05005786
James Smart2e0fef82007-06-17 19:56:36 -05005787 spin_lock_irq(shost->host_lock);
5788 ndlp = __lpfc_find_node(vport, lpfc_filter_by_wwpn, wwpn);
5789 spin_unlock_irq(shost->host_lock);
James Smart858c9f62007-06-17 19:56:39 -05005790 return ndlp;
James Smart488d1462006-03-07 15:02:37 -05005791}
5792
James Smartcb69f7d2011-12-13 13:21:57 -05005793/*
5794 * This routine looks up the ndlp lists for the given RPI. If the rpi
5795 * is found, the routine returns the node element list pointer else
5796 * return NULL.
5797 */
5798struct lpfc_nodelist *
5799lpfc_findnode_rpi(struct lpfc_vport *vport, uint16_t rpi)
5800{
5801 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
5802 struct lpfc_nodelist *ndlp;
5803
5804 spin_lock_irq(shost->host_lock);
5805 ndlp = __lpfc_findnode_rpi(vport, rpi);
5806 spin_unlock_irq(shost->host_lock);
5807 return ndlp;
5808}
5809
5810/**
5811 * lpfc_find_vport_by_vpid - Find a vport on a HBA through vport identifier
5812 * @phba: pointer to lpfc hba data structure.
5813 * @vpi: the physical host virtual N_Port identifier.
5814 *
5815 * This routine finds a vport on a HBA (referred by @phba) through a
5816 * @vpi. The function walks the HBA's vport list and returns the address
5817 * of the vport with the matching @vpi.
5818 *
5819 * Return code
5820 * NULL - No vport with the matching @vpi found
5821 * Otherwise - Address to the vport with the matching @vpi.
5822 **/
5823struct lpfc_vport *
5824lpfc_find_vport_by_vpid(struct lpfc_hba *phba, uint16_t vpi)
5825{
5826 struct lpfc_vport *vport;
5827 unsigned long flags;
5828 int i = 0;
5829
5830 /* The physical ports are always vpi 0 - translate is unnecessary. */
5831 if (vpi > 0) {
5832 /*
5833 * Translate the physical vpi to the logical vpi. The
5834 * vport stores the logical vpi.
5835 */
5836 for (i = 0; i < phba->max_vpi; i++) {
5837 if (vpi == phba->vpi_ids[i])
5838 break;
5839 }
5840
5841 if (i >= phba->max_vpi) {
5842 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
5843 "2936 Could not find Vport mapped "
5844 "to vpi %d\n", vpi);
5845 return NULL;
5846 }
5847 }
5848
5849 spin_lock_irqsave(&phba->hbalock, flags);
5850 list_for_each_entry(vport, &phba->port_list, listentry) {
5851 if (vport->vpi == i) {
5852 spin_unlock_irqrestore(&phba->hbalock, flags);
5853 return vport;
5854 }
5855 }
5856 spin_unlock_irqrestore(&phba->hbalock, flags);
5857 return NULL;
5858}
5859
dea31012005-04-17 16:05:31 -05005860void
James Smart2e0fef82007-06-17 19:56:36 -05005861lpfc_nlp_init(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
5862 uint32_t did)
dea31012005-04-17 16:05:31 -05005863{
5864 memset(ndlp, 0, sizeof (struct lpfc_nodelist));
James Smart109f6ed2008-12-04 22:39:08 -05005865
5866 lpfc_initialize_node(vport, ndlp, did);
James Smart685f0bf2007-04-25 09:53:08 -04005867 INIT_LIST_HEAD(&ndlp->nlp_listp);
James Smartcff261f2013-12-17 20:29:47 -05005868 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
James Smart725dd392013-10-10 12:23:30 -04005869 ndlp->nlp_rpi = lpfc_sli4_alloc_rpi(vport->phba);
James Smartbe6bb942015-04-07 15:07:22 -04005870 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE,
5871 "0007 rpi:%x DID:%x flg:%x refcnt:%d "
5872 "map:%x %p\n", ndlp->nlp_rpi, ndlp->nlp_DID,
5873 ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005874 kref_read(&ndlp->kref),
James Smartbe6bb942015-04-07 15:07:22 -04005875 ndlp->nlp_usg_map, ndlp);
5876
James Smartcff261f2013-12-17 20:29:47 -05005877 ndlp->active_rrqs_xri_bitmap =
5878 mempool_alloc(vport->phba->active_rrq_pool,
5879 GFP_KERNEL);
James Smart59c5f612014-05-21 08:05:10 -04005880 if (ndlp->active_rrqs_xri_bitmap)
5881 memset(ndlp->active_rrqs_xri_bitmap, 0,
5882 ndlp->phba->cfg_rrq_xri_bitmap_sz);
James Smartcff261f2013-12-17 20:29:47 -05005883 }
5884
James Smart725dd392013-10-10 12:23:30 -04005885
James Smart858c9f62007-06-17 19:56:39 -05005886
5887 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_NODE,
5888 "node init: did:x%x",
5889 ndlp->nlp_DID, 0, 0);
5890
dea31012005-04-17 16:05:31 -05005891 return;
5892}
James Smart329f9bc2007-04-25 09:53:01 -04005893
James Smart98c9ea52007-10-27 13:37:33 -04005894/* This routine releases all resources associated with a specifc NPort's ndlp
5895 * and mempool_free's the nodelist.
5896 */
James Smart311464e2007-08-02 11:10:37 -04005897static void
James Smart329f9bc2007-04-25 09:53:01 -04005898lpfc_nlp_release(struct kref *kref)
5899{
James Smarte47c9092008-02-08 18:49:26 -05005900 struct lpfc_hba *phba;
5901 unsigned long flags;
James Smart329f9bc2007-04-25 09:53:01 -04005902 struct lpfc_nodelist *ndlp = container_of(kref, struct lpfc_nodelist,
5903 kref);
James Smart858c9f62007-06-17 19:56:39 -05005904
5905 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5906 "node release: did:x%x flg:x%x type:x%x",
5907 ndlp->nlp_DID, ndlp->nlp_flag, ndlp->nlp_type);
5908
James Smarte47c9092008-02-08 18:49:26 -05005909 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NODE,
James Smart939723a2012-05-09 21:19:03 -04005910 "0279 lpfc_nlp_release: ndlp:x%p did %x "
James Smartbe6bb942015-04-07 15:07:22 -04005911 "usgmap:x%x refcnt:%d rpi:%x\n",
James Smart939723a2012-05-09 21:19:03 -04005912 (void *)ndlp, ndlp->nlp_DID, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005913 kref_read(&ndlp->kref), ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005914
5915 /* remove ndlp from action. */
James Smart2e0fef82007-06-17 19:56:36 -05005916 lpfc_nlp_remove(ndlp->vport, ndlp);
James Smarte47c9092008-02-08 18:49:26 -05005917
5918 /* clear the ndlp active flag for all release cases */
James Smarta257bf92009-04-06 18:48:10 -04005919 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005920 spin_lock_irqsave(&phba->ndlp_lock, flags);
5921 NLP_CLR_NODE_ACT(ndlp);
5922 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart40426292010-12-15 17:58:10 -05005923 if (phba->sli_rev == LPFC_SLI_REV4)
5924 lpfc_sli4_free_rpi(phba, ndlp->nlp_rpi);
James Smarte47c9092008-02-08 18:49:26 -05005925
5926 /* free ndlp memory for final ndlp release */
James Smartea2151b2008-09-07 11:52:10 -04005927 if (NLP_CHK_FREE_REQ(ndlp)) {
5928 kfree(ndlp->lat_data);
James Smartcff261f2013-12-17 20:29:47 -05005929 if (phba->sli_rev == LPFC_SLI_REV4)
5930 mempool_free(ndlp->active_rrqs_xri_bitmap,
5931 ndlp->phba->active_rrq_pool);
James Smarta257bf92009-04-06 18:48:10 -04005932 mempool_free(ndlp, ndlp->phba->nlp_mem_pool);
James Smartea2151b2008-09-07 11:52:10 -04005933 }
James Smart329f9bc2007-04-25 09:53:01 -04005934}
5935
James Smart98c9ea52007-10-27 13:37:33 -04005936/* This routine bumps the reference count for a ndlp structure to ensure
5937 * that one discovery thread won't free a ndlp while another discovery thread
5938 * is using it.
5939 */
James Smart329f9bc2007-04-25 09:53:01 -04005940struct lpfc_nodelist *
5941lpfc_nlp_get(struct lpfc_nodelist *ndlp)
5942{
James Smarte47c9092008-02-08 18:49:26 -05005943 struct lpfc_hba *phba;
5944 unsigned long flags;
5945
James Smart98c9ea52007-10-27 13:37:33 -04005946 if (ndlp) {
5947 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5948 "node get: did:x%x flg:x%x refcnt:x%x",
5949 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005950 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005951 /* The check of ndlp usage to prevent incrementing the
5952 * ndlp reference count that is in the process of being
5953 * released.
5954 */
James Smarta257bf92009-04-06 18:48:10 -04005955 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005956 spin_lock_irqsave(&phba->ndlp_lock, flags);
5957 if (!NLP_CHK_NODE_ACT(ndlp) || NLP_CHK_FREE_ACK(ndlp)) {
5958 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5959 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
5960 "0276 lpfc_nlp_get: ndlp:x%p "
5961 "usgmap:x%x refcnt:%d\n",
5962 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005963 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05005964 return NULL;
5965 } else
5966 kref_get(&ndlp->kref);
5967 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
James Smart98c9ea52007-10-27 13:37:33 -04005968 }
James Smart329f9bc2007-04-25 09:53:01 -04005969 return ndlp;
5970}
5971
James Smart98c9ea52007-10-27 13:37:33 -04005972/* This routine decrements the reference count for a ndlp structure. If the
James Smarte47c9092008-02-08 18:49:26 -05005973 * count goes to 0, this indicates the the associated nodelist should be
5974 * freed. Returning 1 indicates the ndlp resource has been released; on the
5975 * other hand, returning 0 indicates the ndlp resource has not been released
5976 * yet.
James Smart98c9ea52007-10-27 13:37:33 -04005977 */
James Smart329f9bc2007-04-25 09:53:01 -04005978int
5979lpfc_nlp_put(struct lpfc_nodelist *ndlp)
5980{
James Smarte47c9092008-02-08 18:49:26 -05005981 struct lpfc_hba *phba;
5982 unsigned long flags;
5983
5984 if (!ndlp)
5985 return 1;
5986
5987 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
5988 "node put: did:x%x flg:x%x refcnt:x%x",
5989 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01005990 kref_read(&ndlp->kref));
James Smarta257bf92009-04-06 18:48:10 -04005991 phba = ndlp->phba;
James Smarte47c9092008-02-08 18:49:26 -05005992 spin_lock_irqsave(&phba->ndlp_lock, flags);
5993 /* Check the ndlp memory free acknowledge flag to avoid the
5994 * possible race condition that kref_put got invoked again
5995 * after previous one has done ndlp memory free.
5996 */
5997 if (NLP_CHK_FREE_ACK(ndlp)) {
5998 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
5999 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6000 "0274 lpfc_nlp_put: ndlp:x%p "
6001 "usgmap:x%x refcnt:%d\n",
6002 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006003 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006004 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006005 }
James Smarte47c9092008-02-08 18:49:26 -05006006 /* Check the ndlp inactivate log flag to avoid the possible
6007 * race condition that kref_put got invoked again after ndlp
6008 * is already in inactivating state.
6009 */
6010 if (NLP_CHK_IACT_REQ(ndlp)) {
6011 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6012 lpfc_printf_vlog(ndlp->vport, KERN_WARNING, LOG_NODE,
6013 "0275 lpfc_nlp_put: ndlp:x%p "
6014 "usgmap:x%x refcnt:%d\n",
6015 (void *)ndlp, ndlp->nlp_usg_map,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006016 kref_read(&ndlp->kref));
James Smarte47c9092008-02-08 18:49:26 -05006017 return 1;
6018 }
6019 /* For last put, mark the ndlp usage flags to make sure no
6020 * other kref_get and kref_put on the same ndlp shall get
6021 * in between the process when the final kref_put has been
6022 * invoked on this ndlp.
6023 */
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006024 if (kref_read(&ndlp->kref) == 1) {
James Smarte47c9092008-02-08 18:49:26 -05006025 /* Indicate ndlp is put to inactive state. */
6026 NLP_SET_IACT_REQ(ndlp);
6027 /* Acknowledge ndlp memory free has been seen. */
6028 if (NLP_CHK_FREE_REQ(ndlp))
6029 NLP_SET_FREE_ACK(ndlp);
6030 }
6031 spin_unlock_irqrestore(&phba->ndlp_lock, flags);
6032 /* Note, the kref_put returns 1 when decrementing a reference
6033 * count that was 1, it invokes the release callback function,
6034 * but it still left the reference count as 1 (not actually
6035 * performs the last decrementation). Otherwise, it actually
6036 * decrements the reference count and returns 0.
6037 */
6038 return kref_put(&ndlp->kref, lpfc_nlp_release);
James Smart329f9bc2007-04-25 09:53:01 -04006039}
James Smart98c9ea52007-10-27 13:37:33 -04006040
6041/* This routine free's the specified nodelist if it is not in use
James Smarte47c9092008-02-08 18:49:26 -05006042 * by any other discovery thread. This routine returns 1 if the
6043 * ndlp has been freed. A return value of 0 indicates the ndlp is
6044 * not yet been released.
James Smart98c9ea52007-10-27 13:37:33 -04006045 */
6046int
6047lpfc_nlp_not_used(struct lpfc_nodelist *ndlp)
6048{
6049 lpfc_debugfs_disc_trc(ndlp->vport, LPFC_DISC_TRC_NODE,
6050 "node not used: did:x%x flg:x%x refcnt:x%x",
6051 ndlp->nlp_DID, ndlp->nlp_flag,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +01006052 kref_read(&ndlp->kref));
6053 if (kref_read(&ndlp->kref) == 1)
James Smarte47c9092008-02-08 18:49:26 -05006054 if (lpfc_nlp_put(ndlp))
6055 return 1;
James Smart98c9ea52007-10-27 13:37:33 -04006056 return 0;
6057}
James Smart6fb120a2009-05-22 14:52:59 -04006058
6059/**
6060 * lpfc_fcf_inuse - Check if FCF can be unregistered.
6061 * @phba: Pointer to hba context object.
6062 *
6063 * This function iterate through all FC nodes associated
6064 * will all vports to check if there is any node with
6065 * fc_rports associated with it. If there is an fc_rport
6066 * associated with the node, then the node is either in
6067 * discovered state or its devloss_timer is pending.
6068 */
6069static int
6070lpfc_fcf_inuse(struct lpfc_hba *phba)
6071{
6072 struct lpfc_vport **vports;
6073 int i, ret = 0;
6074 struct lpfc_nodelist *ndlp;
6075 struct Scsi_Host *shost;
6076
6077 vports = lpfc_create_vport_work_array(phba);
6078
James Smart63e801c2010-11-20 23:14:19 -05006079 /* If driver cannot allocate memory, indicate fcf is in use */
6080 if (!vports)
6081 return 1;
6082
James Smart6fb120a2009-05-22 14:52:59 -04006083 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
6084 shost = lpfc_shost_from_vport(vports[i]);
6085 spin_lock_irq(shost->host_lock);
James Smart05580562011-05-24 11:40:48 -04006086 /*
6087 * IF the CVL_RCVD bit is not set then we have sent the
6088 * flogi.
6089 * If dev_loss fires while we are waiting we do not want to
6090 * unreg the fcf.
6091 */
6092 if (!(vports[i]->fc_flag & FC_VPORT_CVL_RCVD)) {
6093 spin_unlock_irq(shost->host_lock);
6094 ret = 1;
6095 goto out;
6096 }
James Smart6fb120a2009-05-22 14:52:59 -04006097 list_for_each_entry(ndlp, &vports[i]->fc_nodes, nlp_listp) {
6098 if (NLP_CHK_NODE_ACT(ndlp) && ndlp->rport &&
6099 (ndlp->rport->roles & FC_RPORT_ROLE_FCP_TARGET)) {
6100 ret = 1;
6101 spin_unlock_irq(shost->host_lock);
6102 goto out;
James Smart80c17842012-03-01 22:35:45 -05006103 } else if (ndlp->nlp_flag & NLP_RPI_REGISTERED) {
6104 ret = 1;
James Smart891478a2009-11-18 15:40:23 -05006105 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smart80c17842012-03-01 22:35:45 -05006106 "2624 RPI %x DID %x flag %x "
6107 "still logged in\n",
6108 ndlp->nlp_rpi, ndlp->nlp_DID,
6109 ndlp->nlp_flag);
James Smart6fb120a2009-05-22 14:52:59 -04006110 }
6111 }
6112 spin_unlock_irq(shost->host_lock);
6113 }
6114out:
6115 lpfc_destroy_vport_work_array(phba, vports);
6116 return ret;
6117}
6118
6119/**
6120 * lpfc_unregister_vfi_cmpl - Completion handler for unreg vfi.
6121 * @phba: Pointer to hba context object.
6122 * @mboxq: Pointer to mailbox object.
6123 *
6124 * This function frees memory associated with the mailbox command.
6125 */
James Smart1b511972011-12-13 13:23:09 -05006126void
James Smart6fb120a2009-05-22 14:52:59 -04006127lpfc_unregister_vfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6128{
6129 struct lpfc_vport *vport = mboxq->vport;
James Smart38b92ef2010-08-04 16:11:39 -04006130 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
James Smart6fb120a2009-05-22 14:52:59 -04006131
6132 if (mboxq->u.mb.mbxStatus) {
6133 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6134 "2555 UNREG_VFI mbxStatus error x%x "
6135 "HBA state x%x\n",
6136 mboxq->u.mb.mbxStatus, vport->port_state);
6137 }
James Smart38b92ef2010-08-04 16:11:39 -04006138 spin_lock_irq(shost->host_lock);
6139 phba->pport->fc_flag &= ~FC_VFI_REGISTERED;
6140 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006141 mempool_free(mboxq, phba->mbox_mem_pool);
6142 return;
6143}
6144
6145/**
6146 * lpfc_unregister_fcfi_cmpl - Completion handler for unreg fcfi.
6147 * @phba: Pointer to hba context object.
6148 * @mboxq: Pointer to mailbox object.
6149 *
6150 * This function frees memory associated with the mailbox command.
6151 */
6152static void
6153lpfc_unregister_fcfi_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6154{
6155 struct lpfc_vport *vport = mboxq->vport;
6156
6157 if (mboxq->u.mb.mbxStatus) {
6158 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
6159 "2550 UNREG_FCFI mbxStatus error x%x "
6160 "HBA state x%x\n",
6161 mboxq->u.mb.mbxStatus, vport->port_state);
6162 }
6163 mempool_free(mboxq, phba->mbox_mem_pool);
6164 return;
6165}
6166
6167/**
James Smartecfd03c2010-02-12 14:41:27 -05006168 * lpfc_unregister_fcf_prep - Unregister fcf record preparation
James Smart6fb120a2009-05-22 14:52:59 -04006169 * @phba: Pointer to hba context object.
6170 *
James Smartecfd03c2010-02-12 14:41:27 -05006171 * This function prepare the HBA for unregistering the currently registered
6172 * FCF from the HBA. It performs unregistering, in order, RPIs, VPIs, and
6173 * VFIs.
James Smart6fb120a2009-05-22 14:52:59 -04006174 */
James Smartecfd03c2010-02-12 14:41:27 -05006175int
6176lpfc_unregister_fcf_prep(struct lpfc_hba *phba)
James Smart6fb120a2009-05-22 14:52:59 -04006177{
James Smart6fb120a2009-05-22 14:52:59 -04006178 struct lpfc_vport **vports;
James Smart695a8142010-01-26 23:08:03 -05006179 struct lpfc_nodelist *ndlp;
James Smart72100cc2010-02-12 14:43:01 -05006180 struct Scsi_Host *shost;
James Smarte74c03c2013-04-17 20:15:19 -04006181 int i = 0, rc;
James Smart6fb120a2009-05-22 14:52:59 -04006182
James Smartecfd03c2010-02-12 14:41:27 -05006183 /* Unregister RPIs */
James Smart6fb120a2009-05-22 14:52:59 -04006184 if (lpfc_fcf_inuse(phba))
James Smartecfd03c2010-02-12 14:41:27 -05006185 lpfc_unreg_hba_rpis(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006186
James Smart4d9ab992009-10-02 15:16:39 -04006187 /* At this point, all discovery is aborted */
6188 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
James Smart6fb120a2009-05-22 14:52:59 -04006189
6190 /* Unregister VPIs */
6191 vports = lpfc_create_vport_work_array(phba);
James Smartecfd03c2010-02-12 14:41:27 -05006192 if (vports && (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))
James Smart6fb120a2009-05-22 14:52:59 -04006193 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
James Smart695a8142010-01-26 23:08:03 -05006194 /* Stop FLOGI/FDISC retries */
6195 ndlp = lpfc_findnode_did(vports[i], Fabric_DID);
6196 if (ndlp)
6197 lpfc_cancel_retry_delay_tmo(vports[i], ndlp);
James Smart78730cf2010-04-06 15:06:30 -04006198 lpfc_cleanup_pending_mbox(vports[i]);
James Smart5af5eee2010-10-22 11:06:38 -04006199 if (phba->sli_rev == LPFC_SLI_REV4)
6200 lpfc_sli4_unreg_all_rpis(vports[i]);
James Smart6fb120a2009-05-22 14:52:59 -04006201 lpfc_mbx_unreg_vpi(vports[i]);
James Smart72100cc2010-02-12 14:43:01 -05006202 shost = lpfc_shost_from_vport(vports[i]);
6203 spin_lock_irq(shost->host_lock);
James Smart891478a2009-11-18 15:40:23 -05006204 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
James Smartc8685952009-11-18 15:39:16 -05006205 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
James Smart72100cc2010-02-12 14:43:01 -05006206 spin_unlock_irq(shost->host_lock);
James Smart6fb120a2009-05-22 14:52:59 -04006207 }
6208 lpfc_destroy_vport_work_array(phba, vports);
James Smarte74c03c2013-04-17 20:15:19 -04006209 if (i == 0 && (!(phba->sli3_options & LPFC_SLI3_NPIV_ENABLED))) {
6210 ndlp = lpfc_findnode_did(phba->pport, Fabric_DID);
6211 if (ndlp)
6212 lpfc_cancel_retry_delay_tmo(phba->pport, ndlp);
6213 lpfc_cleanup_pending_mbox(phba->pport);
6214 if (phba->sli_rev == LPFC_SLI_REV4)
6215 lpfc_sli4_unreg_all_rpis(phba->pport);
6216 lpfc_mbx_unreg_vpi(phba->pport);
6217 shost = lpfc_shost_from_vport(phba->pport);
6218 spin_lock_irq(shost->host_lock);
6219 phba->pport->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
6220 phba->pport->vpi_state &= ~LPFC_VPI_REGISTERED;
6221 spin_unlock_irq(shost->host_lock);
6222 }
James Smart6fb120a2009-05-22 14:52:59 -04006223
James Smart695a8142010-01-26 23:08:03 -05006224 /* Cleanup any outstanding ELS commands */
6225 lpfc_els_flush_all_cmd(phba);
6226
James Smart1b511972011-12-13 13:23:09 -05006227 /* Unregister the physical port VFI */
6228 rc = lpfc_issue_unreg_vfi(phba->pport);
6229 return rc;
James Smartecfd03c2010-02-12 14:41:27 -05006230}
6231
6232/**
6233 * lpfc_sli4_unregister_fcf - Unregister currently registered FCF record
6234 * @phba: Pointer to hba context object.
6235 *
6236 * This function issues synchronous unregister FCF mailbox command to HBA to
6237 * unregister the currently registered FCF record. The driver does not reset
6238 * the driver FCF usage state flags.
6239 *
6240 * Return 0 if successfully issued, none-zero otherwise.
6241 */
6242int
6243lpfc_sli4_unregister_fcf(struct lpfc_hba *phba)
6244{
6245 LPFC_MBOXQ_t *mbox;
6246 int rc;
6247
James Smart6fb120a2009-05-22 14:52:59 -04006248 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6249 if (!mbox) {
6250 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006251 "2551 UNREG_FCFI mbox allocation failed"
6252 "HBA state x%x\n", phba->pport->port_state);
6253 return -ENOMEM;
James Smart6fb120a2009-05-22 14:52:59 -04006254 }
James Smart6fb120a2009-05-22 14:52:59 -04006255 lpfc_unreg_fcfi(mbox, phba->fcf.fcfi);
6256 mbox->vport = phba->pport;
6257 mbox->mbox_cmpl = lpfc_unregister_fcfi_cmpl;
6258 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
6259
6260 if (rc == MBX_NOT_FINISHED) {
James Smartecfd03c2010-02-12 14:41:27 -05006261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6262 "2552 Unregister FCFI command failed rc x%x "
6263 "HBA state x%x\n",
6264 rc, phba->pport->port_state);
6265 return -EINVAL;
6266 }
6267 return 0;
6268}
6269
6270/**
6271 * lpfc_unregister_fcf_rescan - Unregister currently registered fcf and rescan
6272 * @phba: Pointer to hba context object.
6273 *
6274 * This function unregisters the currently reigstered FCF. This function
6275 * also tries to find another FCF for discovery by rescan the HBA FCF table.
6276 */
6277void
6278lpfc_unregister_fcf_rescan(struct lpfc_hba *phba)
6279{
6280 int rc;
6281
6282 /* Preparation for unregistering fcf */
6283 rc = lpfc_unregister_fcf_prep(phba);
6284 if (rc) {
6285 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6286 "2748 Failed to prepare for unregistering "
6287 "HBA's FCF record: rc=%d\n", rc);
James Smart6fb120a2009-05-22 14:52:59 -04006288 return;
6289 }
6290
James Smartecfd03c2010-02-12 14:41:27 -05006291 /* Now, unregister FCF record and reset HBA FCF state */
6292 rc = lpfc_sli4_unregister_fcf(phba);
6293 if (rc)
6294 return;
6295 /* Reset HBA FCF states after successful unregister FCF */
6296 phba->fcf.fcf_flag = 0;
James Smartfc2b9892010-02-26 14:15:29 -05006297 phba->fcf.current_rec.flag = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006298
6299 /*
6300 * If driver is not unloading, check if there is any other
6301 * FCF record that can be used for discovery.
6302 */
6303 if ((phba->pport->load_flag & FC_UNLOADING) ||
James Smartecfd03c2010-02-12 14:41:27 -05006304 (phba->link_state < LPFC_LINK_UP))
James Smart6fb120a2009-05-22 14:52:59 -04006305 return;
6306
James Smart0c9ab6f2010-02-26 14:15:57 -05006307 /* This is considered as the initial FCF discovery scan */
6308 spin_lock_irq(&phba->hbalock);
6309 phba->fcf.fcf_flag |= FCF_INIT_DISC;
6310 spin_unlock_irq(&phba->hbalock);
James Smart38b92ef2010-08-04 16:11:39 -04006311
6312 /* Reset FCF roundrobin bmask for new discovery */
James Smart7d791df2011-07-22 18:37:52 -04006313 lpfc_sli4_clear_fcf_rr_bmask(phba);
James Smart38b92ef2010-08-04 16:11:39 -04006314
James Smart0c9ab6f2010-02-26 14:15:57 -05006315 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
James Smart6fb120a2009-05-22 14:52:59 -04006316
James Smart0c9ab6f2010-02-26 14:15:57 -05006317 if (rc) {
6318 spin_lock_irq(&phba->hbalock);
6319 phba->fcf.fcf_flag &= ~FCF_INIT_DISC;
6320 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -04006321 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY|LOG_MBOX,
James Smartecfd03c2010-02-12 14:41:27 -05006322 "2553 lpfc_unregister_unused_fcf failed "
6323 "to read FCF record HBA state x%x\n",
6324 phba->pport->port_state);
James Smart0c9ab6f2010-02-26 14:15:57 -05006325 }
James Smartecfd03c2010-02-12 14:41:27 -05006326}
6327
6328/**
6329 * lpfc_unregister_fcf - Unregister the currently registered fcf record
6330 * @phba: Pointer to hba context object.
6331 *
6332 * This function just unregisters the currently reigstered FCF. It does not
6333 * try to find another FCF for discovery.
6334 */
6335void
6336lpfc_unregister_fcf(struct lpfc_hba *phba)
6337{
6338 int rc;
6339
6340 /* Preparation for unregistering fcf */
6341 rc = lpfc_unregister_fcf_prep(phba);
6342 if (rc) {
6343 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY,
6344 "2749 Failed to prepare for unregistering "
6345 "HBA's FCF record: rc=%d\n", rc);
6346 return;
6347 }
6348
6349 /* Now, unregister FCF record and reset HBA FCF state */
6350 rc = lpfc_sli4_unregister_fcf(phba);
6351 if (rc)
6352 return;
6353 /* Set proper HBA FCF states after successful unregister FCF */
6354 spin_lock_irq(&phba->hbalock);
6355 phba->fcf.fcf_flag &= ~FCF_REGISTERED;
6356 spin_unlock_irq(&phba->hbalock);
6357}
6358
6359/**
6360 * lpfc_unregister_unused_fcf - Unregister FCF if all devices are disconnected.
6361 * @phba: Pointer to hba context object.
6362 *
6363 * This function check if there are any connected remote port for the FCF and
6364 * if all the devices are disconnected, this function unregister FCFI.
6365 * This function also tries to use another FCF for discovery.
6366 */
6367void
6368lpfc_unregister_unused_fcf(struct lpfc_hba *phba)
6369{
6370 /*
James Smart3804dc82010-07-14 15:31:37 -04006371 * If HBA is not running in FIP mode, if HBA does not support
6372 * FCoE, if FCF discovery is ongoing, or if FCF has not been
6373 * registered, do nothing.
James Smartecfd03c2010-02-12 14:41:27 -05006374 */
6375 spin_lock_irq(&phba->hbalock);
James Smart76a95d72010-11-20 23:11:48 -05006376 if (!(phba->hba_flag & HBA_FCOE_MODE) ||
James Smartecfd03c2010-02-12 14:41:27 -05006377 !(phba->fcf.fcf_flag & FCF_REGISTERED) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006378 !(phba->hba_flag & HBA_FIP_SUPPORT) ||
James Smart3804dc82010-07-14 15:31:37 -04006379 (phba->fcf.fcf_flag & FCF_DISCOVERY) ||
James Smartdbb6b3a2010-06-08 18:31:37 -04006380 (phba->pport->port_state == LPFC_FLOGI)) {
James Smartecfd03c2010-02-12 14:41:27 -05006381 spin_unlock_irq(&phba->hbalock);
6382 return;
6383 }
6384 spin_unlock_irq(&phba->hbalock);
6385
6386 if (lpfc_fcf_inuse(phba))
6387 return;
6388
6389 lpfc_unregister_fcf_rescan(phba);
James Smart6fb120a2009-05-22 14:52:59 -04006390}
6391
6392/**
6393 * lpfc_read_fcf_conn_tbl - Create driver FCF connection table.
6394 * @phba: Pointer to hba context object.
6395 * @buff: Buffer containing the FCF connection table as in the config
6396 * region.
6397 * This function create driver data structure for the FCF connection
6398 * record table read from config region 23.
6399 */
6400static void
6401lpfc_read_fcf_conn_tbl(struct lpfc_hba *phba,
6402 uint8_t *buff)
6403{
6404 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
6405 struct lpfc_fcf_conn_hdr *conn_hdr;
6406 struct lpfc_fcf_conn_rec *conn_rec;
6407 uint32_t record_count;
6408 int i;
6409
6410 /* Free the current connect table */
6411 list_for_each_entry_safe(conn_entry, next_conn_entry,
James Smart4d9ab992009-10-02 15:16:39 -04006412 &phba->fcf_conn_rec_list, list) {
6413 list_del_init(&conn_entry->list);
James Smart6fb120a2009-05-22 14:52:59 -04006414 kfree(conn_entry);
James Smart4d9ab992009-10-02 15:16:39 -04006415 }
James Smart6fb120a2009-05-22 14:52:59 -04006416
6417 conn_hdr = (struct lpfc_fcf_conn_hdr *) buff;
6418 record_count = conn_hdr->length * sizeof(uint32_t)/
6419 sizeof(struct lpfc_fcf_conn_rec);
6420
6421 conn_rec = (struct lpfc_fcf_conn_rec *)
6422 (buff + sizeof(struct lpfc_fcf_conn_hdr));
6423
6424 for (i = 0; i < record_count; i++) {
6425 if (!(conn_rec[i].flags & FCFCNCT_VALID))
6426 continue;
6427 conn_entry = kzalloc(sizeof(struct lpfc_fcf_conn_entry),
6428 GFP_KERNEL);
6429 if (!conn_entry) {
6430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6431 "2566 Failed to allocate connection"
6432 " table entry\n");
6433 return;
6434 }
6435
6436 memcpy(&conn_entry->conn_rec, &conn_rec[i],
6437 sizeof(struct lpfc_fcf_conn_rec));
James Smart6fb120a2009-05-22 14:52:59 -04006438 list_add_tail(&conn_entry->list,
6439 &phba->fcf_conn_rec_list);
6440 }
James Smartdf0d0852013-05-31 17:05:08 -04006441
6442 if (!list_empty(&phba->fcf_conn_rec_list)) {
6443 i = 0;
6444 list_for_each_entry(conn_entry, &phba->fcf_conn_rec_list,
6445 list) {
6446 conn_rec = &conn_entry->conn_rec;
6447 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6448 "3345 FCF connection list rec[%02d]: "
6449 "flags:x%04x, vtag:x%04x, "
6450 "fabric_name:x%02x:%02x:%02x:%02x:"
6451 "%02x:%02x:%02x:%02x, "
6452 "switch_name:x%02x:%02x:%02x:%02x:"
6453 "%02x:%02x:%02x:%02x\n", i++,
6454 conn_rec->flags, conn_rec->vlan_tag,
6455 conn_rec->fabric_name[0],
6456 conn_rec->fabric_name[1],
6457 conn_rec->fabric_name[2],
6458 conn_rec->fabric_name[3],
6459 conn_rec->fabric_name[4],
6460 conn_rec->fabric_name[5],
6461 conn_rec->fabric_name[6],
6462 conn_rec->fabric_name[7],
6463 conn_rec->switch_name[0],
6464 conn_rec->switch_name[1],
6465 conn_rec->switch_name[2],
6466 conn_rec->switch_name[3],
6467 conn_rec->switch_name[4],
6468 conn_rec->switch_name[5],
6469 conn_rec->switch_name[6],
6470 conn_rec->switch_name[7]);
6471 }
6472 }
James Smart6fb120a2009-05-22 14:52:59 -04006473}
6474
6475/**
6476 * lpfc_read_fcoe_param - Read FCoe parameters from conf region..
6477 * @phba: Pointer to hba context object.
6478 * @buff: Buffer containing the FCoE parameter data structure.
6479 *
6480 * This function update driver data structure with config
6481 * parameters read from config region 23.
6482 */
6483static void
6484lpfc_read_fcoe_param(struct lpfc_hba *phba,
6485 uint8_t *buff)
6486{
6487 struct lpfc_fip_param_hdr *fcoe_param_hdr;
6488 struct lpfc_fcoe_params *fcoe_param;
6489
6490 fcoe_param_hdr = (struct lpfc_fip_param_hdr *)
6491 buff;
6492 fcoe_param = (struct lpfc_fcoe_params *)
James Smart32b97932009-07-19 10:01:21 -04006493 (buff + sizeof(struct lpfc_fip_param_hdr));
James Smart6fb120a2009-05-22 14:52:59 -04006494
6495 if ((fcoe_param_hdr->parm_version != FIPP_VERSION) ||
6496 (fcoe_param_hdr->length != FCOE_PARAM_LENGTH))
6497 return;
6498
James Smart6fb120a2009-05-22 14:52:59 -04006499 if (fcoe_param_hdr->parm_flags & FIPP_VLAN_VALID) {
6500 phba->valid_vlan = 1;
6501 phba->vlan_id = le16_to_cpu(fcoe_param->vlan_tag) &
6502 0xFFF;
6503 }
6504
6505 phba->fc_map[0] = fcoe_param->fc_map[0];
6506 phba->fc_map[1] = fcoe_param->fc_map[1];
6507 phba->fc_map[2] = fcoe_param->fc_map[2];
6508 return;
6509}
6510
6511/**
6512 * lpfc_get_rec_conf23 - Get a record type in config region data.
6513 * @buff: Buffer containing config region 23 data.
6514 * @size: Size of the data buffer.
6515 * @rec_type: Record type to be searched.
6516 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006517 * This function searches config region data to find the beginning
James Smart6fb120a2009-05-22 14:52:59 -04006518 * of the record specified by record_type. If record found, this
6519 * function return pointer to the record else return NULL.
6520 */
6521static uint8_t *
6522lpfc_get_rec_conf23(uint8_t *buff, uint32_t size, uint8_t rec_type)
6523{
6524 uint32_t offset = 0, rec_length;
6525
6526 if ((buff[0] == LPFC_REGION23_LAST_REC) ||
6527 (size < sizeof(uint32_t)))
6528 return NULL;
6529
6530 rec_length = buff[offset + 1];
6531
6532 /*
6533 * One TLV record has one word header and number of data words
6534 * specified in the rec_length field of the record header.
6535 */
6536 while ((offset + rec_length * sizeof(uint32_t) + sizeof(uint32_t))
6537 <= size) {
6538 if (buff[offset] == rec_type)
6539 return &buff[offset];
6540
6541 if (buff[offset] == LPFC_REGION23_LAST_REC)
6542 return NULL;
6543
6544 offset += rec_length * sizeof(uint32_t) + sizeof(uint32_t);
6545 rec_length = buff[offset + 1];
6546 }
6547 return NULL;
6548}
6549
6550/**
6551 * lpfc_parse_fcoe_conf - Parse FCoE config data read from config region 23.
6552 * @phba: Pointer to lpfc_hba data structure.
6553 * @buff: Buffer containing config region 23 data.
6554 * @size: Size of the data buffer.
6555 *
Stefan Weileef35c22010-08-06 21:11:15 +02006556 * This function parses the FCoE config parameters in config region 23 and
James Smart6fb120a2009-05-22 14:52:59 -04006557 * populate driver data structure with the parameters.
6558 */
6559void
6560lpfc_parse_fcoe_conf(struct lpfc_hba *phba,
6561 uint8_t *buff,
6562 uint32_t size)
6563{
James Smarteb016562014-09-03 12:58:06 -04006564 uint32_t offset = 0;
James Smart6fb120a2009-05-22 14:52:59 -04006565 uint8_t *rec_ptr;
6566
6567 /*
6568 * If data size is less than 2 words signature and version cannot be
6569 * verified.
6570 */
6571 if (size < 2*sizeof(uint32_t))
6572 return;
6573
6574 /* Check the region signature first */
6575 if (memcmp(buff, LPFC_REGION23_SIGNATURE, 4)) {
6576 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6577 "2567 Config region 23 has bad signature\n");
6578 return;
6579 }
6580
6581 offset += 4;
6582
6583 /* Check the data structure version */
6584 if (buff[offset] != LPFC_REGION23_VERSION) {
6585 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6586 "2568 Config region 23 has bad version\n");
6587 return;
6588 }
6589 offset += 4;
6590
James Smart6fb120a2009-05-22 14:52:59 -04006591 /* Read FCoE param record */
6592 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6593 size - offset, FCOE_PARAM_TYPE);
6594 if (rec_ptr)
6595 lpfc_read_fcoe_param(phba, rec_ptr);
6596
6597 /* Read FCF connection table */
6598 rec_ptr = lpfc_get_rec_conf23(&buff[offset],
6599 size - offset, FCOE_CONN_TBL_TYPE);
6600 if (rec_ptr)
6601 lpfc_read_fcf_conn_tbl(phba, rec_ptr);
6602
6603}